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