Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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         *pos++ |= ifmsh->adjusting_tbtt ?
299                         IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
300         *pos++ = 0x00;
301
302         return 0;
303 }
304
305 int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
306 {
307         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
308         u8 *pos;
309
310         if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
311                 return -ENOMEM;
312
313         pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
314         *pos++ = WLAN_EID_MESH_ID;
315         *pos++ = ifmsh->mesh_id_len;
316         if (ifmsh->mesh_id_len)
317                 memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
318
319         return 0;
320 }
321
322 static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
323                                     struct sk_buff *skb)
324 {
325         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
326         u8 *pos;
327
328         /* see IEEE802.11-2012 13.14.6 */
329         if (ifmsh->ps_peers_light_sleep == 0 &&
330             ifmsh->ps_peers_deep_sleep == 0 &&
331             ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
332                 return 0;
333
334         if (skb_tailroom(skb) < 4)
335                 return -ENOMEM;
336
337         pos = skb_put(skb, 2 + 2);
338         *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
339         *pos++ = 2;
340         put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
341
342         return 0;
343 }
344
345 int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
346                         struct sk_buff *skb)
347 {
348         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
349         u8 offset, len;
350         const u8 *data;
351
352         if (!ifmsh->ie || !ifmsh->ie_len)
353                 return 0;
354
355         /* fast-forward to vendor IEs */
356         offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
357
358         if (offset < ifmsh->ie_len) {
359                 len = ifmsh->ie_len - offset;
360                 data = ifmsh->ie + offset;
361                 if (skb_tailroom(skb) < len)
362                         return -ENOMEM;
363                 memcpy(skb_put(skb, len), data, len);
364         }
365
366         return 0;
367 }
368
369 int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
370 {
371         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
372         u8 len = 0;
373         const u8 *data;
374
375         if (!ifmsh->ie || !ifmsh->ie_len)
376                 return 0;
377
378         /* find RSN IE */
379         data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
380         if (!data)
381                 return 0;
382
383         len = data[1] + 2;
384
385         if (skb_tailroom(skb) < len)
386                 return -ENOMEM;
387         memcpy(skb_put(skb, len), data, len);
388
389         return 0;
390 }
391
392 static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
393                                  struct sk_buff *skb)
394 {
395         struct ieee80211_chanctx_conf *chanctx_conf;
396         struct ieee80211_channel *chan;
397         u8 *pos;
398
399         if (skb_tailroom(skb) < 3)
400                 return -ENOMEM;
401
402         rcu_read_lock();
403         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
404         if (WARN_ON(!chanctx_conf)) {
405                 rcu_read_unlock();
406                 return -EINVAL;
407         }
408         chan = chanctx_conf->def.chan;
409         rcu_read_unlock();
410
411         pos = skb_put(skb, 2 + 1);
412         *pos++ = WLAN_EID_DS_PARAMS;
413         *pos++ = 1;
414         *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
415
416         return 0;
417 }
418
419 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
420                        struct sk_buff *skb)
421 {
422         struct ieee80211_local *local = sdata->local;
423         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
424         struct ieee80211_supported_band *sband;
425         u8 *pos;
426
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 ||
430             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
431             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
432                 return 0;
433
434         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
435                 return -ENOMEM;
436
437         pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
438         ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
439
440         return 0;
441 }
442
443 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
444                         struct sk_buff *skb)
445 {
446         struct ieee80211_local *local = sdata->local;
447         struct ieee80211_chanctx_conf *chanctx_conf;
448         struct ieee80211_channel *channel;
449         struct ieee80211_supported_band *sband;
450         struct ieee80211_sta_ht_cap *ht_cap;
451         u8 *pos;
452
453         rcu_read_lock();
454         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
455         if (WARN_ON(!chanctx_conf)) {
456                 rcu_read_unlock();
457                 return -EINVAL;
458         }
459         channel = chanctx_conf->def.chan;
460         rcu_read_unlock();
461
462         sband = local->hw.wiphy->bands[channel->band];
463         ht_cap = &sband->ht_cap;
464
465         if (!ht_cap->ht_supported ||
466             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
467             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
468             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
469                 return 0;
470
471         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
472                 return -ENOMEM;
473
474         pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
475         ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
476                                    sdata->vif.bss_conf.ht_operation_mode,
477                                    false);
478
479         return 0;
480 }
481
482 int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
483                         struct sk_buff *skb)
484 {
485         struct ieee80211_local *local = sdata->local;
486         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
487         struct ieee80211_supported_band *sband;
488         u8 *pos;
489
490         sband = local->hw.wiphy->bands[band];
491         if (!sband->vht_cap.vht_supported ||
492             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
493             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
494             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
495                 return 0;
496
497         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
498                 return -ENOMEM;
499
500         pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
501         ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
502
503         return 0;
504 }
505
506 int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
507                          struct sk_buff *skb)
508 {
509         struct ieee80211_local *local = sdata->local;
510         struct ieee80211_chanctx_conf *chanctx_conf;
511         struct ieee80211_channel *channel;
512         struct ieee80211_supported_band *sband;
513         struct ieee80211_sta_vht_cap *vht_cap;
514         u8 *pos;
515
516         rcu_read_lock();
517         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
518         if (WARN_ON(!chanctx_conf)) {
519                 rcu_read_unlock();
520                 return -EINVAL;
521         }
522         channel = chanctx_conf->def.chan;
523         rcu_read_unlock();
524
525         sband = local->hw.wiphy->bands[channel->band];
526         vht_cap = &sband->vht_cap;
527
528         if (!vht_cap->vht_supported ||
529             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
530             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
531             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
532                 return 0;
533
534         if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
535                 return -ENOMEM;
536
537         pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
538         ieee80211_ie_build_vht_oper(pos, vht_cap,
539                                     &sdata->vif.bss_conf.chandef);
540
541         return 0;
542 }
543
544 static void ieee80211_mesh_path_timer(unsigned long data)
545 {
546         struct ieee80211_sub_if_data *sdata =
547                 (struct ieee80211_sub_if_data *) data;
548
549         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
550 }
551
552 static void ieee80211_mesh_path_root_timer(unsigned long data)
553 {
554         struct ieee80211_sub_if_data *sdata =
555                 (struct ieee80211_sub_if_data *) data;
556         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
557
558         set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
559
560         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
561 }
562
563 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
564 {
565         if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
566                 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
567         else {
568                 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
569                 /* stop running timer */
570                 del_timer_sync(&ifmsh->mesh_path_root_timer);
571         }
572 }
573
574 /**
575  * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
576  * @hdr:        802.11 frame header
577  * @fc:         frame control field
578  * @meshda:     destination address in the mesh
579  * @meshsa:     source address address in the mesh.  Same as TA, as frame is
580  *              locally originated.
581  *
582  * Return the length of the 802.11 (does not include a mesh control header)
583  */
584 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
585                                   const u8 *meshda, const u8 *meshsa)
586 {
587         if (is_multicast_ether_addr(meshda)) {
588                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
589                 /* DA TA SA */
590                 memcpy(hdr->addr1, meshda, ETH_ALEN);
591                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
592                 memcpy(hdr->addr3, meshsa, ETH_ALEN);
593                 return 24;
594         } else {
595                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
596                 /* RA TA DA SA */
597                 eth_zero_addr(hdr->addr1);   /* RA is resolved later */
598                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
599                 memcpy(hdr->addr3, meshda, ETH_ALEN);
600                 memcpy(hdr->addr4, meshsa, ETH_ALEN);
601                 return 30;
602         }
603 }
604
605 /**
606  * ieee80211_new_mesh_header - create a new mesh header
607  * @sdata:      mesh interface to be used
608  * @meshhdr:    uninitialized mesh header
609  * @addr4or5:   1st address in the ae header, which may correspond to address 4
610  *              (if addr6 is NULL) or address 5 (if addr6 is present). It may
611  *              be NULL.
612  * @addr6:      2nd address in the ae header, which corresponds to addr6 of the
613  *              mesh frame
614  *
615  * Return the header length.
616  */
617 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
618                                        struct ieee80211s_hdr *meshhdr,
619                                        const char *addr4or5, const char *addr6)
620 {
621         if (WARN_ON(!addr4or5 && addr6))
622                 return 0;
623
624         memset(meshhdr, 0, sizeof(*meshhdr));
625
626         meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
627
628         /* FIXME: racy -- TX on multiple queues can be concurrent */
629         put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
630         sdata->u.mesh.mesh_seqnum++;
631
632         if (addr4or5 && !addr6) {
633                 meshhdr->flags |= MESH_FLAGS_AE_A4;
634                 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
635                 return 2 * ETH_ALEN;
636         } else if (addr4or5 && addr6) {
637                 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
638                 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
639                 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
640                 return 3 * ETH_ALEN;
641         }
642
643         return ETH_ALEN;
644 }
645
646 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
647 {
648         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
649         u32 changed;
650
651         if (ifmsh->mshcfg.plink_timeout > 0)
652                 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
653         mesh_path_expire(sdata);
654
655         changed = mesh_accept_plinks_update(sdata);
656         ieee80211_mbss_info_change_notify(sdata, changed);
657
658         mod_timer(&ifmsh->housekeeping_timer,
659                   round_jiffies(jiffies +
660                                 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
661 }
662
663 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
664 {
665         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
666         u32 interval;
667
668         mesh_path_tx_root_frame(sdata);
669
670         if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
671                 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
672         else
673                 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
674
675         mod_timer(&ifmsh->mesh_path_root_timer,
676                   round_jiffies(TU_TO_EXP_TIME(interval)));
677 }
678
679 static int
680 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
681 {
682         struct beacon_data *bcn;
683         int head_len, tail_len;
684         struct sk_buff *skb;
685         struct ieee80211_mgmt *mgmt;
686         struct ieee80211_chanctx_conf *chanctx_conf;
687         struct mesh_csa_settings *csa;
688         enum ieee80211_band band;
689         u8 *pos;
690         struct ieee80211_sub_if_data *sdata;
691         int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
692                       sizeof(mgmt->u.beacon);
693
694         sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
695         rcu_read_lock();
696         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
697         band = chanctx_conf->def.chan->band;
698         rcu_read_unlock();
699
700         head_len = hdr_len +
701                    2 + /* NULL SSID */
702                    /* Channel Switch Announcement */
703                    2 + sizeof(struct ieee80211_channel_sw_ie) +
704                    /* Mesh Channel Swith Parameters */
705                    2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
706                    2 + 8 + /* supported rates */
707                    2 + 3; /* DS params */
708         tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
709                    2 + sizeof(struct ieee80211_ht_cap) +
710                    2 + sizeof(struct ieee80211_ht_operation) +
711                    2 + ifmsh->mesh_id_len +
712                    2 + sizeof(struct ieee80211_meshconf_ie) +
713                    2 + sizeof(__le16) + /* awake window */
714                    2 + sizeof(struct ieee80211_vht_cap) +
715                    2 + sizeof(struct ieee80211_vht_operation) +
716                    ifmsh->ie_len;
717
718         bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
719         /* need an skb for IE builders to operate on */
720         skb = dev_alloc_skb(max(head_len, tail_len));
721
722         if (!bcn || !skb)
723                 goto out_free;
724
725         /*
726          * pointers go into the block we allocated,
727          * memory is | beacon_data | head | tail |
728          */
729         bcn->head = ((u8 *) bcn) + sizeof(*bcn);
730
731         /* fill in the head */
732         mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
733         memset(mgmt, 0, hdr_len);
734         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
735                                           IEEE80211_STYPE_BEACON);
736         eth_broadcast_addr(mgmt->da);
737         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
738         memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
739         ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
740         mgmt->u.beacon.beacon_int =
741                 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
742         mgmt->u.beacon.capab_info |= cpu_to_le16(
743                 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
744
745         pos = skb_put(skb, 2);
746         *pos++ = WLAN_EID_SSID;
747         *pos++ = 0x0;
748
749         rcu_read_lock();
750         csa = rcu_dereference(ifmsh->csa);
751         if (csa) {
752                 pos = skb_put(skb, 13);
753                 memset(pos, 0, 13);
754                 *pos++ = WLAN_EID_CHANNEL_SWITCH;
755                 *pos++ = 3;
756                 *pos++ = 0x0;
757                 *pos++ = ieee80211_frequency_to_channel(
758                                 csa->settings.chandef.chan->center_freq);
759                 bcn->csa_current_counter = csa->settings.count;
760                 bcn->csa_counter_offsets[0] = hdr_len + 6;
761                 *pos++ = csa->settings.count;
762                 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
763                 *pos++ = 6;
764                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
765                         *pos++ = ifmsh->mshcfg.dot11MeshTTL;
766                         *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
767                 } else {
768                         *pos++ = ifmsh->chsw_ttl;
769                 }
770                 *pos++ |= csa->settings.block_tx ?
771                           WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
772                 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
773                 pos += 2;
774                 put_unaligned_le16(ifmsh->pre_value, pos);
775                 pos += 2;
776         }
777         rcu_read_unlock();
778
779         if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
780             mesh_add_ds_params_ie(sdata, skb))
781                 goto out_free;
782
783         bcn->head_len = skb->len;
784         memcpy(bcn->head, skb->data, bcn->head_len);
785
786         /* now the tail */
787         skb_trim(skb, 0);
788         bcn->tail = bcn->head + bcn->head_len;
789
790         if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
791             mesh_add_rsn_ie(sdata, skb) ||
792             mesh_add_ht_cap_ie(sdata, skb) ||
793             mesh_add_ht_oper_ie(sdata, skb) ||
794             mesh_add_meshid_ie(sdata, skb) ||
795             mesh_add_meshconf_ie(sdata, skb) ||
796             mesh_add_awake_window_ie(sdata, skb) ||
797             mesh_add_vht_cap_ie(sdata, skb) ||
798             mesh_add_vht_oper_ie(sdata, skb) ||
799             mesh_add_vendor_ies(sdata, skb))
800                 goto out_free;
801
802         bcn->tail_len = skb->len;
803         memcpy(bcn->tail, skb->data, bcn->tail_len);
804         bcn->meshconf = (struct ieee80211_meshconf_ie *)
805                                         (bcn->tail + ifmsh->meshconf_offset);
806
807         dev_kfree_skb(skb);
808         rcu_assign_pointer(ifmsh->beacon, bcn);
809         return 0;
810 out_free:
811         kfree(bcn);
812         dev_kfree_skb(skb);
813         return -ENOMEM;
814 }
815
816 static int
817 ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
818 {
819         struct beacon_data *old_bcn;
820         int ret;
821
822         old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
823                                             lockdep_is_held(&sdata->wdev.mtx));
824         ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
825         if (ret)
826                 /* just reuse old beacon */
827                 return ret;
828
829         if (old_bcn)
830                 kfree_rcu(old_bcn, rcu_head);
831         return 0;
832 }
833
834 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
835                                        u32 changed)
836 {
837         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
838         unsigned long bits = changed;
839         u32 bit;
840
841         if (!bits)
842                 return;
843
844         /* if we race with running work, worst case this work becomes a noop */
845         for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
846                 set_bit(bit, &ifmsh->mbss_changed);
847         set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
848         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
849 }
850
851 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
852 {
853         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
854         struct ieee80211_local *local = sdata->local;
855         u32 changed = BSS_CHANGED_BEACON |
856                       BSS_CHANGED_BEACON_ENABLED |
857                       BSS_CHANGED_HT |
858                       BSS_CHANGED_BASIC_RATES |
859                       BSS_CHANGED_BEACON_INT;
860
861         local->fif_other_bss++;
862         /* mesh ifaces must set allmulti to forward mcast traffic */
863         atomic_inc(&local->iff_allmultis);
864         ieee80211_configure_filter(local);
865
866         ifmsh->mesh_cc_id = 0;  /* Disabled */
867         /* register sync ops from extensible synchronization framework */
868         ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
869         ifmsh->adjusting_tbtt = false;
870         ifmsh->sync_offset_clockdrift_max = 0;
871         set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
872         ieee80211_mesh_root_setup(ifmsh);
873         ieee80211_queue_work(&local->hw, &sdata->work);
874         sdata->vif.bss_conf.ht_operation_mode =
875                                 ifmsh->mshcfg.ht_opmode;
876         sdata->vif.bss_conf.enable_beacon = true;
877
878         changed |= ieee80211_mps_local_status_update(sdata);
879
880         if (ieee80211_mesh_build_beacon(ifmsh)) {
881                 ieee80211_stop_mesh(sdata);
882                 return -ENOMEM;
883         }
884
885         ieee80211_recalc_dtim(local, sdata);
886         ieee80211_bss_info_change_notify(sdata, changed);
887
888         netif_carrier_on(sdata->dev);
889         return 0;
890 }
891
892 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
893 {
894         struct ieee80211_local *local = sdata->local;
895         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
896         struct beacon_data *bcn;
897
898         netif_carrier_off(sdata->dev);
899
900         /* stop the beacon */
901         ifmsh->mesh_id_len = 0;
902         sdata->vif.bss_conf.enable_beacon = false;
903         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
904         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
905         bcn = rcu_dereference_protected(ifmsh->beacon,
906                                         lockdep_is_held(&sdata->wdev.mtx));
907         RCU_INIT_POINTER(ifmsh->beacon, NULL);
908         kfree_rcu(bcn, rcu_head);
909
910         /* flush STAs and mpaths on this iface */
911         sta_info_flush(sdata);
912         mesh_path_flush_by_iface(sdata);
913
914         /* free all potentially still buffered group-addressed frames */
915         local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
916         skb_queue_purge(&ifmsh->ps.bc_buf);
917
918         del_timer_sync(&sdata->u.mesh.housekeeping_timer);
919         del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
920         del_timer_sync(&sdata->u.mesh.mesh_path_timer);
921
922         /* clear any mesh work (for next join) we may have accrued */
923         ifmsh->wrkq_flags = 0;
924         ifmsh->mbss_changed = 0;
925
926         local->fif_other_bss--;
927         atomic_dec(&local->iff_allmultis);
928         ieee80211_configure_filter(local);
929 }
930
931 static bool
932 ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
933                                  struct ieee802_11_elems *elems, bool beacon)
934 {
935         struct cfg80211_csa_settings params;
936         struct ieee80211_csa_ie csa_ie;
937         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
938         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
939         int err;
940         u32 sta_flags;
941
942         sdata_assert_lock(sdata);
943
944         sta_flags = IEEE80211_STA_DISABLE_VHT;
945         switch (sdata->vif.bss_conf.chandef.width) {
946         case NL80211_CHAN_WIDTH_20_NOHT:
947                 sta_flags |= IEEE80211_STA_DISABLE_HT;
948         case NL80211_CHAN_WIDTH_20:
949                 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
950                 break;
951         default:
952                 break;
953         }
954
955         memset(&params, 0, sizeof(params));
956         memset(&csa_ie, 0, sizeof(csa_ie));
957         err = ieee80211_parse_ch_switch_ie(sdata, elems, band,
958                                            sta_flags, sdata->vif.addr,
959                                            &csa_ie);
960         if (err < 0)
961                 return false;
962         if (err)
963                 return false;
964
965         params.chandef = csa_ie.chandef;
966         params.count = csa_ie.count;
967
968         if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
969                                      IEEE80211_CHAN_DISABLED)) {
970                 sdata_info(sdata,
971                            "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
972                            sdata->vif.addr,
973                            params.chandef.chan->center_freq,
974                            params.chandef.width,
975                            params.chandef.center_freq1,
976                            params.chandef.center_freq2);
977                 return false;
978         }
979
980         err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
981                                             &params.chandef,
982                                             NL80211_IFTYPE_MESH_POINT);
983         if (err < 0)
984                 return false;
985         if (err > 0)
986                 /* TODO: DFS not (yet) supported */
987                 return false;
988
989         params.radar_required = err;
990
991         if (cfg80211_chandef_identical(&params.chandef,
992                                        &sdata->vif.bss_conf.chandef)) {
993                 mcsa_dbg(sdata,
994                          "received csa with an identical chandef, ignoring\n");
995                 return true;
996         }
997
998         mcsa_dbg(sdata,
999                  "received channel switch announcement to go to channel %d MHz\n",
1000                  params.chandef.chan->center_freq);
1001
1002         params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
1003         if (beacon) {
1004                 ifmsh->chsw_ttl = csa_ie.ttl - 1;
1005                 if (ifmsh->pre_value >= csa_ie.pre_value)
1006                         return false;
1007                 ifmsh->pre_value = csa_ie.pre_value;
1008         }
1009
1010         if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
1011                 return false;
1012
1013         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
1014
1015         if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1016                                      &params) < 0)
1017                 return false;
1018
1019         return true;
1020 }
1021
1022 static void
1023 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1024                             struct ieee80211_mgmt *mgmt, size_t len)
1025 {
1026         struct ieee80211_local *local = sdata->local;
1027         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1028         struct sk_buff *presp;
1029         struct beacon_data *bcn;
1030         struct ieee80211_mgmt *hdr;
1031         struct ieee802_11_elems elems;
1032         size_t baselen;
1033         u8 *pos;
1034
1035         pos = mgmt->u.probe_req.variable;
1036         baselen = (u8 *) pos - (u8 *) mgmt;
1037         if (baselen > len)
1038                 return;
1039
1040         ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1041
1042         if (!elems.mesh_id)
1043                 return;
1044
1045         /* 802.11-2012 10.1.4.3.2 */
1046         if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1047              !is_broadcast_ether_addr(mgmt->da)) ||
1048             elems.ssid_len != 0)
1049                 return;
1050
1051         if (elems.mesh_id_len != 0 &&
1052             (elems.mesh_id_len != ifmsh->mesh_id_len ||
1053              memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
1054                 return;
1055
1056         rcu_read_lock();
1057         bcn = rcu_dereference(ifmsh->beacon);
1058
1059         if (!bcn)
1060                 goto out;
1061
1062         presp = dev_alloc_skb(local->tx_headroom +
1063                               bcn->head_len + bcn->tail_len);
1064         if (!presp)
1065                 goto out;
1066
1067         skb_reserve(presp, local->tx_headroom);
1068         memcpy(skb_put(presp, bcn->head_len), bcn->head, bcn->head_len);
1069         memcpy(skb_put(presp, bcn->tail_len), bcn->tail, bcn->tail_len);
1070         hdr = (struct ieee80211_mgmt *) presp->data;
1071         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1072                                          IEEE80211_STYPE_PROBE_RESP);
1073         memcpy(hdr->da, mgmt->sa, ETH_ALEN);
1074         IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1075         ieee80211_tx_skb(sdata, presp);
1076 out:
1077         rcu_read_unlock();
1078 }
1079
1080 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1081                                         u16 stype,
1082                                         struct ieee80211_mgmt *mgmt,
1083                                         size_t len,
1084                                         struct ieee80211_rx_status *rx_status)
1085 {
1086         struct ieee80211_local *local = sdata->local;
1087         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1088         struct ieee802_11_elems elems;
1089         struct ieee80211_channel *channel;
1090         size_t baselen;
1091         int freq;
1092         enum ieee80211_band band = rx_status->band;
1093
1094         /* ignore ProbeResp to foreign address */
1095         if (stype == IEEE80211_STYPE_PROBE_RESP &&
1096             !ether_addr_equal(mgmt->da, sdata->vif.addr))
1097                 return;
1098
1099         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1100         if (baselen > len)
1101                 return;
1102
1103         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1104                                false, &elems);
1105
1106         /* ignore non-mesh or secure / unsecure mismatch */
1107         if ((!elems.mesh_id || !elems.mesh_config) ||
1108             (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1109             (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
1110                 return;
1111
1112         if (elems.ds_params)
1113                 freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
1114         else
1115                 freq = rx_status->freq;
1116
1117         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1118
1119         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1120                 return;
1121
1122         if (mesh_matches_local(sdata, &elems))
1123                 mesh_neighbour_update(sdata, mgmt->sa, &elems);
1124
1125         if (ifmsh->sync_ops)
1126                 ifmsh->sync_ops->rx_bcn_presp(sdata,
1127                         stype, mgmt, &elems, rx_status);
1128
1129         if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1130             !sdata->vif.csa_active)
1131                 ieee80211_mesh_process_chnswitch(sdata, &elems, true);
1132 }
1133
1134 int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1135 {
1136         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1137         struct mesh_csa_settings *tmp_csa_settings;
1138         int ret = 0;
1139         int changed = 0;
1140
1141         /* Reset the TTL value and Initiator flag */
1142         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1143         ifmsh->chsw_ttl = 0;
1144
1145         /* Remove the CSA and MCSP elements from the beacon */
1146         tmp_csa_settings = rcu_dereference(ifmsh->csa);
1147         RCU_INIT_POINTER(ifmsh->csa, NULL);
1148         if (tmp_csa_settings)
1149                 kfree_rcu(tmp_csa_settings, rcu_head);
1150         ret = ieee80211_mesh_rebuild_beacon(sdata);
1151         if (ret)
1152                 return -EINVAL;
1153
1154         changed |= BSS_CHANGED_BEACON;
1155
1156         mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1157                  sdata->vif.bss_conf.chandef.chan->center_freq);
1158         return changed;
1159 }
1160
1161 int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1162                               struct cfg80211_csa_settings *csa_settings)
1163 {
1164         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1165         struct mesh_csa_settings *tmp_csa_settings;
1166         int ret = 0;
1167
1168         tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1169                                    GFP_ATOMIC);
1170         if (!tmp_csa_settings)
1171                 return -ENOMEM;
1172
1173         memcpy(&tmp_csa_settings->settings, csa_settings,
1174                sizeof(struct cfg80211_csa_settings));
1175
1176         rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1177
1178         ret = ieee80211_mesh_rebuild_beacon(sdata);
1179         if (ret) {
1180                 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1181                 RCU_INIT_POINTER(ifmsh->csa, NULL);
1182                 kfree_rcu(tmp_csa_settings, rcu_head);
1183                 return ret;
1184         }
1185
1186         return BSS_CHANGED_BEACON;
1187 }
1188
1189 static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1190                                struct ieee80211_mgmt *mgmt, size_t len)
1191 {
1192         struct ieee80211_mgmt *mgmt_fwd;
1193         struct sk_buff *skb;
1194         struct ieee80211_local *local = sdata->local;
1195         u8 *pos = mgmt->u.action.u.chan_switch.variable;
1196         size_t offset_ttl;
1197
1198         skb = dev_alloc_skb(local->tx_headroom + len);
1199         if (!skb)
1200                 return -ENOMEM;
1201         skb_reserve(skb, local->tx_headroom);
1202         mgmt_fwd = (struct ieee80211_mgmt *) skb_put(skb, len);
1203
1204         /* offset_ttl is based on whether the secondary channel
1205          * offset is available or not. Subtract 1 from the mesh TTL
1206          * and disable the initiator flag before forwarding.
1207          */
1208         offset_ttl = (len < 42) ? 7 : 10;
1209         *(pos + offset_ttl) -= 1;
1210         *(pos + offset_ttl + 1) &= ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1211
1212         memcpy(mgmt_fwd, mgmt, len);
1213         eth_broadcast_addr(mgmt_fwd->da);
1214         memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1215         memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1216
1217         ieee80211_tx_skb(sdata, skb);
1218         return 0;
1219 }
1220
1221 static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1222                               struct ieee80211_mgmt *mgmt, size_t len)
1223 {
1224         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1225         struct ieee802_11_elems elems;
1226         u16 pre_value;
1227         bool fwd_csa = true;
1228         size_t baselen;
1229         u8 *pos;
1230
1231         if (mgmt->u.action.u.measurement.action_code !=
1232             WLAN_ACTION_SPCT_CHL_SWITCH)
1233                 return;
1234
1235         pos = mgmt->u.action.u.chan_switch.variable;
1236         baselen = offsetof(struct ieee80211_mgmt,
1237                            u.action.u.chan_switch.variable);
1238         ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1239
1240         ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
1241         if (!--ifmsh->chsw_ttl)
1242                 fwd_csa = false;
1243
1244         pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
1245         if (ifmsh->pre_value >= pre_value)
1246                 return;
1247
1248         ifmsh->pre_value = pre_value;
1249
1250         if (!sdata->vif.csa_active &&
1251             !ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
1252                 mcsa_dbg(sdata, "Failed to process CSA action frame");
1253                 return;
1254         }
1255
1256         /* forward or re-broadcast the CSA frame */
1257         if (fwd_csa) {
1258                 if (mesh_fwd_csa_frame(sdata, mgmt, len) < 0)
1259                         mcsa_dbg(sdata, "Failed to forward the CSA frame");
1260         }
1261 }
1262
1263 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1264                                           struct ieee80211_mgmt *mgmt,
1265                                           size_t len,
1266                                           struct ieee80211_rx_status *rx_status)
1267 {
1268         switch (mgmt->u.action.category) {
1269         case WLAN_CATEGORY_SELF_PROTECTED:
1270                 switch (mgmt->u.action.u.self_prot.action_code) {
1271                 case WLAN_SP_MESH_PEERING_OPEN:
1272                 case WLAN_SP_MESH_PEERING_CLOSE:
1273                 case WLAN_SP_MESH_PEERING_CONFIRM:
1274                         mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1275                         break;
1276                 }
1277                 break;
1278         case WLAN_CATEGORY_MESH_ACTION:
1279                 if (mesh_action_is_path_sel(mgmt))
1280                         mesh_rx_path_sel_frame(sdata, mgmt, len);
1281                 break;
1282         case WLAN_CATEGORY_SPECTRUM_MGMT:
1283                 mesh_rx_csa_frame(sdata, mgmt, len);
1284                 break;
1285         }
1286 }
1287
1288 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1289                                    struct sk_buff *skb)
1290 {
1291         struct ieee80211_rx_status *rx_status;
1292         struct ieee80211_mgmt *mgmt;
1293         u16 stype;
1294
1295         sdata_lock(sdata);
1296
1297         /* mesh already went down */
1298         if (!sdata->u.mesh.mesh_id_len)
1299                 goto out;
1300
1301         rx_status = IEEE80211_SKB_RXCB(skb);
1302         mgmt = (struct ieee80211_mgmt *) skb->data;
1303         stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1304
1305         switch (stype) {
1306         case IEEE80211_STYPE_PROBE_RESP:
1307         case IEEE80211_STYPE_BEACON:
1308                 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1309                                             rx_status);
1310                 break;
1311         case IEEE80211_STYPE_PROBE_REQ:
1312                 ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1313                 break;
1314         case IEEE80211_STYPE_ACTION:
1315                 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1316                 break;
1317         }
1318 out:
1319         sdata_unlock(sdata);
1320 }
1321
1322 static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1323 {
1324         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1325         u32 bit, changed = 0;
1326
1327         for_each_set_bit(bit, &ifmsh->mbss_changed,
1328                          sizeof(changed) * BITS_PER_BYTE) {
1329                 clear_bit(bit, &ifmsh->mbss_changed);
1330                 changed |= BIT(bit);
1331         }
1332
1333         if (sdata->vif.bss_conf.enable_beacon &&
1334             (changed & (BSS_CHANGED_BEACON |
1335                         BSS_CHANGED_HT |
1336                         BSS_CHANGED_BASIC_RATES |
1337                         BSS_CHANGED_BEACON_INT)))
1338                 if (ieee80211_mesh_rebuild_beacon(sdata))
1339                         return;
1340
1341         ieee80211_bss_info_change_notify(sdata, changed);
1342 }
1343
1344 void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
1345 {
1346         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1347
1348         sdata_lock(sdata);
1349
1350         /* mesh already went down */
1351         if (!sdata->u.mesh.mesh_id_len)
1352                 goto out;
1353
1354         if (ifmsh->preq_queue_len &&
1355             time_after(jiffies,
1356                        ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1357                 mesh_path_start_discovery(sdata);
1358
1359         if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
1360                 mesh_mpath_table_grow();
1361
1362         if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
1363                 mesh_mpp_table_grow();
1364
1365         if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
1366                 ieee80211_mesh_housekeeping(sdata);
1367
1368         if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1369                 ieee80211_mesh_rootpath(sdata);
1370
1371         if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1372                 mesh_sync_adjust_tbtt(sdata);
1373
1374         if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1375                 mesh_bss_info_changed(sdata);
1376 out:
1377         sdata_unlock(sdata);
1378 }
1379
1380
1381 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1382 {
1383         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1384         static u8 zero_addr[ETH_ALEN] = {};
1385
1386         setup_timer(&ifmsh->housekeeping_timer,
1387                     ieee80211_mesh_housekeeping_timer,
1388                     (unsigned long) sdata);
1389
1390         ifmsh->accepting_plinks = true;
1391         atomic_set(&ifmsh->mpaths, 0);
1392         mesh_rmc_init(sdata);
1393         ifmsh->last_preq = jiffies;
1394         ifmsh->next_perr = jiffies;
1395         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1396         /* Allocate all mesh structures when creating the first mesh interface. */
1397         if (!mesh_allocated)
1398                 ieee80211s_init();
1399         setup_timer(&ifmsh->mesh_path_timer,
1400                     ieee80211_mesh_path_timer,
1401                     (unsigned long) sdata);
1402         setup_timer(&ifmsh->mesh_path_root_timer,
1403                     ieee80211_mesh_path_root_timer,
1404                     (unsigned long) sdata);
1405         INIT_LIST_HEAD(&ifmsh->preq_queue.list);
1406         skb_queue_head_init(&ifmsh->ps.bc_buf);
1407         spin_lock_init(&ifmsh->mesh_preq_queue_lock);
1408         spin_lock_init(&ifmsh->sync_offset_lock);
1409         RCU_INIT_POINTER(ifmsh->beacon, NULL);
1410
1411         sdata->vif.bss_conf.bssid = zero_addr;
1412 }