xref: /freebsd/usr.sbin/bluetooth/sdpd/ssr.c (revision c7d813a93eeb447470734c9bc0c140d90a54c271)
1 /*
2  * ssr.c
3  *
4  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/queue.h>
33 #include <sys/uio.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <assert.h>
37 #define L2CAP_SOCKET_CHECKED
38 #include <bluetooth.h>
39 #include <errno.h>
40 #include <sdp.h>
41 #include <string.h>
42 #include "profile.h"
43 #include "provider.h"
44 #include "server.h"
45 #include "uuid-private.h"
46 
47 /*
48  * Prepare SDP Service Search Response
49  */
50 
51 int32_t
52 server_prepare_service_search_response(server_p srv, int32_t fd)
53 {
54 	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
55 	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
56 	uint8_t		*rsp = srv->fdidx[fd].rsp;
57 	uint8_t const	*rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
58 
59 	uint8_t		*ptr = NULL;
60 	provider_t	*provider = NULL;
61 	int32_t		 type, ssplen, rsp_limit, rcount, cslen, cs;
62 	uint128_t	 uuid, puuid;
63 
64 	/*
65 	 * Minimal SDP Service Search Request
66 	 *
67 	 * seq8 len8		- 2 bytes
68 	 *	uuid16 value16	- 3 bytes ServiceSearchPattern
69 	 * value16		- 2 bytes MaximumServiceRecordCount
70 	 * value8		- 1 byte  ContinuationState
71 	 */
72 
73 	if (req_end - req < 8)
74 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
75 
76 	/* Get size of ServiceSearchPattern */
77 	ssplen = 0;
78 	SDP_GET8(type, req);
79 	switch (type) {
80 	case SDP_DATA_SEQ8:
81 		SDP_GET8(ssplen, req);
82 		break;
83 
84 	case SDP_DATA_SEQ16:
85 		SDP_GET16(ssplen, req);
86 		break;
87 
88 	case SDP_DATA_SEQ32:
89 		SDP_GET32(ssplen, req);
90 		break;
91 	}
92 	if (ssplen <= 0)
93 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
94 
95 	ptr = (uint8_t *) req + ssplen;
96 
97 	/* Get MaximumServiceRecordCount */
98 	if (ptr + 2 > req_end)
99 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
100 
101 	SDP_GET16(rsp_limit, ptr);
102 	if (rsp_limit <= 0)
103 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
104 
105 	/* Get ContinuationState */
106 	if (ptr + 1 > req_end)
107 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
108 
109 	SDP_GET8(cslen, ptr);
110 	if (cslen != 0) {
111 		if (cslen != 2 || req_end - ptr != 2)
112 			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
113 
114 		SDP_GET16(cs, ptr);
115 	} else
116 		cs = 0;
117 
118 	/* Process the request. First, check continuation state */
119 	if (srv->fdidx[fd].rsp_cs != cs)
120 		return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
121 	if (srv->fdidx[fd].rsp_size > 0)
122 		return (0);
123 
124 	/*
125 	 * Service Search Response format
126 	 *
127 	 * value16	- 2 bytes TotalServiceRecordCount (not incl.)
128 	 * value16	- 2 bytes CurrentServiceRecordCount (not incl.)
129 	 * value32	- 4 bytes handle
130 	 * [ value32 ]
131 	 *
132 	 * Calculate how many record handles we can fit
133 	 * in our reply buffer and adjust rlimit.
134 	 */
135 
136 	ptr = rsp;
137 	rcount = (rsp_end - ptr) / 4;
138 	if (rcount < rsp_limit)
139 		rsp_limit = rcount;
140 
141 	/* Look for the record handles */
142 	for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
143 		SDP_GET8(type, req);
144 		ssplen --;
145 
146 		switch (type) {
147 		case SDP_DATA_UUID16:
148 			if (ssplen < 2)
149 				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
150 
151 			memcpy(&uuid, &uuid_base, sizeof(uuid));
152 			uuid.b[2] = *req ++;
153 			uuid.b[3] = *req ++;
154 			ssplen -= 2;
155 			break;
156 
157 		case SDP_DATA_UUID32:
158 			if (ssplen < 4)
159 				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
160 
161 			memcpy(&uuid, &uuid_base, sizeof(uuid));
162 			uuid.b[0] = *req ++;
163 			uuid.b[1] = *req ++;
164 			uuid.b[2] = *req ++;
165 			uuid.b[3] = *req ++;
166 			ssplen -= 4;
167 			break;
168 
169 		case SDP_DATA_UUID128:
170 			if (ssplen < 16)
171 				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
172 
173 			memcpy(uuid.b, req, 16);
174 			req += 16;
175 			ssplen -= 16;
176 			break;
177 
178 		default:
179 			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
180 			/* NOT REACHED */
181 		}
182 
183 		for (provider = provider_get_first();
184 		     provider != NULL && rcount < rsp_limit;
185 		     provider = provider_get_next(provider)) {
186 			if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
187 				continue;
188 
189 			memcpy(&puuid, &uuid_base, sizeof(puuid));
190 			puuid.b[2] = provider->profile->uuid >> 8;
191 			puuid.b[3] = provider->profile->uuid;
192 
193 			if (memcmp(&uuid, &puuid, sizeof(uuid)) == 0 ||
194 			    memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) == 0) {
195 				SDP_PUT32(provider->handle, ptr);
196 				rcount ++;
197 			}
198 		}
199 	}
200 
201 	/* Set reply size (not counting PDU header and continuation state) */
202 	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
203 	srv->fdidx[fd].rsp_size = ptr - rsp;
204 	srv->fdidx[fd].rsp_cs = 0;
205 
206 	return (0);
207 }
208 
209 /*
210  * Send SDP Service Search Response
211  */
212 
213 int32_t
214 server_send_service_search_response(server_p srv, int32_t fd)
215 {
216 	uint8_t		*rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
217 	uint8_t		*rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
218 
219 	struct iovec	iov[4];
220 	sdp_pdu_t	pdu;
221 	uint16_t	rcounts[2];
222 	uint8_t		cs[3];
223 	int32_t		size;
224 
225 	/* First update continuation state (assume we will send all data) */
226 	size = rsp_end - rsp;
227 	srv->fdidx[fd].rsp_cs += size;
228 
229 	if (size + 1 > srv->fdidx[fd].rsp_limit) {
230 		/*
231 		 * We need to split out response. Add 3 more bytes for the
232 		 * continuation state and move rsp_end and rsp_cs backwards.
233 		 */
234 
235 		while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
236 			rsp_end -= 4;
237 			srv->fdidx[fd].rsp_cs -= 4;
238 		}
239 
240 		cs[0] = 2;
241 		cs[1] = srv->fdidx[fd].rsp_cs >> 8;
242 		cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
243 	} else
244 		cs[0] = 0;
245 
246 	assert(rsp_end >= rsp);
247 
248 	rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
249 	rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
250 
251 	pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
252 	pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
253 	pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
254 
255 	rcounts[0] = htons(rcounts[0]);
256 	rcounts[1] = htons(rcounts[1]);
257 
258 	iov[0].iov_base = &pdu;
259 	iov[0].iov_len = sizeof(pdu);
260 
261 	iov[1].iov_base = rcounts;
262 	iov[1].iov_len = sizeof(rcounts);
263 
264 	iov[2].iov_base = rsp;
265 	iov[2].iov_len = rsp_end - rsp;
266 
267 	iov[3].iov_base = cs;
268 	iov[3].iov_len = 1 + cs[0];
269 
270 	do {
271 		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
272 	} while (size < 0 && errno == EINTR);
273 
274 	/* Check if we have sent (or failed to sent) last response chunk */
275 	if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
276 		srv->fdidx[fd].rsp_cs = 0;
277 		srv->fdidx[fd].rsp_size = 0;
278 		srv->fdidx[fd].rsp_limit = 0;
279 	}
280 
281 	return ((size < 0)? errno : 0);
282 }
283 
284