mac80211: cache mesh beacon
[linux-block.git] / net / mac80211 / mesh.c
CommitLineData
2e3c8736 1/*
264d9b7d 2 * Copyright (c) 2008, 2009 open80211s Ltd.
2e3c8736
LCC
3 * Authors: Luis Carlos Cobo <luisca@cozybit.com>
4 * Javier Cardona <javier@cozybit.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
5a0e3ad6 11#include <linux/slab.h>
51ceddad 12#include <asm/unaligned.h>
2e3c8736
LCC
13#include "ieee80211_i.h"
14#include "mesh.h"
15
5bb644a0
JB
16#define TMR_RUNNING_HK 0
17#define TMR_RUNNING_MP 1
e304bfd3 18#define TMR_RUNNING_MPR 2
5bb644a0 19
2e3c8736
LCC
20int mesh_allocated;
21static struct kmem_cache *rm_cache;
22
25d49e4d
TP
23bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
24{
25 return (mgmt->u.action.u.mesh_action.action_code ==
26 WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
27}
25d49e4d 28
2e3c8736
LCC
29void ieee80211s_init(void)
30{
31 mesh_pathtbl_init();
32 mesh_allocated = 1;
33 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
34 0, 0, NULL);
35}
36
37void ieee80211s_stop(void)
38{
39 mesh_pathtbl_unregister();
40 kmem_cache_destroy(rm_cache);
41}
42
472dbc45
JB
43static void ieee80211_mesh_housekeeping_timer(unsigned long data)
44{
45 struct ieee80211_sub_if_data *sdata = (void *) data;
46 struct ieee80211_local *local = sdata->local;
47 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
48
6b9ac442 49 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
5bb644a0
JB
50
51 if (local->quiescing) {
52 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
53 return;
54 }
55
64592c8f 56 ieee80211_queue_work(&local->hw, &sdata->work);
472dbc45
JB
57}
58
2e3c8736
LCC
59/**
60 * mesh_matches_local - check if the config of a mesh point matches ours
61 *
f698d856 62 * @sdata: local mesh subif
f743ff49 63 * @ie: information elements of a management frame from the mesh peer
2e3c8736
LCC
64 *
65 * This function checks if the mesh configuration of a mesh point matches the
66 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
67 */
f743ff49
TP
68bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
69 struct ieee802_11_elems *ie)
2e3c8736 70{
472dbc45 71 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
739522ba 72 struct ieee80211_local *local = sdata->local;
f743ff49 73 u32 basic_rates = 0;
4bf88530 74 struct cfg80211_chan_def sta_chan_def;
2e3c8736 75
2e3c8736
LCC
76 /*
77 * As support for each feature is added, check for matching
78 * - On mesh config capabilities
79 * - Power Save Support En
80 * - Sync support enabled
81 * - Sync support active
82 * - Sync support required from peer
83 * - MDA enabled
84 * - Power management control on fc
85 */
739522ba
TP
86 if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
87 memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
88 (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
89 (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
90 (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
91 (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
92 (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
93 goto mismatch;
94
55de908a 95 ieee80211_sta_get_rates(local, ie, ieee80211_get_sdata_band(sdata),
f743ff49
TP
96 &basic_rates);
97
fe40cb62
AN
98 if (sdata->vif.bss_conf.basic_rates != basic_rates)
99 goto mismatch;
100
4bf88530
JB
101 ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
102 ie->ht_operation, &sta_chan_def);
103
104 if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
105 &sta_chan_def))
739522ba 106 goto mismatch;
2e3c8736 107
739522ba
TP
108 return true;
109mismatch:
2e3c8736
LCC
110 return false;
111}
112
113/**
114 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
115 *
116 * @ie: information elements of a management frame from the mesh peer
2e3c8736 117 */
f698d856 118bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
2e3c8736 119{
136cfa28 120 return (ie->mesh_config->meshconf_cap &
65821635 121 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
2e3c8736
LCC
122}
123
124/**
2c53040f 125 * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
2e3c8736 126 *
d0709a65 127 * @sdata: mesh interface in which mesh beacons are going to be updated
df323818
MP
128 *
129 * Returns: beacon changed flag if the beacon content changed.
2e3c8736 130 */
df323818 131u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
2e3c8736 132{
2e3c8736 133 bool free_plinks;
df323818 134 u32 changed = 0;
2e3c8736
LCC
135
136 /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
137 * the mesh interface might be able to establish plinks with peers that
b4e08ea1
LCC
138 * are already on the table but are not on PLINK_ESTAB state. However,
139 * in general the mesh interface is not accepting peer link requests
140 * from new peers, and that must be reflected in the beacon
2e3c8736
LCC
141 */
142 free_plinks = mesh_plink_availables(sdata);
143
df323818
MP
144 if (free_plinks != sdata->u.mesh.accepting_plinks) {
145 sdata->u.mesh.accepting_plinks = free_plinks;
146 changed = BSS_CHANGED_BEACON;
147 }
148
149 return changed;
2e3c8736
LCC
150}
151
45b5028e
TP
152/*
153 * mesh_sta_cleanup - clean up any mesh sta state
154 *
155 * @sta: mesh sta to clean up.
156 */
157void mesh_sta_cleanup(struct sta_info *sta)
158{
159 struct ieee80211_sub_if_data *sdata = sta->sdata;
160 u32 changed;
161
162 /*
163 * maybe userspace handles peer allocation and peering, but in either
164 * case the beacon is still generated by the kernel and we might need
165 * an update.
166 */
167 changed = mesh_accept_plinks_update(sdata);
168 if (sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
169 changed |= mesh_plink_deactivate(sta);
170 del_timer_sync(&sta->plink_timer);
171 }
172
173 if (changed)
2b5e1967 174 ieee80211_mbss_info_change_notify(sdata, changed);
45b5028e
TP
175}
176
f698d856 177int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
2e3c8736 178{
2e3c8736
LCC
179 int i;
180
472dbc45
JB
181 sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
182 if (!sdata->u.mesh.rmc)
2e3c8736 183 return -ENOMEM;
472dbc45 184 sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
2e3c8736 185 for (i = 0; i < RMC_BUCKETS; i++)
b7cfcd11 186 INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
2e3c8736
LCC
187 return 0;
188}
189
f698d856 190void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
2e3c8736 191{
472dbc45 192 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
2e3c8736
LCC
193 struct rmc_entry *p, *n;
194 int i;
195
472dbc45 196 if (!sdata->u.mesh.rmc)
2e3c8736
LCC
197 return;
198
199 for (i = 0; i < RMC_BUCKETS; i++)
b7cfcd11 200 list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
2e3c8736
LCC
201 list_del(&p->list);
202 kmem_cache_free(rm_cache, p);
203 }
204
205 kfree(rmc);
472dbc45 206 sdata->u.mesh.rmc = NULL;
2e3c8736
LCC
207}
208
209/**
210 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
211 *
212 * @sa: source address
213 * @mesh_hdr: mesh_header
214 *
215 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
216 *
217 * Checks using the source address and the mesh sequence number if we have
218 * received this frame lately. If the frame is not in the cache, it is added to
219 * it.
220 */
221int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
f698d856 222 struct ieee80211_sub_if_data *sdata)
2e3c8736 223{
472dbc45 224 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
2e3c8736
LCC
225 u32 seqnum = 0;
226 int entries = 0;
227 u8 idx;
228 struct rmc_entry *p, *n;
229
230 /* Don't care about endianness since only match matters */
51ceddad
LCC
231 memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
232 idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
b7cfcd11 233 list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
2e3c8736
LCC
234 ++entries;
235 if (time_after(jiffies, p->exp_time) ||
236 (entries == RMC_QUEUE_MAX_LEN)) {
237 list_del(&p->list);
238 kmem_cache_free(rm_cache, p);
239 --entries;
f64f9e71 240 } else if ((seqnum == p->seqnum) &&
b203ca39 241 (ether_addr_equal(sa, p->sa)))
2e3c8736
LCC
242 return -1;
243 }
244
245 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
d15b8459 246 if (!p)
2e3c8736 247 return 0;
d15b8459 248
2e3c8736
LCC
249 p->seqnum = seqnum;
250 p->exp_time = jiffies + RMC_TIMEOUT;
251 memcpy(p->sa, sa, ETH_ALEN);
b7cfcd11 252 list_add(&p->list, &rmc->bucket[idx]);
2e3c8736
LCC
253 return 0;
254}
255
082ebb0c
TP
256int
257mesh_add_meshconf_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
258{
259 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
260 u8 *pos, neighbors;
261 u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
262
263 if (skb_tailroom(skb) < 2 + meshconf_len)
264 return -ENOMEM;
265
266 pos = skb_put(skb, 2 + meshconf_len);
267 *pos++ = WLAN_EID_MESH_CONFIG;
268 *pos++ = meshconf_len;
269
270 /* Active path selection protocol ID */
271 *pos++ = ifmsh->mesh_pp_id;
272 /* Active path selection metric ID */
273 *pos++ = ifmsh->mesh_pm_id;
274 /* Congestion control mode identifier */
275 *pos++ = ifmsh->mesh_cc_id;
276 /* Synchronization protocol identifier */
277 *pos++ = ifmsh->mesh_sp_id;
278 /* Authentication Protocol identifier */
279 *pos++ = ifmsh->mesh_auth_id;
280 /* Mesh Formation Info - number of neighbors */
1258d976 281 neighbors = atomic_read(&ifmsh->estab_plinks);
082ebb0c
TP
282 /* Number of neighbor mesh STAs or 15 whichever is smaller */
283 neighbors = (neighbors > 15) ? 15 : neighbors;
284 *pos++ = neighbors << 1;
285 /* Mesh capability */
65821635 286 *pos = IEEE80211_MESHCONF_CAPAB_FORWARDING;
dbf498fb 287 *pos |= ifmsh->accepting_plinks ?
65821635 288 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
3f52b7e3
MP
289 /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
290 *pos |= ifmsh->ps_peers_deep_sleep ?
291 IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
dbf498fb 292 *pos++ |= ifmsh->adjusting_tbtt ?
65821635 293 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
082ebb0c
TP
294 *pos++ = 0x00;
295
296 return 0;
297}
298
299int
300mesh_add_meshid_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
301{
302 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
303 u8 *pos;
304
305 if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
306 return -ENOMEM;
307
308 pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
309 *pos++ = WLAN_EID_MESH_ID;
310 *pos++ = ifmsh->mesh_id_len;
311 if (ifmsh->mesh_id_len)
312 memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
313
314 return 0;
315}
316
3f52b7e3
MP
317int mesh_add_awake_window_ie(struct sk_buff *skb,
318 struct ieee80211_sub_if_data *sdata)
319{
320 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
321 u8 *pos;
322
323 /* see IEEE802.11-2012 13.14.6 */
324 if (ifmsh->ps_peers_light_sleep == 0 &&
325 ifmsh->ps_peers_deep_sleep == 0 &&
326 ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
327 return 0;
328
329 if (skb_tailroom(skb) < 4)
330 return -ENOMEM;
331
332 pos = skb_put(skb, 2 + 2);
333 *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
334 *pos++ = 2;
335 put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
336
337 return 0;
338}
339
082ebb0c
TP
340int
341mesh_add_vendor_ies(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
342{
343 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
344 u8 offset, len;
345 const u8 *data;
346
347 if (!ifmsh->ie || !ifmsh->ie_len)
348 return 0;
349
350 /* fast-forward to vendor IEs */
351 offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
352
353 if (offset) {
354 len = ifmsh->ie_len - offset;
355 data = ifmsh->ie + offset;
356 if (skb_tailroom(skb) < len)
357 return -ENOMEM;
358 memcpy(skb_put(skb, len), data, len);
359 }
360
361 return 0;
362}
363
364int
365mesh_add_rsn_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
366{
367 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
368 u8 len = 0;
369 const u8 *data;
370
371 if (!ifmsh->ie || !ifmsh->ie_len)
372 return 0;
373
374 /* find RSN IE */
375 data = ifmsh->ie;
376 while (data < ifmsh->ie + ifmsh->ie_len) {
377 if (*data == WLAN_EID_RSN) {
378 len = data[1] + 2;
379 break;
380 }
381 data++;
382 }
383
384 if (len) {
385 if (skb_tailroom(skb) < len)
386 return -ENOMEM;
387 memcpy(skb_put(skb, len), data, len);
388 }
389
390 return 0;
391}
392
082ebb0c
TP
393int mesh_add_ds_params_ie(struct sk_buff *skb,
394 struct ieee80211_sub_if_data *sdata)
395{
55de908a
JB
396 struct ieee80211_chanctx_conf *chanctx_conf;
397 struct ieee80211_channel *chan;
082ebb0c 398 u8 *pos;
2e3c8736 399
082ebb0c
TP
400 if (skb_tailroom(skb) < 3)
401 return -ENOMEM;
402
55de908a
JB
403 rcu_read_lock();
404 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
405 if (WARN_ON(!chanctx_conf)) {
406 rcu_read_unlock();
407 return -EINVAL;
408 }
4bf88530 409 chan = chanctx_conf->def.chan;
55de908a
JB
410 rcu_read_unlock();
411
601513aa
ET
412 pos = skb_put(skb, 2 + 1);
413 *pos++ = WLAN_EID_DS_PARAMS;
414 *pos++ = 1;
415 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
be125c60 416
082ebb0c 417 return 0;
2e3c8736
LCC
418}
419
176f3608
TP
420int mesh_add_ht_cap_ie(struct sk_buff *skb,
421 struct ieee80211_sub_if_data *sdata)
422{
423 struct ieee80211_local *local = sdata->local;
55de908a 424 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
176f3608
TP
425 struct ieee80211_supported_band *sband;
426 u8 *pos;
427
55de908a 428 sband = local->hw.wiphy->bands[band];
176f3608 429 if (!sband->ht_cap.ht_supported ||
4bf88530 430 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
176f3608
TP
431 return 0;
432
433 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
434 return -ENOMEM;
435
436 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
ef96a842 437 ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
176f3608
TP
438
439 return 0;
440}
441
074d46d1 442int mesh_add_ht_oper_ie(struct sk_buff *skb,
176f3608
TP
443 struct ieee80211_sub_if_data *sdata)
444{
445 struct ieee80211_local *local = sdata->local;
55de908a
JB
446 struct ieee80211_chanctx_conf *chanctx_conf;
447 struct ieee80211_channel *channel;
466f310d 448 enum nl80211_channel_type channel_type =
4bf88530 449 cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
55de908a
JB
450 struct ieee80211_supported_band *sband;
451 struct ieee80211_sta_ht_cap *ht_cap;
176f3608
TP
452 u8 *pos;
453
55de908a
JB
454 rcu_read_lock();
455 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
456 if (WARN_ON(!chanctx_conf)) {
457 rcu_read_unlock();
458 return -EINVAL;
459 }
4bf88530 460 channel = chanctx_conf->def.chan;
55de908a
JB
461 rcu_read_unlock();
462
463 sband = local->hw.wiphy->bands[channel->band];
464 ht_cap = &sband->ht_cap;
465
176f3608
TP
466 if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
467 return 0;
468
074d46d1 469 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
176f3608
TP
470 return -ENOMEM;
471
074d46d1 472 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
4bf88530 473 ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
431e3154 474 sdata->vif.bss_conf.ht_operation_mode);
176f3608
TP
475
476 return 0;
477}
2e3c8736
LCC
478static void ieee80211_mesh_path_timer(unsigned long data)
479{
480 struct ieee80211_sub_if_data *sdata =
481 (struct ieee80211_sub_if_data *) data;
472dbc45 482 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
133b8226 483 struct ieee80211_local *local = sdata->local;
2e3c8736 484
5bb644a0
JB
485 if (local->quiescing) {
486 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
487 return;
488 }
489
64592c8f 490 ieee80211_queue_work(&local->hw, &sdata->work);
2e3c8736
LCC
491}
492
e304bfd3
RP
493static void ieee80211_mesh_path_root_timer(unsigned long data)
494{
495 struct ieee80211_sub_if_data *sdata =
496 (struct ieee80211_sub_if_data *) data;
497 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
498 struct ieee80211_local *local = sdata->local;
499
500 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
501
502 if (local->quiescing) {
503 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
504 return;
505 }
506
64592c8f 507 ieee80211_queue_work(&local->hw, &sdata->work);
e304bfd3
RP
508}
509
63c5723b
RP
510void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
511{
dbb912cd 512 if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
63c5723b
RP
513 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
514 else {
515 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
516 /* stop running timer */
517 del_timer_sync(&ifmsh->mesh_path_root_timer);
518 }
519}
520
3c5772a5
JC
521/**
522 * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
523 * @hdr: 802.11 frame header
524 * @fc: frame control field
525 * @meshda: destination address in the mesh
526 * @meshsa: source address address in the mesh. Same as TA, as frame is
527 * locally originated.
528 *
529 * Return the length of the 802.11 (does not include a mesh control header)
530 */
15ff6365
JB
531int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
532 const u8 *meshda, const u8 *meshsa)
533{
3c5772a5
JC
534 if (is_multicast_ether_addr(meshda)) {
535 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
536 /* DA TA SA */
537 memcpy(hdr->addr1, meshda, ETH_ALEN);
538 memcpy(hdr->addr2, meshsa, ETH_ALEN);
539 memcpy(hdr->addr3, meshsa, ETH_ALEN);
540 return 24;
541 } else {
2154c81c 542 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
3c5772a5
JC
543 /* RA TA DA SA */
544 memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
545 memcpy(hdr->addr2, meshsa, ETH_ALEN);
546 memcpy(hdr->addr3, meshda, ETH_ALEN);
547 memcpy(hdr->addr4, meshsa, ETH_ALEN);
548 return 30;
549 }
550}
551
902acc78
JB
552/**
553 * ieee80211_new_mesh_header - create a new mesh header
554 * @meshhdr: uninitialized mesh header
555 * @sdata: mesh interface to be used
61ad5394
JC
556 * @addr4or5: 1st address in the ae header, which may correspond to address 4
557 * (if addr6 is NULL) or address 5 (if addr6 is present). It may
558 * be NULL.
559 * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
560 * mesh frame
902acc78
JB
561 *
562 * Return the header length.
563 */
564int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
61ad5394
JC
565 struct ieee80211_sub_if_data *sdata, char *addr4or5,
566 char *addr6)
902acc78 567{
3c5772a5 568 int aelen = 0;
61ad5394 569 BUG_ON(!addr4or5 && addr6);
0c3cee72 570 memset(meshhdr, 0, sizeof(*meshhdr));
472dbc45
JB
571 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
572 put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
573 sdata->u.mesh.mesh_seqnum++;
61ad5394 574 if (addr4or5 && !addr6) {
3c5772a5
JC
575 meshhdr->flags |= MESH_FLAGS_AE_A4;
576 aelen += ETH_ALEN;
61ad5394
JC
577 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
578 } else if (addr4or5 && addr6) {
3c5772a5
JC
579 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
580 aelen += 2 * ETH_ALEN;
61ad5394
JC
581 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
582 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
3c5772a5
JC
583 }
584 return 6 + aelen;
902acc78
JB
585}
586
472dbc45
JB
587static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
588 struct ieee80211_if_mesh *ifmsh)
589{
df323818 590 u32 changed;
472dbc45 591
472dbc45
JB
592 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
593 mesh_path_expire(sdata);
594
df323818 595 changed = mesh_accept_plinks_update(sdata);
2b5e1967 596 ieee80211_mbss_info_change_notify(sdata, changed);
472dbc45 597
472dbc45
JB
598 mod_timer(&ifmsh->housekeeping_timer,
599 round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
600}
601
e304bfd3
RP
602static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
603{
604 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
a69cc44f 605 u32 interval;
e304bfd3
RP
606
607 mesh_path_tx_root_frame(sdata);
a69cc44f
CYY
608
609 if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
610 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
611 else
612 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
613
e304bfd3 614 mod_timer(&ifmsh->mesh_path_root_timer,
a69cc44f 615 round_jiffies(TU_TO_EXP_TIME(interval)));
e304bfd3
RP
616}
617
5bb644a0
JB
618#ifdef CONFIG_PM
619void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
620{
621 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
622
5ee68e5b 623 /* use atomic bitops in case all timers fire at the same time */
5bb644a0
JB
624
625 if (del_timer_sync(&ifmsh->housekeeping_timer))
626 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
627 if (del_timer_sync(&ifmsh->mesh_path_timer))
628 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
e304bfd3
RP
629 if (del_timer_sync(&ifmsh->mesh_path_root_timer))
630 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
5bb644a0
JB
631}
632
633void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
634{
635 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
636
637 if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
638 add_timer(&ifmsh->housekeeping_timer);
639 if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
640 add_timer(&ifmsh->mesh_path_timer);
e304bfd3
RP
641 if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
642 add_timer(&ifmsh->mesh_path_root_timer);
63c5723b 643 ieee80211_mesh_root_setup(ifmsh);
5bb644a0
JB
644}
645#endif
472dbc45 646
2b5e1967
TP
647static int
648ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
649{
650 struct beacon_data *bcn;
651 int head_len, tail_len;
652 struct sk_buff *skb;
653 struct ieee80211_mgmt *mgmt;
654 struct ieee80211_chanctx_conf *chanctx_conf;
655 enum ieee80211_band band;
656 u8 *pos;
657 struct ieee80211_sub_if_data *sdata;
658 int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
659 sizeof(mgmt->u.beacon);
660
661 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
662 rcu_read_lock();
663 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
664 band = chanctx_conf->def.chan->band;
665 rcu_read_unlock();
666
667 head_len = hdr_len +
668 2 + /* NULL SSID */
669 2 + 8 + /* supported rates */
670 2 + 3; /* DS params */
671 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
672 2 + sizeof(struct ieee80211_ht_cap) +
673 2 + sizeof(struct ieee80211_ht_operation) +
674 2 + ifmsh->mesh_id_len +
675 2 + sizeof(struct ieee80211_meshconf_ie) +
676 2 + sizeof(__le16) + /* awake window */
677 ifmsh->ie_len;
678
679 bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
680 /* need an skb for IE builders to operate on */
681 skb = dev_alloc_skb(max(head_len, tail_len));
682
683 if (!bcn || !skb)
684 goto out_free;
685
686 /*
687 * pointers go into the block we allocated,
688 * memory is | beacon_data | head | tail |
689 */
690 bcn->head = ((u8 *) bcn) + sizeof(*bcn);
691
692 /* fill in the head */
693 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
694 memset(mgmt, 0, hdr_len);
695 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
696 IEEE80211_STYPE_BEACON);
697 eth_broadcast_addr(mgmt->da);
698 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
699 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
700 ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
701 mgmt->u.beacon.beacon_int =
702 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
703 mgmt->u.beacon.capab_info |= cpu_to_le16(
704 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
705
706 pos = skb_put(skb, 2);
707 *pos++ = WLAN_EID_SSID;
708 *pos++ = 0x0;
709
710 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
711 mesh_add_ds_params_ie(skb, sdata))
712 goto out_free;
713
714 bcn->head_len = skb->len;
715 memcpy(bcn->head, skb->data, bcn->head_len);
716
717 /* now the tail */
718 skb_trim(skb, 0);
719 bcn->tail = bcn->head + bcn->head_len;
720
721 if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
722 mesh_add_rsn_ie(skb, sdata) ||
723 mesh_add_ht_cap_ie(skb, sdata) ||
724 mesh_add_ht_oper_ie(skb, sdata) ||
725 mesh_add_meshid_ie(skb, sdata) ||
726 mesh_add_meshconf_ie(skb, sdata) ||
727 mesh_add_awake_window_ie(skb, sdata) ||
728 mesh_add_vendor_ies(skb, sdata))
729 goto out_free;
730
731 bcn->tail_len = skb->len;
732 memcpy(bcn->tail, skb->data, bcn->tail_len);
733
734 dev_kfree_skb(skb);
735 rcu_assign_pointer(ifmsh->beacon, bcn);
736 return 0;
737out_free:
738 kfree(bcn);
739 dev_kfree_skb(skb);
740 return -ENOMEM;
741}
742
743static int
744ieee80211_mesh_rebuild_beacon(struct ieee80211_if_mesh *ifmsh)
745{
746 struct ieee80211_sub_if_data *sdata;
747 struct beacon_data *old_bcn;
748 int ret;
749 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
750
751 mutex_lock(&ifmsh->mtx);
752
753 old_bcn = rcu_dereference_protected(ifmsh->beacon,
754 lockdep_is_held(&ifmsh->mtx));
755 ret = ieee80211_mesh_build_beacon(ifmsh);
756 if (ret)
757 /* just reuse old beacon */
758 goto out;
759
760 if (old_bcn)
761 kfree_rcu(old_bcn, rcu_head);
762out:
763 mutex_unlock(&ifmsh->mtx);
764 return ret;
765}
766
767void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
768 u32 changed)
769{
770 if (sdata->vif.bss_conf.enable_beacon &&
771 (changed & (BSS_CHANGED_BEACON |
772 BSS_CHANGED_HT |
773 BSS_CHANGED_BASIC_RATES |
774 BSS_CHANGED_BEACON_INT)))
775 if (ieee80211_mesh_rebuild_beacon(&sdata->u.mesh))
776 return;
777 ieee80211_bss_info_change_notify(sdata, changed);
778}
779
780int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
472dbc45
JB
781{
782 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
783 struct ieee80211_local *local = sdata->local;
f4eabc91
CYY
784 u32 changed = BSS_CHANGED_BEACON |
785 BSS_CHANGED_BEACON_ENABLED |
786 BSS_CHANGED_HT |
787 BSS_CHANGED_BASIC_RATES |
788 BSS_CHANGED_BEACON_INT;
789 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
472dbc45 790
09b17470
JB
791 local->fif_other_bss++;
792 /* mesh ifaces must set allmulti to forward mcast traffic */
793 atomic_inc(&local->iff_allmultis);
794 ieee80211_configure_filter(local);
795
c7108a71 796 ifmsh->mesh_cc_id = 0; /* Disabled */
c7108a71 797 ifmsh->mesh_auth_id = 0; /* Disabled */
dbf498fb
JC
798 /* register sync ops from extensible synchronization framework */
799 ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
800 ifmsh->adjusting_tbtt = false;
801 ifmsh->sync_offset_clockdrift_max = 0;
6b9ac442 802 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
63c5723b 803 ieee80211_mesh_root_setup(ifmsh);
64592c8f 804 ieee80211_queue_work(&local->hw, &sdata->work);
70c33eaa
AN
805 sdata->vif.bss_conf.ht_operation_mode =
806 ifmsh->mshcfg.ht_opmode;
d6a83228 807 sdata->vif.bss_conf.enable_beacon = true;
d934f7d0 808 sdata->vif.bss_conf.basic_rates =
f4eabc91
CYY
809 ieee80211_mandatory_rates(local, band);
810
39886b61 811 changed |= ieee80211_mps_local_status_update(sdata);
3f52b7e3 812
2b5e1967
TP
813 if (ieee80211_mesh_build_beacon(ifmsh)) {
814 ieee80211_stop_mesh(sdata);
815 return -ENOMEM;
816 }
817
f4eabc91 818 ieee80211_bss_info_change_notify(sdata, changed);
c405c629
JB
819
820 netif_carrier_on(sdata->dev);
2b5e1967 821 return 0;
472dbc45
JB
822}
823
824void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
825{
09b17470 826 struct ieee80211_local *local = sdata->local;
29cbe68c 827 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2b5e1967 828 struct beacon_data *bcn;
29cbe68c 829
c405c629
JB
830 netif_carrier_off(sdata->dev);
831
0d466b9c 832 /* stop the beacon */
29cbe68c 833 ifmsh->mesh_id_len = 0;
d6a83228
JB
834 sdata->vif.bss_conf.enable_beacon = false;
835 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
29cbe68c 836 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
2b5e1967
TP
837 mutex_lock(&ifmsh->mtx);
838 bcn = rcu_dereference_protected(ifmsh->beacon,
839 lockdep_is_held(&ifmsh->mtx));
840 rcu_assign_pointer(ifmsh->beacon, NULL);
841 kfree_rcu(bcn, rcu_head);
842 mutex_unlock(&ifmsh->mtx);
0d466b9c
TP
843
844 /* flush STAs and mpaths on this iface */
b998e8bb 845 sta_info_flush(sdata);
0d466b9c 846 mesh_path_flush_by_iface(sdata);
09b17470 847
3f52b7e3
MP
848 /* free all potentially still buffered group-addressed frames */
849 local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
850 skb_queue_purge(&ifmsh->ps.bc_buf);
851
472dbc45 852 del_timer_sync(&sdata->u.mesh.housekeeping_timer);
e304bfd3 853 del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
dd4c9260 854 del_timer_sync(&sdata->u.mesh.mesh_path_timer);
b7413430
JB
855 /*
856 * If the timer fired while we waited for it, it will have
857 * requeued the work. Now the work will be running again
858 * but will not rearm the timer again because it checks
859 * whether the interface is running, which, at this point,
860 * it no longer is.
861 */
64592c8f 862 cancel_work_sync(&sdata->work);
09b17470
JB
863
864 local->fif_other_bss--;
865 atomic_dec(&local->iff_allmultis);
866 ieee80211_configure_filter(local);
2d9957cc
JB
867
868 sdata->u.mesh.timers_running = 0;
472dbc45
JB
869}
870
871static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
872 u16 stype,
873 struct ieee80211_mgmt *mgmt,
874 size_t len,
875 struct ieee80211_rx_status *rx_status)
876{
c6a1fa12 877 struct ieee80211_local *local = sdata->local;
dbf498fb 878 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
472dbc45
JB
879 struct ieee802_11_elems elems;
880 struct ieee80211_channel *channel;
472dbc45
JB
881 size_t baselen;
882 int freq;
883 enum ieee80211_band band = rx_status->band;
884
885 /* ignore ProbeResp to foreign address */
886 if (stype == IEEE80211_STYPE_PROBE_RESP &&
b203ca39 887 !ether_addr_equal(mgmt->da, sdata->vif.addr))
472dbc45
JB
888 return;
889
890 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
891 if (baselen > len)
892 return;
893
894 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
895 &elems);
896
9a90bc81
TP
897 /* ignore non-mesh or secure / unsecure mismatch */
898 if ((!elems.mesh_id || !elems.mesh_config) ||
899 (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
900 (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
5cff5e01
JC
901 return;
902
472dbc45 903 if (elems.ds_params && elems.ds_params_len == 1)
59eb21a6 904 freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
472dbc45
JB
905 else
906 freq = rx_status->freq;
907
908 channel = ieee80211_get_channel(local->hw.wiphy, freq);
909
910 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
911 return;
912
9a90bc81 913 if (mesh_matches_local(sdata, &elems))
f743ff49 914 mesh_neighbour_update(sdata, mgmt->sa, &elems);
dbf498fb
JC
915
916 if (ifmsh->sync_ops)
917 ifmsh->sync_ops->rx_bcn_presp(sdata,
918 stype, mgmt, &elems, rx_status);
472dbc45
JB
919}
920
921static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
922 struct ieee80211_mgmt *mgmt,
923 size_t len,
924 struct ieee80211_rx_status *rx_status)
925{
926 switch (mgmt->u.action.category) {
8db09850
TP
927 case WLAN_CATEGORY_SELF_PROTECTED:
928 switch (mgmt->u.action.u.self_prot.action_code) {
929 case WLAN_SP_MESH_PEERING_OPEN:
930 case WLAN_SP_MESH_PEERING_CLOSE:
931 case WLAN_SP_MESH_PEERING_CONFIRM:
932 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
933 break;
934 }
472dbc45 935 break;
25d49e4d
TP
936 case WLAN_CATEGORY_MESH_ACTION:
937 if (mesh_action_is_path_sel(mgmt))
938 mesh_rx_path_sel_frame(sdata, mgmt, len);
472dbc45
JB
939 break;
940 }
941}
942
1fa57d01
JB
943void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
944 struct sk_buff *skb)
472dbc45
JB
945{
946 struct ieee80211_rx_status *rx_status;
472dbc45
JB
947 struct ieee80211_mgmt *mgmt;
948 u16 stype;
949
f1d58c25 950 rx_status = IEEE80211_SKB_RXCB(skb);
472dbc45
JB
951 mgmt = (struct ieee80211_mgmt *) skb->data;
952 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
953
954 switch (stype) {
955 case IEEE80211_STYPE_PROBE_RESP:
956 case IEEE80211_STYPE_BEACON:
957 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
958 rx_status);
959 break;
960 case IEEE80211_STYPE_ACTION:
961 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
962 break;
963 }
472dbc45
JB
964}
965
1fa57d01 966void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
472dbc45 967{
472dbc45 968 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
472dbc45
JB
969
970 if (ifmsh->preq_queue_len &&
971 time_after(jiffies,
972 ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
973 mesh_path_start_discovery(sdata);
974
18889231
JC
975 if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
976 mesh_mpath_table_grow();
977
dcac908b 978 if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
18889231
JC
979 mesh_mpp_table_grow();
980
981 if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
472dbc45 982 ieee80211_mesh_housekeeping(sdata, ifmsh);
e304bfd3
RP
983
984 if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
985 ieee80211_mesh_rootpath(sdata);
dbf498fb
JC
986
987 if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
988 mesh_sync_adjust_tbtt(sdata);
472dbc45
JB
989}
990
991void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
992{
993 struct ieee80211_sub_if_data *sdata;
994
995 rcu_read_lock();
996 list_for_each_entry_rcu(sdata, &local->interfaces, list)
997 if (ieee80211_vif_is_mesh(&sdata->vif))
64592c8f 998 ieee80211_queue_work(&local->hw, &sdata->work);
472dbc45
JB
999 rcu_read_unlock();
1000}
1001
902acc78
JB
1002void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1003{
472dbc45 1004 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
ad2d223a 1005 static u8 zero_addr[ETH_ALEN] = {};
472dbc45 1006
472dbc45
JB
1007 setup_timer(&ifmsh->housekeeping_timer,
1008 ieee80211_mesh_housekeeping_timer,
1009 (unsigned long) sdata);
472dbc45 1010
472dbc45
JB
1011 ifmsh->accepting_plinks = true;
1012 ifmsh->preq_id = 0;
d19b3bf6 1013 ifmsh->sn = 0;
5ee68e5b 1014 ifmsh->num_gates = 0;
472dbc45 1015 atomic_set(&ifmsh->mpaths, 0);
f698d856 1016 mesh_rmc_init(sdata);
472dbc45 1017 ifmsh->last_preq = jiffies;
dca7e943 1018 ifmsh->next_perr = jiffies;
902acc78
JB
1019 /* Allocate all mesh structures when creating the first mesh interface. */
1020 if (!mesh_allocated)
1021 ieee80211s_init();
472dbc45 1022 setup_timer(&ifmsh->mesh_path_timer,
902acc78
JB
1023 ieee80211_mesh_path_timer,
1024 (unsigned long) sdata);
e304bfd3
RP
1025 setup_timer(&ifmsh->mesh_path_root_timer,
1026 ieee80211_mesh_path_root_timer,
1027 (unsigned long) sdata);
472dbc45 1028 INIT_LIST_HEAD(&ifmsh->preq_queue.list);
3f52b7e3 1029 skb_queue_head_init(&ifmsh->ps.bc_buf);
472dbc45 1030 spin_lock_init(&ifmsh->mesh_preq_queue_lock);
dbf498fb 1031 spin_lock_init(&ifmsh->sync_offset_lock);
2b5e1967
TP
1032 RCU_INIT_POINTER(ifmsh->beacon, NULL);
1033 mutex_init(&ifmsh->mtx);
ad2d223a
JB
1034
1035 sdata->vif.bss_conf.bssid = zero_addr;
472dbc45 1036}