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 batadv_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 batadv_dbg(DBG_BLA, bat_priv,
306 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
309 case CLAIM_TYPE_ANNOUNCE:
310 /* announcement frame
311 * set HW SRC to the special mac containg the crc
313 memcpy(hw_src, mac, ETH_ALEN);
314 batadv_dbg(DBG_BLA, bat_priv,
315 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
316 ethhdr->h_source, vid);
318 case CLAIM_TYPE_REQUEST:
320 * set HW SRC to the special mac containg the crc
322 memcpy(hw_src, mac, ETH_ALEN);
323 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
324 batadv_dbg(DBG_BLA, bat_priv,
325 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
326 ethhdr->h_source, ethhdr->h_dest, vid);
332 skb = vlan_insert_tag(skb, vid);
334 skb_reset_mac_header(skb);
335 skb->protocol = eth_type_trans(skb, soft_iface);
336 bat_priv->stats.rx_packets++;
337 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
338 soft_iface->last_rx = jiffies;
343 batadv_hardif_free_ref(primary_if);
346 /* @bat_priv: the bat priv with all the soft interface information
347 * @orig: the mac address of the originator
350 * searches for the backbone gw or creates a new one if it could not
353 static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,
354 uint8_t *orig, short vid)
356 struct backbone_gw *entry;
357 struct orig_node *orig_node;
360 entry = backbone_hash_find(bat_priv, orig, vid);
365 batadv_dbg(DBG_BLA, bat_priv,
366 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
369 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
374 entry->lasttime = jiffies;
375 entry->crc = BLA_CRC_INIT;
376 entry->bat_priv = bat_priv;
377 atomic_set(&entry->request_sent, 0);
378 memcpy(entry->orig, orig, ETH_ALEN);
380 /* one for the hash, one for returning */
381 atomic_set(&entry->refcount, 2);
383 hash_added = batadv_hash_add(bat_priv->backbone_hash,
384 compare_backbone_gw, choose_backbone_gw,
385 entry, &entry->hash_entry);
387 if (unlikely(hash_added != 0)) {
388 /* hash failed, free the structure */
393 /* this is a gateway now, remove any tt entries */
394 orig_node = batadv_orig_hash_find(bat_priv, orig);
396 batadv_tt_global_del_orig(bat_priv, orig_node,
397 "became a backbone gateway");
398 batadv_orig_node_free_ref(orig_node);
403 /* update or add the own backbone gw to make sure we announce
404 * where we receive other backbone gws
406 static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
407 struct hard_iface *primary_if,
410 struct backbone_gw *backbone_gw;
412 backbone_gw = bla_get_backbone_gw(bat_priv,
413 primary_if->net_dev->dev_addr, vid);
414 if (unlikely(!backbone_gw))
417 backbone_gw->lasttime = jiffies;
418 backbone_gw_free_ref(backbone_gw);
421 /* @bat_priv: the bat priv with all the soft interface information
422 * @vid: the vid where the request came on
424 * Repeat all of our own claims, and finally send an ANNOUNCE frame
425 * to allow the requester another check if the CRC is correct now.
427 static void bla_answer_request(struct bat_priv *bat_priv,
428 struct hard_iface *primary_if, short vid)
430 struct hlist_node *node;
431 struct hlist_head *head;
432 struct hashtable_t *hash;
434 struct backbone_gw *backbone_gw;
437 batadv_dbg(DBG_BLA, bat_priv,
438 "bla_answer_request(): received a claim request, send all of our own claims again\n");
440 backbone_gw = backbone_hash_find(bat_priv,
441 primary_if->net_dev->dev_addr, vid);
445 hash = bat_priv->claim_hash;
446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
450 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
451 /* only own claims are interesting */
452 if (claim->backbone_gw != backbone_gw)
455 bla_send_claim(bat_priv, claim->addr, claim->vid,
461 /* finally, send an announcement frame */
462 bla_send_announce(bat_priv, backbone_gw);
463 backbone_gw_free_ref(backbone_gw);
466 /* @backbone_gw: the backbone gateway from whom we are out of sync
468 * When the crc is wrong, ask the backbone gateway for a full table update.
469 * After the request, it will repeat all of his own claims and finally
470 * send an announcement claim with which we can check again.
472 static void bla_send_request(struct backbone_gw *backbone_gw)
474 /* first, remove all old entries */
475 bla_del_backbone_claims(backbone_gw);
477 batadv_dbg(DBG_BLA, backbone_gw->bat_priv, "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 batadv_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 batadv_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 batadv_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac,
596 batadv_hash_remove(bat_priv->claim_hash, compare_claim, choose_claim,
598 claim_free_ref(claim); /* reference from the hash is gone */
600 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
602 /* don't need the reference from hash_find() anymore */
603 claim_free_ref(claim);
606 /* check for ANNOUNCE frame, return 1 if handled */
607 static int handle_announce(struct bat_priv *bat_priv,
608 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
610 struct backbone_gw *backbone_gw;
613 if (memcmp(an_addr, announce_mac, 4) != 0)
616 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
618 if (unlikely(!backbone_gw))
622 /* handle as ANNOUNCE frame */
623 backbone_gw->lasttime = jiffies;
624 crc = ntohs(*((__be16 *)(&an_addr[4])));
626 batadv_dbg(DBG_BLA, bat_priv,
627 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
628 vid, backbone_gw->orig, crc);
630 if (backbone_gw->crc != crc) {
631 batadv_dbg(DBG_BLA, backbone_gw->bat_priv,
632 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
633 backbone_gw->orig, backbone_gw->vid,
634 backbone_gw->crc, crc);
636 bla_send_request(backbone_gw);
638 /* if we have sent a request and the crc was OK,
639 * we can allow traffic again.
641 if (atomic_read(&backbone_gw->request_sent)) {
642 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
643 atomic_set(&backbone_gw->request_sent, 0);
647 backbone_gw_free_ref(backbone_gw);
651 /* check for REQUEST frame, return 1 if handled */
652 static int handle_request(struct bat_priv *bat_priv,
653 struct hard_iface *primary_if,
654 uint8_t *backbone_addr,
655 struct ethhdr *ethhdr, short vid)
657 /* check for REQUEST frame */
658 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
661 /* sanity check, this should not happen on a normal switch,
662 * we ignore it in this case.
664 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
667 batadv_dbg(DBG_BLA, bat_priv,
668 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
669 vid, ethhdr->h_source);
671 bla_answer_request(bat_priv, primary_if, vid);
675 /* check for UNCLAIM frame, return 1 if handled */
676 static int handle_unclaim(struct bat_priv *bat_priv,
677 struct hard_iface *primary_if,
678 uint8_t *backbone_addr,
679 uint8_t *claim_addr, short vid)
681 struct backbone_gw *backbone_gw;
683 /* unclaim in any case if it is our own */
684 if (primary_if && batadv_compare_eth(backbone_addr,
685 primary_if->net_dev->dev_addr))
686 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
688 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
693 /* this must be an UNCLAIM frame */
694 batadv_dbg(DBG_BLA, bat_priv,
695 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
696 claim_addr, vid, backbone_gw->orig);
698 bla_del_claim(bat_priv, claim_addr, vid);
699 backbone_gw_free_ref(backbone_gw);
703 /* check for CLAIM frame, return 1 if handled */
704 static int handle_claim(struct bat_priv *bat_priv,
705 struct hard_iface *primary_if, uint8_t *backbone_addr,
706 uint8_t *claim_addr, short vid)
708 struct backbone_gw *backbone_gw;
710 /* register the gateway if not yet available, and add the claim. */
712 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
714 if (unlikely(!backbone_gw))
717 /* this must be a CLAIM frame */
718 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
719 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
720 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
722 /* TODO: we could call something like tt_local_del() here. */
724 backbone_gw_free_ref(backbone_gw);
728 /* @bat_priv: the bat priv with all the soft interface information
729 * @hw_src: the Hardware source in the ARP Header
730 * @hw_dst: the Hardware destination in the ARP Header
731 * @ethhdr: pointer to the Ethernet header of the claim frame
733 * checks if it is a claim packet and if its on the same group.
734 * This function also applies the group ID of the sender
735 * if it is in the same mesh.
738 * 2 - if it is a claim packet and on the same group
739 * 1 - if is a claim packet from another group
740 * 0 - if it is not a claim packet
742 static int check_claim_group(struct bat_priv *bat_priv,
743 struct hard_iface *primary_if,
744 uint8_t *hw_src, uint8_t *hw_dst,
745 struct ethhdr *ethhdr)
747 uint8_t *backbone_addr;
748 struct orig_node *orig_node;
749 struct bla_claim_dst *bla_dst, *bla_dst_own;
751 bla_dst = (struct bla_claim_dst *)hw_dst;
752 bla_dst_own = &bat_priv->claim_dest;
754 /* check if it is a claim packet in general */
755 if (memcmp(bla_dst->magic, bla_dst_own->magic,
756 sizeof(bla_dst->magic)) != 0)
759 /* if announcement packet, use the source,
760 * otherwise assume it is in the hw_src
762 switch (bla_dst->type) {
764 backbone_addr = hw_src;
766 case CLAIM_TYPE_REQUEST:
767 case CLAIM_TYPE_ANNOUNCE:
769 backbone_addr = ethhdr->h_source;
775 /* don't accept claim frames from ourselves */
776 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
779 /* if its already the same group, it is fine. */
780 if (bla_dst->group == bla_dst_own->group)
783 /* lets see if this originator is in our mesh */
784 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
786 /* dont accept claims from gateways which are not in
787 * the same mesh or group.
792 /* if our mesh friends mac is bigger, use it for ourselves. */
793 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
794 batadv_dbg(DBG_BLA, bat_priv,
795 "taking other backbones claim group: %04x\n",
796 ntohs(bla_dst->group));
797 bla_dst_own->group = bla_dst->group;
800 batadv_orig_node_free_ref(orig_node);
806 /* @bat_priv: the bat priv with all the soft interface information
807 * @skb: the frame to be checked
809 * Check if this is a claim frame, and process it accordingly.
811 * returns 1 if it was a claim frame, otherwise return 0 to
812 * tell the callee that it can use the frame on its own.
814 static int bla_process_claim(struct bat_priv *bat_priv,
815 struct hard_iface *primary_if,
818 struct ethhdr *ethhdr;
819 struct vlan_ethhdr *vhdr;
820 struct arphdr *arphdr;
821 uint8_t *hw_src, *hw_dst;
822 struct bla_claim_dst *bla_dst;
828 ethhdr = (struct ethhdr *)skb_mac_header(skb);
830 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
831 vhdr = (struct vlan_ethhdr *)ethhdr;
832 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
833 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
834 headlen = sizeof(*vhdr);
836 proto = ntohs(ethhdr->h_proto);
840 if (proto != ETH_P_ARP)
841 return 0; /* not a claim frame */
843 /* this must be a ARP frame. check if it is a claim. */
845 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
848 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
849 ethhdr = (struct ethhdr *)skb_mac_header(skb);
850 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
852 /* Check whether the ARP frame carries a valid
855 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
857 if (arphdr->ar_pro != htons(ETH_P_IP))
859 if (arphdr->ar_hln != ETH_ALEN)
861 if (arphdr->ar_pln != 4)
864 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
865 hw_dst = hw_src + ETH_ALEN + 4;
866 bla_dst = (struct bla_claim_dst *)hw_dst;
868 /* check if it is a claim frame. */
869 ret = check_claim_group(bat_priv, primary_if, hw_src, hw_dst, ethhdr);
871 batadv_dbg(DBG_BLA, bat_priv,
872 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
873 ethhdr->h_source, vid, hw_src, hw_dst);
878 /* become a backbone gw ourselves on this vlan if not happened yet */
879 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
881 /* check for the different types of claim frames ... */
882 switch (bla_dst->type) {
884 if (handle_claim(bat_priv, primary_if, hw_src,
885 ethhdr->h_source, vid))
889 if (handle_unclaim(bat_priv, primary_if,
890 ethhdr->h_source, hw_src, vid))
894 case CLAIM_TYPE_ANNOUNCE:
895 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
898 case CLAIM_TYPE_REQUEST:
899 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
904 batadv_dbg(DBG_BLA, bat_priv,
905 "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",
906 ethhdr->h_source, vid, hw_src, hw_dst);
910 /* Check when we last heard from other nodes, and remove them in case of
911 * a time out, or clean all backbone gws if now is set.
913 static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
915 struct backbone_gw *backbone_gw;
916 struct hlist_node *node, *node_tmp;
917 struct hlist_head *head;
918 struct hashtable_t *hash;
919 spinlock_t *list_lock; /* protects write access to the hash lists */
922 hash = bat_priv->backbone_hash;
926 for (i = 0; i < hash->size; i++) {
927 head = &hash->table[i];
928 list_lock = &hash->list_locks[i];
930 spin_lock_bh(list_lock);
931 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
935 if (!batadv_has_timed_out(backbone_gw->lasttime,
936 BLA_BACKBONE_TIMEOUT))
939 batadv_dbg(DBG_BLA, backbone_gw->bat_priv,
940 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
944 /* don't wait for the pending request anymore */
945 if (atomic_read(&backbone_gw->request_sent))
946 atomic_dec(&bat_priv->bla_num_requests);
948 bla_del_backbone_claims(backbone_gw);
951 backbone_gw_free_ref(backbone_gw);
953 spin_unlock_bh(list_lock);
957 /* @bat_priv: the bat priv with all the soft interface information
958 * @primary_if: the selected primary interface, may be NULL if now is set
959 * @now: whether the whole hash shall be wiped now
961 * Check when we heard last time from our own claims, and remove them in case of
962 * a time out, or clean all claims if now is set
964 static void bla_purge_claims(struct bat_priv *bat_priv,
965 struct hard_iface *primary_if, int now)
968 struct hlist_node *node;
969 struct hlist_head *head;
970 struct hashtable_t *hash;
973 hash = bat_priv->claim_hash;
977 for (i = 0; i < hash->size; i++) {
978 head = &hash->table[i];
981 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
984 if (!batadv_compare_eth(claim->backbone_gw->orig,
985 primary_if->net_dev->dev_addr))
987 if (!batadv_has_timed_out(claim->lasttime,
991 batadv_dbg(DBG_BLA, bat_priv,
992 "bla_purge_claims(): %pM, vid %d, time out\n",
993 claim->addr, claim->vid);
996 handle_unclaim(bat_priv, primary_if,
997 claim->backbone_gw->orig,
998 claim->addr, claim->vid);
1004 /* @bat_priv: the bat priv with all the soft interface information
1005 * @primary_if: the new selected primary_if
1006 * @oldif: the old primary interface, may be NULL
1008 * Update the backbone gateways when the own orig address changes.
1010 void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1011 struct hard_iface *primary_if,
1012 struct hard_iface *oldif)
1014 struct backbone_gw *backbone_gw;
1015 struct hlist_node *node;
1016 struct hlist_head *head;
1017 struct hashtable_t *hash;
1020 /* reset bridge loop avoidance group id */
1021 bat_priv->claim_dest.group =
1022 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1025 bla_purge_claims(bat_priv, NULL, 1);
1026 bla_purge_backbone_gw(bat_priv, 1);
1030 hash = bat_priv->backbone_hash;
1034 for (i = 0; i < hash->size; i++) {
1035 head = &hash->table[i];
1038 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1039 /* own orig still holds the old value. */
1040 if (!batadv_compare_eth(backbone_gw->orig,
1041 oldif->net_dev->dev_addr))
1044 memcpy(backbone_gw->orig,
1045 primary_if->net_dev->dev_addr, ETH_ALEN);
1046 /* send an announce frame so others will ask for our
1047 * claims and update their tables.
1049 bla_send_announce(bat_priv, backbone_gw);
1057 /* (re)start the timer */
1058 static void bla_start_timer(struct bat_priv *bat_priv)
1060 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
1061 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
1062 msecs_to_jiffies(BLA_PERIOD_LENGTH));
1065 /* periodic work to do:
1066 * * purge structures when they are too old
1067 * * send announcements
1069 static void bla_periodic_work(struct work_struct *work)
1071 struct delayed_work *delayed_work =
1072 container_of(work, struct delayed_work, work);
1073 struct bat_priv *bat_priv =
1074 container_of(delayed_work, struct bat_priv, bla_work);
1075 struct hlist_node *node;
1076 struct hlist_head *head;
1077 struct backbone_gw *backbone_gw;
1078 struct hashtable_t *hash;
1079 struct hard_iface *primary_if;
1082 primary_if = batadv_primary_if_get_selected(bat_priv);
1086 bla_purge_claims(bat_priv, primary_if, 0);
1087 bla_purge_backbone_gw(bat_priv, 0);
1089 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1092 hash = bat_priv->backbone_hash;
1096 for (i = 0; i < hash->size; i++) {
1097 head = &hash->table[i];
1100 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1101 if (!batadv_compare_eth(backbone_gw->orig,
1102 primary_if->net_dev->dev_addr))
1105 backbone_gw->lasttime = jiffies;
1107 bla_send_announce(bat_priv, backbone_gw);
1113 batadv_hardif_free_ref(primary_if);
1115 bla_start_timer(bat_priv);
1118 /* The hash for claim and backbone hash receive the same key because they
1119 * are getting initialized by hash_new with the same key. Reinitializing
1120 * them with to different keys to allow nested locking without generating
1123 static struct lock_class_key claim_hash_lock_class_key;
1124 static struct lock_class_key backbone_hash_lock_class_key;
1126 /* initialize all bla structures */
1127 int batadv_bla_init(struct bat_priv *bat_priv)
1130 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1131 struct hard_iface *primary_if;
1133 batadv_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
1135 /* setting claim destination address */
1136 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1137 bat_priv->claim_dest.type = 0;
1138 primary_if = batadv_primary_if_get_selected(bat_priv);
1140 bat_priv->claim_dest.group =
1141 htons(crc16(0, primary_if->net_dev->dev_addr,
1143 batadv_hardif_free_ref(primary_if);
1145 bat_priv->claim_dest.group = 0; /* will be set later */
1148 /* initialize the duplicate list */
1149 for (i = 0; i < DUPLIST_SIZE; i++)
1150 bat_priv->bcast_duplist[i].entrytime =
1151 jiffies - msecs_to_jiffies(DUPLIST_TIMEOUT);
1152 bat_priv->bcast_duplist_curr = 0;
1154 if (bat_priv->claim_hash)
1157 bat_priv->claim_hash = batadv_hash_new(128);
1158 bat_priv->backbone_hash = batadv_hash_new(32);
1160 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
1163 batadv_hash_set_lock_class(bat_priv->claim_hash,
1164 &claim_hash_lock_class_key);
1165 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1166 &backbone_hash_lock_class_key);
1168 batadv_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
1170 bla_start_timer(bat_priv);
1174 /* @bat_priv: the bat priv with all the soft interface information
1175 * @bcast_packet: originator mac address
1176 * @hdr_size: maximum length of the frame
1178 * check if it is on our broadcast list. Another gateway might
1179 * have sent the same packet because it is connected to the same backbone,
1180 * so we have to remove this duplicate.
1182 * This is performed by checking the CRC, which will tell us
1183 * with a good chance that it is the same packet. If it is furthermore
1184 * sent by another host, drop it. We allow equal packets from
1185 * the same host however as this might be intended.
1187 int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1188 struct bcast_packet *bcast_packet,
1191 int i, length, curr;
1194 struct bcast_duplist_entry *entry;
1196 length = hdr_size - sizeof(*bcast_packet);
1197 content = (uint8_t *)bcast_packet;
1198 content += sizeof(*bcast_packet);
1200 /* calculate the crc ... */
1201 crc = crc16(0, content, length);
1203 for (i = 0 ; i < DUPLIST_SIZE; i++) {
1204 curr = (bat_priv->bcast_duplist_curr + i) % DUPLIST_SIZE;
1205 entry = &bat_priv->bcast_duplist[curr];
1207 /* we can stop searching if the entry is too old ;
1208 * later entries will be even older
1210 if (batadv_has_timed_out(entry->entrytime, DUPLIST_TIMEOUT))
1213 if (entry->crc != crc)
1216 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1219 /* this entry seems to match: same crc, not too old,
1220 * and from another gw. therefore return 1 to forbid it.
1224 /* not found, add a new entry (overwrite the oldest entry) */
1225 curr = (bat_priv->bcast_duplist_curr + DUPLIST_SIZE - 1) % DUPLIST_SIZE;
1226 entry = &bat_priv->bcast_duplist[curr];
1228 entry->entrytime = jiffies;
1229 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1230 bat_priv->bcast_duplist_curr = curr;
1232 /* allow it, its the first occurence. */
1238 /* @bat_priv: the bat priv with all the soft interface information
1239 * @orig: originator mac address
1241 * check if the originator is a gateway for any VLAN ID.
1243 * returns 1 if it is found, 0 otherwise
1245 int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
1247 struct hashtable_t *hash = bat_priv->backbone_hash;
1248 struct hlist_head *head;
1249 struct hlist_node *node;
1250 struct backbone_gw *backbone_gw;
1253 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1259 for (i = 0; i < hash->size; i++) {
1260 head = &hash->table[i];
1263 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1264 if (batadv_compare_eth(backbone_gw->orig, orig)) {
1276 /* @skb: the frame to be checked
1277 * @orig_node: the orig_node of the frame
1278 * @hdr_size: maximum length of the frame
1280 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1281 * if the orig_node is also a gateway on the soft interface, otherwise it
1284 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1285 struct orig_node *orig_node, int hdr_size)
1287 struct ethhdr *ethhdr;
1288 struct vlan_ethhdr *vhdr;
1289 struct backbone_gw *backbone_gw;
1292 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1295 /* first, find out the vid. */
1296 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1299 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1301 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1302 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1305 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1307 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1310 /* see if this originator is a backbone gw for this VLAN */
1311 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1312 orig_node->orig, vid);
1316 backbone_gw_free_ref(backbone_gw);
1320 /* free all bla structures (for softinterface free or module unload) */
1321 void batadv_bla_free(struct bat_priv *bat_priv)
1323 struct hard_iface *primary_if;
1325 cancel_delayed_work_sync(&bat_priv->bla_work);
1326 primary_if = batadv_primary_if_get_selected(bat_priv);
1328 if (bat_priv->claim_hash) {
1329 bla_purge_claims(bat_priv, primary_if, 1);
1330 batadv_hash_destroy(bat_priv->claim_hash);
1331 bat_priv->claim_hash = NULL;
1333 if (bat_priv->backbone_hash) {
1334 bla_purge_backbone_gw(bat_priv, 1);
1335 batadv_hash_destroy(bat_priv->backbone_hash);
1336 bat_priv->backbone_hash = NULL;
1339 batadv_hardif_free_ref(primary_if);
1342 /* @bat_priv: the bat priv with all the soft interface information
1343 * @skb: the frame to be checked
1344 * @vid: the VLAN ID of the frame
1346 * bla_rx avoidance checks if:
1347 * * we have to race for a claim
1348 * * if the frame is allowed on the LAN
1350 * in these cases, the skb is further handled by this function and
1351 * returns 1, otherwise it returns 0 and the caller shall further
1354 int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1356 struct ethhdr *ethhdr;
1357 struct claim search_claim, *claim = NULL;
1358 struct hard_iface *primary_if;
1361 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1363 primary_if = batadv_primary_if_get_selected(bat_priv);
1367 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1371 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1372 /* don't allow broadcasts while requests are in flight */
1373 if (is_multicast_ether_addr(ethhdr->h_dest))
1376 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1377 search_claim.vid = vid;
1378 claim = claim_hash_find(bat_priv, &search_claim);
1381 /* possible optimization: race for a claim */
1382 /* No claim exists yet, claim it for us!
1384 handle_claim(bat_priv, primary_if,
1385 primary_if->net_dev->dev_addr,
1386 ethhdr->h_source, vid);
1390 /* if it is our own claim ... */
1391 if (batadv_compare_eth(claim->backbone_gw->orig,
1392 primary_if->net_dev->dev_addr)) {
1393 /* ... allow it in any case */
1394 claim->lasttime = jiffies;
1398 /* if it is a broadcast ... */
1399 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1400 /* ... drop it. the responsible gateway is in charge. */
1403 /* seems the client considers us as its best gateway.
1404 * send a claim and update the claim table
1407 handle_claim(bat_priv, primary_if,
1408 primary_if->net_dev->dev_addr,
1409 ethhdr->h_source, vid);
1413 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1423 batadv_hardif_free_ref(primary_if);
1425 claim_free_ref(claim);
1429 /* @bat_priv: the bat priv with all the soft interface information
1430 * @skb: the frame to be checked
1431 * @vid: the VLAN ID of the frame
1434 * * a claim was received which has to be processed
1435 * * the frame is allowed on the mesh
1437 * in these cases, the skb is further handled by this function and
1438 * returns 1, otherwise it returns 0 and the caller shall further
1441 int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1443 struct ethhdr *ethhdr;
1444 struct claim search_claim, *claim = NULL;
1445 struct hard_iface *primary_if;
1448 primary_if = batadv_primary_if_get_selected(bat_priv);
1452 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1455 /* in VLAN case, the mac header might not be set. */
1456 skb_reset_mac_header(skb);
1458 if (bla_process_claim(bat_priv, primary_if, skb))
1461 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1463 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1464 /* don't allow broadcasts while requests are in flight */
1465 if (is_multicast_ether_addr(ethhdr->h_dest))
1468 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1469 search_claim.vid = vid;
1471 claim = claim_hash_find(bat_priv, &search_claim);
1473 /* if no claim exists, allow it. */
1477 /* check if we are responsible. */
1478 if (batadv_compare_eth(claim->backbone_gw->orig,
1479 primary_if->net_dev->dev_addr)) {
1480 /* if yes, the client has roamed and we have
1483 handle_unclaim(bat_priv, primary_if,
1484 primary_if->net_dev->dev_addr,
1485 ethhdr->h_source, vid);
1489 /* check if it is a multicast/broadcast frame */
1490 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1491 /* drop it. the responsible gateway has forwarded it into
1492 * the backbone network.
1496 /* we must allow it. at least if we are
1497 * responsible for the DESTINATION.
1502 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1509 batadv_hardif_free_ref(primary_if);
1511 claim_free_ref(claim);
1515 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1517 struct net_device *net_dev = (struct net_device *)seq->private;
1518 struct bat_priv *bat_priv = netdev_priv(net_dev);
1519 struct hashtable_t *hash = bat_priv->claim_hash;
1520 struct claim *claim;
1521 struct hard_iface *primary_if;
1522 struct hlist_node *node;
1523 struct hlist_head *head;
1527 uint8_t *primary_addr;
1529 primary_if = batadv_primary_if_get_selected(bat_priv);
1531 ret = seq_printf(seq,
1532 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1537 if (primary_if->if_status != IF_ACTIVE) {
1538 ret = seq_printf(seq,
1539 "BATMAN mesh %s disabled - primary interface not active\n",
1544 primary_addr = primary_if->net_dev->dev_addr;
1546 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1547 net_dev->name, primary_addr,
1548 ntohs(bat_priv->claim_dest.group));
1549 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1550 "Client", "VID", "Originator", "CRC");
1551 for (i = 0; i < hash->size; i++) {
1552 head = &hash->table[i];
1555 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1556 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1558 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1559 claim->addr, claim->vid,
1560 claim->backbone_gw->orig,
1561 (is_own ? 'x' : ' '),
1562 claim->backbone_gw->crc);
1568 batadv_hardif_free_ref(primary_if);