blob: 33a4dca46bd425089d7637f54de27cd0d7ed884a [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 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 +020047static void udp4_add_listener(struct listener *listener, int port);
48static void udp6_add_listener(struct listener *listener, int port);
49
50/* Note: must not be declared <const> as its list will be overwritten */
51static struct protocol proto_udp4 = {
52 .name = "udp4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020053 .fam = &proto_fam_inet4,
Willy Tarreaua54553f2020-09-16 17:50:45 +020054 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020055 .sock_domain = AF_INET,
Emeric Brun3835c0d2020-07-07 09:46:09 +020056 .sock_type = SOCK_DGRAM,
57 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020058 .add = udp4_add_listener,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020059 .listen = udp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020060 .enable = udp_enable_listener,
61 .disable = udp_disable_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020062 .rx_enable = sock_enable,
63 .rx_disable = sock_disable,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020064 .rx_suspend = udp_suspend_receiver,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020065 .receivers = LIST_HEAD_INIT(proto_udp4.receivers),
66 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +020067};
68
69INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
70
71/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_udp6 = {
73 .name = "udp6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020074 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020075 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020076 .sock_domain = AF_INET6,
Emeric Brun3835c0d2020-07-07 09:46:09 +020077 .sock_type = SOCK_DGRAM,
78 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020079 .add = udp6_add_listener,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020080 .listen = udp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020081 .enable = udp_enable_listener,
82 .disable = udp_disable_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020083 .rx_enable = sock_enable,
84 .rx_disable = sock_disable,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020085 .rx_suspend = udp_suspend_receiver,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020086 .receivers = LIST_HEAD_INIT(proto_udp6.receivers),
87 .nb_receivers = 0,
Emeric Brun3835c0d2020-07-07 09:46:09 +020088};
89
90INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
91
Emeric Brun3835c0d2020-07-07 09:46:09 +020092/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
93 * an error message in <errmsg> if the message is at most <errlen> bytes long
94 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
95 * The return value is composed from ERR_ABORT, ERR_WARN,
96 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
97 * was alright and that no message was returned. ERR_RETRYABLE means that an
98 * error occurred but that it may vanish after a retry (eg: port in use), and
99 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
100 * the meaning of the error, but just indicate that a message is present which
101 * should be displayed with the respective level. Last, ERR_ABORT indicates
102 * that it's pointless to try to start other listeners. No error message is
103 * returned if errlen is NULL.
104 */
105int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
106{
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200107 int err = ERR_NONE;
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200108 char *msg = NULL;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200109
110 /* ensure we never return garbage */
111 if (errlen)
112 *errmsg = 0;
113
114 if (listener->state != LI_ASSIGNED)
115 return ERR_NONE; /* already bound */
116
Willy Tarreauad33acf2020-09-02 18:40:02 +0200117 if (!(listener->rx.flags & RX_F_BOUND)) {
118 msg = "receiving socket not bound";
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200119 goto udp_return;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200120 }
121
Willy Tarreaua37b2442020-09-24 07:23:45 +0200122 listener_set_state(listener, LI_LISTEN);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200123
Emeric Brun3835c0d2020-07-07 09:46:09 +0200124 udp_return:
125 if (msg && errlen) {
126 char pn[INET6_ADDRSTRLEN];
127
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200128 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
129 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Emeric Brun3835c0d2020-07-07 09:46:09 +0200130 }
131 return err;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200132}
133
Emeric Brun3835c0d2020-07-07 09:46:09 +0200134/* Add <listener> to the list of udp4 listeners, on port <port>. The
135 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
136 * The number of listeners for the protocol is updated.
137 */
138static void udp4_add_listener(struct listener *listener, int port)
139{
140 if (listener->state != LI_INIT)
141 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200142 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200143 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200144 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200145 LIST_ADDQ(&proto_udp4.receivers, &listener->rx.proto_list);
146 proto_udp4.nb_receivers++;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200147}
148
149/* Add <listener> to the list of udp6 listeners, on port <port>. The
150 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
151 * The number of listeners for the protocol is updated.
152 */
153static void udp6_add_listener(struct listener *listener, int port)
154{
155 if (listener->state != LI_INIT)
156 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200157 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200158 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200159 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200160 LIST_ADDQ(&proto_udp6.receivers, &listener->rx.proto_list);
161 proto_udp6.nb_receivers++;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200162}
163
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200164/* Enable receipt of incoming connections for listener <l>. The receiver must
165 * still be valid. Does nothing in early boot (needs fd_updt).
166 */
167static void udp_enable_listener(struct listener *l)
168{
169 if (fd_updt)
170 fd_want_recv(l->rx.fd);
171}
172
173/* Disable receipt of incoming connections for listener <l>. The receiver must
174 * still be valid. Does nothing in early boot (needs fd_updt).
175 */
176static void udp_disable_listener(struct listener *l)
177{
178 if (fd_updt)
179 fd_stop_recv(l->rx.fd);
180}
181
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200182/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
183 * was totally stopped, or > 0 if correctly suspended.
Emeric Brun3835c0d2020-07-07 09:46:09 +0200184 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200185static int udp_suspend_receiver(struct receiver *rx)
Emeric Brun3835c0d2020-07-07 09:46:09 +0200186{
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200187 /* we don't support suspend on UDP */
Emeric Brun3835c0d2020-07-07 09:46:09 +0200188 return -1;
189}
190
191/*
192 * Local variables:
193 * c-indent-level: 8
194 * c-basic-offset: 8
195 * End:
196 */