1 /*
2  * Socket Pair protocol layer (sockpair)
3  *
4  * Copyright HAProxy Technologies - William Lallemand <wlallemand@haproxy.com>
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 <ctype.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <pwd.h>
17 #include <grp.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <syslog.h>
22 #include <time.h>
23 
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 
29 #include <common/compat.h>
30 #include <common/config.h>
31 #include <common/debug.h>
32 #include <common/errors.h>
33 #include <common/initcall.h>
34 #include <common/mini-clist.h>
35 #include <common/standard.h>
36 #include <common/time.h>
37 #include <common/version.h>
38 
39 #include <types/global.h>
40 
41 #include <proto/connection.h>
42 #include <proto/fd.h>
43 #include <proto/freq_ctr.h>
44 #include <proto/listener.h>
45 #include <proto/log.h>
46 #include <proto/protocol.h>
47 #include <proto/task.h>
48 
49 static void sockpair_add_listener(struct listener *listener, int port);
50 static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
51 static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
52 static int sockpair_connect_server(struct connection *conn, int flags);
53 
54 /* Note: must not be declared <const> as its list will be overwritten */
55 static struct protocol proto_sockpair = {
56 	.name = "sockpair",
57 	.sock_domain = AF_CUST_SOCKPAIR,
58 	.sock_type = SOCK_STREAM,
59 	.sock_prot = 0,
60 	.sock_family = AF_UNIX,
61 	.sock_addrlen = sizeof(struct sockaddr_un),
62 	.l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
63 	.accept = &listener_accept,
64 	.connect = &sockpair_connect_server,
65 	.bind = sockpair_bind_listener,
66 	.bind_all = sockpair_bind_listeners,
67 	.unbind_all = NULL,
68 	.enable_all = enable_all_listeners,
69 	.disable_all = disable_all_listeners,
70 	.get_src = NULL,
71 	.get_dst = NULL,
72 	.pause = NULL,
73 	.add = sockpair_add_listener,
74 	.listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
75 	.nb_listeners = 0,
76 };
77 
78 INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
79 
80 /* Add <listener> to the list of sockpair listeners (port is ignored). The
81  * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
82  * The number of listeners for the protocol is updated.
83  *
84  * Must be called with proto_lock held.
85  *
86  */
sockpair_add_listener(struct listener * listener,int port)87 static void sockpair_add_listener(struct listener *listener, int port)
88 {
89 	if (listener->state != LI_INIT)
90 		return;
91 	listener->state = LI_ASSIGNED;
92 	listener->proto = &proto_sockpair;
93 	LIST_ADDQ(&proto_sockpair.listeners, &listener->proto_list);
94 	proto_sockpair.nb_listeners++;
95 }
96 
97 /* This function creates all UNIX sockets bound to the protocol entry <proto>.
98  * It is intended to be used as the protocol's bind_all() function.
99  * The sockets will be registered but not added to any fd_set, in order not to
100  * loose them across the fork(). A call to uxst_enable_listeners() is needed
101  * to complete initialization.
102  *
103  * Must be called with proto_lock held.
104  *
105  * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
106  */
sockpair_bind_listeners(struct protocol * proto,char * errmsg,int errlen)107 static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
108 {
109 	struct listener *listener;
110 	int err = ERR_NONE;
111 
112 	list_for_each_entry(listener, &proto->listeners, proto_list) {
113 		err |= sockpair_bind_listener(listener, errmsg, errlen);
114 		if (err & ERR_ABORT)
115 			break;
116 	}
117 	return err;
118 }
119 
120 /* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
121  * enabled for polling.  The return value is composed from ERR_NONE,
122  * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
123  * <errmsg> if the message is at most <errlen> bytes long (including '\0').
124  * Note that <errmsg> may be NULL if <errlen> is also zero.
125  */
sockpair_bind_listener(struct listener * listener,char * errmsg,int errlen)126 static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
127 {
128 	int fd = listener->fd;
129 	int err;
130 	const char *msg = NULL;
131 
132 	err = ERR_NONE;
133 
134 	/* ensure we never return garbage */
135 	if (errlen)
136 		*errmsg = 0;
137 
138 	if (listener->state != LI_ASSIGNED)
139 		return ERR_NONE; /* already bound */
140 
141 	if (listener->fd == -1) {
142 		err |= ERR_FATAL | ERR_ALERT;
143 		msg = "sockpair can be only used with inherited FDs";
144 		goto err_return;
145 	}
146 
147 	if (fd >= global.maxsock) {
148 		err |= ERR_FATAL | ERR_ALERT;
149 		msg = "socket(): not enough free sockets, raise -n argument";
150 		goto err_return;
151 	}
152 	if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
153 		err |= ERR_FATAL | ERR_ALERT;
154 		msg = "cannot make sockpair non-blocking";
155 		goto err_return;
156 	}
157 
158 	listener->state = LI_LISTEN;
159 
160 	fd_insert(fd, listener, listener->proto->accept,
161 	          thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
162 
163 	return err;
164 
165  err_return:
166 	if (msg && errlen)
167 		snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
168 	return err;
169 }
170 
171 /*
172  * Send FD over a unix socket
173  *
174  * <send_fd> is the FD to send
175  * <fd> is the fd of the unix socket to use for the transfer
176  *
177  * The iobuf variable could be use in the future to enhance the protocol.
178  */
send_fd_uxst(int fd,int send_fd)179 int send_fd_uxst(int fd, int send_fd)
180 {
181 	char iobuf[2];
182 	struct iovec iov;
183 	struct msghdr msghdr;
184 
185 	char cmsgbuf[CMSG_SPACE(sizeof(int))];
186 	char buf[CMSG_SPACE(sizeof(int))];
187 	struct cmsghdr *cmsg = (void *)buf;
188 
189 	int *fdptr;
190 
191 	iov.iov_base = iobuf;
192 	iov.iov_len = sizeof(iobuf);
193 
194 	memset(&msghdr, 0, sizeof(msghdr));
195 	msghdr.msg_iov = &iov;
196 	msghdr.msg_iovlen = 1;
197 
198 	/* Now send the fds */
199 	msghdr.msg_control = cmsgbuf;
200 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
201 
202 	cmsg = CMSG_FIRSTHDR(&msghdr);
203 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
204 	cmsg->cmsg_level = SOL_SOCKET;
205 	cmsg->cmsg_type = SCM_RIGHTS;
206 
207 	fdptr = (int *)CMSG_DATA(cmsg);
208 	memcpy(fdptr, &send_fd, sizeof(send_fd));
209 
210 	if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
211 		ha_warning("Failed to transfer socket\n");
212 		return 1;
213 	}
214 
215 	return 0;
216 }
217 
218 /*
219  *
220  * This function works like uxst_connect_server but instead of creating a
221  * socket and establishing a connection, it creates a pair of connected
222  * sockets, and send one of them through the destination FD. The destination FD
223  * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
224  *
225  * conn->target may point either to a valid server or to a backend, depending
226  * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
227  * <data> parameter is a boolean indicating whether there are data waiting for
228  * being sent or not, in order to adjust data write polling and on some
229  * platforms. The <delack> argument is ignored.
230  *
231  * Note that a pending send_proxy message accounts for data.
232  *
233  * It can return one of :
234  *  - SF_ERR_NONE if everything's OK
235  *  - SF_ERR_SRVTO if there are no more servers
236  *  - SF_ERR_SRVCL if the connection was refused by the server
237  *  - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
238  *  - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
239  *  - SF_ERR_INTERNAL for any other purely internal errors
240  * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
241  *
242  * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
243  * it's invalid and the caller has nothing to do.
244  */
sockpair_connect_server(struct connection * conn,int flags)245 static int sockpair_connect_server(struct connection *conn, int flags)
246 {
247 	int sv[2], fd, dst_fd = -1;
248 
249 	/* the FD is stored in the sockaddr struct */
250 	dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
251 
252 	if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
253 	    obj_type(conn->target) != OBJ_TYPE_SERVER) {
254 		conn->flags |= CO_FL_ERROR;
255 		return SF_ERR_INTERNAL;
256 	}
257 
258 	if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
259 		ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
260 		conn->flags |= CO_FL_ERROR;
261 		return SF_ERR_RESOURCE;
262 	}
263 
264 	fd = conn->handle.fd = sv[1];
265 
266 	if (fd >= global.maxsock) {
267 		/* do not log anything there, it's a normal condition when this option
268 		 * is used to serialize connections to a server !
269 		 */
270 		ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
271 		close(sv[0]);
272 		close(sv[1]);
273 		conn->err_code = CO_ER_CONF_FDLIM;
274 		conn->flags |= CO_FL_ERROR;
275 		return SF_ERR_PRXCOND; /* it is a configuration limit */
276 	}
277 
278 	if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
279 		qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
280 		close(sv[0]);
281 		close(sv[1]);
282 		conn->err_code = CO_ER_SOCK_ERR;
283 		conn->flags |= CO_FL_ERROR;
284 		return SF_ERR_INTERNAL;
285 	}
286 
287 	if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
288 		ha_alert("Cannot set CLOEXEC on client socket.\n");
289 		close(sv[0]);
290 		close(sv[1]);
291 		conn->err_code = CO_ER_SOCK_ERR;
292 		conn->flags |= CO_FL_ERROR;
293 		return SF_ERR_INTERNAL;
294 	}
295 
296 	/* if a send_proxy is there, there are data */
297 	if (conn->send_proxy_ofs)
298 		flags |= CONNECT_HAS_DATA;
299 
300 	if (global.tune.server_sndbuf)
301                 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
302 
303 	if (global.tune.server_rcvbuf)
304                 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
305 
306 	/* The new socket is sent on the other side, it should be retrieved and
307 	 * considered as an 'accept' socket on the server side */
308 	if (send_fd_uxst(dst_fd, sv[0]) == -1) {
309 		close(sv[0]);
310 		close(sv[1]);
311 		conn->err_code = CO_ER_SOCK_ERR;
312 		conn->flags |= CO_FL_ERROR;
313 		return SF_ERR_INTERNAL;
314 	}
315 
316 	close(sv[0]); /* we don't need this side anymore */
317 
318 	conn->flags &= ~CO_FL_WAIT_L4_CONN;
319 
320 	conn->flags |= CO_FL_ADDR_TO_SET;
321 
322 	/* Prepare to send a few handshakes related to the on-wire protocol. */
323 	if (conn->send_proxy_ofs)
324 		conn->flags |= CO_FL_SEND_PROXY;
325 
326 	conn_ctrl_init(conn);       /* registers the FD */
327 	fdtab[fd].linger_risk = 0;  /* no need to disable lingering */
328 
329 	if (conn_xprt_init(conn) < 0) {
330 		conn_full_close(conn);
331 		conn->flags |= CO_FL_ERROR;
332 		return SF_ERR_RESOURCE;
333 	}
334 
335 	conn_xprt_want_send(conn);  /* for connect status, proxy protocol or SSL */
336 	return SF_ERR_NONE;  /* connection is OK */
337 }
338 
339 
340 /*
341  * Receives a file descriptor transferred from a unix socket.
342  *
343  * Return -1 or a socket fd;
344  *
345  * The iobuf variable could be used in the future to enhance the protocol.
346  */
recv_fd_uxst(int sock)347 int recv_fd_uxst(int sock)
348 {
349 	struct msghdr msghdr;
350 	struct iovec iov;
351 	char iobuf[2];
352 
353 	char cmsgbuf[CMSG_SPACE(sizeof(int))];
354 	char buf[CMSG_SPACE(sizeof(int))];
355 	struct cmsghdr *cmsg = (void *)buf;
356 
357 
358 	int recv_fd = -1;
359 	int ret = -1;
360 
361 	memset(&msghdr, 0, sizeof(msghdr));
362 
363 	iov.iov_base = iobuf;
364 	iov.iov_len = sizeof(iobuf);
365 
366 	msghdr.msg_iov = &iov;
367 	msghdr.msg_iovlen = 1;
368 
369 	msghdr.msg_control = cmsgbuf;
370 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
371 
372 	iov.iov_len = sizeof(iobuf);
373 	iov.iov_base = iobuf;
374 
375 	while (1) {
376 		ret = recvmsg(sock, &msghdr, 0);
377 		if (ret == -1 && errno == EINTR)
378 			continue;
379 		else
380 			break;
381 	}
382 
383 	if (ret == -1)
384 		return ret;
385 
386 	cmsg = CMSG_FIRSTHDR(&msghdr);
387 	if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
388 	    cmsg->cmsg_type == SCM_RIGHTS) {
389 		size_t totlen = cmsg->cmsg_len -
390 			CMSG_LEN(0);
391 		memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
392 	}
393 	return recv_fd;
394 }
395 
396 /*
397  * Local variables:
398  *  c-indent-level: 8
399  *  c-basic-offset: 8
400  * End:
401  */
402