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