OSDN Git Service

RESTRICT AUTOMERGE: Fixes two bluetooth bugs causing remote overreads (1/2)
[android-x86/system-bt.git] / stack / sdp / sdp_discovery.cc
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  this file contains SDP discovery functions
22  *
23  ******************************************************************************/
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "bt_common.h"
30 #include "bt_target.h"
31 #include "btm_api.h"
32 #include "btu.h"
33 #include "hcidefs.h"
34 #include "hcimsgs.h"
35 #include "l2cdefs.h"
36 #include "log/log.h"
37 #include "sdp_api.h"
38 #include "sdpint.h"
39
40 #ifndef SDP_DEBUG_RAW
41 #define SDP_DEBUG_RAW false
42 #endif
43
44 /******************************************************************************/
45 /*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
46 /******************************************************************************/
47 static void process_service_search_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
48                                        uint8_t* p_reply_end);
49 static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
50                                      uint8_t* p_reply_end);
51 static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
52                                             uint8_t* p_reply_end);
53 static uint8_t* save_attr_seq(tCONN_CB* p_ccb, uint8_t* p, uint8_t* p_msg_end);
54 static tSDP_DISC_REC* add_record(tSDP_DISCOVERY_DB* p_db, BD_ADDR p_bda);
55 static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db,
56                          tSDP_DISC_REC* p_rec, uint16_t attr_id,
57                          tSDP_DISC_ATTR* p_parent_attr, uint8_t nest_level);
58
59 /* Safety check in case we go crazy */
60 #define MAX_NEST_LEVELS 5
61
62 extern fixed_queue_t* btu_general_alarm_queue;
63
64 /*******************************************************************************
65  *
66  * Function         sdpu_build_uuid_seq
67  *
68  * Description      This function builds a UUID sequence from the list of
69  *                  passed UUIDs. It is also passed the address of the output
70  *                  buffer.
71  *
72  * Returns          Pointer to next byte in the output buffer.
73  *
74  ******************************************************************************/
75 static uint8_t* sdpu_build_uuid_seq(uint8_t* p_out, uint16_t num_uuids,
76                                     tSDP_UUID* p_uuid_list) {
77   uint16_t xx;
78   uint8_t* p_len;
79
80   /* First thing is the data element header */
81   UINT8_TO_BE_STREAM(p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
82
83   /* Remember where the length goes. Leave space for it. */
84   p_len = p_out;
85   p_out += 1;
86
87   /* Now, loop through and put in all the UUID(s) */
88   for (xx = 0; xx < num_uuids; xx++, p_uuid_list++) {
89     if (p_uuid_list->len == 2) {
90       UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
91       UINT16_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid16);
92     } else if (p_uuid_list->len == 4) {
93       UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
94       UINT32_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid32);
95     } else {
96       UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
97       ARRAY_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid128, p_uuid_list->len);
98     }
99   }
100
101   /* Now, put in the length */
102   xx = (uint16_t)(p_out - p_len - 1);
103   UINT8_TO_BE_STREAM(p_len, xx);
104
105   return (p_out);
106 }
107
108 /*******************************************************************************
109  *
110  * Function         sdp_snd_service_search_req
111  *
112  * Description      Send a service search request to the SDP server.
113  *
114  * Returns          void
115  *
116  ******************************************************************************/
117 static void sdp_snd_service_search_req(tCONN_CB* p_ccb, uint8_t cont_len,
118                                        uint8_t* p_cont) {
119   uint8_t *p, *p_start, *p_param_len;
120   BT_HDR* p_cmd = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
121   uint16_t param_len;
122
123   /* Prepare the buffer for sending the packet to L2CAP */
124   p_cmd->offset = L2CAP_MIN_OFFSET;
125   p = p_start = (uint8_t*)(p_cmd + 1) + L2CAP_MIN_OFFSET;
126
127   /* Build a service search request packet */
128   UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_SEARCH_REQ);
129   UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
130   p_ccb->transaction_id++;
131
132   /* Skip the length, we need to add it at the end */
133   p_param_len = p;
134   p += 2;
135
136 /* Build the UID sequence. */
137 #if (SDP_BROWSE_PLUS == TRUE)
138   p = sdpu_build_uuid_seq(p, 1,
139                           &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
140 #else
141   p = sdpu_build_uuid_seq(p, p_ccb->p_db->num_uuid_filters,
142                           p_ccb->p_db->uuid_filters);
143 #endif
144
145   /* Set max service record count */
146   UINT16_TO_BE_STREAM(p, sdp_cb.max_recs_per_search);
147
148   /* Set continuation state */
149   UINT8_TO_BE_STREAM(p, cont_len);
150
151   /* if this is not the first request */
152   if (cont_len && p_cont) {
153     memcpy(p, p_cont, cont_len);
154     p += cont_len;
155   }
156
157   /* Go back and put the parameter length into the buffer */
158   param_len = (uint16_t)(p - p_param_len - 2);
159   UINT16_TO_BE_STREAM(p_param_len, param_len);
160
161   p_ccb->disc_state = SDP_DISC_WAIT_HANDLES;
162
163   /* Set the length of the SDP data in the buffer */
164   p_cmd->len = (uint16_t)(p - p_start);
165
166 #if (SDP_DEBUG_RAW == TRUE)
167   SDP_TRACE_WARNING("sdp_snd_service_search_req cont_len :%d disc_state:%d",
168                     cont_len, p_ccb->disc_state);
169 #endif
170
171   L2CA_DataWrite(p_ccb->connection_id, p_cmd);
172
173   /* Start inactivity timer */
174   alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
175                      sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
176 }
177
178 /*******************************************************************************
179  *
180  * Function         sdp_disc_connected
181  *
182  * Description      This function is called when an SDP discovery attempt is
183  *                  connected.
184  *
185  * Returns          void
186  *
187  ******************************************************************************/
188 void sdp_disc_connected(tCONN_CB* p_ccb) {
189   if (p_ccb->is_attr_search) {
190     p_ccb->disc_state = SDP_DISC_WAIT_SEARCH_ATTR;
191
192     process_service_search_attr_rsp(p_ccb, NULL, NULL);
193   } else {
194     /* First step is to get a list of the handles from the server. */
195     /* We are not searching for a specific attribute, so we will   */
196     /* first search for the service, then get all attributes of it */
197
198     p_ccb->num_handles = 0;
199     sdp_snd_service_search_req(p_ccb, 0, NULL);
200   }
201 }
202
203 /*******************************************************************************
204  *
205  * Function         sdp_disc_server_rsp
206  *
207  * Description      This function is called when there is a response from
208  *                  the server.
209  *
210  * Returns          void
211  *
212  ******************************************************************************/
213 void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg) {
214   uint8_t *p, rsp_pdu;
215   bool invalid_pdu = true;
216
217 #if (SDP_DEBUG_RAW == TRUE)
218   SDP_TRACE_WARNING("sdp_disc_server_rsp disc_state:%d", p_ccb->disc_state);
219 #endif
220
221   /* stop inactivity timer when we receive a response */
222   alarm_cancel(p_ccb->sdp_conn_timer);
223
224   /* Got a reply!! Check what we got back */
225   p = (uint8_t*)(p_msg + 1) + p_msg->offset;
226   uint8_t* p_end = p + p_msg->len;
227
228   BE_STREAM_TO_UINT8(rsp_pdu, p);
229
230   p_msg->len--;
231
232   switch (rsp_pdu) {
233     case SDP_PDU_SERVICE_SEARCH_RSP:
234       if (p_ccb->disc_state == SDP_DISC_WAIT_HANDLES) {
235         process_service_search_rsp(p_ccb, p, p_end);
236         invalid_pdu = false;
237       }
238       break;
239
240     case SDP_PDU_SERVICE_ATTR_RSP:
241       if (p_ccb->disc_state == SDP_DISC_WAIT_ATTR) {
242         process_service_attr_rsp(p_ccb, p, p_end);
243         invalid_pdu = false;
244       }
245       break;
246
247     case SDP_PDU_SERVICE_SEARCH_ATTR_RSP:
248       if (p_ccb->disc_state == SDP_DISC_WAIT_SEARCH_ATTR) {
249         process_service_search_attr_rsp(p_ccb, p, p_end);
250         invalid_pdu = false;
251       }
252       break;
253   }
254
255   if (invalid_pdu) {
256     SDP_TRACE_WARNING("SDP - Unexp. PDU: %d in state: %d", rsp_pdu,
257                       p_ccb->disc_state);
258     sdp_disconnect(p_ccb, SDP_GENERIC_ERROR);
259   }
260 }
261
262 /******************************************************************************
263  *
264  * Function         process_service_search_rsp
265  *
266  * Description      This function is called when there is a search response from
267  *                  the server.
268  *
269  * Returns          void
270  *
271  ******************************************************************************/
272 static void process_service_search_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
273                                        uint8_t* p_reply_end) {
274   uint16_t xx;
275   uint16_t total, cur_handles, orig;
276   uint8_t cont_len;
277
278   /* Skip transaction, and param len */
279   p_reply += 4;
280   BE_STREAM_TO_UINT16(total, p_reply);
281   BE_STREAM_TO_UINT16(cur_handles, p_reply);
282
283   orig = p_ccb->num_handles;
284   p_ccb->num_handles += cur_handles;
285   if (p_ccb->num_handles == 0) {
286     SDP_TRACE_WARNING("SDP - Rcvd ServiceSearchRsp, no matches");
287     sdp_disconnect(p_ccb, SDP_NO_RECS_MATCH);
288     return;
289   }
290
291   /* Save the handles that match. We will can only process a certain number. */
292   if (total > sdp_cb.max_recs_per_search) total = sdp_cb.max_recs_per_search;
293   if (p_ccb->num_handles > sdp_cb.max_recs_per_search)
294     p_ccb->num_handles = sdp_cb.max_recs_per_search;
295
296   for (xx = orig; xx < p_ccb->num_handles; xx++)
297     BE_STREAM_TO_UINT32(p_ccb->handles[xx], p_reply);
298
299   BE_STREAM_TO_UINT8(cont_len, p_reply);
300   if (cont_len != 0) {
301     if (cont_len > SDP_MAX_CONTINUATION_LEN) {
302       sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
303       return;
304     }
305     if (p_reply + cont_len > p_reply_end) {
306       android_errorWriteLog(0x534e4554, "68161546");
307       sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
308       return;
309     }
310     /* stay in the same state */
311     sdp_snd_service_search_req(p_ccb, cont_len, p_reply);
312   } else {
313     /* change state */
314     p_ccb->disc_state = SDP_DISC_WAIT_ATTR;
315
316     /* Kick off the first attribute request */
317     process_service_attr_rsp(p_ccb, NULL, NULL);
318   }
319 }
320
321 /*******************************************************************************
322  *
323  * Function         sdp_copy_raw_data
324  *
325  * Description      copy the raw data
326  *
327  *
328  * Returns          void
329  *
330  ******************************************************************************/
331 #if (SDP_RAW_DATA_INCLUDED == TRUE)
332 static void sdp_copy_raw_data(tCONN_CB* p_ccb, bool offset) {
333   unsigned int cpy_len;
334   uint32_t list_len;
335   uint8_t* p;
336   uint8_t type;
337
338 #if (SDP_DEBUG_RAW == TRUE)
339   uint8_t num_array[SDP_MAX_LIST_BYTE_COUNT];
340   uint32_t i;
341
342   for (i = 0; i < p_ccb->list_len; i++) {
343     snprintf((char*)&num_array[i * 2], sizeof(num_array) - i * 2, "%02X",
344              (uint8_t)(p_ccb->rsp_list[i]));
345   }
346   SDP_TRACE_WARNING("result :%s", num_array);
347 #endif
348
349   if (p_ccb->p_db->raw_data) {
350     cpy_len = p_ccb->p_db->raw_size - p_ccb->p_db->raw_used;
351     list_len = p_ccb->list_len;
352     p = &p_ccb->rsp_list[0];
353
354     if (offset) {
355       type = *p++;
356       p = sdpu_get_len_from_type(p, type, &list_len);
357     }
358     if (list_len < cpy_len) {
359       cpy_len = list_len;
360     }
361     SDP_TRACE_WARNING(
362         "%s: list_len:%d cpy_len:%d p:%p p_ccb:%p p_db:%p raw_size:%d "
363         "raw_used:%d raw_data:%p",
364         __func__, list_len, cpy_len, p, p_ccb, p_ccb->p_db,
365         p_ccb->p_db->raw_size, p_ccb->p_db->raw_used, p_ccb->p_db->raw_data);
366     memcpy(&p_ccb->p_db->raw_data[p_ccb->p_db->raw_used], p, cpy_len);
367     p_ccb->p_db->raw_used += cpy_len;
368   }
369 }
370 #endif
371
372 /*******************************************************************************
373  *
374  * Function         process_service_attr_rsp
375  *
376  * Description      This function is called when there is a attribute response
377  *                  from the server.
378  *
379  * Returns          void
380  *
381  ******************************************************************************/
382 static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
383                                      uint8_t* p_reply_end) {
384   uint8_t *p_start, *p_param_len;
385   uint16_t param_len, list_byte_count;
386   bool cont_request_needed = false;
387
388 #if (SDP_DEBUG_RAW == TRUE)
389   SDP_TRACE_WARNING("process_service_attr_rsp raw inc:%d",
390                     SDP_RAW_DATA_INCLUDED);
391 #endif
392   /* If p_reply is NULL, we were called after the records handles were read */
393   if (p_reply) {
394 #if (SDP_DEBUG_RAW == TRUE)
395     SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x", p_reply[0], p_reply[1],
396                       p_reply[2], p_reply[3]);
397 #endif
398     /* Skip transaction ID and length */
399     p_reply += 4;
400
401     BE_STREAM_TO_UINT16(list_byte_count, p_reply);
402 #if (SDP_DEBUG_RAW == TRUE)
403     SDP_TRACE_WARNING("list_byte_count:%d", list_byte_count);
404 #endif
405
406     /* Copy the response to the scratchpad. First, a safety check on the length
407      */
408     if ((p_ccb->list_len + list_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
409       sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
410       return;
411     }
412
413 #if (SDP_DEBUG_RAW == TRUE)
414     SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d", p_ccb->list_len,
415                       list_byte_count);
416 #endif
417     if (p_ccb->rsp_list == NULL)
418       p_ccb->rsp_list = (uint8_t*)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
419     memcpy(&p_ccb->rsp_list[p_ccb->list_len], p_reply, list_byte_count);
420     p_ccb->list_len += list_byte_count;
421     p_reply += list_byte_count;
422 #if (SDP_DEBUG_RAW == TRUE)
423     SDP_TRACE_WARNING("list_len: %d(attr_rsp)", p_ccb->list_len);
424
425     /* Check if we need to request a continuation */
426     SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
427 #endif
428     if (*p_reply) {
429       if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
430         sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
431         return;
432       }
433       cont_request_needed = true;
434     } else {
435 #if (SDP_RAW_DATA_INCLUDED == TRUE)
436       SDP_TRACE_WARNING("process_service_attr_rsp");
437       sdp_copy_raw_data(p_ccb, false);
438 #endif
439
440       /* Save the response in the database. Stop on any error */
441       if (!save_attr_seq(p_ccb, &p_ccb->rsp_list[0],
442                          &p_ccb->rsp_list[p_ccb->list_len])) {
443         sdp_disconnect(p_ccb, SDP_DB_FULL);
444         return;
445       }
446       p_ccb->list_len = 0;
447       p_ccb->cur_handle++;
448     }
449   }
450
451   /* Now, ask for the next handle. Re-use the buffer we just got. */
452   if (p_ccb->cur_handle < p_ccb->num_handles) {
453     BT_HDR* p_msg = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
454     uint8_t* p;
455
456     p_msg->offset = L2CAP_MIN_OFFSET;
457     p = p_start = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
458
459     /* Get all the attributes from the server */
460     UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_ATTR_REQ);
461     UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
462     p_ccb->transaction_id++;
463
464     /* Skip the length, we need to add it at the end */
465     p_param_len = p;
466     p += 2;
467
468     UINT32_TO_BE_STREAM(p, p_ccb->handles[p_ccb->cur_handle]);
469
470     /* Max attribute byte count */
471     UINT16_TO_BE_STREAM(p, sdp_cb.max_attr_list_size);
472
473     /* If no attribute filters, build a wildcard attribute sequence */
474     if (p_ccb->p_db->num_attr_filters)
475       p = sdpu_build_attrib_seq(p, p_ccb->p_db->attr_filters,
476                                 p_ccb->p_db->num_attr_filters);
477     else
478       p = sdpu_build_attrib_seq(p, NULL, 0);
479
480     /* Was this a continuation request ? */
481     if (cont_request_needed) {
482       if ((p_reply + *p_reply + 1) <= p_reply_end) {
483         memcpy(p, p_reply, *p_reply + 1);
484         p += *p_reply + 1;
485       } else {
486         android_errorWriteLog(0x534e4554, "68161546");
487       }
488     } else
489       UINT8_TO_BE_STREAM(p, 0);
490
491     /* Go back and put the parameter length into the buffer */
492     param_len = (uint16_t)(p - p_param_len - 2);
493     UINT16_TO_BE_STREAM(p_param_len, param_len);
494
495     /* Set the length of the SDP data in the buffer */
496     p_msg->len = (uint16_t)(p - p_start);
497
498     L2CA_DataWrite(p_ccb->connection_id, p_msg);
499
500     /* Start inactivity timer */
501     alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
502                        sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
503   } else {
504     sdp_disconnect(p_ccb, SDP_SUCCESS);
505     return;
506   }
507 }
508
509 /*******************************************************************************
510  *
511  * Function         process_service_search_attr_rsp
512  *
513  * Description      This function is called when there is a search attribute
514  *                  response from the server.
515  *
516  * Returns          void
517  *
518  ******************************************************************************/
519 static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
520                                             uint8_t* p_reply_end) {
521   uint8_t *p, *p_start, *p_end, *p_param_len;
522   uint8_t type;
523   uint32_t seq_len;
524   uint16_t param_len, lists_byte_count = 0;
525   bool cont_request_needed = false;
526
527 #if (SDP_DEBUG_RAW == TRUE)
528   SDP_TRACE_WARNING("process_service_search_attr_rsp");
529 #endif
530   /* If p_reply is NULL, we were called for the initial read */
531   if (p_reply) {
532 #if (SDP_DEBUG_RAW == TRUE)
533     SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x", p_reply[0], p_reply[1],
534                       p_reply[2], p_reply[3]);
535 #endif
536     /* Skip transaction ID and length */
537     p_reply += 4;
538
539     BE_STREAM_TO_UINT16(lists_byte_count, p_reply);
540 #if (SDP_DEBUG_RAW == TRUE)
541     SDP_TRACE_WARNING("lists_byte_count:%d", lists_byte_count);
542 #endif
543
544     /* Copy the response to the scratchpad. First, a safety check on the length
545      */
546     if ((p_ccb->list_len + lists_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
547       sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
548       return;
549     }
550
551 #if (SDP_DEBUG_RAW == TRUE)
552     SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d", p_ccb->list_len,
553                       lists_byte_count);
554 #endif
555     if (p_ccb->rsp_list == NULL)
556       p_ccb->rsp_list = (uint8_t*)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
557     memcpy(&p_ccb->rsp_list[p_ccb->list_len], p_reply, lists_byte_count);
558     p_ccb->list_len += lists_byte_count;
559     p_reply += lists_byte_count;
560 #if (SDP_DEBUG_RAW == TRUE)
561     SDP_TRACE_WARNING("list_len: %d(search_attr_rsp)", p_ccb->list_len);
562
563     /* Check if we need to request a continuation */
564     SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
565 #endif
566     if (*p_reply) {
567       if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
568         sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
569         return;
570       }
571
572       cont_request_needed = true;
573     }
574   }
575
576 #if (SDP_DEBUG_RAW == TRUE)
577   SDP_TRACE_WARNING("cont_request_needed:%d", cont_request_needed);
578 #endif
579   /* If continuation request (or first time request) */
580   if ((cont_request_needed) || (!p_reply)) {
581     BT_HDR* p_msg = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
582     uint8_t* p;
583
584     p_msg->offset = L2CAP_MIN_OFFSET;
585     p = p_start = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
586
587     /* Build a service search request packet */
588     UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_SEARCH_ATTR_REQ);
589     UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
590     p_ccb->transaction_id++;
591
592     /* Skip the length, we need to add it at the end */
593     p_param_len = p;
594     p += 2;
595
596 /* Build the UID sequence. */
597 #if (SDP_BROWSE_PLUS == TRUE)
598     p = sdpu_build_uuid_seq(p, 1,
599                             &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
600 #else
601     p = sdpu_build_uuid_seq(p, p_ccb->p_db->num_uuid_filters,
602                             p_ccb->p_db->uuid_filters);
603 #endif
604
605     /* Max attribute byte count */
606     UINT16_TO_BE_STREAM(p, sdp_cb.max_attr_list_size);
607
608     /* If no attribute filters, build a wildcard attribute sequence */
609     if (p_ccb->p_db->num_attr_filters)
610       p = sdpu_build_attrib_seq(p, p_ccb->p_db->attr_filters,
611                                 p_ccb->p_db->num_attr_filters);
612     else
613       p = sdpu_build_attrib_seq(p, NULL, 0);
614
615     /* No continuation for first request */
616     if (p_reply) {
617       if ((p_reply + *p_reply + 1) <= p_reply_end) {
618         memcpy(p, p_reply, *p_reply + 1);
619         p += *p_reply + 1;
620       } else {
621         android_errorWriteLog(0x534e4554, "68161546");
622       }
623     } else
624       UINT8_TO_BE_STREAM(p, 0);
625
626     /* Go back and put the parameter length into the buffer */
627     param_len = p - p_param_len - 2;
628     UINT16_TO_BE_STREAM(p_param_len, param_len);
629
630     /* Set the length of the SDP data in the buffer */
631     p_msg->len = p - p_start;
632
633     L2CA_DataWrite(p_ccb->connection_id, p_msg);
634
635     /* Start inactivity timer */
636     alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
637                        sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
638
639     return;
640   }
641
642 /*******************************************************************/
643 /* We now have the full response, which is a sequence of sequences */
644 /*******************************************************************/
645
646 #if (SDP_RAW_DATA_INCLUDED == TRUE)
647   SDP_TRACE_WARNING("process_service_search_attr_rsp");
648   sdp_copy_raw_data(p_ccb, true);
649 #endif
650
651   p = &p_ccb->rsp_list[0];
652
653   /* The contents is a sequence of attribute sequences */
654   type = *p++;
655
656   if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
657     SDP_TRACE_WARNING("SDP - Wrong type: 0x%02x in attr_rsp", type);
658     return;
659   }
660   p = sdpu_get_len_from_type(p, type, &seq_len);
661
662   p_end = &p_ccb->rsp_list[p_ccb->list_len];
663
664   if ((p + seq_len) != p_end) {
665     sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
666     return;
667   }
668
669   while (p < p_end) {
670     p = save_attr_seq(p_ccb, p, &p_ccb->rsp_list[p_ccb->list_len]);
671     if (!p) {
672       sdp_disconnect(p_ccb, SDP_DB_FULL);
673       return;
674     }
675   }
676
677   /* Since we got everything we need, disconnect the call */
678   sdp_disconnect(p_ccb, SDP_SUCCESS);
679 }
680
681 /*******************************************************************************
682  *
683  * Function         save_attr_seq
684  *
685  * Description      This function is called when there is a response from
686  *                  the server.
687  *
688  * Returns          pointer to next byte or NULL if error
689  *
690  ******************************************************************************/
691 static uint8_t* save_attr_seq(tCONN_CB* p_ccb, uint8_t* p, uint8_t* p_msg_end) {
692   uint32_t seq_len, attr_len;
693   uint16_t attr_id;
694   uint8_t type, *p_seq_end;
695   tSDP_DISC_REC* p_rec;
696
697   type = *p++;
698
699   if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
700     SDP_TRACE_WARNING("SDP - Wrong type: 0x%02x in attr_rsp", type);
701     return (NULL);
702   }
703
704   p = sdpu_get_len_from_type(p, type, &seq_len);
705   if ((p + seq_len) > p_msg_end) {
706     SDP_TRACE_WARNING("SDP - Bad len in attr_rsp %d", seq_len);
707     return (NULL);
708   }
709
710   /* Create a record */
711   p_rec = add_record(p_ccb->p_db, p_ccb->device_address);
712   if (!p_rec) {
713     SDP_TRACE_WARNING("SDP - DB full add_record");
714     return (NULL);
715   }
716
717   p_seq_end = p + seq_len;
718
719   while (p < p_seq_end) {
720     /* First get the attribute ID */
721     type = *p++;
722     p = sdpu_get_len_from_type(p, type, &attr_len);
723     if (((type >> 3) != UINT_DESC_TYPE) || (attr_len != 2)) {
724       SDP_TRACE_WARNING("SDP - Bad type: 0x%02x or len: %d in attr_rsp", type,
725                         attr_len);
726       return (NULL);
727     }
728     BE_STREAM_TO_UINT16(attr_id, p);
729
730     /* Now, add the attribute value */
731     p = add_attr(p, p_ccb->p_db, p_rec, attr_id, NULL, 0);
732
733     if (!p) {
734       SDP_TRACE_WARNING("SDP - DB full add_attr");
735       return (NULL);
736     }
737   }
738
739   return (p);
740 }
741
742 /*******************************************************************************
743  *
744  * Function         add_record
745  *
746  * Description      This function allocates space for a record from the DB.
747  *
748  * Returns          pointer to next byte in data stream
749  *
750  ******************************************************************************/
751 tSDP_DISC_REC* add_record(tSDP_DISCOVERY_DB* p_db, BD_ADDR p_bda) {
752   tSDP_DISC_REC* p_rec;
753
754   /* See if there is enough space in the database */
755   if (p_db->mem_free < sizeof(tSDP_DISC_REC)) return (NULL);
756
757   p_rec = (tSDP_DISC_REC*)p_db->p_free_mem;
758   p_db->p_free_mem += sizeof(tSDP_DISC_REC);
759   p_db->mem_free -= sizeof(tSDP_DISC_REC);
760
761   p_rec->p_first_attr = NULL;
762   p_rec->p_next_rec = NULL;
763
764   memcpy(p_rec->remote_bd_addr, p_bda, BD_ADDR_LEN);
765
766   /* Add the record to the end of chain */
767   if (!p_db->p_first_rec)
768     p_db->p_first_rec = p_rec;
769   else {
770     tSDP_DISC_REC* p_rec1 = p_db->p_first_rec;
771
772     while (p_rec1->p_next_rec) p_rec1 = p_rec1->p_next_rec;
773
774     p_rec1->p_next_rec = p_rec;
775   }
776
777   return (p_rec);
778 }
779
780 #define SDP_ADDITIONAL_LIST_MASK 0x80
781 /*******************************************************************************
782  *
783  * Function         add_attr
784  *
785  * Description      This function allocates space for an attribute from the DB
786  *                  and copies the data into it.
787  *
788  * Returns          pointer to next byte in data stream
789  *
790  ******************************************************************************/
791 static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db,
792                          tSDP_DISC_REC* p_rec, uint16_t attr_id,
793                          tSDP_DISC_ATTR* p_parent_attr, uint8_t nest_level) {
794   tSDP_DISC_ATTR* p_attr;
795   uint32_t attr_len;
796   uint32_t total_len;
797   uint16_t attr_type;
798   uint16_t id;
799   uint8_t type;
800   uint8_t* p_end;
801   uint8_t is_additional_list = nest_level & SDP_ADDITIONAL_LIST_MASK;
802
803   nest_level &= ~(SDP_ADDITIONAL_LIST_MASK);
804
805   type = *p++;
806   p = sdpu_get_len_from_type(p, type, &attr_len);
807
808   attr_len &= SDP_DISC_ATTR_LEN_MASK;
809   attr_type = (type >> 3) & 0x0f;
810
811   /* See if there is enough space in the database */
812   if (attr_len > 4)
813     total_len = attr_len - 4 + (uint16_t)sizeof(tSDP_DISC_ATTR);
814   else
815     total_len = sizeof(tSDP_DISC_ATTR);
816
817   /* Ensure it is a multiple of 4 */
818   total_len = (total_len + 3) & ~3;
819
820   /* See if there is enough space in the database */
821   if (p_db->mem_free < total_len) return (NULL);
822
823   p_attr = (tSDP_DISC_ATTR*)p_db->p_free_mem;
824   p_attr->attr_id = attr_id;
825   p_attr->attr_len_type = (uint16_t)attr_len | (attr_type << 12);
826   p_attr->p_next_attr = NULL;
827
828   /* Store the attribute value */
829   switch (attr_type) {
830     case UINT_DESC_TYPE:
831       if ((is_additional_list != 0) && (attr_len == 2)) {
832         BE_STREAM_TO_UINT16(id, p);
833         if (id != ATTR_ID_PROTOCOL_DESC_LIST)
834           p -= 2;
835         else {
836           /* Reserve the memory for the attribute now, as we need to add
837            * sub-attributes */
838           p_db->p_free_mem += sizeof(tSDP_DISC_ATTR);
839           p_db->mem_free -= sizeof(tSDP_DISC_ATTR);
840           p_end = p + attr_len;
841           total_len = 0;
842
843           /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d(list)", nest_level); */
844           if (nest_level >= MAX_NEST_LEVELS) {
845             SDP_TRACE_ERROR("SDP - attr nesting too deep");
846             return (p_end);
847           }
848
849           /* Now, add the list entry */
850           p = add_attr(p, p_db, p_rec, ATTR_ID_PROTOCOL_DESC_LIST, p_attr,
851                        (uint8_t)(nest_level + 1));
852
853           break;
854         }
855       }
856     /* Case falls through */
857
858     case TWO_COMP_INT_DESC_TYPE:
859       switch (attr_len) {
860         case 1:
861           p_attr->attr_value.v.u8 = *p++;
862           break;
863         case 2:
864           BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
865           break;
866         case 4:
867           BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
868           break;
869         default:
870           BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array, (int32_t)attr_len);
871           break;
872       }
873       break;
874
875     case UUID_DESC_TYPE:
876       switch (attr_len) {
877         case 2:
878           BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
879           break;
880         case 4:
881           BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
882           if (p_attr->attr_value.v.u32 < 0x10000) {
883             attr_len = 2;
884             p_attr->attr_len_type = (uint16_t)attr_len | (attr_type << 12);
885             p_attr->attr_value.v.u16 = (uint16_t)p_attr->attr_value.v.u32;
886           }
887           break;
888         case 16:
889           /* See if we can compress his UUID down to 16 or 32bit UUIDs */
890           if (sdpu_is_base_uuid(p)) {
891             if ((p[0] == 0) && (p[1] == 0)) {
892               p_attr->attr_len_type =
893                   (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 2;
894               p += 2;
895               BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
896               p += MAX_UUID_SIZE - 4;
897             } else {
898               p_attr->attr_len_type =
899                   (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 4;
900               BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
901               p += MAX_UUID_SIZE - 4;
902             }
903           } else {
904             BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array,
905                                (int32_t)attr_len);
906           }
907           break;
908         default:
909           SDP_TRACE_WARNING("SDP - bad len in UUID attr: %d", attr_len);
910           return (p + attr_len);
911       }
912       break;
913
914     case DATA_ELE_SEQ_DESC_TYPE:
915     case DATA_ELE_ALT_DESC_TYPE:
916       /* Reserve the memory for the attribute now, as we need to add
917        * sub-attributes */
918       p_db->p_free_mem += sizeof(tSDP_DISC_ATTR);
919       p_db->mem_free -= sizeof(tSDP_DISC_ATTR);
920       p_end = p + attr_len;
921       total_len = 0;
922
923       /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d", nest_level); */
924       if (nest_level >= MAX_NEST_LEVELS) {
925         SDP_TRACE_ERROR("SDP - attr nesting too deep");
926         return (p_end);
927       }
928       if (is_additional_list != 0 ||
929           attr_id == ATTR_ID_ADDITION_PROTO_DESC_LISTS)
930         nest_level |= SDP_ADDITIONAL_LIST_MASK;
931       /* SDP_TRACE_DEBUG ("SDP - attr nest level:0x%x(finish)", nest_level); */
932
933       while (p < p_end) {
934         /* Now, add the list entry */
935         p = add_attr(p, p_db, p_rec, 0, p_attr, (uint8_t)(nest_level + 1));
936
937         if (!p) return (NULL);
938       }
939       break;
940
941     case TEXT_STR_DESC_TYPE:
942     case URL_DESC_TYPE:
943       BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array, (int32_t)attr_len);
944       break;
945
946     case BOOLEAN_DESC_TYPE:
947       switch (attr_len) {
948         case 1:
949           p_attr->attr_value.v.u8 = *p++;
950           break;
951         default:
952           SDP_TRACE_WARNING("SDP - bad len in boolean attr: %d", attr_len);
953           return (p + attr_len);
954       }
955       break;
956
957     default: /* switch (attr_type) */
958       break;
959   }
960
961   p_db->p_free_mem += total_len;
962   p_db->mem_free -= total_len;
963
964   /* Add the attribute to the end of the chain */
965   if (!p_parent_attr) {
966     if (!p_rec->p_first_attr)
967       p_rec->p_first_attr = p_attr;
968     else {
969       tSDP_DISC_ATTR* p_attr1 = p_rec->p_first_attr;
970
971       while (p_attr1->p_next_attr) p_attr1 = p_attr1->p_next_attr;
972
973       p_attr1->p_next_attr = p_attr;
974     }
975   } else {
976     if (!p_parent_attr->attr_value.v.p_sub_attr) {
977       p_parent_attr->attr_value.v.p_sub_attr = p_attr;
978       /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch:0x%x(id:%d)",
979           p_parent_attr, p_parent_attr->attr_id, p_attr, p_attr->attr_id); */
980     } else {
981       tSDP_DISC_ATTR* p_attr1 = p_parent_attr->attr_value.v.p_sub_attr;
982       /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch1:0x%x(id:%d)",
983           p_parent_attr, p_parent_attr->attr_id, p_attr1, p_attr1->attr_id); */
984
985       while (p_attr1->p_next_attr) p_attr1 = p_attr1->p_next_attr;
986
987       p_attr1->p_next_attr = p_attr;
988       /* SDP_TRACE_DEBUG ("new ch:0x%x(id:%d)", p_attr, p_attr->attr_id); */
989     }
990   }
991
992   return (p);
993 }