mesh_hwmp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 2009 open80211s Ltd.
00003  * Author:     Luis Carlos Cobo <luisca@cozybit.com>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  */
00009 
00010 #include <linux/slab.h>
00011 #include "wme.h"
00012 #include "mesh.h"
00013 
00014 #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
00015 #define mhwmp_dbg(fmt, args...) \
00016         printk(KERN_DEBUG "Mesh HWMP (%s): " fmt "\n", sdata->name, ##args)
00017 #else
00018 #define mhwmp_dbg(fmt, args...)   do { (void)(0); } while (0)
00019 #endif
00020 
00021 #define TEST_FRAME_LEN  8192
00022 #define MAX_METRIC      0xffffffff
00023 #define ARITH_SHIFT     8
00024 
00025 /* Number of frames buffered per destination for unresolved destinations */
00026 #define MESH_FRAME_QUEUE_LEN    10
00027 #define MAX_PREQ_QUEUE_LEN      64
00028 
00029 /* Destination only */
00030 #define MP_F_DO 0x1
00031 /* Reply and forward */
00032 #define MP_F_RF 0x2
00033 /* Unknown Sequence Number */
00034 #define MP_F_USN    0x01
00035 /* Reason code Present */
00036 #define MP_F_RCODE  0x02
00037 
00038 static void mesh_queue_preq(struct mesh_path *, u8);
00039 
00040 static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
00041 {
00042         if (ae)
00043                 offset += 6;
00044         return get_unaligned_le32(preq_elem + offset);
00045 }
00046 
00047 static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
00048 {
00049         if (ae)
00050                 offset += 6;
00051         return get_unaligned_le16(preq_elem + offset);
00052 }
00053 
00054 /* HWMP IE processing macros */
00055 #define AE_F                    (1<<6)
00056 #define AE_F_SET(x)             (*x & AE_F)
00057 #define PREQ_IE_FLAGS(x)        (*(x))
00058 #define PREQ_IE_HOPCOUNT(x)     (*(x + 1))
00059 #define PREQ_IE_TTL(x)          (*(x + 2))
00060 #define PREQ_IE_PREQ_ID(x)      u32_field_get(x, 3, 0)
00061 #define PREQ_IE_ORIG_ADDR(x)    (x + 7)
00062 #define PREQ_IE_ORIG_SN(x)      u32_field_get(x, 13, 0)
00063 #define PREQ_IE_LIFETIME(x)     u32_field_get(x, 17, AE_F_SET(x))
00064 #define PREQ_IE_METRIC(x)       u32_field_get(x, 21, AE_F_SET(x))
00065 #define PREQ_IE_TARGET_F(x)     (*(AE_F_SET(x) ? x + 32 : x + 26))
00066 #define PREQ_IE_TARGET_ADDR(x)  (AE_F_SET(x) ? x + 33 : x + 27)
00067 #define PREQ_IE_TARGET_SN(x)    u32_field_get(x, 33, AE_F_SET(x))
00068 
00069 
00070 #define PREP_IE_FLAGS(x)        PREQ_IE_FLAGS(x)
00071 #define PREP_IE_HOPCOUNT(x)     PREQ_IE_HOPCOUNT(x)
00072 #define PREP_IE_TTL(x)          PREQ_IE_TTL(x)
00073 #define PREP_IE_ORIG_ADDR(x)    (AE_F_SET(x) ? x + 27 : x + 21)
00074 #define PREP_IE_ORIG_SN(x)      u32_field_get(x, 27, AE_F_SET(x))
00075 #define PREP_IE_LIFETIME(x)     u32_field_get(x, 13, AE_F_SET(x))
00076 #define PREP_IE_METRIC(x)       u32_field_get(x, 17, AE_F_SET(x))
00077 #define PREP_IE_TARGET_ADDR(x)  (x + 3)
00078 #define PREP_IE_TARGET_SN(x)    u32_field_get(x, 9, 0)
00079 
00080 #define PERR_IE_TTL(x)          (*(x))
00081 #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
00082 #define PERR_IE_TARGET_ADDR(x)  (x + 3)
00083 #define PERR_IE_TARGET_SN(x)    u32_field_get(x, 9, 0)
00084 #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
00085 
00086 #define MSEC_TO_TU(x) (x*1000/1024)
00087 #define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
00088 #define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
00089 
00090 #define net_traversal_jiffies(s) \
00091         msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
00092 #define default_lifetime(s) \
00093         MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
00094 #define min_preq_int_jiff(s) \
00095         (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
00096 #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
00097 #define disc_timeout_jiff(s) \
00098         msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
00099 
00100 enum mpath_frame_type {
00101         MPATH_PREQ = 0,
00102         MPATH_PREP,
00103         MPATH_PERR,
00104         MPATH_RANN
00105 };
00106 
00107 static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
00108 
00109 static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
00110                 u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
00111                 __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
00112                 __le32 lifetime, __le32 metric, __le32 preq_id,
00113                 struct ieee80211_sub_if_data *sdata)
00114 {
00115         struct ieee80211_local *local = sdata->local;
00116         struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
00117         struct ieee80211_mgmt *mgmt;
00118         u8 *pos;
00119         int ie_len;
00120 
00121         if (!skb)
00122                 return -1;
00123         skb_reserve(skb, local->hw.extra_tx_headroom);
00124         /* 25 is the size of the common mgmt part (24) plus the size of the
00125          * common action part (1)
00126          */
00127         mgmt = (struct ieee80211_mgmt *)
00128                 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
00129         memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
00130         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
00131                                           IEEE80211_STYPE_ACTION);
00132 
00133         memcpy(mgmt->da, da, ETH_ALEN);
00134         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
00135         /* BSSID == SA */
00136         memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
00137         mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
00138         mgmt->u.action.u.mesh_action.action_code =
00139                                         WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
00140 
00141         switch (action) {
00142         case MPATH_PREQ:
00143                 mhwmp_dbg("sending PREQ to %pM", target);
00144                 ie_len = 37;
00145                 pos = skb_put(skb, 2 + ie_len);
00146                 *pos++ = WLAN_EID_PREQ;
00147                 break;
00148         case MPATH_PREP:
00149                 mhwmp_dbg("sending PREP to %pM", target);
00150                 ie_len = 31;
00151                 pos = skb_put(skb, 2 + ie_len);
00152                 *pos++ = WLAN_EID_PREP;
00153                 break;
00154         case MPATH_RANN:
00155                 mhwmp_dbg("sending RANN from %pM", orig_addr);
00156                 ie_len = sizeof(struct ieee80211_rann_ie);
00157                 pos = skb_put(skb, 2 + ie_len);
00158                 *pos++ = WLAN_EID_RANN;
00159                 break;
00160         default:
00161                 kfree_skb(skb);
00162                 return -ENOTSUPP;
00163                 break;
00164         }
00165         *pos++ = ie_len;
00166         *pos++ = flags;
00167         *pos++ = hop_count;
00168         *pos++ = ttl;
00169         if (action == MPATH_PREP) {
00170                 memcpy(pos, target, ETH_ALEN);
00171                 pos += ETH_ALEN;
00172                 memcpy(pos, &target_sn, 4);
00173                 pos += 4;
00174         } else {
00175                 if (action == MPATH_PREQ) {
00176                         memcpy(pos, &preq_id, 4);
00177                         pos += 4;
00178                 }
00179                 memcpy(pos, orig_addr, ETH_ALEN);
00180                 pos += ETH_ALEN;
00181                 memcpy(pos, &orig_sn, 4);
00182                 pos += 4;
00183         }
00184         memcpy(pos, &lifetime, 4);      /* interval for RANN */
00185         pos += 4;
00186         memcpy(pos, &metric, 4);
00187         pos += 4;
00188         if (action == MPATH_PREQ) {
00189                 *pos++ = 1; /* destination count */
00190                 *pos++ = target_flags;
00191                 memcpy(pos, target, ETH_ALEN);
00192                 pos += ETH_ALEN;
00193                 memcpy(pos, &target_sn, 4);
00194                 pos += 4;
00195         } else if (action == MPATH_PREP) {
00196                 memcpy(pos, orig_addr, ETH_ALEN);
00197                 pos += ETH_ALEN;
00198                 memcpy(pos, &orig_sn, 4);
00199                 pos += 4;
00200         }
00201 
00202         ieee80211_tx_skb(sdata, skb);
00203         return 0;
00204 }
00205 
00206 
00207 /*  Headroom is not adjusted.  Caller should ensure that skb has sufficient
00208  *  headroom in case the frame is encrypted. */
00209 static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
00210                 struct sk_buff *skb)
00211 {
00212         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
00213 
00214         skb_set_mac_header(skb, 0);
00215         skb_set_network_header(skb, 0);
00216         skb_set_transport_header(skb, 0);
00217 
00218         /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
00219         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
00220         skb->priority = 7;
00221 
00222         info->control.vif = &sdata->vif;
00223         ieee80211_set_qos_hdr(sdata, skb);
00224 }
00225 
00238 int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
00239                        __le16 target_rcode, const u8 *ra,
00240                        struct ieee80211_sub_if_data *sdata)
00241 {
00242         struct ieee80211_local *local = sdata->local;
00243         struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
00244         struct ieee80211_mgmt *mgmt;
00245         u8 *pos;
00246         int ie_len;
00247 
00248         if (!skb)
00249                 return -1;
00250         skb_reserve(skb, local->tx_headroom + local->hw.extra_tx_headroom);
00251         /* 25 is the size of the common mgmt part (24) plus the size of the
00252          * common action part (1)
00253          */
00254         mgmt = (struct ieee80211_mgmt *)
00255                 skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
00256         memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
00257         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
00258                                           IEEE80211_STYPE_ACTION);
00259 
00260         memcpy(mgmt->da, ra, ETH_ALEN);
00261         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
00262         /* BSSID == SA */
00263         memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
00264         mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
00265         mgmt->u.action.u.mesh_action.action_code =
00266                                         WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
00267         ie_len = 15;
00268         pos = skb_put(skb, 2 + ie_len);
00269         *pos++ = WLAN_EID_PERR;
00270         *pos++ = ie_len;
00271         /* ttl */
00272         *pos++ = ttl;
00273         /* number of destinations */
00274         *pos++ = 1;
00275         /*
00276          * flags bit, bit 1 is unset if we know the sequence number and
00277          * bit 2 is set if we have a reason code
00278          */
00279         *pos = 0;
00280         if (!target_sn)
00281                 *pos |= MP_F_USN;
00282         if (target_rcode)
00283                 *pos |= MP_F_RCODE;
00284         pos++;
00285         memcpy(pos, target, ETH_ALEN);
00286         pos += ETH_ALEN;
00287         memcpy(pos, &target_sn, 4);
00288         pos += 4;
00289         memcpy(pos, &target_rcode, 2);
00290 
00291         /* see note in function header */
00292         prepare_frame_for_deferred_tx(sdata, skb);
00293         ieee80211_add_pending_skb(local, skb);
00294         return 0;
00295 }
00296 
00297 void ieee80211s_update_metric(struct ieee80211_local *local,
00298                 struct sta_info *stainfo, struct sk_buff *skb)
00299 {
00300         struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
00301         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00302         int failed;
00303 
00304         if (!ieee80211_is_data(hdr->frame_control))
00305                 return;
00306 
00307         failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
00308 
00309         /* moving average, scaled to 100 */
00310         stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
00311         if (stainfo->fail_avg > 95)
00312                 mesh_plink_broken(stainfo);
00313 }
00314 
00315 static u32 airtime_link_metric_get(struct ieee80211_local *local,
00316                                    struct sta_info *sta)
00317 {
00318         struct ieee80211_supported_band *sband;
00319         /* This should be adjusted for each device */
00320         int device_constant = 1 << ARITH_SHIFT;
00321         int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
00322         int s_unit = 1 << ARITH_SHIFT;
00323         int rate, err;
00324         u32 tx_time, estimated_retx;
00325         u64 result;
00326 
00327         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
00328 
00329         if (sta->fail_avg >= 100)
00330                 return MAX_METRIC;
00331 
00332         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
00333                 return MAX_METRIC;
00334 
00335         err = (sta->fail_avg << ARITH_SHIFT) / 100;
00336 
00337         /* bitrate is in units of 100 Kbps, while we need rate in units of
00338          * 1Mbps. This will be corrected on tx_time computation.
00339          */
00340         rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
00341         tx_time = (device_constant + 10 * test_frame_len / rate);
00342         estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
00343         result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
00344         return (u32)result;
00345 }
00346 
00363 static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
00364                             struct ieee80211_mgmt *mgmt,
00365                             u8 *hwmp_ie, enum mpath_frame_type action)
00366 {
00367         struct ieee80211_local *local = sdata->local;
00368         struct mesh_path *mpath;
00369         struct sta_info *sta;
00370         bool fresh_info;
00371         u8 *orig_addr, *ta;
00372         u32 orig_sn, orig_metric;
00373         unsigned long orig_lifetime, exp_time;
00374         u32 last_hop_metric, new_metric;
00375         bool process = true;
00376 
00377         rcu_read_lock();
00378         sta = sta_info_get(sdata, mgmt->sa);
00379         if (!sta) {
00380                 rcu_read_unlock();
00381                 return 0;
00382         }
00383 
00384         last_hop_metric = airtime_link_metric_get(local, sta);
00385         /* Update and check originator routing info */
00386         fresh_info = true;
00387 
00388         switch (action) {
00389         case MPATH_PREQ:
00390                 orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
00391                 orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
00392                 orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
00393                 orig_metric = PREQ_IE_METRIC(hwmp_ie);
00394                 break;
00395         case MPATH_PREP:
00396                 /* Originator here refers to the MP that was the destination in
00397                  * the Path Request. The draft refers to that MP as the
00398                  * destination address, even though usually it is the origin of
00399                  * the PREP frame. We divert from the nomenclature in the draft
00400                  * so that we can easily use a single function to gather path
00401                  * information from both PREQ and PREP frames.
00402                  */
00403                 orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
00404                 orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
00405                 orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
00406                 orig_metric = PREP_IE_METRIC(hwmp_ie);
00407                 break;
00408         default:
00409                 rcu_read_unlock();
00410                 return 0;
00411         }
00412         new_metric = orig_metric + last_hop_metric;
00413         if (new_metric < orig_metric)
00414                 new_metric = MAX_METRIC;
00415         exp_time = TU_TO_EXP_TIME(orig_lifetime);
00416 
00417         if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
00418                 /* This MP is the originator, we are not interested in this
00419                  * frame, except for updating transmitter's path info.
00420                  */
00421                 process = false;
00422                 fresh_info = false;
00423         } else {
00424                 mpath = mesh_path_lookup(orig_addr, sdata);
00425                 if (mpath) {
00426                         spin_lock_bh(&mpath->state_lock);
00427                         if (mpath->flags & MESH_PATH_FIXED)
00428                                 fresh_info = false;
00429                         else if ((mpath->flags & MESH_PATH_ACTIVE) &&
00430                             (mpath->flags & MESH_PATH_SN_VALID)) {
00431                                 if (SN_GT(mpath->sn, orig_sn) ||
00432                                     (mpath->sn == orig_sn &&
00433                                      new_metric >= mpath->metric)) {
00434                                         process = false;
00435                                         fresh_info = false;
00436                                 }
00437                         }
00438                 } else {
00439                         mesh_path_add(orig_addr, sdata);
00440                         mpath = mesh_path_lookup(orig_addr, sdata);
00441                         if (!mpath) {
00442                                 rcu_read_unlock();
00443                                 return 0;
00444                         }
00445                         spin_lock_bh(&mpath->state_lock);
00446                 }
00447 
00448                 if (fresh_info) {
00449                         mesh_path_assign_nexthop(mpath, sta);
00450                         mpath->flags |= MESH_PATH_SN_VALID;
00451                         mpath->metric = new_metric;
00452                         mpath->sn = orig_sn;
00453                         mpath->exp_time = time_after(mpath->exp_time, exp_time)
00454                                           ?  mpath->exp_time : exp_time;
00455                         mesh_path_activate(mpath);
00456                         spin_unlock_bh(&mpath->state_lock);
00457                         mesh_path_tx_pending(mpath);
00458                         /* draft says preq_id should be saved to, but there does
00459                          * not seem to be any use for it, skipping by now
00460                          */
00461                 } else
00462                         spin_unlock_bh(&mpath->state_lock);
00463         }
00464 
00465         /* Update and check transmitter routing info */
00466         ta = mgmt->sa;
00467         if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
00468                 fresh_info = false;
00469         else {
00470                 fresh_info = true;
00471 
00472                 mpath = mesh_path_lookup(ta, sdata);
00473                 if (mpath) {
00474                         spin_lock_bh(&mpath->state_lock);
00475                         if ((mpath->flags & MESH_PATH_FIXED) ||
00476                                 ((mpath->flags & MESH_PATH_ACTIVE) &&
00477                                         (last_hop_metric > mpath->metric)))
00478                                 fresh_info = false;
00479                 } else {
00480                         mesh_path_add(ta, sdata);
00481                         mpath = mesh_path_lookup(ta, sdata);
00482                         if (!mpath) {
00483                                 rcu_read_unlock();
00484                                 return 0;
00485                         }
00486                         spin_lock_bh(&mpath->state_lock);
00487                 }
00488 
00489                 if (fresh_info) {
00490                         mesh_path_assign_nexthop(mpath, sta);
00491                         mpath->metric = last_hop_metric;
00492                         mpath->exp_time = time_after(mpath->exp_time, exp_time)
00493                                           ?  mpath->exp_time : exp_time;
00494                         mesh_path_activate(mpath);
00495                         spin_unlock_bh(&mpath->state_lock);
00496                         mesh_path_tx_pending(mpath);
00497                 } else
00498                         spin_unlock_bh(&mpath->state_lock);
00499         }
00500 
00501         rcu_read_unlock();
00502 
00503         return process ? new_metric : 0;
00504 }
00505 
00506 static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
00507                                     struct ieee80211_mgmt *mgmt,
00508                                     u8 *preq_elem, u32 metric)
00509 {
00510         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
00511         struct mesh_path *mpath;
00512         u8 *target_addr, *orig_addr;
00513         u8 target_flags, ttl;
00514         u32 orig_sn, target_sn, lifetime;
00515         bool reply = false;
00516         bool forward = true;
00517 
00518         /* Update target SN, if present */
00519         target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
00520         orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
00521         target_sn = PREQ_IE_TARGET_SN(preq_elem);
00522         orig_sn = PREQ_IE_ORIG_SN(preq_elem);
00523         target_flags = PREQ_IE_TARGET_F(preq_elem);
00524 
00525         mhwmp_dbg("received PREQ from %pM", orig_addr);
00526 
00527         if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
00528                 mhwmp_dbg("PREQ is for us");
00529                 forward = false;
00530                 reply = true;
00531                 metric = 0;
00532                 if (time_after(jiffies, ifmsh->last_sn_update +
00533                                         net_traversal_jiffies(sdata)) ||
00534                     time_before(jiffies, ifmsh->last_sn_update)) {
00535                         target_sn = ++ifmsh->sn;
00536                         ifmsh->last_sn_update = jiffies;
00537                 }
00538         } else {
00539                 rcu_read_lock();
00540                 mpath = mesh_path_lookup(target_addr, sdata);
00541                 if (mpath) {
00542                         if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
00543                                         SN_LT(mpath->sn, target_sn)) {
00544                                 mpath->sn = target_sn;
00545                                 mpath->flags |= MESH_PATH_SN_VALID;
00546                         } else if ((!(target_flags & MP_F_DO)) &&
00547                                         (mpath->flags & MESH_PATH_ACTIVE)) {
00548                                 reply = true;
00549                                 metric = mpath->metric;
00550                                 target_sn = mpath->sn;
00551                                 if (target_flags & MP_F_RF)
00552                                         target_flags |= MP_F_DO;
00553                                 else
00554                                         forward = false;
00555                         }
00556                 }
00557                 rcu_read_unlock();
00558         }
00559 
00560         if (reply) {
00561                 lifetime = PREQ_IE_LIFETIME(preq_elem);
00562                 ttl = ifmsh->mshcfg.element_ttl;
00563                 if (ttl != 0) {
00564                         mhwmp_dbg("replying to the PREQ");
00565                         mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
00566                                 cpu_to_le32(target_sn), 0, orig_addr,
00567                                 cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
00568                                 cpu_to_le32(lifetime), cpu_to_le32(metric),
00569                                 0, sdata);
00570                 } else
00571                         ifmsh->mshstats.dropped_frames_ttl++;
00572         }
00573 
00574         if (forward) {
00575                 u32 preq_id;
00576                 u8 hopcount, flags;
00577 
00578                 ttl = PREQ_IE_TTL(preq_elem);
00579                 lifetime = PREQ_IE_LIFETIME(preq_elem);
00580                 if (ttl <= 1) {
00581                         ifmsh->mshstats.dropped_frames_ttl++;
00582                         return;
00583                 }
00584                 mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
00585                 --ttl;
00586                 flags = PREQ_IE_FLAGS(preq_elem);
00587                 preq_id = PREQ_IE_PREQ_ID(preq_elem);
00588                 hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
00589                 mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
00590                                 cpu_to_le32(orig_sn), target_flags, target_addr,
00591                                 cpu_to_le32(target_sn), broadcast_addr,
00592                                 hopcount, ttl, cpu_to_le32(lifetime),
00593                                 cpu_to_le32(metric), cpu_to_le32(preq_id),
00594                                 sdata);
00595                 ifmsh->mshstats.fwded_mcast++;
00596                 ifmsh->mshstats.fwded_frames++;
00597         }
00598 }
00599 
00600 
00601 static inline struct sta_info *
00602 next_hop_deref_protected(struct mesh_path *mpath)
00603 {
00604         return rcu_dereference_protected(mpath->next_hop,
00605                                          lockdep_is_held(&mpath->state_lock));
00606 }
00607 
00608 
00609 static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
00610                                     struct ieee80211_mgmt *mgmt,
00611                                     u8 *prep_elem, u32 metric)
00612 {
00613         struct mesh_path *mpath;
00614         u8 *target_addr, *orig_addr;
00615         u8 ttl, hopcount, flags;
00616         u8 next_hop[ETH_ALEN];
00617         u32 target_sn, orig_sn, lifetime;
00618 
00619         mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
00620 
00621         /* Note that we divert from the draft nomenclature and denominate
00622          * destination to what the draft refers to as origininator. So in this
00623          * function destnation refers to the final destination of the PREP,
00624          * which corresponds with the originator of the PREQ which this PREP
00625          * replies
00626          */
00627         target_addr = PREP_IE_TARGET_ADDR(prep_elem);
00628         if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
00629                 /* destination, no forwarding required */
00630                 return;
00631 
00632         ttl = PREP_IE_TTL(prep_elem);
00633         if (ttl <= 1) {
00634                 sdata->u.mesh.mshstats.dropped_frames_ttl++;
00635                 return;
00636         }
00637 
00638         rcu_read_lock();
00639         mpath = mesh_path_lookup(target_addr, sdata);
00640         if (mpath)
00641                 spin_lock_bh(&mpath->state_lock);
00642         else
00643                 goto fail;
00644         if (!(mpath->flags & MESH_PATH_ACTIVE)) {
00645                 spin_unlock_bh(&mpath->state_lock);
00646                 goto fail;
00647         }
00648         memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
00649         spin_unlock_bh(&mpath->state_lock);
00650         --ttl;
00651         flags = PREP_IE_FLAGS(prep_elem);
00652         lifetime = PREP_IE_LIFETIME(prep_elem);
00653         hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
00654         orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
00655         target_sn = PREP_IE_TARGET_SN(prep_elem);
00656         orig_sn = PREP_IE_ORIG_SN(prep_elem);
00657 
00658         mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
00659                 cpu_to_le32(orig_sn), 0, target_addr,
00660                 cpu_to_le32(target_sn), next_hop, hopcount,
00661                 ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
00662                 0, sdata);
00663         rcu_read_unlock();
00664 
00665         sdata->u.mesh.mshstats.fwded_unicast++;
00666         sdata->u.mesh.mshstats.fwded_frames++;
00667         return;
00668 
00669 fail:
00670         rcu_read_unlock();
00671         sdata->u.mesh.mshstats.dropped_frames_no_route++;
00672 }
00673 
00674 static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
00675                              struct ieee80211_mgmt *mgmt, u8 *perr_elem)
00676 {
00677         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
00678         struct mesh_path *mpath;
00679         u8 ttl;
00680         u8 *ta, *target_addr;
00681         u32 target_sn;
00682         u16 target_rcode;
00683 
00684         ta = mgmt->sa;
00685         ttl = PERR_IE_TTL(perr_elem);
00686         if (ttl <= 1) {
00687                 ifmsh->mshstats.dropped_frames_ttl++;
00688                 return;
00689         }
00690         ttl--;
00691         target_addr = PERR_IE_TARGET_ADDR(perr_elem);
00692         target_sn = PERR_IE_TARGET_SN(perr_elem);
00693         target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
00694 
00695         rcu_read_lock();
00696         mpath = mesh_path_lookup(target_addr, sdata);
00697         if (mpath) {
00698                 spin_lock_bh(&mpath->state_lock);
00699                 if (mpath->flags & MESH_PATH_ACTIVE &&
00700                     memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
00701                                                         ETH_ALEN) == 0 &&
00702                     (!(mpath->flags & MESH_PATH_SN_VALID) ||
00703                     SN_GT(target_sn, mpath->sn))) {
00704                         mpath->flags &= ~MESH_PATH_ACTIVE;
00705                         mpath->sn = target_sn;
00706                         spin_unlock_bh(&mpath->state_lock);
00707                         mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
00708                                            cpu_to_le16(target_rcode),
00709                                            broadcast_addr, sdata);
00710                 } else
00711                         spin_unlock_bh(&mpath->state_lock);
00712         }
00713         rcu_read_unlock();
00714 }
00715 
00716 static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
00717                                 struct ieee80211_mgmt *mgmt,
00718                                 struct ieee80211_rann_ie *rann)
00719 {
00720         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
00721         struct mesh_path *mpath;
00722         u8 ttl, flags, hopcount;
00723         u8 *orig_addr;
00724         u32 orig_sn, metric;
00725         u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
00726         bool root_is_gate;
00727 
00728         ttl = rann->rann_ttl;
00729         if (ttl <= 1) {
00730                 ifmsh->mshstats.dropped_frames_ttl++;
00731                 return;
00732         }
00733         ttl--;
00734         flags = rann->rann_flags;
00735         root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
00736         orig_addr = rann->rann_addr;
00737         orig_sn = rann->rann_seq;
00738         hopcount = rann->rann_hopcount;
00739         hopcount++;
00740         metric = rann->rann_metric;
00741 
00742         /*  Ignore our own RANNs */
00743         if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0)
00744                 return;
00745 
00746         mhwmp_dbg("received RANN from %pM (is_gate=%d)", orig_addr,
00747                         root_is_gate);
00748 
00749         rcu_read_lock();
00750         mpath = mesh_path_lookup(orig_addr, sdata);
00751         if (!mpath) {
00752                 mesh_path_add(orig_addr, sdata);
00753                 mpath = mesh_path_lookup(orig_addr, sdata);
00754                 if (!mpath) {
00755                         rcu_read_unlock();
00756                         sdata->u.mesh.mshstats.dropped_frames_no_route++;
00757                         return;
00758                 }
00759         }
00760 
00761         if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
00762              time_after(jiffies, mpath->exp_time - 1*HZ)) &&
00763              !(mpath->flags & MESH_PATH_FIXED)) {
00764                 mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
00765                                                                orig_addr);
00766                 mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
00767         }
00768 
00769         if (mpath->sn < orig_sn) {
00770                 mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
00771                                        cpu_to_le32(orig_sn),
00772                                        0, NULL, 0, broadcast_addr,
00773                                        hopcount, ttl, cpu_to_le32(interval),
00774                                        cpu_to_le32(metric + mpath->metric),
00775                                        0, sdata);
00776                 mpath->sn = orig_sn;
00777         }
00778         if (root_is_gate)
00779                 mesh_path_add_gate(mpath);
00780 
00781         rcu_read_unlock();
00782 }
00783 
00784 
00785 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
00786                             struct ieee80211_mgmt *mgmt,
00787                             size_t len)
00788 {
00789         struct ieee802_11_elems elems;
00790         size_t baselen;
00791         u32 last_hop_metric;
00792         struct sta_info *sta;
00793 
00794         /* need action_code */
00795         if (len < IEEE80211_MIN_ACTION_SIZE + 1)
00796                 return;
00797 
00798         rcu_read_lock();
00799         sta = sta_info_get(sdata, mgmt->sa);
00800         if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
00801                 rcu_read_unlock();
00802                 return;
00803         }
00804         rcu_read_unlock();
00805 
00806         baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
00807         ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
00808                         len - baselen, &elems);
00809 
00810         if (elems.preq) {
00811                 if (elems.preq_len != 37)
00812                         /* Right now we support just 1 destination and no AE */
00813                         return;
00814                 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
00815                                                       MPATH_PREQ);
00816                 if (last_hop_metric)
00817                         hwmp_preq_frame_process(sdata, mgmt, elems.preq,
00818                                                 last_hop_metric);
00819         }
00820         if (elems.prep) {
00821                 if (elems.prep_len != 31)
00822                         /* Right now we support no AE */
00823                         return;
00824                 last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
00825                                                       MPATH_PREP);
00826                 if (last_hop_metric)
00827                         hwmp_prep_frame_process(sdata, mgmt, elems.prep,
00828                                                 last_hop_metric);
00829         }
00830         if (elems.perr) {
00831                 if (elems.perr_len != 15)
00832                         /* Right now we support only one destination per PERR */
00833                         return;
00834                 hwmp_perr_frame_process(sdata, mgmt, elems.perr);
00835         }
00836         if (elems.rann)
00837                 hwmp_rann_frame_process(sdata, mgmt, elems.rann);
00838 }
00839 
00849 static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
00850 {
00851         struct ieee80211_sub_if_data *sdata = mpath->sdata;
00852         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
00853         struct mesh_preq_queue *preq_node;
00854 
00855         preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
00856         if (!preq_node) {
00857                 mhwmp_dbg("could not allocate PREQ node");
00858                 return;
00859         }
00860 
00861         spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
00862         if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
00863                 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
00864                 kfree(preq_node);
00865                 if (printk_ratelimit())
00866                         mhwmp_dbg("PREQ node queue full");
00867                 return;
00868         }
00869 
00870         memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
00871         preq_node->flags = flags;
00872 
00873         list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
00874         ++ifmsh->preq_queue_len;
00875         spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
00876 
00877         if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
00878                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
00879 
00880         else if (time_before(jiffies, ifmsh->last_preq)) {
00881                 /* avoid long wait if did not send preqs for a long time
00882                  * and jiffies wrapped around
00883                  */
00884                 ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
00885                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
00886         } else
00887                 mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
00888                                                 min_preq_int_jiff(sdata));
00889 }
00890 
00896 void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
00897 {
00898         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
00899         struct mesh_preq_queue *preq_node;
00900         struct mesh_path *mpath;
00901         u8 ttl, target_flags;
00902         u32 lifetime;
00903 
00904         spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
00905         if (!ifmsh->preq_queue_len ||
00906                 time_before(jiffies, ifmsh->last_preq +
00907                                 min_preq_int_jiff(sdata))) {
00908                 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
00909                 return;
00910         }
00911 
00912         preq_node = list_first_entry(&ifmsh->preq_queue.list,
00913                         struct mesh_preq_queue, list);
00914         list_del(&preq_node->list);
00915         --ifmsh->preq_queue_len;
00916         spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
00917 
00918         rcu_read_lock();
00919         mpath = mesh_path_lookup(preq_node->dst, sdata);
00920         if (!mpath)
00921                 goto enddiscovery;
00922 
00923         spin_lock_bh(&mpath->state_lock);
00924         if (preq_node->flags & PREQ_Q_F_START) {
00925                 if (mpath->flags & MESH_PATH_RESOLVING) {
00926                         spin_unlock_bh(&mpath->state_lock);
00927                         goto enddiscovery;
00928                 } else {
00929                         mpath->flags &= ~MESH_PATH_RESOLVED;
00930                         mpath->flags |= MESH_PATH_RESOLVING;
00931                         mpath->discovery_retries = 0;
00932                         mpath->discovery_timeout = disc_timeout_jiff(sdata);
00933                 }
00934         } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
00935                         mpath->flags & MESH_PATH_RESOLVED) {
00936                 mpath->flags &= ~MESH_PATH_RESOLVING;
00937                 spin_unlock_bh(&mpath->state_lock);
00938                 goto enddiscovery;
00939         }
00940 
00941         ifmsh->last_preq = jiffies;
00942 
00943         if (time_after(jiffies, ifmsh->last_sn_update +
00944                                 net_traversal_jiffies(sdata)) ||
00945             time_before(jiffies, ifmsh->last_sn_update)) {
00946                 ++ifmsh->sn;
00947                 sdata->u.mesh.last_sn_update = jiffies;
00948         }
00949         lifetime = default_lifetime(sdata);
00950         ttl = sdata->u.mesh.mshcfg.element_ttl;
00951         if (ttl == 0) {
00952                 sdata->u.mesh.mshstats.dropped_frames_ttl++;
00953                 spin_unlock_bh(&mpath->state_lock);
00954                 goto enddiscovery;
00955         }
00956 
00957         if (preq_node->flags & PREQ_Q_F_REFRESH)
00958                 target_flags = MP_F_DO;
00959         else
00960                 target_flags = MP_F_RF;
00961 
00962         spin_unlock_bh(&mpath->state_lock);
00963         mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
00964                         cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
00965                         cpu_to_le32(mpath->sn), broadcast_addr, 0,
00966                         ttl, cpu_to_le32(lifetime), 0,
00967                         cpu_to_le32(ifmsh->preq_id++), sdata);
00968         mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
00969 
00970 enddiscovery:
00971         rcu_read_unlock();
00972         kfree(preq_node);
00973 }
00974 
00986 int mesh_nexthop_lookup(struct sk_buff *skb,
00987                         struct ieee80211_sub_if_data *sdata)
00988 {
00989         struct sk_buff *skb_to_free = NULL;
00990         struct mesh_path *mpath;
00991         struct sta_info *next_hop;
00992         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00993         u8 *target_addr = hdr->addr3;
00994         int err = 0;
00995 
00996         rcu_read_lock();
00997         mpath = mesh_path_lookup(target_addr, sdata);
00998 
00999         if (!mpath) {
01000                 mesh_path_add(target_addr, sdata);
01001                 mpath = mesh_path_lookup(target_addr, sdata);
01002                 if (!mpath) {
01003                         sdata->u.mesh.mshstats.dropped_frames_no_route++;
01004                         err = -ENOSPC;
01005                         goto endlookup;
01006                 }
01007         }
01008 
01009         if (mpath->flags & MESH_PATH_ACTIVE) {
01010                 if (time_after(jiffies,
01011                                mpath->exp_time -
01012                                msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
01013                     !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
01014                     !(mpath->flags & MESH_PATH_RESOLVING) &&
01015                     !(mpath->flags & MESH_PATH_FIXED)) {
01016                         mesh_queue_preq(mpath,
01017                                         PREQ_Q_F_START | PREQ_Q_F_REFRESH);
01018                 }
01019                 next_hop = rcu_dereference(mpath->next_hop);
01020                 if (next_hop)
01021                         memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
01022                 else
01023                         err = -ENOENT;
01024         } else {
01025                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
01026                 if (!(mpath->flags & MESH_PATH_RESOLVING)) {
01027                         /* Start discovery only if it is not running yet */
01028                         mesh_queue_preq(mpath, PREQ_Q_F_START);
01029                 }
01030 
01031                 if (skb_queue_len(&mpath->frame_queue) >=
01032                                 MESH_FRAME_QUEUE_LEN)
01033                         skb_to_free = skb_dequeue(&mpath->frame_queue);
01034 
01035                 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
01036                 skb_queue_tail(&mpath->frame_queue, skb);
01037                 if (skb_to_free)
01038                         mesh_path_discard_frame(skb_to_free, sdata);
01039                 err = -ENOENT;
01040         }
01041 
01042 endlookup:
01043         rcu_read_unlock();
01044         return err;
01045 }
01046 
01047 void mesh_path_timer(unsigned long data)
01048 {
01049         struct mesh_path *mpath = (void *) data;
01050         struct ieee80211_sub_if_data *sdata = mpath->sdata;
01051         int ret;
01052 
01053         if (sdata->local->quiescing)
01054                 return;
01055 
01056         spin_lock_bh(&mpath->state_lock);
01057         if (mpath->flags & MESH_PATH_RESOLVED ||
01058                         (!(mpath->flags & MESH_PATH_RESOLVING))) {
01059                 mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
01060                 spin_unlock_bh(&mpath->state_lock);
01061         } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
01062                 ++mpath->discovery_retries;
01063                 mpath->discovery_timeout *= 2;
01064                 spin_unlock_bh(&mpath->state_lock);
01065                 mesh_queue_preq(mpath, 0);
01066         } else {
01067                 mpath->flags = 0;
01068                 mpath->exp_time = jiffies;
01069                 spin_unlock_bh(&mpath->state_lock);
01070                 if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
01071                         ret = mesh_path_send_to_gates(mpath);
01072                         if (ret)
01073                                 mhwmp_dbg("no gate was reachable");
01074                 } else
01075                         mesh_path_flush_pending(mpath);
01076         }
01077 }
01078 
01079 void
01080 mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
01081 {
01082         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
01083         u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
01084         u8 flags;
01085 
01086         flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
01087                         ? RANN_FLAG_IS_GATE : 0;
01088         mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
01089                                cpu_to_le32(++ifmsh->sn),
01090                                0, NULL, 0, broadcast_addr,
01091                                0, sdata->u.mesh.mshcfg.element_ttl,
01092                                cpu_to_le32(interval), 0, 0, sdata);
01093 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:10