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