xref: /freebsd/usr.sbin/bluetooth/btpand/btpand.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
17718ced0SMaksim Yevmenkin /*	$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
27718ced0SMaksim Yevmenkin 
37718ced0SMaksim Yevmenkin /*-
4*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5*1de7b4b8SPedro 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 /* $FreeBSD$ */
317718ced0SMaksim Yevmenkin 
327718ced0SMaksim Yevmenkin #include <sys/cdefs.h>
337718ced0SMaksim Yevmenkin __COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
347718ced0SMaksim Yevmenkin __RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
357718ced0SMaksim Yevmenkin 
367718ced0SMaksim Yevmenkin #include <sys/wait.h>
377718ced0SMaksim Yevmenkin 
388d6f425dSTakanori Watanabe #define L2CAP_SOCKET_CHECKED
397718ced0SMaksim Yevmenkin #include <bluetooth.h>
407718ced0SMaksim Yevmenkin #include <err.h>
417718ced0SMaksim Yevmenkin #include <fcntl.h>
427718ced0SMaksim Yevmenkin #include <paths.h>
437718ced0SMaksim Yevmenkin #include <sdp.h>
447718ced0SMaksim Yevmenkin #include <stdio.h>
457718ced0SMaksim Yevmenkin #include <signal.h>
467718ced0SMaksim Yevmenkin #include <stdlib.h>
477718ced0SMaksim Yevmenkin #include <string.h>
487718ced0SMaksim Yevmenkin #include <unistd.h>
497718ced0SMaksim Yevmenkin 
507718ced0SMaksim Yevmenkin #include "btpand.h"
517718ced0SMaksim Yevmenkin 
527718ced0SMaksim Yevmenkin /* global variables */
537718ced0SMaksim Yevmenkin const char *	control_path;		/* -c <path> */
547718ced0SMaksim Yevmenkin const char *	interface_name;		/* -i <ifname> */
557718ced0SMaksim Yevmenkin const char *	service_name;		/* -s <service> */
567718ced0SMaksim Yevmenkin uint16_t	service_class;
577718ced0SMaksim Yevmenkin 
587718ced0SMaksim Yevmenkin bdaddr_t	local_bdaddr;		/* -d <addr> */
597718ced0SMaksim Yevmenkin bdaddr_t	remote_bdaddr;		/* -a <addr> */
60905033dcSMaksim Yevmenkin uint16_t	l2cap_psm;		/* -p <psm> */
61905033dcSMaksim Yevmenkin int		l2cap_mode;		/* -m <mode> */
627718ced0SMaksim Yevmenkin 
637718ced0SMaksim Yevmenkin int		server_limit;		/* -n <limit> */
647718ced0SMaksim Yevmenkin 
657718ced0SMaksim Yevmenkin static const struct {
667718ced0SMaksim Yevmenkin 	const char *	name;
677718ced0SMaksim Yevmenkin 	uint16_t	class;
687718ced0SMaksim Yevmenkin 	const char *	desc;
697718ced0SMaksim Yevmenkin } services[] = {
707718ced0SMaksim Yevmenkin 	{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
71beb45c4fSPedro F. Giffuni 	{ "NAP",  SDP_SERVICE_CLASS_NAP,  "Network Access Point"		  },
727718ced0SMaksim Yevmenkin 	{ "GN",	  SDP_SERVICE_CLASS_GN,   "Group Network"		  },
737718ced0SMaksim Yevmenkin };
747718ced0SMaksim Yevmenkin 
757718ced0SMaksim Yevmenkin static void main_exit(int);
767718ced0SMaksim Yevmenkin static void main_detach(void);
777718ced0SMaksim Yevmenkin static void usage(void);
787718ced0SMaksim Yevmenkin 
797718ced0SMaksim Yevmenkin int
807718ced0SMaksim Yevmenkin main(int argc, char *argv[])
817718ced0SMaksim Yevmenkin {
827718ced0SMaksim Yevmenkin 	unsigned long	ul;
837718ced0SMaksim Yevmenkin 	char *		ep;
847718ced0SMaksim Yevmenkin 	int		ch, status;
857718ced0SMaksim Yevmenkin 
867718ced0SMaksim Yevmenkin 	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
877718ced0SMaksim Yevmenkin 		switch (ch) {
887718ced0SMaksim Yevmenkin 		case 'a': /* remote address */
897718ced0SMaksim Yevmenkin 			if (!bt_aton(optarg, &remote_bdaddr)) {
907718ced0SMaksim Yevmenkin 				struct hostent  *he;
917718ced0SMaksim Yevmenkin 
927718ced0SMaksim Yevmenkin 				if ((he = bt_gethostbyname(optarg)) == NULL)
937718ced0SMaksim Yevmenkin 					errx(EXIT_FAILURE, "%s: %s",
947718ced0SMaksim Yevmenkin 					    optarg, hstrerror(h_errno));
957718ced0SMaksim Yevmenkin 
967718ced0SMaksim Yevmenkin 				bdaddr_copy(&remote_bdaddr,
977718ced0SMaksim Yevmenkin 					(bdaddr_t *)he->h_addr);
987718ced0SMaksim Yevmenkin 			}
997718ced0SMaksim Yevmenkin 
1007718ced0SMaksim Yevmenkin 			break;
1017718ced0SMaksim Yevmenkin 
1027718ced0SMaksim Yevmenkin 		case 'c': /* control socket path */
1037718ced0SMaksim Yevmenkin 			control_path = optarg;
1047718ced0SMaksim Yevmenkin 			break;
1057718ced0SMaksim Yevmenkin 
1067718ced0SMaksim Yevmenkin 		case 'd': /* local address */
10771523863SMaksim Yevmenkin 			if (!bt_devaddr(optarg, &local_bdaddr)) {
1087718ced0SMaksim Yevmenkin 				struct hostent  *he;
1097718ced0SMaksim Yevmenkin 
1107718ced0SMaksim Yevmenkin 				if ((he = bt_gethostbyname(optarg)) == NULL)
1117718ced0SMaksim Yevmenkin 					errx(EXIT_FAILURE, "%s: %s",
1127718ced0SMaksim Yevmenkin 					    optarg, hstrerror(h_errno));
1137718ced0SMaksim Yevmenkin 
1147718ced0SMaksim Yevmenkin 				bdaddr_copy(&local_bdaddr,
1157718ced0SMaksim Yevmenkin 					(bdaddr_t *)he->h_addr);
1167718ced0SMaksim Yevmenkin 			}
1177718ced0SMaksim Yevmenkin 			break;
1187718ced0SMaksim Yevmenkin 
1197718ced0SMaksim Yevmenkin 		case 'i': /* tap interface name */
1207718ced0SMaksim Yevmenkin 			if (strchr(optarg, '/') == NULL) {
1217718ced0SMaksim Yevmenkin 				asprintf(&ep, "/dev/%s", optarg);
1227718ced0SMaksim Yevmenkin 				interface_name = ep;
1237718ced0SMaksim Yevmenkin 			} else
1247718ced0SMaksim Yevmenkin 				interface_name = optarg;
1257718ced0SMaksim Yevmenkin 			break;
1267718ced0SMaksim Yevmenkin 
1277718ced0SMaksim Yevmenkin 		case 'l': /* limit server sessions */
1287718ced0SMaksim Yevmenkin 			ul = strtoul(optarg, &ep, 10);
1297718ced0SMaksim Yevmenkin 			if (*optarg == '\0' || *ep != '\0' || ul == 0)
1307718ced0SMaksim Yevmenkin 				errx(EXIT_FAILURE, "%s: invalid session limit",
1317718ced0SMaksim Yevmenkin 					optarg);
1327718ced0SMaksim Yevmenkin 
1337718ced0SMaksim Yevmenkin 			server_limit = ul;
1347718ced0SMaksim Yevmenkin 			break;
1357718ced0SMaksim Yevmenkin 
1367718ced0SMaksim Yevmenkin 		case 'm': /* link mode */
1377718ced0SMaksim Yevmenkin 			warnx("Setting link mode is not yet supported");
1387718ced0SMaksim Yevmenkin 			break;
1397718ced0SMaksim Yevmenkin 
1407718ced0SMaksim Yevmenkin 		case 'p': /* protocol/service multiplexer */
1417718ced0SMaksim Yevmenkin 			ul = strtoul(optarg, &ep, 0);
1427718ced0SMaksim Yevmenkin 			if (*optarg == '\0' || *ep != '\0'
1437718ced0SMaksim Yevmenkin 			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
1447718ced0SMaksim Yevmenkin 				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
1457718ced0SMaksim Yevmenkin 
1467718ced0SMaksim Yevmenkin 			l2cap_psm = ul;
1477718ced0SMaksim Yevmenkin 			break;
1487718ced0SMaksim Yevmenkin 
1497718ced0SMaksim Yevmenkin 		case 's': /* service */
1507718ced0SMaksim Yevmenkin 		case 'S': /* service (no SDP) */
1517718ced0SMaksim Yevmenkin 			for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
1527718ced0SMaksim Yevmenkin 				if (ul == __arraycount(services))
1537718ced0SMaksim Yevmenkin 					errx(EXIT_FAILURE, "%s: unknown service", optarg);
1547718ced0SMaksim Yevmenkin 			}
1557718ced0SMaksim Yevmenkin 
1567718ced0SMaksim Yevmenkin 			if (ch == 's')
1577718ced0SMaksim Yevmenkin 				service_name = services[ul].name;
1587718ced0SMaksim Yevmenkin 
1597718ced0SMaksim Yevmenkin 			service_class = services[ul].class;
1607718ced0SMaksim Yevmenkin 			break;
1617718ced0SMaksim Yevmenkin 
1627718ced0SMaksim Yevmenkin 		default:
1637718ced0SMaksim Yevmenkin 			usage();
1647718ced0SMaksim Yevmenkin 			/* NOTREACHED */
1657718ced0SMaksim Yevmenkin 		}
1667718ced0SMaksim Yevmenkin 	}
1677718ced0SMaksim Yevmenkin 
1687718ced0SMaksim Yevmenkin 	argc -= optind;
1697718ced0SMaksim Yevmenkin 	argv += optind;
1707718ced0SMaksim Yevmenkin 
1717718ced0SMaksim Yevmenkin 	/* validate options */
1727718ced0SMaksim Yevmenkin 	if (bdaddr_any(&local_bdaddr) || service_class == 0)
1737718ced0SMaksim Yevmenkin 		usage();
1747718ced0SMaksim Yevmenkin 
1757718ced0SMaksim Yevmenkin 	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
176d134fb49SMarcelo Araujo 	    control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
1777718ced0SMaksim Yevmenkin 		usage();
1787718ced0SMaksim Yevmenkin 
1797718ced0SMaksim Yevmenkin 	/* default options */
1807718ced0SMaksim Yevmenkin 	if (interface_name == NULL)
1817718ced0SMaksim Yevmenkin 		interface_name = "/dev/tap";
1827718ced0SMaksim Yevmenkin 
183905033dcSMaksim Yevmenkin 	if (l2cap_psm == 0)
184905033dcSMaksim Yevmenkin 		l2cap_psm = L2CAP_PSM_BNEP;
185905033dcSMaksim Yevmenkin 
1867718ced0SMaksim Yevmenkin 	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
1877718ced0SMaksim Yevmenkin 		if (service_class == SDP_SERVICE_CLASS_PANU)
1887718ced0SMaksim Yevmenkin 			server_limit = 1;
1897718ced0SMaksim Yevmenkin 		else
1907718ced0SMaksim Yevmenkin 			server_limit = 7;
1917718ced0SMaksim Yevmenkin 	}
1927718ced0SMaksim Yevmenkin 
1937718ced0SMaksim Yevmenkin #ifdef L2CAP_LM_MASTER
1947718ced0SMaksim Yevmenkin 	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
1957718ced0SMaksim Yevmenkin 		l2cap_mode |= L2CAP_LM_MASTER;
1967718ced0SMaksim Yevmenkin #endif
1977718ced0SMaksim Yevmenkin 
1987718ced0SMaksim Yevmenkin 	/*
1997718ced0SMaksim Yevmenkin 	 * fork() now so that the setup can be done in the child process
2007718ced0SMaksim Yevmenkin 	 * (as kqueue is not inherited) but block in the parent until the
2017718ced0SMaksim Yevmenkin 	 * setup is finished so we can return an error if necessary.
2027718ced0SMaksim Yevmenkin 	 */
2037718ced0SMaksim Yevmenkin 	switch(fork()) {
2047718ced0SMaksim Yevmenkin 	case -1: /* bad */
2057718ced0SMaksim Yevmenkin 		err(EXIT_FAILURE, "fork() failed");
2067718ced0SMaksim Yevmenkin 
2077718ced0SMaksim Yevmenkin 	case 0:	/* child */
2087718ced0SMaksim Yevmenkin 		signal(SIGPIPE, SIG_IGN);
2097718ced0SMaksim Yevmenkin 
2107718ced0SMaksim Yevmenkin 		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
2117718ced0SMaksim Yevmenkin 
2127718ced0SMaksim Yevmenkin 		channel_init();
2137718ced0SMaksim Yevmenkin 		server_init();
2147718ced0SMaksim Yevmenkin 		event_init();
2157718ced0SMaksim Yevmenkin 		client_init();
2167718ced0SMaksim Yevmenkin 		tap_init();
2177718ced0SMaksim Yevmenkin 
2187718ced0SMaksim Yevmenkin 		main_detach();
2197718ced0SMaksim Yevmenkin 
2207718ced0SMaksim Yevmenkin 		event_dispatch();
2217718ced0SMaksim Yevmenkin 		break;
2227718ced0SMaksim Yevmenkin 
2237718ced0SMaksim Yevmenkin 	default: /* parent */
2247718ced0SMaksim Yevmenkin 		signal(SIGUSR1, main_exit);
2257718ced0SMaksim Yevmenkin 		wait(&status);
2267718ced0SMaksim Yevmenkin 
2277718ced0SMaksim Yevmenkin 		if (WIFEXITED(status))
2287718ced0SMaksim Yevmenkin 			exit(WEXITSTATUS(status));
2297718ced0SMaksim Yevmenkin 
2307718ced0SMaksim Yevmenkin 		break;
2317718ced0SMaksim Yevmenkin 	}
2327718ced0SMaksim Yevmenkin 
2337718ced0SMaksim Yevmenkin 	err(EXIT_FAILURE, "exiting");
2347718ced0SMaksim Yevmenkin }
2357718ced0SMaksim Yevmenkin 
2367718ced0SMaksim Yevmenkin static void
2377718ced0SMaksim Yevmenkin main_exit(int s)
2387718ced0SMaksim Yevmenkin {
2397718ced0SMaksim Yevmenkin 
2407718ced0SMaksim Yevmenkin 	/* child is all grown up */
2417718ced0SMaksim Yevmenkin 	_exit(EXIT_SUCCESS);
2427718ced0SMaksim Yevmenkin }
2437718ced0SMaksim Yevmenkin 
2447718ced0SMaksim Yevmenkin static void
2457718ced0SMaksim Yevmenkin main_detach(void)
2467718ced0SMaksim Yevmenkin {
2477718ced0SMaksim Yevmenkin 	int fd;
2487718ced0SMaksim Yevmenkin 
2497718ced0SMaksim Yevmenkin 	if (kill(getppid(), SIGUSR1) == -1)
2507718ced0SMaksim Yevmenkin 		log_err("Could not signal main process: %m");
2517718ced0SMaksim Yevmenkin 
2527718ced0SMaksim Yevmenkin 	if (setsid() == -1)
2537718ced0SMaksim Yevmenkin 		log_err("setsid() failed");
2547718ced0SMaksim Yevmenkin 
2557718ced0SMaksim Yevmenkin 	fd = open(_PATH_DEVNULL, O_RDWR, 0);
2567718ced0SMaksim Yevmenkin 	if (fd == -1) {
2577718ced0SMaksim Yevmenkin 		log_err("Could not open %s", _PATH_DEVNULL);
2587718ced0SMaksim Yevmenkin 	} else {
2597718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDIN_FILENO);
2607718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDOUT_FILENO);
2617718ced0SMaksim Yevmenkin 		(void)dup2(fd, STDERR_FILENO);
2627718ced0SMaksim Yevmenkin 		close(fd);
2637718ced0SMaksim Yevmenkin 	}
2647718ced0SMaksim Yevmenkin }
2657718ced0SMaksim Yevmenkin 
2667718ced0SMaksim Yevmenkin static void
2677718ced0SMaksim Yevmenkin usage(void)
2687718ced0SMaksim Yevmenkin {
2697718ced0SMaksim Yevmenkin 	const char *p = getprogname();
2707718ced0SMaksim Yevmenkin 	int n = strlen(p);
2717718ced0SMaksim Yevmenkin 
2727718ced0SMaksim Yevmenkin 	fprintf(stderr,
2737718ced0SMaksim Yevmenkin 	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
2747718ced0SMaksim Yevmenkin 	    "       %*s {-s service | -S service [-p psm]}\n"
2757718ced0SMaksim Yevmenkin 	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
2767718ced0SMaksim Yevmenkin 	    "       %*s {-s service | -S service}\n"
2777718ced0SMaksim Yevmenkin 	    "\n"
2787718ced0SMaksim Yevmenkin 	    "Where:\n"
2797718ced0SMaksim Yevmenkin 	    "\t-a address  remote bluetooth device\n"
2807718ced0SMaksim Yevmenkin 	    "\t-c path     SDP server socket\n"
2817718ced0SMaksim Yevmenkin 	    "\t-d device   local bluetooth device\n"
2827718ced0SMaksim Yevmenkin 	    "\t-i ifname   tap interface\n"
2837718ced0SMaksim Yevmenkin 	    "\t-l limit    limit server sessions\n"
2847718ced0SMaksim Yevmenkin 	    "\t-m mode     L2CAP link mode (NOT YET SUPPORTED)\n"
2857718ced0SMaksim Yevmenkin 	    "\t-p psm      L2CAP PSM\n"
2867718ced0SMaksim Yevmenkin 	    "\t-S service  service name (no SDP)\n"
2877718ced0SMaksim Yevmenkin 	    "\t-s service  service name\n"
2887718ced0SMaksim Yevmenkin 	    "\n"
2897718ced0SMaksim Yevmenkin 	    "Known services:\n"
2907718ced0SMaksim Yevmenkin 	    "", p, n, "", p, n, "");
2917718ced0SMaksim Yevmenkin 
2927718ced0SMaksim Yevmenkin 	for (n = 0; n < __arraycount(services); n++)
2937718ced0SMaksim Yevmenkin 		fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
2947718ced0SMaksim Yevmenkin 
2957718ced0SMaksim Yevmenkin 	exit(EXIT_FAILURE);
2967718ced0SMaksim Yevmenkin }
297