1 /* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors:
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "hard-interface.h"
23 #include "originator.h"
24 #include "bridge_loop_avoidance.h"
25 #include "translation-table.h"
28 #include <linux/etherdevice.h>
29 #include <linux/crc16.h>
30 #include <linux/if_arp.h>
32 #include <linux/if_vlan.h>
34 static const uint8_t announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
36 static void bla_periodic_work(struct work_struct *work);
37 static void bla_send_announce(struct bat_priv *bat_priv,
38 struct backbone_gw *backbone_gw);
40 /* return the index of the claim */
41 static inline uint32_t choose_claim(const void *data, uint32_t size)
43 const unsigned char *key = data;
47 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
60 /* return the index of the backbone gateway */
61 static inline uint32_t choose_backbone_gw(const void *data, uint32_t size)
63 const unsigned char *key = data;
67 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
81 /* compares address and vid of two backbone gws */
82 static int compare_backbone_gw(const struct hlist_node *node, const void *data2)
84 const void *data1 = container_of(node, struct backbone_gw,
87 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
90 /* compares address and vid of two claims */
91 static int compare_claim(const struct hlist_node *node, const void *data2)
93 const void *data1 = container_of(node, struct claim,
96 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
99 /* free a backbone gw */
100 static void backbone_gw_free_ref(struct backbone_gw *backbone_gw)
102 if (atomic_dec_and_test(&backbone_gw->refcount))
103 kfree_rcu(backbone_gw, rcu);
106 /* finally deinitialize the claim */
107 static void claim_free_rcu(struct rcu_head *rcu)
111 claim = container_of(rcu, struct claim, rcu);
113 backbone_gw_free_ref(claim->backbone_gw);
117 /* free a claim, call claim_free_rcu if its the last reference */
118 static void claim_free_ref(struct claim *claim)
120 if (atomic_dec_and_test(&claim->refcount))
121 call_rcu(&claim->rcu, claim_free_rcu);
124 /* @bat_priv: the bat priv with all the soft interface information
125 * @data: search data (may be local/static data)
127 * looks for a claim in the hash, and returns it if found
130 static struct claim *claim_hash_find(struct bat_priv *bat_priv,
133 struct hashtable_t *hash = bat_priv->claim_hash;
134 struct hlist_head *head;
135 struct hlist_node *node;
137 struct claim *claim_tmp = NULL;
143 index = choose_claim(data, hash->size);
144 head = &hash->table[index];
147 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
148 if (!compare_claim(&claim->hash_entry, data))
151 if (!atomic_inc_not_zero(&claim->refcount))
162 /* @bat_priv: the bat priv with all the soft interface information
163 * @addr: the address of the originator
166 * looks for a claim in the hash, and returns it if found
169 static struct backbone_gw *backbone_hash_find(struct bat_priv *bat_priv,
170 uint8_t *addr, short vid)
172 struct hashtable_t *hash = bat_priv->backbone_hash;
173 struct hlist_head *head;
174 struct hlist_node *node;
175 struct backbone_gw search_entry, *backbone_gw;
176 struct backbone_gw *backbone_gw_tmp = NULL;
182 memcpy(search_entry.orig, addr, ETH_ALEN);
183 search_entry.vid = vid;
185 index = choose_backbone_gw(&search_entry, hash->size);
186 head = &hash->table[index];
189 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
190 if (!compare_backbone_gw(&backbone_gw->hash_entry,
194 if (!atomic_inc_not_zero(&backbone_gw->refcount))
197 backbone_gw_tmp = backbone_gw;
202 return backbone_gw_tmp;
205 /* delete all claims for a backbone */
206 static void bla_del_backbone_claims(struct backbone_gw *backbone_gw)
208 struct hashtable_t *hash;
209 struct hlist_node *node, *node_tmp;
210 struct hlist_head *head;
213 spinlock_t *list_lock; /* protects write access to the hash lists */
215 hash = backbone_gw->bat_priv->claim_hash;
219 for (i = 0; i < hash->size; i++) {
220 head = &hash->table[i];
221 list_lock = &hash->list_locks[i];
223 spin_lock_bh(list_lock);
224 hlist_for_each_entry_safe(claim, node, node_tmp,
227 if (claim->backbone_gw != backbone_gw)
230 claim_free_ref(claim);
233 spin_unlock_bh(list_lock);
236 /* all claims gone, intialize CRC */
237 backbone_gw->crc = BLA_CRC_INIT;
240 /* @bat_priv: the bat priv with all the soft interface information
241 * @orig: the mac address to be announced within the claim
243 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
245 * sends a claim frame according to the provided info.
247 static void bla_send_claim(struct bat_priv *bat_priv, uint8_t *mac,
248 short vid, int claimtype)
251 struct ethhdr *ethhdr;
252 struct hard_iface *primary_if;
253 struct net_device *soft_iface;
255 struct bla_claim_dst local_claim_dest;
258 primary_if = batadv_primary_if_get_selected(bat_priv);
262 memcpy(&local_claim_dest, &bat_priv->claim_dest,
263 sizeof(local_claim_dest));
264 local_claim_dest.type = claimtype;
266 soft_iface = primary_if->soft_iface;
268 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
269 /* IP DST: 0.0.0.0 */
271 primary_if->soft_iface,
272 /* IP SRC: 0.0.0.0 */
274 /* Ethernet DST: Broadcast */
276 /* Ethernet SRC/HW SRC: originator mac */
277 primary_if->net_dev->dev_addr,
278 /* HW DST: FF:43:05:XX:00:00
279 * with XX = claim type
280 * and YY:YY = group id
282 (uint8_t *)&local_claim_dest);
287 ethhdr = (struct ethhdr *)skb->data;
288 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
290 /* now we pretend that the client would have sent this ... */
293 /* normal claim frame
294 * set Ethernet SRC to the clients mac
296 memcpy(ethhdr->h_source, mac, ETH_ALEN);
297 bat_dbg(DBG_BLA, bat_priv,
298 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
302 * set HW SRC to the clients mac
304 memcpy(hw_src, mac, ETH_ALEN);
305 bat_dbg(DBG_BLA, bat_priv,
306 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, vid);
308 case CLAIM_TYPE_ANNOUNCE:
309 /* announcement frame
310 * set HW SRC to the special mac containg the crc
312 memcpy(hw_src, mac, ETH_ALEN);
313 bat_dbg(DBG_BLA, bat_priv,
314 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
315 ethhdr->h_source, vid);
317 case CLAIM_TYPE_REQUEST:
319 * set HW SRC to the special mac containg the crc
321 memcpy(hw_src, mac, ETH_ALEN);
322 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
323 bat_dbg(DBG_BLA, bat_priv,
324 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
325 ethhdr->h_source, ethhdr->h_dest, vid);
331 skb = vlan_insert_tag(skb, vid);
333 skb_reset_mac_header(skb);
334 skb->protocol = eth_type_trans(skb, soft_iface);
335 bat_priv->stats.rx_packets++;
336 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
337 soft_iface->last_rx = jiffies;
342 batadv_hardif_free_ref(primary_if);
345 /* @bat_priv: the bat priv with all the soft interface information
346 * @orig: the mac address of the originator
349 * searches for the backbone gw or creates a new one if it could not
352 static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,
353 uint8_t *orig, short vid)
355 struct backbone_gw *entry;
356 struct orig_node *orig_node;
359 entry = backbone_hash_find(bat_priv, orig, vid);
364 bat_dbg(DBG_BLA, bat_priv,
365 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
368 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
373 entry->lasttime = jiffies;
374 entry->crc = BLA_CRC_INIT;
375 entry->bat_priv = bat_priv;
376 atomic_set(&entry->request_sent, 0);
377 memcpy(entry->orig, orig, ETH_ALEN);
379 /* one for the hash, one for returning */
380 atomic_set(&entry->refcount, 2);
382 hash_added = batadv_hash_add(bat_priv->backbone_hash,
383 compare_backbone_gw, choose_backbone_gw,
384 entry, &entry->hash_entry);
386 if (unlikely(hash_added != 0)) {
387 /* hash failed, free the structure */
392 /* this is a gateway now, remove any tt entries */
393 orig_node = batadv_orig_hash_find(bat_priv, orig);
395 batadv_tt_global_del_orig(bat_priv, orig_node,
396 "became a backbone gateway");
397 batadv_orig_node_free_ref(orig_node);
402 /* update or add the own backbone gw to make sure we announce
403 * where we receive other backbone gws
405 static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
406 struct hard_iface *primary_if,
409 struct backbone_gw *backbone_gw;
411 backbone_gw = bla_get_backbone_gw(bat_priv,
412 primary_if->net_dev->dev_addr, vid);
413 if (unlikely(!backbone_gw))
416 backbone_gw->lasttime = jiffies;
417 backbone_gw_free_ref(backbone_gw);
420 /* @bat_priv: the bat priv with all the soft interface information
421 * @vid: the vid where the request came on
423 * Repeat all of our own claims, and finally send an ANNOUNCE frame
424 * to allow the requester another check if the CRC is correct now.
426 static void bla_answer_request(struct bat_priv *bat_priv,
427 struct hard_iface *primary_if, short vid)
429 struct hlist_node *node;
430 struct hlist_head *head;
431 struct hashtable_t *hash;
433 struct backbone_gw *backbone_gw;
436 bat_dbg(DBG_BLA, bat_priv,
437 "bla_answer_request(): received a claim request, send all of our own claims again\n");
439 backbone_gw = backbone_hash_find(bat_priv,
440 primary_if->net_dev->dev_addr, vid);
444 hash = bat_priv->claim_hash;
445 for (i = 0; i < hash->size; i++) {
446 head = &hash->table[i];
449 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
450 /* only own claims are interesting */
451 if (claim->backbone_gw != backbone_gw)
454 bla_send_claim(bat_priv, claim->addr, claim->vid,
460 /* finally, send an announcement frame */
461 bla_send_announce(bat_priv, backbone_gw);
462 backbone_gw_free_ref(backbone_gw);
465 /* @backbone_gw: the backbone gateway from whom we are out of sync
467 * When the crc is wrong, ask the backbone gateway for a full table update.
468 * After the request, it will repeat all of his own claims and finally
469 * send an announcement claim with which we can check again.
471 static void bla_send_request(struct backbone_gw *backbone_gw)
473 /* first, remove all old entries */
474 bla_del_backbone_claims(backbone_gw);
476 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
477 "Sending REQUEST to %pM\n",
481 bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
482 backbone_gw->vid, CLAIM_TYPE_REQUEST);
484 /* no local broadcasts should be sent or received, for now. */
485 if (!atomic_read(&backbone_gw->request_sent)) {
486 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
487 atomic_set(&backbone_gw->request_sent, 1);
491 /* @bat_priv: the bat priv with all the soft interface information
492 * @backbone_gw: our backbone gateway which should be announced
494 * This function sends an announcement. It is called from multiple
497 static void bla_send_announce(struct bat_priv *bat_priv,
498 struct backbone_gw *backbone_gw)
500 uint8_t mac[ETH_ALEN];
503 memcpy(mac, announce_mac, 4);
504 crc = htons(backbone_gw->crc);
505 memcpy(&mac[4], &crc, 2);
507 bla_send_claim(bat_priv, mac, backbone_gw->vid, CLAIM_TYPE_ANNOUNCE);
511 /* @bat_priv: the bat priv with all the soft interface information
512 * @mac: the mac address of the claim
513 * @vid: the VLAN ID of the frame
514 * @backbone_gw: the backbone gateway which claims it
516 * Adds a claim in the claim hash.
518 static void bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
519 const short vid, struct backbone_gw *backbone_gw)
522 struct claim search_claim;
525 memcpy(search_claim.addr, mac, ETH_ALEN);
526 search_claim.vid = vid;
527 claim = claim_hash_find(bat_priv, &search_claim);
529 /* create a new claim entry if it does not exist yet. */
531 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
535 memcpy(claim->addr, mac, ETH_ALEN);
537 claim->lasttime = jiffies;
538 claim->backbone_gw = backbone_gw;
540 atomic_set(&claim->refcount, 2);
541 bat_dbg(DBG_BLA, bat_priv,
542 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
544 hash_added = batadv_hash_add(bat_priv->claim_hash,
545 compare_claim, choose_claim,
546 claim, &claim->hash_entry);
548 if (unlikely(hash_added != 0)) {
549 /* only local changes happened. */
554 claim->lasttime = jiffies;
555 if (claim->backbone_gw == backbone_gw)
556 /* no need to register a new backbone */
559 bat_dbg(DBG_BLA, bat_priv,
560 "bla_add_claim(): changing ownership for %pM, vid %d\n",
563 claim->backbone_gw->crc ^=
564 crc16(0, claim->addr, ETH_ALEN);
565 backbone_gw_free_ref(claim->backbone_gw);
568 /* set (new) backbone gw */
569 atomic_inc(&backbone_gw->refcount);
570 claim->backbone_gw = backbone_gw;
572 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
573 backbone_gw->lasttime = jiffies;
576 claim_free_ref(claim);
579 /* Delete a claim from the claim hash which has the
580 * given mac address and vid.
582 static void bla_del_claim(struct bat_priv *bat_priv, const uint8_t *mac,
585 struct claim search_claim, *claim;
587 memcpy(search_claim.addr, mac, ETH_ALEN);
588 search_claim.vid = vid;
589 claim = claim_hash_find(bat_priv, &search_claim);
593 bat_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac, vid);
595 batadv_hash_remove(bat_priv->claim_hash, compare_claim, choose_claim,
597 claim_free_ref(claim); /* reference from the hash is gone */
599 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
601 /* don't need the reference from hash_find() anymore */
602 claim_free_ref(claim);
605 /* check for ANNOUNCE frame, return 1 if handled */
606 static int handle_announce(struct bat_priv *bat_priv,
607 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
609 struct backbone_gw *backbone_gw;
612 if (memcmp(an_addr, announce_mac, 4) != 0)
615 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
617 if (unlikely(!backbone_gw))
621 /* handle as ANNOUNCE frame */
622 backbone_gw->lasttime = jiffies;
623 crc = ntohs(*((__be16 *)(&an_addr[4])));
625 bat_dbg(DBG_BLA, bat_priv,
626 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
627 vid, backbone_gw->orig, crc);
629 if (backbone_gw->crc != crc) {
630 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
631 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
632 backbone_gw->orig, backbone_gw->vid, backbone_gw->crc,
635 bla_send_request(backbone_gw);
637 /* if we have sent a request and the crc was OK,
638 * we can allow traffic again.
640 if (atomic_read(&backbone_gw->request_sent)) {
641 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
642 atomic_set(&backbone_gw->request_sent, 0);
646 backbone_gw_free_ref(backbone_gw);
650 /* check for REQUEST frame, return 1 if handled */
651 static int handle_request(struct bat_priv *bat_priv,
652 struct hard_iface *primary_if,
653 uint8_t *backbone_addr,
654 struct ethhdr *ethhdr, short vid)
656 /* check for REQUEST frame */
657 if (!compare_eth(backbone_addr, ethhdr->h_dest))
660 /* sanity check, this should not happen on a normal switch,
661 * we ignore it in this case.
663 if (!compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
666 bat_dbg(DBG_BLA, bat_priv,
667 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
668 vid, ethhdr->h_source);
670 bla_answer_request(bat_priv, primary_if, vid);
674 /* check for UNCLAIM frame, return 1 if handled */
675 static int handle_unclaim(struct bat_priv *bat_priv,
676 struct hard_iface *primary_if,
677 uint8_t *backbone_addr,
678 uint8_t *claim_addr, short vid)
680 struct backbone_gw *backbone_gw;
682 /* unclaim in any case if it is our own */
683 if (primary_if && compare_eth(backbone_addr,
684 primary_if->net_dev->dev_addr))
685 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
687 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
692 /* this must be an UNCLAIM frame */
693 bat_dbg(DBG_BLA, bat_priv,
694 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
695 claim_addr, vid, backbone_gw->orig);
697 bla_del_claim(bat_priv, claim_addr, vid);
698 backbone_gw_free_ref(backbone_gw);
702 /* check for CLAIM frame, return 1 if handled */
703 static int handle_claim(struct bat_priv *bat_priv,
704 struct hard_iface *primary_if, uint8_t *backbone_addr,
705 uint8_t *claim_addr, short vid)
707 struct backbone_gw *backbone_gw;
709 /* register the gateway if not yet available, and add the claim. */
711 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
713 if (unlikely(!backbone_gw))
716 /* this must be a CLAIM frame */
717 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
718 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
719 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
721 /* TODO: we could call something like tt_local_del() here. */
723 backbone_gw_free_ref(backbone_gw);
727 /* @bat_priv: the bat priv with all the soft interface information
728 * @hw_src: the Hardware source in the ARP Header
729 * @hw_dst: the Hardware destination in the ARP Header
730 * @ethhdr: pointer to the Ethernet header of the claim frame
732 * checks if it is a claim packet and if its on the same group.
733 * This function also applies the group ID of the sender
734 * if it is in the same mesh.
737 * 2 - if it is a claim packet and on the same group
738 * 1 - if is a claim packet from another group
739 * 0 - if it is not a claim packet
741 static int check_claim_group(struct bat_priv *bat_priv,
742 struct hard_iface *primary_if,
743 uint8_t *hw_src, uint8_t *hw_dst,
744 struct ethhdr *ethhdr)
746 uint8_t *backbone_addr;
747 struct orig_node *orig_node;
748 struct bla_claim_dst *bla_dst, *bla_dst_own;
750 bla_dst = (struct bla_claim_dst *)hw_dst;
751 bla_dst_own = &bat_priv->claim_dest;
753 /* check if it is a claim packet in general */
754 if (memcmp(bla_dst->magic, bla_dst_own->magic,
755 sizeof(bla_dst->magic)) != 0)
758 /* if announcement packet, use the source,
759 * otherwise assume it is in the hw_src
761 switch (bla_dst->type) {
763 backbone_addr = hw_src;
765 case CLAIM_TYPE_REQUEST:
766 case CLAIM_TYPE_ANNOUNCE:
768 backbone_addr = ethhdr->h_source;
774 /* don't accept claim frames from ourselves */
775 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
778 /* if its already the same group, it is fine. */
779 if (bla_dst->group == bla_dst_own->group)
782 /* lets see if this originator is in our mesh */
783 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
785 /* dont accept claims from gateways which are not in
786 * the same mesh or group.
791 /* if our mesh friends mac is bigger, use it for ourselves. */
792 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
793 bat_dbg(DBG_BLA, bat_priv,
794 "taking other backbones claim group: %04x\n",
795 ntohs(bla_dst->group));
796 bla_dst_own->group = bla_dst->group;
799 batadv_orig_node_free_ref(orig_node);
805 /* @bat_priv: the bat priv with all the soft interface information
806 * @skb: the frame to be checked
808 * Check if this is a claim frame, and process it accordingly.
810 * returns 1 if it was a claim frame, otherwise return 0 to
811 * tell the callee that it can use the frame on its own.
813 static int bla_process_claim(struct bat_priv *bat_priv,
814 struct hard_iface *primary_if,
817 struct ethhdr *ethhdr;
818 struct vlan_ethhdr *vhdr;
819 struct arphdr *arphdr;
820 uint8_t *hw_src, *hw_dst;
821 struct bla_claim_dst *bla_dst;
827 ethhdr = (struct ethhdr *)skb_mac_header(skb);
829 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
830 vhdr = (struct vlan_ethhdr *)ethhdr;
831 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
832 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
833 headlen = sizeof(*vhdr);
835 proto = ntohs(ethhdr->h_proto);
839 if (proto != ETH_P_ARP)
840 return 0; /* not a claim frame */
842 /* this must be a ARP frame. check if it is a claim. */
844 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
847 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
848 ethhdr = (struct ethhdr *)skb_mac_header(skb);
849 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
851 /* Check whether the ARP frame carries a valid
854 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
856 if (arphdr->ar_pro != htons(ETH_P_IP))
858 if (arphdr->ar_hln != ETH_ALEN)
860 if (arphdr->ar_pln != 4)
863 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
864 hw_dst = hw_src + ETH_ALEN + 4;
865 bla_dst = (struct bla_claim_dst *)hw_dst;
867 /* check if it is a claim frame. */
868 ret = check_claim_group(bat_priv, primary_if, hw_src, hw_dst, ethhdr);
870 bat_dbg(DBG_BLA, bat_priv,
871 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
872 ethhdr->h_source, vid, hw_src, hw_dst);
877 /* become a backbone gw ourselves on this vlan if not happened yet */
878 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
880 /* check for the different types of claim frames ... */
881 switch (bla_dst->type) {
883 if (handle_claim(bat_priv, primary_if, hw_src,
884 ethhdr->h_source, vid))
888 if (handle_unclaim(bat_priv, primary_if,
889 ethhdr->h_source, hw_src, vid))
893 case CLAIM_TYPE_ANNOUNCE:
894 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
897 case CLAIM_TYPE_REQUEST:
898 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
903 bat_dbg(DBG_BLA, bat_priv,
904 "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",
905 ethhdr->h_source, vid, hw_src, hw_dst);
909 /* Check when we last heard from other nodes, and remove them in case of
910 * a time out, or clean all backbone gws if now is set.
912 static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
914 struct backbone_gw *backbone_gw;
915 struct hlist_node *node, *node_tmp;
916 struct hlist_head *head;
917 struct hashtable_t *hash;
918 spinlock_t *list_lock; /* protects write access to the hash lists */
921 hash = bat_priv->backbone_hash;
925 for (i = 0; i < hash->size; i++) {
926 head = &hash->table[i];
927 list_lock = &hash->list_locks[i];
929 spin_lock_bh(list_lock);
930 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
934 if (!has_timed_out(backbone_gw->lasttime,
935 BLA_BACKBONE_TIMEOUT))
938 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
939 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
943 /* don't wait for the pending request anymore */
944 if (atomic_read(&backbone_gw->request_sent))
945 atomic_dec(&bat_priv->bla_num_requests);
947 bla_del_backbone_claims(backbone_gw);
950 backbone_gw_free_ref(backbone_gw);
952 spin_unlock_bh(list_lock);
956 /* @bat_priv: the bat priv with all the soft interface information
957 * @primary_if: the selected primary interface, may be NULL if now is set
958 * @now: whether the whole hash shall be wiped now
960 * Check when we heard last time from our own claims, and remove them in case of
961 * a time out, or clean all claims if now is set
963 static void bla_purge_claims(struct bat_priv *bat_priv,
964 struct hard_iface *primary_if, int now)
967 struct hlist_node *node;
968 struct hlist_head *head;
969 struct hashtable_t *hash;
972 hash = bat_priv->claim_hash;
976 for (i = 0; i < hash->size; i++) {
977 head = &hash->table[i];
980 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
983 if (!compare_eth(claim->backbone_gw->orig,
984 primary_if->net_dev->dev_addr))
986 if (!has_timed_out(claim->lasttime,
990 bat_dbg(DBG_BLA, bat_priv,
991 "bla_purge_claims(): %pM, vid %d, time out\n",
992 claim->addr, claim->vid);
995 handle_unclaim(bat_priv, primary_if,
996 claim->backbone_gw->orig,
997 claim->addr, claim->vid);
1003 /* @bat_priv: the bat priv with all the soft interface information
1004 * @primary_if: the new selected primary_if
1005 * @oldif: the old primary interface, may be NULL
1007 * Update the backbone gateways when the own orig address changes.
1009 void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1010 struct hard_iface *primary_if,
1011 struct hard_iface *oldif)
1013 struct backbone_gw *backbone_gw;
1014 struct hlist_node *node;
1015 struct hlist_head *head;
1016 struct hashtable_t *hash;
1019 /* reset bridge loop avoidance group id */
1020 bat_priv->claim_dest.group =
1021 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1024 bla_purge_claims(bat_priv, NULL, 1);
1025 bla_purge_backbone_gw(bat_priv, 1);
1029 hash = bat_priv->backbone_hash;
1033 for (i = 0; i < hash->size; i++) {
1034 head = &hash->table[i];
1037 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1038 /* own orig still holds the old value. */
1039 if (!compare_eth(backbone_gw->orig,
1040 oldif->net_dev->dev_addr))
1043 memcpy(backbone_gw->orig,
1044 primary_if->net_dev->dev_addr, ETH_ALEN);
1045 /* send an announce frame so others will ask for our
1046 * claims and update their tables.
1048 bla_send_announce(bat_priv, backbone_gw);
1056 /* (re)start the timer */
1057 static void bla_start_timer(struct bat_priv *bat_priv)
1059 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
1060 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
1061 msecs_to_jiffies(BLA_PERIOD_LENGTH));
1064 /* periodic work to do:
1065 * * purge structures when they are too old
1066 * * send announcements
1068 static void bla_periodic_work(struct work_struct *work)
1070 struct delayed_work *delayed_work =
1071 container_of(work, struct delayed_work, work);
1072 struct bat_priv *bat_priv =
1073 container_of(delayed_work, struct bat_priv, bla_work);
1074 struct hlist_node *node;
1075 struct hlist_head *head;
1076 struct backbone_gw *backbone_gw;
1077 struct hashtable_t *hash;
1078 struct hard_iface *primary_if;
1081 primary_if = batadv_primary_if_get_selected(bat_priv);
1085 bla_purge_claims(bat_priv, primary_if, 0);
1086 bla_purge_backbone_gw(bat_priv, 0);
1088 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1091 hash = bat_priv->backbone_hash;
1095 for (i = 0; i < hash->size; i++) {
1096 head = &hash->table[i];
1099 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1100 if (!compare_eth(backbone_gw->orig,
1101 primary_if->net_dev->dev_addr))
1104 backbone_gw->lasttime = jiffies;
1106 bla_send_announce(bat_priv, backbone_gw);
1112 batadv_hardif_free_ref(primary_if);
1114 bla_start_timer(bat_priv);
1117 /* The hash for claim and backbone hash receive the same key because they
1118 * are getting initialized by hash_new with the same key. Reinitializing
1119 * them with to different keys to allow nested locking without generating
1122 static struct lock_class_key claim_hash_lock_class_key;
1123 static struct lock_class_key backbone_hash_lock_class_key;
1125 /* initialize all bla structures */
1126 int batadv_bla_init(struct bat_priv *bat_priv)
1129 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1130 struct hard_iface *primary_if;
1132 bat_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
1134 /* setting claim destination address */
1135 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1136 bat_priv->claim_dest.type = 0;
1137 primary_if = batadv_primary_if_get_selected(bat_priv);
1139 bat_priv->claim_dest.group =
1140 htons(crc16(0, primary_if->net_dev->dev_addr,
1142 batadv_hardif_free_ref(primary_if);
1144 bat_priv->claim_dest.group = 0; /* will be set later */
1147 /* initialize the duplicate list */
1148 for (i = 0; i < DUPLIST_SIZE; i++)
1149 bat_priv->bcast_duplist[i].entrytime =
1150 jiffies - msecs_to_jiffies(DUPLIST_TIMEOUT);
1151 bat_priv->bcast_duplist_curr = 0;
1153 if (bat_priv->claim_hash)
1156 bat_priv->claim_hash = batadv_hash_new(128);
1157 bat_priv->backbone_hash = batadv_hash_new(32);
1159 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
1162 batadv_hash_set_lock_class(bat_priv->claim_hash,
1163 &claim_hash_lock_class_key);
1164 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1165 &backbone_hash_lock_class_key);
1167 bat_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
1169 bla_start_timer(bat_priv);
1173 /* @bat_priv: the bat priv with all the soft interface information
1174 * @bcast_packet: originator mac address
1175 * @hdr_size: maximum length of the frame
1177 * check if it is on our broadcast list. Another gateway might
1178 * have sent the same packet because it is connected to the same backbone,
1179 * so we have to remove this duplicate.
1181 * This is performed by checking the CRC, which will tell us
1182 * with a good chance that it is the same packet. If it is furthermore
1183 * sent by another host, drop it. We allow equal packets from
1184 * the same host however as this might be intended.
1186 int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1187 struct bcast_packet *bcast_packet,
1190 int i, length, curr;
1193 struct bcast_duplist_entry *entry;
1195 length = hdr_size - sizeof(*bcast_packet);
1196 content = (uint8_t *)bcast_packet;
1197 content += sizeof(*bcast_packet);
1199 /* calculate the crc ... */
1200 crc = crc16(0, content, length);
1202 for (i = 0 ; i < DUPLIST_SIZE; i++) {
1203 curr = (bat_priv->bcast_duplist_curr + i) % DUPLIST_SIZE;
1204 entry = &bat_priv->bcast_duplist[curr];
1206 /* we can stop searching if the entry is too old ;
1207 * later entries will be even older
1209 if (has_timed_out(entry->entrytime, DUPLIST_TIMEOUT))
1212 if (entry->crc != crc)
1215 if (compare_eth(entry->orig, bcast_packet->orig))
1218 /* this entry seems to match: same crc, not too old,
1219 * and from another gw. therefore return 1 to forbid it.
1223 /* not found, add a new entry (overwrite the oldest entry) */
1224 curr = (bat_priv->bcast_duplist_curr + DUPLIST_SIZE - 1) % DUPLIST_SIZE;
1225 entry = &bat_priv->bcast_duplist[curr];
1227 entry->entrytime = jiffies;
1228 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1229 bat_priv->bcast_duplist_curr = curr;
1231 /* allow it, its the first occurence. */
1237 /* @bat_priv: the bat priv with all the soft interface information
1238 * @orig: originator mac address
1240 * check if the originator is a gateway for any VLAN ID.
1242 * returns 1 if it is found, 0 otherwise
1244 int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
1246 struct hashtable_t *hash = bat_priv->backbone_hash;
1247 struct hlist_head *head;
1248 struct hlist_node *node;
1249 struct backbone_gw *backbone_gw;
1252 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1258 for (i = 0; i < hash->size; i++) {
1259 head = &hash->table[i];
1262 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1263 if (compare_eth(backbone_gw->orig, orig)) {
1275 /* @skb: the frame to be checked
1276 * @orig_node: the orig_node of the frame
1277 * @hdr_size: maximum length of the frame
1279 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1280 * if the orig_node is also a gateway on the soft interface, otherwise it
1283 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1284 struct orig_node *orig_node, int hdr_size)
1286 struct ethhdr *ethhdr;
1287 struct vlan_ethhdr *vhdr;
1288 struct backbone_gw *backbone_gw;
1291 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1294 /* first, find out the vid. */
1295 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1298 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1300 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1301 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1304 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1306 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1309 /* see if this originator is a backbone gw for this VLAN */
1310 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1311 orig_node->orig, vid);
1315 backbone_gw_free_ref(backbone_gw);
1319 /* free all bla structures (for softinterface free or module unload) */
1320 void batadv_bla_free(struct bat_priv *bat_priv)
1322 struct hard_iface *primary_if;
1324 cancel_delayed_work_sync(&bat_priv->bla_work);
1325 primary_if = batadv_primary_if_get_selected(bat_priv);
1327 if (bat_priv->claim_hash) {
1328 bla_purge_claims(bat_priv, primary_if, 1);
1329 batadv_hash_destroy(bat_priv->claim_hash);
1330 bat_priv->claim_hash = NULL;
1332 if (bat_priv->backbone_hash) {
1333 bla_purge_backbone_gw(bat_priv, 1);
1334 batadv_hash_destroy(bat_priv->backbone_hash);
1335 bat_priv->backbone_hash = NULL;
1338 batadv_hardif_free_ref(primary_if);
1341 /* @bat_priv: the bat priv with all the soft interface information
1342 * @skb: the frame to be checked
1343 * @vid: the VLAN ID of the frame
1345 * bla_rx avoidance checks if:
1346 * * we have to race for a claim
1347 * * if the frame is allowed on the LAN
1349 * in these cases, the skb is further handled by this function and
1350 * returns 1, otherwise it returns 0 and the caller shall further
1353 int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1355 struct ethhdr *ethhdr;
1356 struct claim search_claim, *claim = NULL;
1357 struct hard_iface *primary_if;
1360 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1362 primary_if = batadv_primary_if_get_selected(bat_priv);
1366 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1370 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1371 /* don't allow broadcasts while requests are in flight */
1372 if (is_multicast_ether_addr(ethhdr->h_dest))
1375 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1376 search_claim.vid = vid;
1377 claim = claim_hash_find(bat_priv, &search_claim);
1380 /* possible optimization: race for a claim */
1381 /* No claim exists yet, claim it for us!
1383 handle_claim(bat_priv, primary_if,
1384 primary_if->net_dev->dev_addr,
1385 ethhdr->h_source, vid);
1389 /* if it is our own claim ... */
1390 if (compare_eth(claim->backbone_gw->orig,
1391 primary_if->net_dev->dev_addr)) {
1392 /* ... allow it in any case */
1393 claim->lasttime = jiffies;
1397 /* if it is a broadcast ... */
1398 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1399 /* ... drop it. the responsible gateway is in charge. */
1402 /* seems the client considers us as its best gateway.
1403 * send a claim and update the claim table
1406 handle_claim(bat_priv, primary_if,
1407 primary_if->net_dev->dev_addr,
1408 ethhdr->h_source, vid);
1412 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1422 batadv_hardif_free_ref(primary_if);
1424 claim_free_ref(claim);
1428 /* @bat_priv: the bat priv with all the soft interface information
1429 * @skb: the frame to be checked
1430 * @vid: the VLAN ID of the frame
1433 * * a claim was received which has to be processed
1434 * * the frame is allowed on the mesh
1436 * in these cases, the skb is further handled by this function and
1437 * returns 1, otherwise it returns 0 and the caller shall further
1440 int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1442 struct ethhdr *ethhdr;
1443 struct claim search_claim, *claim = NULL;
1444 struct hard_iface *primary_if;
1447 primary_if = batadv_primary_if_get_selected(bat_priv);
1451 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1454 /* in VLAN case, the mac header might not be set. */
1455 skb_reset_mac_header(skb);
1457 if (bla_process_claim(bat_priv, primary_if, skb))
1460 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1462 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1463 /* don't allow broadcasts while requests are in flight */
1464 if (is_multicast_ether_addr(ethhdr->h_dest))
1467 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1468 search_claim.vid = vid;
1470 claim = claim_hash_find(bat_priv, &search_claim);
1472 /* if no claim exists, allow it. */
1476 /* check if we are responsible. */
1477 if (compare_eth(claim->backbone_gw->orig,
1478 primary_if->net_dev->dev_addr)) {
1479 /* if yes, the client has roamed and we have
1482 handle_unclaim(bat_priv, primary_if,
1483 primary_if->net_dev->dev_addr,
1484 ethhdr->h_source, vid);
1488 /* check if it is a multicast/broadcast frame */
1489 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1490 /* drop it. the responsible gateway has forwarded it into
1491 * the backbone network.
1495 /* we must allow it. at least if we are
1496 * responsible for the DESTINATION.
1501 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1508 batadv_hardif_free_ref(primary_if);
1510 claim_free_ref(claim);
1514 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1516 struct net_device *net_dev = (struct net_device *)seq->private;
1517 struct bat_priv *bat_priv = netdev_priv(net_dev);
1518 struct hashtable_t *hash = bat_priv->claim_hash;
1519 struct claim *claim;
1520 struct hard_iface *primary_if;
1521 struct hlist_node *node;
1522 struct hlist_head *head;
1527 primary_if = batadv_primary_if_get_selected(bat_priv);
1529 ret = seq_printf(seq,
1530 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1535 if (primary_if->if_status != IF_ACTIVE) {
1536 ret = seq_printf(seq,
1537 "BATMAN mesh %s disabled - primary interface not active\n",
1543 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1544 net_dev->name, primary_if->net_dev->dev_addr,
1545 ntohs(bat_priv->claim_dest.group));
1546 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1547 "Client", "VID", "Originator", "CRC");
1548 for (i = 0; i < hash->size; i++) {
1549 head = &hash->table[i];
1552 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1553 is_own = compare_eth(claim->backbone_gw->orig,
1554 primary_if->net_dev->dev_addr);
1555 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1556 claim->addr, claim->vid,
1557 claim->backbone_gw->orig,
1558 (is_own ? 'x' : ' '),
1559 claim->backbone_gw->crc);
1565 batadv_hardif_free_ref(primary_if);