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