blob: c24c481b666f776c864e31eefe92fdc2ca5fd779 [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 Eckelmann63b39922016-01-14 15:28:19 +0100130static void batadv_claim_release(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100131{
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200132 batadv_backbone_gw_free_ref(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100133 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100134}
135
136/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800137static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100138{
139 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann63b39922016-01-14 15:28:19 +0100140 batadv_claim_release(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100141}
142
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100143/**
144 * batadv_claim_hash_find
145 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100146 * @data: search data (may be local/static data)
147 *
148 * looks for a claim in the hash, and returns it if found
149 * or NULL otherwise.
150 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800151static struct batadv_bla_claim
152*batadv_claim_hash_find(struct batadv_priv *bat_priv,
153 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100154{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200155 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100156 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800157 struct batadv_bla_claim *claim;
158 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100159 int index;
160
161 if (!hash)
162 return NULL;
163
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200164 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100165 head = &hash->table[index];
166
167 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800168 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200169 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100170 continue;
171
172 if (!atomic_inc_not_zero(&claim->refcount))
173 continue;
174
175 claim_tmp = claim;
176 break;
177 }
178 rcu_read_unlock();
179
180 return claim_tmp;
181}
182
Ben Hutchings2c530402012-07-10 10:55:09 +0000183/**
184 * batadv_backbone_hash_find - looks for a claim in the hash
185 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100186 * @addr: the address of the originator
187 * @vid: the VLAN ID
188 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000189 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100190 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800191static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200192batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
193 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100194{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200195 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100196 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800197 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
198 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100199 int index;
200
201 if (!hash)
202 return NULL;
203
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100204 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100205 search_entry.vid = vid;
206
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200207 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100208 head = &hash->table[index];
209
210 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800211 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200212 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
213 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100214 continue;
215
216 if (!atomic_inc_not_zero(&backbone_gw->refcount))
217 continue;
218
219 backbone_gw_tmp = backbone_gw;
220 break;
221 }
222 rcu_read_unlock();
223
224 return backbone_gw_tmp;
225}
226
227/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800229batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100230{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200231 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800232 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100233 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800234 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100235 int i;
236 spinlock_t *list_lock; /* protects write access to the hash lists */
237
Sven Eckelmann807736f2012-07-15 22:26:51 +0200238 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100239 if (!hash)
240 return;
241
242 for (i = 0; i < hash->size; i++) {
243 head = &hash->table[i];
244 list_lock = &hash->list_locks[i];
245
246 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100248 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100249 if (claim->backbone_gw != backbone_gw)
250 continue;
251
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200252 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800253 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100254 }
255 spin_unlock_bh(list_lock);
256 }
257
Antonio Quartulli3f687852014-11-02 11:29:56 +0100258 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200259 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200260 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200261 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100262}
263
Ben Hutchings2c530402012-07-10 10:55:09 +0000264/**
265 * batadv_bla_send_claim - sends a claim frame according to the provided info
266 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200267 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100268 * @vid: the VLAN ID
269 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100270 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200271static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200272 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100273{
274 struct sk_buff *skb;
275 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200276 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100277 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200278 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200279 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100280 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100281
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200282 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100283 if (!primary_if)
284 return;
285
Sven Eckelmann807736f2012-07-15 22:26:51 +0200286 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100287 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100288 local_claim_dest.type = claimtype;
289
290 soft_iface = primary_if->soft_iface;
291
292 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
293 /* IP DST: 0.0.0.0 */
294 zeroip,
295 primary_if->soft_iface,
296 /* IP SRC: 0.0.0.0 */
297 zeroip,
298 /* Ethernet DST: Broadcast */
299 NULL,
300 /* Ethernet SRC/HW SRC: originator mac */
301 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200302 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100303 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100304 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100305 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200306 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100307
308 if (!skb)
309 goto out;
310
311 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200312 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100313
314 /* now we pretend that the client would have sent this ... */
315 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200316 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100317 /* normal claim frame
318 * set Ethernet SRC to the clients mac
319 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100320 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200321 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200322 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
323 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100324 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200325 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100326 /* unclaim frame
327 * set HW SRC to the clients mac
328 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100329 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200330 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200331 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200332 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100333 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200334 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100335 /* announcement frame
336 * set HW SRC to the special mac containg the crc
337 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100338 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200339 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200340 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200341 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100342 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200343 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100344 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200345 * set HW SRC and header destination to the receiving backbone
346 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100347 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100348 ether_addr_copy(hw_src, mac);
349 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200350 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200351 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200352 ethhdr->h_source, ethhdr->h_dest,
353 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100354 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100355 }
356
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200357 if (vid & BATADV_VLAN_HAS_TAG)
358 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
359 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100360
361 skb_reset_mac_header(skb);
362 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200363 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
364 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
365 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100366 soft_iface->last_rx = jiffies;
367
368 netif_rx(skb);
369out:
370 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200371 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100372}
373
Ben Hutchings2c530402012-07-10 10:55:09 +0000374/**
375 * batadv_bla_get_backbone_gw
376 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100377 * @orig: the mac address of the originator
378 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200379 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100380 *
381 * searches for the backbone gw or creates a new one if it could not
382 * be found.
383 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800384static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200385batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200386 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100387{
Marek Lindnerbae98772012-12-25 17:03:24 +0800388 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200389 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100390 int hash_added;
391
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200392 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100393
394 if (entry)
395 return entry;
396
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200397 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200398 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200399 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100400
401 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
402 if (!entry)
403 return NULL;
404
405 entry->vid = vid;
406 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200407 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100408 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200409 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100410 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200411 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100412 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100413
414 /* one for the hash, one for returning */
415 atomic_set(&entry->refcount, 2);
416
Sven Eckelmann807736f2012-07-15 22:26:51 +0200417 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200418 batadv_compare_backbone_gw,
419 batadv_choose_backbone_gw, entry,
420 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100421
422 if (unlikely(hash_added != 0)) {
423 /* hash failed, free the structure */
424 kfree(entry);
425 return NULL;
426 }
427
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200428 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200429 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100430 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200431 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200432 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200433 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100434 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200435
Simon Wunderlichd807f272012-09-09 22:27:57 +0200436 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200437 batadv_bla_send_announce(bat_priv, entry);
438
Simon Wunderlichd807f272012-09-09 22:27:57 +0200439 /* this will be decreased in the worker thread */
440 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200441 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200442 atomic_inc(&bat_priv->bla.num_requests);
443 }
444
Simon Wunderlich23721382012-01-22 20:00:19 +0100445 return entry;
446}
447
448/* update or add the own backbone gw to make sure we announce
449 * where we receive other backbone gws
450 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200451static void
452batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
453 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200454 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100455{
Marek Lindnerbae98772012-12-25 17:03:24 +0800456 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100457
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200458 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
459 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200460 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100461 if (unlikely(!backbone_gw))
462 return;
463
464 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200465 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100466}
467
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100468/**
469 * batadv_bla_answer_request - answer a bla request by sending own claims
470 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200471 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100472 * @vid: the vid where the request came on
473 *
474 * Repeat all of our own claims, and finally send an ANNOUNCE frame
475 * to allow the requester another check if the CRC is correct now.
476 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200477static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
478 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200479 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100480{
Simon Wunderlich23721382012-01-22 20:00:19 +0100481 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200482 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800483 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800484 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100485 int i;
486
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200487 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200488 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100489
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200490 backbone_gw = batadv_backbone_hash_find(bat_priv,
491 primary_if->net_dev->dev_addr,
492 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100493 if (!backbone_gw)
494 return;
495
Sven Eckelmann807736f2012-07-15 22:26:51 +0200496 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100497 for (i = 0; i < hash->size; i++) {
498 head = &hash->table[i];
499
500 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800501 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100502 /* only own claims are interesting */
503 if (claim->backbone_gw != backbone_gw)
504 continue;
505
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200506 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200507 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100508 }
509 rcu_read_unlock();
510 }
511
512 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200513 batadv_bla_send_announce(bat_priv, backbone_gw);
514 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100515}
516
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100517/**
518 * batadv_bla_send_request - send a request to repeat claims
519 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100520 *
521 * When the crc is wrong, ask the backbone gateway for a full table update.
522 * After the request, it will repeat all of his own claims and finally
523 * send an announcement claim with which we can check again.
524 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800525static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100526{
527 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200528 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100529
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200530 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
531 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100532
533 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200534 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200535 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100536
537 /* no local broadcasts should be sent or received, for now. */
538 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200539 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100540 atomic_set(&backbone_gw->request_sent, 1);
541 }
542}
543
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100544/**
545 * batadv_bla_send_announce
546 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100547 * @backbone_gw: our backbone gateway which should be announced
548 *
549 * This function sends an announcement. It is called from multiple
550 * places.
551 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200552static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800553 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100554{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200555 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100556 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100557
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200558 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200559 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100560 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200561 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100562 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100563
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200564 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200565 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100566}
567
Ben Hutchings2c530402012-07-10 10:55:09 +0000568/**
569 * batadv_bla_add_claim - Adds a claim in the claim hash
570 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100571 * @mac: the mac address of the claim
572 * @vid: the VLAN ID of the frame
573 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100574 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200575static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200576 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800577 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100578{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800579 struct batadv_bla_claim *claim;
580 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100581 int hash_added;
582
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100583 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100584 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200585 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100586
587 /* create a new claim entry if it does not exist yet. */
588 if (!claim) {
589 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
590 if (!claim)
591 return;
592
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100593 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100594 claim->vid = vid;
595 claim->lasttime = jiffies;
596 claim->backbone_gw = backbone_gw;
597
598 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200599 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200600 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200601 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200602 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200603 batadv_compare_claim,
604 batadv_choose_claim, claim,
605 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100606
607 if (unlikely(hash_added != 0)) {
608 /* only local changes happened. */
609 kfree(claim);
610 return;
611 }
612 } else {
613 claim->lasttime = jiffies;
614 if (claim->backbone_gw == backbone_gw)
615 /* no need to register a new backbone */
616 goto claim_free_ref;
617
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200618 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200619 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200620 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100621
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200622 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200623 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200624 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200625 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100626 }
627 /* set (new) backbone gw */
628 atomic_inc(&backbone_gw->refcount);
629 claim->backbone_gw = backbone_gw;
630
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200631 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100632 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200633 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100634 backbone_gw->lasttime = jiffies;
635
636claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200637 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100638}
639
640/* Delete a claim from the claim hash which has the
641 * given mac address and vid.
642 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200643static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200644 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100645{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800646 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100647
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100648 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100649 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200650 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100651 if (!claim)
652 return;
653
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200654 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200655 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100656
Sven Eckelmann807736f2012-07-15 22:26:51 +0200657 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200658 batadv_choose_claim, claim);
659 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100660
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200661 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100662 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200663 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100664
665 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200666 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100667}
668
669/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200670static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
671 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100672{
Marek Lindnerbae98772012-12-25 17:03:24 +0800673 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200674 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100675
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200676 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100677 return 0;
678
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200679 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
680 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100681
682 if (unlikely(!backbone_gw))
683 return 1;
684
Simon Wunderlich23721382012-01-22 20:00:19 +0100685 /* handle as ANNOUNCE frame */
686 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100687 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100688
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200689 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100690 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200691 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100692
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200693 spin_lock_bh(&backbone_gw->crc_lock);
694 backbone_crc = backbone_gw->crc;
695 spin_unlock_bh(&backbone_gw->crc_lock);
696
697 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200698 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100699 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200700 backbone_gw->orig,
701 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200702 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100703
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200704 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100705 } else {
706 /* if we have sent a request and the crc was OK,
707 * we can allow traffic again.
708 */
709 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200710 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100711 atomic_set(&backbone_gw->request_sent, 0);
712 }
713 }
714
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200715 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100716 return 1;
717}
718
719/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200720static int batadv_handle_request(struct batadv_priv *bat_priv,
721 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200722 u8 *backbone_addr, struct ethhdr *ethhdr,
723 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100724{
725 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200726 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100727 return 0;
728
729 /* sanity check, this should not happen on a normal switch,
730 * we ignore it in this case.
731 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200732 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100733 return 1;
734
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200735 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200736 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200737 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100738
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200739 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100740 return 1;
741}
742
743/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
745 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200746 u8 *backbone_addr, u8 *claim_addr,
747 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100748{
Marek Lindnerbae98772012-12-25 17:03:24 +0800749 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100750
751 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200752 if (primary_if && batadv_compare_eth(backbone_addr,
753 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200754 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200755 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100756
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200757 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100758
759 if (!backbone_gw)
760 return 1;
761
762 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200763 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200764 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200765 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100766
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200767 batadv_bla_del_claim(bat_priv, claim_addr, vid);
768 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100769 return 1;
770}
771
772/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200773static int batadv_handle_claim(struct batadv_priv *bat_priv,
774 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200775 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200776 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100777{
Marek Lindnerbae98772012-12-25 17:03:24 +0800778 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100779
780 /* register the gateway if not yet available, and add the claim. */
781
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200782 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
783 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100784
785 if (unlikely(!backbone_gw))
786 return 1;
787
788 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200789 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200790 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200791 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200792 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100793
794 /* TODO: we could call something like tt_local_del() here. */
795
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200796 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100797 return 1;
798}
799
Ben Hutchings2c530402012-07-10 10:55:09 +0000800/**
801 * batadv_check_claim_group
802 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200803 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100804 * @hw_src: the Hardware source in the ARP Header
805 * @hw_dst: the Hardware destination in the ARP Header
806 * @ethhdr: pointer to the Ethernet header of the claim frame
807 *
808 * checks if it is a claim packet and if its on the same group.
809 * This function also applies the group ID of the sender
810 * if it is in the same mesh.
811 *
812 * returns:
813 * 2 - if it is a claim packet and on the same group
814 * 1 - if is a claim packet from another group
815 * 0 - if it is not a claim packet
816 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200817static int batadv_check_claim_group(struct batadv_priv *bat_priv,
818 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200819 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200820 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100821{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200822 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200823 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200824 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100825
Sven Eckelmann96412692012-06-05 22:31:30 +0200826 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200827 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100828
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100829 /* if announcement packet, use the source,
830 * otherwise assume it is in the hw_src
831 */
832 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200833 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100834 backbone_addr = hw_src;
835 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200836 case BATADV_CLAIM_TYPE_REQUEST:
837 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200838 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100839 backbone_addr = ethhdr->h_source;
840 break;
841 default:
842 return 0;
843 }
844
845 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200846 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100847 return 0;
848
849 /* if its already the same group, it is fine. */
850 if (bla_dst->group == bla_dst_own->group)
851 return 2;
852
853 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200854 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100855
856 /* dont accept claims from gateways which are not in
857 * the same mesh or group.
858 */
859 if (!orig_node)
860 return 1;
861
862 /* if our mesh friends mac is bigger, use it for ourselves. */
863 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200864 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100865 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200866 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100867 bla_dst_own->group = bla_dst->group;
868 }
869
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200870 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100871
872 return 2;
873}
874
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100875/**
876 * batadv_bla_process_claim
877 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200878 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100879 * @skb: the frame to be checked
880 *
881 * Check if this is a claim frame, and process it accordingly.
882 *
883 * returns 1 if it was a claim frame, otherwise return 0 to
884 * tell the callee that it can use the frame on its own.
885 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200886static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
887 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200888 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100889{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200890 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200891 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200892 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200893 struct ethhdr *ethhdr;
894 struct arphdr *arphdr;
895 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200896 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200897 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100898 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100899 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100900
Antonio Quartullic018ad32013-06-04 12:11:39 +0200901 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200902 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100903
Antonio Quartullic018ad32013-06-04 12:11:39 +0200904 proto = ethhdr->h_proto;
905 headlen = ETH_HLEN;
906 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200907 /* Traverse the VLAN/Ethertypes.
908 *
909 * At this point it is known that the first protocol is a VLAN
910 * header, so start checking at the encapsulated protocol.
911 *
912 * The depth of the VLAN headers is recorded to drop BLA claim
913 * frames encapsulated into multiple VLAN headers (QinQ).
914 */
915 do {
916 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
917 &vhdr_buf);
918 if (!vhdr)
919 return 0;
920
921 proto = vhdr->h_vlan_encapsulated_proto;
922 headlen += VLAN_HLEN;
923 vlan_depth++;
924 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100925 }
926
Antonio Quartulli293e9332013-05-19 12:55:16 +0200927 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +0100928 return 0; /* not a claim frame */
929
930 /* this must be a ARP frame. check if it is a claim. */
931
932 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
933 return 0;
934
935 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200936 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200937 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +0100938
939 /* Check whether the ARP frame carries a valid
940 * IP information
941 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100942 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
943 return 0;
944 if (arphdr->ar_pro != htons(ETH_P_IP))
945 return 0;
946 if (arphdr->ar_hln != ETH_ALEN)
947 return 0;
948 if (arphdr->ar_pln != 4)
949 return 0;
950
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200951 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100952 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200953 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200954 bla_dst_own = &bat_priv->bla.claim_dest;
955
956 /* check if it is a claim frame in general */
957 if (memcmp(bla_dst->magic, bla_dst_own->magic,
958 sizeof(bla_dst->magic)) != 0)
959 return 0;
960
961 /* check if there is a claim frame encapsulated deeper in (QinQ) and
962 * drop that, as this is not supported by BLA but should also not be
963 * sent via the mesh.
964 */
965 if (vlan_depth > 1)
966 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100967
968 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200969 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
970 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100971 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200972 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200973 "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 +0200974 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
975 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100976
977 if (ret < 2)
978 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100979
980 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200981 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100982
983 /* check for the different types of claim frames ... */
984 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200985 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200986 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
987 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100988 return 1;
989 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200990 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200991 if (batadv_handle_unclaim(bat_priv, primary_if,
992 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100993 return 1;
994 break;
995
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200996 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200997 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
998 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100999 return 1;
1000 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001001 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001002 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1003 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001004 return 1;
1005 break;
1006 }
1007
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001008 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001009 "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 +02001010 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001011 return 1;
1012}
1013
1014/* Check when we last heard from other nodes, and remove them in case of
1015 * a time out, or clean all backbone gws if now is set.
1016 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001017static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001018{
Marek Lindnerbae98772012-12-25 17:03:24 +08001019 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001020 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001021 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001022 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001023 spinlock_t *list_lock; /* protects write access to the hash lists */
1024 int i;
1025
Sven Eckelmann807736f2012-07-15 22:26:51 +02001026 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001027 if (!hash)
1028 return;
1029
1030 for (i = 0; i < hash->size; i++) {
1031 head = &hash->table[i];
1032 list_lock = &hash->list_locks[i];
1033
1034 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001035 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001036 head, hash_entry) {
1037 if (now)
1038 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001039 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001040 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001041 continue;
1042
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001043 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001044 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1045 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001046
1047purge_now:
1048 /* don't wait for the pending request anymore */
1049 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001050 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001051
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001052 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001053
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001055 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001056 }
1057 spin_unlock_bh(list_lock);
1058 }
1059}
1060
Ben Hutchings2c530402012-07-10 10:55:09 +00001061/**
1062 * batadv_bla_purge_claims
1063 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001064 * @primary_if: the selected primary interface, may be NULL if now is set
1065 * @now: whether the whole hash shall be wiped now
1066 *
1067 * Check when we heard last time from our own claims, and remove them in case of
1068 * a time out, or clean all claims if now is set
1069 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001070static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1071 struct batadv_hard_iface *primary_if,
1072 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001073{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001074 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001075 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001076 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001077 int i;
1078
Sven Eckelmann807736f2012-07-15 22:26:51 +02001079 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001080 if (!hash)
1081 return;
1082
1083 for (i = 0; i < hash->size; i++) {
1084 head = &hash->table[i];
1085
1086 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001088 if (now)
1089 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001090 if (!batadv_compare_eth(claim->backbone_gw->orig,
1091 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001092 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001093 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001094 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001095 continue;
1096
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001097 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001098 "bla_purge_claims(): %pM, vid %d, time out\n",
1099 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001100
1101purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001102 batadv_handle_unclaim(bat_priv, primary_if,
1103 claim->backbone_gw->orig,
1104 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001105 }
1106 rcu_read_unlock();
1107 }
1108}
1109
Ben Hutchings2c530402012-07-10 10:55:09 +00001110/**
1111 * batadv_bla_update_orig_address
1112 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001113 * @primary_if: the new selected primary_if
1114 * @oldif: the old primary interface, may be NULL
1115 *
1116 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001117 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001118void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1119 struct batadv_hard_iface *primary_if,
1120 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001121{
Marek Lindnerbae98772012-12-25 17:03:24 +08001122 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001123 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001124 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001125 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001126 int i;
1127
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001128 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001129 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1130 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001131
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001132 /* purge everything when bridge loop avoidance is turned off */
1133 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1134 oldif = NULL;
1135
Simon Wunderlich23721382012-01-22 20:00:19 +01001136 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001137 batadv_bla_purge_claims(bat_priv, NULL, 1);
1138 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001139 return;
1140 }
1141
Sven Eckelmann807736f2012-07-15 22:26:51 +02001142 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001143 if (!hash)
1144 return;
1145
1146 for (i = 0; i < hash->size; i++) {
1147 head = &hash->table[i];
1148
1149 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001150 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001151 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001152 if (!batadv_compare_eth(backbone_gw->orig,
1153 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001154 continue;
1155
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001156 ether_addr_copy(backbone_gw->orig,
1157 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001158 /* send an announce frame so others will ask for our
1159 * claims and update their tables.
1160 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001161 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001162 }
1163 rcu_read_unlock();
1164 }
1165}
1166
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001167/**
1168 * batadv_bla_status_update - purge bla interfaces if necessary
1169 * @net_dev: the soft interface net device
1170 */
1171void batadv_bla_status_update(struct net_device *net_dev)
1172{
1173 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1174 struct batadv_hard_iface *primary_if;
1175
1176 primary_if = batadv_primary_if_get_selected(bat_priv);
1177 if (!primary_if)
1178 return;
1179
1180 /* this function already purges everything when bla is disabled,
1181 * so just call that one.
1182 */
1183 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1184 batadv_hardif_free_ref(primary_if);
1185}
1186
Simon Wunderlich23721382012-01-22 20:00:19 +01001187/* periodic work to do:
1188 * * purge structures when they are too old
1189 * * send announcements
1190 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001191static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001192{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001193 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001194 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001195 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001196 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001197 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001198 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001200 int i;
1201
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001202 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001203 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1204 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001205 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001206 if (!primary_if)
1207 goto out;
1208
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001209 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1210 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001211
1212 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1213 goto out;
1214
Sven Eckelmann807736f2012-07-15 22:26:51 +02001215 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001216 if (!hash)
1217 goto out;
1218
1219 for (i = 0; i < hash->size; i++) {
1220 head = &hash->table[i];
1221
1222 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001223 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001224 if (!batadv_compare_eth(backbone_gw->orig,
1225 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001226 continue;
1227
1228 backbone_gw->lasttime = jiffies;
1229
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001230 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001231
1232 /* request_sent is only set after creation to avoid
1233 * problems when we are not yet known as backbone gw
1234 * in the backbone.
1235 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001236 * We can reset this now after we waited some periods
1237 * to give bridge forward delays and bla group forming
1238 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001239 */
1240
1241 if (atomic_read(&backbone_gw->request_sent) == 0)
1242 continue;
1243
Simon Wunderlich28709872012-09-13 18:18:46 +02001244 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1245 continue;
1246
Simon Wunderlichd807f272012-09-09 22:27:57 +02001247 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1248 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001249 }
1250 rcu_read_unlock();
1251 }
1252out:
1253 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001254 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001255
Antonio Quartulli72414442012-12-25 13:14:37 +01001256 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1257 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001258}
1259
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001260/* The hash for claim and backbone hash receive the same key because they
1261 * are getting initialized by hash_new with the same key. Reinitializing
1262 * them with to different keys to allow nested locking without generating
1263 * lockdep warnings
1264 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001265static struct lock_class_key batadv_claim_hash_lock_class_key;
1266static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001267
Simon Wunderlich23721382012-01-22 20:00:19 +01001268/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001269int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001270{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001271 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001272 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001273 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001274 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001275 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001276
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001277 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1278
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001279 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001280
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001281 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001282 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1283 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001284 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001285 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001286 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1287 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001288 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001289 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001290 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001291 }
1292
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001293 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001294 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001295 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001296 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1297 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001298
Sven Eckelmann807736f2012-07-15 22:26:51 +02001299 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001300 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001301
Sven Eckelmann807736f2012-07-15 22:26:51 +02001302 bat_priv->bla.claim_hash = batadv_hash_new(128);
1303 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001304
Sven Eckelmann807736f2012-07-15 22:26:51 +02001305 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001306 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001307
Sven Eckelmann807736f2012-07-15 22:26:51 +02001308 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001309 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001310 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001311 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001312
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001313 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001314
Antonio Quartulli72414442012-12-25 13:14:37 +01001315 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1316
1317 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1318 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001319 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001320}
1321
Ben Hutchings2c530402012-07-10 10:55:09 +00001322/**
1323 * batadv_bla_check_bcast_duplist
1324 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001325 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001326 *
1327 * check if it is on our broadcast list. Another gateway might
1328 * have sent the same packet because it is connected to the same backbone,
1329 * so we have to remove this duplicate.
1330 *
1331 * This is performed by checking the CRC, which will tell us
1332 * with a good chance that it is the same packet. If it is furthermore
1333 * sent by another host, drop it. We allow equal packets from
1334 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001335 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001336int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001337 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001338{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001339 int i, curr, ret = 0;
1340 __be32 crc;
1341 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001342 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001343
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001344 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001345
1346 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001347 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001348
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001349 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1350
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001351 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001352 curr = (bat_priv->bla.bcast_duplist_curr + i);
1353 curr %= BATADV_DUPLIST_SIZE;
1354 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001355
1356 /* we can stop searching if the entry is too old ;
1357 * later entries will be even older
1358 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001359 if (batadv_has_timed_out(entry->entrytime,
1360 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001361 break;
1362
1363 if (entry->crc != crc)
1364 continue;
1365
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001366 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001367 continue;
1368
1369 /* this entry seems to match: same crc, not too old,
1370 * and from another gw. therefore return 1 to forbid it.
1371 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001372 ret = 1;
1373 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001374 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001375 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001376 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001377 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001378 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001379 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001380 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001381 entry->crc = crc;
1382 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001383 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001384 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001385
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001386out:
1387 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1388
1389 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001390}
1391
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001392/**
1393 * batadv_bla_is_backbone_gw_orig
1394 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001395 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001396 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001397 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001398 * Check if the originator is a gateway for the VLAN identified by vid.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001399 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001400 * Returns true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001401 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001402bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001403 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001404{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001405 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001406 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001407 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001408 int i;
1409
1410 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001411 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001412
1413 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001414 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001415
1416 for (i = 0; i < hash->size; i++) {
1417 head = &hash->table[i];
1418
1419 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001420 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001421 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1422 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001423 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001424 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001425 }
1426 }
1427 rcu_read_unlock();
1428 }
1429
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001430 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001431}
1432
Ben Hutchings2c530402012-07-10 10:55:09 +00001433/**
1434 * batadv_bla_is_backbone_gw
1435 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001436 * @orig_node: the orig_node of the frame
1437 * @hdr_size: maximum length of the frame
1438 *
1439 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1440 * if the orig_node is also a gateway on the soft interface, otherwise it
1441 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001442 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001443int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001444 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001445{
Marek Lindnerbae98772012-12-25 17:03:24 +08001446 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001447 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001448
1449 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1450 return 0;
1451
1452 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001453 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001454 return 0;
1455
Antonio Quartullic018ad32013-06-04 12:11:39 +02001456 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001457
1458 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001459 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1460 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001461 if (!backbone_gw)
1462 return 0;
1463
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001464 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001465 return 1;
1466}
1467
1468/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001469void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001470{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001471 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001472
Sven Eckelmann807736f2012-07-15 22:26:51 +02001473 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001474 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001475
Sven Eckelmann807736f2012-07-15 22:26:51 +02001476 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001477 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001478 batadv_hash_destroy(bat_priv->bla.claim_hash);
1479 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001480 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001481 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001482 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1484 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001485 }
1486 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001487 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001488}
1489
Ben Hutchings2c530402012-07-10 10:55:09 +00001490/**
1491 * batadv_bla_rx
1492 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001493 * @skb: the frame to be checked
1494 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001495 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001496 *
1497 * bla_rx avoidance checks if:
1498 * * we have to race for a claim
1499 * * if the frame is allowed on the LAN
1500 *
1501 * in these cases, the skb is further handled by this function and
1502 * returns 1, otherwise it returns 0 and the caller shall further
1503 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001504 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001505int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1506 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001507{
1508 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001509 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001510 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001511 int ret;
1512
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001513 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001514
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001515 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001516 if (!primary_if)
1517 goto handled;
1518
1519 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1520 goto allow;
1521
Sven Eckelmann807736f2012-07-15 22:26:51 +02001522 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001523 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001524 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001525 goto handled;
1526
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001527 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001528 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001529 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001530
1531 if (!claim) {
1532 /* possible optimization: race for a claim */
1533 /* No claim exists yet, claim it for us!
1534 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001535 batadv_handle_claim(bat_priv, primary_if,
1536 primary_if->net_dev->dev_addr,
1537 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001538 goto allow;
1539 }
1540
1541 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001542 if (batadv_compare_eth(claim->backbone_gw->orig,
1543 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001544 /* ... allow it in any case */
1545 claim->lasttime = jiffies;
1546 goto allow;
1547 }
1548
1549 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001550 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1551 /* ... drop it. the responsible gateway is in charge.
1552 *
1553 * We need to check is_bcast because with the gateway
1554 * feature, broadcasts (like DHCP requests) may be sent
1555 * using a unicast packet type.
1556 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001557 goto handled;
1558 } else {
1559 /* seems the client considers us as its best gateway.
1560 * send a claim and update the claim table
1561 * immediately.
1562 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001563 batadv_handle_claim(bat_priv, primary_if,
1564 primary_if->net_dev->dev_addr,
1565 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001566 goto allow;
1567 }
1568allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001569 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001570 ret = 0;
1571 goto out;
1572
1573handled:
1574 kfree_skb(skb);
1575 ret = 1;
1576
1577out:
1578 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001579 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001580 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001581 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001582 return ret;
1583}
1584
Ben Hutchings2c530402012-07-10 10:55:09 +00001585/**
1586 * batadv_bla_tx
1587 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001588 * @skb: the frame to be checked
1589 * @vid: the VLAN ID of the frame
1590 *
1591 * bla_tx checks if:
1592 * * a claim was received which has to be processed
1593 * * the frame is allowed on the mesh
1594 *
1595 * in these cases, the skb is further handled by this function and
1596 * returns 1, otherwise it returns 0 and the caller shall further
1597 * process the skb.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001598 *
1599 * This call might reallocate skb data.
Simon Wunderlich23721382012-01-22 20:00:19 +01001600 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001601int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1602 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001603{
1604 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001605 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001606 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001607 int ret = 0;
1608
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001609 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001610 if (!primary_if)
1611 goto out;
1612
1613 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1614 goto allow;
1615
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001616 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001617 goto handled;
1618
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001619 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001620
Sven Eckelmann807736f2012-07-15 22:26:51 +02001621 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001622 /* don't allow broadcasts while requests are in flight */
1623 if (is_multicast_ether_addr(ethhdr->h_dest))
1624 goto handled;
1625
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001626 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001627 search_claim.vid = vid;
1628
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001629 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001630
1631 /* if no claim exists, allow it. */
1632 if (!claim)
1633 goto allow;
1634
1635 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001636 if (batadv_compare_eth(claim->backbone_gw->orig,
1637 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001638 /* if yes, the client has roamed and we have
1639 * to unclaim it.
1640 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001641 batadv_handle_unclaim(bat_priv, primary_if,
1642 primary_if->net_dev->dev_addr,
1643 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001644 goto allow;
1645 }
1646
1647 /* check if it is a multicast/broadcast frame */
1648 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1649 /* drop it. the responsible gateway has forwarded it into
1650 * the backbone network.
1651 */
1652 goto handled;
1653 } else {
1654 /* we must allow it. at least if we are
1655 * responsible for the DESTINATION.
1656 */
1657 goto allow;
1658 }
1659allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001660 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001661 ret = 0;
1662 goto out;
1663handled:
1664 ret = 1;
1665out:
1666 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001667 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001668 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001669 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001670 return ret;
1671}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001672
Sven Eckelmann08adf152012-05-12 13:38:47 +02001673int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001674{
1675 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001676 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001677 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001678 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001679 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001680 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001681 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001682 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001683 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001684 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001685
Marek Lindner30da63a2012-08-03 17:15:46 +02001686 primary_if = batadv_seq_print_text_primary_if_get(seq);
1687 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001688 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001689
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001690 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001691 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001692 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001693 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001694 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001695 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001696 "Client", "VID", "Originator", "CRC");
1697 for (i = 0; i < hash->size; i++) {
1698 head = &hash->table[i];
1699
1700 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001701 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001702 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1703 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001704
1705 spin_lock_bh(&claim->backbone_gw->crc_lock);
1706 backbone_crc = claim->backbone_gw->crc;
1707 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001708 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001709 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001710 claim->backbone_gw->orig,
1711 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001712 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001713 }
1714 rcu_read_unlock();
1715 }
1716out:
1717 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001718 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001719 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001720}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001721
1722int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1723{
1724 struct net_device *net_dev = (struct net_device *)seq->private;
1725 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001726 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001727 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001728 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001729 struct hlist_head *head;
1730 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001731 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001732 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001733 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001734 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001735
Marek Lindner30da63a2012-08-03 17:15:46 +02001736 primary_if = batadv_seq_print_text_primary_if_get(seq);
1737 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001738 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001739
1740 primary_addr = primary_if->net_dev->dev_addr;
1741 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001742 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001743 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001744 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001745 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001746 "Originator", "VID", "last seen", "CRC");
1747 for (i = 0; i < hash->size; i++) {
1748 head = &hash->table[i];
1749
1750 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001751 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001752 msecs = jiffies_to_msecs(jiffies -
1753 backbone_gw->lasttime);
1754 secs = msecs / 1000;
1755 msecs = msecs % 1000;
1756
1757 is_own = batadv_compare_eth(backbone_gw->orig,
1758 primary_addr);
1759 if (is_own)
1760 continue;
1761
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001762 spin_lock_bh(&backbone_gw->crc_lock);
1763 backbone_crc = backbone_gw->crc;
1764 spin_unlock_bh(&backbone_gw->crc_lock);
1765
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001766 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001767 backbone_gw->orig,
1768 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001769 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001770 }
1771 rcu_read_unlock();
1772 }
1773out:
1774 if (primary_if)
1775 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001776 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001777}