blob: 8cfe40b8e236f692e4018d761226100ec44fd1ac [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;
42struct licounters;
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 */
50 LI_LISTEN, /* started, listening but not enabled */
51 LI_READY, /* started, listening and enabled */
52 LI_FULL, /* reached its connection limit */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020053 LI_LIMITED, /* transient state: limits have been reached, listener is queued */
Willy Tarreauf6502c52013-12-06 21:18:49 +010054} __attribute__((packed));
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055
56/* Listener transitions
57 * calloc() set() add_listener() bind()
58 * -------> NEW ----> INIT ----------> ASSIGNED -----> LISTEN
59 * <------- <---- <---------- <-----
60 * free() bzero() del_listener() unbind()
61 *
62 * The file descriptor is valid only during these three states :
63 *
64 * disable()
65 * LISTEN <------------ READY
66 * A| ------------> |A
67 * || !max & enable() ||
68 * || ||
69 * || max ||
70 * || max & enable() V| !max
71 * |+---------------> FULL
72 * +-----------------
73 * disable()
74 *
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020075 * The LIMITED state my be used when a limit has been detected just before
76 * using a listener. In this case, the listener MUST be queued into the
77 * appropriate wait queue (either the proxy's or the global one). It may be
78 * set back to the READY state at any instant and for any reason, so one must
79 * not rely on this state.
Willy Tarreaudabf2e22007-10-28 21:59:24 +010080 */
Willy Tarreaudd815982007-10-16 12:25:14 +020081
Willy Tarreau6fb42e02007-10-28 17:02:33 +010082/* listener socket options */
Willy Tarreau7d9736f2016-10-21 16:34:21 +020083#define LI_O_NONE 0x0000
84#define LI_O_NOLINGER 0x0001 /* disable linger on this socket */
85#define LI_O_FOREIGN 0x0002 /* permit listening on foreing addresses ("transparent") */
86#define LI_O_NOQUICKACK 0x0004 /* disable quick ack of immediate data (linux) */
87#define LI_O_DEF_ACCEPT 0x0008 /* wait up to 1 second for data before accepting */
88#define LI_O_TCP_L4_RULES 0x0010 /* run TCP L4 rules checks on the incoming connection */
89#define LI_O_CHK_MONNET 0x0040 /* check the source against a monitor-net rule */
90#define LI_O_ACC_PROXY 0x0080 /* find the proxied address in the first request line */
91#define LI_O_UNLIMITED 0x0100 /* listener not subject to global limits (peers & stats socket) */
92#define LI_O_TCP_FO 0x0200 /* enable TCP Fast Open (linux >= 3.7) */
93#define LI_O_V6ONLY 0x0400 /* bind to IPv6 only on Linux >= 2.4.21 */
94#define LI_O_V4V6 0x0800 /* bind to IPv4/IPv6 on Linux >= 2.4.21 */
95#define LI_O_ACC_CIP 0x1000 /* find the proxied address in the NetScaler Client IP header */
Willy Tarreau3c63fd82011-09-07 18:00:47 +020096
97/* Note: if a listener uses LI_O_UNLIMITED, it is highly recommended that it adds its own
98 * maxconn setting to the global.maxsock value so that its resources are reserved.
99 */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100100
Emeric Brun89675492012-10-05 13:48:26 +0200101#ifdef USE_OPENSSL
102/* bind_conf ssl options */
103#define BC_SSL_O_NONE 0x0000
104#define BC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
105#define BC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
106#define BC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
107#define BC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
108/* 0x000F reserved for 'no' protocol version options */
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200109#define BC_SSL_O_USE_SSLV3 0x0010 /* force SSLv3 */
110#define BC_SSL_O_USE_TLSV10 0x0020 /* force TLSv10 */
111#define BC_SSL_O_USE_TLSV11 0x0040 /* force TLSv11 */
112#define BC_SSL_O_USE_TLSV12 0x0080 /* force TLSv12 */
113/* 0x00F0 reserved for 'force' protocol version options */
Emeric Brun89675492012-10-05 13:48:26 +0200114#define BC_SSL_O_NO_TLS_TICKETS 0x0100 /* disable session resumption tickets */
115#endif
116
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200117/* "bind" line settings */
118struct bind_conf {
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200119#ifdef USE_OPENSSL
Emeric Brunfb510ea2012-10-05 12:00:26 +0200120 char *ca_file; /* CAfile to use on verify */
Emeric Brun81c00f02012-09-21 14:31:21 +0200121 unsigned long long ca_ignerr; /* ignored verify errors in handshake if depth > 0 */
122 unsigned long long crt_ignerr; /* ignored verify errors in handshake if depth == 0 */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200123 char *ciphers; /* cipher suite to use if non-null */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200124 char *crl_file; /* CRLfile to use on verify */
Emeric Brun2b58d042012-09-20 17:10:03 +0200125 char *ecdhe; /* named curve to use for ECDHE */
Emeric Brun89675492012-10-05 13:48:26 +0200126 int ssl_options; /* ssl options */
Emeric Brund94b3fe2012-09-20 18:23:56 +0200127 int verify; /* verify method (set of SSL_VERIFY_* flags) */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200128 SSL_CTX *default_ctx; /* SSL context of first/default certificate */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200129 char *npn_str; /* NPN protocol string */
130 int npn_len; /* NPN protocol string length */
Willy Tarreauab861d32013-04-02 02:30:41 +0200131 char *alpn_str; /* ALPN protocol string */
132 int alpn_len; /* ALPN protocol string length */
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100133 int strict_sni; /* refuse negotiation if sni doesn't match a certificate */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200134 struct eb_root sni_ctx; /* sni_ctx tree of all known certs full-names sorted by name */
135 struct eb_root sni_w_ctx; /* sni_ctx tree of all known certs wildcards sorted by name */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200136 struct tls_keys_ref *keys_ref; /* TLS ticket keys reference */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200137
138 char *ca_sign_file; /* CAFile used to generate and sign server certificates */
139 char *ca_sign_pass; /* CAKey passphrase */
140
141 X509 *ca_sign_cert; /* CA certificate referenced by ca_file */
142 EVP_PKEY *ca_sign_pkey; /* CA private key referenced by ca_key */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200143#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200144 int is_ssl; /* SSL is required for these listeners */
Christopher Faulet31af49d2015-06-09 17:29:50 +0200145 int generate_certs; /* 1 if generate-certificates option is set, else 0 */
Willy Tarreau6ae1ba62014-05-07 19:01:58 +0200146 unsigned long bind_proc; /* bitmask of processes allowed to use these listeners */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200147 struct { /* UNIX socket permissions */
148 uid_t uid; /* -1 to leave unchanged */
149 gid_t gid; /* -1 to leave unchanged */
150 mode_t mode; /* 0 to leave unchanged */
151 } ux;
152 int level; /* stats access level (ACCESS_LVL_*) */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200153 struct list by_fe; /* next binding for the same frontend, or NULL */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200154 struct list listeners; /* list of listeners using this bind config */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100155 uint32_t ns_cip_magic; /* Excepted NetScaler Client IP magic number */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200156 char *arg; /* argument passed to "bind" for better error reporting */
157 char *file; /* file where the section appears */
158 int line; /* line where the section appears */
159};
160
Willy Tarreaudd815982007-10-16 12:25:14 +0200161/* The listener will be directly referenced by the fdtab[] which holds its
162 * socket. The listener provides the protocol-specific accept() function to
163 * the fdtab.
164 */
165struct listener {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100166 enum obj_type obj_type; /* object type = OBJ_TYPE_LISTENER */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100167 enum li_state state; /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
168 short int nice; /* nice value to assign to the instanciated tasks */
Willy Tarreaudd815982007-10-16 12:25:14 +0200169 int fd; /* the listen socket */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100170 char *name; /* listener's name */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200171 int luid; /* listener universally unique ID, used for SNMP */
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100172 int options; /* socket options : LI_O_* */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200173 struct licounters *counters; /* statistics counters */
Willy Tarreaudd815982007-10-16 12:25:14 +0200174 struct protocol *proto; /* protocol this listener belongs to */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200175 struct xprt_ops *xprt; /* transport-layer operations for this socket */
Willy Tarreaudd815982007-10-16 12:25:14 +0200176 int nbconn; /* current number of connections on this listener */
177 int maxconn; /* maximum connections allowed on this listener */
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100178 unsigned int backlog; /* if set, listen backlog */
Willy Tarreau16a21472012-11-19 12:39:59 +0100179 unsigned int maxaccept; /* if set, max number of connections accepted at once */
Willy Tarreaudd815982007-10-16 12:25:14 +0200180 struct list proto_list; /* list in the protocol header */
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 int (*accept)(struct listener *l, int fd, struct sockaddr_storage *addr); /* upper layer's accept() */
Willy Tarreau26c25062009-03-08 09:38:41 +0100182 struct task * (*handler)(struct task *t); /* protocol handler. It is a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200183 struct proxy *frontend; /* the frontend this listener belongs to, or NULL */
Willy Tarreau10b688f2015-03-13 16:43:12 +0100184 enum obj_type *default_target; /* default target to use for accepted sessions or NULL */
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200185 struct list wait_queue; /* link element to make the listener wait for something (LI_LIMITED) */
Willy Tarreau3bc13772008-12-07 11:50:35 +0100186 unsigned int analysers; /* bitmap of required protocol analysers */
Willy Tarreaube1b9182009-06-14 18:48:19 +0200187 int maxseg; /* for TCP, advertised MSS */
Willy Tarreau2af207a2015-02-04 00:45:58 +0100188 int tcp_ut; /* for TCP, user timeout */
Willy Tarreauf6502c52013-12-06 21:18:49 +0100189 char *interface; /* interface name or NULL */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200190
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100191 const struct netns_entry *netns; /* network namespace of the listener*/
192
Willy Tarreau4348fad2012-09-20 16:48:07 +0200193 struct list by_fe; /* chaining in frontend's list of listeners */
194 struct list by_bind; /* chaining in bind_conf's list of listeners */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200195 struct bind_conf *bind_conf; /* "bind" line settings, include SSL settings among other things */
Willy Tarreauf5ae8f72012-09-07 16:58:00 +0200196
Willy Tarreau7b815632011-10-21 18:51:57 +0200197 /* warning: this struct is huge, keep it at the bottom */
198 struct sockaddr_storage addr; /* the address we listen to */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200199 struct {
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200200 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200201 } conf; /* config information */
Willy Tarreaudd815982007-10-16 12:25:14 +0200202};
203
Willy Tarreau26982662012-09-12 23:17:10 +0200204/* Descriptor for a "bind" keyword. The ->parse() function returns 0 in case of
205 * success, or a combination of ERR_* flags if an error is encountered. The
206 * function pointer can be NULL if not implemented. The function also has an
Willy Tarreau4348fad2012-09-20 16:48:07 +0200207 * access to the current "bind" config line. The ->skip value tells the parser
208 * how many words have to be skipped after the keyword.
Willy Tarreau26982662012-09-12 23:17:10 +0200209 */
210struct bind_kw {
211 const char *kw;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200212 int (*parse)(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err);
Willy Tarreau26982662012-09-12 23:17:10 +0200213 int skip; /* nb of args to skip */
214};
215
216/*
217 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
218 * struct list in order to be linked to other lists, allowing it to easily
219 * be declared where it is needed, and linked without duplicating data nor
Willy Tarreau51fb7652012-09-18 18:24:39 +0200220 * allocating memory. It is also possible to indicate a scope for the keywords.
Willy Tarreau26982662012-09-12 23:17:10 +0200221 */
222struct bind_kw_list {
Willy Tarreau51fb7652012-09-18 18:24:39 +0200223 const char *scope;
Willy Tarreau26982662012-09-12 23:17:10 +0200224 struct list list;
225 struct bind_kw kw[VAR_ARRAY];
226};
227
228
Willy Tarreaud1d54542012-09-12 22:58:11 +0200229#endif /* _TYPES_LISTENER_H */
Willy Tarreaudd815982007-10-16 12:25:14 +0200230
231/*
232 * Local variables:
233 * c-indent-level: 8
234 * c-basic-offset: 8
235 * End:
236 */