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", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame^] | 50 | .fam = &proto_fam_inet4, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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, |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 59 | .bind = sock_inet_bind_receiver, |
Willy Tarreau | b3580b1 | 2020-09-01 10:26:22 +0200 | [diff] [blame] | 60 | .listen = udp_bind_listener, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 66 | .addrcmp = sock_inet4_addrcmp, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 67 | .listeners = LIST_HEAD_INIT(proto_udp4.listeners), |
| 68 | .nb_listeners = 0, |
| 69 | }; |
| 70 | |
| 71 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp4); |
| 72 | |
| 73 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 74 | static struct protocol proto_udp6 = { |
| 75 | .name = "udp6", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame^] | 76 | .fam = &proto_fam_inet6, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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, |
Willy Tarreau | d69ce1f | 2020-09-01 14:18:04 +0200 | [diff] [blame] | 85 | .bind = sock_inet_bind_receiver, |
Willy Tarreau | b3580b1 | 2020-09-01 10:26:22 +0200 | [diff] [blame] | 86 | .listen = udp_bind_listener, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 87 | .enable_all = enable_all_listeners, |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 88 | .get_src = udp6_get_src, |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 89 | .get_dst = udp6_get_dst, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 90 | .pause = udp_pause_listener, |
| 91 | .add = udp6_add_listener, |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 92 | .addrcmp = sock_inet6_addrcmp, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 93 | .listeners = LIST_HEAD_INIT(proto_udp6.listeners), |
| 94 | .nb_listeners = 0, |
| 95 | }; |
| 96 | |
| 97 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp6); |
| 98 | |
| 99 | /* |
| 100 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 101 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 102 | * success, -1 in case of error. The socket's source address is stored in |
| 103 | * <sa> for <salen> bytes. |
| 104 | */ |
| 105 | int udp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 106 | { |
| 107 | int ret; |
| 108 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 109 | ret = sock_get_src(fd, sa, salen, dir); |
| 110 | if (!ret) |
| 111 | sa->sa_family = AF_CUST_UDP4; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 116 | /* |
| 117 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 118 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 119 | * success, -1 in case of error. The socket's source address is stored in |
| 120 | * <sa> for <salen> bytes. |
| 121 | */ |
| 122 | int udp6_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 123 | { |
| 124 | int ret; |
| 125 | |
| 126 | ret = sock_get_src(fd, sa, salen, dir); |
| 127 | if (!ret) |
| 128 | sa->sa_family = AF_CUST_UDP6; |
| 129 | |
| 130 | return ret; |
| 131 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 135 | * indicating if we're a listener (=0) or an initiator (!=0). In the case of a |
| 136 | * listener, if the original destination address was translated, the original |
| 137 | * address is retrieved. It returns 0 in case of success, -1 in case of error. |
| 138 | * The socket's source address is stored in <sa> for <salen> bytes. |
| 139 | */ |
| 140 | int udp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 141 | { |
| 142 | int ret; |
| 143 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 144 | ret = sock_inet_get_dst(fd, sa, salen, dir); |
| 145 | if (!ret) |
| 146 | sa->sa_family = AF_CUST_UDP4; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 148 | return ret; |
| 149 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 150 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 151 | /* |
| 152 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 153 | * indicating if we're a listener (=0) or an initiator (!=0). In the case of a |
| 154 | * listener, if the original destination address was translated, the original |
| 155 | * address is retrieved. It returns 0 in case of success, -1 in case of error. |
| 156 | * The socket's source address is stored in <sa> for <salen> bytes. |
| 157 | */ |
| 158 | int udp6_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 159 | { |
| 160 | int ret; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | c5a94c9 | 2020-08-28 15:19:45 +0200 | [diff] [blame] | 162 | ret = sock_get_dst(fd, sa, salen, dir); |
| 163 | if (!ret) |
| 164 | sa->sa_family = AF_CUST_UDP6; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | /* This function tries to bind a UDPv4/v6 listener. It may return a warning or |
| 170 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 171 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 172 | * The return value is composed from ERR_ABORT, ERR_WARN, |
| 173 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 174 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 175 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 176 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
| 177 | * the meaning of the error, but just indicate that a message is present which |
| 178 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 179 | * that it's pointless to try to start other listeners. No error message is |
| 180 | * returned if errlen is NULL. |
| 181 | */ |
| 182 | int udp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 183 | { |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 184 | int err = ERR_NONE; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 185 | char *msg = NULL; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 186 | |
| 187 | /* ensure we never return garbage */ |
| 188 | if (errlen) |
| 189 | *errmsg = 0; |
| 190 | |
| 191 | if (listener->state != LI_ASSIGNED) |
| 192 | return ERR_NONE; /* already bound */ |
| 193 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 194 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 195 | msg = "receiving socket not bound"; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 196 | goto udp_return; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 199 | listener->state = LI_LISTEN; |
| 200 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 201 | udp_return: |
| 202 | if (msg && errlen) { |
| 203 | char pn[INET6_ADDRSTRLEN]; |
| 204 | |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 205 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
| 206 | snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr)); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 207 | } |
| 208 | return err; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 211 | /* Add <listener> to the list of udp4 listeners, on port <port>. The |
| 212 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 213 | * The number of listeners for the protocol is updated. |
| 214 | */ |
| 215 | static void udp4_add_listener(struct listener *listener, int port) |
| 216 | { |
| 217 | if (listener->state != LI_INIT) |
| 218 | return; |
| 219 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 220 | listener->rx.proto = &proto_udp4; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 221 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 222 | LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 223 | proto_udp4.nb_listeners++; |
| 224 | } |
| 225 | |
| 226 | /* Add <listener> to the list of udp6 listeners, on port <port>. The |
| 227 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 228 | * The number of listeners for the protocol is updated. |
| 229 | */ |
| 230 | static void udp6_add_listener(struct listener *listener, int port) |
| 231 | { |
| 232 | if (listener->state != LI_INIT) |
| 233 | return; |
| 234 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 235 | listener->rx.proto = &proto_udp6; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 236 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 237 | LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 238 | proto_udp6.nb_listeners++; |
| 239 | } |
| 240 | |
| 241 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 242 | * was totally stopped, or > 0 if correctly paused. |
| 243 | */ |
| 244 | int udp_pause_listener(struct listener *l) |
| 245 | { |
| 246 | /* we don't support pausing on UDP */ |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Local variables: |
| 252 | * c-indent-level: 8 |
| 253 | * c-basic-offset: 8 |
| 254 | * End: |
| 255 | */ |