WPS: Parse Request Type from WPS IE in (Re)AssocReq and derive mgmt keys
[wpasupplicant] / hostapd / sta_info.c
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007-2008, Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #include "hostapd.h"
19 #include "sta_info.h"
20 #include "eloop.h"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "radius/radius.h"
25 #include "wpa.h"
26 #include "preauth.h"
27 #include "radius/radius_client.h"
28 #include "driver.h"
29 #include "beacon.h"
30 #include "hw_features.h"
31 #include "mlme.h"
32 #include "vlan_init.h"
33
34 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
35                                struct sta_info *sta, u32 flags);
36 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
37 #ifdef CONFIG_IEEE80211W
38 static void ap_ping_timer(void *eloop_ctx, void *timeout_ctx);
39 #endif /* CONFIG_IEEE80211W */
40
41 int ap_for_each_sta(struct hostapd_data *hapd,
42                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
43                               void *ctx),
44                     void *ctx)
45 {
46         struct sta_info *sta;
47
48         for (sta = hapd->sta_list; sta; sta = sta->next) {
49                 if (cb(hapd, sta, ctx))
50                         return 1;
51         }
52
53         return 0;
54 }
55
56
57 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
58 {
59         struct sta_info *s;
60
61         s = hapd->sta_hash[STA_HASH(sta)];
62         while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
63                 s = s->hnext;
64         return s;
65 }
66
67
68 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
69 {
70         struct sta_info *tmp;
71
72         if (hapd->sta_list == sta) {
73                 hapd->sta_list = sta->next;
74                 return;
75         }
76
77         tmp = hapd->sta_list;
78         while (tmp != NULL && tmp->next != sta)
79                 tmp = tmp->next;
80         if (tmp == NULL) {
81                 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
82                            "list.", MAC2STR(sta->addr));
83         } else
84                 tmp->next = sta->next;
85 }
86
87
88 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
89 {
90         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
91         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
92 }
93
94
95 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
96 {
97         struct sta_info *s;
98
99         s = hapd->sta_hash[STA_HASH(sta->addr)];
100         if (s == NULL) return;
101         if (os_memcmp(s->addr, sta->addr, 6) == 0) {
102                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
103                 return;
104         }
105
106         while (s->hnext != NULL &&
107                os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
108                 s = s->hnext;
109         if (s->hnext != NULL)
110                 s->hnext = s->hnext->hnext;
111         else
112                 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
113                            " from hash table", MAC2STR(sta->addr));
114 }
115
116
117 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
118 {
119         int set_beacon = 0;
120
121         accounting_sta_stop(hapd, sta);
122
123         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
124             !(sta->flags & WLAN_STA_PREAUTH))
125                 hostapd_sta_remove(hapd, sta->addr);
126
127         ap_sta_hash_del(hapd, sta);
128         ap_sta_list_del(hapd, sta);
129
130         if (sta->aid > 0)
131                 hapd->sta_aid[sta->aid - 1] = NULL;
132
133         hapd->num_sta--;
134         if (sta->nonerp_set) {
135                 sta->nonerp_set = 0;
136                 hapd->iface->num_sta_non_erp--;
137                 if (hapd->iface->num_sta_non_erp == 0)
138                         set_beacon++;
139         }
140
141         if (sta->no_short_slot_time_set) {
142                 sta->no_short_slot_time_set = 0;
143                 hapd->iface->num_sta_no_short_slot_time--;
144                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
145                     && hapd->iface->num_sta_no_short_slot_time == 0)
146                         set_beacon++;
147         }
148
149         if (sta->no_short_preamble_set) {
150                 sta->no_short_preamble_set = 0;
151                 hapd->iface->num_sta_no_short_preamble--;
152                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
153                     && hapd->iface->num_sta_no_short_preamble == 0)
154                         set_beacon++;
155         }
156
157 #ifdef CONFIG_IEEE80211N
158         if (sta->flags & WLAN_STA_HT) {
159                 u16 ht_capab = le_to_host16(
160                         sta->ht_capabilities.data.capabilities_info);
161                 if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0)
162                         hapd->iface->num_sta_ht_no_gf--;
163                 if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0)
164                         hapd->iface->num_sta_ht_20mhz--;
165         } else
166                 hapd->iface->num_sta_no_ht--;
167
168         if (hostapd_ht_operation_update(hapd->iface) > 0)
169                 set_beacon++;
170 #endif /* CONFIG_IEEE80211N */
171
172         if (set_beacon)
173                 ieee802_11_set_beacons(hapd->iface);
174
175         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
176         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
177
178         ieee802_1x_free_station(sta);
179         wpa_auth_sta_deinit(sta->wpa_sm);
180         rsn_preauth_free_station(hapd, sta);
181         radius_client_flush_auth(hapd->radius, sta->addr);
182
183         os_free(sta->last_assoc_req);
184         os_free(sta->challenge);
185
186 #ifdef CONFIG_IEEE80211W
187         os_free(sta->ping_trans_id);
188         eloop_cancel_timeout(ap_ping_timer, hapd, sta);
189 #endif /* CONFIG_IEEE80211W */
190
191         wpabuf_free(sta->wps_ie);
192
193         os_free(sta);
194 }
195
196
197 void hostapd_free_stas(struct hostapd_data *hapd)
198 {
199         struct sta_info *sta, *prev;
200
201         sta = hapd->sta_list;
202
203         while (sta) {
204                 prev = sta;
205                 if (sta->flags & WLAN_STA_AUTH) {
206                         mlme_deauthenticate_indication(
207                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
208                 }
209                 sta = sta->next;
210                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
211                            MAC2STR(prev->addr));
212                 ap_free_sta(hapd, prev);
213         }
214 }
215
216
217 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
218 {
219         struct hostapd_data *hapd = eloop_ctx;
220         struct sta_info *sta = timeout_ctx;
221         unsigned long next_time = 0;
222
223         if (sta->timeout_next == STA_REMOVE) {
224                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
225                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
226                                "local deauth request");
227                 ap_free_sta(hapd, sta);
228                 return;
229         }
230
231         if ((sta->flags & WLAN_STA_ASSOC) &&
232             (sta->timeout_next == STA_NULLFUNC ||
233              sta->timeout_next == STA_DISASSOC)) {
234                 int inactive_sec;
235                 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
236                            MAC2STR(sta->addr));
237                 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
238                 if (inactive_sec == -1) {
239                         wpa_printf(MSG_DEBUG, "Could not get station info "
240                                    "from kernel driver for " MACSTR ".",
241                                    MAC2STR(sta->addr));
242                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
243                            sta->flags & WLAN_STA_ASSOC) {
244                         /* station activity detected; reset timeout state */
245                         wpa_printf(MSG_DEBUG, "  Station has been active");
246                         sta->timeout_next = STA_NULLFUNC;
247                         next_time = hapd->conf->ap_max_inactivity -
248                                 inactive_sec;
249                 }
250         }
251
252         if ((sta->flags & WLAN_STA_ASSOC) &&
253             sta->timeout_next == STA_DISASSOC &&
254             !(sta->flags & WLAN_STA_PENDING_POLL)) {
255                 wpa_printf(MSG_DEBUG, "  Station has ACKed data poll");
256                 /* data nullfunc frame poll did not produce TX errors; assume
257                  * station ACKed it */
258                 sta->timeout_next = STA_NULLFUNC;
259                 next_time = hapd->conf->ap_max_inactivity;
260         }
261
262         if (next_time) {
263                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
264                                        sta);
265                 return;
266         }
267
268         if (sta->timeout_next == STA_NULLFUNC &&
269             (sta->flags & WLAN_STA_ASSOC)) {
270                 /* send data frame to poll STA and check whether this frame
271                  * is ACKed */
272                 struct ieee80211_hdr hdr;
273
274                 wpa_printf(MSG_DEBUG, "  Polling STA with data frame");
275                 sta->flags |= WLAN_STA_PENDING_POLL;
276
277 #ifndef CONFIG_NATIVE_WINDOWS
278                 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
279                  * it is apparently not retried so TX Exc events are not
280                  * received for it */
281                 os_memset(&hdr, 0, sizeof(hdr));
282                 hdr.frame_control =
283                         IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
284                 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
285                 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
286                 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
287                           ETH_ALEN);
288                 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
289
290                 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
291                         perror("ap_handle_timer: send");
292 #endif /* CONFIG_NATIVE_WINDOWS */
293         } else if (sta->timeout_next != STA_REMOVE) {
294                 int deauth = sta->timeout_next == STA_DEAUTH;
295
296                 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
297                            deauth ? "deauthentication" : "disassociation",
298                            MAC2STR(sta->addr));
299
300                 if (deauth) {
301                         hostapd_sta_deauth(hapd, sta->addr,
302                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
303                 } else {
304                         hostapd_sta_disassoc(
305                                 hapd, sta->addr,
306                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
307                 }
308         }
309
310         switch (sta->timeout_next) {
311         case STA_NULLFUNC:
312                 sta->timeout_next = STA_DISASSOC;
313                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
314                                        hapd, sta);
315                 break;
316         case STA_DISASSOC:
317                 sta->flags &= ~WLAN_STA_ASSOC;
318                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
319                 if (!sta->acct_terminate_cause)
320                         sta->acct_terminate_cause =
321                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
322                 accounting_sta_stop(hapd, sta);
323                 ieee802_1x_free_station(sta);
324                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
325                                HOSTAPD_LEVEL_INFO, "disassociated due to "
326                                "inactivity");
327                 sta->timeout_next = STA_DEAUTH;
328                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
329                                        hapd, sta);
330                 mlme_disassociate_indication(
331                         hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
332                 break;
333         case STA_DEAUTH:
334         case STA_REMOVE:
335                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
336                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
337                                "inactivity");
338                 if (!sta->acct_terminate_cause)
339                         sta->acct_terminate_cause =
340                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
341                 mlme_deauthenticate_indication(
342                         hapd, sta,
343                         WLAN_REASON_PREV_AUTH_NOT_VALID);
344                 ap_free_sta(hapd, sta);
345                 break;
346         }
347 }
348
349
350 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
351 {
352         struct hostapd_data *hapd = eloop_ctx;
353         struct sta_info *sta = timeout_ctx;
354         u8 addr[ETH_ALEN];
355
356         if (!(sta->flags & WLAN_STA_AUTH))
357                 return;
358
359         mlme_deauthenticate_indication(hapd, sta,
360                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
361         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
362                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
363                        "session timeout");
364         sta->acct_terminate_cause =
365                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
366         os_memcpy(addr, sta->addr, ETH_ALEN);
367         ap_free_sta(hapd, sta);
368         hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
369 }
370
371
372 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
373                             u32 session_timeout)
374 {
375         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
376                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
377                        "seconds", session_timeout);
378         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
379         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
380                                hapd, sta);
381 }
382
383
384 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
385 {
386         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
387 }
388
389
390 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
391 {
392         struct sta_info *sta;
393
394         sta = ap_get_sta(hapd, addr);
395         if (sta)
396                 return sta;
397
398         wpa_printf(MSG_DEBUG, "  New STA");
399         if (hapd->num_sta >= hapd->conf->max_num_sta) {
400                 /* FIX: might try to remove some old STAs first? */
401                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
402                            hapd->num_sta, hapd->conf->max_num_sta);
403                 return NULL;
404         }
405
406         sta = os_zalloc(sizeof(struct sta_info));
407         if (sta == NULL) {
408                 wpa_printf(MSG_ERROR, "malloc failed");
409                 return NULL;
410         }
411         sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
412
413         /* initialize STA info data */
414         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
415                                ap_handle_timer, hapd, sta);
416         os_memcpy(sta->addr, addr, ETH_ALEN);
417         sta->next = hapd->sta_list;
418         hapd->sta_list = sta;
419         hapd->num_sta++;
420         ap_sta_hash_add(hapd, sta);
421         sta->ssid = &hapd->conf->ssid;
422
423         return sta;
424 }
425
426
427 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
428 {
429         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
430
431         wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
432                    MAC2STR(sta->addr));
433         if (hostapd_sta_remove(hapd, sta->addr) &&
434             sta->flags & WLAN_STA_ASSOC) {
435                 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
436                            " from kernel driver.", MAC2STR(sta->addr));
437                 return -1;
438         }
439         return 0;
440 }
441
442
443 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
444                                struct sta_info *sta, u32 flags)
445 {
446         struct hostapd_iface *iface = hapd->iface;
447         size_t i;
448
449         for (i = 0; i < iface->num_bss; i++) {
450                 struct hostapd_data *bss = iface->bss[i];
451                 struct sta_info *sta2;
452                 /* bss should always be set during operation, but it may be
453                  * NULL during reconfiguration. Assume the STA is not
454                  * associated to another BSS in that case to avoid NULL pointer
455                  * dereferences. */
456                 if (bss == hapd || bss == NULL)
457                         continue;
458                 sta2 = ap_get_sta(bss, sta->addr);
459                 if (sta2 && ((sta2->flags & flags) == flags))
460                         return 1;
461         }
462
463         return 0;
464 }
465
466
467 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
468                          u16 reason)
469 {
470         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
471                    hapd->conf->iface, MAC2STR(sta->addr));
472         sta->flags &= ~WLAN_STA_ASSOC;
473         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
474                 ap_sta_remove(hapd, sta);
475         sta->timeout_next = STA_DEAUTH;
476         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
477         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
478                                ap_handle_timer, hapd, sta);
479         accounting_sta_stop(hapd, sta);
480         ieee802_1x_free_station(sta);
481
482         mlme_disassociate_indication(hapd, sta, reason);
483 }
484
485
486 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
487                            u16 reason)
488 {
489         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
490                    hapd->conf->iface, MAC2STR(sta->addr));
491         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
492         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
493                 ap_sta_remove(hapd, sta);
494         sta->timeout_next = STA_REMOVE;
495         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
496         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
497                                ap_handle_timer, hapd, sta);
498         accounting_sta_stop(hapd, sta);
499         ieee802_1x_free_station(sta);
500
501         mlme_deauthenticate_indication(hapd, sta, reason);
502 }
503
504
505 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
506                      int old_vlanid)
507 {
508         const char *iface;
509         struct hostapd_vlan *vlan = NULL;
510
511         /*
512          * Do not proceed furthur if the vlan id remains same. We do not want
513          * duplicate dynamic vlan entries.
514          */
515         if (sta->vlan_id == old_vlanid)
516                 return 0;
517
518         /*
519          * During 1x reauth, if the vlan id changes, then remove the old id and
520          * proceed furthur to add the new one.
521          */
522         if (old_vlanid > 0)
523                 vlan_remove_dynamic(hapd, old_vlanid);
524
525         iface = hapd->conf->iface;
526         if (sta->ssid->vlan[0])
527                 iface = sta->ssid->vlan;
528
529         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
530                 sta->vlan_id = 0;
531         else if (sta->vlan_id > 0) {
532                 vlan = hapd->conf->vlan;
533                 while (vlan) {
534                         if (vlan->vlan_id == sta->vlan_id ||
535                             vlan->vlan_id == VLAN_ID_WILDCARD) {
536                                 iface = vlan->ifname;
537                                 break;
538                         }
539                         vlan = vlan->next;
540                 }
541         }
542
543         if (sta->vlan_id > 0 && vlan == NULL) {
544                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
545                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
546                                "binding station to (vlan_id=%d)",
547                                sta->vlan_id);
548                 return -1;
549         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
550                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
551                 if (vlan == NULL) {
552                         hostapd_logger(hapd, sta->addr,
553                                        HOSTAPD_MODULE_IEEE80211,
554                                        HOSTAPD_LEVEL_DEBUG, "could not add "
555                                        "dynamic VLAN interface for vlan_id=%d",
556                                        sta->vlan_id);
557                         return -1;
558                 }
559
560                 iface = vlan->ifname;
561                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
562                         hostapd_logger(hapd, sta->addr,
563                                        HOSTAPD_MODULE_IEEE80211,
564                                        HOSTAPD_LEVEL_DEBUG, "could not "
565                                        "configure encryption for dynamic VLAN "
566                                        "interface for vlan_id=%d",
567                                        sta->vlan_id);
568                 }
569
570                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
571                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
572                                "interface '%s'", iface);
573         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
574                 if (sta->vlan_id > 0) {
575                         vlan->dynamic_vlan++;
576                         hostapd_logger(hapd, sta->addr,
577                                        HOSTAPD_MODULE_IEEE80211,
578                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
579                                        "dynamic VLAN interface '%s'", iface);
580                 }
581
582                 /*
583                  * Update encryption configuration for statically generated
584                  * VLAN interface. This is only used for static WEP
585                  * configuration for the case where hostapd did not yet know
586                  * which keys are to be used when the interface was added.
587                  */
588                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
589                         hostapd_logger(hapd, sta->addr,
590                                        HOSTAPD_MODULE_IEEE80211,
591                                        HOSTAPD_LEVEL_DEBUG, "could not "
592                                        "configure encryption for VLAN "
593                                        "interface for vlan_id=%d",
594                                        sta->vlan_id);
595                 }
596         }
597
598         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
599                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
600                        "'%s'", iface);
601
602         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
603                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
604
605         return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
606 }
607
608
609 #ifdef CONFIG_IEEE80211W
610
611 /* MLME-PING.request */
612 static void ieee802_11_send_ping_req(struct hostapd_data *hapd, const u8 *addr,
613                                      const u8 *trans_id)
614 {
615         struct ieee80211_mgmt mgmt;
616         u8 *end;
617
618         os_memset(&mgmt, 0, sizeof(mgmt));
619         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
620                                           WLAN_FC_STYPE_ACTION);
621         os_memcpy(mgmt.da, addr, ETH_ALEN);
622         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
623         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
624         mgmt.u.action.category = WLAN_ACTION_PING;
625         mgmt.u.action.u.ping_req.action = WLAN_PING_REQUEST;
626         os_memcpy(mgmt.u.action.u.ping_req.trans_id, trans_id,
627                   WLAN_PING_TRANS_ID_LEN);
628         end = mgmt.u.action.u.ping_req.trans_id + WLAN_PING_TRANS_ID_LEN;
629         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
630                                     end - (u8 *) &mgmt, 0) < 0)
631                 perror("ieee802_11_send_ping_req: send");
632 }
633
634
635 static void ap_ping_timer(void *eloop_ctx, void *timeout_ctx)
636 {
637         struct hostapd_data *hapd = eloop_ctx;
638         struct sta_info *sta = timeout_ctx;
639         unsigned int timeout, sec, usec;
640         u8 *trans_id, *nbuf;
641
642         if (sta->ping_count >= hapd->conf->assoc_ping_attempts) {
643                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
644                                HOSTAPD_LEVEL_DEBUG,
645                                "association ping timed out");
646                 sta->ping_timed_out = 1;
647                 os_free(sta->ping_trans_id);
648                 sta->ping_trans_id = NULL;
649                 sta->ping_count = 0;
650                 return;
651         }
652
653         nbuf = os_realloc(sta->ping_trans_id,
654                           (sta->ping_count + 1) * WLAN_PING_TRANS_ID_LEN);
655         if (nbuf == NULL)
656                 return;
657         trans_id = nbuf + sta->ping_count * WLAN_PING_TRANS_ID_LEN;
658         sta->ping_trans_id = nbuf;
659         sta->ping_count++;
660
661         os_get_random(trans_id, WLAN_PING_TRANS_ID_LEN);
662
663         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
664                        HOSTAPD_LEVEL_DEBUG,
665                        "association ping attempt %d", sta->ping_count);
666
667         ieee802_11_send_ping_req(hapd, sta->addr, trans_id);
668
669         timeout = hapd->conf->assoc_ping_timeout;
670         sec = ((timeout / 1000) * 1024) / 1000;
671         usec = (timeout % 1000) * 1024;
672         eloop_register_timeout(sec, usec, ap_ping_timer, hapd, sta);
673 }
674
675
676 void ap_sta_start_ping(struct hostapd_data *hapd, struct sta_info *sta)
677 {
678         ap_ping_timer(hapd, sta);
679 }
680
681
682 void ap_sta_stop_ping(struct hostapd_data *hapd, struct sta_info *sta)
683 {
684         eloop_cancel_timeout(ap_ping_timer, hapd, sta);
685         os_free(sta->ping_trans_id);
686         sta->ping_trans_id = NULL;
687         sta->ping_count = 0;
688 }
689
690 #endif /* CONFIG_IEEE80211W */