blob: 7346d652b9f6d979d60d9a0c53d812b2ab83654b [file] [log] [blame]
Willy Tarreaude70ca52020-08-28 11:49:31 +02001/*
2 * Configuration parsing for TCP (bind and server keywords)
3 *
4 * Copyright 2000-2020 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24
25#include <netinet/tcp.h>
26#include <netinet/in.h>
27
28#include <haproxy/api.h>
29#include <haproxy/arg.h>
30#include <haproxy/errors.h>
31#include <haproxy/list.h>
32#include <haproxy/listener.h>
33#include <haproxy/namespace.h>
34#include <haproxy/proxy-t.h>
35#include <haproxy/server.h>
36#include <haproxy/tools.h>
37
38
39#ifdef IPV6_V6ONLY
40/* parse the "v4v6" bind keyword */
41static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
42{
43 struct listener *l;
44
45 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +020046 if (l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +020047 l->options |= LI_O_V4V6;
48 }
49
50 return 0;
51}
52
53/* parse the "v6only" bind keyword */
54static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
55{
56 struct listener *l;
57
58 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +020059 if (l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +020060 l->options |= LI_O_V6ONLY;
61 }
62
63 return 0;
64}
65#endif
66
67#ifdef CONFIG_HAP_TRANSPARENT
68/* parse the "transparent" bind keyword */
69static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
70{
71 struct listener *l;
72
73 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +020074 if (l->rx.addr.ss_family == AF_INET || l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +020075 l->options |= LI_O_FOREIGN;
76 }
77
78 return 0;
79}
80#endif
81
82#ifdef TCP_DEFER_ACCEPT
83/* parse the "defer-accept" bind keyword */
84static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
85{
86 struct listener *l;
87
88 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +020089 if (l->rx.addr.ss_family == AF_INET || l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +020090 l->options |= LI_O_DEF_ACCEPT;
91 }
92
93 return 0;
94}
95#endif
96
97#ifdef TCP_FASTOPEN
98/* parse the "tfo" bind keyword */
99static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
100{
101 struct listener *l;
102
103 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +0200104 if (l->rx.addr.ss_family == AF_INET || l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +0200105 l->options |= LI_O_TCP_FO;
106 }
107
108 return 0;
109}
110#endif
111
112#ifdef TCP_MAXSEG
113/* parse the "mss" bind keyword */
114static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
115{
116 struct listener *l;
117 int mss;
118
119 if (!*args[cur_arg + 1]) {
120 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
121 return ERR_ALERT | ERR_FATAL;
122 }
123
124 mss = atoi(args[cur_arg + 1]);
125 if (!mss || abs(mss) > 65535) {
126 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
127 return ERR_ALERT | ERR_FATAL;
128 }
129
130 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +0200131 if (l->rx.addr.ss_family == AF_INET || l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +0200132 l->maxseg = mss;
133 }
134
135 return 0;
136}
137#endif
138
139#ifdef TCP_USER_TIMEOUT
140/* parse the "tcp-ut" bind keyword */
141static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
142{
143 const char *ptr = NULL;
144 struct listener *l;
145 unsigned int timeout;
146
147 if (!*args[cur_arg + 1]) {
148 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
149 return ERR_ALERT | ERR_FATAL;
150 }
151
152 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
153 if (ptr == PARSE_TIME_OVER) {
154 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
155 args[cur_arg+1], args[cur_arg]);
156 return ERR_ALERT | ERR_FATAL;
157 }
158 else if (ptr == PARSE_TIME_UNDER) {
159 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
160 args[cur_arg+1], args[cur_arg]);
161 return ERR_ALERT | ERR_FATAL;
162 }
163 else if (ptr) {
164 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
165 return ERR_ALERT | ERR_FATAL;
166 }
167
168 list_for_each_entry(l, &conf->listeners, by_bind) {
Willy Tarreau37159062020-08-27 07:48:42 +0200169 if (l->rx.addr.ss_family == AF_INET || l->rx.addr.ss_family == AF_INET6)
Willy Tarreaude70ca52020-08-28 11:49:31 +0200170 l->tcp_ut = timeout;
171 }
172
173 return 0;
174}
175#endif
176
177#ifdef SO_BINDTODEVICE
178/* parse the "interface" bind keyword */
179static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
180{
Willy Tarreaude70ca52020-08-28 11:49:31 +0200181 if (!*args[cur_arg + 1]) {
182 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
183 return ERR_ALERT | ERR_FATAL;
184 }
185
Willy Tarreau7e307212020-09-03 07:23:34 +0200186 conf->settings.interface = strdup(args[cur_arg + 1]);
Willy Tarreaude70ca52020-08-28 11:49:31 +0200187 return 0;
188}
189#endif
190
191#ifdef USE_NS
192/* parse the "namespace" bind keyword */
193static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
194{
Willy Tarreaude70ca52020-08-28 11:49:31 +0200195 char *namespace = NULL;
196
197 if (!*args[cur_arg + 1]) {
198 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
199 return ERR_ALERT | ERR_FATAL;
200 }
201 namespace = args[cur_arg + 1];
202
Willy Tarreaube56c102020-09-03 07:27:34 +0200203 conf->settings.netns = netns_store_lookup(namespace, strlen(namespace));
Willy Tarreaude70ca52020-08-28 11:49:31 +0200204
Willy Tarreaube56c102020-09-03 07:27:34 +0200205 if (conf->settings.netns == NULL)
206 conf->settings.netns = netns_store_insert(namespace);
Willy Tarreaude70ca52020-08-28 11:49:31 +0200207
Willy Tarreaube56c102020-09-03 07:27:34 +0200208 if (conf->settings.netns == NULL) {
209 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
210 return ERR_ALERT | ERR_FATAL;
Willy Tarreaude70ca52020-08-28 11:49:31 +0200211 }
212 return 0;
213}
214#endif
215
216#ifdef TCP_USER_TIMEOUT
217/* parse the "tcp-ut" server keyword */
218static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
219{
220 const char *ptr = NULL;
221 unsigned int timeout;
222
223 if (!*args[*cur_arg + 1]) {
224 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
225 return ERR_ALERT | ERR_FATAL;
226 }
227
228 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
229 if (ptr == PARSE_TIME_OVER) {
230 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
231 args[*cur_arg+1], args[*cur_arg]);
232 return ERR_ALERT | ERR_FATAL;
233 }
234 else if (ptr == PARSE_TIME_UNDER) {
235 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
236 args[*cur_arg+1], args[*cur_arg]);
237 return ERR_ALERT | ERR_FATAL;
238 }
239 else if (ptr) {
240 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
241 return ERR_ALERT | ERR_FATAL;
242 }
243
244 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
245 newsrv->tcp_ut = timeout;
246
247 return 0;
248}
249#endif
250
251
252/************************************************************************/
253/* All supported bind keywords must be declared here. */
254/************************************************************************/
255
256/* Note: must not be declared <const> as its list will be overwritten.
257 * Please take care of keeping this list alphabetically sorted, doing so helps
258 * all code contributors.
259 * Optional keywords are also declared with a NULL ->parse() function so that
260 * the config parser can report an appropriate error when a known keyword was
261 * not enabled.
262 */
263static struct bind_kw_list bind_kws = { "TCP", { }, {
264#ifdef TCP_DEFER_ACCEPT
265 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
266#endif
267#ifdef SO_BINDTODEVICE
268 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
269#endif
270#ifdef TCP_MAXSEG
271 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
272#endif
273#ifdef TCP_USER_TIMEOUT
274 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
275#endif
276#ifdef TCP_FASTOPEN
277 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
278#endif
279#ifdef CONFIG_HAP_TRANSPARENT
280 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
281#endif
282#ifdef IPV6_V6ONLY
283 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
284 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
285#endif
286#ifdef USE_NS
287 { "namespace", bind_parse_namespace, 1 },
288#endif
289 /* the versions with the NULL parse function*/
290 { "defer-accept", NULL, 0 },
291 { "interface", NULL, 1 },
292 { "mss", NULL, 1 },
293 { "transparent", NULL, 0 },
294 { "v4v6", NULL, 0 },
295 { "v6only", NULL, 0 },
296 { NULL, NULL, 0 },
297}};
298
299INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
300
301static struct srv_kw_list srv_kws = { "TCP", { }, {
302#ifdef TCP_USER_TIMEOUT
303 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
304#endif
305 { NULL, NULL, 0 },
306}};
307
308INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
309
310
311REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
312#if defined(IP_TRANSPARENT)
313 " IP_TRANSPARENT"
314#endif
315#if defined(IPV6_TRANSPARENT)
316 " IPV6_TRANSPARENT"
317#endif
318#if defined(IP_FREEBIND)
319 " IP_FREEBIND"
320#endif
321#if defined(IP_BINDANY)
322 " IP_BINDANY"
323#endif
324#if defined(IPV6_BINDANY)
325 " IPV6_BINDANY"
326#endif
327#if defined(SO_BINDANY)
328 " SO_BINDANY"
329#endif
330 "");
331
332
333/*
334 * Local variables:
335 * c-indent-level: 8
336 * c-basic-offset: 8
337 * End:
338 */