blob: 82a93873ec1cbc3986631ed3bb02b8aa8ab463ed [file] [log] [blame]
Emeric Brun3835c0d2020-07-07 09:46:09 +02001/*
2 * AF_CUST_UDP/AF_CUST_UDP6 UDP protocol layer
3 *
4 * Copyright 2019 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * Partial merge by Emeric Brun <ebrun@haproxy.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14
15#include <ctype.h>
16#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
23#include <sys/param.h>
24#include <sys/socket.h>
25#include <sys/types.h>
26
27#include <netinet/udp.h>
28#include <netinet/in.h>
29
30#include <haproxy/fd.h>
31#include <haproxy/listener.h>
32#include <haproxy/log.h>
33#include <haproxy/namespace.h>
34#include <haproxy/port_range.h>
35#include <haproxy/protocol.h>
36#include <haproxy/proto_udp.h>
37#include <haproxy/proxy.h>
38#include <haproxy/server.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020039#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020040#include <haproxy/sock_inet.h>
Emeric Brun3835c0d2020-07-07 09:46:09 +020041#include <haproxy/task.h>
42
Emeric Brun3835c0d2020-07-07 09:46:09 +020043static int udp_bind_listener(struct listener *listener, char *errmsg, int errlen);
44static void udp4_add_listener(struct listener *listener, int port);
45static void udp6_add_listener(struct listener *listener, int port);
46
47/* Note: must not be declared <const> as its list will be overwritten */
48static struct protocol proto_udp4 = {
49 .name = "udp4",
50 .sock_domain = AF_CUST_UDP4,
51 .sock_type = SOCK_DGRAM,
52 .sock_prot = IPPROTO_UDP,
53 .sock_family = AF_INET,
54 .sock_addrlen = sizeof(struct sockaddr_in),
55 .l3_addrlen = 32/8,
56 .accept = NULL,
57 .connect = NULL,
58 .bind = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020059 .enable_all = enable_all_listeners,
60 .get_src = udp_get_src,
61 .get_dst = udp_get_dst,
62 .pause = udp_pause_listener,
63 .add = udp4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020064 .addrcmp = sock_inet4_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020065 .listeners = LIST_HEAD_INIT(proto_udp4.listeners),
66 .nb_listeners = 0,
67};
68
69INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
70
71/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_udp6 = {
73 .name = "udp6",
74 .sock_domain = AF_CUST_UDP6,
75 .sock_type = SOCK_DGRAM,
76 .sock_prot = IPPROTO_UDP,
77 .sock_family = AF_INET6,
78 .sock_addrlen = sizeof(struct sockaddr_in6),
79 .l3_addrlen = 128/8,
80 .accept = NULL,
81 .connect = NULL,
82 .bind = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020083 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020084 .get_src = udp6_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020085 .get_dst = udp6_get_dst,
Emeric Brun3835c0d2020-07-07 09:46:09 +020086 .pause = udp_pause_listener,
87 .add = udp6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020088 .addrcmp = sock_inet6_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020089 .listeners = LIST_HEAD_INIT(proto_udp6.listeners),
90 .nb_listeners = 0,
91};
92
93INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
94
95/*
96 * Retrieves the source address for the socket <fd>, with <dir> indicating
97 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
98 * success, -1 in case of error. The socket's source address is stored in
99 * <sa> for <salen> bytes.
100 */
101int udp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
102{
103 int ret;
104
Willy Tarreau18b7df72020-08-28 12:07:22 +0200105 ret = sock_get_src(fd, sa, salen, dir);
106 if (!ret)
107 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200108
109 return ret;
110}
111
Willy Tarreau18b7df72020-08-28 12:07:22 +0200112/*
113 * Retrieves the source address for the socket <fd>, with <dir> indicating
114 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
115 * success, -1 in case of error. The socket's source address is stored in
116 * <sa> for <salen> bytes.
117 */
118int udp6_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
119{
120 int ret;
121
122 ret = sock_get_src(fd, sa, salen, dir);
123 if (!ret)
124 sa->sa_family = AF_CUST_UDP6;
125
126 return ret;
127}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200128
129/*
130 * Retrieves the original destination address for the socket <fd>, with <dir>
131 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
132 * listener, if the original destination address was translated, the original
133 * address is retrieved. It returns 0 in case of success, -1 in case of error.
134 * The socket's source address is stored in <sa> for <salen> bytes.
135 */
136int udp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
137{
138 int ret;
139
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200140 ret = sock_inet_get_dst(fd, sa, salen, dir);
141 if (!ret)
142 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200143
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200144 return ret;
145}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200146
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200147/*
148 * Retrieves the original destination address for the socket <fd>, with <dir>
149 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
150 * listener, if the original destination address was translated, the original
151 * address is retrieved. It returns 0 in case of success, -1 in case of error.
152 * The socket's source address is stored in <sa> for <salen> bytes.
153 */
154int udp6_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
155{
156 int ret;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200157
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200158 ret = sock_get_dst(fd, sa, salen, dir);
159 if (!ret)
160 sa->sa_family = AF_CUST_UDP6;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200161
162 return ret;
163}
164
165/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
166 * an error message in <errmsg> if the message is at most <errlen> bytes long
167 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
168 * The return value is composed from ERR_ABORT, ERR_WARN,
169 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
170 * was alright and that no message was returned. ERR_RETRYABLE means that an
171 * error occurred but that it may vanish after a retry (eg: port in use), and
172 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
173 * the meaning of the error, but just indicate that a message is present which
174 * should be displayed with the respective level. Last, ERR_ABORT indicates
175 * that it's pointless to try to start other listeners. No error message is
176 * returned if errlen is NULL.
177 */
178int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
179{
180 __label__ udp_return, udp_close_return;
181 int fd, err;
182 const char *msg = NULL;
183 /* copy listener addr because sometimes we need to switch family */
Willy Tarreau37159062020-08-27 07:48:42 +0200184 struct sockaddr_storage addr_inet = listener->rx.addr;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200185
186 /* force to classic sock family */
Willy Tarreaub7436612020-08-28 19:51:44 +0200187 addr_inet.ss_family = listener->rx.proto->sock_family;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200188
189 /* ensure we never return garbage */
190 if (errlen)
191 *errmsg = 0;
192
193 if (listener->state != LI_ASSIGNED)
194 return ERR_NONE; /* already bound */
195
196 err = ERR_NONE;
197
Willy Tarreau0b915012020-09-01 10:47:07 +0200198 if (listener->rx.flags & RX_F_BOUND)
199 goto bound;
200
Emeric Brun3835c0d2020-07-07 09:46:09 +0200201 /* TODO: Implement reuse fd. Take care that to identify fd to reuse
202 * listeners uses a special AF_CUST_ family and we MUST consider
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500203 * IPPROTO (sockaddr is not enough)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200204 */
205
Willy Tarreau818a92e2020-09-03 07:50:19 +0200206 fd = my_socketat(listener->rx.settings->netns,
Willy Tarreaub7436612020-08-28 19:51:44 +0200207 listener->rx.proto->sock_family,
208 listener->rx.proto->sock_type,
209 listener->rx.proto->sock_prot);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200210 if (fd == -1) {
211 err |= ERR_RETRYABLE | ERR_ALERT;
212 msg = "cannot create listening socket";
213 goto udp_return;
214 }
215
216 if (fd >= global.maxsock) {
217 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
218 msg = "not enough free sockets (raise '-n' parameter)";
219 goto udp_close_return;
220 }
221
222 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
223 err |= ERR_FATAL | ERR_ALERT;
224 msg = "cannot make socket non-blocking";
225 goto udp_close_return;
226 }
227
228 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
229 /* not fatal but should be reported */
230 msg = "cannot do so_reuseaddr";
231 err |= ERR_ALERT;
232 }
233
234#ifdef SO_REUSEPORT
235 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
236 * Linux, it might return an error that we will silently ignore.
237 */
238 if (global.tune.options & GTUNE_USE_REUSEPORT)
239 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
240#endif
241
Willy Tarreau3fd3bdc2020-09-01 15:12:08 +0200242 if (listener->rx.settings->options & RX_O_FOREIGN) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200243 switch (addr_inet.ss_family) {
244 case AF_INET:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200245 if (!sock_inet4_make_foreign(fd)) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200246 msg = "cannot make listening socket transparent";
247 err |= ERR_ALERT;
248 }
249 break;
250 case AF_INET6:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200251 if (!sock_inet6_make_foreign(fd)) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200252 msg = "cannot make listening socket transparent";
253 err |= ERR_ALERT;
254 }
255 break;
256 }
257 }
258
259#ifdef SO_BINDTODEVICE
260 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau818a92e2020-09-03 07:50:19 +0200261 if (listener->rx.settings->interface) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200262 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200263 listener->rx.settings->interface,
264 strlen(listener->rx.settings->interface) + 1) == -1) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200265 msg = "cannot bind listener to device";
266 err |= ERR_WARN;
267 }
268 }
269#endif
270#if defined(IPV6_V6ONLY)
Willy Tarreau3fd3bdc2020-09-01 15:12:08 +0200271 if (listener->rx.addr.ss_family == AF_INET6) {
272 /* Prepare to match the v6only option against what we really want. Note
273 * that sadly the two options are not exclusive to each other and that
274 * v6only is stronger than v4v6.
275 */
276 if ((listener->rx.settings->options & RX_O_V6ONLY) ||
277 (sock_inet6_v6only_default && !(listener->rx.settings->options & RX_O_V4V6)))
278 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
279 else
280 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
281 }
Emeric Brun3835c0d2020-07-07 09:46:09 +0200282#endif
283
Willy Tarreaub7436612020-08-28 19:51:44 +0200284 if (bind(fd, (struct sockaddr *)&addr_inet, listener->rx.proto->sock_addrlen) < 0) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200285 err |= ERR_RETRYABLE | ERR_ALERT;
286 msg = "cannot bind socket";
287 goto udp_close_return;
288 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200289 listener->rx.flags |= RX_F_BOUND;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200290
Willy Tarreau0b915012020-09-01 10:47:07 +0200291 bound:
Emeric Brun3835c0d2020-07-07 09:46:09 +0200292 /* the socket is ready */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200293 listener->rx.fd = fd;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200294 listener->state = LI_LISTEN;
295
Emeric Brun54932b42020-07-07 09:43:24 +0200296 if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG)
297 fd_insert(fd, listener, syslog_fd_handler,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200298 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
Emeric Brun54932b42020-07-07 09:43:24 +0200299 else {
300 err |= ERR_FATAL | ERR_ALERT;
301 msg = "UDP is not yet supported on this proxy mode";
302 goto udp_close_return;
303 }
Emeric Brun3835c0d2020-07-07 09:46:09 +0200304
305 udp_return:
306 if (msg && errlen) {
307 char pn[INET6_ADDRSTRLEN];
308
309 addr_to_str(&addr_inet, pn, sizeof(pn));
310 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&addr_inet));
311 }
312 return err;
313
314 udp_close_return:
315 close(fd);
316 goto udp_return;
317}
318
Emeric Brun3835c0d2020-07-07 09:46:09 +0200319/* Add <listener> to the list of udp4 listeners, on port <port>. The
320 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
321 * The number of listeners for the protocol is updated.
322 */
323static void udp4_add_listener(struct listener *listener, int port)
324{
325 if (listener->state != LI_INIT)
326 return;
327 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200328 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200329 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200330 LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200331 proto_udp4.nb_listeners++;
332}
333
334/* Add <listener> to the list of udp6 listeners, on port <port>. The
335 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
336 * The number of listeners for the protocol is updated.
337 */
338static void udp6_add_listener(struct listener *listener, int port)
339{
340 if (listener->state != LI_INIT)
341 return;
342 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200343 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200344 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200345 LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200346 proto_udp6.nb_listeners++;
347}
348
349/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
350 * was totally stopped, or > 0 if correctly paused.
351 */
352int udp_pause_listener(struct listener *l)
353{
354 /* we don't support pausing on UDP */
355 return -1;
356}
357
358/*
359 * Local variables:
360 * c-indent-level: 8
361 * c-basic-offset: 8
362 * End:
363 */