blob: 96b878a345528bfe2896d14ad28d180e6261c9e6 [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
43static int udp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
44static int udp_bind_listener(struct listener *listener, char *errmsg, int errlen);
45static void udp4_add_listener(struct listener *listener, int port);
46static void udp6_add_listener(struct listener *listener, int port);
47
48/* Note: must not be declared <const> as its list will be overwritten */
49static struct protocol proto_udp4 = {
50 .name = "udp4",
51 .sock_domain = AF_CUST_UDP4,
52 .sock_type = SOCK_DGRAM,
53 .sock_prot = IPPROTO_UDP,
54 .sock_family = AF_INET,
55 .sock_addrlen = sizeof(struct sockaddr_in),
56 .l3_addrlen = 32/8,
57 .accept = NULL,
58 .connect = NULL,
59 .bind = udp_bind_listener,
60 .bind_all = udp_bind_listeners,
61 .unbind_all = unbind_all_listeners,
62 .enable_all = enable_all_listeners,
63 .get_src = udp_get_src,
64 .get_dst = udp_get_dst,
65 .pause = udp_pause_listener,
66 .add = udp4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020067 .addrcmp = sock_inet4_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020068 .listeners = LIST_HEAD_INIT(proto_udp4.listeners),
69 .nb_listeners = 0,
70};
71
72INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
73
74/* Note: must not be declared <const> as its list will be overwritten */
75static struct protocol proto_udp6 = {
76 .name = "udp6",
77 .sock_domain = AF_CUST_UDP6,
78 .sock_type = SOCK_DGRAM,
79 .sock_prot = IPPROTO_UDP,
80 .sock_family = AF_INET6,
81 .sock_addrlen = sizeof(struct sockaddr_in6),
82 .l3_addrlen = 128/8,
83 .accept = NULL,
84 .connect = NULL,
85 .bind = udp_bind_listener,
86 .bind_all = udp_bind_listeners,
87 .unbind_all = unbind_all_listeners,
88 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020089 .get_src = udp6_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020090 .get_dst = udp6_get_dst,
Emeric Brun3835c0d2020-07-07 09:46:09 +020091 .pause = udp_pause_listener,
92 .add = udp6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020093 .addrcmp = sock_inet6_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020094 .listeners = LIST_HEAD_INIT(proto_udp6.listeners),
95 .nb_listeners = 0,
96};
97
98INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
99
100/*
101 * Retrieves the source address for the socket <fd>, with <dir> indicating
102 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
103 * success, -1 in case of error. The socket's source address is stored in
104 * <sa> for <salen> bytes.
105 */
106int udp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
107{
108 int ret;
109
Willy Tarreau18b7df72020-08-28 12:07:22 +0200110 ret = sock_get_src(fd, sa, salen, dir);
111 if (!ret)
112 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200113
114 return ret;
115}
116
Willy Tarreau18b7df72020-08-28 12:07:22 +0200117/*
118 * Retrieves the source address for the socket <fd>, with <dir> indicating
119 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
120 * success, -1 in case of error. The socket's source address is stored in
121 * <sa> for <salen> bytes.
122 */
123int udp6_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
124{
125 int ret;
126
127 ret = sock_get_src(fd, sa, salen, dir);
128 if (!ret)
129 sa->sa_family = AF_CUST_UDP6;
130
131 return ret;
132}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200133
134/*
135 * Retrieves the original destination address for the socket <fd>, with <dir>
136 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
137 * listener, if the original destination address was translated, the original
138 * address is retrieved. It returns 0 in case of success, -1 in case of error.
139 * The socket's source address is stored in <sa> for <salen> bytes.
140 */
141int udp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
142{
143 int ret;
144
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200145 ret = sock_inet_get_dst(fd, sa, salen, dir);
146 if (!ret)
147 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200148
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200149 return ret;
150}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200151
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200152/*
153 * Retrieves the original destination address for the socket <fd>, with <dir>
154 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
155 * listener, if the original destination address was translated, the original
156 * address is retrieved. It returns 0 in case of success, -1 in case of error.
157 * The socket's source address is stored in <sa> for <salen> bytes.
158 */
159int udp6_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
160{
161 int ret;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200162
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200163 ret = sock_get_dst(fd, sa, salen, dir);
164 if (!ret)
165 sa->sa_family = AF_CUST_UDP6;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200166
167 return ret;
168}
169
170/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
171 * an error message in <errmsg> if the message is at most <errlen> bytes long
172 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
173 * The return value is composed from ERR_ABORT, ERR_WARN,
174 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
175 * was alright and that no message was returned. ERR_RETRYABLE means that an
176 * error occurred but that it may vanish after a retry (eg: port in use), and
177 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
178 * the meaning of the error, but just indicate that a message is present which
179 * should be displayed with the respective level. Last, ERR_ABORT indicates
180 * that it's pointless to try to start other listeners. No error message is
181 * returned if errlen is NULL.
182 */
183int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
184{
185 __label__ udp_return, udp_close_return;
186 int fd, err;
187 const char *msg = NULL;
188 /* copy listener addr because sometimes we need to switch family */
189 struct sockaddr_storage addr_inet = listener->addr;
190
191 /* force to classic sock family */
192 addr_inet.ss_family = listener->proto->sock_family;
193
194 /* ensure we never return garbage */
195 if (errlen)
196 *errmsg = 0;
197
198 if (listener->state != LI_ASSIGNED)
199 return ERR_NONE; /* already bound */
200
201 err = ERR_NONE;
202
203 /* TODO: Implement reuse fd. Take care that to identify fd to reuse
204 * listeners uses a special AF_CUST_ family and we MUST consider
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500205 * IPPROTO (sockaddr is not enough)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200206 */
207
208 fd = my_socketat(listener->netns, listener->proto->sock_family, listener->proto->sock_type, listener->proto->sock_prot);
209 if (fd == -1) {
210 err |= ERR_RETRYABLE | ERR_ALERT;
211 msg = "cannot create listening socket";
212 goto udp_return;
213 }
214
215 if (fd >= global.maxsock) {
216 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
217 msg = "not enough free sockets (raise '-n' parameter)";
218 goto udp_close_return;
219 }
220
221 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
222 err |= ERR_FATAL | ERR_ALERT;
223 msg = "cannot make socket non-blocking";
224 goto udp_close_return;
225 }
226
227 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
228 /* not fatal but should be reported */
229 msg = "cannot do so_reuseaddr";
230 err |= ERR_ALERT;
231 }
232
233#ifdef SO_REUSEPORT
234 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
235 * Linux, it might return an error that we will silently ignore.
236 */
237 if (global.tune.options & GTUNE_USE_REUSEPORT)
238 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
239#endif
240
241 if (listener->options & LI_O_FOREIGN) {
242 switch (addr_inet.ss_family) {
243 case AF_INET:
244 if (1
245#if defined(IP_TRANSPARENT)
246 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
247#endif
248#if defined(IP_FREEBIND)
249 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
250#endif
251#if defined(IP_BINDANY)
252 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
253#endif
254#if defined(SO_BINDANY)
255 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
256#endif
257 ) {
258 msg = "cannot make listening socket transparent";
259 err |= ERR_ALERT;
260 }
261 break;
262 case AF_INET6:
263 if (1
264#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
265 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
266#endif
267#if defined(IP_FREEBIND)
268 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
269#endif
270#if defined(IPV6_BINDANY)
271 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
272#endif
273#if defined(SO_BINDANY)
274 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
275#endif
276 ) {
277 msg = "cannot make listening socket transparent";
278 err |= ERR_ALERT;
279 }
280 break;
281 }
282 }
283
284#ifdef SO_BINDTODEVICE
285 /* Note: this might fail if not CAP_NET_RAW */
286 if (listener->interface) {
287 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
288 listener->interface, strlen(listener->interface) + 1) == -1) {
289 msg = "cannot bind listener to device";
290 err |= ERR_WARN;
291 }
292 }
293#endif
294#if defined(IPV6_V6ONLY)
295 if (listener->options & LI_O_V6ONLY)
296 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
297 else if (listener->options & LI_O_V4V6)
298 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
299#endif
300
301 if (bind(fd, (struct sockaddr *)&addr_inet, listener->proto->sock_addrlen) < 0) {
302 err |= ERR_RETRYABLE | ERR_ALERT;
303 msg = "cannot bind socket";
304 goto udp_close_return;
305 }
306
307 /* the socket is ready */
308 listener->fd = fd;
309 listener->state = LI_LISTEN;
310
Emeric Brun54932b42020-07-07 09:43:24 +0200311 if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG)
312 fd_insert(fd, listener, syslog_fd_handler,
313 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
314 else {
315 err |= ERR_FATAL | ERR_ALERT;
316 msg = "UDP is not yet supported on this proxy mode";
317 goto udp_close_return;
318 }
Emeric Brun3835c0d2020-07-07 09:46:09 +0200319
320 udp_return:
321 if (msg && errlen) {
322 char pn[INET6_ADDRSTRLEN];
323
324 addr_to_str(&addr_inet, pn, sizeof(pn));
325 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&addr_inet));
326 }
327 return err;
328
329 udp_close_return:
330 close(fd);
331 goto udp_return;
332}
333
334/* This function creates all UDP sockets bound to the protocol entry <proto>.
335 * It is intended to be used as the protocol's bind_all() function.
336 * The sockets will be registered but not added to any fd_set, in order not to
337 * loose them across the fork(). A call to enable_all_listeners() is needed
338 * to complete initialization. The return value is composed from ERR_*.
339 */
340static int udp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
341{
342 struct listener *listener;
343 int err = ERR_NONE;
344
345 list_for_each_entry(listener, &proto->listeners, proto_list) {
346 err |= udp_bind_listener(listener, errmsg, errlen);
347 if (err & ERR_ABORT)
348 break;
349 }
350
351 return err;
352}
353
354/* Add <listener> to the list of udp4 listeners, on port <port>. The
355 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
356 * The number of listeners for the protocol is updated.
357 */
358static void udp4_add_listener(struct listener *listener, int port)
359{
360 if (listener->state != LI_INIT)
361 return;
362 listener->state = LI_ASSIGNED;
363 listener->proto = &proto_udp4;
364 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
365 LIST_ADDQ(&proto_udp4.listeners, &listener->proto_list);
366 proto_udp4.nb_listeners++;
367}
368
369/* Add <listener> to the list of udp6 listeners, on port <port>. The
370 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
371 * The number of listeners for the protocol is updated.
372 */
373static void udp6_add_listener(struct listener *listener, int port)
374{
375 if (listener->state != LI_INIT)
376 return;
377 listener->state = LI_ASSIGNED;
378 listener->proto = &proto_udp6;
379 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
380 LIST_ADDQ(&proto_udp6.listeners, &listener->proto_list);
381 proto_udp6.nb_listeners++;
382}
383
384/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
385 * was totally stopped, or > 0 if correctly paused.
386 */
387int udp_pause_listener(struct listener *l)
388{
389 /* we don't support pausing on UDP */
390 return -1;
391}
392
393/*
394 * Local variables:
395 * c-indent-level: 8
396 * c-basic-offset: 8
397 * End:
398 */