blob: 8c53c6201bc50eea20177c0107a1c1a873fcaaed [file] [log] [blame]
Emeric Brun3835c0d2020-07-07 09:46:09 +02001/*
Willy Tarreau2b5e0d82020-09-16 21:58:52 +02002 * UDP protocol layer on top of AF_INET/AF_INET6
Emeric Brun3835c0d2020-07-07 09:46:09 +02003 *
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 Tarreau18b7df72020-08-28 12:07:22 +020039#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020040#include <haproxy/sock_inet.h>
Emeric Brun3835c0d2020-07-07 09:46:09 +020041#include <haproxy/task.h>
42
Emeric Brun3835c0d2020-07-07 09:46:09 +020043static int udp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaucb66ea62020-09-25 17:12:32 +020044static int udp_suspend_receiver(struct receiver *rx);
Willy Tarreaue122dc52020-10-07 19:55:15 +020045static int udp_resume_receiver(struct receiver *rx);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020046static void udp_enable_listener(struct listener *listener);
47static void udp_disable_listener(struct listener *listener);
Emeric Brun3835c0d2020-07-07 09:46:09 +020048static void udp4_add_listener(struct listener *listener, int port);
49static void udp6_add_listener(struct listener *listener, int port);
50
51/* Note: must not be declared <const> as its list will be overwritten */
52static struct protocol proto_udp4 = {
53 .name = "udp4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020054 .fam = &proto_fam_inet4,
Willy Tarreaua54553f2020-09-16 17:50:45 +020055 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020056 .sock_domain = AF_INET,
Emeric Brun3835c0d2020-07-07 09:46:09 +020057 .sock_type = SOCK_DGRAM,
58 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020059 .add = udp4_add_listener,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020060 .listen = udp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020061 .enable = udp_enable_listener,
62 .disable = udp_disable_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020063 .rx_enable = sock_enable,
64 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020065 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020066 .rx_suspend = udp_suspend_receiver,
Willy Tarreaue122dc52020-10-07 19:55:15 +020067 .rx_resume = udp_resume_receiver,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020068 .receivers = LIST_HEAD_INIT(proto_udp4.receivers),
69 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +020070};
71
72INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
73
74/* Note: must not be declared <const> as its list will be overwritten */
75static struct protocol proto_udp6 = {
76 .name = "udp6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020077 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020078 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020079 .sock_domain = AF_INET6,
Emeric Brun3835c0d2020-07-07 09:46:09 +020080 .sock_type = SOCK_DGRAM,
81 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020082 .add = udp6_add_listener,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020083 .listen = udp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020084 .enable = udp_enable_listener,
85 .disable = udp_disable_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020086 .rx_enable = sock_enable,
87 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020088 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020089 .rx_suspend = udp_suspend_receiver,
Willy Tarreaue122dc52020-10-07 19:55:15 +020090 .rx_resume = udp_resume_receiver,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020091 .receivers = LIST_HEAD_INIT(proto_udp6.receivers),
92 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +020093};
94
95INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
96
Emeric Brun3835c0d2020-07-07 09:46:09 +020097/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
98 * an error message in <errmsg> if the message is at most <errlen> bytes long
99 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
100 * The return value is composed from ERR_ABORT, ERR_WARN,
101 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
102 * was alright and that no message was returned. ERR_RETRYABLE means that an
103 * error occurred but that it may vanish after a retry (eg: port in use), and
104 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
105 * the meaning of the error, but just indicate that a message is present which
106 * should be displayed with the respective level. Last, ERR_ABORT indicates
107 * that it's pointless to try to start other listeners. No error message is
108 * returned if errlen is NULL.
109 */
110int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
111{
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200112 int err = ERR_NONE;
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200113 char *msg = NULL;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200114
115 /* ensure we never return garbage */
116 if (errlen)
117 *errmsg = 0;
118
119 if (listener->state != LI_ASSIGNED)
120 return ERR_NONE; /* already bound */
121
Willy Tarreauad33acf2020-09-02 18:40:02 +0200122 if (!(listener->rx.flags & RX_F_BOUND)) {
123 msg = "receiving socket not bound";
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200124 goto udp_return;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200125 }
126
Willy Tarreaua37b2442020-09-24 07:23:45 +0200127 listener_set_state(listener, LI_LISTEN);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200128
Emeric Brun3835c0d2020-07-07 09:46:09 +0200129 udp_return:
130 if (msg && errlen) {
131 char pn[INET6_ADDRSTRLEN];
132
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200133 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
134 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Emeric Brun3835c0d2020-07-07 09:46:09 +0200135 }
136 return err;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200137}
138
Emeric Brun3835c0d2020-07-07 09:46:09 +0200139/* Add <listener> to the list of udp4 listeners, on port <port>. The
140 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
141 * The number of listeners for the protocol is updated.
142 */
143static void udp4_add_listener(struct listener *listener, int port)
144{
145 if (listener->state != LI_INIT)
146 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200147 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200148 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200149 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200150 LIST_ADDQ(&proto_udp4.receivers, &listener->rx.proto_list);
151 proto_udp4.nb_receivers++;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200152}
153
154/* Add <listener> to the list of udp6 listeners, on port <port>. The
155 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
156 * The number of listeners for the protocol is updated.
157 */
158static void udp6_add_listener(struct listener *listener, int port)
159{
160 if (listener->state != LI_INIT)
161 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200162 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200163 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200164 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200165 LIST_ADDQ(&proto_udp6.receivers, &listener->rx.proto_list);
166 proto_udp6.nb_receivers++;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200167}
168
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200169/* Enable receipt of incoming connections for listener <l>. The receiver must
170 * still be valid. Does nothing in early boot (needs fd_updt).
171 */
172static void udp_enable_listener(struct listener *l)
173{
174 if (fd_updt)
175 fd_want_recv(l->rx.fd);
176}
177
178/* Disable receipt of incoming connections for listener <l>. The receiver must
179 * still be valid. Does nothing in early boot (needs fd_updt).
180 */
181static void udp_disable_listener(struct listener *l)
182{
183 if (fd_updt)
184 fd_stop_recv(l->rx.fd);
185}
186
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200187/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
188 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreaue122dc52020-10-07 19:55:15 +0200189 * The principle is a bit ugly but works well, at least on Linux: in order to
190 * suspend the receiver, we want it to stop receiving traffic, which means that
191 * the socket must be unhashed from the kernel's socket table. The simple way
192 * to do this is to connect to any address that is reachable and will not be
193 * used by regular traffic, and a great one is reconnecting to self.
Emeric Brun3835c0d2020-07-07 09:46:09 +0200194 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200195static int udp_suspend_receiver(struct receiver *rx)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200196{
Willy Tarreaue122dc52020-10-07 19:55:15 +0200197 struct sockaddr_storage ss;
198 socklen_t len = sizeof(ss);
199
200 if (rx->fd < 0)
201 return 0;
202
203 if (getsockname(rx->fd, (struct sockaddr *)&ss, &len) < 0)
204 return -1;
205
206 if (connect(rx->fd, (struct sockaddr *)&ss, len) < 0)
207 return -1;
208
209 /* not necessary but may make debugging clearer */
210 fd_stop_recv(rx->fd);
211 return 1;
212}
213
214/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
215 * was totally stopped, or > 0 if correctly suspended.
216 * The principle is to reverse the change above, we'll break the connection by
217 * connecting to AF_UNSPEC. The association breaks and the socket starts to
218 * receive from everywhere again.
219 */
220static int udp_resume_receiver(struct receiver *rx)
221{
222 struct sockaddr sa;
223 socklen_t len = sizeof(sa);
224
225 if (rx->fd < 0)
226 return 0;
227
228 sa.sa_family = AF_UNSPEC;
229 if (connect(rx->fd, &sa, len) < 0)
230 return -1;
231
232 fd_want_recv(rx->fd);
233 return 1;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200234}
235
236/*
237 * Local variables:
238 * c-indent-level: 8
239 * c-basic-offset: 8
240 * End:
241 */