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