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