1 /*
2  * Session management functions.
3  *
4  * Copyright 2000-2015 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 <common/config.h>
14 #include <common/buffer.h>
15 #include <common/debug.h>
16 #include <common/http.h>
17 #include <common/memory.h>
18 
19 #include <types/global.h>
20 #include <types/session.h>
21 
22 #include <proto/connection.h>
23 #include <proto/listener.h>
24 #include <proto/log.h>
25 #include <proto/proxy.h>
26 #include <proto/session.h>
27 #include <proto/stream.h>
28 #include <proto/tcp_rules.h>
29 #include <proto/vars.h>
30 
31 DECLARE_POOL(pool_head_session, "session", sizeof(struct session));
32 DECLARE_POOL(pool_head_sess_srv_list, "session server list",
33 		sizeof(struct sess_srv_list));
34 
35 static int conn_complete_session(struct connection *conn);
36 static struct task *session_expire_embryonic(struct task *t, void *context, unsigned short state);
37 
38 /* Create a a new session and assign it to frontend <fe>, listener <li>,
39  * origin <origin>, set the current date and clear the stick counters pointers.
40  * Returns the session upon success or NULL. The session may be released using
41  * session_free(). Note: <li> may be NULL.
42  */
session_new(struct proxy * fe,struct listener * li,enum obj_type * origin)43 struct session *session_new(struct proxy *fe, struct listener *li, enum obj_type *origin)
44 {
45 	struct session *sess;
46 
47 	sess = pool_alloc(pool_head_session);
48 	if (sess) {
49 		sess->listener = li;
50 		sess->fe = fe;
51 		sess->origin = origin;
52 		sess->accept_date = date; /* user-visible date for logging */
53 		sess->tv_accept   = now;  /* corrected date for internal use */
54 		memset(sess->stkctr, 0, sizeof(sess->stkctr));
55 		vars_init(&sess->vars, SCOPE_SESS);
56 		sess->task = NULL;
57 		sess->t_handshake = -1; /* handshake not done yet */
58 		HA_ATOMIC_ADD(&totalconn, 1);
59 		HA_ATOMIC_ADD(&jobs, 1);
60 		LIST_INIT(&sess->srv_list);
61 		sess->idle_conns = 0;
62 		sess->flags = SESS_FL_NONE;
63 	}
64 	return sess;
65 }
66 
session_free(struct session * sess)67 void session_free(struct session *sess)
68 {
69 	struct connection *conn, *conn_back;
70 	struct sess_srv_list *srv_list, *srv_list_back;
71 
72 	if (sess->listener)
73 		listener_release(sess->listener);
74 	session_store_counters(sess);
75 	vars_prune_per_sess(&sess->vars);
76 	conn = objt_conn(sess->origin);
77 	if (conn != NULL && conn->mux)
78 		conn->mux->destroy(conn);
79 	list_for_each_entry_safe(srv_list, srv_list_back, &sess->srv_list, srv_list) {
80 		list_for_each_entry_safe(conn, conn_back, &srv_list->conn_list, session_list) {
81 			if (conn->mux) {
82 
83 				LIST_DEL(&conn->session_list);
84 				LIST_INIT(&conn->session_list);
85 				conn->owner = NULL;
86 				conn->flags &= ~CO_FL_SESS_IDLE;
87 				if (!srv_add_to_idle_list(objt_server(conn->target), conn))
88 					conn->mux->destroy(conn);
89 			} else {
90 				/* We have a connection, but not yet an associated mux.
91 				 * So destroy it now.
92 				 */
93 				if (!LIST_ISEMPTY(&conn->session_list)) {
94 					LIST_DEL(&conn->session_list);
95 					LIST_INIT(&conn->session_list);
96 				}
97 				conn_stop_tracking(conn);
98 				conn_full_close(conn);
99 				conn_free(conn);
100 			}
101 		}
102 		pool_free(pool_head_sess_srv_list, srv_list);
103 	}
104 	pool_free(pool_head_session, sess);
105 	HA_ATOMIC_SUB(&jobs, 1);
106 }
107 
108 /* callback used from the connection/mux layer to notify that a connection is
109  * going to be released.
110  */
conn_session_free(struct connection * conn)111 void conn_session_free(struct connection *conn)
112 {
113 	session_free(conn->owner);
114 }
115 
116 /* count a new session to keep frontend, listener and track stats up to date */
session_count_new(struct session * sess)117 static void session_count_new(struct session *sess)
118 {
119 	struct stkctr *stkctr;
120 	void *ptr;
121 	int i;
122 
123 	proxy_inc_fe_sess_ctr(sess->listener, sess->fe);
124 
125 	for (i = 0; i < MAX_SESS_STKCTR; i++) {
126 		stkctr = &sess->stkctr[i];
127 		if (!stkctr_entry(stkctr))
128 			continue;
129 
130 		ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
131 		if (ptr)
132 			stktable_data_cast(ptr, sess_cnt)++;
133 
134 		ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
135 		if (ptr)
136 			update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
137 					       stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
138 	}
139 }
140 
141 /* This function is called from the protocol layer accept() in order to
142  * instantiate a new session on behalf of a given listener and frontend. It
143  * returns a positive value upon success, 0 if the connection can be ignored,
144  * or a negative value upon critical failure. The accepted file descriptor is
145  * closed if we return <= 0. If no handshake is needed, it immediately tries
146  * to instantiate a new stream. The created connection's owner points to the
147  * new session until the upper layers are created.
148  */
session_accept_fd(struct listener * l,int cfd,struct sockaddr_storage * addr)149 int session_accept_fd(struct listener *l, int cfd, struct sockaddr_storage *addr)
150 {
151 	struct connection *cli_conn;
152 	struct proxy *p = l->bind_conf->frontend;
153 	struct session *sess;
154 	int ret;
155 
156 
157 	ret = -1; /* assume unrecoverable error by default */
158 
159 	if (unlikely((cli_conn = conn_new()) == NULL))
160 		goto out_close;
161 
162 	cli_conn->handle.fd = cfd;
163 	cli_conn->addr.from = *addr;
164 	cli_conn->flags |= CO_FL_ADDR_FROM_SET;
165 	cli_conn->target = &l->obj_type;
166 	cli_conn->proxy_netns = l->netns;
167 
168 	conn_prepare(cli_conn, l->proto, l->bind_conf->xprt);
169 	conn_ctrl_init(cli_conn);
170 
171 	/* wait for a PROXY protocol header */
172 	if (l->options & LI_O_ACC_PROXY) {
173 		cli_conn->flags |= CO_FL_ACCEPT_PROXY;
174 		conn_sock_want_recv(cli_conn);
175 	}
176 
177 	/* wait for a NetScaler client IP insertion protocol header */
178 	if (l->options & LI_O_ACC_CIP) {
179 		cli_conn->flags |= CO_FL_ACCEPT_CIP;
180 		conn_sock_want_recv(cli_conn);
181 	}
182 
183 	conn_xprt_want_recv(cli_conn);
184 	if (conn_xprt_init(cli_conn) < 0)
185 		goto out_free_conn;
186 
187 	sess = session_new(p, l, &cli_conn->obj_type);
188 	if (!sess)
189 		goto out_free_conn;
190 
191 	conn_set_owner(cli_conn, sess, NULL);
192 
193 	/* now evaluate the tcp-request layer4 rules. We only need a session
194 	 * and no stream for these rules.
195 	 */
196 	if ((l->options & LI_O_TCP_L4_RULES) && !tcp_exec_l4_rules(sess)) {
197 		/* let's do a no-linger now to close with a single RST. */
198 		setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
199 		ret = 0; /* successful termination */
200 		goto out_free_sess;
201 	}
202 
203 	/* monitor-net and health mode are processed immediately after TCP
204 	 * connection rules. This way it's possible to block them, but they
205 	 * never use the lower data layers, they send directly over the socket,
206 	 * as they were designed for. We first flush the socket receive buffer
207 	 * in order to avoid emission of an RST by the system. We ignore any
208 	 * error.
209 	 */
210 	if (unlikely((p->mode == PR_MODE_HEALTH) ||
211 		     ((l->options & LI_O_CHK_MONNET) &&
212 		      addr->ss_family == AF_INET &&
213 		      (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
214 		/* we have 4 possibilities here :
215 		 *  - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
216 		 *  - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
217 		 *  - HEALTH mode without HTTP check => just send "OK"
218 		 *  - TCP mode from monitoring address => just close
219 		 */
220 		if (l->proto->drain)
221 			l->proto->drain(cfd);
222 		if (p->mode == PR_MODE_HTTP ||
223 		    (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
224 			send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
225 		else if (p->mode == PR_MODE_HEALTH)
226 			send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
227 		ret = 0;
228 		goto out_free_sess;
229 	}
230 
231 	/* Adjust some socket options */
232 	if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) {
233 		setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one));
234 
235 		if (p->options & PR_O_TCP_CLI_KA)
236 			setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
237 
238 		if (p->options & PR_O_TCP_NOLING)
239 			fdtab[cfd].linger_risk = 1;
240 
241 #if defined(TCP_MAXSEG)
242 		if (l->maxseg < 0) {
243 			/* we just want to reduce the current MSS by that value */
244 			int mss;
245 			socklen_t mss_len = sizeof(mss);
246 			if (getsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &mss_len) == 0) {
247 				mss += l->maxseg; /* remember, it's < 0 */
248 				setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
249 			}
250 		}
251 #endif
252 	}
253 
254 	if (global.tune.client_sndbuf)
255 		setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
256 
257 	if (global.tune.client_rcvbuf)
258 		setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
259 
260 	/* OK, now either we have a pending handshake to execute with and then
261 	 * we must return to the I/O layer, or we can proceed with the end of
262 	 * the stream initialization. In case of handshake, we also set the I/O
263 	 * timeout to the frontend's client timeout and register a task in the
264 	 * session for this purpose. The connection's owner is left to the
265 	 * session during this period.
266 	 *
267 	 * At this point we set the relation between sess/task/conn this way :
268 	 *
269 	 *                   +----------------- task
270 	 *                   |                    |
271 	 *          orig -- sess <-- context      |
272 	 *           |       ^           |        |
273 	 *           v       |           |        |
274 	 *          conn -- owner ---> task <-----+
275 	 */
276 	if (cli_conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS)) {
277 		if (unlikely((sess->task = task_new(tid_bit)) == NULL))
278 			goto out_free_sess;
279 
280 		conn_set_xprt_done_cb(cli_conn, conn_complete_session);
281 
282 		sess->task->context = sess;
283 		sess->task->nice    = l->nice;
284 		sess->task->process = session_expire_embryonic;
285 		sess->task->expire  = tick_add_ifset(now_ms, p->timeout.client);
286 		task_queue(sess->task);
287 		return 1;
288 	}
289 
290 	/* OK let's complete stream initialization since there is no handshake */
291 	if (conn_complete_session(cli_conn) >= 0)
292 		return 1;
293 
294 	/* if we reach here we have deliberately decided not to keep this
295 	 * session (e.g. tcp-request rule), so that's not an error we should
296 	 * try to protect against.
297 	 */
298 	ret = 0;
299 
300 	/* error unrolling */
301  out_free_sess:
302 	 /* prevent call to listener_release during session_free. It will be
303 	  * done below, for all errors. */
304 	sess->listener = NULL;
305 	session_free(sess);
306  out_free_conn:
307 	conn_stop_tracking(cli_conn);
308 	conn_xprt_close(cli_conn);
309 	conn_free(cli_conn);
310  out_close:
311 	listener_release(l);
312 	if (ret < 0 && l->bind_conf->xprt == xprt_get(XPRT_RAW) &&
313 	    p->mode == PR_MODE_HTTP && l->bind_conf->mux_proto == NULL) {
314 		/* critical error, no more memory, try to emit a 500 response */
315 		send(cfd, http_err_msgs[HTTP_ERR_500], strlen(http_err_msgs[HTTP_ERR_500]),
316 		     MSG_DONTWAIT|MSG_NOSIGNAL);
317 	}
318 
319 	if (fdtab[cfd].owner)
320 		fd_delete(cfd);
321 	else
322 		close(cfd);
323 	return ret;
324 }
325 
326 
327 /* prepare the trash with a log prefix for session <sess>. It only works with
328  * embryonic sessions based on a real connection. This function requires that
329  * at sess->origin points to the incoming connection.
330  */
session_prepare_log_prefix(struct session * sess)331 static void session_prepare_log_prefix(struct session *sess)
332 {
333 	struct tm tm;
334 	char pn[INET6_ADDRSTRLEN];
335 	int ret;
336 	char *end;
337 	struct connection *cli_conn = __objt_conn(sess->origin);
338 
339 	ret = addr_to_str(&cli_conn->addr.from, pn, sizeof(pn));
340 	if (ret <= 0)
341 		chunk_printf(&trash, "unknown [");
342 	else if (ret == AF_UNIX)
343 		chunk_printf(&trash, "%s:%d [", pn, sess->listener->luid);
344 	else
345 		chunk_printf(&trash, "%s:%d [", pn, get_host_port(&cli_conn->addr.from));
346 
347 	get_localtime(sess->accept_date.tv_sec, &tm);
348 	end = date2str_log(trash.area + trash.data, &tm, &(sess->accept_date),
349 		           trash.size - trash.data);
350 	trash.data = end - trash.area;
351 	if (sess->listener->name)
352 		chunk_appendf(&trash, "] %s/%s", sess->fe->id, sess->listener->name);
353 	else
354 		chunk_appendf(&trash, "] %s/%d", sess->fe->id, sess->listener->luid);
355 }
356 
357 /* This function kills an existing embryonic session. It stops the connection's
358  * transport layer, releases assigned resources, resumes the listener if it was
359  * disabled and finally kills the file descriptor. This function requires that
360  * sess->origin points to the incoming connection.
361  */
session_kill_embryonic(struct session * sess,unsigned short state)362 static void session_kill_embryonic(struct session *sess, unsigned short state)
363 {
364 	int level = LOG_INFO;
365 	struct connection *conn = __objt_conn(sess->origin);
366 	struct task *task = sess->task;
367 	unsigned int log = sess->fe->to_log;
368 	const char *err_msg;
369 
370 	if (sess->fe->options2 & PR_O2_LOGERRORS)
371 		level = LOG_ERR;
372 
373 	if (log && (sess->fe->options & PR_O_NULLNOLOG)) {
374 		/* with "option dontlognull", we don't log connections with no transfer */
375 		if (!conn->err_code ||
376 		    conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
377 		    conn->err_code == CO_ER_CIP_EMPTY || conn->err_code == CO_ER_CIP_ABORT ||
378 		    conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
379 			log = 0;
380 	}
381 
382 	if (log) {
383 		if (!conn->err_code && (state & TASK_WOKEN_TIMER)) {
384 			if (conn->flags & CO_FL_ACCEPT_PROXY)
385 				conn->err_code = CO_ER_PRX_TIMEOUT;
386 			else if (conn->flags & CO_FL_ACCEPT_CIP)
387 				conn->err_code = CO_ER_CIP_TIMEOUT;
388 			else if (conn->flags & CO_FL_SSL_WAIT_HS)
389 				conn->err_code = CO_ER_SSL_TIMEOUT;
390 		}
391 
392 		session_prepare_log_prefix(sess);
393 		err_msg = conn_err_code_str(conn);
394 		if (err_msg)
395 			send_log(sess->fe, level, "%s: %s\n", trash.area,
396 				 err_msg);
397 		else
398 			send_log(sess->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
399 				 trash.area, conn->err_code, conn->flags);
400 	}
401 
402 	/* kill the connection now */
403 	conn_stop_tracking(conn);
404 	conn_full_close(conn);
405 	conn_free(conn);
406 	sess->origin = NULL;
407 
408 	task_delete(task);
409 	task_free(task);
410 	session_free(sess);
411 }
412 
413 /* Manages the embryonic session timeout. It is only called when the timeout
414  * strikes and performs the required cleanup.
415  */
session_expire_embryonic(struct task * t,void * context,unsigned short state)416 static struct task *session_expire_embryonic(struct task *t, void *context, unsigned short state)
417 {
418 	struct session *sess = context;
419 
420 	if (!(state & TASK_WOKEN_TIMER))
421 		return t;
422 
423 	session_kill_embryonic(sess, state);
424 	return NULL;
425 }
426 
427 /* Finish initializing a session from a connection, or kills it if the
428  * connection shows and error. Returns <0 if the connection was killed. It may
429  * be called either asynchronously as an xprt_done callback with an embryonic
430  * session, or synchronously to finalize the session. The distinction is made
431  * on sess->task which is only set in the embryonic session case.
432  */
conn_complete_session(struct connection * conn)433 static int conn_complete_session(struct connection *conn)
434 {
435 	struct session *sess = conn->owner;
436 
437 	sess->t_handshake = tv_ms_elapsed(&sess->tv_accept, &now);
438 
439 	conn_clear_xprt_done_cb(conn);
440 
441 	/* Verify if the connection just established. */
442 	if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
443 		conn->flags |= CO_FL_CONNECTED;
444 
445 	if (conn->flags & CO_FL_ERROR)
446 		goto fail;
447 
448 	/* if logs require transport layer information, note it on the connection */
449 	if (sess->fe->to_log & LW_XPRT)
450 		conn->flags |= CO_FL_XPRT_TRACKED;
451 
452 	/* we may have some tcp-request-session rules */
453 	if ((sess->listener->options & LI_O_TCP_L5_RULES) && !tcp_exec_l5_rules(sess))
454 		goto fail;
455 
456 	session_count_new(sess);
457 	if (conn_install_mux_fe(conn, NULL) < 0)
458 		goto fail;
459 
460 	/* the embryonic session's task is not needed anymore */
461 	if (sess->task) {
462 		task_delete(sess->task);
463 		task_free(sess->task);
464 		sess->task = NULL;
465 	}
466 
467 	conn_set_owner(conn, sess, conn_session_free);
468 
469 	return 0;
470 
471  fail:
472 	if (sess->task)
473 		session_kill_embryonic(sess, 0);
474 	return -1;
475 }
476 
477 /*
478  * Local variables:
479  *  c-indent-level: 8
480  *  c-basic-offset: 8
481  * End:
482  */
483