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 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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> |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <netinet/udp.h> |
| 27 | #include <netinet/in.h> |
| 28 | |
| 29 | #include <haproxy/fd.h> |
| 30 | #include <haproxy/listener.h> |
| 31 | #include <haproxy/log.h> |
| 32 | #include <haproxy/namespace.h> |
| 33 | #include <haproxy/port_range.h> |
| 34 | #include <haproxy/protocol.h> |
| 35 | #include <haproxy/proto_udp.h> |
| 36 | #include <haproxy/proxy.h> |
| 37 | #include <haproxy/server.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 38 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 39 | #include <haproxy/sock_inet.h> |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 40 | #include <haproxy/task.h> |
Willy Tarreau | ce65cbe | 2021-05-08 13:59:56 +0200 | [diff] [blame] | 41 | #include <haproxy/tools.h> |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 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 | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 44 | static void udp_enable_listener(struct listener *listener); |
| 45 | static void udp_disable_listener(struct listener *listener); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 46 | |
| 47 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b9b2fd7 | 2020-12-08 14:13:11 +0100 | [diff] [blame] | 48 | struct protocol proto_udp4 = { |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 49 | .name = "udp4", |
| 50 | |
| 51 | /* connection layer */ |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 52 | .xprt_type = PROTO_TYPE_DGRAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 53 | .listen = udp_bind_listener, |
| 54 | .enable = udp_enable_listener, |
| 55 | .disable = udp_disable_listener, |
| 56 | .add = default_add_listener, |
| 57 | .unbind = default_unbind_listener, |
| 58 | .suspend = default_suspend_listener, |
| 59 | .resume = default_resume_listener, |
| 60 | |
| 61 | /* binding layer */ |
| 62 | .rx_suspend = udp_suspend_receiver, |
| 63 | .rx_resume = udp_resume_receiver, |
| 64 | |
| 65 | /* address family */ |
| 66 | .fam = &proto_fam_inet4, |
| 67 | |
| 68 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 69 | .proto_type = PROTO_TYPE_DGRAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 70 | .sock_type = SOCK_DGRAM, |
| 71 | .sock_prot = IPPROTO_UDP, |
| 72 | .rx_enable = sock_enable, |
| 73 | .rx_disable = sock_disable, |
| 74 | .rx_unbind = sock_unbind, |
| 75 | .receivers = LIST_HEAD_INIT(proto_udp4.receivers), |
| 76 | .nb_receivers = 0, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp4); |
| 80 | |
| 81 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b9b2fd7 | 2020-12-08 14:13:11 +0100 | [diff] [blame] | 82 | struct protocol proto_udp6 = { |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 83 | .name = "udp6", |
| 84 | |
| 85 | /* connection layer */ |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 86 | .xprt_type = PROTO_TYPE_DGRAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 87 | .listen = udp_bind_listener, |
| 88 | .enable = udp_enable_listener, |
| 89 | .disable = udp_disable_listener, |
| 90 | .add = default_add_listener, |
| 91 | .unbind = default_unbind_listener, |
| 92 | .suspend = default_suspend_listener, |
| 93 | .resume = default_resume_listener, |
| 94 | |
| 95 | /* binding layer */ |
| 96 | .rx_suspend = udp_suspend_receiver, |
| 97 | .rx_resume = udp_resume_receiver, |
| 98 | |
| 99 | /* address family */ |
| 100 | .fam = &proto_fam_inet6, |
| 101 | |
| 102 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 103 | .proto_type = PROTO_TYPE_DGRAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 104 | .sock_type = SOCK_DGRAM, |
| 105 | .sock_prot = IPPROTO_UDP, |
| 106 | .rx_enable = sock_enable, |
| 107 | .rx_disable = sock_disable, |
| 108 | .rx_unbind = sock_unbind, |
| 109 | .receivers = LIST_HEAD_INIT(proto_udp6.receivers), |
| 110 | .nb_receivers = 0, |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | INITCALL1(STG_REGISTER, protocol_register, &proto_udp6); |
| 114 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 115 | /* This function tries to bind a UDPv4/v6 listener. It may return a warning or |
| 116 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 117 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 118 | * The return value is composed from ERR_ABORT, ERR_WARN, |
| 119 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 120 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 121 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 122 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
| 123 | * the meaning of the error, but just indicate that a message is present which |
| 124 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 125 | * that it's pointless to try to start other listeners. No error message is |
| 126 | * returned if errlen is NULL. |
| 127 | */ |
| 128 | int udp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 129 | { |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 130 | int err = ERR_NONE; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 131 | char *msg = NULL; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 132 | |
| 133 | /* ensure we never return garbage */ |
| 134 | if (errlen) |
| 135 | *errmsg = 0; |
| 136 | |
| 137 | if (listener->state != LI_ASSIGNED) |
| 138 | return ERR_NONE; /* already bound */ |
| 139 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 140 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 141 | msg = "receiving socket not bound"; |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 142 | goto udp_return; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 145 | listener_set_state(listener, LI_LISTEN); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 146 | |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 147 | udp_return: |
| 148 | if (msg && errlen) { |
| 149 | char pn[INET6_ADDRSTRLEN]; |
| 150 | |
Willy Tarreau | 2f7687d | 2020-09-01 16:23:29 +0200 | [diff] [blame] | 151 | addr_to_str(&listener->rx.addr, pn, sizeof(pn)); |
Willy Tarreau | 6823a3a | 2021-10-14 11:59:15 +0200 | [diff] [blame] | 152 | snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr)); |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 153 | } |
| 154 | return err; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 157 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 158 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 159 | */ |
| 160 | static void udp_enable_listener(struct listener *l) |
| 161 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 162 | fd_want_recv_safe(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 166 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 167 | */ |
| 168 | static void udp_disable_listener(struct listener *l) |
| 169 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 170 | fd_stop_recv(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 173 | /* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver |
| 174 | * was totally stopped, or > 0 if correctly suspended. |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 175 | * The principle is a bit ugly but works well, at least on Linux: in order to |
| 176 | * suspend the receiver, we want it to stop receiving traffic, which means that |
| 177 | * the socket must be unhashed from the kernel's socket table. The simple way |
| 178 | * to do this is to connect to any address that is reachable and will not be |
Willy Tarreau | 6476334 | 2023-01-16 11:47:01 +0100 | [diff] [blame] | 179 | * used by regular traffic, and a great one is reconnecting to self. Note that |
| 180 | * inherited FDs are neither suspended nor resumed, we only enable/disable |
| 181 | * polling on them. |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 182 | */ |
Willy Tarreau | 29885f0 | 2020-12-08 18:05:16 +0100 | [diff] [blame] | 183 | int udp_suspend_receiver(struct receiver *rx) |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 184 | { |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 185 | struct sockaddr_storage ss; |
| 186 | socklen_t len = sizeof(ss); |
| 187 | |
| 188 | if (rx->fd < 0) |
| 189 | return 0; |
| 190 | |
Willy Tarreau | 59b5da4 | 2020-11-04 14:14:55 +0100 | [diff] [blame] | 191 | /* we never do that with a shared FD otherwise we'd break it in the |
| 192 | * parent process and any possible subsequent worker inheriting it. |
| 193 | */ |
| 194 | if (rx->flags & RX_F_INHERITED) |
Willy Tarreau | 6476334 | 2023-01-16 11:47:01 +0100 | [diff] [blame] | 195 | goto done; |
Willy Tarreau | 59b5da4 | 2020-11-04 14:14:55 +0100 | [diff] [blame] | 196 | |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 197 | if (getsockname(rx->fd, (struct sockaddr *)&ss, &len) < 0) |
| 198 | return -1; |
| 199 | |
| 200 | if (connect(rx->fd, (struct sockaddr *)&ss, len) < 0) |
| 201 | return -1; |
Willy Tarreau | 6476334 | 2023-01-16 11:47:01 +0100 | [diff] [blame] | 202 | done: |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 203 | /* not necessary but may make debugging clearer */ |
| 204 | fd_stop_recv(rx->fd); |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | /* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver |
| 209 | * was totally stopped, or > 0 if correctly suspended. |
| 210 | * The principle is to reverse the change above, we'll break the connection by |
| 211 | * connecting to AF_UNSPEC. The association breaks and the socket starts to |
Willy Tarreau | 6476334 | 2023-01-16 11:47:01 +0100 | [diff] [blame] | 212 | * receive from everywhere again. Note that inherited FDs are neither suspended |
| 213 | * nor resumed, we only enable/disable polling on them. |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 214 | */ |
Willy Tarreau | 29885f0 | 2020-12-08 18:05:16 +0100 | [diff] [blame] | 215 | int udp_resume_receiver(struct receiver *rx) |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 216 | { |
Willy Tarreau | 2f6f362 | 2020-10-14 10:50:41 +0200 | [diff] [blame] | 217 | const struct sockaddr sa = { .sa_family = AF_UNSPEC }; |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 218 | |
| 219 | if (rx->fd < 0) |
| 220 | return 0; |
| 221 | |
Willy Tarreau | 6476334 | 2023-01-16 11:47:01 +0100 | [diff] [blame] | 222 | if (!(rx->flags & RX_F_INHERITED) && connect(rx->fd, &sa, sizeof(sa)) < 0) |
Willy Tarreau | e122dc5 | 2020-10-07 19:55:15 +0200 | [diff] [blame] | 223 | return -1; |
| 224 | |
| 225 | fd_want_recv(rx->fd); |
| 226 | return 1; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Local variables: |
| 231 | * c-indent-level: 8 |
| 232 | * c-basic-offset: 8 |
| 233 | * End: |
| 234 | */ |