2 * hostapd / Neighboring APs DB
3 * Copyright(c) 2013 - 2016 Intel Mobile Communications GmbH.
4 * Copyright(c) 2011 - 2016 Intel Corporation. All rights reserved.
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
14 #include "ieee802_11.h"
15 #include "neighbor_db.h"
18 struct hostapd_neighbor_entry *
19 hostapd_neighbor_get(struct hostapd_data *hapd, const u8 *bssid,
20 const struct wpa_ssid_value *ssid)
22 struct hostapd_neighbor_entry *nr;
24 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
26 if (os_memcmp(bssid, nr->bssid, ETH_ALEN) == 0 &&
28 (ssid->ssid_len == nr->ssid.ssid_len &&
29 os_memcmp(ssid->ssid, nr->ssid.ssid,
30 ssid->ssid_len) == 0)))
37 static void hostapd_neighbor_clear_entry(struct hostapd_neighbor_entry *nr)
43 wpabuf_free(nr->civic);
45 os_memset(nr->bssid, 0, sizeof(nr->bssid));
46 os_memset(&nr->ssid, 0, sizeof(nr->ssid));
51 static struct hostapd_neighbor_entry *
52 hostapd_neighbor_add(struct hostapd_data *hapd)
54 struct hostapd_neighbor_entry *nr;
56 nr = os_zalloc(sizeof(struct hostapd_neighbor_entry));
60 dl_list_add(&hapd->nr_db, &nr->list);
66 int hostapd_neighbor_set(struct hostapd_data *hapd, const u8 *bssid,
67 const struct wpa_ssid_value *ssid,
68 const struct wpabuf *nr, const struct wpabuf *lci,
69 const struct wpabuf *civic, int stationary)
71 struct hostapd_neighbor_entry *entry;
73 entry = hostapd_neighbor_get(hapd, bssid, ssid);
75 entry = hostapd_neighbor_add(hapd);
79 hostapd_neighbor_clear_entry(entry);
81 os_memcpy(entry->bssid, bssid, ETH_ALEN);
82 os_memcpy(&entry->ssid, ssid, sizeof(entry->ssid));
84 entry->nr = wpabuf_dup(nr);
88 if (lci && wpabuf_len(lci)) {
89 entry->lci = wpabuf_dup(lci);
90 if (!entry->lci || os_get_time(&entry->lci_date))
94 if (civic && wpabuf_len(civic)) {
95 entry->civic = wpabuf_dup(civic);
100 entry->stationary = stationary;
105 hostapd_neighbor_remove(hapd, bssid, ssid);
110 int hostapd_neighbor_remove(struct hostapd_data *hapd, const u8 *bssid,
111 const struct wpa_ssid_value *ssid)
113 struct hostapd_neighbor_entry *nr;
115 nr = hostapd_neighbor_get(hapd, bssid, ssid);
119 hostapd_neighbor_clear_entry(nr);
120 dl_list_del(&nr->list);
127 void hostapd_free_neighbor_db(struct hostapd_data *hapd)
129 struct hostapd_neighbor_entry *nr, *prev;
131 dl_list_for_each_safe(nr, prev, &hapd->nr_db,
132 struct hostapd_neighbor_entry, list) {
133 hostapd_neighbor_clear_entry(nr);
134 dl_list_del(&nr->list);
141 static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
145 return NR_CHAN_WIDTH_20;
146 if (!hapd->iconf->secondary_channel)
147 return NR_CHAN_WIDTH_20;
148 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
149 return NR_CHAN_WIDTH_40;
150 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
151 return NR_CHAN_WIDTH_80;
152 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
153 return NR_CHAN_WIDTH_160;
154 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
155 return NR_CHAN_WIDTH_80P80;
156 return NR_CHAN_WIDTH_20;
158 #endif /* NEED_AP_MLME */
161 void hostapd_neighbor_set_own_report(struct hostapd_data *hapd)
164 u16 capab = hostapd_own_capab_info(hapd);
165 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
166 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
167 struct wpa_ssid_value ssid;
168 u8 channel, op_class;
169 u8 center_freq1_idx = 0, center_freq2_idx = 0;
170 enum nr_chan_width width;
174 if (!(hapd->conf->radio_measurements[0] &
175 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
178 bssid_info = 3; /* AP is reachable */
179 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
180 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
182 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
183 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
185 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
187 if (hapd->conf->wmm_enabled) {
188 bssid_info |= NEI_REP_BSSID_INFO_QOS;
190 if (hapd->conf->wmm_uapsd &&
191 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
192 bssid_info |= NEI_REP_BSSID_INFO_APSD;
196 bssid_info |= NEI_REP_BSSID_INFO_HT |
197 NEI_REP_BSSID_INFO_DELAYED_BA;
199 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
201 bssid_info |= NEI_REP_BSSID_INFO_VHT;
204 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
206 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
207 hapd->iconf->secondary_channel,
208 hapd->iconf->vht_oper_chwidth,
209 &op_class, &channel) ==
212 width = hostapd_get_nr_chan_width(hapd, ht, vht);
214 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
215 if (width == NR_CHAN_WIDTH_80P80)
217 hapd->iconf->vht_oper_centr_freq_seg1_idx;
219 ieee80211_freq_to_chan(hapd->iface->freq +
220 10 * hapd->iconf->secondary_channel,
224 ssid.ssid_len = hapd->conf->ssid.ssid_len;
225 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
228 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
229 * phy type + wide bandwidth channel subelement.
231 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
235 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
236 wpabuf_put_le32(nr, bssid_info);
237 wpabuf_put_u8(nr, op_class);
238 wpabuf_put_u8(nr, channel);
239 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
242 * Wide Bandwidth Channel subelement may be needed to allow the
243 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
246 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
247 wpabuf_put_u8(nr, 3);
248 wpabuf_put_u8(nr, width);
249 wpabuf_put_u8(nr, center_freq1_idx);
250 wpabuf_put_u8(nr, center_freq2_idx);
252 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
253 hapd->iconf->civic, hapd->iconf->stationary_ap);
256 #endif /* NEED_AP_MLME */