blob: 0bd903fb5b35051e0861e1b4b4a528cace167221 [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,
Emeric Brun3835c0d2020-07-07 09:46:09 +020090 .get_dst = udp_get_dst,
91 .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
145 if (dir)
146 ret = getpeername(fd, sa, &salen);
147 else {
148 ret = getsockname(fd, sa, &salen);
149
150 if (ret < 0)
151 return ret;
152
153#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
154 /* For TPROXY and Netfilter's NAT, we can retrieve the original
155 * IPv4 address before DNAT/REDIRECT. We must not do that with
156 * other families because v6-mapped IPv4 addresses are still
157 * reported as v4.
158 */
159 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
160 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0) {
161 sa->sa_family = AF_CUST_UDP4;
162 return 0;
163 }
164#endif
165 }
166
167 if (!ret) {
168 if (sa->sa_family == AF_INET)
169 sa->sa_family = AF_CUST_UDP4;
170 else if (sa->sa_family == AF_INET6)
171 sa->sa_family = AF_CUST_UDP6;
172 }
173
174 return ret;
175}
176
177/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
178 * an error message in <errmsg> if the message is at most <errlen> bytes long
179 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
180 * The return value is composed from ERR_ABORT, ERR_WARN,
181 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
182 * was alright and that no message was returned. ERR_RETRYABLE means that an
183 * error occurred but that it may vanish after a retry (eg: port in use), and
184 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
185 * the meaning of the error, but just indicate that a message is present which
186 * should be displayed with the respective level. Last, ERR_ABORT indicates
187 * that it's pointless to try to start other listeners. No error message is
188 * returned if errlen is NULL.
189 */
190int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
191{
192 __label__ udp_return, udp_close_return;
193 int fd, err;
194 const char *msg = NULL;
195 /* copy listener addr because sometimes we need to switch family */
196 struct sockaddr_storage addr_inet = listener->addr;
197
198 /* force to classic sock family */
199 addr_inet.ss_family = listener->proto->sock_family;
200
201 /* ensure we never return garbage */
202 if (errlen)
203 *errmsg = 0;
204
205 if (listener->state != LI_ASSIGNED)
206 return ERR_NONE; /* already bound */
207
208 err = ERR_NONE;
209
210 /* TODO: Implement reuse fd. Take care that to identify fd to reuse
211 * listeners uses a special AF_CUST_ family and we MUST consider
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500212 * IPPROTO (sockaddr is not enough)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200213 */
214
215 fd = my_socketat(listener->netns, listener->proto->sock_family, listener->proto->sock_type, listener->proto->sock_prot);
216 if (fd == -1) {
217 err |= ERR_RETRYABLE | ERR_ALERT;
218 msg = "cannot create listening socket";
219 goto udp_return;
220 }
221
222 if (fd >= global.maxsock) {
223 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
224 msg = "not enough free sockets (raise '-n' parameter)";
225 goto udp_close_return;
226 }
227
228 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
229 err |= ERR_FATAL | ERR_ALERT;
230 msg = "cannot make socket non-blocking";
231 goto udp_close_return;
232 }
233
234 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
235 /* not fatal but should be reported */
236 msg = "cannot do so_reuseaddr";
237 err |= ERR_ALERT;
238 }
239
240#ifdef SO_REUSEPORT
241 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
242 * Linux, it might return an error that we will silently ignore.
243 */
244 if (global.tune.options & GTUNE_USE_REUSEPORT)
245 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
246#endif
247
248 if (listener->options & LI_O_FOREIGN) {
249 switch (addr_inet.ss_family) {
250 case AF_INET:
251 if (1
252#if defined(IP_TRANSPARENT)
253 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
254#endif
255#if defined(IP_FREEBIND)
256 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
257#endif
258#if defined(IP_BINDANY)
259 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
260#endif
261#if defined(SO_BINDANY)
262 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
263#endif
264 ) {
265 msg = "cannot make listening socket transparent";
266 err |= ERR_ALERT;
267 }
268 break;
269 case AF_INET6:
270 if (1
271#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
272 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
273#endif
274#if defined(IP_FREEBIND)
275 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
276#endif
277#if defined(IPV6_BINDANY)
278 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
279#endif
280#if defined(SO_BINDANY)
281 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
282#endif
283 ) {
284 msg = "cannot make listening socket transparent";
285 err |= ERR_ALERT;
286 }
287 break;
288 }
289 }
290
291#ifdef SO_BINDTODEVICE
292 /* Note: this might fail if not CAP_NET_RAW */
293 if (listener->interface) {
294 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
295 listener->interface, strlen(listener->interface) + 1) == -1) {
296 msg = "cannot bind listener to device";
297 err |= ERR_WARN;
298 }
299 }
300#endif
301#if defined(IPV6_V6ONLY)
302 if (listener->options & LI_O_V6ONLY)
303 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
304 else if (listener->options & LI_O_V4V6)
305 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
306#endif
307
308 if (bind(fd, (struct sockaddr *)&addr_inet, listener->proto->sock_addrlen) < 0) {
309 err |= ERR_RETRYABLE | ERR_ALERT;
310 msg = "cannot bind socket";
311 goto udp_close_return;
312 }
313
314 /* the socket is ready */
315 listener->fd = fd;
316 listener->state = LI_LISTEN;
317
Emeric Brun54932b42020-07-07 09:43:24 +0200318 if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG)
319 fd_insert(fd, listener, syslog_fd_handler,
320 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
321 else {
322 err |= ERR_FATAL | ERR_ALERT;
323 msg = "UDP is not yet supported on this proxy mode";
324 goto udp_close_return;
325 }
Emeric Brun3835c0d2020-07-07 09:46:09 +0200326
327 udp_return:
328 if (msg && errlen) {
329 char pn[INET6_ADDRSTRLEN];
330
331 addr_to_str(&addr_inet, pn, sizeof(pn));
332 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&addr_inet));
333 }
334 return err;
335
336 udp_close_return:
337 close(fd);
338 goto udp_return;
339}
340
341/* This function creates all UDP sockets bound to the protocol entry <proto>.
342 * It is intended to be used as the protocol's bind_all() function.
343 * The sockets will be registered but not added to any fd_set, in order not to
344 * loose them across the fork(). A call to enable_all_listeners() is needed
345 * to complete initialization. The return value is composed from ERR_*.
346 */
347static int udp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
348{
349 struct listener *listener;
350 int err = ERR_NONE;
351
352 list_for_each_entry(listener, &proto->listeners, proto_list) {
353 err |= udp_bind_listener(listener, errmsg, errlen);
354 if (err & ERR_ABORT)
355 break;
356 }
357
358 return err;
359}
360
361/* Add <listener> to the list of udp4 listeners, on port <port>. The
362 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
363 * The number of listeners for the protocol is updated.
364 */
365static void udp4_add_listener(struct listener *listener, int port)
366{
367 if (listener->state != LI_INIT)
368 return;
369 listener->state = LI_ASSIGNED;
370 listener->proto = &proto_udp4;
371 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
372 LIST_ADDQ(&proto_udp4.listeners, &listener->proto_list);
373 proto_udp4.nb_listeners++;
374}
375
376/* Add <listener> to the list of udp6 listeners, on port <port>. The
377 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
378 * The number of listeners for the protocol is updated.
379 */
380static void udp6_add_listener(struct listener *listener, int port)
381{
382 if (listener->state != LI_INIT)
383 return;
384 listener->state = LI_ASSIGNED;
385 listener->proto = &proto_udp6;
386 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
387 LIST_ADDQ(&proto_udp6.listeners, &listener->proto_list);
388 proto_udp6.nb_listeners++;
389}
390
391/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
392 * was totally stopped, or > 0 if correctly paused.
393 */
394int udp_pause_listener(struct listener *l)
395{
396 /* we don't support pausing on UDP */
397 return -1;
398}
399
400/*
401 * Local variables:
402 * c-indent-level: 8
403 * c-basic-offset: 8
404 * End:
405 */