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