xref: /freebsd/usr.sbin/bluetooth/btpand/client.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*	$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 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 /* $FreeBSD$ */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $");
34 
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <errno.h>
38 #include <sdp.h>
39 #include <unistd.h>
40 
41 #include "btpand.h"
42 #include "bnep.h"
43 #include "sdp.h"
44 
45 static void client_query(void);
46 
47 void
48 client_init(void)
49 {
50 	struct sockaddr_l2cap sa;
51 	channel_t *chan;
52 	socklen_t len;
53 	int fd, n;
54 	uint16_t mru, mtu;
55 
56 	if (bdaddr_any(&remote_bdaddr))
57 		return;
58 
59 	if (service_name)
60 		client_query();
61 
62 	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
63 	if (fd == -1) {
64 		log_err("Could not open L2CAP socket: %m");
65 		exit(EXIT_FAILURE);
66 	}
67 
68 	memset(&sa, 0, sizeof(sa));
69 	sa.l2cap_family = AF_BLUETOOTH;
70 	sa.l2cap_len = sizeof(sa);
71 	sa.l2cap_bdaddr_type = BDADDR_BREDR;
72 	sa.l2cap_cid = 0;
73 
74 	bdaddr_copy(&sa.l2cap_bdaddr, &local_bdaddr);
75 	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
76 		log_err("Could not bind client socket: %m");
77 		exit(EXIT_FAILURE);
78 	}
79 
80 	mru = BNEP_MTU_MIN;
81 	if (setsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
82 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
83 		exit(EXIT_FAILURE);
84 	}
85 
86 	log_info("Opening connection to service 0x%4.4x at %s",
87 	    service_class, bt_ntoa(&remote_bdaddr, NULL));
88 
89 	sa.l2cap_psm = htole16(l2cap_psm);
90 	bdaddr_copy(&sa.l2cap_bdaddr, &remote_bdaddr);
91 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
92 		log_err("Could not connect: %m");
93 		exit(EXIT_FAILURE);
94 	}
95 
96 	len = sizeof(mru);
97 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
98 		log_err("Could not get IMTU: %m");
99 		exit(EXIT_FAILURE);
100 	}
101 	if (mru < BNEP_MTU_MIN) {
102 		log_err("L2CAP IMTU too small (%d)", mru);
103 		exit(EXIT_FAILURE);
104 	}
105 
106 	len = sizeof(n);
107 	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
108 		log_err("Could not read SO_RCVBUF");
109 		exit(EXIT_FAILURE);
110 	}
111 	if (n < (mru * 10)) {
112 		n = mru * 10;
113 		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
114 			log_info("Could not increase SO_RCVBUF (from %d)", n);
115 	}
116 
117 	len = sizeof(mtu);
118 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
119 		log_err("Could not get L2CAP OMTU: %m");
120 		exit(EXIT_FAILURE);
121 	}
122 	if (mtu < BNEP_MTU_MIN) {
123 		log_err("L2CAP OMTU too small (%d)", mtu);
124 		exit(EXIT_FAILURE);
125 	}
126 
127 	len = sizeof(n);
128 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
129 		log_err("Could not get socket send buffer size: %m");
130 		close(fd);
131 		return;
132 	}
133 	if (n < (mtu * 2)) {
134 		n = mtu * 2;
135 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
136 			log_err("Could not set socket send buffer size (%d): %m", n);
137 			close(fd);
138 			return;
139 		}
140 	}
141 	n = mtu;
142 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
143 		log_err("Could not set socket low water mark (%d): %m", n);
144 		close(fd);
145 		return;
146 	}
147 
148 	chan = channel_alloc();
149 	if (chan == NULL)
150 		exit(EXIT_FAILURE);
151 
152 	chan->send = bnep_send;
153 	chan->recv = bnep_recv;
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
168 client_query(void)
169 {
170 	uint8_t buffer[512];
171 	sdp_attr_t attr;
172 	uint32_t range;
173 	void *ss;
174 	int rv;
175 	uint8_t *seq0, *seq1;
176 
177 	attr.flags = SDP_ATTR_INVALID;
178 	attr.attr = 0;
179 	attr.vlen = sizeof(buffer);
180 	attr.value = buffer;
181 
182 	range = SDP_ATTR_RANGE(SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
183 			       SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
184 
185 	ss = sdp_open(&local_bdaddr, &remote_bdaddr);
186 	if (ss == NULL || (errno = sdp_error(ss)) != 0) {
187 		log_err("%s: %m", service_name);
188 		exit(EXIT_FAILURE);
189 	}
190 
191 	log_info("Searching for %s service at %s",
192 	    service_name, bt_ntoa(&remote_bdaddr, NULL));
193 
194 	rv = sdp_search(ss, 1, &service_class, 1, &range, 1, &attr);
195 	if (rv != 0) {
196 		log_err("%s: %s", service_name, strerror(sdp_error(ss)));
197 		exit(EXIT_FAILURE);
198 	}
199 
200 	sdp_close(ss);
201 
202 	if (attr.flags != SDP_ATTR_OK
203 	    || attr.attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST) {
204 		log_err("%s service not found", service_name);
205 		exit(EXIT_FAILURE);
206 	}
207 
208 	/*
209 	 * we expect the following protocol descriptor list
210 	 *
211 	 *	seq len
212 	 *	  seq len
213 	 *	    uuid value == L2CAP
214 	 *	    uint16 value16 => PSM
215 	 *	  seq len
216 	 *	    uuid value == BNEP
217 	 */
218 	if (_sdp_get_seq(&attr.value, attr.value + attr.vlen, &seq0)
219 	    && _sdp_get_seq(&seq0, attr.value, &seq1)
220 	    && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_L2CAP)
221 	    && _sdp_get_uint16(&seq1, seq0, &l2cap_psm)
222 	    && _sdp_get_seq(&seq0, attr.value, &seq1)
223 	    && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_BNEP)) {
224 		log_info("Found PSM %d for service %s", l2cap_psm, service_name);
225 		return;
226 	}
227 
228 	log_err("%s query failed", service_name);
229 	exit(EXIT_FAILURE);
230 }
231