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