blob: 99dcae316ec89c01dc8342084ff48477139584bc [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
61/* return the index of the claim */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020062static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010063{
Marek Lindner712bbfe42012-12-25 17:03:25 +080064 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020065 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010066
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010067 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
68 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010069
70 return hash % size;
71}
72
73/* return the index of the backbone gateway */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020074static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010075{
Marek Lindner712bbfe42012-12-25 17:03:25 +080076 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020077 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010078
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010079 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
80 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010081
82 return hash % size;
83}
84
Simon Wunderlich23721382012-01-22 20:00:19 +010085/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020086static int batadv_compare_backbone_gw(const struct hlist_node *node,
87 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010088{
Marek Lindnerbae98772012-12-25 17:03:24 +080089 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010090 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +020091 const struct batadv_bla_backbone_gw *gw1 = data1;
92 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010093
Simon Wunderlichc76d1522012-10-15 22:38:04 +020094 if (!batadv_compare_eth(gw1->orig, gw2->orig))
95 return 0;
96
97 if (gw1->vid != gw2->vid)
98 return 0;
99
100 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100101}
102
103/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200104static int batadv_compare_claim(const struct hlist_node *node,
105 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100106{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800107 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100108 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200109 const struct batadv_bla_claim *cl1 = data1;
110 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100111
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200112 if (!batadv_compare_eth(cl1->addr, cl2->addr))
113 return 0;
114
115 if (cl1->vid != cl2->vid)
116 return 0;
117
118 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100119}
120
121/* free a backbone gw */
Marek Lindnerbae98772012-12-25 17:03:24 +0800122static void
123batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100124{
125 if (atomic_dec_and_test(&backbone_gw->refcount))
126 kfree_rcu(backbone_gw, rcu);
127}
128
129/* finally deinitialize the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200130static void batadv_claim_free_rcu(struct rcu_head *rcu)
Simon Wunderlich23721382012-01-22 20:00:19 +0100131{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800132 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100133
Marek Lindner712bbfe42012-12-25 17:03:25 +0800134 claim = container_of(rcu, struct batadv_bla_claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100135
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200136 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100137 kfree(claim);
138}
139
140/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800141static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100142{
143 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200144 call_rcu(&claim->rcu, batadv_claim_free_rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100145}
146
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100147/**
148 * batadv_claim_hash_find
149 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100150 * @data: search data (may be local/static data)
151 *
152 * looks for a claim in the hash, and returns it if found
153 * or NULL otherwise.
154 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800155static struct batadv_bla_claim
156*batadv_claim_hash_find(struct batadv_priv *bat_priv,
157 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100158{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200159 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100160 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800161 struct batadv_bla_claim *claim;
162 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100163 int index;
164
165 if (!hash)
166 return NULL;
167
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200168 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100169 head = &hash->table[index];
170
171 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800172 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200173 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100174 continue;
175
176 if (!atomic_inc_not_zero(&claim->refcount))
177 continue;
178
179 claim_tmp = claim;
180 break;
181 }
182 rcu_read_unlock();
183
184 return claim_tmp;
185}
186
Ben Hutchings2c530402012-07-10 10:55:09 +0000187/**
188 * batadv_backbone_hash_find - looks for a claim in the hash
189 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100190 * @addr: the address of the originator
191 * @vid: the VLAN ID
192 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000193 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100194 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800195static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200196batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
197 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100198{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200199 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100200 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800201 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
202 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100203 int index;
204
205 if (!hash)
206 return NULL;
207
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100208 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100209 search_entry.vid = vid;
210
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200211 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100212 head = &hash->table[index];
213
214 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800215 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200216 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
217 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100218 continue;
219
220 if (!atomic_inc_not_zero(&backbone_gw->refcount))
221 continue;
222
223 backbone_gw_tmp = backbone_gw;
224 break;
225 }
226 rcu_read_unlock();
227
228 return backbone_gw_tmp;
229}
230
231/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200232static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800233batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100234{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200235 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800236 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100237 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800238 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100239 int i;
240 spinlock_t *list_lock; /* protects write access to the hash lists */
241
Sven Eckelmann807736f2012-07-15 22:26:51 +0200242 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100243 if (!hash)
244 return;
245
246 for (i = 0; i < hash->size; i++) {
247 head = &hash->table[i];
248 list_lock = &hash->list_locks[i];
249
250 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800251 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100252 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100253 if (claim->backbone_gw != backbone_gw)
254 continue;
255
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200256 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100258 }
259 spin_unlock_bh(list_lock);
260 }
261
Antonio Quartulli3f687852014-11-02 11:29:56 +0100262 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200263 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200264 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200265 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100266}
267
Ben Hutchings2c530402012-07-10 10:55:09 +0000268/**
269 * batadv_bla_send_claim - sends a claim frame according to the provided info
270 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200271 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100272 * @vid: the VLAN ID
273 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100274 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200275static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200276 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100277{
278 struct sk_buff *skb;
279 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200280 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100281 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200282 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200283 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100284 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100285
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200286 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100287 if (!primary_if)
288 return;
289
Sven Eckelmann807736f2012-07-15 22:26:51 +0200290 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100291 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100292 local_claim_dest.type = claimtype;
293
294 soft_iface = primary_if->soft_iface;
295
296 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
297 /* IP DST: 0.0.0.0 */
298 zeroip,
299 primary_if->soft_iface,
300 /* IP SRC: 0.0.0.0 */
301 zeroip,
302 /* Ethernet DST: Broadcast */
303 NULL,
304 /* Ethernet SRC/HW SRC: originator mac */
305 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200306 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100308 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100309 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200310 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100311
312 if (!skb)
313 goto out;
314
315 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200316 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100317
318 /* now we pretend that the client would have sent this ... */
319 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200320 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100321 /* normal claim frame
322 * set Ethernet SRC to the clients mac
323 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100324 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200325 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200326 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
327 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100328 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200329 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100330 /* unclaim frame
331 * set HW SRC to the clients mac
332 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100333 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200334 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200335 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200336 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100337 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200338 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100339 /* announcement frame
340 * set HW SRC to the special mac containg the crc
341 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100342 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200343 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200344 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200345 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100346 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200347 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100348 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200349 * set HW SRC and header destination to the receiving backbone
350 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100351 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100352 ether_addr_copy(hw_src, mac);
353 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200354 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200355 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200356 ethhdr->h_source, ethhdr->h_dest,
357 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100358 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100359 }
360
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200361 if (vid & BATADV_VLAN_HAS_TAG)
362 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
363 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100364
365 skb_reset_mac_header(skb);
366 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200367 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
368 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
369 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100370 soft_iface->last_rx = jiffies;
371
372 netif_rx(skb);
373out:
374 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200375 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100376}
377
Ben Hutchings2c530402012-07-10 10:55:09 +0000378/**
379 * batadv_bla_get_backbone_gw
380 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100381 * @orig: the mac address of the originator
382 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200383 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100384 *
385 * searches for the backbone gw or creates a new one if it could not
386 * be found.
387 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800388static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200389batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200390 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100391{
Marek Lindnerbae98772012-12-25 17:03:24 +0800392 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200393 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100394 int hash_added;
395
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200396 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100397
398 if (entry)
399 return entry;
400
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200401 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200402 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200403 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100404
405 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
406 if (!entry)
407 return NULL;
408
409 entry->vid = vid;
410 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200411 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100412 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200413 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100414 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200415 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100416 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100417
418 /* one for the hash, one for returning */
419 atomic_set(&entry->refcount, 2);
420
Sven Eckelmann807736f2012-07-15 22:26:51 +0200421 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200422 batadv_compare_backbone_gw,
423 batadv_choose_backbone_gw, entry,
424 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100425
426 if (unlikely(hash_added != 0)) {
427 /* hash failed, free the structure */
428 kfree(entry);
429 return NULL;
430 }
431
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200432 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200433 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100434 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200435 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200436 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200437 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100438 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200439
Simon Wunderlichd807f272012-09-09 22:27:57 +0200440 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200441 batadv_bla_send_announce(bat_priv, entry);
442
Simon Wunderlichd807f272012-09-09 22:27:57 +0200443 /* this will be decreased in the worker thread */
444 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200445 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200446 atomic_inc(&bat_priv->bla.num_requests);
447 }
448
Simon Wunderlich23721382012-01-22 20:00:19 +0100449 return entry;
450}
451
452/* update or add the own backbone gw to make sure we announce
453 * where we receive other backbone gws
454 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200455static void
456batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
457 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200458 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100459{
Marek Lindnerbae98772012-12-25 17:03:24 +0800460 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100461
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200462 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
463 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200464 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100465 if (unlikely(!backbone_gw))
466 return;
467
468 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200469 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100470}
471
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100472/**
473 * batadv_bla_answer_request - answer a bla request by sending own claims
474 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200475 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100476 * @vid: the vid where the request came on
477 *
478 * Repeat all of our own claims, and finally send an ANNOUNCE frame
479 * to allow the requester another check if the CRC is correct now.
480 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
482 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200483 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100484{
Simon Wunderlich23721382012-01-22 20:00:19 +0100485 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200486 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800487 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800488 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100489 int i;
490
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200491 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200492 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100493
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200494 backbone_gw = batadv_backbone_hash_find(bat_priv,
495 primary_if->net_dev->dev_addr,
496 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100497 if (!backbone_gw)
498 return;
499
Sven Eckelmann807736f2012-07-15 22:26:51 +0200500 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100501 for (i = 0; i < hash->size; i++) {
502 head = &hash->table[i];
503
504 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800505 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100506 /* only own claims are interesting */
507 if (claim->backbone_gw != backbone_gw)
508 continue;
509
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200510 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200511 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100512 }
513 rcu_read_unlock();
514 }
515
516 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200517 batadv_bla_send_announce(bat_priv, backbone_gw);
518 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100519}
520
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100521/**
522 * batadv_bla_send_request - send a request to repeat claims
523 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100524 *
525 * When the crc is wrong, ask the backbone gateway for a full table update.
526 * After the request, it will repeat all of his own claims and finally
527 * send an announcement claim with which we can check again.
528 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800529static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100530{
531 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200532 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100533
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200534 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
535 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100536
537 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200538 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200539 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100540
541 /* no local broadcasts should be sent or received, for now. */
542 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200543 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100544 atomic_set(&backbone_gw->request_sent, 1);
545 }
546}
547
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100548/**
549 * batadv_bla_send_announce
550 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100551 * @backbone_gw: our backbone gateway which should be announced
552 *
553 * This function sends an announcement. It is called from multiple
554 * places.
555 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200556static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800557 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100558{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200559 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100560 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100561
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200562 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200563 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100564 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200565 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100566 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100567
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200568 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200569 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100570}
571
Ben Hutchings2c530402012-07-10 10:55:09 +0000572/**
573 * batadv_bla_add_claim - Adds a claim in the claim hash
574 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100575 * @mac: the mac address of the claim
576 * @vid: the VLAN ID of the frame
577 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100578 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200579static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200580 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800581 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100582{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800583 struct batadv_bla_claim *claim;
584 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100585 int hash_added;
586
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100587 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100588 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200589 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100590
591 /* create a new claim entry if it does not exist yet. */
592 if (!claim) {
593 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
594 if (!claim)
595 return;
596
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100597 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100598 claim->vid = vid;
599 claim->lasttime = jiffies;
600 claim->backbone_gw = backbone_gw;
601
602 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200603 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200604 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200605 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200606 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200607 batadv_compare_claim,
608 batadv_choose_claim, claim,
609 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100610
611 if (unlikely(hash_added != 0)) {
612 /* only local changes happened. */
613 kfree(claim);
614 return;
615 }
616 } else {
617 claim->lasttime = jiffies;
618 if (claim->backbone_gw == backbone_gw)
619 /* no need to register a new backbone */
620 goto claim_free_ref;
621
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200622 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200623 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200624 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100625
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200626 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200627 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200628 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200629 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100630 }
631 /* set (new) backbone gw */
632 atomic_inc(&backbone_gw->refcount);
633 claim->backbone_gw = backbone_gw;
634
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200635 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100636 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200637 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100638 backbone_gw->lasttime = jiffies;
639
640claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200641 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100642}
643
644/* Delete a claim from the claim hash which has the
645 * given mac address and vid.
646 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200647static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200648 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100649{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800650 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100651
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100652 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100653 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200654 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100655 if (!claim)
656 return;
657
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200658 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200659 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100660
Sven Eckelmann807736f2012-07-15 22:26:51 +0200661 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200662 batadv_choose_claim, claim);
663 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100664
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200665 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100666 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200667 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100668
669 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200670 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100671}
672
673/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200674static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
675 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100676{
Marek Lindnerbae98772012-12-25 17:03:24 +0800677 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200678 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100679
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200680 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100681 return 0;
682
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200683 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
684 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100685
686 if (unlikely(!backbone_gw))
687 return 1;
688
Simon Wunderlich23721382012-01-22 20:00:19 +0100689 /* handle as ANNOUNCE frame */
690 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100691 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100692
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200693 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100694 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200695 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100696
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200697 spin_lock_bh(&backbone_gw->crc_lock);
698 backbone_crc = backbone_gw->crc;
699 spin_unlock_bh(&backbone_gw->crc_lock);
700
701 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200702 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100703 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200704 backbone_gw->orig,
705 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200706 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100707
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200708 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100709 } else {
710 /* if we have sent a request and the crc was OK,
711 * we can allow traffic again.
712 */
713 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200714 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100715 atomic_set(&backbone_gw->request_sent, 0);
716 }
717 }
718
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200719 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100720 return 1;
721}
722
723/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200724static int batadv_handle_request(struct batadv_priv *bat_priv,
725 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200726 u8 *backbone_addr, struct ethhdr *ethhdr,
727 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100728{
729 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200730 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100731 return 0;
732
733 /* sanity check, this should not happen on a normal switch,
734 * we ignore it in this case.
735 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200736 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100737 return 1;
738
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200739 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200740 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200741 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100742
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200743 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100744 return 1;
745}
746
747/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200748static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
749 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200750 u8 *backbone_addr, u8 *claim_addr,
751 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100752{
Marek Lindnerbae98772012-12-25 17:03:24 +0800753 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100754
755 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200756 if (primary_if && batadv_compare_eth(backbone_addr,
757 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200758 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200759 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100760
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200761 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100762
763 if (!backbone_gw)
764 return 1;
765
766 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200767 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200768 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200769 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100770
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200771 batadv_bla_del_claim(bat_priv, claim_addr, vid);
772 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100773 return 1;
774}
775
776/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200777static int batadv_handle_claim(struct batadv_priv *bat_priv,
778 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200779 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200780 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100781{
Marek Lindnerbae98772012-12-25 17:03:24 +0800782 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100783
784 /* register the gateway if not yet available, and add the claim. */
785
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200786 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
787 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100788
789 if (unlikely(!backbone_gw))
790 return 1;
791
792 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200793 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200794 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200795 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200796 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100797
798 /* TODO: we could call something like tt_local_del() here. */
799
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200800 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100801 return 1;
802}
803
Ben Hutchings2c530402012-07-10 10:55:09 +0000804/**
805 * batadv_check_claim_group
806 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200807 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100808 * @hw_src: the Hardware source in the ARP Header
809 * @hw_dst: the Hardware destination in the ARP Header
810 * @ethhdr: pointer to the Ethernet header of the claim frame
811 *
812 * checks if it is a claim packet and if its on the same group.
813 * This function also applies the group ID of the sender
814 * if it is in the same mesh.
815 *
816 * returns:
817 * 2 - if it is a claim packet and on the same group
818 * 1 - if is a claim packet from another group
819 * 0 - if it is not a claim packet
820 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200821static int batadv_check_claim_group(struct batadv_priv *bat_priv,
822 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200823 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200824 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100825{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200826 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200827 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200828 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100829
Sven Eckelmann96412692012-06-05 22:31:30 +0200830 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200831 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100832
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100833 /* if announcement packet, use the source,
834 * otherwise assume it is in the hw_src
835 */
836 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200837 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100838 backbone_addr = hw_src;
839 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200840 case BATADV_CLAIM_TYPE_REQUEST:
841 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200842 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100843 backbone_addr = ethhdr->h_source;
844 break;
845 default:
846 return 0;
847 }
848
849 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200850 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100851 return 0;
852
853 /* if its already the same group, it is fine. */
854 if (bla_dst->group == bla_dst_own->group)
855 return 2;
856
857 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200858 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100859
860 /* dont accept claims from gateways which are not in
861 * the same mesh or group.
862 */
863 if (!orig_node)
864 return 1;
865
866 /* if our mesh friends mac is bigger, use it for ourselves. */
867 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200868 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100869 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200870 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100871 bla_dst_own->group = bla_dst->group;
872 }
873
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200874 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100875
876 return 2;
877}
878
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100879/**
880 * batadv_bla_process_claim
881 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200882 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100883 * @skb: the frame to be checked
884 *
885 * Check if this is a claim frame, and process it accordingly.
886 *
887 * returns 1 if it was a claim frame, otherwise return 0 to
888 * tell the callee that it can use the frame on its own.
889 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200890static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
891 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200892 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100893{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200894 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200895 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200896 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200897 struct ethhdr *ethhdr;
898 struct arphdr *arphdr;
899 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200900 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200901 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100902 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100903 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100904
Antonio Quartullic018ad32013-06-04 12:11:39 +0200905 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200906 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100907
Antonio Quartullic018ad32013-06-04 12:11:39 +0200908 proto = ethhdr->h_proto;
909 headlen = ETH_HLEN;
910 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200911 /* Traverse the VLAN/Ethertypes.
912 *
913 * At this point it is known that the first protocol is a VLAN
914 * header, so start checking at the encapsulated protocol.
915 *
916 * The depth of the VLAN headers is recorded to drop BLA claim
917 * frames encapsulated into multiple VLAN headers (QinQ).
918 */
919 do {
920 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
921 &vhdr_buf);
922 if (!vhdr)
923 return 0;
924
925 proto = vhdr->h_vlan_encapsulated_proto;
926 headlen += VLAN_HLEN;
927 vlan_depth++;
928 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100929 }
930
Antonio Quartulli293e9332013-05-19 12:55:16 +0200931 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +0100932 return 0; /* not a claim frame */
933
934 /* this must be a ARP frame. check if it is a claim. */
935
936 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
937 return 0;
938
939 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200940 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200941 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +0100942
943 /* Check whether the ARP frame carries a valid
944 * IP information
945 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100946 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
947 return 0;
948 if (arphdr->ar_pro != htons(ETH_P_IP))
949 return 0;
950 if (arphdr->ar_hln != ETH_ALEN)
951 return 0;
952 if (arphdr->ar_pln != 4)
953 return 0;
954
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200955 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100956 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200957 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200958 bla_dst_own = &bat_priv->bla.claim_dest;
959
960 /* check if it is a claim frame in general */
961 if (memcmp(bla_dst->magic, bla_dst_own->magic,
962 sizeof(bla_dst->magic)) != 0)
963 return 0;
964
965 /* check if there is a claim frame encapsulated deeper in (QinQ) and
966 * drop that, as this is not supported by BLA but should also not be
967 * sent via the mesh.
968 */
969 if (vlan_depth > 1)
970 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100971
972 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200973 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
974 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100975 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200976 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200977 "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 +0200978 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
979 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100980
981 if (ret < 2)
982 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100983
984 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200985 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100986
987 /* check for the different types of claim frames ... */
988 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200989 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200990 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
991 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100992 return 1;
993 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200994 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200995 if (batadv_handle_unclaim(bat_priv, primary_if,
996 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100997 return 1;
998 break;
999
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001000 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001001 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1002 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001003 return 1;
1004 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001005 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001006 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1007 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001008 return 1;
1009 break;
1010 }
1011
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001012 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001013 "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 +02001014 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001015 return 1;
1016}
1017
1018/* Check when we last heard from other nodes, and remove them in case of
1019 * a time out, or clean all backbone gws if now is set.
1020 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001021static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001022{
Marek Lindnerbae98772012-12-25 17:03:24 +08001023 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001024 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001025 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001026 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001027 spinlock_t *list_lock; /* protects write access to the hash lists */
1028 int i;
1029
Sven Eckelmann807736f2012-07-15 22:26:51 +02001030 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001031 if (!hash)
1032 return;
1033
1034 for (i = 0; i < hash->size; i++) {
1035 head = &hash->table[i];
1036 list_lock = &hash->list_locks[i];
1037
1038 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001039 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001040 head, hash_entry) {
1041 if (now)
1042 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001043 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001044 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001045 continue;
1046
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001047 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001048 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1049 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001050
1051purge_now:
1052 /* don't wait for the pending request anymore */
1053 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001054 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001055
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001056 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001057
Sasha Levinb67bfe02013-02-27 17:06:00 -08001058 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001059 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001060 }
1061 spin_unlock_bh(list_lock);
1062 }
1063}
1064
Ben Hutchings2c530402012-07-10 10:55:09 +00001065/**
1066 * batadv_bla_purge_claims
1067 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001068 * @primary_if: the selected primary interface, may be NULL if now is set
1069 * @now: whether the whole hash shall be wiped now
1070 *
1071 * Check when we heard last time from our own claims, and remove them in case of
1072 * a time out, or clean all claims if now is set
1073 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001074static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1075 struct batadv_hard_iface *primary_if,
1076 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001077{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001078 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001079 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001080 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001081 int i;
1082
Sven Eckelmann807736f2012-07-15 22:26:51 +02001083 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001084 if (!hash)
1085 return;
1086
1087 for (i = 0; i < hash->size; i++) {
1088 head = &hash->table[i];
1089
1090 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001091 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001092 if (now)
1093 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001094 if (!batadv_compare_eth(claim->backbone_gw->orig,
1095 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001096 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001097 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001098 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001099 continue;
1100
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001101 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001102 "bla_purge_claims(): %pM, vid %d, time out\n",
1103 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001104
1105purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001106 batadv_handle_unclaim(bat_priv, primary_if,
1107 claim->backbone_gw->orig,
1108 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001109 }
1110 rcu_read_unlock();
1111 }
1112}
1113
Ben Hutchings2c530402012-07-10 10:55:09 +00001114/**
1115 * batadv_bla_update_orig_address
1116 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001117 * @primary_if: the new selected primary_if
1118 * @oldif: the old primary interface, may be NULL
1119 *
1120 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001121 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001122void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1123 struct batadv_hard_iface *primary_if,
1124 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001125{
Marek Lindnerbae98772012-12-25 17:03:24 +08001126 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001127 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001128 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001129 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001130 int i;
1131
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001132 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001133 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1134 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001135
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001136 /* purge everything when bridge loop avoidance is turned off */
1137 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1138 oldif = NULL;
1139
Simon Wunderlich23721382012-01-22 20:00:19 +01001140 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001141 batadv_bla_purge_claims(bat_priv, NULL, 1);
1142 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001143 return;
1144 }
1145
Sven Eckelmann807736f2012-07-15 22:26:51 +02001146 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001147 if (!hash)
1148 return;
1149
1150 for (i = 0; i < hash->size; i++) {
1151 head = &hash->table[i];
1152
1153 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001154 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001155 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001156 if (!batadv_compare_eth(backbone_gw->orig,
1157 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001158 continue;
1159
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001160 ether_addr_copy(backbone_gw->orig,
1161 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001162 /* send an announce frame so others will ask for our
1163 * claims and update their tables.
1164 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001165 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001166 }
1167 rcu_read_unlock();
1168 }
1169}
1170
Simon Wunderlich23721382012-01-22 20:00:19 +01001171/* periodic work to do:
1172 * * purge structures when they are too old
1173 * * send announcements
1174 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001175static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001176{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001177 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001178 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001179 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001180 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001181 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001182 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001183 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001184 int i;
1185
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001186 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001187 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1188 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001189 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001190 if (!primary_if)
1191 goto out;
1192
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001193 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1194 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001195
1196 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1197 goto out;
1198
Sven Eckelmann807736f2012-07-15 22:26:51 +02001199 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001200 if (!hash)
1201 goto out;
1202
1203 for (i = 0; i < hash->size; i++) {
1204 head = &hash->table[i];
1205
1206 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001207 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001208 if (!batadv_compare_eth(backbone_gw->orig,
1209 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001210 continue;
1211
1212 backbone_gw->lasttime = jiffies;
1213
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001214 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001215
1216 /* request_sent is only set after creation to avoid
1217 * problems when we are not yet known as backbone gw
1218 * in the backbone.
1219 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001220 * We can reset this now after we waited some periods
1221 * to give bridge forward delays and bla group forming
1222 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001223 */
1224
1225 if (atomic_read(&backbone_gw->request_sent) == 0)
1226 continue;
1227
Simon Wunderlich28709872012-09-13 18:18:46 +02001228 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1229 continue;
1230
Simon Wunderlichd807f272012-09-09 22:27:57 +02001231 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1232 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001233 }
1234 rcu_read_unlock();
1235 }
1236out:
1237 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001238 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001239
Antonio Quartulli72414442012-12-25 13:14:37 +01001240 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1241 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001242}
1243
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001244/* The hash for claim and backbone hash receive the same key because they
1245 * are getting initialized by hash_new with the same key. Reinitializing
1246 * them with to different keys to allow nested locking without generating
1247 * lockdep warnings
1248 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001249static struct lock_class_key batadv_claim_hash_lock_class_key;
1250static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001251
Simon Wunderlich23721382012-01-22 20:00:19 +01001252/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001253int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001254{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001255 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001256 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001257 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001258 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001259 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001260
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001261 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1262
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001263 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001264
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001265 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001266 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1267 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001268 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001269 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001270 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1271 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001272 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001273 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001274 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001275 }
1276
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001277 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001278 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001279 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001280 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1281 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001282
Sven Eckelmann807736f2012-07-15 22:26:51 +02001283 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001284 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001285
Sven Eckelmann807736f2012-07-15 22:26:51 +02001286 bat_priv->bla.claim_hash = batadv_hash_new(128);
1287 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001288
Sven Eckelmann807736f2012-07-15 22:26:51 +02001289 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001290 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001291
Sven Eckelmann807736f2012-07-15 22:26:51 +02001292 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001293 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001294 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001295 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001296
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001297 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001298
Antonio Quartulli72414442012-12-25 13:14:37 +01001299 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1300
1301 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1302 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001303 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001304}
1305
Ben Hutchings2c530402012-07-10 10:55:09 +00001306/**
1307 * batadv_bla_check_bcast_duplist
1308 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001309 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001310 *
1311 * check if it is on our broadcast list. Another gateway might
1312 * have sent the same packet because it is connected to the same backbone,
1313 * so we have to remove this duplicate.
1314 *
1315 * This is performed by checking the CRC, which will tell us
1316 * with a good chance that it is the same packet. If it is furthermore
1317 * sent by another host, drop it. We allow equal packets from
1318 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001319 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001320int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001321 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001322{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001323 int i, curr, ret = 0;
1324 __be32 crc;
1325 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001326 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001327
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001328 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001329
1330 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001331 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001332
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001333 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1334
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001335 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001336 curr = (bat_priv->bla.bcast_duplist_curr + i);
1337 curr %= BATADV_DUPLIST_SIZE;
1338 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001339
1340 /* we can stop searching if the entry is too old ;
1341 * later entries will be even older
1342 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001343 if (batadv_has_timed_out(entry->entrytime,
1344 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001345 break;
1346
1347 if (entry->crc != crc)
1348 continue;
1349
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001350 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001351 continue;
1352
1353 /* this entry seems to match: same crc, not too old,
1354 * and from another gw. therefore return 1 to forbid it.
1355 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001356 ret = 1;
1357 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001358 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001359 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001360 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001361 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001362 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001363 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001364 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001365 entry->crc = crc;
1366 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001367 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001368 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001369
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001370out:
1371 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1372
1373 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001374}
1375
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001376/**
1377 * batadv_bla_is_backbone_gw_orig
1378 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001379 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001380 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001381 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001382 * Check if the originator is a gateway for the VLAN identified by vid.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001383 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001384 * Returns true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001385 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001386bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001387 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001388{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001389 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001390 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001391 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001392 int i;
1393
1394 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001395 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001396
1397 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001398 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001399
1400 for (i = 0; i < hash->size; i++) {
1401 head = &hash->table[i];
1402
1403 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001404 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001405 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1406 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001407 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001408 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001409 }
1410 }
1411 rcu_read_unlock();
1412 }
1413
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001414 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001415}
1416
Ben Hutchings2c530402012-07-10 10:55:09 +00001417/**
1418 * batadv_bla_is_backbone_gw
1419 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001420 * @orig_node: the orig_node of the frame
1421 * @hdr_size: maximum length of the frame
1422 *
1423 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1424 * if the orig_node is also a gateway on the soft interface, otherwise it
1425 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001426 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001427int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001428 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001429{
Marek Lindnerbae98772012-12-25 17:03:24 +08001430 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001431 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001432
1433 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1434 return 0;
1435
1436 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001437 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001438 return 0;
1439
Antonio Quartullic018ad32013-06-04 12:11:39 +02001440 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001441
1442 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001443 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1444 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001445 if (!backbone_gw)
1446 return 0;
1447
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001448 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001449 return 1;
1450}
1451
1452/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001453void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001454{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001455 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001456
Sven Eckelmann807736f2012-07-15 22:26:51 +02001457 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001458 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001459
Sven Eckelmann807736f2012-07-15 22:26:51 +02001460 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001461 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001462 batadv_hash_destroy(bat_priv->bla.claim_hash);
1463 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001464 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001465 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001466 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001467 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1468 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001469 }
1470 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001471 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001472}
1473
Ben Hutchings2c530402012-07-10 10:55:09 +00001474/**
1475 * batadv_bla_rx
1476 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001477 * @skb: the frame to be checked
1478 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001479 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001480 *
1481 * bla_rx avoidance checks if:
1482 * * we have to race for a claim
1483 * * if the frame is allowed on the LAN
1484 *
1485 * in these cases, the skb is further handled by this function and
1486 * returns 1, otherwise it returns 0 and the caller shall further
1487 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001488 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001489int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1490 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001491{
1492 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001493 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001494 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001495 int ret;
1496
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001497 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001498
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001499 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001500 if (!primary_if)
1501 goto handled;
1502
1503 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1504 goto allow;
1505
Sven Eckelmann807736f2012-07-15 22:26:51 +02001506 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001507 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001508 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001509 goto handled;
1510
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001511 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001512 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001513 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001514
1515 if (!claim) {
1516 /* possible optimization: race for a claim */
1517 /* No claim exists yet, claim it for us!
1518 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001519 batadv_handle_claim(bat_priv, primary_if,
1520 primary_if->net_dev->dev_addr,
1521 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001522 goto allow;
1523 }
1524
1525 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001526 if (batadv_compare_eth(claim->backbone_gw->orig,
1527 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001528 /* ... allow it in any case */
1529 claim->lasttime = jiffies;
1530 goto allow;
1531 }
1532
1533 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001534 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1535 /* ... drop it. the responsible gateway is in charge.
1536 *
1537 * We need to check is_bcast because with the gateway
1538 * feature, broadcasts (like DHCP requests) may be sent
1539 * using a unicast packet type.
1540 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001541 goto handled;
1542 } else {
1543 /* seems the client considers us as its best gateway.
1544 * send a claim and update the claim table
1545 * immediately.
1546 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001547 batadv_handle_claim(bat_priv, primary_if,
1548 primary_if->net_dev->dev_addr,
1549 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001550 goto allow;
1551 }
1552allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001553 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001554 ret = 0;
1555 goto out;
1556
1557handled:
1558 kfree_skb(skb);
1559 ret = 1;
1560
1561out:
1562 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001563 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001564 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001565 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001566 return ret;
1567}
1568
Ben Hutchings2c530402012-07-10 10:55:09 +00001569/**
1570 * batadv_bla_tx
1571 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001572 * @skb: the frame to be checked
1573 * @vid: the VLAN ID of the frame
1574 *
1575 * bla_tx checks if:
1576 * * a claim was received which has to be processed
1577 * * the frame is allowed on the mesh
1578 *
1579 * in these cases, the skb is further handled by this function and
1580 * returns 1, otherwise it returns 0 and the caller shall further
1581 * process the skb.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001582 *
1583 * This call might reallocate skb data.
Simon Wunderlich23721382012-01-22 20:00:19 +01001584 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001585int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1586 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001587{
1588 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001589 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001590 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001591 int ret = 0;
1592
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001593 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001594 if (!primary_if)
1595 goto out;
1596
1597 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1598 goto allow;
1599
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001600 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001601 goto handled;
1602
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001603 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001604
Sven Eckelmann807736f2012-07-15 22:26:51 +02001605 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001606 /* don't allow broadcasts while requests are in flight */
1607 if (is_multicast_ether_addr(ethhdr->h_dest))
1608 goto handled;
1609
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001610 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001611 search_claim.vid = vid;
1612
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001613 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001614
1615 /* if no claim exists, allow it. */
1616 if (!claim)
1617 goto allow;
1618
1619 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001620 if (batadv_compare_eth(claim->backbone_gw->orig,
1621 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001622 /* if yes, the client has roamed and we have
1623 * to unclaim it.
1624 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001625 batadv_handle_unclaim(bat_priv, primary_if,
1626 primary_if->net_dev->dev_addr,
1627 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001628 goto allow;
1629 }
1630
1631 /* check if it is a multicast/broadcast frame */
1632 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1633 /* drop it. the responsible gateway has forwarded it into
1634 * the backbone network.
1635 */
1636 goto handled;
1637 } else {
1638 /* we must allow it. at least if we are
1639 * responsible for the DESTINATION.
1640 */
1641 goto allow;
1642 }
1643allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001644 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001645 ret = 0;
1646 goto out;
1647handled:
1648 ret = 1;
1649out:
1650 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001651 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001652 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001653 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001654 return ret;
1655}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001656
Sven Eckelmann08adf152012-05-12 13:38:47 +02001657int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001658{
1659 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001660 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001661 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001662 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001663 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001664 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001665 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001666 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001667 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001668 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001669
Marek Lindner30da63a2012-08-03 17:15:46 +02001670 primary_if = batadv_seq_print_text_primary_if_get(seq);
1671 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001672 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001673
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001674 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001675 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001676 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001677 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001678 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001679 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001680 "Client", "VID", "Originator", "CRC");
1681 for (i = 0; i < hash->size; i++) {
1682 head = &hash->table[i];
1683
1684 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001685 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001686 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1687 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001688
1689 spin_lock_bh(&claim->backbone_gw->crc_lock);
1690 backbone_crc = claim->backbone_gw->crc;
1691 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001692 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001693 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001694 claim->backbone_gw->orig,
1695 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001696 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001697 }
1698 rcu_read_unlock();
1699 }
1700out:
1701 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001702 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001703 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001704}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001705
1706int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1707{
1708 struct net_device *net_dev = (struct net_device *)seq->private;
1709 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001710 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001711 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001712 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001713 struct hlist_head *head;
1714 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001715 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001716 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001717 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001718 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001719
Marek Lindner30da63a2012-08-03 17:15:46 +02001720 primary_if = batadv_seq_print_text_primary_if_get(seq);
1721 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001722 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001723
1724 primary_addr = primary_if->net_dev->dev_addr;
1725 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001726 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001727 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001728 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001729 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001730 "Originator", "VID", "last seen", "CRC");
1731 for (i = 0; i < hash->size; i++) {
1732 head = &hash->table[i];
1733
1734 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001735 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001736 msecs = jiffies_to_msecs(jiffies -
1737 backbone_gw->lasttime);
1738 secs = msecs / 1000;
1739 msecs = msecs % 1000;
1740
1741 is_own = batadv_compare_eth(backbone_gw->orig,
1742 primary_addr);
1743 if (is_own)
1744 continue;
1745
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001746 spin_lock_bh(&backbone_gw->crc_lock);
1747 backbone_crc = backbone_gw->crc;
1748 spin_unlock_bh(&backbone_gw->crc_lock);
1749
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001750 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001751 backbone_gw->orig,
1752 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001753 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001754 }
1755 rcu_read_unlock();
1756 }
1757out:
1758 if (primary_if)
1759 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001760 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001761}