Check for driver initialization before doing driver operations
[mech_eap.git] / src / ap / ctrl_iface_ap.c
1 /*
2  * Control interface for shared AP commands
3  * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "fst/fst_ctrl_iface.h"
16 #include "hostapd.h"
17 #include "ieee802_1x.h"
18 #include "wpa_auth.h"
19 #include "ieee802_11.h"
20 #include "sta_info.h"
21 #include "wps_hostapd.h"
22 #include "p2p_hostapd.h"
23 #include "ctrl_iface_ap.h"
24 #include "ap_drv_ops.h"
25 #include "mbo_ap.h"
26
27
28 static int hostapd_get_sta_tx_rx(struct hostapd_data *hapd,
29                                  struct sta_info *sta,
30                                  char *buf, size_t buflen)
31 {
32         struct hostap_sta_driver_data data;
33         int ret;
34
35         if (hostapd_drv_read_sta_data(hapd, &data, sta->addr) < 0)
36                 return 0;
37
38         ret = os_snprintf(buf, buflen, "rx_packets=%lu\ntx_packets=%lu\n"
39                           "rx_bytes=%llu\ntx_bytes=%llu\ninactive_msec=%lu\n",
40                           data.rx_packets, data.tx_packets,
41                           data.rx_bytes, data.tx_bytes, data.inactive_msec);
42         if (os_snprintf_error(buflen, ret))
43                 return 0;
44         return ret;
45 }
46
47
48 static int hostapd_get_sta_conn_time(struct sta_info *sta,
49                                      char *buf, size_t buflen)
50 {
51         struct os_reltime age;
52         int ret;
53
54         if (!sta->connected_time.sec)
55                 return 0;
56
57         os_reltime_age(&sta->connected_time, &age);
58
59         ret = os_snprintf(buf, buflen, "connected_time=%u\n",
60                           (unsigned int) age.sec);
61         if (os_snprintf_error(buflen, ret))
62                 return 0;
63         return ret;
64 }
65
66
67 static const char * timeout_next_str(int val)
68 {
69         switch (val) {
70         case STA_NULLFUNC:
71                 return "NULLFUNC POLL";
72         case STA_DISASSOC:
73                 return "DISASSOC";
74         case STA_DEAUTH:
75                 return "DEAUTH";
76         case STA_REMOVE:
77                 return "REMOVE";
78         case STA_DISASSOC_FROM_CLI:
79                 return "DISASSOC_FROM_CLI";
80         }
81
82         return "?";
83 }
84
85
86 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
87                                       struct sta_info *sta,
88                                       char *buf, size_t buflen)
89 {
90         int len, res, ret, i;
91
92         if (!sta)
93                 return 0;
94
95         len = 0;
96         ret = os_snprintf(buf + len, buflen - len, MACSTR "\nflags=",
97                           MAC2STR(sta->addr));
98         if (os_snprintf_error(buflen - len, ret))
99                 return len;
100         len += ret;
101
102         ret = ap_sta_flags_txt(sta->flags, buf + len, buflen - len);
103         if (ret < 0)
104                 return len;
105         len += ret;
106
107         ret = os_snprintf(buf + len, buflen - len, "\naid=%d\ncapability=0x%x\n"
108                           "listen_interval=%d\nsupported_rates=",
109                           sta->aid, sta->capability, sta->listen_interval);
110         if (os_snprintf_error(buflen - len, ret))
111                 return len;
112         len += ret;
113
114         for (i = 0; i < sta->supported_rates_len; i++) {
115                 ret = os_snprintf(buf + len, buflen - len, "%02x%s",
116                                   sta->supported_rates[i],
117                                   i + 1 < sta->supported_rates_len ? " " : "");
118                 if (os_snprintf_error(buflen - len, ret))
119                         return len;
120                 len += ret;
121         }
122
123         ret = os_snprintf(buf + len, buflen - len, "\ntimeout_next=%s\n",
124                           timeout_next_str(sta->timeout_next));
125         if (os_snprintf_error(buflen - len, ret))
126                 return len;
127         len += ret;
128
129         res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
130         if (res >= 0)
131                 len += res;
132         res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
133         if (res >= 0)
134                 len += res;
135         res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
136         if (res >= 0)
137                 len += res;
138         res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
139                                       buflen - len);
140         if (res >= 0)
141                 len += res;
142         res = hostapd_p2p_get_mib_sta(hapd, sta, buf + len, buflen - len);
143         if (res >= 0)
144                 len += res;
145
146         len += hostapd_get_sta_tx_rx(hapd, sta, buf + len, buflen - len);
147         len += hostapd_get_sta_conn_time(sta, buf + len, buflen - len);
148
149 #ifdef CONFIG_SAE
150         if (sta->sae && sta->sae->state == SAE_ACCEPTED) {
151                 res = os_snprintf(buf + len, buflen - len, "sae_group=%d\n",
152                                   sta->sae->group);
153                 if (!os_snprintf_error(buflen - len, res))
154                         len += res;
155         }
156 #endif /* CONFIG_SAE */
157
158         if (sta->vlan_id > 0) {
159                 res = os_snprintf(buf + len, buflen - len, "vlan_id=%d\n",
160                                   sta->vlan_id);
161                 if (!os_snprintf_error(buflen - len, res))
162                         len += res;
163         }
164
165         res = mbo_ap_get_info(sta, buf + len, buflen - len);
166         if (res >= 0)
167                 len += res;
168
169         if (sta->supp_op_classes &&
170             buflen - len > (unsigned) (17 + 2 * sta->supp_op_classes[0])) {
171                 len += os_snprintf(buf + len, buflen - len, "supp_op_classes=");
172                 len += wpa_snprintf_hex(buf + len, buflen - len,
173                                         sta->supp_op_classes + 1,
174                                         sta->supp_op_classes[0]);
175                 len += os_snprintf(buf + len, buflen - len, "\n");
176         }
177
178         return len;
179 }
180
181
182 int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
183                                  char *buf, size_t buflen)
184 {
185         return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
186 }
187
188
189 int hostapd_ctrl_iface_sta(struct hostapd_data *hapd, const char *txtaddr,
190                            char *buf, size_t buflen)
191 {
192         u8 addr[ETH_ALEN];
193         int ret;
194         const char *pos;
195         struct sta_info *sta;
196
197         if (hwaddr_aton(txtaddr, addr)) {
198                 ret = os_snprintf(buf, buflen, "FAIL\n");
199                 if (os_snprintf_error(buflen, ret))
200                         return 0;
201                 return ret;
202         }
203
204         sta = ap_get_sta(hapd, addr);
205         if (sta == NULL)
206                 return -1;
207
208         pos = os_strchr(txtaddr, ' ');
209         if (pos) {
210                 pos++;
211
212 #ifdef HOSTAPD_DUMP_STATE
213                 if (os_strcmp(pos, "eapol") == 0) {
214                         if (sta->eapol_sm == NULL)
215                                 return -1;
216                         return eapol_auth_dump_state(sta->eapol_sm, buf,
217                                                      buflen);
218                 }
219 #endif /* HOSTAPD_DUMP_STATE */
220
221                 return -1;
222         }
223
224         ret = hostapd_ctrl_iface_sta_mib(hapd, sta, buf, buflen);
225         ret += fst_ctrl_iface_mb_info(addr, buf + ret, buflen - ret);
226
227         return ret;
228 }
229
230
231 int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd, const char *txtaddr,
232                                 char *buf, size_t buflen)
233 {
234         u8 addr[ETH_ALEN];
235         struct sta_info *sta;
236         int ret;
237
238         if (hwaddr_aton(txtaddr, addr) ||
239             (sta = ap_get_sta(hapd, addr)) == NULL) {
240                 ret = os_snprintf(buf, buflen, "FAIL\n");
241                 if (os_snprintf_error(buflen, ret))
242                         return 0;
243                 return ret;
244         }
245
246         if (!sta->next)
247                 return 0;
248
249         return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
250 }
251
252
253 #ifdef CONFIG_P2P_MANAGER
254 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
255                                   u8 minor_reason_code, const u8 *addr)
256 {
257         struct ieee80211_mgmt *mgmt;
258         int ret;
259         u8 *pos;
260
261         if (!hapd->drv_priv || !hapd->driver->send_frame)
262                 return -1;
263
264         mgmt = os_zalloc(sizeof(*mgmt) + 100);
265         if (mgmt == NULL)
266                 return -1;
267
268         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
269         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
270                 " with minor reason code %u (stype=%u (%s))",
271                 MAC2STR(addr), minor_reason_code, stype,
272                 fc2str(le_to_host16(mgmt->frame_control)));
273
274         os_memcpy(mgmt->da, addr, ETH_ALEN);
275         os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
276         os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
277         if (stype == WLAN_FC_STYPE_DEAUTH) {
278                 mgmt->u.deauth.reason_code =
279                         host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
280                 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
281         } else {
282                 mgmt->u.disassoc.reason_code =
283                         host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
284                 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
285         }
286
287         *pos++ = WLAN_EID_VENDOR_SPECIFIC;
288         *pos++ = 4 + 3 + 1;
289         WPA_PUT_BE32(pos, P2P_IE_VENDOR_TYPE);
290         pos += 4;
291
292         *pos++ = P2P_ATTR_MINOR_REASON_CODE;
293         WPA_PUT_LE16(pos, 1);
294         pos += 2;
295         *pos++ = minor_reason_code;
296
297         ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
298                                        pos - (u8 *) mgmt, 1);
299         os_free(mgmt);
300
301         return ret < 0 ? -1 : 0;
302 }
303 #endif /* CONFIG_P2P_MANAGER */
304
305
306 int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
307                                       const char *txtaddr)
308 {
309         u8 addr[ETH_ALEN];
310         struct sta_info *sta;
311         const char *pos;
312         u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
313
314         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
315                 txtaddr);
316
317         if (hwaddr_aton(txtaddr, addr))
318                 return -1;
319
320         pos = os_strstr(txtaddr, " reason=");
321         if (pos)
322                 reason = atoi(pos + 8);
323
324         pos = os_strstr(txtaddr, " test=");
325         if (pos) {
326                 struct ieee80211_mgmt mgmt;
327                 int encrypt;
328                 if (!hapd->drv_priv || !hapd->driver->send_frame)
329                         return -1;
330                 pos += 6;
331                 encrypt = atoi(pos);
332                 os_memset(&mgmt, 0, sizeof(mgmt));
333                 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
334                                                   WLAN_FC_STYPE_DEAUTH);
335                 os_memcpy(mgmt.da, addr, ETH_ALEN);
336                 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
337                 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
338                 mgmt.u.deauth.reason_code = host_to_le16(reason);
339                 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
340                                              IEEE80211_HDRLEN +
341                                              sizeof(mgmt.u.deauth),
342                                              encrypt) < 0)
343                         return -1;
344                 return 0;
345         }
346
347 #ifdef CONFIG_P2P_MANAGER
348         pos = os_strstr(txtaddr, " p2p=");
349         if (pos) {
350                 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
351                                               atoi(pos + 5), addr);
352         }
353 #endif /* CONFIG_P2P_MANAGER */
354
355         if (os_strstr(txtaddr, " tx=0"))
356                 hostapd_drv_sta_remove(hapd, addr);
357         else
358                 hostapd_drv_sta_deauth(hapd, addr, reason);
359         sta = ap_get_sta(hapd, addr);
360         if (sta)
361                 ap_sta_deauthenticate(hapd, sta, reason);
362         else if (addr[0] == 0xff)
363                 hostapd_free_stas(hapd);
364
365         return 0;
366 }
367
368
369 int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
370                                     const char *txtaddr)
371 {
372         u8 addr[ETH_ALEN];
373         struct sta_info *sta;
374         const char *pos;
375         u16 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
376
377         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
378                 txtaddr);
379
380         if (hwaddr_aton(txtaddr, addr))
381                 return -1;
382
383         pos = os_strstr(txtaddr, " reason=");
384         if (pos)
385                 reason = atoi(pos + 8);
386
387         pos = os_strstr(txtaddr, " test=");
388         if (pos) {
389                 struct ieee80211_mgmt mgmt;
390                 int encrypt;
391                 if (!hapd->drv_priv || !hapd->driver->send_frame)
392                         return -1;
393                 pos += 6;
394                 encrypt = atoi(pos);
395                 os_memset(&mgmt, 0, sizeof(mgmt));
396                 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
397                                                   WLAN_FC_STYPE_DISASSOC);
398                 os_memcpy(mgmt.da, addr, ETH_ALEN);
399                 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
400                 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
401                 mgmt.u.disassoc.reason_code = host_to_le16(reason);
402                 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
403                                              IEEE80211_HDRLEN +
404                                              sizeof(mgmt.u.deauth),
405                                              encrypt) < 0)
406                         return -1;
407                 return 0;
408         }
409
410 #ifdef CONFIG_P2P_MANAGER
411         pos = os_strstr(txtaddr, " p2p=");
412         if (pos) {
413                 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
414                                               atoi(pos + 5), addr);
415         }
416 #endif /* CONFIG_P2P_MANAGER */
417
418         if (os_strstr(txtaddr, " tx=0"))
419                 hostapd_drv_sta_remove(hapd, addr);
420         else
421                 hostapd_drv_sta_disassoc(hapd, addr, reason);
422         sta = ap_get_sta(hapd, addr);
423         if (sta)
424                 ap_sta_disassociate(hapd, sta, reason);
425         else if (addr[0] == 0xff)
426                 hostapd_free_stas(hapd);
427
428         return 0;
429 }
430
431
432 int hostapd_ctrl_iface_poll_sta(struct hostapd_data *hapd,
433                                 const char *txtaddr)
434 {
435         u8 addr[ETH_ALEN];
436         struct sta_info *sta;
437
438         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE POLL_STA %s", txtaddr);
439
440         if (hwaddr_aton(txtaddr, addr))
441                 return -1;
442
443         sta = ap_get_sta(hapd, addr);
444         if (!sta)
445                 return -1;
446
447         hostapd_drv_poll_client(hapd, hapd->own_addr, addr,
448                                 sta->flags & WLAN_STA_WMM);
449         return 0;
450 }
451
452
453 int hostapd_ctrl_iface_status(struct hostapd_data *hapd, char *buf,
454                               size_t buflen)
455 {
456         struct hostapd_iface *iface = hapd->iface;
457         int len = 0, ret;
458         size_t i;
459
460         ret = os_snprintf(buf + len, buflen - len,
461                           "state=%s\n"
462                           "phy=%s\n"
463                           "freq=%d\n"
464                           "num_sta_non_erp=%d\n"
465                           "num_sta_no_short_slot_time=%d\n"
466                           "num_sta_no_short_preamble=%d\n"
467                           "olbc=%d\n"
468                           "num_sta_ht_no_gf=%d\n"
469                           "num_sta_no_ht=%d\n"
470                           "num_sta_ht_20_mhz=%d\n"
471                           "num_sta_ht40_intolerant=%d\n"
472                           "olbc_ht=%d\n"
473                           "ht_op_mode=0x%x\n",
474                           hostapd_state_text(iface->state),
475                           iface->phy,
476                           iface->freq,
477                           iface->num_sta_non_erp,
478                           iface->num_sta_no_short_slot_time,
479                           iface->num_sta_no_short_preamble,
480                           iface->olbc,
481                           iface->num_sta_ht_no_gf,
482                           iface->num_sta_no_ht,
483                           iface->num_sta_ht_20mhz,
484                           iface->num_sta_ht40_intolerant,
485                           iface->olbc_ht,
486                           iface->ht_op_mode);
487         if (os_snprintf_error(buflen - len, ret))
488                 return len;
489         len += ret;
490
491         if (!iface->cac_started || !iface->dfs_cac_ms) {
492                 ret = os_snprintf(buf + len, buflen - len,
493                                   "cac_time_seconds=%d\n"
494                                   "cac_time_left_seconds=N/A\n",
495                                   iface->dfs_cac_ms / 1000);
496         } else {
497                 /* CAC started and CAC time set - calculate remaining time */
498                 struct os_reltime now;
499                 unsigned int left_time;
500
501                 os_reltime_age(&iface->dfs_cac_start, &now);
502                 left_time = iface->dfs_cac_ms / 1000 - now.sec;
503                 ret = os_snprintf(buf + len, buflen - len,
504                                   "cac_time_seconds=%u\n"
505                                   "cac_time_left_seconds=%u\n",
506                                   iface->dfs_cac_ms / 1000,
507                                   left_time);
508         }
509         if (os_snprintf_error(buflen - len, ret))
510                 return len;
511         len += ret;
512
513         ret = os_snprintf(buf + len, buflen - len,
514                           "channel=%u\n"
515                           "secondary_channel=%d\n"
516                           "ieee80211n=%d\n"
517                           "ieee80211ac=%d\n",
518                           iface->conf->channel,
519                           iface->conf->ieee80211n && !hapd->conf->disable_11n ?
520                           iface->conf->secondary_channel : 0,
521                           iface->conf->ieee80211n && !hapd->conf->disable_11n,
522                           iface->conf->ieee80211ac &&
523                           !hapd->conf->disable_11ac);
524         if (os_snprintf_error(buflen - len, ret))
525                 return len;
526         len += ret;
527         if (iface->conf->ieee80211ac && !hapd->conf->disable_11ac) {
528                 ret = os_snprintf(buf + len, buflen - len,
529                                   "vht_oper_chwidth=%d\n"
530                                   "vht_oper_centr_freq_seg0_idx=%d\n"
531                                   "vht_oper_centr_freq_seg1_idx=%d\n",
532                                   iface->conf->vht_oper_chwidth,
533                                   iface->conf->vht_oper_centr_freq_seg0_idx,
534                                   iface->conf->vht_oper_centr_freq_seg1_idx);
535                 if (os_snprintf_error(buflen - len, ret))
536                         return len;
537                 len += ret;
538         }
539
540         for (i = 0; i < iface->num_bss; i++) {
541                 struct hostapd_data *bss = iface->bss[i];
542                 ret = os_snprintf(buf + len, buflen - len,
543                                   "bss[%d]=%s\n"
544                                   "bssid[%d]=" MACSTR "\n"
545                                   "ssid[%d]=%s\n"
546                                   "num_sta[%d]=%d\n",
547                                   (int) i, bss->conf->iface,
548                                   (int) i, MAC2STR(bss->own_addr),
549                                   (int) i,
550                                   wpa_ssid_txt(bss->conf->ssid.ssid,
551                                                bss->conf->ssid.ssid_len),
552                                   (int) i, bss->num_sta);
553                 if (os_snprintf_error(buflen - len, ret))
554                         return len;
555                 len += ret;
556         }
557
558         return len;
559 }
560
561
562 int hostapd_parse_csa_settings(const char *pos,
563                                struct csa_settings *settings)
564 {
565         char *end;
566
567         os_memset(settings, 0, sizeof(*settings));
568         settings->cs_count = strtol(pos, &end, 10);
569         if (pos == end) {
570                 wpa_printf(MSG_ERROR, "chanswitch: invalid cs_count provided");
571                 return -1;
572         }
573
574         settings->freq_params.freq = atoi(end);
575         if (settings->freq_params.freq == 0) {
576                 wpa_printf(MSG_ERROR, "chanswitch: invalid freq provided");
577                 return -1;
578         }
579
580 #define SET_CSA_SETTING(str) \
581         do { \
582                 const char *pos2 = os_strstr(pos, " " #str "="); \
583                 if (pos2) { \
584                         pos2 += sizeof(" " #str "=") - 1; \
585                         settings->freq_params.str = atoi(pos2); \
586                 } \
587         } while (0)
588
589         SET_CSA_SETTING(center_freq1);
590         SET_CSA_SETTING(center_freq2);
591         SET_CSA_SETTING(bandwidth);
592         SET_CSA_SETTING(sec_channel_offset);
593         settings->freq_params.ht_enabled = !!os_strstr(pos, " ht");
594         settings->freq_params.vht_enabled = !!os_strstr(pos, " vht");
595         settings->block_tx = !!os_strstr(pos, " blocktx");
596 #undef SET_CSA_SETTING
597
598         return 0;
599 }
600
601
602 int hostapd_ctrl_iface_stop_ap(struct hostapd_data *hapd)
603 {
604         return hostapd_drv_stop_ap(hapd);
605 }
606
607
608 int hostapd_ctrl_iface_pmksa_list(struct hostapd_data *hapd, char *buf,
609                                   size_t len)
610 {
611         return wpa_auth_pmksa_list(hapd->wpa_auth, buf, len);
612 }
613
614
615 void hostapd_ctrl_iface_pmksa_flush(struct hostapd_data *hapd)
616 {
617         wpa_auth_pmksa_flush(hapd->wpa_auth);
618 }