aboutgitcodebugslistschat
path: root/udp.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2023-03-08 03:29:51 +0100
committerStefano Brivio <sbrivio@redhat.com>2023-03-09 03:43:59 +0100
commita9c59dd91baa6315259328fc0e36ac63a61ab24b (patch)
tree7521d3ff5f142ed9acf20e7b1cafc537a818031a /udp.c
parent70148ce5be9844ba0e3c8112fb0117689c1bfca0 (diff)
downloadpasst-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar.gz
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar.bz2
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar.lz
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar.xz
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.tar.zst
passt-a9c59dd91baa6315259328fc0e36ac63a61ab24b.zip
conf, icmp, tcp, udp: Add options to bind to outbound address and interface
I didn't notice earlier: libslirp (and slirp4netns) supports binding outbound sockets to specific IPv4 and IPv6 addresses, to force the source addresse selection. If we want to claim feature parity, we should implement that as well. Further, Podman supports specifying outbound interfaces as well, but this is simply done by resolving the primary address for an interface when the network back-end is started. However, since kernel version 5.7, commit c427bfec18f2 ("net: core: enable SO_BINDTODEVICE for non-root users"), we can actually bind to a specific interface name, which doesn't need to be validated in advance. Implement -o / --outbound ADDR to bind to IPv4 and IPv6 addresses, and --outbound-if4 and --outbound-if6 to bind IPv4 and IPv6 sockets to given interfaces. Given that it probably makes little sense to select addresses and routes from interfaces different than the ones given for outbound sockets, also assign those as "template" interfaces, by default, unless explicitly overridden by '-i'. For ICMP and UDP, we call sock_l4() to open outbound sockets, as we already needed to bind to given ports or echo identifiers, and we can bind() a socket only once: there, pass address (if any) and interface (if any) for the existing bind() and setsockopt() calls. For TCP, in general, we wouldn't otherwise bind sockets. Add a specific helper to do that. For UDP outbound sockets, we need to know if the final destination of the socket is a loopback address, before we decide whether it makes sense to bind the socket at all: move the block mangling the address destination before the creation of the socket in the IPv4 path. This was already the case for the IPv6 path. Signed-off-by: Stefano Brivio <sbrivio@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'udp.c')
-rw-r--r--udp.c54
1 files changed, 37 insertions, 17 deletions
diff --git a/udp.c b/udp.c
index b7cbfdc..ef486fe 100644
--- a/udp.c
+++ b/udp.c
@@ -843,20 +843,6 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
sa = (struct sockaddr *)&s_in;
sl = sizeof(s_in);
- if (!(s = udp_tap_map[V4][src].sock)) {
- union udp_epoll_ref uref = { .udp.port = src };
-
- s = sock_l4(c, AF_INET, IPPROTO_UDP, NULL, NULL, src,
- uref.u32);
- if (s < 0)
- return p->count;
-
- udp_tap_map[V4][src].sock = s;
- bitmap_set(udp_act[V4][UDP_ACT_TAP], src);
- }
-
- udp_tap_map[V4][src].ts = now->tv_sec;
-
if (IN4_ARE_ADDR_EQUAL(&s_in.sin_addr, &c->ip4.dns_match) &&
ntohs(s_in.sin_port) == 53) {
s_in.sin_addr = c->ip4.dns_host;
@@ -868,13 +854,37 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
else
s_in.sin_addr = c->ip4.addr_seen;
}
+
+ if (!(s = udp_tap_map[V4][src].sock)) {
+ union udp_epoll_ref uref = { .udp.port = src };
+ in_addr_t bind_addr = { 0 };
+ const char *bind_if = NULL;
+
+ if (!IN6_IS_ADDR_LOOPBACK(&s_in.sin_addr) &&
+ *c->ip6.ifname_out)
+ bind_if = c->ip6.ifname_out;
+
+ if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr_out) &&
+ !IN4_IS_ADDR_LOOPBACK(&s_in.sin_addr))
+ bind_addr = c->ip4.addr_out.s_addr;
+
+ s = sock_l4(c, AF_INET, IPPROTO_UDP, &bind_addr,
+ bind_if, src, uref.u32);
+ if (s < 0)
+ return p->count;
+
+ udp_tap_map[V4][src].sock = s;
+ bitmap_set(udp_act[V4][UDP_ACT_TAP], src);
+ }
+
+ udp_tap_map[V4][src].ts = now->tv_sec;
} else {
s_in6 = (struct sockaddr_in6) {
.sin6_family = AF_INET6,
.sin6_port = uh->dest,
.sin6_addr = *(struct in6_addr *)addr,
};
- const void *bind_addr = &in6addr_any;
+ const struct in6_addr *bind_addr = &in6addr_any;
sa = (struct sockaddr *)&s_in6;
sl = sizeof(s_in6);
@@ -898,9 +908,19 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
if (!(s = udp_tap_map[V6][src].sock)) {
union udp_epoll_ref uref = { .udp.v6 = 1,
.udp.port = src };
+ const char *bind_if = NULL;
+
+ if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) &&
+ *c->ip6.ifname_out)
+ bind_if = c->ip6.ifname_out;
+
+ if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out) &&
+ !IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) &&
+ !IN6_IS_ADDR_LINKLOCAL(&s_in6.sin6_addr))
+ bind_addr = &c->ip6.addr_out;
- s = sock_l4(c, AF_INET6, IPPROTO_UDP, bind_addr, NULL,
- src, uref.u32);
+ s = sock_l4(c, AF_INET6, IPPROTO_UDP, bind_addr,
+ bind_if, src, uref.u32);
if (s < 0)
return p->count;