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