blob: c13674b7c25c0ec2fab218798f8458e7483dd5d6 [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) {
46 if (l->addr.ss_family == AF_INET6)
47 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) {
59 if (l->addr.ss_family == AF_INET6)
60 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) {
74 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
75 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) {
89 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
90 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) {
104 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
105 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) {
131 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
132 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) {
169 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
170 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{
181 struct listener *l;
182
183 if (!*args[cur_arg + 1]) {
184 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
185 return ERR_ALERT | ERR_FATAL;
186 }
187
188 list_for_each_entry(l, &conf->listeners, by_bind) {
189 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
190 l->interface = strdup(args[cur_arg + 1]);
191 }
192
193 return 0;
194}
195#endif
196
197#ifdef USE_NS
198/* parse the "namespace" bind keyword */
199static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
200{
201 struct listener *l;
202 char *namespace = NULL;
203
204 if (!*args[cur_arg + 1]) {
205 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
206 return ERR_ALERT | ERR_FATAL;
207 }
208 namespace = args[cur_arg + 1];
209
210 list_for_each_entry(l, &conf->listeners, by_bind) {
211 l->netns = netns_store_lookup(namespace, strlen(namespace));
212
213 if (l->netns == NULL)
214 l->netns = netns_store_insert(namespace);
215
216 if (l->netns == NULL) {
217 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
218 return ERR_ALERT | ERR_FATAL;
219 }
220 }
221 return 0;
222}
223#endif
224
225#ifdef TCP_USER_TIMEOUT
226/* parse the "tcp-ut" server keyword */
227static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
228{
229 const char *ptr = NULL;
230 unsigned int timeout;
231
232 if (!*args[*cur_arg + 1]) {
233 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
234 return ERR_ALERT | ERR_FATAL;
235 }
236
237 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
238 if (ptr == PARSE_TIME_OVER) {
239 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
240 args[*cur_arg+1], args[*cur_arg]);
241 return ERR_ALERT | ERR_FATAL;
242 }
243 else if (ptr == PARSE_TIME_UNDER) {
244 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
245 args[*cur_arg+1], args[*cur_arg]);
246 return ERR_ALERT | ERR_FATAL;
247 }
248 else if (ptr) {
249 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
250 return ERR_ALERT | ERR_FATAL;
251 }
252
253 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
254 newsrv->tcp_ut = timeout;
255
256 return 0;
257}
258#endif
259
260
261/************************************************************************/
262/* All supported bind keywords must be declared here. */
263/************************************************************************/
264
265/* Note: must not be declared <const> as its list will be overwritten.
266 * Please take care of keeping this list alphabetically sorted, doing so helps
267 * all code contributors.
268 * Optional keywords are also declared with a NULL ->parse() function so that
269 * the config parser can report an appropriate error when a known keyword was
270 * not enabled.
271 */
272static struct bind_kw_list bind_kws = { "TCP", { }, {
273#ifdef TCP_DEFER_ACCEPT
274 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
275#endif
276#ifdef SO_BINDTODEVICE
277 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
278#endif
279#ifdef TCP_MAXSEG
280 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
281#endif
282#ifdef TCP_USER_TIMEOUT
283 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
284#endif
285#ifdef TCP_FASTOPEN
286 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
287#endif
288#ifdef CONFIG_HAP_TRANSPARENT
289 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
290#endif
291#ifdef IPV6_V6ONLY
292 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
293 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
294#endif
295#ifdef USE_NS
296 { "namespace", bind_parse_namespace, 1 },
297#endif
298 /* the versions with the NULL parse function*/
299 { "defer-accept", NULL, 0 },
300 { "interface", NULL, 1 },
301 { "mss", NULL, 1 },
302 { "transparent", NULL, 0 },
303 { "v4v6", NULL, 0 },
304 { "v6only", NULL, 0 },
305 { NULL, NULL, 0 },
306}};
307
308INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
309
310static struct srv_kw_list srv_kws = { "TCP", { }, {
311#ifdef TCP_USER_TIMEOUT
312 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
313#endif
314 { NULL, NULL, 0 },
315}};
316
317INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
318
319
320REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
321#if defined(IP_TRANSPARENT)
322 " IP_TRANSPARENT"
323#endif
324#if defined(IPV6_TRANSPARENT)
325 " IPV6_TRANSPARENT"
326#endif
327#if defined(IP_FREEBIND)
328 " IP_FREEBIND"
329#endif
330#if defined(IP_BINDANY)
331 " IP_BINDANY"
332#endif
333#if defined(IPV6_BINDANY)
334 " IPV6_BINDANY"
335#endif
336#if defined(SO_BINDANY)
337 " SO_BINDANY"
338#endif
339 "");
340
341
342/*
343 * Local variables:
344 * c-indent-level: 8
345 * c-basic-offset: 8
346 * End:
347 */