OSDN Git Service

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