blob: 8aae395e0d6b1d3733ae549e4b7ea70750d15baa [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
29#include <openssl/ssl.h>
30#endif
31
Willy Tarreaudd815982007-10-16 12:25:14 +020032#include <common/config.h>
33#include <common/mini-clist.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010034#include <types/obj_type.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010035#include <eb32tree.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020036
Willy Tarreaud1d54542012-09-12 22:58:11 +020037/* Some pointer types reference below */
38struct task;
39struct protocol;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020040struct xprt_ops;
Willy Tarreaud1d54542012-09-12 22:58:11 +020041struct proxy;
Willy Tarreauae9bea02016-11-25 14:44:52 +010042struct fe_counters;
Willy Tarreaudd815982007-10-16 12:25:14 +020043
Willy Tarreaudd815982007-10-16 12:25:14 +020044/* listener state */
Willy Tarreauf6502c52013-12-06 21:18:49 +010045enum li_state {
Willy Tarreaube58c382011-07-24 18:28:10 +020046 LI_NEW = 0, /* not initialized yet */
47 LI_INIT, /* all parameters filled in, but not assigned yet */
48 LI_ASSIGNED, /* assigned to the protocol, but not listening yet */
49 LI_PAUSED, /* listener was paused, it's bound but not listening */
Olivier Houchard1fc05162017-04-06 01:05:05 +020050 LI_ZOMBIE, /* The listener doesn't belong to the process, but is kept opened */
Willy Tarreaube58c382011-07-24 18:28:10 +020051 LI_LISTEN, /* started, listening but not enabled */
52 LI_READY, /* started, listening and enabled */
53 LI_FULL, /* reached its connection limit */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020054 LI_LIMITED, /* transient state: limits have been reached, listener is queued */
Willy Tarreauf6502c52013-12-06 21:18:49 +010055} __attribute__((packed));
Willy Tarreaudabf2e22007-10-28 21:59:24 +010056
57/* Listener transitions
58 * calloc() set() add_listener() bind()
59 * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN
60 * <------- <---- <---------- <-----
61 * free() bzero() del_listener() unbind()
62 *
63 * The file descriptor is valid only during these three states :
64 *
65 * disable()
66 * LISTEN <------------ READY
67 * A| ------------> |A
68 * || !max & enable() ||
69 * || ||
70 * || max ||
71 * || max & enable() V| !max
72 * |+---------------> FULL
73 * +-----------------
74 * disable()
75 *
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020076 * The LIMITED state my be used when a limit has been detected just before
77 * using a listener. In this case, the listener MUST be queued into the
78 * appropriate wait queue (either the proxy's or the global one). It may be
79 * set back to the READY state at any instant and for any reason, so one must
80 * not rely on this state.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010081 */
Willy Tarreaudd815982007-10-16 12:25:14 +020082
Willy Tarreau6fb42e02007-10-28 17:02:33 +010083/* listener socket options */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020084#define LI_O_NONE 0x0000
85#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
86#define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses ("transparent") */
87#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
88#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
89#define LI_O_TCP_L4_RULES 0x0010 /* run TCP L4 rules checks on the incoming connection */
Willy Tarreau620408f2016-10-21 16:37:51 +020090#define LI_O_TCP_L5_RULES 0x0020 /* run TCP L5 rules checks on the incoming session */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020091#define LI_O_CHK_MONNET 0x0040 /* check the source against a monitor-net rule */
92#define LI_O_ACC_PROXY 0x0080 /* find the proxied address in the first request line */
93#define LI_O_UNLIMITED 0x0100 /* listener not subject to global limits (peers & stats socket) */
94#define LI_O_TCP_FO 0x0200 /* enable TCP Fast Open (linux >= 3.7) */
95#define LI_O_V6ONLY 0x0400 /* bind to IPv6 only on Linux >= 2.4.21 */
96#define LI_O_V4V6 0x0800 /* bind to IPv4/IPv6 on Linux >= 2.4.21 */
97#define LI_O_ACC_CIP 0x1000 /* find the proxied address in the NetScaler Client IP header */
Willy Tarreau3c63fd82011-09-07 18:00:47 +020098
99/* Note: if a listener uses LI_O_UNLIMITED, it is highly recommended that it adds its own
100 * maxconn setting to the global.maxsock value so that its resources are reserved.
101 */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100102
Emeric Brun89675492012-10-05 13:48:26 +0200103#ifdef USE_OPENSSL
104/* bind_conf ssl options */
105#define BC_SSL_O_NONE 0x0000
106#define BC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
107#define BC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
108#define BC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
109#define BC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
110/* 0x000F reserved for 'no' protocol version options */
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200111#define BC_SSL_O_USE_SSLV3 0x0010 /* force SSLv3 */
112#define BC_SSL_O_USE_TLSV10 0x0020 /* force TLSv10 */
113#define BC_SSL_O_USE_TLSV11 0x0040 /* force TLSv11 */
114#define BC_SSL_O_USE_TLSV12 0x0080 /* force TLSv12 */
115/* 0x00F0 reserved for 'force' protocol version options */
Emeric Brun89675492012-10-05 13:48:26 +0200116#define BC_SSL_O_NO_TLS_TICKETS 0x0100 /* disable session resumption tickets */
Lukas Tribus53ae85c2017-05-04 15:45:40 +0000117#define BC_SSL_O_PREF_CLIE_CIPH 0x0200 /* prefer client ciphers */
Emeric Brun89675492012-10-05 13:48:26 +0200118#endif
119
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100120/* ssl "bind" settings */
121struct ssl_bind_conf {
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200122#ifdef USE_OPENSSL
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100123#ifdef OPENSSL_NPN_NEGOTIATED
124 char *npn_str; /* NPN protocol string */
125 int npn_len; /* NPN protocol string length */
126#endif
127#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
128 char *alpn_str; /* ALPN protocol string */
129 int alpn_len; /* ALPN protocol string length */
130#endif
131 int verify; /* verify method (set of SSL_VERIFY_* flags) */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200132 char *ca_file; /* CAfile to use on verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200133 char *crl_file; /* CRLfile to use on verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100134 char *ciphers; /* cipher suite to use if non-null */
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +0100135 char *curves; /* curves suite to use for ECDHE */
Emeric Brun2b58d042012-09-20 17:10:03 +0200136 char *ecdhe; /* named curve to use for ECDHE */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100137#endif
138};
139
140/* "bind" line settings */
141struct bind_conf {
142#ifdef USE_OPENSSL
143 struct ssl_bind_conf ssl_conf; /* ssl conf for ctx setting */
144 unsigned long long ca_ignerr; /* ignored verify errors in handshake if depth > 0 */
145 unsigned long long crt_ignerr; /* ignored verify errors in handshake if depth == 0 */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +0100146 SSL_CTX *initial_ctx; /* SSL context for initial negotiation */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200147 SSL_CTX *default_ctx; /* SSL context of first/default certificate */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100148 struct ssl_bind_conf *default_ssl_conf; /* custom SSL conf of default_ctx */
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100149 int strict_sni; /* refuse negotiation if sni doesn't match a certificate */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +0100150 int ssl_options; /* ssl options */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200151 struct eb_root sni_ctx; /* sni_ctx tree of all known certs full-names sorted by name */
152 struct eb_root sni_w_ctx; /* sni_ctx tree of all known certs wildcards sorted by name */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200153 struct tls_keys_ref *keys_ref; /* TLS ticket keys reference */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200154
155 char *ca_sign_file; /* CAFile used to generate and sign server certificates */
156 char *ca_sign_pass; /* CAKey passphrase */
157
158 X509 *ca_sign_cert; /* CA certificate referenced by ca_file */
159 EVP_PKEY *ca_sign_pkey; /* CA private key referenced by ca_key */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200160#endif
Willy Tarreauc95bad52016-12-22 00:13:31 +0100161 struct proxy *frontend; /* the frontend all these listeners belong to, or NULL */
Willy Tarreau71a8c7c2016-12-21 22:04:54 +0100162 struct xprt_ops *xprt; /* transport-layer operations for all listeners */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200163 int is_ssl; /* SSL is required for these listeners */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200164 int generate_certs; /* 1 if generate-certificates option is set, else 0 */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200165 unsigned long bind_proc; /* bitmask of processes allowed to use these listeners */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200166 struct { /* UNIX socket permissions */
167 uid_t uid; /* -1 to leave unchanged */
168 gid_t gid; /* -1 to leave unchanged */
169 mode_t mode; /* 0 to leave unchanged */
170 } ux;
171 int level; /* stats access level (ACCESS_LVL_*) */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200172 struct list by_fe; /* next binding for the same frontend, or NULL */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200173 struct list listeners; /* list of listeners using this bind config */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100174 uint32_t ns_cip_magic; /* Excepted NetScaler Client IP magic number */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200175 char *arg; /* argument passed to "bind" for better error reporting */
176 char *file; /* file where the section appears */
177 int line; /* line where the section appears */
178};
179
Willy Tarreaudd815982007-10-16 12:25:14 +0200180/* The listener will be directly referenced by the fdtab[] which holds its
181 * socket. The listener provides the protocol-specific accept() function to
182 * the fdtab.
183 */
184struct listener {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100185 enum obj_type obj_type; /* object type = OBJ_TYPE_LISTENER */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100186 enum li_state state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
187 short int nice; /* nice value to assign to the instanciated tasks */
Willy Tarreaudd815982007-10-16 12:25:14 +0200188 int fd; /* the listen socket */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100189 char *name; /* listener's name */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200190 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100191 int options; /* socket options : LI_O_* */
Willy Tarreauae9bea02016-11-25 14:44:52 +0100192 struct fe_counters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +0200193 struct protocol *proto; /* protocol this listener belongs to */
194 int nbconn; /* current number of connections on this listener */
195 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100196 unsigned int backlog; /* if set, listen backlog */
Willy Tarreau16a21472012-11-19 12:39:59 +0100197 unsigned int maxaccept; /* if set, max number of connections accepted at once */
Willy Tarreaudd815982007-10-16 12:25:14 +0200198 struct list proto_list; /* list in the protocol header */
Willy Tarreaueb472682010-05-28 18:46:57 +0200199 int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */
Willy Tarreau26c25062009-03-08 09:38:41 +0100200 struct task * (*handler)(struct task *t); /* protocol handler. It is a task */
Willy Tarreau10b688f2015-03-13 16:43:12 +0100201 enum obj_type *default_target; /* default target to use for accepted sessions or NULL */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200202 struct list wait_queue; /* link element to make the listener wait for something (LI_LIMITED) */
Willy Tarreau3bc13772008-12-07 11:50:35 +0100203 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200204 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau2af207a2015-02-04 00:45:58 +0100205 int tcp_ut; /* for TCP, user timeout */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100206 char *interface; /* interface name or NULL */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200207
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100208 const struct netns_entry *netns; /* network namespace of the listener*/
209
Willy Tarreau4348fad2012-09-20 16:48:07 +0200210 struct list by_fe; /* chaining in frontend's list of listeners */
211 struct list by_bind; /* chaining in bind_conf's list of listeners */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200212 struct bind_conf *bind_conf; /* "bind" line settings, include SSL settings among other things */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200213
Willy Tarreau7b815632011-10-21 18:51:57 +0200214 /* warning: this struct is huge, keep it at the bottom */
215 struct sockaddr_storage addr; /* the address we listen to */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200216 struct {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200217 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200218 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200219};
220
Willy Tarreau26982662012-09-12 23:17:10 +0200221/* Descriptor for a "bind" keyword. The ->parse() function returns 0 in case of
222 * success, or a combination of ERR_* flags if an error is encountered. The
223 * function pointer can be NULL if not implemented. The function also has an
Willy Tarreau4348fad2012-09-20 16:48:07 +0200224 * access to the current "bind" config line. The ->skip value tells the parser
225 * how many words have to be skipped after the keyword.
Willy Tarreau26982662012-09-12 23:17:10 +0200226 */
227struct bind_kw {
228 const char *kw;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200229 int (*parse)(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err);
Willy Tarreau26982662012-09-12 23:17:10 +0200230 int skip; /* nb of args to skip */
231};
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100232struct ssl_bind_kw {
233 const char *kw;
234 int (*parse)(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err);
235 int skip; /* nb of args to skip */
236};
Willy Tarreau26982662012-09-12 23:17:10 +0200237
238/*
239 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
240 * struct list in order to be linked to other lists, allowing it to easily
241 * be declared where it is needed, and linked without duplicating data nor
Willy Tarreau51fb7652012-09-18 18:24:39 +0200242 * allocating memory. It is also possible to indicate a scope for the keywords.
Willy Tarreau26982662012-09-12 23:17:10 +0200243 */
244struct bind_kw_list {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200245 const char *scope;
Willy Tarreau26982662012-09-12 23:17:10 +0200246 struct list list;
247 struct bind_kw kw[VAR_ARRAY];
248};
249
250
Olivier Houchardf73629d2017-04-05 22:33:04 +0200251struct xfer_sock_list {
252 int fd;
253 char *iface;
254 char *namespace;
255 int options; /* socket options LI_O_* */
256 struct xfer_sock_list *prev;
257 struct xfer_sock_list *next;
258 struct sockaddr_storage addr;
259};
260
Willy Tarreaud1d54542012-09-12 22:58:11 +0200261#endif /* _TYPES_LISTENER_H */
Willy Tarreaudd815982007-10-16 12:25:14 +0200262
263/*
264 * Local variables:
265 * c-indent-level: 8
266 * c-basic-offset: 8
267 * End:
268 */