blob: aa2416f1200b23f1099b2fca99a7f2e8b4eb380f [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);
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,
Willy Tarreaua54553f2020-09-16 17:50:45 +020051 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020052 .sock_domain = AF_INET,
Emeric Brun3835c0d2020-07-07 09:46:09 +020053 .sock_type = SOCK_DGRAM,
54 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020055 .accept = NULL,
56 .connect = NULL,
Willy Tarreaub3580b12020-09-01 10:26:22 +020057 .listen = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020058 .pause = udp_pause_listener,
59 .add = udp4_add_listener,
60 .listeners = LIST_HEAD_INIT(proto_udp4.listeners),
61 .nb_listeners = 0,
62};
63
64INITCALL1(STG_REGISTER, protocol_register, &proto_udp4);
65
66/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_udp6 = {
68 .name = "udp6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020069 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020070 .ctrl_type = SOCK_DGRAM,
Willy Tarreau2b5e0d82020-09-16 21:58:52 +020071 .sock_domain = AF_INET6,
Emeric Brun3835c0d2020-07-07 09:46:09 +020072 .sock_type = SOCK_DGRAM,
73 .sock_prot = IPPROTO_UDP,
Emeric Brun3835c0d2020-07-07 09:46:09 +020074 .accept = NULL,
75 .connect = NULL,
Willy Tarreaub3580b12020-09-01 10:26:22 +020076 .listen = udp_bind_listener,
Emeric Brun3835c0d2020-07-07 09:46:09 +020077 .pause = udp_pause_listener,
78 .add = udp6_add_listener,
79 .listeners = LIST_HEAD_INIT(proto_udp6.listeners),
80 .nb_listeners = 0,
81};
82
83INITCALL1(STG_REGISTER, protocol_register, &proto_udp6);
84
Emeric Brun3835c0d2020-07-07 09:46:09 +020085/* This function tries to bind a UDPv4/v6 listener. It may return a warning or
86 * an error message in <errmsg> if the message is at most <errlen> bytes long
87 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
88 * The return value is composed from ERR_ABORT, ERR_WARN,
89 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
90 * was alright and that no message was returned. ERR_RETRYABLE means that an
91 * error occurred but that it may vanish after a retry (eg: port in use), and
92 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
93 * the meaning of the error, but just indicate that a message is present which
94 * should be displayed with the respective level. Last, ERR_ABORT indicates
95 * that it's pointless to try to start other listeners. No error message is
96 * returned if errlen is NULL.
97 */
98int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
99{
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200100 int err = ERR_NONE;
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200101 char *msg = NULL;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200102
103 /* ensure we never return garbage */
104 if (errlen)
105 *errmsg = 0;
106
107 if (listener->state != LI_ASSIGNED)
108 return ERR_NONE; /* already bound */
109
Willy Tarreauad33acf2020-09-02 18:40:02 +0200110 if (!(listener->rx.flags & RX_F_BOUND)) {
111 msg = "receiving socket not bound";
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200112 goto udp_return;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200113 }
114
Willy Tarreaua37b2442020-09-24 07:23:45 +0200115 listener_set_state(listener, LI_LISTEN);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200116
Emeric Brun3835c0d2020-07-07 09:46:09 +0200117 udp_return:
118 if (msg && errlen) {
119 char pn[INET6_ADDRSTRLEN];
120
Willy Tarreau2f7687d2020-09-01 16:23:29 +0200121 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
122 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Emeric Brun3835c0d2020-07-07 09:46:09 +0200123 }
124 return err;
Emeric Brun3835c0d2020-07-07 09:46:09 +0200125}
126
Emeric Brun3835c0d2020-07-07 09:46:09 +0200127/* Add <listener> to the list of udp4 listeners, on port <port>. The
128 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
129 * The number of listeners for the protocol is updated.
130 */
131static void udp4_add_listener(struct listener *listener, int port)
132{
133 if (listener->state != LI_INIT)
134 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200135 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200136 listener->rx.proto = &proto_udp4;
Willy Tarreau37159062020-08-27 07:48:42 +0200137 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200138 LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200139 proto_udp4.nb_listeners++;
140}
141
142/* Add <listener> to the list of udp6 listeners, on port <port>. The
143 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
144 * The number of listeners for the protocol is updated.
145 */
146static void udp6_add_listener(struct listener *listener, int port)
147{
148 if (listener->state != LI_INIT)
149 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200150 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200151 listener->rx.proto = &proto_udp6;
Willy Tarreau37159062020-08-27 07:48:42 +0200152 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200153 LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list);
Emeric Brun3835c0d2020-07-07 09:46:09 +0200154 proto_udp6.nb_listeners++;
155}
156
157/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
158 * was totally stopped, or > 0 if correctly paused.
159 */
160int udp_pause_listener(struct listener *l)
161{
162 /* we don't support pausing on UDP */
163 return -1;
164}
165
166/*
167 * Local variables:
168 * c-indent-level: 8
169 * c-basic-offset: 8
170 * End:
171 */