xref: /freebsd/usr.sbin/bluetooth/btpand/btpand.c (revision fbfdf57d65bedfab28f9debc8a4a8d6802f9338a)
17718ced0SMaksim Yevmenkin /*	$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
27718ced0SMaksim Yevmenkin 
37718ced0SMaksim Yevmenkin /*-
4b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
67718ced0SMaksim Yevmenkin  * Copyright (c) 2008 Iain Hibbert
77718ced0SMaksim Yevmenkin  * All rights reserved.
87718ced0SMaksim Yevmenkin  *
97718ced0SMaksim Yevmenkin  * Redistribution and use in source and binary forms, with or without
107718ced0SMaksim Yevmenkin  * modification, are permitted provided that the following conditions
117718ced0SMaksim Yevmenkin  * are met:
127718ced0SMaksim Yevmenkin  * 1. Redistributions of source code must retain the above copyright
137718ced0SMaksim Yevmenkin  *    notice, this list of conditions and the following disclaimer.
147718ced0SMaksim Yevmenkin  * 2. Redistributions in binary form must reproduce the above copyright
157718ced0SMaksim Yevmenkin  *    notice, this list of conditions and the following disclaimer in the
167718ced0SMaksim Yevmenkin  *    documentation and/or other materials provided with the distribution.
177718ced0SMaksim Yevmenkin  *
187718ced0SMaksim Yevmenkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
197718ced0SMaksim Yevmenkin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
207718ced0SMaksim Yevmenkin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
217718ced0SMaksim Yevmenkin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
227718ced0SMaksim Yevmenkin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237718ced0SMaksim Yevmenkin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247718ced0SMaksim Yevmenkin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257718ced0SMaksim Yevmenkin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267718ced0SMaksim Yevmenkin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
277718ced0SMaksim Yevmenkin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287718ced0SMaksim Yevmenkin  */
297718ced0SMaksim Yevmenkin 
307718ced0SMaksim Yevmenkin 
317718ced0SMaksim Yevmenkin #include <sys/wait.h>
327718ced0SMaksim Yevmenkin 
338d6f425dSTakanori Watanabe #define L2CAP_SOCKET_CHECKED
347718ced0SMaksim Yevmenkin #include <bluetooth.h>
357718ced0SMaksim Yevmenkin #include <err.h>
367718ced0SMaksim Yevmenkin #include <fcntl.h>
377718ced0SMaksim Yevmenkin #include <paths.h>
387718ced0SMaksim Yevmenkin #include <sdp.h>
397718ced0SMaksim Yevmenkin #include <stdio.h>
407718ced0SMaksim Yevmenkin #include <signal.h>
417718ced0SMaksim Yevmenkin #include <stdlib.h>
427718ced0SMaksim Yevmenkin #include <string.h>
437718ced0SMaksim Yevmenkin #include <unistd.h>
447718ced0SMaksim Yevmenkin 
457718ced0SMaksim Yevmenkin #include "btpand.h"
467718ced0SMaksim Yevmenkin 
477718ced0SMaksim Yevmenkin /* global variables */
487718ced0SMaksim Yevmenkin const char *	control_path;		/* -c <path> */
497718ced0SMaksim Yevmenkin const char *	interface_name;		/* -i <ifname> */
507718ced0SMaksim Yevmenkin const char *	service_name;		/* -s <service> */
517718ced0SMaksim Yevmenkin uint16_t	service_class;
527718ced0SMaksim Yevmenkin 
537718ced0SMaksim Yevmenkin bdaddr_t	local_bdaddr;		/* -d <addr> */
547718ced0SMaksim Yevmenkin bdaddr_t	remote_bdaddr;		/* -a <addr> */
55905033dcSMaksim Yevmenkin uint16_t	l2cap_psm;		/* -p <psm> */
56905033dcSMaksim Yevmenkin int		l2cap_mode;		/* -m <mode> */
577718ced0SMaksim Yevmenkin 
587718ced0SMaksim Yevmenkin int		server_limit;		/* -n <limit> */
597718ced0SMaksim Yevmenkin 
607718ced0SMaksim Yevmenkin static const struct {
617718ced0SMaksim Yevmenkin 	const char *	name;
627718ced0SMaksim Yevmenkin 	uint16_t	class;
637718ced0SMaksim Yevmenkin 	const char *	desc;
647718ced0SMaksim Yevmenkin } services[] = {
657718ced0SMaksim Yevmenkin 	{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
66beb45c4fSPedro F. Giffuni 	{ "NAP",  SDP_SERVICE_CLASS_NAP,  "Network Access Point"		  },
677718ced0SMaksim Yevmenkin 	{ "GN",	  SDP_SERVICE_CLASS_GN,   "Group Network"		  },
687718ced0SMaksim Yevmenkin };
697718ced0SMaksim Yevmenkin 
7072e1ea2fSAlfonso Gregory static void main_exit(int) __dead2;
717718ced0SMaksim Yevmenkin static void main_detach(void);
7272e1ea2fSAlfonso Gregory static void usage(void) __dead2;
737718ced0SMaksim Yevmenkin 
747718ced0SMaksim Yevmenkin int
757718ced0SMaksim Yevmenkin main(int argc, char *argv[])
767718ced0SMaksim Yevmenkin {
777718ced0SMaksim Yevmenkin 	unsigned long	ul;
787718ced0SMaksim Yevmenkin 	char *		ep;
797718ced0SMaksim Yevmenkin 	int		ch, status;
807718ced0SMaksim Yevmenkin 
817718ced0SMaksim Yevmenkin 	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
827718ced0SMaksim Yevmenkin 		switch (ch) {
837718ced0SMaksim Yevmenkin 		case 'a': /* remote address */
847718ced0SMaksim Yevmenkin 			if (!bt_aton(optarg, &remote_bdaddr)) {
857718ced0SMaksim Yevmenkin 				struct hostent  *he;
867718ced0SMaksim Yevmenkin 
877718ced0SMaksim Yevmenkin 				if ((he = bt_gethostbyname(optarg)) == NULL)
887718ced0SMaksim Yevmenkin 					errx(EXIT_FAILURE, "%s: %s",
897718ced0SMaksim Yevmenkin 					    optarg, hstrerror(h_errno));
907718ced0SMaksim Yevmenkin 
917718ced0SMaksim Yevmenkin 				bdaddr_copy(&remote_bdaddr,
927718ced0SMaksim Yevmenkin 					(bdaddr_t *)he->h_addr);
937718ced0SMaksim Yevmenkin 			}
947718ced0SMaksim Yevmenkin 
957718ced0SMaksim Yevmenkin 			break;
967718ced0SMaksim Yevmenkin 
977718ced0SMaksim Yevmenkin 		case 'c': /* control socket path */
987718ced0SMaksim Yevmenkin 			control_path = optarg;
997718ced0SMaksim Yevmenkin 			break;
1007718ced0SMaksim Yevmenkin 
1017718ced0SMaksim Yevmenkin 		case 'd': /* local address */
10271523863SMaksim Yevmenkin 			if (!bt_devaddr(optarg, &local_bdaddr)) {
1037718ced0SMaksim Yevmenkin 				struct hostent  *he;
1047718ced0SMaksim Yevmenkin 
1057718ced0SMaksim Yevmenkin 				if ((he = bt_gethostbyname(optarg)) == NULL)
1067718ced0SMaksim Yevmenkin 					errx(EXIT_FAILURE, "%s: %s",
1077718ced0SMaksim Yevmenkin 					    optarg, hstrerror(h_errno));
1087718ced0SMaksim Yevmenkin 
1097718ced0SMaksim Yevmenkin 				bdaddr_copy(&local_bdaddr,
1107718ced0SMaksim Yevmenkin 					(bdaddr_t *)he->h_addr);
1117718ced0SMaksim Yevmenkin 			}
1127718ced0SMaksim Yevmenkin 			break;
1137718ced0SMaksim Yevmenkin 
1147718ced0SMaksim Yevmenkin 		case 'i': /* tap interface name */
1157718ced0SMaksim Yevmenkin 			if (strchr(optarg, '/') == NULL) {
1167718ced0SMaksim Yevmenkin 				asprintf(&ep, "/dev/%s", optarg);
1177718ced0SMaksim Yevmenkin 				interface_name = ep;
1187718ced0SMaksim Yevmenkin 			} else
1197718ced0SMaksim Yevmenkin 				interface_name = optarg;
1207718ced0SMaksim Yevmenkin 			break;
1217718ced0SMaksim Yevmenkin 
1227718ced0SMaksim Yevmenkin 		case 'l': /* limit server sessions */
1237718ced0SMaksim Yevmenkin 			ul = strtoul(optarg, &ep, 10);
1247718ced0SMaksim Yevmenkin 			if (*optarg == '\0' || *ep != '\0' || ul == 0)
1257718ced0SMaksim Yevmenkin 				errx(EXIT_FAILURE, "%s: invalid session limit",
1267718ced0SMaksim Yevmenkin 					optarg);
1277718ced0SMaksim Yevmenkin 
1287718ced0SMaksim Yevmenkin 			server_limit = ul;
1297718ced0SMaksim Yevmenkin 			break;
1307718ced0SMaksim Yevmenkin 
1317718ced0SMaksim Yevmenkin 		case 'm': /* link mode */
1327718ced0SMaksim Yevmenkin 			warnx("Setting link mode is not yet supported");
1337718ced0SMaksim Yevmenkin 			break;
1347718ced0SMaksim Yevmenkin 
1357718ced0SMaksim Yevmenkin 		case 'p': /* protocol/service multiplexer */
1367718ced0SMaksim Yevmenkin 			ul = strtoul(optarg, &ep, 0);
1377718ced0SMaksim Yevmenkin 			if (*optarg == '\0' || *ep != '\0'
1387718ced0SMaksim Yevmenkin 			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
1397718ced0SMaksim Yevmenkin 				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
1407718ced0SMaksim Yevmenkin 
1417718ced0SMaksim Yevmenkin 			l2cap_psm = ul;
1427718ced0SMaksim Yevmenkin 			break;
1437718ced0SMaksim Yevmenkin 
1447718ced0SMaksim Yevmenkin 		case 's': /* service */
1457718ced0SMaksim Yevmenkin 		case 'S': /* service (no SDP) */
146*fbfdf57dSDapeng Gao 			for (ul = 0; ul < __arraycount(services); ul++) {
147*fbfdf57dSDapeng Gao 				if (strcasecmp(optarg, services[ul].name) == 0)
148*fbfdf57dSDapeng Gao 					break;
149*fbfdf57dSDapeng Gao 			}
150*fbfdf57dSDapeng Gao 
1517718ced0SMaksim Yevmenkin 			if (ul == __arraycount(services))
1527718ced0SMaksim Yevmenkin 				errx(EXIT_FAILURE, "%s: unknown service", optarg);
1537718ced0SMaksim Yevmenkin 
1547718ced0SMaksim Yevmenkin 			if (ch == 's')
1557718ced0SMaksim Yevmenkin 				service_name = services[ul].name;
1567718ced0SMaksim Yevmenkin 
1577718ced0SMaksim Yevmenkin 			service_class = services[ul].class;
1587718ced0SMaksim Yevmenkin 			break;
1597718ced0SMaksim Yevmenkin 
1607718ced0SMaksim Yevmenkin 		default:
1617718ced0SMaksim Yevmenkin 			usage();
1627718ced0SMaksim Yevmenkin 			/* NOTREACHED */
1637718ced0SMaksim Yevmenkin 		}
1647718ced0SMaksim Yevmenkin 	}
1657718ced0SMaksim Yevmenkin 
1667718ced0SMaksim Yevmenkin 	argc -= optind;
1677718ced0SMaksim Yevmenkin 	argv += optind;
1687718ced0SMaksim Yevmenkin 
1697718ced0SMaksim Yevmenkin 	/* validate options */
1707718ced0SMaksim Yevmenkin 	if (bdaddr_any(&local_bdaddr) || service_class == 0)
1717718ced0SMaksim Yevmenkin 		usage();
1727718ced0SMaksim Yevmenkin 
1737718ced0SMaksim Yevmenkin 	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
174d134fb49SMarcelo Araujo 	    control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
1757718ced0SMaksim Yevmenkin 		usage();
1767718ced0SMaksim Yevmenkin 
1777718ced0SMaksim Yevmenkin 	/* default options */
1787718ced0SMaksim Yevmenkin 	if (interface_name == NULL)
1797718ced0SMaksim Yevmenkin 		interface_name = "/dev/tap";
1807718ced0SMaksim Yevmenkin 
181905033dcSMaksim Yevmenkin 	if (l2cap_psm == 0)
182905033dcSMaksim Yevmenkin 		l2cap_psm = L2CAP_PSM_BNEP;
183905033dcSMaksim Yevmenkin 
1847718ced0SMaksim Yevmenkin 	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
1857718ced0SMaksim Yevmenkin 		if (service_class == SDP_SERVICE_CLASS_PANU)
1867718ced0SMaksim Yevmenkin 			server_limit = 1;
1877718ced0SMaksim Yevmenkin 		else
1887718ced0SMaksim Yevmenkin 			server_limit = 7;
1897718ced0SMaksim Yevmenkin 	}
1907718ced0SMaksim Yevmenkin 
1917718ced0SMaksim Yevmenkin #ifdef L2CAP_LM_MASTER
1927718ced0SMaksim Yevmenkin 	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
1937718ced0SMaksim Yevmenkin 		l2cap_mode |= L2CAP_LM_MASTER;
1947718ced0SMaksim Yevmenkin #endif
1957718ced0SMaksim Yevmenkin 
1967718ced0SMaksim Yevmenkin 	/*
1977718ced0SMaksim Yevmenkin 	 * fork() now so that the setup can be done in the child process
1987718ced0SMaksim Yevmenkin 	 * (as kqueue is not inherited) but block in the parent until the
1997718ced0SMaksim Yevmenkin 	 * setup is finished so we can return an error if necessary.
2007718ced0SMaksim Yevmenkin 	 */
2017718ced0SMaksim Yevmenkin 	switch(fork()) {
2027718ced0SMaksim Yevmenkin 	case -1: /* bad */
2037718ced0SMaksim Yevmenkin 		err(EXIT_FAILURE, "fork() failed");
2047718ced0SMaksim Yevmenkin 
2057718ced0SMaksim Yevmenkin 	case 0:	/* child */
2067718ced0SMaksim Yevmenkin 		signal(SIGPIPE, SIG_IGN);
2077718ced0SMaksim Yevmenkin 
2087718ced0SMaksim Yevmenkin 		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
2097718ced0SMaksim Yevmenkin 
2107718ced0SMaksim Yevmenkin 		channel_init();
2117718ced0SMaksim Yevmenkin 		server_init();
2127718ced0SMaksim Yevmenkin 		event_init();
2137718ced0SMaksim Yevmenkin 		client_init();
2147718ced0SMaksim Yevmenkin 		tap_init();
2157718ced0SMaksim Yevmenkin 
2167718ced0SMaksim Yevmenkin 		main_detach();
2177718ced0SMaksim Yevmenkin 
2187718ced0SMaksim Yevmenkin 		event_dispatch();
2197718ced0SMaksim Yevmenkin 		break;
2207718ced0SMaksim Yevmenkin 
2217718ced0SMaksim Yevmenkin 	default: /* parent */
2227718ced0SMaksim Yevmenkin 		signal(SIGUSR1, main_exit);
2237718ced0SMaksim Yevmenkin 		wait(&status);
2247718ced0SMaksim Yevmenkin 
2257718ced0SMaksim Yevmenkin 		if (WIFEXITED(status))
2267718ced0SMaksim Yevmenkin 			exit(WEXITSTATUS(status));
2277718ced0SMaksim Yevmenkin 
2287718ced0SMaksim Yevmenkin 		break;
2297718ced0SMaksim Yevmenkin 	}
2307718ced0SMaksim Yevmenkin 
2317718ced0SMaksim Yevmenkin 	err(EXIT_FAILURE, "exiting");
2327718ced0SMaksim Yevmenkin }
2337718ced0SMaksim Yevmenkin 
2347718ced0SMaksim Yevmenkin static void
2357718ced0SMaksim Yevmenkin main_exit(int s)
2367718ced0SMaksim Yevmenkin {
2377718ced0SMaksim Yevmenkin 
2387718ced0SMaksim Yevmenkin 	/* child is all grown up */
2397718ced0SMaksim Yevmenkin 	_exit(EXIT_SUCCESS);
2407718ced0SMaksim Yevmenkin }
2417718ced0SMaksim Yevmenkin 
2427718ced0SMaksim Yevmenkin static void
2437718ced0SMaksim Yevmenkin main_detach(void)
2447718ced0SMaksim Yevmenkin {
2457718ced0SMaksim Yevmenkin 	int fd;
2467718ced0SMaksim Yevmenkin 
2477718ced0SMaksim Yevmenkin 	if (kill(getppid(), SIGUSR1) == -1)
2487718ced0SMaksim Yevmenkin 		log_err("Could not signal main process: %m");
2497718ced0SMaksim Yevmenkin 
2507718ced0SMaksim Yevmenkin 	if (setsid() == -1)
2517718ced0SMaksim Yevmenkin 		log_err("setsid() failed");
2527718ced0SMaksim Yevmenkin 
2537718ced0SMaksim Yevmenkin 	fd = open(_PATH_DEVNULL, O_RDWR, 0);
2547718ced0SMaksim Yevmenkin 	if (fd == -1) {
2557718ced0SMaksim Yevmenkin 		log_err("Could not open %s", _PATH_DEVNULL);
2567718ced0SMaksim Yevmenkin 	} else {
2577718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDIN_FILENO);
2587718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDOUT_FILENO);
2597718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDERR_FILENO);
2607718ced0SMaksim Yevmenkin 		close(fd);
2617718ced0SMaksim Yevmenkin 	}
2627718ced0SMaksim Yevmenkin }
2637718ced0SMaksim Yevmenkin 
2647718ced0SMaksim Yevmenkin static void
2657718ced0SMaksim Yevmenkin usage(void)
2667718ced0SMaksim Yevmenkin {
2677718ced0SMaksim Yevmenkin 	const char *p = getprogname();
2687718ced0SMaksim Yevmenkin 	int n = strlen(p);
2697718ced0SMaksim Yevmenkin 
2707718ced0SMaksim Yevmenkin 	fprintf(stderr,
2717718ced0SMaksim Yevmenkin 	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
2727718ced0SMaksim Yevmenkin 	    "       %*s {-s service | -S service [-p psm]}\n"
2737718ced0SMaksim Yevmenkin 	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
2747718ced0SMaksim Yevmenkin 	    "       %*s {-s service | -S service}\n"
2757718ced0SMaksim Yevmenkin 	    "\n"
2767718ced0SMaksim Yevmenkin 	    "Where:\n"
2777718ced0SMaksim Yevmenkin 	    "\t-a address  remote bluetooth device\n"
2787718ced0SMaksim Yevmenkin 	    "\t-c path     SDP server socket\n"
2797718ced0SMaksim Yevmenkin 	    "\t-d device   local bluetooth device\n"
2807718ced0SMaksim Yevmenkin 	    "\t-i ifname   tap interface\n"
2817718ced0SMaksim Yevmenkin 	    "\t-l limit    limit server sessions\n"
2827718ced0SMaksim Yevmenkin 	    "\t-m mode     L2CAP link mode (NOT YET SUPPORTED)\n"
2837718ced0SMaksim Yevmenkin 	    "\t-p psm      L2CAP PSM\n"
2847718ced0SMaksim Yevmenkin 	    "\t-S service  service name (no SDP)\n"
2857718ced0SMaksim Yevmenkin 	    "\t-s service  service name\n"
2867718ced0SMaksim Yevmenkin 	    "\n"
2877718ced0SMaksim Yevmenkin 	    "Known services:\n"
2887718ced0SMaksim Yevmenkin 	    "", p, n, "", p, n, "");
2897718ced0SMaksim Yevmenkin 
2907718ced0SMaksim Yevmenkin 	for (n = 0; n < __arraycount(services); n++)
2917718ced0SMaksim Yevmenkin 		fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
2927718ced0SMaksim Yevmenkin 
2937718ced0SMaksim Yevmenkin 	exit(EXIT_FAILURE);
2947718ced0SMaksim Yevmenkin }
295