OSDN Git Service

Update to new version 0.8.16 from BRCM
[android-x86/external-wpa_supplicant_8.git] / wpa_supplicant / interworking.c
1 /*
2  * Interworking (IEEE 802.11u)
3  * Copyright (c) 2011, Qualcomm Atheros
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/gas.h"
20 #include "common/wpa_ctrl.h"
21 #include "drivers/driver.h"
22 #include "eap_common/eap_defs.h"
23 #include "eap_peer/eap_methods.h"
24 #include "wpa_supplicant_i.h"
25 #include "config.h"
26 #include "bss.h"
27 #include "scan.h"
28 #include "notify.h"
29 #include "gas_query.h"
30 #include "interworking.h"
31
32
33 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
34 #define INTERWORKING_3GPP
35 #else
36 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
37 #define INTERWORKING_3GPP
38 #else
39 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
40 #define INTERWORKING_3GPP
41 #endif
42 #endif
43 #endif
44
45 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
46
47
48 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
49                                       struct wpabuf *extra)
50 {
51         struct wpabuf *buf;
52         size_t i;
53         u8 *len_pos;
54
55         buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
56                                          (extra ? wpabuf_len(extra) : 0));
57         if (buf == NULL)
58                 return NULL;
59
60         len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
61         for (i = 0; i < num_ids; i++)
62                 wpabuf_put_le16(buf, info_ids[i]);
63         gas_anqp_set_element_len(buf, len_pos);
64         if (extra)
65                 wpabuf_put_buf(buf, extra);
66
67         gas_anqp_set_len(buf);
68
69         return buf;
70 }
71
72
73 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
74                                       u8 dialog_token,
75                                       enum gas_query_result result,
76                                       const struct wpabuf *adv_proto,
77                                       const struct wpabuf *resp,
78                                       u16 status_code)
79 {
80         struct wpa_supplicant *wpa_s = ctx;
81
82         anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
83                      status_code);
84         interworking_next_anqp_fetch(wpa_s);
85 }
86
87
88 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
89                                       struct wpa_bss *bss)
90 {
91         struct wpabuf *buf;
92         int ret = 0;
93         int res;
94         u16 info_ids[] = {
95                 ANQP_CAPABILITY_LIST,
96                 ANQP_VENUE_NAME,
97                 ANQP_NETWORK_AUTH_TYPE,
98                 ANQP_ROAMING_CONSORTIUM,
99                 ANQP_IP_ADDR_TYPE_AVAILABILITY,
100                 ANQP_NAI_REALM,
101                 ANQP_3GPP_CELLULAR_NETWORK,
102                 ANQP_DOMAIN_NAME
103         };
104         struct wpabuf *extra = NULL;
105
106         wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
107                    MAC2STR(bss->bssid));
108
109         buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
110                              extra);
111         wpabuf_free(extra);
112         if (buf == NULL)
113                 return -1;
114
115         res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
116                             interworking_anqp_resp_cb, wpa_s);
117         if (res < 0) {
118                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
119                 ret = -1;
120         } else
121                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
122                            "%u", res);
123
124         wpabuf_free(buf);
125         return ret;
126 }
127
128
129 struct nai_realm_eap {
130         u8 method;
131         u8 inner_method;
132         enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
133         u8 cred_type;
134         u8 tunneled_cred_type;
135 };
136
137 struct nai_realm {
138         u8 encoding;
139         char *realm;
140         u8 eap_count;
141         struct nai_realm_eap *eap;
142 };
143
144
145 static void nai_realm_free(struct nai_realm *realms, u16 count)
146 {
147         u16 i;
148
149         if (realms == NULL)
150                 return;
151         for (i = 0; i < count; i++) {
152                 os_free(realms[i].eap);
153                 os_free(realms[i].realm);
154         }
155         os_free(realms);
156 }
157
158
159 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
160                                       const u8 *end)
161 {
162         u8 elen, auth_count, a;
163         const u8 *e_end;
164
165         if (pos + 3 > end) {
166                 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
167                 return NULL;
168         }
169
170         elen = *pos++;
171         if (pos + elen > end || elen < 2) {
172                 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
173                 return NULL;
174         }
175         e_end = pos + elen;
176         e->method = *pos++;
177         auth_count = *pos++;
178         wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
179                    elen, e->method, auth_count);
180
181         for (a = 0; a < auth_count; a++) {
182                 u8 id, len;
183
184                 if (pos + 2 > end || pos + 2 + pos[1] > end) {
185                         wpa_printf(MSG_DEBUG, "No room for Authentication "
186                                    "Parameter subfield");
187                         return NULL;
188                 }
189
190                 id = *pos++;
191                 len = *pos++;
192
193                 switch (id) {
194                 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
195                         if (len < 1)
196                                 break;
197                         e->inner_non_eap = *pos;
198                         if (e->method != EAP_TYPE_TTLS)
199                                 break;
200                         switch (*pos) {
201                         case NAI_REALM_INNER_NON_EAP_PAP:
202                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
203                                 break;
204                         case NAI_REALM_INNER_NON_EAP_CHAP:
205                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
206                                 break;
207                         case NAI_REALM_INNER_NON_EAP_MSCHAP:
208                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
209                                 break;
210                         case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
211                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
212                                 break;
213                         }
214                         break;
215                 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
216                         if (len < 1)
217                                 break;
218                         e->inner_method = *pos;
219                         wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
220                                    e->inner_method);
221                         break;
222                 case NAI_REALM_EAP_AUTH_CRED_TYPE:
223                         if (len < 1)
224                                 break;
225                         e->cred_type = *pos;
226                         wpa_printf(MSG_DEBUG, "Credential Type: %u",
227                                    e->cred_type);
228                         break;
229                 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
230                         if (len < 1)
231                                 break;
232                         e->tunneled_cred_type = *pos;
233                         wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
234                                    "Type: %u", e->tunneled_cred_type);
235                         break;
236                 default:
237                         wpa_printf(MSG_DEBUG, "Unsupported Authentication "
238                                    "Parameter: id=%u len=%u", id, len);
239                         wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
240                                     "Value", pos, len);
241                         break;
242                 }
243
244                 pos += len;
245         }
246
247         return e_end;
248 }
249
250
251 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
252                                         const u8 *end)
253 {
254         u16 len;
255         const u8 *f_end;
256         u8 realm_len, e;
257
258         if (end - pos < 4) {
259                 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
260                            "fixed fields");
261                 return NULL;
262         }
263
264         len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
265         pos += 2;
266         if (pos + len > end || len < 3) {
267                 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
268                            "(len=%u; left=%u)",
269                            len, (unsigned int) (end - pos));
270                 return NULL;
271         }
272         f_end = pos + len;
273
274         r->encoding = *pos++;
275         realm_len = *pos++;
276         if (pos + realm_len > f_end) {
277                 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
278                            "(len=%u; left=%u)",
279                            realm_len, (unsigned int) (f_end - pos));
280                 return NULL;
281         }
282         wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
283         r->realm = os_malloc(realm_len + 1);
284         if (r->realm == NULL)
285                 return NULL;
286         os_memcpy(r->realm, pos, realm_len);
287         r->realm[realm_len] = '\0';
288         pos += realm_len;
289
290         if (pos + 1 > f_end) {
291                 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
292                 return NULL;
293         }
294         r->eap_count = *pos++;
295         wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
296         if (pos + r->eap_count * 3 > f_end) {
297                 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
298                 return NULL;
299         }
300         r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
301         if (r->eap == NULL)
302                 return NULL;
303
304         for (e = 0; e < r->eap_count; e++) {
305                 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
306                 if (pos == NULL)
307                         return NULL;
308         }
309
310         return f_end;
311 }
312
313
314 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
315 {
316         struct nai_realm *realm;
317         const u8 *pos, *end;
318         u16 i, num;
319
320         if (anqp == NULL || wpabuf_len(anqp) < 2)
321                 return NULL;
322
323         pos = wpabuf_head_u8(anqp);
324         end = pos + wpabuf_len(anqp);
325         num = WPA_GET_LE16(pos);
326         wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
327         pos += 2;
328
329         if (num * 5 > end - pos) {
330                 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
331                            "enough data (%u octets) for that many realms",
332                            num, (unsigned int) (end - pos));
333                 return NULL;
334         }
335
336         realm = os_zalloc(num * sizeof(struct nai_realm));
337         if (realm == NULL)
338                 return NULL;
339
340         for (i = 0; i < num; i++) {
341                 pos = nai_realm_parse_realm(&realm[i], pos, end);
342                 if (pos == NULL) {
343                         nai_realm_free(realm, num);
344                         return NULL;
345                 }
346         }
347
348         *count = num;
349         return realm;
350 }
351
352
353 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
354 {
355         char *tmp, *pos, *end;
356         int match = 0;
357
358         if (realm->realm == NULL || home_realm == NULL)
359                 return 0;
360
361         if (os_strchr(realm->realm, ';') == NULL)
362                 return os_strcasecmp(realm->realm, home_realm) == 0;
363
364         tmp = os_strdup(realm->realm);
365         if (tmp == NULL)
366                 return 0;
367
368         pos = tmp;
369         while (*pos) {
370                 end = os_strchr(pos, ';');
371                 if (end)
372                         *end = '\0';
373                 if (os_strcasecmp(pos, home_realm) == 0) {
374                         match = 1;
375                         break;
376                 }
377                 if (end == NULL)
378                         break;
379                 pos = end + 1;
380         }
381
382         os_free(tmp);
383
384         return match;
385 }
386
387
388 static int nai_realm_cred_username(struct nai_realm_eap *eap)
389 {
390         if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
391                 return 0; /* method not supported */
392
393         if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
394                 /* Only tunneled methods with username/password supported */
395                 return 0;
396         }
397
398         if (eap->method == EAP_TYPE_PEAP &&
399             eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
400                 return 0;
401
402         if (eap->method == EAP_TYPE_TTLS) {
403                 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
404                         return 0;
405                 if (eap->inner_method &&
406                     eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
407                         return 0;
408                 if (eap->inner_non_eap &&
409                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
410                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
411                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
412                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
413                         return 0;
414         }
415
416         if (eap->inner_method &&
417             eap->inner_method != EAP_TYPE_GTC &&
418             eap->inner_method != EAP_TYPE_MSCHAPV2)
419                 return 0;
420
421         return 1;
422 }
423
424
425 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
426                                                  struct nai_realm *realm)
427 {
428         u8 e;
429
430         if (wpa_s->conf->home_username == NULL ||
431             wpa_s->conf->home_username[0] == '\0' ||
432             wpa_s->conf->home_password == NULL ||
433             wpa_s->conf->home_password[0] == '\0')
434                 return NULL;
435
436         for (e = 0; e < realm->eap_count; e++) {
437                 struct nai_realm_eap *eap = &realm->eap[e];
438                 if (nai_realm_cred_username(eap))
439                         return eap;
440         }
441
442         return NULL;
443 }
444
445
446 #ifdef INTERWORKING_3GPP
447
448 static int plmn_id_match(struct wpabuf *anqp, const char *imsi)
449 {
450         const char *sep;
451         u8 plmn[3];
452         const u8 *pos, *end;
453         u8 udhl;
454
455         sep = os_strchr(imsi, '-');
456         if (sep == NULL || (sep - imsi != 5 && sep - imsi != 6))
457                 return 0;
458
459         /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
460         plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
461         plmn[1] = imsi[2] - '0';
462         if (sep - imsi == 6)
463                 plmn[1] |= (imsi[5] - '0') << 4;
464         else
465                 plmn[1] |= 0xf0;
466         plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
467
468         if (anqp == NULL)
469                 return 0;
470         pos = wpabuf_head_u8(anqp);
471         end = pos + wpabuf_len(anqp);
472         if (pos + 2 > end)
473                 return 0;
474         if (*pos != 0) {
475                 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
476                 return 0;
477         }
478         pos++;
479         udhl = *pos++;
480         if (pos + udhl > end) {
481                 wpa_printf(MSG_DEBUG, "Invalid UDHL");
482                 return 0;
483         }
484         end = pos + udhl;
485
486         while (pos + 2 <= end) {
487                 u8 iei, len;
488                 const u8 *l_end;
489                 iei = *pos++;
490                 len = *pos++ & 0x7f;
491                 if (pos + len > end)
492                         break;
493                 l_end = pos + len;
494
495                 if (iei == 0 && len > 0) {
496                         /* PLMN List */
497                         u8 num, i;
498                         num = *pos++;
499                         for (i = 0; i < num; i++) {
500                                 if (pos + 3 > end)
501                                         break;
502                                 if (os_memcmp(pos, plmn, 3) == 0)
503                                         return 1; /* Found matching PLMN */
504                         }
505                 }
506
507                 pos = l_end;
508         }
509
510         return 0;
511 }
512
513
514 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
515 {
516         const char *sep, *msin;
517         char nai[100], *end, *pos;
518         size_t msin_len, plmn_len;
519
520         /*
521          * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
522          * Root NAI:
523          * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
524          * <MNC> is zero-padded to three digits in case two-digit MNC is used
525          */
526
527         if (imsi == NULL || os_strlen(imsi) > 16) {
528                 wpa_printf(MSG_DEBUG, "No valid IMSI available");
529                 return -1;
530         }
531         sep = os_strchr(imsi, '-');
532         if (sep == NULL)
533                 return -1;
534         plmn_len = sep - imsi;
535         if (plmn_len != 5 && plmn_len != 6)
536                 return -1;
537         msin = sep + 1;
538         msin_len = os_strlen(msin);
539
540         pos = nai;
541         end = pos + sizeof(nai);
542         *pos++ = prefix;
543         os_memcpy(pos, imsi, plmn_len);
544         pos += plmn_len;
545         os_memcpy(pos, msin, msin_len);
546         pos += msin_len;
547         pos += os_snprintf(pos, end - pos, "@wlan.mnc");
548         if (plmn_len == 5) {
549                 *pos++ = '0';
550                 *pos++ = imsi[3];
551                 *pos++ = imsi[4];
552         } else {
553                 *pos++ = imsi[3];
554                 *pos++ = imsi[4];
555                 *pos++ = imsi[5];
556         }
557         pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
558                            imsi[0], imsi[1], imsi[2]);
559
560         return wpa_config_set_quoted(ssid, "identity", nai);
561 }
562
563 #endif /* INTERWORKING_3GPP */
564
565
566 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
567                                      struct wpa_bss *bss)
568 {
569 #ifdef INTERWORKING_3GPP
570         struct wpa_ssid *ssid;
571         const u8 *ie;
572
573         ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
574         if (ie == NULL)
575                 return -1;
576         wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
577                    MAC2STR(bss->bssid));
578
579         ssid = wpa_config_add_network(wpa_s->conf);
580         if (ssid == NULL)
581                 return -1;
582
583         wpas_notify_network_added(wpa_s, ssid);
584         wpa_config_set_network_defaults(ssid);
585         ssid->temporary = 1;
586         ssid->ssid = os_zalloc(ie[1] + 1);
587         if (ssid->ssid == NULL)
588                 goto fail;
589         os_memcpy(ssid->ssid, ie + 2, ie[1]);
590         ssid->ssid_len = ie[1];
591
592         /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
593         if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
594                 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
595                 goto fail;
596         }
597         if (set_root_nai(ssid, wpa_s->conf->home_imsi, '1') < 0) {
598                 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
599                 goto fail;
600         }
601
602         if (wpa_s->conf->home_milenage && wpa_s->conf->home_milenage[0]) {
603                 if (wpa_config_set_quoted(ssid, "password",
604                                           wpa_s->conf->home_milenage) < 0)
605                         goto fail;
606         } else {
607                 /* TODO: PIN */
608                 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
609                         goto fail;
610         }
611
612         if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
613             wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
614             < 0)
615                 goto fail;
616
617         wpa_supplicant_select_network(wpa_s, ssid);
618
619         return 0;
620
621 fail:
622         wpas_notify_network_removed(wpa_s, ssid);
623         wpa_config_remove_network(wpa_s->conf, ssid->id);
624 #endif /* INTERWORKING_3GPP */
625         return -1;
626 }
627
628
629 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
630 {
631         struct wpa_ssid *ssid;
632         struct nai_realm *realm;
633         struct nai_realm_eap *eap = NULL;
634         u16 count, i;
635         char buf[100];
636         const u8 *ie;
637
638         if (bss == NULL)
639                 return -1;
640         ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
641         if (ie == NULL || ie[1] == 0) {
642                 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
643                            MACSTR, MAC2STR(bss->bssid));
644                 return -1;
645         }
646
647         realm = nai_realm_parse(bss->anqp_nai_realm, &count);
648         if (realm == NULL) {
649                 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
650                            "Realm list from " MACSTR, MAC2STR(bss->bssid));
651                 count = 0;
652         }
653
654         for (i = 0; i < count; i++) {
655                 if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
656                         continue;
657                 eap = nai_realm_find_eap(wpa_s, &realm[i]);
658                 if (eap)
659                         break;
660         }
661
662         if (!eap) {
663                 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
664                         if (realm)
665                                 nai_realm_free(realm, count);
666                         return 0;
667                 }
668
669                 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
670                            "and EAP method found for " MACSTR,
671                            MAC2STR(bss->bssid));
672                 nai_realm_free(realm, count);
673                 return -1;
674         }
675
676         wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
677                    MAC2STR(bss->bssid));
678
679         ssid = wpa_config_add_network(wpa_s->conf);
680         if (ssid == NULL) {
681                 nai_realm_free(realm, count);
682                 return -1;
683         }
684         wpas_notify_network_added(wpa_s, ssid);
685         wpa_config_set_network_defaults(ssid);
686         ssid->temporary = 1;
687         ssid->ssid = os_zalloc(ie[1] + 1);
688         if (ssid->ssid == NULL)
689                 goto fail;
690         os_memcpy(ssid->ssid, ie + 2, ie[1]);
691         ssid->ssid_len = ie[1];
692
693         if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
694                                                      eap->method), 0) < 0)
695                 goto fail;
696
697         if (wpa_s->conf->home_username && wpa_s->conf->home_username[0] &&
698             wpa_config_set_quoted(ssid, "identity",
699                                   wpa_s->conf->home_username) < 0)
700                 goto fail;
701
702         if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
703             wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
704             < 0)
705                 goto fail;
706
707         switch (eap->method) {
708         case EAP_TYPE_TTLS:
709                 if (eap->inner_method) {
710                         os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
711                                     eap_get_name(EAP_VENDOR_IETF,
712                                                  eap->inner_method));
713                         if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
714                                 goto fail;
715                         break;
716                 }
717                 switch (eap->inner_non_eap) {
718                 case NAI_REALM_INNER_NON_EAP_PAP:
719                         if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
720                             0)
721                                 goto fail;
722                         break;
723                 case NAI_REALM_INNER_NON_EAP_CHAP:
724                         if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
725                             < 0)
726                                 goto fail;
727                         break;
728                 case NAI_REALM_INNER_NON_EAP_MSCHAP:
729                         if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
730                             < 0)
731                                 goto fail;
732                         break;
733                 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
734                         if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
735                                            0) < 0)
736                                 goto fail;
737                         break;
738                 }
739                 break;
740         case EAP_TYPE_PEAP:
741                 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
742                             eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
743                 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
744                         goto fail;
745                 break;
746         }
747
748         if (wpa_s->conf->home_ca_cert && wpa_s->conf->home_ca_cert[0] &&
749             wpa_config_set_quoted(ssid, "ca_cert", wpa_s->conf->home_ca_cert) <
750             0)
751                 goto fail;
752
753         nai_realm_free(realm, count);
754
755         wpa_supplicant_select_network(wpa_s, ssid);
756
757         return 0;
758
759 fail:
760         wpas_notify_network_removed(wpa_s, ssid);
761         wpa_config_remove_network(wpa_s->conf, ssid->id);
762         nai_realm_free(realm, count);
763         return -1;
764 }
765
766
767 static int interworking_credentials_available_3gpp(
768         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
769 {
770         int ret = 0;
771
772 #ifdef INTERWORKING_3GPP
773         if (bss->anqp_3gpp == NULL)
774                 return ret;
775
776         if (wpa_s->conf->home_imsi == NULL || !wpa_s->conf->home_imsi[0] ||
777             wpa_s->conf->home_milenage == NULL ||
778             !wpa_s->conf->home_milenage[0])
779                 return ret;
780
781         wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from " MACSTR,
782                    MAC2STR(bss->bssid));
783         ret = plmn_id_match(bss->anqp_3gpp, wpa_s->conf->home_imsi);
784         wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
785 #endif /* INTERWORKING_3GPP */
786         return ret;
787 }
788
789
790 static int interworking_credentials_available_realm(
791         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
792 {
793         struct nai_realm *realm;
794         u16 count, i;
795         int found = 0;
796
797         if (bss->anqp_nai_realm == NULL)
798                 return 0;
799
800         if (wpa_s->conf->home_realm == NULL)
801                 return 0;
802
803         wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
804                    MACSTR, MAC2STR(bss->bssid));
805         realm = nai_realm_parse(bss->anqp_nai_realm, &count);
806         if (realm == NULL) {
807                 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
808                            "Realm list from " MACSTR, MAC2STR(bss->bssid));
809                 return 0;
810         }
811
812         for (i = 0; i < count; i++) {
813                 if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
814                         continue;
815                 if (nai_realm_find_eap(wpa_s, &realm[i])) {
816                         found++;
817                         break;
818                 }
819         }
820
821         nai_realm_free(realm, count);
822
823         return found;
824 }
825
826
827 static int interworking_credentials_available(struct wpa_supplicant *wpa_s,
828                                               struct wpa_bss *bss)
829 {
830         return interworking_credentials_available_realm(wpa_s, bss) ||
831                 interworking_credentials_available_3gpp(wpa_s, bss);
832 }
833
834
835 static void interworking_select_network(struct wpa_supplicant *wpa_s)
836 {
837         struct wpa_bss *bss, *selected = NULL;
838         unsigned int count = 0;
839
840         wpa_s->network_select = 0;
841
842         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
843                 if (!interworking_credentials_available(wpa_s, bss))
844                         continue;
845                 count++;
846                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR,
847                         MAC2STR(bss->bssid));
848                 if (selected == NULL && wpa_s->auto_select)
849                         selected = bss;
850         }
851
852         if (count == 0) {
853                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
854                         "with matching credentials found");
855         }
856
857         if (selected)
858                 interworking_connect(wpa_s, selected);
859 }
860
861
862 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
863 {
864         struct wpa_bss *bss;
865         int found = 0;
866         const u8 *ie;
867
868         if (!wpa_s->fetch_anqp_in_progress)
869                 return;
870
871         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
872                 if (!(bss->caps & IEEE80211_CAP_ESS))
873                         continue;
874                 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
875                 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
876                         continue; /* AP does not support Interworking */
877
878                 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
879                         found++;
880                         bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
881                         wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
882                                 MACSTR, MAC2STR(bss->bssid));
883                         interworking_anqp_send_req(wpa_s, bss);
884                         break;
885                 }
886         }
887
888         if (found == 0) {
889                 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
890                 wpa_s->fetch_anqp_in_progress = 0;
891                 if (wpa_s->network_select)
892                         interworking_select_network(wpa_s);
893         }
894 }
895
896
897 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
898 {
899         struct wpa_bss *bss;
900
901         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
902                 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
903
904         wpa_s->fetch_anqp_in_progress = 1;
905         interworking_next_anqp_fetch(wpa_s);
906 }
907
908
909 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
910 {
911         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
912                 return 0;
913
914         wpa_s->network_select = 0;
915
916         interworking_start_fetch_anqp(wpa_s);
917
918         return 0;
919 }
920
921
922 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
923 {
924         if (!wpa_s->fetch_anqp_in_progress)
925                 return;
926
927         wpa_s->fetch_anqp_in_progress = 0;
928 }
929
930
931 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
932                   u16 info_ids[], size_t num_ids)
933 {
934         struct wpabuf *buf;
935         int ret = 0;
936         int freq;
937         struct wpa_bss *bss;
938         int res;
939
940         freq = wpa_s->assoc_freq;
941         bss = wpa_bss_get_bssid(wpa_s, dst);
942         if (bss)
943                 freq = bss->freq;
944         if (freq <= 0)
945                 return -1;
946
947         wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
948                    MAC2STR(dst), (unsigned int) num_ids);
949
950         buf = anqp_build_req(info_ids, num_ids, NULL);
951         if (buf == NULL)
952                 return -1;
953
954         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
955         if (res < 0) {
956                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
957                 ret = -1;
958         } else
959                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
960                            "%u", res);
961
962         wpabuf_free(buf);
963         return ret;
964 }
965
966
967 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
968                                             const u8 *sa, u16 info_id,
969                                             const u8 *data, size_t slen)
970 {
971         const u8 *pos = data;
972         struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
973
974         switch (info_id) {
975         case ANQP_CAPABILITY_LIST:
976                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
977                         " ANQP Capability list", MAC2STR(sa));
978                 break;
979         case ANQP_VENUE_NAME:
980                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
981                         " Venue Name", MAC2STR(sa));
982                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
983                 if (bss) {
984                         wpabuf_free(bss->anqp_venue_name);
985                         bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
986                 }
987                 break;
988         case ANQP_NETWORK_AUTH_TYPE:
989                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
990                         " Network Authentication Type information",
991                         MAC2STR(sa));
992                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
993                                   "Type", pos, slen);
994                 if (bss) {
995                         wpabuf_free(bss->anqp_network_auth_type);
996                         bss->anqp_network_auth_type =
997                                 wpabuf_alloc_copy(pos, slen);
998                 }
999                 break;
1000         case ANQP_ROAMING_CONSORTIUM:
1001                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1002                         " Roaming Consortium list", MAC2STR(sa));
1003                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1004                                   pos, slen);
1005                 if (bss) {
1006                         wpabuf_free(bss->anqp_roaming_consortium);
1007                         bss->anqp_roaming_consortium =
1008                                 wpabuf_alloc_copy(pos, slen);
1009                 }
1010                 break;
1011         case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1012                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1013                         " IP Address Type Availability information",
1014                         MAC2STR(sa));
1015                 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1016                             pos, slen);
1017                 if (bss) {
1018                         wpabuf_free(bss->anqp_ip_addr_type_availability);
1019                         bss->anqp_ip_addr_type_availability =
1020                                 wpabuf_alloc_copy(pos, slen);
1021                 }
1022                 break;
1023         case ANQP_NAI_REALM:
1024                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1025                         " NAI Realm list", MAC2STR(sa));
1026                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1027                 if (bss) {
1028                         wpabuf_free(bss->anqp_nai_realm);
1029                         bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1030                 }
1031                 break;
1032         case ANQP_3GPP_CELLULAR_NETWORK:
1033                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1034                         " 3GPP Cellular Network information", MAC2STR(sa));
1035                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1036                                   pos, slen);
1037                 if (bss) {
1038                         wpabuf_free(bss->anqp_3gpp);
1039                         bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1040                 }
1041                 break;
1042         case ANQP_DOMAIN_NAME:
1043                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1044                         " Domain Name list", MAC2STR(sa));
1045                 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1046                 if (bss) {
1047                         wpabuf_free(bss->anqp_domain_name);
1048                         bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1049                 }
1050                 break;
1051         case ANQP_VENDOR_SPECIFIC:
1052                 if (slen < 3)
1053                         return;
1054
1055                 switch (WPA_GET_BE24(pos)) {
1056                 default:
1057                         wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1058                                    "vendor-specific ANQP OUI %06x",
1059                                    WPA_GET_BE24(pos));
1060                         return;
1061                 }
1062                 break;
1063         default:
1064                 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1065                            "%u", info_id);
1066                 break;
1067         }
1068 }
1069
1070
1071 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1072                   enum gas_query_result result,
1073                   const struct wpabuf *adv_proto,
1074                   const struct wpabuf *resp, u16 status_code)
1075 {
1076         struct wpa_supplicant *wpa_s = ctx;
1077         const u8 *pos;
1078         const u8 *end;
1079         u16 info_id;
1080         u16 slen;
1081
1082         if (result != GAS_QUERY_SUCCESS)
1083                 return;
1084
1085         pos = wpabuf_head(adv_proto);
1086         if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1087             pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1088                 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1089                            "Protocol in response");
1090                 return;
1091         }
1092
1093         pos = wpabuf_head(resp);
1094         end = pos + wpabuf_len(resp);
1095
1096         while (pos < end) {
1097                 if (pos + 4 > end) {
1098                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1099                         break;
1100                 }
1101                 info_id = WPA_GET_LE16(pos);
1102                 pos += 2;
1103                 slen = WPA_GET_LE16(pos);
1104                 pos += 2;
1105                 if (pos + slen > end) {
1106                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1107                                    "for Info ID %u", info_id);
1108                         break;
1109                 }
1110                 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1111                                                 slen);
1112                 pos += slen;
1113         }
1114 }
1115
1116
1117 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1118                                           struct wpa_scan_results *scan_res)
1119 {
1120         wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1121                    "ANQP fetch");
1122         interworking_start_fetch_anqp(wpa_s);
1123 }
1124
1125
1126 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1127 {
1128         interworking_stop_fetch_anqp(wpa_s);
1129         wpa_s->network_select = 1;
1130         wpa_s->auto_select = !!auto_select;
1131         wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1132                    "selection");
1133         wpa_s->scan_res_handler = interworking_scan_res_handler;
1134         wpa_s->scan_req = 2;
1135         wpa_supplicant_req_scan(wpa_s, 0, 0);
1136
1137         return 0;
1138 }