1124758Semax/*
2124758Semax * ssr.c
3124758Semax *
4124758Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5124758Semax * All rights reserved.
6124758Semax *
7124758Semax * Redistribution and use in source and binary forms, with or without
8124758Semax * modification, are permitted provided that the following conditions
9124758Semax * are met:
10124758Semax * 1. Redistributions of source code must retain the above copyright
11124758Semax *    notice, this list of conditions and the following disclaimer.
12124758Semax * 2. Redistributions in binary form must reproduce the above copyright
13124758Semax *    notice, this list of conditions and the following disclaimer in the
14124758Semax *    documentation and/or other materials provided with the distribution.
15124758Semax *
16124758Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17124758Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18124758Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19124758Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20124758Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21124758Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22124758Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23124758Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24124758Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25124758Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26124758Semax * SUCH DAMAGE.
27124758Semax *
28124758Semax * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
29124758Semax * $FreeBSD: releng/11.0/usr.sbin/bluetooth/sdpd/ssr.c 281210 2015-04-07 16:48:23Z takawata $
30124758Semax */
31124758Semax
32124758Semax#include <sys/queue.h>
33124758Semax#include <sys/uio.h>
34124758Semax#include <netinet/in.h>
35124758Semax#include <arpa/inet.h>
36124758Semax#include <assert.h>
37281210Stakawata#define L2CAP_SOCKET_CHECKED
38124758Semax#include <bluetooth.h>
39124758Semax#include <errno.h>
40124758Semax#include <sdp.h>
41124758Semax#include <string.h>
42124758Semax#include "profile.h"
43124758Semax#include "provider.h"
44124758Semax#include "server.h"
45139721Semax#include "uuid-private.h"
46124758Semax
47124758Semax/*
48124758Semax * Prepare SDP Service Search Response
49124758Semax */
50124758Semax
51124758Semaxint32_t
52124758Semaxserver_prepare_service_search_response(server_p srv, int32_t fd)
53124758Semax{
54124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
55124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
56124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
57124758Semax	uint8_t const	*rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
58124758Semax
59124758Semax	uint8_t		*ptr = NULL;
60124758Semax	provider_t	*provider = NULL;
61139721Semax	int32_t		 type, ssplen, rsp_limit, rcount, cslen, cs;
62139721Semax	uint128_t	 uuid, puuid;
63124758Semax
64124758Semax	/*
65124758Semax	 * Minimal SDP Service Search Request
66124758Semax	 *
67124758Semax	 * seq8 len8		- 2 bytes
68124758Semax	 *	uuid16 value16	- 3 bytes ServiceSearchPattern
69124758Semax	 * value16		- 2 bytes MaximumServiceRecordCount
70124758Semax	 * value8		- 1 byte  ContinuationState
71124758Semax	 */
72124758Semax
73124758Semax	if (req_end - req < 8)
74124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
75124758Semax
76124758Semax	/* Get size of ServiceSearchPattern */
77124758Semax	ssplen = 0;
78124758Semax	SDP_GET8(type, req);
79124758Semax	switch (type) {
80124758Semax	case SDP_DATA_SEQ8:
81124758Semax		SDP_GET8(ssplen, req);
82124758Semax		break;
83124758Semax
84124758Semax	case SDP_DATA_SEQ16:
85124758Semax		SDP_GET16(ssplen, req);
86124758Semax		break;
87124758Semax
88124758Semax	case SDP_DATA_SEQ32:
89124758Semax		SDP_GET32(ssplen, req);
90124758Semax		break;
91124758Semax	}
92124758Semax	if (ssplen <= 0)
93124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
94124758Semax
95124758Semax	ptr = (uint8_t *) req + ssplen;
96124758Semax
97124758Semax	/* Get MaximumServiceRecordCount */
98124758Semax	if (ptr + 2 > req_end)
99124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
100124758Semax
101124758Semax	SDP_GET16(rsp_limit, ptr);
102124758Semax	if (rsp_limit <= 0)
103124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
104124758Semax
105124758Semax	/* Get ContinuationState */
106124758Semax	if (ptr + 1 > req_end)
107124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
108124758Semax
109124758Semax	SDP_GET8(cslen, ptr);
110124758Semax	if (cslen != 0) {
111124758Semax		if (cslen != 2 || req_end - ptr != 2)
112124758Semax			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
113124758Semax
114124758Semax		SDP_GET16(cs, ptr);
115124758Semax	} else
116124758Semax		cs = 0;
117124758Semax
118124758Semax	/* Process the request. First, check continuation state */
119124758Semax	if (srv->fdidx[fd].rsp_cs != cs)
120124758Semax		return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
121124758Semax	if (srv->fdidx[fd].rsp_size > 0)
122124758Semax		return (0);
123124758Semax
124124758Semax	/*
125124758Semax	 * Service Search Response format
126124758Semax	 *
127124758Semax	 * value16	- 2 bytes TotalServiceRecordCount (not incl.)
128124758Semax	 * value16	- 2 bytes CurrentServiceRecordCount (not incl.)
129124758Semax	 * value32	- 4 bytes handle
130124758Semax	 * [ value32 ]
131124758Semax	 *
132124758Semax	 * Calculate how many record handles we can fit
133124758Semax	 * in our reply buffer and adjust rlimit.
134124758Semax	 */
135124758Semax
136124758Semax	ptr = rsp;
137124758Semax	rcount = (rsp_end - ptr) / 4;
138124758Semax	if (rcount < rsp_limit)
139124758Semax		rsp_limit = rcount;
140124758Semax
141124758Semax	/* Look for the record handles */
142124758Semax	for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
143124758Semax		SDP_GET8(type, req);
144124758Semax		ssplen --;
145124758Semax
146124758Semax		switch (type) {
147124758Semax		case SDP_DATA_UUID16:
148124758Semax			if (ssplen < 2)
149124758Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
150124758Semax
151139721Semax			memcpy(&uuid, &uuid_base, sizeof(uuid));
152139721Semax			uuid.b[2] = *req ++;
153139721Semax			uuid.b[3] = *req ++;
154124758Semax			ssplen -= 2;
155124758Semax			break;
156124758Semax
157139721Semax		case SDP_DATA_UUID32:
158139721Semax			if (ssplen < 4)
159139721Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
160139721Semax
161139721Semax			memcpy(&uuid, &uuid_base, sizeof(uuid));
162139721Semax			uuid.b[0] = *req ++;
163139721Semax			uuid.b[1] = *req ++;
164139721Semax			uuid.b[2] = *req ++;
165139721Semax			uuid.b[3] = *req ++;
166139721Semax			ssplen -= 4;
167139721Semax			break;
168139721Semax
169139721Semax		case SDP_DATA_UUID128:
170139721Semax			if (ssplen < 16)
171139721Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
172139721Semax
173139721Semax			memcpy(uuid.b, req, 16);
174139721Semax			req += 16;
175139721Semax			ssplen -= 16;
176139721Semax			break;
177139721Semax
178124758Semax		default:
179124758Semax			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
180124758Semax			/* NOT REACHED */
181124758Semax		}
182124758Semax
183124758Semax		for (provider = provider_get_first();
184124758Semax		     provider != NULL && rcount < rsp_limit;
185124758Semax		     provider = provider_get_next(provider)) {
186124758Semax			if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
187124758Semax				continue;
188124758Semax
189139721Semax			memcpy(&puuid, &uuid_base, sizeof(puuid));
190139721Semax			puuid.b[2] = provider->profile->uuid >> 8;
191139721Semax			puuid.b[3] = provider->profile->uuid;
192139721Semax
193139721Semax			if (memcmp(&uuid, &puuid, sizeof(uuid)) == 0 ||
194139721Semax			    memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) == 0) {
195124758Semax				SDP_PUT32(provider->handle, ptr);
196124758Semax				rcount ++;
197124758Semax			}
198124758Semax		}
199124758Semax	}
200124758Semax
201124758Semax	/* Set reply size (not counting PDU header and continuation state) */
202124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
203124758Semax	srv->fdidx[fd].rsp_size = ptr - rsp;
204124758Semax	srv->fdidx[fd].rsp_cs = 0;
205124758Semax
206124758Semax	return (0);
207124758Semax}
208124758Semax
209124758Semax/*
210124758Semax * Send SDP Service Search Response
211124758Semax */
212124758Semax
213124758Semaxint32_t
214124758Semaxserver_send_service_search_response(server_p srv, int32_t fd)
215124758Semax{
216124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
217124758Semax	uint8_t		*rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
218124758Semax
219124758Semax	struct iovec	iov[4];
220124758Semax	sdp_pdu_t	pdu;
221124758Semax	uint16_t	rcounts[2];
222124758Semax	uint8_t		cs[3];
223124758Semax	int32_t		size;
224124758Semax
225124758Semax	/* First update continuation state (assume we will send all data) */
226124758Semax	size = rsp_end - rsp;
227124758Semax	srv->fdidx[fd].rsp_cs += size;
228124758Semax
229124758Semax	if (size + 1 > srv->fdidx[fd].rsp_limit) {
230124758Semax		/*
231124758Semax		 * We need to split out response. Add 3 more bytes for the
232124758Semax		 * continuation state and move rsp_end and rsp_cs backwards.
233124758Semax		 */
234124758Semax
235124758Semax		while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
236124758Semax			rsp_end -= 4;
237124758Semax			srv->fdidx[fd].rsp_cs -= 4;
238124758Semax		}
239124758Semax
240124758Semax		cs[0] = 2;
241124758Semax		cs[1] = srv->fdidx[fd].rsp_cs >> 8;
242124758Semax		cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
243124758Semax	} else
244124758Semax		cs[0] = 0;
245124758Semax
246124758Semax	assert(rsp_end >= rsp);
247124758Semax
248124758Semax	rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
249124758Semax	rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
250124758Semax
251124758Semax	pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
252124758Semax	pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
253124758Semax	pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
254124758Semax
255126245Semax	rcounts[0] = htons(rcounts[0]);
256126245Semax	rcounts[1] = htons(rcounts[1]);
257126245Semax
258124758Semax	iov[0].iov_base = &pdu;
259124758Semax	iov[0].iov_len = sizeof(pdu);
260124758Semax
261124758Semax	iov[1].iov_base = rcounts;
262124758Semax	iov[1].iov_len = sizeof(rcounts);
263124758Semax
264124758Semax	iov[2].iov_base = rsp;
265124758Semax	iov[2].iov_len = rsp_end - rsp;
266124758Semax
267124758Semax	iov[3].iov_base = cs;
268124758Semax	iov[3].iov_len = 1 + cs[0];
269124758Semax
270124758Semax	do {
271124758Semax		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
272124758Semax	} while (size < 0 && errno == EINTR);
273124758Semax
274124758Semax	/* Check if we have sent (or failed to sent) last response chunk */
275124758Semax	if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
276124758Semax		srv->fdidx[fd].rsp_cs = 0;
277124758Semax		srv->fdidx[fd].rsp_size = 0;
278124758Semax		srv->fdidx[fd].rsp_limit = 0;
279124758Semax	}
280124758Semax
281124758Semax	return ((size < 0)? errno : 0);
282124758Semax}
283124758Semax
284