Add preliminary IEEE 802.11n support into hostapd
[wpasupplicant] / hostapd / ap_list.c
1 /*
2  * hostapd / AP table
3  * Copyright 2002-2003, Jouni Malinen <j@w1.fi>
4  * Copyright 2003-2004, Instant802 Networks, Inc.
5  * Copyright 2006, Devicescape Software, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "includes.h"
18
19 #include "hostapd.h"
20 #include "ieee802_11.h"
21 #include "eloop.h"
22 #include "ap_list.h"
23 #include "hw_features.h"
24 #include "beacon.h"
25
26
27 struct ieee80211_frame_info {
28         u32 version;
29         u32 length;
30         u64 mactime;
31         u64 hosttime;
32         u32 phytype;
33         u32 channel;
34         u32 datarate;
35         u32 antenna;
36         u32 priority;
37         u32 ssi_type;
38         u32 ssi_signal;
39         u32 ssi_noise;
40         u32 preamble;
41         u32 encoding;
42
43         /* Note: this structure is otherwise identical to capture format used
44          * in linux-wlan-ng, but this additional field is used to provide meta
45          * data about the frame to hostapd. This was the easiest method for
46          * providing this information, but this might change in the future. */
47         u32 msg_type;
48 } __attribute__ ((packed));
49
50
51 enum ieee80211_phytype {
52         ieee80211_phytype_fhss_dot11_97  = 1,
53         ieee80211_phytype_dsss_dot11_97  = 2,
54         ieee80211_phytype_irbaseband     = 3,
55         ieee80211_phytype_dsss_dot11_b   = 4,
56         ieee80211_phytype_pbcc_dot11_b   = 5,
57         ieee80211_phytype_ofdm_dot11_g   = 6,
58         ieee80211_phytype_pbcc_dot11_g   = 7,
59         ieee80211_phytype_ofdm_dot11_a   = 8,
60         ieee80211_phytype_dsss_dot11_turbog = 255,
61         ieee80211_phytype_dsss_dot11_turbo = 256,
62 };
63
64
65 /* AP list is a double linked list with head->prev pointing to the end of the
66  * list and tail->next = NULL. Entries are moved to the head of the list
67  * whenever a beacon has been received from the AP in question. The tail entry
68  * in this link will thus be the least recently used entry. */
69
70
71 static void ap_list_new_ap(struct hostapd_iface *iface, struct ap_info *ap)
72 {
73         wpa_printf(MSG_DEBUG, "New AP detected: " MACSTR, MAC2STR(ap->addr));
74
75         /* TODO: could send a notification message to an external program that
76          * would then determine whether a rogue AP has been detected */
77 }
78
79
80 static void ap_list_expired_ap(struct hostapd_iface *iface, struct ap_info *ap)
81 {
82         wpa_printf(MSG_DEBUG, "AP info expired: " MACSTR, MAC2STR(ap->addr));
83
84         /* TODO: could send a notification message to an external program */
85 }
86
87
88 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
89 {
90         int i;
91
92         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
93             ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
94             iface->conf->channel != ap->channel)
95                 return 0;
96
97         if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
98                 return 1;
99
100         for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
101                 int rate = (ap->supported_rates[i] & 0x7f) * 5;
102                 if (rate == 60 || rate == 90 || rate > 110)
103                         return 0;
104         }
105
106         return 1;
107 }
108
109
110 #ifdef CONFIG_IEEE80211N
111 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
112                                   struct ap_info *ap)
113 {
114         return !ap->ht_support;
115 }
116 #endif /* CONFIG_IEEE80211N */
117
118
119 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
120 {
121         struct ap_info *s;
122
123         s = iface->ap_hash[STA_HASH(ap)];
124         while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
125                 s = s->hnext;
126         return s;
127 }
128
129
130 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
131 {
132         if (iface->ap_list) {
133                 ap->prev = iface->ap_list->prev;
134                 iface->ap_list->prev = ap;
135         } else
136                 ap->prev = ap;
137         ap->next = iface->ap_list;
138         iface->ap_list = ap;
139 }
140
141
142 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
143 {
144         if (iface->ap_list == ap)
145                 iface->ap_list = ap->next;
146         else
147                 ap->prev->next = ap->next;
148
149         if (ap->next)
150                 ap->next->prev = ap->prev;
151         else if (iface->ap_list)
152                 iface->ap_list->prev = ap->prev;
153 }
154
155
156 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
157                                 struct ap_info *ap)
158 {
159         if (iface->ap_iter_list) {
160                 ap->iter_prev = iface->ap_iter_list->iter_prev;
161                 iface->ap_iter_list->iter_prev = ap;
162         } else
163                 ap->iter_prev = ap;
164         ap->iter_next = iface->ap_iter_list;
165         iface->ap_iter_list = ap;
166 }
167
168
169 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
170                                 struct ap_info *ap)
171 {
172         if (iface->ap_iter_list == ap)
173                 iface->ap_iter_list = ap->iter_next;
174         else
175                 ap->iter_prev->iter_next = ap->iter_next;
176
177         if (ap->iter_next)
178                 ap->iter_next->iter_prev = ap->iter_prev;
179         else if (iface->ap_iter_list)
180                 iface->ap_iter_list->iter_prev = ap->iter_prev;
181 }
182
183
184 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
185 {
186         ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
187         iface->ap_hash[STA_HASH(ap->addr)] = ap;
188 }
189
190
191 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
192 {
193         struct ap_info *s;
194
195         s = iface->ap_hash[STA_HASH(ap->addr)];
196         if (s == NULL) return;
197         if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
198                 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
199                 return;
200         }
201
202         while (s->hnext != NULL &&
203                os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
204                 s = s->hnext;
205         if (s->hnext != NULL)
206                 s->hnext = s->hnext->hnext;
207         else
208                 printf("AP: could not remove AP " MACSTR " from hash table\n",
209                        MAC2STR(ap->addr));
210 }
211
212
213 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
214 {
215         ap_ap_hash_del(iface, ap);
216         ap_ap_list_del(iface, ap);
217         ap_ap_iter_list_del(iface, ap);
218
219         iface->num_ap--;
220         os_free(ap);
221 }
222
223
224 static void hostapd_free_aps(struct hostapd_iface *iface)
225 {
226         struct ap_info *ap, *prev;
227
228         ap = iface->ap_list;
229
230         while (ap) {
231                 prev = ap;
232                 ap = ap->next;
233                 ap_free_ap(iface, prev);
234         }
235
236         iface->ap_list = NULL;
237 }
238
239
240 int ap_ap_for_each(struct hostapd_iface *iface,
241                    int (*func)(struct ap_info *s, void *data), void *data)
242 {
243         struct ap_info *s;
244         int ret = 0;
245
246         s = iface->ap_list;
247
248         while (s) {
249                 ret = func(s, data);
250                 if (ret)
251                         break;
252                 s = s->next;
253         }
254
255         return ret;
256 }
257
258
259 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
260 {
261         struct ap_info *ap;
262
263         ap = os_zalloc(sizeof(struct ap_info));
264         if (ap == NULL)
265                 return NULL;
266
267         /* initialize AP info data */
268         os_memcpy(ap->addr, addr, ETH_ALEN);
269         ap_ap_list_add(iface, ap);
270         iface->num_ap++;
271         ap_ap_hash_add(iface, ap);
272         ap_ap_iter_list_add(iface, ap);
273
274         if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
275                 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
276                            MACSTR " from AP table", MAC2STR(ap->prev->addr));
277                 if (iface->conf->passive_scan_interval > 0)
278                         ap_list_expired_ap(iface, ap->prev);
279                 ap_free_ap(iface, ap->prev);
280         }
281
282         return ap;
283 }
284
285
286 void ap_list_process_beacon(struct hostapd_iface *iface,
287                             struct ieee80211_mgmt *mgmt,
288                             struct ieee802_11_elems *elems,
289                             struct hostapd_frame_info *fi)
290 {
291         struct ap_info *ap;
292         int new_ap = 0;
293         size_t len;
294         int set_beacon = 0;
295
296         if (iface->conf->ap_table_max_size < 1)
297                 return;
298
299         ap = ap_get_ap(iface, mgmt->bssid);
300         if (!ap) {
301                 ap = ap_ap_add(iface, mgmt->bssid);
302                 if (!ap) {
303                         printf("Failed to allocate AP information entry\n");
304                         return;
305                 }
306                 new_ap = 1;
307         }
308
309         ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
310         ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
311
312         if (elems->ssid) {
313                 len = elems->ssid_len;
314                 if (len >= sizeof(ap->ssid))
315                         len = sizeof(ap->ssid) - 1;
316                 os_memcpy(ap->ssid, elems->ssid, len);
317                 ap->ssid[len] = '\0';
318                 ap->ssid_len = len;
319         }
320
321         os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
322         len = 0;
323         if (elems->supp_rates) {
324                 len = elems->supp_rates_len;
325                 if (len > WLAN_SUPP_RATES_MAX)
326                         len = WLAN_SUPP_RATES_MAX;
327                 os_memcpy(ap->supported_rates, elems->supp_rates, len);
328         }
329         if (elems->ext_supp_rates) {
330                 int len2;
331                 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
332                         len2 = WLAN_SUPP_RATES_MAX - len;
333                 else
334                         len2 = elems->ext_supp_rates_len;
335                 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
336                           len2);
337         }
338
339         ap->wpa = elems->wpa_ie != NULL;
340
341         if (elems->erp_info && elems->erp_info_len == 1)
342                 ap->erp = elems->erp_info[0];
343         else
344                 ap->erp = -1;
345
346         if (elems->ds_params && elems->ds_params_len == 1)
347                 ap->channel = elems->ds_params[0];
348         else if (fi)
349                 ap->channel = fi->channel;
350
351         if (elems->ht_capabilities)
352                 ap->ht_support = 1;
353         else
354                 ap->ht_support = 0;
355
356         ap->num_beacons++;
357         time(&ap->last_beacon);
358         if (fi) {
359                 ap->phytype = fi->phytype;
360                 ap->ssi_signal = fi->ssi_signal;
361                 ap->datarate = fi->datarate;
362         }
363
364         if (new_ap) {
365                 if (iface->conf->passive_scan_interval > 0)
366                         ap_list_new_ap(iface, ap);
367         } else if (ap != iface->ap_list) {
368                 /* move AP entry into the beginning of the list so that the
369                  * oldest entry is always in the end of the list */
370                 ap_ap_list_del(iface, ap);
371                 ap_ap_list_add(iface, ap);
372         }
373
374         if (!iface->olbc &&
375             ap_list_beacon_olbc(iface, ap)) {
376                 iface->olbc = 1;
377                 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
378                            "protection", MAC2STR(ap->addr));
379                 set_beacon++;
380         }
381
382 #ifdef CONFIG_IEEE80211N
383         if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
384                 iface->olbc_ht = 1;
385                 hostapd_ht_operation_update(iface);
386                 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
387                            " - enable protection", MAC2STR(ap->addr));
388                 set_beacon++;
389         }
390 #endif /* CONFIG_IEEE80211N */
391
392         if (set_beacon)
393                 ieee802_11_set_beacons(iface);
394 }
395
396
397 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
398 {
399         struct hostapd_iface *iface = eloop_ctx;
400         time_t now;
401         struct ap_info *ap;
402         int set_beacon = 0;
403
404         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
405
406         if (!iface->ap_list)
407                 return;
408
409         time(&now);
410
411         /* FIX: it looks like jkm-Purina ended up in busy loop in this
412          * function. Apparently, something can still cause a loop in the AP
413          * list.. */
414
415         while (iface->ap_list) {
416                 ap = iface->ap_list->prev;
417                 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
418                     now)
419                         break;
420
421                 if (iface->conf->passive_scan_interval > 0)
422                         ap_list_expired_ap(iface, ap);
423                 ap_free_ap(iface, ap);
424         }
425
426         if (iface->olbc || iface->olbc_ht) {
427                 int olbc = 0;
428                 int olbc_ht = 0;
429
430                 ap = iface->ap_list;
431                 while (ap && (olbc == 0 || olbc_ht == 0)) {
432                         if (ap_list_beacon_olbc(iface, ap))
433                                 olbc = 1;
434 #ifdef CONFIG_IEEE80211N
435                         if (ap_list_beacon_olbc_ht(iface, ap))
436                                 olbc_ht = 1;
437 #endif /* CONFIG_IEEE80211N */
438                         ap = ap->next;
439                 }
440                 if (!olbc && iface->olbc) {
441                         wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
442                         iface->olbc = 0;
443                         set_beacon++;
444                 }
445 #ifdef CONFIG_IEEE80211N
446                 if (!olbc_ht && iface->olbc_ht) {
447                         wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
448                         iface->olbc_ht = 0;
449                         hostapd_ht_operation_update(iface);
450                         set_beacon++;
451                 }
452 #endif /* CONFIG_IEEE80211N */
453         }
454
455         if (set_beacon)
456                 ieee802_11_set_beacons(iface);
457 }
458
459
460 int ap_list_init(struct hostapd_iface *iface)
461 {
462         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
463         return 0;
464 }
465
466
467 void ap_list_deinit(struct hostapd_iface *iface)
468 {
469         eloop_cancel_timeout(ap_list_timer, iface, NULL);
470         hostapd_free_aps(iface);
471 }
472
473
474 int ap_list_reconfig(struct hostapd_iface *iface,
475                      struct hostapd_config *oldconf)
476 {
477         time_t now;
478         struct ap_info *ap;
479
480         if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
481             iface->conf->ap_table_expiration_time ==
482             oldconf->ap_table_expiration_time)
483                 return 0;
484
485         time(&now);
486
487         while (iface->ap_list) {
488                 ap = iface->ap_list->prev;
489                 if (iface->num_ap <= iface->conf->ap_table_max_size &&
490                     ap->last_beacon + iface->conf->ap_table_expiration_time >=
491                     now)
492                         break;
493
494                 if (iface->conf->passive_scan_interval > 0)
495                         ap_list_expired_ap(iface, iface->ap_list->prev);
496                 ap_free_ap(iface, iface->ap_list->prev);
497         }
498
499         return 0;
500 }