diff options
author | Stefano Brivio <sbrivio@redhat.com> | 2021-04-25 13:34:04 +0200 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2021-04-29 17:15:26 +0200 |
commit | db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d (patch) | |
tree | 72c07fb27d823acb1f48ca70284a7298c1060d4d /passt.c | |
parent | 48afbe321eddfb68966a4436884c022e64c3e166 (diff) | |
download | passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar.gz passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar.bz2 passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar.lz passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar.xz passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.tar.zst passt-db1fe773a3eacbf2b3ce0b3333f1684291fa5c2d.zip |
tcp: Avoid SO_ACCEPTCONN getsockopt() by noting listening/data sockets numbers
...the rest is reshuffling existing macros to use the bits we need in
TCP code.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'passt.c')
-rw-r--r-- | passt.c | 51 |
1 files changed, 25 insertions, 26 deletions
@@ -613,40 +613,39 @@ static int tap_handler(struct ctx *c) /** * sock_handler() - Event handler for L4 sockets * @c: Execution context - * @fd: File descriptor associated to event + * @s: Socket associated to event * @events epoll events */ -static void sock_handler(struct ctx *c, int fd, uint32_t events) +static void sock_handler(struct ctx *c, int s, uint32_t events) { socklen_t sl; - int so; - - sl = sizeof(so); - -#define IN(x, proto) (x >= c->proto.fd_min && x <= c->proto.fd_max) - - if (IN(fd, udp) && !IN(fd, icmp) && !IN(fd, tcp)) - so = IPPROTO_UDP; - else if (IN(fd, tcp) && !IN(fd, icmp) && !IN(fd, udp)) - so = IPPROTO_TCP; - else if (IN(fd, icmp) && !IN(fd, udp) && !IN(fd, tcp)) - so = IPPROTO_ICMP; /* Fits ICMPv6 below, too */ - else if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so, &sl)) { - epoll_ctl(c->epollfd, EPOLL_CTL_DEL, fd, NULL); - close(fd); + int proto; + + sl = sizeof(proto); + + if ( FD_PROTO(s, udp) && !FD_PROTO(s, icmp) && !FD_PROTO(s, tcp)) + proto = IPPROTO_UDP; + else if (FD_PROTO(s, tcp) && !FD_PROTO(s, icmp) && !FD_PROTO(s, udp)) + proto = IPPROTO_TCP; + else if (FD_PROTO(s, icmp) && !FD_PROTO(s, udp) && !FD_PROTO(s, tcp)) + proto = IPPROTO_ICMP; /* Fits ICMPv6 below, too */ + else if (getsockopt(s, SOL_SOCKET, SO_PROTOCOL, &proto, &sl)) + proto = -1; + + if (proto == -1) { + epoll_ctl(c->epollfd, EPOLL_CTL_DEL, s, NULL); + close(s); return; } -#undef IN - - debug("%s: packet from socket %i", getprotobynumber(so)->p_name, fd); + debug("%s: packet from socket %i", getprotobynumber(proto)->p_name, s); - if (so == IPPROTO_ICMP || so == IPPROTO_ICMPV6) - icmp_sock_handler(c, fd, events); - else if (so == IPPROTO_TCP) - tcp_sock_handler(c, fd, events); - else if (so == IPPROTO_UDP) - udp_sock_handler(c, fd, events); + if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) + icmp_sock_handler(c, s, events); + else if (proto == IPPROTO_TCP) + tcp_sock_handler(c, s, events); + else if (proto == IPPROTO_UDP) + udp_sock_handler(c, s, events); } /** |