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