conntrackd: restrict multicast reception
Pablo Neira Ayuso
maintainerPATCH v2 1/3 conntrackd: restrict multicast reception
Bind the socket to the multicast address specified by {IPv4,IPv6}_address to discard unicast UDP packets and multicast traffic not coming to the dedicated interface. There is already code to restrict the interface but the socket was bound to any address.
Without this patch, multicast sync messages can be received from any interface if your firewall policy does not restrict the interface used for sending and receiving them.
---
v2: fix previous attempt, use multicast address to bind to the already specified interface.
src/mcast.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
src/mcast.c +6 -5
@@ -49,23 +49,24 @@ struct mcast_sock *mcast_server_create(struct mcast_conf *conf)switch (conf->ipproto) {case AF_INET : mreq.ipv4.imr_multiaddr.s_addr = conf->in.inet_addr.s_addr; - mreq.ipv4.imr_interface.s_addr =conf->ifa.interface_addr.s_addr; + mreq.ipv4.imr_interface.s_addr = conf->ifa.interface_addr.s_addr; m->addr.ipv4.sin_family =AF_INET ; m->addr.ipv4.sin_port =htons (conf->port); - m->addr.ipv4.sin_addr.s_addr =htonl (INADDR_ANY ); + m->addr.ipv4.sin_addr.s_addr = conf->in.inet_addr.s_addr; - m->sockaddr_len =sizeof (struct sockaddr_in ); + m->sockaddr_len =sizeof (struct sockaddr_in );break ;case AF_INET6 :memcpy (&mreq.ipv6.ipv6mr_multiaddr, &conf->in.inet_addr6, -sizeof (uint32_t ) *4 ); +sizeof (struct in6_addr )); mreq.ipv6.ipv6mr_interface = conf->ifa.interface_index6; m->addr.ipv6.sin6_family =AF_INET6 ; m->addr.ipv6.sin6_port =htons (conf->port); - m->addr.ipv6.sin6_addr = in6addr_any; +memcpy (&m->addr.ipv6.sin6_addr, &conf->in.inet_addr6, +sizeof (struct in6_addr )); m->sockaddr_len =sizeof (struct sockaddr_in6 );break ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 2/3 conntrackd: remove double close() in multicast resulting in EBADFD
When using multicast for synchronization, misconfiguration results in misleading EBADFD log errors due to double close() on the file descriptor from the error path.
The caller of __mcast_client_create_{ipv4,ipv6} already deals with closing the socket file descriptor.
---
v2: no changes.
src/mcast.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
src/mcast.c +8 -16
@@ -135,7 +135,7 @@ void mcast_server_destroy(struct mcast_sock *m)free (m); } -static int +static int __mcast_client_create_ipv4 (struct mcast_sock *m,struct mcast_conf *conf) {int no =0 ;
@@ -143,25 +143,21 @@ __mcast_client_create_ipv4(struct mcast_sock *m, struct mcast_conf *conf) m->addr.ipv4.sin_family =AF_INET ; m->addr.ipv4.sin_port =htons (conf->port); m->addr.ipv4.sin_addr = conf->in.inet_addr; - m->sockaddr_len =sizeof (struct sockaddr_in ); + m->sockaddr_len =sizeof (struct sockaddr_in );if (setsockopt (m->fd,IPPROTO_IP ,IP_MULTICAST_LOOP , &no, -sizeof (int )) <0 ) { -close (m->fd); +sizeof (int )) <0 )return -1 ; - }if (setsockopt (m->fd,IPPROTO_IP ,IP_MULTICAST_IF , &conf->ifa.interface_addr, -sizeof (struct in_addr )) ==-1 ) { -close (m->fd); +sizeof (struct in_addr )) ==-1 )return -1 ; - }return 0 ; } -static int +static int __mcast_client_create_ipv6 (struct mcast_sock *m,struct mcast_conf *conf) {int no =0 ;
@@ -171,20 +167,16 @@ __mcast_client_create_ipv6(struct mcast_sock *m, struct mcast_conf *conf)memcpy (&m->addr.ipv6.sin6_addr, &conf->in.inet_addr6,sizeof (struct in6_addr )); - m->sockaddr_len =sizeof (struct sockaddr_in6 ); + m->sockaddr_len =sizeof (struct sockaddr_in6 );if (setsockopt (m->fd,IPPROTO_IPV6 ,IPV6_MULTICAST_LOOP , &no, -sizeof (int )) <0 ) { -close (m->fd); +sizeof (int )) <0 )return -1 ; - }if (setsockopt (m->fd,IPPROTO_IPV6 ,IPV6_MULTICAST_IF , &conf->ifa.interface_index6, -sizeof (unsigned int )) ==-1 ) { -close (m->fd); +sizeof (unsigned int )) ==-1 )return -1 ; - }return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 3/3 conntrackd: update netns test to support IPv6
In reply to Pablo Neira AyusoExtend test to support for IPv6:
- Add IPv6 address and route.
- Use inet instead of ip table for masquerading.
- Annotate the IPv6 multicast address for IPv6_address in conntrackd.conf files.
---
v3: new in this series.
tests/conntrackd/netns/conntrackd-netns-test.sh | 10 ++++++++++ tests/conntrackd/netns/conntrackd-nsr1.conf | 1 + tests/conntrackd/netns/conntrackd-nsr2.conf | 1 + tests/conntrackd/netns/ruleset-nsr1.nft | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-)
tests/conntrackd/netns/conntrackd-netns-test.sh +10 -0
@@ -17,24 +17,34 @@ start () { ip link add veth2 netns nsr1 type veth peer name veth0 netns nsr2 ip -net ns1 addr add 192.168.10.2/24 dev veth0 + ip -6 -net ns1 addr add bbbb::2/64 dev veth0 ip -net ns1 link set up dev veth0 ip -net ns1 ro add 10.0.1.0/24 via 192.168.10.1 dev veth0 + ip -6 -net ns1 ro add aaaa::/64 via bbbb::1 dev veth0 ip -net nsr1 addr add 10.0.1.1/24 dev veth0 ip -net nsr1 addr add 192.168.10.1/24 dev veth1 + ip -6 -net nsr1 addr add aaaa::1/64 dev veth0 + ip -6 -net nsr1 addr add bbbb::1/64 dev veth1 ip -net nsr1 link set up dev veth0 ip -net nsr1 link set up dev veth1 ip -net nsr1 route add default via 192.168.10.2 + ip -6 -net nsr1 route add default via bbbb::2 ip netns exec nsr1 sysctl net.ipv4.ip_forward=1 + ip netns exec nsr1 sysctl net.ipv6.conf.all.forwarding=1 ip -net nsr1 addr add 192.168.100.2/24 dev veth2 + ip -6 -net nsr1 addr add cccc::2/96 dev veth2 ip -net nsr1 link set up dev veth2 ip -net nsr2 addr add 192.168.100.3/24 dev veth0 + ip -6 -net nsr2 addr add cccc::3/96 dev veth0 ip -net nsr2 link set up dev veth0 ip -net ns2 addr add 10.0.1.2/24 dev veth0 + ip -6 -net ns2 addr add aaaa::2/64 dev veth0 ip -net ns2 link set up dev veth0 ip -net ns2 route add default via 10.0.1.1 + ip -6 -net ns2 route add default via aaaa::1 echo 1 > /proc/sys/net/netfilter/nf_log_all_netns
tests/conntrackd/netns/conntrackd-nsr1.conf +1 -0
@@ -3,6 +3,7 @@ Sync { } Multicast { IPv4_address 225.0.0.50 + #IPv6_address ff08::123 Group 3780 IPv4_interface 192.168.100.2 Interface veth2
tests/conntrackd/netns/conntrackd-nsr2.conf +1 -0
@@ -3,6 +3,7 @@ Sync { } Multicast { IPv4_address 225.0.0.50 + #IPv6_address ff08::123 Group 3780 IPv4_interface 192.168.100.3 Interface veth0
tests/conntrackd/netns/ruleset-nsr1.nft +1 -1
@@ -1,4 +1,4 @@ -table ip filter { +table inet filter { chain postrouting { type nat hook postrouting priority srcnat; policy accept; oif veth0 masquerade
Signature · 1 line
2.30.2