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
main(int argc,char * argv[])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; ul < __arraycount(services); ul++) {
151 if (strcasecmp(optarg, services[ul].name) == 0)
152 break;
153 }
154
155 if (ul == __arraycount(services))
156 errx(EXIT_FAILURE, "%s: unknown service", optarg);
157
158 if (ch == 's')
159 service_name = services[ul].name;
160
161 service_class = services[ul].class;
162 break;
163
164 default:
165 usage();
166 /* NOTREACHED */
167 }
168 }
169
170 argc -= optind;
171 argv += optind;
172
173 /* validate options */
174 if (bdaddr_any(&local_bdaddr) || service_class == 0)
175 usage();
176
177 if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
178 control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
179 usage();
180
181 /* default options */
182 if (interface_name == NULL)
183 interface_name = "/dev/tap";
184
185 if (l2cap_psm == 0)
186 l2cap_psm = L2CAP_PSM_BNEP;
187
188 if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
189 if (service_class == SDP_SERVICE_CLASS_PANU)
190 server_limit = 1;
191 else
192 server_limit = 7;
193 }
194
195 #ifdef L2CAP_LM_MASTER
196 if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
197 l2cap_mode |= L2CAP_LM_MASTER;
198 #endif
199
200 /*
201 * fork() now so that the setup can be done in the child process
202 * (as kqueue is not inherited) but block in the parent until the
203 * setup is finished so we can return an error if necessary.
204 */
205 switch(fork()) {
206 case -1: /* bad */
207 err(EXIT_FAILURE, "fork() failed");
208
209 case 0: /* child */
210 signal(SIGPIPE, SIG_IGN);
211
212 openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
213
214 channel_init();
215 server_init();
216 event_init();
217 client_init();
218 tap_init();
219
220 main_detach();
221
222 event_dispatch();
223 break;
224
225 default: /* parent */
226 signal(SIGUSR1, main_exit);
227 wait(&status);
228
229 if (WIFEXITED(status))
230 exit(WEXITSTATUS(status));
231
232 break;
233 }
234
235 err(EXIT_FAILURE, "exiting");
236 }
237
238 static void
main_exit(int s)239 main_exit(int s)
240 {
241
242 /* child is all grown up */
243 _exit(EXIT_SUCCESS);
244 }
245
246 static void
main_detach(void)247 main_detach(void)
248 {
249 int fd;
250
251 if (kill(getppid(), SIGUSR1) == -1)
252 log_err("Could not signal main process: %m");
253
254 if (setsid() == -1)
255 log_err("setsid() failed");
256
257 fd = open(_PATH_DEVNULL, O_RDWR, 0);
258 if (fd == -1) {
259 log_err("Could not open %s", _PATH_DEVNULL);
260 } else {
261 (void)dup2(fd, STDIN_FILENO);
262 (void)dup2(fd, STDOUT_FILENO);
263 (void)dup2(fd, STDERR_FILENO);
264 close(fd);
265 }
266 }
267
268 static void
usage(void)269 usage(void)
270 {
271 const char *p = getprogname();
272 int n = strlen(p);
273
274 fprintf(stderr,
275 "usage: %s [-i ifname] [-m mode] -a address -d device\n"
276 " %*s {-s service | -S service [-p psm]}\n"
277 " %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
278 " %*s {-s service | -S service}\n"
279 "\n"
280 "Where:\n"
281 "\t-a address remote bluetooth device\n"
282 "\t-c path SDP server socket\n"
283 "\t-d device local bluetooth device\n"
284 "\t-i ifname tap interface\n"
285 "\t-l limit limit server sessions\n"
286 "\t-m mode L2CAP link mode (NOT YET SUPPORTED)\n"
287 "\t-p psm L2CAP PSM\n"
288 "\t-S service service name (no SDP)\n"
289 "\t-s service service name\n"
290 "\n"
291 "Known services:\n"
292 "", p, n, "", p, n, "");
293
294 for (n = 0; n < __arraycount(services); n++)
295 fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
296
297 exit(EXIT_FAILURE);
298 }
299