LCOV - code coverage report
Current view: top level - src/p2p - p2p_parse.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 459 504 91.1 %
Date: 2015-09-27 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /*
       2             :  * P2P - IE parser
       3             :  * Copyright (c) 2009-2010, Atheros Communications
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "common/ieee802_11_defs.h"
      13             : #include "common/ieee802_11_common.h"
      14             : #include "wps/wps_i.h"
      15             : #include "p2p_i.h"
      16             : 
      17             : 
      18       19501 : static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
      19             :                                struct p2p_message *msg)
      20             : {
      21             :         const u8 *pos;
      22             :         size_t i, nlen;
      23             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
      24             : 
      25       19501 :         switch (id) {
      26             :         case P2P_ATTR_CAPABILITY:
      27        7529 :                 if (len < 2) {
      28           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
      29             :                                    "attribute (length %d)", len);
      30           1 :                         return -1;
      31             :                 }
      32        7528 :                 msg->capability = data;
      33       15056 :                 wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
      34             :                            "Group Capability %02x",
      35       15056 :                            data[0], data[1]);
      36        7528 :                 break;
      37             :         case P2P_ATTR_DEVICE_ID:
      38         115 :                 if (len < ETH_ALEN) {
      39           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
      40             :                                    "attribute (length %d)", len);
      41           1 :                         return -1;
      42             :                 }
      43         114 :                 msg->device_id = data;
      44         684 :                 wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
      45         684 :                            MAC2STR(msg->device_id));
      46         114 :                 break;
      47             :         case P2P_ATTR_GROUP_OWNER_INTENT:
      48         341 :                 if (len < 1) {
      49           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
      50             :                                    "attribute (length %d)", len);
      51           1 :                         return -1;
      52             :                 }
      53         340 :                 msg->go_intent = data;
      54         680 :                 wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u "
      55         680 :                            "Tie breaker %u", data[0] >> 1, data[0] & 0x01);
      56         340 :                 break;
      57             :         case P2P_ATTR_STATUS:
      58         386 :                 if (len < 1) {
      59           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Status "
      60             :                                    "attribute (length %d)", len);
      61           1 :                         return -1;
      62             :                 }
      63         385 :                 msg->status = data;
      64         385 :                 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
      65         385 :                 break;
      66             :         case P2P_ATTR_LISTEN_CHANNEL:
      67        2807 :                 if (len == 0) {
      68           1 :                         wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
      69             :                                    "null channel");
      70           1 :                         break;
      71             :                 }
      72        2806 :                 if (len < 5) {
      73           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
      74             :                                    "attribute (length %d)", len);
      75           1 :                         return -1;
      76             :                 }
      77        2805 :                 msg->listen_channel = data;
      78       14025 :                 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: "
      79             :                            "Country %c%c(0x%02x) Regulatory "
      80        5610 :                            "Class %d Channel Number %d", data[0], data[1],
      81        8415 :                            data[2], data[3], data[4]);
      82        2805 :                 break;
      83             :         case P2P_ATTR_OPERATING_CHANNEL:
      84         520 :                 if (len == 0) {
      85           1 :                         wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
      86             :                                    "Ignore null channel");
      87           1 :                         break;
      88             :                 }
      89         519 :                 if (len < 5) {
      90           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
      91             :                                    "Channel attribute (length %d)", len);
      92           1 :                         return -1;
      93             :                 }
      94         518 :                 msg->operating_channel = data;
      95        2590 :                 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
      96             :                            "Country %c%c(0x%02x) Regulatory "
      97        1036 :                            "Class %d Channel Number %d", data[0], data[1],
      98        1554 :                            data[2], data[3], data[4]);
      99         518 :                 break;
     100             :         case P2P_ATTR_CHANNEL_LIST:
     101         644 :                 if (len < 3) {
     102           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
     103             :                                    "attribute (length %d)", len);
     104           1 :                         return -1;
     105             :                 }
     106         643 :                 msg->channel_list = data;
     107         643 :                 msg->channel_list_len = len;
     108        1929 :                 wpa_printf(MSG_DEBUG, "P2P: * Channel List: Country String "
     109        1929 :                            "'%c%c(0x%02x)'", data[0], data[1], data[2]);
     110        1286 :                 wpa_hexdump(MSG_MSGDUMP, "P2P: Channel List",
     111        1286 :                             msg->channel_list, msg->channel_list_len);
     112         643 :                 break;
     113             :         case P2P_ATTR_GROUP_INFO:
     114         206 :                 msg->group_info = data;
     115         206 :                 msg->group_info_len = len;
     116         206 :                 wpa_printf(MSG_DEBUG, "P2P: * Group Info");
     117         206 :                 break;
     118             :         case P2P_ATTR_DEVICE_INFO:
     119        4840 :                 if (len < ETH_ALEN + 2 + 8 + 1) {
     120           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Device Info "
     121             :                                    "attribute (length %d)", len);
     122           1 :                         return -1;
     123             :                 }
     124        4839 :                 msg->p2p_device_info = data;
     125        4839 :                 msg->p2p_device_info_len = len;
     126        4839 :                 pos = data;
     127        4839 :                 msg->p2p_device_addr = pos;
     128        4839 :                 pos += ETH_ALEN;
     129        4839 :                 msg->config_methods = WPA_GET_BE16(pos);
     130        4839 :                 pos += 2;
     131        4839 :                 msg->pri_dev_type = pos;
     132        4839 :                 pos += 8;
     133        4839 :                 msg->num_sec_dev_types = *pos++;
     134        4839 :                 if (msg->num_sec_dev_types * 8 > data + len - pos) {
     135           1 :                         wpa_printf(MSG_DEBUG, "P2P: Device Info underflow");
     136           1 :                         return -1;
     137             :                 }
     138        4838 :                 pos += msg->num_sec_dev_types * 8;
     139        4838 :                 if (data + len - pos < 4) {
     140           1 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
     141           1 :                                    "length %d", (int) (data + len - pos));
     142           1 :                         return -1;
     143             :                 }
     144        4837 :                 if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
     145           1 :                         wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
     146             :                                     "header", pos, 4);
     147           1 :                         return -1;
     148             :                 }
     149        4836 :                 pos += 2;
     150        4836 :                 nlen = WPA_GET_BE16(pos);
     151        4836 :                 pos += 2;
     152        4836 :                 if (data + len - pos < (int) nlen ||
     153             :                     nlen > WPS_DEV_NAME_MAX_LEN) {
     154           4 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
     155             :                                    "length %d (buf len %d)", (int) nlen,
     156           4 :                                    (int) (data + len - pos));
     157           4 :                         return -1;
     158             :                 }
     159        4832 :                 os_memcpy(msg->device_name, pos, nlen);
     160        4832 :                 msg->device_name[nlen] = '\0';
     161       43718 :                 for (i = 0; i < nlen; i++) {
     162       38887 :                         if (msg->device_name[i] == '\0')
     163           1 :                                 break;
     164       38886 :                         if (is_ctrl_char(msg->device_name[i]))
     165           1 :                                 msg->device_name[i] = '_';
     166             :                 }
     167       38656 :                 wpa_printf(MSG_DEBUG, "P2P: * Device Info: addr " MACSTR
     168             :                            " primary device type %s device name '%s' "
     169             :                            "config methods 0x%x",
     170       28992 :                            MAC2STR(msg->p2p_device_addr),
     171             :                            wps_dev_type_bin2str(msg->pri_dev_type, devtype,
     172             :                                                 sizeof(devtype)),
     173        9664 :                            msg->device_name, msg->config_methods);
     174        4832 :                 break;
     175             :         case P2P_ATTR_CONFIGURATION_TIMEOUT:
     176         529 :                 if (len < 2) {
     177           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
     178             :                                    "Timeout attribute (length %d)", len);
     179           1 :                         return -1;
     180             :                 }
     181         528 :                 msg->config_timeout = data;
     182         528 :                 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
     183         528 :                 break;
     184             :         case P2P_ATTR_INTENDED_INTERFACE_ADDR:
     185         406 :                 if (len < ETH_ALEN) {
     186           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Intended P2P "
     187             :                                    "Interface Address attribute (length %d)",
     188             :                                    len);
     189           1 :                         return -1;
     190             :                 }
     191         405 :                 msg->intended_addr = data;
     192        2430 :                 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
     193        2430 :                            MACSTR, MAC2STR(msg->intended_addr));
     194         405 :                 break;
     195             :         case P2P_ATTR_GROUP_BSSID:
     196          63 :                 if (len < ETH_ALEN) {
     197           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short P2P Group BSSID "
     198             :                                    "attribute (length %d)", len);
     199           1 :                         return -1;
     200             :                 }
     201          62 :                 msg->group_bssid = data;
     202         372 :                 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
     203         372 :                            MAC2STR(msg->group_bssid));
     204          62 :                 break;
     205             :         case P2P_ATTR_GROUP_ID:
     206         376 :                 if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
     207           2 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid P2P Group ID "
     208             :                                    "attribute length %d", len);
     209           2 :                         return -1;
     210             :                 }
     211         374 :                 msg->group_id = data;
     212         374 :                 msg->group_id_len = len;
     213        2244 :                 wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID: Device Address "
     214        2244 :                            MACSTR, MAC2STR(msg->group_id));
     215         748 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: * P2P Group ID: SSID",
     216         374 :                                   msg->group_id + ETH_ALEN,
     217         374 :                                   msg->group_id_len - ETH_ALEN);
     218         374 :                 break;
     219             :         case P2P_ATTR_INVITATION_FLAGS:
     220          68 :                 if (len < 1) {
     221           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
     222             :                                    "Flag attribute (length %d)", len);
     223           1 :                         return -1;
     224             :                 }
     225          67 :                 msg->invitation_flags = data;
     226          67 :                 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
     227          67 :                            data[0]);
     228          67 :                 break;
     229             :         case P2P_ATTR_MANAGEABILITY:
     230          11 :                 if (len < 1) {
     231           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
     232             :                                    "attribute (length %d)", len);
     233           1 :                         return -1;
     234             :                 }
     235          10 :                 msg->manageability = data;
     236          10 :                 wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
     237          10 :                            data[0]);
     238          10 :                 break;
     239             :         case P2P_ATTR_NOTICE_OF_ABSENCE:
     240          11 :                 if (len < 2) {
     241           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
     242             :                                    "Absence attribute (length %d)", len);
     243           1 :                         return -1;
     244             :                 }
     245          10 :                 msg->noa = data;
     246          10 :                 msg->noa_len = len;
     247          10 :                 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
     248          10 :                 break;
     249             :         case P2P_ATTR_EXT_LISTEN_TIMING:
     250          32 :                 if (len < 4) {
     251           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
     252             :                                    "Timing attribute (length %d)", len);
     253           1 :                         return -1;
     254             :                 }
     255          31 :                 msg->ext_listen_timing = data;
     256          62 :                 wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing "
     257             :                            "(period %u msec  interval %u msec)",
     258          31 :                            WPA_GET_LE16(msg->ext_listen_timing),
     259          31 :                            WPA_GET_LE16(msg->ext_listen_timing + 2));
     260          31 :                 break;
     261             :         case P2P_ATTR_MINOR_REASON_CODE:
     262           4 :                 if (len < 1) {
     263           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
     264             :                                    "Code attribute (length %d)", len);
     265           1 :                         return -1;
     266             :                 }
     267           3 :                 msg->minor_reason_code = data;
     268           3 :                 wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
     269           3 :                            *msg->minor_reason_code);
     270           3 :                 break;
     271             :         case P2P_ATTR_OOB_GO_NEG_CHANNEL:
     272          30 :                 if (len < 6) {
     273           1 :                         wpa_printf(MSG_DEBUG, "P2P: Too short OOB GO Neg "
     274             :                                    "Channel attribute (length %d)", len);
     275           1 :                         return -1;
     276             :                 }
     277          29 :                 msg->oob_go_neg_channel = data;
     278         174 :                 wpa_printf(MSG_DEBUG, "P2P: * OOB GO Neg Channel: "
     279             :                            "Country %c%c(0x%02x) Operating Class %d "
     280             :                            "Channel Number %d Role %d",
     281         145 :                            data[0], data[1], data[2], data[3], data[4],
     282          29 :                            data[5]);
     283          29 :                 break;
     284             :         case P2P_ATTR_SERVICE_HASH:
     285         148 :                 if (len < P2PS_HASH_LEN) {
     286           1 :                         wpa_printf(MSG_DEBUG,
     287             :                                    "P2P: Too short Service Hash (length %u)",
     288             :                                    len);
     289           1 :                         return -1;
     290             :                 }
     291         147 :                 msg->service_hash_count = len / P2PS_HASH_LEN;
     292         147 :                 msg->service_hash = data;
     293         147 :                 wpa_hexdump(MSG_DEBUG, "P2P: * Service Hash(s)", data, len);
     294         147 :                 break;
     295             :         case P2P_ATTR_SESSION_INFORMATION_DATA:
     296           0 :                 msg->session_info = data;
     297           0 :                 msg->session_info_len = len;
     298           0 :                 wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %u bytes - %p",
     299             :                            len, data);
     300           0 :                 break;
     301             :         case P2P_ATTR_CONNECTION_CAPABILITY:
     302          84 :                 if (len < 1) {
     303           1 :                         wpa_printf(MSG_DEBUG,
     304             :                                    "P2P: Too short Connection Capability (length %u)",
     305             :                                    len);
     306           1 :                         return -1;
     307             :                 }
     308          83 :                 msg->conn_cap = data;
     309          83 :                 wpa_printf(MSG_DEBUG, "P2P: * Connection Capability: 0x%x",
     310          83 :                            *msg->conn_cap);
     311          83 :                 break;
     312             :         case P2P_ATTR_ADVERTISEMENT_ID:
     313          92 :                 if (len < 10) {
     314           1 :                         wpa_printf(MSG_DEBUG,
     315             :                                    "P2P: Too short Advertisement ID (length %u)",
     316             :                                    len);
     317           1 :                         return -1;
     318             :                 }
     319          91 :                 msg->adv_id = data;
     320          91 :                 msg->adv_mac = &data[sizeof(u32)];
     321          91 :                 wpa_printf(MSG_DEBUG, "P2P: * Advertisement ID %x",
     322             :                            WPA_GET_LE32(data));
     323          91 :                 break;
     324             :         case P2P_ATTR_ADVERTISED_SERVICE:
     325          70 :                 if (len < 8) {
     326           1 :                         wpa_printf(MSG_DEBUG,
     327             :                                    "P2P: Too short Service Instance (length %u)",
     328             :                                    len);
     329           1 :                         return -1;
     330             :                 }
     331          69 :                 msg->adv_service_instance = data;
     332          69 :                 msg->adv_service_instance_len = len;
     333          69 :                 if (len <= 255 + 8) {
     334             :                         char str[256];
     335             :                         u8 namelen;
     336             : 
     337          68 :                         namelen = data[6];
     338          68 :                         if (namelen > len - 7)
     339           1 :                                 break;
     340          67 :                         os_memcpy(str, &data[7], namelen);
     341          67 :                         str[namelen] = '\0';
     342          67 :                         wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %x-%s",
     343             :                                    WPA_GET_LE32(data), str);
     344             :                 } else {
     345           1 :                         wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %p",
     346             :                                    data);
     347             :                 }
     348          68 :                 break;
     349             :         case P2P_ATTR_SESSION_ID:
     350          92 :                 if (len < sizeof(u32) + ETH_ALEN) {
     351           1 :                         wpa_printf(MSG_DEBUG,
     352             :                                    "P2P: Too short Session ID Info (length %u)",
     353             :                                    len);
     354           1 :                         return -1;
     355             :                 }
     356          91 :                 msg->session_id = data;
     357          91 :                 msg->session_mac = &data[sizeof(u32)];
     358         546 :                 wpa_printf(MSG_DEBUG, "P2P: * Session ID: %x " MACSTR,
     359         546 :                            WPA_GET_LE32(data), MAC2STR(msg->session_mac));
     360          91 :                 break;
     361             :         case P2P_ATTR_FEATURE_CAPABILITY:
     362          92 :                 if (!len) {
     363           1 :                         wpa_printf(MSG_DEBUG,
     364             :                                    "P2P: Too short Feature Capability (length %u)",
     365             :                                    len);
     366           1 :                         return -1;
     367             :                 }
     368          91 :                 msg->feature_cap = data;
     369          91 :                 msg->feature_cap_len = len;
     370          91 :                 wpa_printf(MSG_DEBUG, "P2P: * Feature Cap (length=%u)", len);
     371          91 :                 break;
     372             :         case P2P_ATTR_PERSISTENT_GROUP:
     373             :         {
     374           4 :                 if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
     375           2 :                         wpa_printf(MSG_DEBUG,
     376             :                                    "P2P: Invalid Persistent Group Info (length %u)",
     377             :                                    len);
     378           2 :                         return -1;
     379             :                 }
     380             : 
     381           2 :                 msg->persistent_dev = data;
     382           2 :                 msg->persistent_ssid_len = len - ETH_ALEN;
     383           2 :                 msg->persistent_ssid = &data[ETH_ALEN];
     384          14 :                 wpa_printf(MSG_DEBUG, "P2P: * Persistent Group: " MACSTR " %s",
     385          12 :                            MAC2STR(msg->persistent_dev),
     386             :                            wpa_ssid_txt(msg->persistent_ssid,
     387             :                                         msg->persistent_ssid_len));
     388           2 :                 break;
     389             :         }
     390             :         default:
     391           1 :                 wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
     392             :                            "(length %d)", id, len);
     393           1 :                 break;
     394             :         }
     395             : 
     396       19467 :         return 0;
     397             : }
     398             : 
     399             : 
     400             : /**
     401             :  * p2p_parse_p2p_ie - Parse P2P IE
     402             :  * @buf: Concatenated P2P IE(s) payload
     403             :  * @msg: Buffer for returning parsed attributes
     404             :  * Returns: 0 on success, -1 on failure
     405             :  *
     406             :  * Note: Caller is responsible for clearing the msg data structure before
     407             :  * calling this function.
     408             :  */
     409        7730 : int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
     410             : {
     411        7730 :         const u8 *pos = wpabuf_head_u8(buf);
     412        7730 :         const u8 *end = pos + wpabuf_len(buf);
     413             : 
     414        7730 :         wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
     415             : 
     416       34927 :         while (pos < end) {
     417             :                 u16 attr_len;
     418             :                 u8 id;
     419             : 
     420       19508 :                 if (end - pos < 3) {
     421           5 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
     422           5 :                         return -1;
     423             :                 }
     424       19503 :                 id = *pos++;
     425       19503 :                 attr_len = WPA_GET_LE16(pos);
     426       19503 :                 pos += 2;
     427       19503 :                 wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
     428             :                            id, attr_len);
     429       19503 :                 if (attr_len > end - pos) {
     430           2 :                         wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
     431             :                                    "(len=%u left=%d)",
     432           2 :                                    attr_len, (int) (end - pos));
     433           2 :                         wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
     434           2 :                         return -1;
     435             :                 }
     436       19501 :                 if (p2p_parse_attribute(id, pos, attr_len, msg))
     437          34 :                         return -1;
     438       19467 :                 pos += attr_len;
     439             :         }
     440             : 
     441        7689 :         return 0;
     442             : }
     443             : 
     444             : 
     445        4079 : static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
     446             : {
     447             :         struct wps_parse_attr attr;
     448             :         int i;
     449             : 
     450        4079 :         wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
     451        4079 :         if (wps_parse_msg(buf, &attr))
     452           0 :                 return -1;
     453        7308 :         if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
     454        3229 :             !msg->device_name[0])
     455        3229 :                 os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
     456        4079 :         if (attr.config_methods) {
     457        3518 :                 msg->wps_config_methods =
     458        3518 :                         WPA_GET_BE16(attr.config_methods);
     459        3518 :                 wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
     460        3518 :                            msg->wps_config_methods);
     461             :         }
     462        4079 :         if (attr.dev_password_id) {
     463        2816 :                 msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
     464        2816 :                 wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
     465        2816 :                            msg->dev_password_id);
     466        2816 :                 msg->dev_password_id_present = 1;
     467             :         }
     468        4079 :         if (attr.primary_dev_type) {
     469             :                 char devtype[WPS_DEV_TYPE_BUFSIZE];
     470        3229 :                 msg->wps_pri_dev_type = attr.primary_dev_type;
     471        3229 :                 wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
     472             :                            wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
     473             :                                                 sizeof(devtype)));
     474             :         }
     475        4079 :         if (attr.sec_dev_type_list) {
     476         110 :                 msg->wps_sec_dev_type_list = attr.sec_dev_type_list;
     477         110 :                 msg->wps_sec_dev_type_list_len = attr.sec_dev_type_list_len;
     478             :         }
     479             : 
     480       44869 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
     481       40790 :                 msg->wps_vendor_ext[i] = attr.vendor_ext[i];
     482       40790 :                 msg->wps_vendor_ext_len[i] = attr.vendor_ext_len[i];
     483             :         }
     484             : 
     485        4079 :         msg->manufacturer = attr.manufacturer;
     486        4079 :         msg->manufacturer_len = attr.manufacturer_len;
     487        4079 :         msg->model_name = attr.model_name;
     488        4079 :         msg->model_name_len = attr.model_name_len;
     489        4079 :         msg->model_number = attr.model_number;
     490        4079 :         msg->model_number_len = attr.model_number_len;
     491        4079 :         msg->serial_number = attr.serial_number;
     492        4079 :         msg->serial_number_len = attr.serial_number_len;
     493             : 
     494        4079 :         msg->oob_dev_password = attr.oob_dev_password;
     495        4079 :         msg->oob_dev_password_len = attr.oob_dev_password_len;
     496             : 
     497        4079 :         return 0;
     498             : }
     499             : 
     500             : 
     501             : /**
     502             :  * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
     503             :  * @data: IEs from the message
     504             :  * @len: Length of data buffer in octets
     505             :  * @msg: Buffer for returning parsed attributes
     506             :  * Returns: 0 on success, -1 on failure
     507             :  *
     508             :  * Note: Caller is responsible for clearing the msg data structure before
     509             :  * calling this function.
     510             :  *
     511             :  * Note: Caller must free temporary memory allocations by calling
     512             :  * p2p_parse_free() when the parsed data is not needed anymore.
     513             :  */
     514        4702 : int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
     515             : {
     516             :         struct ieee802_11_elems elems;
     517             : 
     518        4702 :         ieee802_11_parse_elems(data, len, &elems, 0);
     519        4702 :         if (elems.ds_params)
     520        3286 :                 msg->ds_params = elems.ds_params;
     521        4702 :         if (elems.ssid)
     522        3764 :                 msg->ssid = elems.ssid - 2;
     523             : 
     524        4702 :         msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
     525             :                                                           WPS_DEV_OUI_WFA);
     526        8751 :         if (msg->wps_attributes &&
     527        4049 :             p2p_parse_wps_ie(msg->wps_attributes, msg)) {
     528           0 :                 p2p_parse_free(msg);
     529           0 :                 return -1;
     530             :         }
     531             : 
     532        4702 :         msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
     533             :                                                           P2P_IE_VENDOR_TYPE);
     534        9190 :         if (msg->p2p_attributes &&
     535        4488 :             p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
     536          41 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
     537          41 :                 if (msg->p2p_attributes)
     538          41 :                         wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
     539          41 :                                         msg->p2p_attributes);
     540          41 :                 p2p_parse_free(msg);
     541          41 :                 return -1;
     542             :         }
     543             : 
     544             : #ifdef CONFIG_WIFI_DISPLAY
     545        4661 :         if (elems.wfd) {
     546         131 :                 msg->wfd_subelems = ieee802_11_vendor_ie_concat(
     547             :                         data, len, WFD_IE_VENDOR_TYPE);
     548             :         }
     549             : #endif /* CONFIG_WIFI_DISPLAY */
     550             : 
     551        4661 :         msg->pref_freq_list = elems.pref_freq_list;
     552        4661 :         msg->pref_freq_list_len = elems.pref_freq_list_len;
     553             : 
     554        4661 :         return 0;
     555             : }
     556             : 
     557             : 
     558             : /**
     559             :  * p2p_parse - Parse a P2P Action frame contents
     560             :  * @data: Action frame payload after Category and Code fields
     561             :  * @len: Length of data buffer in octets
     562             :  * @msg: Buffer for returning parsed attributes
     563             :  * Returns: 0 on success, -1 on failure
     564             :  *
     565             :  * Note: Caller must free temporary memory allocations by calling
     566             :  * p2p_parse_free() when the parsed data is not needed anymore.
     567             :  */
     568         839 : int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
     569             : {
     570         839 :         os_memset(msg, 0, sizeof(*msg));
     571         839 :         wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
     572         839 :         if (len < 1) {
     573           1 :                 wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
     574           1 :                 return -1;
     575             :         }
     576         838 :         msg->dialog_token = data[0];
     577         838 :         wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
     578             : 
     579         838 :         return p2p_parse_ies(data + 1, len - 1, msg);
     580             : }
     581             : 
     582             : 
     583          30 : int p2p_parse_ies_separate(const u8 *wsc, size_t wsc_len, const u8 *p2p,
     584             :                            size_t p2p_len, struct p2p_message *msg)
     585             : {
     586          30 :         os_memset(msg, 0, sizeof(*msg));
     587             : 
     588          30 :         msg->wps_attributes = wpabuf_alloc_copy(wsc, wsc_len);
     589          60 :         if (msg->wps_attributes &&
     590          30 :             p2p_parse_wps_ie(msg->wps_attributes, msg)) {
     591           0 :                 p2p_parse_free(msg);
     592           0 :                 return -1;
     593             :         }
     594             : 
     595          30 :         msg->p2p_attributes = wpabuf_alloc_copy(p2p, p2p_len);
     596          60 :         if (msg->p2p_attributes &&
     597          30 :             p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
     598           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
     599           0 :                 if (msg->p2p_attributes)
     600           0 :                         wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
     601           0 :                                         msg->p2p_attributes);
     602           0 :                 p2p_parse_free(msg);
     603           0 :                 return -1;
     604             :         }
     605             : 
     606          30 :         return 0;
     607             : }
     608             : 
     609             : 
     610             : /**
     611             :  * p2p_parse_free - Free temporary data from P2P parsing
     612             :  * @msg: Parsed attributes
     613             :  */
     614        4732 : void p2p_parse_free(struct p2p_message *msg)
     615             : {
     616        4732 :         wpabuf_free(msg->p2p_attributes);
     617        4732 :         msg->p2p_attributes = NULL;
     618        4732 :         wpabuf_free(msg->wps_attributes);
     619        4732 :         msg->wps_attributes = NULL;
     620             : #ifdef CONFIG_WIFI_DISPLAY
     621        4732 :         wpabuf_free(msg->wfd_subelems);
     622        4732 :         msg->wfd_subelems = NULL;
     623             : #endif /* CONFIG_WIFI_DISPLAY */
     624        4732 : }
     625             : 
     626             : 
     627          47 : int p2p_group_info_parse(const u8 *gi, size_t gi_len,
     628             :                          struct p2p_group_info *info)
     629             : {
     630             :         const u8 *g, *gend;
     631             : 
     632          47 :         os_memset(info, 0, sizeof(*info));
     633          47 :         if (gi == NULL)
     634          10 :                 return 0;
     635             : 
     636          37 :         g = gi;
     637          37 :         gend = gi + gi_len;
     638         115 :         while (g < gend) {
     639             :                 struct p2p_client_info *cli;
     640             :                 const u8 *t, *cend;
     641             :                 int count;
     642             : 
     643          41 :                 cli = &info->client[info->num_clients];
     644          41 :                 cend = g + 1 + g[0];
     645          41 :                 if (cend > gend)
     646           0 :                         return -1; /* invalid data */
     647             :                 /* g at start of P2P Client Info Descriptor */
     648             :                 /* t at Device Capability Bitmap */
     649          41 :                 t = g + 1 + 2 * ETH_ALEN;
     650          41 :                 if (t > cend)
     651           0 :                         return -1; /* invalid data */
     652          41 :                 cli->p2p_device_addr = g + 1;
     653          41 :                 cli->p2p_interface_addr = g + 1 + ETH_ALEN;
     654          41 :                 cli->dev_capab = t[0];
     655             : 
     656          41 :                 if (t + 1 + 2 + 8 + 1 > cend)
     657           0 :                         return -1; /* invalid data */
     658             : 
     659          41 :                 cli->config_methods = WPA_GET_BE16(&t[1]);
     660          41 :                 cli->pri_dev_type = &t[3];
     661             : 
     662          41 :                 t += 1 + 2 + 8;
     663             :                 /* t at Number of Secondary Device Types */
     664          41 :                 cli->num_sec_dev_types = *t++;
     665          41 :                 if (t + 8 * cli->num_sec_dev_types > cend)
     666           0 :                         return -1; /* invalid data */
     667          41 :                 cli->sec_dev_types = t;
     668          41 :                 t += 8 * cli->num_sec_dev_types;
     669             : 
     670             :                 /* t at Device Name in WPS TLV format */
     671          41 :                 if (t + 2 + 2 > cend)
     672           0 :                         return -1; /* invalid data */
     673          41 :                 if (WPA_GET_BE16(t) != ATTR_DEV_NAME)
     674           0 :                         return -1; /* invalid Device Name TLV */
     675          41 :                 t += 2;
     676          41 :                 count = WPA_GET_BE16(t);
     677          41 :                 t += 2;
     678          41 :                 if (count > cend - t)
     679           0 :                         return -1; /* invalid Device Name TLV */
     680          41 :                 if (count >= WPS_DEV_NAME_MAX_LEN)
     681           9 :                         count = WPS_DEV_NAME_MAX_LEN;
     682          41 :                 cli->dev_name = (const char *) t;
     683          41 :                 cli->dev_name_len = count;
     684             : 
     685          41 :                 g = cend;
     686             : 
     687          41 :                 info->num_clients++;
     688          41 :                 if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
     689           0 :                         return -1;
     690             :         }
     691             : 
     692          37 :         return 0;
     693             : }
     694             : 
     695             : 
     696          11 : static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
     697             :                                char *end)
     698             : {
     699          11 :         char *pos = buf;
     700             :         int ret;
     701             :         struct p2p_group_info info;
     702             :         unsigned int i;
     703             : 
     704          11 :         if (p2p_group_info_parse(gi, gi_len, &info) < 0)
     705           0 :                 return 0;
     706             : 
     707          24 :         for (i = 0; i < info.num_clients; i++) {
     708             :                 struct p2p_client_info *cli;
     709             :                 char name[WPS_DEV_NAME_MAX_LEN + 1];
     710             :                 char devtype[WPS_DEV_TYPE_BUFSIZE];
     711             :                 u8 s;
     712             :                 int count;
     713             : 
     714           1 :                 cli = &info.client[i];
     715          12 :                 ret = os_snprintf(pos, end - pos, "p2p_group_client: "
     716             :                                   "dev=" MACSTR " iface=" MACSTR,
     717           6 :                                   MAC2STR(cli->p2p_device_addr),
     718           6 :                                   MAC2STR(cli->p2p_interface_addr));
     719           1 :                 if (os_snprintf_error(end - pos, ret))
     720           0 :                         return pos - buf;
     721           1 :                 pos += ret;
     722             : 
     723           3 :                 ret = os_snprintf(pos, end - pos,
     724             :                                   " dev_capab=0x%x config_methods=0x%x "
     725             :                                   "dev_type=%s",
     726           2 :                                   cli->dev_capab, cli->config_methods,
     727             :                                   wps_dev_type_bin2str(cli->pri_dev_type,
     728             :                                                        devtype,
     729             :                                                        sizeof(devtype)));
     730           1 :                 if (os_snprintf_error(end - pos, ret))
     731           0 :                         return pos - buf;
     732           1 :                 pos += ret;
     733             : 
     734           1 :                 for (s = 0; s < cli->num_sec_dev_types; s++) {
     735           0 :                         ret = os_snprintf(pos, end - pos, " dev_type=%s",
     736             :                                           wps_dev_type_bin2str(
     737           0 :                                                   &cli->sec_dev_types[s * 8],
     738             :                                                   devtype, sizeof(devtype)));
     739           0 :                         if (os_snprintf_error(end - pos, ret))
     740           0 :                                 return pos - buf;
     741           0 :                         pos += ret;
     742             :                 }
     743             : 
     744           1 :                 os_memcpy(name, cli->dev_name, cli->dev_name_len);
     745           1 :                 name[cli->dev_name_len] = '\0';
     746           1 :                 count = (int) cli->dev_name_len - 1;
     747          10 :                 while (count >= 0) {
     748           8 :                         if (is_ctrl_char(name[count]))
     749           0 :                                 name[count] = '_';
     750           8 :                         count--;
     751             :                 }
     752             : 
     753           1 :                 ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
     754           1 :                 if (os_snprintf_error(end - pos, ret))
     755           0 :                         return pos - buf;
     756           1 :                 pos += ret;
     757             :         }
     758             : 
     759          11 :         return pos - buf;
     760             : }
     761             : 
     762             : 
     763             : /**
     764             :  * p2p_attr_text - Build text format description of P2P IE attributes
     765             :  * @data: P2P IE contents
     766             :  * @buf: Buffer for returning text
     767             :  * @end: Pointer to the end of the buf area
     768             :  * Returns: Number of octets written to the buffer or -1 on faikure
     769             :  *
     770             :  * This function can be used to parse P2P IE contents into text format
     771             :  * field=value lines.
     772             :  */
     773          11 : int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
     774             : {
     775             :         struct p2p_message msg;
     776          11 :         char *pos = buf;
     777             :         int ret;
     778             : 
     779          11 :         os_memset(&msg, 0, sizeof(msg));
     780          11 :         if (p2p_parse_p2p_ie(data, &msg))
     781           0 :                 return -1;
     782             : 
     783          11 :         if (msg.capability) {
     784          22 :                 ret = os_snprintf(pos, end - pos,
     785             :                                   "p2p_dev_capab=0x%x\n"
     786             :                                   "p2p_group_capab=0x%x\n",
     787          22 :                                   msg.capability[0], msg.capability[1]);
     788          11 :                 if (os_snprintf_error(end - pos, ret))
     789           0 :                         return pos - buf;
     790          11 :                 pos += ret;
     791             :         }
     792             : 
     793          11 :         if (msg.pri_dev_type) {
     794             :                 char devtype[WPS_DEV_TYPE_BUFSIZE];
     795          11 :                 ret = os_snprintf(pos, end - pos,
     796             :                                   "p2p_primary_device_type=%s\n",
     797             :                                   wps_dev_type_bin2str(msg.pri_dev_type,
     798             :                                                        devtype,
     799             :                                                        sizeof(devtype)));
     800          11 :                 if (os_snprintf_error(end - pos, ret))
     801           0 :                         return pos - buf;
     802          11 :                 pos += ret;
     803             :         }
     804             : 
     805          11 :         ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
     806             :                           msg.device_name);
     807          11 :         if (os_snprintf_error(end - pos, ret))
     808           0 :                 return pos - buf;
     809          11 :         pos += ret;
     810             : 
     811          11 :         if (msg.p2p_device_addr) {
     812          66 :                 ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
     813             :                                   "\n",
     814          66 :                                   MAC2STR(msg.p2p_device_addr));
     815          11 :                 if (os_snprintf_error(end - pos, ret))
     816           0 :                         return pos - buf;
     817          11 :                 pos += ret;
     818             :         }
     819             : 
     820          11 :         ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
     821          11 :                           msg.config_methods);
     822          11 :         if (os_snprintf_error(end - pos, ret))
     823           0 :                 return pos - buf;
     824          11 :         pos += ret;
     825             : 
     826          11 :         ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
     827             :                                   pos, end);
     828          11 :         if (ret < 0)
     829           0 :                 return pos - buf;
     830          11 :         pos += ret;
     831             : 
     832          11 :         return pos - buf;
     833             : }
     834             : 
     835             : 
     836           4 : int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
     837             : {
     838             :         struct p2p_message msg;
     839             : 
     840           4 :         os_memset(&msg, 0, sizeof(msg));
     841           4 :         if (p2p_parse_p2p_ie(p2p_ie, &msg))
     842           0 :                 return 0;
     843             : 
     844           4 :         if (!msg.manageability)
     845           2 :                 return 0;
     846             : 
     847           2 :         return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
     848             : }
     849             : 
     850             : 
     851         945 : u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
     852             : {
     853             :         struct p2p_message msg;
     854             : 
     855         945 :         os_memset(&msg, 0, sizeof(msg));
     856         945 :         if (p2p_parse_p2p_ie(p2p_ie, &msg))
     857           0 :                 return 0;
     858             : 
     859         945 :         if (!msg.capability)
     860           0 :                 return 0;
     861             : 
     862         945 :         return msg.capability[1];
     863             : }
     864             : 
     865             : 
     866        1134 : const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
     867             : {
     868             :         struct p2p_message msg;
     869             : 
     870        1134 :         os_memset(&msg, 0, sizeof(msg));
     871        1134 :         if (p2p_parse_p2p_ie(p2p_ie, &msg))
     872           0 :                 return NULL;
     873             : 
     874        1134 :         if (msg.p2p_device_addr)
     875        1106 :                 return msg.p2p_device_addr;
     876          28 :         if (msg.device_id)
     877          28 :                 return msg.device_id;
     878             : 
     879           0 :         return NULL;
     880             : }

Generated by: LCOV version 1.10