wpa_supplicant: Update to work without verision tag
[dragonfly.git] / contrib / hostapd-0.5.8 / sta_info.c
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2006, Jouni Malinen <j@w1.fi>
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 "hostapd.h"
18 #include "sta_info.h"
19 #include "eloop.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "radius.h"
24 #include "eapol_sm.h"
25 #include "wpa.h"
26 #include "preauth.h"
27 #include "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
38 int ap_for_each_sta(struct hostapd_data *hapd,
39                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
40                               void *ctx),
41                     void *ctx)
42 {
43         struct sta_info *sta;
44
45         for (sta = hapd->sta_list; sta; sta = sta->next) {
46                 if (cb(hapd, sta, ctx))
47                         return 1;
48         }
49
50         return 0;
51 }
52
53
54 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
55 {
56         struct sta_info *s;
57
58         s = hapd->sta_hash[STA_HASH(sta)];
59         while (s != NULL && memcmp(s->addr, sta, 6) != 0)
60                 s = s->hnext;
61         return s;
62 }
63
64
65 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
66 {
67         struct sta_info *tmp;
68
69         if (hapd->sta_list == sta) {
70                 hapd->sta_list = sta->next;
71                 return;
72         }
73
74         tmp = hapd->sta_list;
75         while (tmp != NULL && tmp->next != sta)
76                 tmp = tmp->next;
77         if (tmp == NULL) {
78                 printf("Could not remove STA " MACSTR " from list.\n",
79                        MAC2STR(sta->addr));
80         } else
81                 tmp->next = sta->next;
82 }
83
84
85 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
86 {
87         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
88         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
89 }
90
91
92 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
93 {
94         struct sta_info *s;
95
96         s = hapd->sta_hash[STA_HASH(sta->addr)];
97         if (s == NULL) return;
98         if (memcmp(s->addr, sta->addr, 6) == 0) {
99                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
100                 return;
101         }
102
103         while (s->hnext != NULL && memcmp(s->hnext->addr, sta->addr, 6) != 0)
104                 s = s->hnext;
105         if (s->hnext != NULL)
106                 s->hnext = s->hnext->hnext;
107         else
108                 printf("AP: could not remove STA " MACSTR " from hash table\n",
109                        MAC2STR(sta->addr));
110 }
111
112
113 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
114 {
115         int set_beacon = 0;
116
117         accounting_sta_stop(hapd, sta);
118
119         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
120             !(sta->flags & WLAN_STA_PREAUTH))
121                 hostapd_sta_remove(hapd, sta->addr);
122
123         ap_sta_hash_del(hapd, sta);
124         ap_sta_list_del(hapd, sta);
125
126         if (sta->aid > 0)
127                 hapd->sta_aid[sta->aid - 1] = NULL;
128
129         hapd->num_sta--;
130         if (sta->nonerp_set) {
131                 sta->nonerp_set = 0;
132                 hapd->iface->num_sta_non_erp--;
133                 if (hapd->iface->num_sta_non_erp == 0)
134                         set_beacon++;
135         }
136
137         if (sta->no_short_slot_time_set) {
138                 sta->no_short_slot_time_set = 0;
139                 hapd->iface->num_sta_no_short_slot_time--;
140                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
141                     && hapd->iface->num_sta_no_short_slot_time == 0)
142                         set_beacon++;
143         }
144
145         if (sta->no_short_preamble_set) {
146                 sta->no_short_preamble_set = 0;
147                 hapd->iface->num_sta_no_short_preamble--;
148                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
149                     && hapd->iface->num_sta_no_short_preamble == 0)
150                         set_beacon++;
151         }
152
153         if (set_beacon)
154                 ieee802_11_set_beacons(hapd->iface);
155
156         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
157         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
158
159         ieee802_1x_free_station(sta);
160         wpa_auth_sta_deinit(sta->wpa_sm);
161         rsn_preauth_free_station(hapd, sta);
162         radius_client_flush_auth(hapd->radius, sta->addr);
163
164         if (sta->last_assoc_req)
165                 free(sta->last_assoc_req);
166
167         free(sta->challenge);
168
169         free(sta);
170 }
171
172
173 void hostapd_free_stas(struct hostapd_data *hapd)
174 {
175         struct sta_info *sta, *prev;
176
177         sta = hapd->sta_list;
178
179         while (sta) {
180                 prev = sta;
181                 if (sta->flags & WLAN_STA_AUTH) {
182                         mlme_deauthenticate_indication(
183                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
184                 }
185                 sta = sta->next;
186                 printf("Removing station " MACSTR "\n", MAC2STR(prev->addr));
187                 ap_free_sta(hapd, prev);
188         }
189 }
190
191
192 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
193 {
194         struct hostapd_data *hapd = eloop_ctx;
195         struct sta_info *sta = timeout_ctx;
196         unsigned long next_time = 0;
197
198         if (sta->timeout_next == STA_REMOVE) {
199                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
200                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
201                                "local deauth request");
202                 ap_free_sta(hapd, sta);
203                 return;
204         }
205
206         if ((sta->flags & WLAN_STA_ASSOC) &&
207             (sta->timeout_next == STA_NULLFUNC ||
208              sta->timeout_next == STA_DISASSOC)) {
209                 int inactive_sec;
210                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
211                               "Checking STA " MACSTR " inactivity:\n",
212                               MAC2STR(sta->addr));
213                 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
214                 if (inactive_sec == -1) {
215                         printf("  Could not get station info from kernel "
216                                "driver for " MACSTR ".\n",
217                                MAC2STR(sta->addr));
218                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
219                            sta->flags & WLAN_STA_ASSOC) {
220                         /* station activity detected; reset timeout state */
221                         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
222                                       "  Station has been active\n");
223                         sta->timeout_next = STA_NULLFUNC;
224                         next_time = hapd->conf->ap_max_inactivity -
225                                 inactive_sec;
226                 }
227         }
228
229         if ((sta->flags & WLAN_STA_ASSOC) &&
230             sta->timeout_next == STA_DISASSOC &&
231             !(sta->flags & WLAN_STA_PENDING_POLL)) {
232                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
233                               "  Station has ACKed data poll\n");
234                 /* data nullfunc frame poll did not produce TX errors; assume
235                  * station ACKed it */
236                 sta->timeout_next = STA_NULLFUNC;
237                 next_time = hapd->conf->ap_max_inactivity;
238         }
239
240         if (next_time) {
241                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
242                                        sta);
243                 return;
244         }
245
246         if (sta->timeout_next == STA_NULLFUNC &&
247             (sta->flags & WLAN_STA_ASSOC)) {
248                 /* send data frame to poll STA and check whether this frame
249                  * is ACKed */
250                 struct ieee80211_hdr hdr;
251
252                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
253                               "  Polling STA with data frame\n");
254                 sta->flags |= WLAN_STA_PENDING_POLL;
255
256 #ifndef CONFIG_NATIVE_WINDOWS
257                 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
258                  * it is apparently not retried so TX Exc events are not
259                  * received for it */
260                 memset(&hdr, 0, sizeof(hdr));
261                 hdr.frame_control =
262                         IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
263                 hdr.frame_control |= host_to_le16(BIT(1));
264                 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
265                 memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
266                 memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr, ETH_ALEN);
267                 memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
268
269                 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
270                         perror("ap_handle_timer: send");
271 #endif /* CONFIG_NATIVE_WINDOWS */
272         } else if (sta->timeout_next != STA_REMOVE) {
273                 int deauth = sta->timeout_next == STA_DEAUTH;
274
275                 printf("  Sending %s info to STA " MACSTR "\n",
276                        deauth ? "deauthentication" : "disassociation",
277                        MAC2STR(sta->addr));
278
279                 if (deauth) {
280                         hostapd_sta_deauth(hapd, sta->addr,
281                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
282                 } else {
283                         hostapd_sta_disassoc(
284                                 hapd, sta->addr,
285                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
286                 }
287         }
288
289         switch (sta->timeout_next) {
290         case STA_NULLFUNC:
291                 sta->timeout_next = STA_DISASSOC;
292                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
293                                        hapd, sta);
294                 break;
295         case STA_DISASSOC:
296                 sta->flags &= ~WLAN_STA_ASSOC;
297                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
298                 if (!sta->acct_terminate_cause)
299                         sta->acct_terminate_cause =
300                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
301                 accounting_sta_stop(hapd, sta);
302                 ieee802_1x_free_station(sta);
303                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
304                                HOSTAPD_LEVEL_INFO, "disassociated due to "
305                                "inactivity");
306                 sta->timeout_next = STA_DEAUTH;
307                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
308                                        hapd, sta);
309                 mlme_disassociate_indication(
310                         hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
311                 break;
312         case STA_DEAUTH:
313         case STA_REMOVE:
314                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
315                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
316                                "inactivity");
317                 if (!sta->acct_terminate_cause)
318                         sta->acct_terminate_cause =
319                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
320                 mlme_deauthenticate_indication(
321                         hapd, sta,
322                         WLAN_REASON_PREV_AUTH_NOT_VALID);
323                 ap_free_sta(hapd, sta);
324                 break;
325         }
326 }
327
328
329 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
330 {
331         struct hostapd_data *hapd = eloop_ctx;
332         struct sta_info *sta = timeout_ctx;
333         u8 addr[ETH_ALEN];
334
335         if (!(sta->flags & WLAN_STA_AUTH))
336                 return;
337
338         mlme_deauthenticate_indication(hapd, sta,
339                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
340         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
341                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
342                        "session timeout");
343         sta->acct_terminate_cause =
344                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
345         memcpy(addr, sta->addr, ETH_ALEN);
346         ap_free_sta(hapd, sta);
347         hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
348 }
349
350
351 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
352                             u32 session_timeout)
353 {
354         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
355                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
356                        "seconds", session_timeout);
357         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
358         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
359                                hapd, sta);
360 }
361
362
363 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
364 {
365         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
366 }
367
368
369 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
370 {
371         struct sta_info *sta;
372
373         sta = ap_get_sta(hapd, addr);
374         if (sta)
375                 return sta;
376
377         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "  New STA\n");
378         if (hapd->num_sta >= hapd->conf->max_num_sta) {
379                 /* FIX: might try to remove some old STAs first? */
380                 printf("  no more room for new STAs (%d/%d)\n",
381                        hapd->num_sta, hapd->conf->max_num_sta);
382                 return NULL;
383         }
384
385         sta = wpa_zalloc(sizeof(struct sta_info));
386         if (sta == NULL) {
387                 printf("  malloc failed\n");
388                 return NULL;
389         }
390         sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
391
392         /* initialize STA info data */
393         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
394                                ap_handle_timer, hapd, sta);
395         memcpy(sta->addr, addr, ETH_ALEN);
396         sta->next = hapd->sta_list;
397         hapd->sta_list = sta;
398         hapd->num_sta++;
399         ap_sta_hash_add(hapd, sta);
400         sta->ssid = &hapd->conf->ssid;
401
402         return sta;
403 }
404
405
406 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
407 {
408         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
409
410         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Removing STA " MACSTR
411                       " from kernel driver\n", MAC2STR(sta->addr));
412         if (hostapd_sta_remove(hapd, sta->addr) &&
413             sta->flags & WLAN_STA_ASSOC) {
414                 printf("Could not remove station " MACSTR " from kernel "
415                        "driver.\n", MAC2STR(sta->addr));
416                 return -1;
417         }
418         return 0;
419 }
420
421
422 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
423                                struct sta_info *sta, u32 flags)
424 {
425         struct hostapd_iface *iface = hapd->iface;
426         size_t i;
427
428         for (i = 0; i < iface->num_bss; i++) {
429                 struct hostapd_data *bss = iface->bss[i];
430                 struct sta_info *sta2;
431                 /* bss should always be set during operation, but it may be
432                  * NULL during reconfiguration. Assume the STA is not
433                  * associated to another BSS in that case to avoid NULL pointer
434                  * dereferences. */
435                 if (bss == hapd || bss == NULL)
436                         continue;
437                 sta2 = ap_get_sta(bss, sta->addr);
438                 if (sta2 && ((sta2->flags & flags) == flags))
439                         return 1;
440         }
441
442         return 0;
443 }
444
445
446 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
447                          u16 reason)
448 {
449         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: disassociate STA " MACSTR
450                       "\n", hapd->conf->iface, MAC2STR(sta->addr));
451         sta->flags &= ~WLAN_STA_ASSOC;
452         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
453                 ap_sta_remove(hapd, sta);
454         sta->timeout_next = STA_DEAUTH;
455         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
456         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
457                                ap_handle_timer, hapd, sta);
458         accounting_sta_stop(hapd, sta);
459         ieee802_1x_free_station(sta);
460
461         mlme_disassociate_indication(hapd, sta, reason);
462 }
463
464
465 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
466                            u16 reason)
467 {
468         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: deauthenticate STA " MACSTR
469                       "\n", hapd->conf->iface, MAC2STR(sta->addr));
470         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
471         if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
472                 ap_sta_remove(hapd, sta);
473         sta->timeout_next = STA_REMOVE;
474         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
475         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
476                                ap_handle_timer, hapd, sta);
477         accounting_sta_stop(hapd, sta);
478         ieee802_1x_free_station(sta);
479
480         mlme_deauthenticate_indication(hapd, sta, reason);
481 }
482
483
484 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
485                      int old_vlanid)
486 {
487         const char *iface;
488         struct hostapd_vlan *vlan = NULL;
489
490         /*
491          * Do not proceed furthur if the vlan id remains same. We do not want
492          * duplicate dynamic vlan entries.
493          */
494         if (sta->vlan_id == old_vlanid)
495                 return 0;
496
497         /*
498          * During 1x reauth, if the vlan id changes, then remove the old id and
499          * proceed furthur to add the new one.
500          */
501         if (old_vlanid > 0)
502                 vlan_remove_dynamic(hapd, old_vlanid);
503
504         iface = hapd->conf->iface;
505         if (sta->ssid->vlan[0])
506                 iface = sta->ssid->vlan;
507
508         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
509                 sta->vlan_id = 0;
510         else if (sta->vlan_id > 0) {
511                 vlan = hapd->conf->vlan;
512                 while (vlan) {
513                         if (vlan->vlan_id == sta->vlan_id ||
514                             vlan->vlan_id == VLAN_ID_WILDCARD) {
515                                 iface = vlan->ifname;
516                                 break;
517                         }
518                         vlan = vlan->next;
519                 }
520         }
521
522         if (sta->vlan_id > 0 && vlan == NULL) {
523                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
524                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
525                                "binding station to (vlan_id=%d)",
526                                sta->vlan_id);
527                 return -1;
528         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
529                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
530                 if (vlan == NULL) {
531                         hostapd_logger(hapd, sta->addr,
532                                        HOSTAPD_MODULE_IEEE80211,
533                                        HOSTAPD_LEVEL_DEBUG, "could not add "
534                                        "dynamic VLAN interface for vlan_id=%d",
535                                        sta->vlan_id);
536                         return -1;
537                 }
538
539                 iface = vlan->ifname;
540                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
541                         hostapd_logger(hapd, sta->addr,
542                                        HOSTAPD_MODULE_IEEE80211,
543                                        HOSTAPD_LEVEL_DEBUG, "could not "
544                                        "configure encryption for dynamic VLAN "
545                                        "interface for vlan_id=%d",
546                                        sta->vlan_id);
547                 }
548
549                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
550                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
551                                "interface '%s'", iface);
552         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
553                 if (sta->vlan_id > 0) {
554                         vlan->dynamic_vlan++;
555                         hostapd_logger(hapd, sta->addr,
556                                        HOSTAPD_MODULE_IEEE80211,
557                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
558                                        "dynamic VLAN interface '%s'", iface);
559                 }
560
561                 /*
562                  * Update encryption configuration for statically generated
563                  * VLAN interface. This is only used for static WEP
564                  * configuration for the case where hostapd did not yet know
565                  * which keys are to be used when the interface was added.
566                  */
567                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
568                         hostapd_logger(hapd, sta->addr,
569                                        HOSTAPD_MODULE_IEEE80211,
570                                        HOSTAPD_LEVEL_DEBUG, "could not "
571                                        "configure encryption for VLAN "
572                                        "interface for vlan_id=%d",
573                                        sta->vlan_id);
574                 }
575         }
576
577         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
578                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
579                        "'%s'", iface);
580
581         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
582                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
583
584         return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
585 }