157 FWPM_SUBLAYER0 *sublayer_ptr = NULL;
160 FWP_BYTE_BLOB *openvpnblob = NULL;
161 FWPM_FILTER0 Filter = { 0 };
162 FWPM_FILTER_CONDITION0 Condition[2];
163 FWPM_FILTER_CONDITION0 match_openvpn = { 0 };
164 FWPM_FILTER_CONDITION0 match_port_53 = { 0 };
165 FWPM_FILTER_CONDITION0 match_interface = { 0 };
166 FWPM_FILTER_CONDITION0 match_loopback = { 0 };
167 FWPM_FILTER_CONDITION0 match_not_loopback = { 0 };
178 *engine_handle = NULL;
180 err = FwpmEngineOpen0(NULL, RPC_C_AUTHN_WINNT, NULL, &
session, engine_handle);
181 OUT_ON_ERROR(err,
"FwpEngineOpen: open fwp session failed");
182 msg_handler(0,
"WFP Block: WFP engine opened");
185 if (FwpmSubLayerGetByKey0(*engine_handle, &OPENVPN_WFP_BLOCK_SUBLAYER, &sublayer_ptr)
188 msg_handler(0,
"WFP Block: Using existing sublayer");
189 FwpmFreeMemory0((
void **)&sublayer_ptr);
196 if (err == FWP_E_ALREADY_EXISTS || err == ERROR_SUCCESS)
198 msg_handler(0,
"WFP Block: Added a persistent sublayer with pre-defined UUID");
202 OUT_ON_ERROR(err,
"add_sublayer: failed to add persistent sublayer");
206 err = ConvertInterfaceIndexToLuid(index, &itf_luid);
207 OUT_ON_ERROR(err,
"Convert interface index to luid failed");
209 err = FwpmGetAppIdFromFileName0(exe_path, &openvpnblob);
210 OUT_ON_ERROR(err,
"Get byte blob for openvpn executable name failed");
213 match_openvpn.fieldKey = FWPM_CONDITION_ALE_APP_ID;
214 match_openvpn.matchType = FWP_MATCH_EQUAL;
215 match_openvpn.conditionValue.type = FWP_BYTE_BLOB_TYPE;
216 match_openvpn.conditionValue.byteBlob = openvpnblob;
218 match_port_53.fieldKey = FWPM_CONDITION_IP_REMOTE_PORT;
219 match_port_53.matchType = FWP_MATCH_EQUAL;
220 match_port_53.conditionValue.type = FWP_UINT16;
221 match_port_53.conditionValue.uint16 = 53;
223 match_interface.fieldKey = FWPM_CONDITION_IP_LOCAL_INTERFACE;
224 match_interface.matchType = FWP_MATCH_EQUAL;
225 match_interface.conditionValue.type = FWP_UINT64;
226 match_interface.conditionValue.uint64 = &itf_luid.Value;
228 match_loopback.fieldKey = FWPM_CONDITION_FLAGS;
229 match_loopback.matchType = FWP_MATCH_FLAGS_ALL_SET;
230 match_loopback.conditionValue.type = FWP_UINT32;
231 match_loopback.conditionValue.uint32 = FWP_CONDITION_FLAG_IS_LOOPBACK;
233 match_not_loopback.fieldKey = FWPM_CONDITION_FLAGS;
234 match_not_loopback.matchType = FWP_MATCH_FLAGS_NONE_SET;
235 match_not_loopback.conditionValue.type = FWP_UINT32;
236 match_not_loopback.conditionValue.uint32 = FWP_CONDITION_FLAG_IS_LOOPBACK;
239 Filter.subLayerKey = OPENVPN_WFP_BLOCK_SUBLAYER;
241 Filter.weight.type = FWP_UINT8;
242 Filter.weight.uint8 = 0xF;
243 Filter.filterCondition = Condition;
244 Filter.numFilterConditions = 1;
247 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
248 Filter.action.type = FWP_ACTION_PERMIT;
249 Condition[0] = match_openvpn;
252 Filter.numFilterConditions = 2;
253 Condition[1] = match_port_53;
255 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
256 OUT_ON_ERROR(err,
"Add filter to permit IPv4 traffic from OpenVPN failed");
259 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
260 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
261 OUT_ON_ERROR(err,
"Add filter to permit IPv6 traffic from OpenVPN failed");
263 msg_handler(0,
"WFP Block: Added permit filters for exe_path");
266 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
267 Filter.action.type = FWP_ACTION_BLOCK;
268 Filter.weight.type = FWP_EMPTY;
269 Filter.numFilterConditions = 1;
270 Condition[0] = match_not_loopback;
273 Filter.numFilterConditions = 2;
274 Condition[1] = match_port_53;
276 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
277 OUT_ON_ERROR(err,
"Add filter to block IPv4 traffic failed");
280 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
281 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
282 OUT_ON_ERROR(err,
"Add filter to block IPv6 traffic failed");
284 msg_handler(0,
"WFP Block: Added block filters for all interfaces");
289 Filter.weight.type = FWP_UINT8;
290 Filter.weight.uint8 = 0xE;
291 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
292 Filter.action.type = FWP_ACTION_PERMIT;
293 Filter.numFilterConditions = 1;
294 Condition[0] = match_interface;
297 Filter.numFilterConditions = 2;
298 Condition[1] = match_port_53;
300 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
301 OUT_ON_ERROR(err,
"Add filter to permit IPv4 traffic through VPN interface failed");
305 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
306 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
307 OUT_ON_ERROR(err,
"Add filter to permit IPv6 traffic through VPN interface failed");
309 msg_handler(0,
"WFP Block: Added permit filters for VPN interface");
312 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
313 Filter.action.type = FWP_ACTION_BLOCK;
314 Filter.weight.type = FWP_EMPTY;
315 Filter.numFilterConditions = 2;
316 Condition[0] = match_loopback;
317 Condition[1] = match_port_53;
318 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
319 OUT_ON_ERROR(err,
"Add filter to block IPv4 DNS traffic to loopback failed");
322 Filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
323 err = FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid);
324 OUT_ON_ERROR(err,
"Add filter to block IPv6 DNS traffic to loopback failed");
326 msg_handler(0,
"WFP Block: Added block filters for DNS traffic to loopback");
331 FwpmFreeMemory0((
void **)&openvpnblob);
334 if (err && *engine_handle)
336 FwpmEngineClose0(*engine_handle);
337 *engine_handle = NULL;