GNU Linux-libre 4.19.264-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         if (!bat_priv->bla.claim_hash)
1578                 return -ENOMEM;
1579
1580         bat_priv->bla.backbone_hash = batadv_hash_new(32);
1581         if (!bat_priv->bla.backbone_hash) {
1582                 batadv_hash_destroy(bat_priv->bla.claim_hash);
1583                 return -ENOMEM;
1584         }
1585
1586         batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1587                                    &batadv_claim_hash_lock_class_key);
1588         batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1589                                    &batadv_backbone_hash_lock_class_key);
1590
1591         batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1592
1593         INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1594
1595         queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1596                            msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1597         return 0;
1598 }
1599
1600 /**
1601  * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
1602  * @bat_priv: the bat priv with all the soft interface information
1603  * @skb: contains the multicast packet to be checked
1604  * @payload_ptr: pointer to position inside the head buffer of the skb
1605  *  marking the start of the data to be CRC'ed
1606  * @orig: originator mac address, NULL if unknown
1607  *
1608  * Check if it is on our broadcast list. Another gateway might have sent the
1609  * same packet because it is connected to the same backbone, so we have to
1610  * remove this duplicate.
1611  *
1612  * This is performed by checking the CRC, which will tell us
1613  * with a good chance that it is the same packet. If it is furthermore
1614  * sent by another host, drop it. We allow equal packets from
1615  * the same host however as this might be intended.
1616  *
1617  * Return: true if a packet is in the duplicate list, false otherwise.
1618  */
1619 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
1620                                      struct sk_buff *skb, u8 *payload_ptr,
1621                                      const u8 *orig)
1622 {
1623         struct batadv_bcast_duplist_entry *entry;
1624         bool ret = false;
1625         int i, curr;
1626         __be32 crc;
1627
1628         /* calculate the crc ... */
1629         crc = batadv_skb_crc32(skb, payload_ptr);
1630
1631         spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1632
1633         for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1634                 curr = (bat_priv->bla.bcast_duplist_curr + i);
1635                 curr %= BATADV_DUPLIST_SIZE;
1636                 entry = &bat_priv->bla.bcast_duplist[curr];
1637
1638                 /* we can stop searching if the entry is too old ;
1639                  * later entries will be even older
1640                  */
1641                 if (batadv_has_timed_out(entry->entrytime,
1642                                          BATADV_DUPLIST_TIMEOUT))
1643                         break;
1644
1645                 if (entry->crc != crc)
1646                         continue;
1647
1648                 /* are the originators both known and not anonymous? */
1649                 if (orig && !is_zero_ether_addr(orig) &&
1650                     !is_zero_ether_addr(entry->orig)) {
1651                         /* If known, check if the new frame came from
1652                          * the same originator:
1653                          * We are safe to take identical frames from the
1654                          * same orig, if known, as multiplications in
1655                          * the mesh are detected via the (orig, seqno) pair.
1656                          * So we can be a bit more liberal here and allow
1657                          * identical frames from the same orig which the source
1658                          * host might have sent multiple times on purpose.
1659                          */
1660                         if (batadv_compare_eth(entry->orig, orig))
1661                                 continue;
1662                 }
1663
1664                 /* this entry seems to match: same crc, not too old,
1665                  * and from another gw. therefore return true to forbid it.
1666                  */
1667                 ret = true;
1668                 goto out;
1669         }
1670         /* not found, add a new entry (overwrite the oldest entry)
1671          * and allow it, its the first occurrence.
1672          */
1673         curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1674         curr %= BATADV_DUPLIST_SIZE;
1675         entry = &bat_priv->bla.bcast_duplist[curr];
1676         entry->crc = crc;
1677         entry->entrytime = jiffies;
1678
1679         /* known originator */
1680         if (orig)
1681                 ether_addr_copy(entry->orig, orig);
1682         /* anonymous originator */
1683         else
1684                 eth_zero_addr(entry->orig);
1685
1686         bat_priv->bla.bcast_duplist_curr = curr;
1687
1688 out:
1689         spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1690
1691         return ret;
1692 }
1693
1694 /**
1695  * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
1696  * @bat_priv: the bat priv with all the soft interface information
1697  * @skb: contains the multicast packet to be checked, decapsulated from a
1698  *  unicast_packet
1699  *
1700  * Check if it is on our broadcast list. Another gateway might have sent the
1701  * same packet because it is connected to the same backbone, so we have to
1702  * remove this duplicate.
1703  *
1704  * Return: true if a packet is in the duplicate list, false otherwise.
1705  */
1706 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
1707                                            struct sk_buff *skb)
1708 {
1709         return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);
1710 }
1711
1712 /**
1713  * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
1714  * @bat_priv: the bat priv with all the soft interface information
1715  * @skb: contains the bcast_packet to be checked
1716  *
1717  * Check if it is on our broadcast list. Another gateway might have sent the
1718  * same packet because it is connected to the same backbone, so we have to
1719  * remove this duplicate.
1720  *
1721  * Return: true if a packet is in the duplicate list, false otherwise.
1722  */
1723 bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1724                                     struct sk_buff *skb)
1725 {
1726         struct batadv_bcast_packet *bcast_packet;
1727         u8 *payload_ptr;
1728
1729         bcast_packet = (struct batadv_bcast_packet *)skb->data;
1730         payload_ptr = (u8 *)(bcast_packet + 1);
1731
1732         return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,
1733                                         bcast_packet->orig);
1734 }
1735
1736 /**
1737  * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for
1738  *  the VLAN identified by vid.
1739  * @bat_priv: the bat priv with all the soft interface information
1740  * @orig: originator mac address
1741  * @vid: VLAN identifier
1742  *
1743  * Return: true if orig is a backbone for this vid, false otherwise.
1744  */
1745 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1746                                     unsigned short vid)
1747 {
1748         struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1749         struct hlist_head *head;
1750         struct batadv_bla_backbone_gw *backbone_gw;
1751         int i;
1752
1753         if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1754                 return false;
1755
1756         if (!hash)
1757                 return false;
1758
1759         for (i = 0; i < hash->size; i++) {
1760                 head = &hash->table[i];
1761
1762                 rcu_read_lock();
1763                 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1764                         if (batadv_compare_eth(backbone_gw->orig, orig) &&
1765                             backbone_gw->vid == vid) {
1766                                 rcu_read_unlock();
1767                                 return true;
1768                         }
1769                 }
1770                 rcu_read_unlock();
1771         }
1772
1773         return false;
1774 }
1775
1776 /**
1777  * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN
1778  * @skb: the frame to be checked
1779  * @orig_node: the orig_node of the frame
1780  * @hdr_size: maximum length of the frame
1781  *
1782  * Return: true if the orig_node is also a gateway on the soft interface,
1783  * otherwise it returns false.
1784  */
1785 bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1786                                struct batadv_orig_node *orig_node, int hdr_size)
1787 {
1788         struct batadv_bla_backbone_gw *backbone_gw;
1789         unsigned short vid;
1790
1791         if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1792                 return false;
1793
1794         /* first, find out the vid. */
1795         if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1796                 return false;
1797
1798         vid = batadv_get_vid(skb, hdr_size);
1799
1800         /* see if this originator is a backbone gw for this VLAN */
1801         backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1802                                                 orig_node->orig, vid);
1803         if (!backbone_gw)
1804                 return false;
1805
1806         batadv_backbone_gw_put(backbone_gw);
1807         return true;
1808 }
1809
1810 /**
1811  * batadv_bla_free() - free all bla structures
1812  * @bat_priv: the bat priv with all the soft interface information
1813  *
1814  * for softinterface free or module unload
1815  */
1816 void batadv_bla_free(struct batadv_priv *bat_priv)
1817 {
1818         struct batadv_hard_iface *primary_if;
1819
1820         cancel_delayed_work_sync(&bat_priv->bla.work);
1821         primary_if = batadv_primary_if_get_selected(bat_priv);
1822
1823         if (bat_priv->bla.claim_hash) {
1824                 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1825                 batadv_hash_destroy(bat_priv->bla.claim_hash);
1826                 bat_priv->bla.claim_hash = NULL;
1827         }
1828         if (bat_priv->bla.backbone_hash) {
1829                 batadv_bla_purge_backbone_gw(bat_priv, 1);
1830                 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1831                 bat_priv->bla.backbone_hash = NULL;
1832         }
1833         if (primary_if)
1834                 batadv_hardif_put(primary_if);
1835 }
1836
1837 /**
1838  * batadv_bla_loopdetect_check() - check and handle a detected loop
1839  * @bat_priv: the bat priv with all the soft interface information
1840  * @skb: the packet to check
1841  * @primary_if: interface where the request came on
1842  * @vid: the VLAN ID of the frame
1843  *
1844  * Checks if this packet is a loop detect frame which has been sent by us,
1845  * throw an uevent and log the event if that is the case.
1846  *
1847  * Return: true if it is a loop detect frame which is to be dropped, false
1848  * otherwise.
1849  */
1850 static bool
1851 batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1852                             struct batadv_hard_iface *primary_if,
1853                             unsigned short vid)
1854 {
1855         struct batadv_bla_backbone_gw *backbone_gw;
1856         struct ethhdr *ethhdr;
1857         bool ret;
1858
1859         ethhdr = eth_hdr(skb);
1860
1861         /* Only check for the MAC address and skip more checks here for
1862          * performance reasons - this function is on the hotpath, after all.
1863          */
1864         if (!batadv_compare_eth(ethhdr->h_source,
1865                                 bat_priv->bla.loopdetect_addr))
1866                 return false;
1867
1868         /* If the packet came too late, don't forward it on the mesh
1869          * but don't consider that as loop. It might be a coincidence.
1870          */
1871         if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1872                                  BATADV_BLA_LOOPDETECT_TIMEOUT))
1873                 return true;
1874
1875         backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1876                                                  primary_if->net_dev->dev_addr,
1877                                                  vid, true);
1878         if (unlikely(!backbone_gw))
1879                 return true;
1880
1881         ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1882
1883         /* backbone_gw is unreferenced in the report work function function
1884          * if queue_work() call was successful
1885          */
1886         if (!ret)
1887                 batadv_backbone_gw_put(backbone_gw);
1888
1889         return true;
1890 }
1891
1892 /**
1893  * batadv_bla_rx() - check packets coming from the mesh.
1894  * @bat_priv: the bat priv with all the soft interface information
1895  * @skb: the frame to be checked
1896  * @vid: the VLAN ID of the frame
1897  * @packet_type: the batman packet type this frame came in
1898  *
1899  * batadv_bla_rx avoidance checks if:
1900  *  * we have to race for a claim
1901  *  * if the frame is allowed on the LAN
1902  *
1903  * in these cases, the skb is further handled by this function
1904  *
1905  * Return: true if handled, otherwise it returns false and the caller shall
1906  * further process the skb.
1907  */
1908 bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1909                    unsigned short vid, int packet_type)
1910 {
1911         struct batadv_bla_backbone_gw *backbone_gw;
1912         struct ethhdr *ethhdr;
1913         struct batadv_bla_claim search_claim, *claim = NULL;
1914         struct batadv_hard_iface *primary_if;
1915         bool own_claim;
1916         bool ret;
1917
1918         ethhdr = eth_hdr(skb);
1919
1920         primary_if = batadv_primary_if_get_selected(bat_priv);
1921         if (!primary_if)
1922                 goto handled;
1923
1924         if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1925                 goto allow;
1926
1927         if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1928                 goto handled;
1929
1930         if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1931                 /* don't allow multicast packets while requests are in flight */
1932                 if (is_multicast_ether_addr(ethhdr->h_dest))
1933                         /* Both broadcast flooding or multicast-via-unicasts
1934                          * delivery might send to multiple backbone gateways
1935                          * sharing the same LAN and therefore need to coordinate
1936                          * which backbone gateway forwards into the LAN,
1937                          * by claiming the payload source address.
1938                          *
1939                          * Broadcast flooding and multicast-via-unicasts
1940                          * delivery use the following two batman packet types.
1941                          * Note: explicitly exclude BATADV_UNICAST_4ADDR,
1942                          * as the DHCP gateway feature will send explicitly
1943                          * to only one BLA gateway, so the claiming process
1944                          * should be avoided there.
1945                          */
1946                         if (packet_type == BATADV_BCAST ||
1947                             packet_type == BATADV_UNICAST)
1948                                 goto handled;
1949
1950         /* potential duplicates from foreign BLA backbone gateways via
1951          * multicast-in-unicast packets
1952          */
1953         if (is_multicast_ether_addr(ethhdr->h_dest) &&
1954             packet_type == BATADV_UNICAST &&
1955             batadv_bla_check_ucast_duplist(bat_priv, skb))
1956                 goto handled;
1957
1958         ether_addr_copy(search_claim.addr, ethhdr->h_source);
1959         search_claim.vid = vid;
1960         claim = batadv_claim_hash_find(bat_priv, &search_claim);
1961
1962         if (!claim) {
1963                 /* possible optimization: race for a claim */
1964                 /* No claim exists yet, claim it for us!
1965                  */
1966
1967                 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1968                            "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
1969                            __func__, ethhdr->h_source,
1970                            batadv_is_my_client(bat_priv,
1971                                                ethhdr->h_source, vid) ?
1972                            "yes" : "no");
1973                 batadv_handle_claim(bat_priv, primary_if,
1974                                     primary_if->net_dev->dev_addr,
1975                                     ethhdr->h_source, vid);
1976                 goto allow;
1977         }
1978
1979         /* if it is our own claim ... */
1980         backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1981         own_claim = batadv_compare_eth(backbone_gw->orig,
1982                                        primary_if->net_dev->dev_addr);
1983         batadv_backbone_gw_put(backbone_gw);
1984
1985         if (own_claim) {
1986                 /* ... allow it in any case */
1987                 claim->lasttime = jiffies;
1988                 goto allow;
1989         }
1990
1991         /* if it is a multicast ... */
1992         if (is_multicast_ether_addr(ethhdr->h_dest) &&
1993             (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
1994                 /* ... drop it. the responsible gateway is in charge.
1995                  *
1996                  * We need to check packet type because with the gateway
1997                  * feature, broadcasts (like DHCP requests) may be sent
1998                  * using a unicast 4 address packet type. See comment above.
1999                  */
2000                 goto handled;
2001         } else {
2002                 /* seems the client considers us as its best gateway.
2003                  * send a claim and update the claim table
2004                  * immediately.
2005                  */
2006                 batadv_handle_claim(bat_priv, primary_if,
2007                                     primary_if->net_dev->dev_addr,
2008                                     ethhdr->h_source, vid);
2009                 goto allow;
2010         }
2011 allow:
2012         batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2013         ret = false;
2014         goto out;
2015
2016 handled:
2017         kfree_skb(skb);
2018         ret = true;
2019
2020 out:
2021         if (primary_if)
2022                 batadv_hardif_put(primary_if);
2023         if (claim)
2024                 batadv_claim_put(claim);
2025         return ret;
2026 }
2027
2028 /**
2029  * batadv_bla_tx() - check packets going into the mesh
2030  * @bat_priv: the bat priv with all the soft interface information
2031  * @skb: the frame to be checked
2032  * @vid: the VLAN ID of the frame
2033  *
2034  * batadv_bla_tx checks if:
2035  *  * a claim was received which has to be processed
2036  *  * the frame is allowed on the mesh
2037  *
2038  * in these cases, the skb is further handled by this function.
2039  *
2040  * This call might reallocate skb data.
2041  *
2042  * Return: true if handled, otherwise it returns false and the caller shall
2043  * further process the skb.
2044  */
2045 bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
2046                    unsigned short vid)
2047 {
2048         struct ethhdr *ethhdr;
2049         struct batadv_bla_claim search_claim, *claim = NULL;
2050         struct batadv_bla_backbone_gw *backbone_gw;
2051         struct batadv_hard_iface *primary_if;
2052         bool client_roamed;
2053         bool ret = false;
2054
2055         primary_if = batadv_primary_if_get_selected(bat_priv);
2056         if (!primary_if)
2057                 goto out;
2058
2059         if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2060                 goto allow;
2061
2062         if (batadv_bla_process_claim(bat_priv, primary_if, skb))
2063                 goto handled;
2064
2065         ethhdr = eth_hdr(skb);
2066
2067         if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
2068                 /* don't allow broadcasts while requests are in flight */
2069                 if (is_multicast_ether_addr(ethhdr->h_dest))
2070                         goto handled;
2071
2072         ether_addr_copy(search_claim.addr, ethhdr->h_source);
2073         search_claim.vid = vid;
2074
2075         claim = batadv_claim_hash_find(bat_priv, &search_claim);
2076
2077         /* if no claim exists, allow it. */
2078         if (!claim)
2079                 goto allow;
2080
2081         /* check if we are responsible. */
2082         backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2083         client_roamed = batadv_compare_eth(backbone_gw->orig,
2084                                            primary_if->net_dev->dev_addr);
2085         batadv_backbone_gw_put(backbone_gw);
2086
2087         if (client_roamed) {
2088                 /* if yes, the client has roamed and we have
2089                  * to unclaim it.
2090                  */
2091                 if (batadv_has_timed_out(claim->lasttime, 100)) {
2092                         /* only unclaim if the last claim entry is
2093                          * older than 100 ms to make sure we really
2094                          * have a roaming client here.
2095                          */
2096                         batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
2097                                    __func__, ethhdr->h_source);
2098                         batadv_handle_unclaim(bat_priv, primary_if,
2099                                               primary_if->net_dev->dev_addr,
2100                                               ethhdr->h_source, vid);
2101                         goto allow;
2102                 } else {
2103                         batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
2104                                    __func__, ethhdr->h_source);
2105                         goto handled;
2106                 }
2107         }
2108
2109         /* check if it is a multicast/broadcast frame */
2110         if (is_multicast_ether_addr(ethhdr->h_dest)) {
2111                 /* drop it. the responsible gateway has forwarded it into
2112                  * the backbone network.
2113                  */
2114                 goto handled;
2115         } else {
2116                 /* we must allow it. at least if we are
2117                  * responsible for the DESTINATION.
2118                  */
2119                 goto allow;
2120         }
2121 allow:
2122         batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2123         ret = false;
2124         goto out;
2125 handled:
2126         ret = true;
2127 out:
2128         if (primary_if)
2129                 batadv_hardif_put(primary_if);
2130         if (claim)
2131                 batadv_claim_put(claim);
2132         return ret;
2133 }
2134
2135 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2136 /**
2137  * batadv_bla_claim_table_seq_print_text() - print the claim table in a seq file
2138  * @seq: seq file to print on
2139  * @offset: not used
2140  *
2141  * Return: always 0
2142  */
2143 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
2144 {
2145         struct net_device *net_dev = (struct net_device *)seq->private;
2146         struct batadv_priv *bat_priv = netdev_priv(net_dev);
2147         struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
2148         struct batadv_bla_backbone_gw *backbone_gw;
2149         struct batadv_bla_claim *claim;
2150         struct batadv_hard_iface *primary_if;
2151         struct hlist_head *head;
2152         u16 backbone_crc;
2153         u32 i;
2154         bool is_own;
2155         u8 *primary_addr;
2156
2157         primary_if = batadv_seq_print_text_primary_if_get(seq);
2158         if (!primary_if)
2159                 goto out;
2160
2161         primary_addr = primary_if->net_dev->dev_addr;
2162         seq_printf(seq,
2163                    "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
2164                    net_dev->name, primary_addr,
2165                    ntohs(bat_priv->bla.claim_dest.group));
2166         seq_puts(seq,
2167                  "   Client               VID      Originator        [o] (CRC   )\n");
2168         for (i = 0; i < hash->size; i++) {
2169                 head = &hash->table[i];
2170
2171                 rcu_read_lock();
2172                 hlist_for_each_entry_rcu(claim, head, hash_entry) {
2173                         backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2174
2175                         is_own = batadv_compare_eth(backbone_gw->orig,
2176                                                     primary_addr);
2177
2178                         spin_lock_bh(&backbone_gw->crc_lock);
2179                         backbone_crc = backbone_gw->crc;
2180                         spin_unlock_bh(&backbone_gw->crc_lock);
2181                         seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
2182                                    claim->addr, batadv_print_vid(claim->vid),
2183                                    backbone_gw->orig,
2184                                    (is_own ? 'x' : ' '),
2185                                    backbone_crc);
2186
2187                         batadv_backbone_gw_put(backbone_gw);
2188                 }
2189                 rcu_read_unlock();
2190         }
2191 out:
2192         if (primary_if)
2193                 batadv_hardif_put(primary_if);
2194         return 0;
2195 }
2196 #endif
2197
2198 /**
2199  * batadv_bla_claim_dump_entry() - dump one entry of the claim table
2200  * to a netlink socket
2201  * @msg: buffer for the message
2202  * @portid: netlink port
2203  * @seq: Sequence number of netlink message
2204  * @primary_if: primary interface
2205  * @claim: entry to dump
2206  *
2207  * Return: 0 or error code.
2208  */
2209 static int
2210 batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2211                             struct batadv_hard_iface *primary_if,
2212                             struct batadv_bla_claim *claim)
2213 {
2214         u8 *primary_addr = primary_if->net_dev->dev_addr;
2215         u16 backbone_crc;
2216         bool is_own;
2217         void *hdr;
2218         int ret = -EINVAL;
2219
2220         hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2221                           NLM_F_MULTI, BATADV_CMD_GET_BLA_CLAIM);
2222         if (!hdr) {
2223                 ret = -ENOBUFS;
2224                 goto out;
2225         }
2226
2227         is_own = batadv_compare_eth(claim->backbone_gw->orig,
2228                                     primary_addr);
2229
2230         spin_lock_bh(&claim->backbone_gw->crc_lock);
2231         backbone_crc = claim->backbone_gw->crc;
2232         spin_unlock_bh(&claim->backbone_gw->crc_lock);
2233
2234         if (is_own)
2235                 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2236                         genlmsg_cancel(msg, hdr);
2237                         goto out;
2238                 }
2239
2240         if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
2241             nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
2242             nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2243                     claim->backbone_gw->orig) ||
2244             nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2245                         backbone_crc)) {
2246                 genlmsg_cancel(msg, hdr);
2247                 goto out;
2248         }
2249
2250         genlmsg_end(msg, hdr);
2251         ret = 0;
2252
2253 out:
2254         return ret;
2255 }
2256
2257 /**
2258  * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table
2259  * to a netlink socket
2260  * @msg: buffer for the message
2261  * @portid: netlink port
2262  * @seq: Sequence number of netlink message
2263  * @primary_if: primary interface
2264  * @head: bucket to dump
2265  * @idx_skip: How many entries to skip
2266  *
2267  * Return: always 0.
2268  */
2269 static int
2270 batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2271                              struct batadv_hard_iface *primary_if,
2272                              struct hlist_head *head, int *idx_skip)
2273 {
2274         struct batadv_bla_claim *claim;
2275         int idx = 0;
2276         int ret = 0;
2277
2278         rcu_read_lock();
2279         hlist_for_each_entry_rcu(claim, head, hash_entry) {
2280                 if (idx++ < *idx_skip)
2281                         continue;
2282
2283                 ret = batadv_bla_claim_dump_entry(msg, portid, seq,
2284                                                   primary_if, claim);
2285                 if (ret) {
2286                         *idx_skip = idx - 1;
2287                         goto unlock;
2288                 }
2289         }
2290
2291         *idx_skip = 0;
2292 unlock:
2293         rcu_read_unlock();
2294         return ret;
2295 }
2296
2297 /**
2298  * batadv_bla_claim_dump() - dump claim table to a netlink socket
2299  * @msg: buffer for the message
2300  * @cb: callback structure containing arguments
2301  *
2302  * Return: message length.
2303  */
2304 int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
2305 {
2306         struct batadv_hard_iface *primary_if = NULL;
2307         int portid = NETLINK_CB(cb->skb).portid;
2308         struct net *net = sock_net(cb->skb->sk);
2309         struct net_device *soft_iface;
2310         struct batadv_hashtable *hash;
2311         struct batadv_priv *bat_priv;
2312         int bucket = cb->args[0];
2313         struct hlist_head *head;
2314         int idx = cb->args[1];
2315         int ifindex;
2316         int ret = 0;
2317
2318         ifindex = batadv_netlink_get_ifindex(cb->nlh,
2319                                              BATADV_ATTR_MESH_IFINDEX);
2320         if (!ifindex)
2321                 return -EINVAL;
2322
2323         soft_iface = dev_get_by_index(net, ifindex);
2324         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2325                 ret = -ENODEV;
2326                 goto out;
2327         }
2328
2329         bat_priv = netdev_priv(soft_iface);
2330         hash = bat_priv->bla.claim_hash;
2331
2332         primary_if = batadv_primary_if_get_selected(bat_priv);
2333         if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2334                 ret = -ENOENT;
2335                 goto out;
2336         }
2337
2338         while (bucket < hash->size) {
2339                 head = &hash->table[bucket];
2340
2341                 if (batadv_bla_claim_dump_bucket(msg, portid,
2342                                                  cb->nlh->nlmsg_seq,
2343                                                  primary_if, head, &idx))
2344                         break;
2345                 bucket++;
2346         }
2347
2348         cb->args[0] = bucket;
2349         cb->args[1] = idx;
2350
2351         ret = msg->len;
2352
2353 out:
2354         if (primary_if)
2355                 batadv_hardif_put(primary_if);
2356
2357         if (soft_iface)
2358                 dev_put(soft_iface);
2359
2360         return ret;
2361 }
2362
2363 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2364 /**
2365  * batadv_bla_backbone_table_seq_print_text() - print the backbone table in a
2366  *  seq file
2367  * @seq: seq file to print on
2368  * @offset: not used
2369  *
2370  * Return: always 0
2371  */
2372 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2373 {
2374         struct net_device *net_dev = (struct net_device *)seq->private;
2375         struct batadv_priv *bat_priv = netdev_priv(net_dev);
2376         struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
2377         struct batadv_bla_backbone_gw *backbone_gw;
2378         struct batadv_hard_iface *primary_if;
2379         struct hlist_head *head;
2380         int secs, msecs;
2381         u16 backbone_crc;
2382         u32 i;
2383         bool is_own;
2384         u8 *primary_addr;
2385
2386         primary_if = batadv_seq_print_text_primary_if_get(seq);
2387         if (!primary_if)
2388                 goto out;
2389
2390         primary_addr = primary_if->net_dev->dev_addr;
2391         seq_printf(seq,
2392                    "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2393                    net_dev->name, primary_addr,
2394                    ntohs(bat_priv->bla.claim_dest.group));
2395         seq_puts(seq, "   Originator           VID   last seen (CRC   )\n");
2396         for (i = 0; i < hash->size; i++) {
2397                 head = &hash->table[i];
2398
2399                 rcu_read_lock();
2400                 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2401                         msecs = jiffies_to_msecs(jiffies -
2402                                                  backbone_gw->lasttime);
2403                         secs = msecs / 1000;
2404                         msecs = msecs % 1000;
2405
2406                         is_own = batadv_compare_eth(backbone_gw->orig,
2407                                                     primary_addr);
2408                         if (is_own)
2409                                 continue;
2410
2411                         spin_lock_bh(&backbone_gw->crc_lock);
2412                         backbone_crc = backbone_gw->crc;
2413                         spin_unlock_bh(&backbone_gw->crc_lock);
2414
2415                         seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
2416                                    backbone_gw->orig,
2417                                    batadv_print_vid(backbone_gw->vid), secs,
2418                                    msecs, backbone_crc);
2419                 }
2420                 rcu_read_unlock();
2421         }
2422 out:
2423         if (primary_if)
2424                 batadv_hardif_put(primary_if);
2425         return 0;
2426 }
2427 #endif
2428
2429 /**
2430  * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a
2431  *  netlink socket
2432  * @msg: buffer for the message
2433  * @portid: netlink port
2434  * @seq: Sequence number of netlink message
2435  * @primary_if: primary interface
2436  * @backbone_gw: entry to dump
2437  *
2438  * Return: 0 or error code.
2439  */
2440 static int
2441 batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2442                                struct batadv_hard_iface *primary_if,
2443                                struct batadv_bla_backbone_gw *backbone_gw)
2444 {
2445         u8 *primary_addr = primary_if->net_dev->dev_addr;
2446         u16 backbone_crc;
2447         bool is_own;
2448         int msecs;
2449         void *hdr;
2450         int ret = -EINVAL;
2451
2452         hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2453                           NLM_F_MULTI, BATADV_CMD_GET_BLA_BACKBONE);
2454         if (!hdr) {
2455                 ret = -ENOBUFS;
2456                 goto out;
2457         }
2458
2459         is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2460
2461         spin_lock_bh(&backbone_gw->crc_lock);
2462         backbone_crc = backbone_gw->crc;
2463         spin_unlock_bh(&backbone_gw->crc_lock);
2464
2465         msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
2466
2467         if (is_own)
2468                 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2469                         genlmsg_cancel(msg, hdr);
2470                         goto out;
2471                 }
2472
2473         if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2474                     backbone_gw->orig) ||
2475             nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
2476             nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2477                         backbone_crc) ||
2478             nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
2479                 genlmsg_cancel(msg, hdr);
2480                 goto out;
2481         }
2482
2483         genlmsg_end(msg, hdr);
2484         ret = 0;
2485
2486 out:
2487         return ret;
2488 }
2489
2490 /**
2491  * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to
2492  *  a netlink socket
2493  * @msg: buffer for the message
2494  * @portid: netlink port
2495  * @seq: Sequence number of netlink message
2496  * @primary_if: primary interface
2497  * @head: bucket to dump
2498  * @idx_skip: How many entries to skip
2499  *
2500  * Return: always 0.
2501  */
2502 static int
2503 batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2504                                 struct batadv_hard_iface *primary_if,
2505                                 struct hlist_head *head, int *idx_skip)
2506 {
2507         struct batadv_bla_backbone_gw *backbone_gw;
2508         int idx = 0;
2509         int ret = 0;
2510
2511         rcu_read_lock();
2512         hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2513                 if (idx++ < *idx_skip)
2514                         continue;
2515
2516                 ret = batadv_bla_backbone_dump_entry(msg, portid, seq,
2517                                                      primary_if, backbone_gw);
2518                 if (ret) {
2519                         *idx_skip = idx - 1;
2520                         goto unlock;
2521                 }
2522         }
2523
2524         *idx_skip = 0;
2525 unlock:
2526         rcu_read_unlock();
2527         return ret;
2528 }
2529
2530 /**
2531  * batadv_bla_backbone_dump() - dump backbone table to a netlink socket
2532  * @msg: buffer for the message
2533  * @cb: callback structure containing arguments
2534  *
2535  * Return: message length.
2536  */
2537 int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
2538 {
2539         struct batadv_hard_iface *primary_if = NULL;
2540         int portid = NETLINK_CB(cb->skb).portid;
2541         struct net *net = sock_net(cb->skb->sk);
2542         struct net_device *soft_iface;
2543         struct batadv_hashtable *hash;
2544         struct batadv_priv *bat_priv;
2545         int bucket = cb->args[0];
2546         struct hlist_head *head;
2547         int idx = cb->args[1];
2548         int ifindex;
2549         int ret = 0;
2550
2551         ifindex = batadv_netlink_get_ifindex(cb->nlh,
2552                                              BATADV_ATTR_MESH_IFINDEX);
2553         if (!ifindex)
2554                 return -EINVAL;
2555
2556         soft_iface = dev_get_by_index(net, ifindex);
2557         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2558                 ret = -ENODEV;
2559                 goto out;
2560         }
2561
2562         bat_priv = netdev_priv(soft_iface);
2563         hash = bat_priv->bla.backbone_hash;
2564
2565         primary_if = batadv_primary_if_get_selected(bat_priv);
2566         if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2567                 ret = -ENOENT;
2568                 goto out;
2569         }
2570
2571         while (bucket < hash->size) {
2572                 head = &hash->table[bucket];
2573
2574                 if (batadv_bla_backbone_dump_bucket(msg, portid,
2575                                                     cb->nlh->nlmsg_seq,
2576                                                     primary_if, head, &idx))
2577                         break;
2578                 bucket++;
2579         }
2580
2581         cb->args[0] = bucket;
2582         cb->args[1] = idx;
2583
2584         ret = msg->len;
2585
2586 out:
2587         if (primary_if)
2588                 batadv_hardif_put(primary_if);
2589
2590         if (soft_iface)
2591                 dev_put(soft_iface);
2592
2593         return ret;
2594 }
2595
2596 #ifdef CONFIG_BATMAN_ADV_DAT
2597 /**
2598  * batadv_bla_check_claim() - check if address is claimed
2599  *
2600  * @bat_priv: the bat priv with all the soft interface information
2601  * @addr: mac address of which the claim status is checked
2602  * @vid: the VLAN ID
2603  *
2604  * addr is checked if this address is claimed by the local device itself.
2605  *
2606  * Return: true if bla is disabled or the mac is claimed by the device,
2607  * false if the device addr is already claimed by another gateway
2608  */
2609 bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
2610                             u8 *addr, unsigned short vid)
2611 {
2612         struct batadv_bla_claim search_claim;
2613         struct batadv_bla_claim *claim = NULL;
2614         struct batadv_hard_iface *primary_if = NULL;
2615         bool ret = true;
2616
2617         if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2618                 return ret;
2619
2620         primary_if = batadv_primary_if_get_selected(bat_priv);
2621         if (!primary_if)
2622                 return ret;
2623
2624         /* First look if the mac address is claimed */
2625         ether_addr_copy(search_claim.addr, addr);
2626         search_claim.vid = vid;
2627
2628         claim = batadv_claim_hash_find(bat_priv, &search_claim);
2629
2630         /* If there is a claim and we are not owner of the claim,
2631          * return false.
2632          */
2633         if (claim) {
2634                 if (!batadv_compare_eth(claim->backbone_gw->orig,
2635                                         primary_if->net_dev->dev_addr))
2636                         ret = false;
2637                 batadv_claim_put(claim);
2638         }
2639
2640         batadv_hardif_put(primary_if);
2641         return ret;
2642 }
2643 #endif