xref: /freebsd/sbin/ggate/ggated/ggated.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <pthread.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/endian.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/disk.h>
43 #include <sys/bio.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <arpa/inet.h>
47 #include <signal.h>
48 #include <assert.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <libgen.h>
53 #include <syslog.h>
54 #include <stdarg.h>
55 
56 #include "ggate.h"
57 
58 
59 #define	GGATED_EXPORT_FILE	"/etc/gg.exports"
60 
61 struct ggd_connection {
62 	off_t		 c_mediasize;
63 	unsigned	 c_sectorsize;
64 	unsigned	 c_flags;	/* flags (RO/RW) */
65 	int		 c_diskfd;
66 	int		 c_sendfd;
67 	int		 c_recvfd;
68 	time_t		 c_birthtime;
69 	char		*c_path;
70 	uint64_t	 c_token;
71 	in_addr_t	 c_srcip;
72 	LIST_ENTRY(ggd_connection) c_next;
73 };
74 
75 struct ggd_request {
76 	struct g_gate_hdr	 r_hdr;
77 	char			*r_data;
78 	TAILQ_ENTRY(ggd_request) r_next;
79 };
80 #define	r_cmd		r_hdr.gh_cmd
81 #define	r_offset	r_hdr.gh_offset
82 #define	r_length	r_hdr.gh_length
83 #define	r_error		r_hdr.gh_error
84 
85 struct ggd_export {
86 	char		*e_path;	/* path to device/file */
87 	in_addr_t	 e_ip;		/* remote IP address */
88 	in_addr_t	 e_mask;	/* IP mask */
89 	unsigned	 e_flags;	/* flags (RO/RW) */
90 	SLIST_ENTRY(ggd_export) e_next;
91 };
92 
93 static const char *exports_file = GGATED_EXPORT_FILE;
94 static int got_sighup = 0;
95 in_addr_t bindaddr;
96 
97 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
98 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
99 pthread_mutex_t inqueue_mtx, outqueue_mtx;
100 pthread_cond_t inqueue_cond, outqueue_cond;
101 
102 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(&exports);
103 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(&connection);
104 
105 static void *recv_thread(void *arg);
106 static void *disk_thread(void *arg);
107 static void *send_thread(void *arg);
108 
109 static void
110 usage(void)
111 {
112 
113 	fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] "
114 	    "[-S sndbuf] [exports file]\n", getprogname());
115 	exit(EXIT_FAILURE);
116 }
117 
118 static char *
119 ip2str(in_addr_t ip)
120 {
121 	static char sip[16];
122 
123 	snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
124 	    ((ip >> 24) & 0xff),
125 	    ((ip >> 16) & 0xff),
126 	    ((ip >> 8) & 0xff),
127 	    (ip & 0xff));
128 	return (sip);
129 }
130 
131 static in_addr_t
132 countmask(unsigned m)
133 {
134 	in_addr_t mask;
135 
136 	if (m == 0) {
137 		mask = 0x0;
138 	} else {
139 		mask = 1 << (32 - m);
140 		mask--;
141 		mask = ~mask;
142 	}
143 	return (mask);
144 }
145 
146 static void
147 line_parse(char *line, unsigned lineno)
148 {
149 	struct ggd_export *ex;
150 	char *word, *path, *sflags;
151 	unsigned flags, i, vmask;
152 	in_addr_t ip, mask;
153 
154 	ip = mask = flags = vmask = 0;
155 	path = NULL;
156 	sflags = NULL;
157 
158 	for (i = 0, word = strtok(line, " \t"); word != NULL;
159 	    i++, word = strtok(NULL, " \t")) {
160 		switch (i) {
161 		case 0: /* IP address or host name */
162 			ip = g_gate_str2ip(strsep(&word, "/"));
163 			if (ip == INADDR_NONE) {
164 				g_gate_xlog("Invalid IP/host name at line %u.",
165 				    lineno);
166 			}
167 			ip = ntohl(ip);
168 			if (word == NULL)
169 				vmask = 32;
170 			else {
171 				errno = 0;
172 				vmask = strtoul(word, NULL, 10);
173 				if (vmask == 0 && errno != 0) {
174 					g_gate_xlog("Invalid IP mask value at "
175 					    "line %u.", lineno);
176 				}
177 				if ((unsigned)vmask > 32) {
178 					g_gate_xlog("Invalid IP mask value at line %u.",
179 					    lineno);
180 				}
181 			}
182 			mask = countmask(vmask);
183 			break;
184 		case 1:	/* flags */
185 			if (strcasecmp("rd", word) == 0 ||
186 			    strcasecmp("ro", word) == 0) {
187 				flags = O_RDONLY;
188 			} else if (strcasecmp("wo", word) == 0) {
189 				flags = O_WRONLY;
190 			} else if (strcasecmp("rw", word) == 0) {
191 				flags = O_RDWR;
192 			} else {
193 				g_gate_xlog("Invalid value in flags field at "
194 				    "line %u.", lineno);
195 			}
196 			sflags = word;
197 			break;
198 		case 2:	/* path */
199 			if (strlen(word) >= MAXPATHLEN) {
200 				g_gate_xlog("Path too long at line %u. ",
201 				    lineno);
202 			}
203 			path = word;
204 			break;
205 		default:
206 			g_gate_xlog("Too many arguments at line %u. ", lineno);
207 		}
208 	}
209 	if (i != 3)
210 		g_gate_xlog("Too few arguments at line %u.", lineno);
211 
212 	ex = malloc(sizeof(*ex));
213 	if (ex == NULL)
214 		g_gate_xlog("No enough memory.");
215 	ex->e_path = strdup(path);
216 	if (ex->e_path == NULL)
217 		g_gate_xlog("No enough memory.");
218 
219 	/* Made 'and' here. */
220 	ex->e_ip = (ip & mask);
221 	ex->e_mask = mask;
222 	ex->e_flags = flags;
223 
224 	SLIST_INSERT_HEAD(&exports, ex, e_next);
225 
226 	g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
227 	    ip2str(ex->e_ip), vmask, path, sflags);
228 }
229 
230 static void
231 exports_clear(void)
232 {
233 	struct ggd_export *ex;
234 
235 	while (!SLIST_EMPTY(&exports)) {
236 		ex = SLIST_FIRST(&exports);
237 		SLIST_REMOVE_HEAD(&exports, e_next);
238 		free(ex);
239 	}
240 }
241 
242 #define	EXPORTS_LINE_SIZE	2048
243 static void
244 exports_get(void)
245 {
246 	char buf[EXPORTS_LINE_SIZE], *line;
247 	unsigned lineno = 0, objs = 0, len;
248 	FILE *fd;
249 
250 	exports_clear();
251 
252 	fd = fopen(exports_file, "r");
253 	if (fd == NULL) {
254 		g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
255 		    strerror(errno));
256 	}
257 
258 	g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
259 
260 	for (;;) {
261 		if (fgets(buf, sizeof(buf), fd) == NULL) {
262 			if (feof(fd))
263 				break;
264 
265 			g_gate_xlog("Error while reading exports file: %s.",
266 			    strerror(errno));
267 		}
268 
269 		/* Increase line count. */
270 		lineno++;
271 
272 		/* Skip spaces and tabs. */
273 		for (line = buf; *line == ' ' || *line == '\t'; ++line)
274 			;
275 
276 		/* Empty line, comment or empty line at the end of file. */
277 		if (*line == '\n' || *line == '#' || *line == '\0')
278 			continue;
279 
280 		len = strlen(line);
281 		if (line[len - 1] == '\n') {
282 			/* Remove new line char. */
283 			line[len - 1] = '\0';
284 		} else {
285 			if (!feof(fd))
286 				g_gate_xlog("Line %u too long.", lineno);
287 		}
288 
289 		line_parse(line, lineno);
290 		objs++;
291 	}
292 
293 	fclose(fd);
294 
295 	if (objs == 0)
296 		g_gate_xlog("There are no objects to export.");
297 
298 	g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
299 }
300 
301 static int
302 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
303     struct ggd_connection *conn)
304 {
305 	char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
306 	int error = 0, flags;
307 
308 	strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
309 	strlcat(ipmask, "/", sizeof(ipmask));
310 	strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
311 	if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
312 		if (ex->e_flags == O_WRONLY) {
313 			g_gate_log(LOG_WARNING, "Read-only access requested, "
314 			    "but %s (%s) is exported write-only.", ex->e_path,
315 			    ipmask);
316 			return (EPERM);
317 		} else {
318 			conn->c_flags |= GGATE_FLAG_RDONLY;
319 		}
320 	} else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
321 		if (ex->e_flags == O_RDONLY) {
322 			g_gate_log(LOG_WARNING, "Write-only access requested, "
323 			    "but %s (%s) is exported read-only.", ex->e_path,
324 			    ipmask);
325 			return (EPERM);
326 		} else {
327 			conn->c_flags |= GGATE_FLAG_WRONLY;
328 		}
329 	} else {
330 		if (ex->e_flags == O_RDONLY) {
331 			g_gate_log(LOG_WARNING, "Read-write access requested, "
332 			    "but %s (%s) is exported read-only.", ex->e_path,
333 			    ipmask);
334 			return (EPERM);
335 		} else if (ex->e_flags == O_WRONLY) {
336 			g_gate_log(LOG_WARNING, "Read-write access requested, "
337 			    "but %s (%s) is exported write-only.", ex->e_path,
338 			    ipmask);
339 			return (EPERM);
340 		}
341 	}
342 	if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
343 		flags = O_RDONLY;
344 	else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
345 		flags = O_WRONLY;
346 	else
347 		flags = O_RDWR;
348 	conn->c_diskfd = open(ex->e_path, flags);
349 	if (conn->c_diskfd == -1) {
350 		error = errno;
351 		g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
352 		    strerror(error));
353 		return (error);
354 	}
355 	return (0);
356 }
357 
358 static struct ggd_export *
359 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
360     struct ggd_connection *conn)
361 {
362 	struct ggd_export *ex;
363 	in_addr_t ip;
364 	int error;
365 
366 	ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
367 	SLIST_FOREACH(ex, &exports, e_next) {
368 		if ((ip & ex->e_mask) != ex->e_ip) {
369 			g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
370 			    ex->e_path);
371 			continue;
372 		}
373 		if (strcmp(cinit->gc_path, ex->e_path) != 0) {
374 			g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
375 			    ex->e_path);
376 			continue;
377 		}
378 		error = exports_check(ex, cinit, conn);
379 		if (error == 0)
380 			return (ex);
381 		else {
382 			errno = error;
383 			return (NULL);
384 		}
385 	}
386 	g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
387 	    ip2str(ip));
388 	errno = EPERM;
389 	return (NULL);
390 }
391 
392 /*
393  * Remove timed out connections.
394  */
395 static void
396 connection_cleanups(void)
397 {
398 	struct ggd_connection *conn, *tconn;
399 	time_t now;
400 
401 	time(&now);
402 	LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
403 		if (now - conn->c_birthtime > 10) {
404 			LIST_REMOVE(conn, c_next);
405 			g_gate_log(LOG_NOTICE,
406 			    "Connection from %s [%s] removed.",
407 			    ip2str(conn->c_srcip), conn->c_path);
408 			close(conn->c_diskfd);
409 			close(conn->c_sendfd);
410 			close(conn->c_recvfd);
411 			free(conn->c_path);
412 			free(conn);
413 		}
414 	}
415 }
416 
417 static struct ggd_connection *
418 connection_find(struct g_gate_cinit *cinit)
419 {
420 	struct ggd_connection *conn;
421 
422 	LIST_FOREACH(conn, &connections, c_next) {
423 		if (conn->c_token == cinit->gc_token)
424 			break;
425 	}
426 	return (conn);
427 }
428 
429 static struct ggd_connection *
430 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
431 {
432 	struct ggd_connection *conn;
433 	in_addr_t ip;
434 
435 	/*
436 	 * First, look for old connections.
437 	 * We probably should do it every X seconds, but what for?
438 	 * It is only dangerous if an attacker wants to overload connections
439 	 * queue, so here is a good place to do the cleanups.
440 	 */
441 	connection_cleanups();
442 
443 	conn = malloc(sizeof(*conn));
444 	if (conn == NULL)
445 		return (NULL);
446 	conn->c_path = strdup(cinit->gc_path);
447 	if (conn->c_path == NULL) {
448 		free(conn);
449 		return (NULL);
450 	}
451 	conn->c_token = cinit->gc_token;
452 	ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
453 	conn->c_srcip = ip;
454 	conn->c_sendfd = conn->c_recvfd = -1;
455 	if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
456 		conn->c_sendfd = sfd;
457 	else
458 		conn->c_recvfd = sfd;
459 	conn->c_mediasize = 0;
460 	conn->c_sectorsize = 0;
461 	time(&conn->c_birthtime);
462 	conn->c_flags = cinit->gc_flags;
463 	LIST_INSERT_HEAD(&connections, conn, c_next);
464 	g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
465 	    conn->c_path);
466 	return (conn);
467 }
468 
469 static int
470 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
471     struct sockaddr *s, int sfd)
472 {
473 	in_addr_t ip;
474 
475 	ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
476 	if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
477 		if (conn->c_sendfd != -1) {
478 			g_gate_log(LOG_WARNING,
479 			    "Send socket already exists [%s, %s].", ip2str(ip),
480 			    conn->c_path);
481 			return (EEXIST);
482 		}
483 		conn->c_sendfd = sfd;
484 	} else {
485 		if (conn->c_recvfd != -1) {
486 			g_gate_log(LOG_WARNING,
487 			    "Receive socket already exists [%s, %s].",
488 			    ip2str(ip), conn->c_path);
489 			return (EEXIST);
490 		}
491 		conn->c_recvfd = sfd;
492 	}
493 	g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
494 	    conn->c_path);
495 	return (0);
496 }
497 
498 /*
499  * Remove one socket from the given connection or the whole
500  * connection if sfd == -1.
501  */
502 static void
503 connection_remove(struct ggd_connection *conn)
504 {
505 
506 	LIST_REMOVE(conn, c_next);
507 	g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
508 	    ip2str(conn->c_srcip), conn->c_path);
509 	if (conn->c_sendfd != -1)
510 		close(conn->c_sendfd);
511 	if (conn->c_recvfd != -1)
512 		close(conn->c_recvfd);
513 	free(conn->c_path);
514 	free(conn);
515 }
516 
517 static int
518 connection_ready(struct ggd_connection *conn)
519 {
520 
521 	return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
522 }
523 
524 static void
525 connection_launch(struct ggd_connection *conn)
526 {
527 	pthread_t td;
528 	int error, pid;
529 
530 	pid = fork();
531 	if (pid > 0)
532 		return;
533 	else if (pid == -1) {
534 		g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
535 		return;
536 	}
537 	g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
538 
539 	/*
540 	 * Create condition variables and mutexes for in-queue and out-queue
541 	 * synchronization.
542 	 */
543 	error = pthread_mutex_init(&inqueue_mtx, NULL);
544 	if (error != 0) {
545 		g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
546 		    strerror(error));
547 	}
548 	error = pthread_cond_init(&inqueue_cond, NULL);
549 	if (error != 0) {
550 		g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
551 		    strerror(error));
552 	}
553 	error = pthread_mutex_init(&outqueue_mtx, NULL);
554 	if (error != 0) {
555 		g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
556 		    strerror(error));
557 	}
558 	error = pthread_cond_init(&outqueue_cond, NULL);
559 	if (error != 0) {
560 		g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
561 		    strerror(error));
562 	}
563 
564 	/*
565 	 * Create threads:
566 	 * recvtd - thread for receiving I/O request
567 	 * diskio - thread for doing I/O request
568 	 * sendtd - thread for sending I/O requests back
569 	 */
570 	error = pthread_create(&td, NULL, send_thread, conn);
571 	if (error != 0) {
572 		g_gate_xlog("pthread_create(send_thread): %s.",
573 		    strerror(error));
574 	}
575 	error = pthread_create(&td, NULL, recv_thread, conn);
576 	if (error != 0) {
577 		g_gate_xlog("pthread_create(recv_thread): %s.",
578 		    strerror(error));
579 	}
580 	disk_thread(conn);
581 }
582 
583 static void
584 sendfail(int sfd, int error, const char *fmt, ...)
585 {
586 	struct g_gate_sinit sinit;
587 	va_list ap;
588 	ssize_t data;
589 
590 	sinit.gs_error = error;
591 	g_gate_swap2n_sinit(&sinit);
592 	data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
593 	g_gate_swap2h_sinit(&sinit);
594 	if (data != sizeof(sinit)) {
595 		g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
596 		    strerror(errno));
597 		return;
598 	}
599 	if (fmt != NULL) {
600 		va_start(ap, fmt);
601 		g_gate_vlog(LOG_WARNING, fmt, ap);
602 		va_end(ap);
603 	}
604 }
605 
606 static void *
607 malloc_waitok(size_t size)
608 {
609 	void *p;
610 
611 	while ((p = malloc(size)) == NULL) {
612 		g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
613 		sleep(1);
614 	}
615 	return (p);
616 }
617 
618 static void *
619 recv_thread(void *arg)
620 {
621 	struct ggd_connection *conn;
622 	struct ggd_request *req;
623 	ssize_t data;
624 	int error, fd;
625 
626 	conn = arg;
627 	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
628 	fd = conn->c_recvfd;
629 	for (;;) {
630 		/*
631 		 * Get header packet.
632 		 */
633 		req = malloc_waitok(sizeof(*req));
634 		data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
635 		    MSG_WAITALL);
636 		if (data == 0) {
637 			g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
638 			exit(EXIT_SUCCESS);
639 		} else if (data == -1) {
640 			g_gate_xlog("Error while receiving hdr packet: %s.",
641 			    strerror(errno));
642 		} else if (data != sizeof(req->r_hdr)) {
643 			g_gate_xlog("Malformed hdr packet received.");
644 		}
645 		g_gate_log(LOG_DEBUG, "Received hdr packet.");
646 		g_gate_swap2h_hdr(&req->r_hdr);
647 
648 		g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
649 		    (intmax_t)req->r_offset, (unsigned)req->r_length);
650 
651 		/*
652 		 * Allocate memory for data.
653 		 */
654 		req->r_data = malloc_waitok(req->r_length);
655 
656 		/*
657 		 * Receive data to write for WRITE request.
658 		 */
659 		if (req->r_cmd == GGATE_CMD_WRITE) {
660 			g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
661 			    req->r_length);
662 			data = g_gate_recv(fd, req->r_data, req->r_length,
663 			    MSG_WAITALL);
664 			if (data == -1) {
665 				g_gate_xlog("Error while receiving data: %s.",
666 				    strerror(errno));
667 			}
668 		}
669 
670 		/*
671 		 * Put the request onto the incoming queue.
672 		 */
673 		error = pthread_mutex_lock(&inqueue_mtx);
674 		assert(error == 0);
675 		TAILQ_INSERT_TAIL(&inqueue, req, r_next);
676 		error = pthread_cond_signal(&inqueue_cond);
677 		assert(error == 0);
678 		error = pthread_mutex_unlock(&inqueue_mtx);
679 		assert(error == 0);
680 	}
681 }
682 
683 static void *
684 disk_thread(void *arg)
685 {
686 	struct ggd_connection *conn;
687 	struct ggd_request *req;
688 	ssize_t data;
689 	int error, fd;
690 
691 	conn = arg;
692 	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
693 	fd = conn->c_diskfd;
694 	for (;;) {
695 		/*
696 		 * Get a request from the incoming queue.
697 		 */
698 		error = pthread_mutex_lock(&inqueue_mtx);
699 		assert(error == 0);
700 		while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
701 			error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
702 			assert(error == 0);
703 		}
704 		TAILQ_REMOVE(&inqueue, req, r_next);
705 		error = pthread_mutex_unlock(&inqueue_mtx);
706 		assert(error == 0);
707 
708 		/*
709 		 * Check the request.
710 		 */
711 		assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
712 		assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
713 		assert((req->r_offset % conn->c_sectorsize) == 0);
714 		assert((req->r_length % conn->c_sectorsize) == 0);
715 
716 		g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
717 		    (intmax_t)req->r_offset, (unsigned)req->r_length);
718 
719 		/*
720 		 * Do the request.
721 		 */
722 		data = 0;
723 		switch (req->r_cmd) {
724 		case GGATE_CMD_READ:
725 			data = pread(fd, req->r_data, req->r_length,
726 			    req->r_offset);
727 			break;
728 		case GGATE_CMD_WRITE:
729 			data = pwrite(fd, req->r_data, req->r_length,
730 			    req->r_offset);
731 			/* Free data memory here - better sooner. */
732 			free(req->r_data);
733 			req->r_data = NULL;
734 			break;
735 		}
736 		if (data != (ssize_t)req->r_length) {
737 			/* Report short reads/writes as I/O errors. */
738 			if (errno == 0)
739 				errno = EIO;
740 			g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
741 			req->r_error = errno;
742 			if (req->r_data != NULL) {
743 				free(req->r_data);
744 				req->r_data = NULL;
745 			}
746 		}
747 
748 		/*
749 		 * Put the request onto the outgoing queue.
750 		 */
751 		error = pthread_mutex_lock(&outqueue_mtx);
752 		assert(error == 0);
753 		TAILQ_INSERT_TAIL(&outqueue, req, r_next);
754 		error = pthread_cond_signal(&outqueue_cond);
755 		assert(error == 0);
756 		error = pthread_mutex_unlock(&outqueue_mtx);
757 		assert(error == 0);
758 	}
759 }
760 
761 static void *
762 send_thread(void *arg)
763 {
764 	struct ggd_connection *conn;
765 	struct ggd_request *req;
766 	ssize_t data;
767 	int error, fd;
768 
769 	conn = arg;
770 	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
771 	fd = conn->c_sendfd;
772 	for (;;) {
773 		/*
774 		 * Get a request from the outgoing queue.
775 		 */
776 		error = pthread_mutex_lock(&outqueue_mtx);
777 		assert(error == 0);
778 		while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
779 			error = pthread_cond_wait(&outqueue_cond,
780 			    &outqueue_mtx);
781 			assert(error == 0);
782 		}
783 		TAILQ_REMOVE(&outqueue, req, r_next);
784 		error = pthread_mutex_unlock(&outqueue_mtx);
785 		assert(error == 0);
786 
787 		g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
788 		    (intmax_t)req->r_offset, (unsigned)req->r_length);
789 
790 		/*
791 		 * Send the request.
792 		 */
793 		g_gate_swap2n_hdr(&req->r_hdr);
794 		if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
795 			g_gate_xlog("Error while sending hdr packet: %s.",
796 			    strerror(errno));
797 		}
798 		g_gate_log(LOG_DEBUG, "Sent hdr packet.");
799 		g_gate_swap2h_hdr(&req->r_hdr);
800 		if (req->r_data != NULL) {
801 			data = g_gate_send(fd, req->r_data, req->r_length, 0);
802 			if (data != (ssize_t)req->r_length) {
803 				g_gate_xlog("Error while sending data: %s.",
804 				    strerror(errno));
805 			}
806 			g_gate_log(LOG_DEBUG,
807 			    "Sent %zd bytes (offset=%ju, size=%zu).", data,
808 			    (uintmax_t)req->r_offset, (size_t)req->r_length);
809 			free(req->r_data);
810 		}
811 		free(req);
812 	}
813 }
814 
815 static void
816 log_connection(struct sockaddr *from)
817 {
818 	in_addr_t ip;
819 
820 	ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
821 	g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
822 }
823 
824 static int
825 handshake(struct sockaddr *from, int sfd)
826 {
827 	struct g_gate_version ver;
828 	struct g_gate_cinit cinit;
829 	struct g_gate_sinit sinit;
830 	struct ggd_connection *conn;
831 	struct ggd_export *ex;
832 	ssize_t data;
833 
834 	log_connection(from);
835 	/*
836 	 * Phase 1: Version verification.
837 	 */
838 	g_gate_log(LOG_DEBUG, "Receiving version packet.");
839 	data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
840 	g_gate_swap2h_version(&ver);
841 	if (data != sizeof(ver)) {
842 		g_gate_log(LOG_WARNING, "Malformed version packet.");
843 		return (0);
844 	}
845 	g_gate_log(LOG_DEBUG, "Version packet received.");
846 	if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
847 		g_gate_log(LOG_WARNING, "Invalid magic field.");
848 		return (0);
849 	}
850 	if (ver.gv_version != GGATE_VERSION) {
851 		g_gate_log(LOG_WARNING, "Version %u is not supported.",
852 		    ver.gv_version);
853 		return (0);
854 	}
855 	ver.gv_error = 0;
856 	g_gate_swap2n_version(&ver);
857 	data = g_gate_send(sfd, &ver, sizeof(ver), 0);
858 	g_gate_swap2h_version(&ver);
859 	if (data == -1) {
860 		sendfail(sfd, errno, "Error while sending version packet: %s.",
861 		    strerror(errno));
862 		return (0);
863 	}
864 
865 	/*
866 	 * Phase 2: Request verification.
867 	 */
868 	g_gate_log(LOG_DEBUG, "Receiving initial packet.");
869 	data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
870 	g_gate_swap2h_cinit(&cinit);
871 	if (data != sizeof(cinit)) {
872 		g_gate_log(LOG_WARNING, "Malformed initial packet.");
873 		return (0);
874 	}
875 	g_gate_log(LOG_DEBUG, "Initial packet received.");
876 	conn = connection_find(&cinit);
877 	if (conn != NULL) {
878 		/*
879 		 * Connection should already exists.
880 		 */
881 		g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
882 		    (unsigned long)conn->c_token);
883 		if (connection_add(conn, &cinit, from, sfd) == -1) {
884 			connection_remove(conn);
885 			return (0);
886 		}
887 	} else {
888 		/*
889 		 * New connection, allocate space.
890 		 */
891 		conn = connection_new(&cinit, from, sfd);
892 		if (conn == NULL) {
893 			sendfail(sfd, ENOMEM,
894 			    "Cannot allocate new connection.");
895 			return (0);
896 		}
897 		g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
898 		    (unsigned long)conn->c_token);
899 	}
900 
901 	ex = exports_find(from, &cinit, conn);
902 	if (ex == NULL) {
903 		connection_remove(conn);
904 		sendfail(sfd, errno, NULL);
905 		return (0);
906 	}
907 	if (conn->c_mediasize == 0) {
908 		conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
909 		conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
910 	}
911 	sinit.gs_mediasize = conn->c_mediasize;
912 	sinit.gs_sectorsize = conn->c_sectorsize;
913 	sinit.gs_error = 0;
914 
915 	g_gate_log(LOG_DEBUG, "Sending initial packet.");
916 
917 	g_gate_swap2n_sinit(&sinit);
918 	data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
919 	g_gate_swap2h_sinit(&sinit);
920 	if (data == -1) {
921 		sendfail(sfd, errno, "Error while sending initial packet: %s.",
922 		    strerror(errno));
923 		return (0);
924 	}
925 
926 	if (connection_ready(conn)) {
927 		connection_launch(conn);
928 		connection_remove(conn);
929 	}
930 	return (1);
931 }
932 
933 static void
934 huphandler(int sig __unused)
935 {
936 
937 	got_sighup = 1;
938 }
939 
940 int
941 main(int argc, char *argv[])
942 {
943 	struct sockaddr_in serv;
944 	struct sockaddr from;
945 	socklen_t fromlen;
946 	int sfd, tmpsfd;
947 	unsigned port;
948 
949 	bindaddr = htonl(INADDR_ANY);
950 	port = G_GATE_PORT;
951 	for (;;) {
952 		int ch;
953 
954 		ch = getopt(argc, argv, "a:hnp:R:S:v");
955 		if (ch == -1)
956 			break;
957 		switch (ch) {
958 		case 'a':
959 			bindaddr = g_gate_str2ip(optarg);
960 			if (bindaddr == INADDR_NONE) {
961 				errx(EXIT_FAILURE,
962 				    "Invalid IP/host name to bind to.");
963 			}
964 			break;
965 		case 'n':
966 			nagle = 0;
967 			break;
968 		case 'p':
969 			errno = 0;
970 			port = strtoul(optarg, NULL, 10);
971 			if (port == 0 && errno != 0)
972 				errx(EXIT_FAILURE, "Invalid port.");
973 			break;
974 		case 'R':
975 			errno = 0;
976 			rcvbuf = strtoul(optarg, NULL, 10);
977 			if (rcvbuf == 0 && errno != 0)
978 				errx(EXIT_FAILURE, "Invalid rcvbuf.");
979 			break;
980 		case 'S':
981 			errno = 0;
982 			sndbuf = strtoul(optarg, NULL, 10);
983 			if (sndbuf == 0 && errno != 0)
984 				errx(EXIT_FAILURE, "Invalid sndbuf.");
985 			break;
986 		case 'v':
987 			g_gate_verbose++;
988 			break;
989 		case 'h':
990 		default:
991 			usage();
992 		}
993 	}
994 	argc -= optind;
995 	argv += optind;
996 
997 	if (argv[0] != NULL)
998 		exports_file = argv[0];
999 	exports_get();
1000 
1001 	if (!g_gate_verbose) {
1002 		/* Run in daemon mode. */
1003 		if (daemon(0, 0) == -1)
1004 			g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1005 	}
1006 
1007 	signal(SIGCHLD, SIG_IGN);
1008 
1009 	sfd = socket(AF_INET, SOCK_STREAM, 0);
1010 	if (sfd == -1)
1011 		g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1012 	bzero(&serv, sizeof(serv));
1013 	serv.sin_family = AF_INET;
1014 	serv.sin_addr.s_addr = bindaddr;
1015 	serv.sin_port = htons(port);
1016 
1017 	g_gate_socket_settings(sfd);
1018 
1019 	if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1020 		g_gate_xlog("bind(): %s.", strerror(errno));
1021 	if (listen(sfd, 5) == -1)
1022 		g_gate_xlog("listen(): %s.", strerror(errno));
1023 
1024 	g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1025 
1026 	signal(SIGHUP, huphandler);
1027 
1028 	for (;;) {
1029 		fromlen = sizeof(from);
1030 		tmpsfd = accept(sfd, &from, &fromlen);
1031 		if (tmpsfd == -1)
1032 			g_gate_xlog("accept(): %s.", strerror(errno));
1033 
1034 		if (got_sighup) {
1035 			got_sighup = 0;
1036 			exports_get();
1037 		}
1038 
1039 		if (!handshake(&from, tmpsfd))
1040 			close(tmpsfd);
1041 	}
1042 	close(sfd);
1043 	exit(EXIT_SUCCESS);
1044 }
1045