tcp, udp: Make {tcp,udp}_sock_init() take an inany address
tcp_sock_init() and udp_sock_init() take an address to bind to as an address family and void * pair. Use an inany instead. Formerly AF_UNSPEC was used to indicate that we want to listen on both 0.0.0.0 and ::, now use a NULL inany to indicate that. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
parent
b8d4fac6a2
commit
cbde4192ee
5 changed files with 45 additions and 67 deletions
28
conf.c
28
conf.c
|
@ -116,11 +116,10 @@ static int parse_port_range(const char *s, char **endptr,
|
||||||
static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
struct fwd_ports *fwd)
|
struct fwd_ports *fwd)
|
||||||
{
|
{
|
||||||
char addr_buf[sizeof(struct in6_addr)] = { 0 }, *addr = addr_buf;
|
union inany_addr addr_buf = inany_any6, *addr = &addr_buf;
|
||||||
char buf[BUFSIZ], *spec, *ifname = NULL, *p;
|
char buf[BUFSIZ], *spec, *ifname = NULL, *p;
|
||||||
bool exclude_only = true, bound_one = false;
|
bool exclude_only = true, bound_one = false;
|
||||||
uint8_t exclude[PORT_BITMAP_SIZE] = { 0 };
|
uint8_t exclude[PORT_BITMAP_SIZE] = { 0 };
|
||||||
sa_family_t af = AF_UNSPEC;
|
|
||||||
unsigned i;
|
unsigned i;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
@ -166,15 +165,13 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
|
|
||||||
bitmap_set(fwd->map, i);
|
bitmap_set(fwd->map, i);
|
||||||
if (optname == 't') {
|
if (optname == 't') {
|
||||||
ret = tcp_sock_init(c, AF_UNSPEC, NULL, NULL,
|
ret = tcp_sock_init(c, NULL, NULL, i);
|
||||||
i);
|
|
||||||
if (ret == -ENFILE || ret == -EMFILE)
|
if (ret == -ENFILE || ret == -EMFILE)
|
||||||
goto enfile;
|
goto enfile;
|
||||||
if (!ret)
|
if (!ret)
|
||||||
bound_one = true;
|
bound_one = true;
|
||||||
} else if (optname == 'u') {
|
} else if (optname == 'u') {
|
||||||
ret = udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL,
|
ret = udp_sock_init(c, 0, NULL, NULL, i);
|
||||||
i);
|
|
||||||
if (ret == -ENFILE || ret == -EMFILE)
|
if (ret == -ENFILE || ret == -EMFILE)
|
||||||
goto enfile;
|
goto enfile;
|
||||||
if (!ret)
|
if (!ret)
|
||||||
|
@ -218,6 +215,9 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
if (ifname == buf + 1) { /* Interface without address */
|
if (ifname == buf + 1) { /* Interface without address */
|
||||||
addr = NULL;
|
addr = NULL;
|
||||||
} else {
|
} else {
|
||||||
|
struct in6_addr a6;
|
||||||
|
struct in_addr a4;
|
||||||
|
|
||||||
p = buf;
|
p = buf;
|
||||||
|
|
||||||
/* Allow square brackets for IPv4 too for convenience */
|
/* Allow square brackets for IPv4 too for convenience */
|
||||||
|
@ -226,10 +226,10 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inet_pton(AF_INET, p, addr))
|
if (inet_pton(AF_INET, p, &a4))
|
||||||
af = AF_INET;
|
inany_from_af(addr, AF_INET, &a4);
|
||||||
else if (inet_pton(AF_INET6, p, addr))
|
else if (inet_pton(AF_INET6, p, &a6))
|
||||||
af = AF_INET6;
|
inany_from_af(addr, AF_INET6, &a6);
|
||||||
else
|
else
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
|
@ -276,13 +276,13 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
bitmap_set(fwd->map, i);
|
bitmap_set(fwd->map, i);
|
||||||
|
|
||||||
if (optname == 't') {
|
if (optname == 't') {
|
||||||
ret = tcp_sock_init(c, af, addr, ifname, i);
|
ret = tcp_sock_init(c, addr, ifname, i);
|
||||||
if (ret == -ENFILE || ret == -EMFILE)
|
if (ret == -ENFILE || ret == -EMFILE)
|
||||||
goto enfile;
|
goto enfile;
|
||||||
if (!ret)
|
if (!ret)
|
||||||
bound_one = true;
|
bound_one = true;
|
||||||
} else if (optname == 'u') {
|
} else if (optname == 'u') {
|
||||||
ret = udp_sock_init(c, 0, af, addr, ifname, i);
|
ret = udp_sock_init(c, 0, addr, ifname, i);
|
||||||
if (ret == -ENFILE || ret == -EMFILE)
|
if (ret == -ENFILE || ret == -EMFILE)
|
||||||
goto enfile;
|
goto enfile;
|
||||||
if (!ret)
|
if (!ret)
|
||||||
|
@ -338,9 +338,9 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if (optname == 't')
|
if (optname == 't')
|
||||||
ret = tcp_sock_init(c, af, addr, ifname, i);
|
ret = tcp_sock_init(c, addr, ifname, i);
|
||||||
else if (optname == 'u')
|
else if (optname == 'u')
|
||||||
ret = udp_sock_init(c, 0, af, addr, ifname, i);
|
ret = udp_sock_init(c, 0, addr, ifname, i);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto bind_fail;
|
goto bind_fail;
|
||||||
}
|
}
|
||||||
|
|
47
tcp.c
47
tcp.c
|
@ -2273,17 +2273,16 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tcp_sock_init_af() - Initialise listening socket for a given af and port
|
* tcp_sock_init_one() - Initialise listening socket for address and port
|
||||||
* @c: Execution context
|
* @c: Execution context
|
||||||
* @af: Address family to listen on
|
* @addr: Pointer to address for binding, NULL for dual stack any
|
||||||
* @port: Port, host order
|
|
||||||
* @addr: Pointer to address for binding, NULL if not configured
|
|
||||||
* @ifname: Name of interface to bind to, NULL if not configured
|
* @ifname: Name of interface to bind to, NULL if not configured
|
||||||
|
* @port: Port, host order
|
||||||
*
|
*
|
||||||
* Return: fd for the new listening socket, negative error code on failure
|
* Return: fd for the new listening socket, negative error code on failure
|
||||||
*/
|
*/
|
||||||
static int tcp_sock_init_af(const struct ctx *c, sa_family_t af, in_port_t port,
|
static int tcp_sock_init_one(const struct ctx *c, const union inany_addr *addr,
|
||||||
const void *addr, const char *ifname)
|
const char *ifname, in_port_t port)
|
||||||
{
|
{
|
||||||
union tcp_listen_epoll_ref tref = {
|
union tcp_listen_epoll_ref tref = {
|
||||||
.port = port,
|
.port = port,
|
||||||
|
@ -2291,24 +2290,13 @@ static int tcp_sock_init_af(const struct ctx *c, sa_family_t af, in_port_t port,
|
||||||
};
|
};
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
if (af == AF_UNSPEC) {
|
s = pif_sock_l4(c, EPOLL_TYPE_TCP_LISTEN, PIF_HOST, addr,
|
||||||
ASSERT(!addr);
|
|
||||||
s = pif_sock_l4(c, EPOLL_TYPE_TCP_LISTEN, PIF_HOST, NULL,
|
|
||||||
ifname, port, tref.u32);
|
ifname, port, tref.u32);
|
||||||
} else {
|
|
||||||
union inany_addr aany = af == AF_INET ? inany_any4 : inany_any6;
|
|
||||||
|
|
||||||
if (addr)
|
|
||||||
inany_from_af(&aany, af, addr);
|
|
||||||
|
|
||||||
s = pif_sock_l4(c, EPOLL_TYPE_TCP_LISTEN, PIF_HOST, &aany,
|
|
||||||
ifname, port, tref.u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c->tcp.fwd_in.mode == FWD_AUTO) {
|
if (c->tcp.fwd_in.mode == FWD_AUTO) {
|
||||||
if (af == AF_INET || af == AF_UNSPEC)
|
if (!addr || inany_v4(addr))
|
||||||
tcp_sock_init_ext[port][V4] = s < 0 ? -1 : s;
|
tcp_sock_init_ext[port][V4] = s < 0 ? -1 : s;
|
||||||
if (af == AF_INET6 || af == AF_UNSPEC)
|
if (!addr || !inany_v4(addr))
|
||||||
tcp_sock_init_ext[port][V6] = s < 0 ? -1 : s;
|
tcp_sock_init_ext[port][V6] = s < 0 ? -1 : s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2322,31 +2310,32 @@ static int tcp_sock_init_af(const struct ctx *c, sa_family_t af, in_port_t port,
|
||||||
/**
|
/**
|
||||||
* tcp_sock_init() - Create listening sockets for a given host ("inbound") port
|
* tcp_sock_init() - Create listening sockets for a given host ("inbound") port
|
||||||
* @c: Execution context
|
* @c: Execution context
|
||||||
* @af: Address family to select a specific IP version, or AF_UNSPEC
|
|
||||||
* @addr: Pointer to address for binding, NULL if not configured
|
* @addr: Pointer to address for binding, NULL if not configured
|
||||||
* @ifname: Name of interface to bind to, NULL if not configured
|
* @ifname: Name of interface to bind to, NULL if not configured
|
||||||
* @port: Port, host order
|
* @port: Port, host order
|
||||||
*
|
*
|
||||||
* Return: 0 on (partial) success, negative error code on (complete) failure
|
* Return: 0 on (partial) success, negative error code on (complete) failure
|
||||||
*/
|
*/
|
||||||
int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
int tcp_sock_init(const struct ctx *c, const union inany_addr *addr,
|
||||||
const char *ifname, in_port_t port)
|
const char *ifname, in_port_t port)
|
||||||
{
|
{
|
||||||
int r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
|
int r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
|
||||||
|
|
||||||
ASSERT(!c->no_tcp);
|
ASSERT(!c->no_tcp);
|
||||||
|
|
||||||
if (af == AF_UNSPEC && c->ifi4 && c->ifi6)
|
if (!addr && c->ifi4 && c->ifi6)
|
||||||
/* Attempt to get a dual stack socket */
|
/* Attempt to get a dual stack socket */
|
||||||
if (tcp_sock_init_af(c, AF_UNSPEC, port, addr, ifname) >= 0)
|
if (tcp_sock_init_one(c, NULL, ifname, port) >= 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Otherwise create a socket per IP version */
|
/* Otherwise create a socket per IP version */
|
||||||
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4)
|
if ((!addr || inany_v4(addr)) && c->ifi4)
|
||||||
r4 = tcp_sock_init_af(c, AF_INET, port, addr, ifname);
|
r4 = tcp_sock_init_one(c, addr ? addr : &inany_any4,
|
||||||
|
ifname, port);
|
||||||
|
|
||||||
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6)
|
if ((!addr || !inany_v4(addr)) && c->ifi6)
|
||||||
r6 = tcp_sock_init_af(c, AF_INET6, port, addr, ifname);
|
r6 = tcp_sock_init_one(c, addr ? addr : &inany_any6,
|
||||||
|
ifname, port);
|
||||||
|
|
||||||
if (IN_INTERVAL(0, FD_REF_MAX, r4) || IN_INTERVAL(0, FD_REF_MAX, r6))
|
if (IN_INTERVAL(0, FD_REF_MAX, r4) || IN_INTERVAL(0, FD_REF_MAX, r6))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -2629,7 +2618,7 @@ static void tcp_port_rebind(struct ctx *c, bool outbound)
|
||||||
if (outbound)
|
if (outbound)
|
||||||
tcp_ns_sock_init(c, port);
|
tcp_ns_sock_init(c, port);
|
||||||
else
|
else
|
||||||
tcp_sock_init(c, AF_UNSPEC, NULL, NULL, port);
|
tcp_sock_init(c, NULL, NULL, port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
tcp.h
2
tcp.h
|
@ -18,7 +18,7 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
|
int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
|
||||||
const void *saddr, const void *daddr,
|
const void *saddr, const void *daddr,
|
||||||
const struct pool *p, int idx, const struct timespec *now);
|
const struct pool *p, int idx, const struct timespec *now);
|
||||||
int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
int tcp_sock_init(const struct ctx *c, const union inany_addr *addr,
|
||||||
const char *ifname, in_port_t port);
|
const char *ifname, in_port_t port);
|
||||||
int tcp_init(struct ctx *c);
|
int tcp_init(struct ctx *c);
|
||||||
void tcp_timer(struct ctx *c, const struct timespec *now);
|
void tcp_timer(struct ctx *c, const struct timespec *now);
|
||||||
|
|
31
udp.c
31
udp.c
|
@ -785,15 +785,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
|
||||||
* udp_sock_init() - Initialise listening sockets for a given port
|
* udp_sock_init() - Initialise listening sockets for a given port
|
||||||
* @c: Execution context
|
* @c: Execution context
|
||||||
* @ns: In pasta mode, if set, bind with loopback address in namespace
|
* @ns: In pasta mode, if set, bind with loopback address in namespace
|
||||||
* @af: Address family to select a specific IP version, or AF_UNSPEC
|
|
||||||
* @addr: Pointer to address for binding, NULL if not configured
|
* @addr: Pointer to address for binding, NULL if not configured
|
||||||
* @ifname: Name of interface to bind to, NULL if not configured
|
* @ifname: Name of interface to bind to, NULL if not configured
|
||||||
* @port: Port, host order
|
* @port: Port, host order
|
||||||
*
|
*
|
||||||
* Return: 0 on (partial) success, negative error code on (complete) failure
|
* Return: 0 on (partial) success, negative error code on (complete) failure
|
||||||
*/
|
*/
|
||||||
int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
int udp_sock_init(const struct ctx *c, int ns, const union inany_addr *addr,
|
||||||
const void *addr, const char *ifname, in_port_t port)
|
const char *ifname, in_port_t port)
|
||||||
{
|
{
|
||||||
union udp_listen_epoll_ref uref = {
|
union udp_listen_epoll_ref uref = {
|
||||||
.pif = ns ? PIF_SPLICE : PIF_HOST,
|
.pif = ns ? PIF_SPLICE : PIF_HOST,
|
||||||
|
@ -803,11 +802,9 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
|
|
||||||
ASSERT(!c->no_udp);
|
ASSERT(!c->no_udp);
|
||||||
|
|
||||||
if (af == AF_UNSPEC && c->ifi4 && c->ifi6 && !ns) {
|
if (!addr && c->ifi4 && c->ifi6 && !ns) {
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
ASSERT(!addr);
|
|
||||||
|
|
||||||
/* Attempt to get a dual stack socket */
|
/* Attempt to get a dual stack socket */
|
||||||
s = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
s = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
||||||
NULL, ifname, port, uref.u32);
|
NULL, ifname, port, uref.u32);
|
||||||
|
@ -817,15 +814,11 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) {
|
if ((!addr || inany_v4(addr)) && c->ifi4) {
|
||||||
if (!ns) {
|
if (!ns) {
|
||||||
union inany_addr aany = inany_any4;
|
|
||||||
|
|
||||||
if (addr)
|
|
||||||
inany_from_af(&aany, AF_INET, addr);
|
|
||||||
|
|
||||||
r4 = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
r4 = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
||||||
&aany, ifname, port, uref.u32);
|
addr ? addr : &inany_any4, ifname,
|
||||||
|
port, uref.u32);
|
||||||
|
|
||||||
udp_splice_init[V4][port] = r4 < 0 ? -1 : r4;
|
udp_splice_init[V4][port] = r4 < 0 ? -1 : r4;
|
||||||
} else {
|
} else {
|
||||||
|
@ -836,15 +829,11 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) {
|
if ((!addr || !inany_v4(addr)) && c->ifi6) {
|
||||||
if (!ns) {
|
if (!ns) {
|
||||||
union inany_addr aany = inany_any6;
|
|
||||||
|
|
||||||
if (addr)
|
|
||||||
inany_from_af(&aany, AF_INET6, addr);
|
|
||||||
|
|
||||||
r6 = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
r6 = pif_sock_l4(c, EPOLL_TYPE_UDP_LISTEN, PIF_HOST,
|
||||||
&aany, ifname, port, uref.u32);
|
addr ? addr : &inany_any6, ifname,
|
||||||
|
port, uref.u32);
|
||||||
|
|
||||||
udp_splice_init[V6][port] = r6 < 0 ? -1 : r6;
|
udp_splice_init[V6][port] = r6 < 0 ? -1 : r6;
|
||||||
} else {
|
} else {
|
||||||
|
@ -918,7 +907,7 @@ static void udp_port_rebind(struct ctx *c, bool outbound)
|
||||||
|
|
||||||
if ((c->ifi4 && socks[V4][port] == -1) ||
|
if ((c->ifi4 && socks[V4][port] == -1) ||
|
||||||
(c->ifi6 && socks[V6][port] == -1))
|
(c->ifi6 && socks[V6][port] == -1))
|
||||||
udp_sock_init(c, outbound, AF_UNSPEC, NULL, NULL, port);
|
udp_sock_init(c, outbound, NULL, NULL, port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
udp.h
4
udp.h
|
@ -16,8 +16,8 @@ void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
int udp_tap_handler(const struct ctx *c, uint8_t pif,
|
int udp_tap_handler(const struct ctx *c, uint8_t pif,
|
||||||
sa_family_t af, const void *saddr, const void *daddr,
|
sa_family_t af, const void *saddr, const void *daddr,
|
||||||
const struct pool *p, int idx, const struct timespec *now);
|
const struct pool *p, int idx, const struct timespec *now);
|
||||||
int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
int udp_sock_init(const struct ctx *c, int ns, const union inany_addr *addr,
|
||||||
const void *addr, const char *ifname, in_port_t port);
|
const char *ifname, in_port_t port);
|
||||||
int udp_init(struct ctx *c);
|
int udp_init(struct ctx *c);
|
||||||
void udp_timer(struct ctx *c, const struct timespec *now);
|
void udp_timer(struct ctx *c, const struct timespec *now);
|
||||||
void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s);
|
void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s);
|
||||||
|
|
Loading…
Reference in a new issue