xref: /netbsd/usr.bin/rfcomm_sppd/rfcomm_sppd.c (revision 5545111f)
1*5545111fSchristos /*	$NetBSD: rfcomm_sppd.c,v 1.17 2017/01/10 21:12:03 christos Exp $	*/
2a5c89047Sgdamore 
3a5c89047Sgdamore /*-
4a5c89047Sgdamore  * Copyright (c) 2006 Itronix Inc.
5a5c89047Sgdamore  * All rights reserved.
6a5c89047Sgdamore  *
7a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
8a5c89047Sgdamore  * modification, are permitted provided that the following conditions
9a5c89047Sgdamore  * are met:
10a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
11a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
12a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
13a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
14a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
15a5c89047Sgdamore  * 3. The name of Itronix Inc. may not be used to endorse
16a5c89047Sgdamore  *    or promote products derived from this software without specific
17a5c89047Sgdamore  *    prior written permission.
18a5c89047Sgdamore  *
19a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20a5c89047Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a5c89047Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a5c89047Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23a5c89047Sgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24a5c89047Sgdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25a5c89047Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26a5c89047Sgdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
27a5c89047Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a5c89047Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a5c89047Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
30a5c89047Sgdamore  */
31a5c89047Sgdamore /*
32a0922929Splunky  * Copyright (c) 2009 The NetBSD Foundation, Inc.
33defc1075Splunky  * Copyright (c) 2007 Iain Hibbert
34a5c89047Sgdamore  * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
35a5c89047Sgdamore  * All rights reserved.
36a5c89047Sgdamore  *
37a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
38a5c89047Sgdamore  * modification, are permitted provided that the following conditions
39a5c89047Sgdamore  * are met:
40a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
41a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
42a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
43a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
44a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
45a5c89047Sgdamore  *
46a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47a5c89047Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48a5c89047Sgdamore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49a5c89047Sgdamore  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50a5c89047Sgdamore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51a5c89047Sgdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52a5c89047Sgdamore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53a5c89047Sgdamore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54a5c89047Sgdamore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55a5c89047Sgdamore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56a5c89047Sgdamore  * SUCH DAMAGE.
57a5c89047Sgdamore  */
58a5c89047Sgdamore 
59a5c89047Sgdamore #include <sys/cdefs.h>
60a0922929Splunky __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
61a0922929Splunky   Copyright (c) 2007 Iain Hibbert.\
6298e5374cSlukem   Copyright (c) 2006 Itronix, Inc.\
6398e5374cSlukem   Copyright (c) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\
6498e5374cSlukem   All rights reserved.");
65*5545111fSchristos __RCSID("$NetBSD: rfcomm_sppd.c,v 1.17 2017/01/10 21:12:03 christos Exp $");
669b85369aSplunky 
679b85369aSplunky #include <sys/param.h>
68*5545111fSchristos #include <sys/stat.h>
69a5c89047Sgdamore 
70a5c89047Sgdamore #include <bluetooth.h>
71a5c89047Sgdamore #include <ctype.h>
72a5c89047Sgdamore #include <err.h>
73a5c89047Sgdamore #include <errno.h>
74a5c89047Sgdamore #include <fcntl.h>
75a5c89047Sgdamore #include <grp.h>
76a5c89047Sgdamore #include <limits.h>
77a5c89047Sgdamore #include <paths.h>
78a5c89047Sgdamore #include <sdp.h>
79a5c89047Sgdamore #include <signal.h>
80a5c89047Sgdamore #include <stdarg.h>
81bc12f975Schristos #include <poll.h>
82a5c89047Sgdamore #include <stdio.h>
83a5c89047Sgdamore #include <stdlib.h>
84a5c89047Sgdamore #include <string.h>
85a5c89047Sgdamore #include <syslog.h>
86a5c89047Sgdamore #include <termios.h>
87a5c89047Sgdamore #include <unistd.h>
88a5c89047Sgdamore 
89f5db72e7Splunky #include <netbt/rfcomm.h>
90f5db72e7Splunky 
91bc12f975Schristos static int open_tty(const char *);
92bc12f975Schristos static int open_client(bdaddr_t *, bdaddr_t *, int, uintmax_t, const char *);
93bc12f975Schristos static int open_server(bdaddr_t *, uint16_t, uint8_t, int, const char *);
94bc12f975Schristos static void copy_data(int, int);
95bc12f975Schristos static int service_search(const bdaddr_t *, const bdaddr_t *, uint16_t,
96bc12f975Schristos     uintmax_t *, uintmax_t *);
97bc12f975Schristos static void sighandler(int);
98bc12f975Schristos static void usage(void) __attribute__((__noreturn__));
99bc12f975Schristos static void reset_tio(void);
100a5c89047Sgdamore 
101bc12f975Schristos static sig_atomic_t done;	/* got a signal */
102bc12f975Schristos static struct termios tio;	/* stored termios for reset on exit */
103a5c89047Sgdamore 
104bc12f975Schristos static const struct service {
105defc1075Splunky 	const char *	name;
106defc1075Splunky 	const char *	description;
107defc1075Splunky 	uint16_t	class;
108defc1075Splunky } services[] = {
109defc1075Splunky 	{ "DUN",	"Dialup Networking",
110a0922929Splunky 	    SDP_SERVICE_CLASS_DIALUP_NETWORKING		},
111a0922929Splunky 	{ "LAN",	"LAN access using PPP",
112a0922929Splunky 	    SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP	},
113defc1075Splunky 	{ "SP",		"Serial Port",
114a0922929Splunky 	    SDP_SERVICE_CLASS_SERIAL_PORT		},
115a0922929Splunky 	{ NULL,		NULL,		0		}
116defc1075Splunky };
117defc1075Splunky 
118a5c89047Sgdamore int
main(int argc,char * argv[])119a5c89047Sgdamore main(int argc, char *argv[])
120a5c89047Sgdamore {
121defc1075Splunky 	struct termios		t;
122a5c89047Sgdamore 	bdaddr_t		laddr, raddr;
123bc12f975Schristos 	struct pollfd		pfd[2];
12489f52265Splunky 	const char		*service;
12589f52265Splunky 	char			*ep, *tty;
126bc12f975Schristos 	int			n, lm, rfcomm, tty_in, tty_out;
12748b6f8e4Splunky 	uint16_t		psm;
128a5c89047Sgdamore 	uint8_t			channel;
129a5c89047Sgdamore 
130bc12f975Schristos 	setprogname(argv[0]);
131a5c89047Sgdamore 	bdaddr_copy(&laddr, BDADDR_ANY);
132a5c89047Sgdamore 	bdaddr_copy(&raddr, BDADDR_ANY);
133d7e75bccSplunky 	service = "SP";
134d7e75bccSplunky 	tty = NULL;
135a4d96955Splunky 	channel = RFCOMM_CHANNEL_ANY;
13648b6f8e4Splunky 	psm = L2CAP_PSM_RFCOMM;
137f5db72e7Splunky 	lm = 0;
138a5c89047Sgdamore 
139a5c89047Sgdamore 	/* Parse command line options */
14048b6f8e4Splunky 	while ((n = getopt(argc, argv, "a:c:d:hm:p:s:t:")) != -1) {
141a5c89047Sgdamore 		switch (n) {
142defc1075Splunky 		case 'a': /* remote device address */
143a5c89047Sgdamore 			if (!bt_aton(optarg, &raddr)) {
144a5c89047Sgdamore 				struct hostent	*he = NULL;
145a5c89047Sgdamore 
146a5c89047Sgdamore 				if ((he = bt_gethostbyname(optarg)) == NULL)
147defc1075Splunky 					errx(EXIT_FAILURE, "%s: %s", optarg,
148defc1075Splunky 					    hstrerror(h_errno));
149a5c89047Sgdamore 
150a5c89047Sgdamore 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
151a5c89047Sgdamore 			}
152a5c89047Sgdamore 			break;
153a5c89047Sgdamore 
154a5c89047Sgdamore 		case 'c': /* RFCOMM channel */
155a5c89047Sgdamore 			channel = strtoul(optarg, &ep, 10);
156a4d96955Splunky 			if (*ep != '\0'
157a4d96955Splunky 			    || channel < RFCOMM_CHANNEL_MIN
158a4d96955Splunky 			    || channel > RFCOMM_CHANNEL_MAX)
159bc12f975Schristos 				errx(EXIT_FAILURE, "Invalid channel: %s",
160bc12f975Schristos 				    optarg);
161d7e75bccSplunky 
162a5c89047Sgdamore 			break;
163a5c89047Sgdamore 
164defc1075Splunky 		case 'd': /* local device address */
165a5c89047Sgdamore 			if (!bt_devaddr(optarg, &laddr))
166a5c89047Sgdamore 				err(EXIT_FAILURE, "%s", optarg);
167defc1075Splunky 
168defc1075Splunky 			break;
169defc1075Splunky 
170f5db72e7Splunky 		case 'm': /* Link Mode */
171f5db72e7Splunky 			if (strcasecmp(optarg, "auth") == 0)
172f5db72e7Splunky 				lm = RFCOMM_LM_AUTH;
173f5db72e7Splunky 			else if (strcasecmp(optarg, "encrypt") == 0)
174f5db72e7Splunky 				lm = RFCOMM_LM_ENCRYPT;
175f5db72e7Splunky 			else if (strcasecmp(optarg, "secure") == 0)
176f5db72e7Splunky 				lm = RFCOMM_LM_SECURE;
177f5db72e7Splunky 			else
178bc12f975Schristos 				errx(EXIT_FAILURE, "Unknown mode: %s", optarg);
179f5db72e7Splunky 
180f5db72e7Splunky 			break;
181f5db72e7Splunky 
18248b6f8e4Splunky 		case 'p': /* PSM */
18348b6f8e4Splunky 			psm = strtoul(optarg, &ep, 0);
18448b6f8e4Splunky 			if (*ep != '\0' || L2CAP_PSM_INVALID(psm))
18548b6f8e4Splunky 				errx(EXIT_FAILURE, "Invalid PSM: %s", optarg);
18648b6f8e4Splunky 
18748b6f8e4Splunky 			break;
18848b6f8e4Splunky 
189d7e75bccSplunky 		case 's': /* service class */
190defc1075Splunky 			service = optarg;
191a5c89047Sgdamore 			break;
192a5c89047Sgdamore 
193a5c89047Sgdamore 		case 't': /* Slave TTY name */
194a5c89047Sgdamore 			if (optarg[0] != '/')
195a5c89047Sgdamore 				asprintf(&tty, "%s%s", _PATH_DEV, optarg);
196a5c89047Sgdamore 			else
197a5c89047Sgdamore 				tty = optarg;
198defc1075Splunky 
199a5c89047Sgdamore 			break;
200a5c89047Sgdamore 
201a5c89047Sgdamore 		case 'h':
202a5c89047Sgdamore 		default:
203a5c89047Sgdamore 			usage();
204a5c89047Sgdamore 			/* NOT REACHED */
205a5c89047Sgdamore 		}
206a5c89047Sgdamore 	}
207a5c89047Sgdamore 
208defc1075Splunky 	/*
209defc1075Splunky 	 * validate options:
210a4d96955Splunky 	 *	cannot have remote address if channel was given
211defc1075Splunky 	 */
212a4d96955Splunky 	if (channel != RFCOMM_CHANNEL_ANY && !bdaddr_any(&raddr))
213a5c89047Sgdamore 		usage();
214a5c89047Sgdamore 
215defc1075Splunky 	/*
216defc1075Splunky 	 * grab ttys before we start the bluetooth
217defc1075Splunky 	 */
218a5c89047Sgdamore 	if (tty == NULL) {
219defc1075Splunky 		tty_in = STDIN_FILENO;
220defc1075Splunky 		tty_out = STDOUT_FILENO;
221a5c89047Sgdamore 	} else {
222defc1075Splunky 		tty_in = open_tty(tty);
223defc1075Splunky 		tty_out = tty_in;
224a5c89047Sgdamore 	}
225a5c89047Sgdamore 
226defc1075Splunky 	/* open RFCOMM */
227a4d96955Splunky 	if (!bdaddr_any(&raddr))
22848b6f8e4Splunky 		rfcomm = open_client(&laddr, &raddr, lm, psm, service);
229defc1075Splunky 	else
23048b6f8e4Splunky 		rfcomm = open_server(&laddr, psm, channel, lm, service);
231a5c89047Sgdamore 
232defc1075Splunky 	/*
233e55e3c24Splunky 	 * now we are ready to go, so either detach or maybe turn
234defc1075Splunky 	 * off some input processing, so that rfcomm_sppd can
235defc1075Splunky 	 * be used directly with stdio
236defc1075Splunky 	 */
237defc1075Splunky 	if (tty == NULL) {
238bc12f975Schristos 		if (tcgetattr(tty_in, &t) != -1) {
239bc12f975Schristos 			tio = t;
240defc1075Splunky 			t.c_lflag &= ~(ECHO | ICANON);
241defc1075Splunky 			t.c_iflag &= ~(ICRNL);
242a5c89047Sgdamore 
243bc12f975Schristos 			if (tio.c_lflag != t.c_lflag ||
244bc12f975Schristos 			    tio.c_iflag != t.c_iflag) {
245bc12f975Schristos 				if (tcsetattr(tty_in, TCSANOW, &t) == -1)
246defc1075Splunky 					err(EXIT_FAILURE, "tcsetattr");
247a5c89047Sgdamore 
248defc1075Splunky 				atexit(reset_tio);
249e55e3c24Splunky 			}
250efaca1f1Splunky 		}
251defc1075Splunky 	} else {
252bc12f975Schristos 		if (daemon(0, 0) == -1)
253defc1075Splunky 			err(EXIT_FAILURE, "daemon() failed");
254a5c89047Sgdamore 	}
255a5c89047Sgdamore 
256defc1075Splunky 	/* catch signals */
257defc1075Splunky 	done = 0;
258defc1075Splunky 	(void)signal(SIGHUP, sighandler);
259defc1075Splunky 	(void)signal(SIGINT, sighandler);
260defc1075Splunky 	(void)signal(SIGPIPE, sighandler);
261defc1075Splunky 	(void)signal(SIGTERM, sighandler);
262a5c89047Sgdamore 
263defc1075Splunky 	openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);
264defc1075Splunky 	syslog(LOG_INFO, "Starting on %s...", (tty ? tty : "stdio"));
265a5c89047Sgdamore 
266bc12f975Schristos 	pfd[0].fd = tty_in;
267bc12f975Schristos 	pfd[1].fd = rfcomm;
268bc12f975Schristos 	pfd[0].events = POLLIN|POLLRDNORM;
269bc12f975Schristos 	pfd[1].events = POLLIN|POLLRDNORM;
270defc1075Splunky 
271bc12f975Schristos 	while (!done) {
272bc12f975Schristos 		if (poll(pfd, 2, INFTIM) == -1) {
273a5c89047Sgdamore 			if (errno == EINTR)
274a5c89047Sgdamore 				continue;
275a5c89047Sgdamore 
276bc12f975Schristos 			syslog(LOG_ERR, "poll error: %m");
277a5c89047Sgdamore 		}
278bc12f975Schristos 		if (pfd[0].revents & (POLLIN|POLLRDNORM))
279defc1075Splunky 			copy_data(tty_in, rfcomm);
280a5c89047Sgdamore 
281bc12f975Schristos 		if (pfd[1].revents & (POLLIN|POLLRDNORM))
282defc1075Splunky 			copy_data(rfcomm, tty_out);
283a5c89047Sgdamore 	}
284a5c89047Sgdamore 
285defc1075Splunky 	syslog(LOG_INFO, "Completed on %s", (tty ? tty : "stdio"));
286bc12f975Schristos 	return EXIT_SUCCESS;
287a5c89047Sgdamore }
288a5c89047Sgdamore 
289bc12f975Schristos static int
open_tty(const char * tty)290defc1075Splunky open_tty(const char *tty)
291a5c89047Sgdamore {
292a5c89047Sgdamore 	char		 pty[PATH_MAX], *slash;
293a5c89047Sgdamore 	struct group	*gr = NULL;
294a5c89047Sgdamore 	gid_t		 ttygid;
295defc1075Splunky 	int		 master;
296a5c89047Sgdamore 
297a5c89047Sgdamore 	/*
298f6e9fe77Swiz 	 * Construct master PTY name. The slave tty name must be less than
299a5c89047Sgdamore 	 * PATH_MAX characters in length, must contain '/' character and
300a5c89047Sgdamore 	 * must not end with '/'.
301a5c89047Sgdamore 	 */
302bc12f975Schristos 	if (strlcpy(pty, tty, sizeof(pty)) >= sizeof(pty))
303bc12f975Schristos 		errx(EXIT_FAILURE, "Tty name too long `%s'", tty);
304a5c89047Sgdamore 
305a5c89047Sgdamore 	slash = strrchr(pty, '/');
306defc1075Splunky 	if (slash == NULL || slash[1] == '\0')
307bc12f975Schristos 		errx(EXIT_FAILURE, "Invalid tty `%s'", tty);
308a5c89047Sgdamore 
309a5c89047Sgdamore 	slash[1] = 'p';
310defc1075Splunky 	if (strcmp(pty, tty) == 0)
311bc12f975Schristos 		errx(EXIT_FAILURE, "Master and slave tty are the same (%s)",
312bc12f975Schristos 		    tty);
313a5c89047Sgdamore 
314bc12f975Schristos 	if ((master = open(pty, O_RDWR)) == -1)
315bc12f975Schristos 		err(EXIT_FAILURE, "Cannot open `%s'", pty);
316a5c89047Sgdamore 
317a5c89047Sgdamore 	/*
318a5c89047Sgdamore 	 * Slave TTY
319a5c89047Sgdamore 	 */
320a5c89047Sgdamore 	if ((gr = getgrnam("tty")) != NULL)
321a5c89047Sgdamore 		ttygid = gr->gr_gid;
322a5c89047Sgdamore 	else
323a5c89047Sgdamore 		ttygid = (gid_t)-1;
324a5c89047Sgdamore 
325bc12f975Schristos 	if (chown(tty, getuid(), ttygid) == -1)
326bc12f975Schristos 		err(EXIT_FAILURE, "Cannot chown `%s'", pty);
327bc12f975Schristos 	if (chmod(tty, S_IRUSR | S_IWUSR | S_IWGRP) == -1)
328bc12f975Schristos 		err(EXIT_FAILURE, "Cannot chmod `%s'", pty);
329bc12f975Schristos 	if (revoke(tty) == -1)
330bc12f975Schristos 		err(EXIT_FAILURE, "Cannot revoke `%s'", pty);
331a5c89047Sgdamore 
332defc1075Splunky 	return master;
333defc1075Splunky }
334defc1075Splunky 
335bc12f975Schristos static int
open_client(bdaddr_t * laddr,bdaddr_t * raddr,int lm,uintmax_t psm,const char * service)336bc12f975Schristos open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, uintmax_t psm,
337bc12f975Schristos     const char *service)
338defc1075Splunky {
339defc1075Splunky 	struct sockaddr_bt sa;
340bc12f975Schristos 	const struct service *s;
341defc1075Splunky 	struct linger l;
342d7e75bccSplunky 	char *ep;
343bc12f975Schristos 	int fd;
344a0922929Splunky 	uintmax_t channel;
345d7e75bccSplunky 
346d7e75bccSplunky 	for (s = services ; ; s++) {
347d7e75bccSplunky 		if (s->name == NULL) {
348bc12f975Schristos 			errno = 0;
349ca0b33b2Splunky 			channel = strtoul(service, &ep, 10);
350bc12f975Schristos 			if (service == ep || *ep != '\0')
351bc12f975Schristos 				errx(EXIT_FAILURE, "Unknown service `%s'",
352bc12f975Schristos 				    service);
353bc12f975Schristos 			if (channel == ULONG_MAX && errno == ERANGE)
354bc12f975Schristos 				err(EXIT_FAILURE, "Service `%s'",
355bc12f975Schristos 				    service);
356d7e75bccSplunky 
357d7e75bccSplunky 			break;
358d7e75bccSplunky 		}
359d7e75bccSplunky 
360d7e75bccSplunky 		if (strcasecmp(s->name, service) == 0) {
361bc12f975Schristos 			if (service_search(laddr, raddr, s->class, &psm,
362bc12f975Schristos 			    &channel) == -1)
363bc12f975Schristos 				err(EXIT_FAILURE, "%s", s->name);
364d7e75bccSplunky 
365d7e75bccSplunky 			break;
366d7e75bccSplunky 		}
367d7e75bccSplunky 	}
368defc1075Splunky 
369a0922929Splunky 	if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
370a0922929Splunky 		errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel);
371a0922929Splunky 
37248b6f8e4Splunky 	if (L2CAP_PSM_INVALID(psm))
37348b6f8e4Splunky 		errx(EXIT_FAILURE, "Invalid PSM 0x%04"PRIxMAX, psm);
37448b6f8e4Splunky 
375defc1075Splunky 	memset(&sa, 0, sizeof(sa));
376defc1075Splunky 	sa.bt_len = sizeof(sa);
377defc1075Splunky 	sa.bt_family = AF_BLUETOOTH;
378defc1075Splunky 	bdaddr_copy(&sa.bt_bdaddr, laddr);
379defc1075Splunky 
380defc1075Splunky 	fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
381bc12f975Schristos 	if (fd == -1)
382defc1075Splunky 		err(EXIT_FAILURE, "socket()");
383defc1075Splunky 
384bc12f975Schristos 	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
385defc1075Splunky 		err(EXIT_FAILURE, "bind(%s)", bt_ntoa(laddr, NULL));
386defc1075Splunky 
387defc1075Splunky 	memset(&l, 0, sizeof(l));
388defc1075Splunky 	l.l_onoff = 1;
389defc1075Splunky 	l.l_linger = 5;
390bc12f975Schristos 	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
391defc1075Splunky 		err(EXIT_FAILURE, "linger()");
392defc1075Splunky 
393bc12f975Schristos 	if (setsockopt(fd, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) == -1)
394f5db72e7Splunky 		err(EXIT_FAILURE, "link mode");
395f5db72e7Splunky 
39648b6f8e4Splunky 	sa.bt_psm = psm;
397defc1075Splunky 	sa.bt_channel = channel;
398defc1075Splunky 	bdaddr_copy(&sa.bt_bdaddr, raddr);
399defc1075Splunky 
400bc12f975Schristos 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
40148b6f8e4Splunky 		err(EXIT_FAILURE, "connect(%s, 0x%04"PRIxMAX", %"PRIuMAX")",
40248b6f8e4Splunky 		    bt_ntoa(raddr, NULL), psm, channel);
403defc1075Splunky 
404defc1075Splunky 	return fd;
405a5c89047Sgdamore }
406a5c89047Sgdamore 
407bc12f975Schristos static int
open_server(bdaddr_t * laddr,uint16_t psm,uint8_t channel,int lm,const char * service)408bc12f975Schristos open_server(bdaddr_t *laddr, uint16_t psm, uint8_t channel, int lm,
409bc12f975Schristos     const char *service)
410a5c89047Sgdamore {
411a0922929Splunky 	uint8_t	buffer[256];
412defc1075Splunky 	struct sockaddr_bt sa;
413bc12f975Schristos 	const struct service *s;
414defc1075Splunky 	struct linger l;
415defc1075Splunky 	socklen_t len;
416a0922929Splunky 	sdp_session_t ss;
417a0922929Splunky 	sdp_data_t rec;
418a0922929Splunky 	int sv, fd;
419a5c89047Sgdamore 
420a0922929Splunky 	for (s = services; ; s++) {
421a0922929Splunky 		if (s->name == NULL)
422a0922929Splunky 			usage();
423a5c89047Sgdamore 
424a0922929Splunky 		if (strcasecmp(s->name, service) == 0)
425a0922929Splunky 			break;
426a0922929Splunky 	}
427a0922929Splunky 
428a0922929Splunky 	/* Open server socket */
429defc1075Splunky 	sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
430bc12f975Schristos 	if (sv == -1)
431defc1075Splunky 		err(EXIT_FAILURE, "socket()");
432a5c89047Sgdamore 
433a0922929Splunky 	memset(&sa, 0, sizeof(sa));
434a0922929Splunky 	sa.bt_len = sizeof(sa);
435a0922929Splunky 	sa.bt_family = AF_BLUETOOTH;
43648b6f8e4Splunky 	sa.bt_psm = psm;
437a0922929Splunky 	sa.bt_channel = channel;
438a0922929Splunky 	bdaddr_copy(&sa.bt_bdaddr, laddr);
439bc12f975Schristos 	if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) == -1)
44048b6f8e4Splunky 		err(EXIT_FAILURE, "bind(%s, 0x%04x, %d)",
44148b6f8e4Splunky 		    bt_ntoa(laddr, NULL), psm, channel);
442a5c89047Sgdamore 
443bc12f975Schristos 	if (setsockopt(sv, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) == -1)
444f5db72e7Splunky 		err(EXIT_FAILURE, "link mode");
445f5db72e7Splunky 
446bc12f975Schristos 	if (listen(sv, 1) == -1)
447defc1075Splunky 		err(EXIT_FAILURE, "listen()");
448a5c89047Sgdamore 
449a4d96955Splunky 	len = sizeof(sa);
450bc12f975Schristos 	if (getsockname(sv, (struct sockaddr *)&sa, &len) == -1)
451a4d96955Splunky 		err(EXIT_FAILURE, "getsockname()");
452a4d96955Splunky 	if (len != sizeof(sa))
453a4d96955Splunky 		errx(EXIT_FAILURE, "getsockname()");
454a4d96955Splunky 
455a0922929Splunky 	/* Build SDP record */
456a0922929Splunky 	rec.next = buffer;
457a0922929Splunky 	rec.end = buffer + sizeof(buffer);
458a5c89047Sgdamore 
459a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE);
460a0922929Splunky 	sdp_put_uint32(&rec, 0x00000000);
461a0922929Splunky 
462a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_SERVICE_CLASS_ID_LIST);
463a0922929Splunky 	sdp_put_seq(&rec, 3);
464a0922929Splunky 	sdp_put_uuid16(&rec, s->class);
465a0922929Splunky 
46648b6f8e4Splunky 	len = (psm == L2CAP_PSM_RFCOMM ? 0 : 3);
46748b6f8e4Splunky 
468a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
46948b6f8e4Splunky 	sdp_put_seq(&rec, 12 + len);
47048b6f8e4Splunky 	sdp_put_seq(&rec, 3 + len);
471a0922929Splunky 	sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_L2CAP);
47248b6f8e4Splunky 	if (len > 0)
47348b6f8e4Splunky 		sdp_put_uint16(&rec, psm);
474a0922929Splunky 	sdp_put_seq(&rec, 5);
475a0922929Splunky 	sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_RFCOMM);
476a4d96955Splunky 	sdp_put_uint8(&rec, sa.bt_channel);
477a0922929Splunky 
478a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_BROWSE_GROUP_LIST);
479a0922929Splunky 	sdp_put_seq(&rec, 3);
480a0922929Splunky 	sdp_put_uuid16(&rec, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
481a0922929Splunky 
482a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
483a0922929Splunky 	sdp_put_seq(&rec, 9);
484a0922929Splunky 	sdp_put_uint16(&rec, 0x656e);	/* "en" */
485a0922929Splunky 	sdp_put_uint16(&rec, 106);	/* UTF-8 */
486a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
487a0922929Splunky 
488a0922929Splunky 	if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
489a0922929Splunky 		sdp_put_uint16(&rec, SDP_ATTR_SERVICE_AVAILABILITY);
490a0922929Splunky 		sdp_put_uint8(&rec, 0x00);
491a5c89047Sgdamore 	}
492defc1075Splunky 
493a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
494a0922929Splunky 	sdp_put_seq(&rec, 8);
495a0922929Splunky 	sdp_put_seq(&rec, 6);
496a0922929Splunky 	sdp_put_uuid16(&rec, s->class);
497a0922929Splunky 	sdp_put_uint16(&rec, 0x0100);	/* v1.0 */
498defc1075Splunky 
499a0922929Splunky 	sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
500a0922929Splunky 	    + SDP_ATTR_SERVICE_NAME_OFFSET);
501a0922929Splunky 	sdp_put_str(&rec, s->description, -1);
502a0922929Splunky 
503a0922929Splunky 	if (s->class == SDP_SERVICE_CLASS_DIALUP_NETWORKING) {
504a0922929Splunky 		sdp_put_uint16(&rec, SDP_ATTR_AUDIO_FEEDBACK_SUPPORT);
505a0922929Splunky 		sdp_put_bool(&rec, false);
506a0922929Splunky 	}
507a0922929Splunky 
508a0922929Splunky #if 0
509a0922929Splunky 	if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
510a0922929Splunky 		sdp_put_uint16(&rec, SDP_ATTR_IP_SUBNET);	/* TODO */
511a0922929Splunky 		sdp_put_str(&rec, "0.0.0.0/0", -1);
512a0922929Splunky 	}
513a0922929Splunky #endif
514a0922929Splunky 
515a0922929Splunky 	rec.end = rec.next;
516a0922929Splunky 	rec.next = buffer;
517a0922929Splunky 
518a0922929Splunky 	/* Register service with SDP server */
519defc1075Splunky 	ss = sdp_open_local(NULL);
520a0922929Splunky 	if (ss == NULL)
521defc1075Splunky 		err(EXIT_FAILURE, "sdp_open_local");
522defc1075Splunky 
523a0922929Splunky 	if (!sdp_record_insert(ss, laddr, NULL, &rec))
524a0922929Splunky 		err(EXIT_FAILURE, "sdp_record_insert");
525a5c89047Sgdamore 
526a0922929Splunky 	/* Accept client connection */
527defc1075Splunky 	len = sizeof(sa);
528defc1075Splunky 	fd = accept(sv, (struct sockaddr *)&sa, &len);
529bc12f975Schristos 	if (fd == -1)
530defc1075Splunky 		err(EXIT_FAILURE, "accept");
531a5c89047Sgdamore 
532defc1075Splunky 	memset(&l, 0, sizeof(l));
533defc1075Splunky 	l.l_onoff = 1;
534defc1075Splunky 	l.l_linger = 5;
535bc12f975Schristos 	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
536defc1075Splunky 		err(EXIT_FAILURE, "linger()");
537defc1075Splunky 
538defc1075Splunky 	close(sv);
539defc1075Splunky 	return fd;
540defc1075Splunky }
541defc1075Splunky 
542bc12f975Schristos static void
copy_data(int src,int dst)543defc1075Splunky copy_data(int src, int dst)
544a5c89047Sgdamore {
545defc1075Splunky 	static char	buf[BUFSIZ];
546defc1075Splunky 	ssize_t		nr, nw, off;
547a5c89047Sgdamore 
548defc1075Splunky 	while ((nr = read(src, buf, sizeof(buf))) == -1) {
549defc1075Splunky 		if (errno != EINTR) {
550defc1075Splunky 			syslog(LOG_ERR, "read failed: %m");
551defc1075Splunky 			exit(EXIT_FAILURE);
552defc1075Splunky 		}
553defc1075Splunky 	}
554defc1075Splunky 
555d7e75bccSplunky 	if (nr == 0)	/* reached EOF */
556d7e75bccSplunky 		done++;
557d7e75bccSplunky 
558defc1075Splunky 	for (off = 0 ; nr ; nr -= nw, off += nw) {
559defc1075Splunky 		if ((nw = write(dst, buf + off, (size_t)nr)) == -1) {
560defc1075Splunky 			syslog(LOG_ERR, "write failed: %m");
561defc1075Splunky 			exit(EXIT_FAILURE);
562defc1075Splunky 		}
563defc1075Splunky 	}
564defc1075Splunky }
565defc1075Splunky 
566bc12f975Schristos static int
service_search(bdaddr_t const * laddr,bdaddr_t const * raddr,uint16_t class,uintmax_t * psm,uintmax_t * channel)56748b6f8e4Splunky service_search(bdaddr_t const *laddr, bdaddr_t const *raddr,
56848b6f8e4Splunky     uint16_t class, uintmax_t *psm, uintmax_t *channel)
569a0922929Splunky {
570a0922929Splunky 	uint8_t		buffer[6];	/* SSP (3 bytes) + AIL (3 bytes) */
571a0922929Splunky 	sdp_session_t	ss;
572a0922929Splunky 	sdp_data_t	ail, ssp, rsp, rec, value, pdl, seq;
573a0922929Splunky 	uint16_t	attr;
574a0922929Splunky 	bool		rv;
575a0922929Splunky 
576a0922929Splunky 	seq.next = buffer;
577a0922929Splunky 	seq.end = buffer + sizeof(buffer);
578a0922929Splunky 
579a0922929Splunky 	/*
580a0922929Splunky 	 * build ServiceSearchPattern (3 bytes)
581a0922929Splunky 	 */
582a0922929Splunky 	ssp.next = seq.next;
583a0922929Splunky 	sdp_put_uuid16(&seq, class);
584a0922929Splunky 	ssp.end = seq.next;
585a0922929Splunky 
586a0922929Splunky 	/*
587a0922929Splunky 	 * build AttributeIDList (3 bytes)
588a0922929Splunky 	 */
589a0922929Splunky 	ail.next = seq.next;
590a0922929Splunky 	sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
591a0922929Splunky 	ail.end = seq.next;
592a0922929Splunky 
593a0922929Splunky 	ss = sdp_open(laddr, raddr);
594a0922929Splunky 	if (ss == NULL)
595bc12f975Schristos 		return -1;
596a0922929Splunky 
597a0922929Splunky 	rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
598a0922929Splunky 	if (!rv) {
599a0922929Splunky 		sdp_close(ss);
600bc12f975Schristos 		return -1;
601a0922929Splunky 	}
602a0922929Splunky 
603a0922929Splunky 	/*
604a0922929Splunky 	 * The response will be a list of records that matched our
605a0922929Splunky 	 * ServiceSearchPattern, where each record is a sequence
606a0922929Splunky 	 * containing a single ProtocolDescriptorList attribute and
607a0922929Splunky 	 * value
608a0922929Splunky 	 *
609a0922929Splunky 	 *	seq
610a0922929Splunky 	 *	  uint16	ProtocolDescriptorList
611a0922929Splunky 	 *	  value
612a0922929Splunky 	 *	seq
613a0922929Splunky 	 *	  uint16	ProtocolDescriptorList
614a0922929Splunky 	 *	  value
615a0922929Splunky 	 *
616a0922929Splunky 	 * If the ProtocolDescriptorList describes a single stack,
617a0922929Splunky 	 * the attribute value takes the form of a single Data Element
618a0922929Splunky 	 * Sequence where each member is a protocol descriptor.
619a0922929Splunky 	 *
620a0922929Splunky 	 *	seq
621a0922929Splunky 	 *	  list
622a0922929Splunky 	 *
623a0922929Splunky 	 * If it is possible for more than one kind of protocol
624a0922929Splunky 	 * stack to be used to gain access to the service, the
625a0922929Splunky 	 * ProtocolDescriptorList takes the form of a Data Element
626a0922929Splunky 	 * Alternative where each member is a Data Element Sequence
627a0922929Splunky 	 * describing an alternative protocol stack.
628a0922929Splunky 	 *
629a0922929Splunky 	 *	alt
630a0922929Splunky 	 *	  seq
631a0922929Splunky 	 *	    list
632a0922929Splunky 	 *	  seq
633a0922929Splunky 	 *	    list
634a0922929Splunky 	 *
635a0922929Splunky 	 * Each protocol stack description contains a sequence for each
636a0922929Splunky 	 * protocol, where each sequence contains the protocol UUID as
637a0922929Splunky 	 * the first element, and any ProtocolSpecificParameters. We are
63848b6f8e4Splunky 	 * interested in the L2CAP psm if provided, and the RFCOMM channel
63948b6f8e4Splunky 	 * number, stored as parameter#1 in each case.
640a0922929Splunky 	 *
641a0922929Splunky 	 *	seq
642a0922929Splunky 	 *	  uuid		L2CAP
643a0922929Splunky 	 *	  uint16	psm
644a0922929Splunky 	 *	seq
645a0922929Splunky 	 *	  uuid		RFCOMM
646a0922929Splunky 	 *	  uint8		channel
647a0922929Splunky 	 */
648a0922929Splunky 
649a0922929Splunky 	rv = false;
650a0922929Splunky 	while (!rv && sdp_get_seq(&rsp, &rec)) {
651a0922929Splunky 		if (!sdp_get_attr(&rec, &attr, &value)
652a0922929Splunky 		    || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
653a0922929Splunky 			continue;
654a0922929Splunky 
655a0922929Splunky 		sdp_get_alt(&value, &value);	/* strip any alt container */
656a0922929Splunky 		while (!rv && sdp_get_seq(&value, &pdl)) {
65748b6f8e4Splunky 			*psm = L2CAP_PSM_RFCOMM;
658a0922929Splunky 			if (sdp_get_seq(&pdl, &seq)
659a0922929Splunky 			    && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
66048b6f8e4Splunky 			    && (sdp_get_uint(&seq, psm) || true)
661a0922929Splunky 			    && sdp_get_seq(&pdl, &seq)
662a0922929Splunky 			    && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM)
663a0922929Splunky 			    && sdp_get_uint(&seq, channel))
664a0922929Splunky 				rv = true;
665a0922929Splunky 		}
666a0922929Splunky 	}
667a0922929Splunky 
668a0922929Splunky 	sdp_close(ss);
669bc12f975Schristos 	if (rv)
670bc12f975Schristos 		return 0;
671bc12f975Schristos 	errno = ENOATTR;
672bc12f975Schristos 	return -1;
673a0922929Splunky }
674a0922929Splunky 
675bc12f975Schristos static void
sighandler(int s)676defc1075Splunky sighandler(int s)
677defc1075Splunky {
678defc1075Splunky 
679defc1075Splunky 	done++;
680defc1075Splunky }
681defc1075Splunky 
682bc12f975Schristos static void
reset_tio(void)683defc1075Splunky reset_tio(void)
684defc1075Splunky {
685defc1075Splunky 
686defc1075Splunky 	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
687defc1075Splunky }
688defc1075Splunky 
689bc12f975Schristos static void
usage(void)690a5c89047Sgdamore usage(void)
691a5c89047Sgdamore {
692f5db72e7Splunky 	const char *cmd = getprogname();
693bc12f975Schristos 	const struct service *s;
694defc1075Splunky 
695bc12f975Schristos 	fprintf(stderr, "Usage: %s [-d device] [-m mode] [-p psm] [-s service]"
696bc12f975Schristos 	    " [-t tty]\n"
697a4d96955Splunky 	    "       %*s {-a bdaddr | [-c channel]}\n"
698defc1075Splunky 	    "\n"
699defc1075Splunky 	    "Where:\n"
700defc1075Splunky 	    "\t-a bdaddr    remote device address\n"
701d7e75bccSplunky 	    "\t-c channel   local RFCOMM channel\n"
702defc1075Splunky 	    "\t-d device    local device address\n"
703f5db72e7Splunky 	    "\t-m mode      link mode\n"
70448b6f8e4Splunky 	    "\t-p psm       protocol/service multiplexer\n"
705d7e75bccSplunky 	    "\t-s service   service class\n"
706defc1075Splunky 	    "\t-t tty       run in background using pty\n"
707858e9242Sdsl 	    "\n", cmd, (int)strlen(cmd), "");
708defc1075Splunky 
709d7e75bccSplunky 	fprintf(stderr, "Known service classes:\n");
710defc1075Splunky 	for (s = services ; s->name != NULL ; s++)
711d7e75bccSplunky 		fprintf(stderr, "\t%-13s%s\n", s->name, s->description);
712a5c89047Sgdamore 
713a5c89047Sgdamore 	exit(EXIT_FAILURE);
714defc1075Splunky }
715