Home | History | Annotate | Line # | Download | only in btpand
      1 /*	$NetBSD: btpand.c,v 1.9 2024/06/04 06:24:58 plunky Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008-2009 Iain Hibbert
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __COPYRIGHT("@(#) Copyright (c) 2008-2009 Iain Hibbert. All rights reserved.");
     30 __RCSID("$NetBSD: btpand.c,v 1.9 2024/06/04 06:24:58 plunky Exp $");
     31 
     32 #include <sys/wait.h>
     33 
     34 #include <bluetooth.h>
     35 #include <err.h>
     36 #include <fcntl.h>
     37 #include <paths.h>
     38 #include <sdp.h>
     39 #include <stdio.h>
     40 #include <signal.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 
     45 #include "btpand.h"
     46 
     47 /* global variables */
     48 const char *	control_path;		/* -c <path> */
     49 const char *	interface_name;		/* -i <ifname> */
     50 const char *	service_type;		/* -s <service> */
     51 uint16_t	service_class;
     52 const char *	service_name;
     53 const char *	service_desc;
     54 
     55 bdaddr_t	local_bdaddr;		/* -d <addr> */
     56 bdaddr_t	remote_bdaddr;		/* -a <addr> */
     57 uint16_t	l2cap_psm;		/* -p <psm> */
     58 int		l2cap_mode;		/* -m <mode> */
     59 int		server_limit;		/* -n <limit> */
     60 
     61 static const struct {
     62 	const char *	type;
     63 	uint16_t	class;
     64 	const char *	name;
     65 	const char *	desc;
     66 } services[] = {
     67 	{ "PANU", SDP_SERVICE_CLASS_PANU,
     68 	  "Personal Ad-hoc User Service",
     69 	  "Personal Ad-hoc User Service"
     70 	},
     71 	{ "NAP",  SDP_SERVICE_CLASS_NAP,
     72 	  "Network Access Point",
     73 	  "Personal Ad-hoc Network Service"
     74 	},
     75 	{ "GN",	  SDP_SERVICE_CLASS_GN,
     76 	  "Group Ad-hoc Network",
     77 	  "Personal Group Ad-hoc Network Service"
     78 	},
     79 };
     80 
     81 __dead static void main_exit(int);
     82 static void main_detach(void);
     83 __dead static void usage(void);
     84 
     85 int
     86 main(int argc, char *argv[])
     87 {
     88 	unsigned long	ul;
     89 	char *		ep;
     90 	int		ch, status;
     91 
     92 	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
     93 		switch (ch) {
     94 		case 'a': /* remote address */
     95 			if (!bt_aton(optarg, &remote_bdaddr)) {
     96 				struct hostent  *he;
     97 
     98 				if ((he = bt_gethostbyname(optarg)) == NULL)
     99 					errx(EXIT_FAILURE, "%s: %s",
    100 					    optarg, hstrerror(h_errno));
    101 
    102 				bdaddr_copy(&remote_bdaddr, (bdaddr_t *)he->h_addr);
    103 			}
    104 
    105 			break;
    106 
    107 		case 'c': /* control socket path */
    108 			control_path = optarg;
    109 			break;
    110 
    111 		case 'd': /* local address */
    112 			if (!bt_devaddr(optarg, &local_bdaddr))
    113 				err(EXIT_FAILURE, "%s", optarg);
    114 
    115 			break;
    116 
    117 		case 'i': /* tap interface name */
    118 			if (strchr(optarg, '/') == NULL) {
    119 				asprintf(&ep, "/dev/%s", optarg);
    120 				interface_name = ep;
    121 			} else {
    122 				interface_name = optarg;
    123 			}
    124 
    125 			break;
    126 
    127 		case 'l': /* limit server sessions */
    128 			ul = strtoul(optarg, &ep, 10);
    129 			if (*optarg == '\0' || *ep != '\0' || ul == 0)
    130 				errx(EXIT_FAILURE, "%s: invalid session limit", optarg);
    131 
    132 			server_limit = ul;
    133 			break;
    134 
    135 		case 'm': /* link mode */
    136 			if (strcasecmp(optarg, "auth") == 0)
    137 				l2cap_mode = L2CAP_LM_AUTH;
    138 			else if (strcasecmp(optarg, "encrypt") == 0)
    139 				l2cap_mode = L2CAP_LM_ENCRYPT;
    140 			else if (strcasecmp(optarg, "secure") == 0)
    141 				l2cap_mode = L2CAP_LM_SECURE;
    142 			else if (strcasecmp(optarg, "none"))
    143 				errx(EXIT_FAILURE, "%s: unknown mode", optarg);
    144 
    145 			break;
    146 
    147 		case 'p': /* protocol/service multiplexer */
    148 			ul = strtoul(optarg, &ep, 0);
    149 			if (*optarg == '\0' || *ep != '\0'
    150 			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
    151 				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
    152 
    153 			l2cap_psm = (uint16_t)ul;
    154 			break;
    155 
    156 		case 's': /* service */
    157 		case 'S': /* service (no SDP) */
    158 			for (ul = 0; ul < __arraycount(services); ul++) {
    159 				if (strcasecmp(optarg, services[ul].type) == 0)
    160 					break;
    161 			}
    162 
    163 			if (ul == __arraycount(services))
    164 				errx(EXIT_FAILURE, "%s: unknown service", optarg);
    165 
    166 			if (ch == 's') {
    167 				service_type = services[ul].type;
    168 				service_name = services[ul].name;
    169 				service_desc = services[ul].desc;
    170 			}
    171 
    172 			service_class = services[ul].class;
    173 			break;
    174 
    175 		default:
    176 			usage();
    177 			/* NOTREACHED */
    178 		}
    179 	}
    180 
    181 	argc -= optind;
    182 	argv += optind;
    183 
    184 	/* validate options */
    185 	if (bdaddr_any(&local_bdaddr) || service_class == 0)
    186 		usage();
    187 
    188 	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
    189 	    control_path != 0 || (service_type != NULL && l2cap_psm != 0)))
    190 		usage();
    191 
    192 	/* default options */
    193 	if (interface_name == NULL)
    194 		interface_name = "/dev/tap";
    195 
    196 	if (l2cap_psm == 0)
    197 		l2cap_psm = L2CAP_PSM_BNEP;
    198 
    199 	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
    200 		if (service_class == SDP_SERVICE_CLASS_PANU)
    201 			server_limit = 1;
    202 		else
    203 			server_limit = 7;
    204 	}
    205 
    206 #ifdef L2CAP_LM_MASTER
    207 	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
    208 		l2cap_mode |= L2CAP_LM_MASTER;
    209 #endif
    210 
    211 	/*
    212 	 * fork() now so that the setup can be done in the child process
    213 	 * (as kqueue is not inherited) but block in the parent until the
    214 	 * setup is finished so we can return an error if necessary.
    215 	 */
    216 	switch(fork()) {
    217 	case -1: /* bad */
    218 		err(EXIT_FAILURE, "fork() failed");
    219 		/*NOTREACHED*/
    220 
    221 	case 0:	/* child */
    222 		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
    223 
    224 		channel_init();
    225 		event_init();
    226 		server_init();
    227 		client_init();
    228 		tap_init();
    229 
    230 		main_detach();
    231 
    232 		event_dispatch();
    233 		break;
    234 
    235 	default: /* parent */
    236 		signal(SIGUSR1, main_exit);
    237 		wait(&status);
    238 
    239 		if (WIFEXITED(status))
    240 			exit(WEXITSTATUS(status));
    241 
    242 		break;
    243 	}
    244 
    245 	err(EXIT_FAILURE, "exiting");
    246 }
    247 
    248 static void
    249 main_exit(int s)
    250 {
    251 
    252 	/* child is all grown up */
    253 	_exit(EXIT_SUCCESS);
    254 }
    255 
    256 static void
    257 main_detach(void)
    258 {
    259 	int fd;
    260 
    261 	if (kill(getppid(), SIGUSR1) == -1)
    262 		log_err("Could not signal main process: %m");
    263 
    264 	if (setsid() == -1)
    265 		log_err("setsid() failed");
    266 
    267 	fd = open(_PATH_DEVNULL, O_RDWR, 0);
    268 	if (fd == -1) {
    269 		log_err("Could not open %s", _PATH_DEVNULL);
    270 	} else {
    271 		(void)dup2(fd, STDIN_FILENO);
    272 		(void)dup2(fd, STDOUT_FILENO);
    273 		(void)dup2(fd, STDERR_FILENO);
    274 		close(fd);
    275 	}
    276 }
    277 
    278 static void
    279 usage(void)
    280 {
    281 	const char *p = getprogname();
    282 	size_t n = strlen(p);
    283 
    284 	fprintf(stderr,
    285 	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
    286 	    "       %*s {-s service | -S service [-p psm]}\n"
    287 	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
    288 	    "       %*s {-s service | -S service}\n"
    289 	    "\n"
    290 	    "Where:\n"
    291 	    "\t-a address  remote bluetooth device\n"
    292 	    "\t-c path     SDP server socket\n"
    293 	    "\t-d device   local bluetooth device\n"
    294 	    "\t-i ifname   tap interface\n"
    295 	    "\t-l limit    limit server sessions\n"
    296 	    "\t-m mode     L2CAP link mode\n"
    297 	    "\t-p psm      L2CAP PSM\n"
    298 	    "\t-S service  service name (no SDP)\n"
    299 	    "\t-s service  service name\n"
    300 	    "\n"
    301 	    "Known services:\n"
    302 	    "", p, (int)n, "", p, (int)n, "");
    303 
    304 	for (n = 0; n < __arraycount(services); n++)
    305 		fprintf(stderr, "\t%s\t%s\n", services[n].type, services[n].name);
    306 
    307 	exit(EXIT_FAILURE);
    308 }
    309