xref: /netbsd-src/usr.sbin/btpand/client.c (revision c61317e3bf5092d043ad515e3631c97eeba79180)
1 /*	$NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008-2009 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 __RCSID("$NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $");
30 
31 #include <bluetooth.h>
32 #include <errno.h>
33 #include <sdp.h>
34 #include <unistd.h>
35 
36 #include "btpand.h"
37 #include "bnep.h"
38 
39 __dead static void client_down(channel_t *);
40 static void client_query(void);
41 
42 void
client_init(void)43 client_init(void)
44 {
45 	struct sockaddr_bt sa;
46 	channel_t *chan;
47 	socklen_t len;
48 	int fd, n;
49 	uint16_t mru, mtu;
50 
51 	if (bdaddr_any(&remote_bdaddr))
52 		return;
53 
54 	if (service_type)
55 		client_query();
56 
57 	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
58 	if (fd == -1) {
59 		log_err("Could not open L2CAP socket: %m");
60 		exit(EXIT_FAILURE);
61 	}
62 
63 	memset(&sa, 0, sizeof(sa));
64 	sa.bt_family = AF_BLUETOOTH;
65 	sa.bt_len = sizeof(sa);
66 	bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
67 	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
68 		log_err("Could not bind client socket: %m");
69 		exit(EXIT_FAILURE);
70 	}
71 
72 	if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM,
73 	    &l2cap_mode, sizeof(l2cap_mode)) == -1) {
74 		log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
75 		exit(EXIT_FAILURE);
76 	}
77 
78 	mru = BNEP_MTU_MIN;
79 	if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU,
80 	    &mru, sizeof(mru)) == -1) {
81 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
82 		exit(EXIT_FAILURE);
83 	}
84 
85 	log_info("Opening connection to service 0x%4.4x at %s",
86 	    service_class, bt_ntoa(&remote_bdaddr, NULL));
87 
88 	sa.bt_psm = l2cap_psm;
89 	bdaddr_copy(&sa.bt_bdaddr, &remote_bdaddr);
90 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
91 		log_err("Could not connect: %m");
92 		exit(EXIT_FAILURE);
93 	}
94 
95 	len = sizeof(mru);
96 	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
97 		log_err("Could not get IMTU: %m");
98 		exit(EXIT_FAILURE);
99 	}
100 	if (mru < BNEP_MTU_MIN) {
101 		log_err("L2CAP IMTU too small (%d)", mru);
102 		exit(EXIT_FAILURE);
103 	}
104 
105 	len = sizeof(n);
106 	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
107 		log_err("Could not read SO_RCVBUF");
108 		exit(EXIT_FAILURE);
109 	}
110 	if (n < 10 * mru) {
111 		n = 10 * mru;
112 		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
113 			log_info("Could not increase SO_RCVBUF (to %d)", n);
114 	}
115 
116 	len = sizeof(mtu);
117 	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
118 		log_err("Could not get L2CAP OMTU: %m");
119 		exit(EXIT_FAILURE);
120 	}
121 	if (mtu < BNEP_MTU_MIN) {
122 		log_err("L2CAP OMTU too small (%d)", mtu);
123 		exit(EXIT_FAILURE);
124 	}
125 
126 	len = sizeof(n);
127 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
128 		log_err("Could not get socket send buffer size: %m");
129 		close(fd);
130 		return;
131 	}
132 	if (n < (mtu * 2)) {
133 		n = mtu * 2;
134 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
135 			log_err("Could not set socket send buffer size (%d): %m", n);
136 			close(fd);
137 			return;
138 		}
139 	}
140 	n = mtu;
141 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
142 		log_err("Could not set socket low water mark (%d): %m", n);
143 		close(fd);
144 		return;
145 	}
146 
147 	chan = channel_alloc();
148 	if (chan == NULL)
149 		exit(EXIT_FAILURE);
150 
151 	chan->send = bnep_send;
152 	chan->recv = bnep_recv;
153 	chan->down = client_down;
154 	chan->mru = mru;
155 	chan->mtu = mtu;
156 	b2eaddr(chan->raddr, &remote_bdaddr);
157 	b2eaddr(chan->laddr, &local_bdaddr);
158 	chan->state = CHANNEL_WAIT_CONNECT_RSP;
159 	channel_timeout(chan, 10);
160 	if (!channel_open(chan, fd))
161 		exit(EXIT_FAILURE);
162 
163 	bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
164 	    2, service_class, SDP_SERVICE_CLASS_PANU);
165 }
166 
167 static void
client_down(channel_t * chan)168 client_down(channel_t *chan)
169 {
170 
171 	log_err("Client connection shut down, exiting");
172 	exit(EXIT_FAILURE);
173 }
174 
175 static void
client_query(void)176 client_query(void)
177 {
178 	uint8_t buf[12];	/* enough for SSP and AIL both */
179 	sdp_session_t ss;
180 	sdp_data_t ssp, ail, rsp, rec, value, pdl, seq;
181 	uintmax_t psm;
182 	uint16_t attr;
183 	bool rv;
184 
185 	ss = sdp_open(&local_bdaddr, &remote_bdaddr);
186 	if (ss == NULL) {
187 		log_err("%s: %m", service_type);
188 		exit(EXIT_FAILURE);
189 	}
190 
191 	log_info("Searching for %s service at %s",
192 	    service_type, bt_ntoa(&remote_bdaddr, NULL));
193 
194 	seq.next = buf;
195 	seq.end = buf + sizeof(buf);
196 
197 	/*
198 	 * build ServiceSearchPattern (9 bytes)
199 	 *
200 	 *	uuid16	"service_class"
201 	 *	uuid16	L2CAP
202 	 *	uuid16	BNEP
203 	 */
204 	ssp.next = seq.next;
205 	sdp_put_uuid16(&seq, service_class);
206 	sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP);
207 	sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP);
208 	ssp.end = seq.next;
209 
210 	/*
211 	 * build AttributeIDList (3 bytes)
212 	 *
213 	 *	uint16	ProtocolDescriptorList
214 	 */
215 	ail.next = seq.next;
216 	sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
217 	ail.end = seq.next;
218 
219 	rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
220 	if (!rv) {
221 		log_err("%s: %m", service_type);
222 		exit(EXIT_FAILURE);
223 	}
224 
225 	/*
226 	 * we expect the response to contain a list of records
227 	 * containing a ProtocolDescriptorList. Find the first
228 	 * one containing L2CAP and BNEP protocols and extract
229 	 * the PSM.
230 	 */
231 	rv = false;
232 	while (!rv && sdp_get_seq(&rsp, &rec)) {
233 		if (!sdp_get_attr(&rec, &attr, &value)
234 		    || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
235 			continue;
236 
237 		sdp_get_alt(&value, &value);	/* drop any alt header */
238 		while (!rv && sdp_get_seq(&value, &pdl)) {
239 			if (sdp_get_seq(&pdl, &seq)
240 			    && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
241 			    && sdp_get_uint(&seq, &psm)
242 			    && sdp_get_seq(&pdl, &seq)
243 			    && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP))
244 				rv = true;
245 		}
246 	}
247 
248 	sdp_close(ss);
249 
250 	if (!rv) {
251 		log_err("%s query failed", service_type);
252 		exit(EXIT_FAILURE);
253 	}
254 
255 	l2cap_psm = (uint16_t)psm;
256 	log_info("Found PSM %u for service %s", l2cap_psm, service_type);
257 }
258