162 FWPM_SUBLAYER0 *sublayer_ptr = NULL;
165 FWP_BYTE_BLOB *openvpnblob = NULL;
166 FWPM_FILTER0 Filter = { 0 };
167 FWPM_FILTER_CONDITION0 Condition[2];
168 FWPM_FILTER_CONDITION0 match_openvpn = { 0 };
169 FWPM_FILTER_CONDITION0 match_port_53 = { 0 };
170 FWPM_FILTER_CONDITION0 match_interface = { 0 };
171 FWPM_FILTER_CONDITION0 match_loopback = { 0 };
172 FWPM_FILTER_CONDITION0 match_not_loopback = { 0 };
183 *engine_handle = NULL;
185 err = FwpmEngineOpen0(NULL, RPC_C_AUTHN_WINNT, NULL, &
session, engine_handle);
186 OUT_ON_ERROR(err,
"FwpEngineOpen: open fwp session failed");
187 msg_handler(0,
"WFP Block: WFP engine opened");
190 if (FwpmSubLayerGetByKey0(*engine_handle, &OPENVPN_WFP_BLOCK_SUBLAYER, &sublayer_ptr)
193 msg_handler(0,
"WFP Block: Using existing sublayer");
194 FwpmFreeMemory0((
void **)&sublayer_ptr);
201 if (err == FWP_E_ALREADY_EXISTS || err == ERROR_SUCCESS)
203 msg_handler(0,
"WFP Block: Added a persistent sublayer with pre-defined UUID");
207 OUT_ON_ERROR(err,
"add_sublayer: failed to add persistent sublayer");
211 err = ConvertInterfaceIndexToLuid(index, &itf_luid);
212 OUT_ON_ERROR(err,
"Convert interface index to luid failed");
214 err = FwpmGetAppIdFromFileName0(exe_path, &openvpnblob);
215 OUT_ON_ERROR(err,
"Get byte blob for openvpn executable name failed");
218 match_openvpn.fieldKey = FWPM_CONDITION_ALE_APP_ID;
219 match_openvpn.matchType = FWP_MATCH_EQUAL;
220 match_openvpn.conditionValue.type = FWP_BYTE_BLOB_TYPE;
221 match_openvpn.conditionValue.byteBlob = openvpnblob;
223 match_port_53.fieldKey = FWPM_CONDITION_IP_REMOTE_PORT;
224 match_port_53.matchType = FWP_MATCH_EQUAL;
225 match_port_53.conditionValue.type = FWP_UINT16;
226 match_port_53.conditionValue.uint16 = 53;
228 match_interface.fieldKey = FWPM_CONDITION_IP_LOCAL_INTERFACE;
229 match_interface.matchType = FWP_MATCH_EQUAL;
230 match_interface.conditionValue.type = FWP_UINT64;
231 match_interface.conditionValue.uint64 = &itf_luid.Value;
233 match_loopback.fieldKey = FWPM_CONDITION_FLAGS;
234 match_loopback.matchType = FWP_MATCH_FLAGS_ALL_SET;
235 match_loopback.conditionValue.type = FWP_UINT32;
236 match_loopback.conditionValue.uint32 = FWP_CONDITION_FLAG_IS_LOOPBACK;
238 match_not_loopback.fieldKey = FWPM_CONDITION_FLAGS;
239 match_not_loopback.matchType = FWP_MATCH_FLAGS_NONE_SET;
240 match_not_loopback.conditionValue.type = FWP_UINT32;
241 match_not_loopback.conditionValue.uint32 = FWP_CONDITION_FLAG_IS_LOOPBACK;
244 Filter.subLayerKey = OPENVPN_WFP_BLOCK_SUBLAYER;
246 Filter.weight.type = FWP_UINT8;
247 Filter.weight.uint8 = 0xF;
248 Filter.filterCondition = Condition;
249 Filter.numFilterConditions = 1;
252 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
253 Filter.action.type = FWP_ACTION_PERMIT;
254 Condition[0] = match_openvpn;
257 Filter.numFilterConditions = 2;
258 Condition[1] = match_port_53;
260 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
261 OUT_ON_ERROR(err,
"Add filter to permit IPv4 traffic from OpenVPN failed");
264 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
265 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
266 OUT_ON_ERROR(err,
"Add filter to permit IPv6 traffic from OpenVPN failed");
268 msg_handler(0,
"WFP Block: Added permit filters for exe_path");
271 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
272 Filter.action.type = FWP_ACTION_BLOCK;
273 Filter.weight.type = FWP_EMPTY;
274 Filter.numFilterConditions = 1;
275 Condition[0] = match_not_loopback;
278 Filter.numFilterConditions = 2;
279 Condition[1] = match_port_53;
281 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
282 OUT_ON_ERROR(err,
"Add filter to block IPv4 traffic failed");
285 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
286 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
287 OUT_ON_ERROR(err,
"Add filter to block IPv6 traffic failed");
289 msg_handler(0,
"WFP Block: Added block filters for all interfaces");
294 Filter.weight.type = FWP_UINT8;
295 Filter.weight.uint8 = 0xE;
296 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
297 Filter.action.type = FWP_ACTION_PERMIT;
298 Filter.numFilterConditions = 1;
299 Condition[0] = match_interface;
302 Filter.numFilterConditions = 2;
303 Condition[1] = match_port_53;
305 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
306 OUT_ON_ERROR(err,
"Add filter to permit IPv4 traffic through VPN interface failed");
310 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
311 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
312 OUT_ON_ERROR(err,
"Add filter to permit IPv6 traffic through VPN interface failed");
314 msg_handler(0,
"WFP Block: Added permit filters for VPN interface");
317 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
318 Filter.action.type = FWP_ACTION_BLOCK;
319 Filter.weight.type = FWP_EMPTY;
320 Filter.numFilterConditions = 2;
321 Condition[0] = match_loopback;
322 Condition[1] = match_port_53;
323 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
324 OUT_ON_ERROR(err,
"Add filter to block IPv4 DNS traffic to loopback failed");
327 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
328 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
329 OUT_ON_ERROR(err,
"Add filter to block IPv6 DNS traffic to loopback failed");
331 msg_handler(0,
"WFP Block: Added block filters for DNS traffic to loopback");
336 FwpmFreeMemory0((
void **)&openvpnblob);
339 if (err && *engine_handle)
341 FwpmEngineClose0(*engine_handle);
342 *engine_handle = NULL;