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