diff --git a/src/network/network_nftables.c b/src/network/network_nftables.c index b3605bd40e..5d716264bf 100644 --- a/src/network/network_nftables.c +++ b/src/network/network_nftables.c @@ -29,6 +29,7 @@ #include "internal.h" #include "virfirewalld.h" +#include "vircommand.h" #include "virerror.h" #include "virlog.h" #include "virhash.h" @@ -924,6 +925,67 @@ nftablesAddIPSpecificFirewallRules(virFirewall *fw, } +/** + * nftablesAddUdpChecksumFixWithTC: + * + * Add a tc filter rule to @ifname (the bridge device of this network) + * that will recompute the checksum of udp packets output from @iface with + * destination port @port. + * + * Normally the checksum should be filled by some part of the basic + * network stack, but there are cases (e.g. DHCP response packets sent + * from virtualization host to a QEMU guest when the guest NIC uses + * vhost-net packet processing) when the host (sender) thinks that + * packet checksums will be computed elsewhere (and so leaves a + * partially computed checksum in the packet header) while the guest + * (receiver) thinks that the checksum has already been fully + * computed; in the meantime none of the code in between has actually + * finished computing the checksum. + * + * An example of this is DHCP response packets from host to guest. If + * the checksum of each of these packets isn't properly computed, then + * many guests (e.g. FreeBSD) will drop them with reason BAD CHECKSUM; + * this tc filter rule will fix the ip and udp checksums, and the + * FreeBSD dhcp client will happily accept the packet. + * + * (NB: if you're wondering how the tc qdisc and filter are removed + * when the network is destroyed, the answer is that the kernel + * automatically (and properly) removes them for us, so we don't need + * to worry about keeping track/deleting as we do with nftables rules) + */ +static int +nftablesAddUdpChecksumFixWithTC(virFirewall *fw, + const char *iface, + int port) +{ + g_autofree char *portstr = g_strdup_printf("%d", port); + + /* this will add the qdisc (that the filter below is attached to) + * unless it already exists + */ + if (virNetDevBandWidthAddTxFilterParentQdisc(iface, true) < 0) + return -1; + + /* add a filter to catch all udp packets with dst "port" and + * recompute their checksum + */ + virFirewallAddCmd(fw, VIR_FIREWALL_LAYER_TC, + "filter", "add", "dev", iface, + "prio", "2", "protocol", "ip", "parent", "1:", + "u32", "match", "ip", "dport", portstr, "ffff", + "action", "csum", "ip", "and", "udp", + NULL); + + virFirewallAddRollbackCmd(fw, VIR_FIREWALL_LAYER_TC, + "filter", "del", "dev", iface, + "prio", "2", "protocol", "ip", "parent", "1:", + "u32", "match", "ip", "dport", portstr, "ffff", + "action", "csum", "ip", "and", "udp", + NULL); + return 0; +} + + /* nftablesAddFirewallrules: * * @def - the network that needs an nftables firewall added @@ -944,6 +1006,12 @@ nftablesAddFirewallRules(virNetworkDef *def, virFirewall **fwRemoval) virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_AUTO_ROLLBACK); + /* add the tc filter rule needed to fixup the checksum of dhcp + * response packets going from host to guest. + */ + if (nftablesAddUdpChecksumFixWithTC(fw, def->bridge, 68) < 0) + return -1; + nftablesAddGeneralFirewallRules(fw, def); for (i = 0; diff --git a/tests/networkxml2firewalldata/forward-dev-linux.nftables b/tests/networkxml2firewalldata/forward-dev-linux.nftables index 8badb74beb..6772383b37 100644 --- a/tests/networkxml2firewalldata/forward-dev-linux.nftables +++ b/tests/networkxml2firewalldata/forward-dev-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/isolated-linux.nftables b/tests/networkxml2firewalldata/isolated-linux.nftables index d1b4dac178..546a18b75a 100644 --- a/tests/networkxml2firewalldata/isolated-linux.nftables +++ b/tests/networkxml2firewalldata/isolated-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-default-linux.nftables b/tests/networkxml2firewalldata/nat-default-linux.nftables index 28508292f9..08623c1381 100644 --- a/tests/networkxml2firewalldata/nat-default-linux.nftables +++ b/tests/networkxml2firewalldata/nat-default-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-ipv6-linux.nftables b/tests/networkxml2firewalldata/nat-ipv6-linux.nftables index d8a9ba706d..3fd6b94eef 100644 --- a/tests/networkxml2firewalldata/nat-ipv6-linux.nftables +++ b/tests/networkxml2firewalldata/nat-ipv6-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-ipv6-masquerade-linux.nftables b/tests/networkxml2firewalldata/nat-ipv6-masquerade-linux.nftables index a7f09cda59..2811e098d1 100644 --- a/tests/networkxml2firewalldata/nat-ipv6-masquerade-linux.nftables +++ b/tests/networkxml2firewalldata/nat-ipv6-masquerade-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-many-ips-linux.nftables b/tests/networkxml2firewalldata/nat-many-ips-linux.nftables index b826fe6134..5409d5b552 100644 --- a/tests/networkxml2firewalldata/nat-many-ips-linux.nftables +++ b/tests/networkxml2firewalldata/nat-many-ips-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-no-dhcp-linux.nftables b/tests/networkxml2firewalldata/nat-no-dhcp-linux.nftables index d8a9ba706d..3fd6b94eef 100644 --- a/tests/networkxml2firewalldata/nat-no-dhcp-linux.nftables +++ b/tests/networkxml2firewalldata/nat-no-dhcp-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-port-range-ipv6-linux.nftables b/tests/networkxml2firewalldata/nat-port-range-ipv6-linux.nftables index ceaed6fa40..d74417cdb3 100644 --- a/tests/networkxml2firewalldata/nat-port-range-ipv6-linux.nftables +++ b/tests/networkxml2firewalldata/nat-port-range-ipv6-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-port-range-linux.nftables b/tests/networkxml2firewalldata/nat-port-range-linux.nftables index 1dc37a26ec..b55bb287a9 100644 --- a/tests/networkxml2firewalldata/nat-port-range-linux.nftables +++ b/tests/networkxml2firewalldata/nat-port-range-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/nat-tftp-linux.nftables b/tests/networkxml2firewalldata/nat-tftp-linux.nftables index 28508292f9..08623c1381 100644 --- a/tests/networkxml2firewalldata/nat-tftp-linux.nftables +++ b/tests/networkxml2firewalldata/nat-tftp-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \ diff --git a/tests/networkxml2firewalldata/route-default-linux.nftables b/tests/networkxml2firewalldata/route-default-linux.nftables index 282c9542a5..76d6902517 100644 --- a/tests/networkxml2firewalldata/route-default-linux.nftables +++ b/tests/networkxml2firewalldata/route-default-linux.nftables @@ -1,3 +1,43 @@ +tc \ +qdisc \ +show \ +dev \ +virbr0 \ +handle \ +1: +tc \ +qdisc \ +add \ +dev \ +virbr0 \ +root \ +handle \ +1: \ +htb \ +default \ +2 +tc \ +filter \ +add \ +dev \ +virbr0 \ +prio \ +2 \ +protocol \ +ip \ +parent \ +1: \ +u32 \ +match \ +ip \ +dport \ +68 \ +ffff \ +action \ +csum \ +ip \ +and \ +udp nft \ -ae insert \ rule \