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 */ |
| 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 Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 200 | * IPPROTO (sockaddr is not enough) |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 201 | */ |
| 202 | |
Willy Tarreau | be56c10 | 2020-09-03 07:27:34 +0200 | [diff] [blame] | 203 | fd = my_socketat(listener->bind_conf->settings.netns, listener->proto->sock_family, listener->proto->sock_type, listener->proto->sock_prot); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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: |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 239 | if (!sock_inet4_make_foreign(fd)) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 240 | msg = "cannot make listening socket transparent"; |
| 241 | err |= ERR_ALERT; |
| 242 | } |
| 243 | break; |
| 244 | case AF_INET6: |
Willy Tarreau | 37bafdc | 2020-08-28 17:23:40 +0200 | [diff] [blame] | 245 | if (!sock_inet6_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 | } |
| 251 | } |
| 252 | |
| 253 | #ifdef SO_BINDTODEVICE |
| 254 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | 7e30721 | 2020-09-03 07:23:34 +0200 | [diff] [blame] | 255 | if (listener->bind_conf->settings.interface) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 256 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 7e30721 | 2020-09-03 07:23:34 +0200 | [diff] [blame] | 257 | listener->bind_conf->settings.interface, |
| 258 | strlen(listener->bind_conf->settings.interface) + 1) == -1) { |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 259 | msg = "cannot bind listener to device"; |
| 260 | err |= ERR_WARN; |
| 261 | } |
| 262 | } |
| 263 | #endif |
| 264 | #if defined(IPV6_V6ONLY) |
| 265 | if (listener->options & LI_O_V6ONLY) |
| 266 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
| 267 | else if (listener->options & LI_O_V4V6) |
| 268 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
| 269 | #endif |
| 270 | |
| 271 | if (bind(fd, (struct sockaddr *)&addr_inet, listener->proto->sock_addrlen) < 0) { |
| 272 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 273 | msg = "cannot bind socket"; |
| 274 | goto udp_close_return; |
| 275 | } |
| 276 | |
| 277 | /* the socket is ready */ |
| 278 | listener->fd = fd; |
| 279 | listener->state = LI_LISTEN; |
| 280 | |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 281 | if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG) |
| 282 | fd_insert(fd, listener, syslog_fd_handler, |
Willy Tarreau | e26993c | 2020-09-03 07:18:55 +0200 | [diff] [blame] | 283 | thread_mask(listener->bind_conf->settings.bind_thread) & all_threads_mask); |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 284 | else { |
| 285 | err |= ERR_FATAL | ERR_ALERT; |
| 286 | msg = "UDP is not yet supported on this proxy mode"; |
| 287 | goto udp_close_return; |
| 288 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 289 | |
| 290 | udp_return: |
| 291 | if (msg && errlen) { |
| 292 | char pn[INET6_ADDRSTRLEN]; |
| 293 | |
| 294 | addr_to_str(&addr_inet, pn, sizeof(pn)); |
| 295 | snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&addr_inet)); |
| 296 | } |
| 297 | return err; |
| 298 | |
| 299 | udp_close_return: |
| 300 | close(fd); |
| 301 | goto udp_return; |
| 302 | } |
| 303 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 304 | /* Add <listener> to the list of udp4 listeners, on port <port>. The |
| 305 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 306 | * The number of listeners for the protocol is updated. |
| 307 | */ |
| 308 | static void udp4_add_listener(struct listener *listener, int port) |
| 309 | { |
| 310 | if (listener->state != LI_INIT) |
| 311 | return; |
| 312 | listener->state = LI_ASSIGNED; |
| 313 | listener->proto = &proto_udp4; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame^] | 314 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 315 | LIST_ADDQ(&proto_udp4.listeners, &listener->proto_list); |
| 316 | proto_udp4.nb_listeners++; |
| 317 | } |
| 318 | |
| 319 | /* Add <listener> to the list of udp6 listeners, on port <port>. The |
| 320 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 321 | * The number of listeners for the protocol is updated. |
| 322 | */ |
| 323 | static void udp6_add_listener(struct listener *listener, int port) |
| 324 | { |
| 325 | if (listener->state != LI_INIT) |
| 326 | return; |
| 327 | listener->state = LI_ASSIGNED; |
| 328 | listener->proto = &proto_udp6; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame^] | 329 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 330 | LIST_ADDQ(&proto_udp6.listeners, &listener->proto_list); |
| 331 | proto_udp6.nb_listeners++; |
| 332 | } |
| 333 | |
| 334 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 335 | * was totally stopped, or > 0 if correctly paused. |
| 336 | */ |
| 337 | int udp_pause_listener(struct listener *l) |
| 338 | { |
| 339 | /* we don't support pausing on UDP */ |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Local variables: |
| 345 | * c-indent-level: 8 |
| 346 | * c-basic-offset: 8 |
| 347 | * End: |
| 348 | */ |