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