~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/mesh.c

Version: ~ [ linux-5.11-rc3 ] ~ [ linux-5.10.7 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.89 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.167 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.215 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.251 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.251 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp