blob: 4b71ddbcb0298bfd3eb178f05007f146fb12cda5 [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreaud1d54542012-09-12 22:58:11 +02002 * include/types/listener.h
3 * This file defines the structures needed to manage listeners.
Willy Tarreaube58c382011-07-24 18:28:10 +02004 *
Willy Tarreaud1d54542012-09-12 22:58:11 +02005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreaube58c382011-07-24 18:28:10 +02006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaudd815982007-10-16 12:25:14 +020021
Willy Tarreaud1d54542012-09-12 22:58:11 +020022#ifndef _TYPES_LISTENER_H
23#define _TYPES_LISTENER_H
Willy Tarreaudd815982007-10-16 12:25:14 +020024
25#include <sys/types.h>
26#include <sys/socket.h>
27
Emeric Brun0b8d4d92012-05-18 15:46:21 +020028#ifdef USE_OPENSSL
Willy Tarreau8d164dc2019-05-10 09:35:00 +020029#include <common/openssl-compat.h>
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020030#include <types/ssl_sock.h>
Emeric Brun0b8d4d92012-05-18 15:46:21 +020031#endif
32
Willy Tarreaudd815982007-10-16 12:25:14 +020033#include <common/config.h>
34#include <common/mini-clist.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020035#include <common/hathreads.h>
36
Willy Tarreau3fdb3662012-11-12 00:42:33 +010037#include <types/obj_type.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010038#include <eb32tree.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020039
Willy Tarreaud1d54542012-09-12 22:58:11 +020040/* Some pointer types reference below */
41struct task;
42struct protocol;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020043struct xprt_ops;
Willy Tarreaud1d54542012-09-12 22:58:11 +020044struct proxy;
Willy Tarreauae9bea02016-11-25 14:44:52 +010045struct fe_counters;
Willy Tarreaudd815982007-10-16 12:25:14 +020046
Willy Tarreaudd815982007-10-16 12:25:14 +020047/* listener state */
Willy Tarreauf6502c52013-12-06 21:18:49 +010048enum li_state {
Willy Tarreaube58c382011-07-24 18:28:10 +020049 LI_NEW = 0, /* not initialized yet */
50 LI_INIT, /* all parameters filled in, but not assigned yet */
51 LI_ASSIGNED, /* assigned to the protocol, but not listening yet */
52 LI_PAUSED, /* listener was paused, it's bound but not listening */
Olivier Houchard1fc05162017-04-06 01:05:05 +020053 LI_ZOMBIE, /* The listener doesn't belong to the process, but is kept opened */
Willy Tarreaube58c382011-07-24 18:28:10 +020054 LI_LISTEN, /* started, listening but not enabled */
55 LI_READY, /* started, listening and enabled */
56 LI_FULL, /* reached its connection limit */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020057 LI_LIMITED, /* transient state: limits have been reached, listener is queued */
Willy Tarreauf6502c52013-12-06 21:18:49 +010058} __attribute__((packed));
Willy Tarreaudabf2e22007-10-28 21:59:24 +010059
60/* Listener transitions
61 * calloc() set() add_listener() bind()
62 * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN
63 * <------- <---- <---------- <-----
64 * free() bzero() del_listener() unbind()
65 *
66 * The file descriptor is valid only during these three states :
67 *
68 * disable()
69 * LISTEN <------------ READY
70 * A| ------------> |A
71 * || !max & enable() ||
72 * || ||
73 * || max ||
74 * || max & enable() V| !max
75 * |+---------------> FULL
76 * +-----------------
77 * disable()
78 *
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020079 * The LIMITED state my be used when a limit has been detected just before
80 * using a listener. In this case, the listener MUST be queued into the
81 * appropriate wait queue (either the proxy's or the global one). It may be
82 * set back to the READY state at any instant and for any reason, so one must
83 * not rely on this state.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010084 */
Willy Tarreaudd815982007-10-16 12:25:14 +020085
Willy Tarreau6fb42e02007-10-28 17:02:33 +010086/* listener socket options */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020087#define LI_O_NONE 0x0000
88#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
Joseph Herlant75a323f2018-11-25 13:36:58 -080089#define LI_O_FOREIGN 0x0002 /* permit listening on foreign addresses ("transparent") */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020090#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
91#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
92#define LI_O_TCP_L4_RULES 0x0010 /* run TCP L4 rules checks on the incoming connection */
Willy Tarreau620408f2016-10-21 16:37:51 +020093#define LI_O_TCP_L5_RULES 0x0020 /* run TCP L5 rules checks on the incoming session */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020094#define LI_O_CHK_MONNET 0x0040 /* check the source against a monitor-net rule */
95#define LI_O_ACC_PROXY 0x0080 /* find the proxied address in the first request line */
96#define LI_O_UNLIMITED 0x0100 /* listener not subject to global limits (peers & stats socket) */
97#define LI_O_TCP_FO 0x0200 /* enable TCP Fast Open (linux >= 3.7) */
98#define LI_O_V6ONLY 0x0400 /* bind to IPv6 only on Linux >= 2.4.21 */
99#define LI_O_V4V6 0x0800 /* bind to IPv4/IPv6 on Linux >= 2.4.21 */
100#define LI_O_ACC_CIP 0x1000 /* find the proxied address in the NetScaler Client IP header */
William Lallemand75ea0a02017-11-15 19:02:58 +0100101#define LI_O_INHERITED 0x2000 /* inherited FD from the parent process (fd@) */
William Lallemande22f11f2018-09-11 10:06:27 +0200102#define LI_O_MWORKER 0x4000 /* keep the FD open in the master but close it in the children */
William Lallemandc59f9882018-11-16 16:57:21 +0100103#define LI_O_NOSTOP 0x8000 /* keep the listener active even after a soft stop */
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200104
105/* Note: if a listener uses LI_O_UNLIMITED, it is highly recommended that it adds its own
106 * maxconn setting to the global.maxsock value so that its resources are reserved.
107 */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100108
Emeric Brun89675492012-10-05 13:48:26 +0200109#ifdef USE_OPENSSL
Emeric Brun89675492012-10-05 13:48:26 +0200110#define BC_SSL_O_NONE 0x0000
Emeric Brun89675492012-10-05 13:48:26 +0200111#define BC_SSL_O_NO_TLS_TICKETS 0x0100 /* disable session resumption tickets */
Lukas Tribus53ae85c2017-05-04 15:45:40 +0000112#define BC_SSL_O_PREF_CLIE_CIPH 0x0200 /* prefer client ciphers */
Emeric Brun89675492012-10-05 13:48:26 +0200113#endif
114
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100115/* ssl "bind" settings */
116struct ssl_bind_conf {
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200117#ifdef USE_OPENSSL
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100118#ifdef OPENSSL_NPN_NEGOTIATED
119 char *npn_str; /* NPN protocol string */
120 int npn_len; /* NPN protocol string length */
121#endif
122#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
123 char *alpn_str; /* ALPN protocol string */
124 int alpn_len; /* ALPN protocol string length */
125#endif
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100126 unsigned int verify:3; /* verify method (set of SSL_VERIFY_* flags) */
127 unsigned int no_ca_names:1;/* do not send ca names to clients (ca_file related) */
128 unsigned int early_data:1; /* early data allowed */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +0100129 char *ca_file; /* CAfile to use on verify and ca-names */
130 char *ca_verify_file; /* CAverify file to use on verify only */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200131 char *crl_file; /* CRLfile to use on verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100132 char *ciphers; /* cipher suite to use if non-null */
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200133#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200134 char *ciphersuites; /* TLS 1.3 cipher suite to use if non-null */
135#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +0100136 char *curves; /* curves suite to use for ECDHE */
Emeric Brun2b58d042012-09-20 17:10:03 +0200137 char *ecdhe; /* named curve to use for ECDHE */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +0200138 struct tls_version_filter ssl_methods; /* ssl methods */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100139#endif
140};
141
142/* "bind" line settings */
143struct bind_conf {
144#ifdef USE_OPENSSL
145 struct ssl_bind_conf ssl_conf; /* ssl conf for ctx setting */
146 unsigned long long ca_ignerr; /* ignored verify errors in handshake if depth > 0 */
147 unsigned long long crt_ignerr; /* ignored verify errors in handshake if depth == 0 */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +0100148 SSL_CTX *initial_ctx; /* SSL context for initial negotiation */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200149 SSL_CTX *default_ctx; /* SSL context of first/default certificate */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100150 struct ssl_bind_conf *default_ssl_conf; /* custom SSL conf of default_ctx */
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100151 int strict_sni; /* refuse negotiation if sni doesn't match a certificate */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +0100152 int ssl_options; /* ssl options */
William Lallemand150bfa82019-09-19 17:12:49 +0200153 __decl_hathreads(HA_RWLOCK_T sni_lock); /* lock the SNI trees during add/del operations */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200154 struct eb_root sni_ctx; /* sni_ctx tree of all known certs full-names sorted by name */
155 struct eb_root sni_w_ctx; /* sni_ctx tree of all known certs wildcards sorted by name */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200156 struct tls_keys_ref *keys_ref; /* TLS ticket keys reference */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200157
158 char *ca_sign_file; /* CAFile used to generate and sign server certificates */
159 char *ca_sign_pass; /* CAKey passphrase */
160
161 X509 *ca_sign_cert; /* CA certificate referenced by ca_file */
162 EVP_PKEY *ca_sign_pkey; /* CA private key referenced by ca_key */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200163#endif
Willy Tarreauc95bad52016-12-22 00:13:31 +0100164 struct proxy *frontend; /* the frontend all these listeners belong to, or NULL */
Willy Tarreaua004ae32018-12-02 13:03:57 +0100165 const struct mux_proto_list *mux_proto; /* the mux to use for all incoming connections (specified by the "proto" keyword) */
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100166 struct xprt_ops *xprt; /* transport-layer operations for all listeners */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200167 int is_ssl; /* SSL is required for these listeners */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200168 int generate_certs; /* 1 if generate-certificates option is set, else 0 */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200169 int level; /* stats access level (ACCESS_LVL_*) */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200170 int severity_output; /* default severity output format in cli feedback messages */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200171 struct list listeners; /* list of listeners using this bind config */
Willy Tarreaub2b50a72019-02-03 11:14:25 +0100172 unsigned long bind_proc; /* bitmask of processes allowed to use these listeners */
173 unsigned long bind_thread; /* bitmask of threads allowed to use these listeners */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100174 uint32_t ns_cip_magic; /* Excepted NetScaler Client IP magic number */
Willy Tarreaub2b50a72019-02-03 11:14:25 +0100175 struct list by_fe; /* next binding for the same frontend, or NULL */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200176 char *arg; /* argument passed to "bind" for better error reporting */
177 char *file; /* file where the section appears */
178 int line; /* line where the section appears */
Willy Tarreaub2b50a72019-02-03 11:14:25 +0100179 struct { /* UNIX socket permissions */
180 uid_t uid; /* -1 to leave unchanged */
181 gid_t gid; /* -1 to leave unchanged */
182 mode_t mode; /* 0 to leave unchanged */
183 } ux;
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200184};
185
Willy Tarreaudd815982007-10-16 12:25:14 +0200186/* The listener will be directly referenced by the fdtab[] which holds its
187 * socket. The listener provides the protocol-specific accept() function to
188 * the fdtab.
189 */
190struct listener {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100191 enum obj_type obj_type; /* object type = OBJ_TYPE_LISTENER */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100192 enum li_state state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500193 short int nice; /* nice value to assign to the instantiated tasks */
Willy Tarreaudd815982007-10-16 12:25:14 +0200194 int fd; /* the listen socket */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200195 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100196 int options; /* socket options : LI_O_* */
Willy Tarreauae9bea02016-11-25 14:44:52 +0100197 struct fe_counters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +0200198 struct protocol *proto; /* protocol this listener belongs to */
199 int nbconn; /* current number of connections on this listener */
200 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100201 unsigned int backlog; /* if set, listen backlog */
Christopher Faulet102854c2019-04-30 12:17:13 +0200202 int maxaccept; /* if set, max number of connections accepted at once (-1 when disabled) */
Willy Tarreaueb472682010-05-28 18:46:57 +0200203 int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */
Willy Tarreau10b688f2015-03-13 16:43:12 +0100204 enum obj_type *default_target; /* default target to use for accepted sessions or NULL */
Willy Tarreau4ed84c92019-02-03 10:28:24 +0100205 /* cache line boundary */
Olivier Houchard859dc802019-08-08 15:47:21 +0200206 struct mt_list wait_queue; /* link element to make the listener wait for something (LI_LIMITED) */
Willy Tarreau0cf33172019-03-06 15:26:33 +0100207 unsigned int thr_idx; /* thread indexes for queue distribution : (t2<<16)+t1 */
Willy Tarreau3bc13772008-12-07 11:50:35 +0100208 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200209 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau2af207a2015-02-04 00:45:58 +0100210 int tcp_ut; /* for TCP, user timeout */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100211 char *interface; /* interface name or NULL */
Willy Tarreau4ed84c92019-02-03 10:28:24 +0100212 char *name; /* listener's name */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200213
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100214 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200215
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100216 const struct netns_entry *netns; /* network namespace of the listener*/
217
Willy Tarreau4ed84c92019-02-03 10:28:24 +0100218 /* cache line boundary */
Willy Tarreau9e853182019-02-03 10:36:29 +0100219 unsigned int thr_conn[MAX_THREADS]; /* number of connections per thread */
220
221 /* cache line boundary */
Willy Tarreau0cf33172019-03-06 15:26:33 +0100222
Willy Tarreau4348fad2012-09-20 16:48:07 +0200223 struct list by_fe; /* chaining in frontend's list of listeners */
224 struct list by_bind; /* chaining in bind_conf's list of listeners */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200225 struct bind_conf *bind_conf; /* "bind" line settings, include SSL settings among other things */
Willy Tarreau4ed84c92019-02-03 10:28:24 +0100226 struct list proto_list; /* list in the protocol header */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200227
Willy Tarreau7b815632011-10-21 18:51:57 +0200228 /* warning: this struct is huge, keep it at the bottom */
229 struct sockaddr_storage addr; /* the address we listen to */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200230 struct {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200231 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200232 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200233};
234
Willy Tarreau26982662012-09-12 23:17:10 +0200235/* Descriptor for a "bind" keyword. The ->parse() function returns 0 in case of
236 * success, or a combination of ERR_* flags if an error is encountered. The
237 * function pointer can be NULL if not implemented. The function also has an
Willy Tarreau4348fad2012-09-20 16:48:07 +0200238 * access to the current "bind" config line. The ->skip value tells the parser
239 * how many words have to be skipped after the keyword.
Willy Tarreau26982662012-09-12 23:17:10 +0200240 */
241struct bind_kw {
242 const char *kw;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200243 int (*parse)(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err);
Willy Tarreau26982662012-09-12 23:17:10 +0200244 int skip; /* nb of args to skip */
245};
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100246struct ssl_bind_kw {
247 const char *kw;
248 int (*parse)(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err);
249 int skip; /* nb of args to skip */
250};
Willy Tarreau26982662012-09-12 23:17:10 +0200251
252/*
253 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
254 * struct list in order to be linked to other lists, allowing it to easily
255 * be declared where it is needed, and linked without duplicating data nor
Willy Tarreau51fb7652012-09-18 18:24:39 +0200256 * allocating memory. It is also possible to indicate a scope for the keywords.
Willy Tarreau26982662012-09-12 23:17:10 +0200257 */
258struct bind_kw_list {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200259 const char *scope;
Willy Tarreau26982662012-09-12 23:17:10 +0200260 struct list list;
261 struct bind_kw kw[VAR_ARRAY];
262};
263
264
Olivier Houchardf73629d2017-04-05 22:33:04 +0200265struct xfer_sock_list {
266 int fd;
267 char *iface;
268 char *namespace;
269 int options; /* socket options LI_O_* */
270 struct xfer_sock_list *prev;
271 struct xfer_sock_list *next;
272 struct sockaddr_storage addr;
273};
274
Willy Tarreau1efafce2019-01-27 15:37:19 +0100275/* This is used to create the accept queue, optimized to be 64 bytes long. */
276struct accept_queue_entry {
277 struct listener *listener; // 8 bytes
278 int fd __attribute__((aligned(8))); // 4 bytes
279 int addr_len; // 4 bytes
280
281 union {
282 sa_family_t family; // 2 bytes
283 struct sockaddr_in in; // 16 bytes
284 struct sockaddr_in6 in6; // 28 bytes
285 } addr; // this is normally 28 bytes
286 /* 20-bytes hole here */
287 char pad0[0] __attribute((aligned(64)));
288};
289
290/* The per-thread accept queue ring, must be a power of two minus 1 */
291#define ACCEPT_QUEUE_SIZE ((1<<8) - 1)
292
293struct accept_queue_ring {
294 unsigned int head;
295 unsigned int tail;
Willy Tarreau2bd65a72019-09-24 06:55:18 +0200296 struct tasklet *tasklet; /* tasklet of the thread owning this ring */
Willy Tarreau1efafce2019-01-27 15:37:19 +0100297 struct accept_queue_entry entry[ACCEPT_QUEUE_SIZE] __attribute((aligned(64)));
298};
299
300
Willy Tarreaud1d54542012-09-12 22:58:11 +0200301#endif /* _TYPES_LISTENER_H */
Willy Tarreaudd815982007-10-16 12:25:14 +0200302
303/*
304 * Local variables:
305 * c-indent-level: 8
306 * c-basic-offset: 8
307 * End:
308 */