Interworking: Support for using EAP-SIM credentials in network selection
[mech_eap.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 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         wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
575                    MAC2STR(bss->bssid));
576
577         ssid = wpa_config_add_network(wpa_s->conf);
578         if (ssid == NULL)
579                 return -1;
580
581         wpas_notify_network_added(wpa_s, ssid);
582         wpa_config_set_network_defaults(ssid);
583         ssid->temporary = 1;
584         ssid->ssid = os_zalloc(ie[1] + 1);
585         if (ssid->ssid == NULL)
586                 goto fail;
587         os_memcpy(ssid->ssid, ie + 2, ie[1]);
588         ssid->ssid_len = ie[1];
589
590         /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
591         if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
592                 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
593                 goto fail;
594         }
595         if (set_root_nai(ssid, wpa_s->conf->home_imsi, '1') < 0) {
596                 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
597                 goto fail;
598         }
599
600         if (wpa_s->conf->home_milenage && wpa_s->conf->home_milenage[0]) {
601                 if (wpa_config_set_quoted(ssid, "password",
602                                           wpa_s->conf->home_milenage) < 0)
603                         goto fail;
604         } else {
605                 /* TODO: PIN */
606                 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
607                         goto fail;
608         }
609
610         if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
611             wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
612             < 0)
613                 goto fail;
614
615         wpa_supplicant_select_network(wpa_s, ssid);
616
617         return 0;
618
619 fail:
620         wpas_notify_network_removed(wpa_s, ssid);
621         wpa_config_remove_network(wpa_s->conf, ssid->id);
622 #endif /* INTERWORKING_3GPP */
623         return -1;
624 }
625
626
627 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
628 {
629         struct wpa_ssid *ssid;
630         struct nai_realm *realm;
631         struct nai_realm_eap *eap = NULL;
632         u16 count, i;
633         char buf[100];
634         const u8 *ie;
635
636         if (bss == NULL)
637                 return -1;
638         ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
639         if (ie == NULL || ie[1] == 0) {
640                 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
641                            MACSTR, MAC2STR(bss->bssid));
642                 return -1;
643         }
644
645         realm = nai_realm_parse(bss->anqp_nai_realm, &count);
646         if (realm == NULL) {
647                 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
648                            "Realm list from " MACSTR, MAC2STR(bss->bssid));
649                 count = 0;
650         }
651
652         for (i = 0; i < count; i++) {
653                 if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
654                         continue;
655                 eap = nai_realm_find_eap(wpa_s, &realm[i]);
656                 if (eap)
657                         break;
658         }
659
660         if (!eap) {
661                 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
662                         if (realm)
663                                 nai_realm_free(realm, count);
664                         return 0;
665                 }
666
667                 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
668                            "and EAP method found for " MACSTR,
669                            MAC2STR(bss->bssid));
670                 nai_realm_free(realm, count);
671                 return -1;
672         }
673
674         wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
675                    MAC2STR(bss->bssid));
676
677         ssid = wpa_config_add_network(wpa_s->conf);
678         if (ssid == NULL) {
679                 nai_realm_free(realm, count);
680                 return -1;
681         }
682         wpas_notify_network_added(wpa_s, ssid);
683         wpa_config_set_network_defaults(ssid);
684         ssid->temporary = 1;
685         ssid->ssid = os_zalloc(ie[1] + 1);
686         if (ssid->ssid == NULL)
687                 goto fail;
688         os_memcpy(ssid->ssid, ie + 2, ie[1]);
689         ssid->ssid_len = ie[1];
690
691         if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
692                                                      eap->method), 0) < 0)
693                 goto fail;
694
695         if (wpa_s->conf->home_username && wpa_s->conf->home_username[0] &&
696             wpa_config_set_quoted(ssid, "identity",
697                                   wpa_s->conf->home_username) < 0)
698                 goto fail;
699
700         if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
701             wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
702             < 0)
703                 goto fail;
704
705         switch (eap->method) {
706         case EAP_TYPE_TTLS:
707                 if (eap->inner_method) {
708                         os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
709                                     eap_get_name(EAP_VENDOR_IETF,
710                                                  eap->inner_method));
711                         if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
712                                 goto fail;
713                         break;
714                 }
715                 switch (eap->inner_non_eap) {
716                 case NAI_REALM_INNER_NON_EAP_PAP:
717                         if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
718                             0)
719                                 goto fail;
720                         break;
721                 case NAI_REALM_INNER_NON_EAP_CHAP:
722                         if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
723                             < 0)
724                                 goto fail;
725                         break;
726                 case NAI_REALM_INNER_NON_EAP_MSCHAP:
727                         if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
728                             < 0)
729                                 goto fail;
730                         break;
731                 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
732                         if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
733                                            0) < 0)
734                                 goto fail;
735                         break;
736                 }
737                 break;
738         case EAP_TYPE_PEAP:
739                 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
740                             eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
741                 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
742                         goto fail;
743                 break;
744         }
745
746         if (wpa_s->conf->home_ca_cert && wpa_s->conf->home_ca_cert[0] &&
747             wpa_config_set_quoted(ssid, "ca_cert", wpa_s->conf->home_ca_cert) <
748             0)
749                 goto fail;
750
751         nai_realm_free(realm, count);
752
753         wpa_supplicant_select_network(wpa_s, ssid);
754
755         return 0;
756
757 fail:
758         wpas_notify_network_removed(wpa_s, ssid);
759         wpa_config_remove_network(wpa_s->conf, ssid->id);
760         nai_realm_free(realm, count);
761         return -1;
762 }
763
764
765 static int interworking_credentials_available_3gpp(
766         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
767 {
768         int ret = 0;
769
770 #ifdef INTERWORKING_3GPP
771         if (bss->anqp_3gpp == NULL)
772                 return ret;
773
774         if (wpa_s->conf->home_imsi == NULL || !wpa_s->conf->home_imsi[0] ||
775             wpa_s->conf->home_milenage == NULL ||
776             !wpa_s->conf->home_milenage[0])
777                 return ret;
778
779         wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from " MACSTR,
780                    MAC2STR(bss->bssid));
781         ret = plmn_id_match(bss->anqp_3gpp, wpa_s->conf->home_imsi);
782         wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
783 #endif /* INTERWORKING_3GPP */
784         return ret;
785 }
786
787
788 static int interworking_credentials_available_realm(
789         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
790 {
791         struct nai_realm *realm;
792         u16 count, i;
793         int found = 0;
794
795         if (bss->anqp_nai_realm == NULL)
796                 return 0;
797
798         if (wpa_s->conf->home_realm == NULL)
799                 return 0;
800
801         wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
802                    MACSTR, MAC2STR(bss->bssid));
803         realm = nai_realm_parse(bss->anqp_nai_realm, &count);
804         if (realm == NULL) {
805                 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
806                            "Realm list from " MACSTR, MAC2STR(bss->bssid));
807                 return 0;
808         }
809
810         for (i = 0; i < count; i++) {
811                 if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
812                         continue;
813                 if (nai_realm_find_eap(wpa_s, &realm[i])) {
814                         found++;
815                         break;
816                 }
817         }
818
819         nai_realm_free(realm, count);
820
821         return found;
822 }
823
824
825 static int interworking_credentials_available(struct wpa_supplicant *wpa_s,
826                                               struct wpa_bss *bss)
827 {
828         return interworking_credentials_available_realm(wpa_s, bss) ||
829                 interworking_credentials_available_3gpp(wpa_s, bss);
830 }
831
832
833 static void interworking_select_network(struct wpa_supplicant *wpa_s)
834 {
835         struct wpa_bss *bss, *selected = NULL;
836         unsigned int count = 0;
837
838         wpa_s->network_select = 0;
839
840         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
841                 if (!interworking_credentials_available(wpa_s, bss))
842                         continue;
843                 count++;
844                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR,
845                         MAC2STR(bss->bssid));
846                 if (selected == NULL && wpa_s->auto_select)
847                         selected = bss;
848         }
849
850         if (count == 0) {
851                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
852                         "with matching credentials found");
853         }
854
855         if (selected)
856                 interworking_connect(wpa_s, selected);
857 }
858
859
860 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
861 {
862         struct wpa_bss *bss;
863         int found = 0;
864         const u8 *ie;
865
866         if (!wpa_s->fetch_anqp_in_progress)
867                 return;
868
869         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
870                 if (!(bss->caps & IEEE80211_CAP_ESS))
871                         continue;
872                 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
873                 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
874                         continue; /* AP does not support Interworking */
875
876                 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
877                         found++;
878                         bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
879                         wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
880                                 MACSTR, MAC2STR(bss->bssid));
881                         interworking_anqp_send_req(wpa_s, bss);
882                         break;
883                 }
884         }
885
886         if (found == 0) {
887                 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
888                 wpa_s->fetch_anqp_in_progress = 0;
889                 if (wpa_s->network_select)
890                         interworking_select_network(wpa_s);
891         }
892 }
893
894
895 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
896 {
897         struct wpa_bss *bss;
898
899         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
900                 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
901
902         wpa_s->fetch_anqp_in_progress = 1;
903         interworking_next_anqp_fetch(wpa_s);
904 }
905
906
907 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
908 {
909         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
910                 return 0;
911
912         wpa_s->network_select = 0;
913
914         interworking_start_fetch_anqp(wpa_s);
915
916         return 0;
917 }
918
919
920 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
921 {
922         if (!wpa_s->fetch_anqp_in_progress)
923                 return;
924
925         wpa_s->fetch_anqp_in_progress = 0;
926 }
927
928
929 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
930                   u16 info_ids[], size_t num_ids)
931 {
932         struct wpabuf *buf;
933         int ret = 0;
934         int freq;
935         struct wpa_bss *bss;
936         int res;
937
938         freq = wpa_s->assoc_freq;
939         bss = wpa_bss_get_bssid(wpa_s, dst);
940         if (bss)
941                 freq = bss->freq;
942         if (freq <= 0)
943                 return -1;
944
945         wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
946                    MAC2STR(dst), (unsigned int) num_ids);
947
948         buf = anqp_build_req(info_ids, num_ids, NULL);
949         if (buf == NULL)
950                 return -1;
951
952         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
953         if (res < 0) {
954                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
955                 ret = -1;
956         } else
957                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
958                            "%u", res);
959
960         wpabuf_free(buf);
961         return ret;
962 }
963
964
965 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
966                                             const u8 *sa, u16 info_id,
967                                             const u8 *data, size_t slen)
968 {
969         const u8 *pos = data;
970         struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
971
972         switch (info_id) {
973         case ANQP_CAPABILITY_LIST:
974                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
975                         " ANQP Capability list", MAC2STR(sa));
976                 break;
977         case ANQP_VENUE_NAME:
978                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
979                         " Venue Name", MAC2STR(sa));
980                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
981                 if (bss) {
982                         wpabuf_free(bss->anqp_venue_name);
983                         bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
984                 }
985                 break;
986         case ANQP_NETWORK_AUTH_TYPE:
987                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
988                         " Network Authentication Type information",
989                         MAC2STR(sa));
990                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
991                                   "Type", pos, slen);
992                 if (bss) {
993                         wpabuf_free(bss->anqp_network_auth_type);
994                         bss->anqp_network_auth_type =
995                                 wpabuf_alloc_copy(pos, slen);
996                 }
997                 break;
998         case ANQP_ROAMING_CONSORTIUM:
999                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1000                         " Roaming Consortium list", MAC2STR(sa));
1001                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1002                                   pos, slen);
1003                 if (bss) {
1004                         wpabuf_free(bss->anqp_roaming_consortium);
1005                         bss->anqp_roaming_consortium =
1006                                 wpabuf_alloc_copy(pos, slen);
1007                 }
1008                 break;
1009         case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1010                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1011                         " IP Address Type Availability information",
1012                         MAC2STR(sa));
1013                 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1014                             pos, slen);
1015                 if (bss) {
1016                         wpabuf_free(bss->anqp_ip_addr_type_availability);
1017                         bss->anqp_ip_addr_type_availability =
1018                                 wpabuf_alloc_copy(pos, slen);
1019                 }
1020                 break;
1021         case ANQP_NAI_REALM:
1022                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1023                         " NAI Realm list", MAC2STR(sa));
1024                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1025                 if (bss) {
1026                         wpabuf_free(bss->anqp_nai_realm);
1027                         bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1028                 }
1029                 break;
1030         case ANQP_3GPP_CELLULAR_NETWORK:
1031                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1032                         " 3GPP Cellular Network information", MAC2STR(sa));
1033                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1034                                   pos, slen);
1035                 if (bss) {
1036                         wpabuf_free(bss->anqp_3gpp);
1037                         bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1038                 }
1039                 break;
1040         case ANQP_DOMAIN_NAME:
1041                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1042                         " Domain Name list", MAC2STR(sa));
1043                 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1044                 if (bss) {
1045                         wpabuf_free(bss->anqp_domain_name);
1046                         bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1047                 }
1048                 break;
1049         case ANQP_VENDOR_SPECIFIC:
1050                 if (slen < 3)
1051                         return;
1052
1053                 switch (WPA_GET_BE24(pos)) {
1054                 default:
1055                         wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1056                                    "vendor-specific ANQP OUI %06x",
1057                                    WPA_GET_BE24(pos));
1058                         return;
1059                 }
1060                 break;
1061         default:
1062                 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1063                            "%u", info_id);
1064                 break;
1065         }
1066 }
1067
1068
1069 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1070                   enum gas_query_result result,
1071                   const struct wpabuf *adv_proto,
1072                   const struct wpabuf *resp, u16 status_code)
1073 {
1074         struct wpa_supplicant *wpa_s = ctx;
1075         const u8 *pos;
1076         const u8 *end;
1077         u16 info_id;
1078         u16 slen;
1079
1080         if (result != GAS_QUERY_SUCCESS)
1081                 return;
1082
1083         pos = wpabuf_head(adv_proto);
1084         if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1085             pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1086                 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1087                            "Protocol in response");
1088                 return;
1089         }
1090
1091         pos = wpabuf_head(resp);
1092         end = pos + wpabuf_len(resp);
1093
1094         while (pos < end) {
1095                 if (pos + 4 > end) {
1096                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1097                         break;
1098                 }
1099                 info_id = WPA_GET_LE16(pos);
1100                 pos += 2;
1101                 slen = WPA_GET_LE16(pos);
1102                 pos += 2;
1103                 if (pos + slen > end) {
1104                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1105                                    "for Info ID %u", info_id);
1106                         break;
1107                 }
1108                 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1109                                                 slen);
1110                 pos += slen;
1111         }
1112 }
1113
1114
1115 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1116                                           struct wpa_scan_results *scan_res)
1117 {
1118         wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1119                    "ANQP fetch");
1120         interworking_start_fetch_anqp(wpa_s);
1121 }
1122
1123
1124 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1125 {
1126         interworking_stop_fetch_anqp(wpa_s);
1127         wpa_s->network_select = 1;
1128         wpa_s->auto_select = !!auto_select;
1129         wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1130                    "selection");
1131         wpa_s->scan_res_handler = interworking_scan_res_handler;
1132         wpa_s->scan_req = 2;
1133         wpa_supplicant_req_scan(wpa_s, 0, 0);
1134
1135         return 0;
1136 }