aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2021-02-21 11:33:38 +0100
committerStefano Brivio <sbrivio@redhat.com>2021-02-21 11:55:49 +0100
commit8bca388e8a771d069b2a2d4ac47589112f6f0af3 (patch)
tree6e934d87ab33ef3116da032c3f5acb044c0cc6cc /util.c
parent105b916361ca6e9e63112444c323cc193303120c (diff)
downloadpasst-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar.gz
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar.bz2
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar.lz
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar.xz
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.tar.zst
passt-8bca388e8a771d069b2a2d4ac47589112f6f0af3.zip
passt: Assorted fixes from "fresh eyes" review
A bunch of fixes not worth single commits at this stage, notably: - make buffer, length parameter ordering consistent in ARP, DHCP, NDP handlers - strict checking of buffer, message and option length in DHCP handler (a malicious client could have easily crashed it) - set up forwarding for IPv4 and IPv6, and masquerading with nft for IPv4, from demo script - get rid of separate slow and fast timers, we don't save any overhead that way - stricter checking of buffer lengths as passed to tap handlers - proper dequeuing from qemu socket back-end: I accidentally trashed messages that were bundled up together in a single tap read operation -- the length header tells us what's the size of the next frame, but there's no apparent limit to the number of messages we get with one single receive - rework some bits of the TCP state machine, now passive and active connection closes appear to be robust -- introduce a new FIN_WAIT_1_SOCK_FIN state indicating a FIN_WAIT_1 with a FIN flag from socket - streamline TCP option parsing routine - track TCP state changes to stderr (this is temporary, proper debugging and syslogging support pending) - observe that multiplying a number by four might very well change its value, and this happens to be the case for the data offset from the TCP header as we check if it's the same as the total length to find out if it's a duplicated ACK segment - recent estimates suggest that the duration of a millisecond is closer to a million nanoseconds than a thousand of them, this trend is now reflected into the timespec_diff_ms() convenience routine Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/util.c b/util.c
index 324f800..e8ee57f 100644
--- a/util.c
+++ b/util.c
@@ -139,7 +139,7 @@ char *ipv6_l4hdr(struct ipv6hdr *ip6h, uint8_t *proto)
* sock_l4_add() - Create and bind socket for given L4, add to epoll list
* @c: Execution context
* @v: IP protocol, 4 or 6
- * @proto: Protocol number, network order
+ * @proto: Protocol number, host order
* @port: Port, network order
*
* Return: newly created socket, -1 on error
@@ -148,17 +148,17 @@ int sock_l4_add(struct ctx *c, int v, uint16_t proto, uint16_t port)
{
struct sockaddr_in addr4 = {
.sin_family = AF_INET,
- .sin_port = port,
+ .sin_port = htons(port),
.sin_addr = { .s_addr = INADDR_ANY },
};
struct sockaddr_in6 addr6 = {
.sin6_family = AF_INET6,
- .sin6_port = port,
+ .sin6_port = htons(port),
.sin6_addr = IN6ADDR_ANY_INIT,
};
struct epoll_event ev = { 0 };
const struct sockaddr *sa;
- int fd, sl;
+ int fd, sl, one = 1;
if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
return -1; /* Not implemented. */
@@ -176,6 +176,8 @@ int sock_l4_add(struct ctx *c, int v, uint16_t proto, uint16_t port)
} else {
sa = (const struct sockaddr *)&addr6;
sl = sizeof(addr6);
+
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
}
if (bind(fd, sa, sl) < 0) {
@@ -213,10 +215,10 @@ int sock_l4_add(struct ctx *c, int v, uint16_t proto, uint16_t port)
int timespec_diff_ms(struct timespec *a, struct timespec *b)
{
if (a->tv_nsec < b->tv_nsec) {
- return (b->tv_nsec - a->tv_nsec) / 1000 +
+ return (b->tv_nsec - a->tv_nsec) / 1000000 +
(a->tv_sec - b->tv_sec - 1) * 1000;
}
- return (a->tv_nsec - b->tv_nsec) / 1000 +
+ return (a->tv_nsec - b->tv_nsec) / 1000000 +
(a->tv_sec - b->tv_sec) * 1000;
}