blob: 29ce2aa87e7b60fdd344f7fd8acb1148cbc477fd [file] [log] [blame]
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01003 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -070012#include <asm/unaligned.h>
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010013#include "ieee80211_i.h"
14#include "mesh.h"
15
Johannes Berg5bb644a2009-05-17 11:40:42 +020016#define TMR_RUNNING_HK 0
17#define TMR_RUNNING_MP 1
Rui Pauloe304bfd2009-11-09 23:46:56 +000018#define TMR_RUNNING_MPR 2
Johannes Berg5bb644a2009-05-17 11:40:42 +020019
Johannes Bergbf7cd942013-02-15 14:40:31 +010020static int mesh_allocated;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010021static struct kmem_cache *rm_cache;
22
Thomas Pedersen25d49e42011-08-11 19:35:15 -070023bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
24{
25 return (mgmt->u.action.u.mesh_action.action_code ==
26 WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
27}
Thomas Pedersen25d49e42011-08-11 19:35:15 -070028
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010029void ieee80211s_init(void)
30{
31 mesh_pathtbl_init();
32 mesh_allocated = 1;
33 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
34 0, 0, NULL);
35}
36
37void ieee80211s_stop(void)
38{
Johannes Bergbf7cd942013-02-15 14:40:31 +010039 if (!mesh_allocated)
40 return;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010041 mesh_pathtbl_unregister();
42 kmem_cache_destroy(rm_cache);
43}
44
Johannes Berg472dbc42008-09-11 00:01:49 +020045static void ieee80211_mesh_housekeeping_timer(unsigned long data)
46{
47 struct ieee80211_sub_if_data *sdata = (void *) data;
48 struct ieee80211_local *local = sdata->local;
49 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
50
Rui Paulo6b9ac442009-10-20 21:21:48 +010051 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
Johannes Berg5bb644a2009-05-17 11:40:42 +020052
53 if (local->quiescing) {
54 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
55 return;
56 }
57
Johannes Berg64592c82010-06-10 10:21:31 +020058 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg472dbc42008-09-11 00:01:49 +020059}
60
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010061/**
62 * mesh_matches_local - check if the config of a mesh point matches ours
63 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120064 * @sdata: local mesh subif
Thomas Pedersenf743ff42012-04-18 19:23:43 -070065 * @ie: information elements of a management frame from the mesh peer
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010066 *
67 * This function checks if the mesh configuration of a mesh point matches the
68 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
69 */
Thomas Pedersenf743ff42012-04-18 19:23:43 -070070bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
71 struct ieee802_11_elems *ie)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010072{
Johannes Berg472dbc42008-09-11 00:01:49 +020073 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen739522b2011-10-26 14:47:28 -070074 struct ieee80211_local *local = sdata->local;
Thomas Pedersenf743ff42012-04-18 19:23:43 -070075 u32 basic_rates = 0;
Johannes Berg4bf88532012-11-09 11:39:59 +010076 struct cfg80211_chan_def sta_chan_def;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010077
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010078 /*
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 */
Thomas Pedersen739522b2011-10-26 14:47:28 -070088 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_psel) &&
91 (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
92 (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
93 (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
94 (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
Johannes Bergbf7cd942013-02-15 14:40:31 +010095 return false;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010096
Johannes Berg55de9082012-07-26 17:24:39 +020097 ieee80211_sta_get_rates(local, ie, ieee80211_get_sdata_band(sdata),
Thomas Pedersenf743ff42012-04-18 19:23:43 -070098 &basic_rates);
99
Ashok Nagarajanfe40cb62012-04-02 21:21:22 -0700100 if (sdata->vif.bss_conf.basic_rates != basic_rates)
Johannes Bergbf7cd942013-02-15 14:40:31 +0100101 return false;
Ashok Nagarajanfe40cb62012-04-02 21:21:22 -0700102
Johannes Berg4bf88532012-11-09 11:39:59 +0100103 ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
104 ie->ht_operation, &sta_chan_def);
Ashok Nagarajanb91e64a2012-04-30 14:20:31 -0700105
Johannes Berg4bf88532012-11-09 11:39:59 +0100106 if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
107 &sta_chan_def))
Johannes Bergbf7cd942013-02-15 14:40:31 +0100108 return false;
Thomas Pedersen739522b2011-10-26 14:47:28 -0700109
110 return true;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100111}
112
113/**
114 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
115 *
116 * @ie: information elements of a management frame from the mesh peer
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100117 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200118bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100119{
Rui Paulo136cfa22009-11-18 18:40:00 +0000120 return (ie->mesh_config->meshconf_cap &
Johannes Bergbf7cd942013-02-15 14:40:31 +0100121 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100122}
123
124/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000125 * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100126 *
Johannes Bergd0709a62008-02-25 16:27:46 +0100127 * @sdata: mesh interface in which mesh beacons are going to be updated
Marco Porschdf323812012-08-08 07:58:43 +0200128 *
129 * Returns: beacon changed flag if the beacon content changed.
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100130 */
Marco Porschdf323812012-08-08 07:58:43 +0200131u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100132{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100133 bool free_plinks;
Marco Porschdf323812012-08-08 07:58:43 +0200134 u32 changed = 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100135
136 /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
137 * the mesh interface might be able to establish plinks with peers that
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800138 * are already on the table but are not on PLINK_ESTAB state. However,
139 * in general the mesh interface is not accepting peer link requests
140 * from new peers, and that must be reflected in the beacon
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100141 */
142 free_plinks = mesh_plink_availables(sdata);
143
Marco Porschdf323812012-08-08 07:58:43 +0200144 if (free_plinks != sdata->u.mesh.accepting_plinks) {
145 sdata->u.mesh.accepting_plinks = free_plinks;
146 changed = BSS_CHANGED_BEACON;
147 }
148
149 return changed;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100150}
151
Thomas Pedersen45b50282013-02-06 10:17:21 -0800152/*
153 * mesh_sta_cleanup - clean up any mesh sta state
154 *
155 * @sta: mesh sta to clean up.
156 */
157void mesh_sta_cleanup(struct sta_info *sta)
158{
159 struct ieee80211_sub_if_data *sdata = sta->sdata;
160 u32 changed;
161
162 /*
163 * maybe userspace handles peer allocation and peering, but in either
164 * case the beacon is still generated by the kernel and we might need
165 * an update.
166 */
167 changed = mesh_accept_plinks_update(sdata);
168 if (sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
169 changed |= mesh_plink_deactivate(sta);
170 del_timer_sync(&sta->plink_timer);
171 }
172
173 if (changed)
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800174 ieee80211_mbss_info_change_notify(sdata, changed);
Thomas Pedersen45b50282013-02-06 10:17:21 -0800175}
176
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200177int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100178{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100179 int i;
180
Johannes Berg472dbc42008-09-11 00:01:49 +0200181 sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
182 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100183 return -ENOMEM;
Johannes Berg472dbc42008-09-11 00:01:49 +0200184 sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100185 for (i = 0; i < RMC_BUCKETS; i++)
Thomas Pedersenb7cfcd12012-12-17 18:41:57 -0800186 INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100187 return 0;
188}
189
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200190void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100191{
Johannes Berg472dbc42008-09-11 00:01:49 +0200192 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100193 struct rmc_entry *p, *n;
194 int i;
195
Johannes Berg472dbc42008-09-11 00:01:49 +0200196 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100197 return;
198
Johannes Bergbf7cd942013-02-15 14:40:31 +0100199 for (i = 0; i < RMC_BUCKETS; i++) {
Thomas Pedersenb7cfcd12012-12-17 18:41:57 -0800200 list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100201 list_del(&p->list);
202 kmem_cache_free(rm_cache, p);
203 }
Johannes Bergbf7cd942013-02-15 14:40:31 +0100204 }
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100205
206 kfree(rmc);
Johannes Berg472dbc42008-09-11 00:01:49 +0200207 sdata->u.mesh.rmc = NULL;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100208}
209
210/**
211 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
212 *
Johannes Bergbf7cd942013-02-15 14:40:31 +0100213 * @sdata: interface
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100214 * @sa: source address
215 * @mesh_hdr: mesh_header
216 *
217 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
218 *
219 * Checks using the source address and the mesh sequence number if we have
220 * received this frame lately. If the frame is not in the cache, it is added to
221 * it.
222 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100223int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
224 const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100225{
Johannes Berg472dbc42008-09-11 00:01:49 +0200226 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100227 u32 seqnum = 0;
228 int entries = 0;
229 u8 idx;
230 struct rmc_entry *p, *n;
231
232 /* Don't care about endianness since only match matters */
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -0700233 memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
234 idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
Thomas Pedersenb7cfcd12012-12-17 18:41:57 -0800235 list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100236 ++entries;
237 if (time_after(jiffies, p->exp_time) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100238 entries == RMC_QUEUE_MAX_LEN) {
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100239 list_del(&p->list);
240 kmem_cache_free(rm_cache, p);
241 --entries;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100242 } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100243 return -1;
244 }
245
246 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
Joe Perchesd15b8452011-08-29 14:17:31 -0700247 if (!p)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100248 return 0;
Joe Perchesd15b8452011-08-29 14:17:31 -0700249
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100250 p->seqnum = seqnum;
251 p->exp_time = jiffies + RMC_TIMEOUT;
252 memcpy(p->sa, sa, ETH_ALEN);
Thomas Pedersenb7cfcd12012-12-17 18:41:57 -0800253 list_add(&p->list, &rmc->bucket[idx]);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100254 return 0;
255}
256
Johannes Bergbf7cd942013-02-15 14:40:31 +0100257int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
258 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700259{
260 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
261 u8 *pos, neighbors;
262 u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
263
264 if (skb_tailroom(skb) < 2 + meshconf_len)
265 return -ENOMEM;
266
267 pos = skb_put(skb, 2 + meshconf_len);
268 *pos++ = WLAN_EID_MESH_CONFIG;
269 *pos++ = meshconf_len;
270
271 /* Active path selection protocol ID */
272 *pos++ = ifmsh->mesh_pp_id;
273 /* Active path selection metric ID */
274 *pos++ = ifmsh->mesh_pm_id;
275 /* Congestion control mode identifier */
276 *pos++ = ifmsh->mesh_cc_id;
277 /* Synchronization protocol identifier */
278 *pos++ = ifmsh->mesh_sp_id;
279 /* Authentication Protocol identifier */
280 *pos++ = ifmsh->mesh_auth_id;
281 /* Mesh Formation Info - number of neighbors */
Ashok Nagarajan1258d972012-10-09 13:27:47 -0700282 neighbors = atomic_read(&ifmsh->estab_plinks);
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700283 /* Number of neighbor mesh STAs or 15 whichever is smaller */
284 neighbors = (neighbors > 15) ? 15 : neighbors;
285 *pos++ = neighbors << 1;
286 /* Mesh capability */
Marco Porsch65821632012-11-21 18:40:30 -0800287 *pos = IEEE80211_MESHCONF_CAPAB_FORWARDING;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700288 *pos |= ifmsh->accepting_plinks ?
Johannes Bergbf7cd942013-02-15 14:40:31 +0100289 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100290 /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
291 *pos |= ifmsh->ps_peers_deep_sleep ?
Johannes Bergbf7cd942013-02-15 14:40:31 +0100292 IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700293 *pos++ |= ifmsh->adjusting_tbtt ?
Johannes Bergbf7cd942013-02-15 14:40:31 +0100294 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700295 *pos++ = 0x00;
296
297 return 0;
298}
299
Johannes Bergbf7cd942013-02-15 14:40:31 +0100300int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700301{
302 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
303 u8 *pos;
304
305 if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
306 return -ENOMEM;
307
308 pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
309 *pos++ = WLAN_EID_MESH_ID;
310 *pos++ = ifmsh->mesh_id_len;
311 if (ifmsh->mesh_id_len)
312 memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
313
314 return 0;
315}
316
Johannes Bergbf7cd942013-02-15 14:40:31 +0100317static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
318 struct sk_buff *skb)
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100319{
320 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
321 u8 *pos;
322
323 /* see IEEE802.11-2012 13.14.6 */
324 if (ifmsh->ps_peers_light_sleep == 0 &&
325 ifmsh->ps_peers_deep_sleep == 0 &&
326 ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
327 return 0;
328
329 if (skb_tailroom(skb) < 4)
330 return -ENOMEM;
331
332 pos = skb_put(skb, 2 + 2);
333 *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
334 *pos++ = 2;
335 put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
336
337 return 0;
338}
339
Johannes Bergbf7cd942013-02-15 14:40:31 +0100340int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
341 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700342{
343 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
344 u8 offset, len;
345 const u8 *data;
346
347 if (!ifmsh->ie || !ifmsh->ie_len)
348 return 0;
349
350 /* fast-forward to vendor IEs */
351 offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
352
353 if (offset) {
354 len = ifmsh->ie_len - offset;
355 data = ifmsh->ie + offset;
356 if (skb_tailroom(skb) < len)
357 return -ENOMEM;
358 memcpy(skb_put(skb, len), data, len);
359 }
360
361 return 0;
362}
363
Johannes Bergbf7cd942013-02-15 14:40:31 +0100364int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700365{
366 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
367 u8 len = 0;
368 const u8 *data;
369
370 if (!ifmsh->ie || !ifmsh->ie_len)
371 return 0;
372
373 /* find RSN IE */
374 data = ifmsh->ie;
375 while (data < ifmsh->ie + ifmsh->ie_len) {
376 if (*data == WLAN_EID_RSN) {
377 len = data[1] + 2;
378 break;
379 }
380 data++;
381 }
382
383 if (len) {
384 if (skb_tailroom(skb) < len)
385 return -ENOMEM;
386 memcpy(skb_put(skb, len), data, len);
387 }
388
389 return 0;
390}
391
Johannes Bergbf7cd942013-02-15 14:40:31 +0100392static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
393 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700394{
Johannes Berg55de9082012-07-26 17:24:39 +0200395 struct ieee80211_chanctx_conf *chanctx_conf;
396 struct ieee80211_channel *chan;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700397 u8 *pos;
398
399 if (skb_tailroom(skb) < 3)
400 return -ENOMEM;
401
Johannes Berg55de9082012-07-26 17:24:39 +0200402 rcu_read_lock();
403 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
404 if (WARN_ON(!chanctx_conf)) {
405 rcu_read_unlock();
406 return -EINVAL;
407 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100408 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200409 rcu_read_unlock();
410
Emanuel Taube601513a2013-02-06 14:17:17 +0100411 pos = skb_put(skb, 2 + 1);
412 *pos++ = WLAN_EID_DS_PARAMS;
413 *pos++ = 1;
414 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Rui Paulobe125c62009-11-09 23:46:54 +0000415
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700416 return 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100417}
418
Johannes Bergbf7cd942013-02-15 14:40:31 +0100419int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
420 struct sk_buff *skb)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700421{
422 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +0200423 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Thomas Pedersen176f3602011-10-26 14:47:27 -0700424 struct ieee80211_supported_band *sband;
425 u8 *pos;
426
Johannes Berg55de9082012-07-26 17:24:39 +0200427 sband = local->hw.wiphy->bands[band];
Thomas Pedersen176f3602011-10-26 14:47:27 -0700428 if (!sband->ht_cap.ht_supported ||
Johannes Berg4bf88532012-11-09 11:39:59 +0100429 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700430 return 0;
431
432 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
433 return -ENOMEM;
434
435 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
Ben Greearef96a8422011-11-18 11:32:00 -0800436 ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
Thomas Pedersen176f3602011-10-26 14:47:27 -0700437
438 return 0;
439}
440
Johannes Bergbf7cd942013-02-15 14:40:31 +0100441int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
442 struct sk_buff *skb)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700443{
444 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +0200445 struct ieee80211_chanctx_conf *chanctx_conf;
446 struct ieee80211_channel *channel;
Johannes Berg466f3102012-07-25 13:51:49 +0200447 enum nl80211_channel_type channel_type =
Johannes Berg4bf88532012-11-09 11:39:59 +0100448 cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
Johannes Berg55de9082012-07-26 17:24:39 +0200449 struct ieee80211_supported_band *sband;
450 struct ieee80211_sta_ht_cap *ht_cap;
Thomas Pedersen176f3602011-10-26 14:47:27 -0700451 u8 *pos;
452
Johannes Berg55de9082012-07-26 17:24:39 +0200453 rcu_read_lock();
454 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
455 if (WARN_ON(!chanctx_conf)) {
456 rcu_read_unlock();
457 return -EINVAL;
458 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100459 channel = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200460 rcu_read_unlock();
461
462 sband = local->hw.wiphy->bands[channel->band];
463 ht_cap = &sband->ht_cap;
464
Thomas Pedersen176f3602011-10-26 14:47:27 -0700465 if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
466 return 0;
467
Johannes Berg074d46d2012-03-15 19:45:16 +0100468 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
Thomas Pedersen176f3602011-10-26 14:47:27 -0700469 return -ENOMEM;
470
Johannes Berg074d46d2012-03-15 19:45:16 +0100471 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
Johannes Berg4bf88532012-11-09 11:39:59 +0100472 ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
Ashok Nagarajan431e3152012-04-30 14:20:29 -0700473 sdata->vif.bss_conf.ht_operation_mode);
Thomas Pedersen176f3602011-10-26 14:47:27 -0700474
475 return 0;
476}
Johannes Bergbf7cd942013-02-15 14:40:31 +0100477
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100478static void ieee80211_mesh_path_timer(unsigned long data)
479{
480 struct ieee80211_sub_if_data *sdata =
481 (struct ieee80211_sub_if_data *) data;
Johannes Berg472dbc42008-09-11 00:01:49 +0200482 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg133b8222008-09-16 14:18:59 +0200483 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100484
Johannes Berg5bb644a2009-05-17 11:40:42 +0200485 if (local->quiescing) {
486 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
487 return;
488 }
489
Johannes Berg64592c82010-06-10 10:21:31 +0200490 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100491}
492
Rui Pauloe304bfd2009-11-09 23:46:56 +0000493static void ieee80211_mesh_path_root_timer(unsigned long data)
494{
495 struct ieee80211_sub_if_data *sdata =
496 (struct ieee80211_sub_if_data *) data;
497 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
498 struct ieee80211_local *local = sdata->local;
499
500 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
501
502 if (local->quiescing) {
503 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
504 return;
505 }
506
Johannes Berg64592c82010-06-10 10:21:31 +0200507 ieee80211_queue_work(&local->hw, &sdata->work);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000508}
509
Rui Paulo63c57232009-11-09 23:46:57 +0000510void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
511{
Chun-Yeow Yeohdbb912c2012-06-14 02:06:09 +0800512 if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
Rui Paulo63c57232009-11-09 23:46:57 +0000513 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
514 else {
515 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
516 /* stop running timer */
517 del_timer_sync(&ifmsh->mesh_path_root_timer);
518 }
519}
520
Johannes Berg902acc72008-02-23 15:17:19 +0100521/**
Javier Cardona3c5772a2009-08-10 12:15:48 -0700522 * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
Johannes Bergbf7cd942013-02-15 14:40:31 +0100523 * @hdr: 802.11 frame header
Javier Cardona3c5772a2009-08-10 12:15:48 -0700524 * @fc: frame control field
525 * @meshda: destination address in the mesh
526 * @meshsa: source address address in the mesh. Same as TA, as frame is
527 * locally originated.
528 *
529 * Return the length of the 802.11 (does not include a mesh control header)
530 */
Johannes Berg15ff6362009-11-17 13:34:04 +0100531int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
532 const u8 *meshda, const u8 *meshsa)
533{
Javier Cardona3c5772a2009-08-10 12:15:48 -0700534 if (is_multicast_ether_addr(meshda)) {
535 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
536 /* DA TA SA */
537 memcpy(hdr->addr1, meshda, ETH_ALEN);
538 memcpy(hdr->addr2, meshsa, ETH_ALEN);
539 memcpy(hdr->addr3, meshsa, ETH_ALEN);
540 return 24;
541 } else {
Javier Cardona2154c812011-09-07 17:49:53 -0700542 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700543 /* RA TA DA SA */
544 memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
545 memcpy(hdr->addr2, meshsa, ETH_ALEN);
546 memcpy(hdr->addr3, meshda, ETH_ALEN);
547 memcpy(hdr->addr4, meshsa, ETH_ALEN);
548 return 30;
549 }
550}
551
552/**
Johannes Berg902acc72008-02-23 15:17:19 +0100553 * ieee80211_new_mesh_header - create a new mesh header
Johannes Berg902acc72008-02-23 15:17:19 +0100554 * @sdata: mesh interface to be used
Johannes Bergbf7cd942013-02-15 14:40:31 +0100555 * @meshhdr: uninitialized mesh header
Javier Cardona61ad5392010-12-16 17:23:34 -0800556 * @addr4or5: 1st address in the ae header, which may correspond to address 4
557 * (if addr6 is NULL) or address 5 (if addr6 is present). It may
558 * be NULL.
559 * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
560 * mesh frame
Johannes Berg902acc72008-02-23 15:17:19 +0100561 *
562 * Return the header length.
563 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100564int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
565 struct ieee80211s_hdr *meshhdr,
566 const char *addr4or5, const char *addr6)
Johannes Berg902acc72008-02-23 15:17:19 +0100567{
Johannes Bergbf7cd942013-02-15 14:40:31 +0100568 if (WARN_ON(!addr4or5 && addr6))
569 return 0;
570
Julia Lawall0c3cee72009-12-09 20:25:59 +0100571 memset(meshhdr, 0, sizeof(*meshhdr));
Johannes Bergbf7cd942013-02-15 14:40:31 +0100572
Johannes Berg472dbc42008-09-11 00:01:49 +0200573 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100574
575 /* FIXME: racy -- TX on multiple queues can be concurrent */
Johannes Berg472dbc42008-09-11 00:01:49 +0200576 put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
577 sdata->u.mesh.mesh_seqnum++;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100578
Javier Cardona61ad5392010-12-16 17:23:34 -0800579 if (addr4or5 && !addr6) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700580 meshhdr->flags |= MESH_FLAGS_AE_A4;
Javier Cardona61ad5392010-12-16 17:23:34 -0800581 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100582 return 2 * ETH_ALEN;
Javier Cardona61ad5392010-12-16 17:23:34 -0800583 } else if (addr4or5 && addr6) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700584 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
Javier Cardona61ad5392010-12-16 17:23:34 -0800585 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
586 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100587 return 3 * ETH_ALEN;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700588 }
Johannes Bergbf7cd942013-02-15 14:40:31 +0100589
590 return ETH_ALEN;
Johannes Berg902acc72008-02-23 15:17:19 +0100591}
592
Johannes Bergbf7cd942013-02-15 14:40:31 +0100593static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +0200594{
Johannes Bergbf7cd942013-02-15 14:40:31 +0100595 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Marco Porschdf323812012-08-08 07:58:43 +0200596 u32 changed;
Johannes Berg472dbc42008-09-11 00:01:49 +0200597
Johannes Berg472dbc42008-09-11 00:01:49 +0200598 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
599 mesh_path_expire(sdata);
600
Marco Porschdf323812012-08-08 07:58:43 +0200601 changed = mesh_accept_plinks_update(sdata);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800602 ieee80211_mbss_info_change_notify(sdata, changed);
Johannes Berg472dbc42008-09-11 00:01:49 +0200603
Johannes Berg472dbc42008-09-11 00:01:49 +0200604 mod_timer(&ifmsh->housekeeping_timer,
Johannes Bergbf7cd942013-02-15 14:40:31 +0100605 round_jiffies(jiffies +
606 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
Johannes Berg472dbc42008-09-11 00:01:49 +0200607}
608
Rui Pauloe304bfd2009-11-09 23:46:56 +0000609static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
610{
611 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800612 u32 interval;
Rui Pauloe304bfd2009-11-09 23:46:56 +0000613
614 mesh_path_tx_root_frame(sdata);
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800615
616 if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
617 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
618 else
619 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
620
Rui Pauloe304bfd2009-11-09 23:46:56 +0000621 mod_timer(&ifmsh->mesh_path_root_timer,
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800622 round_jiffies(TU_TO_EXP_TIME(interval)));
Rui Pauloe304bfd2009-11-09 23:46:56 +0000623}
624
Johannes Berg5bb644a2009-05-17 11:40:42 +0200625#ifdef CONFIG_PM
626void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
627{
628 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
629
Javier Cardona5ee68e52011-08-09 16:45:08 -0700630 /* use atomic bitops in case all timers fire at the same time */
Johannes Berg5bb644a2009-05-17 11:40:42 +0200631
632 if (del_timer_sync(&ifmsh->housekeeping_timer))
633 set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
634 if (del_timer_sync(&ifmsh->mesh_path_timer))
635 set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000636 if (del_timer_sync(&ifmsh->mesh_path_root_timer))
637 set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
Johannes Berg5bb644a2009-05-17 11:40:42 +0200638}
639
640void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
641{
642 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
643
644 if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
645 add_timer(&ifmsh->housekeeping_timer);
646 if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
647 add_timer(&ifmsh->mesh_path_timer);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000648 if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
649 add_timer(&ifmsh->mesh_path_root_timer);
Rui Paulo63c57232009-11-09 23:46:57 +0000650 ieee80211_mesh_root_setup(ifmsh);
Johannes Berg5bb644a2009-05-17 11:40:42 +0200651}
652#endif
Johannes Berg472dbc42008-09-11 00:01:49 +0200653
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800654static int
655ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
656{
657 struct beacon_data *bcn;
658 int head_len, tail_len;
659 struct sk_buff *skb;
660 struct ieee80211_mgmt *mgmt;
661 struct ieee80211_chanctx_conf *chanctx_conf;
662 enum ieee80211_band band;
663 u8 *pos;
664 struct ieee80211_sub_if_data *sdata;
665 int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
666 sizeof(mgmt->u.beacon);
667
668 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
669 rcu_read_lock();
670 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
671 band = chanctx_conf->def.chan->band;
672 rcu_read_unlock();
673
674 head_len = hdr_len +
675 2 + /* NULL SSID */
676 2 + 8 + /* supported rates */
677 2 + 3; /* DS params */
678 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
679 2 + sizeof(struct ieee80211_ht_cap) +
680 2 + sizeof(struct ieee80211_ht_operation) +
681 2 + ifmsh->mesh_id_len +
682 2 + sizeof(struct ieee80211_meshconf_ie) +
683 2 + sizeof(__le16) + /* awake window */
684 ifmsh->ie_len;
685
686 bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
687 /* need an skb for IE builders to operate on */
688 skb = dev_alloc_skb(max(head_len, tail_len));
689
690 if (!bcn || !skb)
691 goto out_free;
692
693 /*
694 * pointers go into the block we allocated,
695 * memory is | beacon_data | head | tail |
696 */
697 bcn->head = ((u8 *) bcn) + sizeof(*bcn);
698
699 /* fill in the head */
700 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
701 memset(mgmt, 0, hdr_len);
702 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
703 IEEE80211_STYPE_BEACON);
704 eth_broadcast_addr(mgmt->da);
705 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
706 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
707 ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
708 mgmt->u.beacon.beacon_int =
709 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
710 mgmt->u.beacon.capab_info |= cpu_to_le16(
711 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
712
713 pos = skb_put(skb, 2);
714 *pos++ = WLAN_EID_SSID;
715 *pos++ = 0x0;
716
717 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100718 mesh_add_ds_params_ie(sdata, skb))
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800719 goto out_free;
720
721 bcn->head_len = skb->len;
722 memcpy(bcn->head, skb->data, bcn->head_len);
723
724 /* now the tail */
725 skb_trim(skb, 0);
726 bcn->tail = bcn->head + bcn->head_len;
727
728 if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100729 mesh_add_rsn_ie(sdata, skb) ||
730 mesh_add_ht_cap_ie(sdata, skb) ||
731 mesh_add_ht_oper_ie(sdata, skb) ||
732 mesh_add_meshid_ie(sdata, skb) ||
733 mesh_add_meshconf_ie(sdata, skb) ||
734 mesh_add_awake_window_ie(sdata, skb) ||
735 mesh_add_vendor_ies(sdata, skb))
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800736 goto out_free;
737
738 bcn->tail_len = skb->len;
739 memcpy(bcn->tail, skb->data, bcn->tail_len);
740
741 dev_kfree_skb(skb);
742 rcu_assign_pointer(ifmsh->beacon, bcn);
743 return 0;
744out_free:
745 kfree(bcn);
746 dev_kfree_skb(skb);
747 return -ENOMEM;
748}
749
750static int
751ieee80211_mesh_rebuild_beacon(struct ieee80211_if_mesh *ifmsh)
752{
753 struct ieee80211_sub_if_data *sdata;
754 struct beacon_data *old_bcn;
755 int ret;
756 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
757
758 mutex_lock(&ifmsh->mtx);
759
760 old_bcn = rcu_dereference_protected(ifmsh->beacon,
761 lockdep_is_held(&ifmsh->mtx));
762 ret = ieee80211_mesh_build_beacon(ifmsh);
763 if (ret)
764 /* just reuse old beacon */
765 goto out;
766
767 if (old_bcn)
768 kfree_rcu(old_bcn, rcu_head);
769out:
770 mutex_unlock(&ifmsh->mtx);
771 return ret;
772}
773
774void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
775 u32 changed)
776{
777 if (sdata->vif.bss_conf.enable_beacon &&
778 (changed & (BSS_CHANGED_BEACON |
779 BSS_CHANGED_HT |
780 BSS_CHANGED_BASIC_RATES |
781 BSS_CHANGED_BEACON_INT)))
782 if (ieee80211_mesh_rebuild_beacon(&sdata->u.mesh))
783 return;
784 ieee80211_bss_info_change_notify(sdata, changed);
785}
786
787int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +0200788{
789 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
790 struct ieee80211_local *local = sdata->local;
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +0800791 u32 changed = BSS_CHANGED_BEACON |
792 BSS_CHANGED_BEACON_ENABLED |
793 BSS_CHANGED_HT |
794 BSS_CHANGED_BASIC_RATES |
795 BSS_CHANGED_BEACON_INT;
796 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200797
Johannes Berg09b17472010-12-03 09:20:41 +0100798 local->fif_other_bss++;
799 /* mesh ifaces must set allmulti to forward mcast traffic */
800 atomic_inc(&local->iff_allmultis);
801 ieee80211_configure_filter(local);
802
Javier Cardonac7108a72010-12-16 17:37:50 -0800803 ifmsh->mesh_cc_id = 0; /* Disabled */
Javier Cardonac7108a72010-12-16 17:37:50 -0800804 ifmsh->mesh_auth_id = 0; /* Disabled */
Javier Cardonadbf498f2012-03-31 11:31:32 -0700805 /* register sync ops from extensible synchronization framework */
806 ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
807 ifmsh->adjusting_tbtt = false;
808 ifmsh->sync_offset_clockdrift_max = 0;
Rui Paulo6b9ac442009-10-20 21:21:48 +0100809 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
Rui Paulo63c57232009-11-09 23:46:57 +0000810 ieee80211_mesh_root_setup(ifmsh);
Johannes Berg64592c82010-06-10 10:21:31 +0200811 ieee80211_queue_work(&local->hw, &sdata->work);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -0700812 sdata->vif.bss_conf.ht_operation_mode =
813 ifmsh->mshcfg.ht_opmode;
Johannes Bergd6a83222012-12-14 14:06:28 +0100814 sdata->vif.bss_conf.enable_beacon = true;
Ashok Nagarajand934f7d2012-04-02 21:21:19 -0700815 sdata->vif.bss_conf.basic_rates =
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +0800816 ieee80211_mandatory_rates(local, band);
817
Thomas Pedersen39886b62013-02-13 12:14:19 -0800818 changed |= ieee80211_mps_local_status_update(sdata);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100819
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800820 if (ieee80211_mesh_build_beacon(ifmsh)) {
821 ieee80211_stop_mesh(sdata);
822 return -ENOMEM;
823 }
824
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +0800825 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergc405c622012-07-30 19:44:12 +0200826
827 netif_carrier_on(sdata->dev);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800828 return 0;
Johannes Berg472dbc42008-09-11 00:01:49 +0200829}
830
831void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
832{
Johannes Berg09b17472010-12-03 09:20:41 +0100833 struct ieee80211_local *local = sdata->local;
Johannes Berg29cbe682010-12-03 09:20:44 +0100834 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800835 struct beacon_data *bcn;
Johannes Berg29cbe682010-12-03 09:20:44 +0100836
Johannes Bergc405c622012-07-30 19:44:12 +0200837 netif_carrier_off(sdata->dev);
838
Thomas Pedersen0d466b9c2012-08-03 12:21:32 -0700839 /* stop the beacon */
Johannes Berg29cbe682010-12-03 09:20:44 +0100840 ifmsh->mesh_id_len = 0;
Johannes Bergd6a83222012-12-14 14:06:28 +0100841 sdata->vif.bss_conf.enable_beacon = false;
842 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
Johannes Berg29cbe682010-12-03 09:20:44 +0100843 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800844 mutex_lock(&ifmsh->mtx);
845 bcn = rcu_dereference_protected(ifmsh->beacon,
846 lockdep_is_held(&ifmsh->mtx));
847 rcu_assign_pointer(ifmsh->beacon, NULL);
848 kfree_rcu(bcn, rcu_head);
849 mutex_unlock(&ifmsh->mtx);
Thomas Pedersen0d466b9c2012-08-03 12:21:32 -0700850
851 /* flush STAs and mpaths on this iface */
Johannes Bergb998e8b2012-12-13 23:07:46 +0100852 sta_info_flush(sdata);
Thomas Pedersen0d466b9c2012-08-03 12:21:32 -0700853 mesh_path_flush_by_iface(sdata);
Johannes Berg09b17472010-12-03 09:20:41 +0100854
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100855 /* free all potentially still buffered group-addressed frames */
856 local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
857 skb_queue_purge(&ifmsh->ps.bc_buf);
858
Johannes Berg472dbc42008-09-11 00:01:49 +0200859 del_timer_sync(&sdata->u.mesh.housekeeping_timer);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000860 del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
Johannes Bergdd4c9262012-08-01 21:03:21 +0200861 del_timer_sync(&sdata->u.mesh.mesh_path_timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200862 /*
Johannes Bergb7413432008-09-11 00:01:50 +0200863 * If the timer fired while we waited for it, it will have
864 * requeued the work. Now the work will be running again
865 * but will not rearm the timer again because it checks
866 * whether the interface is running, which, at this point,
867 * it no longer is.
868 */
Johannes Berg64592c82010-06-10 10:21:31 +0200869 cancel_work_sync(&sdata->work);
Johannes Berg09b17472010-12-03 09:20:41 +0100870
871 local->fif_other_bss--;
872 atomic_dec(&local->iff_allmultis);
873 ieee80211_configure_filter(local);
Johannes Berg2d9957c2012-08-01 20:54:52 +0200874
875 sdata->u.mesh.timers_running = 0;
Johannes Berg472dbc42008-09-11 00:01:49 +0200876}
877
Thomas Pedersen9fb04b52013-02-14 11:20:14 -0800878static void
879ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
880 struct ieee80211_mgmt *mgmt, size_t len)
881{
882 struct ieee80211_local *local = sdata->local;
883 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
884 struct sk_buff *presp;
885 struct beacon_data *bcn;
886 struct ieee80211_mgmt *hdr;
887 struct ieee802_11_elems elems;
888 size_t baselen;
889 u8 *pos, *end;
890
891 end = ((u8 *) mgmt) + len;
892 pos = mgmt->u.probe_req.variable;
893 baselen = (u8 *) pos - (u8 *) mgmt;
894 if (baselen > len)
895 return;
896
897 ieee802_11_parse_elems(pos, len - baselen, &elems);
898
899 /* 802.11-2012 10.1.4.3.2 */
900 if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
901 !is_broadcast_ether_addr(mgmt->da)) ||
902 elems.ssid_len != 0)
903 return;
904
905 if (elems.mesh_id_len != 0 &&
906 (elems.mesh_id_len != ifmsh->mesh_id_len ||
907 memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
908 return;
909
910 rcu_read_lock();
911 bcn = rcu_dereference(ifmsh->beacon);
912
913 if (!bcn)
914 goto out;
915
916 presp = dev_alloc_skb(local->tx_headroom +
917 bcn->head_len + bcn->tail_len);
918 if (!presp)
919 goto out;
920
921 skb_reserve(presp, local->tx_headroom);
922 memcpy(skb_put(presp, bcn->head_len), bcn->head, bcn->head_len);
923 memcpy(skb_put(presp, bcn->tail_len), bcn->tail, bcn->tail_len);
924 hdr = (struct ieee80211_mgmt *) presp->data;
925 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
926 IEEE80211_STYPE_PROBE_RESP);
927 memcpy(hdr->da, mgmt->sa, ETH_ALEN);
Thomas Pedersen9fb04b52013-02-14 11:20:14 -0800928 IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
929 ieee80211_tx_skb(sdata, presp);
930out:
931 rcu_read_unlock();
932}
933
Johannes Berg472dbc42008-09-11 00:01:49 +0200934static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
935 u16 stype,
936 struct ieee80211_mgmt *mgmt,
937 size_t len,
938 struct ieee80211_rx_status *rx_status)
939{
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200940 struct ieee80211_local *local = sdata->local;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700941 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +0200942 struct ieee802_11_elems elems;
943 struct ieee80211_channel *channel;
Johannes Berg472dbc42008-09-11 00:01:49 +0200944 size_t baselen;
945 int freq;
946 enum ieee80211_band band = rx_status->band;
947
948 /* ignore ProbeResp to foreign address */
949 if (stype == IEEE80211_STYPE_PROBE_RESP &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000950 !ether_addr_equal(mgmt->da, sdata->vif.addr))
Johannes Berg472dbc42008-09-11 00:01:49 +0200951 return;
952
953 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
954 if (baselen > len)
955 return;
956
957 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
958 &elems);
959
Thomas Pedersen9a90bc82012-10-20 19:03:10 -0700960 /* ignore non-mesh or secure / unsecure mismatch */
961 if ((!elems.mesh_id || !elems.mesh_config) ||
962 (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
963 (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
Javier Cardona5cff5e02011-04-07 15:08:29 -0700964 return;
965
Johannes Berg472dbc42008-09-11 00:01:49 +0200966 if (elems.ds_params && elems.ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900967 freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
Johannes Berg472dbc42008-09-11 00:01:49 +0200968 else
969 freq = rx_status->freq;
970
971 channel = ieee80211_get_channel(local->hw.wiphy, freq);
972
973 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
974 return;
975
Thomas Pedersen9a90bc82012-10-20 19:03:10 -0700976 if (mesh_matches_local(sdata, &elems))
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700977 mesh_neighbour_update(sdata, mgmt->sa, &elems);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700978
979 if (ifmsh->sync_ops)
980 ifmsh->sync_ops->rx_bcn_presp(sdata,
981 stype, mgmt, &elems, rx_status);
Johannes Berg472dbc42008-09-11 00:01:49 +0200982}
983
984static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
985 struct ieee80211_mgmt *mgmt,
986 size_t len,
987 struct ieee80211_rx_status *rx_status)
988{
989 switch (mgmt->u.action.category) {
Thomas Pedersen8db09852011-08-12 20:01:00 -0700990 case WLAN_CATEGORY_SELF_PROTECTED:
991 switch (mgmt->u.action.u.self_prot.action_code) {
992 case WLAN_SP_MESH_PEERING_OPEN:
993 case WLAN_SP_MESH_PEERING_CLOSE:
994 case WLAN_SP_MESH_PEERING_CONFIRM:
995 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
996 break;
997 }
Johannes Berg472dbc42008-09-11 00:01:49 +0200998 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700999 case WLAN_CATEGORY_MESH_ACTION:
1000 if (mesh_action_is_path_sel(mgmt))
1001 mesh_rx_path_sel_frame(sdata, mgmt, len);
Johannes Berg472dbc42008-09-11 00:01:49 +02001002 break;
1003 }
1004}
1005
Johannes Berg1fa57d02010-06-10 10:21:32 +02001006void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1007 struct sk_buff *skb)
Johannes Berg472dbc42008-09-11 00:01:49 +02001008{
1009 struct ieee80211_rx_status *rx_status;
Johannes Berg472dbc42008-09-11 00:01:49 +02001010 struct ieee80211_mgmt *mgmt;
1011 u16 stype;
1012
Johannes Bergf1d58c22009-06-17 13:13:00 +02001013 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001014 mgmt = (struct ieee80211_mgmt *) skb->data;
1015 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1016
1017 switch (stype) {
1018 case IEEE80211_STYPE_PROBE_RESP:
1019 case IEEE80211_STYPE_BEACON:
1020 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1021 rx_status);
1022 break;
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001023 case IEEE80211_STYPE_PROBE_REQ:
1024 ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1025 break;
Johannes Berg472dbc42008-09-11 00:01:49 +02001026 case IEEE80211_STYPE_ACTION:
1027 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1028 break;
1029 }
Johannes Berg472dbc42008-09-11 00:01:49 +02001030}
1031
Johannes Berg1fa57d02010-06-10 10:21:32 +02001032void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +02001033{
Johannes Berg472dbc42008-09-11 00:01:49 +02001034 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02001035
1036 if (ifmsh->preq_queue_len &&
1037 time_after(jiffies,
1038 ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1039 mesh_path_start_discovery(sdata);
1040
Javier Cardona18889232009-08-10 12:15:52 -07001041 if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
1042 mesh_mpath_table_grow();
1043
Nick Ledovskikhdcac9082011-01-11 14:35:12 +00001044 if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
Javier Cardona18889232009-08-10 12:15:52 -07001045 mesh_mpp_table_grow();
1046
1047 if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
Johannes Bergbf7cd942013-02-15 14:40:31 +01001048 ieee80211_mesh_housekeeping(sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001049
1050 if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1051 ieee80211_mesh_rootpath(sdata);
Javier Cardonadbf498f2012-03-31 11:31:32 -07001052
1053 if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1054 mesh_sync_adjust_tbtt(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001055}
1056
1057void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
1058{
1059 struct ieee80211_sub_if_data *sdata;
1060
1061 rcu_read_lock();
1062 list_for_each_entry_rcu(sdata, &local->interfaces, list)
1063 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Berg64592c82010-06-10 10:21:31 +02001064 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg472dbc42008-09-11 00:01:49 +02001065 rcu_read_unlock();
1066}
1067
Johannes Berg902acc72008-02-23 15:17:19 +01001068void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1069{
Johannes Berg472dbc42008-09-11 00:01:49 +02001070 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Bergad2d2232012-12-14 14:34:25 +01001071 static u8 zero_addr[ETH_ALEN] = {};
Johannes Berg902acc72008-02-23 15:17:19 +01001072
Johannes Berg472dbc42008-09-11 00:01:49 +02001073 setup_timer(&ifmsh->housekeeping_timer,
1074 ieee80211_mesh_housekeeping_timer,
1075 (unsigned long) sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001076
Johannes Berg472dbc42008-09-11 00:01:49 +02001077 ifmsh->accepting_plinks = true;
1078 ifmsh->preq_id = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001079 ifmsh->sn = 0;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001080 ifmsh->num_gates = 0;
Johannes Berg472dbc42008-09-11 00:01:49 +02001081 atomic_set(&ifmsh->mpaths, 0);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001082 mesh_rmc_init(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001083 ifmsh->last_preq = jiffies;
Thomas Pedersendca7e942011-11-24 17:15:24 -08001084 ifmsh->next_perr = jiffies;
Johannes Berg902acc72008-02-23 15:17:19 +01001085 /* Allocate all mesh structures when creating the first mesh interface. */
1086 if (!mesh_allocated)
1087 ieee80211s_init();
Johannes Berg472dbc42008-09-11 00:01:49 +02001088 setup_timer(&ifmsh->mesh_path_timer,
Johannes Berg902acc72008-02-23 15:17:19 +01001089 ieee80211_mesh_path_timer,
1090 (unsigned long) sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001091 setup_timer(&ifmsh->mesh_path_root_timer,
1092 ieee80211_mesh_path_root_timer,
1093 (unsigned long) sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001094 INIT_LIST_HEAD(&ifmsh->preq_queue.list);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001095 skb_queue_head_init(&ifmsh->ps.bc_buf);
Johannes Berg472dbc42008-09-11 00:01:49 +02001096 spin_lock_init(&ifmsh->mesh_preq_queue_lock);
Javier Cardonadbf498f2012-03-31 11:31:32 -07001097 spin_lock_init(&ifmsh->sync_offset_lock);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001098 RCU_INIT_POINTER(ifmsh->beacon, NULL);
1099 mutex_init(&ifmsh->mtx);
Johannes Bergad2d2232012-12-14 14:34:25 +01001100
1101 sdata->vif.bss_conf.bssid = zero_addr;
Johannes Berg472dbc42008-09-11 00:01:49 +02001102}