1187938Semax/*	$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
2187938Semax
3187938Semax/*-
4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5330449Seadler *
6187938Semax * Copyright (c) 2008 Iain Hibbert
7187938Semax * All rights reserved.
8187938Semax *
9187938Semax * Redistribution and use in source and binary forms, with or without
10187938Semax * modification, are permitted provided that the following conditions
11187938Semax * are met:
12187938Semax * 1. Redistributions of source code must retain the above copyright
13187938Semax *    notice, this list of conditions and the following disclaimer.
14187938Semax * 2. Redistributions in binary form must reproduce the above copyright
15187938Semax *    notice, this list of conditions and the following disclaimer in the
16187938Semax *    documentation and/or other materials provided with the distribution.
17187938Semax *
18187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28187938Semax */
29187938Semax
30187938Semax/* $FreeBSD: stable/11/usr.sbin/bluetooth/btpand/btpand.c 330449 2018-03-05 07:26:05Z eadler $ */
31187938Semax
32187938Semax#include <sys/cdefs.h>
33187938Semax__COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
34187938Semax__RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
35187938Semax
36187938Semax#include <sys/wait.h>
37187938Semax
38281210Stakawata#define L2CAP_SOCKET_CHECKED
39187938Semax#include <bluetooth.h>
40187938Semax#include <err.h>
41187938Semax#include <fcntl.h>
42187938Semax#include <paths.h>
43187938Semax#include <sdp.h>
44187938Semax#include <stdio.h>
45187938Semax#include <signal.h>
46187938Semax#include <stdlib.h>
47187938Semax#include <string.h>
48187938Semax#include <unistd.h>
49187938Semax
50187938Semax#include "btpand.h"
51187938Semax
52187938Semax/* global variables */
53187938Semaxconst char *	control_path;		/* -c <path> */
54187938Semaxconst char *	interface_name;		/* -i <ifname> */
55187938Semaxconst char *	service_name;		/* -s <service> */
56187938Semaxuint16_t	service_class;
57187938Semax
58187938Semaxbdaddr_t	local_bdaddr;		/* -d <addr> */
59187938Semaxbdaddr_t	remote_bdaddr;		/* -a <addr> */
60188013Semaxuint16_t	l2cap_psm;		/* -p <psm> */
61188013Semaxint		l2cap_mode;		/* -m <mode> */
62187938Semax
63187938Semaxint		server_limit;		/* -n <limit> */
64187938Semax
65187938Semaxstatic const struct {
66187938Semax	const char *	name;
67187938Semax	uint16_t	class;
68187938Semax	const char *	desc;
69187938Semax} services[] = {
70187938Semax	{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
71298883Spfg	{ "NAP",  SDP_SERVICE_CLASS_NAP,  "Network Access Point"		  },
72187938Semax	{ "GN",	  SDP_SERVICE_CLASS_GN,   "Group Network"		  },
73187938Semax};
74187938Semax
75187938Semaxstatic void main_exit(int);
76187938Semaxstatic void main_detach(void);
77187938Semaxstatic void usage(void);
78187938Semax
79187938Semaxint
80187938Semaxmain(int argc, char *argv[])
81187938Semax{
82187938Semax	unsigned long	ul;
83187938Semax	char *		ep;
84187938Semax	int		ch, status;
85187938Semax
86187938Semax	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
87187938Semax		switch (ch) {
88187938Semax		case 'a': /* remote address */
89187938Semax			if (!bt_aton(optarg, &remote_bdaddr)) {
90187938Semax				struct hostent  *he;
91187938Semax
92187938Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
93187938Semax					errx(EXIT_FAILURE, "%s: %s",
94187938Semax					    optarg, hstrerror(h_errno));
95187938Semax
96187938Semax				bdaddr_copy(&remote_bdaddr,
97187938Semax					(bdaddr_t *)he->h_addr);
98187938Semax			}
99187938Semax
100187938Semax			break;
101187938Semax
102187938Semax		case 'c': /* control socket path */
103187938Semax			control_path = optarg;
104187938Semax			break;
105187938Semax
106187938Semax		case 'd': /* local address */
107192308Semax			if (!bt_devaddr(optarg, &local_bdaddr)) {
108187938Semax				struct hostent  *he;
109187938Semax
110187938Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
111187938Semax					errx(EXIT_FAILURE, "%s: %s",
112187938Semax					    optarg, hstrerror(h_errno));
113187938Semax
114187938Semax				bdaddr_copy(&local_bdaddr,
115187938Semax					(bdaddr_t *)he->h_addr);
116187938Semax			}
117187938Semax			break;
118187938Semax
119187938Semax		case 'i': /* tap interface name */
120187938Semax			if (strchr(optarg, '/') == NULL) {
121187938Semax				asprintf(&ep, "/dev/%s", optarg);
122187938Semax				interface_name = ep;
123187938Semax			} else
124187938Semax				interface_name = optarg;
125187938Semax			break;
126187938Semax
127187938Semax		case 'l': /* limit server sessions */
128187938Semax			ul = strtoul(optarg, &ep, 10);
129187938Semax			if (*optarg == '\0' || *ep != '\0' || ul == 0)
130187938Semax				errx(EXIT_FAILURE, "%s: invalid session limit",
131187938Semax					optarg);
132187938Semax
133187938Semax			server_limit = ul;
134187938Semax			break;
135187938Semax
136187938Semax		case 'm': /* link mode */
137187938Semax			warnx("Setting link mode is not yet supported");
138187938Semax			break;
139187938Semax
140187938Semax		case 'p': /* protocol/service multiplexer */
141187938Semax			ul = strtoul(optarg, &ep, 0);
142187938Semax			if (*optarg == '\0' || *ep != '\0'
143187938Semax			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
144187938Semax				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
145187938Semax
146187938Semax			l2cap_psm = ul;
147187938Semax			break;
148187938Semax
149187938Semax		case 's': /* service */
150187938Semax		case 'S': /* service (no SDP) */
151187938Semax			for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
152187938Semax				if (ul == __arraycount(services))
153187938Semax					errx(EXIT_FAILURE, "%s: unknown service", optarg);
154187938Semax			}
155187938Semax
156187938Semax			if (ch == 's')
157187938Semax				service_name = services[ul].name;
158187938Semax
159187938Semax			service_class = services[ul].class;
160187938Semax			break;
161187938Semax
162187938Semax		default:
163187938Semax			usage();
164187938Semax			/* NOTREACHED */
165187938Semax		}
166187938Semax	}
167187938Semax
168187938Semax	argc -= optind;
169187938Semax	argv += optind;
170187938Semax
171187938Semax	/* validate options */
172187938Semax	if (bdaddr_any(&local_bdaddr) || service_class == 0)
173187938Semax		usage();
174187938Semax
175187938Semax	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
176301482Saraujo	    control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
177187938Semax		usage();
178187938Semax
179187938Semax	/* default options */
180187938Semax	if (interface_name == NULL)
181187938Semax		interface_name = "/dev/tap";
182187938Semax
183188013Semax	if (l2cap_psm == 0)
184188013Semax		l2cap_psm = L2CAP_PSM_BNEP;
185188013Semax
186187938Semax	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
187187938Semax		if (service_class == SDP_SERVICE_CLASS_PANU)
188187938Semax			server_limit = 1;
189187938Semax		else
190187938Semax			server_limit = 7;
191187938Semax	}
192187938Semax
193187938Semax#ifdef L2CAP_LM_MASTER
194187938Semax	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
195187938Semax		l2cap_mode |= L2CAP_LM_MASTER;
196187938Semax#endif
197187938Semax
198187938Semax	/*
199187938Semax	 * fork() now so that the setup can be done in the child process
200187938Semax	 * (as kqueue is not inherited) but block in the parent until the
201187938Semax	 * setup is finished so we can return an error if necessary.
202187938Semax	 */
203187938Semax	switch(fork()) {
204187938Semax	case -1: /* bad */
205187938Semax		err(EXIT_FAILURE, "fork() failed");
206187938Semax
207187938Semax	case 0:	/* child */
208187938Semax		signal(SIGPIPE, SIG_IGN);
209187938Semax
210187938Semax		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
211187938Semax
212187938Semax		channel_init();
213187938Semax		server_init();
214187938Semax		event_init();
215187938Semax		client_init();
216187938Semax		tap_init();
217187938Semax
218187938Semax		main_detach();
219187938Semax
220187938Semax		event_dispatch();
221187938Semax		break;
222187938Semax
223187938Semax	default: /* parent */
224187938Semax		signal(SIGUSR1, main_exit);
225187938Semax		wait(&status);
226187938Semax
227187938Semax		if (WIFEXITED(status))
228187938Semax			exit(WEXITSTATUS(status));
229187938Semax
230187938Semax		break;
231187938Semax	}
232187938Semax
233187938Semax	err(EXIT_FAILURE, "exiting");
234187938Semax}
235187938Semax
236187938Semaxstatic void
237187938Semaxmain_exit(int s)
238187938Semax{
239187938Semax
240187938Semax	/* child is all grown up */
241187938Semax	_exit(EXIT_SUCCESS);
242187938Semax}
243187938Semax
244187938Semaxstatic void
245187938Semaxmain_detach(void)
246187938Semax{
247187938Semax	int fd;
248187938Semax
249187938Semax	if (kill(getppid(), SIGUSR1) == -1)
250187938Semax		log_err("Could not signal main process: %m");
251187938Semax
252187938Semax	if (setsid() == -1)
253187938Semax		log_err("setsid() failed");
254187938Semax
255187938Semax	fd = open(_PATH_DEVNULL, O_RDWR, 0);
256187938Semax	if (fd == -1) {
257187938Semax		log_err("Could not open %s", _PATH_DEVNULL);
258187938Semax	} else {
259187938Semax		(void)dup2(fd, STDIN_FILENO);
260187938Semax		(void)dup2(fd, STDOUT_FILENO);
261187938Semax		(void)dup2(fd, STDERR_FILENO);
262187938Semax		close(fd);
263187938Semax	}
264187938Semax}
265187938Semax
266187938Semaxstatic void
267187938Semaxusage(void)
268187938Semax{
269187938Semax	const char *p = getprogname();
270187938Semax	int n = strlen(p);
271187938Semax
272187938Semax	fprintf(stderr,
273187938Semax	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
274187938Semax	    "       %*s {-s service | -S service [-p psm]}\n"
275187938Semax	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
276187938Semax	    "       %*s {-s service | -S service}\n"
277187938Semax	    "\n"
278187938Semax	    "Where:\n"
279187938Semax	    "\t-a address  remote bluetooth device\n"
280187938Semax	    "\t-c path     SDP server socket\n"
281187938Semax	    "\t-d device   local bluetooth device\n"
282187938Semax	    "\t-i ifname   tap interface\n"
283187938Semax	    "\t-l limit    limit server sessions\n"
284187938Semax	    "\t-m mode     L2CAP link mode (NOT YET SUPPORTED)\n"
285187938Semax	    "\t-p psm      L2CAP PSM\n"
286187938Semax	    "\t-S service  service name (no SDP)\n"
287187938Semax	    "\t-s service  service name\n"
288187938Semax	    "\n"
289187938Semax	    "Known services:\n"
290187938Semax	    "", p, n, "", p, n, "");
291187938Semax
292187938Semax	for (n = 0; n < __arraycount(services); n++)
293187938Semax		fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
294187938Semax
295187938Semax	exit(EXIT_FAILURE);
296187938Semax}
297