Use C11 anonymous members to make poll refs less verbose to use
union epoll_ref has a deeply nested set of structs and unions to let us subdivide it into the various different fields we want. This means that referencing elements can involve an awkward long string of intermediate fields. Using C11 anonymous structs and unions lets us do this less clumsily. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
649068a287
commit
8218d99013
10 changed files with 73 additions and 78 deletions
22
icmp.c
22
icmp.c
|
@ -69,7 +69,7 @@ static uint8_t icmp_act[IP_VERSIONS][DIV_ROUND_UP(ICMP_NUM_IDS, 8)];
|
||||||
void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
uint32_t events, const struct timespec *now)
|
uint32_t events, const struct timespec *now)
|
||||||
{
|
{
|
||||||
union icmp_epoll_ref *iref = &ref.r.p.icmp;
|
union icmp_epoll_ref *iref = &ref.icmp;
|
||||||
struct sockaddr_storage sr;
|
struct sockaddr_storage sr;
|
||||||
socklen_t sl = sizeof(sr);
|
socklen_t sl = sizeof(sr);
|
||||||
char buf[USHRT_MAX];
|
char buf[USHRT_MAX];
|
||||||
|
@ -79,11 +79,11 @@ void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
(void)events;
|
(void)events;
|
||||||
(void)now;
|
(void)now;
|
||||||
|
|
||||||
n = recvfrom(ref.r.s, buf, sizeof(buf), 0, (struct sockaddr *)&sr, &sl);
|
n = recvfrom(ref.s, buf, sizeof(buf), 0, (struct sockaddr *)&sr, &sl);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (iref->icmp.v6) {
|
if (iref->v6) {
|
||||||
struct sockaddr_in6 *sr6 = (struct sockaddr_in6 *)&sr;
|
struct sockaddr_in6 *sr6 = (struct sockaddr_in6 *)&sr;
|
||||||
struct icmp6hdr *ih = (struct icmp6hdr *)buf;
|
struct icmp6hdr *ih = (struct icmp6hdr *)buf;
|
||||||
|
|
||||||
|
@ -93,8 +93,8 @@ void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
/* If bind() fails e.g. because of a broken SELinux policy, this
|
/* If bind() fails e.g. because of a broken SELinux policy, this
|
||||||
* might happen. Fix up the identifier to match the sent one.
|
* might happen. Fix up the identifier to match the sent one.
|
||||||
*/
|
*/
|
||||||
if (id != iref->icmp.id)
|
if (id != iref->id)
|
||||||
ih->icmp6_identifier = htons(iref->icmp.id);
|
ih->icmp6_identifier = htons(iref->id);
|
||||||
|
|
||||||
/* In PASTA mode, we'll get any reply we send, discard them. */
|
/* In PASTA mode, we'll get any reply we send, discard them. */
|
||||||
if (c->mode == MODE_PASTA) {
|
if (c->mode == MODE_PASTA) {
|
||||||
|
@ -116,8 +116,8 @@ void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
id = ntohs(ih->un.echo.id);
|
id = ntohs(ih->un.echo.id);
|
||||||
seq = ntohs(ih->un.echo.sequence);
|
seq = ntohs(ih->un.echo.sequence);
|
||||||
|
|
||||||
if (id != iref->icmp.id)
|
if (id != iref->id)
|
||||||
ih->un.echo.id = htons(iref->icmp.id);
|
ih->un.echo.id = htons(iref->id);
|
||||||
|
|
||||||
if (c->mode == MODE_PASTA) {
|
if (c->mode == MODE_PASTA) {
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
size_t plen;
|
size_t plen;
|
||||||
|
|
||||||
if (af == AF_INET) {
|
if (af == AF_INET) {
|
||||||
union icmp_epoll_ref iref = { .icmp.v6 = 0 };
|
union icmp_epoll_ref iref = { .v6 = 0 };
|
||||||
struct sockaddr_in sa = {
|
struct sockaddr_in sa = {
|
||||||
.sin_family = AF_INET,
|
.sin_family = AF_INET,
|
||||||
.sin_addr = { .s_addr = htonl(INADDR_ANY) },
|
.sin_addr = { .s_addr = htonl(INADDR_ANY) },
|
||||||
|
@ -167,7 +167,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
|
|
||||||
sa.sin_port = ih->un.echo.id;
|
sa.sin_port = ih->un.echo.id;
|
||||||
|
|
||||||
iref.icmp.id = id = ntohs(ih->un.echo.id);
|
iref.id = id = ntohs(ih->un.echo.id);
|
||||||
|
|
||||||
if ((s = icmp_id_map[V4][id].sock) <= 0) {
|
if ((s = icmp_id_map[V4][id].sock) <= 0) {
|
||||||
const struct in_addr *bind_addr = NULL;
|
const struct in_addr *bind_addr = NULL;
|
||||||
|
@ -204,7 +204,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
id, ntohs(ih->un.echo.sequence));
|
id, ntohs(ih->un.echo.sequence));
|
||||||
}
|
}
|
||||||
} else if (af == AF_INET6) {
|
} else if (af == AF_INET6) {
|
||||||
union icmp_epoll_ref iref = { .icmp.v6 = 1 };
|
union icmp_epoll_ref iref = { .v6 = 1 };
|
||||||
struct sockaddr_in6 sa = {
|
struct sockaddr_in6 sa = {
|
||||||
.sin6_family = AF_INET6,
|
.sin6_family = AF_INET6,
|
||||||
.sin6_addr = IN6ADDR_ANY_INIT,
|
.sin6_addr = IN6ADDR_ANY_INIT,
|
||||||
|
@ -222,7 +222,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
|
|
||||||
sa.sin6_port = ih->icmp6_identifier;
|
sa.sin6_port = ih->icmp6_identifier;
|
||||||
|
|
||||||
iref.icmp.id = id = ntohs(ih->icmp6_identifier);
|
iref.id = id = ntohs(ih->icmp6_identifier);
|
||||||
if ((s = icmp_id_map[V6][id].sock) <= 0) {
|
if ((s = icmp_id_map[V6][id].sock) <= 0) {
|
||||||
const struct in6_addr *bind_addr = NULL;
|
const struct in6_addr *bind_addr = NULL;
|
||||||
const char *bind_if;
|
const char *bind_if;
|
||||||
|
|
2
icmp.h
2
icmp.h
|
@ -27,7 +27,7 @@ union icmp_epoll_ref {
|
||||||
struct {
|
struct {
|
||||||
uint32_t v6:1,
|
uint32_t v6:1,
|
||||||
id:16;
|
id:16;
|
||||||
} icmp;
|
};
|
||||||
uint32_t u32;
|
uint32_t u32;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
8
passt.c
8
passt.c
|
@ -75,14 +75,14 @@ static void sock_handler(struct ctx *c, union epoll_ref ref,
|
||||||
{
|
{
|
||||||
trace("%s: %s packet from socket %i (events: 0x%08x)",
|
trace("%s: %s packet from socket %i (events: 0x%08x)",
|
||||||
c->mode == MODE_PASST ? "passt" : "pasta",
|
c->mode == MODE_PASST ? "passt" : "pasta",
|
||||||
IP_PROTO_STR(ref.r.proto), ref.r.s, events);
|
IP_PROTO_STR(ref.proto), ref.s, events);
|
||||||
|
|
||||||
if (!c->no_tcp && ref.r.proto == IPPROTO_TCP)
|
if (!c->no_tcp && ref.proto == IPPROTO_TCP)
|
||||||
tcp_sock_handler( c, ref, events, now);
|
tcp_sock_handler( c, ref, events, now);
|
||||||
else if (!c->no_udp && ref.r.proto == IPPROTO_UDP)
|
else if (!c->no_udp && ref.proto == IPPROTO_UDP)
|
||||||
udp_sock_handler( c, ref, events, now);
|
udp_sock_handler( c, ref, events, now);
|
||||||
else if (!c->no_icmp &&
|
else if (!c->no_icmp &&
|
||||||
(ref.r.proto == IPPROTO_ICMP || ref.r.proto == IPPROTO_ICMPV6))
|
(ref.proto == IPPROTO_ICMP || ref.proto == IPPROTO_ICMPV6))
|
||||||
icmp_sock_handler(c, ref, events, now);
|
icmp_sock_handler(c, ref, events, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
passt.h
4
passt.h
|
@ -60,8 +60,8 @@ union epoll_ref {
|
||||||
union udp_epoll_ref udp;
|
union udp_epoll_ref udp;
|
||||||
union icmp_epoll_ref icmp;
|
union icmp_epoll_ref icmp;
|
||||||
uint32_t data;
|
uint32_t data;
|
||||||
} p;
|
};
|
||||||
} r;
|
};
|
||||||
uint64_t u64;
|
uint64_t u64;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
46
tcp.c
46
tcp.c
|
@ -643,8 +643,8 @@ static void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
|
||||||
static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
||||||
{
|
{
|
||||||
int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
||||||
union epoll_ref ref = { .r.proto = IPPROTO_TCP, .r.s = conn->sock,
|
union epoll_ref ref = { .proto = IPPROTO_TCP, .s = conn->sock,
|
||||||
.r.p.tcp.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
struct epoll_event ev = { .data.u64 = ref.u64 };
|
struct epoll_event ev = { .data.u64 = ref.u64 };
|
||||||
|
|
||||||
if (conn->events == CLOSED) {
|
if (conn->events == CLOSED) {
|
||||||
|
@ -663,10 +663,10 @@ static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
||||||
conn->c.in_epoll = true;
|
conn->c.in_epoll = true;
|
||||||
|
|
||||||
if (conn->timer != -1) {
|
if (conn->timer != -1) {
|
||||||
union epoll_ref ref_t = { .r.proto = IPPROTO_TCP,
|
union epoll_ref ref_t = { .proto = IPPROTO_TCP,
|
||||||
.r.s = conn->sock,
|
.s = conn->sock,
|
||||||
.r.p.tcp.tcp.timer = 1,
|
.tcp.timer = 1,
|
||||||
.r.p.tcp.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
struct epoll_event ev_t = { .data.u64 = ref_t.u64,
|
struct epoll_event ev_t = { .data.u64 = ref_t.u64,
|
||||||
.events = EPOLLIN | EPOLLET };
|
.events = EPOLLIN | EPOLLET };
|
||||||
|
|
||||||
|
@ -692,10 +692,10 @@ static void tcp_timer_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (conn->timer == -1) {
|
if (conn->timer == -1) {
|
||||||
union epoll_ref ref = { .r.proto = IPPROTO_TCP,
|
union epoll_ref ref = { .proto = IPPROTO_TCP,
|
||||||
.r.s = conn->sock,
|
.s = conn->sock,
|
||||||
.r.p.tcp.tcp.timer = 1,
|
.tcp.timer = 1,
|
||||||
.r.p.tcp.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
struct epoll_event ev = { .data.u64 = ref.u64,
|
struct epoll_event ev = { .data.u64 = ref.u64,
|
||||||
.events = EPOLLIN | EPOLLET };
|
.events = EPOLLIN | EPOLLET };
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -2749,7 +2749,7 @@ static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref,
|
||||||
conn_event(c, conn, SOCK_ACCEPTED);
|
conn_event(c, conn, SOCK_ACCEPTED);
|
||||||
|
|
||||||
inany_from_sockaddr(&conn->addr, &conn->sock_port, sa);
|
inany_from_sockaddr(&conn->addr, &conn->sock_port, sa);
|
||||||
conn->tap_port = ref.r.p.tcp.tcp.index;
|
conn->tap_port = ref.tcp.index;
|
||||||
|
|
||||||
tcp_snat_inbound(c, &conn->addr);
|
tcp_snat_inbound(c, &conn->addr);
|
||||||
|
|
||||||
|
@ -2780,7 +2780,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref,
|
||||||
socklen_t sl;
|
socklen_t sl;
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
ASSERT(ref.r.p.tcp.tcp.listen);
|
ASSERT(ref.tcp.listen);
|
||||||
|
|
||||||
if (c->tcp.conn_count >= TCP_MAX_CONNS)
|
if (c->tcp.conn_count >= TCP_MAX_CONNS)
|
||||||
return;
|
return;
|
||||||
|
@ -2791,7 +2791,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref,
|
||||||
* https://github.com/llvm/llvm-project/issues/58992
|
* https://github.com/llvm/llvm-project/issues/58992
|
||||||
*/
|
*/
|
||||||
memset(&sa, 0, sizeof(struct sockaddr_in6));
|
memset(&sa, 0, sizeof(struct sockaddr_in6));
|
||||||
s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
|
s = accept4(ref.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -2815,7 +2815,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref,
|
||||||
*/
|
*/
|
||||||
static void tcp_timer_handler(struct ctx *c, union epoll_ref ref)
|
static void tcp_timer_handler(struct ctx *c, union epoll_ref ref)
|
||||||
{
|
{
|
||||||
struct tcp_tap_conn *conn = conn_at_idx(ref.r.p.tcp.tcp.index);
|
struct tcp_tap_conn *conn = conn_at_idx(ref.tcp.index);
|
||||||
struct itimerspec check_armed = { { 0 }, { 0 } };
|
struct itimerspec check_armed = { { 0 }, { 0 } };
|
||||||
|
|
||||||
if (!conn)
|
if (!conn)
|
||||||
|
@ -2935,20 +2935,20 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
{
|
{
|
||||||
union tcp_conn *conn;
|
union tcp_conn *conn;
|
||||||
|
|
||||||
if (ref.r.p.tcp.tcp.timer) {
|
if (ref.tcp.timer) {
|
||||||
tcp_timer_handler(c, ref);
|
tcp_timer_handler(c, ref);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ref.r.p.tcp.tcp.listen) {
|
if (ref.tcp.listen) {
|
||||||
tcp_conn_from_sock(c, ref, now);
|
tcp_conn_from_sock(c, ref, now);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = tc + ref.r.p.tcp.tcp.index;
|
conn = tc + ref.tcp.index;
|
||||||
|
|
||||||
if (conn->c.spliced)
|
if (conn->c.spliced)
|
||||||
tcp_splice_sock_handler(c, &conn->splice, ref.r.s, events);
|
tcp_splice_sock_handler(c, &conn->splice, ref.s, events);
|
||||||
else
|
else
|
||||||
tcp_tap_sock_handler(c, &conn->tap, events);
|
tcp_tap_sock_handler(c, &conn->tap, events);
|
||||||
}
|
}
|
||||||
|
@ -2967,7 +2967,7 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
|
||||||
const struct in_addr *addr, const char *ifname)
|
const struct in_addr *addr, const char *ifname)
|
||||||
{
|
{
|
||||||
in_port_t idx = port + c->tcp.fwd_in.delta[port];
|
in_port_t idx = port + c->tcp.fwd_in.delta[port];
|
||||||
union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx };
|
union tcp_epoll_ref tref = { .listen = 1, .index = idx };
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32);
|
s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32);
|
||||||
|
@ -3027,8 +3027,8 @@ int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
||||||
static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port)
|
static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port)
|
||||||
{
|
{
|
||||||
in_port_t idx = port + c->tcp.fwd_out.delta[port];
|
in_port_t idx = port + c->tcp.fwd_out.delta[port];
|
||||||
union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1,
|
union tcp_epoll_ref tref = { .listen = 1, .outbound = 1,
|
||||||
.tcp.index = idx };
|
.index = idx };
|
||||||
struct in_addr loopback = { htonl(INADDR_LOOPBACK) };
|
struct in_addr loopback = { htonl(INADDR_LOOPBACK) };
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
|
@ -3052,8 +3052,8 @@ static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port)
|
||||||
static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port)
|
static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port)
|
||||||
{
|
{
|
||||||
in_port_t idx = port + c->tcp.fwd_out.delta[port];
|
in_port_t idx = port + c->tcp.fwd_out.delta[port];
|
||||||
union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1,
|
union tcp_epoll_ref tref = { .listen = 1, .outbound = 1,
|
||||||
.tcp.index = idx };
|
.index = idx };
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
ASSERT(c->mode == MODE_PASTA);
|
ASSERT(c->mode == MODE_PASTA);
|
||||||
|
|
2
tcp.h
2
tcp.h
|
@ -41,7 +41,7 @@ union tcp_epoll_ref {
|
||||||
outbound:1,
|
outbound:1,
|
||||||
timer:1,
|
timer:1,
|
||||||
index:20;
|
index:20;
|
||||||
} tcp;
|
};
|
||||||
uint32_t u32;
|
uint32_t u32;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
11
tcp_splice.c
11
tcp_splice.c
|
@ -173,10 +173,10 @@ static int tcp_splice_epoll_ctl(const struct ctx *c,
|
||||||
struct tcp_splice_conn *conn)
|
struct tcp_splice_conn *conn)
|
||||||
{
|
{
|
||||||
int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
||||||
union epoll_ref ref_a = { .r.proto = IPPROTO_TCP, .r.s = conn->a,
|
union epoll_ref ref_a = { .proto = IPPROTO_TCP, .s = conn->a,
|
||||||
.r.p.tcp.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
union epoll_ref ref_b = { .r.proto = IPPROTO_TCP, .r.s = conn->b,
|
union epoll_ref ref_b = { .proto = IPPROTO_TCP, .s = conn->b,
|
||||||
.r.p.tcp.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
struct epoll_event ev_a = { .data.u64 = ref_a.u64 };
|
struct epoll_event ev_a = { .data.u64 = ref_a.u64 };
|
||||||
struct epoll_event ev_b = { .data.u64 = ref_b.u64 };
|
struct epoll_event ev_b = { .data.u64 = ref_b.u64 };
|
||||||
uint32_t events_a, events_b;
|
uint32_t events_a, events_b;
|
||||||
|
@ -516,8 +516,7 @@ bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref,
|
||||||
c->tcp.splice_conn_count++;
|
c->tcp.splice_conn_count++;
|
||||||
conn->a = s;
|
conn->a = s;
|
||||||
|
|
||||||
if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index,
|
if (tcp_splice_new(c, conn, ref.tcp.index, ref.tcp.outbound))
|
||||||
ref.r.p.tcp.tcp.outbound))
|
|
||||||
conn_flag(c, conn, CLOSING);
|
conn_flag(c, conn, CLOSING);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
48
udp.c
48
udp.c
|
@ -388,8 +388,8 @@ static void udp_sock6_iov_init(const struct ctx *c)
|
||||||
int udp_splice_new(const struct ctx *c, int v6, in_port_t src, bool ns)
|
int udp_splice_new(const struct ctx *c, int v6, in_port_t src, bool ns)
|
||||||
{
|
{
|
||||||
struct epoll_event ev = { .events = EPOLLIN | EPOLLRDHUP | EPOLLHUP };
|
struct epoll_event ev = { .events = EPOLLIN | EPOLLRDHUP | EPOLLHUP };
|
||||||
union epoll_ref ref = { .r.proto = IPPROTO_UDP,
|
union epoll_ref ref = { .proto = IPPROTO_UDP,
|
||||||
.r.p.udp.udp = { .splice = true, .ns = ns,
|
.udp = { .splice = true, .ns = ns,
|
||||||
.v6 = v6, .port = src }
|
.v6 = v6, .port = src }
|
||||||
};
|
};
|
||||||
struct udp_splice_port *sp;
|
struct udp_splice_port *sp;
|
||||||
|
@ -414,7 +414,7 @@ int udp_splice_new(const struct ctx *c, int v6, in_port_t src, bool ns)
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
ref.r.s = s;
|
ref.s = s;
|
||||||
|
|
||||||
if (v6) {
|
if (v6) {
|
||||||
struct sockaddr_in6 addr6 = {
|
struct sockaddr_in6 addr6 = {
|
||||||
|
@ -752,8 +752,8 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
* for pasta mode.
|
* for pasta mode.
|
||||||
*/
|
*/
|
||||||
ssize_t n = (c->mode == MODE_PASST ? UDP_MAX_FRAMES : 1);
|
ssize_t n = (c->mode == MODE_PASST ? UDP_MAX_FRAMES : 1);
|
||||||
in_port_t dstport = ref.r.p.udp.udp.port;
|
in_port_t dstport = ref.udp.port;
|
||||||
bool v6 = ref.r.p.udp.udp.v6;
|
bool v6 = ref.udp.v6;
|
||||||
struct mmsghdr *mmh_recv;
|
struct mmsghdr *mmh_recv;
|
||||||
int i, m;
|
int i, m;
|
||||||
|
|
||||||
|
@ -768,7 +768,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
udp4_localname.sin_port = htons(dstport);
|
udp4_localname.sin_port = htons(dstport);
|
||||||
}
|
}
|
||||||
|
|
||||||
n = recvmmsg(ref.r.s, mmh_recv, n, 0, NULL);
|
n = recvmmsg(ref.s, mmh_recv, n, 0, NULL);
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -776,7 +776,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
int splicefrom = -1;
|
int splicefrom = -1;
|
||||||
m = n;
|
m = n;
|
||||||
|
|
||||||
if (ref.r.p.udp.udp.splice) {
|
if (ref.udp.splice) {
|
||||||
splicefrom = udp_mmh_splice_port(v6, mmh_recv + i);
|
splicefrom = udp_mmh_splice_port(v6, mmh_recv + i);
|
||||||
|
|
||||||
for (m = 1; i + m < n; m++) {
|
for (m = 1; i + m < n; m++) {
|
||||||
|
@ -790,8 +790,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
|
|
||||||
if (splicefrom >= 0)
|
if (splicefrom >= 0)
|
||||||
udp_splice_sendfrom(c, i, m, splicefrom, dstport,
|
udp_splice_sendfrom(c, i, m, splicefrom, dstport,
|
||||||
v6, ref.r.p.udp.udp.ns,
|
v6, ref.udp.ns, ref.udp.orig, now);
|
||||||
ref.r.p.udp.udp.orig, now);
|
|
||||||
else
|
else
|
||||||
udp_tap_send(c, i, m, dstport, v6, now);
|
udp_tap_send(c, i, m, dstport, v6, now);
|
||||||
}
|
}
|
||||||
|
@ -857,7 +856,7 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(s = udp_tap_map[V4][src].sock)) {
|
if (!(s = udp_tap_map[V4][src].sock)) {
|
||||||
union udp_epoll_ref uref = { .udp.port = src };
|
union udp_epoll_ref uref = { .port = src };
|
||||||
in_addr_t bind_addr = { 0 };
|
in_addr_t bind_addr = { 0 };
|
||||||
const char *bind_if = NULL;
|
const char *bind_if = NULL;
|
||||||
|
|
||||||
|
@ -907,8 +906,7 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(s = udp_tap_map[V6][src].sock)) {
|
if (!(s = udp_tap_map[V6][src].sock)) {
|
||||||
union udp_epoll_ref uref = { .udp.v6 = 1,
|
union udp_epoll_ref uref = { .v6 = 1, .port = src };
|
||||||
.udp.port = src };
|
|
||||||
const char *bind_if = NULL;
|
const char *bind_if = NULL;
|
||||||
|
|
||||||
if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) &&
|
if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) &&
|
||||||
|
@ -986,27 +984,25 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
int s, r4 = SOCKET_MAX + 1, r6 = SOCKET_MAX + 1;
|
int s, r4 = SOCKET_MAX + 1, r6 = SOCKET_MAX + 1;
|
||||||
|
|
||||||
if (ns) {
|
if (ns) {
|
||||||
uref.udp.port = (in_port_t)(port +
|
uref.port = (in_port_t)(port + c->udp.fwd_out.f.delta[port]);
|
||||||
c->udp.fwd_out.f.delta[port]);
|
|
||||||
} else {
|
} else {
|
||||||
uref.udp.port = (in_port_t)(port +
|
uref.port = (in_port_t)(port + c->udp.fwd_in.f.delta[port]);
|
||||||
c->udp.fwd_in.f.delta[port]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) {
|
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) {
|
||||||
uref.udp.v6 = 0;
|
uref.v6 = 0;
|
||||||
uref.udp.splice = (c->mode == MODE_PASTA);
|
uref.splice = (c->mode == MODE_PASTA);
|
||||||
uref.udp.orig = true;
|
uref.orig = true;
|
||||||
|
|
||||||
if (!ns) {
|
if (!ns) {
|
||||||
r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, addr,
|
r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, addr,
|
||||||
ifname, port, uref.u32);
|
ifname, port, uref.u32);
|
||||||
|
|
||||||
udp_tap_map[V4][uref.udp.port].sock = s < 0 ? -1 : s;
|
udp_tap_map[V4][uref.port].sock = s < 0 ? -1 : s;
|
||||||
udp_splice_init[V4][port].sock = s < 0 ? -1 : s;
|
udp_splice_init[V4][port].sock = s < 0 ? -1 : s;
|
||||||
} else {
|
} else {
|
||||||
struct in_addr loopback = { htonl(INADDR_LOOPBACK) };
|
struct in_addr loopback = { htonl(INADDR_LOOPBACK) };
|
||||||
uref.udp.ns = true;
|
uref.ns = true;
|
||||||
|
|
||||||
r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, &loopback,
|
r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, &loopback,
|
||||||
ifname, port, uref.u32);
|
ifname, port, uref.u32);
|
||||||
|
@ -1015,18 +1011,18 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) {
|
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) {
|
||||||
uref.udp.v6 = 1;
|
uref.v6 = 1;
|
||||||
uref.udp.splice = (c->mode == MODE_PASTA);
|
uref.splice = (c->mode == MODE_PASTA);
|
||||||
uref.udp.orig = true;
|
uref.orig = true;
|
||||||
|
|
||||||
if (!ns) {
|
if (!ns) {
|
||||||
r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP, addr,
|
r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP, addr,
|
||||||
ifname, port, uref.u32);
|
ifname, port, uref.u32);
|
||||||
|
|
||||||
udp_tap_map[V6][uref.udp.port].sock = s < 0 ? -1 : s;
|
udp_tap_map[V6][uref.port].sock = s < 0 ? -1 : s;
|
||||||
udp_splice_init[V6][port].sock = s < 0 ? -1 : s;
|
udp_splice_init[V6][port].sock = s < 0 ? -1 : s;
|
||||||
} else {
|
} else {
|
||||||
uref.udp.ns = true;
|
uref.ns = true;
|
||||||
|
|
||||||
r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP,
|
r6 = s = sock_l4(c, AF_INET6, IPPROTO_UDP,
|
||||||
&in6addr_loopback,
|
&in6addr_loopback,
|
||||||
|
|
2
udp.h
2
udp.h
|
@ -36,7 +36,7 @@ union udp_epoll_ref {
|
||||||
ns:1,
|
ns:1,
|
||||||
v6:1;
|
v6:1;
|
||||||
uint32_t port:16;
|
uint32_t port:16;
|
||||||
} udp;
|
};
|
||||||
uint32_t u32;
|
uint32_t u32;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
4
util.c
4
util.c
|
@ -102,7 +102,7 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
|
||||||
const void *bind_addr, const char *ifname, uint16_t port,
|
const void *bind_addr, const char *ifname, uint16_t port,
|
||||||
uint32_t data)
|
uint32_t data)
|
||||||
{
|
{
|
||||||
union epoll_ref ref = { .r.proto = proto, .r.p.data = data };
|
union epoll_ref ref = { .proto = proto, .data = data };
|
||||||
struct sockaddr_in addr4 = {
|
struct sockaddr_in addr4 = {
|
||||||
.sin_family = AF_INET,
|
.sin_family = AF_INET,
|
||||||
.sin_port = htons(port),
|
.sin_port = htons(port),
|
||||||
|
@ -145,7 +145,7 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
}
|
}
|
||||||
|
|
||||||
ref.r.s = fd;
|
ref.s = fd;
|
||||||
|
|
||||||
if (af == AF_INET) {
|
if (af == AF_INET) {
|
||||||
if (bind_addr)
|
if (bind_addr)
|
||||||
|
|
Loading…
Reference in a new issue