blob: f053213e5adbf6d22b21102c12e311a800627164 [file] [log] [blame]
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01003 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010011#include <linux/etherdevice.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010012#include <asm/unaligned.h>
Javier Cardona2cca3972011-09-06 12:10:43 -070013#include "wme.h"
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010014#include "mesh.h"
15
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010016#define TEST_FRAME_LEN 8192
17#define MAX_METRIC 0xffffffff
18#define ARITH_SHIFT 8
19
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010020#define MAX_PREQ_QUEUE_LEN 64
21
22/* Destination only */
23#define MP_F_DO 0x1
24/* Reply and forward */
25#define MP_F_RF 0x2
Rui Paulod611f062009-11-09 23:46:50 +000026/* Unknown Sequence Number */
27#define MP_F_USN 0x01
28/* Reason code Present */
29#define MP_F_RCODE 0x02
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010030
Rui Paulo90a5e162009-11-11 00:01:31 +000031static void mesh_queue_preq(struct mesh_path *, u8);
32
Johannes Berg4a3cb702013-02-12 16:43:19 +010033static inline u32 u32_field_get(const u8 *preq_elem, int offset, bool ae)
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080034{
35 if (ae)
36 offset += 6;
Harvey Harrisonae7245c2008-05-01 22:19:33 -070037 return get_unaligned_le32(preq_elem + offset);
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080038}
39
Bob Copelandaee64992014-04-15 10:43:06 -040040static inline u16 u16_field_get(const u8 *preq_elem, int offset, bool ae)
Rui Paulod611f062009-11-09 23:46:50 +000041{
42 if (ae)
43 offset += 6;
44 return get_unaligned_le16(preq_elem + offset);
45}
46
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010047/* HWMP IE processing macros */
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080048#define AE_F (1<<6)
49#define AE_F_SET(x) (*x & AE_F)
50#define PREQ_IE_FLAGS(x) (*(x))
51#define PREQ_IE_HOPCOUNT(x) (*(x + 1))
52#define PREQ_IE_TTL(x) (*(x + 2))
53#define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
54#define PREQ_IE_ORIG_ADDR(x) (x + 7)
Phil Carmody497888c2011-07-14 15:07:13 +030055#define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
56#define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
57#define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000058#define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
59#define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
Phil Carmody497888c2011-07-14 15:07:13 +030060#define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010061
62
Luis Carlos Coboa00de5d2008-02-29 17:07:54 -080063#define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
64#define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
65#define PREP_IE_TTL(x) PREQ_IE_TTL(x)
Thomas Pedersen25d49e42011-08-11 19:35:15 -070066#define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
67#define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
Phil Carmody497888c2011-07-14 15:07:13 +030068#define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
69#define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
Thomas Pedersen25d49e42011-08-11 19:35:15 -070070#define PREP_IE_TARGET_ADDR(x) (x + 3)
71#define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010072
Rui Paulod611f062009-11-09 23:46:50 +000073#define PERR_IE_TTL(x) (*(x))
Rui Paulod19b3bf2009-11-09 23:46:55 +000074#define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
75#define PERR_IE_TARGET_ADDR(x) (x + 3)
Phil Carmody497888c2011-07-14 15:07:13 +030076#define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
77#define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010078
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010079#define MSEC_TO_TU(x) (x*1000/1024)
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +080080#define SN_GT(x, y) ((s32)(y - x) < 0)
81#define SN_LT(x, y) ((s32)(x - y) < 0)
Jesse Jonesd8f03002015-06-12 15:38:07 -070082#define MAX_SANE_SN_DELTA 32
83
84static inline u32 SN_DELTA(u32 x, u32 y)
85{
86 return x >= y ? x - y : y - x;
87}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010088
89#define net_traversal_jiffies(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020090 msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010091#define default_lifetime(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020092 MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010093#define min_preq_int_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020094 (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
95#define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +010096#define disc_timeout_jiff(s) \
Johannes Berg472dbc42008-09-11 00:01:49 +020097 msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +080098#define root_path_confirmation_jiffies(s) \
99 msecs_to_jiffies(sdata->u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100100
101enum mpath_frame_type {
102 MPATH_PREQ = 0,
103 MPATH_PREP,
Rui Paulo90a5e162009-11-11 00:01:31 +0000104 MPATH_PERR,
105 MPATH_RANN
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100106};
107
Johannes Berg15ff6362009-11-17 13:34:04 +0100108static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
109
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100110static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800111 const u8 *orig_addr, u32 orig_sn,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100112 u8 target_flags, const u8 *target,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800113 u32 target_sn, const u8 *da,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100114 u8 hop_count, u8 ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800115 u32 lifetime, u32 metric, u32 preq_id,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100116 struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100117{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200118 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700119 struct sk_buff *skb;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100120 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700121 u8 *pos, ie_len;
122 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
123 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100124
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800125 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700126 hdr_len +
127 2 + 37); /* max HWMP IE */
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100128 if (!skb)
129 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800130 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700131 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
132 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700133 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
134 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100135
136 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100137 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000138 /* BSSID == SA */
Johannes Berg47846c92009-11-25 17:46:19 +0100139 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700140 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
141 mgmt->u.action.u.mesh_action.action_code =
142 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100143
144 switch (action) {
145 case MPATH_PREQ:
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200146 mhwmp_dbg(sdata, "sending PREQ to %pM\n", target);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100147 ie_len = 37;
148 pos = skb_put(skb, 2 + ie_len);
149 *pos++ = WLAN_EID_PREQ;
150 break;
151 case MPATH_PREP:
Chun-Yeow Yeoh0f716512013-04-03 17:49:53 +0800152 mhwmp_dbg(sdata, "sending PREP to %pM\n", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100153 ie_len = 31;
154 pos = skb_put(skb, 2 + ie_len);
155 *pos++ = WLAN_EID_PREP;
156 break;
Rui Paulo90a5e162009-11-11 00:01:31 +0000157 case MPATH_RANN:
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200158 mhwmp_dbg(sdata, "sending RANN from %pM\n", orig_addr);
Rui Paulo90a5e162009-11-11 00:01:31 +0000159 ie_len = sizeof(struct ieee80211_rann_ie);
160 pos = skb_put(skb, 2 + ie_len);
161 *pos++ = WLAN_EID_RANN;
162 break;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100163 default:
Patrick McHardy812714d2008-05-06 12:52:07 +0200164 kfree_skb(skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100165 return -ENOTSUPP;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100166 }
167 *pos++ = ie_len;
168 *pos++ = flags;
169 *pos++ = hop_count;
170 *pos++ = ttl;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700171 if (action == MPATH_PREP) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000172 memcpy(pos, target, ETH_ALEN);
Rui Paulo90a5e162009-11-11 00:01:31 +0000173 pos += ETH_ALEN;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800174 put_unaligned_le32(target_sn, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700175 pos += 4;
176 } else {
177 if (action == MPATH_PREQ) {
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800178 put_unaligned_le32(preq_id, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700179 pos += 4;
180 }
181 memcpy(pos, orig_addr, ETH_ALEN);
182 pos += ETH_ALEN;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800183 put_unaligned_le32(orig_sn, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700184 pos += 4;
185 }
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800186 put_unaligned_le32(lifetime, pos); /* interval for RANN */
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700187 pos += 4;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800188 put_unaligned_le32(metric, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700189 pos += 4;
190 if (action == MPATH_PREQ) {
191 *pos++ = 1; /* destination count */
192 *pos++ = target_flags;
193 memcpy(pos, target, ETH_ALEN);
194 pos += ETH_ALEN;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800195 put_unaligned_le32(target_sn, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700196 pos += 4;
197 } else if (action == MPATH_PREP) {
198 memcpy(pos, orig_addr, ETH_ALEN);
199 pos += ETH_ALEN;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800200 put_unaligned_le32(orig_sn, pos);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700201 pos += 4;
Rui Paulo90a5e162009-11-11 00:01:31 +0000202 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100203
Johannes Berg62ae67b2009-11-18 18:42:05 +0100204 ieee80211_tx_skb(sdata, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100205 return 0;
206}
207
Javier Cardona2cca3972011-09-06 12:10:43 -0700208
209/* Headroom is not adjusted. Caller should ensure that skb has sufficient
210 * headroom in case the frame is encrypted. */
211static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
212 struct sk_buff *skb)
213{
Javier Cardona2cca3972011-09-06 12:10:43 -0700214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100215 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Javier Cardona2cca3972011-09-06 12:10:43 -0700216
217 skb_set_mac_header(skb, 0);
218 skb_set_network_header(skb, 0);
219 skb_set_transport_header(skb, 0);
220
221 /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
222 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
223 skb->priority = 7;
224
225 info->control.vif = &sdata->vif;
Bob Copeland9cbbffe2013-01-09 12:34:55 -0500226 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Javier Cardona2154c812011-09-07 17:49:53 -0700227 ieee80211_set_qos_hdr(sdata, skb);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100228 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
Javier Cardona2cca3972011-09-06 12:10:43 -0700229}
230
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100231/**
Bob Copeland75ea7192013-01-03 00:09:46 -0500232 * mesh_path_error_tx - Sends a PERR mesh management frame
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100233 *
Bob Copeland75ea7192013-01-03 00:09:46 -0500234 * @ttl: allowed remaining hops
Rui Paulod19b3bf2009-11-09 23:46:55 +0000235 * @target: broken destination
236 * @target_sn: SN of the broken destination
237 * @target_rcode: reason code for this PERR
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100238 * @ra: node this frame is addressed to
Bob Copeland75ea7192013-01-03 00:09:46 -0500239 * @sdata: local mesh subif
Javier Cardona2cca3972011-09-06 12:10:43 -0700240 *
241 * Note: This function may be called with driver locks taken that the driver
242 * also acquires in the TX path. To avoid a deadlock we don't transmit the
243 * frame directly but add it to the pending queue instead.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100244 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100245int mesh_path_error_tx(struct ieee80211_sub_if_data *sdata,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800246 u8 ttl, const u8 *target, u32 target_sn,
247 u16 target_rcode, const u8 *ra)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100248{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200249 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700250 struct sk_buff *skb;
Thomas Pedersendca7e942011-11-24 17:15:24 -0800251 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100252 struct ieee80211_mgmt *mgmt;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700253 u8 *pos, ie_len;
254 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
255 sizeof(mgmt->u.action.u.mesh_action);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100256
Thomas Pedersendca7e942011-11-24 17:15:24 -0800257 if (time_before(jiffies, ifmsh->next_perr))
258 return -EAGAIN;
259
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800260 skb = dev_alloc_skb(local->tx_headroom +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200261 sdata->encrypt_headroom +
Bob Copeland86804512013-01-09 12:34:56 -0500262 IEEE80211_ENCRYPT_TAILROOM +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700263 hdr_len +
264 2 + 15 /* PERR IE */);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100265 if (!skb)
266 return -1;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200267 skb_reserve(skb, local->tx_headroom + sdata->encrypt_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700268 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
269 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700270 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
271 IEEE80211_STYPE_ACTION);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100272
273 memcpy(mgmt->da, ra, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100274 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700275 /* BSSID == SA */
276 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
277 mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
278 mgmt->u.action.u.mesh_action.action_code =
279 WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
Rui Paulod611f062009-11-09 23:46:50 +0000280 ie_len = 15;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100281 pos = skb_put(skb, 2 + ie_len);
282 *pos++ = WLAN_EID_PERR;
283 *pos++ = ie_len;
Rui Paulod611f062009-11-09 23:46:50 +0000284 /* ttl */
Javier Cardona45904f22010-12-03 09:20:40 +0100285 *pos++ = ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100286 /* number of destinations */
287 *pos++ = 1;
Rui Paulod611f062009-11-09 23:46:50 +0000288 /*
289 * flags bit, bit 1 is unset if we know the sequence number and
290 * bit 2 is set if we have a reason code
291 */
292 *pos = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000293 if (!target_sn)
Rui Paulod611f062009-11-09 23:46:50 +0000294 *pos |= MP_F_USN;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000295 if (target_rcode)
Rui Paulod611f062009-11-09 23:46:50 +0000296 *pos |= MP_F_RCODE;
297 pos++;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000298 memcpy(pos, target, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100299 pos += ETH_ALEN;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800300 put_unaligned_le32(target_sn, pos);
Rui Paulod611f062009-11-09 23:46:50 +0000301 pos += 4;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800302 put_unaligned_le16(target_rcode, pos);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100303
Javier Cardona2cca3972011-09-06 12:10:43 -0700304 /* see note in function header */
305 prepare_frame_for_deferred_tx(sdata, skb);
Thomas Pedersendca7e942011-11-24 17:15:24 -0800306 ifmsh->next_perr = TU_TO_EXP_TIME(
307 ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
Javier Cardona2cca3972011-09-06 12:10:43 -0700308 ieee80211_add_pending_skb(local, skb);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100309 return 0;
310}
311
Javier Cardonabfc32e62009-08-17 17:15:55 -0700312void ieee80211s_update_metric(struct ieee80211_local *local,
Javier Cardona35b3fe1c2012-06-08 13:30:25 -0700313 struct sta_info *sta, struct sk_buff *skb)
Javier Cardonabfc32e62009-08-17 17:15:55 -0700314{
315 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
316 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
317 int failed;
318
319 if (!ieee80211_is_data(hdr->frame_control))
320 return;
321
322 failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
323
324 /* moving average, scaled to 100 */
Javier Cardona35b3fe1c2012-06-08 13:30:25 -0700325 sta->fail_avg = ((80 * sta->fail_avg + 5) / 100 + 20 * failed);
326 if (sta->fail_avg > 95)
327 mesh_plink_broken(sta);
Javier Cardonabfc32e62009-08-17 17:15:55 -0700328}
329
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100330static u32 airtime_link_metric_get(struct ieee80211_local *local,
331 struct sta_info *sta)
332{
Thomas Pedersen6b62bf32012-03-05 15:31:48 -0800333 struct rate_info rinfo;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100334 /* This should be adjusted for each device */
335 int device_constant = 1 << ARITH_SHIFT;
336 int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
337 int s_unit = 1 << ARITH_SHIFT;
338 int rate, err;
339 u32 tx_time, estimated_retx;
340 u64 result;
341
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100342 if (sta->fail_avg >= 100)
343 return MAX_METRIC;
Johannes Berge6a98542008-10-21 12:40:02 +0200344
Thomas Pedersen6b62bf32012-03-05 15:31:48 -0800345 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &rinfo);
346 rate = cfg80211_calculate_bitrate(&rinfo);
347 if (WARN_ON(!rate))
Johannes Berge6a98542008-10-21 12:40:02 +0200348 return MAX_METRIC;
349
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100350 err = (sta->fail_avg << ARITH_SHIFT) / 100;
351
352 /* bitrate is in units of 100 Kbps, while we need rate in units of
353 * 1Mbps. This will be corrected on tx_time computation.
354 */
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100355 tx_time = (device_constant + 10 * test_frame_len / rate);
356 estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
357 result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
358 return (u32)result;
359}
360
361/**
362 * hwmp_route_info_get - Update routing info to originator and transmitter
363 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200364 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100365 * @mgmt: mesh management frame
366 * @hwmp_ie: hwmp information element (PREP or PREQ)
Bob Copeland75ea7192013-01-03 00:09:46 -0500367 * @action: type of hwmp ie
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100368 *
369 * This function updates the path routing information to the originator and the
Andrey Yurovskyf99288d2009-10-20 12:17:34 -0700370 * transmitter of a HWMP PREQ or PREP frame.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100371 *
372 * Returns: metric to frame originator or 0 if the frame should not be further
373 * processed
374 *
375 * Notes: this function is the only place (besides user-provided info) where
376 * path routing information is updated.
377 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200378static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100379 struct ieee80211_mgmt *mgmt,
380 const u8 *hwmp_ie, enum mpath_frame_type action)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100381{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200382 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100383 struct mesh_path *mpath;
384 struct sta_info *sta;
385 bool fresh_info;
Johannes Berg4a3cb702013-02-12 16:43:19 +0100386 const u8 *orig_addr, *ta;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000387 u32 orig_sn, orig_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100388 unsigned long orig_lifetime, exp_time;
389 u32 last_hop_metric, new_metric;
390 bool process = true;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100391
392 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100393 sta = sta_info_get(sdata, mgmt->sa);
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100394 if (!sta) {
395 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100396 return 0;
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100397 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100398
399 last_hop_metric = airtime_link_metric_get(local, sta);
400 /* Update and check originator routing info */
401 fresh_info = true;
402
403 switch (action) {
404 case MPATH_PREQ:
405 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000406 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100407 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
408 orig_metric = PREQ_IE_METRIC(hwmp_ie);
409 break;
410 case MPATH_PREP:
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800411 /* Originator here refers to the MP that was the target in the
412 * Path Request. We divert from the nomenclature in the draft
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100413 * so that we can easily use a single function to gather path
414 * information from both PREQ and PREP frames.
415 */
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800416 orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
417 orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100418 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
419 orig_metric = PREP_IE_METRIC(hwmp_ie);
420 break;
421 default:
Johannes Bergdc0b0f72008-02-23 15:17:20 +0100422 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100423 return 0;
424 }
425 new_metric = orig_metric + last_hop_metric;
426 if (new_metric < orig_metric)
427 new_metric = MAX_METRIC;
428 exp_time = TU_TO_EXP_TIME(orig_lifetime);
429
Joe Perchesb203ca32012-05-08 18:56:52 +0000430 if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100431 /* This MP is the originator, we are not interested in this
432 * frame, except for updating transmitter's path info.
433 */
434 process = false;
435 fresh_info = false;
436 } else {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100437 mpath = mesh_path_lookup(sdata, orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100438 if (mpath) {
439 spin_lock_bh(&mpath->state_lock);
440 if (mpath->flags & MESH_PATH_FIXED)
441 fresh_info = false;
442 else if ((mpath->flags & MESH_PATH_ACTIVE) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000443 (mpath->flags & MESH_PATH_SN_VALID)) {
444 if (SN_GT(mpath->sn, orig_sn) ||
445 (mpath->sn == orig_sn &&
Porsch, Marco533866b2010-02-24 09:53:13 +0100446 new_metric >= mpath->metric)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100447 process = false;
448 fresh_info = false;
449 }
Jesse Jonesd8f03002015-06-12 15:38:07 -0700450 } else if (!(mpath->flags & MESH_PATH_ACTIVE)) {
451 bool have_sn, newer_sn, bounced;
452
453 have_sn = mpath->flags & MESH_PATH_SN_VALID;
454 newer_sn = have_sn && SN_GT(orig_sn, mpath->sn);
455 bounced = have_sn &&
456 (SN_DELTA(orig_sn, mpath->sn) >
457 MAX_SANE_SN_DELTA);
458
459 if (!have_sn || newer_sn) {
460 /* if SN is newer than what we had
461 * then we can take it */;
462 } else if (bounced) {
463 /* if SN is way different than what
464 * we had then assume the other side
465 * rebooted or restarted */;
466 } else {
467 process = false;
468 fresh_info = false;
469 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100470 }
471 } else {
Bob Copelandae76eef2013-03-29 09:38:39 -0400472 mpath = mesh_path_add(sdata, orig_addr);
473 if (IS_ERR(mpath)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100474 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100475 return 0;
476 }
477 spin_lock_bh(&mpath->state_lock);
478 }
479
480 if (fresh_info) {
481 mesh_path_assign_nexthop(mpath, sta);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000482 mpath->flags |= MESH_PATH_SN_VALID;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100483 mpath->metric = new_metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000484 mpath->sn = orig_sn;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100485 mpath->exp_time = time_after(mpath->exp_time, exp_time)
486 ? mpath->exp_time : exp_time;
487 mesh_path_activate(mpath);
488 spin_unlock_bh(&mpath->state_lock);
489 mesh_path_tx_pending(mpath);
490 /* draft says preq_id should be saved to, but there does
491 * not seem to be any use for it, skipping by now
492 */
493 } else
494 spin_unlock_bh(&mpath->state_lock);
495 }
496
497 /* Update and check transmitter routing info */
498 ta = mgmt->sa;
Joe Perchesb203ca32012-05-08 18:56:52 +0000499 if (ether_addr_equal(orig_addr, ta))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100500 fresh_info = false;
501 else {
502 fresh_info = true;
503
Johannes Bergbf7cd942013-02-15 14:40:31 +0100504 mpath = mesh_path_lookup(sdata, ta);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100505 if (mpath) {
506 spin_lock_bh(&mpath->state_lock);
507 if ((mpath->flags & MESH_PATH_FIXED) ||
508 ((mpath->flags & MESH_PATH_ACTIVE) &&
509 (last_hop_metric > mpath->metric)))
510 fresh_info = false;
511 } else {
Bob Copelandae76eef2013-03-29 09:38:39 -0400512 mpath = mesh_path_add(sdata, ta);
513 if (IS_ERR(mpath)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100514 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100515 return 0;
516 }
517 spin_lock_bh(&mpath->state_lock);
518 }
519
520 if (fresh_info) {
521 mesh_path_assign_nexthop(mpath, sta);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100522 mpath->metric = last_hop_metric;
523 mpath->exp_time = time_after(mpath->exp_time, exp_time)
524 ? mpath->exp_time : exp_time;
525 mesh_path_activate(mpath);
526 spin_unlock_bh(&mpath->state_lock);
527 mesh_path_tx_pending(mpath);
528 } else
529 spin_unlock_bh(&mpath->state_lock);
530 }
531
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100532 rcu_read_unlock();
533
534 return process ? new_metric : 0;
535}
536
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200537static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100538 struct ieee80211_mgmt *mgmt,
Alexis Green5ec596c2015-06-09 16:20:24 -0700539 const u8 *preq_elem, u32 orig_metric)
David Woo57ef5dd2009-08-12 11:03:43 -0700540{
Johannes Berg472dbc42008-09-11 00:01:49 +0200541 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800542 struct mesh_path *mpath = NULL;
Johannes Berg4a3cb702013-02-12 16:43:19 +0100543 const u8 *target_addr, *orig_addr;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800544 const u8 *da;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800545 u8 target_flags, ttl, flags;
Alexis Green5ec596c2015-06-09 16:20:24 -0700546 u32 orig_sn, target_sn, lifetime, target_metric;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100547 bool reply = false;
548 bool forward = true;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800549 bool root_is_gate;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100550
Rui Paulod19b3bf2009-11-09 23:46:55 +0000551 /* Update target SN, if present */
552 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100553 orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000554 target_sn = PREQ_IE_TARGET_SN(preq_elem);
555 orig_sn = PREQ_IE_ORIG_SN(preq_elem);
556 target_flags = PREQ_IE_TARGET_F(preq_elem);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800557 /* Proactive PREQ gate announcements */
558 flags = PREQ_IE_FLAGS(preq_elem);
559 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100560
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200561 mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
Rui Paulo27db2e42009-11-09 23:46:45 +0000562
Joe Perchesb203ca32012-05-08 18:56:52 +0000563 if (ether_addr_equal(target_addr, sdata->vif.addr)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200564 mhwmp_dbg(sdata, "PREQ is for us\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100565 forward = false;
566 reply = true;
Alexis Green5ec596c2015-06-09 16:20:24 -0700567 target_metric = 0;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000568 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100569 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +0000570 time_before(jiffies, ifmsh->last_sn_update)) {
Bob Copelandbc3ce0b2014-04-15 10:43:08 -0400571 ++ifmsh->sn;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000572 ifmsh->last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100573 }
Bob Copelandbc3ce0b2014-04-15 10:43:08 -0400574 target_sn = ifmsh->sn;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800575 } else if (is_broadcast_ether_addr(target_addr) &&
576 (target_flags & IEEE80211_PREQ_TO_FLAG)) {
577 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +0100578 mpath = mesh_path_lookup(sdata, orig_addr);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800579 if (mpath) {
580 if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
581 reply = true;
582 target_addr = sdata->vif.addr;
583 target_sn = ++ifmsh->sn;
Alexis Green5ec596c2015-06-09 16:20:24 -0700584 target_metric = 0;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800585 ifmsh->last_sn_update = jiffies;
586 }
587 if (root_is_gate)
588 mesh_path_add_gate(mpath);
589 }
590 rcu_read_unlock();
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100591 } else {
592 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +0100593 mpath = mesh_path_lookup(sdata, target_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100594 if (mpath) {
Rui Paulod19b3bf2009-11-09 23:46:55 +0000595 if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
596 SN_LT(mpath->sn, target_sn)) {
597 mpath->sn = target_sn;
598 mpath->flags |= MESH_PATH_SN_VALID;
599 } else if ((!(target_flags & MP_F_DO)) &&
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100600 (mpath->flags & MESH_PATH_ACTIVE)) {
601 reply = true;
Alexis Green5ec596c2015-06-09 16:20:24 -0700602 target_metric = mpath->metric;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000603 target_sn = mpath->sn;
604 if (target_flags & MP_F_RF)
605 target_flags |= MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100606 else
607 forward = false;
608 }
609 }
610 rcu_read_unlock();
611 }
612
613 if (reply) {
614 lifetime = PREQ_IE_LIFETIME(preq_elem);
Javier Cardona45904f22010-12-03 09:20:40 +0100615 ttl = ifmsh->mshcfg.element_ttl;
Rui Paulo27db2e42009-11-09 23:46:45 +0000616 if (ttl != 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200617 mhwmp_dbg(sdata, "replying to the PREQ\n");
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800618 mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800619 orig_sn, 0, target_addr,
620 target_sn, mgmt->sa, 0, ttl,
Alexis Green5ec596c2015-06-09 16:20:24 -0700621 lifetime, target_metric, 0,
622 sdata);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800623 } else {
Johannes Berg472dbc42008-09-11 00:01:49 +0200624 ifmsh->mshstats.dropped_frames_ttl++;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800625 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100626 }
627
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +0800628 if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100629 u32 preq_id;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800630 u8 hopcount;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100631
632 ttl = PREQ_IE_TTL(preq_elem);
633 lifetime = PREQ_IE_LIFETIME(preq_elem);
634 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200635 ifmsh->mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100636 return;
637 }
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200638 mhwmp_dbg(sdata, "forwarding the PREQ from %pM\n", orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100639 --ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100640 preq_id = PREQ_IE_PREQ_ID(preq_elem);
641 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800642 da = (mpath && mpath->is_root) ?
643 mpath->rann_snd_addr : broadcast_addr;
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800644
645 if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
646 target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
647 target_sn = PREQ_IE_TARGET_SN(preq_elem);
Chun-Yeow Yeoh3fbf4b71b2012-06-17 02:27:40 +0800648 }
649
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100650 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800651 orig_sn, target_flags, target_addr,
652 target_sn, da, hopcount, ttl, lifetime,
Alexis Green5ec596c2015-06-09 16:20:24 -0700653 orig_metric, preq_id, sdata);
Chun-Yeow Yeoh7d4e15b2012-05-04 14:57:50 +0800654 if (!is_multicast_ether_addr(da))
655 ifmsh->mshstats.fwded_unicast++;
656 else
657 ifmsh->mshstats.fwded_mcast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200658 ifmsh->mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100659 }
660}
661
662
Johannes Berg40b275b2011-05-13 14:15:49 +0200663static inline struct sta_info *
664next_hop_deref_protected(struct mesh_path *mpath)
665{
666 return rcu_dereference_protected(mpath->next_hop,
667 lockdep_is_held(&mpath->state_lock));
668}
669
670
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200671static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100672 struct ieee80211_mgmt *mgmt,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100673 const u8 *prep_elem, u32 metric)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100674{
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800675 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100676 struct mesh_path *mpath;
Johannes Berg4a3cb702013-02-12 16:43:19 +0100677 const u8 *target_addr, *orig_addr;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100678 u8 ttl, hopcount, flags;
679 u8 next_hop[ETH_ALEN];
Rui Paulod19b3bf2009-11-09 23:46:55 +0000680 u32 target_sn, orig_sn, lifetime;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100681
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200682 mhwmp_dbg(sdata, "received PREP from %pM\n",
Chun-Yeow Yeoh0f716512013-04-03 17:49:53 +0800683 PREP_IE_TARGET_ADDR(prep_elem));
Rui Paulodbb81c42009-11-09 23:46:46 +0000684
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800685 orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
Joe Perchesb203ca32012-05-08 18:56:52 +0000686 if (ether_addr_equal(orig_addr, sdata->vif.addr))
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100687 /* destination, no forwarding required */
688 return;
689
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800690 if (!ifmsh->mshcfg.dot11MeshForwarding)
691 return;
692
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100693 ttl = PREP_IE_TTL(prep_elem);
694 if (ttl <= 1) {
Johannes Berg472dbc42008-09-11 00:01:49 +0200695 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100696 return;
697 }
698
699 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +0100700 mpath = mesh_path_lookup(sdata, orig_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100701 if (mpath)
702 spin_lock_bh(&mpath->state_lock);
703 else
704 goto fail;
705 if (!(mpath->flags & MESH_PATH_ACTIVE)) {
706 spin_unlock_bh(&mpath->state_lock);
707 goto fail;
708 }
Johannes Berg40b275b2011-05-13 14:15:49 +0200709 memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100710 spin_unlock_bh(&mpath->state_lock);
711 --ttl;
712 flags = PREP_IE_FLAGS(prep_elem);
713 lifetime = PREP_IE_LIFETIME(prep_elem);
714 hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
Thomas Pedersen3c26f1f2011-11-24 17:15:22 -0800715 target_addr = PREP_IE_TARGET_ADDR(prep_elem);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000716 target_sn = PREP_IE_TARGET_SN(prep_elem);
717 orig_sn = PREP_IE_ORIG_SN(prep_elem);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100718
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800719 mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, orig_sn, 0,
720 target_addr, target_sn, next_hop, hopcount,
721 ttl, lifetime, metric, 0, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100722 rcu_read_unlock();
Daniel Walkerc8a61a72009-08-18 10:59:00 -0700723
724 sdata->u.mesh.mshstats.fwded_unicast++;
Johannes Berg472dbc42008-09-11 00:01:49 +0200725 sdata->u.mesh.mshstats.fwded_frames++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100726 return;
727
728fail:
729 rcu_read_unlock();
Johannes Berg472dbc42008-09-11 00:01:49 +0200730 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100731}
732
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200733static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100734 struct ieee80211_mgmt *mgmt,
735 const u8 *perr_elem)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100736{
Rui Paulod611f062009-11-09 23:46:50 +0000737 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100738 struct mesh_path *mpath;
Rui Paulod611f062009-11-09 23:46:50 +0000739 u8 ttl;
Johannes Berg4a3cb702013-02-12 16:43:19 +0100740 const u8 *ta, *target_addr;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000741 u32 target_sn;
742 u16 target_rcode;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100743
744 ta = mgmt->sa;
Rui Paulod611f062009-11-09 23:46:50 +0000745 ttl = PERR_IE_TTL(perr_elem);
746 if (ttl <= 1) {
747 ifmsh->mshstats.dropped_frames_ttl++;
748 return;
749 }
750 ttl--;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000751 target_addr = PERR_IE_TARGET_ADDR(perr_elem);
752 target_sn = PERR_IE_TARGET_SN(perr_elem);
753 target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
Rui Paulod611f062009-11-09 23:46:50 +0000754
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100755 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +0100756 mpath = mesh_path_lookup(sdata, target_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100757 if (mpath) {
Felix Fietkau888d04d2012-03-01 15:22:09 +0100758 struct sta_info *sta;
759
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100760 spin_lock_bh(&mpath->state_lock);
Felix Fietkau888d04d2012-03-01 15:22:09 +0100761 sta = next_hop_deref_protected(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100762 if (mpath->flags & MESH_PATH_ACTIVE &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000763 ether_addr_equal(ta, sta->sta.addr) &&
Rui Paulod19b3bf2009-11-09 23:46:55 +0000764 (!(mpath->flags & MESH_PATH_SN_VALID) ||
Alexis Green703ee732015-06-10 11:02:09 -0700765 SN_GT(target_sn, mpath->sn) || target_sn == 0)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100766 mpath->flags &= ~MESH_PATH_ACTIVE;
Alexis Green703ee732015-06-10 11:02:09 -0700767 if (target_sn != 0)
768 mpath->sn = target_sn;
769 else
770 mpath->sn += 1;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100771 spin_unlock_bh(&mpath->state_lock);
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800772 if (!ifmsh->mshcfg.dot11MeshForwarding)
773 goto endperr;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100774 mesh_path_error_tx(sdata, ttl, target_addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800775 target_sn, target_rcode,
Johannes Bergbf7cd942013-02-15 14:40:31 +0100776 broadcast_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100777 } else
778 spin_unlock_bh(&mpath->state_lock);
779 }
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +0800780endperr:
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100781 rcu_read_unlock();
782}
783
Rui Paulo90a5e162009-11-11 00:01:31 +0000784static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +0100785 struct ieee80211_mgmt *mgmt,
786 const struct ieee80211_rann_ie *rann)
Rui Paulo90a5e162009-11-11 00:01:31 +0000787{
788 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800789 struct ieee80211_local *local = sdata->local;
790 struct sta_info *sta;
Rui Paulo90a5e162009-11-11 00:01:31 +0000791 struct mesh_path *mpath;
Rui Paulo90a5e162009-11-11 00:01:31 +0000792 u8 ttl, flags, hopcount;
Johannes Berg4a3cb702013-02-12 16:43:19 +0100793 const u8 *orig_addr;
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800794 u32 orig_sn, metric, metric_txsta, interval;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700795 bool root_is_gate;
Rui Paulo90a5e162009-11-11 00:01:31 +0000796
Rui Paulo90a5e162009-11-11 00:01:31 +0000797 ttl = rann->rann_ttl;
Rui Paulo90a5e162009-11-11 00:01:31 +0000798 flags = rann->rann_flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700799 root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
Rui Paulo90a5e162009-11-11 00:01:31 +0000800 orig_addr = rann->rann_addr;
Chun-Yeow Yeoh292c41a2012-03-19 21:38:46 +0800801 orig_sn = le32_to_cpu(rann->rann_seq);
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800802 interval = le32_to_cpu(rann->rann_interval);
Rui Paulo90a5e162009-11-11 00:01:31 +0000803 hopcount = rann->rann_hopcount;
Rui Pauloa6a58b42009-11-09 23:46:51 +0000804 hopcount++;
Chun-Yeow Yeoh292c41a2012-03-19 21:38:46 +0800805 metric = le32_to_cpu(rann->rann_metric);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700806
807 /* Ignore our own RANNs */
Joe Perchesb203ca32012-05-08 18:56:52 +0000808 if (ether_addr_equal(orig_addr, sdata->vif.addr))
Javier Cardona5ee68e52011-08-09 16:45:08 -0700809 return;
810
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200811 mhwmp_dbg(sdata,
812 "received RANN from %pM via neighbour %pM (is_gate=%d)\n",
813 orig_addr, mgmt->sa, root_is_gate);
Rui Paulo90a5e162009-11-11 00:01:31 +0000814
815 rcu_read_lock();
Chun-Yeow Yeohd2a079f2012-03-23 18:48:51 +0800816 sta = sta_info_get(sdata, mgmt->sa);
817 if (!sta) {
818 rcu_read_unlock();
819 return;
820 }
821
822 metric_txsta = airtime_link_metric_get(local, sta);
823
Johannes Bergbf7cd942013-02-15 14:40:31 +0100824 mpath = mesh_path_lookup(sdata, orig_addr);
Rui Paulo90a5e162009-11-11 00:01:31 +0000825 if (!mpath) {
Bob Copelandae76eef2013-03-29 09:38:39 -0400826 mpath = mesh_path_add(sdata, orig_addr);
827 if (IS_ERR(mpath)) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000828 rcu_read_unlock();
829 sdata->u.mesh.mshstats.dropped_frames_no_route++;
830 return;
831 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000832 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700833
Chun-Yeow Yeoh7ebfa462012-06-15 10:20:02 +0800834 if (!(SN_LT(mpath->sn, orig_sn)) &&
835 !(mpath->sn == orig_sn && metric < mpath->rann_metric)) {
836 rcu_read_unlock();
837 return;
838 }
839
Javier Cardona5ee68e52011-08-09 16:45:08 -0700840 if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +0800841 (time_after(jiffies, mpath->last_preq_to_root +
842 root_path_confirmation_jiffies(sdata)) ||
843 time_before(jiffies, mpath->last_preq_to_root))) &&
Chun-Yeow Yeoh7ebfa462012-06-15 10:20:02 +0800844 !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200845 mhwmp_dbg(sdata,
846 "time to refresh root mpath %pM\n",
847 orig_addr);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700848 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +0800849 mpath->last_preq_to_root = jiffies;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700850 }
851
Chun-Yeow Yeoh7ebfa462012-06-15 10:20:02 +0800852 mpath->sn = orig_sn;
853 mpath->rann_metric = metric + metric_txsta;
854 mpath->is_root = true;
855 /* Recording RANNs sender address to send individually
856 * addressed PREQs destined for root mesh STA */
857 memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
858
859 if (root_is_gate)
860 mesh_path_add_gate(mpath);
861
862 if (ttl <= 1) {
863 ifmsh->mshstats.dropped_frames_ttl++;
864 rcu_read_unlock();
865 return;
866 }
867 ttl--;
868
869 if (ifmsh->mshcfg.dot11MeshForwarding) {
Rui Paulo90a5e162009-11-11 00:01:31 +0000870 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800871 orig_sn, 0, NULL, 0, broadcast_addr,
872 hopcount, ttl, interval,
873 metric + metric_txsta, 0, sdata);
Rui Paulo90a5e162009-11-11 00:01:31 +0000874 }
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +0800875
Rui Paulo90a5e162009-11-11 00:01:31 +0000876 rcu_read_unlock();
877}
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100878
879
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200880void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
Johannes Bergbf7cd942013-02-15 14:40:31 +0100881 struct ieee80211_mgmt *mgmt, size_t len)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100882{
883 struct ieee802_11_elems elems;
884 size_t baselen;
Alexis Greenafd2efb2015-06-05 12:43:54 -0700885 u32 path_metric;
Javier Cardona97091312011-10-06 14:54:22 -0700886 struct sta_info *sta;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100887
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200888 /* need action_code */
889 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
890 return;
891
Javier Cardona97091312011-10-06 14:54:22 -0700892 rcu_read_lock();
893 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg433f5bc2015-06-17 10:31:00 +0200894 if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
Javier Cardona97091312011-10-06 14:54:22 -0700895 rcu_read_unlock();
896 return;
897 }
898 rcu_read_unlock();
899
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100900 baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
901 ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +0100902 len - baselen, false, &elems);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100903
Rui Paulodbb81c42009-11-09 23:46:46 +0000904 if (elems.preq) {
905 if (elems.preq_len != 37)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100906 /* Right now we support just 1 destination and no AE */
907 return;
Alexis Greenafd2efb2015-06-05 12:43:54 -0700908 path_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
909 MPATH_PREQ);
910 if (path_metric)
Rui Paulodbb81c42009-11-09 23:46:46 +0000911 hwmp_preq_frame_process(sdata, mgmt, elems.preq,
Alexis Greenafd2efb2015-06-05 12:43:54 -0700912 path_metric);
Rui Paulodbb81c42009-11-09 23:46:46 +0000913 }
914 if (elems.prep) {
915 if (elems.prep_len != 31)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100916 /* Right now we support no AE */
917 return;
Alexis Greenafd2efb2015-06-05 12:43:54 -0700918 path_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
919 MPATH_PREP);
920 if (path_metric)
Rui Paulodbb81c42009-11-09 23:46:46 +0000921 hwmp_prep_frame_process(sdata, mgmt, elems.prep,
Alexis Greenafd2efb2015-06-05 12:43:54 -0700922 path_metric);
Rui Paulodbb81c42009-11-09 23:46:46 +0000923 }
924 if (elems.perr) {
Rui Paulod611f062009-11-09 23:46:50 +0000925 if (elems.perr_len != 15)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100926 /* Right now we support only one destination per PERR */
927 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200928 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100929 }
Rui Paulo90a5e162009-11-11 00:01:31 +0000930 if (elems.rann)
931 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100932}
933
934/**
935 * mesh_queue_preq - queue a PREQ to a given destination
936 *
937 * @mpath: mesh path to discover
938 * @flags: special attributes of the PREQ to be sent
939 *
940 * Locking: the function must be called from within a rcu read lock block.
941 *
942 */
943static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
944{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200945 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Johannes Berg472dbc42008-09-11 00:01:49 +0200946 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100947 struct mesh_preq_queue *preq_node;
948
Andrey Yurovsky59615b52009-06-25 16:07:42 -0700949 preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100950 if (!preq_node) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200951 mhwmp_dbg(sdata, "could not allocate PREQ node\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100952 return;
953 }
954
Baruch Siach987dafa2011-07-28 08:51:05 +0300955 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +0200956 if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
Baruch Siach987dafa2011-07-28 08:51:05 +0300957 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100958 kfree(preq_node);
959 if (printk_ratelimit())
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200960 mhwmp_dbg(sdata, "PREQ node queue full\n");
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100961 return;
962 }
963
Johannes Bergf2dc7982011-11-18 15:27:31 +0100964 spin_lock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700965 if (mpath->flags & MESH_PATH_REQ_QUEUED) {
Johannes Bergf2dc7982011-11-18 15:27:31 +0100966 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700967 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Dan Carpenter88d53462011-11-15 09:33:31 +0300968 kfree(preq_node);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700969 return;
970 }
971
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100972 memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
973 preq_node->flags = flags;
974
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700975 mpath->flags |= MESH_PATH_REQ_QUEUED;
Johannes Bergf2dc7982011-11-18 15:27:31 +0100976 spin_unlock(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -0700977
Johannes Berg472dbc42008-09-11 00:01:49 +0200978 list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
979 ++ifmsh->preq_queue_len;
Baruch Siach987dafa2011-07-28 08:51:05 +0300980 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100981
Johannes Berg472dbc42008-09-11 00:01:49 +0200982 if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
Johannes Berg64592c82010-06-10 10:21:31 +0200983 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100984
Johannes Berg472dbc42008-09-11 00:01:49 +0200985 else if (time_before(jiffies, ifmsh->last_preq)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100986 /* avoid long wait if did not send preqs for a long time
987 * and jiffies wrapped around
988 */
Johannes Berg472dbc42008-09-11 00:01:49 +0200989 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
Johannes Berg64592c82010-06-10 10:21:31 +0200990 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100991 } else
Johannes Berg472dbc42008-09-11 00:01:49 +0200992 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +0100993 min_preq_int_jiff(sdata));
994}
995
996/**
997 * mesh_path_start_discovery - launch a path discovery from the PREQ queue
998 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200999 * @sdata: local mesh subif
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001000 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001001void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001002{
Johannes Berg472dbc42008-09-11 00:01:49 +02001003 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001004 struct mesh_preq_queue *preq_node;
1005 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001006 u8 ttl, target_flags;
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +08001007 const u8 *da;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001008 u32 lifetime;
1009
Johannes Berga43816d2009-07-10 11:39:26 +02001010 spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
Johannes Berg472dbc42008-09-11 00:01:49 +02001011 if (!ifmsh->preq_queue_len ||
1012 time_before(jiffies, ifmsh->last_preq +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001013 min_preq_int_jiff(sdata))) {
Johannes Berga43816d2009-07-10 11:39:26 +02001014 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001015 return;
1016 }
1017
Johannes Berg472dbc42008-09-11 00:01:49 +02001018 preq_node = list_first_entry(&ifmsh->preq_queue.list,
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001019 struct mesh_preq_queue, list);
1020 list_del(&preq_node->list);
Johannes Berg472dbc42008-09-11 00:01:49 +02001021 --ifmsh->preq_queue_len;
Johannes Berga43816d2009-07-10 11:39:26 +02001022 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001023
1024 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01001025 mpath = mesh_path_lookup(sdata, preq_node->dst);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001026 if (!mpath)
1027 goto enddiscovery;
1028
1029 spin_lock_bh(&mpath->state_lock);
Javier Cardonaf3011cf2011-11-03 21:11:10 -07001030 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001031 if (preq_node->flags & PREQ_Q_F_START) {
1032 if (mpath->flags & MESH_PATH_RESOLVING) {
1033 spin_unlock_bh(&mpath->state_lock);
1034 goto enddiscovery;
1035 } else {
1036 mpath->flags &= ~MESH_PATH_RESOLVED;
1037 mpath->flags |= MESH_PATH_RESOLVING;
1038 mpath->discovery_retries = 0;
1039 mpath->discovery_timeout = disc_timeout_jiff(sdata);
1040 }
1041 } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
1042 mpath->flags & MESH_PATH_RESOLVED) {
1043 mpath->flags &= ~MESH_PATH_RESOLVING;
1044 spin_unlock_bh(&mpath->state_lock);
1045 goto enddiscovery;
1046 }
1047
Johannes Berg472dbc42008-09-11 00:01:49 +02001048 ifmsh->last_preq = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001049
Rui Paulod19b3bf2009-11-09 23:46:55 +00001050 if (time_after(jiffies, ifmsh->last_sn_update +
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001051 net_traversal_jiffies(sdata)) ||
Rui Paulod19b3bf2009-11-09 23:46:55 +00001052 time_before(jiffies, ifmsh->last_sn_update)) {
1053 ++ifmsh->sn;
1054 sdata->u.mesh.last_sn_update = jiffies;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001055 }
1056 lifetime = default_lifetime(sdata);
Javier Cardona45904f22010-12-03 09:20:40 +01001057 ttl = sdata->u.mesh.mshcfg.element_ttl;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001058 if (ttl == 0) {
Johannes Berg472dbc42008-09-11 00:01:49 +02001059 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001060 spin_unlock_bh(&mpath->state_lock);
1061 goto enddiscovery;
1062 }
1063
1064 if (preq_node->flags & PREQ_Q_F_REFRESH)
Rui Paulod19b3bf2009-11-09 23:46:55 +00001065 target_flags = MP_F_DO;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001066 else
Rui Paulod19b3bf2009-11-09 23:46:55 +00001067 target_flags = MP_F_RF;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001068
1069 spin_unlock_bh(&mpath->state_lock);
Chun-Yeow Yeoh3d045a52012-02-28 22:00:06 +08001070 da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08001071 mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, ifmsh->sn,
1072 target_flags, mpath->dst, mpath->sn, da, 0,
1073 ttl, lifetime, 0, ifmsh->preq_id++, sdata);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001074 mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
1075
1076enddiscovery:
1077 rcu_read_unlock();
1078 kfree(preq_node);
1079}
1080
Ben Hutchings2c530402012-07-10 10:55:09 +00001081/**
1082 * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001083 *
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001084 * @skb: 802.11 frame to be sent
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001085 * @sdata: network subif the frame will be sent through
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001086 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001087 * Lookup next hop for given skb and start path discovery if no
1088 * forwarding information is found.
1089 *
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001090 * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
1091 * skb is freeed here if no mpath could be allocated.
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001092 */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001093int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
1094 struct sk_buff *skb)
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001095{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001096 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001097 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1098 struct mesh_path *mpath;
1099 struct sk_buff *skb_to_free = NULL;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001100 u8 *target_addr = hdr->addr3;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001101 int err = 0;
1102
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001103 /* Nulls are only sent to peers for PS and should be pre-addressed */
1104 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
1105 return 0;
1106
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001107 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01001108 err = mesh_nexthop_lookup(sdata, skb);
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001109 if (!err)
1110 goto endlookup;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001111
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001112 /* no nexthop found, start resolving */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001113 mpath = mesh_path_lookup(sdata, target_addr);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001114 if (!mpath) {
Bob Copelandae76eef2013-03-29 09:38:39 -04001115 mpath = mesh_path_add(sdata, target_addr);
1116 if (IS_ERR(mpath)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01001117 mesh_path_discard_frame(sdata, skb);
Bob Copelandae76eef2013-03-29 09:38:39 -04001118 err = PTR_ERR(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001119 goto endlookup;
1120 }
1121 }
1122
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001123 if (!(mpath->flags & MESH_PATH_RESOLVING))
1124 mesh_queue_preq(mpath, PREQ_Q_F_START);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001125
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001126 if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
1127 skb_to_free = skb_dequeue(&mpath->frame_queue);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001128
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001129 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1130 ieee80211_set_qos_hdr(sdata, skb);
1131 skb_queue_tail(&mpath->frame_queue, skb);
1132 err = -ENOENT;
1133 if (skb_to_free)
Johannes Bergbf7cd942013-02-15 14:40:31 +01001134 mesh_path_discard_frame(sdata, skb_to_free);
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001135
1136endlookup:
1137 rcu_read_unlock();
1138 return err;
1139}
Johannes Bergbf7cd942013-02-15 14:40:31 +01001140
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001141/**
1142 * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
1143 * this function is considered "using" the associated mpath, so preempt a path
1144 * refresh if this mpath expires soon.
1145 *
1146 * @skb: 802.11 frame to be sent
1147 * @sdata: network subif the frame will be sent through
1148 *
1149 * Returns: 0 if the next hop was found. Nonzero otherwise.
1150 */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001151int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
1152 struct sk_buff *skb)
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001153{
1154 struct mesh_path *mpath;
1155 struct sta_info *next_hop;
1156 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1157 u8 *target_addr = hdr->addr3;
1158 int err = -ENOENT;
1159
1160 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01001161 mpath = mesh_path_lookup(sdata, target_addr);
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001162
1163 if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
1164 goto endlookup;
1165
1166 if (time_after(jiffies,
1167 mpath->exp_time -
1168 msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001169 ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001170 !(mpath->flags & MESH_PATH_RESOLVING) &&
1171 !(mpath->flags & MESH_PATH_FIXED))
1172 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
1173
1174 next_hop = rcu_dereference(mpath->next_hop);
1175 if (next_hop) {
1176 memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
1177 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001178 ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001179 err = 0;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001180 }
1181
1182endlookup:
1183 rcu_read_unlock();
1184 return err;
1185}
1186
1187void mesh_path_timer(unsigned long data)
1188{
Johannes Bergdea40962011-05-12 15:03:32 +02001189 struct mesh_path *mpath = (void *) data;
1190 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001191 int ret;
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001192
Johannes Bergdea40962011-05-12 15:03:32 +02001193 if (sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001194 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001195
1196 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -08001197 if (mpath->flags & MESH_PATH_RESOLVED ||
Javier Cardona5ee68e52011-08-09 16:45:08 -07001198 (!(mpath->flags & MESH_PATH_RESOLVING))) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001199 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001200 spin_unlock_bh(&mpath->state_lock);
1201 } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001202 ++mpath->discovery_retries;
1203 mpath->discovery_timeout *= 2;
Javier Cardonaf3011cf2011-11-03 21:11:10 -07001204 mpath->flags &= ~MESH_PATH_REQ_QUEUED;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001205 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001206 mesh_queue_preq(mpath, 0);
1207 } else {
Jesse Jonesd8254712015-06-12 14:13:09 -07001208 mpath->flags &= ~(MESH_PATH_RESOLVING |
1209 MESH_PATH_RESOLVED |
1210 MESH_PATH_REQ_QUEUED);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001211 mpath->exp_time = jiffies;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001212 spin_unlock_bh(&mpath->state_lock);
1213 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
1214 ret = mesh_path_send_to_gates(mpath);
1215 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001216 mhwmp_dbg(sdata, "no gate was reachable\n");
Javier Cardona5ee68e52011-08-09 16:45:08 -07001217 } else
1218 mesh_path_flush_pending(mpath);
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001219 }
Luis Carlos Cobo050ac522008-02-23 15:17:15 +01001220}
Rui Pauloe304bfd2009-11-09 23:46:56 +00001221
Johannes Bergbf7cd942013-02-15 14:40:31 +01001222void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
Rui Pauloe304bfd2009-11-09 23:46:56 +00001223{
1224 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Javier Cardona0507e152011-08-09 16:45:10 -07001225 u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001226 u8 flags, target_flags = 0;
Rui Pauloe304bfd2009-11-09 23:46:56 +00001227
Javier Cardona16dd7262011-08-09 16:45:11 -07001228 flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
1229 ? RANN_FLAG_IS_GATE : 0;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001230
1231 switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
1232 case IEEE80211_PROACTIVE_RANN:
1233 mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08001234 ++ifmsh->sn, 0, NULL, 0, broadcast_addr,
1235 0, ifmsh->mshcfg.element_ttl,
1236 interval, 0, 0, sdata);
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001237 break;
1238 case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
1239 flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
1240 case IEEE80211_PROACTIVE_PREQ_NO_PREP:
1241 interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
1242 target_flags |= IEEE80211_PREQ_TO_FLAG |
1243 IEEE80211_PREQ_USN_FLAG;
1244 mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08001245 ++ifmsh->sn, target_flags,
1246 (u8 *) broadcast_addr, 0, broadcast_addr,
1247 0, ifmsh->mshcfg.element_ttl, interval,
1248 0, ifmsh->preq_id++, sdata);
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001249 break;
1250 default:
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001251 mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +08001252 return;
1253 }
Rui Pauloe304bfd2009-11-09 23:46:56 +00001254}