blob: 64ea63931895cc38e808d86973064f0ecb89f533 [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
242 if (listener->options & LI_O_FOREIGN) {
243 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)
271 if (listener->options & LI_O_V6ONLY)
272 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
273 else if (listener->options & LI_O_V4V6)
274 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
275#endif
276
Willy Tarreaub7436612020-08-28 19:51:44 +0200277 if (bind(fd, (struct sockaddr *)&addr_inet, listener->rx.proto->sock_addrlen) < 0) {
Emeric Brun3835c0d2020-07-07 09:46:09 +0200278 err |= ERR_RETRYABLE | ERR_ALERT;
279 msg = "cannot bind socket";
280 goto udp_close_return;
281 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200282 listener->rx.flags |= RX_F_BOUND;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200283
Willy Tarreau0b915012020-09-01 10:47:07 +0200284 bound:
Emeric Brun3835c0d2020-07-07 09:46:09 +0200285 /* the socket is ready */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200286 listener->rx.fd = fd;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200287 listener->state = LI_LISTEN;
288
Emeric Brun54932b42020-07-07 09:43:24 +0200289 if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG)
290 fd_insert(fd, listener, syslog_fd_handler,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200291 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
Emeric Brun54932b42020-07-07 09:43:24 +0200292 else {
293 err |= ERR_FATAL | ERR_ALERT;
294 msg = "UDP is not yet supported on this proxy mode";
295 goto udp_close_return;
296 }
Emeric Brun3835c0d2020-07-07 09:46:09 +0200297
298 udp_return:
299 if (msg && errlen) {
300 char pn[INET6_ADDRSTRLEN];
301
302 addr_to_str(&addr_inet, pn, sizeof(pn));
303 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&addr_inet));
304 }
305 return err;
306
307 udp_close_return:
308 close(fd);
309 goto udp_return;
310}
311
Emeric Brun3835c0d2020-07-07 09:46:09 +0200312/* Add <listener> to the list of udp4 listeners, on port <port>. The
313 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
314 * The number of listeners for the protocol is updated.
315 */
316static void udp4_add_listener(struct listener *listener, int port)
317{
318 if (listener->state != LI_INIT)
319 return;
320 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200321 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200322 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200323 LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200324 proto_udp4.nb_listeners++;
325}
326
327/* Add <listener> to the list of udp6 listeners, on port <port>. The
328 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
329 * The number of listeners for the protocol is updated.
330 */
331static void udp6_add_listener(struct listener *listener, int port)
332{
333 if (listener->state != LI_INIT)
334 return;
335 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200336 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200337 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200338 LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200339 proto_udp6.nb_listeners++;
340}
341
342/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
343 * was totally stopped, or > 0 if correctly paused.
344 */
345int udp_pause_listener(struct listener *l)
346{
347 /* we don't support pausing on UDP */
348 return -1;
349}
350
351/*
352 * Local variables:
353 * c-indent-level: 8
354 * c-basic-offset: 8
355 * End:
356 */