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