Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2b5e0d8 | 2020-09-16 21:58:52 +0200 | [diff] [blame] | 2 | * UDP protocol layer on top of AF_INET/AF_INET6 |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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); |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 44 | static int udp_suspend_receiver(struct receiver *rx); |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 45 | static int udp_resume_receiver(struct receiver *rx); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 46 | static void udp_enable_listener(struct listener *listener); |
| 47 | static void udp_disable_listener(struct listener *listener); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 48 | static void udp4_add_listener(struct listener *listener, int port); |
| 49 | static void udp6_add_listener(struct listener *listener, int port); |
| 50 | |
| 51 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 52 | static struct protocol proto_udp4 = { |
| 53 | .name = "udp4", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 54 | .fam = &proto_fam_inet4, |
Willy Tarreau | a54553f | 2020-09-16 17:50:45 +0200 | [diff] [blame] | 55 | .ctrl_type = SOCK_DGRAM, |
Willy Tarreau | 2b5e0d8 | 2020-09-16 21:58:52 +0200 | [diff] [blame] | 56 | .sock_domain = AF_INET, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 57 | .sock_type = SOCK_DGRAM, |
| 58 | .sock_prot = IPPROTO_UDP, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 59 | .add = udp4_add_listener, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 60 | .listen = udp_bind_listener, |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 61 | .enable = udp_enable_listener, |
| 62 | .disable = udp_disable_listener, |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 63 | .unbind = default_unbind_listener, |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 64 | .suspend = default_suspend_listener, |
| 65 | .resume = default_resume_listener, |
Willy Tarreau | 686fa3d | 2020-09-25 19:09:53 +0200 | [diff] [blame] | 66 | .rx_enable = sock_enable, |
| 67 | .rx_disable = sock_disable, |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 68 | .rx_unbind = sock_unbind, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 69 | .rx_suspend = udp_suspend_receiver, |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 70 | .rx_resume = udp_resume_receiver, |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 71 | .receivers = LIST_HEAD_INIT(proto_udp4.receivers), |
| 72 | .nb_receivers = 0, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp4); |
| 76 | |
| 77 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 78 | static struct protocol proto_udp6 = { |
| 79 | .name = "udp6", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 80 | .fam = &proto_fam_inet6, |
Willy Tarreau | a54553f | 2020-09-16 17:50:45 +0200 | [diff] [blame] | 81 | .ctrl_type = SOCK_DGRAM, |
Willy Tarreau | 2b5e0d8 | 2020-09-16 21:58:52 +0200 | [diff] [blame] | 82 | .sock_domain = AF_INET6, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 83 | .sock_type = SOCK_DGRAM, |
| 84 | .sock_prot = IPPROTO_UDP, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 85 | .add = udp6_add_listener, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 86 | .listen = udp_bind_listener, |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 87 | .enable = udp_enable_listener, |
| 88 | .disable = udp_disable_listener, |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 89 | .unbind = default_unbind_listener, |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 90 | .suspend = default_suspend_listener, |
| 91 | .resume = default_resume_listener, |
Willy Tarreau | 686fa3d | 2020-09-25 19:09:53 +0200 | [diff] [blame] | 92 | .rx_enable = sock_enable, |
| 93 | .rx_disable = sock_disable, |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 94 | .rx_unbind = sock_unbind, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 95 | .rx_suspend = udp_suspend_receiver, |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 96 | .rx_resume = udp_resume_receiver, |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 97 | .receivers = LIST_HEAD_INIT(proto_udp6.receivers), |
| 98 | .nb_receivers = 0, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp6); |
| 102 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 103 | /* This function tries to bind a UDPv4/v6 listener. It may return a warning or |
| 104 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 105 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 106 | * The return value is composed from ERR_ABORT, ERR_WARN, |
| 107 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 108 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 109 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 110 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
| 111 | * the meaning of the error, but just indicate that a message is present which |
| 112 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 113 | * that it's pointless to try to start other listeners. No error message is |
| 114 | * returned if errlen is NULL. |
| 115 | */ |
| 116 | int udp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 117 | { |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 118 | int err = ERR_NONE; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 119 | char *msg = NULL; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 120 | |
| 121 | /* ensure we never return garbage */ |
| 122 | if (errlen) |
| 123 | *errmsg = 0; |
| 124 | |
| 125 | if (listener->state != LI_ASSIGNED) |
| 126 | return ERR_NONE; /* already bound */ |
| 127 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 128 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 129 | msg = "receiving socket not bound"; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 130 | goto udp_return; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 133 | listener_set_state(listener, LI_LISTEN); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 134 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 135 | udp_return: |
| 136 | if (msg && errlen) { |
| 137 | char pn[INET6_ADDRSTRLEN]; |
| 138 | |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 139 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
| 140 | 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] | 141 | } |
| 142 | return err; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 145 | /* Add <listener> to the list of udp4 listeners, on port <port>. The |
| 146 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 147 | * The number of listeners for the protocol is updated. |
| 148 | */ |
| 149 | static void udp4_add_listener(struct listener *listener, int port) |
| 150 | { |
| 151 | if (listener->state != LI_INIT) |
| 152 | return; |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 153 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 154 | listener->rx.proto = &proto_udp4; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 155 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 156 | LIST_ADDQ(&proto_udp4.receivers, &listener->rx.proto_list); |
| 157 | proto_udp4.nb_receivers++; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Add <listener> to the list of udp6 listeners, on port <port>. The |
| 161 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 162 | * The number of listeners for the protocol is updated. |
| 163 | */ |
| 164 | static void udp6_add_listener(struct listener *listener, int port) |
| 165 | { |
| 166 | if (listener->state != LI_INIT) |
| 167 | return; |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 168 | listener_set_state(listener, LI_ASSIGNED); |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 169 | listener->rx.proto = &proto_udp6; |
Willy Tarreau | 3715906 | 2020-08-27 07:48:42 +0200 | [diff] [blame] | 170 | ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port); |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 171 | LIST_ADDQ(&proto_udp6.receivers, &listener->rx.proto_list); |
| 172 | proto_udp6.nb_receivers++; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 175 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
| 176 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 177 | */ |
| 178 | static void udp_enable_listener(struct listener *l) |
| 179 | { |
| 180 | if (fd_updt) |
| 181 | fd_want_recv(l->rx.fd); |
| 182 | } |
| 183 | |
| 184 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
| 185 | * still be valid. Does nothing in early boot (needs fd_updt). |
| 186 | */ |
| 187 | static void udp_disable_listener(struct listener *l) |
| 188 | { |
| 189 | if (fd_updt) |
| 190 | fd_stop_recv(l->rx.fd); |
| 191 | } |
| 192 | |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 193 | /* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver |
| 194 | * was totally stopped, or > 0 if correctly suspended. |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 195 | * The principle is a bit ugly but works well, at least on Linux: in order to |
| 196 | * suspend the receiver, we want it to stop receiving traffic, which means that |
| 197 | * the socket must be unhashed from the kernel's socket table. The simple way |
| 198 | * to do this is to connect to any address that is reachable and will not be |
| 199 | * used by regular traffic, and a great one is reconnecting to self. |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 200 | */ |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 201 | static int udp_suspend_receiver(struct receiver *rx) |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 202 | { |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 203 | struct sockaddr_storage ss; |
| 204 | socklen_t len = sizeof(ss); |
| 205 | |
| 206 | if (rx->fd < 0) |
| 207 | return 0; |
| 208 | |
| 209 | if (getsockname(rx->fd, (struct sockaddr *)&ss, &len) < 0) |
| 210 | return -1; |
| 211 | |
| 212 | if (connect(rx->fd, (struct sockaddr *)&ss, len) < 0) |
| 213 | return -1; |
| 214 | |
| 215 | /* not necessary but may make debugging clearer */ |
| 216 | fd_stop_recv(rx->fd); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | /* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver |
| 221 | * was totally stopped, or > 0 if correctly suspended. |
| 222 | * The principle is to reverse the change above, we'll break the connection by |
| 223 | * connecting to AF_UNSPEC. The association breaks and the socket starts to |
| 224 | * receive from everywhere again. |
| 225 | */ |
| 226 | static int udp_resume_receiver(struct receiver *rx) |
| 227 | { |
| 228 | struct sockaddr sa; |
| 229 | socklen_t len = sizeof(sa); |
| 230 | |
| 231 | if (rx->fd < 0) |
| 232 | return 0; |
| 233 | |
| 234 | sa.sa_family = AF_UNSPEC; |
| 235 | if (connect(rx->fd, &sa, len) < 0) |
| 236 | return -1; |
| 237 | |
| 238 | fd_want_recv(rx->fd); |
| 239 | return 1; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Local variables: |
| 244 | * c-indent-level: 8 |
| 245 | * c-basic-offset: 8 |
| 246 | * End: |
| 247 | */ |