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