xref: /freebsd/usr.sbin/bluetooth/btpand/btpand.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*	$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
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 
31 #include <sys/wait.h>
32 
33 #define L2CAP_SOCKET_CHECKED
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_name;		/* -s <service> */
51 uint16_t	service_class;
52 
53 bdaddr_t	local_bdaddr;		/* -d <addr> */
54 bdaddr_t	remote_bdaddr;		/* -a <addr> */
55 uint16_t	l2cap_psm;		/* -p <psm> */
56 int		l2cap_mode;		/* -m <mode> */
57 
58 int		server_limit;		/* -n <limit> */
59 
60 static const struct {
61 	const char *	name;
62 	uint16_t	class;
63 	const char *	desc;
64 } services[] = {
65 	{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
66 	{ "NAP",  SDP_SERVICE_CLASS_NAP,  "Network Access Point"		  },
67 	{ "GN",	  SDP_SERVICE_CLASS_GN,   "Group Network"		  },
68 };
69 
70 static void main_exit(int) __dead2;
71 static void main_detach(void);
72 static void usage(void) __dead2;
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	unsigned long	ul;
78 	char *		ep;
79 	int		ch, status;
80 
81 	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
82 		switch (ch) {
83 		case 'a': /* remote address */
84 			if (!bt_aton(optarg, &remote_bdaddr)) {
85 				struct hostent  *he;
86 
87 				if ((he = bt_gethostbyname(optarg)) == NULL)
88 					errx(EXIT_FAILURE, "%s: %s",
89 					    optarg, hstrerror(h_errno));
90 
91 				bdaddr_copy(&remote_bdaddr,
92 					(bdaddr_t *)he->h_addr);
93 			}
94 
95 			break;
96 
97 		case 'c': /* control socket path */
98 			control_path = optarg;
99 			break;
100 
101 		case 'd': /* local address */
102 			if (!bt_devaddr(optarg, &local_bdaddr)) {
103 				struct hostent  *he;
104 
105 				if ((he = bt_gethostbyname(optarg)) == NULL)
106 					errx(EXIT_FAILURE, "%s: %s",
107 					    optarg, hstrerror(h_errno));
108 
109 				bdaddr_copy(&local_bdaddr,
110 					(bdaddr_t *)he->h_addr);
111 			}
112 			break;
113 
114 		case 'i': /* tap interface name */
115 			if (strchr(optarg, '/') == NULL) {
116 				asprintf(&ep, "/dev/%s", optarg);
117 				interface_name = ep;
118 			} else
119 				interface_name = optarg;
120 			break;
121 
122 		case 'l': /* limit server sessions */
123 			ul = strtoul(optarg, &ep, 10);
124 			if (*optarg == '\0' || *ep != '\0' || ul == 0)
125 				errx(EXIT_FAILURE, "%s: invalid session limit",
126 					optarg);
127 
128 			server_limit = ul;
129 			break;
130 
131 		case 'm': /* link mode */
132 			warnx("Setting link mode is not yet supported");
133 			break;
134 
135 		case 'p': /* protocol/service multiplexer */
136 			ul = strtoul(optarg, &ep, 0);
137 			if (*optarg == '\0' || *ep != '\0'
138 			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
139 				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
140 
141 			l2cap_psm = ul;
142 			break;
143 
144 		case 's': /* service */
145 		case 'S': /* service (no SDP) */
146 			for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
147 				if (ul == __arraycount(services))
148 					errx(EXIT_FAILURE, "%s: unknown service", optarg);
149 			}
150 
151 			if (ch == 's')
152 				service_name = services[ul].name;
153 
154 			service_class = services[ul].class;
155 			break;
156 
157 		default:
158 			usage();
159 			/* NOTREACHED */
160 		}
161 	}
162 
163 	argc -= optind;
164 	argv += optind;
165 
166 	/* validate options */
167 	if (bdaddr_any(&local_bdaddr) || service_class == 0)
168 		usage();
169 
170 	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
171 	    control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
172 		usage();
173 
174 	/* default options */
175 	if (interface_name == NULL)
176 		interface_name = "/dev/tap";
177 
178 	if (l2cap_psm == 0)
179 		l2cap_psm = L2CAP_PSM_BNEP;
180 
181 	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
182 		if (service_class == SDP_SERVICE_CLASS_PANU)
183 			server_limit = 1;
184 		else
185 			server_limit = 7;
186 	}
187 
188 #ifdef L2CAP_LM_MASTER
189 	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
190 		l2cap_mode |= L2CAP_LM_MASTER;
191 #endif
192 
193 	/*
194 	 * fork() now so that the setup can be done in the child process
195 	 * (as kqueue is not inherited) but block in the parent until the
196 	 * setup is finished so we can return an error if necessary.
197 	 */
198 	switch(fork()) {
199 	case -1: /* bad */
200 		err(EXIT_FAILURE, "fork() failed");
201 
202 	case 0:	/* child */
203 		signal(SIGPIPE, SIG_IGN);
204 
205 		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
206 
207 		channel_init();
208 		server_init();
209 		event_init();
210 		client_init();
211 		tap_init();
212 
213 		main_detach();
214 
215 		event_dispatch();
216 		break;
217 
218 	default: /* parent */
219 		signal(SIGUSR1, main_exit);
220 		wait(&status);
221 
222 		if (WIFEXITED(status))
223 			exit(WEXITSTATUS(status));
224 
225 		break;
226 	}
227 
228 	err(EXIT_FAILURE, "exiting");
229 }
230 
231 static void
232 main_exit(int s)
233 {
234 
235 	/* child is all grown up */
236 	_exit(EXIT_SUCCESS);
237 }
238 
239 static void
240 main_detach(void)
241 {
242 	int fd;
243 
244 	if (kill(getppid(), SIGUSR1) == -1)
245 		log_err("Could not signal main process: %m");
246 
247 	if (setsid() == -1)
248 		log_err("setsid() failed");
249 
250 	fd = open(_PATH_DEVNULL, O_RDWR, 0);
251 	if (fd == -1) {
252 		log_err("Could not open %s", _PATH_DEVNULL);
253 	} else {
254 		(void)dup2(fd, STDIN_FILENO);
255 		(void)dup2(fd, STDOUT_FILENO);
256 		(void)dup2(fd, STDERR_FILENO);
257 		close(fd);
258 	}
259 }
260 
261 static void
262 usage(void)
263 {
264 	const char *p = getprogname();
265 	int n = strlen(p);
266 
267 	fprintf(stderr,
268 	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
269 	    "       %*s {-s service | -S service [-p psm]}\n"
270 	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
271 	    "       %*s {-s service | -S service}\n"
272 	    "\n"
273 	    "Where:\n"
274 	    "\t-a address  remote bluetooth device\n"
275 	    "\t-c path     SDP server socket\n"
276 	    "\t-d device   local bluetooth device\n"
277 	    "\t-i ifname   tap interface\n"
278 	    "\t-l limit    limit server sessions\n"
279 	    "\t-m mode     L2CAP link mode (NOT YET SUPPORTED)\n"
280 	    "\t-p psm      L2CAP PSM\n"
281 	    "\t-S service  service name (no SDP)\n"
282 	    "\t-s service  service name\n"
283 	    "\n"
284 	    "Known services:\n"
285 	    "", p, n, "", p, n, "");
286 
287 	for (n = 0; n < __arraycount(services); n++)
288 		fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
289 
290 	exit(EXIT_FAILURE);
291 }
292