1 /*
2  * Frontend variables and functions.
3  *
4  * Copyright 2000-2013 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 <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <netinet/tcp.h>
24 
25 #include <common/chunk.h>
26 #include <common/compat.h>
27 #include <common/config.h>
28 #include <common/debug.h>
29 #include <common/standard.h>
30 #include <common/time.h>
31 
32 #include <types/global.h>
33 
34 #include <proto/acl.h>
35 #include <proto/arg.h>
36 #include <proto/channel.h>
37 #include <proto/fd.h>
38 #include <proto/frontend.h>
39 #include <proto/log.h>
40 #include <proto/hdr_idx.h>
41 #include <proto/proto_tcp.h>
42 #include <proto/proto_http.h>
43 #include <proto/proxy.h>
44 #include <proto/sample.h>
45 #include <proto/stream.h>
46 #include <proto/stream_interface.h>
47 #include <proto/task.h>
48 
49 /* Finish a stream accept() for a proxy (TCP or HTTP). It returns a negative
50  * value in case of a critical failure which must cause the listener to be
51  * disabled, a positive or null value in case of success.
52  */
frontend_accept(struct stream * s)53 int frontend_accept(struct stream *s)
54 {
55 	struct session *sess = s->sess;
56 	struct connection *conn = objt_conn(sess->origin);
57 	struct listener *l = sess->listener;
58 	struct proxy *fe = sess->fe;
59 
60 	if ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
61 	    && (!LIST_ISEMPTY(&fe->logsrvs))) {
62 		if (likely(!LIST_ISEMPTY(&fe->logformat))) {
63 			/* we have the client ip */
64 			if (s->logs.logwait & LW_CLIP)
65 				if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
66 					s->do_log(s);
67 		}
68 		else if (conn) {
69 			char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
70 
71 			conn_get_from_addr(conn);
72 			conn_get_to_addr(conn);
73 
74 			switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
75 			case AF_INET:
76 			case AF_INET6:
77 				addr_to_str(&conn->addr.to, sn, sizeof(sn));
78 				send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
79 					 pn, get_host_port(&conn->addr.from),
80 					 sn, get_host_port(&conn->addr.to),
81 					 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
82 				break;
83 			case AF_UNIX:
84 				/* UNIX socket, only the destination is known */
85 				send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
86 					 l->luid,
87 					 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
88 				break;
89 			}
90 		}
91 	}
92 
93 	if (unlikely((global.mode & MODE_DEBUG) && conn &&
94 		     (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
95 		char pn[INET6_ADDRSTRLEN];
96 		char alpn[16] = "<none>";
97 		const char *alpn_str = NULL;
98 		int alpn_len;
99 
100 		conn_get_from_addr(conn);
101 
102 		/* try to report the ALPN value when available (also works for NPN) */
103 
104 		if (conn && conn == cs_conn(objt_cs(s->si[0].end))) {
105 			if (conn_get_alpn(conn, &alpn_str, &alpn_len) && alpn_str) {
106 				int len = MIN(alpn_len, sizeof(alpn) - 1);
107 				memcpy(alpn, alpn_str, len);
108 				alpn[len] = 0;
109 			}
110 		}
111 
112 		switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
113 		case AF_INET:
114 		case AF_INET6:
115 			chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d] ALPN=%s\n",
116 			             s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
117 			             pn, get_host_port(&conn->addr.from), alpn);
118 			break;
119 		case AF_UNIX:
120 			/* UNIX socket, only the destination is known */
121 			chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d] ALPN=%s\n",
122 			             s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
123 			             l->luid, alpn);
124 			break;
125 		}
126 
127 		shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
128 	}
129 
130 	if (fe->mode == PR_MODE_HTTP)
131 		s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
132 
133 	if (unlikely(fe->nb_req_cap > 0)) {
134 		if ((s->req_cap = pool_alloc(fe->req_cap_pool)) == NULL)
135 			goto out_return;	/* no memory */
136 		memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
137 	}
138 
139 	if (unlikely(fe->nb_rsp_cap > 0)) {
140 		if ((s->res_cap = pool_alloc(fe->rsp_cap_pool)) == NULL)
141 			goto out_free_reqcap;	/* no memory */
142 		memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
143 	}
144 
145 	if (fe->http_needed) {
146 		/* we have to allocate header indexes only if we know
147 		 * that we may make use of them. This of course includes
148 		 * (mode == PR_MODE_HTTP).
149 		 */
150 		if (unlikely(!http_alloc_txn(s)))
151 			goto out_free_rspcap; /* no memory */
152 
153 		/* and now initialize the HTTP transaction state */
154 		http_init_txn(s);
155 	}
156 
157 	/* everything's OK, let's go on */
158 	return 1;
159 
160 	/* Error unrolling */
161  out_free_rspcap:
162 	pool_free(fe->rsp_cap_pool, s->res_cap);
163  out_free_reqcap:
164 	pool_free(fe->req_cap_pool, s->req_cap);
165  out_return:
166 	return -1;
167 }
168 
169 /************************************************************************/
170 /*      All supported sample and ACL keywords must be declared here.    */
171 /************************************************************************/
172 
173 /* set temp integer to the id of the frontend */
174 static int
smp_fetch_fe_id(const struct arg * args,struct sample * smp,const char * kw,void * private)175 smp_fetch_fe_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
176 {
177 	smp->flags = SMP_F_VOL_SESS;
178 	smp->data.type = SMP_T_SINT;
179 	smp->data.u.sint = smp->sess->fe->uuid;
180 	return 1;
181 }
182 
183 /* set string to the name of the frontend */
184 static int
smp_fetch_fe_name(const struct arg * args,struct sample * smp,const char * kw,void * private)185 smp_fetch_fe_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
186 {
187 	smp->data.u.str.str = (char *)smp->sess->fe->id;
188 	if (!smp->data.u.str.str)
189 		return 0;
190 
191 	smp->data.type = SMP_T_STR;
192 	smp->flags = SMP_F_CONST;
193 	smp->data.u.str.len = strlen(smp->data.u.str.str);
194 	return 1;
195 }
196 
197 /* set temp integer to the number of HTTP requests per second reaching the frontend.
198  * Accepts exactly 1 argument. Argument is a frontend, other types will cause
199  * an undefined behaviour.
200  */
201 static int
smp_fetch_fe_req_rate(const struct arg * args,struct sample * smp,const char * kw,void * private)202 smp_fetch_fe_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
203 {
204 	smp->flags = SMP_F_VOL_TEST;
205 	smp->data.type = SMP_T_SINT;
206 	smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_req_per_sec);
207 	return 1;
208 }
209 
210 /* set temp integer to the number of connections per second reaching the frontend.
211  * Accepts exactly 1 argument. Argument is a frontend, other types will cause
212  * an undefined behaviour.
213  */
214 static int
smp_fetch_fe_sess_rate(const struct arg * args,struct sample * smp,const char * kw,void * private)215 smp_fetch_fe_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
216 {
217 	smp->flags = SMP_F_VOL_TEST;
218 	smp->data.type = SMP_T_SINT;
219 	smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
220 	return 1;
221 }
222 
223 /* set temp integer to the number of concurrent connections on the frontend
224  * Accepts exactly 1 argument. Argument is a frontend, other types will cause
225  * an undefined behaviour.
226  */
227 static int
smp_fetch_fe_conn(const struct arg * args,struct sample * smp,const char * kw,void * private)228 smp_fetch_fe_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
229 {
230 	smp->flags = SMP_F_VOL_TEST;
231 	smp->data.type = SMP_T_SINT;
232 	smp->data.u.sint = args->data.prx->feconn;
233 	return 1;
234 }
235 
236 
237 /* Note: must not be declared <const> as its list will be overwritten.
238  * Please take care of keeping this list alphabetically sorted.
239  */
240 static struct sample_fetch_kw_list smp_kws = {ILH, {
241 	{ "fe_conn",      smp_fetch_fe_conn,      ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
242 	{ "fe_id",        smp_fetch_fe_id,        0,          NULL, SMP_T_SINT, SMP_USE_FTEND, },
243 	{ "fe_name",      smp_fetch_fe_name,      0,          NULL, SMP_T_STR,  SMP_USE_FTEND, },
244 	{ "fe_req_rate",  smp_fetch_fe_req_rate,  ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
245 	{ "fe_sess_rate", smp_fetch_fe_sess_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
246 	{ /* END */ },
247 }};
248 
249 
250 /* Note: must not be declared <const> as its list will be overwritten.
251  * Please take care of keeping this list alphabetically sorted.
252  */
253 static struct acl_kw_list acl_kws = {ILH, {
254 	{ /* END */ },
255 }};
256 
257 
258 __attribute__((constructor))
__frontend_init(void)259 static void __frontend_init(void)
260 {
261 	sample_register_fetches(&smp_kws);
262 	acl_register_keywords(&acl_kws);
263 }
264 
265 
266 /*
267  * Local variables:
268  *  c-indent-level: 8
269  *  c-basic-offset: 8
270  * End:
271  */
272