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