LCOV - code coverage report
Current view: top level - src/p2p - p2p_sd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 202 511 39.5 %
Date: 2013-12-29 Functions: 12 19 63.2 %
Branches: 74 238 31.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Wi-Fi Direct - P2P service discovery
       3                 :            :  * Copyright (c) 2009, 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/gas.h"
      14                 :            : #include "p2p_i.h"
      15                 :            : #include "p2p.h"
      16                 :            : 
      17                 :            : 
      18                 :            : #ifdef CONFIG_WIFI_DISPLAY
      19                 :        443 : static int wfd_wsd_supported(struct wpabuf *wfd)
      20                 :            : {
      21                 :            :         const u8 *pos, *end;
      22                 :            :         u8 subelem;
      23                 :            :         u16 len;
      24                 :            : 
      25         [ +  + ]:        443 :         if (wfd == NULL)
      26                 :         50 :                 return 0;
      27                 :            : 
      28                 :        393 :         pos = wpabuf_head(wfd);
      29                 :        393 :         end = pos + wpabuf_len(wfd);
      30                 :            : 
      31         [ +  - ]:        393 :         while (pos + 3 <= end) {
      32                 :        393 :                 subelem = *pos++;
      33                 :        393 :                 len = WPA_GET_BE16(pos);
      34                 :        393 :                 pos += 2;
      35         [ -  + ]:        393 :                 if (pos + len > end)
      36                 :          0 :                         break;
      37                 :            : 
      38 [ +  - ][ +  - ]:        393 :                 if (subelem == WFD_SUBELEM_DEVICE_INFO && len >= 6) {
      39                 :        393 :                         u16 info = WPA_GET_BE16(pos);
      40                 :        393 :                         return !!(info & 0x0040);
      41                 :            :                 }
      42                 :            : 
      43                 :          0 :                 pos += len;
      44                 :            :         }
      45                 :            : 
      46                 :        443 :         return 0;
      47                 :            : }
      48                 :            : #endif /* CONFIG_WIFI_DISPLAY */
      49                 :            : 
      50                 :        443 : struct p2p_sd_query * p2p_pending_sd_req(struct p2p_data *p2p,
      51                 :            :                                          struct p2p_device *dev)
      52                 :            : {
      53                 :            :         struct p2p_sd_query *q;
      54                 :        443 :         int wsd = 0;
      55                 :            : 
      56         [ -  + ]:        443 :         if (!(dev->info.dev_capab & P2P_DEV_CAPAB_SERVICE_DISCOVERY))
      57                 :          0 :                 return NULL; /* peer does not support SD */
      58                 :            : #ifdef CONFIG_WIFI_DISPLAY
      59         [ -  + ]:        443 :         if (wfd_wsd_supported(dev->info.wfd_subelems))
      60                 :          0 :                 wsd = 1;
      61                 :            : #endif /* CONFIG_WIFI_DISPLAY */
      62                 :            : 
      63         [ +  + ]:        455 :         for (q = p2p->sd_queries; q; q = q->next) {
      64                 :            :                 /* Use WSD only if the peer indicates support or it */
      65 [ -  + ][ #  # ]:         24 :                 if (q->wsd && !wsd)
      66                 :          0 :                         continue;
      67 [ +  - ][ +  + ]:         24 :                 if (q->for_all_peers && !(dev->flags & P2P_DEV_SD_INFO))
      68                 :         12 :                         return q;
      69 [ -  + ][ #  # ]:         12 :                 if (!q->for_all_peers &&
      70                 :          0 :                     os_memcmp(q->peer, dev->info.p2p_device_addr, ETH_ALEN) ==
      71                 :            :                     0)
      72                 :          0 :                         return q;
      73                 :            :         }
      74                 :            : 
      75                 :        443 :         return NULL;
      76                 :            : }
      77                 :            : 
      78                 :            : 
      79                 :          0 : static int p2p_unlink_sd_query(struct p2p_data *p2p,
      80                 :            :                                struct p2p_sd_query *query)
      81                 :            : {
      82                 :            :         struct p2p_sd_query *q, *prev;
      83                 :          0 :         q = p2p->sd_queries;
      84                 :          0 :         prev = NULL;
      85         [ #  # ]:          0 :         while (q) {
      86         [ #  # ]:          0 :                 if (q == query) {
      87         [ #  # ]:          0 :                         if (prev)
      88                 :          0 :                                 prev->next = q->next;
      89                 :            :                         else
      90                 :          0 :                                 p2p->sd_queries = q->next;
      91         [ #  # ]:          0 :                         if (p2p->sd_query == query)
      92                 :          0 :                                 p2p->sd_query = NULL;
      93                 :          0 :                         return 1;
      94                 :            :                 }
      95                 :          0 :                 prev = q;
      96                 :          0 :                 q = q->next;
      97                 :            :         }
      98                 :          0 :         return 0;
      99                 :            : }
     100                 :            : 
     101                 :            : 
     102                 :          6 : static void p2p_free_sd_query(struct p2p_sd_query *q)
     103                 :            : {
     104         [ -  + ]:          6 :         if (q == NULL)
     105                 :          6 :                 return;
     106                 :          6 :         wpabuf_free(q->tlvs);
     107                 :          6 :         os_free(q);
     108                 :            : }
     109                 :            : 
     110                 :            : 
     111                 :        666 : void p2p_free_sd_queries(struct p2p_data *p2p)
     112                 :            : {
     113                 :            :         struct p2p_sd_query *q, *prev;
     114                 :        666 :         q = p2p->sd_queries;
     115                 :        666 :         p2p->sd_queries = NULL;
     116         [ +  + ]:        672 :         while (q) {
     117                 :          6 :                 prev = q;
     118                 :          6 :                 q = q->next;
     119                 :          6 :                 p2p_free_sd_query(prev);
     120                 :            :         }
     121                 :        666 : }
     122                 :            : 
     123                 :            : 
     124                 :          6 : static struct wpabuf * p2p_build_sd_query(u16 update_indic,
     125                 :            :                                           struct wpabuf *tlvs)
     126                 :            : {
     127                 :            :         struct wpabuf *buf;
     128                 :            :         u8 *len_pos;
     129                 :            : 
     130                 :          6 :         buf = gas_anqp_build_initial_req(0, 100 + wpabuf_len(tlvs));
     131         [ -  + ]:          6 :         if (buf == NULL)
     132                 :          0 :                 return NULL;
     133                 :            : 
     134                 :            :         /* ANQP Query Request Frame */
     135                 :          6 :         len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
     136                 :          6 :         wpabuf_put_be24(buf, OUI_WFA);
     137                 :          6 :         wpabuf_put_u8(buf, P2P_OUI_TYPE);
     138                 :          6 :         wpabuf_put_le16(buf, update_indic); /* Service Update Indicator */
     139                 :          6 :         wpabuf_put_buf(buf, tlvs);
     140                 :          6 :         gas_anqp_set_element_len(buf, len_pos);
     141                 :            : 
     142                 :          6 :         gas_anqp_set_len(buf);
     143                 :            : 
     144                 :          6 :         return buf;
     145                 :            : }
     146                 :            : 
     147                 :            : 
     148                 :          0 : static void p2p_send_gas_comeback_req(struct p2p_data *p2p, const u8 *dst,
     149                 :            :                                       u8 dialog_token, int freq)
     150                 :            : {
     151                 :            :         struct wpabuf *req;
     152                 :            : 
     153                 :          0 :         req = gas_build_comeback_req(dialog_token);
     154         [ #  # ]:          0 :         if (req == NULL)
     155                 :          0 :                 return;
     156                 :            : 
     157                 :          0 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
     158         [ #  # ]:          0 :         if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr, dst,
     159                 :          0 :                             wpabuf_head(req), wpabuf_len(req), 200) < 0)
     160                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     161                 :            : 
     162                 :          0 :         wpabuf_free(req);
     163                 :            : }
     164                 :            : 
     165                 :            : 
     166                 :          6 : static struct wpabuf * p2p_build_sd_response(u8 dialog_token, u16 status_code,
     167                 :            :                                              u16 comeback_delay,
     168                 :            :                                              u16 update_indic,
     169                 :            :                                              const struct wpabuf *tlvs)
     170                 :            : {
     171                 :            :         struct wpabuf *buf;
     172                 :            :         u8 *len_pos;
     173                 :            : 
     174         [ +  - ]:         12 :         buf = gas_anqp_build_initial_resp(dialog_token, status_code,
     175                 :            :                                           comeback_delay,
     176                 :          6 :                                           100 + (tlvs ? wpabuf_len(tlvs) : 0));
     177         [ -  + ]:          6 :         if (buf == NULL)
     178                 :          0 :                 return NULL;
     179                 :            : 
     180         [ +  - ]:          6 :         if (tlvs) {
     181                 :            :                 /* ANQP Query Response Frame */
     182                 :          6 :                 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
     183                 :          6 :                 wpabuf_put_be24(buf, OUI_WFA);
     184                 :          6 :                 wpabuf_put_u8(buf, P2P_OUI_TYPE);
     185                 :            :                  /* Service Update Indicator */
     186                 :          6 :                 wpabuf_put_le16(buf, update_indic);
     187                 :          6 :                 wpabuf_put_buf(buf, tlvs);
     188                 :          6 :                 gas_anqp_set_element_len(buf, len_pos);
     189                 :            :         }
     190                 :            : 
     191                 :          6 :         gas_anqp_set_len(buf);
     192                 :            : 
     193                 :          6 :         return buf;
     194                 :            : }
     195                 :            : 
     196                 :            : 
     197                 :          0 : static struct wpabuf * p2p_build_gas_comeback_resp(u8 dialog_token,
     198                 :            :                                                    u16 status_code,
     199                 :            :                                                    u16 update_indic,
     200                 :            :                                                    const u8 *data, size_t len,
     201                 :            :                                                    u8 frag_id, u8 more,
     202                 :            :                                                    u16 total_len)
     203                 :            : {
     204                 :            :         struct wpabuf *buf;
     205                 :            : 
     206                 :          0 :         buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
     207                 :            :                                            more, 0, 100 + len);
     208         [ #  # ]:          0 :         if (buf == NULL)
     209                 :          0 :                 return NULL;
     210                 :            : 
     211         [ #  # ]:          0 :         if (frag_id == 0) {
     212                 :            :                 /* ANQP Query Response Frame */
     213                 :          0 :                 wpabuf_put_le16(buf, ANQP_VENDOR_SPECIFIC); /* Info ID */
     214                 :          0 :                 wpabuf_put_le16(buf, 3 + 1 + 2 + total_len);
     215                 :          0 :                 wpabuf_put_be24(buf, OUI_WFA);
     216                 :          0 :                 wpabuf_put_u8(buf, P2P_OUI_TYPE);
     217                 :            :                 /* Service Update Indicator */
     218                 :          0 :                 wpabuf_put_le16(buf, update_indic);
     219                 :            :         }
     220                 :            : 
     221                 :          0 :         wpabuf_put_data(buf, data, len);
     222                 :          0 :         gas_anqp_set_len(buf);
     223                 :            : 
     224                 :          0 :         return buf;
     225                 :            : }
     226                 :            : 
     227                 :            : 
     228                 :          6 : int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev)
     229                 :            : {
     230                 :            :         struct wpabuf *req;
     231                 :          6 :         int ret = 0;
     232                 :            :         struct p2p_sd_query *query;
     233                 :            :         int freq;
     234                 :            : 
     235         [ +  - ]:          6 :         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
     236         [ -  + ]:          6 :         if (freq <= 0) {
     237                 :          0 :                 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
     238                 :            :                         MACSTR " to send SD Request",
     239                 :          0 :                         MAC2STR(dev->info.p2p_device_addr));
     240                 :          0 :                 return -1;
     241                 :            :         }
     242                 :            : 
     243                 :          6 :         query = p2p_pending_sd_req(p2p, dev);
     244         [ -  + ]:          6 :         if (query == NULL)
     245                 :          0 :                 return -1;
     246                 :            : 
     247                 :          6 :         p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
     248                 :         36 :                 MAC2STR(dev->info.p2p_device_addr));
     249                 :            : 
     250                 :          6 :         req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
     251         [ -  + ]:          6 :         if (req == NULL)
     252                 :          0 :                 return -1;
     253                 :            : 
     254                 :          6 :         p2p->sd_peer = dev;
     255                 :          6 :         p2p->sd_query = query;
     256                 :          6 :         p2p->pending_action_state = P2P_PENDING_SD;
     257                 :            : 
     258         [ -  + ]:         12 :         if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
     259                 :          6 :                             p2p->cfg->dev_addr, dev->info.p2p_device_addr,
     260                 :          6 :                             wpabuf_head(req), wpabuf_len(req), 5000) < 0) {
     261                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     262                 :          0 :                 ret = -1;
     263                 :            :         }
     264                 :            : 
     265                 :          6 :         wpabuf_free(req);
     266                 :            : 
     267                 :          6 :         return ret;
     268                 :            : }
     269                 :            : 
     270                 :            : 
     271                 :          6 : void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
     272                 :            :                             const u8 *data, size_t len, int rx_freq)
     273                 :            : {
     274                 :          6 :         const u8 *pos = data;
     275                 :          6 :         const u8 *end = data + len;
     276                 :            :         const u8 *next;
     277                 :            :         u8 dialog_token;
     278                 :            :         u16 slen;
     279                 :            :         int freq;
     280                 :            :         u16 update_indic;
     281                 :            : 
     282                 :            : 
     283         [ -  + ]:          6 :         if (p2p->cfg->sd_request == NULL)
     284                 :          0 :                 return;
     285                 :            : 
     286         [ +  - ]:          6 :         if (rx_freq > 0)
     287                 :          6 :                 freq = rx_freq;
     288                 :            :         else
     289                 :          0 :                 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
     290                 :          0 :                                            p2p->cfg->channel);
     291         [ -  + ]:          6 :         if (freq < 0)
     292                 :          0 :                 return;
     293                 :            : 
     294         [ -  + ]:          6 :         if (len < 1 + 2)
     295                 :          0 :                 return;
     296                 :            : 
     297                 :          6 :         dialog_token = *pos++;
     298                 :          6 :         p2p_dbg(p2p, "GAS Initial Request from " MACSTR
     299                 :            :                 " (dialog token %u, freq %d)",
     300                 :         36 :                 MAC2STR(sa), dialog_token, rx_freq);
     301                 :            : 
     302         [ -  + ]:          6 :         if (*pos != WLAN_EID_ADV_PROTO) {
     303                 :          0 :                 p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
     304                 :          0 :                 return;
     305                 :            :         }
     306                 :          6 :         pos++;
     307                 :            : 
     308                 :          6 :         slen = *pos++;
     309                 :          6 :         next = pos + slen;
     310 [ +  - ][ -  + ]:          6 :         if (next > end || slen < 2) {
     311                 :          0 :                 p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
     312                 :          0 :                 return;
     313                 :            :         }
     314                 :          6 :         pos++; /* skip QueryRespLenLimit and PAME-BI */
     315                 :            : 
     316         [ -  + ]:          6 :         if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
     317                 :          0 :                 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
     318                 :          0 :                         *pos);
     319                 :          0 :                 return;
     320                 :            :         }
     321                 :            : 
     322                 :          6 :         pos = next;
     323                 :            :         /* Query Request */
     324         [ -  + ]:          6 :         if (pos + 2 > end)
     325                 :          0 :                 return;
     326                 :          6 :         slen = WPA_GET_LE16(pos);
     327                 :          6 :         pos += 2;
     328         [ -  + ]:          6 :         if (pos + slen > end)
     329                 :          0 :                 return;
     330                 :          6 :         end = pos + slen;
     331                 :            : 
     332                 :            :         /* ANQP Query Request */
     333         [ -  + ]:          6 :         if (pos + 4 > end)
     334                 :          0 :                 return;
     335         [ -  + ]:          6 :         if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
     336                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
     337                 :          0 :                 return;
     338                 :            :         }
     339                 :          6 :         pos += 2;
     340                 :            : 
     341                 :          6 :         slen = WPA_GET_LE16(pos);
     342                 :          6 :         pos += 2;
     343 [ +  - ][ -  + ]:          6 :         if (pos + slen > end || slen < 3 + 1) {
     344                 :          0 :                 p2p_dbg(p2p, "Invalid ANQP Query Request length");
     345                 :          0 :                 return;
     346                 :            :         }
     347                 :            : 
     348         [ -  + ]:          6 :         if (WPA_GET_BE24(pos) != OUI_WFA) {
     349                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
     350                 :          0 :                 return;
     351                 :            :         }
     352                 :          6 :         pos += 3;
     353                 :            : 
     354         [ -  + ]:          6 :         if (*pos != P2P_OUI_TYPE) {
     355                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
     356                 :          0 :                 return;
     357                 :            :         }
     358                 :          6 :         pos++;
     359                 :            : 
     360         [ -  + ]:          6 :         if (pos + 2 > end)
     361                 :          0 :                 return;
     362                 :          6 :         update_indic = WPA_GET_LE16(pos);
     363                 :          6 :         p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
     364                 :          6 :         pos += 2;
     365                 :            : 
     366                 :          6 :         p2p->cfg->sd_request(p2p->cfg->cb_ctx, freq, sa, dialog_token,
     367                 :          6 :                              update_indic, pos, end - pos);
     368                 :            :         /* the response will be indicated with a call to p2p_sd_response() */
     369                 :            : }
     370                 :            : 
     371                 :            : 
     372                 :          6 : void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
     373                 :            :                      u8 dialog_token, const struct wpabuf *resp_tlvs)
     374                 :            : {
     375                 :            :         struct wpabuf *resp;
     376                 :            : 
     377                 :            :         /* TODO: fix the length limit to match with the maximum frame length */
     378         [ -  + ]:          6 :         if (wpabuf_len(resp_tlvs) > 1400) {
     379                 :          0 :                 p2p_dbg(p2p, "SD response long enough to require fragmentation");
     380         [ #  # ]:          0 :                 if (p2p->sd_resp) {
     381                 :            :                         /*
     382                 :            :                          * TODO: Could consider storing the fragmented response
     383                 :            :                          * separately for each peer to avoid having to drop old
     384                 :            :                          * one if there is more than one pending SD query.
     385                 :            :                          * Though, that would eat more memory, so there are
     386                 :            :                          * also benefits to just using a single buffer.
     387                 :            :                          */
     388                 :          0 :                         p2p_dbg(p2p, "Drop previous SD response");
     389                 :          0 :                         wpabuf_free(p2p->sd_resp);
     390                 :            :                 }
     391                 :          0 :                 p2p->sd_resp = wpabuf_dup(resp_tlvs);
     392         [ #  # ]:          0 :                 if (p2p->sd_resp == NULL) {
     393                 :          0 :                         p2p_err(p2p, "Failed to allocate SD response fragmentation area");
     394                 :          0 :                         return;
     395                 :            :                 }
     396                 :          0 :                 os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
     397                 :          0 :                 p2p->sd_resp_dialog_token = dialog_token;
     398                 :          0 :                 p2p->sd_resp_pos = 0;
     399                 :          0 :                 p2p->sd_frag_id = 0;
     400                 :          0 :                 resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
     401                 :          0 :                                              1, p2p->srv_update_indic, NULL);
     402                 :            :         } else {
     403                 :          6 :                 p2p_dbg(p2p, "SD response fits in initial response");
     404                 :          6 :                 resp = p2p_build_sd_response(dialog_token,
     405                 :            :                                              WLAN_STATUS_SUCCESS, 0,
     406                 :          6 :                                              p2p->srv_update_indic, resp_tlvs);
     407                 :            :         }
     408         [ -  + ]:          6 :         if (resp == NULL)
     409                 :          0 :                 return;
     410                 :            : 
     411                 :          6 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
     412         [ -  + ]:         12 :         if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
     413                 :          6 :                             p2p->cfg->dev_addr,
     414                 :          6 :                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
     415                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     416                 :            : 
     417                 :          6 :         wpabuf_free(resp);
     418                 :            : }
     419                 :            : 
     420                 :            : 
     421                 :          6 : void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
     422                 :            :                              const u8 *data, size_t len, int rx_freq)
     423                 :            : {
     424                 :          6 :         const u8 *pos = data;
     425                 :          6 :         const u8 *end = data + len;
     426                 :            :         const u8 *next;
     427                 :            :         u8 dialog_token;
     428                 :            :         u16 status_code;
     429                 :            :         u16 comeback_delay;
     430                 :            :         u16 slen;
     431                 :            :         u16 update_indic;
     432                 :            : 
     433 [ +  - ][ +  - ]:          6 :         if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
                 [ -  + ]
     434                 :          6 :             os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
     435                 :          0 :                 p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
     436                 :          0 :                         MACSTR, MAC2STR(sa));
     437                 :          0 :                 return;
     438                 :            :         }
     439                 :          6 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
     440                 :          6 :         p2p_clear_timeout(p2p);
     441                 :            : 
     442                 :          6 :         p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
     443                 :         36 :                 MAC2STR(sa), (int) len);
     444                 :            : 
     445         [ -  + ]:          6 :         if (len < 5 + 2) {
     446                 :          0 :                 p2p_dbg(p2p, "Too short GAS Initial Response frame");
     447                 :          0 :                 return;
     448                 :            :         }
     449                 :            : 
     450                 :          6 :         dialog_token = *pos++;
     451                 :            :         /* TODO: check dialog_token match */
     452                 :          6 :         status_code = WPA_GET_LE16(pos);
     453                 :          6 :         pos += 2;
     454                 :          6 :         comeback_delay = WPA_GET_LE16(pos);
     455                 :          6 :         pos += 2;
     456                 :          6 :         p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
     457                 :            :                 dialog_token, status_code, comeback_delay);
     458         [ -  + ]:          6 :         if (status_code) {
     459                 :          0 :                 p2p_dbg(p2p, "Service Discovery failed: status code %u",
     460                 :            :                         status_code);
     461                 :          0 :                 return;
     462                 :            :         }
     463                 :            : 
     464         [ -  + ]:          6 :         if (*pos != WLAN_EID_ADV_PROTO) {
     465                 :          0 :                 p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
     466                 :          0 :                 return;
     467                 :            :         }
     468                 :          6 :         pos++;
     469                 :            : 
     470                 :          6 :         slen = *pos++;
     471                 :          6 :         next = pos + slen;
     472 [ +  - ][ -  + ]:          6 :         if (next > end || slen < 2) {
     473                 :          0 :                 p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
     474                 :          0 :                 return;
     475                 :            :         }
     476                 :          6 :         pos++; /* skip QueryRespLenLimit and PAME-BI */
     477                 :            : 
     478         [ -  + ]:          6 :         if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
     479                 :          0 :                 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
     480                 :          0 :                         *pos);
     481                 :          0 :                 return;
     482                 :            :         }
     483                 :            : 
     484                 :          6 :         pos = next;
     485                 :            :         /* Query Response */
     486         [ -  + ]:          6 :         if (pos + 2 > end) {
     487                 :          0 :                 p2p_dbg(p2p, "Too short Query Response");
     488                 :          0 :                 return;
     489                 :            :         }
     490                 :          6 :         slen = WPA_GET_LE16(pos);
     491                 :          6 :         pos += 2;
     492                 :          6 :         p2p_dbg(p2p, "Query Response Length: %d", slen);
     493         [ -  + ]:          6 :         if (pos + slen > end) {
     494                 :          0 :                 p2p_dbg(p2p, "Not enough Query Response data");
     495                 :          0 :                 return;
     496                 :            :         }
     497                 :          6 :         end = pos + slen;
     498                 :            : 
     499         [ -  + ]:          6 :         if (comeback_delay) {
     500                 :          0 :                 p2p_dbg(p2p, "Fragmented response - request fragments");
     501         [ #  # ]:          0 :                 if (p2p->sd_rx_resp) {
     502                 :          0 :                         p2p_dbg(p2p, "Drop old SD reassembly buffer");
     503                 :          0 :                         wpabuf_free(p2p->sd_rx_resp);
     504                 :          0 :                         p2p->sd_rx_resp = NULL;
     505                 :            :                 }
     506                 :          0 :                 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
     507                 :          0 :                 return;
     508                 :            :         }
     509                 :            : 
     510                 :            :         /* ANQP Query Response */
     511         [ -  + ]:          6 :         if (pos + 4 > end)
     512                 :          0 :                 return;
     513         [ -  + ]:          6 :         if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
     514                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
     515                 :          0 :                 return;
     516                 :            :         }
     517                 :          6 :         pos += 2;
     518                 :            : 
     519                 :          6 :         slen = WPA_GET_LE16(pos);
     520                 :          6 :         pos += 2;
     521 [ +  - ][ -  + ]:          6 :         if (pos + slen > end || slen < 3 + 1) {
     522                 :          0 :                 p2p_dbg(p2p, "Invalid ANQP Query Response length");
     523                 :          0 :                 return;
     524                 :            :         }
     525                 :            : 
     526         [ -  + ]:          6 :         if (WPA_GET_BE24(pos) != OUI_WFA) {
     527                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
     528                 :          0 :                 return;
     529                 :            :         }
     530                 :          6 :         pos += 3;
     531                 :            : 
     532         [ -  + ]:          6 :         if (*pos != P2P_OUI_TYPE) {
     533                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
     534                 :          0 :                 return;
     535                 :            :         }
     536                 :          6 :         pos++;
     537                 :            : 
     538         [ -  + ]:          6 :         if (pos + 2 > end)
     539                 :          0 :                 return;
     540                 :          6 :         update_indic = WPA_GET_LE16(pos);
     541                 :          6 :         p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
     542                 :          6 :         pos += 2;
     543                 :            : 
     544                 :          6 :         p2p->sd_peer->flags |= P2P_DEV_SD_INFO;
     545                 :          6 :         p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
     546                 :          6 :         p2p->sd_peer = NULL;
     547                 :            : 
     548         [ +  - ]:          6 :         if (p2p->sd_query) {
     549         [ -  + ]:          6 :                 if (!p2p->sd_query->for_all_peers) {
     550                 :            :                         struct p2p_sd_query *q;
     551                 :          0 :                         p2p_dbg(p2p, "Remove completed SD query %p",
     552                 :            :                                 p2p->sd_query);
     553                 :          0 :                         q = p2p->sd_query;
     554                 :          0 :                         p2p_unlink_sd_query(p2p, p2p->sd_query);
     555                 :          0 :                         p2p_free_sd_query(q);
     556                 :            :                 }
     557                 :          6 :                 p2p->sd_query = NULL;
     558                 :            :         }
     559                 :            : 
     560         [ +  - ]:          6 :         if (p2p->cfg->sd_response)
     561                 :          6 :                 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
     562                 :          6 :                                       pos, end - pos);
     563                 :          6 :         p2p_continue_find(p2p);
     564                 :            : }
     565                 :            : 
     566                 :            : 
     567                 :          0 : void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
     568                 :            :                              const u8 *data, size_t len, int rx_freq)
     569                 :            : {
     570                 :            :         struct wpabuf *resp;
     571                 :            :         u8 dialog_token;
     572                 :            :         size_t frag_len;
     573                 :          0 :         int more = 0;
     574                 :            : 
     575                 :          0 :         wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
     576         [ #  # ]:          0 :         if (len < 1)
     577                 :          0 :                 return;
     578                 :          0 :         dialog_token = *data;
     579                 :          0 :         p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
     580         [ #  # ]:          0 :         if (dialog_token != p2p->sd_resp_dialog_token) {
     581                 :          0 :                 p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
     582                 :            :                         dialog_token);
     583                 :          0 :                 return;
     584                 :            :         }
     585                 :            : 
     586         [ #  # ]:          0 :         if (p2p->sd_resp == NULL) {
     587                 :          0 :                 p2p_dbg(p2p, "No pending SD response fragment available");
     588                 :          0 :                 return;
     589                 :            :         }
     590         [ #  # ]:          0 :         if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
     591                 :          0 :                 p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
     592                 :          0 :                         MAC2STR(sa));
     593                 :          0 :                 return;
     594                 :            :         }
     595                 :            : 
     596                 :          0 :         frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
     597         [ #  # ]:          0 :         if (frag_len > 1400) {
     598                 :          0 :                 frag_len = 1400;
     599                 :          0 :                 more = 1;
     600                 :            :         }
     601                 :          0 :         resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
     602                 :          0 :                                            p2p->srv_update_indic,
     603                 :          0 :                                            wpabuf_head_u8(p2p->sd_resp) +
     604                 :            :                                            p2p->sd_resp_pos, frag_len,
     605                 :          0 :                                            p2p->sd_frag_id, more,
     606                 :          0 :                                            wpabuf_len(p2p->sd_resp));
     607         [ #  # ]:          0 :         if (resp == NULL)
     608                 :          0 :                 return;
     609                 :          0 :         p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
     610                 :          0 :                 p2p->sd_frag_id, more, (int) frag_len);
     611                 :          0 :         p2p->sd_frag_id++;
     612                 :          0 :         p2p->sd_resp_pos += frag_len;
     613                 :            : 
     614         [ #  # ]:          0 :         if (more) {
     615                 :          0 :                 p2p_dbg(p2p, "%d more bytes remain to be sent",
     616                 :          0 :                         (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
     617                 :            :         } else {
     618                 :          0 :                 p2p_dbg(p2p, "All fragments of SD response sent");
     619                 :          0 :                 wpabuf_free(p2p->sd_resp);
     620                 :          0 :                 p2p->sd_resp = NULL;
     621                 :            :         }
     622                 :            : 
     623                 :          0 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
     624         [ #  # ]:          0 :         if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
     625                 :          0 :                             p2p->cfg->dev_addr,
     626                 :          0 :                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
     627                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     628                 :            : 
     629                 :          0 :         wpabuf_free(resp);
     630                 :            : }
     631                 :            : 
     632                 :            : 
     633                 :          0 : void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
     634                 :            :                               const u8 *data, size_t len, int rx_freq)
     635                 :            : {
     636                 :          0 :         const u8 *pos = data;
     637                 :          0 :         const u8 *end = data + len;
     638                 :            :         const u8 *next;
     639                 :            :         u8 dialog_token;
     640                 :            :         u16 status_code;
     641                 :            :         u8 frag_id;
     642                 :            :         u8 more_frags;
     643                 :            :         u16 comeback_delay;
     644                 :            :         u16 slen;
     645                 :            : 
     646                 :          0 :         wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
     647                 :            : 
     648 [ #  # ][ #  # ]:          0 :         if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
                 [ #  # ]
     649                 :          0 :             os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
     650                 :          0 :                 p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
     651                 :          0 :                         MACSTR, MAC2STR(sa));
     652                 :          0 :                 return;
     653                 :            :         }
     654                 :          0 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
     655                 :          0 :         p2p_clear_timeout(p2p);
     656                 :            : 
     657                 :          0 :         p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
     658                 :          0 :                 MAC2STR(sa), (int) len);
     659                 :            : 
     660         [ #  # ]:          0 :         if (len < 6 + 2) {
     661                 :          0 :                 p2p_dbg(p2p, "Too short GAS Comeback Response frame");
     662                 :          0 :                 return;
     663                 :            :         }
     664                 :            : 
     665                 :          0 :         dialog_token = *pos++;
     666                 :            :         /* TODO: check dialog_token match */
     667                 :          0 :         status_code = WPA_GET_LE16(pos);
     668                 :          0 :         pos += 2;
     669                 :          0 :         frag_id = *pos & 0x7f;
     670                 :          0 :         more_frags = (*pos & 0x80) >> 7;
     671                 :          0 :         pos++;
     672                 :          0 :         comeback_delay = WPA_GET_LE16(pos);
     673                 :          0 :         pos += 2;
     674                 :          0 :         p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
     675                 :            :                 "comeback_delay=%u",
     676                 :            :                 dialog_token, status_code, frag_id, more_frags,
     677                 :            :                 comeback_delay);
     678                 :            :         /* TODO: check frag_id match */
     679         [ #  # ]:          0 :         if (status_code) {
     680                 :          0 :                 p2p_dbg(p2p, "Service Discovery failed: status code %u",
     681                 :            :                         status_code);
     682                 :          0 :                 return;
     683                 :            :         }
     684                 :            : 
     685         [ #  # ]:          0 :         if (*pos != WLAN_EID_ADV_PROTO) {
     686                 :          0 :                 p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
     687                 :          0 :                         *pos);
     688                 :          0 :                 return;
     689                 :            :         }
     690                 :          0 :         pos++;
     691                 :            : 
     692                 :          0 :         slen = *pos++;
     693                 :          0 :         next = pos + slen;
     694 [ #  # ][ #  # ]:          0 :         if (next > end || slen < 2) {
     695                 :          0 :                 p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
     696                 :          0 :                 return;
     697                 :            :         }
     698                 :          0 :         pos++; /* skip QueryRespLenLimit and PAME-BI */
     699                 :            : 
     700         [ #  # ]:          0 :         if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
     701                 :          0 :                 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
     702                 :          0 :                         *pos);
     703                 :          0 :                 return;
     704                 :            :         }
     705                 :            : 
     706                 :          0 :         pos = next;
     707                 :            :         /* Query Response */
     708         [ #  # ]:          0 :         if (pos + 2 > end) {
     709                 :          0 :                 p2p_dbg(p2p, "Too short Query Response");
     710                 :          0 :                 return;
     711                 :            :         }
     712                 :          0 :         slen = WPA_GET_LE16(pos);
     713                 :          0 :         pos += 2;
     714                 :          0 :         p2p_dbg(p2p, "Query Response Length: %d", slen);
     715         [ #  # ]:          0 :         if (pos + slen > end) {
     716                 :          0 :                 p2p_dbg(p2p, "Not enough Query Response data");
     717                 :          0 :                 return;
     718                 :            :         }
     719         [ #  # ]:          0 :         if (slen == 0) {
     720                 :          0 :                 p2p_dbg(p2p, "No Query Response data");
     721                 :          0 :                 return;
     722                 :            :         }
     723                 :          0 :         end = pos + slen;
     724                 :            : 
     725         [ #  # ]:          0 :         if (p2p->sd_rx_resp) {
     726                 :            :                  /*
     727                 :            :                   * ANQP header is only included in the first fragment; rest of
     728                 :            :                   * the fragments start with continue TLVs.
     729                 :            :                   */
     730                 :          0 :                 goto skip_nqp_header;
     731                 :            :         }
     732                 :            : 
     733                 :            :         /* ANQP Query Response */
     734         [ #  # ]:          0 :         if (pos + 4 > end)
     735                 :          0 :                 return;
     736         [ #  # ]:          0 :         if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
     737                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
     738                 :          0 :                 return;
     739                 :            :         }
     740                 :          0 :         pos += 2;
     741                 :            : 
     742                 :          0 :         slen = WPA_GET_LE16(pos);
     743                 :          0 :         pos += 2;
     744                 :          0 :         p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
     745         [ #  # ]:          0 :         if (slen < 3 + 1) {
     746                 :          0 :                 p2p_dbg(p2p, "Invalid ANQP Query Response length");
     747                 :          0 :                 return;
     748                 :            :         }
     749         [ #  # ]:          0 :         if (pos + 4 > end)
     750                 :          0 :                 return;
     751                 :            : 
     752         [ #  # ]:          0 :         if (WPA_GET_BE24(pos) != OUI_WFA) {
     753                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
     754                 :          0 :                 return;
     755                 :            :         }
     756                 :          0 :         pos += 3;
     757                 :            : 
     758         [ #  # ]:          0 :         if (*pos != P2P_OUI_TYPE) {
     759                 :          0 :                 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
     760                 :          0 :                 return;
     761                 :            :         }
     762                 :          0 :         pos++;
     763                 :            : 
     764         [ #  # ]:          0 :         if (pos + 2 > end)
     765                 :          0 :                 return;
     766                 :          0 :         p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
     767                 :          0 :         p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
     768                 :          0 :         pos += 2;
     769                 :            : 
     770                 :            : skip_nqp_header:
     771         [ #  # ]:          0 :         if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
     772                 :          0 :                 return;
     773                 :          0 :         wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
     774                 :          0 :         p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
     775                 :          0 :                 (unsigned int) wpabuf_len(p2p->sd_rx_resp));
     776                 :            : 
     777         [ #  # ]:          0 :         if (more_frags) {
     778                 :          0 :                 p2p_dbg(p2p, "More fragments remains");
     779                 :            :                 /* TODO: what would be a good size limit? */
     780         [ #  # ]:          0 :                 if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
     781                 :          0 :                         wpabuf_free(p2p->sd_rx_resp);
     782                 :          0 :                         p2p->sd_rx_resp = NULL;
     783                 :          0 :                         p2p_dbg(p2p, "Too long SD response - drop it");
     784                 :          0 :                         return;
     785                 :            :                 }
     786                 :          0 :                 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
     787                 :          0 :                 return;
     788                 :            :         }
     789                 :            : 
     790                 :          0 :         p2p->sd_peer->flags |= P2P_DEV_SD_INFO;
     791                 :          0 :         p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
     792                 :          0 :         p2p->sd_peer = NULL;
     793                 :            : 
     794         [ #  # ]:          0 :         if (p2p->sd_query) {
     795         [ #  # ]:          0 :                 if (!p2p->sd_query->for_all_peers) {
     796                 :            :                         struct p2p_sd_query *q;
     797                 :          0 :                         p2p_dbg(p2p, "Remove completed SD query %p",
     798                 :            :                                 p2p->sd_query);
     799                 :          0 :                         q = p2p->sd_query;
     800                 :          0 :                         p2p_unlink_sd_query(p2p, p2p->sd_query);
     801                 :          0 :                         p2p_free_sd_query(q);
     802                 :            :                 }
     803                 :          0 :                 p2p->sd_query = NULL;
     804                 :            :         }
     805                 :            : 
     806         [ #  # ]:          0 :         if (p2p->cfg->sd_response)
     807                 :          0 :                 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
     808                 :          0 :                                       p2p->sd_rx_update_indic,
     809                 :          0 :                                       wpabuf_head(p2p->sd_rx_resp),
     810                 :          0 :                                       wpabuf_len(p2p->sd_rx_resp));
     811                 :          0 :         wpabuf_free(p2p->sd_rx_resp);
     812                 :          0 :         p2p->sd_rx_resp = NULL;
     813                 :            : 
     814                 :          0 :         p2p_continue_find(p2p);
     815                 :            : }
     816                 :            : 
     817                 :            : 
     818                 :          6 : void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
     819                 :            :                       const struct wpabuf *tlvs)
     820                 :            : {
     821                 :            :         struct p2p_sd_query *q;
     822                 :            : 
     823                 :          6 :         q = os_zalloc(sizeof(*q));
     824         [ -  + ]:          6 :         if (q == NULL)
     825                 :          0 :                 return NULL;
     826                 :            : 
     827         [ -  + ]:          6 :         if (dst)
     828                 :          0 :                 os_memcpy(q->peer, dst, ETH_ALEN);
     829                 :            :         else
     830                 :          6 :                 q->for_all_peers = 1;
     831                 :            : 
     832                 :          6 :         q->tlvs = wpabuf_dup(tlvs);
     833         [ -  + ]:          6 :         if (q->tlvs == NULL) {
     834                 :          0 :                 p2p_free_sd_query(q);
     835                 :          0 :                 return NULL;
     836                 :            :         }
     837                 :            : 
     838                 :          6 :         q->next = p2p->sd_queries;
     839                 :          6 :         p2p->sd_queries = q;
     840                 :          6 :         p2p_dbg(p2p, "Added SD Query %p", q);
     841                 :            : 
     842         [ +  - ]:          6 :         if (dst == NULL) {
     843                 :            :                 struct p2p_device *dev;
     844         [ -  + ]:          6 :                 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
     845                 :          0 :                         dev->flags &= ~P2P_DEV_SD_INFO;
     846                 :            :         }
     847                 :            : 
     848                 :          6 :         return q;
     849                 :            : }
     850                 :            : 
     851                 :            : 
     852                 :            : #ifdef CONFIG_WIFI_DISPLAY
     853                 :          0 : void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
     854                 :            :                           const struct wpabuf *tlvs)
     855                 :            : {
     856                 :            :         struct p2p_sd_query *q;
     857                 :          0 :         q = p2p_sd_request(p2p, dst, tlvs);
     858         [ #  # ]:          0 :         if (q)
     859                 :          0 :                 q->wsd = 1;
     860                 :          0 :         return q;
     861                 :            : }
     862                 :            : #endif /* CONFIG_WIFI_DISPLAY */
     863                 :            : 
     864                 :            : 
     865                 :        708 : void p2p_sd_service_update(struct p2p_data *p2p)
     866                 :            : {
     867                 :        708 :         p2p->srv_update_indic++;
     868                 :        708 : }
     869                 :            : 
     870                 :            : 
     871                 :          0 : int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
     872                 :            : {
     873         [ #  # ]:          0 :         if (p2p_unlink_sd_query(p2p, req)) {
     874                 :          0 :                 p2p_dbg(p2p, "Cancel pending SD query %p", req);
     875                 :          0 :                 p2p_free_sd_query(req);
     876                 :          0 :                 return 0;
     877                 :            :         }
     878                 :          0 :         return -1;
     879                 :            : }

Generated by: LCOV version 1.9