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