]> git.ipfire.org Git - thirdparty/haproxy.git/blob
f8f235c1a
[thirdparty/haproxy.git] /
1 /*
2 * include/haproxy/connection.h
3 * This file contains connection function prototypes
4 *
5 * Copyright (C) 2000-2002 Willy Tarreau - w@1wt.eu
6 *
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 */
21
22 #ifndef _HAPROXY_CONNECTION_H
23 #define _HAPROXY_CONNECTION_H
24
25 #include <import/ist.h>
26
27 #include <haproxy/api.h>
28 #include <haproxy/connection-t.h>
29 #include <haproxy/fd.h>
30 #include <haproxy/http_ana.h>
31 #include <haproxy/listener-t.h>
32 #include <haproxy/obj_type.h>
33 #include <haproxy/pool.h>
34 #include <haproxy/session.h>
35 #include <haproxy/task-t.h>
36 #include <haproxy/tcpcheck-t.h>
37
38
39 extern struct pool_head *pool_head_connection;
40 extern struct pool_head *pool_head_connstream;
41 extern struct pool_head *pool_head_sockaddr;
42 extern struct pool_head *pool_head_authority;
43 extern struct xprt_ops *registered_xprt[XPRT_ENTRIES];
44 extern struct mux_proto_list mux_proto_list;
45
46 #define IS_HTX_CONN(conn) ((conn)->mux && ((conn)->mux->flags & MX_FL_HTX))
47 #define IS_HTX_CS(cs) (IS_HTX_CONN((cs)->conn))
48
49 /* I/O callback for fd-based connections. It calls the read/write handlers
50 * provided by the connection's sock_ops.
51 */
52 void conn_fd_handler(int fd);
53 int conn_fd_check(struct connection *conn);
54
55 /* receive a PROXY protocol header over a connection */
56 int conn_recv_proxy(struct connection *conn, int flag);
57 int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
58 int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
59 int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
60
61 int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
62 int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
63
64 /* receive a NetScaler Client IP insertion header over a connection */
65 int conn_recv_netscaler_cip(struct connection *conn, int flag);
66
67 /* raw send() directly on the socket */
68 int conn_sock_send(struct connection *conn, const void *buf, int len, int flags);
69
70 /* drains any pending bytes from the socket */
71 int conn_sock_drain(struct connection *conn);
72
73 /* scoks4 proxy handshake */
74 int conn_send_socks4_proxy_request(struct connection *conn);
75 int conn_recv_socks4_proxy_response(struct connection *conn);
76
77 /* If we delayed the mux creation because we were waiting for the handshake, do it now */
78 int conn_create_mux(struct connection *conn);
79
80 extern struct idle_conns idle_conns[MAX_THREADS];
81
82 /* returns true is the transport layer is ready */
83 static inline int conn_xprt_ready(const struct connection *conn)
84 {
85 return (conn->flags & CO_FL_XPRT_READY);
86 }
87
88 /* returns true is the control layer is ready */
89 static inline int conn_ctrl_ready(const struct connection *conn)
90 {
91 return (conn->flags & CO_FL_CTRL_READY);
92 }
93
94 /* Calls the init() function of the transport layer if any and if not done yet,
95 * and sets the CO_FL_XPRT_READY flag to indicate it was properly initialized.
96 * Returns <0 in case of error.
97 */
98 static inline int conn_xprt_init(struct connection *conn)
99 {
100 int ret = 0;
101
102 if (!conn_xprt_ready(conn) && conn->xprt && conn->xprt->init)
103 ret = conn->xprt->init(conn, &conn->xprt_ctx);
104
105 if (ret >= 0)
106 conn->flags |= CO_FL_XPRT_READY;
107
108 return ret;
109 }
110
111 /* Calls the close() function of the transport layer if any and if not done
112 * yet, and clears the CO_FL_XPRT_READY flag. However this is not done if the
113 * CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the
114 * transport layer very late if needed.
115 */
116 static inline void conn_xprt_close(struct connection *conn)
117 {
118 if ((conn->flags & (CO_FL_XPRT_READY|CO_FL_XPRT_TRACKED)) == CO_FL_XPRT_READY) {
119 if (conn->xprt->close)
120 conn->xprt->close(conn, conn->xprt_ctx);
121 conn->xprt_ctx = NULL;
122 conn->flags &= ~CO_FL_XPRT_READY;
123 }
124 }
125
126 /* Initializes the connection's control layer which essentially consists in
127 * registering the file descriptor for polling and setting the CO_FL_CTRL_READY
128 * flag. The caller is responsible for ensuring that the control layer is
129 * already assigned to the connection prior to the call.
130 */
131 static inline void conn_ctrl_init(struct connection *conn)
132 {
133 if (!conn_ctrl_ready(conn)) {
134 int fd = conn->handle.fd;
135
136 fd_insert(fd, conn, conn_fd_handler, tid_bit);
137 conn->flags |= CO_FL_CTRL_READY;
138 }
139 }
140
141 /* Deletes the FD if the transport layer is already gone. Once done,
142 * it then removes the CO_FL_CTRL_READY flag.
143 */
144 static inline void conn_ctrl_close(struct connection *conn)
145 {
146 if ((conn->flags & (CO_FL_XPRT_READY|CO_FL_CTRL_READY)) == CO_FL_CTRL_READY) {
147 fd_delete(conn->handle.fd);
148 conn->handle.fd = DEAD_FD_MAGIC;
149 conn->flags &= ~CO_FL_CTRL_READY;
150 }
151 }
152
153 /* If the connection still has a transport layer, then call its close() function
154 * if any, and delete the file descriptor if a control layer is set. This is
155 * used to close everything at once and atomically. However this is not done if
156 * the CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the
157 * transport layer very late if needed.
158 */
159 static inline void conn_full_close(struct connection *conn)
160 {
161 conn_xprt_close(conn);
162 conn_ctrl_close(conn);
163 }
164
165 /* stop tracking a connection, allowing conn_full_close() to always
166 * succeed.
167 */
168 static inline void conn_stop_tracking(struct connection *conn)
169 {
170 conn->flags &= ~CO_FL_XPRT_TRACKED;
171 }
172
173 /* Stop all polling on the fd. This might be used when an error is encountered
174 * for example.
175 */
176 static inline void conn_stop_polling(struct connection *c)
177 {
178 if (conn_ctrl_ready(c))
179 fd_stop_both(c->handle.fd);
180 }
181
182 /* Stops polling in case of error on the connection. */
183 static inline void conn_cond_update_polling(struct connection *c)
184 {
185 if (unlikely(c->flags & CO_FL_ERROR))
186 conn_stop_polling(c);
187 }
188
189 /* read shutdown, called from the rcv_buf/rcv_pipe handlers when
190 * detecting an end of connection.
191 */
192 static inline void conn_sock_read0(struct connection *c)
193 {
194 c->flags |= CO_FL_SOCK_RD_SH;
195 if (conn_ctrl_ready(c)) {
196 fd_stop_recv(c->handle.fd);
197 /* we don't risk keeping ports unusable if we found the
198 * zero from the other side.
199 */
200 fdtab[c->handle.fd].linger_risk = 0;
201 }
202 }
203
204 /* write shutdown, indication that the upper layer is not willing to send
205 * anything anymore and wants to close after pending data are sent. The
206 * <clean> argument will allow not to perform the socket layer shutdown if
207 * equal to 0.
208 */
209 static inline void conn_sock_shutw(struct connection *c, int clean)
210 {
211 c->flags |= CO_FL_SOCK_WR_SH;
212 if (conn_ctrl_ready(c)) {
213 fd_stop_send(c->handle.fd);
214 /* don't perform a clean shutdown if we're going to reset or
215 * if the shutr was already received.
216 */
217 if (!(c->flags & CO_FL_SOCK_RD_SH) && clean)
218 shutdown(c->handle.fd, SHUT_WR);
219 }
220 }
221
222 static inline void conn_xprt_shutw(struct connection *c)
223 {
224 if (conn_ctrl_ready(c))
225 fd_stop_send(c->handle.fd);
226
227 /* clean data-layer shutdown */
228 if (c->xprt && c->xprt->shutw)
229 c->xprt->shutw(c, c->xprt_ctx, 1);
230 }
231
232 static inline void conn_xprt_shutw_hard(struct connection *c)
233 {
234 if (conn_ctrl_ready(c))
235 fd_stop_send(c->handle.fd);
236
237 /* unclean data-layer shutdown */
238 if (c->xprt && c->xprt->shutw)
239 c->xprt->shutw(c, c->xprt_ctx, 0);
240 }
241
242 /* shut read */
243 static inline void cs_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
244 {
245
246 /* clean data-layer shutdown */
247 if (cs->conn->mux && cs->conn->mux->shutr)
248 cs->conn->mux->shutr(cs, mode);
249 cs->flags |= (mode == CS_SHR_DRAIN) ? CS_FL_SHRD : CS_FL_SHRR;
250 }
251
252 /* shut write */
253 static inline void cs_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
254 {
255
256 /* clean data-layer shutdown */
257 if (cs->conn->mux && cs->conn->mux->shutw)
258 cs->conn->mux->shutw(cs, mode);
259 cs->flags |= (mode == CS_SHW_NORMAL) ? CS_FL_SHWN : CS_FL_SHWS;
260 }
261
262 /* completely close a conn_stream (but do not detach it) */
263 static inline void cs_close(struct conn_stream *cs)
264 {
265 cs_shutw(cs, CS_SHW_SILENT);
266 cs_shutr(cs, CS_SHR_RESET);
267 cs->flags = CS_FL_NONE;
268 }
269
270 /* sets CS_FL_ERROR or CS_FL_ERR_PENDING on the cs */
271 static inline void cs_set_error(struct conn_stream *cs)
272 {
273 if (cs->flags & CS_FL_EOS)
274 cs->flags |= CS_FL_ERROR;
275 else
276 cs->flags |= CS_FL_ERR_PENDING;
277 }
278
279 /* detect sock->data read0 transition */
280 static inline int conn_xprt_read0_pending(struct connection *c)
281 {
282 return (c->flags & CO_FL_SOCK_RD_SH) != 0;
283 }
284
285 /* prepares a connection to work with protocol <proto> and transport <xprt>.
286 * The transport's is initialized as well, and the mux and its context are
287 * cleared. The target is not reinitialized and it is recommended that it is
288 * set prior to calling this function so that the function may make use of it
289 * in the future to refine the mux choice if needed.
290 */
291 static inline void conn_prepare(struct connection *conn, const struct protocol *proto, const struct xprt_ops *xprt)
292 {
293 conn->ctrl = proto;
294 conn->xprt = xprt;
295 conn->mux = NULL;
296 conn->xprt_ctx = NULL;
297 conn->ctx = NULL;
298 }
299
300 /*
301 * Initializes all required fields for a new conn_strema.
302 */
303 static inline void cs_init(struct conn_stream *cs, struct connection *conn)
304 {
305 cs->obj_type = OBJ_TYPE_CS;
306 cs->flags = CS_FL_NONE;
307 cs->conn = conn;
308 }
309
310 /* Initializes all required fields for a new connection. Note that it does the
311 * minimum acceptable initialization for a connection that already exists and
312 * is about to be reused. It also leaves the addresses untouched, which makes
313 * it usable across connection retries to reset a connection to a known state.
314 */
315 static inline void conn_init(struct connection *conn)
316 {
317 conn->obj_type = OBJ_TYPE_CONN;
318 conn->flags = CO_FL_NONE;
319 conn->mux = NULL;
320 conn->ctx = NULL;
321 conn->owner = NULL;
322 conn->send_proxy_ofs = 0;
323 conn->handle.fd = DEAD_FD_MAGIC;
324 conn->err_code = CO_ER_NONE;
325 conn->target = NULL;
326 conn->destroy_cb = NULL;
327 conn->proxy_netns = NULL;
328 MT_LIST_INIT(&conn->list);
329 LIST_INIT(&conn->session_list);
330 conn->subs = NULL;
331 conn->idle_time = 0;
332 conn->src = NULL;
333 conn->dst = NULL;
334 conn->proxy_authority = NULL;
335 conn->proxy_unique_id = IST_NULL;
336 }
337
338 /* sets <owner> as the connection's owner */
339 static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *))
340 {
341 conn->owner = owner;
342 conn->destroy_cb = cb;
343 }
344
345 /* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
346 * returns it. If <sap> is NULL, the address is always allocated and returned.
347 * if <sap> is non-null, an address will only be allocated if it points to a
348 * non-null pointer. In this case the allocated address will be assigned there.
349 * In both situations the new pointer is returned.
350 */
351 static inline struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap)
352 {
353 struct sockaddr_storage *sa;
354
355 if (sap && *sap)
356 return *sap;
357
358 sa = pool_alloc(pool_head_sockaddr);
359 if (sap)
360 *sap = sa;
361 return sa;
362 }
363
364 /* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
365 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
366 * there.
367 */
368 static inline void sockaddr_free(struct sockaddr_storage **sap)
369 {
370 if (!sap)
371 return;
372 pool_free(pool_head_sockaddr, *sap);
373 *sap = NULL;
374 }
375
376 /* Tries to allocate a new connection and initialized its main fields. The
377 * connection is returned on success, NULL on failure. The connection must
378 * be released using pool_free() or conn_free().
379 */
380 static inline struct connection *conn_new()
381 {
382 struct connection *conn;
383
384 conn = pool_alloc(pool_head_connection);
385 if (likely(conn != NULL))
386 conn_init(conn);
387 return conn;
388 }
389
390 /* Releases a conn_stream previously allocated by cs_new(), as well as any
391 * buffer it would still hold.
392 */
393 static inline void cs_free(struct conn_stream *cs)
394 {
395
396 pool_free(pool_head_connstream, cs);
397 }
398
399 /* Tries to allocate a new conn_stream and initialize its main fields. If
400 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
401 * and assigned to cs->conn ; this connection will then have to be released
402 * using pool_free() or conn_free(). The conn_stream is initialized and added
403 * to the mux's stream list on success, then returned. On failure, nothing is
404 * allocated and NULL is returned.
405 */
406 static inline struct conn_stream *cs_new(struct connection *conn)
407 {
408 struct conn_stream *cs;
409
410 cs = pool_alloc(pool_head_connstream);
411 if (unlikely(!cs))
412 return NULL;
413
414 if (!conn) {
415 conn = conn_new();
416 if (unlikely(!conn)) {
417 cs_free(cs);
418 return NULL;
419 }
420 conn_init(conn);
421 }
422
423 cs_init(cs, conn);
424 return cs;
425 }
426
427 /* Retrieves any valid conn_stream from this connection, preferably the first
428 * valid one. The purpose is to be able to figure one other end of a private
429 * connection for purposes like source binding or proxy protocol header
430 * emission. In such cases, any conn_stream is expected to be valid so the
431 * mux is encouraged to return the first one it finds. If the connection has
432 * no mux or the mux has no get_first_cs() method or the mux has no valid
433 * conn_stream, NULL is returned. The output pointer is purposely marked
434 * const to discourage the caller from modifying anything there.
435 */
436 static inline const struct conn_stream *cs_get_first(const struct connection *conn)
437 {
438 if (!conn || !conn->mux || !conn->mux->get_first_cs)
439 return NULL;
440 return conn->mux->get_first_cs(conn);
441 }
442
443 static inline void conn_force_unsubscribe(struct connection *conn)
444 {
445 if (!conn->subs)
446 return;
447 conn->subs->events = 0;
448 conn->subs = NULL;
449 }
450
451 /* Releases a connection previously allocated by conn_new() */
452 static inline void conn_free(struct connection *conn)
453 {
454 /* Remove ourself from the session's connections list, if any. */
455 if (!LIST_ISEMPTY(&conn->session_list)) {
456 struct session *sess = conn->owner;
457 if (conn->flags & CO_FL_SESS_IDLE)
458 sess->idle_conns--;
459 session_unown_conn(sess, conn);
460 }
461
462 sockaddr_free(&conn->src);
463 sockaddr_free(&conn->dst);
464
465 if (conn->proxy_authority != NULL) {
466 pool_free(pool_head_authority, conn->proxy_authority);
467 conn->proxy_authority = NULL;
468 }
469 if (isttest(conn->proxy_unique_id)) {
470 pool_free(pool_head_uniqueid, conn->proxy_unique_id.ptr);
471 conn->proxy_unique_id = IST_NULL;
472 }
473
474 /* By convention we always place a NULL where the ctx points to if the
475 * mux is null. It may have been used to store the connection as a
476 * stream_interface's end point for example.
477 */
478 if (conn->ctx != NULL && conn->mux == NULL)
479 *(void **)conn->ctx = NULL;
480
481 /* The connection is currently in the server's idle list, so tell it
482 * there's one less connection available in that list.
483 */
484 if (conn->idle_time > 0) {
485 struct server *srv = __objt_server(conn->target);
486 _HA_ATOMIC_SUB(&srv->curr_idle_conns, 1);
487 _HA_ATOMIC_SUB(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb, 1);
488 _HA_ATOMIC_SUB(&srv->curr_idle_thr[tid], 1);
489 } else {
490 struct server *srv = objt_server(conn->target);
491
492 if (srv)
493 _HA_ATOMIC_SUB(&srv->curr_used_conns, 1);
494 }
495
496 conn_force_unsubscribe(conn);
497 MT_LIST_DEL((struct mt_list *)&conn->list);
498 pool_free(pool_head_connection, conn);
499 }
500
501 /* Release a conn_stream */
502 static inline void cs_destroy(struct conn_stream *cs)
503 {
504 if (cs->conn->mux)
505 cs->conn->mux->detach(cs);
506 else {
507 /* It's too early to have a mux, let's just destroy
508 * the connection
509 */
510 struct connection *conn = cs->conn;
511
512 conn_stop_tracking(conn);
513 conn_full_close(conn);
514 if (conn->destroy_cb)
515 conn->destroy_cb(conn);
516 conn_free(conn);
517 }
518 cs_free(cs);
519 }
520
521 /* Returns the conn from a cs. If cs is NULL, returns NULL */
522 static inline struct connection *cs_conn(const struct conn_stream *cs)
523 {
524 return cs ? cs->conn : NULL;
525 }
526
527 /* Retrieves the connection's original source address. Returns non-zero on
528 * success or zero on failure. The operation is only performed once and the
529 * address is stored in the connection for future use.
530 */
531 static inline int conn_get_src(struct connection *conn)
532 {
533 if (conn->flags & CO_FL_ADDR_FROM_SET)
534 return 1;
535
536 if (!conn_ctrl_ready(conn) || !conn->ctrl->get_src)
537 return 0;
538
539 if (!sockaddr_alloc(&conn->src))
540 return 0;
541
542 if (conn->ctrl->get_src(conn->handle.fd, (struct sockaddr *)conn->src,
543 sizeof(*conn->src),
544 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
545 return 0;
546 conn->flags |= CO_FL_ADDR_FROM_SET;
547 return 1;
548 }
549
550 /* Retrieves the connection's original destination address. Returns non-zero on
551 * success or zero on failure. The operation is only performed once and the
552 * address is stored in the connection for future use.
553 */
554 static inline int conn_get_dst(struct connection *conn)
555 {
556 if (conn->flags & CO_FL_ADDR_TO_SET)
557 return 1;
558
559 if (!conn_ctrl_ready(conn) || !conn->ctrl->get_dst)
560 return 0;
561
562 if (!sockaddr_alloc(&conn->dst))
563 return 0;
564
565 if (conn->ctrl->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst,
566 sizeof(*conn->dst),
567 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
568 return 0;
569 conn->flags |= CO_FL_ADDR_TO_SET;
570 return 1;
571 }
572
573 /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
574 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). The connection is tested and if
575 * it is null, nothing is done.
576 */
577 static inline void conn_set_tos(const struct connection *conn, int tos)
578 {
579 if (!conn || !conn_ctrl_ready(conn))
580 return;
581
582 #ifdef IP_TOS
583 if (conn->src->ss_family == AF_INET)
584 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
585 #endif
586 #ifdef IPV6_TCLASS
587 if (conn->src->ss_family == AF_INET6) {
588 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)conn->src)->sin6_addr))
589 /* v4-mapped addresses need IP_TOS */
590 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
591 else
592 setsockopt(conn->handle.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
593 }
594 #endif
595 }
596
597 /* Sets the netfilter mark on the connection's socket. The connection is tested
598 * and if it is null, nothing is done.
599 */
600 static inline void conn_set_mark(const struct connection *conn, int mark)
601 {
602 if (!conn || !conn_ctrl_ready(conn))
603 return;
604
605 #ifdef SO_MARK
606 setsockopt(conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
607 #endif
608 }
609
610 /* Sets adjust the TCP quick-ack feature on the connection's socket. The
611 * connection is tested and if it is null, nothing is done.
612 */
613 static inline void conn_set_quickack(const struct connection *conn, int value)
614 {
615 if (!conn || !conn_ctrl_ready(conn))
616 return;
617
618 #ifdef TCP_QUICKACK
619 setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &value, sizeof(value));
620 #endif
621 }
622
623 /* Attaches a conn_stream to a data layer and sets the relevant callbacks */
624 static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb)
625 {
626 cs->data_cb = data_cb;
627 cs->data = data;
628 }
629
630 static inline struct wait_event *wl_set_waitcb(struct wait_event *wl, struct task *(*cb)(struct task *, void *, unsigned short), void *ctx)
631 {
632 if (!wl->tasklet->process) {
633 wl->tasklet->process = cb;
634 wl->tasklet->context = ctx;
635 }
636 return wl;
637 }
638
639 /* Installs the connection's mux layer for upper context <ctx>.
640 * Returns < 0 on error.
641 */
642 static inline int conn_install_mux(struct connection *conn, const struct mux_ops *mux,
643 void *ctx, struct proxy *prx, struct session *sess)
644 {
645 int ret;
646
647 conn->mux = mux;
648 conn->ctx = ctx;
649 ret = mux->init ? mux->init(conn, prx, sess, &BUF_NULL) : 0;
650 if (ret < 0) {
651 conn->mux = NULL;
652 conn->ctx = NULL;
653 }
654 return ret;
655 }
656
657 /* returns a human-readable error code for conn->err_code, or NULL if the code
658 * is unknown.
659 */
660 static inline const char *conn_err_code_str(struct connection *c)
661 {
662 switch (c->err_code) {
663 case CO_ER_NONE: return "Success";
664
665 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
666 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
667 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
668 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
669 case CO_ER_NOPROTO: return "Protocol or address family not supported";
670 case CO_ER_SOCK_ERR: return "General socket error";
671 case CO_ER_PORT_RANGE: return "Source port range exhausted";
672 case CO_ER_CANT_BIND: return "Can't bind to source address";
673 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
674 case CO_ER_ADDR_INUSE: return "Local source address already in use";
675
676 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
677 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
678 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
679 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
680 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
681 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
682 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
683
684 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
685 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
686 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
687 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
688 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
689
690 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
691 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
692 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
693 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
694 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
695 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
696 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
697 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
698 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
699 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
700 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
701 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
702 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
703 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
704
705 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
706 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
707 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
708 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
709 }
710 return NULL;
711 }
712
713 static inline const char *conn_get_ctrl_name(const struct connection *conn)
714 {
715 if (!conn || !conn_ctrl_ready(conn))
716 return "NONE";
717 return conn->ctrl->name;
718 }
719
720 static inline const char *conn_get_xprt_name(const struct connection *conn)
721 {
722 if (!conn || !conn_xprt_ready(conn))
723 return "NONE";
724 return conn->xprt->name;
725 }
726
727 static inline const char *conn_get_mux_name(const struct connection *conn)
728 {
729 if (!conn || !conn->mux)
730 return "NONE";
731 return conn->mux->name;
732 }
733
734 static inline const char *cs_get_data_name(const struct conn_stream *cs)
735 {
736 if (!cs || !cs->data_cb)
737 return "NONE";
738 return cs->data_cb->name;
739 }
740
741 /* registers pointer to transport layer <id> (XPRT_*) */
742 static inline void xprt_register(int id, struct xprt_ops *xprt)
743 {
744 if (id >= XPRT_ENTRIES)
745 return;
746 registered_xprt[id] = xprt;
747 }
748
749 /* returns pointer to transport layer <id> (XPRT_*) or NULL if not registered */
750 static inline struct xprt_ops *xprt_get(int id)
751 {
752 if (id >= XPRT_ENTRIES)
753 return NULL;
754 return registered_xprt[id];
755 }
756
757 /* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
758 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
759 * call the xprt's add_xprt() method.
760 * Returns 0 on success, or non-zero on failure.
761 */
762 static inline int xprt_add_hs(struct connection *conn)
763 {
764 void *xprt_ctx = NULL;
765 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
766 void *nextxprt_ctx = NULL;
767 const struct xprt_ops *nextxprt_ops = NULL;
768
769 if (conn->flags & CO_FL_ERROR)
770 return -1;
771 if (ops->init(conn, &xprt_ctx) < 0)
772 return -1;
773 if (conn->xprt == xprt_get(XPRT_RAW)) {
774 nextxprt_ctx = conn->xprt_ctx;
775 nextxprt_ops = conn->xprt;
776 conn->xprt_ctx = xprt_ctx;
777 conn->xprt = ops;
778 } else {
779 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
780 &nextxprt_ctx, &nextxprt_ops) != 0) {
781 ops->close(conn, xprt_ctx);
782 return -1;
783 }
784 }
785 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
786 ops->close(conn, xprt_ctx);
787 return -1;
788 }
789 return 0;
790 }
791
792 static inline int conn_get_alpn(const struct connection *conn, const char **str, int *len)
793 {
794 if (!conn_xprt_ready(conn) || !conn->xprt->get_alpn)
795 return 0;
796 return conn->xprt->get_alpn(conn, conn->xprt_ctx, str, len);
797 }
798
799 /* registers proto mux list <list>. Modifies the list element! */
800 static inline void register_mux_proto(struct mux_proto_list *list)
801 {
802 LIST_ADDQ(&mux_proto_list.list, &list->list);
803 }
804
805 /* unregisters proto mux list <list> */
806 static inline void unregister_mux_proto(struct mux_proto_list *list)
807 {
808 LIST_DEL(&list->list);
809 LIST_INIT(&list->list);
810 }
811
812 static inline struct mux_proto_list *get_mux_proto(const struct ist proto)
813 {
814 struct mux_proto_list *item;
815
816 list_for_each_entry(item, &mux_proto_list.list, list) {
817 if (isteq(proto, item->token))
818 return item;
819 }
820 return NULL;
821 }
822
823 /* Lists the known proto mux on <out> */
824 static inline void list_mux_proto(FILE *out)
825 {
826 struct mux_proto_list *item;
827 struct ist proto;
828 char *mode, *side;
829
830 fprintf(out, "Available multiplexer protocols :\n"
831 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
832 list_for_each_entry(item, &mux_proto_list.list, list) {
833 proto = item->token;
834
835 if (item->mode == PROTO_MODE_ANY)
836 mode = "TCP|HTTP";
837 else if (item->mode == PROTO_MODE_TCP)
838 mode = "TCP";
839 else if (item->mode == PROTO_MODE_HTTP)
840 mode = "HTTP";
841 else
842 mode = "NONE";
843
844 if (item->side == PROTO_SIDE_BOTH)
845 side = "FE|BE";
846 else if (item->side == PROTO_SIDE_FE)
847 side = "FE";
848 else if (item->side == PROTO_SIDE_BE)
849 side = "BE";
850 else
851 side = "NONE";
852
853 fprintf(out, " %15s : mode=%-10s side=%-8s mux=%s\n",
854 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name);
855 }
856 }
857
858 /* returns the first mux entry in the list matching the exact same <mux_proto>
859 * and compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
860 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
861 * with exactly the same <proto_mode> or with an empty name. May return
862 * null if the code improperly registered the default mux to use as a fallback.
863 */
864 static inline const struct mux_proto_list *conn_get_best_mux_entry(
865 const struct ist mux_proto,
866 int proto_side, int proto_mode)
867 {
868 struct mux_proto_list *item;
869 struct mux_proto_list *fallback = NULL;
870
871 list_for_each_entry(item, &mux_proto_list.list, list) {
872 if (!(item->side & proto_side) || !(item->mode & proto_mode))
873 continue;
874 if (istlen(mux_proto) && isteq(mux_proto, item->token))
875 return item;
876 else if (!istlen(item->token)) {
877 if (!fallback || (item->mode == proto_mode && fallback->mode != proto_mode))
878 fallback = item;
879 }
880 }
881 return fallback;
882
883 }
884
885 /* returns the first mux in the list matching the exact same <mux_proto> and
886 * compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
887 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
888 * with exactly the same <proto_mode> or with an empty name. May return
889 * null if the code improperly registered the default mux to use as a fallback.
890 */
891 static inline const struct mux_ops *conn_get_best_mux(struct connection *conn,
892 const struct ist mux_proto,
893 int proto_side, int proto_mode)
894 {
895 const struct mux_proto_list *item;
896
897 item = conn_get_best_mux_entry(mux_proto, proto_side, proto_mode);
898
899 return item ? item->mux : NULL;
900 }
901
902 /* returns 0 if the connection is valid and is a frontend connection, otherwise
903 * returns 1 indicating it's a backend connection. And uninitialized connection
904 * also returns 1 to better handle the usage in the middle of initialization.
905 */
906 static inline int conn_is_back(const struct connection *conn)
907 {
908 return !objt_listener(conn->target);
909 }
910
911 /* returns a pointer to the proxy associated with this connection. For a front
912 * connection it returns a pointer to the frontend ; for a back connection, it
913 * returns a pointer to the backend.
914 */
915 static inline struct proxy *conn_get_proxy(const struct connection *conn)
916 {
917 struct listener *l;
918 struct server *s;
919
920 /* check if it's a frontend connection */
921 l = objt_listener(conn->target);
922 if (l)
923 return l->bind_conf->frontend;
924
925 /* check if it's a backend connection */
926 s = objt_server(conn->target);
927 if (s)
928 return s->proxy;
929
930 return objt_proxy(conn->target);
931 }
932
933 /* installs the best mux for incoming connection <conn> using the upper context
934 * <ctx>. If the mux protocol is forced, we use it to find the best
935 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
936 */
937 static inline int conn_install_mux_fe(struct connection *conn, void *ctx)
938 {
939 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
940 const struct mux_ops *mux_ops;
941
942 if (bind_conf->mux_proto)
943 mux_ops = bind_conf->mux_proto->mux;
944 else {
945 struct ist mux_proto;
946 const char *alpn_str = NULL;
947 int alpn_len = 0;
948 int mode;
949
950 if (bind_conf->frontend->mode == PR_MODE_HTTP)
951 mode = PROTO_MODE_HTTP;
952 else
953 mode = PROTO_MODE_TCP;
954
955 conn_get_alpn(conn, &alpn_str, &alpn_len);
956 mux_proto = ist2(alpn_str, alpn_len);
957 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
958 if (!mux_ops)
959 return -1;
960 }
961 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
962 }
963
964 /* installs the best mux for outgoing connection <conn> using the upper context
965 * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns
966 * < 0 on error.
967 */
968 static inline int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess)
969 {
970 struct server *srv = objt_server(conn->target);
971 struct proxy *prx = objt_proxy(conn->target);
972 const struct mux_ops *mux_ops;
973
974 if (srv)
975 prx = srv->proxy;
976
977 if (!prx) // target must be either proxy or server
978 return -1;
979
980 if (srv && srv->mux_proto)
981 mux_ops = srv->mux_proto->mux;
982 else {
983 struct ist mux_proto;
984 const char *alpn_str = NULL;
985 int alpn_len = 0;
986 int mode;
987
988 if (prx->mode == PR_MODE_HTTP)
989 mode = PROTO_MODE_HTTP;
990 else
991 mode = PROTO_MODE_TCP;
992
993 conn_get_alpn(conn, &alpn_str, &alpn_len);
994 mux_proto = ist2(alpn_str, alpn_len);
995
996 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
997 if (!mux_ops)
998 return -1;
999 }
1000 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
1001 }
1002
1003 /* installs the best mux for outgoing connection <conn> for a check using the
1004 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
1005 * find the best mux. Returns < 0 on error.
1006 */
1007 static inline int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
1008 {
1009 struct check *check = objt_check(sess->origin);
1010 struct server *srv = objt_server(conn->target);
1011 struct proxy *prx = objt_proxy(conn->target);
1012 const struct mux_ops *mux_ops;
1013
1014 if (!check) // Check must be defined
1015 return -1;
1016
1017 if (srv)
1018 prx = srv->proxy;
1019
1020 if (!prx) // target must be either proxy or server
1021 return -1;
1022
1023 if (check->mux_proto)
1024 mux_ops = check->mux_proto->mux;
1025 else {
1026 struct ist mux_proto;
1027 const char *alpn_str = NULL;
1028 int alpn_len = 0;
1029 int mode;
1030
1031 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
1032 mode = PROTO_MODE_HTTP;
1033 else
1034 mode = PROTO_MODE_TCP;
1035
1036 conn_get_alpn(conn, &alpn_str, &alpn_len);
1037 mux_proto = ist2(alpn_str, alpn_len);
1038
1039 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
1040 if (!mux_ops)
1041 return -1;
1042 }
1043 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
1044 }
1045
1046 /* Change the mux for the connection.
1047 * The caller should make sure he's not subscribed to the underlying XPRT.
1048 */
1049 static inline int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
1050 struct ist mux_proto, int mode)
1051 {
1052 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
1053 const struct mux_ops *old_mux, *new_mux;
1054 void *old_mux_ctx;
1055 const char *alpn_str = NULL;
1056 int alpn_len = 0;
1057
1058 if (!mux_proto.len) {
1059 conn_get_alpn(conn, &alpn_str, &alpn_len);
1060 mux_proto = ist2(alpn_str, alpn_len);
1061 }
1062 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
1063 old_mux = conn->mux;
1064
1065 /* No mux found */
1066 if (!new_mux)
1067 return -1;
1068
1069 /* Same mux, nothing to do */
1070 if (old_mux == new_mux)
1071 return 0;
1072
1073 old_mux_ctx = conn->ctx;
1074 conn->mux = new_mux;
1075 conn->ctx = ctx;
1076 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
1077 /* The mux upgrade failed, so restore the old mux */
1078 conn->ctx = old_mux_ctx;
1079 conn->mux = old_mux;
1080 return -1;
1081 }
1082
1083 /* The mux was upgraded, destroy the old one */
1084 *buf = BUF_NULL;
1085 old_mux->destroy(old_mux_ctx);
1086 return 0;
1087 }
1088
1089 #endif /* _HAPROXY_CONNECTION_H */
1090
1091 /*
1092 * Local variables:
1093 * c-indent-level: 8
1094 * c-basic-offset: 8
1095 * End:
1096 */