1/*	$NetBSD: btpand.c,v 1.8 2022/05/18 13:56:32 andvar 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.8 2022/05/18 13:56:32 andvar 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 */
48const char *	control_path;		/* -c <path> */
49const char *	interface_name;		/* -i <ifname> */
50const char *	service_type;		/* -s <service> */
51uint16_t	service_class;
52const char *	service_name;
53const char *	service_desc;
54
55bdaddr_t	local_bdaddr;		/* -d <addr> */
56bdaddr_t	remote_bdaddr;		/* -a <addr> */
57uint16_t	l2cap_psm;		/* -p <psm> */
58int		l2cap_mode;		/* -m <mode> */
59int		server_limit;		/* -n <limit> */
60
61static 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);
82static void main_detach(void);
83__dead static void usage(void);
84
85int
86main(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; strcasecmp(optarg, services[ul].type); ul++) {
159				if (ul == __arraycount(services))
160					errx(EXIT_FAILURE, "%s: unknown service", optarg);
161			}
162
163			if (ch == 's') {
164				service_type = services[ul].type;
165				service_name = services[ul].name;
166				service_desc = services[ul].desc;
167			}
168
169			service_class = services[ul].class;
170			break;
171
172		default:
173			usage();
174			/* NOTREACHED */
175		}
176	}
177
178	argc -= optind;
179	argv += optind;
180
181	/* validate options */
182	if (bdaddr_any(&local_bdaddr) || service_class == 0)
183		usage();
184
185	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
186	    control_path != 0 || (service_type != NULL && l2cap_psm != 0)))
187		usage();
188
189	/* default options */
190	if (interface_name == NULL)
191		interface_name = "/dev/tap";
192
193	if (l2cap_psm == 0)
194		l2cap_psm = L2CAP_PSM_BNEP;
195
196	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
197		if (service_class == SDP_SERVICE_CLASS_PANU)
198			server_limit = 1;
199		else
200			server_limit = 7;
201	}
202
203#ifdef L2CAP_LM_MASTER
204	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
205		l2cap_mode |= L2CAP_LM_MASTER;
206#endif
207
208	/*
209	 * fork() now so that the setup can be done in the child process
210	 * (as kqueue is not inherited) but block in the parent until the
211	 * setup is finished so we can return an error if necessary.
212	 */
213	switch(fork()) {
214	case -1: /* bad */
215		err(EXIT_FAILURE, "fork() failed");
216		/*NOTREACHED*/
217
218	case 0:	/* child */
219		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
220
221		channel_init();
222		event_init();
223		server_init();
224		client_init();
225		tap_init();
226
227		main_detach();
228
229		event_dispatch();
230		break;
231
232	default: /* parent */
233		signal(SIGUSR1, main_exit);
234		wait(&status);
235
236		if (WIFEXITED(status))
237			exit(WEXITSTATUS(status));
238
239		break;
240	}
241
242	err(EXIT_FAILURE, "exiting");
243}
244
245static void
246main_exit(int s)
247{
248
249	/* child is all grown up */
250	_exit(EXIT_SUCCESS);
251}
252
253static void
254main_detach(void)
255{
256	int fd;
257
258	if (kill(getppid(), SIGUSR1) == -1)
259		log_err("Could not signal main process: %m");
260
261	if (setsid() == -1)
262		log_err("setsid() failed");
263
264	fd = open(_PATH_DEVNULL, O_RDWR, 0);
265	if (fd == -1) {
266		log_err("Could not open %s", _PATH_DEVNULL);
267	} else {
268		(void)dup2(fd, STDIN_FILENO);
269		(void)dup2(fd, STDOUT_FILENO);
270		(void)dup2(fd, STDERR_FILENO);
271		close(fd);
272	}
273}
274
275static void
276usage(void)
277{
278	const char *p = getprogname();
279	size_t n = strlen(p);
280
281	fprintf(stderr,
282	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
283	    "       %*s {-s service | -S service [-p psm]}\n"
284	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
285	    "       %*s {-s service | -S service}\n"
286	    "\n"
287	    "Where:\n"
288	    "\t-a address  remote bluetooth device\n"
289	    "\t-c path     SDP server socket\n"
290	    "\t-d device   local bluetooth device\n"
291	    "\t-i ifname   tap interface\n"
292	    "\t-l limit    limit server sessions\n"
293	    "\t-m mode     L2CAP link mode\n"
294	    "\t-p psm      L2CAP PSM\n"
295	    "\t-S service  service name (no SDP)\n"
296	    "\t-s service  service name\n"
297	    "\n"
298	    "Known services:\n"
299	    "", p, (int)n, "", p, (int)n, "");
300
301	for (n = 0; n < __arraycount(services); n++)
302		fprintf(stderr, "\t%s\t%s\n", services[n].type, services[n].name);
303
304	exit(EXIT_FAILURE);
305}
306