1 From 608aa9fcfca2ffeba40d78c7c4e0dcb50e0d5704 Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Sun, 10 Mar 2019 22:44:15 +0000
4 Subject: [PATCH 44/57] Support TCP fastopen on incoming and outgoing
7 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
11 src/forward.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
12 src/network.c | 5 +++++
13 4 files changed, 51 insertions(+), 10 deletions(-)
17 @@ -26,7 +26,10 @@ version 2.81
18 Thanks to Norman Rasmussen, Sven Mueller and Maciej Żenczykowski
19 for spotting and diagnosing the bug and providing patches.
22 + Support TCP-fastopen (RFC-7413) on both incoming and
23 + outgoing TCP connections, if supported and enabled in the OS.
27 Add support for RFC 4039 DHCP rapid commit. Thanks to Ashram Method
28 for the initial patch and motivation.
31 @@ -128,6 +128,7 @@ typedef unsigned long long u64;
32 #include <netinet/ip.h>
33 #include <netinet/ip6.h>
34 #include <netinet/ip_icmp.h>
35 +#include <netinet/tcp.h>
41 @@ -1635,6 +1635,8 @@ static int tcp_key_recurse(time_t now, i
50 @@ -1667,8 +1669,22 @@ static int tcp_key_recurse(time_t now, i
51 setsockopt(server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
54 - if (!local_bind(server->tcpfd, &server->source_addr, server->interface, 0, 1) ||
55 - connect(server->tcpfd, &server->addr.sa, sa_len(&server->addr)) == -1)
56 + if (!local_bind(server->tcpfd, &server->source_addr, server->interface, 0, 1))
58 + close(server->tcpfd);
60 + continue; /* No good, next server */
64 + while(retry_send(sendto(server->tcpfd, packet, m + sizeof(u16),
65 + MSG_FASTOPEN, &server->addr.sa, sa_len(&server->addr))));
71 + if (!data_sent && connect(server->tcpfd, &server->addr.sa, sa_len(&server->addr)) == -1)
75 @@ -1678,7 +1694,7 @@ static int tcp_key_recurse(time_t now, i
76 server->flags &= ~SERV_GOT_TCP;
79 - if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) ||
80 + if ((!data_sent && !read_write(server->tcpfd, packet, m + sizeof(u16), 0)) ||
81 !read_write(server->tcpfd, &c1, 1, 1) ||
82 !read_write(server->tcpfd, &c2, 1, 1) ||
83 !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1))
84 @@ -1951,6 +1967,8 @@ unsigned char *tcp_request(int confd, ti
85 which can go to the same server, do so. */
91 firstsendto = last_server;
93 @@ -1969,6 +1987,8 @@ unsigned char *tcp_request(int confd, ti
97 + *length = htons(size);
99 if (last_server->tcpfd == -1)
101 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
102 @@ -1978,10 +1998,24 @@ unsigned char *tcp_request(int confd, ti
103 /* Copy connection mark of incoming query to outgoing connection. */
105 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
109 - if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 0, 1) ||
110 - connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
111 + if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 0, 1)))
113 + close(last_server->tcpfd);
114 + last_server->tcpfd = -1;
119 + while(retry_send(sendto(last_server->tcpfd, packet, size + sizeof(u16),
120 + MSG_FASTOPEN, &last_server->addr.sa, sa_len(&last_server->addr))));
126 + if (!data_sent && connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1)
128 close(last_server->tcpfd);
129 last_server->tcpfd = -1;
130 @@ -1991,13 +2025,11 @@ unsigned char *tcp_request(int confd, ti
131 last_server->flags &= ~SERV_GOT_TCP;
134 - *length = htons(size);
136 /* get query name again for logging - may have been overwritten */
137 if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
138 strcpy(daemon->namebuff, "query");
140 - if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
141 + if ((!data_sent && !read_write(last_server->tcpfd, packet, size + sizeof(u16), 0)) ||
142 !read_write(last_server->tcpfd, &c1, 1, 1) ||
143 !read_write(last_server->tcpfd, &c2, 1, 1) ||
144 !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1))
147 @@ -726,6 +726,11 @@ static int make_sock(union mysockaddr *a
149 if (type == SOCK_STREAM)
153 + setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
156 if (listen(fd, TCP_BACKLOG) == -1)