blob: d2849c035102b0f35614f5ef7d04d8e84b5b13af [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>
Willy Tarreauce65cbe2021-05-08 13:59:56 +020042#include <haproxy/tools.h>
Emeric Brun3835c0d2020-07-07 09:46:09 +020043
Emeric Brun3835c0d2020-07-07 09:46:09 +020044static int udp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020045static void udp_enable_listener(struct listener *listener);
46static void udp_disable_listener(struct listener *listener);
Emeric Brun3835c0d2020-07-07 09:46:09 +020047
48/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010049struct protocol proto_udp4 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010050 .name = "udp4",
51
52 /* connection layer */
53 .ctrl_type = SOCK_DGRAM,
54 .listen = udp_bind_listener,
55 .enable = udp_enable_listener,
56 .disable = udp_disable_listener,
57 .add = default_add_listener,
58 .unbind = default_unbind_listener,
59 .suspend = default_suspend_listener,
60 .resume = default_resume_listener,
61
62 /* binding layer */
63 .rx_suspend = udp_suspend_receiver,
64 .rx_resume = udp_resume_receiver,
65
66 /* address family */
67 .fam = &proto_fam_inet4,
68
69 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020070 .proto_type = PROTO_TYPE_DGRAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010071 .sock_type = SOCK_DGRAM,
72 .sock_prot = IPPROTO_UDP,
73 .rx_enable = sock_enable,
74 .rx_disable = sock_disable,
75 .rx_unbind = sock_unbind,
76 .receivers = LIST_HEAD_INIT(proto_udp4.receivers),
77 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +020078};
79
80INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
81
82/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010083struct protocol proto_udp6 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010084 .name = "udp6",
85
86 /* connection layer */
87 .ctrl_type = SOCK_DGRAM,
88 .listen = udp_bind_listener,
89 .enable = udp_enable_listener,
90 .disable = udp_disable_listener,
91 .add = default_add_listener,
92 .unbind = default_unbind_listener,
93 .suspend = default_suspend_listener,
94 .resume = default_resume_listener,
95
96 /* binding layer */
97 .rx_suspend = udp_suspend_receiver,
98 .rx_resume = udp_resume_receiver,
99
100 /* address family */
101 .fam = &proto_fam_inet6,
102
103 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200104 .proto_type = PROTO_TYPE_DGRAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100105 .sock_type = SOCK_DGRAM,
106 .sock_prot = IPPROTO_UDP,
107 .rx_enable = sock_enable,
108 .rx_disable = sock_disable,
109 .rx_unbind = sock_unbind,
110 .receivers = LIST_HEAD_INIT(proto_udp6.receivers),
111 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +0200112};
113
114INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
115
Emeric Brun3835c0d2020-07-07 09:46:09 +0200116/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
117 * an error message in <errmsg> if the message is at most <errlen> bytes long
118 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
119 * The return value is composed from ERR_ABORT, ERR_WARN,
120 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
121 * was alright and that no message was returned. ERR_RETRYABLE means that an
122 * error occurred but that it may vanish after a retry (eg: port in use), and
123 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
124 * the meaning of the error, but just indicate that a message is present which
125 * should be displayed with the respective level. Last, ERR_ABORT indicates
126 * that it's pointless to try to start other listeners. No error message is
127 * returned if errlen is NULL.
128 */
129int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
130{
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200131 int err = ERR_NONE;
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200132 char *msg = NULL;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200133
134 /* ensure we never return garbage */
135 if (errlen)
136 *errmsg = 0;
137
138 if (listener->state != LI_ASSIGNED)
139 return ERR_NONE; /* already bound */
140
Willy Tarreauad33acf2020-09-02 18:40:02 +0200141 if (!(listener->rx.flags & RX_F_BOUND)) {
142 msg = "receiving socket not bound";
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200143 goto udp_return;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200144 }
145
Willy Tarreaua37b2442020-09-24 07:23:45 +0200146 listener_set_state(listener, LI_LISTEN);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200147
Emeric Brun3835c0d2020-07-07 09:46:09 +0200148 udp_return:
149 if (msg && errlen) {
150 char pn[INET6_ADDRSTRLEN];
151
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200152 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200153 snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Emeric Brun3835c0d2020-07-07 09:46:09 +0200154 }
155 return err;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200156}
157
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200158/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100159 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200160 */
161static void udp_enable_listener(struct listener *l)
162{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100163 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200164}
165
166/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100167 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200168 */
169static void udp_disable_listener(struct listener *l)
170{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100171 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200172}
173
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200174/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
175 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreaue122dc52020-10-07 19:55:15 +0200176 * The principle is a bit ugly but works well, at least on Linux: in order to
177 * suspend the receiver, we want it to stop receiving traffic, which means that
178 * the socket must be unhashed from the kernel's socket table. The simple way
179 * to do this is to connect to any address that is reachable and will not be
180 * used by regular traffic, and a great one is reconnecting to self.
Emeric Brun3835c0d2020-07-07 09:46:09 +0200181 */
Willy Tarreau29885f02020-12-08 18:05:16 +0100182int udp_suspend_receiver(struct receiver *rx)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200183{
Willy Tarreaue122dc52020-10-07 19:55:15 +0200184 struct sockaddr_storage ss;
185 socklen_t len = sizeof(ss);
186
187 if (rx->fd < 0)
188 return 0;
189
Willy Tarreau59b5da42020-11-04 14:14:55 +0100190 /* we never do that with a shared FD otherwise we'd break it in the
191 * parent process and any possible subsequent worker inheriting it.
192 */
193 if (rx->flags & RX_F_INHERITED)
194 return -1;
195
Willy Tarreaue122dc52020-10-07 19:55:15 +0200196 if (getsockname(rx->fd, (struct sockaddr *)&ss, &len) < 0)
197 return -1;
198
199 if (connect(rx->fd, (struct sockaddr *)&ss, len) < 0)
200 return -1;
201
202 /* not necessary but may make debugging clearer */
203 fd_stop_recv(rx->fd);
204 return 1;
205}
206
207/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
208 * was totally stopped, or > 0 if correctly suspended.
209 * The principle is to reverse the change above, we'll break the connection by
210 * connecting to AF_UNSPEC. The association breaks and the socket starts to
211 * receive from everywhere again.
212 */
Willy Tarreau29885f02020-12-08 18:05:16 +0100213int udp_resume_receiver(struct receiver *rx)
Willy Tarreaue122dc52020-10-07 19:55:15 +0200214{
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200215 const struct sockaddr sa = { .sa_family = AF_UNSPEC };
Willy Tarreaue122dc52020-10-07 19:55:15 +0200216
217 if (rx->fd < 0)
218 return 0;
219
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200220 if (connect(rx->fd, &sa, sizeof(sa)) < 0)
Willy Tarreaue122dc52020-10-07 19:55:15 +0200221 return -1;
222
223 fd_want_recv(rx->fd);
224 return 1;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200225}
226
227/*
228 * Local variables:
229 * c-indent-level: 8
230 * c-basic-offset: 8
231 * End:
232 */