]>
Commit | Line | Data |
---|---|---|
afc064fe JM |
1 | /* |
2 | * Interworking (IEEE 802.11u) | |
1a712d2f | 3 | * Copyright (c) 2011-2012, Qualcomm Atheros, Inc. |
afc064fe | 4 | * |
e22d4d95 JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
afc064fe JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
12 | #include "common/ieee802_11_defs.h" | |
13 | #include "common/gas.h" | |
b02fe7ff | 14 | #include "common/wpa_ctrl.h" |
7f41c92b | 15 | #include "utils/pcsc_funcs.h" |
afc064fe | 16 | #include "drivers/driver.h" |
73c41a8f | 17 | #include "eap_common/eap_defs.h" |
8ca93c59 | 18 | #include "eap_peer/eap.h" |
67e1b984 | 19 | #include "eap_peer/eap_methods.h" |
afc064fe | 20 | #include "wpa_supplicant_i.h" |
73c41a8f | 21 | #include "config.h" |
5ed7880d | 22 | #include "config_ssid.h" |
afc064fe | 23 | #include "bss.h" |
b02fe7ff | 24 | #include "scan.h" |
67e1b984 | 25 | #include "notify.h" |
afc064fe | 26 | #include "gas_query.h" |
2249d714 | 27 | #include "hs20_supplicant.h" |
afc064fe JM |
28 | #include "interworking.h" |
29 | ||
30 | ||
3b840b67 JM |
31 | #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC) |
32 | #define INTERWORKING_3GPP | |
33 | #else | |
34 | #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC) | |
35 | #define INTERWORKING_3GPP | |
36 | #else | |
37 | #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC) | |
38 | #define INTERWORKING_3GPP | |
39 | #endif | |
40 | #endif | |
41 | #endif | |
42 | ||
afc064fe JM |
43 | static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s); |
44 | ||
45 | ||
08410f08 JM |
46 | static void interworking_reconnect(struct wpa_supplicant *wpa_s) |
47 | { | |
48 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { | |
49 | wpa_supplicant_cancel_sched_scan(wpa_s); | |
50 | wpa_supplicant_deauthenticate(wpa_s, | |
51 | WLAN_REASON_DEAUTH_LEAVING); | |
52 | } | |
53 | wpa_s->disconnected = 0; | |
54 | wpa_s->reassociate = 1; | |
a594e2a9 JM |
55 | |
56 | if (wpa_s->last_scan_res_used > 0) { | |
57 | struct os_time now; | |
58 | os_get_time(&now); | |
59 | if (now.sec - wpa_s->last_scan.sec <= 5) { | |
60 | wpa_printf(MSG_DEBUG, "Interworking: Old scan results " | |
61 | "are fresh - connect without new scan"); | |
0dd54313 | 62 | if (wpas_select_network_from_last_scan(wpa_s) >= 0) |
a594e2a9 JM |
63 | return; |
64 | } | |
65 | } | |
66 | ||
08410f08 JM |
67 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
68 | } | |
69 | ||
70 | ||
afc064fe JM |
71 | static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids, |
72 | struct wpabuf *extra) | |
73 | { | |
74 | struct wpabuf *buf; | |
75 | size_t i; | |
76 | u8 *len_pos; | |
77 | ||
78 | buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 + | |
79 | (extra ? wpabuf_len(extra) : 0)); | |
80 | if (buf == NULL) | |
81 | return NULL; | |
82 | ||
83 | len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST); | |
84 | for (i = 0; i < num_ids; i++) | |
85 | wpabuf_put_le16(buf, info_ids[i]); | |
86 | gas_anqp_set_element_len(buf, len_pos); | |
87 | if (extra) | |
88 | wpabuf_put_buf(buf, extra); | |
89 | ||
90 | gas_anqp_set_len(buf); | |
91 | ||
92 | return buf; | |
93 | } | |
94 | ||
95 | ||
96 | static void interworking_anqp_resp_cb(void *ctx, const u8 *dst, | |
97 | u8 dialog_token, | |
98 | enum gas_query_result result, | |
99 | const struct wpabuf *adv_proto, | |
100 | const struct wpabuf *resp, | |
101 | u16 status_code) | |
102 | { | |
103 | struct wpa_supplicant *wpa_s = ctx; | |
104 | ||
105 | anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp, | |
106 | status_code); | |
107 | interworking_next_anqp_fetch(wpa_s); | |
108 | } | |
109 | ||
110 | ||
7ff83367 JM |
111 | static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s) |
112 | { | |
113 | struct wpa_cred *cred; | |
114 | ||
115 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
116 | if (cred->roaming_consortium_len) | |
117 | return 1; | |
118 | } | |
119 | return 0; | |
120 | } | |
121 | ||
122 | ||
123 | static int cred_with_3gpp(struct wpa_supplicant *wpa_s) | |
124 | { | |
125 | struct wpa_cred *cred; | |
126 | ||
127 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
128 | if (cred->pcsc || cred->imsi) | |
129 | return 1; | |
130 | } | |
131 | return 0; | |
132 | } | |
133 | ||
134 | ||
135 | static int cred_with_nai_realm(struct wpa_supplicant *wpa_s) | |
136 | { | |
137 | struct wpa_cred *cred; | |
138 | ||
139 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
140 | if (cred->pcsc || cred->imsi) | |
141 | continue; | |
142 | if (!cred->eap_method) | |
143 | return 1; | |
144 | if (cred->realm && cred->roaming_consortium_len == 0) | |
145 | return 1; | |
146 | } | |
147 | return 0; | |
148 | } | |
149 | ||
150 | ||
151 | static int cred_with_domain(struct wpa_supplicant *wpa_s) | |
152 | { | |
153 | struct wpa_cred *cred; | |
154 | ||
155 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
156 | if (cred->domain || cred->pcsc || cred->imsi) | |
157 | return 1; | |
158 | } | |
159 | return 0; | |
160 | } | |
161 | ||
162 | ||
163 | static int additional_roaming_consortiums(struct wpa_bss *bss) | |
164 | { | |
165 | const u8 *ie; | |
166 | ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); | |
167 | if (ie == NULL || ie[1] == 0) | |
168 | return 0; | |
169 | return ie[2]; /* Number of ANQP OIs */ | |
170 | } | |
171 | ||
172 | ||
afc064fe JM |
173 | static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s, |
174 | struct wpa_bss *bss) | |
175 | { | |
176 | struct wpabuf *buf; | |
177 | int ret = 0; | |
178 | int res; | |
7ff83367 JM |
179 | u16 info_ids[8]; |
180 | size_t num_info_ids = 0; | |
afc064fe | 181 | struct wpabuf *extra = NULL; |
7ff83367 | 182 | int all = wpa_s->fetch_all_anqp; |
afc064fe JM |
183 | |
184 | wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR, | |
185 | MAC2STR(bss->bssid)); | |
186 | ||
7ff83367 JM |
187 | info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST; |
188 | if (all) { | |
189 | info_ids[num_info_ids++] = ANQP_VENUE_NAME; | |
190 | info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE; | |
191 | } | |
192 | if (all || (cred_with_roaming_consortium(wpa_s) && | |
193 | additional_roaming_consortiums(bss))) | |
194 | info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM; | |
195 | if (all) | |
196 | info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY; | |
197 | if (all || cred_with_nai_realm(wpa_s)) | |
198 | info_ids[num_info_ids++] = ANQP_NAI_REALM; | |
199 | if (all || cred_with_3gpp(wpa_s)) | |
200 | info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK; | |
201 | if (all || cred_with_domain(wpa_s)) | |
202 | info_ids[num_info_ids++] = ANQP_DOMAIN_NAME; | |
203 | wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info", | |
204 | (u8 *) info_ids, num_info_ids * 2); | |
205 | ||
2249d714 JM |
206 | #ifdef CONFIG_HS20 |
207 | if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) { | |
208 | u8 *len_pos; | |
209 | ||
210 | extra = wpabuf_alloc(100); | |
211 | if (!extra) | |
212 | return -1; | |
213 | ||
214 | len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC); | |
215 | wpabuf_put_be24(extra, OUI_WFA); | |
216 | wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE); | |
217 | wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST); | |
218 | wpabuf_put_u8(extra, 0); /* Reserved */ | |
219 | wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST); | |
7ff83367 JM |
220 | if (all) { |
221 | wpabuf_put_u8(extra, | |
222 | HS20_STYPE_OPERATOR_FRIENDLY_NAME); | |
223 | wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS); | |
224 | wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY); | |
225 | wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS); | |
226 | } | |
2249d714 JM |
227 | gas_anqp_set_element_len(extra, len_pos); |
228 | } | |
229 | #endif /* CONFIG_HS20 */ | |
230 | ||
7ff83367 | 231 | buf = anqp_build_req(info_ids, num_info_ids, extra); |
afc064fe JM |
232 | wpabuf_free(extra); |
233 | if (buf == NULL) | |
234 | return -1; | |
235 | ||
236 | res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf, | |
237 | interworking_anqp_resp_cb, wpa_s); | |
238 | if (res < 0) { | |
239 | wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request"); | |
240 | ret = -1; | |
241 | } else | |
242 | wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token " | |
243 | "%u", res); | |
244 | ||
245 | wpabuf_free(buf); | |
246 | return ret; | |
247 | } | |
248 | ||
249 | ||
73c41a8f JM |
250 | struct nai_realm_eap { |
251 | u8 method; | |
252 | u8 inner_method; | |
253 | enum nai_realm_eap_auth_inner_non_eap inner_non_eap; | |
254 | u8 cred_type; | |
255 | u8 tunneled_cred_type; | |
256 | }; | |
257 | ||
258 | struct nai_realm { | |
259 | u8 encoding; | |
260 | char *realm; | |
261 | u8 eap_count; | |
262 | struct nai_realm_eap *eap; | |
263 | }; | |
264 | ||
265 | ||
266 | static void nai_realm_free(struct nai_realm *realms, u16 count) | |
267 | { | |
268 | u16 i; | |
269 | ||
270 | if (realms == NULL) | |
271 | return; | |
272 | for (i = 0; i < count; i++) { | |
273 | os_free(realms[i].eap); | |
274 | os_free(realms[i].realm); | |
275 | } | |
276 | os_free(realms); | |
277 | } | |
278 | ||
279 | ||
280 | static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos, | |
281 | const u8 *end) | |
282 | { | |
283 | u8 elen, auth_count, a; | |
284 | const u8 *e_end; | |
285 | ||
286 | if (pos + 3 > end) { | |
287 | wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields"); | |
288 | return NULL; | |
289 | } | |
290 | ||
291 | elen = *pos++; | |
292 | if (pos + elen > end || elen < 2) { | |
293 | wpa_printf(MSG_DEBUG, "No room for EAP Method subfield"); | |
294 | return NULL; | |
295 | } | |
296 | e_end = pos + elen; | |
297 | e->method = *pos++; | |
298 | auth_count = *pos++; | |
299 | wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u", | |
300 | elen, e->method, auth_count); | |
301 | ||
302 | for (a = 0; a < auth_count; a++) { | |
303 | u8 id, len; | |
304 | ||
305 | if (pos + 2 > end || pos + 2 + pos[1] > end) { | |
306 | wpa_printf(MSG_DEBUG, "No room for Authentication " | |
307 | "Parameter subfield"); | |
308 | return NULL; | |
309 | } | |
310 | ||
311 | id = *pos++; | |
312 | len = *pos++; | |
313 | ||
314 | switch (id) { | |
315 | case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH: | |
316 | if (len < 1) | |
317 | break; | |
318 | e->inner_non_eap = *pos; | |
319 | if (e->method != EAP_TYPE_TTLS) | |
320 | break; | |
321 | switch (*pos) { | |
322 | case NAI_REALM_INNER_NON_EAP_PAP: | |
323 | wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP"); | |
324 | break; | |
325 | case NAI_REALM_INNER_NON_EAP_CHAP: | |
326 | wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP"); | |
327 | break; | |
328 | case NAI_REALM_INNER_NON_EAP_MSCHAP: | |
329 | wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP"); | |
330 | break; | |
331 | case NAI_REALM_INNER_NON_EAP_MSCHAPV2: | |
332 | wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2"); | |
333 | break; | |
334 | } | |
335 | break; | |
336 | case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD: | |
337 | if (len < 1) | |
338 | break; | |
339 | e->inner_method = *pos; | |
340 | wpa_printf(MSG_DEBUG, "Inner EAP method: %u", | |
341 | e->inner_method); | |
342 | break; | |
343 | case NAI_REALM_EAP_AUTH_CRED_TYPE: | |
344 | if (len < 1) | |
345 | break; | |
346 | e->cred_type = *pos; | |
347 | wpa_printf(MSG_DEBUG, "Credential Type: %u", | |
348 | e->cred_type); | |
349 | break; | |
350 | case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE: | |
351 | if (len < 1) | |
352 | break; | |
353 | e->tunneled_cred_type = *pos; | |
354 | wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential " | |
355 | "Type: %u", e->tunneled_cred_type); | |
356 | break; | |
357 | default: | |
358 | wpa_printf(MSG_DEBUG, "Unsupported Authentication " | |
359 | "Parameter: id=%u len=%u", id, len); | |
360 | wpa_hexdump(MSG_DEBUG, "Authentication Parameter " | |
361 | "Value", pos, len); | |
362 | break; | |
363 | } | |
364 | ||
365 | pos += len; | |
366 | } | |
367 | ||
368 | return e_end; | |
369 | } | |
370 | ||
371 | ||
372 | static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos, | |
373 | const u8 *end) | |
374 | { | |
375 | u16 len; | |
376 | const u8 *f_end; | |
377 | u8 realm_len, e; | |
378 | ||
379 | if (end - pos < 4) { | |
380 | wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " | |
381 | "fixed fields"); | |
382 | return NULL; | |
383 | } | |
384 | ||
385 | len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */ | |
386 | pos += 2; | |
387 | if (pos + len > end || len < 3) { | |
388 | wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " | |
389 | "(len=%u; left=%u)", | |
390 | len, (unsigned int) (end - pos)); | |
391 | return NULL; | |
392 | } | |
393 | f_end = pos + len; | |
394 | ||
395 | r->encoding = *pos++; | |
396 | realm_len = *pos++; | |
397 | if (pos + realm_len > f_end) { | |
398 | wpa_printf(MSG_DEBUG, "No room for NAI Realm " | |
399 | "(len=%u; left=%u)", | |
400 | realm_len, (unsigned int) (f_end - pos)); | |
401 | return NULL; | |
402 | } | |
403 | wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len); | |
404 | r->realm = os_malloc(realm_len + 1); | |
405 | if (r->realm == NULL) | |
406 | return NULL; | |
407 | os_memcpy(r->realm, pos, realm_len); | |
408 | r->realm[realm_len] = '\0'; | |
409 | pos += realm_len; | |
410 | ||
411 | if (pos + 1 > f_end) { | |
412 | wpa_printf(MSG_DEBUG, "No room for EAP Method Count"); | |
413 | return NULL; | |
414 | } | |
415 | r->eap_count = *pos++; | |
416 | wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count); | |
417 | if (pos + r->eap_count * 3 > f_end) { | |
418 | wpa_printf(MSG_DEBUG, "No room for EAP Methods"); | |
419 | return NULL; | |
420 | } | |
f9884c09 | 421 | r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap)); |
73c41a8f JM |
422 | if (r->eap == NULL) |
423 | return NULL; | |
424 | ||
425 | for (e = 0; e < r->eap_count; e++) { | |
426 | pos = nai_realm_parse_eap(&r->eap[e], pos, f_end); | |
427 | if (pos == NULL) | |
428 | return NULL; | |
429 | } | |
430 | ||
431 | return f_end; | |
432 | } | |
433 | ||
434 | ||
435 | static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count) | |
436 | { | |
437 | struct nai_realm *realm; | |
438 | const u8 *pos, *end; | |
439 | u16 i, num; | |
440 | ||
441 | if (anqp == NULL || wpabuf_len(anqp) < 2) | |
442 | return NULL; | |
443 | ||
444 | pos = wpabuf_head_u8(anqp); | |
445 | end = pos + wpabuf_len(anqp); | |
446 | num = WPA_GET_LE16(pos); | |
447 | wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num); | |
448 | pos += 2; | |
449 | ||
450 | if (num * 5 > end - pos) { | |
451 | wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not " | |
452 | "enough data (%u octets) for that many realms", | |
453 | num, (unsigned int) (end - pos)); | |
454 | return NULL; | |
455 | } | |
456 | ||
f9884c09 | 457 | realm = os_calloc(num, sizeof(struct nai_realm)); |
73c41a8f JM |
458 | if (realm == NULL) |
459 | return NULL; | |
460 | ||
461 | for (i = 0; i < num; i++) { | |
462 | pos = nai_realm_parse_realm(&realm[i], pos, end); | |
463 | if (pos == NULL) { | |
464 | nai_realm_free(realm, num); | |
465 | return NULL; | |
466 | } | |
467 | } | |
468 | ||
469 | *count = num; | |
470 | return realm; | |
471 | } | |
472 | ||
473 | ||
474 | static int nai_realm_match(struct nai_realm *realm, const char *home_realm) | |
475 | { | |
476 | char *tmp, *pos, *end; | |
477 | int match = 0; | |
478 | ||
3b840b67 | 479 | if (realm->realm == NULL || home_realm == NULL) |
73c41a8f JM |
480 | return 0; |
481 | ||
482 | if (os_strchr(realm->realm, ';') == NULL) | |
483 | return os_strcasecmp(realm->realm, home_realm) == 0; | |
484 | ||
485 | tmp = os_strdup(realm->realm); | |
486 | if (tmp == NULL) | |
487 | return 0; | |
488 | ||
489 | pos = tmp; | |
490 | while (*pos) { | |
491 | end = os_strchr(pos, ';'); | |
492 | if (end) | |
493 | *end = '\0'; | |
494 | if (os_strcasecmp(pos, home_realm) == 0) { | |
495 | match = 1; | |
496 | break; | |
497 | } | |
498 | if (end == NULL) | |
499 | break; | |
500 | pos = end + 1; | |
501 | } | |
502 | ||
503 | os_free(tmp); | |
504 | ||
505 | return match; | |
506 | } | |
507 | ||
508 | ||
67e1b984 JM |
509 | static int nai_realm_cred_username(struct nai_realm_eap *eap) |
510 | { | |
511 | if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) | |
512 | return 0; /* method not supported */ | |
513 | ||
514 | if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) { | |
515 | /* Only tunneled methods with username/password supported */ | |
516 | return 0; | |
517 | } | |
518 | ||
519 | if (eap->method == EAP_TYPE_PEAP && | |
520 | eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) | |
521 | return 0; | |
522 | ||
523 | if (eap->method == EAP_TYPE_TTLS) { | |
524 | if (eap->inner_method == 0 && eap->inner_non_eap == 0) | |
525 | return 0; | |
526 | if (eap->inner_method && | |
527 | eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) | |
528 | return 0; | |
529 | if (eap->inner_non_eap && | |
530 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP && | |
531 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP && | |
532 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP && | |
533 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) | |
534 | return 0; | |
535 | } | |
536 | ||
537 | if (eap->inner_method && | |
538 | eap->inner_method != EAP_TYPE_GTC && | |
539 | eap->inner_method != EAP_TYPE_MSCHAPV2) | |
540 | return 0; | |
541 | ||
542 | return 1; | |
543 | } | |
544 | ||
545 | ||
11e4f46a JM |
546 | static int nai_realm_cred_cert(struct nai_realm_eap *eap) |
547 | { | |
548 | if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) | |
549 | return 0; /* method not supported */ | |
550 | ||
551 | if (eap->method != EAP_TYPE_TLS) { | |
552 | /* Only EAP-TLS supported for credential authentication */ | |
553 | return 0; | |
554 | } | |
555 | ||
556 | return 1; | |
557 | } | |
558 | ||
559 | ||
1bb7b8e8 | 560 | static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred, |
19df9b07 | 561 | struct nai_realm *realm) |
67e1b984 JM |
562 | { |
563 | u8 e; | |
564 | ||
1bb7b8e8 JM |
565 | if (cred == NULL || |
566 | cred->username == NULL || | |
567 | cred->username[0] == '\0' || | |
11e4f46a JM |
568 | ((cred->password == NULL || |
569 | cred->password[0] == '\0') && | |
570 | (cred->private_key == NULL || | |
571 | cred->private_key[0] == '\0'))) | |
67e1b984 JM |
572 | return NULL; |
573 | ||
574 | for (e = 0; e < realm->eap_count; e++) { | |
575 | struct nai_realm_eap *eap = &realm->eap[e]; | |
11e4f46a JM |
576 | if (cred->password && cred->password[0] && |
577 | nai_realm_cred_username(eap)) | |
578 | return eap; | |
579 | if (cred->private_key && cred->private_key[0] && | |
580 | nai_realm_cred_cert(eap)) | |
67e1b984 JM |
581 | return eap; |
582 | } | |
583 | ||
584 | return NULL; | |
585 | } | |
586 | ||
587 | ||
3b840b67 JM |
588 | #ifdef INTERWORKING_3GPP |
589 | ||
c21246f5 | 590 | static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len) |
3b840b67 | 591 | { |
3b840b67 JM |
592 | u8 plmn[3]; |
593 | const u8 *pos, *end; | |
594 | u8 udhl; | |
595 | ||
3b840b67 JM |
596 | /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */ |
597 | plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4); | |
598 | plmn[1] = imsi[2] - '0'; | |
c21246f5 JM |
599 | /* default to MNC length 3 if unknown */ |
600 | if (mnc_len != 2) | |
3b840b67 JM |
601 | plmn[1] |= (imsi[5] - '0') << 4; |
602 | else | |
603 | plmn[1] |= 0xf0; | |
604 | plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4); | |
605 | ||
606 | if (anqp == NULL) | |
607 | return 0; | |
608 | pos = wpabuf_head_u8(anqp); | |
609 | end = pos + wpabuf_len(anqp); | |
610 | if (pos + 2 > end) | |
611 | return 0; | |
612 | if (*pos != 0) { | |
613 | wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos); | |
614 | return 0; | |
615 | } | |
616 | pos++; | |
617 | udhl = *pos++; | |
618 | if (pos + udhl > end) { | |
619 | wpa_printf(MSG_DEBUG, "Invalid UDHL"); | |
620 | return 0; | |
621 | } | |
622 | end = pos + udhl; | |
623 | ||
624 | while (pos + 2 <= end) { | |
625 | u8 iei, len; | |
626 | const u8 *l_end; | |
627 | iei = *pos++; | |
628 | len = *pos++ & 0x7f; | |
629 | if (pos + len > end) | |
630 | break; | |
631 | l_end = pos + len; | |
632 | ||
633 | if (iei == 0 && len > 0) { | |
634 | /* PLMN List */ | |
635 | u8 num, i; | |
636 | num = *pos++; | |
637 | for (i = 0; i < num; i++) { | |
638 | if (pos + 3 > end) | |
639 | break; | |
640 | if (os_memcmp(pos, plmn, 3) == 0) | |
641 | return 1; /* Found matching PLMN */ | |
8f234012 | 642 | pos += 3; |
3b840b67 JM |
643 | } |
644 | } | |
645 | ||
646 | pos = l_end; | |
647 | } | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
652 | ||
d8e59fea | 653 | static int build_root_nai(char *nai, size_t nai_len, const char *imsi, |
632c9458 | 654 | size_t mnc_len, char prefix) |
3b840b67 JM |
655 | { |
656 | const char *sep, *msin; | |
9914c96f | 657 | char *end, *pos; |
3b840b67 JM |
658 | size_t msin_len, plmn_len; |
659 | ||
660 | /* | |
661 | * TS 23.003, Clause 14 (3GPP to WLAN Interworking) | |
662 | * Root NAI: | |
663 | * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org | |
664 | * <MNC> is zero-padded to three digits in case two-digit MNC is used | |
665 | */ | |
666 | ||
667 | if (imsi == NULL || os_strlen(imsi) > 16) { | |
668 | wpa_printf(MSG_DEBUG, "No valid IMSI available"); | |
669 | return -1; | |
670 | } | |
671 | sep = os_strchr(imsi, '-'); | |
632c9458 JM |
672 | if (sep) { |
673 | plmn_len = sep - imsi; | |
674 | msin = sep + 1; | |
675 | } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) { | |
676 | plmn_len = 3 + mnc_len; | |
677 | msin = imsi + plmn_len; | |
678 | } else | |
3b840b67 | 679 | return -1; |
3b840b67 JM |
680 | if (plmn_len != 5 && plmn_len != 6) |
681 | return -1; | |
3b840b67 JM |
682 | msin_len = os_strlen(msin); |
683 | ||
684 | pos = nai; | |
d8e59fea | 685 | end = nai + nai_len; |
9914c96f JM |
686 | if (prefix) |
687 | *pos++ = prefix; | |
3b840b67 JM |
688 | os_memcpy(pos, imsi, plmn_len); |
689 | pos += plmn_len; | |
690 | os_memcpy(pos, msin, msin_len); | |
691 | pos += msin_len; | |
692 | pos += os_snprintf(pos, end - pos, "@wlan.mnc"); | |
693 | if (plmn_len == 5) { | |
694 | *pos++ = '0'; | |
695 | *pos++ = imsi[3]; | |
696 | *pos++ = imsi[4]; | |
697 | } else { | |
698 | *pos++ = imsi[3]; | |
699 | *pos++ = imsi[4]; | |
700 | *pos++ = imsi[5]; | |
701 | } | |
702 | pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org", | |
703 | imsi[0], imsi[1], imsi[2]); | |
704 | ||
9914c96f JM |
705 | return 0; |
706 | } | |
707 | ||
708 | ||
709 | static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix) | |
710 | { | |
711 | char nai[100]; | |
632c9458 | 712 | if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0) |
9914c96f | 713 | return -1; |
3b840b67 JM |
714 | return wpa_config_set_quoted(ssid, "identity", nai); |
715 | } | |
716 | ||
717 | #endif /* INTERWORKING_3GPP */ | |
718 | ||
719 | ||
8601b503 JM |
720 | static int interworking_set_hs20_params(struct wpa_ssid *ssid) |
721 | { | |
722 | if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0) | |
723 | return -1; | |
724 | if (wpa_config_set(ssid, "proto", "RSN", 0) < 0) | |
725 | return -1; | |
726 | if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0) | |
727 | return -1; | |
728 | return 0; | |
729 | } | |
730 | ||
731 | ||
3b840b67 JM |
732 | static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s, |
733 | struct wpa_bss *bss) | |
734 | { | |
735 | #ifdef INTERWORKING_3GPP | |
1bb7b8e8 | 736 | struct wpa_cred *cred; |
3b840b67 JM |
737 | struct wpa_ssid *ssid; |
738 | const u8 *ie; | |
7e414b21 JM |
739 | int eap_type; |
740 | int res; | |
741 | char prefix; | |
3b840b67 | 742 | |
476aed35 | 743 | if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) |
1bb7b8e8 JM |
744 | return -1; |
745 | ||
746 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
c21246f5 | 747 | char *sep; |
d7b01abd JM |
748 | const char *imsi; |
749 | int mnc_len; | |
750 | ||
751 | #ifdef PCSC_FUNCS | |
752 | if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard && | |
753 | wpa_s->imsi[0]) { | |
754 | imsi = wpa_s->imsi; | |
755 | mnc_len = wpa_s->mnc_len; | |
756 | goto compare; | |
757 | } | |
758 | #endif /* PCSC_FUNCS */ | |
c21246f5 | 759 | |
1bb7b8e8 JM |
760 | if (cred->imsi == NULL || !cred->imsi[0] || |
761 | cred->milenage == NULL || !cred->milenage[0]) | |
762 | continue; | |
c21246f5 JM |
763 | |
764 | sep = os_strchr(cred->imsi, '-'); | |
765 | if (sep == NULL || | |
766 | (sep - cred->imsi != 5 && sep - cred->imsi != 6)) | |
767 | continue; | |
d7b01abd JM |
768 | mnc_len = sep - cred->imsi - 3; |
769 | imsi = cred->imsi; | |
c21246f5 | 770 | |
d7b01abd JM |
771 | #ifdef PCSC_FUNCS |
772 | compare: | |
773 | #endif /* PCSC_FUNCS */ | |
476aed35 | 774 | if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len)) |
1bb7b8e8 JM |
775 | break; |
776 | } | |
777 | if (cred == NULL) | |
778 | return -1; | |
779 | ||
3b840b67 | 780 | ie = wpa_bss_get_ie(bss, WLAN_EID_SSID); |
dbfdb392 JM |
781 | if (ie == NULL) |
782 | return -1; | |
3b840b67 JM |
783 | wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)", |
784 | MAC2STR(bss->bssid)); | |
785 | ||
786 | ssid = wpa_config_add_network(wpa_s->conf); | |
787 | if (ssid == NULL) | |
788 | return -1; | |
736d4f2d | 789 | ssid->parent_cred = cred; |
3b840b67 JM |
790 | |
791 | wpas_notify_network_added(wpa_s, ssid); | |
792 | wpa_config_set_network_defaults(ssid); | |
1a712d2f | 793 | ssid->priority = cred->priority; |
3b840b67 JM |
794 | ssid->temporary = 1; |
795 | ssid->ssid = os_zalloc(ie[1] + 1); | |
796 | if (ssid->ssid == NULL) | |
797 | goto fail; | |
798 | os_memcpy(ssid->ssid, ie + 2, ie[1]); | |
799 | ssid->ssid_len = ie[1]; | |
800 | ||
8601b503 | 801 | if (interworking_set_hs20_params(ssid) < 0) |
1ecd2ada JM |
802 | goto fail; |
803 | ||
7e414b21 JM |
804 | eap_type = EAP_TYPE_SIM; |
805 | if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard)) | |
806 | eap_type = EAP_TYPE_AKA; | |
807 | if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) { | |
808 | if (cred->eap_method[0].method == EAP_TYPE_SIM || | |
809 | cred->eap_method[0].method == EAP_TYPE_AKA || | |
810 | cred->eap_method[0].method == EAP_TYPE_AKA_PRIME) | |
811 | eap_type = cred->eap_method[0].method; | |
812 | } | |
813 | ||
814 | switch (eap_type) { | |
815 | case EAP_TYPE_SIM: | |
816 | prefix = '1'; | |
817 | res = wpa_config_set(ssid, "eap", "SIM", 0); | |
818 | break; | |
819 | case EAP_TYPE_AKA: | |
820 | prefix = '0'; | |
821 | res = wpa_config_set(ssid, "eap", "AKA", 0); | |
822 | break; | |
823 | case EAP_TYPE_AKA_PRIME: | |
824 | prefix = '6'; | |
825 | res = wpa_config_set(ssid, "eap", "AKA'", 0); | |
826 | break; | |
827 | default: | |
828 | res = -1; | |
829 | break; | |
830 | } | |
831 | if (res < 0) { | |
832 | wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported", | |
833 | eap_type); | |
3b840b67 JM |
834 | goto fail; |
835 | } | |
7e414b21 JM |
836 | |
837 | if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) { | |
3b840b67 JM |
838 | wpa_printf(MSG_DEBUG, "Failed to set Root NAI"); |
839 | goto fail; | |
840 | } | |
841 | ||
1bb7b8e8 | 842 | if (cred->milenage && cred->milenage[0]) { |
3b840b67 | 843 | if (wpa_config_set_quoted(ssid, "password", |
1bb7b8e8 | 844 | cred->milenage) < 0) |
3b840b67 | 845 | goto fail; |
d7b01abd | 846 | } else if (cred->pcsc) { |
3b840b67 JM |
847 | if (wpa_config_set_quoted(ssid, "pcsc", "") < 0) |
848 | goto fail; | |
d7b01abd JM |
849 | if (wpa_s->conf->pcsc_pin && |
850 | wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin) | |
851 | < 0) | |
852 | goto fail; | |
3b840b67 JM |
853 | } |
854 | ||
1bb7b8e8 JM |
855 | if (cred->password && cred->password[0] && |
856 | wpa_config_set_quoted(ssid, "password", cred->password) < 0) | |
3b840b67 JM |
857 | goto fail; |
858 | ||
1a712d2f | 859 | wpa_config_update_prio_list(wpa_s->conf); |
08410f08 | 860 | interworking_reconnect(wpa_s); |
3b840b67 JM |
861 | |
862 | return 0; | |
863 | ||
864 | fail: | |
865 | wpas_notify_network_removed(wpa_s, ssid); | |
866 | wpa_config_remove_network(wpa_s->conf, ssid->id); | |
867 | #endif /* INTERWORKING_3GPP */ | |
868 | return -1; | |
869 | } | |
870 | ||
871 | ||
955567bc JM |
872 | static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id, |
873 | size_t rc_len) | |
874 | { | |
875 | const u8 *pos, *end; | |
876 | u8 lens; | |
877 | ||
878 | if (ie == NULL) | |
879 | return 0; | |
880 | ||
881 | pos = ie + 2; | |
882 | end = ie + 2 + ie[1]; | |
883 | ||
884 | /* Roaming Consortium element: | |
885 | * Number of ANQP OIs | |
886 | * OI #1 and #2 lengths | |
887 | * OI #1, [OI #2], [OI #3] | |
888 | */ | |
889 | ||
890 | if (pos + 2 > end) | |
891 | return 0; | |
892 | ||
893 | pos++; /* skip Number of ANQP OIs */ | |
894 | lens = *pos++; | |
895 | if (pos + (lens & 0x0f) + (lens >> 4) > end) | |
896 | return 0; | |
897 | ||
898 | if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) | |
899 | return 1; | |
900 | pos += lens & 0x0f; | |
901 | ||
902 | if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) | |
903 | return 1; | |
904 | pos += lens >> 4; | |
905 | ||
906 | if (pos < end && (size_t) (end - pos) == rc_len && | |
907 | os_memcmp(pos, rc_id, rc_len) == 0) | |
908 | return 1; | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | ||
914 | static int roaming_consortium_anqp_match(const struct wpabuf *anqp, | |
915 | const u8 *rc_id, size_t rc_len) | |
916 | { | |
917 | const u8 *pos, *end; | |
918 | u8 len; | |
919 | ||
920 | if (anqp == NULL) | |
921 | return 0; | |
922 | ||
923 | pos = wpabuf_head(anqp); | |
924 | end = pos + wpabuf_len(anqp); | |
925 | ||
926 | /* Set of <OI Length, OI> duples */ | |
927 | while (pos < end) { | |
928 | len = *pos++; | |
929 | if (pos + len > end) | |
930 | break; | |
931 | if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) | |
932 | return 1; | |
933 | pos += len; | |
934 | } | |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
939 | ||
940 | static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp, | |
941 | const u8 *rc_id, size_t rc_len) | |
942 | { | |
943 | return roaming_consortium_element_match(ie, rc_id, rc_len) || | |
944 | roaming_consortium_anqp_match(anqp, rc_id, rc_len); | |
945 | } | |
946 | ||
947 | ||
948 | static struct wpa_cred * interworking_credentials_available_roaming_consortium( | |
949 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | |
950 | { | |
951 | struct wpa_cred *cred, *selected = NULL; | |
952 | const u8 *ie; | |
953 | ||
954 | ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); | |
955 | ||
476aed35 JM |
956 | if (ie == NULL && |
957 | (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL)) | |
955567bc JM |
958 | return NULL; |
959 | ||
960 | if (wpa_s->conf->cred == NULL) | |
961 | return NULL; | |
962 | ||
963 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { | |
964 | if (cred->roaming_consortium_len == 0) | |
965 | continue; | |
966 | ||
476aed35 JM |
967 | if (!roaming_consortium_match(ie, |
968 | bss->anqp ? | |
969 | bss->anqp->roaming_consortium : | |
970 | NULL, | |
955567bc JM |
971 | cred->roaming_consortium, |
972 | cred->roaming_consortium_len)) | |
973 | continue; | |
974 | ||
975 | if (selected == NULL || | |
976 | selected->priority < cred->priority) | |
977 | selected = cred; | |
978 | } | |
979 | ||
980 | return selected; | |
981 | } | |
982 | ||
983 | ||
d5b5d801 JM |
984 | static int interworking_set_eap_params(struct wpa_ssid *ssid, |
985 | struct wpa_cred *cred, int ttls) | |
986 | { | |
8ca93c59 JM |
987 | if (cred->eap_method) { |
988 | ttls = cred->eap_method->vendor == EAP_VENDOR_IETF && | |
989 | cred->eap_method->method == EAP_TYPE_TTLS; | |
990 | ||
991 | os_free(ssid->eap.eap_methods); | |
992 | ssid->eap.eap_methods = | |
993 | os_malloc(sizeof(struct eap_method_type) * 2); | |
994 | if (ssid->eap.eap_methods == NULL) | |
995 | return -1; | |
996 | os_memcpy(ssid->eap.eap_methods, cred->eap_method, | |
997 | sizeof(*cred->eap_method)); | |
998 | ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF; | |
999 | ssid->eap.eap_methods[1].method = EAP_TYPE_NONE; | |
1000 | } | |
1001 | ||
d5b5d801 JM |
1002 | if (ttls && cred->username && cred->username[0]) { |
1003 | const char *pos; | |
1004 | char *anon; | |
1005 | /* Use anonymous NAI in Phase 1 */ | |
1006 | pos = os_strchr(cred->username, '@'); | |
1007 | if (pos) { | |
1008 | size_t buflen = 9 + os_strlen(pos) + 1; | |
1009 | anon = os_malloc(buflen); | |
1010 | if (anon == NULL) | |
1011 | return -1; | |
1012 | os_snprintf(anon, buflen, "anonymous%s", pos); | |
1013 | } else if (cred->realm) { | |
1014 | size_t buflen = 10 + os_strlen(cred->realm) + 1; | |
1015 | anon = os_malloc(buflen); | |
1016 | if (anon == NULL) | |
1017 | return -1; | |
1018 | os_snprintf(anon, buflen, "anonymous@%s", cred->realm); | |
1019 | } else { | |
1020 | anon = os_strdup("anonymous"); | |
1021 | if (anon == NULL) | |
1022 | return -1; | |
1023 | } | |
1024 | if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) < | |
1025 | 0) { | |
1026 | os_free(anon); | |
1027 | return -1; | |
1028 | } | |
1029 | os_free(anon); | |
1030 | } | |
1031 | ||
1032 | if (cred->username && cred->username[0] && | |
1033 | wpa_config_set_quoted(ssid, "identity", cred->username) < 0) | |
1034 | return -1; | |
1035 | ||
02af9c90 JM |
1036 | if (cred->password && cred->password[0]) { |
1037 | if (cred->ext_password && | |
1038 | wpa_config_set(ssid, "password", cred->password, 0) < 0) | |
1039 | return -1; | |
1040 | if (!cred->ext_password && | |
1041 | wpa_config_set_quoted(ssid, "password", cred->password) < | |
1042 | 0) | |
1043 | return -1; | |
1044 | } | |
d5b5d801 JM |
1045 | |
1046 | if (cred->client_cert && cred->client_cert[0] && | |
1047 | wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0) | |
1048 | return -1; | |
1049 | ||
93face0e JM |
1050 | #ifdef ANDROID |
1051 | if (cred->private_key && | |
1052 | os_strncmp(cred->private_key, "keystore://", 11) == 0) { | |
1053 | /* Use OpenSSL engine configuration for Android keystore */ | |
1054 | if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 || | |
1055 | wpa_config_set_quoted(ssid, "key_id", | |
1056 | cred->private_key + 11) < 0 || | |
1057 | wpa_config_set(ssid, "engine", "1", 0) < 0) | |
1058 | return -1; | |
1059 | } else | |
1060 | #endif /* ANDROID */ | |
d5b5d801 JM |
1061 | if (cred->private_key && cred->private_key[0] && |
1062 | wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0) | |
1063 | return -1; | |
1064 | ||
1065 | if (cred->private_key_passwd && cred->private_key_passwd[0] && | |
1066 | wpa_config_set_quoted(ssid, "private_key_passwd", | |
1067 | cred->private_key_passwd) < 0) | |
1068 | return -1; | |
1069 | ||
8ca93c59 JM |
1070 | if (cred->phase1) { |
1071 | os_free(ssid->eap.phase1); | |
1072 | ssid->eap.phase1 = os_strdup(cred->phase1); | |
1073 | } | |
1074 | if (cred->phase2) { | |
1075 | os_free(ssid->eap.phase2); | |
1076 | ssid->eap.phase2 = os_strdup(cred->phase2); | |
1077 | } | |
1078 | ||
d5b5d801 JM |
1079 | if (cred->ca_cert && cred->ca_cert[0] && |
1080 | wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0) | |
1081 | return -1; | |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
1086 | ||
955567bc JM |
1087 | static int interworking_connect_roaming_consortium( |
1088 | struct wpa_supplicant *wpa_s, struct wpa_cred *cred, | |
1089 | struct wpa_bss *bss, const u8 *ssid_ie) | |
1090 | { | |
1091 | struct wpa_ssid *ssid; | |
1092 | ||
1093 | wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on " | |
1094 | "roaming consortium match", MAC2STR(bss->bssid)); | |
1095 | ||
1096 | ssid = wpa_config_add_network(wpa_s->conf); | |
1097 | if (ssid == NULL) | |
1098 | return -1; | |
736d4f2d | 1099 | ssid->parent_cred = cred; |
955567bc JM |
1100 | wpas_notify_network_added(wpa_s, ssid); |
1101 | wpa_config_set_network_defaults(ssid); | |
1102 | ssid->priority = cred->priority; | |
1103 | ssid->temporary = 1; | |
1104 | ssid->ssid = os_zalloc(ssid_ie[1] + 1); | |
1105 | if (ssid->ssid == NULL) | |
1106 | goto fail; | |
1107 | os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]); | |
1108 | ssid->ssid_len = ssid_ie[1]; | |
1109 | ||
8601b503 | 1110 | if (interworking_set_hs20_params(ssid) < 0) |
1ecd2ada JM |
1111 | goto fail; |
1112 | ||
955567bc JM |
1113 | if (cred->eap_method == NULL) { |
1114 | wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for " | |
1115 | "credential using roaming consortium"); | |
1116 | goto fail; | |
1117 | } | |
1118 | ||
1119 | if (interworking_set_eap_params( | |
1120 | ssid, cred, | |
1121 | cred->eap_method->vendor == EAP_VENDOR_IETF && | |
1122 | cred->eap_method->method == EAP_TYPE_TTLS) < 0) | |
1123 | goto fail; | |
1124 | ||
1125 | wpa_config_update_prio_list(wpa_s->conf); | |
1126 | interworking_reconnect(wpa_s); | |
1127 | ||
1128 | return 0; | |
1129 | ||
1130 | fail: | |
1131 | wpas_notify_network_removed(wpa_s, ssid); | |
1132 | wpa_config_remove_network(wpa_s->conf, ssid->id); | |
1133 | return -1; | |
1134 | } | |
1135 | ||
1136 | ||
b02fe7ff JM |
1137 | int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
1138 | { | |
1bb7b8e8 | 1139 | struct wpa_cred *cred; |
67e1b984 JM |
1140 | struct wpa_ssid *ssid; |
1141 | struct nai_realm *realm; | |
1142 | struct nai_realm_eap *eap = NULL; | |
1143 | u16 count, i; | |
1144 | char buf[100]; | |
1145 | const u8 *ie; | |
1146 | ||
1bb7b8e8 | 1147 | if (wpa_s->conf->cred == NULL || bss == NULL) |
b02fe7ff | 1148 | return -1; |
67e1b984 JM |
1149 | ie = wpa_bss_get_ie(bss, WLAN_EID_SSID); |
1150 | if (ie == NULL || ie[1] == 0) { | |
1151 | wpa_printf(MSG_DEBUG, "Interworking: No SSID known for " | |
1152 | MACSTR, MAC2STR(bss->bssid)); | |
1153 | return -1; | |
1154 | } | |
1155 | ||
63e5caaa JM |
1156 | if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { |
1157 | /* | |
1158 | * We currently support only HS 2.0 networks and those are | |
1159 | * required to use WPA2-Enterprise. | |
1160 | */ | |
1161 | wpa_printf(MSG_DEBUG, "Interworking: Network does not use " | |
1162 | "RSN"); | |
1163 | return -1; | |
1164 | } | |
1165 | ||
955567bc JM |
1166 | cred = interworking_credentials_available_roaming_consortium(wpa_s, |
1167 | bss); | |
1168 | if (cred) | |
1169 | return interworking_connect_roaming_consortium(wpa_s, cred, | |
1170 | bss, ie); | |
1171 | ||
476aed35 JM |
1172 | realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL, |
1173 | &count); | |
67e1b984 JM |
1174 | if (realm == NULL) { |
1175 | wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI " | |
1176 | "Realm list from " MACSTR, MAC2STR(bss->bssid)); | |
3b840b67 | 1177 | count = 0; |
67e1b984 JM |
1178 | } |
1179 | ||
1bb7b8e8 JM |
1180 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
1181 | for (i = 0; i < count; i++) { | |
1182 | if (!nai_realm_match(&realm[i], cred->realm)) | |
1183 | continue; | |
1184 | eap = nai_realm_find_eap(cred, &realm[i]); | |
1185 | if (eap) | |
1186 | break; | |
1187 | } | |
67e1b984 JM |
1188 | if (eap) |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | if (!eap) { | |
3b840b67 JM |
1193 | if (interworking_connect_3gpp(wpa_s, bss) == 0) { |
1194 | if (realm) | |
1195 | nai_realm_free(realm, count); | |
1196 | return 0; | |
1197 | } | |
1198 | ||
67e1b984 JM |
1199 | wpa_printf(MSG_DEBUG, "Interworking: No matching credentials " |
1200 | "and EAP method found for " MACSTR, | |
1201 | MAC2STR(bss->bssid)); | |
1202 | nai_realm_free(realm, count); | |
1203 | return -1; | |
1204 | } | |
b02fe7ff JM |
1205 | |
1206 | wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR, | |
1207 | MAC2STR(bss->bssid)); | |
67e1b984 JM |
1208 | |
1209 | ssid = wpa_config_add_network(wpa_s->conf); | |
1210 | if (ssid == NULL) { | |
1211 | nai_realm_free(realm, count); | |
1212 | return -1; | |
1213 | } | |
736d4f2d | 1214 | ssid->parent_cred = cred; |
67e1b984 JM |
1215 | wpas_notify_network_added(wpa_s, ssid); |
1216 | wpa_config_set_network_defaults(ssid); | |
1a712d2f | 1217 | ssid->priority = cred->priority; |
67e1b984 JM |
1218 | ssid->temporary = 1; |
1219 | ssid->ssid = os_zalloc(ie[1] + 1); | |
1220 | if (ssid->ssid == NULL) | |
1221 | goto fail; | |
1222 | os_memcpy(ssid->ssid, ie + 2, ie[1]); | |
1223 | ssid->ssid_len = ie[1]; | |
1224 | ||
8601b503 | 1225 | if (interworking_set_hs20_params(ssid) < 0) |
1ecd2ada JM |
1226 | goto fail; |
1227 | ||
67e1b984 JM |
1228 | if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF, |
1229 | eap->method), 0) < 0) | |
1230 | goto fail; | |
1231 | ||
67e1b984 JM |
1232 | switch (eap->method) { |
1233 | case EAP_TYPE_TTLS: | |
1234 | if (eap->inner_method) { | |
1235 | os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", | |
1236 | eap_get_name(EAP_VENDOR_IETF, | |
1237 | eap->inner_method)); | |
1238 | if (wpa_config_set(ssid, "phase2", buf, 0) < 0) | |
1239 | goto fail; | |
1240 | break; | |
1241 | } | |
1242 | switch (eap->inner_non_eap) { | |
1243 | case NAI_REALM_INNER_NON_EAP_PAP: | |
1244 | if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) < | |
1245 | 0) | |
1246 | goto fail; | |
1247 | break; | |
1248 | case NAI_REALM_INNER_NON_EAP_CHAP: | |
1249 | if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0) | |
1250 | < 0) | |
1251 | goto fail; | |
1252 | break; | |
1253 | case NAI_REALM_INNER_NON_EAP_MSCHAP: | |
d3f57d0f JM |
1254 | if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"", |
1255 | 0) < 0) | |
67e1b984 JM |
1256 | goto fail; |
1257 | break; | |
1258 | case NAI_REALM_INNER_NON_EAP_MSCHAPV2: | |
1259 | if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"", | |
1260 | 0) < 0) | |
1261 | goto fail; | |
1262 | break; | |
1263 | } | |
1264 | break; | |
1265 | case EAP_TYPE_PEAP: | |
1266 | os_snprintf(buf, sizeof(buf), "\"auth=%s\"", | |
1267 | eap_get_name(EAP_VENDOR_IETF, eap->inner_method)); | |
1268 | if (wpa_config_set(ssid, "phase2", buf, 0) < 0) | |
1269 | goto fail; | |
1270 | break; | |
11e4f46a JM |
1271 | case EAP_TYPE_TLS: |
1272 | break; | |
67e1b984 JM |
1273 | } |
1274 | ||
d5b5d801 JM |
1275 | if (interworking_set_eap_params(ssid, cred, |
1276 | eap->method == EAP_TYPE_TTLS) < 0) | |
67e1b984 JM |
1277 | goto fail; |
1278 | ||
1279 | nai_realm_free(realm, count); | |
1280 | ||
1a712d2f | 1281 | wpa_config_update_prio_list(wpa_s->conf); |
08410f08 | 1282 | interworking_reconnect(wpa_s); |
67e1b984 | 1283 | |
b02fe7ff | 1284 | return 0; |
67e1b984 JM |
1285 | |
1286 | fail: | |
1287 | wpas_notify_network_removed(wpa_s, ssid); | |
1288 | wpa_config_remove_network(wpa_s->conf, ssid->id); | |
1289 | nai_realm_free(realm, count); | |
1290 | return -1; | |
b02fe7ff JM |
1291 | } |
1292 | ||
1293 | ||
1a712d2f | 1294 | static struct wpa_cred * interworking_credentials_available_3gpp( |
3b840b67 JM |
1295 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
1296 | { | |
1a712d2f | 1297 | struct wpa_cred *cred, *selected = NULL; |
1bb7b8e8 | 1298 | int ret; |
3b840b67 JM |
1299 | |
1300 | #ifdef INTERWORKING_3GPP | |
476aed35 | 1301 | if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) |
1a712d2f | 1302 | return NULL; |
3b840b67 | 1303 | |
1bb7b8e8 | 1304 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
c21246f5 | 1305 | char *sep; |
d7b01abd JM |
1306 | const char *imsi; |
1307 | int mnc_len; | |
1308 | ||
1309 | #ifdef PCSC_FUNCS | |
1310 | if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard && | |
1311 | wpa_s->imsi[0]) { | |
1312 | imsi = wpa_s->imsi; | |
1313 | mnc_len = wpa_s->mnc_len; | |
1314 | goto compare; | |
1315 | } | |
1316 | #endif /* PCSC_FUNCS */ | |
c21246f5 | 1317 | |
1bb7b8e8 JM |
1318 | if (cred->imsi == NULL || !cred->imsi[0] || |
1319 | cred->milenage == NULL || !cred->milenage[0]) | |
1320 | continue; | |
3b840b67 | 1321 | |
c21246f5 JM |
1322 | sep = os_strchr(cred->imsi, '-'); |
1323 | if (sep == NULL || | |
1324 | (sep - cred->imsi != 5 && sep - cred->imsi != 6)) | |
1325 | continue; | |
d7b01abd JM |
1326 | mnc_len = sep - cred->imsi - 3; |
1327 | imsi = cred->imsi; | |
c21246f5 | 1328 | |
d7b01abd JM |
1329 | #ifdef PCSC_FUNCS |
1330 | compare: | |
1331 | #endif /* PCSC_FUNCS */ | |
1bb7b8e8 JM |
1332 | wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from " |
1333 | MACSTR, MAC2STR(bss->bssid)); | |
476aed35 | 1334 | ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len); |
1bb7b8e8 | 1335 | wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not "); |
1a712d2f JM |
1336 | if (ret) { |
1337 | if (selected == NULL || | |
1338 | selected->priority < cred->priority) | |
1339 | selected = cred; | |
1340 | } | |
1bb7b8e8 | 1341 | } |
3b840b67 | 1342 | #endif /* INTERWORKING_3GPP */ |
1a712d2f | 1343 | return selected; |
3b840b67 JM |
1344 | } |
1345 | ||
1346 | ||
1a712d2f | 1347 | static struct wpa_cred * interworking_credentials_available_realm( |
3b840b67 | 1348 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
73c41a8f | 1349 | { |
1a712d2f | 1350 | struct wpa_cred *cred, *selected = NULL; |
73c41a8f JM |
1351 | struct nai_realm *realm; |
1352 | u16 count, i; | |
73c41a8f | 1353 | |
476aed35 | 1354 | if (bss->anqp == NULL || bss->anqp->nai_realm == NULL) |
1a712d2f | 1355 | return NULL; |
73c41a8f | 1356 | |
1bb7b8e8 | 1357 | if (wpa_s->conf->cred == NULL) |
1a712d2f | 1358 | return NULL; |
73c41a8f JM |
1359 | |
1360 | wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from " | |
1361 | MACSTR, MAC2STR(bss->bssid)); | |
476aed35 | 1362 | realm = nai_realm_parse(bss->anqp->nai_realm, &count); |
73c41a8f JM |
1363 | if (realm == NULL) { |
1364 | wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI " | |
1365 | "Realm list from " MACSTR, MAC2STR(bss->bssid)); | |
1a712d2f | 1366 | return NULL; |
73c41a8f JM |
1367 | } |
1368 | ||
1bb7b8e8 JM |
1369 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
1370 | if (cred->realm == NULL) | |
67e1b984 | 1371 | continue; |
1bb7b8e8 JM |
1372 | |
1373 | for (i = 0; i < count; i++) { | |
1374 | if (!nai_realm_match(&realm[i], cred->realm)) | |
1375 | continue; | |
1376 | if (nai_realm_find_eap(cred, &realm[i])) { | |
1a712d2f JM |
1377 | if (selected == NULL || |
1378 | selected->priority < cred->priority) | |
1379 | selected = cred; | |
1bb7b8e8 JM |
1380 | break; |
1381 | } | |
73c41a8f JM |
1382 | } |
1383 | } | |
1384 | ||
1385 | nai_realm_free(realm, count); | |
1386 | ||
1a712d2f | 1387 | return selected; |
73c41a8f JM |
1388 | } |
1389 | ||
1390 | ||
1a712d2f JM |
1391 | static struct wpa_cred * interworking_credentials_available( |
1392 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | |
3b840b67 | 1393 | { |
1a712d2f JM |
1394 | struct wpa_cred *cred, *cred2; |
1395 | ||
1396 | cred = interworking_credentials_available_realm(wpa_s, bss); | |
1397 | cred2 = interworking_credentials_available_3gpp(wpa_s, bss); | |
1398 | if (cred && cred2 && cred2->priority >= cred->priority) | |
1399 | cred = cred2; | |
1400 | if (!cred) | |
1401 | cred = cred2; | |
1402 | ||
955567bc JM |
1403 | cred2 = interworking_credentials_available_roaming_consortium(wpa_s, |
1404 | bss); | |
1405 | if (cred && cred2 && cred2->priority >= cred->priority) | |
1406 | cred = cred2; | |
1407 | if (!cred) | |
1408 | cred = cred2; | |
1409 | ||
1a712d2f | 1410 | return cred; |
3b840b67 JM |
1411 | } |
1412 | ||
1413 | ||
9914c96f JM |
1414 | static int domain_name_list_contains(struct wpabuf *domain_names, |
1415 | const char *domain) | |
00bf219d JM |
1416 | { |
1417 | const u8 *pos, *end; | |
1418 | size_t len; | |
1419 | ||
9914c96f | 1420 | len = os_strlen(domain); |
00bf219d JM |
1421 | pos = wpabuf_head(domain_names); |
1422 | end = pos + wpabuf_len(domain_names); | |
1423 | ||
1424 | while (pos + 1 < end) { | |
1425 | if (pos + 1 + pos[0] > end) | |
1426 | break; | |
1427 | ||
9914c96f JM |
1428 | wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name", |
1429 | pos + 1, pos[0]); | |
00bf219d | 1430 | if (pos[0] == len && |
9914c96f | 1431 | os_strncasecmp(domain, (const char *) (pos + 1), len) == 0) |
00bf219d JM |
1432 | return 1; |
1433 | ||
1434 | pos += 1 + pos[0]; | |
1435 | } | |
1436 | ||
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | ||
9914c96f JM |
1441 | static int interworking_home_sp(struct wpa_supplicant *wpa_s, |
1442 | struct wpabuf *domain_names) | |
1443 | { | |
1bb7b8e8 | 1444 | struct wpa_cred *cred; |
9914c96f JM |
1445 | #ifdef INTERWORKING_3GPP |
1446 | char nai[100], *realm; | |
1447 | #endif /* INTERWORKING_3GPP */ | |
1448 | ||
1bb7b8e8 | 1449 | if (domain_names == NULL || wpa_s->conf->cred == NULL) |
9914c96f JM |
1450 | return -1; |
1451 | ||
1bb7b8e8 | 1452 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
9914c96f | 1453 | #ifdef INTERWORKING_3GPP |
632c9458 JM |
1454 | char *imsi = NULL; |
1455 | int mnc_len = 0; | |
1456 | if (cred->imsi) | |
1457 | imsi = cred->imsi; | |
1f3a3ec4 | 1458 | #ifdef CONFIG_PCSC |
632c9458 JM |
1459 | else if (cred->pcsc && wpa_s->conf->pcsc_reader && |
1460 | wpa_s->scard && wpa_s->imsi[0]) { | |
1461 | imsi = wpa_s->imsi; | |
1462 | mnc_len = wpa_s->mnc_len; | |
1463 | } | |
1f3a3ec4 | 1464 | #endif /* CONFIG_PCSC */ |
632c9458 JM |
1465 | if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) |
1466 | == 0) { | |
1bb7b8e8 JM |
1467 | realm = os_strchr(nai, '@'); |
1468 | if (realm) | |
1469 | realm++; | |
1470 | wpa_printf(MSG_DEBUG, "Interworking: Search for match " | |
1471 | "with SIM/USIM domain %s", realm); | |
1472 | if (realm && | |
1473 | domain_name_list_contains(domain_names, realm)) | |
1474 | return 1; | |
1475 | } | |
1476 | #endif /* INTERWORKING_3GPP */ | |
1477 | ||
1478 | if (cred->domain == NULL) | |
1479 | continue; | |
1480 | ||
9914c96f | 1481 | wpa_printf(MSG_DEBUG, "Interworking: Search for match with " |
1bb7b8e8 JM |
1482 | "home SP FQDN %s", cred->domain); |
1483 | if (domain_name_list_contains(domain_names, cred->domain)) | |
9914c96f JM |
1484 | return 1; |
1485 | } | |
9914c96f | 1486 | |
1bb7b8e8 | 1487 | return 0; |
9914c96f JM |
1488 | } |
1489 | ||
1490 | ||
5ed7880d JM |
1491 | static int interworking_find_network_match(struct wpa_supplicant *wpa_s) |
1492 | { | |
1493 | struct wpa_bss *bss; | |
1494 | struct wpa_ssid *ssid; | |
1495 | ||
1496 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
1497 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { | |
349493bd | 1498 | if (wpas_network_disabled(wpa_s, ssid) || |
fea7c3a0 | 1499 | ssid->mode != WPAS_MODE_INFRA) |
5ed7880d JM |
1500 | continue; |
1501 | if (ssid->ssid_len != bss->ssid_len || | |
1502 | os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != | |
1503 | 0) | |
1504 | continue; | |
1505 | /* | |
1506 | * TODO: Consider more accurate matching of security | |
1507 | * configuration similarly to what is done in events.c | |
1508 | */ | |
1509 | return 1; | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | return 0; | |
1514 | } | |
1515 | ||
1516 | ||
b02fe7ff JM |
1517 | static void interworking_select_network(struct wpa_supplicant *wpa_s) |
1518 | { | |
00bf219d | 1519 | struct wpa_bss *bss, *selected = NULL, *selected_home = NULL; |
1a712d2f | 1520 | int selected_prio = -999999, selected_home_prio = -999999; |
b02fe7ff | 1521 | unsigned int count = 0; |
00bf219d JM |
1522 | const char *type; |
1523 | int res; | |
1a712d2f | 1524 | struct wpa_cred *cred; |
b02fe7ff JM |
1525 | |
1526 | wpa_s->network_select = 0; | |
1527 | ||
1528 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
1a712d2f JM |
1529 | cred = interworking_credentials_available(wpa_s, bss); |
1530 | if (!cred) | |
b02fe7ff | 1531 | continue; |
63e5caaa JM |
1532 | if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { |
1533 | /* | |
1534 | * We currently support only HS 2.0 networks and those | |
1535 | * are required to use WPA2-Enterprise. | |
1536 | */ | |
1537 | wpa_printf(MSG_DEBUG, "Interworking: Credential match " | |
1538 | "with " MACSTR " but network does not use " | |
1539 | "RSN", MAC2STR(bss->bssid)); | |
1540 | continue; | |
1541 | } | |
b02fe7ff | 1542 | count++; |
476aed35 JM |
1543 | res = interworking_home_sp(wpa_s, bss->anqp ? |
1544 | bss->anqp->domain_name : NULL); | |
00bf219d JM |
1545 | if (res > 0) |
1546 | type = "home"; | |
1547 | else if (res == 0) | |
1548 | type = "roaming"; | |
1549 | else | |
1550 | type = "unknown"; | |
1551 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s", | |
1552 | MAC2STR(bss->bssid), type); | |
4d5bda5f JM |
1553 | if (wpa_s->auto_select || |
1554 | (wpa_s->conf->auto_interworking && | |
1555 | wpa_s->auto_network_select)) { | |
1a712d2f JM |
1556 | if (selected == NULL || |
1557 | cred->priority > selected_prio) { | |
00bf219d | 1558 | selected = bss; |
1a712d2f JM |
1559 | selected_prio = cred->priority; |
1560 | } | |
1561 | if (res > 0 && | |
1562 | (selected_home == NULL || | |
1563 | cred->priority > selected_home_prio)) { | |
00bf219d | 1564 | selected_home = bss; |
1a712d2f JM |
1565 | selected_home_prio = cred->priority; |
1566 | } | |
00bf219d JM |
1567 | } |
1568 | } | |
1569 | ||
1a712d2f JM |
1570 | if (selected_home && selected_home != selected && |
1571 | selected_home_prio >= selected_prio) { | |
00bf219d JM |
1572 | /* Prefer network operated by the Home SP */ |
1573 | selected = selected_home; | |
b02fe7ff JM |
1574 | } |
1575 | ||
1576 | if (count == 0) { | |
5ed7880d JM |
1577 | /* |
1578 | * No matching network was found based on configured | |
1579 | * credentials. Check whether any of the enabled network blocks | |
1580 | * have matching APs. | |
1581 | */ | |
1582 | if (interworking_find_network_match(wpa_s)) { | |
1583 | wpa_printf(MSG_DEBUG, "Interworking: Possible BSS " | |
1584 | "match for enabled network configurations"); | |
2b95f57b JM |
1585 | if (wpa_s->auto_select) |
1586 | interworking_reconnect(wpa_s); | |
5ed7880d JM |
1587 | return; |
1588 | } | |
1589 | ||
4d5bda5f JM |
1590 | if (wpa_s->auto_network_select) { |
1591 | wpa_printf(MSG_DEBUG, "Interworking: Continue " | |
1592 | "scanning after ANQP fetch"); | |
1593 | wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, | |
1594 | 0); | |
1595 | return; | |
1596 | } | |
1597 | ||
b02fe7ff JM |
1598 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network " |
1599 | "with matching credentials found"); | |
1600 | } | |
1601 | ||
1602 | if (selected) | |
1603 | interworking_connect(wpa_s, selected); | |
1604 | } | |
1605 | ||
1606 | ||
53c9fc1d JM |
1607 | static struct wpa_bss_anqp * |
1608 | interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | |
1609 | { | |
1610 | struct wpa_bss *other; | |
1611 | ||
1612 | if (is_zero_ether_addr(bss->hessid)) | |
1613 | return NULL; /* Cannot be in the same homegenous ESS */ | |
1614 | ||
1615 | dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) { | |
1616 | if (other == bss) | |
1617 | continue; | |
1618 | if (other->anqp == NULL) | |
1619 | continue; | |
1620 | if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED)) | |
1621 | continue; | |
1622 | if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0) | |
1623 | continue; | |
1624 | if (bss->ssid_len != other->ssid_len || | |
1625 | os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0) | |
1626 | continue; | |
1627 | ||
1628 | wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with " | |
1629 | "already fetched BSSID " MACSTR " and " MACSTR, | |
1630 | MAC2STR(other->bssid), MAC2STR(bss->bssid)); | |
1631 | other->anqp->users++; | |
1632 | return other->anqp; | |
1633 | } | |
1634 | ||
1635 | return NULL; | |
1636 | } | |
1637 | ||
1638 | ||
afc064fe JM |
1639 | static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s) |
1640 | { | |
1641 | struct wpa_bss *bss; | |
1642 | int found = 0; | |
1643 | const u8 *ie; | |
1644 | ||
1645 | if (!wpa_s->fetch_anqp_in_progress) | |
1646 | return; | |
1647 | ||
1648 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
1649 | if (!(bss->caps & IEEE80211_CAP_ESS)) | |
1650 | continue; | |
1651 | ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB); | |
1652 | if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80)) | |
1653 | continue; /* AP does not support Interworking */ | |
1654 | ||
1655 | if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) { | |
476aed35 | 1656 | if (bss->anqp == NULL) { |
53c9fc1d JM |
1657 | bss->anqp = interworking_match_anqp_info(wpa_s, |
1658 | bss); | |
1659 | if (bss->anqp) { | |
1660 | /* Shared data already fetched */ | |
1661 | continue; | |
1662 | } | |
476aed35 JM |
1663 | bss->anqp = wpa_bss_anqp_alloc(); |
1664 | if (bss->anqp == NULL) | |
1665 | break; | |
1666 | } | |
afc064fe JM |
1667 | found++; |
1668 | bss->flags |= WPA_BSS_ANQP_FETCH_TRIED; | |
1669 | wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for " | |
1670 | MACSTR, MAC2STR(bss->bssid)); | |
1671 | interworking_anqp_send_req(wpa_s, bss); | |
1672 | break; | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | if (found == 0) { | |
1677 | wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed"); | |
1678 | wpa_s->fetch_anqp_in_progress = 0; | |
b02fe7ff JM |
1679 | if (wpa_s->network_select) |
1680 | interworking_select_network(wpa_s); | |
afc064fe JM |
1681 | } |
1682 | } | |
1683 | ||
1684 | ||
4d5bda5f | 1685 | void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s) |
afc064fe JM |
1686 | { |
1687 | struct wpa_bss *bss; | |
1688 | ||
afc064fe JM |
1689 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) |
1690 | bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED; | |
1691 | ||
1692 | wpa_s->fetch_anqp_in_progress = 1; | |
1693 | interworking_next_anqp_fetch(wpa_s); | |
b02fe7ff JM |
1694 | } |
1695 | ||
1696 | ||
1697 | int interworking_fetch_anqp(struct wpa_supplicant *wpa_s) | |
1698 | { | |
1699 | if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) | |
1700 | return 0; | |
1701 | ||
1702 | wpa_s->network_select = 0; | |
7ff83367 | 1703 | wpa_s->fetch_all_anqp = 1; |
b02fe7ff JM |
1704 | |
1705 | interworking_start_fetch_anqp(wpa_s); | |
afc064fe JM |
1706 | |
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | ||
1711 | void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s) | |
1712 | { | |
1713 | if (!wpa_s->fetch_anqp_in_progress) | |
1714 | return; | |
1715 | ||
1716 | wpa_s->fetch_anqp_in_progress = 0; | |
1717 | } | |
1718 | ||
1719 | ||
1720 | int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, | |
1721 | u16 info_ids[], size_t num_ids) | |
1722 | { | |
1723 | struct wpabuf *buf; | |
1724 | int ret = 0; | |
1725 | int freq; | |
1726 | struct wpa_bss *bss; | |
1727 | int res; | |
1728 | ||
1729 | freq = wpa_s->assoc_freq; | |
1730 | bss = wpa_bss_get_bssid(wpa_s, dst); | |
485e3a92 JM |
1731 | if (bss) { |
1732 | wpa_bss_anqp_unshare_alloc(bss); | |
afc064fe | 1733 | freq = bss->freq; |
485e3a92 | 1734 | } |
afc064fe JM |
1735 | if (freq <= 0) |
1736 | return -1; | |
1737 | ||
1738 | wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)", | |
1739 | MAC2STR(dst), (unsigned int) num_ids); | |
1740 | ||
1741 | buf = anqp_build_req(info_ids, num_ids, NULL); | |
1742 | if (buf == NULL) | |
1743 | return -1; | |
1744 | ||
1745 | res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s); | |
1746 | if (res < 0) { | |
1747 | wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request"); | |
1748 | ret = -1; | |
1749 | } else | |
1750 | wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token " | |
1751 | "%u", res); | |
1752 | ||
1753 | wpabuf_free(buf); | |
1754 | return ret; | |
1755 | } | |
1756 | ||
1757 | ||
1758 | static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s, | |
1759 | const u8 *sa, u16 info_id, | |
1760 | const u8 *data, size_t slen) | |
1761 | { | |
1762 | const u8 *pos = data; | |
1763 | struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa); | |
476aed35 | 1764 | struct wpa_bss_anqp *anqp = NULL; |
2249d714 JM |
1765 | #ifdef CONFIG_HS20 |
1766 | u8 type; | |
1767 | #endif /* CONFIG_HS20 */ | |
afc064fe | 1768 | |
476aed35 JM |
1769 | if (bss) |
1770 | anqp = bss->anqp; | |
1771 | ||
afc064fe JM |
1772 | switch (info_id) { |
1773 | case ANQP_CAPABILITY_LIST: | |
1774 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1775 | " ANQP Capability list", MAC2STR(sa)); | |
1776 | break; | |
1777 | case ANQP_VENUE_NAME: | |
1778 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1779 | " Venue Name", MAC2STR(sa)); | |
1780 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen); | |
476aed35 JM |
1781 | if (anqp) { |
1782 | wpabuf_free(anqp->venue_name); | |
1783 | anqp->venue_name = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1784 | } |
1785 | break; | |
1786 | case ANQP_NETWORK_AUTH_TYPE: | |
1787 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1788 | " Network Authentication Type information", | |
1789 | MAC2STR(sa)); | |
1790 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication " | |
1791 | "Type", pos, slen); | |
476aed35 JM |
1792 | if (anqp) { |
1793 | wpabuf_free(anqp->network_auth_type); | |
1794 | anqp->network_auth_type = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1795 | } |
1796 | break; | |
1797 | case ANQP_ROAMING_CONSORTIUM: | |
1798 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1799 | " Roaming Consortium list", MAC2STR(sa)); | |
1800 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium", | |
1801 | pos, slen); | |
476aed35 JM |
1802 | if (anqp) { |
1803 | wpabuf_free(anqp->roaming_consortium); | |
1804 | anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1805 | } |
1806 | break; | |
1807 | case ANQP_IP_ADDR_TYPE_AVAILABILITY: | |
1808 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1809 | " IP Address Type Availability information", | |
1810 | MAC2STR(sa)); | |
1811 | wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability", | |
1812 | pos, slen); | |
476aed35 JM |
1813 | if (anqp) { |
1814 | wpabuf_free(anqp->ip_addr_type_availability); | |
1815 | anqp->ip_addr_type_availability = | |
afc064fe JM |
1816 | wpabuf_alloc_copy(pos, slen); |
1817 | } | |
1818 | break; | |
1819 | case ANQP_NAI_REALM: | |
1820 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1821 | " NAI Realm list", MAC2STR(sa)); | |
1822 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen); | |
476aed35 JM |
1823 | if (anqp) { |
1824 | wpabuf_free(anqp->nai_realm); | |
1825 | anqp->nai_realm = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1826 | } |
1827 | break; | |
1828 | case ANQP_3GPP_CELLULAR_NETWORK: | |
1829 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1830 | " 3GPP Cellular Network information", MAC2STR(sa)); | |
1831 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network", | |
1832 | pos, slen); | |
476aed35 JM |
1833 | if (anqp) { |
1834 | wpabuf_free(anqp->anqp_3gpp); | |
1835 | anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1836 | } |
1837 | break; | |
1838 | case ANQP_DOMAIN_NAME: | |
1839 | wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR | |
1840 | " Domain Name list", MAC2STR(sa)); | |
1841 | wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen); | |
476aed35 JM |
1842 | if (anqp) { |
1843 | wpabuf_free(anqp->domain_name); | |
1844 | anqp->domain_name = wpabuf_alloc_copy(pos, slen); | |
afc064fe JM |
1845 | } |
1846 | break; | |
1847 | case ANQP_VENDOR_SPECIFIC: | |
1848 | if (slen < 3) | |
1849 | return; | |
1850 | ||
1851 | switch (WPA_GET_BE24(pos)) { | |
2249d714 JM |
1852 | #ifdef CONFIG_HS20 |
1853 | case OUI_WFA: | |
1854 | pos += 3; | |
1855 | slen -= 3; | |
1856 | ||
1857 | if (slen < 1) | |
1858 | return; | |
1859 | type = *pos++; | |
1860 | slen--; | |
1861 | ||
1862 | switch (type) { | |
1863 | case HS20_ANQP_OUI_TYPE: | |
1864 | hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos, | |
1865 | slen); | |
1866 | break; | |
1867 | default: | |
1868 | wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP " | |
1869 | "vendor type %u", type); | |
1870 | break; | |
1871 | } | |
1872 | break; | |
1873 | #endif /* CONFIG_HS20 */ | |
afc064fe JM |
1874 | default: |
1875 | wpa_printf(MSG_DEBUG, "Interworking: Unsupported " | |
1876 | "vendor-specific ANQP OUI %06x", | |
1877 | WPA_GET_BE24(pos)); | |
1878 | return; | |
1879 | } | |
1880 | break; | |
1881 | default: | |
1882 | wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID " | |
1883 | "%u", info_id); | |
1884 | break; | |
1885 | } | |
1886 | } | |
1887 | ||
1888 | ||
1889 | void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token, | |
1890 | enum gas_query_result result, | |
1891 | const struct wpabuf *adv_proto, | |
1892 | const struct wpabuf *resp, u16 status_code) | |
1893 | { | |
1894 | struct wpa_supplicant *wpa_s = ctx; | |
1895 | const u8 *pos; | |
1896 | const u8 *end; | |
1897 | u16 info_id; | |
1898 | u16 slen; | |
1899 | ||
1900 | if (result != GAS_QUERY_SUCCESS) | |
1901 | return; | |
1902 | ||
1903 | pos = wpabuf_head(adv_proto); | |
1904 | if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO || | |
1905 | pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) { | |
1906 | wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement " | |
1907 | "Protocol in response"); | |
1908 | return; | |
1909 | } | |
1910 | ||
1911 | pos = wpabuf_head(resp); | |
1912 | end = pos + wpabuf_len(resp); | |
1913 | ||
1914 | while (pos < end) { | |
1915 | if (pos + 4 > end) { | |
1916 | wpa_printf(MSG_DEBUG, "ANQP: Invalid element"); | |
1917 | break; | |
1918 | } | |
1919 | info_id = WPA_GET_LE16(pos); | |
1920 | pos += 2; | |
1921 | slen = WPA_GET_LE16(pos); | |
1922 | pos += 2; | |
1923 | if (pos + slen > end) { | |
1924 | wpa_printf(MSG_DEBUG, "ANQP: Invalid element length " | |
1925 | "for Info ID %u", info_id); | |
1926 | break; | |
1927 | } | |
1928 | interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos, | |
1929 | slen); | |
1930 | pos += slen; | |
1931 | } | |
1932 | } | |
b02fe7ff JM |
1933 | |
1934 | ||
1935 | static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s, | |
1936 | struct wpa_scan_results *scan_res) | |
1937 | { | |
1938 | wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start " | |
1939 | "ANQP fetch"); | |
1940 | interworking_start_fetch_anqp(wpa_s); | |
1941 | } | |
1942 | ||
1943 | ||
1944 | int interworking_select(struct wpa_supplicant *wpa_s, int auto_select) | |
1945 | { | |
1946 | interworking_stop_fetch_anqp(wpa_s); | |
1947 | wpa_s->network_select = 1; | |
4d5bda5f | 1948 | wpa_s->auto_network_select = 0; |
b02fe7ff | 1949 | wpa_s->auto_select = !!auto_select; |
7ff83367 | 1950 | wpa_s->fetch_all_anqp = 0; |
b02fe7ff JM |
1951 | wpa_printf(MSG_DEBUG, "Interworking: Start scan for network " |
1952 | "selection"); | |
1953 | wpa_s->scan_res_handler = interworking_scan_res_handler; | |
1954 | wpa_s->scan_req = 2; | |
1955 | wpa_supplicant_req_scan(wpa_s, 0, 0); | |
1956 | ||
1957 | return 0; | |
1958 | } | |
b1f12296 JM |
1959 | |
1960 | ||
1961 | static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token, | |
1962 | enum gas_query_result result, | |
1963 | const struct wpabuf *adv_proto, | |
1964 | const struct wpabuf *resp, u16 status_code) | |
1965 | { | |
1966 | struct wpa_supplicant *wpa_s = ctx; | |
1967 | ||
1968 | wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR | |
1969 | " dialog_token=%d status_code=%d resp_len=%d", | |
1970 | MAC2STR(addr), dialog_token, status_code, | |
1971 | resp ? (int) wpabuf_len(resp) : -1); | |
1972 | if (!resp) | |
1973 | return; | |
1974 | ||
1975 | wpabuf_free(wpa_s->last_gas_resp); | |
1976 | wpa_s->last_gas_resp = wpabuf_dup(resp); | |
1977 | if (wpa_s->last_gas_resp == NULL) | |
1978 | return; | |
1979 | os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN); | |
1980 | wpa_s->last_gas_dialog_token = dialog_token; | |
1981 | } | |
1982 | ||
1983 | ||
1984 | int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst, | |
1985 | const struct wpabuf *adv_proto, | |
1986 | const struct wpabuf *query) | |
1987 | { | |
1988 | struct wpabuf *buf; | |
1989 | int ret = 0; | |
1990 | int freq; | |
1991 | struct wpa_bss *bss; | |
1992 | int res; | |
1993 | size_t len; | |
1994 | u8 query_resp_len_limit = 0, pame_bi = 0; | |
1995 | ||
1996 | freq = wpa_s->assoc_freq; | |
1997 | bss = wpa_bss_get_bssid(wpa_s, dst); | |
1998 | if (bss) | |
1999 | freq = bss->freq; | |
2000 | if (freq <= 0) | |
2001 | return -1; | |
2002 | ||
2003 | wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)", | |
2004 | MAC2STR(dst), freq); | |
2005 | wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto); | |
2006 | wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query); | |
2007 | ||
2008 | len = 3 + wpabuf_len(adv_proto) + 2; | |
2009 | if (query) | |
2010 | len += wpabuf_len(query); | |
2011 | buf = gas_build_initial_req(0, len); | |
2012 | if (buf == NULL) | |
2013 | return -1; | |
2014 | ||
2015 | /* Advertisement Protocol IE */ | |
2016 | wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO); | |
2017 | wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */ | |
2018 | wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) | | |
2019 | (pame_bi ? 0x80 : 0)); | |
2020 | wpabuf_put_buf(buf, adv_proto); | |
2021 | ||
2022 | /* GAS Query */ | |
2023 | if (query) { | |
2024 | wpabuf_put_le16(buf, wpabuf_len(query)); | |
2025 | wpabuf_put_buf(buf, query); | |
2026 | } else | |
2027 | wpabuf_put_le16(buf, 0); | |
2028 | ||
2029 | res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s); | |
2030 | if (res < 0) { | |
2031 | wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request"); | |
2032 | ret = -1; | |
2033 | } else | |
2034 | wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token " | |
2035 | "%u", res); | |
2036 | ||
2037 | wpabuf_free(buf); | |
2038 | return ret; | |
2039 | } |