GNU Linux-libre 4.4.285-gnu1
[releases.git] / net / mac80211 / mesh.c
1 /*
2  * Copyright (c) 2008, 2009 open80211s Ltd.
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
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
13 #include "ieee80211_i.h"
14 #include "mesh.h"
15 #include "driver-ops.h"
16
17 static int mesh_allocated;
18 static struct kmem_cache *rm_cache;
19
20 bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
21 {
22         return (mgmt->u.action.u.mesh_action.action_code ==
23                         WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
24 }
25
26 void ieee80211s_init(void)
27 {
28         mesh_pathtbl_init();
29         mesh_allocated = 1;
30         rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
31                                      0, 0, NULL);
32 }
33
34 void ieee80211s_stop(void)
35 {
36         if (!mesh_allocated)
37                 return;
38         mesh_pathtbl_unregister();
39         kmem_cache_destroy(rm_cache);
40 }
41
42 static void ieee80211_mesh_housekeeping_timer(unsigned long data)
43 {
44         struct ieee80211_sub_if_data *sdata = (void *) data;
45         struct ieee80211_local *local = sdata->local;
46         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
47
48         set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
49
50         ieee80211_queue_work(&local->hw, &sdata->work);
51 }
52
53 /**
54  * mesh_matches_local - check if the config of a mesh point matches ours
55  *
56  * @sdata: local mesh subif
57  * @ie: information elements of a management frame from the mesh peer
58  *
59  * This function checks if the mesh configuration of a mesh point matches the
60  * local mesh configuration, i.e. if both nodes belong to the same mesh network.
61  */
62 bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
63                         struct ieee802_11_elems *ie)
64 {
65         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
66         u32 basic_rates = 0;
67         struct cfg80211_chan_def sta_chan_def;
68
69         /*
70          * As support for each feature is added, check for matching
71          * - On mesh config capabilities
72          *   - Power Save Support En
73          *   - Sync support enabled
74          *   - Sync support active
75          *   - Sync support required from peer
76          *   - MDA enabled
77          * - Power management control on fc
78          */
79         if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
80              memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
81              (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
82              (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
83              (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
84              (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
85              (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
86                 return false;
87
88         ieee80211_sta_get_rates(sdata, ie, ieee80211_get_sdata_band(sdata),
89                                 &basic_rates);
90
91         if (sdata->vif.bss_conf.basic_rates != basic_rates)
92                 return false;
93
94         ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
95                                      ie->ht_operation, &sta_chan_def);
96
97         ieee80211_vht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
98                                       ie->vht_operation, &sta_chan_def);
99
100         if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
101                                          &sta_chan_def))
102                 return false;
103
104         return true;
105 }
106
107 /**
108  * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
109  *
110  * @ie: information elements of a management frame from the mesh peer
111  */
112 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
113 {
114         return (ie->mesh_config->meshconf_cap &
115                         IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
116 }
117
118 /**
119  * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
120  *
121  * @sdata: mesh interface in which mesh beacons are going to be updated
122  *
123  * Returns: beacon changed flag if the beacon content changed.
124  */
125 u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
126 {
127         bool free_plinks;
128         u32 changed = 0;
129
130         /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
131          * the mesh interface might be able to establish plinks with peers that
132          * are already on the table but are not on PLINK_ESTAB state. However,
133          * in general the mesh interface is not accepting peer link requests
134          * from new peers, and that must be reflected in the beacon
135          */
136         free_plinks = mesh_plink_availables(sdata);
137
138         if (free_plinks != sdata->u.mesh.accepting_plinks) {
139                 sdata->u.mesh.accepting_plinks = free_plinks;
140                 changed = BSS_CHANGED_BEACON;
141         }
142
143         return changed;
144 }
145
146 /*
147  * mesh_sta_cleanup - clean up any mesh sta state
148  *
149  * @sta: mesh sta to clean up.
150  */
151 void mesh_sta_cleanup(struct sta_info *sta)
152 {
153         struct ieee80211_sub_if_data *sdata = sta->sdata;
154         u32 changed = 0;
155
156         /*
157          * maybe userspace handles peer allocation and peering, but in either
158          * case the beacon is still generated by the kernel and we might need
159          * an update.
160          */
161         if (sdata->u.mesh.user_mpm &&
162             sta->mesh->plink_state == NL80211_PLINK_ESTAB)
163                 changed |= mesh_plink_dec_estab_count(sdata);
164         changed |= mesh_accept_plinks_update(sdata);
165         if (!sdata->u.mesh.user_mpm) {
166                 changed |= mesh_plink_deactivate(sta);
167                 del_timer_sync(&sta->mesh->plink_timer);
168         }
169
170         /* make sure no readers can access nexthop sta from here on */
171         mesh_path_flush_by_nexthop(sta);
172         synchronize_net();
173
174         if (changed)
175                 ieee80211_mbss_info_change_notify(sdata, changed);
176 }
177
178 int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
179 {
180         int i;
181
182         sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
183         if (!sdata->u.mesh.rmc)
184                 return -ENOMEM;
185         sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
186         for (i = 0; i < RMC_BUCKETS; i++)
187                 INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
188         return 0;
189 }
190
191 void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
192 {
193         struct mesh_rmc *rmc = sdata->u.mesh.rmc;
194         struct rmc_entry *p, *n;
195         int i;
196
197         if (!sdata->u.mesh.rmc)
198                 return;
199
200         for (i = 0; i < RMC_BUCKETS; i++) {
201                 list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
202                         list_del(&p->list);
203                         kmem_cache_free(rm_cache, p);
204                 }
205         }
206
207         kfree(rmc);
208         sdata->u.mesh.rmc = NULL;
209 }
210
211 /**
212  * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
213  *
214  * @sdata:      interface
215  * @sa:         source address
216  * @mesh_hdr:   mesh_header
217  *
218  * Returns: 0 if the frame is not in the cache, nonzero otherwise.
219  *
220  * Checks using the source address and the mesh sequence number if we have
221  * received this frame lately. If the frame is not in the cache, it is added to
222  * it.
223  */
224 int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
225                    const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
226 {
227         struct mesh_rmc *rmc = sdata->u.mesh.rmc;
228         u32 seqnum = 0;
229         int entries = 0;
230         u8 idx;
231         struct rmc_entry *p, *n;
232
233         /* Don't care about endianness since only match matters */
234         memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
235         idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
236         list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
237                 ++entries;
238                 if (time_after(jiffies, p->exp_time) ||
239                     entries == RMC_QUEUE_MAX_LEN) {
240                         list_del(&p->list);
241                         kmem_cache_free(rm_cache, p);
242                         --entries;
243                 } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
244                         return -1;
245         }
246
247         p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
248         if (!p)
249                 return 0;
250
251         p->seqnum = seqnum;
252         p->exp_time = jiffies + RMC_TIMEOUT;
253         memcpy(p->sa, sa, ETH_ALEN);
254         list_add(&p->list, &rmc->bucket[idx]);
255         return 0;
256 }
257
258 int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
259                          struct sk_buff *skb)
260 {
261         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
262         u8 *pos, neighbors;
263         u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
264
265         if (skb_tailroom(skb) < 2 + meshconf_len)
266                 return -ENOMEM;
267
268         pos = skb_put(skb, 2 + meshconf_len);
269         *pos++ = WLAN_EID_MESH_CONFIG;
270         *pos++ = meshconf_len;
271
272         /* save a pointer for quick updates in pre-tbtt */
273         ifmsh->meshconf_offset = pos - skb->data;
274
275         /* Active path selection protocol ID */
276         *pos++ = ifmsh->mesh_pp_id;
277         /* Active path selection metric ID   */
278         *pos++ = ifmsh->mesh_pm_id;
279         /* Congestion control mode identifier */
280         *pos++ = ifmsh->mesh_cc_id;
281         /* Synchronization protocol identifier */
282         *pos++ = ifmsh->mesh_sp_id;
283         /* Authentication Protocol identifier */
284         *pos++ = ifmsh->mesh_auth_id;
285         /* Mesh Formation Info - number of neighbors */
286         neighbors = atomic_read(&ifmsh->estab_plinks);
287         neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
288         *pos++ = neighbors << 1;
289         /* Mesh capability */
290         *pos = 0x00;
291         *pos |= ifmsh->mshcfg.dot11MeshForwarding ?
292                         IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
293         *pos |= ifmsh->accepting_plinks ?
294                         IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
295         /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
296         *pos |= ifmsh->ps_peers_deep_sleep ?
297                         IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
298         return 0;
299 }
300
301 int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
302 {
303         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
304         u8 *pos;
305
306         if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
307                 return -ENOMEM;
308
309         pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
310         *pos++ = WLAN_EID_MESH_ID;
311         *pos++ = ifmsh->mesh_id_len;
312         if (ifmsh->mesh_id_len)
313                 memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
314
315         return 0;
316 }
317
318 static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
319                                     struct sk_buff *skb)
320 {
321         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
322         u8 *pos;
323
324         /* see IEEE802.11-2012 13.14.6 */
325         if (ifmsh->ps_peers_light_sleep == 0 &&
326             ifmsh->ps_peers_deep_sleep == 0 &&
327             ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
328                 return 0;
329
330         if (skb_tailroom(skb) < 4)
331                 return -ENOMEM;
332
333         pos = skb_put(skb, 2 + 2);
334         *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
335         *pos++ = 2;
336         put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
337
338         return 0;
339 }
340
341 int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
342                         struct sk_buff *skb)
343 {
344         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
345         u8 offset, len;
346         const u8 *data;
347
348         if (!ifmsh->ie || !ifmsh->ie_len)
349                 return 0;
350
351         /* fast-forward to vendor IEs */
352         offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
353
354         if (offset < ifmsh->ie_len) {
355                 len = ifmsh->ie_len - offset;
356                 data = ifmsh->ie + offset;
357                 if (skb_tailroom(skb) < len)
358                         return -ENOMEM;
359                 memcpy(skb_put(skb, len), data, len);
360         }
361
362         return 0;
363 }
364
365 int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
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 = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
376         if (!data)
377                 return 0;
378
379         len = data[1] + 2;
380
381         if (skb_tailroom(skb) < len)
382                 return -ENOMEM;
383         memcpy(skb_put(skb, len), data, len);
384
385         return 0;
386 }
387
388 static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
389                                  struct sk_buff *skb)
390 {
391         struct ieee80211_chanctx_conf *chanctx_conf;
392         struct ieee80211_channel *chan;
393         u8 *pos;
394
395         if (skb_tailroom(skb) < 3)
396                 return -ENOMEM;
397
398         rcu_read_lock();
399         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
400         if (WARN_ON(!chanctx_conf)) {
401                 rcu_read_unlock();
402                 return -EINVAL;
403         }
404         chan = chanctx_conf->def.chan;
405         rcu_read_unlock();
406
407         pos = skb_put(skb, 2 + 1);
408         *pos++ = WLAN_EID_DS_PARAMS;
409         *pos++ = 1;
410         *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
411
412         return 0;
413 }
414
415 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
416                        struct sk_buff *skb)
417 {
418         struct ieee80211_local *local = sdata->local;
419         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
420         struct ieee80211_supported_band *sband;
421         u8 *pos;
422
423         sband = local->hw.wiphy->bands[band];
424         if (!sband->ht_cap.ht_supported ||
425             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
426             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
427             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
428                 return 0;
429
430         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
431                 return -ENOMEM;
432
433         pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
434         ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
435
436         return 0;
437 }
438
439 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
440                         struct sk_buff *skb)
441 {
442         struct ieee80211_local *local = sdata->local;
443         struct ieee80211_chanctx_conf *chanctx_conf;
444         struct ieee80211_channel *channel;
445         struct ieee80211_supported_band *sband;
446         struct ieee80211_sta_ht_cap *ht_cap;
447         u8 *pos;
448
449         rcu_read_lock();
450         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
451         if (WARN_ON(!chanctx_conf)) {
452                 rcu_read_unlock();
453                 return -EINVAL;
454         }
455         channel = chanctx_conf->def.chan;
456         rcu_read_unlock();
457
458         sband = local->hw.wiphy->bands[channel->band];
459         ht_cap = &sband->ht_cap;
460
461         if (!ht_cap->ht_supported ||
462             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
463             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
464             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
465                 return 0;
466
467         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
468                 return -ENOMEM;
469
470         pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
471         ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
472                                    sdata->vif.bss_conf.ht_operation_mode,
473                                    false);
474
475         return 0;
476 }
477
478 int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
479                         struct sk_buff *skb)
480 {
481         struct ieee80211_local *local = sdata->local;
482         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
483         struct ieee80211_supported_band *sband;
484         u8 *pos;
485
486         sband = local->hw.wiphy->bands[band];
487         if (!sband->vht_cap.vht_supported ||
488             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
489             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
490             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
491                 return 0;
492
493         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
494                 return -ENOMEM;
495
496         pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
497         ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
498
499         return 0;
500 }
501
502 int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
503                          struct sk_buff *skb)
504 {
505         struct ieee80211_local *local = sdata->local;
506         struct ieee80211_chanctx_conf *chanctx_conf;
507         struct ieee80211_channel *channel;
508         struct ieee80211_supported_band *sband;
509         struct ieee80211_sta_vht_cap *vht_cap;
510         u8 *pos;
511
512         rcu_read_lock();
513         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
514         if (WARN_ON(!chanctx_conf)) {
515                 rcu_read_unlock();
516                 return -EINVAL;
517         }
518         channel = chanctx_conf->def.chan;
519         rcu_read_unlock();
520
521         sband = local->hw.wiphy->bands[channel->band];
522         vht_cap = &sband->vht_cap;
523
524         if (!vht_cap->vht_supported ||
525             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
526             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
527             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
528                 return 0;
529
530         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
531                 return -ENOMEM;
532
533         pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
534         ieee80211_ie_build_vht_oper(pos, vht_cap,
535                                     &sdata->vif.bss_conf.chandef);
536
537         return 0;
538 }
539
540 static void ieee80211_mesh_path_timer(unsigned long data)
541 {
542         struct ieee80211_sub_if_data *sdata =
543                 (struct ieee80211_sub_if_data *) data;
544
545         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
546 }
547
548 static void ieee80211_mesh_path_root_timer(unsigned long data)
549 {
550         struct ieee80211_sub_if_data *sdata =
551                 (struct ieee80211_sub_if_data *) data;
552         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
553
554         set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
555
556         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
557 }
558
559 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
560 {
561         if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
562                 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
563         else {
564                 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
565                 /* stop running timer */
566                 del_timer_sync(&ifmsh->mesh_path_root_timer);
567         }
568 }
569
570 /**
571  * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
572  * @hdr:        802.11 frame header
573  * @fc:         frame control field
574  * @meshda:     destination address in the mesh
575  * @meshsa:     source address address in the mesh.  Same as TA, as frame is
576  *              locally originated.
577  *
578  * Return the length of the 802.11 (does not include a mesh control header)
579  */
580 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
581                                   const u8 *meshda, const u8 *meshsa)
582 {
583         if (is_multicast_ether_addr(meshda)) {
584                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
585                 /* DA TA SA */
586                 memcpy(hdr->addr1, meshda, ETH_ALEN);
587                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
588                 memcpy(hdr->addr3, meshsa, ETH_ALEN);
589                 return 24;
590         } else {
591                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
592                 /* RA TA DA SA */
593                 eth_zero_addr(hdr->addr1);   /* RA is resolved later */
594                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
595                 memcpy(hdr->addr3, meshda, ETH_ALEN);
596                 memcpy(hdr->addr4, meshsa, ETH_ALEN);
597                 return 30;
598         }
599 }
600
601 /**
602  * ieee80211_new_mesh_header - create a new mesh header
603  * @sdata:      mesh interface to be used
604  * @meshhdr:    uninitialized mesh header
605  * @addr4or5:   1st address in the ae header, which may correspond to address 4
606  *              (if addr6 is NULL) or address 5 (if addr6 is present). It may
607  *              be NULL.
608  * @addr6:      2nd address in the ae header, which corresponds to addr6 of the
609  *              mesh frame
610  *
611  * Return the header length.
612  */
613 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
614                                        struct ieee80211s_hdr *meshhdr,
615                                        const char *addr4or5, const char *addr6)
616 {
617         if (WARN_ON(!addr4or5 && addr6))
618                 return 0;
619
620         memset(meshhdr, 0, sizeof(*meshhdr));
621
622         meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
623
624         /* FIXME: racy -- TX on multiple queues can be concurrent */
625         put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
626         sdata->u.mesh.mesh_seqnum++;
627
628         if (addr4or5 && !addr6) {
629                 meshhdr->flags |= MESH_FLAGS_AE_A4;
630                 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
631                 return 2 * ETH_ALEN;
632         } else if (addr4or5 && addr6) {
633                 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
634                 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
635                 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
636                 return 3 * ETH_ALEN;
637         }
638
639         return ETH_ALEN;
640 }
641
642 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
643 {
644         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
645         u32 changed;
646
647         if (ifmsh->mshcfg.plink_timeout > 0)
648                 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
649         mesh_path_expire(sdata);
650
651         changed = mesh_accept_plinks_update(sdata);
652         ieee80211_mbss_info_change_notify(sdata, changed);
653
654         mod_timer(&ifmsh->housekeeping_timer,
655                   round_jiffies(jiffies +
656                                 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
657 }
658
659 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
660 {
661         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
662         u32 interval;
663
664         mesh_path_tx_root_frame(sdata);
665
666         if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
667                 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
668         else
669                 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
670
671         mod_timer(&ifmsh->mesh_path_root_timer,
672                   round_jiffies(TU_TO_EXP_TIME(interval)));
673 }
674
675 static int
676 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
677 {
678         struct beacon_data *bcn;
679         int head_len, tail_len;
680         struct sk_buff *skb;
681         struct ieee80211_mgmt *mgmt;
682         struct ieee80211_chanctx_conf *chanctx_conf;
683         struct mesh_csa_settings *csa;
684         enum ieee80211_band band;
685         u8 *pos;
686         struct ieee80211_sub_if_data *sdata;
687         int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
688                       sizeof(mgmt->u.beacon);
689
690         sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
691         rcu_read_lock();
692         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
693         band = chanctx_conf->def.chan->band;
694         rcu_read_unlock();
695
696         head_len = hdr_len +
697                    2 + /* NULL SSID */
698                    /* Channel Switch Announcement */
699                    2 + sizeof(struct ieee80211_channel_sw_ie) +
700                    /* Mesh Channel Swith Parameters */
701                    2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
702                    2 + 8 + /* supported rates */
703                    2 + 3; /* DS params */
704         tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
705                    2 + sizeof(struct ieee80211_ht_cap) +
706                    2 + sizeof(struct ieee80211_ht_operation) +
707                    2 + ifmsh->mesh_id_len +
708                    2 + sizeof(struct ieee80211_meshconf_ie) +
709                    2 + sizeof(__le16) + /* awake window */
710                    2 + sizeof(struct ieee80211_vht_cap) +
711                    2 + sizeof(struct ieee80211_vht_operation) +
712                    ifmsh->ie_len;
713
714         bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
715         /* need an skb for IE builders to operate on */
716         skb = dev_alloc_skb(max(head_len, tail_len));
717
718         if (!bcn || !skb)
719                 goto out_free;
720
721         /*
722          * pointers go into the block we allocated,
723          * memory is | beacon_data | head | tail |
724          */
725         bcn->head = ((u8 *) bcn) + sizeof(*bcn);
726
727         /* fill in the head */
728         mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
729         memset(mgmt, 0, hdr_len);
730         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
731                                           IEEE80211_STYPE_BEACON);
732         eth_broadcast_addr(mgmt->da);
733         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
734         memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
735         ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
736         mgmt->u.beacon.beacon_int =
737                 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
738         mgmt->u.beacon.capab_info |= cpu_to_le16(
739                 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
740
741         pos = skb_put(skb, 2);
742         *pos++ = WLAN_EID_SSID;
743         *pos++ = 0x0;
744
745         rcu_read_lock();
746         csa = rcu_dereference(ifmsh->csa);
747         if (csa) {
748                 pos = skb_put(skb, 13);
749                 memset(pos, 0, 13);
750                 *pos++ = WLAN_EID_CHANNEL_SWITCH;
751                 *pos++ = 3;
752                 *pos++ = 0x0;
753                 *pos++ = ieee80211_frequency_to_channel(
754                                 csa->settings.chandef.chan->center_freq);
755                 bcn->csa_current_counter = csa->settings.count;
756                 bcn->csa_counter_offsets[0] = hdr_len + 6;
757                 *pos++ = csa->settings.count;
758                 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
759                 *pos++ = 6;
760                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
761                         *pos++ = ifmsh->mshcfg.dot11MeshTTL;
762                         *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
763                 } else {
764                         *pos++ = ifmsh->chsw_ttl;
765                 }
766                 *pos++ |= csa->settings.block_tx ?
767                           WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
768                 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
769                 pos += 2;
770                 put_unaligned_le16(ifmsh->pre_value, pos);
771                 pos += 2;
772         }
773         rcu_read_unlock();
774
775         if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
776             mesh_add_ds_params_ie(sdata, skb))
777                 goto out_free;
778
779         bcn->head_len = skb->len;
780         memcpy(bcn->head, skb->data, bcn->head_len);
781
782         /* now the tail */
783         skb_trim(skb, 0);
784         bcn->tail = bcn->head + bcn->head_len;
785
786         if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
787             mesh_add_rsn_ie(sdata, skb) ||
788             mesh_add_ht_cap_ie(sdata, skb) ||
789             mesh_add_ht_oper_ie(sdata, skb) ||
790             mesh_add_meshid_ie(sdata, skb) ||
791             mesh_add_meshconf_ie(sdata, skb) ||
792             mesh_add_awake_window_ie(sdata, skb) ||
793             mesh_add_vht_cap_ie(sdata, skb) ||
794             mesh_add_vht_oper_ie(sdata, skb) ||
795             mesh_add_vendor_ies(sdata, skb))
796                 goto out_free;
797
798         bcn->tail_len = skb->len;
799         memcpy(bcn->tail, skb->data, bcn->tail_len);
800         bcn->meshconf = (struct ieee80211_meshconf_ie *)
801                                         (bcn->tail + ifmsh->meshconf_offset);
802
803         dev_kfree_skb(skb);
804         rcu_assign_pointer(ifmsh->beacon, bcn);
805         return 0;
806 out_free:
807         kfree(bcn);
808         dev_kfree_skb(skb);
809         return -ENOMEM;
810 }
811
812 static int
813 ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
814 {
815         struct beacon_data *old_bcn;
816         int ret;
817
818         old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
819                                             lockdep_is_held(&sdata->wdev.mtx));
820         ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
821         if (ret)
822                 /* just reuse old beacon */
823                 return ret;
824
825         if (old_bcn)
826                 kfree_rcu(old_bcn, rcu_head);
827         return 0;
828 }
829
830 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
831                                        u32 changed)
832 {
833         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
834         unsigned long bits = changed;
835         u32 bit;
836
837         if (!bits)
838                 return;
839
840         /* if we race with running work, worst case this work becomes a noop */
841         for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
842                 set_bit(bit, &ifmsh->mbss_changed);
843         set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
844         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
845 }
846
847 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
848 {
849         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
850         struct ieee80211_local *local = sdata->local;
851         u32 changed = BSS_CHANGED_BEACON |
852                       BSS_CHANGED_BEACON_ENABLED |
853                       BSS_CHANGED_HT |
854                       BSS_CHANGED_BASIC_RATES |
855                       BSS_CHANGED_BEACON_INT;
856
857         local->fif_other_bss++;
858         /* mesh ifaces must set allmulti to forward mcast traffic */
859         atomic_inc(&local->iff_allmultis);
860         ieee80211_configure_filter(local);
861
862         ifmsh->mesh_cc_id = 0;  /* Disabled */
863         /* register sync ops from extensible synchronization framework */
864         ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
865         ifmsh->sync_offset_clockdrift_max = 0;
866         set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
867         ieee80211_mesh_root_setup(ifmsh);
868         ieee80211_queue_work(&local->hw, &sdata->work);
869         sdata->vif.bss_conf.ht_operation_mode =
870                                 ifmsh->mshcfg.ht_opmode;
871         sdata->vif.bss_conf.enable_beacon = true;
872
873         changed |= ieee80211_mps_local_status_update(sdata);
874
875         if (ieee80211_mesh_build_beacon(ifmsh)) {
876                 ieee80211_stop_mesh(sdata);
877                 return -ENOMEM;
878         }
879
880         ieee80211_recalc_dtim(local, sdata);
881         ieee80211_bss_info_change_notify(sdata, changed);
882
883         netif_carrier_on(sdata->dev);
884         return 0;
885 }
886
887 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
888 {
889         struct ieee80211_local *local = sdata->local;
890         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
891         struct beacon_data *bcn;
892
893         netif_carrier_off(sdata->dev);
894
895         /* stop the beacon */
896         ifmsh->mesh_id_len = 0;
897         sdata->vif.bss_conf.enable_beacon = false;
898         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
899         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
900         bcn = rcu_dereference_protected(ifmsh->beacon,
901                                         lockdep_is_held(&sdata->wdev.mtx));
902         RCU_INIT_POINTER(ifmsh->beacon, NULL);
903         kfree_rcu(bcn, rcu_head);
904
905         /* flush STAs and mpaths on this iface */
906         sta_info_flush(sdata);
907         mesh_path_flush_by_iface(sdata);
908
909         /* free all potentially still buffered group-addressed frames */
910         local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
911         skb_queue_purge(&ifmsh->ps.bc_buf);
912
913         del_timer_sync(&sdata->u.mesh.housekeeping_timer);
914         del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
915         del_timer_sync(&sdata->u.mesh.mesh_path_timer);
916
917         /* clear any mesh work (for next join) we may have accrued */
918         ifmsh->wrkq_flags = 0;
919         ifmsh->mbss_changed = 0;
920
921         local->fif_other_bss--;
922         atomic_dec(&local->iff_allmultis);
923         ieee80211_configure_filter(local);
924 }
925
926 static bool
927 ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
928                                  struct ieee802_11_elems *elems, bool beacon)
929 {
930         struct cfg80211_csa_settings params;
931         struct ieee80211_csa_ie csa_ie;
932         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
933         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
934         int err;
935         u32 sta_flags;
936
937         sdata_assert_lock(sdata);
938
939         sta_flags = IEEE80211_STA_DISABLE_VHT;
940         switch (sdata->vif.bss_conf.chandef.width) {
941         case NL80211_CHAN_WIDTH_20_NOHT:
942                 sta_flags |= IEEE80211_STA_DISABLE_HT;
943         case NL80211_CHAN_WIDTH_20:
944                 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
945                 break;
946         default:
947                 break;
948         }
949
950         memset(&params, 0, sizeof(params));
951         memset(&csa_ie, 0, sizeof(csa_ie));
952         err = ieee80211_parse_ch_switch_ie(sdata, elems, band,
953                                            sta_flags, sdata->vif.addr,
954                                            &csa_ie);
955         if (err < 0)
956                 return false;
957         if (err)
958                 return false;
959
960         params.chandef = csa_ie.chandef;
961         params.count = csa_ie.count;
962
963         if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
964                                      IEEE80211_CHAN_DISABLED)) {
965                 sdata_info(sdata,
966                            "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
967                            sdata->vif.addr,
968                            params.chandef.chan->center_freq,
969                            params.chandef.width,
970                            params.chandef.center_freq1,
971                            params.chandef.center_freq2);
972                 return false;
973         }
974
975         err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
976                                             &params.chandef,
977                                             NL80211_IFTYPE_MESH_POINT);
978         if (err < 0)
979                 return false;
980         if (err > 0)
981                 /* TODO: DFS not (yet) supported */
982                 return false;
983
984         params.radar_required = err;
985
986         if (cfg80211_chandef_identical(&params.chandef,
987                                        &sdata->vif.bss_conf.chandef)) {
988                 mcsa_dbg(sdata,
989                          "received csa with an identical chandef, ignoring\n");
990                 return true;
991         }
992
993         mcsa_dbg(sdata,
994                  "received channel switch announcement to go to channel %d MHz\n",
995                  params.chandef.chan->center_freq);
996
997         params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
998         if (beacon) {
999                 ifmsh->chsw_ttl = csa_ie.ttl - 1;
1000                 if (ifmsh->pre_value >= csa_ie.pre_value)
1001                         return false;
1002                 ifmsh->pre_value = csa_ie.pre_value;
1003         }
1004
1005         if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
1006                 return false;
1007
1008         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
1009
1010         if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1011                                      &params) < 0)
1012                 return false;
1013
1014         return true;
1015 }
1016
1017 static void
1018 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1019                             struct ieee80211_mgmt *mgmt, size_t len)
1020 {
1021         struct ieee80211_local *local = sdata->local;
1022         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1023         struct sk_buff *presp;
1024         struct beacon_data *bcn;
1025         struct ieee80211_mgmt *hdr;
1026         struct ieee802_11_elems elems;
1027         size_t baselen;
1028         u8 *pos;
1029
1030         pos = mgmt->u.probe_req.variable;
1031         baselen = (u8 *) pos - (u8 *) mgmt;
1032         if (baselen > len)
1033                 return;
1034
1035         ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1036
1037         if (!elems.mesh_id)
1038                 return;
1039
1040         /* 802.11-2012 10.1.4.3.2 */
1041         if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1042              !is_broadcast_ether_addr(mgmt->da)) ||
1043             elems.ssid_len != 0)
1044                 return;
1045
1046         if (elems.mesh_id_len != 0 &&
1047             (elems.mesh_id_len != ifmsh->mesh_id_len ||
1048              memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
1049                 return;
1050
1051         rcu_read_lock();
1052         bcn = rcu_dereference(ifmsh->beacon);
1053
1054         if (!bcn)
1055                 goto out;
1056
1057         presp = dev_alloc_skb(local->tx_headroom +
1058                               bcn->head_len + bcn->tail_len);
1059         if (!presp)
1060                 goto out;
1061
1062         skb_reserve(presp, local->tx_headroom);
1063         memcpy(skb_put(presp, bcn->head_len), bcn->head, bcn->head_len);
1064         memcpy(skb_put(presp, bcn->tail_len), bcn->tail, bcn->tail_len);
1065         hdr = (struct ieee80211_mgmt *) presp->data;
1066         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1067                                          IEEE80211_STYPE_PROBE_RESP);
1068         memcpy(hdr->da, mgmt->sa, ETH_ALEN);
1069         IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1070         ieee80211_tx_skb(sdata, presp);
1071 out:
1072         rcu_read_unlock();
1073 }
1074
1075 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1076                                         u16 stype,
1077                                         struct ieee80211_mgmt *mgmt,
1078                                         size_t len,
1079                                         struct ieee80211_rx_status *rx_status)
1080 {
1081         struct ieee80211_local *local = sdata->local;
1082         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1083         struct ieee802_11_elems elems;
1084         struct ieee80211_channel *channel;
1085         size_t baselen;
1086         int freq;
1087         enum ieee80211_band band = rx_status->band;
1088
1089         /* ignore ProbeResp to foreign address */
1090         if (stype == IEEE80211_STYPE_PROBE_RESP &&
1091             !ether_addr_equal(mgmt->da, sdata->vif.addr))
1092                 return;
1093
1094         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1095         if (baselen > len)
1096                 return;
1097
1098         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1099                                false, &elems);
1100
1101         /* ignore non-mesh or secure / unsecure mismatch */
1102         if ((!elems.mesh_id || !elems.mesh_config) ||
1103             (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1104             (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
1105                 return;
1106
1107         if (elems.ds_params)
1108                 freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
1109         else
1110                 freq = rx_status->freq;
1111
1112         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1113
1114         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1115                 return;
1116
1117         if (mesh_matches_local(sdata, &elems))
1118                 mesh_neighbour_update(sdata, mgmt->sa, &elems);
1119
1120         if (ifmsh->sync_ops)
1121                 ifmsh->sync_ops->rx_bcn_presp(sdata,
1122                         stype, mgmt, &elems, rx_status);
1123
1124         if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1125             !sdata->vif.csa_active)
1126                 ieee80211_mesh_process_chnswitch(sdata, &elems, true);
1127 }
1128
1129 int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1130 {
1131         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1132         struct mesh_csa_settings *tmp_csa_settings;
1133         int ret = 0;
1134         int changed = 0;
1135
1136         /* Reset the TTL value and Initiator flag */
1137         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1138         ifmsh->chsw_ttl = 0;
1139
1140         /* Remove the CSA and MCSP elements from the beacon */
1141         tmp_csa_settings = rcu_dereference_protected(ifmsh->csa,
1142                                             lockdep_is_held(&sdata->wdev.mtx));
1143         RCU_INIT_POINTER(ifmsh->csa, NULL);
1144         if (tmp_csa_settings)
1145                 kfree_rcu(tmp_csa_settings, rcu_head);
1146         ret = ieee80211_mesh_rebuild_beacon(sdata);
1147         if (ret)
1148                 return -EINVAL;
1149
1150         changed |= BSS_CHANGED_BEACON;
1151
1152         mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1153                  sdata->vif.bss_conf.chandef.chan->center_freq);
1154         return changed;
1155 }
1156
1157 int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1158                               struct cfg80211_csa_settings *csa_settings)
1159 {
1160         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1161         struct mesh_csa_settings *tmp_csa_settings;
1162         int ret = 0;
1163
1164         lockdep_assert_held(&sdata->wdev.mtx);
1165
1166         tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1167                                    GFP_ATOMIC);
1168         if (!tmp_csa_settings)
1169                 return -ENOMEM;
1170
1171         memcpy(&tmp_csa_settings->settings, csa_settings,
1172                sizeof(struct cfg80211_csa_settings));
1173
1174         rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1175
1176         ret = ieee80211_mesh_rebuild_beacon(sdata);
1177         if (ret) {
1178                 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1179                 RCU_INIT_POINTER(ifmsh->csa, NULL);
1180                 kfree_rcu(tmp_csa_settings, rcu_head);
1181                 return ret;
1182         }
1183
1184         return BSS_CHANGED_BEACON;
1185 }
1186
1187 static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1188                                struct ieee80211_mgmt *mgmt, size_t len)
1189 {
1190         struct ieee80211_mgmt *mgmt_fwd;
1191         struct sk_buff *skb;
1192         struct ieee80211_local *local = sdata->local;
1193         u8 *pos = mgmt->u.action.u.chan_switch.variable;
1194         size_t offset_ttl;
1195
1196         skb = dev_alloc_skb(local->tx_headroom + len);
1197         if (!skb)
1198                 return -ENOMEM;
1199         skb_reserve(skb, local->tx_headroom);
1200         mgmt_fwd = (struct ieee80211_mgmt *) skb_put(skb, len);
1201
1202         /* offset_ttl is based on whether the secondary channel
1203          * offset is available or not. Subtract 1 from the mesh TTL
1204          * and disable the initiator flag before forwarding.
1205          */
1206         offset_ttl = (len < 42) ? 7 : 10;
1207         *(pos + offset_ttl) -= 1;
1208         *(pos + offset_ttl + 1) &= ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1209
1210         memcpy(mgmt_fwd, mgmt, len);
1211         eth_broadcast_addr(mgmt_fwd->da);
1212         memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1213         memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1214
1215         ieee80211_tx_skb(sdata, skb);
1216         return 0;
1217 }
1218
1219 static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1220                               struct ieee80211_mgmt *mgmt, size_t len)
1221 {
1222         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1223         struct ieee802_11_elems elems;
1224         u16 pre_value;
1225         bool fwd_csa = true;
1226         size_t baselen;
1227         u8 *pos;
1228
1229         if (mgmt->u.action.u.measurement.action_code !=
1230             WLAN_ACTION_SPCT_CHL_SWITCH)
1231                 return;
1232
1233         pos = mgmt->u.action.u.chan_switch.variable;
1234         baselen = offsetof(struct ieee80211_mgmt,
1235                            u.action.u.chan_switch.variable);
1236         ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1237
1238         ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
1239         if (!--ifmsh->chsw_ttl)
1240                 fwd_csa = false;
1241
1242         pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
1243         if (ifmsh->pre_value >= pre_value)
1244                 return;
1245
1246         ifmsh->pre_value = pre_value;
1247
1248         if (!sdata->vif.csa_active &&
1249             !ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
1250                 mcsa_dbg(sdata, "Failed to process CSA action frame");
1251                 return;
1252         }
1253
1254         /* forward or re-broadcast the CSA frame */
1255         if (fwd_csa) {
1256                 if (mesh_fwd_csa_frame(sdata, mgmt, len) < 0)
1257                         mcsa_dbg(sdata, "Failed to forward the CSA frame");
1258         }
1259 }
1260
1261 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1262                                           struct ieee80211_mgmt *mgmt,
1263                                           size_t len,
1264                                           struct ieee80211_rx_status *rx_status)
1265 {
1266         switch (mgmt->u.action.category) {
1267         case WLAN_CATEGORY_SELF_PROTECTED:
1268                 switch (mgmt->u.action.u.self_prot.action_code) {
1269                 case WLAN_SP_MESH_PEERING_OPEN:
1270                 case WLAN_SP_MESH_PEERING_CLOSE:
1271                 case WLAN_SP_MESH_PEERING_CONFIRM:
1272                         mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1273                         break;
1274                 }
1275                 break;
1276         case WLAN_CATEGORY_MESH_ACTION:
1277                 if (mesh_action_is_path_sel(mgmt))
1278                         mesh_rx_path_sel_frame(sdata, mgmt, len);
1279                 break;
1280         case WLAN_CATEGORY_SPECTRUM_MGMT:
1281                 mesh_rx_csa_frame(sdata, mgmt, len);
1282                 break;
1283         }
1284 }
1285
1286 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1287                                    struct sk_buff *skb)
1288 {
1289         struct ieee80211_rx_status *rx_status;
1290         struct ieee80211_mgmt *mgmt;
1291         u16 stype;
1292
1293         sdata_lock(sdata);
1294
1295         /* mesh already went down */
1296         if (!sdata->u.mesh.mesh_id_len)
1297                 goto out;
1298
1299         rx_status = IEEE80211_SKB_RXCB(skb);
1300         mgmt = (struct ieee80211_mgmt *) skb->data;
1301         stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1302
1303         switch (stype) {
1304         case IEEE80211_STYPE_PROBE_RESP:
1305         case IEEE80211_STYPE_BEACON:
1306                 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1307                                             rx_status);
1308                 break;
1309         case IEEE80211_STYPE_PROBE_REQ:
1310                 ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1311                 break;
1312         case IEEE80211_STYPE_ACTION:
1313                 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1314                 break;
1315         }
1316 out:
1317         sdata_unlock(sdata);
1318 }
1319
1320 static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1321 {
1322         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1323         u32 bit, changed = 0;
1324
1325         for_each_set_bit(bit, &ifmsh->mbss_changed,
1326                          sizeof(changed) * BITS_PER_BYTE) {
1327                 clear_bit(bit, &ifmsh->mbss_changed);
1328                 changed |= BIT(bit);
1329         }
1330
1331         if (sdata->vif.bss_conf.enable_beacon &&
1332             (changed & (BSS_CHANGED_BEACON |
1333                         BSS_CHANGED_HT |
1334                         BSS_CHANGED_BASIC_RATES |
1335                         BSS_CHANGED_BEACON_INT)))
1336                 if (ieee80211_mesh_rebuild_beacon(sdata))
1337                         return;
1338
1339         ieee80211_bss_info_change_notify(sdata, changed);
1340 }
1341
1342 void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
1343 {
1344         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1345
1346         sdata_lock(sdata);
1347
1348         /* mesh already went down */
1349         if (!sdata->u.mesh.mesh_id_len)
1350                 goto out;
1351
1352         if (ifmsh->preq_queue_len &&
1353             time_after(jiffies,
1354                        ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1355                 mesh_path_start_discovery(sdata);
1356
1357         if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
1358                 mesh_mpath_table_grow();
1359
1360         if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
1361                 mesh_mpp_table_grow();
1362
1363         if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
1364                 ieee80211_mesh_housekeeping(sdata);
1365
1366         if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1367                 ieee80211_mesh_rootpath(sdata);
1368
1369         if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1370                 mesh_sync_adjust_tbtt(sdata);
1371
1372         if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1373                 mesh_bss_info_changed(sdata);
1374 out:
1375         sdata_unlock(sdata);
1376 }
1377
1378
1379 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1380 {
1381         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1382         static u8 zero_addr[ETH_ALEN] = {};
1383
1384         setup_timer(&ifmsh->housekeeping_timer,
1385                     ieee80211_mesh_housekeeping_timer,
1386                     (unsigned long) sdata);
1387
1388         ifmsh->accepting_plinks = true;
1389         atomic_set(&ifmsh->mpaths, 0);
1390         mesh_rmc_init(sdata);
1391         ifmsh->last_preq = jiffies;
1392         ifmsh->next_perr = jiffies;
1393         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1394         /* Allocate all mesh structures when creating the first mesh interface. */
1395         if (!mesh_allocated)
1396                 ieee80211s_init();
1397         setup_timer(&ifmsh->mesh_path_timer,
1398                     ieee80211_mesh_path_timer,
1399                     (unsigned long) sdata);
1400         setup_timer(&ifmsh->mesh_path_root_timer,
1401                     ieee80211_mesh_path_root_timer,
1402                     (unsigned long) sdata);
1403         INIT_LIST_HEAD(&ifmsh->preq_queue.list);
1404         skb_queue_head_init(&ifmsh->ps.bc_buf);
1405         spin_lock_init(&ifmsh->mesh_preq_queue_lock);
1406         spin_lock_init(&ifmsh->sync_offset_lock);
1407         RCU_INIT_POINTER(ifmsh->beacon, NULL);
1408
1409         sdata->vif.bss_conf.bssid = zero_addr;
1410 }