epoll: Generalize epoll_ref to cover things other than sockets
The epoll_ref type includes fields for the IP protocol of a socket, and the socket fd. However, we already have a few things in the epoll which aren't protocol sockets, and we may have more in future. Rename these fields to an abstract "fd type" and file descriptor for more generality. Similarly, rather than using existing IP protocol numbers for the type, introduce our own number space. For now these just correspond to the supported protocols, but we'll expand on that in future. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
e26282b67d
commit
3401644453
8 changed files with 86 additions and 56 deletions
6
icmp.c
6
icmp.c
|
@ -79,7 +79,7 @@ void icmp_sock_handler(const struct ctx *c, union epoll_ref ref,
|
||||||
(void)events;
|
(void)events;
|
||||||
(void)now;
|
(void)now;
|
||||||
|
|
||||||
n = recvfrom(ref.s, buf, sizeof(buf), 0, (struct sockaddr *)&sr, &sl);
|
n = recvfrom(ref.fd, buf, sizeof(buf), 0, (struct sockaddr *)&sr, &sl);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -182,7 +182,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
bind_if, id, iref.u32);
|
bind_if, id, iref.u32);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
goto fail_sock;
|
goto fail_sock;
|
||||||
if (s > SOCKET_MAX) {
|
if (s > FD_REF_MAX) {
|
||||||
close(s);
|
close(s);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ int icmp_tap_handler(const struct ctx *c, int af, const void *addr,
|
||||||
bind_if, id, iref.u32);
|
bind_if, id, iref.u32);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
goto fail_sock;
|
goto fail_sock;
|
||||||
if (s > SOCKET_MAX) {
|
if (s > FD_REF_MAX) {
|
||||||
close(s);
|
close(s);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
21
passt.c
21
passt.c
|
@ -55,12 +55,11 @@
|
||||||
|
|
||||||
char pkt_buf[PKT_BUF_BYTES] __attribute__ ((aligned(PAGE_SIZE)));
|
char pkt_buf[PKT_BUF_BYTES] __attribute__ ((aligned(PAGE_SIZE)));
|
||||||
|
|
||||||
char *ip_proto_str[IPPROTO_SCTP + 1] = {
|
char *epoll_type_str[EPOLL_TYPE_MAX + 1] = {
|
||||||
[IPPROTO_ICMP] = "ICMP",
|
[EPOLL_TYPE_TCP] = "TCP socket",
|
||||||
[IPPROTO_TCP] = "TCP",
|
[EPOLL_TYPE_UDP] = "UDP socket",
|
||||||
[IPPROTO_UDP] = "UDP",
|
[EPOLL_TYPE_ICMP] = "ICMP socket",
|
||||||
[IPPROTO_ICMPV6] = "ICMPV6",
|
[EPOLL_TYPE_ICMPV6] = "ICMPv6 socket",
|
||||||
[IPPROTO_SCTP] = "SCTP",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,16 +72,16 @@ char *ip_proto_str[IPPROTO_SCTP + 1] = {
|
||||||
static void sock_handler(struct ctx *c, union epoll_ref ref,
|
static void sock_handler(struct ctx *c, union epoll_ref ref,
|
||||||
uint32_t events, const struct timespec *now)
|
uint32_t events, const struct timespec *now)
|
||||||
{
|
{
|
||||||
trace("%s: %s packet from socket %i (events: 0x%08x)",
|
trace("%s: packet from %s %i (events: 0x%08x)",
|
||||||
c->mode == MODE_PASST ? "passt" : "pasta",
|
c->mode == MODE_PASST ? "passt" : "pasta",
|
||||||
IP_PROTO_STR(ref.proto), ref.s, events);
|
EPOLL_TYPE_STR(ref.type), ref.fd, events);
|
||||||
|
|
||||||
if (!c->no_tcp && ref.proto == IPPROTO_TCP)
|
if (!c->no_tcp && ref.type == EPOLL_TYPE_TCP)
|
||||||
tcp_sock_handler(c, ref, events, now);
|
tcp_sock_handler(c, ref, events, now);
|
||||||
else if (!c->no_udp && ref.proto == IPPROTO_UDP)
|
else if (!c->no_udp && ref.type == EPOLL_TYPE_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.proto == IPPROTO_ICMP || ref.proto == IPPROTO_ICMPV6))
|
(ref.type == EPOLL_TYPE_ICMP || ref.type == EPOLL_TYPE_ICMPV6))
|
||||||
icmp_sock_handler(c, ref, events, now);
|
icmp_sock_handler(c, ref, events, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
40
passt.h
40
passt.h
|
@ -42,9 +42,27 @@ union epoll_ref;
|
||||||
#include "udp.h"
|
#include "udp.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* union epoll_ref - Breakdown of reference for epoll socket bookkeeping
|
* enum epoll_type - Different types of fds we poll over
|
||||||
* @proto: IP protocol number
|
*/
|
||||||
* @s: Socket number (implies 2^24-1 limit on number of descriptors)
|
enum epoll_type {
|
||||||
|
/* Special value to indicate an invalid type */
|
||||||
|
EPOLL_TYPE_NONE = 0,
|
||||||
|
/* Sockets and timerfds for TCP handling */
|
||||||
|
EPOLL_TYPE_TCP,
|
||||||
|
/* UDP sockets */
|
||||||
|
EPOLL_TYPE_UDP,
|
||||||
|
/* IPv4 ICMP sockets */
|
||||||
|
EPOLL_TYPE_ICMP,
|
||||||
|
/* ICMPv6 sockets */
|
||||||
|
EPOLL_TYPE_ICMPV6,
|
||||||
|
|
||||||
|
EPOLL_TYPE_MAX = EPOLL_TYPE_ICMPV6,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* union epoll_ref - Breakdown of reference for epoll fd bookkeeping
|
||||||
|
* @type: Type of fd (tells us what to do with events)
|
||||||
|
* @fd: File descriptor number (implies < 2^24 total descriptors)
|
||||||
* @tcp: TCP-specific reference part
|
* @tcp: TCP-specific reference part
|
||||||
* @udp: UDP-specific reference part
|
* @udp: UDP-specific reference part
|
||||||
* @icmp: ICMP-specific reference part
|
* @icmp: ICMP-specific reference part
|
||||||
|
@ -53,10 +71,10 @@ union epoll_ref;
|
||||||
*/
|
*/
|
||||||
union epoll_ref {
|
union epoll_ref {
|
||||||
struct {
|
struct {
|
||||||
int32_t proto:8,
|
enum epoll_type type:8;
|
||||||
#define SOCKET_REF_BITS 24
|
#define FD_REF_BITS 24
|
||||||
#define SOCKET_MAX MAX_FROM_BITS(SOCKET_REF_BITS)
|
#define FD_REF_MAX MAX_FROM_BITS(FD_REF_BITS)
|
||||||
s:SOCKET_REF_BITS;
|
int32_t fd:FD_REF_BITS;
|
||||||
union {
|
union {
|
||||||
union tcp_epoll_ref tcp;
|
union tcp_epoll_ref tcp;
|
||||||
union udp_epoll_ref udp;
|
union udp_epoll_ref udp;
|
||||||
|
@ -78,10 +96,10 @@ static_assert(sizeof(union epoll_ref) <= sizeof(union epoll_data),
|
||||||
#define PKT_BUF_BYTES MAX(TAP_BUF_BYTES, 0)
|
#define PKT_BUF_BYTES MAX(TAP_BUF_BYTES, 0)
|
||||||
extern char pkt_buf [PKT_BUF_BYTES];
|
extern char pkt_buf [PKT_BUF_BYTES];
|
||||||
|
|
||||||
extern char *ip_proto_str[];
|
extern char *epoll_type_str[];
|
||||||
#define IP_PROTO_STR(n) \
|
#define EPOLL_TYPE_STR(n) \
|
||||||
(((uint8_t)(n) <= IPPROTO_SCTP && ip_proto_str[(n)]) ? \
|
(((uint8_t)(n) <= EPOLL_TYPE_MAX && epoll_type_str[(n)]) ? \
|
||||||
ip_proto_str[(n)] : "?")
|
epoll_type_str[(n)] : "?")
|
||||||
|
|
||||||
#include <resolv.h> /* For MAXNS below */
|
#include <resolv.h> /* For MAXNS below */
|
||||||
|
|
||||||
|
|
22
tcp.c
22
tcp.c
|
@ -643,7 +643,7 @@ 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 = { .proto = IPPROTO_TCP, .s = conn->sock,
|
union epoll_ref ref = { .type = EPOLL_TYPE_TCP, .fd = conn->sock,
|
||||||
.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 };
|
||||||
|
|
||||||
|
@ -663,8 +663,8 @@ 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 = { .proto = IPPROTO_TCP,
|
union epoll_ref ref_t = { .type = EPOLL_TYPE_TCP,
|
||||||
.s = conn->sock,
|
.fd = conn->sock,
|
||||||
.tcp.timer = 1,
|
.tcp.timer = 1,
|
||||||
.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,
|
||||||
|
@ -692,8 +692,8 @@ 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 = { .proto = IPPROTO_TCP,
|
union epoll_ref ref = { .type = EPOLL_TYPE_TCP,
|
||||||
.s = conn->sock,
|
.fd = conn->sock,
|
||||||
.tcp.timer = 1,
|
.tcp.timer = 1,
|
||||||
.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,
|
||||||
|
@ -701,7 +701,7 @@ static void tcp_timer_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
||||||
if (fd == -1 || fd > SOCKET_MAX) {
|
if (fd == -1 || fd > FD_REF_MAX) {
|
||||||
debug("TCP: failed to get timer: %s", strerror(errno));
|
debug("TCP: failed to get timer: %s", strerror(errno));
|
||||||
if (fd > -1)
|
if (fd > -1)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
@ -1908,7 +1908,7 @@ int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
|
||||||
|
|
||||||
s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
||||||
|
|
||||||
if (s > SOCKET_MAX) {
|
if (s > FD_REF_MAX) {
|
||||||
close(s);
|
close(s);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -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.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
|
s = accept4(ref.fd, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -2948,7 +2948,7 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
|
||||||
conn = tc + ref.tcp.index;
|
conn = tc + ref.tcp.index;
|
||||||
|
|
||||||
if (conn->c.spliced)
|
if (conn->c.spliced)
|
||||||
tcp_splice_sock_handler(c, &conn->splice, ref.s, events);
|
tcp_splice_sock_handler(c, &conn->splice, ref.fd, events);
|
||||||
else
|
else
|
||||||
tcp_tap_sock_handler(c, &conn->tap, events);
|
tcp_tap_sock_handler(c, &conn->tap, events);
|
||||||
}
|
}
|
||||||
|
@ -2999,7 +2999,7 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
|
||||||
int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
||||||
const char *ifname, in_port_t port)
|
const char *ifname, in_port_t port)
|
||||||
{
|
{
|
||||||
int r4 = SOCKET_MAX + 1, r6 = SOCKET_MAX + 1;
|
int r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
|
||||||
|
|
||||||
if (af == AF_UNSPEC && c->ifi4 && c->ifi6)
|
if (af == AF_UNSPEC && c->ifi4 && c->ifi6)
|
||||||
/* Attempt to get a dual stack socket */
|
/* Attempt to get a dual stack socket */
|
||||||
|
@ -3013,7 +3013,7 @@ int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
|
||||||
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6)
|
if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6)
|
||||||
r6 = tcp_sock_init_af(c, AF_INET6, port, addr, ifname);
|
r6 = tcp_sock_init_af(c, AF_INET6, port, addr, ifname);
|
||||||
|
|
||||||
if (IN_INTERVAL(0, SOCKET_MAX, r4) || IN_INTERVAL(0, SOCKET_MAX, r6))
|
if (IN_INTERVAL(0, FD_REF_MAX, r4) || IN_INTERVAL(0, FD_REF_MAX, r6))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return r4 < 0 ? r4 : r6;
|
return r4 < 0 ? r4 : r6;
|
||||||
|
|
|
@ -62,7 +62,7 @@ struct tcp_tap_conn {
|
||||||
unsigned int ws_to_tap :TCP_WS_BITS;
|
unsigned int ws_to_tap :TCP_WS_BITS;
|
||||||
|
|
||||||
|
|
||||||
int sock :SOCKET_REF_BITS;
|
int sock :FD_REF_BITS;
|
||||||
|
|
||||||
uint8_t events;
|
uint8_t events;
|
||||||
#define CLOSED 0
|
#define CLOSED 0
|
||||||
|
@ -80,7 +80,7 @@ struct tcp_tap_conn {
|
||||||
(SOCK_ACCEPTED | TAP_SYN_RCVD | ESTABLISHED)
|
(SOCK_ACCEPTED | TAP_SYN_RCVD | ESTABLISHED)
|
||||||
|
|
||||||
|
|
||||||
int timer :SOCKET_REF_BITS;
|
int timer :FD_REF_BITS;
|
||||||
|
|
||||||
uint8_t flags;
|
uint8_t flags;
|
||||||
#define STALLED BIT(0)
|
#define STALLED BIT(0)
|
||||||
|
|
|
@ -173,9 +173,9 @@ 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 = { .proto = IPPROTO_TCP, .s = conn->a,
|
union epoll_ref ref_a = { .type = EPOLL_TYPE_TCP, .fd = conn->a,
|
||||||
.tcp.index = CONN_IDX(conn) };
|
.tcp.index = CONN_IDX(conn) };
|
||||||
union epoll_ref ref_b = { .proto = IPPROTO_TCP, .s = conn->b,
|
union epoll_ref ref_b = { .type = EPOLL_TYPE_TCP, .fd = conn->b,
|
||||||
.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 };
|
||||||
|
|
12
udp.c
12
udp.c
|
@ -388,7 +388,7 @@ 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 = { .proto = IPPROTO_UDP,
|
union epoll_ref ref = { .type = EPOLL_TYPE_UDP,
|
||||||
.udp = { .splice = true, .ns = ns,
|
.udp = { .splice = true, .ns = ns,
|
||||||
.v6 = v6, .port = src }
|
.v6 = v6, .port = src }
|
||||||
};
|
};
|
||||||
|
@ -406,7 +406,7 @@ int udp_splice_new(const struct ctx *c, int v6, in_port_t src, bool ns)
|
||||||
s = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM | SOCK_NONBLOCK,
|
s = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM | SOCK_NONBLOCK,
|
||||||
IPPROTO_UDP);
|
IPPROTO_UDP);
|
||||||
|
|
||||||
if (s > SOCKET_MAX) {
|
if (s > FD_REF_MAX) {
|
||||||
close(s);
|
close(s);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -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.s = s;
|
ref.fd = s;
|
||||||
|
|
||||||
if (v6) {
|
if (v6) {
|
||||||
struct sockaddr_in6 addr6 = {
|
struct sockaddr_in6 addr6 = {
|
||||||
|
@ -767,7 +767,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.s, mmh_recv, n, 0, NULL);
|
n = recvmmsg(ref.fd, mmh_recv, n, 0, NULL);
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -980,7 +980,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
const void *addr, const char *ifname, in_port_t port)
|
const void *addr, const char *ifname, in_port_t port)
|
||||||
{
|
{
|
||||||
union udp_epoll_ref uref = { .u32 = 0 };
|
union udp_epoll_ref uref = { .u32 = 0 };
|
||||||
int s, r4 = SOCKET_MAX + 1, r6 = SOCKET_MAX + 1;
|
int s, r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
|
||||||
|
|
||||||
if (ns) {
|
if (ns) {
|
||||||
uref.port = (in_port_t)(port + c->udp.fwd_out.f.delta[port]);
|
uref.port = (in_port_t)(port + c->udp.fwd_out.f.delta[port]);
|
||||||
|
@ -1030,7 +1030,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IN_INTERVAL(0, SOCKET_MAX, r4) || IN_INTERVAL(0, SOCKET_MAX, r6))
|
if (IN_INTERVAL(0, FD_REF_MAX, r4) || IN_INTERVAL(0, FD_REF_MAX, r6))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return r4 < 0 ? r4 : r6;
|
return r4 < 0 ? r4 : r6;
|
||||||
|
|
27
util.c
27
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 = { .proto = proto, .data = data };
|
union epoll_ref ref = { .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),
|
||||||
|
@ -118,9 +118,22 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
|
||||||
int fd, sl, y = 1, ret;
|
int fd, sl, y = 1, ret;
|
||||||
struct epoll_event ev;
|
struct epoll_event ev;
|
||||||
|
|
||||||
if (proto != IPPROTO_TCP && proto != IPPROTO_UDP &&
|
switch (proto) {
|
||||||
proto != IPPROTO_ICMP && proto != IPPROTO_ICMPV6)
|
case IPPROTO_TCP:
|
||||||
|
ref.type = EPOLL_TYPE_TCP;
|
||||||
|
break;
|
||||||
|
case IPPROTO_UDP:
|
||||||
|
ref.type = EPOLL_TYPE_UDP;
|
||||||
|
break;
|
||||||
|
case IPPROTO_ICMP:
|
||||||
|
ref.type = EPOLL_TYPE_ICMP;
|
||||||
|
break;
|
||||||
|
case IPPROTO_ICMPV6:
|
||||||
|
ref.type = EPOLL_TYPE_ICMPV6;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
return -EPFNOSUPPORT; /* Not implemented. */
|
return -EPFNOSUPPORT; /* Not implemented. */
|
||||||
|
}
|
||||||
|
|
||||||
if (af == AF_UNSPEC) {
|
if (af == AF_UNSPEC) {
|
||||||
if (!DUAL_STACK_SOCKETS || bind_addr)
|
if (!DUAL_STACK_SOCKETS || bind_addr)
|
||||||
|
@ -140,12 +153,12 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd > SOCKET_MAX) {
|
if (fd > FD_REF_MAX) {
|
||||||
close(fd);
|
close(fd);
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
}
|
}
|
||||||
|
|
||||||
ref.s = fd;
|
ref.fd = fd;
|
||||||
|
|
||||||
if (af == AF_INET) {
|
if (af == AF_INET) {
|
||||||
if (bind_addr)
|
if (bind_addr)
|
||||||
|
@ -188,8 +201,8 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
|
||||||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
||||||
ifname, strlen(ifname))) {
|
ifname, strlen(ifname))) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
warn("Can't bind socket for %s port %u to %s, closing",
|
warn("Can't bind %s socket for port %u to %s, closing",
|
||||||
ip_proto_str[proto], port, ifname);
|
EPOLL_TYPE_STR(proto), port, ifname);
|
||||||
close(fd);
|
close(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue