2 * Copyright (c) 2008, 2009 open80211s Ltd.
3 * Authors: Luis Carlos Cobo <luisca@cozybit.com>
4 * Javier Cardona <javier@cozybit.com>
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.
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
13 #include "ieee80211_i.h"
16 #define TMR_RUNNING_HK 0
17 #define TMR_RUNNING_MP 1
18 #define TMR_RUNNING_MPR 2
20 static int mesh_allocated
;
21 static struct kmem_cache
*rm_cache
;
23 bool mesh_action_is_path_sel(struct ieee80211_mgmt
*mgmt
)
25 return (mgmt
->u
.action
.u
.mesh_action
.action_code
==
26 WLAN_MESH_ACTION_HWMP_PATH_SELECTION
);
29 void ieee80211s_init(void)
33 rm_cache
= kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry
),
37 void ieee80211s_stop(void)
41 mesh_pathtbl_unregister();
42 kmem_cache_destroy(rm_cache
);
45 static void ieee80211_mesh_housekeeping_timer(unsigned long data
)
47 struct ieee80211_sub_if_data
*sdata
= (void *) data
;
48 struct ieee80211_local
*local
= sdata
->local
;
49 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
51 set_bit(MESH_WORK_HOUSEKEEPING
, &ifmsh
->wrkq_flags
);
53 if (local
->quiescing
) {
54 set_bit(TMR_RUNNING_HK
, &ifmsh
->timers_running
);
58 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
62 * mesh_matches_local - check if the config of a mesh point matches ours
64 * @sdata: local mesh subif
65 * @ie: information elements of a management frame from the mesh peer
67 * This function checks if the mesh configuration of a mesh point matches the
68 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
70 bool mesh_matches_local(struct ieee80211_sub_if_data
*sdata
,
71 struct ieee802_11_elems
*ie
)
73 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
74 struct ieee80211_local
*local
= sdata
->local
;
76 struct cfg80211_chan_def sta_chan_def
;
79 * As support for each feature is added, check for matching
80 * - On mesh config capabilities
81 * - Power Save Support En
82 * - Sync support enabled
83 * - Sync support active
84 * - Sync support required from peer
86 * - Power management control on fc
88 if (!(ifmsh
->mesh_id_len
== ie
->mesh_id_len
&&
89 memcmp(ifmsh
->mesh_id
, ie
->mesh_id
, ie
->mesh_id_len
) == 0 &&
90 (ifmsh
->mesh_pp_id
== ie
->mesh_config
->meshconf_psel
) &&
91 (ifmsh
->mesh_pm_id
== ie
->mesh_config
->meshconf_pmetric
) &&
92 (ifmsh
->mesh_cc_id
== ie
->mesh_config
->meshconf_congest
) &&
93 (ifmsh
->mesh_sp_id
== ie
->mesh_config
->meshconf_synch
) &&
94 (ifmsh
->mesh_auth_id
== ie
->mesh_config
->meshconf_auth
)))
97 ieee80211_sta_get_rates(local
, ie
, ieee80211_get_sdata_band(sdata
),
100 if (sdata
->vif
.bss_conf
.basic_rates
!= basic_rates
)
103 ieee80211_ht_oper_to_chandef(sdata
->vif
.bss_conf
.chandef
.chan
,
104 ie
->ht_operation
, &sta_chan_def
);
106 if (!cfg80211_chandef_compatible(&sdata
->vif
.bss_conf
.chandef
,
114 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
116 * @ie: information elements of a management frame from the mesh peer
118 bool mesh_peer_accepts_plinks(struct ieee802_11_elems
*ie
)
120 return (ie
->mesh_config
->meshconf_cap
&
121 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS
) != 0;
125 * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
127 * @sdata: mesh interface in which mesh beacons are going to be updated
129 * Returns: beacon changed flag if the beacon content changed.
131 u32
mesh_accept_plinks_update(struct ieee80211_sub_if_data
*sdata
)
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
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
142 free_plinks
= mesh_plink_availables(sdata
);
144 if (free_plinks
!= sdata
->u
.mesh
.accepting_plinks
) {
145 sdata
->u
.mesh
.accepting_plinks
= free_plinks
;
146 changed
= BSS_CHANGED_BEACON
;
153 * mesh_sta_cleanup - clean up any mesh sta state
155 * @sta: mesh sta to clean up.
157 void mesh_sta_cleanup(struct sta_info
*sta
)
159 struct ieee80211_sub_if_data
*sdata
= sta
->sdata
;
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
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
);
174 ieee80211_mbss_info_change_notify(sdata
, changed
);
177 int mesh_rmc_init(struct ieee80211_sub_if_data
*sdata
)
181 sdata
->u
.mesh
.rmc
= kmalloc(sizeof(struct mesh_rmc
), GFP_KERNEL
);
182 if (!sdata
->u
.mesh
.rmc
)
184 sdata
->u
.mesh
.rmc
->idx_mask
= RMC_BUCKETS
- 1;
185 for (i
= 0; i
< RMC_BUCKETS
; i
++)
186 INIT_LIST_HEAD(&sdata
->u
.mesh
.rmc
->bucket
[i
]);
190 void mesh_rmc_free(struct ieee80211_sub_if_data
*sdata
)
192 struct mesh_rmc
*rmc
= sdata
->u
.mesh
.rmc
;
193 struct rmc_entry
*p
, *n
;
196 if (!sdata
->u
.mesh
.rmc
)
199 for (i
= 0; i
< RMC_BUCKETS
; i
++) {
200 list_for_each_entry_safe(p
, n
, &rmc
->bucket
[i
], list
) {
202 kmem_cache_free(rm_cache
, p
);
207 sdata
->u
.mesh
.rmc
= NULL
;
211 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
214 * @sa: source address
215 * @mesh_hdr: mesh_header
217 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
219 * Checks using the source address and the mesh sequence number if we have
220 * received this frame lately. If the frame is not in the cache, it is added to
223 int mesh_rmc_check(struct ieee80211_sub_if_data
*sdata
,
224 const u8
*sa
, struct ieee80211s_hdr
*mesh_hdr
)
226 struct mesh_rmc
*rmc
= sdata
->u
.mesh
.rmc
;
230 struct rmc_entry
*p
, *n
;
232 /* Don't care about endianness since only match matters */
233 memcpy(&seqnum
, &mesh_hdr
->seqnum
, sizeof(mesh_hdr
->seqnum
));
234 idx
= le32_to_cpu(mesh_hdr
->seqnum
) & rmc
->idx_mask
;
235 list_for_each_entry_safe(p
, n
, &rmc
->bucket
[idx
], list
) {
237 if (time_after(jiffies
, p
->exp_time
) ||
238 entries
== RMC_QUEUE_MAX_LEN
) {
240 kmem_cache_free(rm_cache
, p
);
242 } else if ((seqnum
== p
->seqnum
) && ether_addr_equal(sa
, p
->sa
))
246 p
= kmem_cache_alloc(rm_cache
, GFP_ATOMIC
);
251 p
->exp_time
= jiffies
+ RMC_TIMEOUT
;
252 memcpy(p
->sa
, sa
, ETH_ALEN
);
253 list_add(&p
->list
, &rmc
->bucket
[idx
]);
257 int mesh_add_meshconf_ie(struct ieee80211_sub_if_data
*sdata
,
260 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
262 u8 meshconf_len
= sizeof(struct ieee80211_meshconf_ie
);
264 if (skb_tailroom(skb
) < 2 + meshconf_len
)
267 pos
= skb_put(skb
, 2 + meshconf_len
);
268 *pos
++ = WLAN_EID_MESH_CONFIG
;
269 *pos
++ = meshconf_len
;
271 /* Active path selection protocol ID */
272 *pos
++ = ifmsh
->mesh_pp_id
;
273 /* Active path selection metric ID */
274 *pos
++ = ifmsh
->mesh_pm_id
;
275 /* Congestion control mode identifier */
276 *pos
++ = ifmsh
->mesh_cc_id
;
277 /* Synchronization protocol identifier */
278 *pos
++ = ifmsh
->mesh_sp_id
;
279 /* Authentication Protocol identifier */
280 *pos
++ = ifmsh
->mesh_auth_id
;
281 /* Mesh Formation Info - number of neighbors */
282 neighbors
= atomic_read(&ifmsh
->estab_plinks
);
283 /* Number of neighbor mesh STAs or 15 whichever is smaller */
284 neighbors
= (neighbors
> 15) ? 15 : neighbors
;
285 *pos
++ = neighbors
<< 1;
286 /* Mesh capability */
287 *pos
= IEEE80211_MESHCONF_CAPAB_FORWARDING
;
288 *pos
|= ifmsh
->accepting_plinks
?
289 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS
: 0x00;
290 /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
291 *pos
|= ifmsh
->ps_peers_deep_sleep
?
292 IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL
: 0x00;
293 *pos
++ |= ifmsh
->adjusting_tbtt
?
294 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING
: 0x00;
300 int mesh_add_meshid_ie(struct ieee80211_sub_if_data
*sdata
, struct sk_buff
*skb
)
302 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
305 if (skb_tailroom(skb
) < 2 + ifmsh
->mesh_id_len
)
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
);
317 static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data
*sdata
,
320 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
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
)
329 if (skb_tailroom(skb
) < 4)
332 pos
= skb_put(skb
, 2 + 2);
333 *pos
++ = WLAN_EID_MESH_AWAKE_WINDOW
;
335 put_unaligned_le16(ifmsh
->mshcfg
.dot11MeshAwakeWindowDuration
, pos
);
340 int mesh_add_vendor_ies(struct ieee80211_sub_if_data
*sdata
,
343 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
347 if (!ifmsh
->ie
|| !ifmsh
->ie_len
)
350 /* fast-forward to vendor IEs */
351 offset
= ieee80211_ie_split_vendor(ifmsh
->ie
, ifmsh
->ie_len
, 0);
354 len
= ifmsh
->ie_len
- offset
;
355 data
= ifmsh
->ie
+ offset
;
356 if (skb_tailroom(skb
) < len
)
358 memcpy(skb_put(skb
, len
), data
, len
);
364 int mesh_add_rsn_ie(struct ieee80211_sub_if_data
*sdata
, struct sk_buff
*skb
)
366 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
370 if (!ifmsh
->ie
|| !ifmsh
->ie_len
)
375 while (data
< ifmsh
->ie
+ ifmsh
->ie_len
) {
376 if (*data
== WLAN_EID_RSN
) {
384 if (skb_tailroom(skb
) < len
)
386 memcpy(skb_put(skb
, len
), data
, len
);
392 static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data
*sdata
,
395 struct ieee80211_chanctx_conf
*chanctx_conf
;
396 struct ieee80211_channel
*chan
;
399 if (skb_tailroom(skb
) < 3)
403 chanctx_conf
= rcu_dereference(sdata
->vif
.chanctx_conf
);
404 if (WARN_ON(!chanctx_conf
)) {
408 chan
= chanctx_conf
->def
.chan
;
411 pos
= skb_put(skb
, 2 + 1);
412 *pos
++ = WLAN_EID_DS_PARAMS
;
414 *pos
++ = ieee80211_frequency_to_channel(chan
->center_freq
);
419 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data
*sdata
,
422 struct ieee80211_local
*local
= sdata
->local
;
423 enum ieee80211_band band
= ieee80211_get_sdata_band(sdata
);
424 struct ieee80211_supported_band
*sband
;
427 sband
= local
->hw
.wiphy
->bands
[band
];
428 if (!sband
->ht_cap
.ht_supported
||
429 sdata
->vif
.bss_conf
.chandef
.width
== NL80211_CHAN_WIDTH_20_NOHT
)
432 if (skb_tailroom(skb
) < 2 + sizeof(struct ieee80211_ht_cap
))
435 pos
= skb_put(skb
, 2 + sizeof(struct ieee80211_ht_cap
));
436 ieee80211_ie_build_ht_cap(pos
, &sband
->ht_cap
, sband
->ht_cap
.cap
);
441 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data
*sdata
,
444 struct ieee80211_local
*local
= sdata
->local
;
445 struct ieee80211_chanctx_conf
*chanctx_conf
;
446 struct ieee80211_channel
*channel
;
447 enum nl80211_channel_type channel_type
=
448 cfg80211_get_chandef_type(&sdata
->vif
.bss_conf
.chandef
);
449 struct ieee80211_supported_band
*sband
;
450 struct ieee80211_sta_ht_cap
*ht_cap
;
454 chanctx_conf
= rcu_dereference(sdata
->vif
.chanctx_conf
);
455 if (WARN_ON(!chanctx_conf
)) {
459 channel
= chanctx_conf
->def
.chan
;
462 sband
= local
->hw
.wiphy
->bands
[channel
->band
];
463 ht_cap
= &sband
->ht_cap
;
465 if (!ht_cap
->ht_supported
|| channel_type
== NL80211_CHAN_NO_HT
)
468 if (skb_tailroom(skb
) < 2 + sizeof(struct ieee80211_ht_operation
))
471 pos
= skb_put(skb
, 2 + sizeof(struct ieee80211_ht_operation
));
472 ieee80211_ie_build_ht_oper(pos
, ht_cap
, &sdata
->vif
.bss_conf
.chandef
,
473 sdata
->vif
.bss_conf
.ht_operation_mode
);
478 static void ieee80211_mesh_path_timer(unsigned long data
)
480 struct ieee80211_sub_if_data
*sdata
=
481 (struct ieee80211_sub_if_data
*) data
;
482 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
483 struct ieee80211_local
*local
= sdata
->local
;
485 if (local
->quiescing
) {
486 set_bit(TMR_RUNNING_MP
, &ifmsh
->timers_running
);
490 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
493 static void ieee80211_mesh_path_root_timer(unsigned long data
)
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
;
500 set_bit(MESH_WORK_ROOT
, &ifmsh
->wrkq_flags
);
502 if (local
->quiescing
) {
503 set_bit(TMR_RUNNING_MPR
, &ifmsh
->timers_running
);
507 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
510 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh
*ifmsh
)
512 if (ifmsh
->mshcfg
.dot11MeshHWMPRootMode
> IEEE80211_ROOTMODE_ROOT
)
513 set_bit(MESH_WORK_ROOT
, &ifmsh
->wrkq_flags
);
515 clear_bit(MESH_WORK_ROOT
, &ifmsh
->wrkq_flags
);
516 /* stop running timer */
517 del_timer_sync(&ifmsh
->mesh_path_root_timer
);
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.
529 * Return the length of the 802.11 (does not include a mesh control header)
531 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr
*hdr
, __le16
*fc
,
532 const u8
*meshda
, const u8
*meshsa
)
534 if (is_multicast_ether_addr(meshda
)) {
535 *fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
);
537 memcpy(hdr
->addr1
, meshda
, ETH_ALEN
);
538 memcpy(hdr
->addr2
, meshsa
, ETH_ALEN
);
539 memcpy(hdr
->addr3
, meshsa
, ETH_ALEN
);
542 *fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
| IEEE80211_FCTL_TODS
);
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
);
553 * ieee80211_new_mesh_header - create a new mesh header
554 * @sdata: mesh interface to be used
555 * @meshhdr: uninitialized mesh header
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
559 * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
562 * Return the header length.
564 int ieee80211_new_mesh_header(struct ieee80211_sub_if_data
*sdata
,
565 struct ieee80211s_hdr
*meshhdr
,
566 const char *addr4or5
, const char *addr6
)
568 if (WARN_ON(!addr4or5
&& addr6
))
571 memset(meshhdr
, 0, sizeof(*meshhdr
));
573 meshhdr
->ttl
= sdata
->u
.mesh
.mshcfg
.dot11MeshTTL
;
575 /* FIXME: racy -- TX on multiple queues can be concurrent */
576 put_unaligned(cpu_to_le32(sdata
->u
.mesh
.mesh_seqnum
), &meshhdr
->seqnum
);
577 sdata
->u
.mesh
.mesh_seqnum
++;
579 if (addr4or5
&& !addr6
) {
580 meshhdr
->flags
|= MESH_FLAGS_AE_A4
;
581 memcpy(meshhdr
->eaddr1
, addr4or5
, ETH_ALEN
);
583 } else if (addr4or5
&& addr6
) {
584 meshhdr
->flags
|= MESH_FLAGS_AE_A5_A6
;
585 memcpy(meshhdr
->eaddr1
, addr4or5
, ETH_ALEN
);
586 memcpy(meshhdr
->eaddr2
, addr6
, ETH_ALEN
);
593 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data
*sdata
)
595 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
598 ieee80211_sta_expire(sdata
, IEEE80211_MESH_PEER_INACTIVITY_LIMIT
);
599 mesh_path_expire(sdata
);
601 changed
= mesh_accept_plinks_update(sdata
);
602 ieee80211_mbss_info_change_notify(sdata
, changed
);
604 mod_timer(&ifmsh
->housekeeping_timer
,
605 round_jiffies(jiffies
+
606 IEEE80211_MESH_HOUSEKEEPING_INTERVAL
));
609 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data
*sdata
)
611 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
614 mesh_path_tx_root_frame(sdata
);
616 if (ifmsh
->mshcfg
.dot11MeshHWMPRootMode
== IEEE80211_PROACTIVE_RANN
)
617 interval
= ifmsh
->mshcfg
.dot11MeshHWMPRannInterval
;
619 interval
= ifmsh
->mshcfg
.dot11MeshHWMProotInterval
;
621 mod_timer(&ifmsh
->mesh_path_root_timer
,
622 round_jiffies(TU_TO_EXP_TIME(interval
)));
626 void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data
*sdata
)
628 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
630 /* use atomic bitops in case all timers fire at the same time */
632 if (del_timer_sync(&ifmsh
->housekeeping_timer
))
633 set_bit(TMR_RUNNING_HK
, &ifmsh
->timers_running
);
634 if (del_timer_sync(&ifmsh
->mesh_path_timer
))
635 set_bit(TMR_RUNNING_MP
, &ifmsh
->timers_running
);
636 if (del_timer_sync(&ifmsh
->mesh_path_root_timer
))
637 set_bit(TMR_RUNNING_MPR
, &ifmsh
->timers_running
);
640 void ieee80211_mesh_restart(struct ieee80211_sub_if_data
*sdata
)
642 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
644 if (test_and_clear_bit(TMR_RUNNING_HK
, &ifmsh
->timers_running
))
645 add_timer(&ifmsh
->housekeeping_timer
);
646 if (test_and_clear_bit(TMR_RUNNING_MP
, &ifmsh
->timers_running
))
647 add_timer(&ifmsh
->mesh_path_timer
);
648 if (test_and_clear_bit(TMR_RUNNING_MPR
, &ifmsh
->timers_running
))
649 add_timer(&ifmsh
->mesh_path_root_timer
);
650 ieee80211_mesh_root_setup(ifmsh
);
655 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh
*ifmsh
)
657 struct beacon_data
*bcn
;
658 int head_len
, tail_len
;
660 struct ieee80211_mgmt
*mgmt
;
661 struct ieee80211_chanctx_conf
*chanctx_conf
;
662 enum ieee80211_band band
;
664 struct ieee80211_sub_if_data
*sdata
;
665 int hdr_len
= offsetof(struct ieee80211_mgmt
, u
.beacon
) +
666 sizeof(mgmt
->u
.beacon
);
668 sdata
= container_of(ifmsh
, struct ieee80211_sub_if_data
, u
.mesh
);
670 chanctx_conf
= rcu_dereference(sdata
->vif
.chanctx_conf
);
671 band
= chanctx_conf
->def
.chan
->band
;
676 2 + 8 + /* supported rates */
677 2 + 3; /* DS params */
678 tail_len
= 2 + (IEEE80211_MAX_SUPP_RATES
- 8) +
679 2 + sizeof(struct ieee80211_ht_cap
) +
680 2 + sizeof(struct ieee80211_ht_operation
) +
681 2 + ifmsh
->mesh_id_len
+
682 2 + sizeof(struct ieee80211_meshconf_ie
) +
683 2 + sizeof(__le16
) + /* awake window */
686 bcn
= kzalloc(sizeof(*bcn
) + head_len
+ tail_len
, GFP_KERNEL
);
687 /* need an skb for IE builders to operate on */
688 skb
= dev_alloc_skb(max(head_len
, tail_len
));
694 * pointers go into the block we allocated,
695 * memory is | beacon_data | head | tail |
697 bcn
->head
= ((u8
*) bcn
) + sizeof(*bcn
);
699 /* fill in the head */
700 mgmt
= (struct ieee80211_mgmt
*) skb_put(skb
, hdr_len
);
701 memset(mgmt
, 0, hdr_len
);
702 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
703 IEEE80211_STYPE_BEACON
);
704 eth_broadcast_addr(mgmt
->da
);
705 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
706 memcpy(mgmt
->bssid
, sdata
->vif
.addr
, ETH_ALEN
);
707 ieee80211_mps_set_frame_flags(sdata
, NULL
, (void *) mgmt
);
708 mgmt
->u
.beacon
.beacon_int
=
709 cpu_to_le16(sdata
->vif
.bss_conf
.beacon_int
);
710 mgmt
->u
.beacon
.capab_info
|= cpu_to_le16(
711 sdata
->u
.mesh
.security
? WLAN_CAPABILITY_PRIVACY
: 0);
713 pos
= skb_put(skb
, 2);
714 *pos
++ = WLAN_EID_SSID
;
717 if (ieee80211_add_srates_ie(sdata
, skb
, true, band
) ||
718 mesh_add_ds_params_ie(sdata
, skb
))
721 bcn
->head_len
= skb
->len
;
722 memcpy(bcn
->head
, skb
->data
, bcn
->head_len
);
726 bcn
->tail
= bcn
->head
+ bcn
->head_len
;
728 if (ieee80211_add_ext_srates_ie(sdata
, skb
, true, band
) ||
729 mesh_add_rsn_ie(sdata
, skb
) ||
730 mesh_add_ht_cap_ie(sdata
, skb
) ||
731 mesh_add_ht_oper_ie(sdata
, skb
) ||
732 mesh_add_meshid_ie(sdata
, skb
) ||
733 mesh_add_meshconf_ie(sdata
, skb
) ||
734 mesh_add_awake_window_ie(sdata
, skb
) ||
735 mesh_add_vendor_ies(sdata
, skb
))
738 bcn
->tail_len
= skb
->len
;
739 memcpy(bcn
->tail
, skb
->data
, bcn
->tail_len
);
742 rcu_assign_pointer(ifmsh
->beacon
, bcn
);
751 ieee80211_mesh_rebuild_beacon(struct ieee80211_if_mesh
*ifmsh
)
753 struct ieee80211_sub_if_data
*sdata
;
754 struct beacon_data
*old_bcn
;
756 sdata
= container_of(ifmsh
, struct ieee80211_sub_if_data
, u
.mesh
);
758 mutex_lock(&ifmsh
->mtx
);
760 old_bcn
= rcu_dereference_protected(ifmsh
->beacon
,
761 lockdep_is_held(&ifmsh
->mtx
));
762 ret
= ieee80211_mesh_build_beacon(ifmsh
);
764 /* just reuse old beacon */
768 kfree_rcu(old_bcn
, rcu_head
);
770 mutex_unlock(&ifmsh
->mtx
);
774 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data
*sdata
,
777 if (sdata
->vif
.bss_conf
.enable_beacon
&&
778 (changed
& (BSS_CHANGED_BEACON
|
780 BSS_CHANGED_BASIC_RATES
|
781 BSS_CHANGED_BEACON_INT
)))
782 if (ieee80211_mesh_rebuild_beacon(&sdata
->u
.mesh
))
784 ieee80211_bss_info_change_notify(sdata
, changed
);
787 int ieee80211_start_mesh(struct ieee80211_sub_if_data
*sdata
)
789 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
790 struct ieee80211_local
*local
= sdata
->local
;
791 u32 changed
= BSS_CHANGED_BEACON
|
792 BSS_CHANGED_BEACON_ENABLED
|
794 BSS_CHANGED_BASIC_RATES
|
795 BSS_CHANGED_BEACON_INT
;
796 enum ieee80211_band band
= ieee80211_get_sdata_band(sdata
);
798 local
->fif_other_bss
++;
799 /* mesh ifaces must set allmulti to forward mcast traffic */
800 atomic_inc(&local
->iff_allmultis
);
801 ieee80211_configure_filter(local
);
803 ifmsh
->mesh_cc_id
= 0; /* Disabled */
804 ifmsh
->mesh_auth_id
= 0; /* Disabled */
805 /* register sync ops from extensible synchronization framework */
806 ifmsh
->sync_ops
= ieee80211_mesh_sync_ops_get(ifmsh
->mesh_sp_id
);
807 ifmsh
->adjusting_tbtt
= false;
808 ifmsh
->sync_offset_clockdrift_max
= 0;
809 set_bit(MESH_WORK_HOUSEKEEPING
, &ifmsh
->wrkq_flags
);
810 ieee80211_mesh_root_setup(ifmsh
);
811 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
812 sdata
->vif
.bss_conf
.ht_operation_mode
=
813 ifmsh
->mshcfg
.ht_opmode
;
814 sdata
->vif
.bss_conf
.enable_beacon
= true;
815 sdata
->vif
.bss_conf
.basic_rates
=
816 ieee80211_mandatory_rates(local
, band
);
818 changed
|= ieee80211_mps_local_status_update(sdata
);
820 if (ieee80211_mesh_build_beacon(ifmsh
)) {
821 ieee80211_stop_mesh(sdata
);
825 ieee80211_bss_info_change_notify(sdata
, changed
);
827 netif_carrier_on(sdata
->dev
);
831 void ieee80211_stop_mesh(struct ieee80211_sub_if_data
*sdata
)
833 struct ieee80211_local
*local
= sdata
->local
;
834 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
835 struct beacon_data
*bcn
;
837 netif_carrier_off(sdata
->dev
);
839 /* stop the beacon */
840 ifmsh
->mesh_id_len
= 0;
841 sdata
->vif
.bss_conf
.enable_beacon
= false;
842 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED
, &sdata
->state
);
843 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_BEACON_ENABLED
);
844 mutex_lock(&ifmsh
->mtx
);
845 bcn
= rcu_dereference_protected(ifmsh
->beacon
,
846 lockdep_is_held(&ifmsh
->mtx
));
847 rcu_assign_pointer(ifmsh
->beacon
, NULL
);
848 kfree_rcu(bcn
, rcu_head
);
849 mutex_unlock(&ifmsh
->mtx
);
851 /* flush STAs and mpaths on this iface */
852 sta_info_flush(sdata
);
853 mesh_path_flush_by_iface(sdata
);
855 /* free all potentially still buffered group-addressed frames */
856 local
->total_ps_buffered
-= skb_queue_len(&ifmsh
->ps
.bc_buf
);
857 skb_queue_purge(&ifmsh
->ps
.bc_buf
);
859 del_timer_sync(&sdata
->u
.mesh
.housekeeping_timer
);
860 del_timer_sync(&sdata
->u
.mesh
.mesh_path_root_timer
);
861 del_timer_sync(&sdata
->u
.mesh
.mesh_path_timer
);
863 * If the timer fired while we waited for it, it will have
864 * requeued the work. Now the work will be running again
865 * but will not rearm the timer again because it checks
866 * whether the interface is running, which, at this point,
869 cancel_work_sync(&sdata
->work
);
871 local
->fif_other_bss
--;
872 atomic_dec(&local
->iff_allmultis
);
873 ieee80211_configure_filter(local
);
875 sdata
->u
.mesh
.timers_running
= 0;
879 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data
*sdata
,
880 struct ieee80211_mgmt
*mgmt
, size_t len
)
882 struct ieee80211_local
*local
= sdata
->local
;
883 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
884 struct sk_buff
*presp
;
885 struct beacon_data
*bcn
;
886 struct ieee80211_mgmt
*hdr
;
887 struct ieee802_11_elems elems
;
891 end
= ((u8
*) mgmt
) + len
;
892 pos
= mgmt
->u
.probe_req
.variable
;
893 baselen
= (u8
*) pos
- (u8
*) mgmt
;
897 ieee802_11_parse_elems(pos
, len
- baselen
, &elems
);
899 /* 802.11-2012 10.1.4.3.2 */
900 if ((!ether_addr_equal(mgmt
->da
, sdata
->vif
.addr
) &&
901 !is_broadcast_ether_addr(mgmt
->da
)) ||
905 if (elems
.mesh_id_len
!= 0 &&
906 (elems
.mesh_id_len
!= ifmsh
->mesh_id_len
||
907 memcmp(elems
.mesh_id
, ifmsh
->mesh_id
, ifmsh
->mesh_id_len
)))
911 bcn
= rcu_dereference(ifmsh
->beacon
);
916 presp
= dev_alloc_skb(local
->tx_headroom
+
917 bcn
->head_len
+ bcn
->tail_len
);
921 skb_reserve(presp
, local
->tx_headroom
);
922 memcpy(skb_put(presp
, bcn
->head_len
), bcn
->head
, bcn
->head_len
);
923 memcpy(skb_put(presp
, bcn
->tail_len
), bcn
->tail
, bcn
->tail_len
);
924 hdr
= (struct ieee80211_mgmt
*) presp
->data
;
925 hdr
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
926 IEEE80211_STYPE_PROBE_RESP
);
927 memcpy(hdr
->da
, mgmt
->sa
, ETH_ALEN
);
928 IEEE80211_SKB_CB(presp
)->flags
|= IEEE80211_TX_INTFL_DONT_ENCRYPT
;
929 ieee80211_tx_skb(sdata
, presp
);
934 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data
*sdata
,
936 struct ieee80211_mgmt
*mgmt
,
938 struct ieee80211_rx_status
*rx_status
)
940 struct ieee80211_local
*local
= sdata
->local
;
941 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
942 struct ieee802_11_elems elems
;
943 struct ieee80211_channel
*channel
;
946 enum ieee80211_band band
= rx_status
->band
;
948 /* ignore ProbeResp to foreign address */
949 if (stype
== IEEE80211_STYPE_PROBE_RESP
&&
950 !ether_addr_equal(mgmt
->da
, sdata
->vif
.addr
))
953 baselen
= (u8
*) mgmt
->u
.probe_resp
.variable
- (u8
*) mgmt
;
957 ieee802_11_parse_elems(mgmt
->u
.probe_resp
.variable
, len
- baselen
,
960 /* ignore non-mesh or secure / unsecure mismatch */
961 if ((!elems
.mesh_id
|| !elems
.mesh_config
) ||
962 (elems
.rsn
&& sdata
->u
.mesh
.security
== IEEE80211_MESH_SEC_NONE
) ||
963 (!elems
.rsn
&& sdata
->u
.mesh
.security
!= IEEE80211_MESH_SEC_NONE
))
966 if (elems
.ds_params
&& elems
.ds_params_len
== 1)
967 freq
= ieee80211_channel_to_frequency(elems
.ds_params
[0], band
);
969 freq
= rx_status
->freq
;
971 channel
= ieee80211_get_channel(local
->hw
.wiphy
, freq
);
973 if (!channel
|| channel
->flags
& IEEE80211_CHAN_DISABLED
)
976 if (mesh_matches_local(sdata
, &elems
))
977 mesh_neighbour_update(sdata
, mgmt
->sa
, &elems
);
980 ifmsh
->sync_ops
->rx_bcn_presp(sdata
,
981 stype
, mgmt
, &elems
, rx_status
);
984 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data
*sdata
,
985 struct ieee80211_mgmt
*mgmt
,
987 struct ieee80211_rx_status
*rx_status
)
989 switch (mgmt
->u
.action
.category
) {
990 case WLAN_CATEGORY_SELF_PROTECTED
:
991 switch (mgmt
->u
.action
.u
.self_prot
.action_code
) {
992 case WLAN_SP_MESH_PEERING_OPEN
:
993 case WLAN_SP_MESH_PEERING_CLOSE
:
994 case WLAN_SP_MESH_PEERING_CONFIRM
:
995 mesh_rx_plink_frame(sdata
, mgmt
, len
, rx_status
);
999 case WLAN_CATEGORY_MESH_ACTION
:
1000 if (mesh_action_is_path_sel(mgmt
))
1001 mesh_rx_path_sel_frame(sdata
, mgmt
, len
);
1006 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data
*sdata
,
1007 struct sk_buff
*skb
)
1009 struct ieee80211_rx_status
*rx_status
;
1010 struct ieee80211_mgmt
*mgmt
;
1013 rx_status
= IEEE80211_SKB_RXCB(skb
);
1014 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
1015 stype
= le16_to_cpu(mgmt
->frame_control
) & IEEE80211_FCTL_STYPE
;
1018 case IEEE80211_STYPE_PROBE_RESP
:
1019 case IEEE80211_STYPE_BEACON
:
1020 ieee80211_mesh_rx_bcn_presp(sdata
, stype
, mgmt
, skb
->len
,
1023 case IEEE80211_STYPE_PROBE_REQ
:
1024 ieee80211_mesh_rx_probe_req(sdata
, mgmt
, skb
->len
);
1026 case IEEE80211_STYPE_ACTION
:
1027 ieee80211_mesh_rx_mgmt_action(sdata
, mgmt
, skb
->len
, rx_status
);
1032 void ieee80211_mesh_work(struct ieee80211_sub_if_data
*sdata
)
1034 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
1036 if (ifmsh
->preq_queue_len
&&
1038 ifmsh
->last_preq
+ msecs_to_jiffies(ifmsh
->mshcfg
.dot11MeshHWMPpreqMinInterval
)))
1039 mesh_path_start_discovery(sdata
);
1041 if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE
, &ifmsh
->wrkq_flags
))
1042 mesh_mpath_table_grow();
1044 if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE
, &ifmsh
->wrkq_flags
))
1045 mesh_mpp_table_grow();
1047 if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING
, &ifmsh
->wrkq_flags
))
1048 ieee80211_mesh_housekeeping(sdata
);
1050 if (test_and_clear_bit(MESH_WORK_ROOT
, &ifmsh
->wrkq_flags
))
1051 ieee80211_mesh_rootpath(sdata
);
1053 if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST
, &ifmsh
->wrkq_flags
))
1054 mesh_sync_adjust_tbtt(sdata
);
1057 void ieee80211_mesh_notify_scan_completed(struct ieee80211_local
*local
)
1059 struct ieee80211_sub_if_data
*sdata
;
1062 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
)
1063 if (ieee80211_vif_is_mesh(&sdata
->vif
) &&
1064 ieee80211_sdata_running(sdata
))
1065 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
1069 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data
*sdata
)
1071 struct ieee80211_if_mesh
*ifmsh
= &sdata
->u
.mesh
;
1072 static u8 zero_addr
[ETH_ALEN
] = {};
1074 setup_timer(&ifmsh
->housekeeping_timer
,
1075 ieee80211_mesh_housekeeping_timer
,
1076 (unsigned long) sdata
);
1078 ifmsh
->accepting_plinks
= true;
1081 ifmsh
->num_gates
= 0;
1082 atomic_set(&ifmsh
->mpaths
, 0);
1083 mesh_rmc_init(sdata
);
1084 ifmsh
->last_preq
= jiffies
;
1085 ifmsh
->next_perr
= jiffies
;
1086 /* Allocate all mesh structures when creating the first mesh interface. */
1087 if (!mesh_allocated
)
1089 setup_timer(&ifmsh
->mesh_path_timer
,
1090 ieee80211_mesh_path_timer
,
1091 (unsigned long) sdata
);
1092 setup_timer(&ifmsh
->mesh_path_root_timer
,
1093 ieee80211_mesh_path_root_timer
,
1094 (unsigned long) sdata
);
1095 INIT_LIST_HEAD(&ifmsh
->preq_queue
.list
);
1096 skb_queue_head_init(&ifmsh
->ps
.bc_buf
);
1097 spin_lock_init(&ifmsh
->mesh_preq_queue_lock
);
1098 spin_lock_init(&ifmsh
->sync_offset_lock
);
1099 RCU_INIT_POINTER(ifmsh
->beacon
, NULL
);
1100 mutex_init(&ifmsh
->mtx
);
1102 sdata
->vif
.bss_conf
.bssid
= zero_addr
;