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