anqp.c (6090B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdint.h> 28 29 #include <ell/ell.h> 30 31 #include "src/module.h" 32 #include "src/anqp.h" 33 #include "src/util.h" 34 #include "src/ie.h" 35 #include "src/scan.h" 36 #include "src/iwd.h" 37 #include "src/mpdu.h" 38 #include "src/frame-xchg.h" 39 40 #include "linux/nl80211.h" 41 42 #define ANQP_GROUP 0 43 44 struct anqp_request { 45 uint64_t wdev_id; 46 anqp_response_func_t anqp_cb; 47 anqp_destroy_func_t anqp_destroy; 48 void *anqp_data; 49 uint8_t anqp_token; 50 uint32_t frequency; 51 uint8_t *frame; 52 size_t frame_len; 53 uint32_t id; 54 }; 55 56 static void anqp_destroy(void *user_data) 57 { 58 struct anqp_request *request = user_data; 59 60 if (request->anqp_destroy) 61 request->anqp_destroy(request->anqp_data); 62 63 l_free(request->frame); 64 l_free(request); 65 } 66 67 /* 68 * By using frame-xchg we should get called back here for any frame matching our 69 * prefix until the duration expires. If frame-xchg is never signalled 'done' 70 * (returning true) we should get a timeout in anqp_frame_timeout. This is 71 * why we drop any improperly formatted frames without cleaning up the request. 72 */ 73 static bool anqp_response_frame_event(const struct mmpdu_header *hdr, 74 const void *body, size_t body_len, 75 int rssi, void *user_data) 76 { 77 struct anqp_request *request = user_data; 78 const uint8_t *ptr = body; 79 uint16_t status_code; 80 uint16_t delay; 81 uint16_t qrlen; 82 uint8_t adv_proto_len; 83 uint8_t token; 84 85 if (body_len < 9) 86 return false; 87 88 /* Skip past category/action since this frame was prefixed matched */ 89 ptr += 2; 90 body_len -= 2; 91 92 /* dialog token */ 93 token = *ptr++; 94 95 if (request->anqp_token != token) 96 return false; 97 98 status_code = l_get_le16(ptr); 99 ptr += 2; 100 body_len -= 2; 101 102 if (status_code != 0) { 103 l_error("Bad status code on GAS response %u", status_code); 104 return false; 105 } 106 107 delay = l_get_le16(ptr); 108 ptr += 2; 109 body_len -= 2; 110 111 /* 112 * IEEE 80211-2016 Section 9.6.8.13 113 * 114 * The value 0 will be returned by the STA when a Query Response is 115 * provided in this frame 116 */ 117 if (delay != 0) { 118 l_error("GAS comeback delay was not zero"); 119 return false; 120 } 121 122 if (*ptr != IE_TYPE_ADVERTISEMENT_PROTOCOL) { 123 l_error("GAS request not advertisement protocol"); 124 return false; 125 } 126 127 ptr++; 128 body_len--; 129 130 adv_proto_len = *ptr++; 131 body_len--; 132 133 if (body_len < adv_proto_len) 134 return false; 135 136 ptr += adv_proto_len; 137 body_len -= adv_proto_len; 138 139 if (body_len < 2) 140 return false; 141 142 qrlen = l_get_le16(ptr); 143 ptr += 2; 144 145 if (body_len < qrlen) 146 return false; 147 148 l_debug("ANQP response received from "MAC, MAC_STR(hdr->address_2)); 149 150 if (request->anqp_cb) 151 request->anqp_cb(ANQP_SUCCESS, ptr, qrlen, request->anqp_data); 152 153 anqp_destroy(request); 154 155 return true; 156 } 157 158 static const struct frame_xchg_prefix anqp_frame_prefix = { 159 .frame_type = 0x00d0, 160 .data = (uint8_t []) { 161 0x04, 0x0b, 162 }, 163 .len = 2, 164 }; 165 166 static void anqp_frame_timeout(int error, void *user_data) 167 { 168 struct anqp_request *request = user_data; 169 enum anqp_result result = ANQP_TIMEOUT; 170 171 if (error < 0) { 172 result = ANQP_FAILED; 173 l_error("Sending ANQP request failed: %s (%i)", 174 strerror(-error), -error); 175 } 176 177 if (request->anqp_cb) 178 request->anqp_cb(result, NULL, 0, request->anqp_data); 179 180 if (request->anqp_destroy) 181 request->anqp_destroy(request->anqp_data); 182 183 anqp_destroy(request); 184 } 185 186 static uint8_t *anqp_build_frame(const uint8_t *addr, struct scan_bss *bss, 187 uint8_t anqp_token, 188 const uint8_t *anqp, size_t len, 189 size_t *len_out) 190 { 191 uint8_t *frame = l_malloc(len + 33); 192 uint8_t *ptr; 193 194 memset(frame, 0, len + 33); 195 196 l_put_le16(0x00d0, frame + 0); 197 memcpy(frame + 4, bss->addr, 6); 198 memcpy(frame + 10, addr, 6); 199 memcpy(frame + 16, bss->addr, 6); 200 201 ptr = frame + 24; 202 203 *ptr++ = 0x04; /* Category: Public */ 204 *ptr++ = 0x0a; /* Action: GAS initial Request */ 205 *ptr++ = anqp_token; /* Dialog Token */ 206 *ptr++ = IE_TYPE_ADVERTISEMENT_PROTOCOL; 207 *ptr++ = 2; 208 209 *ptr++ = 0x7f; 210 *ptr++ = IE_ADVERTISEMENT_ANQP; 211 l_put_le16(len, ptr); 212 ptr += 2; 213 214 memcpy(ptr, anqp, len); 215 ptr += len; 216 217 *len_out = ptr - frame; 218 219 return frame; 220 } 221 222 uint32_t anqp_request(uint64_t wdev_id, const uint8_t *addr, 223 struct scan_bss *bss, const uint8_t *anqp, 224 size_t len, anqp_response_func_t cb, 225 void *user_data, anqp_destroy_func_t destroy) 226 { 227 struct anqp_request *request; 228 struct iovec iov[2]; 229 230 request = l_new(struct anqp_request, 1); 231 232 request->wdev_id = wdev_id; 233 request->frequency = bss->frequency; 234 request->anqp_cb = cb; 235 request->anqp_destroy = destroy; 236 /* 237 * WPA3 Specification version 3, Section 9.4: 238 * "A STA shall use a randomized dialog token for every new GAS 239 * exchange." 240 */ 241 l_getrandom(&request->anqp_token, sizeof(request->anqp_token)); 242 request->anqp_data = user_data; 243 244 request->frame = anqp_build_frame(addr, bss, request->anqp_token, 245 anqp, len, 246 &request->frame_len); 247 248 iov[0].iov_base = request->frame; 249 iov[0].iov_len = request->frame_len; 250 iov[1].iov_base = NULL; 251 252 l_debug("Sending ANQP request to "MAC, MAC_STR(bss->addr)); 253 254 request->id = frame_xchg_start(request->wdev_id, iov, 255 request->frequency, 0, 300, 0, 256 ANQP_GROUP, anqp_frame_timeout, request, NULL, 257 &anqp_frame_prefix, anqp_response_frame_event, 258 NULL); 259 260 return request->id; 261 } 262 263 void anqp_cancel(uint32_t id) 264 { 265 frame_xchg_cancel(id); 266 }