bbd56b0878995141b3f54701e234fd7378037cda
[safe/jmp/linux-2.6] / 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 <asm/unaligned.h>
12 #include "ieee80211_i.h"
13 #include "mesh.h"
14
15 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
16 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
17 #define IEEE80211_MESH_RANN_INTERVAL         (1 * HZ)
18
19 #define MESHCONF_PP_OFFSET      0               /* Path Selection Protocol */
20 #define MESHCONF_PM_OFFSET      1               /* Path Selection Metric   */
21 #define MESHCONF_CC_OFFSET      2               /* Congestion Control Mode */
22 #define MESHCONF_SP_OFFSET      3               /* Synchronization Protocol */
23 #define MESHCONF_AUTH_OFFSET    4               /* Authentication Protocol */
24 #define MESHCONF_CAPAB_OFFSET   6
25 #define MESHCONF_CAPAB_ACCEPT_PLINKS 0x01
26 #define MESHCONF_CAPAB_FORWARDING    0x08
27
28 #define TMR_RUNNING_HK  0
29 #define TMR_RUNNING_MP  1
30 #define TMR_RUNNING_MPR 2
31
32 int mesh_allocated;
33 static struct kmem_cache *rm_cache;
34
35 void ieee80211s_init(void)
36 {
37         mesh_pathtbl_init();
38         mesh_allocated = 1;
39         rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
40                                      0, 0, NULL);
41 }
42
43 void ieee80211s_stop(void)
44 {
45         mesh_pathtbl_unregister();
46         kmem_cache_destroy(rm_cache);
47 }
48
49 static void ieee80211_mesh_housekeeping_timer(unsigned long data)
50 {
51         struct ieee80211_sub_if_data *sdata = (void *) data;
52         struct ieee80211_local *local = sdata->local;
53         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
54
55         set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
56
57         if (local->quiescing) {
58                 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
59                 return;
60         }
61
62         ieee80211_queue_work(&local->hw, &ifmsh->work);
63 }
64
65 /**
66  * mesh_matches_local - check if the config of a mesh point matches ours
67  *
68  * @ie: information elements of a management frame from the mesh peer
69  * @sdata: local mesh subif
70  *
71  * This function checks if the mesh configuration of a mesh point matches the
72  * local mesh configuration, i.e. if both nodes belong to the same mesh network.
73  */
74 bool mesh_matches_local(struct ieee802_11_elems *ie, struct ieee80211_sub_if_data *sdata)
75 {
76         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
77
78         /*
79          * As support for each feature is added, check for matching
80          * - On mesh config capabilities
81          *   - Power Save Support En
82          *   - Sync support enabled
83          *   - Sync support active
84          *   - Sync support required from peer
85          *   - MDA enabled
86          * - Power management control on fc
87          */
88         if (ifmsh->mesh_id_len == ie->mesh_id_len &&
89                 memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
90                 (ifmsh->mesh_pp_id == *(ie->mesh_config + MESHCONF_PP_OFFSET))&&
91                 (ifmsh->mesh_pm_id == *(ie->mesh_config + MESHCONF_PM_OFFSET))&&
92                 (ifmsh->mesh_cc_id == *(ie->mesh_config + MESHCONF_CC_OFFSET))&&
93                 (ifmsh->mesh_sp_id == *(ie->mesh_config + MESHCONF_SP_OFFSET))&&
94                 (ifmsh->mesh_auth_id == *(ie->mesh_config +
95                     MESHCONF_AUTH_OFFSET)))
96                 return true;
97
98         return false;
99 }
100
101 /**
102  * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
103  *
104  * @ie: information elements of a management frame from the mesh peer
105  */
106 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
107 {
108         return (*(ie->mesh_config + MESHCONF_CAPAB_OFFSET) &
109             MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
110 }
111
112 /**
113  * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
114  *
115  * @sdata: mesh interface in which mesh beacons are going to be updated
116  */
117 void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
118 {
119         bool free_plinks;
120
121         /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
122          * the mesh interface might be able to establish plinks with peers that
123          * are already on the table but are not on PLINK_ESTAB state. However,
124          * in general the mesh interface is not accepting peer link requests
125          * from new peers, and that must be reflected in the beacon
126          */
127         free_plinks = mesh_plink_availables(sdata);
128
129         if (free_plinks != sdata->u.mesh.accepting_plinks)
130                 ieee80211_mesh_housekeeping_timer((unsigned long) sdata);
131 }
132
133 void mesh_ids_set_default(struct ieee80211_if_mesh *sta)
134 {
135         sta->mesh_pp_id = 0;    /* HWMP */
136         sta->mesh_pm_id = 0;    /* Airtime */
137         sta->mesh_cc_id = 0;    /* Disabled */
138         sta->mesh_sp_id = 0;    /* Neighbor Offset */
139         sta->mesh_auth_id = 0;  /* Disabled */
140 }
141
142 int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
143 {
144         int i;
145
146         sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
147         if (!sdata->u.mesh.rmc)
148                 return -ENOMEM;
149         sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
150         for (i = 0; i < RMC_BUCKETS; i++)
151                 INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i].list);
152         return 0;
153 }
154
155 void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
156 {
157         struct mesh_rmc *rmc = sdata->u.mesh.rmc;
158         struct rmc_entry *p, *n;
159         int i;
160
161         if (!sdata->u.mesh.rmc)
162                 return;
163
164         for (i = 0; i < RMC_BUCKETS; i++)
165                 list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
166                         list_del(&p->list);
167                         kmem_cache_free(rm_cache, p);
168                 }
169
170         kfree(rmc);
171         sdata->u.mesh.rmc = NULL;
172 }
173
174 /**
175  * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
176  *
177  * @sa:         source address
178  * @mesh_hdr:   mesh_header
179  *
180  * Returns: 0 if the frame is not in the cache, nonzero otherwise.
181  *
182  * Checks using the source address and the mesh sequence number if we have
183  * received this frame lately. If the frame is not in the cache, it is added to
184  * it.
185  */
186 int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
187                    struct ieee80211_sub_if_data *sdata)
188 {
189         struct mesh_rmc *rmc = sdata->u.mesh.rmc;
190         u32 seqnum = 0;
191         int entries = 0;
192         u8 idx;
193         struct rmc_entry *p, *n;
194
195         /* Don't care about endianness since only match matters */
196         memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
197         idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
198         list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
199                 ++entries;
200                 if (time_after(jiffies, p->exp_time) ||
201                                 (entries == RMC_QUEUE_MAX_LEN)) {
202                         list_del(&p->list);
203                         kmem_cache_free(rm_cache, p);
204                         --entries;
205                 } else if ((seqnum == p->seqnum)
206                                 && (memcmp(sa, p->sa, ETH_ALEN) == 0))
207                         return -1;
208         }
209
210         p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
211         if (!p) {
212                 printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
213                 return 0;
214         }
215         p->seqnum = seqnum;
216         p->exp_time = jiffies + RMC_TIMEOUT;
217         memcpy(p->sa, sa, ETH_ALEN);
218         list_add(&p->list, &rmc->bucket[idx].list);
219         return 0;
220 }
221
222 void mesh_mgmt_ies_add(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
223 {
224         struct ieee80211_local *local = sdata->local;
225         struct ieee80211_supported_band *sband;
226         u8 *pos;
227         int len, i, rate;
228         u8 neighbors;
229
230         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
231         len = sband->n_bitrates;
232         if (len > 8)
233                 len = 8;
234         pos = skb_put(skb, len + 2);
235         *pos++ = WLAN_EID_SUPP_RATES;
236         *pos++ = len;
237         for (i = 0; i < len; i++) {
238                 rate = sband->bitrates[i].bitrate;
239                 *pos++ = (u8) (rate / 5);
240         }
241
242         if (sband->n_bitrates > len) {
243                 pos = skb_put(skb, sband->n_bitrates - len + 2);
244                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
245                 *pos++ = sband->n_bitrates - len;
246                 for (i = len; i < sband->n_bitrates; i++) {
247                         rate = sband->bitrates[i].bitrate;
248                         *pos++ = (u8) (rate / 5);
249                 }
250         }
251
252         if (sband->band == IEEE80211_BAND_2GHZ) {
253                 pos = skb_put(skb, 2 + 1);
254                 *pos++ = WLAN_EID_DS_PARAMS;
255                 *pos++ = 1;
256                 *pos++ = ieee80211_frequency_to_channel(local->hw.conf.channel->center_freq);
257         }
258
259         pos = skb_put(skb, 2 + sdata->u.mesh.mesh_id_len);
260         *pos++ = WLAN_EID_MESH_ID;
261         *pos++ = sdata->u.mesh.mesh_id_len;
262         if (sdata->u.mesh.mesh_id_len)
263                 memcpy(pos, sdata->u.mesh.mesh_id, sdata->u.mesh.mesh_id_len);
264
265         pos = skb_put(skb, 2 + IEEE80211_MESH_CONFIG_LEN);
266         *pos++ = WLAN_EID_MESH_CONFIG;
267         *pos++ = IEEE80211_MESH_CONFIG_LEN;
268
269         /* Active path selection protocol ID */
270         *pos++ = sdata->u.mesh.mesh_pp_id;
271
272         /* Active path selection metric ID   */
273         *pos++ = sdata->u.mesh.mesh_pm_id;
274
275         /* Congestion control mode identifier */
276         *pos++ = sdata->u.mesh.mesh_cc_id;
277
278         /* Synchronization protocol identifier */
279         *pos++ = sdata->u.mesh.mesh_sp_id;
280
281         /* Authentication Protocol identifier */
282         *pos++ = sdata->u.mesh.mesh_auth_id;
283
284         /* Mesh Formation Info - number of neighbors */
285         neighbors = atomic_read(&sdata->u.mesh.mshstats.estab_plinks);
286         /* Number of neighbor mesh STAs or 15 whichever is smaller */
287         neighbors = (neighbors > 15) ? 15 : neighbors;
288         *pos++ = neighbors << 1;
289
290         /* Mesh capability */
291         sdata->u.mesh.accepting_plinks = mesh_plink_availables(sdata);
292         *pos = MESHCONF_CAPAB_FORWARDING;
293         *pos++ |= sdata->u.mesh.accepting_plinks ?
294             MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
295         *pos++ = 0x00;
296
297         return;
298 }
299
300 u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, struct mesh_table *tbl)
301 {
302         /* Use last four bytes of hw addr and interface index as hash index */
303         return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
304                 & tbl->hash_mask;
305 }
306
307 struct mesh_table *mesh_table_alloc(int size_order)
308 {
309         int i;
310         struct mesh_table *newtbl;
311
312         newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL);
313         if (!newtbl)
314                 return NULL;
315
316         newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
317                         (1 << size_order), GFP_KERNEL);
318
319         if (!newtbl->hash_buckets) {
320                 kfree(newtbl);
321                 return NULL;
322         }
323
324         newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
325                         (1 << size_order), GFP_KERNEL);
326         if (!newtbl->hashwlock) {
327                 kfree(newtbl->hash_buckets);
328                 kfree(newtbl);
329                 return NULL;
330         }
331
332         newtbl->size_order = size_order;
333         newtbl->hash_mask = (1 << size_order) - 1;
334         atomic_set(&newtbl->entries,  0);
335         get_random_bytes(&newtbl->hash_rnd,
336                         sizeof(newtbl->hash_rnd));
337         for (i = 0; i <= newtbl->hash_mask; i++)
338                 spin_lock_init(&newtbl->hashwlock[i]);
339
340         return newtbl;
341 }
342
343
344 static void ieee80211_mesh_path_timer(unsigned long data)
345 {
346         struct ieee80211_sub_if_data *sdata =
347                 (struct ieee80211_sub_if_data *) data;
348         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
349         struct ieee80211_local *local = sdata->local;
350
351         if (local->quiescing) {
352                 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
353                 return;
354         }
355
356         ieee80211_queue_work(&local->hw, &ifmsh->work);
357 }
358
359 static void ieee80211_mesh_path_root_timer(unsigned long data)
360 {
361         struct ieee80211_sub_if_data *sdata =
362                 (struct ieee80211_sub_if_data *) data;
363         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
364         struct ieee80211_local *local = sdata->local;
365
366         set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
367
368         if (local->quiescing) {
369                 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
370                 return;
371         }
372
373         ieee80211_queue_work(&local->hw, &ifmsh->work);
374 }
375
376 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
377 {
378         if (ifmsh->mshcfg.dot11MeshHWMPRootMode)
379                 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
380         else {
381                 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
382                 /* stop running timer */
383                 del_timer_sync(&ifmsh->mesh_path_root_timer);
384         }
385 }
386
387 /**
388  * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
389  * @hdr:        802.11 frame header
390  * @fc:         frame control field
391  * @meshda:     destination address in the mesh
392  * @meshsa:     source address address in the mesh.  Same as TA, as frame is
393  *              locally originated.
394  *
395  * Return the length of the 802.11 (does not include a mesh control header)
396  */
397 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc, char
398                 *meshda, char *meshsa) {
399         if (is_multicast_ether_addr(meshda)) {
400                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
401                 /* DA TA SA */
402                 memcpy(hdr->addr1, meshda, ETH_ALEN);
403                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
404                 memcpy(hdr->addr3, meshsa, ETH_ALEN);
405                 return 24;
406         } else {
407                 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
408                                 IEEE80211_FCTL_TODS);
409                 /* RA TA DA SA */
410                 memset(hdr->addr1, 0, ETH_ALEN);   /* RA is resolved later */
411                 memcpy(hdr->addr2, meshsa, ETH_ALEN);
412                 memcpy(hdr->addr3, meshda, ETH_ALEN);
413                 memcpy(hdr->addr4, meshsa, ETH_ALEN);
414                 return 30;
415         }
416 }
417
418 /**
419  * ieee80211_new_mesh_header - create a new mesh header
420  * @meshhdr:    uninitialized mesh header
421  * @sdata:      mesh interface to be used
422  * @addr4:      addr4 of the mesh frame (1st in ae header)
423  *              may be NULL
424  * @addr5:      addr5 of the mesh frame (1st or 2nd in ae header)
425  *              may be NULL unless addr6 is present
426  * @addr6:      addr6 of the mesh frame (2nd or 3rd in ae header)
427  *              may be NULL unless addr5 is present
428  *
429  * Return the header length.
430  */
431 int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
432                 struct ieee80211_sub_if_data *sdata, char *addr4,
433                 char *addr5, char *addr6)
434 {
435         int aelen = 0;
436         memset(meshhdr, 0, sizeof(meshhdr));
437         meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
438         put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
439         sdata->u.mesh.mesh_seqnum++;
440         if (addr4) {
441                 meshhdr->flags |= MESH_FLAGS_AE_A4;
442                 aelen += ETH_ALEN;
443                 memcpy(meshhdr->eaddr1, addr4, ETH_ALEN);
444         }
445         if (addr5 && addr6) {
446                 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
447                 aelen += 2 * ETH_ALEN;
448                 if (!addr4) {
449                         memcpy(meshhdr->eaddr1, addr5, ETH_ALEN);
450                         memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
451                 } else {
452                         memcpy(meshhdr->eaddr2, addr5, ETH_ALEN);
453                         memcpy(meshhdr->eaddr3, addr6, ETH_ALEN);
454                 }
455         }
456         return 6 + aelen;
457 }
458
459 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
460                            struct ieee80211_if_mesh *ifmsh)
461 {
462         bool free_plinks;
463
464 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
465         printk(KERN_DEBUG "%s: running mesh housekeeping\n",
466                sdata->dev->name);
467 #endif
468
469         ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
470         mesh_path_expire(sdata);
471
472         free_plinks = mesh_plink_availables(sdata);
473         if (free_plinks != sdata->u.mesh.accepting_plinks)
474                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
475
476         mod_timer(&ifmsh->housekeeping_timer,
477                   round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
478 }
479
480 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
481 {
482         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
483
484         mesh_path_tx_root_frame(sdata);
485         mod_timer(&ifmsh->mesh_path_root_timer,
486                   round_jiffies(jiffies + IEEE80211_MESH_RANN_INTERVAL));
487 }
488
489 #ifdef CONFIG_PM
490 void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
491 {
492         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
493
494         /* might restart the timer but that doesn't matter */
495         cancel_work_sync(&ifmsh->work);
496
497         /* use atomic bitops in case both timers fire at the same time */
498
499         if (del_timer_sync(&ifmsh->housekeeping_timer))
500                 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
501         if (del_timer_sync(&ifmsh->mesh_path_timer))
502                 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
503         if (del_timer_sync(&ifmsh->mesh_path_root_timer))
504                 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
505 }
506
507 void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
508 {
509         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
510
511         if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
512                 add_timer(&ifmsh->housekeeping_timer);
513         if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
514                 add_timer(&ifmsh->mesh_path_timer);
515         if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
516                 add_timer(&ifmsh->mesh_path_root_timer);
517         ieee80211_mesh_root_setup(ifmsh);
518 }
519 #endif
520
521 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
522 {
523         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
524         struct ieee80211_local *local = sdata->local;
525
526         set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
527         ieee80211_mesh_root_setup(ifmsh);
528         ieee80211_queue_work(&local->hw, &ifmsh->work);
529         sdata->vif.bss_conf.beacon_int = MESH_DEFAULT_BEACON_INTERVAL;
530         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON |
531                                                 BSS_CHANGED_BEACON_ENABLED |
532                                                 BSS_CHANGED_BEACON_INT);
533 }
534
535 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
536 {
537         del_timer_sync(&sdata->u.mesh.housekeeping_timer);
538         del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
539         /*
540          * If the timer fired while we waited for it, it will have
541          * requeued the work. Now the work will be running again
542          * but will not rearm the timer again because it checks
543          * whether the interface is running, which, at this point,
544          * it no longer is.
545          */
546         cancel_work_sync(&sdata->u.mesh.work);
547
548         /*
549          * When we get here, the interface is marked down.
550          * Call synchronize_rcu() to wait for the RX path
551          * should it be using the interface and enqueuing
552          * frames at this very time on another CPU.
553          */
554         rcu_barrier(); /* Wait for RX path and call_rcu()'s */
555         skb_queue_purge(&sdata->u.mesh.skb_queue);
556 }
557
558 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
559                                         u16 stype,
560                                         struct ieee80211_mgmt *mgmt,
561                                         size_t len,
562                                         struct ieee80211_rx_status *rx_status)
563 {
564         struct ieee80211_local *local = sdata->local;
565         struct ieee802_11_elems elems;
566         struct ieee80211_channel *channel;
567         u32 supp_rates = 0;
568         size_t baselen;
569         int freq;
570         enum ieee80211_band band = rx_status->band;
571
572         /* ignore ProbeResp to foreign address */
573         if (stype == IEEE80211_STYPE_PROBE_RESP &&
574             compare_ether_addr(mgmt->da, sdata->dev->dev_addr))
575                 return;
576
577         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
578         if (baselen > len)
579                 return;
580
581         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
582                                &elems);
583
584         if (elems.ds_params && elems.ds_params_len == 1)
585                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
586         else
587                 freq = rx_status->freq;
588
589         channel = ieee80211_get_channel(local->hw.wiphy, freq);
590
591         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
592                 return;
593
594         if (elems.mesh_id && elems.mesh_config &&
595             mesh_matches_local(&elems, sdata)) {
596                 supp_rates = ieee80211_sta_get_rates(local, &elems, band);
597
598                 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
599                                       mesh_peer_accepts_plinks(&elems));
600         }
601 }
602
603 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
604                                           struct ieee80211_mgmt *mgmt,
605                                           size_t len,
606                                           struct ieee80211_rx_status *rx_status)
607 {
608         switch (mgmt->u.action.category) {
609         case MESH_PLINK_CATEGORY:
610                 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
611                 break;
612         case MESH_PATH_SEL_CATEGORY:
613                 mesh_rx_path_sel_frame(sdata, mgmt, len);
614                 break;
615         }
616 }
617
618 static void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
619                                           struct sk_buff *skb)
620 {
621         struct ieee80211_rx_status *rx_status;
622         struct ieee80211_if_mesh *ifmsh;
623         struct ieee80211_mgmt *mgmt;
624         u16 stype;
625
626         ifmsh = &sdata->u.mesh;
627
628         rx_status = IEEE80211_SKB_RXCB(skb);
629         mgmt = (struct ieee80211_mgmt *) skb->data;
630         stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
631
632         switch (stype) {
633         case IEEE80211_STYPE_PROBE_RESP:
634         case IEEE80211_STYPE_BEACON:
635                 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
636                                             rx_status);
637                 break;
638         case IEEE80211_STYPE_ACTION:
639                 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
640                 break;
641         }
642
643         kfree_skb(skb);
644 }
645
646 static void ieee80211_mesh_work(struct work_struct *work)
647 {
648         struct ieee80211_sub_if_data *sdata =
649                 container_of(work, struct ieee80211_sub_if_data, u.mesh.work);
650         struct ieee80211_local *local = sdata->local;
651         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
652         struct sk_buff *skb;
653
654         if (!netif_running(sdata->dev))
655                 return;
656
657         if (local->scanning)
658                 return;
659
660         while ((skb = skb_dequeue(&ifmsh->skb_queue)))
661                 ieee80211_mesh_rx_queued_mgmt(sdata, skb);
662
663         if (ifmsh->preq_queue_len &&
664             time_after(jiffies,
665                        ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
666                 mesh_path_start_discovery(sdata);
667
668         if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
669                 mesh_mpath_table_grow();
670
671         if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
672                 mesh_mpp_table_grow();
673
674         if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
675                 ieee80211_mesh_housekeeping(sdata, ifmsh);
676
677         if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
678                 ieee80211_mesh_rootpath(sdata);
679 }
680
681 void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
682 {
683         struct ieee80211_sub_if_data *sdata;
684
685         rcu_read_lock();
686         list_for_each_entry_rcu(sdata, &local->interfaces, list)
687                 if (ieee80211_vif_is_mesh(&sdata->vif))
688                         ieee80211_queue_work(&local->hw, &sdata->u.mesh.work);
689         rcu_read_unlock();
690 }
691
692 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
693 {
694         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
695
696         INIT_WORK(&ifmsh->work, ieee80211_mesh_work);
697         setup_timer(&ifmsh->housekeeping_timer,
698                     ieee80211_mesh_housekeeping_timer,
699                     (unsigned long) sdata);
700         skb_queue_head_init(&sdata->u.mesh.skb_queue);
701
702         ifmsh->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
703         ifmsh->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
704         ifmsh->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
705         ifmsh->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
706         ifmsh->mshcfg.dot11MeshTTL = MESH_TTL;
707         ifmsh->mshcfg.auto_open_plinks = true;
708         ifmsh->mshcfg.dot11MeshMaxPeerLinks =
709                 MESH_MAX_ESTAB_PLINKS;
710         ifmsh->mshcfg.dot11MeshHWMPactivePathTimeout =
711                 MESH_PATH_TIMEOUT;
712         ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval =
713                 MESH_PREQ_MIN_INT;
714         ifmsh->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
715                 MESH_DIAM_TRAVERSAL_TIME;
716         ifmsh->mshcfg.dot11MeshHWMPmaxPREQretries =
717                 MESH_MAX_PREQ_RETRIES;
718         ifmsh->mshcfg.path_refresh_time =
719                 MESH_PATH_REFRESH_TIME;
720         ifmsh->mshcfg.min_discovery_timeout =
721                 MESH_MIN_DISCOVERY_TIMEOUT;
722         ifmsh->accepting_plinks = true;
723         ifmsh->preq_id = 0;
724         ifmsh->sn = 0;
725         atomic_set(&ifmsh->mpaths, 0);
726         mesh_rmc_init(sdata);
727         ifmsh->last_preq = jiffies;
728         /* Allocate all mesh structures when creating the first mesh interface. */
729         if (!mesh_allocated)
730                 ieee80211s_init();
731         mesh_ids_set_default(ifmsh);
732         setup_timer(&ifmsh->mesh_path_timer,
733                     ieee80211_mesh_path_timer,
734                     (unsigned long) sdata);
735         setup_timer(&ifmsh->mesh_path_root_timer,
736                     ieee80211_mesh_path_root_timer,
737                     (unsigned long) sdata);
738         INIT_LIST_HEAD(&ifmsh->preq_queue.list);
739         spin_lock_init(&ifmsh->mesh_preq_queue_lock);
740 }
741
742 ieee80211_rx_result
743 ieee80211_mesh_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
744 {
745         struct ieee80211_local *local = sdata->local;
746         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
747         struct ieee80211_mgmt *mgmt;
748         u16 fc;
749
750         if (skb->len < 24)
751                 return RX_DROP_MONITOR;
752
753         mgmt = (struct ieee80211_mgmt *) skb->data;
754         fc = le16_to_cpu(mgmt->frame_control);
755
756         switch (fc & IEEE80211_FCTL_STYPE) {
757         case IEEE80211_STYPE_ACTION:
758                 if (skb->len < IEEE80211_MIN_ACTION_SIZE)
759                         return RX_DROP_MONITOR;
760                 /* fall through */
761         case IEEE80211_STYPE_PROBE_RESP:
762         case IEEE80211_STYPE_BEACON:
763                 skb_queue_tail(&ifmsh->skb_queue, skb);
764                 ieee80211_queue_work(&local->hw, &ifmsh->work);
765                 return RX_QUEUED;
766         }
767
768         return RX_CONTINUE;
769 }