blob: d4d6b2afa5e0344c8bb4e1c7dbeb5e10679d5bcf [file] [log] [blame]
Emeric Brun3835c0d2020-07-07 09:46:09 +02001/*
2 * AF_CUST_UDP/AF_CUST_UDP6 UDP protocol layer
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 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);
44static void udp4_add_listener(struct listener *listener, int port);
45static void udp6_add_listener(struct listener *listener, int port);
46
47/* Note: must not be declared <const> as its list will be overwritten */
48static struct protocol proto_udp4 = {
49 .name = "udp4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020050 .fam = &proto_fam_inet4,
Emeric Brun3835c0d2020-07-07 09:46:09 +020051 .sock_domain = AF_CUST_UDP4,
52 .sock_type = SOCK_DGRAM,
53 .sock_prot = IPPROTO_UDP,
54 .sock_family = AF_INET,
55 .sock_addrlen = sizeof(struct sockaddr_in),
56 .l3_addrlen = 32/8,
57 .accept = NULL,
58 .connect = NULL,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020059 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020060 .listen = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020061 .enable_all = enable_all_listeners,
62 .get_src = udp_get_src,
63 .get_dst = udp_get_dst,
64 .pause = udp_pause_listener,
65 .add = udp4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020066 .addrcmp = sock_inet4_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020067 .listeners = LIST_HEAD_INIT(proto_udp4.listeners),
68 .nb_listeners = 0,
69};
70
71INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
72
73/* Note: must not be declared <const> as its list will be overwritten */
74static struct protocol proto_udp6 = {
75 .name = "udp6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020076 .fam = &proto_fam_inet6,
Emeric Brun3835c0d2020-07-07 09:46:09 +020077 .sock_domain = AF_CUST_UDP6,
78 .sock_type = SOCK_DGRAM,
79 .sock_prot = IPPROTO_UDP,
80 .sock_family = AF_INET6,
81 .sock_addrlen = sizeof(struct sockaddr_in6),
82 .l3_addrlen = 128/8,
83 .accept = NULL,
84 .connect = NULL,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020085 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020086 .listen = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020087 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020088 .get_src = udp6_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020089 .get_dst = udp6_get_dst,
Emeric Brun3835c0d2020-07-07 09:46:09 +020090 .pause = udp_pause_listener,
91 .add = udp6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020092 .addrcmp = sock_inet6_addrcmp,
Emeric Brun3835c0d2020-07-07 09:46:09 +020093 .listeners = LIST_HEAD_INIT(proto_udp6.listeners),
94 .nb_listeners = 0,
95};
96
97INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
98
99/*
100 * Retrieves the source address for the socket <fd>, with <dir> indicating
101 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
102 * success, -1 in case of error. The socket's source address is stored in
103 * <sa> for <salen> bytes.
104 */
105int udp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
106{
107 int ret;
108
Willy Tarreau18b7df72020-08-28 12:07:22 +0200109 ret = sock_get_src(fd, sa, salen, dir);
110 if (!ret)
111 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200112
113 return ret;
114}
115
Willy Tarreau18b7df72020-08-28 12:07:22 +0200116/*
117 * Retrieves the source address for the socket <fd>, with <dir> indicating
118 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
119 * success, -1 in case of error. The socket's source address is stored in
120 * <sa> for <salen> bytes.
121 */
122int udp6_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
123{
124 int ret;
125
126 ret = sock_get_src(fd, sa, salen, dir);
127 if (!ret)
128 sa->sa_family = AF_CUST_UDP6;
129
130 return ret;
131}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200132
133/*
134 * Retrieves the original destination address for the socket <fd>, with <dir>
135 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
136 * listener, if the original destination address was translated, the original
137 * address is retrieved. It returns 0 in case of success, -1 in case of error.
138 * The socket's source address is stored in <sa> for <salen> bytes.
139 */
140int udp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
141{
142 int ret;
143
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200144 ret = sock_inet_get_dst(fd, sa, salen, dir);
145 if (!ret)
146 sa->sa_family = AF_CUST_UDP4;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200147
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200148 return ret;
149}
Emeric Brun3835c0d2020-07-07 09:46:09 +0200150
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200151/*
152 * Retrieves the original destination address for the socket <fd>, with <dir>
153 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
154 * listener, if the original destination address was translated, the original
155 * address is retrieved. It returns 0 in case of success, -1 in case of error.
156 * The socket's source address is stored in <sa> for <salen> bytes.
157 */
158int udp6_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
159{
160 int ret;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200161
Willy Tarreauc5a94c92020-08-28 15:19:45 +0200162 ret = sock_get_dst(fd, sa, salen, dir);
163 if (!ret)
164 sa->sa_family = AF_CUST_UDP6;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200165
166 return ret;
167}
168
169/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
170 * an error message in <errmsg> if the message is at most <errlen> bytes long
171 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
172 * The return value is composed from ERR_ABORT, ERR_WARN,
173 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
174 * was alright and that no message was returned. ERR_RETRYABLE means that an
175 * error occurred but that it may vanish after a retry (eg: port in use), and
176 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
177 * the meaning of the error, but just indicate that a message is present which
178 * should be displayed with the respective level. Last, ERR_ABORT indicates
179 * that it's pointless to try to start other listeners. No error message is
180 * returned if errlen is NULL.
181 */
182int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
183{
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200184 int err = ERR_NONE;
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200185 char *msg = NULL;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200186
187 /* ensure we never return garbage */
188 if (errlen)
189 *errmsg = 0;
190
191 if (listener->state != LI_ASSIGNED)
192 return ERR_NONE; /* already bound */
193
Willy Tarreauad33acf2020-09-02 18:40:02 +0200194 if (!(listener->rx.flags & RX_F_BOUND)) {
195 msg = "receiving socket not bound";
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200196 goto udp_return;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200197 }
198
Emeric Brun3835c0d2020-07-07 09:46:09 +0200199 listener->state = LI_LISTEN;
200
Emeric Brun3835c0d2020-07-07 09:46:09 +0200201 udp_return:
202 if (msg && errlen) {
203 char pn[INET6_ADDRSTRLEN];
204
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200205 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
206 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Emeric Brun3835c0d2020-07-07 09:46:09 +0200207 }
208 return err;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200209}
210
Emeric Brun3835c0d2020-07-07 09:46:09 +0200211/* Add <listener> to the list of udp4 listeners, on port <port>. The
212 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
213 * The number of listeners for the protocol is updated.
214 */
215static void udp4_add_listener(struct listener *listener, int port)
216{
217 if (listener->state != LI_INIT)
218 return;
219 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200220 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200221 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200222 LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200223 proto_udp4.nb_listeners++;
224}
225
226/* Add <listener> to the list of udp6 listeners, on port <port>. The
227 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
228 * The number of listeners for the protocol is updated.
229 */
230static void udp6_add_listener(struct listener *listener, int port)
231{
232 if (listener->state != LI_INIT)
233 return;
234 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200235 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200236 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200237 LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200238 proto_udp6.nb_listeners++;
239}
240
241/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
242 * was totally stopped, or > 0 if correctly paused.
243 */
244int udp_pause_listener(struct listener *l)
245{
246 /* we don't support pausing on UDP */
247 return -1;
248}
249
250/*
251 * Local variables:
252 * c-indent-level: 8
253 * c-basic-offset: 8
254 * End:
255 */