Merge commit 'garage/master'
[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 int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
76 {
77         int i;
78
79         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
80             ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
81             iface->conf->channel != ap->channel)
82                 return 0;
83
84         if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
85                 return 1;
86
87         for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
88                 int rate = (ap->supported_rates[i] & 0x7f) * 5;
89                 if (rate == 60 || rate == 90 || rate > 110)
90                         return 0;
91         }
92
93         return 1;
94 }
95
96
97 #ifdef CONFIG_IEEE80211N
98 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
99                                   struct ap_info *ap)
100 {
101         return !ap->ht_support;
102 }
103 #endif /* CONFIG_IEEE80211N */
104
105
106 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
107 {
108         struct ap_info *s;
109
110         s = iface->ap_hash[STA_HASH(ap)];
111         while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
112                 s = s->hnext;
113         return s;
114 }
115
116
117 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
118 {
119         if (iface->ap_list) {
120                 ap->prev = iface->ap_list->prev;
121                 iface->ap_list->prev = ap;
122         } else
123                 ap->prev = ap;
124         ap->next = iface->ap_list;
125         iface->ap_list = ap;
126 }
127
128
129 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
130 {
131         if (iface->ap_list == ap)
132                 iface->ap_list = ap->next;
133         else
134                 ap->prev->next = ap->next;
135
136         if (ap->next)
137                 ap->next->prev = ap->prev;
138         else if (iface->ap_list)
139                 iface->ap_list->prev = ap->prev;
140 }
141
142
143 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
144                                 struct ap_info *ap)
145 {
146         if (iface->ap_iter_list) {
147                 ap->iter_prev = iface->ap_iter_list->iter_prev;
148                 iface->ap_iter_list->iter_prev = ap;
149         } else
150                 ap->iter_prev = ap;
151         ap->iter_next = iface->ap_iter_list;
152         iface->ap_iter_list = ap;
153 }
154
155
156 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
157                                 struct ap_info *ap)
158 {
159         if (iface->ap_iter_list == ap)
160                 iface->ap_iter_list = ap->iter_next;
161         else
162                 ap->iter_prev->iter_next = ap->iter_next;
163
164         if (ap->iter_next)
165                 ap->iter_next->iter_prev = ap->iter_prev;
166         else if (iface->ap_iter_list)
167                 iface->ap_iter_list->iter_prev = ap->iter_prev;
168 }
169
170
171 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
172 {
173         ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
174         iface->ap_hash[STA_HASH(ap->addr)] = ap;
175 }
176
177
178 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
179 {
180         struct ap_info *s;
181
182         s = iface->ap_hash[STA_HASH(ap->addr)];
183         if (s == NULL) return;
184         if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
185                 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
186                 return;
187         }
188
189         while (s->hnext != NULL &&
190                os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
191                 s = s->hnext;
192         if (s->hnext != NULL)
193                 s->hnext = s->hnext->hnext;
194         else
195                 printf("AP: could not remove AP " MACSTR " from hash table\n",
196                        MAC2STR(ap->addr));
197 }
198
199
200 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
201 {
202         ap_ap_hash_del(iface, ap);
203         ap_ap_list_del(iface, ap);
204         ap_ap_iter_list_del(iface, ap);
205
206         iface->num_ap--;
207         os_free(ap);
208 }
209
210
211 static void hostapd_free_aps(struct hostapd_iface *iface)
212 {
213         struct ap_info *ap, *prev;
214
215         ap = iface->ap_list;
216
217         while (ap) {
218                 prev = ap;
219                 ap = ap->next;
220                 ap_free_ap(iface, prev);
221         }
222
223         iface->ap_list = NULL;
224 }
225
226
227 int ap_ap_for_each(struct hostapd_iface *iface,
228                    int (*func)(struct ap_info *s, void *data), void *data)
229 {
230         struct ap_info *s;
231         int ret = 0;
232
233         s = iface->ap_list;
234
235         while (s) {
236                 ret = func(s, data);
237                 if (ret)
238                         break;
239                 s = s->next;
240         }
241
242         return ret;
243 }
244
245
246 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
247 {
248         struct ap_info *ap;
249
250         ap = os_zalloc(sizeof(struct ap_info));
251         if (ap == NULL)
252                 return NULL;
253
254         /* initialize AP info data */
255         os_memcpy(ap->addr, addr, ETH_ALEN);
256         ap_ap_list_add(iface, ap);
257         iface->num_ap++;
258         ap_ap_hash_add(iface, ap);
259         ap_ap_iter_list_add(iface, ap);
260
261         if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
262                 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
263                            MACSTR " from AP table", MAC2STR(ap->prev->addr));
264                 ap_free_ap(iface, ap->prev);
265         }
266
267         return ap;
268 }
269
270
271 void ap_list_process_beacon(struct hostapd_iface *iface,
272                             struct ieee80211_mgmt *mgmt,
273                             struct ieee802_11_elems *elems,
274                             struct hostapd_frame_info *fi)
275 {
276         struct ap_info *ap;
277         int new_ap = 0;
278         size_t len;
279         int set_beacon = 0;
280
281         if (iface->conf->ap_table_max_size < 1)
282                 return;
283
284         ap = ap_get_ap(iface, mgmt->bssid);
285         if (!ap) {
286                 ap = ap_ap_add(iface, mgmt->bssid);
287                 if (!ap) {
288                         printf("Failed to allocate AP information entry\n");
289                         return;
290                 }
291                 new_ap = 1;
292         }
293
294         ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
295         ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
296
297         if (elems->ssid) {
298                 len = elems->ssid_len;
299                 if (len >= sizeof(ap->ssid))
300                         len = sizeof(ap->ssid) - 1;
301                 os_memcpy(ap->ssid, elems->ssid, len);
302                 ap->ssid[len] = '\0';
303                 ap->ssid_len = len;
304         }
305
306         os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
307         len = 0;
308         if (elems->supp_rates) {
309                 len = elems->supp_rates_len;
310                 if (len > WLAN_SUPP_RATES_MAX)
311                         len = WLAN_SUPP_RATES_MAX;
312                 os_memcpy(ap->supported_rates, elems->supp_rates, len);
313         }
314         if (elems->ext_supp_rates) {
315                 int len2;
316                 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
317                         len2 = WLAN_SUPP_RATES_MAX - len;
318                 else
319                         len2 = elems->ext_supp_rates_len;
320                 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
321                           len2);
322         }
323
324         ap->wpa = elems->wpa_ie != NULL;
325
326         if (elems->erp_info && elems->erp_info_len == 1)
327                 ap->erp = elems->erp_info[0];
328         else
329                 ap->erp = -1;
330
331         if (elems->ds_params && elems->ds_params_len == 1)
332                 ap->channel = elems->ds_params[0];
333         else if (fi)
334                 ap->channel = fi->channel;
335
336         if (elems->ht_capabilities)
337                 ap->ht_support = 1;
338         else
339                 ap->ht_support = 0;
340
341         ap->num_beacons++;
342         time(&ap->last_beacon);
343         if (fi) {
344                 ap->phytype = fi->phytype;
345                 ap->ssi_signal = fi->ssi_signal;
346                 ap->datarate = fi->datarate;
347         }
348
349         if (!new_ap && ap != iface->ap_list) {
350                 /* move AP entry into the beginning of the list so that the
351                  * oldest entry is always in the end of the list */
352                 ap_ap_list_del(iface, ap);
353                 ap_ap_list_add(iface, ap);
354         }
355
356         if (!iface->olbc &&
357             ap_list_beacon_olbc(iface, ap)) {
358                 iface->olbc = 1;
359                 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
360                            "protection", MAC2STR(ap->addr));
361                 set_beacon++;
362         }
363
364 #ifdef CONFIG_IEEE80211N
365         if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
366                 iface->olbc_ht = 1;
367                 hostapd_ht_operation_update(iface);
368                 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
369                            " - enable protection", MAC2STR(ap->addr));
370                 set_beacon++;
371         }
372 #endif /* CONFIG_IEEE80211N */
373
374         if (set_beacon)
375                 ieee802_11_set_beacons(iface);
376 }
377
378
379 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
380 {
381         struct hostapd_iface *iface = eloop_ctx;
382         time_t now;
383         struct ap_info *ap;
384         int set_beacon = 0;
385
386         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
387
388         if (!iface->ap_list)
389                 return;
390
391         time(&now);
392
393         while (iface->ap_list) {
394                 ap = iface->ap_list->prev;
395                 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
396                     now)
397                         break;
398
399                 ap_free_ap(iface, ap);
400         }
401
402         if (iface->olbc || iface->olbc_ht) {
403                 int olbc = 0;
404                 int olbc_ht = 0;
405
406                 ap = iface->ap_list;
407                 while (ap && (olbc == 0 || olbc_ht == 0)) {
408                         if (ap_list_beacon_olbc(iface, ap))
409                                 olbc = 1;
410 #ifdef CONFIG_IEEE80211N
411                         if (ap_list_beacon_olbc_ht(iface, ap))
412                                 olbc_ht = 1;
413 #endif /* CONFIG_IEEE80211N */
414                         ap = ap->next;
415                 }
416                 if (!olbc && iface->olbc) {
417                         wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
418                         iface->olbc = 0;
419                         set_beacon++;
420                 }
421 #ifdef CONFIG_IEEE80211N
422                 if (!olbc_ht && iface->olbc_ht) {
423                         wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
424                         iface->olbc_ht = 0;
425                         hostapd_ht_operation_update(iface);
426                         set_beacon++;
427                 }
428 #endif /* CONFIG_IEEE80211N */
429         }
430
431         if (set_beacon)
432                 ieee802_11_set_beacons(iface);
433 }
434
435
436 int ap_list_init(struct hostapd_iface *iface)
437 {
438         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
439         return 0;
440 }
441
442
443 void ap_list_deinit(struct hostapd_iface *iface)
444 {
445         eloop_cancel_timeout(ap_list_timer, iface, NULL);
446         hostapd_free_aps(iface);
447 }
448
449
450 int ap_list_reconfig(struct hostapd_iface *iface,
451                      struct hostapd_config *oldconf)
452 {
453         time_t now;
454         struct ap_info *ap;
455
456         if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
457             iface->conf->ap_table_expiration_time ==
458             oldconf->ap_table_expiration_time)
459                 return 0;
460
461         time(&now);
462
463         while (iface->ap_list) {
464                 ap = iface->ap_list->prev;
465                 if (iface->num_ap <= iface->conf->ap_table_max_size &&
466                     ap->last_beacon + iface->conf->ap_table_expiration_time >=
467                     now)
468                         break;
469
470                 ap_free_ap(iface, iface->ap_list->prev);
471         }
472
473         return 0;
474 }