]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/batman-adv/bridge_loop_avoidance.c
Merge tag 'docs-for-linus' of git://git.lwn.net/linux
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / bridge_loop_avoidance.c
CommitLineData
0046b040 1/* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
23721387
SW
2 *
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
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23721387
SW
16 */
17
23721387 18#include "bridge_loop_avoidance.h"
1e2c2a4f 19#include "main.h"
23721387 20
1e2c2a4f
SE
21#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/compiler.h>
23721387 24#include <linux/crc16.h>
1e2c2a4f
SE
25#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
23721387 28#include <linux/if_arp.h>
1e2c2a4f 29#include <linux/if_ether.h>
23721387 30#include <linux/if_vlan.h>
1e2c2a4f
SE
31#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
06e56ded 34#include <linux/kref.h>
1e2c2a4f
SE
35#include <linux/list.h>
36#include <linux/lockdep.h>
37#include <linux/netdevice.h>
38#include <linux/rculist.h>
39#include <linux/rcupdate.h>
40#include <linux/seq_file.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/stddef.h>
45#include <linux/string.h>
46#include <linux/workqueue.h>
47#include <net/arp.h>
48
49#include "hard-interface.h"
50#include "hash.h"
51#include "originator.h"
52#include "packet.h"
cd9c7bfb 53#include "sysfs.h"
1e2c2a4f 54#include "translation-table.h"
23721387 55
6b5e971a 56static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
23721387 57
3b300de3 58static void batadv_bla_periodic_work(struct work_struct *work);
bae98774
ML
59static void
60batadv_bla_send_announce(struct batadv_priv *bat_priv,
61 struct batadv_bla_backbone_gw *backbone_gw);
23721387 62
62fe710f 63/**
04e14be6
SW
64 * batadv_choose_claim - choose the right bucket for a claim.
65 * @data: data to hash
66 * @size: size of the hash table
62fe710f 67 *
04e14be6 68 * Return: the hash index of the claim
62fe710f 69 */
6b5e971a 70static inline u32 batadv_choose_claim(const void *data, u32 size)
23721387 71{
712bbfe4 72 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
6b5e971a 73 u32 hash = 0;
23721387 74
36fd61cb
SE
75 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
76 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
23721387
SW
77
78 return hash % size;
79}
80
62fe710f 81/**
04e14be6
SW
82 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
83 * @data: data to hash
84 * @size: size of the hash table
62fe710f 85 *
04e14be6 86 * Return: the hash index of the backbone gateway
62fe710f 87 */
6b5e971a 88static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
23721387 89{
712bbfe4 90 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
6b5e971a 91 u32 hash = 0;
23721387 92
36fd61cb
SE
93 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
94 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
23721387
SW
95
96 return hash % size;
97}
98
04e14be6
SW
99/**
100 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
101 * @node: list node of the first entry to compare
102 * @data2: pointer to the second backbone gateway
103 *
4b426b10 104 * Return: true if the backbones have the same data, false otherwise
04e14be6 105 */
4b426b10
SE
106static bool batadv_compare_backbone_gw(const struct hlist_node *node,
107 const void *data2)
23721387 108{
bae98774 109 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
23721387 110 hash_entry);
4f248cff
SE
111 const struct batadv_bla_backbone_gw *gw1 = data1;
112 const struct batadv_bla_backbone_gw *gw2 = data2;
23721387 113
c76d1525 114 if (!batadv_compare_eth(gw1->orig, gw2->orig))
4b426b10 115 return false;
c76d1525
SW
116
117 if (gw1->vid != gw2->vid)
4b426b10 118 return false;
c76d1525 119
4b426b10 120 return true;
23721387
SW
121}
122
04e14be6 123/**
98a5b1d8 124 * batadv_compare_claim - compare address and vid of two claims
04e14be6
SW
125 * @node: list node of the first entry to compare
126 * @data2: pointer to the second claims
127 *
4b426b10 128 * Return: true if the claim have the same data, 0 otherwise
04e14be6 129 */
4b426b10
SE
130static bool batadv_compare_claim(const struct hlist_node *node,
131 const void *data2)
23721387 132{
712bbfe4 133 const void *data1 = container_of(node, struct batadv_bla_claim,
23721387 134 hash_entry);
4f248cff
SE
135 const struct batadv_bla_claim *cl1 = data1;
136 const struct batadv_bla_claim *cl2 = data2;
c76d1525
SW
137
138 if (!batadv_compare_eth(cl1->addr, cl2->addr))
4b426b10 139 return false;
c76d1525
SW
140
141 if (cl1->vid != cl2->vid)
4b426b10 142 return false;
23721387 143
4b426b10 144 return true;
23721387
SW
145}
146
04e14be6 147/**
06e56ded
SE
148 * batadv_backbone_gw_release - release backbone gw from lists and queue for
149 * free after rcu grace period
150 * @ref: kref pointer of the backbone gw
151 */
152static void batadv_backbone_gw_release(struct kref *ref)
153{
154 struct batadv_bla_backbone_gw *backbone_gw;
155
156 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
157 refcount);
158
159 kfree_rcu(backbone_gw, rcu);
160}
161
162/**
c8b86c12
SE
163 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
164 * release it
04e14be6
SW
165 * @backbone_gw: backbone gateway to be free'd
166 */
c8b86c12 167static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
23721387 168{
06e56ded 169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
23721387
SW
170}
171
04e14be6
SW
172/**
173 * batadv_claim_release - release claim from lists and queue for free after rcu
174 * grace period
175 * @ref: kref pointer of the claim
176 */
71b7e3d3 177static void batadv_claim_release(struct kref *ref)
23721387 178{
71b7e3d3 179 struct batadv_bla_claim *claim;
3db0decf 180 struct batadv_bla_backbone_gw *old_backbone_gw;
71b7e3d3
SE
181
182 claim = container_of(ref, struct batadv_bla_claim, refcount);
183
3db0decf
SE
184 spin_lock_bh(&claim->backbone_lock);
185 old_backbone_gw = claim->backbone_gw;
186 claim->backbone_gw = NULL;
187 spin_unlock_bh(&claim->backbone_lock);
188
189 spin_lock_bh(&old_backbone_gw->crc_lock);
190 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
191 spin_unlock_bh(&old_backbone_gw->crc_lock);
192
193 batadv_backbone_gw_put(old_backbone_gw);
194
63b39927 195 kfree_rcu(claim, rcu);
23721387
SW
196}
197
04e14be6 198/**
321e3e08 199 * batadv_claim_put - decrement the claim refcounter and possibly
ec9b83ca 200 * release it
04e14be6
SW
201 * @claim: claim to be free'd
202 */
321e3e08 203static void batadv_claim_put(struct batadv_bla_claim *claim)
23721387 204{
71b7e3d3 205 kref_put(&claim->refcount, batadv_claim_release);
23721387
SW
206}
207
1b371d13 208/**
04e14be6 209 * batadv_claim_hash_find - looks for a claim in the claim hash
1b371d13 210 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
211 * @data: search data (may be local/static data)
212 *
62fe710f 213 * Return: claim if found or NULL otherwise.
23721387 214 */
6fc77a54
SE
215static struct batadv_bla_claim *
216batadv_claim_hash_find(struct batadv_priv *bat_priv,
217 struct batadv_bla_claim *data)
23721387 218{
807736f6 219 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
23721387 220 struct hlist_head *head;
712bbfe4
ML
221 struct batadv_bla_claim *claim;
222 struct batadv_bla_claim *claim_tmp = NULL;
23721387
SW
223 int index;
224
225 if (!hash)
226 return NULL;
227
3b300de3 228 index = batadv_choose_claim(data, hash->size);
23721387
SW
229 head = &hash->table[index];
230
231 rcu_read_lock();
b67bfe0d 232 hlist_for_each_entry_rcu(claim, head, hash_entry) {
3b300de3 233 if (!batadv_compare_claim(&claim->hash_entry, data))
23721387
SW
234 continue;
235
71b7e3d3 236 if (!kref_get_unless_zero(&claim->refcount))
23721387
SW
237 continue;
238
239 claim_tmp = claim;
240 break;
241 }
242 rcu_read_unlock();
243
244 return claim_tmp;
245}
246
2c53040f 247/**
04e14be6 248 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
2c53040f 249 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
250 * @addr: the address of the originator
251 * @vid: the VLAN ID
252 *
04e14be6 253 * Return: backbone gateway if found or NULL otherwise
23721387 254 */
bae98774 255static struct batadv_bla_backbone_gw *
6b5e971a
SE
256batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
257 unsigned short vid)
23721387 258{
807736f6 259 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
23721387 260 struct hlist_head *head;
bae98774
ML
261 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
262 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
23721387
SW
263 int index;
264
265 if (!hash)
266 return NULL;
267
8fdd0153 268 ether_addr_copy(search_entry.orig, addr);
23721387
SW
269 search_entry.vid = vid;
270
3b300de3 271 index = batadv_choose_backbone_gw(&search_entry, hash->size);
23721387
SW
272 head = &hash->table[index];
273
274 rcu_read_lock();
b67bfe0d 275 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
3b300de3
SE
276 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
277 &search_entry))
23721387
SW
278 continue;
279
06e56ded 280 if (!kref_get_unless_zero(&backbone_gw->refcount))
23721387
SW
281 continue;
282
283 backbone_gw_tmp = backbone_gw;
284 break;
285 }
286 rcu_read_unlock();
287
288 return backbone_gw_tmp;
289}
290
04e14be6
SW
291/**
292 * batadv_bla_del_backbone_claims - delete all claims for a backbone
293 * @backbone_gw: backbone gateway where the claims should be removed
294 */
56303d34 295static void
bae98774 296batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
23721387 297{
5bf74e9c 298 struct batadv_hashtable *hash;
b67bfe0d 299 struct hlist_node *node_tmp;
23721387 300 struct hlist_head *head;
712bbfe4 301 struct batadv_bla_claim *claim;
23721387
SW
302 int i;
303 spinlock_t *list_lock; /* protects write access to the hash lists */
304
807736f6 305 hash = backbone_gw->bat_priv->bla.claim_hash;
23721387
SW
306 if (!hash)
307 return;
308
309 for (i = 0; i < hash->size; i++) {
310 head = &hash->table[i];
311 list_lock = &hash->list_locks[i];
312
313 spin_lock_bh(list_lock);
b67bfe0d 314 hlist_for_each_entry_safe(claim, node_tmp,
23721387 315 head, hash_entry) {
23721387
SW
316 if (claim->backbone_gw != backbone_gw)
317 continue;
318
321e3e08 319 batadv_claim_put(claim);
b67bfe0d 320 hlist_del_rcu(&claim->hash_entry);
23721387
SW
321 }
322 spin_unlock_bh(list_lock);
323 }
324
3f68785e 325 /* all claims gone, initialize CRC */
5a1dd8a4 326 spin_lock_bh(&backbone_gw->crc_lock);
3964f728 327 backbone_gw->crc = BATADV_BLA_CRC_INIT;
5a1dd8a4 328 spin_unlock_bh(&backbone_gw->crc_lock);
23721387
SW
329}
330
2c53040f
BH
331/**
332 * batadv_bla_send_claim - sends a claim frame according to the provided info
333 * @bat_priv: the bat priv with all the soft interface information
e3357189 334 * @mac: the mac address to be announced within the claim
23721387
SW
335 * @vid: the VLAN ID
336 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
23721387 337 */
6b5e971a 338static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
eb2deb6b 339 unsigned short vid, int claimtype)
23721387
SW
340{
341 struct sk_buff *skb;
342 struct ethhdr *ethhdr;
56303d34 343 struct batadv_hard_iface *primary_if;
23721387 344 struct net_device *soft_iface;
6b5e971a 345 u8 *hw_src;
96412690 346 struct batadv_bla_claim_dst local_claim_dest;
3e2f1a1b 347 __be32 zeroip = 0;
23721387 348
e5d89254 349 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
350 if (!primary_if)
351 return;
352
807736f6 353 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
38ef3d1d 354 sizeof(local_claim_dest));
23721387
SW
355 local_claim_dest.type = claimtype;
356
357 soft_iface = primary_if->soft_iface;
358
359 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
360 /* IP DST: 0.0.0.0 */
361 zeroip,
362 primary_if->soft_iface,
363 /* IP SRC: 0.0.0.0 */
364 zeroip,
365 /* Ethernet DST: Broadcast */
366 NULL,
367 /* Ethernet SRC/HW SRC: originator mac */
368 primary_if->net_dev->dev_addr,
99e966fc 369 /* HW DST: FF:43:05:XX:YY:YY
23721387 370 * with XX = claim type
38ef3d1d 371 * and YY:YY = group id
23721387 372 */
6b5e971a 373 (u8 *)&local_claim_dest);
23721387
SW
374
375 if (!skb)
376 goto out;
377
378 ethhdr = (struct ethhdr *)skb->data;
6b5e971a 379 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
23721387
SW
380
381 /* now we pretend that the client would have sent this ... */
382 switch (claimtype) {
3eb8773e 383 case BATADV_CLAIM_TYPE_CLAIM:
23721387
SW
384 /* normal claim frame
385 * set Ethernet SRC to the clients mac
386 */
8fdd0153 387 ether_addr_copy(ethhdr->h_source, mac);
39c75a51 388 batadv_dbg(BATADV_DBG_BLA, bat_priv,
5f80df67
AQ
389 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
390 BATADV_PRINT_VID(vid));
23721387 391 break;
3eb8773e 392 case BATADV_CLAIM_TYPE_UNCLAIM:
23721387
SW
393 /* unclaim frame
394 * set HW SRC to the clients mac
395 */
8fdd0153 396 ether_addr_copy(hw_src, mac);
39c75a51 397 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 398 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
5f80df67 399 BATADV_PRINT_VID(vid));
23721387 400 break;
acd34afa 401 case BATADV_CLAIM_TYPE_ANNOUNCE:
23721387
SW
402 /* announcement frame
403 * set HW SRC to the special mac containg the crc
404 */
8fdd0153 405 ether_addr_copy(hw_src, mac);
39c75a51 406 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 407 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
5f80df67 408 ethhdr->h_source, BATADV_PRINT_VID(vid));
23721387 409 break;
acd34afa 410 case BATADV_CLAIM_TYPE_REQUEST:
23721387 411 /* request frame
99e966fc
SW
412 * set HW SRC and header destination to the receiving backbone
413 * gws mac
23721387 414 */
8fdd0153
AQ
415 ether_addr_copy(hw_src, mac);
416 ether_addr_copy(ethhdr->h_dest, mac);
39c75a51 417 batadv_dbg(BATADV_DBG_BLA, bat_priv,
eb2deb6b 418 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
5f80df67
AQ
419 ethhdr->h_source, ethhdr->h_dest,
420 BATADV_PRINT_VID(vid));
23721387 421 break;
cd9c7bfb
SW
422 case BATADV_CLAIM_TYPE_LOOPDETECT:
423 ether_addr_copy(ethhdr->h_source, mac);
424 batadv_dbg(BATADV_DBG_BLA, bat_priv,
425 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
426 ethhdr->h_source, ethhdr->h_dest,
427 BATADV_PRINT_VID(vid));
428
429 break;
23721387
SW
430 }
431
10c78f58 432 if (vid & BATADV_VLAN_HAS_TAG) {
eb2deb6b
AQ
433 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
434 vid & VLAN_VID_MASK);
10c78f58
SE
435 if (!skb)
436 goto out;
437 }
23721387
SW
438
439 skb_reset_mac_header(skb);
440 skb->protocol = eth_type_trans(skb, soft_iface);
1c9b0550
ML
441 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
442 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
443 skb->len + ETH_HLEN);
23721387
SW
444 soft_iface->last_rx = jiffies;
445
446 netif_rx(skb);
447out:
448 if (primary_if)
82047ad7 449 batadv_hardif_put(primary_if);
23721387
SW
450}
451
cd9c7bfb
SW
452/**
453 * batadv_bla_loopdetect_report - worker for reporting the loop
454 * @work: work queue item
455 *
456 * Throws an uevent, as the loopdetect check function can't do that itself
457 * since the kernel may sleep while throwing uevents.
458 */
459static void batadv_bla_loopdetect_report(struct work_struct *work)
460{
461 struct batadv_bla_backbone_gw *backbone_gw;
462 struct batadv_priv *bat_priv;
463 char vid_str[6] = { '\0' };
464
465 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
466 report_work);
467 bat_priv = backbone_gw->bat_priv;
468
469 batadv_info(bat_priv->soft_iface,
470 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
471 BATADV_PRINT_VID(backbone_gw->vid));
472 snprintf(vid_str, sizeof(vid_str), "%d",
473 BATADV_PRINT_VID(backbone_gw->vid));
474 vid_str[sizeof(vid_str) - 1] = 0;
475
476 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
477 vid_str);
478
479 batadv_backbone_gw_put(backbone_gw);
480}
481
2c53040f 482/**
04e14be6 483 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
2c53040f 484 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
485 * @orig: the mac address of the originator
486 * @vid: the VLAN ID
e3357189 487 * @own_backbone: set if the requested backbone is local
23721387 488 *
04e14be6 489 * Return: the (possibly created) backbone gateway or NULL on error
23721387 490 */
bae98774 491static struct batadv_bla_backbone_gw *
6b5e971a 492batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
eb2deb6b 493 unsigned short vid, bool own_backbone)
23721387 494{
bae98774 495 struct batadv_bla_backbone_gw *entry;
56303d34 496 struct batadv_orig_node *orig_node;
23721387
SW
497 int hash_added;
498
3b300de3 499 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
23721387
SW
500
501 if (entry)
502 return entry;
503
39c75a51 504 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 505 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
5f80df67 506 orig, BATADV_PRINT_VID(vid));
23721387
SW
507
508 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
509 if (!entry)
510 return NULL;
511
512 entry->vid = vid;
513 entry->lasttime = jiffies;
3964f728 514 entry->crc = BATADV_BLA_CRC_INIT;
23721387 515 entry->bat_priv = bat_priv;
5a1dd8a4 516 spin_lock_init(&entry->crc_lock);
23721387 517 atomic_set(&entry->request_sent, 0);
28709878 518 atomic_set(&entry->wait_periods, 0);
8fdd0153 519 ether_addr_copy(entry->orig, orig);
cd9c7bfb 520 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
23721387
SW
521
522 /* one for the hash, one for returning */
06e56ded
SE
523 kref_init(&entry->refcount);
524 kref_get(&entry->refcount);
23721387 525
807736f6 526 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
3b300de3
SE
527 batadv_compare_backbone_gw,
528 batadv_choose_backbone_gw, entry,
529 &entry->hash_entry);
23721387
SW
530
531 if (unlikely(hash_added != 0)) {
532 /* hash failed, free the structure */
533 kfree(entry);
534 return NULL;
535 }
536
95fb130d 537 /* this is a gateway now, remove any TT entry on this VLAN */
da641193 538 orig_node = batadv_orig_hash_find(bat_priv, orig);
20ff9d59 539 if (orig_node) {
95fb130d 540 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
08c36d3e 541 "became a backbone gateway");
5d967310 542 batadv_orig_node_put(orig_node);
20ff9d59 543 }
52aebd6a 544
d807f272 545 if (own_backbone) {
52aebd6a
SW
546 batadv_bla_send_announce(bat_priv, entry);
547
d807f272
SW
548 /* this will be decreased in the worker thread */
549 atomic_inc(&entry->request_sent);
28709878 550 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
d807f272
SW
551 atomic_inc(&bat_priv->bla.num_requests);
552 }
553
23721387
SW
554 return entry;
555}
556
04e14be6
SW
557/**
558 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
559 * @bat_priv: the bat priv with all the soft interface information
560 * @primary_if: the selected primary interface
561 * @vid: VLAN identifier
562 *
563 * update or add the own backbone gw to make sure we announce
23721387
SW
564 * where we receive other backbone gws
565 */
56303d34
SE
566static void
567batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
568 struct batadv_hard_iface *primary_if,
eb2deb6b 569 unsigned short vid)
23721387 570{
bae98774 571 struct batadv_bla_backbone_gw *backbone_gw;
23721387 572
3b300de3
SE
573 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
574 primary_if->net_dev->dev_addr,
52aebd6a 575 vid, true);
23721387
SW
576 if (unlikely(!backbone_gw))
577 return;
578
579 backbone_gw->lasttime = jiffies;
c8b86c12 580 batadv_backbone_gw_put(backbone_gw);
23721387
SW
581}
582
1b371d13
SW
583/**
584 * batadv_bla_answer_request - answer a bla request by sending own claims
585 * @bat_priv: the bat priv with all the soft interface information
e3357189 586 * @primary_if: interface where the request came on
23721387
SW
587 * @vid: the vid where the request came on
588 *
589 * Repeat all of our own claims, and finally send an ANNOUNCE frame
590 * to allow the requester another check if the CRC is correct now.
591 */
56303d34
SE
592static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
593 struct batadv_hard_iface *primary_if,
eb2deb6b 594 unsigned short vid)
23721387 595{
23721387 596 struct hlist_head *head;
5bf74e9c 597 struct batadv_hashtable *hash;
712bbfe4 598 struct batadv_bla_claim *claim;
bae98774 599 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
600 int i;
601
39c75a51 602 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 603 "bla_answer_request(): received a claim request, send all of our own claims again\n");
23721387 604
3b300de3
SE
605 backbone_gw = batadv_backbone_hash_find(bat_priv,
606 primary_if->net_dev->dev_addr,
607 vid);
23721387
SW
608 if (!backbone_gw)
609 return;
610
807736f6 611 hash = bat_priv->bla.claim_hash;
23721387
SW
612 for (i = 0; i < hash->size; i++) {
613 head = &hash->table[i];
614
615 rcu_read_lock();
b67bfe0d 616 hlist_for_each_entry_rcu(claim, head, hash_entry) {
23721387
SW
617 /* only own claims are interesting */
618 if (claim->backbone_gw != backbone_gw)
619 continue;
620
3b300de3 621 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
3eb8773e 622 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
623 }
624 rcu_read_unlock();
625 }
626
627 /* finally, send an announcement frame */
3b300de3 628 batadv_bla_send_announce(bat_priv, backbone_gw);
c8b86c12 629 batadv_backbone_gw_put(backbone_gw);
23721387
SW
630}
631
1b371d13
SW
632/**
633 * batadv_bla_send_request - send a request to repeat claims
634 * @backbone_gw: the backbone gateway from whom we are out of sync
23721387
SW
635 *
636 * When the crc is wrong, ask the backbone gateway for a full table update.
637 * After the request, it will repeat all of his own claims and finally
638 * send an announcement claim with which we can check again.
639 */
bae98774 640static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
23721387
SW
641{
642 /* first, remove all old entries */
3b300de3 643 batadv_bla_del_backbone_claims(backbone_gw);
23721387 644
39c75a51
SE
645 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
646 "Sending REQUEST to %pM\n", backbone_gw->orig);
23721387
SW
647
648 /* send request */
3b300de3 649 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
acd34afa 650 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
23721387
SW
651
652 /* no local broadcasts should be sent or received, for now. */
653 if (!atomic_read(&backbone_gw->request_sent)) {
807736f6 654 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
655 atomic_set(&backbone_gw->request_sent, 1);
656 }
657}
658
1b371d13 659/**
04e14be6 660 * batadv_bla_send_announce - Send an announcement frame
1b371d13 661 * @bat_priv: the bat priv with all the soft interface information
23721387 662 * @backbone_gw: our backbone gateway which should be announced
23721387 663 */
56303d34 664static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
bae98774 665 struct batadv_bla_backbone_gw *backbone_gw)
23721387 666{
6b5e971a 667 u8 mac[ETH_ALEN];
3e2f1a1b 668 __be16 crc;
23721387 669
3b300de3 670 memcpy(mac, batadv_announce_mac, 4);
5a1dd8a4 671 spin_lock_bh(&backbone_gw->crc_lock);
23721387 672 crc = htons(backbone_gw->crc);
5a1dd8a4 673 spin_unlock_bh(&backbone_gw->crc_lock);
1a5852d8 674 memcpy(&mac[4], &crc, 2);
23721387 675
3b300de3 676 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
acd34afa 677 BATADV_CLAIM_TYPE_ANNOUNCE);
23721387
SW
678}
679
2c53040f
BH
680/**
681 * batadv_bla_add_claim - Adds a claim in the claim hash
682 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
683 * @mac: the mac address of the claim
684 * @vid: the VLAN ID of the frame
685 * @backbone_gw: the backbone gateway which claims it
23721387 686 */
56303d34 687static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
6b5e971a 688 const u8 *mac, const unsigned short vid,
bae98774 689 struct batadv_bla_backbone_gw *backbone_gw)
23721387 690{
3db0decf 691 struct batadv_bla_backbone_gw *old_backbone_gw;
712bbfe4
ML
692 struct batadv_bla_claim *claim;
693 struct batadv_bla_claim search_claim;
3db0decf 694 bool remove_crc = false;
23721387
SW
695 int hash_added;
696
8fdd0153 697 ether_addr_copy(search_claim.addr, mac);
23721387 698 search_claim.vid = vid;
3b300de3 699 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
700
701 /* create a new claim entry if it does not exist yet. */
702 if (!claim) {
703 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
704 if (!claim)
705 return;
706
8fdd0153 707 ether_addr_copy(claim->addr, mac);
3db0decf 708 spin_lock_init(&claim->backbone_lock);
23721387
SW
709 claim->vid = vid;
710 claim->lasttime = jiffies;
3db0decf 711 kref_get(&backbone_gw->refcount);
23721387
SW
712 claim->backbone_gw = backbone_gw;
713
71b7e3d3
SE
714 kref_init(&claim->refcount);
715 kref_get(&claim->refcount);
39c75a51 716 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 717 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
5f80df67 718 mac, BATADV_PRINT_VID(vid));
807736f6 719 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
3b300de3
SE
720 batadv_compare_claim,
721 batadv_choose_claim, claim,
722 &claim->hash_entry);
23721387
SW
723
724 if (unlikely(hash_added != 0)) {
725 /* only local changes happened. */
726 kfree(claim);
727 return;
728 }
729 } else {
730 claim->lasttime = jiffies;
731 if (claim->backbone_gw == backbone_gw)
732 /* no need to register a new backbone */
733 goto claim_free_ref;
734
39c75a51 735 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 736 "bla_add_claim(): changing ownership for %pM, vid %d\n",
5f80df67 737 mac, BATADV_PRINT_VID(vid));
23721387 738
3db0decf 739 remove_crc = true;
23721387 740 }
3db0decf
SE
741
742 /* replace backbone_gw atomically and adjust reference counters */
743 spin_lock_bh(&claim->backbone_lock);
744 old_backbone_gw = claim->backbone_gw;
06e56ded 745 kref_get(&backbone_gw->refcount);
23721387 746 claim->backbone_gw = backbone_gw;
3db0decf 747 spin_unlock_bh(&claim->backbone_lock);
23721387 748
3db0decf
SE
749 if (remove_crc) {
750 /* remove claim address from old backbone_gw */
751 spin_lock_bh(&old_backbone_gw->crc_lock);
752 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
753 spin_unlock_bh(&old_backbone_gw->crc_lock);
754 }
755
756 batadv_backbone_gw_put(old_backbone_gw);
757
758 /* add claim address to new backbone_gw */
5a1dd8a4 759 spin_lock_bh(&backbone_gw->crc_lock);
23721387 760 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
5a1dd8a4 761 spin_unlock_bh(&backbone_gw->crc_lock);
23721387
SW
762 backbone_gw->lasttime = jiffies;
763
764claim_free_ref:
321e3e08 765 batadv_claim_put(claim);
23721387
SW
766}
767
3db0decf
SE
768/**
769 * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
770 * claim
771 * @claim: claim whose backbone_gw should be returned
772 *
773 * Return: valid reference to claim::backbone_gw
774 */
775static struct batadv_bla_backbone_gw *
776batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
777{
778 struct batadv_bla_backbone_gw *backbone_gw;
779
780 spin_lock_bh(&claim->backbone_lock);
781 backbone_gw = claim->backbone_gw;
782 kref_get(&backbone_gw->refcount);
783 spin_unlock_bh(&claim->backbone_lock);
784
785 return backbone_gw;
786}
787
04e14be6
SW
788/**
789 * batadv_bla_del_claim - delete a claim from the claim hash
790 * @bat_priv: the bat priv with all the soft interface information
791 * @mac: mac address of the claim to be removed
792 * @vid: VLAN id for the claim to be removed
23721387 793 */
56303d34 794static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
6b5e971a 795 const u8 *mac, const unsigned short vid)
23721387 796{
712bbfe4 797 struct batadv_bla_claim search_claim, *claim;
23721387 798
8fdd0153 799 ether_addr_copy(search_claim.addr, mac);
23721387 800 search_claim.vid = vid;
3b300de3 801 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
802 if (!claim)
803 return;
804
39c75a51 805 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
5f80df67 806 mac, BATADV_PRINT_VID(vid));
23721387 807
807736f6 808 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
3b300de3 809 batadv_choose_claim, claim);
321e3e08 810 batadv_claim_put(claim); /* reference from the hash is gone */
23721387 811
23721387 812 /* don't need the reference from hash_find() anymore */
321e3e08 813 batadv_claim_put(claim);
23721387
SW
814}
815
62fe710f
SE
816/**
817 * batadv_handle_announce - check for ANNOUNCE frame
04e14be6
SW
818 * @bat_priv: the bat priv with all the soft interface information
819 * @an_addr: announcement mac address (ARP Sender HW address)
820 * @backbone_addr: originator address of the sender (Ethernet source MAC)
821 * @vid: the VLAN ID of the frame
62fe710f 822 *
4b426b10 823 * Return: true if handled
62fe710f 824 */
4b426b10
SE
825static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
826 u8 *backbone_addr, unsigned short vid)
23721387 827{
bae98774 828 struct batadv_bla_backbone_gw *backbone_gw;
5a1dd8a4 829 u16 backbone_crc, crc;
23721387 830
3b300de3 831 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
4b426b10 832 return false;
23721387 833
52aebd6a
SW
834 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
835 false);
23721387
SW
836
837 if (unlikely(!backbone_gw))
4b426b10 838 return true;
23721387 839
23721387
SW
840 /* handle as ANNOUNCE frame */
841 backbone_gw->lasttime = jiffies;
3e2f1a1b 842 crc = ntohs(*((__be16 *)(&an_addr[4])));
23721387 843
39c75a51 844 batadv_dbg(BATADV_DBG_BLA, bat_priv,
39a32991 845 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
5f80df67 846 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
23721387 847
5a1dd8a4
SW
848 spin_lock_bh(&backbone_gw->crc_lock);
849 backbone_crc = backbone_gw->crc;
850 spin_unlock_bh(&backbone_gw->crc_lock);
851
852 if (backbone_crc != crc) {
39c75a51 853 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
39a32991 854 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
5f80df67
AQ
855 backbone_gw->orig,
856 BATADV_PRINT_VID(backbone_gw->vid),
5a1dd8a4 857 backbone_crc, crc);
23721387 858
3b300de3 859 batadv_bla_send_request(backbone_gw);
23721387
SW
860 } else {
861 /* if we have sent a request and the crc was OK,
862 * we can allow traffic again.
863 */
864 if (atomic_read(&backbone_gw->request_sent)) {
807736f6 865 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
866 atomic_set(&backbone_gw->request_sent, 0);
867 }
868 }
869
c8b86c12 870 batadv_backbone_gw_put(backbone_gw);
4b426b10 871 return true;
23721387
SW
872}
873
62fe710f
SE
874/**
875 * batadv_handle_request - check for REQUEST frame
04e14be6
SW
876 * @bat_priv: the bat priv with all the soft interface information
877 * @primary_if: the primary hard interface of this batman soft interface
878 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
879 * @ethhdr: ethernet header of a packet
880 * @vid: the VLAN ID of the frame
62fe710f 881 *
4b426b10 882 * Return: true if handled
62fe710f 883 */
4b426b10
SE
884static bool batadv_handle_request(struct batadv_priv *bat_priv,
885 struct batadv_hard_iface *primary_if,
886 u8 *backbone_addr, struct ethhdr *ethhdr,
887 unsigned short vid)
23721387
SW
888{
889 /* check for REQUEST frame */
1eda58bf 890 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
4b426b10 891 return false;
23721387
SW
892
893 /* sanity check, this should not happen on a normal switch,
894 * we ignore it in this case.
895 */
1eda58bf 896 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
4b426b10 897 return true;
23721387 898
39c75a51 899 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 900 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
5f80df67 901 BATADV_PRINT_VID(vid), ethhdr->h_source);
23721387 902
3b300de3 903 batadv_bla_answer_request(bat_priv, primary_if, vid);
4b426b10 904 return true;
23721387
SW
905}
906
62fe710f
SE
907/**
908 * batadv_handle_unclaim - check for UNCLAIM frame
04e14be6
SW
909 * @bat_priv: the bat priv with all the soft interface information
910 * @primary_if: the primary hard interface of this batman soft interface
911 * @backbone_addr: originator address of the backbone (Ethernet source)
912 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
913 * @vid: the VLAN ID of the frame
62fe710f 914 *
4b426b10 915 * Return: true if handled
62fe710f 916 */
4b426b10
SE
917static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
918 struct batadv_hard_iface *primary_if,
919 u8 *backbone_addr, u8 *claim_addr,
920 unsigned short vid)
23721387 921{
bae98774 922 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
923
924 /* unclaim in any case if it is our own */
1eda58bf
SE
925 if (primary_if && batadv_compare_eth(backbone_addr,
926 primary_if->net_dev->dev_addr))
3b300de3 927 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 928 BATADV_CLAIM_TYPE_UNCLAIM);
23721387 929
3b300de3 930 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
23721387
SW
931
932 if (!backbone_gw)
4b426b10 933 return true;
23721387
SW
934
935 /* this must be an UNCLAIM frame */
39c75a51 936 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 937 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
5f80df67 938 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
23721387 939
3b300de3 940 batadv_bla_del_claim(bat_priv, claim_addr, vid);
c8b86c12 941 batadv_backbone_gw_put(backbone_gw);
4b426b10 942 return true;
23721387
SW
943}
944
62fe710f
SE
945/**
946 * batadv_handle_claim - check for CLAIM frame
04e14be6
SW
947 * @bat_priv: the bat priv with all the soft interface information
948 * @primary_if: the primary hard interface of this batman soft interface
949 * @backbone_addr: originator address of the backbone (Ethernet Source)
950 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
951 * @vid: the VLAN ID of the frame
62fe710f 952 *
4b426b10 953 * Return: true if handled
62fe710f 954 */
4b426b10
SE
955static bool batadv_handle_claim(struct batadv_priv *bat_priv,
956 struct batadv_hard_iface *primary_if,
957 u8 *backbone_addr, u8 *claim_addr,
958 unsigned short vid)
23721387 959{
bae98774 960 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
961
962 /* register the gateway if not yet available, and add the claim. */
963
52aebd6a
SW
964 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
965 false);
23721387
SW
966
967 if (unlikely(!backbone_gw))
4b426b10 968 return true;
23721387
SW
969
970 /* this must be a CLAIM frame */
3b300de3 971 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
1eda58bf 972 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
3b300de3 973 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 974 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
975
976 /* TODO: we could call something like tt_local_del() here. */
977
c8b86c12 978 batadv_backbone_gw_put(backbone_gw);
4b426b10 979 return true;
23721387
SW
980}
981
2c53040f 982/**
04e14be6 983 * batadv_check_claim_group - check for claim group membership
2c53040f 984 * @bat_priv: the bat priv with all the soft interface information
e3357189 985 * @primary_if: the primary interface of this batman interface
38ef3d1d
SW
986 * @hw_src: the Hardware source in the ARP Header
987 * @hw_dst: the Hardware destination in the ARP Header
988 * @ethhdr: pointer to the Ethernet header of the claim frame
989 *
990 * checks if it is a claim packet and if its on the same group.
991 * This function also applies the group ID of the sender
992 * if it is in the same mesh.
993 *
62fe710f 994 * Return:
38ef3d1d
SW
995 * 2 - if it is a claim packet and on the same group
996 * 1 - if is a claim packet from another group
997 * 0 - if it is not a claim packet
998 */
56303d34
SE
999static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1000 struct batadv_hard_iface *primary_if,
6b5e971a 1001 u8 *hw_src, u8 *hw_dst,
3b300de3 1002 struct ethhdr *ethhdr)
38ef3d1d 1003{
6b5e971a 1004 u8 *backbone_addr;
56303d34 1005 struct batadv_orig_node *orig_node;
96412690 1006 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
38ef3d1d 1007
96412690 1008 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
807736f6 1009 bla_dst_own = &bat_priv->bla.claim_dest;
38ef3d1d 1010
38ef3d1d
SW
1011 /* if announcement packet, use the source,
1012 * otherwise assume it is in the hw_src
1013 */
1014 switch (bla_dst->type) {
3eb8773e 1015 case BATADV_CLAIM_TYPE_CLAIM:
38ef3d1d
SW
1016 backbone_addr = hw_src;
1017 break;
acd34afa
SE
1018 case BATADV_CLAIM_TYPE_REQUEST:
1019 case BATADV_CLAIM_TYPE_ANNOUNCE:
3eb8773e 1020 case BATADV_CLAIM_TYPE_UNCLAIM:
38ef3d1d
SW
1021 backbone_addr = ethhdr->h_source;
1022 break;
1023 default:
1024 return 0;
1025 }
1026
1027 /* don't accept claim frames from ourselves */
1eda58bf 1028 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
38ef3d1d
SW
1029 return 0;
1030
1031 /* if its already the same group, it is fine. */
1032 if (bla_dst->group == bla_dst_own->group)
1033 return 2;
1034
1035 /* lets see if this originator is in our mesh */
da641193 1036 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
38ef3d1d
SW
1037
1038 /* dont accept claims from gateways which are not in
1039 * the same mesh or group.
1040 */
1041 if (!orig_node)
1042 return 1;
1043
1044 /* if our mesh friends mac is bigger, use it for ourselves. */
1045 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
39c75a51 1046 batadv_dbg(BATADV_DBG_BLA, bat_priv,
39a32991 1047 "taking other backbones claim group: %#.4x\n",
1eda58bf 1048 ntohs(bla_dst->group));
38ef3d1d
SW
1049 bla_dst_own->group = bla_dst->group;
1050 }
1051
5d967310 1052 batadv_orig_node_put(orig_node);
38ef3d1d
SW
1053
1054 return 2;
1055}
1056
1b371d13 1057/**
04e14be6 1058 * batadv_bla_process_claim - Check if this is a claim frame, and process it
1b371d13 1059 * @bat_priv: the bat priv with all the soft interface information
e3357189 1060 * @primary_if: the primary hard interface of this batman soft interface
23721387
SW
1061 * @skb: the frame to be checked
1062 *
4b426b10 1063 * Return: true if it was a claim frame, otherwise return false to
23721387
SW
1064 * tell the callee that it can use the frame on its own.
1065 */
4b426b10
SE
1066static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1067 struct batadv_hard_iface *primary_if,
1068 struct sk_buff *skb)
23721387 1069{
d46b6bfa 1070 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
6b5e971a 1071 u8 *hw_src, *hw_dst;
d46b6bfa 1072 struct vlan_hdr *vhdr, vhdr_buf;
c018ad3d 1073 struct ethhdr *ethhdr;
23721387 1074 struct arphdr *arphdr;
c018ad3d 1075 unsigned short vid;
d46b6bfa 1076 int vlan_depth = 0;
293e9338 1077 __be16 proto;
23721387 1078 int headlen;
38ef3d1d 1079 int ret;
23721387 1080
c018ad3d 1081 vid = batadv_get_vid(skb, 0);
7ed4be95 1082 ethhdr = eth_hdr(skb);
23721387 1083
c018ad3d
AQ
1084 proto = ethhdr->h_proto;
1085 headlen = ETH_HLEN;
1086 if (vid & BATADV_VLAN_HAS_TAG) {
d46b6bfa
SW
1087 /* Traverse the VLAN/Ethertypes.
1088 *
1089 * At this point it is known that the first protocol is a VLAN
1090 * header, so start checking at the encapsulated protocol.
1091 *
1092 * The depth of the VLAN headers is recorded to drop BLA claim
1093 * frames encapsulated into multiple VLAN headers (QinQ).
1094 */
1095 do {
1096 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1097 &vhdr_buf);
1098 if (!vhdr)
4b426b10 1099 return false;
d46b6bfa
SW
1100
1101 proto = vhdr->h_vlan_encapsulated_proto;
1102 headlen += VLAN_HLEN;
1103 vlan_depth++;
1104 } while (proto == htons(ETH_P_8021Q));
23721387
SW
1105 }
1106
293e9338 1107 if (proto != htons(ETH_P_ARP))
4b426b10 1108 return false; /* not a claim frame */
23721387
SW
1109
1110 /* this must be a ARP frame. check if it is a claim. */
1111
1112 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
4b426b10 1113 return false;
23721387
SW
1114
1115 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
7ed4be95 1116 ethhdr = eth_hdr(skb);
6b5e971a 1117 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
23721387
SW
1118
1119 /* Check whether the ARP frame carries a valid
1120 * IP information
1121 */
23721387 1122 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
4b426b10 1123 return false;
23721387 1124 if (arphdr->ar_pro != htons(ETH_P_IP))
4b426b10 1125 return false;
23721387 1126 if (arphdr->ar_hln != ETH_ALEN)
4b426b10 1127 return false;
23721387 1128 if (arphdr->ar_pln != 4)
4b426b10 1129 return false;
23721387 1130
6b5e971a 1131 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
23721387 1132 hw_dst = hw_src + ETH_ALEN + 4;
96412690 1133 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
d46b6bfa
SW
1134 bla_dst_own = &bat_priv->bla.claim_dest;
1135
1136 /* check if it is a claim frame in general */
1137 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1138 sizeof(bla_dst->magic)) != 0)
4b426b10 1139 return false;
d46b6bfa
SW
1140
1141 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1142 * drop that, as this is not supported by BLA but should also not be
1143 * sent via the mesh.
1144 */
1145 if (vlan_depth > 1)
4b426b10 1146 return true;
23721387 1147
cd9c7bfb
SW
1148 /* Let the loopdetect frames on the mesh in any case. */
1149 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1150 return 0;
1151
23721387 1152 /* check if it is a claim frame. */
3b300de3
SE
1153 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1154 ethhdr);
38ef3d1d 1155 if (ret == 1)
39c75a51 1156 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 1157 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
5f80df67
AQ
1158 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1159 hw_dst);
38ef3d1d
SW
1160
1161 if (ret < 2)
4b426b10 1162 return !!ret;
23721387
SW
1163
1164 /* become a backbone gw ourselves on this vlan if not happened yet */
3b300de3 1165 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
23721387
SW
1166
1167 /* check for the different types of claim frames ... */
1168 switch (bla_dst->type) {
3eb8773e 1169 case BATADV_CLAIM_TYPE_CLAIM:
3b300de3
SE
1170 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1171 ethhdr->h_source, vid))
4b426b10 1172 return true;
23721387 1173 break;
3eb8773e 1174 case BATADV_CLAIM_TYPE_UNCLAIM:
3b300de3
SE
1175 if (batadv_handle_unclaim(bat_priv, primary_if,
1176 ethhdr->h_source, hw_src, vid))
4b426b10 1177 return true;
23721387
SW
1178 break;
1179
acd34afa 1180 case BATADV_CLAIM_TYPE_ANNOUNCE:
3b300de3
SE
1181 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1182 vid))
4b426b10 1183 return true;
23721387 1184 break;
acd34afa 1185 case BATADV_CLAIM_TYPE_REQUEST:
3b300de3
SE
1186 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1187 vid))
4b426b10 1188 return true;
23721387
SW
1189 break;
1190 }
1191
39c75a51 1192 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 1193 "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",
5f80df67 1194 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
4b426b10 1195 return true;
23721387
SW
1196}
1197
04e14be6
SW
1198/**
1199 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1200 * immediately
1201 * @bat_priv: the bat priv with all the soft interface information
1202 * @now: whether the whole hash shall be wiped now
1203 *
1204 * Check when we last heard from other nodes, and remove them in case of
23721387
SW
1205 * a time out, or clean all backbone gws if now is set.
1206 */
56303d34 1207static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
23721387 1208{
bae98774 1209 struct batadv_bla_backbone_gw *backbone_gw;
b67bfe0d 1210 struct hlist_node *node_tmp;
23721387 1211 struct hlist_head *head;
5bf74e9c 1212 struct batadv_hashtable *hash;
23721387
SW
1213 spinlock_t *list_lock; /* protects write access to the hash lists */
1214 int i;
1215
807736f6 1216 hash = bat_priv->bla.backbone_hash;
23721387
SW
1217 if (!hash)
1218 return;
1219
1220 for (i = 0; i < hash->size; i++) {
1221 head = &hash->table[i];
1222 list_lock = &hash->list_locks[i];
1223
1224 spin_lock_bh(list_lock);
b67bfe0d 1225 hlist_for_each_entry_safe(backbone_gw, node_tmp,
23721387
SW
1226 head, hash_entry) {
1227 if (now)
1228 goto purge_now;
1eda58bf 1229 if (!batadv_has_timed_out(backbone_gw->lasttime,
42d0b044 1230 BATADV_BLA_BACKBONE_TIMEOUT))
23721387
SW
1231 continue;
1232
39c75a51 1233 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1eda58bf
SE
1234 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1235 backbone_gw->orig);
23721387
SW
1236
1237purge_now:
1238 /* don't wait for the pending request anymore */
1239 if (atomic_read(&backbone_gw->request_sent))
807736f6 1240 atomic_dec(&bat_priv->bla.num_requests);
23721387 1241
3b300de3 1242 batadv_bla_del_backbone_claims(backbone_gw);
23721387 1243
b67bfe0d 1244 hlist_del_rcu(&backbone_gw->hash_entry);
c8b86c12 1245 batadv_backbone_gw_put(backbone_gw);
23721387
SW
1246 }
1247 spin_unlock_bh(list_lock);
1248 }
1249}
1250
2c53040f 1251/**
04e14be6 1252 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
2c53040f 1253 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1254 * @primary_if: the selected primary interface, may be NULL if now is set
1255 * @now: whether the whole hash shall be wiped now
1256 *
1257 * Check when we heard last time from our own claims, and remove them in case of
1258 * a time out, or clean all claims if now is set
1259 */
56303d34
SE
1260static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1261 struct batadv_hard_iface *primary_if,
1262 int now)
23721387 1263{
3db0decf 1264 struct batadv_bla_backbone_gw *backbone_gw;
712bbfe4 1265 struct batadv_bla_claim *claim;
23721387 1266 struct hlist_head *head;
5bf74e9c 1267 struct batadv_hashtable *hash;
23721387
SW
1268 int i;
1269
807736f6 1270 hash = bat_priv->bla.claim_hash;
23721387
SW
1271 if (!hash)
1272 return;
1273
1274 for (i = 0; i < hash->size; i++) {
1275 head = &hash->table[i];
1276
1277 rcu_read_lock();
b67bfe0d 1278 hlist_for_each_entry_rcu(claim, head, hash_entry) {
3db0decf 1279 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
23721387
SW
1280 if (now)
1281 goto purge_now;
3db0decf
SE
1282
1283 if (!batadv_compare_eth(backbone_gw->orig,
1eda58bf 1284 primary_if->net_dev->dev_addr))
3db0decf
SE
1285 goto skip;
1286
1eda58bf 1287 if (!batadv_has_timed_out(claim->lasttime,
42d0b044 1288 BATADV_BLA_CLAIM_TIMEOUT))
3db0decf 1289 goto skip;
23721387 1290
39c75a51 1291 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
1292 "bla_purge_claims(): %pM, vid %d, time out\n",
1293 claim->addr, claim->vid);
23721387
SW
1294
1295purge_now:
3b300de3 1296 batadv_handle_unclaim(bat_priv, primary_if,
3db0decf 1297 backbone_gw->orig,
3b300de3 1298 claim->addr, claim->vid);
3db0decf
SE
1299skip:
1300 batadv_backbone_gw_put(backbone_gw);
23721387
SW
1301 }
1302 rcu_read_unlock();
1303 }
1304}
1305
2c53040f 1306/**
04e14be6
SW
1307 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1308 * originator address changes
2c53040f 1309 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1310 * @primary_if: the new selected primary_if
1311 * @oldif: the old primary interface, may be NULL
23721387 1312 */
56303d34
SE
1313void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1314 struct batadv_hard_iface *primary_if,
1315 struct batadv_hard_iface *oldif)
23721387 1316{
bae98774 1317 struct batadv_bla_backbone_gw *backbone_gw;
23721387 1318 struct hlist_head *head;
5bf74e9c 1319 struct batadv_hashtable *hash;
807736f6 1320 __be16 group;
23721387
SW
1321 int i;
1322
38ef3d1d 1323 /* reset bridge loop avoidance group id */
807736f6
SE
1324 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1325 bat_priv->bla.claim_dest.group = group;
38ef3d1d 1326
d5b4c93e
SW
1327 /* purge everything when bridge loop avoidance is turned off */
1328 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1329 oldif = NULL;
1330
23721387 1331 if (!oldif) {
3b300de3
SE
1332 batadv_bla_purge_claims(bat_priv, NULL, 1);
1333 batadv_bla_purge_backbone_gw(bat_priv, 1);
23721387
SW
1334 return;
1335 }
1336
807736f6 1337 hash = bat_priv->bla.backbone_hash;
23721387
SW
1338 if (!hash)
1339 return;
1340
1341 for (i = 0; i < hash->size; i++) {
1342 head = &hash->table[i];
1343
1344 rcu_read_lock();
b67bfe0d 1345 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
23721387 1346 /* own orig still holds the old value. */
1eda58bf
SE
1347 if (!batadv_compare_eth(backbone_gw->orig,
1348 oldif->net_dev->dev_addr))
23721387
SW
1349 continue;
1350
8fdd0153
AQ
1351 ether_addr_copy(backbone_gw->orig,
1352 primary_if->net_dev->dev_addr);
23721387
SW
1353 /* send an announce frame so others will ask for our
1354 * claims and update their tables.
1355 */
3b300de3 1356 batadv_bla_send_announce(bat_priv, backbone_gw);
23721387
SW
1357 }
1358 rcu_read_unlock();
1359 }
1360}
1361
cd9c7bfb
SW
1362/**
1363 * batadv_bla_send_loopdetect - send a loopdetect frame
1364 * @bat_priv: the bat priv with all the soft interface information
1365 * @backbone_gw: the backbone gateway for which a loop should be detected
1366 *
1367 * To detect loops that the bridge loop avoidance can't handle, send a loop
1368 * detection packet on the backbone. Unlike other BLA frames, this frame will
1369 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1370 * indicates that there is a loop.
1371 */
1372static void
1373batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1374 struct batadv_bla_backbone_gw *backbone_gw)
1375{
1376 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1377 backbone_gw->vid);
1378 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1379 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1380}
1381
d68081a2
SW
1382/**
1383 * batadv_bla_status_update - purge bla interfaces if necessary
1384 * @net_dev: the soft interface net device
1385 */
1386void batadv_bla_status_update(struct net_device *net_dev)
1387{
1388 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1389 struct batadv_hard_iface *primary_if;
1390
1391 primary_if = batadv_primary_if_get_selected(bat_priv);
1392 if (!primary_if)
1393 return;
1394
1395 /* this function already purges everything when bla is disabled,
1396 * so just call that one.
1397 */
1398 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
82047ad7 1399 batadv_hardif_put(primary_if);
d68081a2
SW
1400}
1401
04e14be6
SW
1402/**
1403 * batadv_bla_periodic_work - performs periodic bla work
1404 * @work: kernel work struct
1405 *
1406 * periodic work to do:
23721387
SW
1407 * * purge structures when they are too old
1408 * * send announcements
1409 */
3b300de3 1410static void batadv_bla_periodic_work(struct work_struct *work)
23721387 1411{
bbb1f90e 1412 struct delayed_work *delayed_work;
56303d34 1413 struct batadv_priv *bat_priv;
807736f6 1414 struct batadv_priv_bla *priv_bla;
23721387 1415 struct hlist_head *head;
bae98774 1416 struct batadv_bla_backbone_gw *backbone_gw;
5bf74e9c 1417 struct batadv_hashtable *hash;
56303d34 1418 struct batadv_hard_iface *primary_if;
cd9c7bfb 1419 bool send_loopdetect = false;
23721387
SW
1420 int i;
1421
4ba4bc0f 1422 delayed_work = to_delayed_work(work);
807736f6
SE
1423 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1424 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
e5d89254 1425 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1426 if (!primary_if)
1427 goto out;
1428
3b300de3
SE
1429 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1430 batadv_bla_purge_backbone_gw(bat_priv, 0);
23721387
SW
1431
1432 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1433 goto out;
1434
cd9c7bfb
SW
1435 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1436 /* set a new random mac address for the next bridge loop
1437 * detection frames. Set the locally administered bit to avoid
1438 * collisions with users mac addresses.
1439 */
1440 random_ether_addr(bat_priv->bla.loopdetect_addr);
1441 bat_priv->bla.loopdetect_addr[0] = 0xba;
1442 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1443 bat_priv->bla.loopdetect_lasttime = jiffies;
1444 atomic_set(&bat_priv->bla.loopdetect_next,
1445 BATADV_BLA_LOOPDETECT_PERIODS);
1446
1447 /* mark for sending loop detect on all VLANs */
1448 send_loopdetect = true;
1449 }
1450
807736f6 1451 hash = bat_priv->bla.backbone_hash;
23721387
SW
1452 if (!hash)
1453 goto out;
1454
1455 for (i = 0; i < hash->size; i++) {
1456 head = &hash->table[i];
1457
1458 rcu_read_lock();
b67bfe0d 1459 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1eda58bf
SE
1460 if (!batadv_compare_eth(backbone_gw->orig,
1461 primary_if->net_dev->dev_addr))
23721387
SW
1462 continue;
1463
1464 backbone_gw->lasttime = jiffies;
1465
3b300de3 1466 batadv_bla_send_announce(bat_priv, backbone_gw);
cd9c7bfb
SW
1467 if (send_loopdetect)
1468 batadv_bla_send_loopdetect(bat_priv,
1469 backbone_gw);
d807f272
SW
1470
1471 /* request_sent is only set after creation to avoid
1472 * problems when we are not yet known as backbone gw
1473 * in the backbone.
1474 *
28709878
SW
1475 * We can reset this now after we waited some periods
1476 * to give bridge forward delays and bla group forming
1477 * some grace time.
d807f272
SW
1478 */
1479
1480 if (atomic_read(&backbone_gw->request_sent) == 0)
1481 continue;
1482
28709878
SW
1483 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1484 continue;
1485
d807f272
SW
1486 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1487 atomic_set(&backbone_gw->request_sent, 0);
23721387
SW
1488 }
1489 rcu_read_unlock();
1490 }
1491out:
1492 if (primary_if)
82047ad7 1493 batadv_hardif_put(primary_if);
23721387 1494
72414442
AQ
1495 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1496 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
23721387
SW
1497}
1498
5d52dad2
SE
1499/* The hash for claim and backbone hash receive the same key because they
1500 * are getting initialized by hash_new with the same key. Reinitializing
1501 * them with to different keys to allow nested locking without generating
1502 * lockdep warnings
1503 */
3b300de3
SE
1504static struct lock_class_key batadv_claim_hash_lock_class_key;
1505static struct lock_class_key batadv_backbone_hash_lock_class_key;
5d52dad2 1506
04e14be6
SW
1507/**
1508 * batadv_bla_init - initialize all bla structures
1509 * @bat_priv: the bat priv with all the soft interface information
1510 *
1511 * Return: 0 on success, < 0 on error.
1512 */
56303d34 1513int batadv_bla_init(struct batadv_priv *bat_priv)
23721387 1514{
fe2da6ff 1515 int i;
6b5e971a 1516 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
56303d34 1517 struct batadv_hard_iface *primary_if;
6b5e971a 1518 u16 crc;
807736f6 1519 unsigned long entrytime;
fe2da6ff 1520
7dac7b76
LL
1521 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1522
39c75a51 1523 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
23721387 1524
38ef3d1d 1525 /* setting claim destination address */
807736f6
SE
1526 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1527 bat_priv->bla.claim_dest.type = 0;
e5d89254 1528 primary_if = batadv_primary_if_get_selected(bat_priv);
38ef3d1d 1529 if (primary_if) {
807736f6
SE
1530 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1531 bat_priv->bla.claim_dest.group = htons(crc);
82047ad7 1532 batadv_hardif_put(primary_if);
38ef3d1d 1533 } else {
807736f6 1534 bat_priv->bla.claim_dest.group = 0; /* will be set later */
38ef3d1d
SW
1535 }
1536
fe2da6ff 1537 /* initialize the duplicate list */
807736f6 1538 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
42d0b044 1539 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
807736f6
SE
1540 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1541 bat_priv->bla.bcast_duplist_curr = 0;
fe2da6ff 1542
cd9c7bfb
SW
1543 atomic_set(&bat_priv->bla.loopdetect_next,
1544 BATADV_BLA_LOOPDETECT_PERIODS);
1545
807736f6 1546 if (bat_priv->bla.claim_hash)
5346c35e 1547 return 0;
23721387 1548
807736f6
SE
1549 bat_priv->bla.claim_hash = batadv_hash_new(128);
1550 bat_priv->bla.backbone_hash = batadv_hash_new(32);
23721387 1551
807736f6 1552 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
5346c35e 1553 return -ENOMEM;
23721387 1554
807736f6 1555 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
3b300de3 1556 &batadv_claim_hash_lock_class_key);
807736f6 1557 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
3b300de3 1558 &batadv_backbone_hash_lock_class_key);
5d52dad2 1559
39c75a51 1560 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
23721387 1561
72414442
AQ
1562 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1563
1564 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1565 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
5346c35e 1566 return 0;
23721387
SW
1567}
1568
2c53040f 1569/**
04e14be6 1570 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
2c53040f 1571 * @bat_priv: the bat priv with all the soft interface information
004e86fc 1572 * @skb: contains the bcast_packet to be checked
fe2da6ff
SW
1573 *
1574 * check if it is on our broadcast list. Another gateway might
1575 * have sent the same packet because it is connected to the same backbone,
1576 * so we have to remove this duplicate.
1577 *
1578 * This is performed by checking the CRC, which will tell us
1579 * with a good chance that it is the same packet. If it is furthermore
1580 * sent by another host, drop it. We allow equal packets from
1581 * the same host however as this might be intended.
04e14be6 1582 *
4b426b10 1583 * Return: true if a packet is in the duplicate list, false otherwise.
9cfc7bd6 1584 */
4b426b10
SE
1585bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1586 struct sk_buff *skb)
fe2da6ff 1587{
4b426b10 1588 int i, curr;
004e86fc
SW
1589 __be32 crc;
1590 struct batadv_bcast_packet *bcast_packet;
56303d34 1591 struct batadv_bcast_duplist_entry *entry;
4b426b10 1592 bool ret = false;
fe2da6ff 1593
004e86fc 1594 bcast_packet = (struct batadv_bcast_packet *)skb->data;
fe2da6ff
SW
1595
1596 /* calculate the crc ... */
004e86fc 1597 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
fe2da6ff 1598
7dac7b76
LL
1599 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1600
42d0b044 1601 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
807736f6
SE
1602 curr = (bat_priv->bla.bcast_duplist_curr + i);
1603 curr %= BATADV_DUPLIST_SIZE;
1604 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1605
1606 /* we can stop searching if the entry is too old ;
1607 * later entries will be even older
1608 */
42d0b044
SE
1609 if (batadv_has_timed_out(entry->entrytime,
1610 BATADV_DUPLIST_TIMEOUT))
fe2da6ff
SW
1611 break;
1612
1613 if (entry->crc != crc)
1614 continue;
1615
1eda58bf 1616 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
fe2da6ff
SW
1617 continue;
1618
1619 /* this entry seems to match: same crc, not too old,
4b426b10 1620 * and from another gw. therefore return true to forbid it.
fe2da6ff 1621 */
4b426b10 1622 ret = true;
7dac7b76 1623 goto out;
fe2da6ff 1624 }
7dac7b76 1625 /* not found, add a new entry (overwrite the oldest entry)
3f68785e 1626 * and allow it, its the first occurrence.
7dac7b76 1627 */
807736f6 1628 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
42d0b044 1629 curr %= BATADV_DUPLIST_SIZE;
807736f6 1630 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1631 entry->crc = crc;
1632 entry->entrytime = jiffies;
8fdd0153 1633 ether_addr_copy(entry->orig, bcast_packet->orig);
807736f6 1634 bat_priv->bla.bcast_duplist_curr = curr;
fe2da6ff 1635
7dac7b76
LL
1636out:
1637 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1638
1639 return ret;
fe2da6ff
SW
1640}
1641
1b371d13 1642/**
04e14be6
SW
1643 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1644 * the VLAN identified by vid.
1b371d13 1645 * @bat_priv: the bat priv with all the soft interface information
20ff9d59 1646 * @orig: originator mac address
cfd4f757 1647 * @vid: VLAN identifier
20ff9d59 1648 *
62fe710f 1649 * Return: true if orig is a backbone for this vid, false otherwise.
20ff9d59 1650 */
6b5e971a 1651bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
cfd4f757 1652 unsigned short vid)
20ff9d59 1653{
807736f6 1654 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
20ff9d59 1655 struct hlist_head *head;
bae98774 1656 struct batadv_bla_backbone_gw *backbone_gw;
20ff9d59
SW
1657 int i;
1658
1659 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
cfd4f757 1660 return false;
20ff9d59
SW
1661
1662 if (!hash)
cfd4f757 1663 return false;
20ff9d59
SW
1664
1665 for (i = 0; i < hash->size; i++) {
1666 head = &hash->table[i];
1667
1668 rcu_read_lock();
b67bfe0d 1669 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
cfd4f757
AQ
1670 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1671 backbone_gw->vid == vid) {
20ff9d59 1672 rcu_read_unlock();
cfd4f757 1673 return true;
20ff9d59
SW
1674 }
1675 }
1676 rcu_read_unlock();
1677 }
1678
cfd4f757 1679 return false;
20ff9d59
SW
1680}
1681
2c53040f 1682/**
04e14be6 1683 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
2c53040f 1684 * @skb: the frame to be checked
23721387
SW
1685 * @orig_node: the orig_node of the frame
1686 * @hdr_size: maximum length of the frame
1687 *
4b426b10
SE
1688 * Return: true if the orig_node is also a gateway on the soft interface,
1689 * otherwise it returns false.
23721387 1690 */
4b426b10
SE
1691bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1692 struct batadv_orig_node *orig_node, int hdr_size)
23721387 1693{
bae98774 1694 struct batadv_bla_backbone_gw *backbone_gw;
c018ad3d 1695 unsigned short vid;
23721387
SW
1696
1697 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
4b426b10 1698 return false;
23721387
SW
1699
1700 /* first, find out the vid. */
0d125074 1701 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
4b426b10 1702 return false;
23721387 1703
c018ad3d 1704 vid = batadv_get_vid(skb, hdr_size);
23721387
SW
1705
1706 /* see if this originator is a backbone gw for this VLAN */
3b300de3
SE
1707 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1708 orig_node->orig, vid);
23721387 1709 if (!backbone_gw)
4b426b10 1710 return false;
23721387 1711
c8b86c12 1712 batadv_backbone_gw_put(backbone_gw);
4b426b10 1713 return true;
23721387
SW
1714}
1715
04e14be6 1716/**
6d030de8 1717 * batadv_bla_free - free all bla structures
04e14be6
SW
1718 * @bat_priv: the bat priv with all the soft interface information
1719 *
1720 * for softinterface free or module unload
1721 */
56303d34 1722void batadv_bla_free(struct batadv_priv *bat_priv)
23721387 1723{
56303d34 1724 struct batadv_hard_iface *primary_if;
23721387 1725
807736f6 1726 cancel_delayed_work_sync(&bat_priv->bla.work);
e5d89254 1727 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387 1728
807736f6 1729 if (bat_priv->bla.claim_hash) {
3b300de3 1730 batadv_bla_purge_claims(bat_priv, primary_if, 1);
807736f6
SE
1731 batadv_hash_destroy(bat_priv->bla.claim_hash);
1732 bat_priv->bla.claim_hash = NULL;
23721387 1733 }
807736f6 1734 if (bat_priv->bla.backbone_hash) {
3b300de3 1735 batadv_bla_purge_backbone_gw(bat_priv, 1);
807736f6
SE
1736 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1737 bat_priv->bla.backbone_hash = NULL;
23721387
SW
1738 }
1739 if (primary_if)
82047ad7 1740 batadv_hardif_put(primary_if);
23721387
SW
1741}
1742
cd9c7bfb
SW
1743/**
1744 * batadv_bla_loopdetect_check - check and handle a detected loop
1745 * @bat_priv: the bat priv with all the soft interface information
1746 * @skb: the packet to check
1747 * @primary_if: interface where the request came on
1748 * @vid: the VLAN ID of the frame
1749 *
1750 * Checks if this packet is a loop detect frame which has been sent by us,
1751 * throw an uevent and log the event if that is the case.
1752 *
1753 * Return: true if it is a loop detect frame which is to be dropped, false
1754 * otherwise.
1755 */
1756static bool
1757batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1758 struct batadv_hard_iface *primary_if,
1759 unsigned short vid)
1760{
1761 struct batadv_bla_backbone_gw *backbone_gw;
1762 struct ethhdr *ethhdr;
1763
1764 ethhdr = eth_hdr(skb);
1765
1766 /* Only check for the MAC address and skip more checks here for
1767 * performance reasons - this function is on the hotpath, after all.
1768 */
1769 if (!batadv_compare_eth(ethhdr->h_source,
1770 bat_priv->bla.loopdetect_addr))
1771 return false;
1772
1773 /* If the packet came too late, don't forward it on the mesh
1774 * but don't consider that as loop. It might be a coincidence.
1775 */
1776 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1777 BATADV_BLA_LOOPDETECT_TIMEOUT))
1778 return true;
1779
1780 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1781 primary_if->net_dev->dev_addr,
1782 vid, true);
1783 if (unlikely(!backbone_gw))
1784 return true;
1785
1786 queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1787 /* backbone_gw is unreferenced in the report work function function */
1788
1789 return true;
1790}
1791
2c53040f 1792/**
04e14be6 1793 * batadv_bla_rx - check packets coming from the mesh.
2c53040f 1794 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1795 * @skb: the frame to be checked
1796 * @vid: the VLAN ID of the frame
2d3f6ccc 1797 * @is_bcast: the packet came in a broadcast packet type.
23721387 1798 *
04e14be6 1799 * batadv_bla_rx avoidance checks if:
23721387
SW
1800 * * we have to race for a claim
1801 * * if the frame is allowed on the LAN
1802 *
62fe710f
SE
1803 * in these cases, the skb is further handled by this function
1804 *
4b426b10
SE
1805 * Return: true if handled, otherwise it returns false and the caller shall
1806 * further process the skb.
23721387 1807 */
4b426b10
SE
1808bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1809 unsigned short vid, bool is_bcast)
23721387 1810{
3db0decf 1811 struct batadv_bla_backbone_gw *backbone_gw;
23721387 1812 struct ethhdr *ethhdr;
712bbfe4 1813 struct batadv_bla_claim search_claim, *claim = NULL;
56303d34 1814 struct batadv_hard_iface *primary_if;
3db0decf 1815 bool own_claim;
4b426b10 1816 bool ret;
23721387 1817
7ed4be95 1818 ethhdr = eth_hdr(skb);
23721387 1819
e5d89254 1820 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1821 if (!primary_if)
1822 goto handled;
1823
1824 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1825 goto allow;
1826
cd9c7bfb
SW
1827 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1828 goto handled;
1829
807736f6 1830 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
23721387 1831 /* don't allow broadcasts while requests are in flight */
2d3f6ccc 1832 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
23721387
SW
1833 goto handled;
1834
8fdd0153 1835 ether_addr_copy(search_claim.addr, ethhdr->h_source);
23721387 1836 search_claim.vid = vid;
3b300de3 1837 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
1838
1839 if (!claim) {
1840 /* possible optimization: race for a claim */
1841 /* No claim exists yet, claim it for us!
1842 */
3b300de3
SE
1843 batadv_handle_claim(bat_priv, primary_if,
1844 primary_if->net_dev->dev_addr,
1845 ethhdr->h_source, vid);
23721387
SW
1846 goto allow;
1847 }
1848
1849 /* if it is our own claim ... */
3db0decf
SE
1850 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1851 own_claim = batadv_compare_eth(backbone_gw->orig,
1852 primary_if->net_dev->dev_addr);
1853 batadv_backbone_gw_put(backbone_gw);
1854
1855 if (own_claim) {
23721387
SW
1856 /* ... allow it in any case */
1857 claim->lasttime = jiffies;
1858 goto allow;
1859 }
1860
1861 /* if it is a broadcast ... */
2d3f6ccc
SW
1862 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1863 /* ... drop it. the responsible gateway is in charge.
1864 *
1865 * We need to check is_bcast because with the gateway
1866 * feature, broadcasts (like DHCP requests) may be sent
1867 * using a unicast packet type.
1868 */
23721387
SW
1869 goto handled;
1870 } else {
1871 /* seems the client considers us as its best gateway.
1872 * send a claim and update the claim table
1873 * immediately.
1874 */
3b300de3
SE
1875 batadv_handle_claim(bat_priv, primary_if,
1876 primary_if->net_dev->dev_addr,
1877 ethhdr->h_source, vid);
23721387
SW
1878 goto allow;
1879 }
1880allow:
3b300de3 1881 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
4b426b10 1882 ret = false;
23721387
SW
1883 goto out;
1884
1885handled:
1886 kfree_skb(skb);
4b426b10 1887 ret = true;
23721387
SW
1888
1889out:
1890 if (primary_if)
82047ad7 1891 batadv_hardif_put(primary_if);
23721387 1892 if (claim)
321e3e08 1893 batadv_claim_put(claim);
23721387
SW
1894 return ret;
1895}
1896
2c53040f 1897/**
04e14be6 1898 * batadv_bla_tx - check packets going into the mesh
2c53040f 1899 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1900 * @skb: the frame to be checked
1901 * @vid: the VLAN ID of the frame
1902 *
04e14be6 1903 * batadv_bla_tx checks if:
23721387
SW
1904 * * a claim was received which has to be processed
1905 * * the frame is allowed on the mesh
1906 *
62fe710f 1907 * in these cases, the skb is further handled by this function.
9d2c9488
LL
1908 *
1909 * This call might reallocate skb data.
62fe710f 1910 *
4b426b10
SE
1911 * Return: true if handled, otherwise it returns false and the caller shall
1912 * further process the skb.
23721387 1913 */
4b426b10
SE
1914bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1915 unsigned short vid)
23721387
SW
1916{
1917 struct ethhdr *ethhdr;
712bbfe4 1918 struct batadv_bla_claim search_claim, *claim = NULL;
3db0decf 1919 struct batadv_bla_backbone_gw *backbone_gw;
56303d34 1920 struct batadv_hard_iface *primary_if;
3db0decf 1921 bool client_roamed;
4b426b10 1922 bool ret = false;
23721387 1923
e5d89254 1924 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1925 if (!primary_if)
1926 goto out;
1927
1928 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1929 goto allow;
1930
3b300de3 1931 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
23721387
SW
1932 goto handled;
1933
7ed4be95 1934 ethhdr = eth_hdr(skb);
23721387 1935
807736f6 1936 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
23721387
SW
1937 /* don't allow broadcasts while requests are in flight */
1938 if (is_multicast_ether_addr(ethhdr->h_dest))
1939 goto handled;
1940
8fdd0153 1941 ether_addr_copy(search_claim.addr, ethhdr->h_source);
23721387
SW
1942 search_claim.vid = vid;
1943
3b300de3 1944 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
1945
1946 /* if no claim exists, allow it. */
1947 if (!claim)
1948 goto allow;
1949
1950 /* check if we are responsible. */
3db0decf
SE
1951 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1952 client_roamed = batadv_compare_eth(backbone_gw->orig,
1953 primary_if->net_dev->dev_addr);
1954 batadv_backbone_gw_put(backbone_gw);
1955
1956 if (client_roamed) {
23721387
SW
1957 /* if yes, the client has roamed and we have
1958 * to unclaim it.
1959 */
3b300de3
SE
1960 batadv_handle_unclaim(bat_priv, primary_if,
1961 primary_if->net_dev->dev_addr,
1962 ethhdr->h_source, vid);
23721387
SW
1963 goto allow;
1964 }
1965
1966 /* check if it is a multicast/broadcast frame */
1967 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1968 /* drop it. the responsible gateway has forwarded it into
1969 * the backbone network.
1970 */
1971 goto handled;
1972 } else {
1973 /* we must allow it. at least if we are
1974 * responsible for the DESTINATION.
1975 */
1976 goto allow;
1977 }
1978allow:
3b300de3 1979 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
4b426b10 1980 ret = false;
23721387
SW
1981 goto out;
1982handled:
4b426b10 1983 ret = true;
23721387
SW
1984out:
1985 if (primary_if)
82047ad7 1986 batadv_hardif_put(primary_if);
23721387 1987 if (claim)
321e3e08 1988 batadv_claim_put(claim);
23721387
SW
1989 return ret;
1990}
9bf8e4d4 1991
04e14be6
SW
1992/**
1993 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1994 * @seq: seq file to print on
1995 * @offset: not used
1996 *
1997 * Return: always 0
1998 */
08adf151 1999int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
9bf8e4d4
SW
2000{
2001 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 2002 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 2003 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
3db0decf 2004 struct batadv_bla_backbone_gw *backbone_gw;
712bbfe4 2005 struct batadv_bla_claim *claim;
56303d34 2006 struct batadv_hard_iface *primary_if;
9bf8e4d4 2007 struct hlist_head *head;
5a1dd8a4 2008 u16 backbone_crc;
6b5e971a 2009 u32 i;
9bf8e4d4 2010 bool is_own;
6b5e971a 2011 u8 *primary_addr;
9bf8e4d4 2012
30da63a6
ML
2013 primary_if = batadv_seq_print_text_primary_if_get(seq);
2014 if (!primary_if)
9bf8e4d4 2015 goto out;
9bf8e4d4 2016
1eda58bf 2017 primary_addr = primary_if->net_dev->dev_addr;
38ef3d1d 2018 seq_printf(seq,
39a32991 2019 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1eda58bf 2020 net_dev->name, primary_addr,
807736f6 2021 ntohs(bat_priv->bla.claim_dest.group));
925a6f37
AQ
2022 seq_puts(seq,
2023 " Client VID Originator [o] (CRC )\n");
9bf8e4d4
SW
2024 for (i = 0; i < hash->size; i++) {
2025 head = &hash->table[i];
2026
2027 rcu_read_lock();
b67bfe0d 2028 hlist_for_each_entry_rcu(claim, head, hash_entry) {
3db0decf
SE
2029 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2030
2031 is_own = batadv_compare_eth(backbone_gw->orig,
1eda58bf 2032 primary_addr);
5a1dd8a4 2033
3db0decf
SE
2034 spin_lock_bh(&backbone_gw->crc_lock);
2035 backbone_crc = backbone_gw->crc;
2036 spin_unlock_bh(&backbone_gw->crc_lock);
eb2deb6b 2037 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
5f80df67 2038 claim->addr, BATADV_PRINT_VID(claim->vid),
3db0decf 2039 backbone_gw->orig,
9bf8e4d4 2040 (is_own ? 'x' : ' '),
5a1dd8a4 2041 backbone_crc);
3db0decf
SE
2042
2043 batadv_backbone_gw_put(backbone_gw);
9bf8e4d4
SW
2044 }
2045 rcu_read_unlock();
2046 }
2047out:
2048 if (primary_if)
82047ad7 2049 batadv_hardif_put(primary_if);
30da63a6 2050 return 0;
9bf8e4d4 2051}
536a23f1 2052
04e14be6
SW
2053/**
2054 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
2055 * file
2056 * @seq: seq file to print on
2057 * @offset: not used
2058 *
2059 * Return: always 0
2060 */
536a23f1
SW
2061int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2062{
2063 struct net_device *net_dev = (struct net_device *)seq->private;
2064 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 2065 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
bae98774 2066 struct batadv_bla_backbone_gw *backbone_gw;
536a23f1 2067 struct batadv_hard_iface *primary_if;
536a23f1
SW
2068 struct hlist_head *head;
2069 int secs, msecs;
5a1dd8a4 2070 u16 backbone_crc;
6b5e971a 2071 u32 i;
536a23f1 2072 bool is_own;
6b5e971a 2073 u8 *primary_addr;
536a23f1 2074
30da63a6
ML
2075 primary_if = batadv_seq_print_text_primary_if_get(seq);
2076 if (!primary_if)
536a23f1 2077 goto out;
536a23f1
SW
2078
2079 primary_addr = primary_if->net_dev->dev_addr;
2080 seq_printf(seq,
39a32991 2081 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
536a23f1 2082 net_dev->name, primary_addr,
807736f6 2083 ntohs(bat_priv->bla.claim_dest.group));
925a6f37 2084 seq_puts(seq, " Originator VID last seen (CRC )\n");
536a23f1
SW
2085 for (i = 0; i < hash->size; i++) {
2086 head = &hash->table[i];
2087
2088 rcu_read_lock();
b67bfe0d 2089 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
536a23f1
SW
2090 msecs = jiffies_to_msecs(jiffies -
2091 backbone_gw->lasttime);
2092 secs = msecs / 1000;
2093 msecs = msecs % 1000;
2094
2095 is_own = batadv_compare_eth(backbone_gw->orig,
2096 primary_addr);
2097 if (is_own)
2098 continue;
2099
5a1dd8a4
SW
2100 spin_lock_bh(&backbone_gw->crc_lock);
2101 backbone_crc = backbone_gw->crc;
2102 spin_unlock_bh(&backbone_gw->crc_lock);
2103
eb2deb6b 2104 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
5f80df67
AQ
2105 backbone_gw->orig,
2106 BATADV_PRINT_VID(backbone_gw->vid), secs,
5a1dd8a4 2107 msecs, backbone_crc);
536a23f1
SW
2108 }
2109 rcu_read_unlock();
2110 }
2111out:
2112 if (primary_if)
82047ad7 2113 batadv_hardif_put(primary_if);
30da63a6 2114 return 0;
536a23f1 2115}