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