diff options
| author | David Gibson <david@gibson.dropbear.id.au> | 2025-12-02 15:02:06 +1100 |
|---|---|---|
| committer | Stefano Brivio <sbrivio@redhat.com> | 2025-12-02 23:07:17 +0100 |
| commit | 3b8b8342999b9dfd5f68adc7a5855c4e9a134892 (patch) | |
| tree | 7c434346c9bbb790bb930cdf26954b508d8954f7 | |
| parent | b0523f6b0629358024e95b5d01dc81512cfa8e10 (diff) | |
| download | passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar.gz passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar.bz2 passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar.lz passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar.xz passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.tar.zst passt-3b8b8342999b9dfd5f68adc7a5855c4e9a134892.zip | |
util, flow, pif: Simplify sock_l4_sa() interface
sock_l4_sa() has a somewhat confusing 'v6only' option controlling whether
to set the IPV6_V6ONLY socket option. Usually it's set when the given
address is IPv6, but not when we want to create a dual stack listening
socket. The latter only makes sense when the address is :: however.
Clarify this by only keeping the v6only option in an internal helper
sock_l4_(). External users will call either sock_l4() which always creates
a socket bound to a specific IP version, or sock_l4_dualstack() which
creates a dual stack socket, but takes only a port not an address.
We drop the '_sa' suffix while we're at it - it exists because this used
to be an internal version with a sock_l4() wrapper. The wrapper no longer
exists so the '_sa' is no longer useful.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
| -rw-r--r-- | flow.c | 6 | ||||
| -rw-r--r-- | pif.c | 12 | ||||
| -rw-r--r-- | util.c | 45 | ||||
| -rw-r--r-- | util.h | 6 |
4 files changed, 52 insertions, 17 deletions
@@ -183,8 +183,7 @@ static int flowside_sock_splice(void *arg) ns_enter(a->c); - a->fd = sock_l4_sa(a->c, a->type, a->sa, NULL, - a->sa->sa_family == AF_INET6); + a->fd = sock_l4(a->c, a->type, a->sa, NULL); a->err = errno; return 0; @@ -219,8 +218,7 @@ int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif, else if (sa.sa_family == AF_INET6) ifname = c->ip6.ifname_out; - return sock_l4_sa(c, type, &sa, ifname, - sa.sa_family == AF_INET6); + return sock_l4(c, type, &sa, ifname); case PIF_SPLICE: { struct flowside_sock_args args = { @@ -76,11 +76,6 @@ int pif_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif, const union inany_addr *addr, const char *ifname, in_port_t port, uint32_t data) { - union sockaddr_inany sa = { - .sa6.sin6_family = AF_INET6, - .sa6.sin6_addr = in6addr_any, - .sa6.sin6_port = htons(port), - }; union epoll_ref ref; int ret; @@ -93,11 +88,12 @@ int pif_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif, } if (!addr) { - ref.fd = sock_l4_sa(c, type, &sa, ifname, false); + ref.fd = sock_l4_dualstack(c, type, port, ifname); } else { + union sockaddr_inany sa; + pif_sockaddr(c, &sa, pif, addr, port); - ref.fd = sock_l4_sa(c, type, &sa, ifname, - sa.sa_family == AF_INET6); + ref.fd = sock_l4(c, type, &sa, ifname); } if (ref.fd < 0) @@ -40,7 +40,7 @@ #endif /** - * sock_l4_sa() - Create and bind socket to socket address, add to epoll list + * sock_l4_() - Create and bind socket to socket address * @c: Execution context * @type: epoll type * @sa: Socket address to bind to @@ -49,8 +49,9 @@ * * Return: newly created socket, negative error code on failure */ -int sock_l4_sa(const struct ctx *c, enum epoll_type type, - const union sockaddr_inany *sa, const char *ifname, bool v6only) +static int sock_l4_(const struct ctx *c, enum epoll_type type, + const union sockaddr_inany *sa, const char *ifname, + bool v6only) { sa_family_t af = sa->sa_family; bool freebind = false; @@ -169,6 +170,44 @@ int sock_l4_sa(const struct ctx *c, enum epoll_type type, } /** + * sock_l4() - Create and bind socket to given address + * @c: Execution context + * @type: epoll type + * @sa: Socket address to bind to + * @ifname: Interface for binding, NULL for any + * + * Return: newly created socket, negative error code on failure + */ +int sock_l4(const struct ctx *c, enum epoll_type type, + const union sockaddr_inany *sa, const char *ifname) +{ + return sock_l4_(c, type, sa, ifname, sa->sa_family == AF_INET6); +} + +/** + * sock_l4_dualstack() - Create a dual stack socket bound with wildcard address + * @c: Execution context + * @type: epoll type + * @port Port to bind to (:: and 0.0.0.0) + * @ifname: Interface for binding, NULL for any + * + * Return: newly created socket, negative error code on failure + * + * A dual stack socket is effectively bound to both :: and 0.0.0.0. + */ +int sock_l4_dualstack(const struct ctx *c, enum epoll_type type, + in_port_t port, const char *ifname) +{ + union sockaddr_inany sa = { + .sa6.sin6_family = AF_INET6, + .sa6.sin6_addr = in6addr_any, + .sa6.sin6_port = htons(port), + }; + + return sock_l4_(c, type, &sa, ifname, 0); +} + +/** * sock_unix() - Create and bind AF_UNIX socket * @sock_path: Socket path. If empty, set on return (UNIX_SOCK_PATH as prefix) * @@ -208,8 +208,10 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags, struct ctx; union sockaddr_inany; -int sock_l4_sa(const struct ctx *c, enum epoll_type type, - const union sockaddr_inany *sa, const char *ifname, bool v6only); +int sock_l4(const struct ctx *c, enum epoll_type type, + const union sockaddr_inany *sa, const char *ifname); +int sock_l4_dualstack(const struct ctx *c, enum epoll_type type, + in_port_t port, const char *ifname); int sock_unix(char *sock_path); void sock_probe_features(struct ctx *c); long timespec_diff_ms(const struct timespec *a, const struct timespec *b); |
