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