blob: 90c79948979c64d9fd14f8a3805cffb8bf0dab38 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2011-2015 B.A.T.M.A.N. contributors:
Simon Wunderlich23721382012-01-22 20:00:19 +01002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Simon Wunderlich23721382012-01-22 20:00:19 +010016 */
17
Simon Wunderlich23721382012-01-22 20:00:19 +010018#include "bridge_loop_avoidance.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/compiler.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include <linux/crc16.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/if_ether.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/skbuff.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/stddef.h>
44#include <linux/string.h>
45#include <linux/workqueue.h>
46#include <net/arp.h>
47
48#include "hard-interface.h"
49#include "hash.h"
50#include "originator.h"
51#include "packet.h"
52#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010053
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020054static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010055
Sven Eckelmann3b300de2012-05-12 18:33:53 +020056static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080057static void
58batadv_bla_send_announce(struct batadv_priv *bat_priv,
59 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010060
Sven Eckelmann62fe7102015-09-15 19:00:48 +020061/**
62 * batadv_choose_claim
63 *
64 * Return: the index of the claim
65 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020066static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010067{
Marek Lindner712bbfe42012-12-25 17:03:25 +080068 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020069 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010070
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010071 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
72 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010073
74 return hash % size;
75}
76
Sven Eckelmann62fe7102015-09-15 19:00:48 +020077/**
78 * batadv_choose_backbone_gw
79 *
80 * Return: the index of the backbone gateway
81 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020082static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010083{
Marek Lindner712bbfe42012-12-25 17:03:25 +080084 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020085 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010086
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010087 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
88 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010089
90 return hash % size;
91}
92
Simon Wunderlich23721382012-01-22 20:00:19 +010093/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020094static int batadv_compare_backbone_gw(const struct hlist_node *node,
95 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010096{
Marek Lindnerbae98772012-12-25 17:03:24 +080097 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010098 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +020099 const struct batadv_bla_backbone_gw *gw1 = data1;
100 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100101
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200102 if (!batadv_compare_eth(gw1->orig, gw2->orig))
103 return 0;
104
105 if (gw1->vid != gw2->vid)
106 return 0;
107
108 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100109}
110
111/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200112static int batadv_compare_claim(const struct hlist_node *node,
113 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100114{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800115 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100116 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200117 const struct batadv_bla_claim *cl1 = data1;
118 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100119
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200120 if (!batadv_compare_eth(cl1->addr, cl2->addr))
121 return 0;
122
123 if (cl1->vid != cl2->vid)
124 return 0;
125
126 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100127}
128
129/* free a backbone gw */
Marek Lindnerbae98772012-12-25 17:03:24 +0800130static void
131batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100132{
133 if (atomic_dec_and_test(&backbone_gw->refcount))
134 kfree_rcu(backbone_gw, rcu);
135}
136
137/* finally deinitialize the claim */
Sven Eckelmann63b39922016-01-14 15:28:19 +0100138static void batadv_claim_release(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100139{
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200140 batadv_backbone_gw_free_ref(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100141 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100142}
143
144/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800145static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100146{
147 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann63b39922016-01-14 15:28:19 +0100148 batadv_claim_release(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100149}
150
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100151/**
152 * batadv_claim_hash_find
153 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100154 * @data: search data (may be local/static data)
155 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200156 * looks for a claim in the hash
157 *
158 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100159 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800160static struct batadv_bla_claim
161*batadv_claim_hash_find(struct batadv_priv *bat_priv,
162 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100163{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200164 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100165 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800166 struct batadv_bla_claim *claim;
167 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100168 int index;
169
170 if (!hash)
171 return NULL;
172
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200173 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100174 head = &hash->table[index];
175
176 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800177 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200178 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100179 continue;
180
181 if (!atomic_inc_not_zero(&claim->refcount))
182 continue;
183
184 claim_tmp = claim;
185 break;
186 }
187 rcu_read_unlock();
188
189 return claim_tmp;
190}
191
Ben Hutchings2c530402012-07-10 10:55:09 +0000192/**
193 * batadv_backbone_hash_find - looks for a claim in the hash
194 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100195 * @addr: the address of the originator
196 * @vid: the VLAN ID
197 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200198 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100199 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800200static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200201batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
202 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100203{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200204 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100205 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800206 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
207 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100208 int index;
209
210 if (!hash)
211 return NULL;
212
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100213 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100214 search_entry.vid = vid;
215
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200216 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100217 head = &hash->table[index];
218
219 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800220 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200221 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
222 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100223 continue;
224
225 if (!atomic_inc_not_zero(&backbone_gw->refcount))
226 continue;
227
228 backbone_gw_tmp = backbone_gw;
229 break;
230 }
231 rcu_read_unlock();
232
233 return backbone_gw_tmp;
234}
235
236/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200237static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800238batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100239{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200240 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800241 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100242 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800243 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100244 int i;
245 spinlock_t *list_lock; /* protects write access to the hash lists */
246
Sven Eckelmann807736f2012-07-15 22:26:51 +0200247 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100248 if (!hash)
249 return;
250
251 for (i = 0; i < hash->size; i++) {
252 head = &hash->table[i];
253 list_lock = &hash->list_locks[i];
254
255 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800256 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100257 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100258 if (claim->backbone_gw != backbone_gw)
259 continue;
260
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200261 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800262 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100263 }
264 spin_unlock_bh(list_lock);
265 }
266
Antonio Quartulli3f687852014-11-02 11:29:56 +0100267 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200268 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200269 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200270 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100271}
272
Ben Hutchings2c530402012-07-10 10:55:09 +0000273/**
274 * batadv_bla_send_claim - sends a claim frame according to the provided info
275 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200276 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100277 * @vid: the VLAN ID
278 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100279 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200280static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200281 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100282{
283 struct sk_buff *skb;
284 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200285 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100286 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200287 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200288 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100289 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100290
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200291 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100292 if (!primary_if)
293 return;
294
Sven Eckelmann807736f2012-07-15 22:26:51 +0200295 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100296 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100297 local_claim_dest.type = claimtype;
298
299 soft_iface = primary_if->soft_iface;
300
301 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
302 /* IP DST: 0.0.0.0 */
303 zeroip,
304 primary_if->soft_iface,
305 /* IP SRC: 0.0.0.0 */
306 zeroip,
307 /* Ethernet DST: Broadcast */
308 NULL,
309 /* Ethernet SRC/HW SRC: originator mac */
310 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200311 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100312 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100313 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100314 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200315 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100316
317 if (!skb)
318 goto out;
319
320 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200321 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100322
323 /* now we pretend that the client would have sent this ... */
324 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200325 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100326 /* normal claim frame
327 * set Ethernet SRC to the clients mac
328 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100329 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200330 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200331 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
332 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100333 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200334 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100335 /* unclaim frame
336 * set HW SRC to the clients mac
337 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100338 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200339 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200340 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200341 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100342 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200343 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100344 /* announcement frame
345 * set HW SRC to the special mac containg the crc
346 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100347 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200348 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200349 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200350 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100351 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200352 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100353 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200354 * set HW SRC and header destination to the receiving backbone
355 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100356 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100357 ether_addr_copy(hw_src, mac);
358 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200359 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200360 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200361 ethhdr->h_source, ethhdr->h_dest,
362 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100363 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100364 }
365
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200366 if (vid & BATADV_VLAN_HAS_TAG)
367 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
368 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100369
370 skb_reset_mac_header(skb);
371 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200372 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
373 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
374 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100375 soft_iface->last_rx = jiffies;
376
377 netif_rx(skb);
378out:
379 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200380 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100381}
382
Ben Hutchings2c530402012-07-10 10:55:09 +0000383/**
384 * batadv_bla_get_backbone_gw
385 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100386 * @orig: the mac address of the originator
387 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200388 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100389 *
390 * searches for the backbone gw or creates a new one if it could not
391 * be found.
392 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800393static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200394batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200395 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100396{
Marek Lindnerbae98772012-12-25 17:03:24 +0800397 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200398 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100399 int hash_added;
400
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200401 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100402
403 if (entry)
404 return entry;
405
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200406 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200407 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200408 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100409
410 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
411 if (!entry)
412 return NULL;
413
414 entry->vid = vid;
415 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200416 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100417 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200418 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100419 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200420 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100421 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100422
423 /* one for the hash, one for returning */
424 atomic_set(&entry->refcount, 2);
425
Sven Eckelmann807736f2012-07-15 22:26:51 +0200426 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200427 batadv_compare_backbone_gw,
428 batadv_choose_backbone_gw, entry,
429 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100430
431 if (unlikely(hash_added != 0)) {
432 /* hash failed, free the structure */
433 kfree(entry);
434 return NULL;
435 }
436
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200437 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200438 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100439 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200440 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200441 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200442 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100443 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200444
Simon Wunderlichd807f272012-09-09 22:27:57 +0200445 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200446 batadv_bla_send_announce(bat_priv, entry);
447
Simon Wunderlichd807f272012-09-09 22:27:57 +0200448 /* this will be decreased in the worker thread */
449 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200450 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200451 atomic_inc(&bat_priv->bla.num_requests);
452 }
453
Simon Wunderlich23721382012-01-22 20:00:19 +0100454 return entry;
455}
456
457/* update or add the own backbone gw to make sure we announce
458 * where we receive other backbone gws
459 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200460static void
461batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
462 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200463 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100464{
Marek Lindnerbae98772012-12-25 17:03:24 +0800465 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100466
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200467 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
468 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200469 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100470 if (unlikely(!backbone_gw))
471 return;
472
473 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200474 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100475}
476
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100477/**
478 * batadv_bla_answer_request - answer a bla request by sending own claims
479 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200480 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100481 * @vid: the vid where the request came on
482 *
483 * Repeat all of our own claims, and finally send an ANNOUNCE frame
484 * to allow the requester another check if the CRC is correct now.
485 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200486static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
487 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200488 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100489{
Simon Wunderlich23721382012-01-22 20:00:19 +0100490 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200491 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800492 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800493 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100494 int i;
495
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200496 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200497 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100498
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200499 backbone_gw = batadv_backbone_hash_find(bat_priv,
500 primary_if->net_dev->dev_addr,
501 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100502 if (!backbone_gw)
503 return;
504
Sven Eckelmann807736f2012-07-15 22:26:51 +0200505 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100506 for (i = 0; i < hash->size; i++) {
507 head = &hash->table[i];
508
509 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800510 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100511 /* only own claims are interesting */
512 if (claim->backbone_gw != backbone_gw)
513 continue;
514
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200515 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200516 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100517 }
518 rcu_read_unlock();
519 }
520
521 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200522 batadv_bla_send_announce(bat_priv, backbone_gw);
523 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100524}
525
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100526/**
527 * batadv_bla_send_request - send a request to repeat claims
528 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100529 *
530 * When the crc is wrong, ask the backbone gateway for a full table update.
531 * After the request, it will repeat all of his own claims and finally
532 * send an announcement claim with which we can check again.
533 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800534static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100535{
536 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200537 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100538
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200539 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
540 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100541
542 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200543 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200544 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100545
546 /* no local broadcasts should be sent or received, for now. */
547 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200548 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100549 atomic_set(&backbone_gw->request_sent, 1);
550 }
551}
552
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100553/**
554 * batadv_bla_send_announce
555 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100556 * @backbone_gw: our backbone gateway which should be announced
557 *
558 * This function sends an announcement. It is called from multiple
559 * places.
560 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200561static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800562 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100563{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200564 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100565 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100566
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200567 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200568 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100569 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200570 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100571 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100572
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200573 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200574 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100575}
576
Ben Hutchings2c530402012-07-10 10:55:09 +0000577/**
578 * batadv_bla_add_claim - Adds a claim in the claim hash
579 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100580 * @mac: the mac address of the claim
581 * @vid: the VLAN ID of the frame
582 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100583 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200584static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200585 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800586 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100587{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800588 struct batadv_bla_claim *claim;
589 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100590 int hash_added;
591
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100592 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100593 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200594 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100595
596 /* create a new claim entry if it does not exist yet. */
597 if (!claim) {
598 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
599 if (!claim)
600 return;
601
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100602 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100603 claim->vid = vid;
604 claim->lasttime = jiffies;
605 claim->backbone_gw = backbone_gw;
606
607 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200608 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200609 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200610 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200611 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200612 batadv_compare_claim,
613 batadv_choose_claim, claim,
614 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100615
616 if (unlikely(hash_added != 0)) {
617 /* only local changes happened. */
618 kfree(claim);
619 return;
620 }
621 } else {
622 claim->lasttime = jiffies;
623 if (claim->backbone_gw == backbone_gw)
624 /* no need to register a new backbone */
625 goto claim_free_ref;
626
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200627 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200628 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200629 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100630
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200631 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200632 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200633 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200634 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100635 }
636 /* set (new) backbone gw */
637 atomic_inc(&backbone_gw->refcount);
638 claim->backbone_gw = backbone_gw;
639
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200640 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100641 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200642 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100643 backbone_gw->lasttime = jiffies;
644
645claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200646 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100647}
648
649/* Delete a claim from the claim hash which has the
650 * given mac address and vid.
651 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200653 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100654{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800655 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100656
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100657 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100658 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200659 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100660 if (!claim)
661 return;
662
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200663 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200664 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100665
Sven Eckelmann807736f2012-07-15 22:26:51 +0200666 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200667 batadv_choose_claim, claim);
668 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100669
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200670 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100671 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200672 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100673
674 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200675 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100676}
677
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200678/**
679 * batadv_handle_announce - check for ANNOUNCE frame
680 *
681 * Return: 1 if handled
682 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200683static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
684 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100685{
Marek Lindnerbae98772012-12-25 17:03:24 +0800686 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200687 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100688
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200689 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100690 return 0;
691
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200692 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
693 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100694
695 if (unlikely(!backbone_gw))
696 return 1;
697
Simon Wunderlich23721382012-01-22 20:00:19 +0100698 /* handle as ANNOUNCE frame */
699 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100700 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100701
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200702 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100703 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200704 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100705
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200706 spin_lock_bh(&backbone_gw->crc_lock);
707 backbone_crc = backbone_gw->crc;
708 spin_unlock_bh(&backbone_gw->crc_lock);
709
710 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200711 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100712 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200713 backbone_gw->orig,
714 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200715 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100716
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200717 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100718 } else {
719 /* if we have sent a request and the crc was OK,
720 * we can allow traffic again.
721 */
722 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200723 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100724 atomic_set(&backbone_gw->request_sent, 0);
725 }
726 }
727
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200728 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100729 return 1;
730}
731
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200732/**
733 * batadv_handle_request - check for REQUEST frame
734 *
735 * Return: 1 if handled
736 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200737static int batadv_handle_request(struct batadv_priv *bat_priv,
738 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200739 u8 *backbone_addr, struct ethhdr *ethhdr,
740 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100741{
742 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200743 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100744 return 0;
745
746 /* sanity check, this should not happen on a normal switch,
747 * we ignore it in this case.
748 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200749 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100750 return 1;
751
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200752 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200753 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200754 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100755
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200756 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100757 return 1;
758}
759
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200760/**
761 * batadv_handle_unclaim - check for UNCLAIM frame
762 *
763 * Return: 1 if handled
764 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200765static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
766 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200767 u8 *backbone_addr, u8 *claim_addr,
768 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100769{
Marek Lindnerbae98772012-12-25 17:03:24 +0800770 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100771
772 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200773 if (primary_if && batadv_compare_eth(backbone_addr,
774 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200775 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200776 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100777
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200778 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100779
780 if (!backbone_gw)
781 return 1;
782
783 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200784 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200785 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200786 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100787
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200788 batadv_bla_del_claim(bat_priv, claim_addr, vid);
789 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100790 return 1;
791}
792
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200793/**
794 * batadv_handle_claim - check for CLAIM frame
795 *
796 * Return: 1 if handled
797 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200798static int batadv_handle_claim(struct batadv_priv *bat_priv,
799 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200800 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200801 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100802{
Marek Lindnerbae98772012-12-25 17:03:24 +0800803 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100804
805 /* register the gateway if not yet available, and add the claim. */
806
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200807 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
808 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100809
810 if (unlikely(!backbone_gw))
811 return 1;
812
813 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200814 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200815 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200816 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200817 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100818
819 /* TODO: we could call something like tt_local_del() here. */
820
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200821 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100822 return 1;
823}
824
Ben Hutchings2c530402012-07-10 10:55:09 +0000825/**
826 * batadv_check_claim_group
827 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200828 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100829 * @hw_src: the Hardware source in the ARP Header
830 * @hw_dst: the Hardware destination in the ARP Header
831 * @ethhdr: pointer to the Ethernet header of the claim frame
832 *
833 * checks if it is a claim packet and if its on the same group.
834 * This function also applies the group ID of the sender
835 * if it is in the same mesh.
836 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200837 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100838 * 2 - if it is a claim packet and on the same group
839 * 1 - if is a claim packet from another group
840 * 0 - if it is not a claim packet
841 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200842static int batadv_check_claim_group(struct batadv_priv *bat_priv,
843 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200844 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200845 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100846{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200847 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200848 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200849 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100850
Sven Eckelmann96412692012-06-05 22:31:30 +0200851 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200852 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100853
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100854 /* if announcement packet, use the source,
855 * otherwise assume it is in the hw_src
856 */
857 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200858 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100859 backbone_addr = hw_src;
860 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200861 case BATADV_CLAIM_TYPE_REQUEST:
862 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200863 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100864 backbone_addr = ethhdr->h_source;
865 break;
866 default:
867 return 0;
868 }
869
870 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200871 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100872 return 0;
873
874 /* if its already the same group, it is fine. */
875 if (bla_dst->group == bla_dst_own->group)
876 return 2;
877
878 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200879 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100880
881 /* dont accept claims from gateways which are not in
882 * the same mesh or group.
883 */
884 if (!orig_node)
885 return 1;
886
887 /* if our mesh friends mac is bigger, use it for ourselves. */
888 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200889 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100890 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200891 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100892 bla_dst_own->group = bla_dst->group;
893 }
894
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200895 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100896
897 return 2;
898}
899
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100900/**
901 * batadv_bla_process_claim
902 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200903 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100904 * @skb: the frame to be checked
905 *
906 * Check if this is a claim frame, and process it accordingly.
907 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200908 * Return: 1 if it was a claim frame, otherwise return 0 to
Simon Wunderlich23721382012-01-22 20:00:19 +0100909 * tell the callee that it can use the frame on its own.
910 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200911static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
912 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200913 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100914{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200915 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200916 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200917 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200918 struct ethhdr *ethhdr;
919 struct arphdr *arphdr;
920 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200921 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200922 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100923 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100924 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100925
Antonio Quartullic018ad32013-06-04 12:11:39 +0200926 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200927 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100928
Antonio Quartullic018ad32013-06-04 12:11:39 +0200929 proto = ethhdr->h_proto;
930 headlen = ETH_HLEN;
931 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200932 /* Traverse the VLAN/Ethertypes.
933 *
934 * At this point it is known that the first protocol is a VLAN
935 * header, so start checking at the encapsulated protocol.
936 *
937 * The depth of the VLAN headers is recorded to drop BLA claim
938 * frames encapsulated into multiple VLAN headers (QinQ).
939 */
940 do {
941 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
942 &vhdr_buf);
943 if (!vhdr)
944 return 0;
945
946 proto = vhdr->h_vlan_encapsulated_proto;
947 headlen += VLAN_HLEN;
948 vlan_depth++;
949 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100950 }
951
Antonio Quartulli293e9332013-05-19 12:55:16 +0200952 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +0100953 return 0; /* not a claim frame */
954
955 /* this must be a ARP frame. check if it is a claim. */
956
957 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
958 return 0;
959
960 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200961 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200962 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +0100963
964 /* Check whether the ARP frame carries a valid
965 * IP information
966 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100967 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
968 return 0;
969 if (arphdr->ar_pro != htons(ETH_P_IP))
970 return 0;
971 if (arphdr->ar_hln != ETH_ALEN)
972 return 0;
973 if (arphdr->ar_pln != 4)
974 return 0;
975
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200976 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100977 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200978 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200979 bla_dst_own = &bat_priv->bla.claim_dest;
980
981 /* check if it is a claim frame in general */
982 if (memcmp(bla_dst->magic, bla_dst_own->magic,
983 sizeof(bla_dst->magic)) != 0)
984 return 0;
985
986 /* check if there is a claim frame encapsulated deeper in (QinQ) and
987 * drop that, as this is not supported by BLA but should also not be
988 * sent via the mesh.
989 */
990 if (vlan_depth > 1)
991 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100992
993 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200994 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
995 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100996 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200997 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200998 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200999 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1000 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001001
1002 if (ret < 2)
1003 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001004
1005 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001006 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001007
1008 /* check for the different types of claim frames ... */
1009 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001010 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001011 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1012 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001013 return 1;
1014 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001015 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001016 if (batadv_handle_unclaim(bat_priv, primary_if,
1017 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001018 return 1;
1019 break;
1020
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001021 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001022 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1023 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001024 return 1;
1025 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001026 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001027 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1028 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001029 return 1;
1030 break;
1031 }
1032
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001033 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001034 "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001035 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001036 return 1;
1037}
1038
1039/* Check when we last heard from other nodes, and remove them in case of
1040 * a time out, or clean all backbone gws if now is set.
1041 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001043{
Marek Lindnerbae98772012-12-25 17:03:24 +08001044 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001045 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001046 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001047 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001048 spinlock_t *list_lock; /* protects write access to the hash lists */
1049 int i;
1050
Sven Eckelmann807736f2012-07-15 22:26:51 +02001051 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001052 if (!hash)
1053 return;
1054
1055 for (i = 0; i < hash->size; i++) {
1056 head = &hash->table[i];
1057 list_lock = &hash->list_locks[i];
1058
1059 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001060 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001061 head, hash_entry) {
1062 if (now)
1063 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001064 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001065 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001066 continue;
1067
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001068 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001069 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1070 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001071
1072purge_now:
1073 /* don't wait for the pending request anymore */
1074 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001075 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001076
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001077 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001078
Sasha Levinb67bfe02013-02-27 17:06:00 -08001079 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001080 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001081 }
1082 spin_unlock_bh(list_lock);
1083 }
1084}
1085
Ben Hutchings2c530402012-07-10 10:55:09 +00001086/**
1087 * batadv_bla_purge_claims
1088 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001089 * @primary_if: the selected primary interface, may be NULL if now is set
1090 * @now: whether the whole hash shall be wiped now
1091 *
1092 * Check when we heard last time from our own claims, and remove them in case of
1093 * a time out, or clean all claims if now is set
1094 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001095static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1096 struct batadv_hard_iface *primary_if,
1097 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001098{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001099 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001100 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001101 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001102 int i;
1103
Sven Eckelmann807736f2012-07-15 22:26:51 +02001104 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001105 if (!hash)
1106 return;
1107
1108 for (i = 0; i < hash->size; i++) {
1109 head = &hash->table[i];
1110
1111 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001112 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001113 if (now)
1114 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001115 if (!batadv_compare_eth(claim->backbone_gw->orig,
1116 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001117 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001118 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001119 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001120 continue;
1121
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001122 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001123 "bla_purge_claims(): %pM, vid %d, time out\n",
1124 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001125
1126purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001127 batadv_handle_unclaim(bat_priv, primary_if,
1128 claim->backbone_gw->orig,
1129 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001130 }
1131 rcu_read_unlock();
1132 }
1133}
1134
Ben Hutchings2c530402012-07-10 10:55:09 +00001135/**
1136 * batadv_bla_update_orig_address
1137 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001138 * @primary_if: the new selected primary_if
1139 * @oldif: the old primary interface, may be NULL
1140 *
1141 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001142 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1144 struct batadv_hard_iface *primary_if,
1145 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001146{
Marek Lindnerbae98772012-12-25 17:03:24 +08001147 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001148 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001149 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001150 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001151 int i;
1152
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001153 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001154 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1155 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001156
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001157 /* purge everything when bridge loop avoidance is turned off */
1158 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1159 oldif = NULL;
1160
Simon Wunderlich23721382012-01-22 20:00:19 +01001161 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001162 batadv_bla_purge_claims(bat_priv, NULL, 1);
1163 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001164 return;
1165 }
1166
Sven Eckelmann807736f2012-07-15 22:26:51 +02001167 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001168 if (!hash)
1169 return;
1170
1171 for (i = 0; i < hash->size; i++) {
1172 head = &hash->table[i];
1173
1174 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001175 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001176 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001177 if (!batadv_compare_eth(backbone_gw->orig,
1178 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001179 continue;
1180
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001181 ether_addr_copy(backbone_gw->orig,
1182 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001183 /* send an announce frame so others will ask for our
1184 * claims and update their tables.
1185 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001186 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001187 }
1188 rcu_read_unlock();
1189 }
1190}
1191
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001192/**
1193 * batadv_bla_status_update - purge bla interfaces if necessary
1194 * @net_dev: the soft interface net device
1195 */
1196void batadv_bla_status_update(struct net_device *net_dev)
1197{
1198 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1199 struct batadv_hard_iface *primary_if;
1200
1201 primary_if = batadv_primary_if_get_selected(bat_priv);
1202 if (!primary_if)
1203 return;
1204
1205 /* this function already purges everything when bla is disabled,
1206 * so just call that one.
1207 */
1208 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1209 batadv_hardif_free_ref(primary_if);
1210}
1211
Simon Wunderlich23721382012-01-22 20:00:19 +01001212/* periodic work to do:
1213 * * purge structures when they are too old
1214 * * send announcements
1215 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001216static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001217{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001218 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001219 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001220 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001221 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001222 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001223 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001224 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001225 int i;
1226
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001227 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001228 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1229 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001230 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001231 if (!primary_if)
1232 goto out;
1233
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001234 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1235 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001236
1237 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1238 goto out;
1239
Sven Eckelmann807736f2012-07-15 22:26:51 +02001240 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001241 if (!hash)
1242 goto out;
1243
1244 for (i = 0; i < hash->size; i++) {
1245 head = &hash->table[i];
1246
1247 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001248 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001249 if (!batadv_compare_eth(backbone_gw->orig,
1250 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001251 continue;
1252
1253 backbone_gw->lasttime = jiffies;
1254
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001255 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001256
1257 /* request_sent is only set after creation to avoid
1258 * problems when we are not yet known as backbone gw
1259 * in the backbone.
1260 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001261 * We can reset this now after we waited some periods
1262 * to give bridge forward delays and bla group forming
1263 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001264 */
1265
1266 if (atomic_read(&backbone_gw->request_sent) == 0)
1267 continue;
1268
Simon Wunderlich28709872012-09-13 18:18:46 +02001269 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1270 continue;
1271
Simon Wunderlichd807f272012-09-09 22:27:57 +02001272 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1273 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001274 }
1275 rcu_read_unlock();
1276 }
1277out:
1278 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001279 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001280
Antonio Quartulli72414442012-12-25 13:14:37 +01001281 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1282 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001283}
1284
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001285/* The hash for claim and backbone hash receive the same key because they
1286 * are getting initialized by hash_new with the same key. Reinitializing
1287 * them with to different keys to allow nested locking without generating
1288 * lockdep warnings
1289 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001290static struct lock_class_key batadv_claim_hash_lock_class_key;
1291static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001292
Simon Wunderlich23721382012-01-22 20:00:19 +01001293/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001294int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001295{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001296 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001297 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001298 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001299 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001300 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001301
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001302 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1303
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001304 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001305
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001306 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001307 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1308 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001309 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001310 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001311 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1312 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001313 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001314 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001315 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001316 }
1317
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001318 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001319 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001320 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001321 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1322 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001323
Sven Eckelmann807736f2012-07-15 22:26:51 +02001324 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001325 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001326
Sven Eckelmann807736f2012-07-15 22:26:51 +02001327 bat_priv->bla.claim_hash = batadv_hash_new(128);
1328 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001329
Sven Eckelmann807736f2012-07-15 22:26:51 +02001330 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001331 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001332
Sven Eckelmann807736f2012-07-15 22:26:51 +02001333 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001334 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001335 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001336 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001337
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001338 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001339
Antonio Quartulli72414442012-12-25 13:14:37 +01001340 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1341
1342 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1343 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001344 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001345}
1346
Ben Hutchings2c530402012-07-10 10:55:09 +00001347/**
1348 * batadv_bla_check_bcast_duplist
1349 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001350 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001351 *
1352 * check if it is on our broadcast list. Another gateway might
1353 * have sent the same packet because it is connected to the same backbone,
1354 * so we have to remove this duplicate.
1355 *
1356 * This is performed by checking the CRC, which will tell us
1357 * with a good chance that it is the same packet. If it is furthermore
1358 * sent by another host, drop it. We allow equal packets from
1359 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001360 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001361int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001362 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001363{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001364 int i, curr, ret = 0;
1365 __be32 crc;
1366 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001367 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001368
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001369 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001370
1371 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001372 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001373
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001374 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1375
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001376 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001377 curr = (bat_priv->bla.bcast_duplist_curr + i);
1378 curr %= BATADV_DUPLIST_SIZE;
1379 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001380
1381 /* we can stop searching if the entry is too old ;
1382 * later entries will be even older
1383 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001384 if (batadv_has_timed_out(entry->entrytime,
1385 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001386 break;
1387
1388 if (entry->crc != crc)
1389 continue;
1390
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001391 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001392 continue;
1393
1394 /* this entry seems to match: same crc, not too old,
1395 * and from another gw. therefore return 1 to forbid it.
1396 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001397 ret = 1;
1398 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001399 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001400 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001401 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001402 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001403 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001404 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001405 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001406 entry->crc = crc;
1407 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001408 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001409 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001410
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001411out:
1412 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1413
1414 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001415}
1416
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001417/**
1418 * batadv_bla_is_backbone_gw_orig
1419 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001420 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001421 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001422 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001423 * Check if the originator is a gateway for the VLAN identified by vid.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001424 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001425 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001426 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001427bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001428 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001429{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001430 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001431 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001432 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001433 int i;
1434
1435 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001436 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001437
1438 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001439 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001440
1441 for (i = 0; i < hash->size; i++) {
1442 head = &hash->table[i];
1443
1444 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001445 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001446 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1447 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001448 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001449 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001450 }
1451 }
1452 rcu_read_unlock();
1453 }
1454
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001455 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001456}
1457
Ben Hutchings2c530402012-07-10 10:55:09 +00001458/**
1459 * batadv_bla_is_backbone_gw
1460 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001461 * @orig_node: the orig_node of the frame
1462 * @hdr_size: maximum length of the frame
1463 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001464 * bla_is_backbone_gw inspects the skb for the VLAN ID
1465 *
1466 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1467 * it returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001468 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001469int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001470 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001471{
Marek Lindnerbae98772012-12-25 17:03:24 +08001472 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001473 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001474
1475 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1476 return 0;
1477
1478 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001479 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001480 return 0;
1481
Antonio Quartullic018ad32013-06-04 12:11:39 +02001482 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001483
1484 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001485 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1486 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001487 if (!backbone_gw)
1488 return 0;
1489
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001490 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001491 return 1;
1492}
1493
1494/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001495void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001496{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001497 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001498
Sven Eckelmann807736f2012-07-15 22:26:51 +02001499 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001500 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001501
Sven Eckelmann807736f2012-07-15 22:26:51 +02001502 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001503 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001504 batadv_hash_destroy(bat_priv->bla.claim_hash);
1505 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001506 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001507 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001508 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001509 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1510 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001511 }
1512 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001513 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001514}
1515
Ben Hutchings2c530402012-07-10 10:55:09 +00001516/**
1517 * batadv_bla_rx
1518 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001519 * @skb: the frame to be checked
1520 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001521 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001522 *
1523 * bla_rx avoidance checks if:
1524 * * we have to race for a claim
1525 * * if the frame is allowed on the LAN
1526 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001527 * in these cases, the skb is further handled by this function
1528 *
1529 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
Simon Wunderlich23721382012-01-22 20:00:19 +01001530 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001531 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001532int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1533 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001534{
1535 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001536 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001537 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001538 int ret;
1539
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001540 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001541
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001542 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001543 if (!primary_if)
1544 goto handled;
1545
1546 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1547 goto allow;
1548
Sven Eckelmann807736f2012-07-15 22:26:51 +02001549 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001550 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001551 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001552 goto handled;
1553
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001554 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001555 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001556 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001557
1558 if (!claim) {
1559 /* possible optimization: race for a claim */
1560 /* No claim exists yet, claim it for us!
1561 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001562 batadv_handle_claim(bat_priv, primary_if,
1563 primary_if->net_dev->dev_addr,
1564 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001565 goto allow;
1566 }
1567
1568 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001569 if (batadv_compare_eth(claim->backbone_gw->orig,
1570 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001571 /* ... allow it in any case */
1572 claim->lasttime = jiffies;
1573 goto allow;
1574 }
1575
1576 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001577 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1578 /* ... drop it. the responsible gateway is in charge.
1579 *
1580 * We need to check is_bcast because with the gateway
1581 * feature, broadcasts (like DHCP requests) may be sent
1582 * using a unicast packet type.
1583 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001584 goto handled;
1585 } else {
1586 /* seems the client considers us as its best gateway.
1587 * send a claim and update the claim table
1588 * immediately.
1589 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001590 batadv_handle_claim(bat_priv, primary_if,
1591 primary_if->net_dev->dev_addr,
1592 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001593 goto allow;
1594 }
1595allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001596 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001597 ret = 0;
1598 goto out;
1599
1600handled:
1601 kfree_skb(skb);
1602 ret = 1;
1603
1604out:
1605 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001606 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001607 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001608 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001609 return ret;
1610}
1611
Ben Hutchings2c530402012-07-10 10:55:09 +00001612/**
1613 * batadv_bla_tx
1614 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001615 * @skb: the frame to be checked
1616 * @vid: the VLAN ID of the frame
1617 *
1618 * bla_tx checks if:
1619 * * a claim was received which has to be processed
1620 * * the frame is allowed on the mesh
1621 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001622 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001623 *
1624 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001625 *
1626 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1627 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001628 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001629int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1630 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001631{
1632 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001633 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001634 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001635 int ret = 0;
1636
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001637 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001638 if (!primary_if)
1639 goto out;
1640
1641 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1642 goto allow;
1643
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001644 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001645 goto handled;
1646
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001647 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001648
Sven Eckelmann807736f2012-07-15 22:26:51 +02001649 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001650 /* don't allow broadcasts while requests are in flight */
1651 if (is_multicast_ether_addr(ethhdr->h_dest))
1652 goto handled;
1653
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001654 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001655 search_claim.vid = vid;
1656
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001657 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001658
1659 /* if no claim exists, allow it. */
1660 if (!claim)
1661 goto allow;
1662
1663 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001664 if (batadv_compare_eth(claim->backbone_gw->orig,
1665 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001666 /* if yes, the client has roamed and we have
1667 * to unclaim it.
1668 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001669 batadv_handle_unclaim(bat_priv, primary_if,
1670 primary_if->net_dev->dev_addr,
1671 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001672 goto allow;
1673 }
1674
1675 /* check if it is a multicast/broadcast frame */
1676 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1677 /* drop it. the responsible gateway has forwarded it into
1678 * the backbone network.
1679 */
1680 goto handled;
1681 } else {
1682 /* we must allow it. at least if we are
1683 * responsible for the DESTINATION.
1684 */
1685 goto allow;
1686 }
1687allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001688 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001689 ret = 0;
1690 goto out;
1691handled:
1692 ret = 1;
1693out:
1694 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001695 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001696 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001697 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001698 return ret;
1699}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001700
Sven Eckelmann08adf152012-05-12 13:38:47 +02001701int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001702{
1703 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001704 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001705 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001706 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001707 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001708 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001709 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001710 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001711 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001712 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001713
Marek Lindner30da63a2012-08-03 17:15:46 +02001714 primary_if = batadv_seq_print_text_primary_if_get(seq);
1715 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001716 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001717
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001718 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001719 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001720 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001721 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001722 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001723 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001724 "Client", "VID", "Originator", "CRC");
1725 for (i = 0; i < hash->size; i++) {
1726 head = &hash->table[i];
1727
1728 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001729 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001730 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1731 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001732
1733 spin_lock_bh(&claim->backbone_gw->crc_lock);
1734 backbone_crc = claim->backbone_gw->crc;
1735 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001736 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001737 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001738 claim->backbone_gw->orig,
1739 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001740 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001741 }
1742 rcu_read_unlock();
1743 }
1744out:
1745 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001746 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001747 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001748}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001749
1750int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1751{
1752 struct net_device *net_dev = (struct net_device *)seq->private;
1753 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001754 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001755 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001756 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001757 struct hlist_head *head;
1758 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001759 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001760 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001761 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001762 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001763
Marek Lindner30da63a2012-08-03 17:15:46 +02001764 primary_if = batadv_seq_print_text_primary_if_get(seq);
1765 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001766 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001767
1768 primary_addr = primary_if->net_dev->dev_addr;
1769 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001770 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001771 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001772 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001773 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001774 "Originator", "VID", "last seen", "CRC");
1775 for (i = 0; i < hash->size; i++) {
1776 head = &hash->table[i];
1777
1778 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001779 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001780 msecs = jiffies_to_msecs(jiffies -
1781 backbone_gw->lasttime);
1782 secs = msecs / 1000;
1783 msecs = msecs % 1000;
1784
1785 is_own = batadv_compare_eth(backbone_gw->orig,
1786 primary_addr);
1787 if (is_own)
1788 continue;
1789
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001790 spin_lock_bh(&backbone_gw->crc_lock);
1791 backbone_crc = backbone_gw->crc;
1792 spin_unlock_bh(&backbone_gw->crc_lock);
1793
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001794 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001795 backbone_gw->orig,
1796 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001797 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001798 }
1799 rcu_read_unlock();
1800 }
1801out:
1802 if (primary_if)
1803 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001804 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001805}