zram: use __bio_add_page for adding single page to bio
[linux-block.git] / net / batman-adv / bridge_loop_avoidance.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
cfa55c6d 2/* Copyright (C) B.A.T.M.A.N. contributors:
23721387
SW
3 *
4 * Simon Wunderlich
23721387
SW
5 */
6
23721387 7#include "bridge_loop_avoidance.h"
1e2c2a4f 8#include "main.h"
23721387 9
1e2c2a4f
SE
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
12#include <linux/compiler.h>
eb7da4f1 13#include <linux/container_of.h>
23721387 14#include <linux/crc16.h>
1e2c2a4f
SE
15#include <linux/errno.h>
16#include <linux/etherdevice.h>
b92b94ac 17#include <linux/gfp.h>
23721387 18#include <linux/if_arp.h>
1e2c2a4f 19#include <linux/if_ether.h>
23721387 20#include <linux/if_vlan.h>
1e2c2a4f
SE
21#include <linux/jhash.h>
22#include <linux/jiffies.h>
23#include <linux/kernel.h>
06e56ded 24#include <linux/kref.h>
1e2c2a4f
SE
25#include <linux/list.h>
26#include <linux/lockdep.h>
27#include <linux/netdevice.h>
04f3f5bf 28#include <linux/netlink.h>
1e2c2a4f
SE
29#include <linux/rculist.h>
30#include <linux/rcupdate.h>
1e2c2a4f
SE
31#include <linux/skbuff.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
34#include <linux/stddef.h>
35#include <linux/string.h>
36#include <linux/workqueue.h>
37#include <net/arp.h>
04f3f5bf
AL
38#include <net/genetlink.h>
39#include <net/netlink.h>
40#include <net/sock.h>
fec149f5 41#include <uapi/linux/batadv_packet.h>
04f3f5bf 42#include <uapi/linux/batman_adv.h>
1e2c2a4f
SE
43
44#include "hard-interface.h"
45#include "hash.h"
ba412080 46#include "log.h"
04f3f5bf 47#include "netlink.h"
1e2c2a4f 48#include "originator.h"
04f3f5bf 49#include "soft-interface.h"
1e2c2a4f 50#include "translation-table.h"
23721387 51
6b5e971a 52static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
23721387 53
3b300de3 54static void batadv_bla_periodic_work(struct work_struct *work);
bae98774
ML
55static void
56batadv_bla_send_announce(struct batadv_priv *bat_priv,
57 struct batadv_bla_backbone_gw *backbone_gw);
23721387 58
62fe710f 59/**
7e9a8c2c 60 * batadv_choose_claim() - choose the right bucket for a claim.
04e14be6
SW
61 * @data: data to hash
62 * @size: size of the hash table
62fe710f 63 *
04e14be6 64 * Return: the hash index of the claim
62fe710f 65 */
6b5e971a 66static inline u32 batadv_choose_claim(const void *data, u32 size)
23721387 67{
8864d2fc 68 const struct batadv_bla_claim *claim = data;
6b5e971a 69 u32 hash = 0;
23721387 70
36fd61cb
SE
71 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
72 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
23721387
SW
73
74 return hash % size;
75}
76
62fe710f 77/**
7e9a8c2c 78 * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway.
04e14be6
SW
79 * @data: data to hash
80 * @size: size of the hash table
62fe710f 81 *
04e14be6 82 * Return: the hash index of the backbone gateway
62fe710f 83 */
6b5e971a 84static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
23721387 85{
097930e8 86 const struct batadv_bla_backbone_gw *gw;
6b5e971a 87 u32 hash = 0;
23721387 88
8864d2fc 89 gw = data;
097930e8
LL
90 hash = jhash(&gw->orig, sizeof(gw->orig), hash);
91 hash = jhash(&gw->vid, sizeof(gw->vid), hash);
23721387
SW
92
93 return hash % size;
94}
95
04e14be6 96/**
7e9a8c2c 97 * batadv_compare_backbone_gw() - compare address and vid of two backbone gws
04e14be6
SW
98 * @node: list node of the first entry to compare
99 * @data2: pointer to the second backbone gateway
100 *
4b426b10 101 * Return: true if the backbones have the same data, false otherwise
04e14be6 102 */
4b426b10
SE
103static bool batadv_compare_backbone_gw(const struct hlist_node *node,
104 const void *data2)
23721387 105{
bae98774 106 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
23721387 107 hash_entry);
4f248cff
SE
108 const struct batadv_bla_backbone_gw *gw1 = data1;
109 const struct batadv_bla_backbone_gw *gw2 = data2;
23721387 110
c76d1525 111 if (!batadv_compare_eth(gw1->orig, gw2->orig))
4b426b10 112 return false;
c76d1525
SW
113
114 if (gw1->vid != gw2->vid)
4b426b10 115 return false;
c76d1525 116
4b426b10 117 return true;
23721387
SW
118}
119
04e14be6 120/**
7e9a8c2c 121 * batadv_compare_claim() - compare address and vid of two claims
04e14be6
SW
122 * @node: list node of the first entry to compare
123 * @data2: pointer to the second claims
124 *
4b426b10 125 * Return: true if the claim have the same data, 0 otherwise
04e14be6 126 */
4b426b10
SE
127static bool batadv_compare_claim(const struct hlist_node *node,
128 const void *data2)
23721387 129{
712bbfe4 130 const void *data1 = container_of(node, struct batadv_bla_claim,
23721387 131 hash_entry);
4f248cff
SE
132 const struct batadv_bla_claim *cl1 = data1;
133 const struct batadv_bla_claim *cl2 = data2;
c76d1525
SW
134
135 if (!batadv_compare_eth(cl1->addr, cl2->addr))
4b426b10 136 return false;
c76d1525
SW
137
138 if (cl1->vid != cl2->vid)
4b426b10 139 return false;
23721387 140
4b426b10 141 return true;
23721387
SW
142}
143
04e14be6 144/**
7e9a8c2c 145 * batadv_backbone_gw_release() - release backbone gw from lists and queue for
06e56ded
SE
146 * free after rcu grace period
147 * @ref: kref pointer of the backbone gw
148 */
149static void batadv_backbone_gw_release(struct kref *ref)
150{
151 struct batadv_bla_backbone_gw *backbone_gw;
152
153 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
154 refcount);
155
156 kfree_rcu(backbone_gw, rcu);
157}
158
159/**
7e9a8c2c 160 * batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly
c8b86c12 161 * release it
04e14be6
SW
162 * @backbone_gw: backbone gateway to be free'd
163 */
c8b86c12 164static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
23721387 165{
6340dcbd
SE
166 if (!backbone_gw)
167 return;
168
06e56ded 169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
23721387
SW
170}
171
04e14be6 172/**
7e9a8c2c
SE
173 * batadv_claim_release() - release claim from lists and queue for free after
174 * rcu grace period
04e14be6
SW
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/**
7e9a8c2c 199 * batadv_claim_put() - decrement the claim refcounter and possibly release it
04e14be6
SW
200 * @claim: claim to be free'd
201 */
321e3e08 202static void batadv_claim_put(struct batadv_bla_claim *claim)
23721387 203{
6340dcbd
SE
204 if (!claim)
205 return;
206
71b7e3d3 207 kref_put(&claim->refcount, batadv_claim_release);
23721387
SW
208}
209
1b371d13 210/**
7e9a8c2c 211 * batadv_claim_hash_find() - looks for a claim in the claim hash
1b371d13 212 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
213 * @data: search data (may be local/static data)
214 *
62fe710f 215 * Return: claim if found or NULL otherwise.
23721387 216 */
6fc77a54
SE
217static struct batadv_bla_claim *
218batadv_claim_hash_find(struct batadv_priv *bat_priv,
219 struct batadv_bla_claim *data)
23721387 220{
807736f6 221 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
23721387 222 struct hlist_head *head;
712bbfe4
ML
223 struct batadv_bla_claim *claim;
224 struct batadv_bla_claim *claim_tmp = NULL;
23721387
SW
225 int index;
226
227 if (!hash)
228 return NULL;
229
3b300de3 230 index = batadv_choose_claim(data, hash->size);
23721387
SW
231 head = &hash->table[index];
232
233 rcu_read_lock();
b67bfe0d 234 hlist_for_each_entry_rcu(claim, head, hash_entry) {
3b300de3 235 if (!batadv_compare_claim(&claim->hash_entry, data))
23721387
SW
236 continue;
237
71b7e3d3 238 if (!kref_get_unless_zero(&claim->refcount))
23721387
SW
239 continue;
240
241 claim_tmp = claim;
242 break;
243 }
244 rcu_read_unlock();
245
246 return claim_tmp;
247}
248
2c53040f 249/**
7e9a8c2c 250 * batadv_backbone_hash_find() - looks for a backbone gateway in the hash
2c53040f 251 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
252 * @addr: the address of the originator
253 * @vid: the VLAN ID
254 *
04e14be6 255 * Return: backbone gateway if found or NULL otherwise
23721387 256 */
bae98774 257static struct batadv_bla_backbone_gw *
47ce5f1e 258batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
6b5e971a 259 unsigned short vid)
23721387 260{
807736f6 261 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
23721387 262 struct hlist_head *head;
bae98774
ML
263 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
264 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
23721387
SW
265 int index;
266
267 if (!hash)
268 return NULL;
269
8fdd0153 270 ether_addr_copy(search_entry.orig, addr);
23721387
SW
271 search_entry.vid = vid;
272
3b300de3 273 index = batadv_choose_backbone_gw(&search_entry, hash->size);
23721387
SW
274 head = &hash->table[index];
275
276 rcu_read_lock();
b67bfe0d 277 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
3b300de3
SE
278 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
279 &search_entry))
23721387
SW
280 continue;
281
06e56ded 282 if (!kref_get_unless_zero(&backbone_gw->refcount))
23721387
SW
283 continue;
284
285 backbone_gw_tmp = backbone_gw;
286 break;
287 }
288 rcu_read_unlock();
289
290 return backbone_gw_tmp;
291}
292
04e14be6 293/**
7e9a8c2c 294 * batadv_bla_del_backbone_claims() - delete all claims for a backbone
04e14be6
SW
295 * @backbone_gw: backbone gateway where the claims should be removed
296 */
56303d34 297static void
bae98774 298batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
23721387 299{
5bf74e9c 300 struct batadv_hashtable *hash;
b67bfe0d 301 struct hlist_node *node_tmp;
23721387 302 struct hlist_head *head;
712bbfe4 303 struct batadv_bla_claim *claim;
23721387
SW
304 int i;
305 spinlock_t *list_lock; /* protects write access to the hash lists */
306
807736f6 307 hash = backbone_gw->bat_priv->bla.claim_hash;
23721387
SW
308 if (!hash)
309 return;
310
311 for (i = 0; i < hash->size; i++) {
312 head = &hash->table[i];
313 list_lock = &hash->list_locks[i];
314
315 spin_lock_bh(list_lock);
b67bfe0d 316 hlist_for_each_entry_safe(claim, node_tmp,
23721387 317 head, hash_entry) {
23721387
SW
318 if (claim->backbone_gw != backbone_gw)
319 continue;
320
321e3e08 321 batadv_claim_put(claim);
b67bfe0d 322 hlist_del_rcu(&claim->hash_entry);
23721387
SW
323 }
324 spin_unlock_bh(list_lock);
325 }
326
3f68785e 327 /* all claims gone, initialize CRC */
5a1dd8a4 328 spin_lock_bh(&backbone_gw->crc_lock);
3964f728 329 backbone_gw->crc = BATADV_BLA_CRC_INIT;
5a1dd8a4 330 spin_unlock_bh(&backbone_gw->crc_lock);
23721387
SW
331}
332
2c53040f 333/**
7e9a8c2c 334 * batadv_bla_send_claim() - sends a claim frame according to the provided info
2c53040f 335 * @bat_priv: the bat priv with all the soft interface information
e3357189 336 * @mac: the mac address to be announced within the claim
23721387
SW
337 * @vid: the VLAN ID
338 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
23721387 339 */
47ce5f1e 340static void batadv_bla_send_claim(struct batadv_priv *bat_priv, const u8 *mac,
eb2deb6b 341 unsigned short vid, int claimtype)
23721387
SW
342{
343 struct sk_buff *skb;
344 struct ethhdr *ethhdr;
56303d34 345 struct batadv_hard_iface *primary_if;
23721387 346 struct net_device *soft_iface;
6b5e971a 347 u8 *hw_src;
96412690 348 struct batadv_bla_claim_dst local_claim_dest;
3e2f1a1b 349 __be32 zeroip = 0;
23721387 350
e5d89254 351 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
352 if (!primary_if)
353 return;
354
807736f6 355 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
38ef3d1d 356 sizeof(local_claim_dest));
23721387
SW
357 local_claim_dest.type = claimtype;
358
359 soft_iface = primary_if->soft_iface;
360
361 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
362 /* IP DST: 0.0.0.0 */
363 zeroip,
364 primary_if->soft_iface,
365 /* IP SRC: 0.0.0.0 */
366 zeroip,
367 /* Ethernet DST: Broadcast */
368 NULL,
369 /* Ethernet SRC/HW SRC: originator mac */
370 primary_if->net_dev->dev_addr,
99e966fc 371 /* HW DST: FF:43:05:XX:YY:YY
23721387 372 * with XX = claim type
38ef3d1d 373 * and YY:YY = group id
23721387 374 */
6b5e971a 375 (u8 *)&local_claim_dest);
23721387
SW
376
377 if (!skb)
378 goto out;
379
380 ethhdr = (struct ethhdr *)skb->data;
6b5e971a 381 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
23721387
SW
382
383 /* now we pretend that the client would have sent this ... */
384 switch (claimtype) {
3eb8773e 385 case BATADV_CLAIM_TYPE_CLAIM:
23721387
SW
386 /* normal claim frame
387 * set Ethernet SRC to the clients mac
388 */
8fdd0153 389 ether_addr_copy(ethhdr->h_source, mac);
39c75a51 390 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 391 "%s(): CLAIM %pM on vid %d\n", __func__, mac,
f7a2bd65 392 batadv_print_vid(vid));
23721387 393 break;
3eb8773e 394 case BATADV_CLAIM_TYPE_UNCLAIM:
23721387
SW
395 /* unclaim frame
396 * set HW SRC to the clients mac
397 */
8fdd0153 398 ether_addr_copy(hw_src, mac);
39c75a51 399 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 400 "%s(): UNCLAIM %pM on vid %d\n", __func__, mac,
f7a2bd65 401 batadv_print_vid(vid));
23721387 402 break;
acd34afa 403 case BATADV_CLAIM_TYPE_ANNOUNCE:
23721387 404 /* announcement frame
791ad7f5 405 * set HW SRC to the special mac containing the crc
23721387 406 */
8fdd0153 407 ether_addr_copy(hw_src, mac);
39c75a51 408 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 409 "%s(): ANNOUNCE of %pM on vid %d\n", __func__,
f7a2bd65 410 ethhdr->h_source, batadv_print_vid(vid));
23721387 411 break;
acd34afa 412 case BATADV_CLAIM_TYPE_REQUEST:
23721387 413 /* request frame
99e966fc
SW
414 * set HW SRC and header destination to the receiving backbone
415 * gws mac
23721387 416 */
8fdd0153
AQ
417 ether_addr_copy(hw_src, mac);
418 ether_addr_copy(ethhdr->h_dest, mac);
39c75a51 419 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 420 "%s(): REQUEST of %pM to %pM on vid %d\n", __func__,
5f80df67 421 ethhdr->h_source, ethhdr->h_dest,
f7a2bd65 422 batadv_print_vid(vid));
23721387 423 break;
cd9c7bfb
SW
424 case BATADV_CLAIM_TYPE_LOOPDETECT:
425 ether_addr_copy(ethhdr->h_source, mac);
426 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
427 "%s(): LOOPDETECT of %pM to %pM on vid %d\n",
428 __func__, ethhdr->h_source, ethhdr->h_dest,
f7a2bd65 429 batadv_print_vid(vid));
cd9c7bfb
SW
430
431 break;
23721387
SW
432 }
433
10c78f58 434 if (vid & BATADV_VLAN_HAS_TAG) {
eb2deb6b
AQ
435 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
436 vid & VLAN_VID_MASK);
10c78f58
SE
437 if (!skb)
438 goto out;
439 }
23721387
SW
440
441 skb_reset_mac_header(skb);
442 skb->protocol = eth_type_trans(skb, soft_iface);
1c9b0550
ML
443 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
444 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
445 skb->len + ETH_HLEN);
23721387 446
94da81e2 447 netif_rx(skb);
23721387 448out:
79a0bffb 449 batadv_hardif_put(primary_if);
23721387
SW
450}
451
cd9c7bfb 452/**
7e9a8c2c 453 * batadv_bla_loopdetect_report() - worker for reporting the loop
cd9c7bfb
SW
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",
f7a2bd65 471 batadv_print_vid(backbone_gw->vid));
cd9c7bfb 472 snprintf(vid_str, sizeof(vid_str), "%d",
f7a2bd65 473 batadv_print_vid(backbone_gw->vid));
cd9c7bfb
SW
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/**
7e9a8c2c 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 *
47ce5f1e 492batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const 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,
22f0502e 505 "%s(): not found (%pM, %d), creating new entry\n", __func__,
f7a2bd65 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);
06e56ded 521 kref_init(&entry->refcount);
23721387 522
4e8389e1 523 kref_get(&entry->refcount);
807736f6 524 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
3b300de3
SE
525 batadv_compare_backbone_gw,
526 batadv_choose_backbone_gw, entry,
527 &entry->hash_entry);
23721387
SW
528
529 if (unlikely(hash_added != 0)) {
530 /* hash failed, free the structure */
531 kfree(entry);
532 return NULL;
533 }
534
95fb130d 535 /* this is a gateway now, remove any TT entry on this VLAN */
da641193 536 orig_node = batadv_orig_hash_find(bat_priv, orig);
20ff9d59 537 if (orig_node) {
95fb130d 538 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
08c36d3e 539 "became a backbone gateway");
5d967310 540 batadv_orig_node_put(orig_node);
20ff9d59 541 }
52aebd6a 542
d807f272 543 if (own_backbone) {
52aebd6a
SW
544 batadv_bla_send_announce(bat_priv, entry);
545
d807f272
SW
546 /* this will be decreased in the worker thread */
547 atomic_inc(&entry->request_sent);
28709878 548 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
d807f272
SW
549 atomic_inc(&bat_priv->bla.num_requests);
550 }
551
23721387
SW
552 return entry;
553}
554
04e14be6 555/**
7e9a8c2c 556 * batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN
04e14be6
SW
557 * @bat_priv: the bat priv with all the soft interface information
558 * @primary_if: the selected primary interface
559 * @vid: VLAN identifier
560 *
561 * update or add the own backbone gw to make sure we announce
23721387
SW
562 * where we receive other backbone gws
563 */
56303d34
SE
564static void
565batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
566 struct batadv_hard_iface *primary_if,
eb2deb6b 567 unsigned short vid)
23721387 568{
bae98774 569 struct batadv_bla_backbone_gw *backbone_gw;
23721387 570
3b300de3
SE
571 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
572 primary_if->net_dev->dev_addr,
52aebd6a 573 vid, true);
23721387
SW
574 if (unlikely(!backbone_gw))
575 return;
576
577 backbone_gw->lasttime = jiffies;
c8b86c12 578 batadv_backbone_gw_put(backbone_gw);
23721387
SW
579}
580
1b371d13 581/**
7e9a8c2c 582 * batadv_bla_answer_request() - answer a bla request by sending own claims
1b371d13 583 * @bat_priv: the bat priv with all the soft interface information
e3357189 584 * @primary_if: interface where the request came on
23721387
SW
585 * @vid: the vid where the request came on
586 *
587 * Repeat all of our own claims, and finally send an ANNOUNCE frame
588 * to allow the requester another check if the CRC is correct now.
589 */
56303d34
SE
590static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
591 struct batadv_hard_iface *primary_if,
eb2deb6b 592 unsigned short vid)
23721387 593{
23721387 594 struct hlist_head *head;
5bf74e9c 595 struct batadv_hashtable *hash;
712bbfe4 596 struct batadv_bla_claim *claim;
bae98774 597 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
598 int i;
599
39c75a51 600 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
601 "%s(): received a claim request, send all of our own claims again\n",
602 __func__);
23721387 603
3b300de3
SE
604 backbone_gw = batadv_backbone_hash_find(bat_priv,
605 primary_if->net_dev->dev_addr,
606 vid);
23721387
SW
607 if (!backbone_gw)
608 return;
609
807736f6 610 hash = bat_priv->bla.claim_hash;
23721387
SW
611 for (i = 0; i < hash->size; i++) {
612 head = &hash->table[i];
613
614 rcu_read_lock();
b67bfe0d 615 hlist_for_each_entry_rcu(claim, head, hash_entry) {
23721387
SW
616 /* only own claims are interesting */
617 if (claim->backbone_gw != backbone_gw)
618 continue;
619
3b300de3 620 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
3eb8773e 621 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
622 }
623 rcu_read_unlock();
624 }
625
626 /* finally, send an announcement frame */
3b300de3 627 batadv_bla_send_announce(bat_priv, backbone_gw);
c8b86c12 628 batadv_backbone_gw_put(backbone_gw);
23721387
SW
629}
630
1b371d13 631/**
7e9a8c2c 632 * batadv_bla_send_request() - send a request to repeat claims
1b371d13 633 * @backbone_gw: the backbone gateway from whom we are out of sync
23721387
SW
634 *
635 * When the crc is wrong, ask the backbone gateway for a full table update.
636 * After the request, it will repeat all of his own claims and finally
637 * send an announcement claim with which we can check again.
638 */
bae98774 639static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
23721387
SW
640{
641 /* first, remove all old entries */
3b300de3 642 batadv_bla_del_backbone_claims(backbone_gw);
23721387 643
39c75a51
SE
644 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
645 "Sending REQUEST to %pM\n", backbone_gw->orig);
23721387
SW
646
647 /* send request */
3b300de3 648 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
acd34afa 649 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
23721387
SW
650
651 /* no local broadcasts should be sent or received, for now. */
652 if (!atomic_read(&backbone_gw->request_sent)) {
807736f6 653 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
654 atomic_set(&backbone_gw->request_sent, 1);
655 }
656}
657
1b371d13 658/**
7e9a8c2c 659 * batadv_bla_send_announce() - Send an announcement frame
1b371d13 660 * @bat_priv: the bat priv with all the soft interface information
23721387 661 * @backbone_gw: our backbone gateway which should be announced
23721387 662 */
56303d34 663static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
bae98774 664 struct batadv_bla_backbone_gw *backbone_gw)
23721387 665{
6b5e971a 666 u8 mac[ETH_ALEN];
3e2f1a1b 667 __be16 crc;
23721387 668
3b300de3 669 memcpy(mac, batadv_announce_mac, 4);
5a1dd8a4 670 spin_lock_bh(&backbone_gw->crc_lock);
23721387 671 crc = htons(backbone_gw->crc);
5a1dd8a4 672 spin_unlock_bh(&backbone_gw->crc_lock);
1a5852d8 673 memcpy(&mac[4], &crc, 2);
23721387 674
3b300de3 675 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
acd34afa 676 BATADV_CLAIM_TYPE_ANNOUNCE);
23721387
SW
677}
678
2c53040f 679/**
7e9a8c2c 680 * batadv_bla_add_claim() - Adds a claim in the claim hash
2c53040f 681 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
682 * @mac: the mac address of the claim
683 * @vid: the VLAN ID of the frame
684 * @backbone_gw: the backbone gateway which claims it
23721387 685 */
56303d34 686static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
6b5e971a 687 const u8 *mac, const unsigned short vid,
bae98774 688 struct batadv_bla_backbone_gw *backbone_gw)
23721387 689{
3db0decf 690 struct batadv_bla_backbone_gw *old_backbone_gw;
712bbfe4
ML
691 struct batadv_bla_claim *claim;
692 struct batadv_bla_claim search_claim;
3db0decf 693 bool remove_crc = false;
23721387
SW
694 int hash_added;
695
8fdd0153 696 ether_addr_copy(search_claim.addr, mac);
23721387 697 search_claim.vid = vid;
3b300de3 698 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
699
700 /* create a new claim entry if it does not exist yet. */
701 if (!claim) {
702 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
703 if (!claim)
704 return;
705
8fdd0153 706 ether_addr_copy(claim->addr, mac);
3db0decf 707 spin_lock_init(&claim->backbone_lock);
23721387
SW
708 claim->vid = vid;
709 claim->lasttime = jiffies;
3db0decf 710 kref_get(&backbone_gw->refcount);
23721387 711 claim->backbone_gw = backbone_gw;
71b7e3d3 712 kref_init(&claim->refcount);
7282ac39 713
39c75a51 714 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
715 "%s(): adding new entry %pM, vid %d to hash ...\n",
716 __func__, mac, batadv_print_vid(vid));
7282ac39
SE
717
718 kref_get(&claim->refcount);
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,
22f0502e
SE
736 "%s(): changing ownership for %pM, vid %d to gw %pM\n",
737 __func__, mac, batadv_print_vid(vid),
738 backbone_gw->orig);
23721387 739
3db0decf 740 remove_crc = true;
23721387 741 }
3db0decf
SE
742
743 /* replace backbone_gw atomically and adjust reference counters */
744 spin_lock_bh(&claim->backbone_lock);
745 old_backbone_gw = claim->backbone_gw;
06e56ded 746 kref_get(&backbone_gw->refcount);
23721387 747 claim->backbone_gw = backbone_gw;
3db0decf 748 spin_unlock_bh(&claim->backbone_lock);
23721387 749
3db0decf
SE
750 if (remove_crc) {
751 /* remove claim address from old backbone_gw */
752 spin_lock_bh(&old_backbone_gw->crc_lock);
753 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
754 spin_unlock_bh(&old_backbone_gw->crc_lock);
755 }
23721387 756
3db0decf
SE
757 batadv_backbone_gw_put(old_backbone_gw);
758
759 /* add claim address to new backbone_gw */
5a1dd8a4 760 spin_lock_bh(&backbone_gw->crc_lock);
23721387 761 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
5a1dd8a4 762 spin_unlock_bh(&backbone_gw->crc_lock);
23721387
SW
763 backbone_gw->lasttime = jiffies;
764
765claim_free_ref:
321e3e08 766 batadv_claim_put(claim);
23721387
SW
767}
768
3db0decf 769/**
7e9a8c2c 770 * batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of
3db0decf
SE
771 * claim
772 * @claim: claim whose backbone_gw should be returned
773 *
774 * Return: valid reference to claim::backbone_gw
775 */
776static struct batadv_bla_backbone_gw *
777batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
778{
779 struct batadv_bla_backbone_gw *backbone_gw;
780
781 spin_lock_bh(&claim->backbone_lock);
782 backbone_gw = claim->backbone_gw;
783 kref_get(&backbone_gw->refcount);
784 spin_unlock_bh(&claim->backbone_lock);
785
786 return backbone_gw;
787}
788
04e14be6 789/**
7e9a8c2c 790 * batadv_bla_del_claim() - delete a claim from the claim hash
04e14be6
SW
791 * @bat_priv: the bat priv with all the soft interface information
792 * @mac: mac address of the claim to be removed
793 * @vid: VLAN id for the claim to be removed
23721387 794 */
56303d34 795static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
6b5e971a 796 const u8 *mac, const unsigned short vid)
23721387 797{
712bbfe4 798 struct batadv_bla_claim search_claim, *claim;
4ba104f4
SE
799 struct batadv_bla_claim *claim_removed_entry;
800 struct hlist_node *claim_removed_node;
23721387 801
8fdd0153 802 ether_addr_copy(search_claim.addr, mac);
23721387 803 search_claim.vid = vid;
3b300de3 804 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
805 if (!claim)
806 return;
807
22f0502e 808 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,
f7a2bd65 809 mac, batadv_print_vid(vid));
23721387 810
4ba104f4
SE
811 claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,
812 batadv_compare_claim,
813 batadv_choose_claim, claim);
814 if (!claim_removed_node)
815 goto free_claim;
23721387 816
4ba104f4
SE
817 /* reference from the hash is gone */
818 claim_removed_entry = hlist_entry(claim_removed_node,
819 struct batadv_bla_claim, hash_entry);
820 batadv_claim_put(claim_removed_entry);
821
822free_claim:
23721387 823 /* don't need the reference from hash_find() anymore */
321e3e08 824 batadv_claim_put(claim);
23721387
SW
825}
826
62fe710f 827/**
7e9a8c2c 828 * batadv_handle_announce() - check for ANNOUNCE frame
04e14be6
SW
829 * @bat_priv: the bat priv with all the soft interface information
830 * @an_addr: announcement mac address (ARP Sender HW address)
831 * @backbone_addr: originator address of the sender (Ethernet source MAC)
832 * @vid: the VLAN ID of the frame
62fe710f 833 *
4b426b10 834 * Return: true if handled
62fe710f 835 */
4b426b10
SE
836static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
837 u8 *backbone_addr, unsigned short vid)
23721387 838{
bae98774 839 struct batadv_bla_backbone_gw *backbone_gw;
5a1dd8a4 840 u16 backbone_crc, crc;
23721387 841
3b300de3 842 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
4b426b10 843 return false;
23721387 844
52aebd6a
SW
845 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
846 false);
23721387
SW
847
848 if (unlikely(!backbone_gw))
4b426b10 849 return true;
23721387 850
23721387
SW
851 /* handle as ANNOUNCE frame */
852 backbone_gw->lasttime = jiffies;
61a29286 853 crc = ntohs(*((__force __be16 *)(&an_addr[4])));
23721387 854
39c75a51 855 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
856 "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
857 __func__, batadv_print_vid(vid), backbone_gw->orig, crc);
23721387 858
5a1dd8a4
SW
859 spin_lock_bh(&backbone_gw->crc_lock);
860 backbone_crc = backbone_gw->crc;
861 spin_unlock_bh(&backbone_gw->crc_lock);
862
863 if (backbone_crc != crc) {
39c75a51 864 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
22f0502e
SE
865 "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
866 __func__, backbone_gw->orig,
f7a2bd65 867 batadv_print_vid(backbone_gw->vid),
5a1dd8a4 868 backbone_crc, crc);
23721387 869
3b300de3 870 batadv_bla_send_request(backbone_gw);
23721387
SW
871 } else {
872 /* if we have sent a request and the crc was OK,
873 * we can allow traffic again.
874 */
875 if (atomic_read(&backbone_gw->request_sent)) {
807736f6 876 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
877 atomic_set(&backbone_gw->request_sent, 0);
878 }
879 }
880
c8b86c12 881 batadv_backbone_gw_put(backbone_gw);
4b426b10 882 return true;
23721387
SW
883}
884
62fe710f 885/**
7e9a8c2c 886 * batadv_handle_request() - check for REQUEST frame
04e14be6
SW
887 * @bat_priv: the bat priv with all the soft interface information
888 * @primary_if: the primary hard interface of this batman soft interface
889 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
890 * @ethhdr: ethernet header of a packet
891 * @vid: the VLAN ID of the frame
62fe710f 892 *
4b426b10 893 * Return: true if handled
62fe710f 894 */
4b426b10
SE
895static bool batadv_handle_request(struct batadv_priv *bat_priv,
896 struct batadv_hard_iface *primary_if,
897 u8 *backbone_addr, struct ethhdr *ethhdr,
898 unsigned short vid)
23721387
SW
899{
900 /* check for REQUEST frame */
1eda58bf 901 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
4b426b10 902 return false;
23721387
SW
903
904 /* sanity check, this should not happen on a normal switch,
905 * we ignore it in this case.
906 */
1eda58bf 907 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
4b426b10 908 return true;
23721387 909
39c75a51 910 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
911 "%s(): REQUEST vid %d (sent by %pM)...\n",
912 __func__, batadv_print_vid(vid), ethhdr->h_source);
23721387 913
3b300de3 914 batadv_bla_answer_request(bat_priv, primary_if, vid);
4b426b10 915 return true;
23721387
SW
916}
917
62fe710f 918/**
7e9a8c2c 919 * batadv_handle_unclaim() - check for UNCLAIM frame
04e14be6
SW
920 * @bat_priv: the bat priv with all the soft interface information
921 * @primary_if: the primary hard interface of this batman soft interface
922 * @backbone_addr: originator address of the backbone (Ethernet source)
923 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
924 * @vid: the VLAN ID of the frame
62fe710f 925 *
4b426b10 926 * Return: true if handled
62fe710f 927 */
4b426b10
SE
928static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
929 struct batadv_hard_iface *primary_if,
47ce5f1e 930 const u8 *backbone_addr, const u8 *claim_addr,
4b426b10 931 unsigned short vid)
23721387 932{
bae98774 933 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
934
935 /* unclaim in any case if it is our own */
1eda58bf
SE
936 if (primary_if && batadv_compare_eth(backbone_addr,
937 primary_if->net_dev->dev_addr))
3b300de3 938 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 939 BATADV_CLAIM_TYPE_UNCLAIM);
23721387 940
3b300de3 941 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
23721387
SW
942
943 if (!backbone_gw)
4b426b10 944 return true;
23721387
SW
945
946 /* this must be an UNCLAIM frame */
39c75a51 947 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 948 "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,
f7a2bd65 949 claim_addr, batadv_print_vid(vid), backbone_gw->orig);
23721387 950
3b300de3 951 batadv_bla_del_claim(bat_priv, claim_addr, vid);
c8b86c12 952 batadv_backbone_gw_put(backbone_gw);
4b426b10 953 return true;
23721387
SW
954}
955
62fe710f 956/**
7e9a8c2c 957 * batadv_handle_claim() - check for CLAIM frame
04e14be6
SW
958 * @bat_priv: the bat priv with all the soft interface information
959 * @primary_if: the primary hard interface of this batman soft interface
960 * @backbone_addr: originator address of the backbone (Ethernet Source)
961 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
962 * @vid: the VLAN ID of the frame
62fe710f 963 *
4b426b10 964 * Return: true if handled
62fe710f 965 */
4b426b10
SE
966static bool batadv_handle_claim(struct batadv_priv *bat_priv,
967 struct batadv_hard_iface *primary_if,
47ce5f1e 968 const u8 *backbone_addr, const u8 *claim_addr,
4b426b10 969 unsigned short vid)
23721387 970{
bae98774 971 struct batadv_bla_backbone_gw *backbone_gw;
23721387
SW
972
973 /* register the gateway if not yet available, and add the claim. */
974
52aebd6a
SW
975 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
976 false);
23721387
SW
977
978 if (unlikely(!backbone_gw))
4b426b10 979 return true;
23721387
SW
980
981 /* this must be a CLAIM frame */
3b300de3 982 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
1eda58bf 983 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
3b300de3 984 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 985 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
986
987 /* TODO: we could call something like tt_local_del() here. */
988
c8b86c12 989 batadv_backbone_gw_put(backbone_gw);
4b426b10 990 return true;
23721387
SW
991}
992
2c53040f 993/**
7e9a8c2c 994 * batadv_check_claim_group() - check for claim group membership
2c53040f 995 * @bat_priv: the bat priv with all the soft interface information
e3357189 996 * @primary_if: the primary interface of this batman interface
38ef3d1d
SW
997 * @hw_src: the Hardware source in the ARP Header
998 * @hw_dst: the Hardware destination in the ARP Header
999 * @ethhdr: pointer to the Ethernet header of the claim frame
1000 *
bccb48c8 1001 * checks if it is a claim packet and if it's on the same group.
38ef3d1d
SW
1002 * This function also applies the group ID of the sender
1003 * if it is in the same mesh.
1004 *
62fe710f 1005 * Return:
38ef3d1d
SW
1006 * 2 - if it is a claim packet and on the same group
1007 * 1 - if is a claim packet from another group
1008 * 0 - if it is not a claim packet
1009 */
56303d34
SE
1010static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1011 struct batadv_hard_iface *primary_if,
6b5e971a 1012 u8 *hw_src, u8 *hw_dst,
3b300de3 1013 struct ethhdr *ethhdr)
38ef3d1d 1014{
6b5e971a 1015 u8 *backbone_addr;
56303d34 1016 struct batadv_orig_node *orig_node;
96412690 1017 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
38ef3d1d 1018
96412690 1019 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
807736f6 1020 bla_dst_own = &bat_priv->bla.claim_dest;
38ef3d1d 1021
38ef3d1d
SW
1022 /* if announcement packet, use the source,
1023 * otherwise assume it is in the hw_src
1024 */
1025 switch (bla_dst->type) {
3eb8773e 1026 case BATADV_CLAIM_TYPE_CLAIM:
38ef3d1d
SW
1027 backbone_addr = hw_src;
1028 break;
acd34afa
SE
1029 case BATADV_CLAIM_TYPE_REQUEST:
1030 case BATADV_CLAIM_TYPE_ANNOUNCE:
3eb8773e 1031 case BATADV_CLAIM_TYPE_UNCLAIM:
38ef3d1d
SW
1032 backbone_addr = ethhdr->h_source;
1033 break;
1034 default:
1035 return 0;
1036 }
1037
1038 /* don't accept claim frames from ourselves */
1eda58bf 1039 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
38ef3d1d
SW
1040 return 0;
1041
1042 /* if its already the same group, it is fine. */
1043 if (bla_dst->group == bla_dst_own->group)
1044 return 2;
1045
1046 /* lets see if this originator is in our mesh */
da641193 1047 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
38ef3d1d 1048
791ad7f5 1049 /* don't accept claims from gateways which are not in
38ef3d1d
SW
1050 * the same mesh or group.
1051 */
1052 if (!orig_node)
1053 return 1;
1054
1055 /* if our mesh friends mac is bigger, use it for ourselves. */
1056 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
39c75a51 1057 batadv_dbg(BATADV_DBG_BLA, bat_priv,
39a32991 1058 "taking other backbones claim group: %#.4x\n",
1eda58bf 1059 ntohs(bla_dst->group));
38ef3d1d
SW
1060 bla_dst_own->group = bla_dst->group;
1061 }
1062
5d967310 1063 batadv_orig_node_put(orig_node);
38ef3d1d
SW
1064
1065 return 2;
1066}
1067
1b371d13 1068/**
7e9a8c2c 1069 * batadv_bla_process_claim() - Check if this is a claim frame, and process it
1b371d13 1070 * @bat_priv: the bat priv with all the soft interface information
e3357189 1071 * @primary_if: the primary hard interface of this batman soft interface
23721387
SW
1072 * @skb: the frame to be checked
1073 *
4b426b10 1074 * Return: true if it was a claim frame, otherwise return false to
23721387
SW
1075 * tell the callee that it can use the frame on its own.
1076 */
4b426b10
SE
1077static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1078 struct batadv_hard_iface *primary_if,
1079 struct sk_buff *skb)
23721387 1080{
d46b6bfa 1081 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
6b5e971a 1082 u8 *hw_src, *hw_dst;
d46b6bfa 1083 struct vlan_hdr *vhdr, vhdr_buf;
c018ad3d 1084 struct ethhdr *ethhdr;
23721387 1085 struct arphdr *arphdr;
c018ad3d 1086 unsigned short vid;
d46b6bfa 1087 int vlan_depth = 0;
293e9338 1088 __be16 proto;
23721387 1089 int headlen;
38ef3d1d 1090 int ret;
23721387 1091
c018ad3d 1092 vid = batadv_get_vid(skb, 0);
7ed4be95 1093 ethhdr = eth_hdr(skb);
23721387 1094
c018ad3d
AQ
1095 proto = ethhdr->h_proto;
1096 headlen = ETH_HLEN;
1097 if (vid & BATADV_VLAN_HAS_TAG) {
d46b6bfa
SW
1098 /* Traverse the VLAN/Ethertypes.
1099 *
1100 * At this point it is known that the first protocol is a VLAN
1101 * header, so start checking at the encapsulated protocol.
1102 *
1103 * The depth of the VLAN headers is recorded to drop BLA claim
1104 * frames encapsulated into multiple VLAN headers (QinQ).
1105 */
1106 do {
1107 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1108 &vhdr_buf);
1109 if (!vhdr)
4b426b10 1110 return false;
d46b6bfa
SW
1111
1112 proto = vhdr->h_vlan_encapsulated_proto;
1113 headlen += VLAN_HLEN;
1114 vlan_depth++;
1115 } while (proto == htons(ETH_P_8021Q));
23721387
SW
1116 }
1117
293e9338 1118 if (proto != htons(ETH_P_ARP))
4b426b10 1119 return false; /* not a claim frame */
23721387
SW
1120
1121 /* this must be a ARP frame. check if it is a claim. */
1122
1123 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
4b426b10 1124 return false;
23721387
SW
1125
1126 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
7ed4be95 1127 ethhdr = eth_hdr(skb);
6b5e971a 1128 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
23721387
SW
1129
1130 /* Check whether the ARP frame carries a valid
1131 * IP information
1132 */
23721387 1133 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
4b426b10 1134 return false;
23721387 1135 if (arphdr->ar_pro != htons(ETH_P_IP))
4b426b10 1136 return false;
23721387 1137 if (arphdr->ar_hln != ETH_ALEN)
4b426b10 1138 return false;
23721387 1139 if (arphdr->ar_pln != 4)
4b426b10 1140 return false;
23721387 1141
6b5e971a 1142 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
23721387 1143 hw_dst = hw_src + ETH_ALEN + 4;
96412690 1144 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
d46b6bfa
SW
1145 bla_dst_own = &bat_priv->bla.claim_dest;
1146
1147 /* check if it is a claim frame in general */
1148 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1149 sizeof(bla_dst->magic)) != 0)
4b426b10 1150 return false;
d46b6bfa
SW
1151
1152 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1153 * drop that, as this is not supported by BLA but should also not be
1154 * sent via the mesh.
1155 */
1156 if (vlan_depth > 1)
4b426b10 1157 return true;
23721387 1158
cd9c7bfb
SW
1159 /* Let the loopdetect frames on the mesh in any case. */
1160 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
4fd261bf 1161 return false;
cd9c7bfb 1162
23721387 1163 /* check if it is a claim frame. */
3b300de3
SE
1164 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1165 ethhdr);
38ef3d1d 1166 if (ret == 1)
39c75a51 1167 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
1168 "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1169 __func__, ethhdr->h_source, batadv_print_vid(vid),
1170 hw_src, hw_dst);
38ef3d1d
SW
1171
1172 if (ret < 2)
4b426b10 1173 return !!ret;
23721387
SW
1174
1175 /* become a backbone gw ourselves on this vlan if not happened yet */
3b300de3 1176 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
23721387
SW
1177
1178 /* check for the different types of claim frames ... */
1179 switch (bla_dst->type) {
3eb8773e 1180 case BATADV_CLAIM_TYPE_CLAIM:
3b300de3
SE
1181 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1182 ethhdr->h_source, vid))
4b426b10 1183 return true;
23721387 1184 break;
3eb8773e 1185 case BATADV_CLAIM_TYPE_UNCLAIM:
3b300de3
SE
1186 if (batadv_handle_unclaim(bat_priv, primary_if,
1187 ethhdr->h_source, hw_src, vid))
4b426b10 1188 return true;
23721387
SW
1189 break;
1190
acd34afa 1191 case BATADV_CLAIM_TYPE_ANNOUNCE:
3b300de3
SE
1192 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1193 vid))
4b426b10 1194 return true;
23721387 1195 break;
acd34afa 1196 case BATADV_CLAIM_TYPE_REQUEST:
3b300de3
SE
1197 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1198 vid))
4b426b10 1199 return true;
23721387
SW
1200 break;
1201 }
1202
39c75a51 1203 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
1204 "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1205 __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,
1206 hw_dst);
4b426b10 1207 return true;
23721387
SW
1208}
1209
04e14be6 1210/**
7e9a8c2c 1211 * batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or
04e14be6
SW
1212 * immediately
1213 * @bat_priv: the bat priv with all the soft interface information
1214 * @now: whether the whole hash shall be wiped now
1215 *
1216 * Check when we last heard from other nodes, and remove them in case of
23721387
SW
1217 * a time out, or clean all backbone gws if now is set.
1218 */
56303d34 1219static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
23721387 1220{
bae98774 1221 struct batadv_bla_backbone_gw *backbone_gw;
b67bfe0d 1222 struct hlist_node *node_tmp;
23721387 1223 struct hlist_head *head;
5bf74e9c 1224 struct batadv_hashtable *hash;
23721387
SW
1225 spinlock_t *list_lock; /* protects write access to the hash lists */
1226 int i;
1227
807736f6 1228 hash = bat_priv->bla.backbone_hash;
23721387
SW
1229 if (!hash)
1230 return;
1231
1232 for (i = 0; i < hash->size; i++) {
1233 head = &hash->table[i];
1234 list_lock = &hash->list_locks[i];
1235
1236 spin_lock_bh(list_lock);
b67bfe0d 1237 hlist_for_each_entry_safe(backbone_gw, node_tmp,
23721387
SW
1238 head, hash_entry) {
1239 if (now)
1240 goto purge_now;
1eda58bf 1241 if (!batadv_has_timed_out(backbone_gw->lasttime,
42d0b044 1242 BATADV_BLA_BACKBONE_TIMEOUT))
23721387
SW
1243 continue;
1244
39c75a51 1245 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
22f0502e
SE
1246 "%s(): backbone gw %pM timed out\n",
1247 __func__, backbone_gw->orig);
23721387
SW
1248
1249purge_now:
1250 /* don't wait for the pending request anymore */
1251 if (atomic_read(&backbone_gw->request_sent))
807736f6 1252 atomic_dec(&bat_priv->bla.num_requests);
23721387 1253
3b300de3 1254 batadv_bla_del_backbone_claims(backbone_gw);
23721387 1255
b67bfe0d 1256 hlist_del_rcu(&backbone_gw->hash_entry);
c8b86c12 1257 batadv_backbone_gw_put(backbone_gw);
23721387
SW
1258 }
1259 spin_unlock_bh(list_lock);
1260 }
1261}
1262
2c53040f 1263/**
7e9a8c2c 1264 * batadv_bla_purge_claims() - Remove claims after a timeout or immediately
2c53040f 1265 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1266 * @primary_if: the selected primary interface, may be NULL if now is set
1267 * @now: whether the whole hash shall be wiped now
1268 *
1269 * Check when we heard last time from our own claims, and remove them in case of
1270 * a time out, or clean all claims if now is set
1271 */
56303d34
SE
1272static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1273 struct batadv_hard_iface *primary_if,
1274 int now)
23721387 1275{
3db0decf 1276 struct batadv_bla_backbone_gw *backbone_gw;
712bbfe4 1277 struct batadv_bla_claim *claim;
23721387 1278 struct hlist_head *head;
5bf74e9c 1279 struct batadv_hashtable *hash;
23721387
SW
1280 int i;
1281
807736f6 1282 hash = bat_priv->bla.claim_hash;
23721387
SW
1283 if (!hash)
1284 return;
1285
1286 for (i = 0; i < hash->size; i++) {
1287 head = &hash->table[i];
1288
1289 rcu_read_lock();
b67bfe0d 1290 hlist_for_each_entry_rcu(claim, head, hash_entry) {
3db0decf 1291 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
23721387
SW
1292 if (now)
1293 goto purge_now;
3db0decf
SE
1294
1295 if (!batadv_compare_eth(backbone_gw->orig,
1eda58bf 1296 primary_if->net_dev->dev_addr))
3db0decf
SE
1297 goto skip;
1298
1eda58bf 1299 if (!batadv_has_timed_out(claim->lasttime,
42d0b044 1300 BATADV_BLA_CLAIM_TIMEOUT))
3db0decf 1301 goto skip;
23721387 1302
39c75a51 1303 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 1304 "%s(): timed out.\n", __func__);
23721387
SW
1305
1306purge_now:
4dd72f73 1307 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e 1308 "%s(): %pM, vid %d\n", __func__,
4dd72f73
AP
1309 claim->addr, claim->vid);
1310
3b300de3 1311 batadv_handle_unclaim(bat_priv, primary_if,
3db0decf 1312 backbone_gw->orig,
3b300de3 1313 claim->addr, claim->vid);
3db0decf
SE
1314skip:
1315 batadv_backbone_gw_put(backbone_gw);
23721387
SW
1316 }
1317 rcu_read_unlock();
1318 }
1319}
1320
2c53040f 1321/**
7e9a8c2c 1322 * batadv_bla_update_orig_address() - Update the backbone gateways when the own
04e14be6 1323 * originator address changes
2c53040f 1324 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1325 * @primary_if: the new selected primary_if
1326 * @oldif: the old primary interface, may be NULL
23721387 1327 */
56303d34
SE
1328void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1329 struct batadv_hard_iface *primary_if,
1330 struct batadv_hard_iface *oldif)
23721387 1331{
bae98774 1332 struct batadv_bla_backbone_gw *backbone_gw;
23721387 1333 struct hlist_head *head;
5bf74e9c 1334 struct batadv_hashtable *hash;
807736f6 1335 __be16 group;
23721387
SW
1336 int i;
1337
38ef3d1d 1338 /* reset bridge loop avoidance group id */
807736f6
SE
1339 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1340 bat_priv->bla.claim_dest.group = group;
38ef3d1d 1341
d5b4c93e
SW
1342 /* purge everything when bridge loop avoidance is turned off */
1343 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1344 oldif = NULL;
1345
23721387 1346 if (!oldif) {
3b300de3
SE
1347 batadv_bla_purge_claims(bat_priv, NULL, 1);
1348 batadv_bla_purge_backbone_gw(bat_priv, 1);
23721387
SW
1349 return;
1350 }
1351
807736f6 1352 hash = bat_priv->bla.backbone_hash;
23721387
SW
1353 if (!hash)
1354 return;
1355
1356 for (i = 0; i < hash->size; i++) {
1357 head = &hash->table[i];
1358
1359 rcu_read_lock();
b67bfe0d 1360 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
23721387 1361 /* own orig still holds the old value. */
1eda58bf
SE
1362 if (!batadv_compare_eth(backbone_gw->orig,
1363 oldif->net_dev->dev_addr))
23721387
SW
1364 continue;
1365
8fdd0153
AQ
1366 ether_addr_copy(backbone_gw->orig,
1367 primary_if->net_dev->dev_addr);
23721387
SW
1368 /* send an announce frame so others will ask for our
1369 * claims and update their tables.
1370 */
3b300de3 1371 batadv_bla_send_announce(bat_priv, backbone_gw);
23721387
SW
1372 }
1373 rcu_read_unlock();
1374 }
1375}
1376
cd9c7bfb 1377/**
7e9a8c2c 1378 * batadv_bla_send_loopdetect() - send a loopdetect frame
cd9c7bfb
SW
1379 * @bat_priv: the bat priv with all the soft interface information
1380 * @backbone_gw: the backbone gateway for which a loop should be detected
1381 *
1382 * To detect loops that the bridge loop avoidance can't handle, send a loop
1383 * detection packet on the backbone. Unlike other BLA frames, this frame will
1384 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1385 * indicates that there is a loop.
1386 */
1387static void
1388batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1389 struct batadv_bla_backbone_gw *backbone_gw)
1390{
1391 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1392 backbone_gw->vid);
1393 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1394 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1395}
1396
d68081a2 1397/**
7e9a8c2c 1398 * batadv_bla_status_update() - purge bla interfaces if necessary
d68081a2
SW
1399 * @net_dev: the soft interface net device
1400 */
1401void batadv_bla_status_update(struct net_device *net_dev)
1402{
1403 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1404 struct batadv_hard_iface *primary_if;
1405
1406 primary_if = batadv_primary_if_get_selected(bat_priv);
1407 if (!primary_if)
1408 return;
1409
1410 /* this function already purges everything when bla is disabled,
1411 * so just call that one.
1412 */
1413 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
82047ad7 1414 batadv_hardif_put(primary_if);
d68081a2
SW
1415}
1416
04e14be6 1417/**
7e9a8c2c 1418 * batadv_bla_periodic_work() - performs periodic bla work
04e14be6
SW
1419 * @work: kernel work struct
1420 *
1421 * periodic work to do:
23721387
SW
1422 * * purge structures when they are too old
1423 * * send announcements
1424 */
3b300de3 1425static void batadv_bla_periodic_work(struct work_struct *work)
23721387 1426{
bbb1f90e 1427 struct delayed_work *delayed_work;
56303d34 1428 struct batadv_priv *bat_priv;
807736f6 1429 struct batadv_priv_bla *priv_bla;
23721387 1430 struct hlist_head *head;
bae98774 1431 struct batadv_bla_backbone_gw *backbone_gw;
5bf74e9c 1432 struct batadv_hashtable *hash;
56303d34 1433 struct batadv_hard_iface *primary_if;
cd9c7bfb 1434 bool send_loopdetect = false;
23721387
SW
1435 int i;
1436
4ba4bc0f 1437 delayed_work = to_delayed_work(work);
807736f6
SE
1438 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1439 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
e5d89254 1440 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1441 if (!primary_if)
1442 goto out;
1443
3b300de3
SE
1444 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1445 batadv_bla_purge_backbone_gw(bat_priv, 0);
23721387
SW
1446
1447 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1448 goto out;
1449
cd9c7bfb
SW
1450 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1451 /* set a new random mac address for the next bridge loop
1452 * detection frames. Set the locally administered bit to avoid
1453 * collisions with users mac addresses.
1454 */
6c1f0a1f 1455 eth_random_addr(bat_priv->bla.loopdetect_addr);
cd9c7bfb
SW
1456 bat_priv->bla.loopdetect_addr[0] = 0xba;
1457 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1458 bat_priv->bla.loopdetect_lasttime = jiffies;
1459 atomic_set(&bat_priv->bla.loopdetect_next,
1460 BATADV_BLA_LOOPDETECT_PERIODS);
1461
1462 /* mark for sending loop detect on all VLANs */
1463 send_loopdetect = true;
1464 }
1465
807736f6 1466 hash = bat_priv->bla.backbone_hash;
23721387
SW
1467 if (!hash)
1468 goto out;
1469
1470 for (i = 0; i < hash->size; i++) {
1471 head = &hash->table[i];
1472
1473 rcu_read_lock();
b67bfe0d 1474 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1eda58bf
SE
1475 if (!batadv_compare_eth(backbone_gw->orig,
1476 primary_if->net_dev->dev_addr))
23721387
SW
1477 continue;
1478
1479 backbone_gw->lasttime = jiffies;
1480
3b300de3 1481 batadv_bla_send_announce(bat_priv, backbone_gw);
cd9c7bfb
SW
1482 if (send_loopdetect)
1483 batadv_bla_send_loopdetect(bat_priv,
1484 backbone_gw);
d807f272
SW
1485
1486 /* request_sent is only set after creation to avoid
1487 * problems when we are not yet known as backbone gw
1488 * in the backbone.
1489 *
28709878
SW
1490 * We can reset this now after we waited some periods
1491 * to give bridge forward delays and bla group forming
1492 * some grace time.
d807f272
SW
1493 */
1494
1495 if (atomic_read(&backbone_gw->request_sent) == 0)
1496 continue;
1497
28709878
SW
1498 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1499 continue;
1500
d807f272
SW
1501 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1502 atomic_set(&backbone_gw->request_sent, 0);
23721387
SW
1503 }
1504 rcu_read_unlock();
1505 }
1506out:
79a0bffb 1507 batadv_hardif_put(primary_if);
23721387 1508
72414442
AQ
1509 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1510 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
23721387
SW
1511}
1512
5d52dad2
SE
1513/* The hash for claim and backbone hash receive the same key because they
1514 * are getting initialized by hash_new with the same key. Reinitializing
1515 * them with to different keys to allow nested locking without generating
1516 * lockdep warnings
1517 */
3b300de3
SE
1518static struct lock_class_key batadv_claim_hash_lock_class_key;
1519static struct lock_class_key batadv_backbone_hash_lock_class_key;
5d52dad2 1520
04e14be6 1521/**
7e9a8c2c 1522 * batadv_bla_init() - initialize all bla structures
04e14be6
SW
1523 * @bat_priv: the bat priv with all the soft interface information
1524 *
1525 * Return: 0 on success, < 0 on error.
1526 */
56303d34 1527int batadv_bla_init(struct batadv_priv *bat_priv)
23721387 1528{
fe2da6ff 1529 int i;
6b5e971a 1530 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
56303d34 1531 struct batadv_hard_iface *primary_if;
6b5e971a 1532 u16 crc;
807736f6 1533 unsigned long entrytime;
fe2da6ff 1534
7dac7b76
LL
1535 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1536
39c75a51 1537 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
23721387 1538
38ef3d1d 1539 /* setting claim destination address */
807736f6
SE
1540 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1541 bat_priv->bla.claim_dest.type = 0;
e5d89254 1542 primary_if = batadv_primary_if_get_selected(bat_priv);
38ef3d1d 1543 if (primary_if) {
807736f6
SE
1544 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1545 bat_priv->bla.claim_dest.group = htons(crc);
82047ad7 1546 batadv_hardif_put(primary_if);
38ef3d1d 1547 } else {
807736f6 1548 bat_priv->bla.claim_dest.group = 0; /* will be set later */
38ef3d1d
SW
1549 }
1550
fe2da6ff 1551 /* initialize the duplicate list */
807736f6 1552 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
42d0b044 1553 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
807736f6
SE
1554 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1555 bat_priv->bla.bcast_duplist_curr = 0;
fe2da6ff 1556
cd9c7bfb
SW
1557 atomic_set(&bat_priv->bla.loopdetect_next,
1558 BATADV_BLA_LOOPDETECT_PERIODS);
1559
807736f6 1560 if (bat_priv->bla.claim_hash)
5346c35e 1561 return 0;
23721387 1562
807736f6 1563 bat_priv->bla.claim_hash = batadv_hash_new(128);
6f68cd63
PS
1564 if (!bat_priv->bla.claim_hash)
1565 return -ENOMEM;
23721387 1566
6f68cd63
PS
1567 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1568 if (!bat_priv->bla.backbone_hash) {
1569 batadv_hash_destroy(bat_priv->bla.claim_hash);
5346c35e 1570 return -ENOMEM;
6f68cd63 1571 }
23721387 1572
807736f6 1573 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
3b300de3 1574 &batadv_claim_hash_lock_class_key);
807736f6 1575 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
3b300de3 1576 &batadv_backbone_hash_lock_class_key);
5d52dad2 1577
39c75a51 1578 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
23721387 1579
72414442
AQ
1580 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1581
1582 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1583 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
5346c35e 1584 return 0;
23721387
SW
1585}
1586
2c53040f 1587/**
2369e827 1588 * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
2c53040f 1589 * @bat_priv: the bat priv with all the soft interface information
2369e827
LL
1590 * @skb: contains the multicast packet to be checked
1591 * @payload_ptr: pointer to position inside the head buffer of the skb
1592 * marking the start of the data to be CRC'ed
1593 * @orig: originator mac address, NULL if unknown
fe2da6ff 1594 *
2369e827
LL
1595 * Check if it is on our broadcast list. Another gateway might have sent the
1596 * same packet because it is connected to the same backbone, so we have to
1597 * remove this duplicate.
fe2da6ff
SW
1598 *
1599 * This is performed by checking the CRC, which will tell us
1600 * with a good chance that it is the same packet. If it is furthermore
1601 * sent by another host, drop it. We allow equal packets from
1602 * the same host however as this might be intended.
04e14be6 1603 *
4b426b10 1604 * Return: true if a packet is in the duplicate list, false otherwise.
9cfc7bd6 1605 */
2369e827
LL
1606static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
1607 struct sk_buff *skb, u8 *payload_ptr,
1608 const u8 *orig)
fe2da6ff 1609{
56303d34 1610 struct batadv_bcast_duplist_entry *entry;
4b426b10 1611 bool ret = false;
2369e827
LL
1612 int i, curr;
1613 __be32 crc;
fe2da6ff
SW
1614
1615 /* calculate the crc ... */
2369e827 1616 crc = batadv_skb_crc32(skb, payload_ptr);
fe2da6ff 1617
7dac7b76
LL
1618 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1619
42d0b044 1620 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
807736f6
SE
1621 curr = (bat_priv->bla.bcast_duplist_curr + i);
1622 curr %= BATADV_DUPLIST_SIZE;
1623 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1624
1625 /* we can stop searching if the entry is too old ;
1626 * later entries will be even older
1627 */
42d0b044
SE
1628 if (batadv_has_timed_out(entry->entrytime,
1629 BATADV_DUPLIST_TIMEOUT))
fe2da6ff
SW
1630 break;
1631
1632 if (entry->crc != crc)
1633 continue;
1634
2369e827
LL
1635 /* are the originators both known and not anonymous? */
1636 if (orig && !is_zero_ether_addr(orig) &&
1637 !is_zero_ether_addr(entry->orig)) {
1638 /* If known, check if the new frame came from
1639 * the same originator:
1640 * We are safe to take identical frames from the
1641 * same orig, if known, as multiplications in
1642 * the mesh are detected via the (orig, seqno) pair.
1643 * So we can be a bit more liberal here and allow
1644 * identical frames from the same orig which the source
1645 * host might have sent multiple times on purpose.
1646 */
1647 if (batadv_compare_eth(entry->orig, orig))
1648 continue;
1649 }
fe2da6ff
SW
1650
1651 /* this entry seems to match: same crc, not too old,
4b426b10 1652 * and from another gw. therefore return true to forbid it.
fe2da6ff 1653 */
4b426b10 1654 ret = true;
7dac7b76 1655 goto out;
fe2da6ff 1656 }
7dac7b76 1657 /* not found, add a new entry (overwrite the oldest entry)
3f68785e 1658 * and allow it, its the first occurrence.
7dac7b76 1659 */
807736f6 1660 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
42d0b044 1661 curr %= BATADV_DUPLIST_SIZE;
807736f6 1662 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1663 entry->crc = crc;
1664 entry->entrytime = jiffies;
2369e827
LL
1665
1666 /* known originator */
1667 if (orig)
1668 ether_addr_copy(entry->orig, orig);
1669 /* anonymous originator */
1670 else
1671 eth_zero_addr(entry->orig);
1672
807736f6 1673 bat_priv->bla.bcast_duplist_curr = curr;
fe2da6ff 1674
7dac7b76
LL
1675out:
1676 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1677
1678 return ret;
fe2da6ff
SW
1679}
1680
2369e827
LL
1681/**
1682 * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
1683 * @bat_priv: the bat priv with all the soft interface information
1684 * @skb: contains the multicast packet to be checked, decapsulated from a
1685 * unicast_packet
1686 *
1687 * Check if it is on our broadcast list. Another gateway might have sent the
1688 * same packet because it is connected to the same backbone, so we have to
1689 * remove this duplicate.
1690 *
1691 * Return: true if a packet is in the duplicate list, false otherwise.
1692 */
1693static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
1694 struct sk_buff *skb)
1695{
1696 return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);
1697}
1698
1699/**
1700 * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
1701 * @bat_priv: the bat priv with all the soft interface information
1702 * @skb: contains the bcast_packet to be checked
1703 *
1704 * Check if it is on our broadcast list. Another gateway might have sent the
1705 * same packet because it is connected to the same backbone, so we have to
1706 * remove this duplicate.
1707 *
1708 * Return: true if a packet is in the duplicate list, false otherwise.
1709 */
1710bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1711 struct sk_buff *skb)
1712{
1713 struct batadv_bcast_packet *bcast_packet;
1714 u8 *payload_ptr;
1715
1716 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1717 payload_ptr = (u8 *)(bcast_packet + 1);
1718
1719 return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,
1720 bcast_packet->orig);
1721}
1722
1b371d13 1723/**
7e9a8c2c 1724 * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for
04e14be6 1725 * the VLAN identified by vid.
1b371d13 1726 * @bat_priv: the bat priv with all the soft interface information
20ff9d59 1727 * @orig: originator mac address
cfd4f757 1728 * @vid: VLAN identifier
20ff9d59 1729 *
62fe710f 1730 * Return: true if orig is a backbone for this vid, false otherwise.
20ff9d59 1731 */
6b5e971a 1732bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
cfd4f757 1733 unsigned short vid)
20ff9d59 1734{
807736f6 1735 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
20ff9d59 1736 struct hlist_head *head;
bae98774 1737 struct batadv_bla_backbone_gw *backbone_gw;
20ff9d59
SW
1738 int i;
1739
1740 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
cfd4f757 1741 return false;
20ff9d59
SW
1742
1743 if (!hash)
cfd4f757 1744 return false;
20ff9d59
SW
1745
1746 for (i = 0; i < hash->size; i++) {
1747 head = &hash->table[i];
1748
1749 rcu_read_lock();
b67bfe0d 1750 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
cfd4f757
AQ
1751 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1752 backbone_gw->vid == vid) {
20ff9d59 1753 rcu_read_unlock();
cfd4f757 1754 return true;
20ff9d59
SW
1755 }
1756 }
1757 rcu_read_unlock();
1758 }
1759
cfd4f757 1760 return false;
20ff9d59
SW
1761}
1762
2c53040f 1763/**
7e9a8c2c 1764 * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN
2c53040f 1765 * @skb: the frame to be checked
23721387
SW
1766 * @orig_node: the orig_node of the frame
1767 * @hdr_size: maximum length of the frame
1768 *
4b426b10
SE
1769 * Return: true if the orig_node is also a gateway on the soft interface,
1770 * otherwise it returns false.
23721387 1771 */
4b426b10
SE
1772bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1773 struct batadv_orig_node *orig_node, int hdr_size)
23721387 1774{
bae98774 1775 struct batadv_bla_backbone_gw *backbone_gw;
c018ad3d 1776 unsigned short vid;
23721387
SW
1777
1778 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
4b426b10 1779 return false;
23721387
SW
1780
1781 /* first, find out the vid. */
0d125074 1782 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
4b426b10 1783 return false;
23721387 1784
c018ad3d 1785 vid = batadv_get_vid(skb, hdr_size);
23721387
SW
1786
1787 /* see if this originator is a backbone gw for this VLAN */
3b300de3
SE
1788 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1789 orig_node->orig, vid);
23721387 1790 if (!backbone_gw)
4b426b10 1791 return false;
23721387 1792
c8b86c12 1793 batadv_backbone_gw_put(backbone_gw);
4b426b10 1794 return true;
23721387
SW
1795}
1796
04e14be6 1797/**
7e9a8c2c 1798 * batadv_bla_free() - free all bla structures
04e14be6
SW
1799 * @bat_priv: the bat priv with all the soft interface information
1800 *
1801 * for softinterface free or module unload
1802 */
56303d34 1803void batadv_bla_free(struct batadv_priv *bat_priv)
23721387 1804{
56303d34 1805 struct batadv_hard_iface *primary_if;
23721387 1806
807736f6 1807 cancel_delayed_work_sync(&bat_priv->bla.work);
e5d89254 1808 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387 1809
807736f6 1810 if (bat_priv->bla.claim_hash) {
3b300de3 1811 batadv_bla_purge_claims(bat_priv, primary_if, 1);
807736f6
SE
1812 batadv_hash_destroy(bat_priv->bla.claim_hash);
1813 bat_priv->bla.claim_hash = NULL;
23721387 1814 }
807736f6 1815 if (bat_priv->bla.backbone_hash) {
3b300de3 1816 batadv_bla_purge_backbone_gw(bat_priv, 1);
807736f6
SE
1817 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1818 bat_priv->bla.backbone_hash = NULL;
23721387 1819 }
79a0bffb 1820 batadv_hardif_put(primary_if);
23721387
SW
1821}
1822
cd9c7bfb 1823/**
7e9a8c2c 1824 * batadv_bla_loopdetect_check() - check and handle a detected loop
cd9c7bfb
SW
1825 * @bat_priv: the bat priv with all the soft interface information
1826 * @skb: the packet to check
1827 * @primary_if: interface where the request came on
1828 * @vid: the VLAN ID of the frame
1829 *
1830 * Checks if this packet is a loop detect frame which has been sent by us,
bccb48c8 1831 * throws an uevent and logs the event if that is the case.
cd9c7bfb
SW
1832 *
1833 * Return: true if it is a loop detect frame which is to be dropped, false
1834 * otherwise.
1835 */
1836static bool
1837batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1838 struct batadv_hard_iface *primary_if,
1839 unsigned short vid)
1840{
1841 struct batadv_bla_backbone_gw *backbone_gw;
1842 struct ethhdr *ethhdr;
5af96b9c 1843 bool ret;
cd9c7bfb
SW
1844
1845 ethhdr = eth_hdr(skb);
1846
1847 /* Only check for the MAC address and skip more checks here for
1848 * performance reasons - this function is on the hotpath, after all.
1849 */
1850 if (!batadv_compare_eth(ethhdr->h_source,
1851 bat_priv->bla.loopdetect_addr))
1852 return false;
1853
1854 /* If the packet came too late, don't forward it on the mesh
1855 * but don't consider that as loop. It might be a coincidence.
1856 */
1857 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1858 BATADV_BLA_LOOPDETECT_TIMEOUT))
1859 return true;
1860
1861 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1862 primary_if->net_dev->dev_addr,
1863 vid, true);
1864 if (unlikely(!backbone_gw))
1865 return true;
1866
5af96b9c
ML
1867 ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1868
21ba5ab2 1869 /* backbone_gw is unreferenced in the report work function
5af96b9c
ML
1870 * if queue_work() call was successful
1871 */
1872 if (!ret)
1873 batadv_backbone_gw_put(backbone_gw);
cd9c7bfb
SW
1874
1875 return true;
1876}
1877
2c53040f 1878/**
7e9a8c2c 1879 * batadv_bla_rx() - check packets coming from the mesh.
2c53040f 1880 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1881 * @skb: the frame to be checked
1882 * @vid: the VLAN ID of the frame
74c09b72 1883 * @packet_type: the batman packet type this frame came in
23721387 1884 *
04e14be6 1885 * batadv_bla_rx avoidance checks if:
23721387
SW
1886 * * we have to race for a claim
1887 * * if the frame is allowed on the LAN
1888 *
bccb48c8 1889 * In these cases, the skb is further handled by this function
62fe710f 1890 *
4b426b10
SE
1891 * Return: true if handled, otherwise it returns false and the caller shall
1892 * further process the skb.
23721387 1893 */
4b426b10 1894bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
74c09b72 1895 unsigned short vid, int packet_type)
23721387 1896{
3db0decf 1897 struct batadv_bla_backbone_gw *backbone_gw;
23721387 1898 struct ethhdr *ethhdr;
712bbfe4 1899 struct batadv_bla_claim search_claim, *claim = NULL;
56303d34 1900 struct batadv_hard_iface *primary_if;
3db0decf 1901 bool own_claim;
4b426b10 1902 bool ret;
23721387 1903
7ed4be95 1904 ethhdr = eth_hdr(skb);
23721387 1905
e5d89254 1906 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1907 if (!primary_if)
1908 goto handled;
1909
1910 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1911 goto allow;
1912
cd9c7bfb
SW
1913 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1914 goto handled;
1915
807736f6 1916 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
74c09b72
LL
1917 /* don't allow multicast packets while requests are in flight */
1918 if (is_multicast_ether_addr(ethhdr->h_dest))
1919 /* Both broadcast flooding or multicast-via-unicasts
1920 * delivery might send to multiple backbone gateways
1921 * sharing the same LAN and therefore need to coordinate
1922 * which backbone gateway forwards into the LAN,
1923 * by claiming the payload source address.
1924 *
1925 * Broadcast flooding and multicast-via-unicasts
1926 * delivery use the following two batman packet types.
1927 * Note: explicitly exclude BATADV_UNICAST_4ADDR,
1928 * as the DHCP gateway feature will send explicitly
1929 * to only one BLA gateway, so the claiming process
1930 * should be avoided there.
1931 */
1932 if (packet_type == BATADV_BCAST ||
1933 packet_type == BATADV_UNICAST)
1934 goto handled;
23721387 1935
2369e827
LL
1936 /* potential duplicates from foreign BLA backbone gateways via
1937 * multicast-in-unicast packets
1938 */
1939 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1940 packet_type == BATADV_UNICAST &&
1941 batadv_bla_check_ucast_duplist(bat_priv, skb))
1942 goto handled;
23721387 1943
8fdd0153 1944 ether_addr_copy(search_claim.addr, ethhdr->h_source);
23721387 1945 search_claim.vid = vid;
3b300de3 1946 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
1947
1948 if (!claim) {
1949 /* possible optimization: race for a claim */
1950 /* No claim exists yet, claim it for us!
1951 */
4dd72f73
AP
1952
1953 batadv_dbg(BATADV_DBG_BLA, bat_priv,
22f0502e
SE
1954 "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
1955 __func__, ethhdr->h_source,
4dd72f73
AP
1956 batadv_is_my_client(bat_priv,
1957 ethhdr->h_source, vid) ?
1958 "yes" : "no");
3b300de3
SE
1959 batadv_handle_claim(bat_priv, primary_if,
1960 primary_if->net_dev->dev_addr,
1961 ethhdr->h_source, vid);
23721387
SW
1962 goto allow;
1963 }
1964
1965 /* if it is our own claim ... */
3db0decf
SE
1966 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1967 own_claim = batadv_compare_eth(backbone_gw->orig,
1968 primary_if->net_dev->dev_addr);
1969 batadv_backbone_gw_put(backbone_gw);
1970
1971 if (own_claim) {
23721387
SW
1972 /* ... allow it in any case */
1973 claim->lasttime = jiffies;
1974 goto allow;
1975 }
1976
74c09b72
LL
1977 /* if it is a multicast ... */
1978 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1979 (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
2d3f6ccc
SW
1980 /* ... drop it. the responsible gateway is in charge.
1981 *
74c09b72 1982 * We need to check packet type because with the gateway
2d3f6ccc 1983 * feature, broadcasts (like DHCP requests) may be sent
74c09b72 1984 * using a unicast 4 address packet type. See comment above.
2d3f6ccc 1985 */
23721387
SW
1986 goto handled;
1987 } else {
1988 /* seems the client considers us as its best gateway.
1989 * send a claim and update the claim table
1990 * immediately.
1991 */
3b300de3
SE
1992 batadv_handle_claim(bat_priv, primary_if,
1993 primary_if->net_dev->dev_addr,
1994 ethhdr->h_source, vid);
23721387
SW
1995 goto allow;
1996 }
1997allow:
3b300de3 1998 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
4b426b10 1999 ret = false;
23721387
SW
2000 goto out;
2001
2002handled:
2003 kfree_skb(skb);
4b426b10 2004 ret = true;
23721387
SW
2005
2006out:
79a0bffb
SE
2007 batadv_hardif_put(primary_if);
2008 batadv_claim_put(claim);
23721387
SW
2009 return ret;
2010}
2011
2c53040f 2012/**
7e9a8c2c 2013 * batadv_bla_tx() - check packets going into the mesh
2c53040f 2014 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
2015 * @skb: the frame to be checked
2016 * @vid: the VLAN ID of the frame
2017 *
04e14be6 2018 * batadv_bla_tx checks if:
23721387
SW
2019 * * a claim was received which has to be processed
2020 * * the frame is allowed on the mesh
2021 *
62fe710f 2022 * in these cases, the skb is further handled by this function.
9d2c9488
LL
2023 *
2024 * This call might reallocate skb data.
62fe710f 2025 *
4b426b10
SE
2026 * Return: true if handled, otherwise it returns false and the caller shall
2027 * further process the skb.
23721387 2028 */
4b426b10
SE
2029bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
2030 unsigned short vid)
23721387
SW
2031{
2032 struct ethhdr *ethhdr;
712bbfe4 2033 struct batadv_bla_claim search_claim, *claim = NULL;
3db0decf 2034 struct batadv_bla_backbone_gw *backbone_gw;
56303d34 2035 struct batadv_hard_iface *primary_if;
3db0decf 2036 bool client_roamed;
4b426b10 2037 bool ret = false;
23721387 2038
e5d89254 2039 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
2040 if (!primary_if)
2041 goto out;
2042
2043 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2044 goto allow;
2045
3b300de3 2046 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
23721387
SW
2047 goto handled;
2048
7ed4be95 2049 ethhdr = eth_hdr(skb);
23721387 2050
807736f6 2051 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
23721387
SW
2052 /* don't allow broadcasts while requests are in flight */
2053 if (is_multicast_ether_addr(ethhdr->h_dest))
2054 goto handled;
2055
8fdd0153 2056 ether_addr_copy(search_claim.addr, ethhdr->h_source);
23721387
SW
2057 search_claim.vid = vid;
2058
3b300de3 2059 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
2060
2061 /* if no claim exists, allow it. */
2062 if (!claim)
2063 goto allow;
2064
2065 /* check if we are responsible. */
3db0decf
SE
2066 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2067 client_roamed = batadv_compare_eth(backbone_gw->orig,
2068 primary_if->net_dev->dev_addr);
2069 batadv_backbone_gw_put(backbone_gw);
2070
2071 if (client_roamed) {
23721387
SW
2072 /* if yes, the client has roamed and we have
2073 * to unclaim it.
2074 */
a3a5129e
AP
2075 if (batadv_has_timed_out(claim->lasttime, 100)) {
2076 /* only unclaim if the last claim entry is
2077 * older than 100 ms to make sure we really
2078 * have a roaming client here.
2079 */
22f0502e
SE
2080 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
2081 __func__, ethhdr->h_source);
a3a5129e
AP
2082 batadv_handle_unclaim(bat_priv, primary_if,
2083 primary_if->net_dev->dev_addr,
2084 ethhdr->h_source, vid);
2085 goto allow;
2086 } else {
22f0502e
SE
2087 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
2088 __func__, ethhdr->h_source);
a3a5129e
AP
2089 goto handled;
2090 }
23721387
SW
2091 }
2092
2093 /* check if it is a multicast/broadcast frame */
2094 if (is_multicast_ether_addr(ethhdr->h_dest)) {
2095 /* drop it. the responsible gateway has forwarded it into
2096 * the backbone network.
2097 */
2098 goto handled;
2099 } else {
2100 /* we must allow it. at least if we are
2101 * responsible for the DESTINATION.
2102 */
2103 goto allow;
2104 }
2105allow:
3b300de3 2106 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
4b426b10 2107 ret = false;
23721387
SW
2108 goto out;
2109handled:
4b426b10 2110 ret = true;
23721387 2111out:
79a0bffb
SE
2112 batadv_hardif_put(primary_if);
2113 batadv_claim_put(claim);
23721387
SW
2114 return ret;
2115}
9bf8e4d4 2116
04f3f5bf 2117/**
7e9a8c2c 2118 * batadv_bla_claim_dump_entry() - dump one entry of the claim table
04f3f5bf
AL
2119 * to a netlink socket
2120 * @msg: buffer for the message
2121 * @portid: netlink port
24d71b92 2122 * @cb: Control block containing additional options
04f3f5bf
AL
2123 * @primary_if: primary interface
2124 * @claim: entry to dump
2125 *
2126 * Return: 0 or error code.
2127 */
2128static int
24d71b92
SE
2129batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,
2130 struct netlink_callback *cb,
04f3f5bf
AL
2131 struct batadv_hard_iface *primary_if,
2132 struct batadv_bla_claim *claim)
2133{
47ce5f1e 2134 const u8 *primary_addr = primary_if->net_dev->dev_addr;
04f3f5bf
AL
2135 u16 backbone_crc;
2136 bool is_own;
2137 void *hdr;
2138 int ret = -EINVAL;
2139
24d71b92
SE
2140 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2141 &batadv_netlink_family, NLM_F_MULTI,
2142 BATADV_CMD_GET_BLA_CLAIM);
04f3f5bf
AL
2143 if (!hdr) {
2144 ret = -ENOBUFS;
2145 goto out;
2146 }
2147
24d71b92
SE
2148 genl_dump_check_consistent(cb, hdr);
2149
04f3f5bf
AL
2150 is_own = batadv_compare_eth(claim->backbone_gw->orig,
2151 primary_addr);
2152
2153 spin_lock_bh(&claim->backbone_gw->crc_lock);
2154 backbone_crc = claim->backbone_gw->crc;
2155 spin_unlock_bh(&claim->backbone_gw->crc_lock);
2156
2157 if (is_own)
2158 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2159 genlmsg_cancel(msg, hdr);
2160 goto out;
2161 }
2162
2163 if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
2164 nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
2165 nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2166 claim->backbone_gw->orig) ||
2167 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2168 backbone_crc)) {
2169 genlmsg_cancel(msg, hdr);
2170 goto out;
2171 }
2172
2173 genlmsg_end(msg, hdr);
2174 ret = 0;
2175
2176out:
2177 return ret;
2178}
2179
2180/**
7e9a8c2c 2181 * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table
04f3f5bf
AL
2182 * to a netlink socket
2183 * @msg: buffer for the message
2184 * @portid: netlink port
24d71b92 2185 * @cb: Control block containing additional options
04f3f5bf 2186 * @primary_if: primary interface
24d71b92
SE
2187 * @hash: hash to dump
2188 * @bucket: bucket index to dump
04f3f5bf
AL
2189 * @idx_skip: How many entries to skip
2190 *
2191 * Return: always 0.
2192 */
2193static int
24d71b92
SE
2194batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid,
2195 struct netlink_callback *cb,
04f3f5bf 2196 struct batadv_hard_iface *primary_if,
24d71b92
SE
2197 struct batadv_hashtable *hash, unsigned int bucket,
2198 int *idx_skip)
04f3f5bf
AL
2199{
2200 struct batadv_bla_claim *claim;
2201 int idx = 0;
b0264ecd 2202 int ret = 0;
04f3f5bf 2203
24d71b92
SE
2204 spin_lock_bh(&hash->list_locks[bucket]);
2205 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2206
2207 hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) {
04f3f5bf
AL
2208 if (idx++ < *idx_skip)
2209 continue;
b0264ecd 2210
24d71b92 2211 ret = batadv_bla_claim_dump_entry(msg, portid, cb,
b0264ecd
SE
2212 primary_if, claim);
2213 if (ret) {
04f3f5bf
AL
2214 *idx_skip = idx - 1;
2215 goto unlock;
2216 }
2217 }
2218
b0264ecd 2219 *idx_skip = 0;
04f3f5bf 2220unlock:
24d71b92 2221 spin_unlock_bh(&hash->list_locks[bucket]);
b0264ecd 2222 return ret;
04f3f5bf
AL
2223}
2224
2225/**
7e9a8c2c 2226 * batadv_bla_claim_dump() - dump claim table to a netlink socket
04f3f5bf
AL
2227 * @msg: buffer for the message
2228 * @cb: callback structure containing arguments
2229 *
2230 * Return: message length.
2231 */
2232int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
2233{
2234 struct batadv_hard_iface *primary_if = NULL;
2235 int portid = NETLINK_CB(cb->skb).portid;
2236 struct net *net = sock_net(cb->skb->sk);
2237 struct net_device *soft_iface;
2238 struct batadv_hashtable *hash;
2239 struct batadv_priv *bat_priv;
2240 int bucket = cb->args[0];
04f3f5bf
AL
2241 int idx = cb->args[1];
2242 int ifindex;
2243 int ret = 0;
2244
2245 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2246 BATADV_ATTR_MESH_IFINDEX);
2247 if (!ifindex)
2248 return -EINVAL;
2249
2250 soft_iface = dev_get_by_index(net, ifindex);
2251 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2252 ret = -ENODEV;
2253 goto out;
2254 }
2255
2256 bat_priv = netdev_priv(soft_iface);
2257 hash = bat_priv->bla.claim_hash;
2258
2259 primary_if = batadv_primary_if_get_selected(bat_priv);
2260 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2261 ret = -ENOENT;
2262 goto out;
2263 }
2264
2265 while (bucket < hash->size) {
24d71b92
SE
2266 if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if,
2267 hash, bucket, &idx))
04f3f5bf
AL
2268 break;
2269 bucket++;
2270 }
2271
2272 cb->args[0] = bucket;
2273 cb->args[1] = idx;
2274
2275 ret = msg->len;
2276
2277out:
79a0bffb 2278 batadv_hardif_put(primary_if);
04f3f5bf 2279
1160dfa1 2280 dev_put(soft_iface);
04f3f5bf
AL
2281
2282 return ret;
2283}
2284
ea4152e1 2285/**
7e9a8c2c
SE
2286 * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a
2287 * netlink socket
ea4152e1
SW
2288 * @msg: buffer for the message
2289 * @portid: netlink port
b00d0e6a 2290 * @cb: Control block containing additional options
ea4152e1
SW
2291 * @primary_if: primary interface
2292 * @backbone_gw: entry to dump
2293 *
2294 * Return: 0 or error code.
2295 */
2296static int
b00d0e6a
SE
2297batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,
2298 struct netlink_callback *cb,
ea4152e1
SW
2299 struct batadv_hard_iface *primary_if,
2300 struct batadv_bla_backbone_gw *backbone_gw)
2301{
47ce5f1e 2302 const u8 *primary_addr = primary_if->net_dev->dev_addr;
ea4152e1
SW
2303 u16 backbone_crc;
2304 bool is_own;
2305 int msecs;
2306 void *hdr;
2307 int ret = -EINVAL;
2308
b00d0e6a
SE
2309 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2310 &batadv_netlink_family, NLM_F_MULTI,
2311 BATADV_CMD_GET_BLA_BACKBONE);
ea4152e1
SW
2312 if (!hdr) {
2313 ret = -ENOBUFS;
2314 goto out;
2315 }
2316
b00d0e6a
SE
2317 genl_dump_check_consistent(cb, hdr);
2318
ea4152e1
SW
2319 is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2320
2321 spin_lock_bh(&backbone_gw->crc_lock);
2322 backbone_crc = backbone_gw->crc;
2323 spin_unlock_bh(&backbone_gw->crc_lock);
2324
2325 msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
2326
2327 if (is_own)
2328 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2329 genlmsg_cancel(msg, hdr);
2330 goto out;
2331 }
2332
2333 if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2334 backbone_gw->orig) ||
2335 nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
2336 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2337 backbone_crc) ||
2338 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
2339 genlmsg_cancel(msg, hdr);
2340 goto out;
2341 }
2342
2343 genlmsg_end(msg, hdr);
2344 ret = 0;
2345
2346out:
2347 return ret;
2348}
2349
2350/**
7e9a8c2c
SE
2351 * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to
2352 * a netlink socket
ea4152e1
SW
2353 * @msg: buffer for the message
2354 * @portid: netlink port
b00d0e6a 2355 * @cb: Control block containing additional options
ea4152e1 2356 * @primary_if: primary interface
b00d0e6a
SE
2357 * @hash: hash to dump
2358 * @bucket: bucket index to dump
ea4152e1
SW
2359 * @idx_skip: How many entries to skip
2360 *
2361 * Return: always 0.
2362 */
2363static int
b00d0e6a
SE
2364batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid,
2365 struct netlink_callback *cb,
ea4152e1 2366 struct batadv_hard_iface *primary_if,
b00d0e6a
SE
2367 struct batadv_hashtable *hash,
2368 unsigned int bucket, int *idx_skip)
ea4152e1
SW
2369{
2370 struct batadv_bla_backbone_gw *backbone_gw;
2371 int idx = 0;
fce672db 2372 int ret = 0;
ea4152e1 2373
b00d0e6a
SE
2374 spin_lock_bh(&hash->list_locks[bucket]);
2375 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2376
2377 hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) {
ea4152e1
SW
2378 if (idx++ < *idx_skip)
2379 continue;
fce672db 2380
b00d0e6a 2381 ret = batadv_bla_backbone_dump_entry(msg, portid, cb,
fce672db
SE
2382 primary_if, backbone_gw);
2383 if (ret) {
ea4152e1
SW
2384 *idx_skip = idx - 1;
2385 goto unlock;
2386 }
2387 }
2388
fce672db 2389 *idx_skip = 0;
ea4152e1 2390unlock:
b00d0e6a 2391 spin_unlock_bh(&hash->list_locks[bucket]);
fce672db 2392 return ret;
ea4152e1
SW
2393}
2394
2395/**
7e9a8c2c 2396 * batadv_bla_backbone_dump() - dump backbone table to a netlink socket
ea4152e1
SW
2397 * @msg: buffer for the message
2398 * @cb: callback structure containing arguments
2399 *
2400 * Return: message length.
2401 */
2402int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
2403{
2404 struct batadv_hard_iface *primary_if = NULL;
2405 int portid = NETLINK_CB(cb->skb).portid;
2406 struct net *net = sock_net(cb->skb->sk);
2407 struct net_device *soft_iface;
2408 struct batadv_hashtable *hash;
2409 struct batadv_priv *bat_priv;
2410 int bucket = cb->args[0];
ea4152e1
SW
2411 int idx = cb->args[1];
2412 int ifindex;
2413 int ret = 0;
2414
2415 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2416 BATADV_ATTR_MESH_IFINDEX);
2417 if (!ifindex)
2418 return -EINVAL;
2419
2420 soft_iface = dev_get_by_index(net, ifindex);
2421 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2422 ret = -ENODEV;
2423 goto out;
2424 }
2425
2426 bat_priv = netdev_priv(soft_iface);
2427 hash = bat_priv->bla.backbone_hash;
2428
2429 primary_if = batadv_primary_if_get_selected(bat_priv);
2430 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2431 ret = -ENOENT;
2432 goto out;
2433 }
2434
2435 while (bucket < hash->size) {
b00d0e6a
SE
2436 if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if,
2437 hash, bucket, &idx))
ea4152e1
SW
2438 break;
2439 bucket++;
2440 }
2441
2442 cb->args[0] = bucket;
2443 cb->args[1] = idx;
2444
2445 ret = msg->len;
2446
2447out:
79a0bffb 2448 batadv_hardif_put(primary_if);
ea4152e1 2449
1160dfa1 2450 dev_put(soft_iface);
ea4152e1
SW
2451
2452 return ret;
2453}
00311de5
AP
2454
2455#ifdef CONFIG_BATMAN_ADV_DAT
2456/**
7e9a8c2c 2457 * batadv_bla_check_claim() - check if address is claimed
00311de5
AP
2458 *
2459 * @bat_priv: the bat priv with all the soft interface information
2460 * @addr: mac address of which the claim status is checked
2461 * @vid: the VLAN ID
2462 *
2463 * addr is checked if this address is claimed by the local device itself.
2464 *
2465 * Return: true if bla is disabled or the mac is claimed by the device,
2466 * false if the device addr is already claimed by another gateway
2467 */
2468bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
2469 u8 *addr, unsigned short vid)
2470{
2471 struct batadv_bla_claim search_claim;
2472 struct batadv_bla_claim *claim = NULL;
2473 struct batadv_hard_iface *primary_if = NULL;
2474 bool ret = true;
2475
2476 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2477 return ret;
2478
2479 primary_if = batadv_primary_if_get_selected(bat_priv);
2480 if (!primary_if)
2481 return ret;
2482
2483 /* First look if the mac address is claimed */
2484 ether_addr_copy(search_claim.addr, addr);
2485 search_claim.vid = vid;
2486
2487 claim = batadv_claim_hash_find(bat_priv, &search_claim);
2488
2489 /* If there is a claim and we are not owner of the claim,
2490 * return false.
2491 */
2492 if (claim) {
2493 if (!batadv_compare_eth(claim->backbone_gw->orig,
2494 primary_if->net_dev->dev_addr))
2495 ret = false;
2496 batadv_claim_put(claim);
2497 }
2498
2499 batadv_hardif_put(primary_if);
2500 return ret;
2501}
2502#endif