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