From 49631a38a6ec550fb9254f2f9e4a049eea02ed6d Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Wed, 21 Jul 2021 17:44:39 +0200 Subject: tcp, udp: Split IPv4 and IPv6 bound port sets Allow to bind IPv4 and IPv6 ports to tap, namespace or init separately. Port numbers of TCP ports that are bound in a namespace are also bound for UDP for convenience (e.g. iperf3), and IPv4 ports are always bound if the corresponding IPv6 port is bound (socket might not have the IPV6_V6ONLY option set). This will also be configurable later. Signed-off-by: Stefano Brivio --- udp.c | 68 ++++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 29 deletions(-) (limited to 'udp.c') diff --git a/udp.c b/udp.c index 3eb2ecf..30659e0 100644 --- a/udp.c +++ b/udp.c @@ -890,19 +890,24 @@ int udp_sock_init_ns(void *arg) ns_enter(c->pasta_pid); - for (port = 0; port < USHRT_MAX; port++) { - if (!bitmap_isset(c->udp.port_to_init, port)) - continue; - - uref.port = port; + if (c->v4) { + uref.v6 = 0; + for (port = 0; port < USHRT_MAX; port++) { + if (!bitmap_isset(c->udp.port4_to_init, port)) + continue; - if (c->v4) { - uref.v6 = 0; + uref.port = port; sock_l4(c, AF_INET, IPPROTO_UDP, port, 1, uref.u32); } + } + + if (c->v6) { + uref.v6 = 1; + for (port = 0; port < USHRT_MAX; port++) { + if (!bitmap_isset(c->udp.port6_to_init, port)) + continue; - if (c->v6) { - uref.v6 = 1; + uref.port = port; sock_l4(c, AF_INET6, IPPROTO_UDP, port, 1, uref.u32); } } @@ -976,40 +981,45 @@ int udp_sock_init(struct ctx *c) in_port_t port; int s; - for (port = 0; port < USHRT_MAX; port++) { - if (bitmap_isset(c->udp.port_to_ns, port)) - uref.splice = UDP_TO_NS; - else if (bitmap_isset(c->udp.port_to_tap, port)) - uref.splice = 0; - else - continue; - - uref.port = port; + if (c->v4) { + uref.v6 = 0; + for (port = 0; port < USHRT_MAX; port++) { + if (bitmap_isset(c->udp.port4_to_ns, port)) + uref.splice = UDP_TO_NS; + else if (bitmap_isset(c->udp.port4_to_tap, port)) + uref.splice = 0; + else + continue; - if (c->v4) { - uref.v6 = 0; + uref.port = port; s = sock_l4(c, AF_INET, IPPROTO_UDP, port, uref.splice == UDP_TO_NS, uref.u32); - if (!uref.splice && s > 0) udp_tap_map[V4][port].sock = s; } - if (c->v6) { - uref.v6 = 1; + udp_sock4_iov_init(); + } + + if (c->v6) { + uref.v6 = 1; + for (port = 0; port < USHRT_MAX; port++) { + if (bitmap_isset(c->udp.port6_to_ns, port)) + uref.splice = UDP_TO_NS; + else if (bitmap_isset(c->udp.port6_to_tap, port)) + uref.splice = 0; + else + continue; + + uref.port = port; s = sock_l4(c, AF_INET6, IPPROTO_UDP, port, uref.splice == UDP_TO_NS, uref.u32); - if (!uref.splice && s > 0) udp_tap_map[V6][port].sock = s; } - } - - if (c->v4) - udp_sock4_iov_init(); - if (c->v6) udp_sock6_iov_init(); + } if (c->mode == MODE_PASTA) { udp_splice_iov_init(); -- cgit v1.2.3