Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 39 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 40 | #include <haproxy/sock_inet.h> |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 41 | #include <haproxy/task.h> |
| 42 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 43 | static int udp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
| 44 | static void udp4_add_listener(struct listener *listener, int port); |
| 45 | static void udp6_add_listener(struct listener *listener, int port); |
| 46 | |
| 47 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 48 | static 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 Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 59 | .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 Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 64 | .addrcmp = sock_inet4_addrcmp, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 65 | .listeners = LIST_HEAD_INIT(proto_udp4.listeners), |
| 66 | .nb_listeners = 0, |
| 67 | }; |
| 68 | |
| 69 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp4); |
| 70 | |
| 71 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 72 | static 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 Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 83 | .enable_all = enable_all_listeners, |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 84 | .get_src = udp6_get_src, |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 85 | .get_dst = udp6_get_dst, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 86 | .pause = udp_pause_listener, |
| 87 | .add = udp6_add_listener, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 88 | .addrcmp = sock_inet6_addrcmp, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 89 | .listeners = LIST_HEAD_INIT(proto_udp6.listeners), |
| 90 | .nb_listeners = 0, |
| 91 | }; |
| 92 | |
| 93 | INITCALL1(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 | */ |
| 101 | int udp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 102 | { |
| 103 | int ret; |
| 104 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 105 | ret = sock_get_src(fd, sa, salen, dir); |
| 106 | if (!ret) |
| 107 | sa->sa_family = AF_CUST_UDP4; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 112 | /* |
| 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 | */ |
| 118 | int 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 Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 128 | |
| 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 | */ |
| 136 | int udp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 137 | { |
| 138 | int ret; |
| 139 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 140 | ret = sock_inet_get_dst(fd, sa, salen, dir); |
| 141 | if (!ret) |
| 142 | sa->sa_family = AF_CUST_UDP4; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 144 | return ret; |
| 145 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 147 | /* |
| 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 | */ |
| 154 | int udp6_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 155 | { |
| 156 | int ret; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 158 | ret = sock_get_dst(fd, sa, salen, dir); |
| 159 | if (!ret) |
| 160 | sa->sa_family = AF_CUST_UDP6; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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 | */ |
| 178 | int 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 Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 184 | struct sockaddr_storage addr_inet = listener->rx.addr; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 185 | |
| 186 | /* force to classic sock family */ |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 187 | addr_inet.ss_family = listener->rx.proto->sock_family; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame^] | 198 | if (listener->rx.flags & RX_F_BOUND) |
| 199 | goto bound; |
| 200 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 201 | /* 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 Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 203 | * IPPROTO (sockaddr is not enough) |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 204 | */ |
| 205 | |
Willy Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 206 | fd = my_socketat(listener->rx.settings->netns, |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 207 | listener->rx.proto->sock_family, |
| 208 | listener->rx.proto->sock_type, |
| 209 | listener->rx.proto->sock_prot); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 210 | 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 Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 245 | if (!sock_inet4_make_foreign(fd)) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 246 | msg = "cannot make listening socket transparent"; |
| 247 | err |= ERR_ALERT; |
| 248 | } |
| 249 | break; |
| 250 | case AF_INET6: |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 251 | if (!sock_inet6_make_foreign(fd)) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 252 | 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 Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 261 | if (listener->rx.settings->interface) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 262 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 263 | listener->rx.settings->interface, |
| 264 | strlen(listener->rx.settings->interface) + 1) == -1) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 265 | 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 Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 277 | if (bind(fd, (struct sockaddr *)&addr_inet, listener->rx.proto->sock_addrlen) < 0) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 278 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 279 | msg = "cannot bind socket"; |
| 280 | goto udp_close_return; |
| 281 | } |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame^] | 282 | listener->rx.flags |= RX_F_BOUND; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 283 | |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame^] | 284 | bound: |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 285 | /* the socket is ready */ |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame] | 286 | listener->rx.fd = fd; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 287 | listener->state = LI_LISTEN; |
| 288 | |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 289 | if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG) |
| 290 | fd_insert(fd, listener, syslog_fd_handler, |
Willy Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 291 | thread_mask(listener->rx.settings->bind_thread) & all_threads_mask); |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 292 | 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 Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 297 | |
| 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 Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 312 | /* 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 | */ |
| 316 | static void udp4_add_listener(struct listener *listener, int port) |
| 317 | { |
| 318 | if (listener->state != LI_INIT) |
| 319 | return; |
| 320 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 321 | listener->rx.proto = &proto_udp4; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 322 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 323 | LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 324 | 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 | */ |
| 331 | static void udp6_add_listener(struct listener *listener, int port) |
| 332 | { |
| 333 | if (listener->state != LI_INIT) |
| 334 | return; |
| 335 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 336 | listener->rx.proto = &proto_udp6; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 337 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 338 | LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 339 | 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 | */ |
| 345 | int 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 | */ |