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 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
36 static void batadv_bla_periodic_work(struct work_struct *work);
37 static void batadv_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 batadv_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 batadv_choose_backbone_gw(const void *data,
64 const unsigned char *key = data;
68 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
82 /* compares address and vid of two backbone gws */
83 static int batadv_compare_backbone_gw(const struct hlist_node *node,
86 const void *data1 = container_of(node, struct backbone_gw,
89 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
92 /* compares address and vid of two claims */
93 static int batadv_compare_claim(const struct hlist_node *node,
96 const void *data1 = container_of(node, struct claim,
99 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
102 /* free a backbone gw */
103 static void batadv_backbone_gw_free_ref(struct backbone_gw *backbone_gw)
105 if (atomic_dec_and_test(&backbone_gw->refcount))
106 kfree_rcu(backbone_gw, rcu);
109 /* finally deinitialize the claim */
110 static void batadv_claim_free_rcu(struct rcu_head *rcu)
114 claim = container_of(rcu, struct claim, rcu);
116 batadv_backbone_gw_free_ref(claim->backbone_gw);
120 /* free a claim, call claim_free_rcu if its the last reference */
121 static void batadv_claim_free_ref(struct claim *claim)
123 if (atomic_dec_and_test(&claim->refcount))
124 call_rcu(&claim->rcu, batadv_claim_free_rcu);
127 /* @bat_priv: the bat priv with all the soft interface information
128 * @data: search data (may be local/static data)
130 * looks for a claim in the hash, and returns it if found
133 static struct claim *batadv_claim_hash_find(struct bat_priv *bat_priv,
136 struct batadv_hashtable *hash = bat_priv->claim_hash;
137 struct hlist_head *head;
138 struct hlist_node *node;
140 struct claim *claim_tmp = NULL;
146 index = batadv_choose_claim(data, hash->size);
147 head = &hash->table[index];
150 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
151 if (!batadv_compare_claim(&claim->hash_entry, data))
154 if (!atomic_inc_not_zero(&claim->refcount))
165 /* @bat_priv: the bat priv with all the soft interface information
166 * @addr: the address of the originator
169 * looks for a claim in the hash, and returns it if found
172 static struct backbone_gw *batadv_backbone_hash_find(struct bat_priv *bat_priv,
173 uint8_t *addr, short vid)
175 struct batadv_hashtable *hash = bat_priv->backbone_hash;
176 struct hlist_head *head;
177 struct hlist_node *node;
178 struct backbone_gw search_entry, *backbone_gw;
179 struct backbone_gw *backbone_gw_tmp = NULL;
185 memcpy(search_entry.orig, addr, ETH_ALEN);
186 search_entry.vid = vid;
188 index = batadv_choose_backbone_gw(&search_entry, hash->size);
189 head = &hash->table[index];
192 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
193 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
197 if (!atomic_inc_not_zero(&backbone_gw->refcount))
200 backbone_gw_tmp = backbone_gw;
205 return backbone_gw_tmp;
208 /* delete all claims for a backbone */
209 static void batadv_bla_del_backbone_claims(struct backbone_gw *backbone_gw)
211 struct batadv_hashtable *hash;
212 struct hlist_node *node, *node_tmp;
213 struct hlist_head *head;
216 spinlock_t *list_lock; /* protects write access to the hash lists */
218 hash = backbone_gw->bat_priv->claim_hash;
222 for (i = 0; i < hash->size; i++) {
223 head = &hash->table[i];
224 list_lock = &hash->list_locks[i];
226 spin_lock_bh(list_lock);
227 hlist_for_each_entry_safe(claim, node, node_tmp,
230 if (claim->backbone_gw != backbone_gw)
233 batadv_claim_free_ref(claim);
236 spin_unlock_bh(list_lock);
239 /* all claims gone, intialize CRC */
240 backbone_gw->crc = BATADV_BLA_CRC_INIT;
243 /* @bat_priv: the bat priv with all the soft interface information
244 * @orig: the mac address to be announced within the claim
246 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
248 * sends a claim frame according to the provided info.
250 static void batadv_bla_send_claim(struct bat_priv *bat_priv, uint8_t *mac,
251 short vid, int claimtype)
254 struct ethhdr *ethhdr;
255 struct hard_iface *primary_if;
256 struct net_device *soft_iface;
258 struct batadv_bla_claim_dst local_claim_dest;
261 primary_if = batadv_primary_if_get_selected(bat_priv);
265 memcpy(&local_claim_dest, &bat_priv->claim_dest,
266 sizeof(local_claim_dest));
267 local_claim_dest.type = claimtype;
269 soft_iface = primary_if->soft_iface;
271 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
272 /* IP DST: 0.0.0.0 */
274 primary_if->soft_iface,
275 /* IP SRC: 0.0.0.0 */
277 /* Ethernet DST: Broadcast */
279 /* Ethernet SRC/HW SRC: originator mac */
280 primary_if->net_dev->dev_addr,
281 /* HW DST: FF:43:05:XX:00:00
282 * with XX = claim type
283 * and YY:YY = group id
285 (uint8_t *)&local_claim_dest);
290 ethhdr = (struct ethhdr *)skb->data;
291 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
293 /* now we pretend that the client would have sent this ... */
295 case BATADV_CLAIM_TYPE_ADD:
296 /* normal claim frame
297 * set Ethernet SRC to the clients mac
299 memcpy(ethhdr->h_source, mac, ETH_ALEN);
300 batadv_dbg(BATADV_DBG_BLA, bat_priv,
301 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
303 case BATADV_CLAIM_TYPE_DEL:
305 * set HW SRC to the clients mac
307 memcpy(hw_src, mac, ETH_ALEN);
308 batadv_dbg(BATADV_DBG_BLA, bat_priv,
309 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
312 case BATADV_CLAIM_TYPE_ANNOUNCE:
313 /* announcement frame
314 * set HW SRC to the special mac containg the crc
316 memcpy(hw_src, mac, ETH_ALEN);
317 batadv_dbg(BATADV_DBG_BLA, bat_priv,
318 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
319 ethhdr->h_source, vid);
321 case BATADV_CLAIM_TYPE_REQUEST:
323 * set HW SRC to the special mac containg the crc
325 memcpy(hw_src, mac, ETH_ALEN);
326 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
327 batadv_dbg(BATADV_DBG_BLA, bat_priv,
328 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
329 ethhdr->h_source, ethhdr->h_dest, vid);
335 skb = vlan_insert_tag(skb, vid);
337 skb_reset_mac_header(skb);
338 skb->protocol = eth_type_trans(skb, soft_iface);
339 bat_priv->stats.rx_packets++;
340 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
341 soft_iface->last_rx = jiffies;
346 batadv_hardif_free_ref(primary_if);
349 /* @bat_priv: the bat priv with all the soft interface information
350 * @orig: the mac address of the originator
353 * searches for the backbone gw or creates a new one if it could not
356 static struct backbone_gw *batadv_bla_get_backbone_gw(struct bat_priv *bat_priv,
357 uint8_t *orig, short vid)
359 struct backbone_gw *entry;
360 struct orig_node *orig_node;
363 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
368 batadv_dbg(BATADV_DBG_BLA, bat_priv,
369 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
372 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
377 entry->lasttime = jiffies;
378 entry->crc = BATADV_BLA_CRC_INIT;
379 entry->bat_priv = bat_priv;
380 atomic_set(&entry->request_sent, 0);
381 memcpy(entry->orig, orig, ETH_ALEN);
383 /* one for the hash, one for returning */
384 atomic_set(&entry->refcount, 2);
386 hash_added = batadv_hash_add(bat_priv->backbone_hash,
387 batadv_compare_backbone_gw,
388 batadv_choose_backbone_gw, entry,
391 if (unlikely(hash_added != 0)) {
392 /* hash failed, free the structure */
397 /* this is a gateway now, remove any tt entries */
398 orig_node = batadv_orig_hash_find(bat_priv, orig);
400 batadv_tt_global_del_orig(bat_priv, orig_node,
401 "became a backbone gateway");
402 batadv_orig_node_free_ref(orig_node);
407 /* update or add the own backbone gw to make sure we announce
408 * where we receive other backbone gws
410 static void batadv_bla_update_own_backbone_gw(struct bat_priv *bat_priv,
411 struct hard_iface *primary_if,
414 struct backbone_gw *backbone_gw;
416 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
417 primary_if->net_dev->dev_addr,
419 if (unlikely(!backbone_gw))
422 backbone_gw->lasttime = jiffies;
423 batadv_backbone_gw_free_ref(backbone_gw);
426 /* @bat_priv: the bat priv with all the soft interface information
427 * @vid: the vid where the request came on
429 * Repeat all of our own claims, and finally send an ANNOUNCE frame
430 * to allow the requester another check if the CRC is correct now.
432 static void batadv_bla_answer_request(struct bat_priv *bat_priv,
433 struct hard_iface *primary_if, short vid)
435 struct hlist_node *node;
436 struct hlist_head *head;
437 struct batadv_hashtable *hash;
439 struct backbone_gw *backbone_gw;
442 batadv_dbg(BATADV_DBG_BLA, bat_priv,
443 "bla_answer_request(): received a claim request, send all of our own claims again\n");
445 backbone_gw = batadv_backbone_hash_find(bat_priv,
446 primary_if->net_dev->dev_addr,
451 hash = bat_priv->claim_hash;
452 for (i = 0; i < hash->size; i++) {
453 head = &hash->table[i];
456 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
457 /* only own claims are interesting */
458 if (claim->backbone_gw != backbone_gw)
461 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
462 BATADV_CLAIM_TYPE_ADD);
467 /* finally, send an announcement frame */
468 batadv_bla_send_announce(bat_priv, backbone_gw);
469 batadv_backbone_gw_free_ref(backbone_gw);
472 /* @backbone_gw: the backbone gateway from whom we are out of sync
474 * When the crc is wrong, ask the backbone gateway for a full table update.
475 * After the request, it will repeat all of his own claims and finally
476 * send an announcement claim with which we can check again.
478 static void batadv_bla_send_request(struct backbone_gw *backbone_gw)
480 /* first, remove all old entries */
481 batadv_bla_del_backbone_claims(backbone_gw);
483 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
484 "Sending REQUEST to %pM\n", backbone_gw->orig);
487 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
488 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
490 /* no local broadcasts should be sent or received, for now. */
491 if (!atomic_read(&backbone_gw->request_sent)) {
492 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
493 atomic_set(&backbone_gw->request_sent, 1);
497 /* @bat_priv: the bat priv with all the soft interface information
498 * @backbone_gw: our backbone gateway which should be announced
500 * This function sends an announcement. It is called from multiple
503 static void batadv_bla_send_announce(struct bat_priv *bat_priv,
504 struct backbone_gw *backbone_gw)
506 uint8_t mac[ETH_ALEN];
509 memcpy(mac, batadv_announce_mac, 4);
510 crc = htons(backbone_gw->crc);
511 memcpy(&mac[4], &crc, 2);
513 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
514 BATADV_CLAIM_TYPE_ANNOUNCE);
518 /* @bat_priv: the bat priv with all the soft interface information
519 * @mac: the mac address of the claim
520 * @vid: the VLAN ID of the frame
521 * @backbone_gw: the backbone gateway which claims it
523 * Adds a claim in the claim hash.
525 static void batadv_bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
527 struct backbone_gw *backbone_gw)
530 struct claim search_claim;
533 memcpy(search_claim.addr, mac, ETH_ALEN);
534 search_claim.vid = vid;
535 claim = batadv_claim_hash_find(bat_priv, &search_claim);
537 /* create a new claim entry if it does not exist yet. */
539 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
543 memcpy(claim->addr, mac, ETH_ALEN);
545 claim->lasttime = jiffies;
546 claim->backbone_gw = backbone_gw;
548 atomic_set(&claim->refcount, 2);
549 batadv_dbg(BATADV_DBG_BLA, bat_priv,
550 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
552 hash_added = batadv_hash_add(bat_priv->claim_hash,
553 batadv_compare_claim,
554 batadv_choose_claim, claim,
557 if (unlikely(hash_added != 0)) {
558 /* only local changes happened. */
563 claim->lasttime = jiffies;
564 if (claim->backbone_gw == backbone_gw)
565 /* no need to register a new backbone */
568 batadv_dbg(BATADV_DBG_BLA, bat_priv,
569 "bla_add_claim(): changing ownership for %pM, vid %d\n",
572 claim->backbone_gw->crc ^=
573 crc16(0, claim->addr, ETH_ALEN);
574 batadv_backbone_gw_free_ref(claim->backbone_gw);
577 /* set (new) backbone gw */
578 atomic_inc(&backbone_gw->refcount);
579 claim->backbone_gw = backbone_gw;
581 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
582 backbone_gw->lasttime = jiffies;
585 batadv_claim_free_ref(claim);
588 /* Delete a claim from the claim hash which has the
589 * given mac address and vid.
591 static void batadv_bla_del_claim(struct bat_priv *bat_priv, const uint8_t *mac,
594 struct claim search_claim, *claim;
596 memcpy(search_claim.addr, mac, ETH_ALEN);
597 search_claim.vid = vid;
598 claim = batadv_claim_hash_find(bat_priv, &search_claim);
602 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
605 batadv_hash_remove(bat_priv->claim_hash, batadv_compare_claim,
606 batadv_choose_claim, claim);
607 batadv_claim_free_ref(claim); /* reference from the hash is gone */
609 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
611 /* don't need the reference from hash_find() anymore */
612 batadv_claim_free_ref(claim);
615 /* check for ANNOUNCE frame, return 1 if handled */
616 static int batadv_handle_announce(struct bat_priv *bat_priv,
617 uint8_t *an_addr, uint8_t *backbone_addr,
620 struct backbone_gw *backbone_gw;
623 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
626 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
628 if (unlikely(!backbone_gw))
632 /* handle as ANNOUNCE frame */
633 backbone_gw->lasttime = jiffies;
634 crc = ntohs(*((__be16 *)(&an_addr[4])));
636 batadv_dbg(BATADV_DBG_BLA, bat_priv,
637 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
638 vid, backbone_gw->orig, crc);
640 if (backbone_gw->crc != crc) {
641 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
642 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
643 backbone_gw->orig, backbone_gw->vid,
644 backbone_gw->crc, crc);
646 batadv_bla_send_request(backbone_gw);
648 /* if we have sent a request and the crc was OK,
649 * we can allow traffic again.
651 if (atomic_read(&backbone_gw->request_sent)) {
652 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
653 atomic_set(&backbone_gw->request_sent, 0);
657 batadv_backbone_gw_free_ref(backbone_gw);
661 /* check for REQUEST frame, return 1 if handled */
662 static int batadv_handle_request(struct bat_priv *bat_priv,
663 struct hard_iface *primary_if,
664 uint8_t *backbone_addr,
665 struct ethhdr *ethhdr, short vid)
667 /* check for REQUEST frame */
668 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
671 /* sanity check, this should not happen on a normal switch,
672 * we ignore it in this case.
674 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
677 batadv_dbg(BATADV_DBG_BLA, bat_priv,
678 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
679 vid, ethhdr->h_source);
681 batadv_bla_answer_request(bat_priv, primary_if, vid);
685 /* check for UNCLAIM frame, return 1 if handled */
686 static int batadv_handle_unclaim(struct bat_priv *bat_priv,
687 struct hard_iface *primary_if,
688 uint8_t *backbone_addr,
689 uint8_t *claim_addr, short vid)
691 struct backbone_gw *backbone_gw;
693 /* unclaim in any case if it is our own */
694 if (primary_if && batadv_compare_eth(backbone_addr,
695 primary_if->net_dev->dev_addr))
696 batadv_bla_send_claim(bat_priv, claim_addr, vid,
697 BATADV_CLAIM_TYPE_DEL);
699 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
704 /* this must be an UNCLAIM frame */
705 batadv_dbg(BATADV_DBG_BLA, bat_priv,
706 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
707 claim_addr, vid, backbone_gw->orig);
709 batadv_bla_del_claim(bat_priv, claim_addr, vid);
710 batadv_backbone_gw_free_ref(backbone_gw);
714 /* check for CLAIM frame, return 1 if handled */
715 static int batadv_handle_claim(struct bat_priv *bat_priv,
716 struct hard_iface *primary_if,
717 uint8_t *backbone_addr, uint8_t *claim_addr,
720 struct backbone_gw *backbone_gw;
722 /* register the gateway if not yet available, and add the claim. */
724 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
726 if (unlikely(!backbone_gw))
729 /* this must be a CLAIM frame */
730 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
731 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
732 batadv_bla_send_claim(bat_priv, claim_addr, vid,
733 BATADV_CLAIM_TYPE_ADD);
735 /* TODO: we could call something like tt_local_del() here. */
737 batadv_backbone_gw_free_ref(backbone_gw);
741 /* @bat_priv: the bat priv with all the soft interface information
742 * @hw_src: the Hardware source in the ARP Header
743 * @hw_dst: the Hardware destination in the ARP Header
744 * @ethhdr: pointer to the Ethernet header of the claim frame
746 * checks if it is a claim packet and if its on the same group.
747 * This function also applies the group ID of the sender
748 * if it is in the same mesh.
751 * 2 - if it is a claim packet and on the same group
752 * 1 - if is a claim packet from another group
753 * 0 - if it is not a claim packet
755 static int batadv_check_claim_group(struct bat_priv *bat_priv,
756 struct hard_iface *primary_if,
757 uint8_t *hw_src, uint8_t *hw_dst,
758 struct ethhdr *ethhdr)
760 uint8_t *backbone_addr;
761 struct orig_node *orig_node;
762 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
764 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
765 bla_dst_own = &bat_priv->claim_dest;
767 /* check if it is a claim packet in general */
768 if (memcmp(bla_dst->magic, bla_dst_own->magic,
769 sizeof(bla_dst->magic)) != 0)
772 /* if announcement packet, use the source,
773 * otherwise assume it is in the hw_src
775 switch (bla_dst->type) {
776 case BATADV_CLAIM_TYPE_ADD:
777 backbone_addr = hw_src;
779 case BATADV_CLAIM_TYPE_REQUEST:
780 case BATADV_CLAIM_TYPE_ANNOUNCE:
781 case BATADV_CLAIM_TYPE_DEL:
782 backbone_addr = ethhdr->h_source;
788 /* don't accept claim frames from ourselves */
789 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
792 /* if its already the same group, it is fine. */
793 if (bla_dst->group == bla_dst_own->group)
796 /* lets see if this originator is in our mesh */
797 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
799 /* dont accept claims from gateways which are not in
800 * the same mesh or group.
805 /* if our mesh friends mac is bigger, use it for ourselves. */
806 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
807 batadv_dbg(BATADV_DBG_BLA, bat_priv,
808 "taking other backbones claim group: %04x\n",
809 ntohs(bla_dst->group));
810 bla_dst_own->group = bla_dst->group;
813 batadv_orig_node_free_ref(orig_node);
819 /* @bat_priv: the bat priv with all the soft interface information
820 * @skb: the frame to be checked
822 * Check if this is a claim frame, and process it accordingly.
824 * returns 1 if it was a claim frame, otherwise return 0 to
825 * tell the callee that it can use the frame on its own.
827 static int batadv_bla_process_claim(struct bat_priv *bat_priv,
828 struct hard_iface *primary_if,
831 struct ethhdr *ethhdr;
832 struct vlan_ethhdr *vhdr;
833 struct arphdr *arphdr;
834 uint8_t *hw_src, *hw_dst;
835 struct batadv_bla_claim_dst *bla_dst;
841 ethhdr = (struct ethhdr *)skb_mac_header(skb);
843 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
844 vhdr = (struct vlan_ethhdr *)ethhdr;
845 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
846 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
847 headlen = sizeof(*vhdr);
849 proto = ntohs(ethhdr->h_proto);
853 if (proto != ETH_P_ARP)
854 return 0; /* not a claim frame */
856 /* this must be a ARP frame. check if it is a claim. */
858 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
861 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
862 ethhdr = (struct ethhdr *)skb_mac_header(skb);
863 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
865 /* Check whether the ARP frame carries a valid
868 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
870 if (arphdr->ar_pro != htons(ETH_P_IP))
872 if (arphdr->ar_hln != ETH_ALEN)
874 if (arphdr->ar_pln != 4)
877 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
878 hw_dst = hw_src + ETH_ALEN + 4;
879 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
881 /* check if it is a claim frame. */
882 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
885 batadv_dbg(BATADV_DBG_BLA, bat_priv,
886 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
887 ethhdr->h_source, vid, hw_src, hw_dst);
892 /* become a backbone gw ourselves on this vlan if not happened yet */
893 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
895 /* check for the different types of claim frames ... */
896 switch (bla_dst->type) {
897 case BATADV_CLAIM_TYPE_ADD:
898 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
899 ethhdr->h_source, vid))
902 case BATADV_CLAIM_TYPE_DEL:
903 if (batadv_handle_unclaim(bat_priv, primary_if,
904 ethhdr->h_source, hw_src, vid))
908 case BATADV_CLAIM_TYPE_ANNOUNCE:
909 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
913 case BATADV_CLAIM_TYPE_REQUEST:
914 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
920 batadv_dbg(BATADV_DBG_BLA, bat_priv,
921 "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",
922 ethhdr->h_source, vid, hw_src, hw_dst);
926 /* Check when we last heard from other nodes, and remove them in case of
927 * a time out, or clean all backbone gws if now is set.
929 static void batadv_bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
931 struct backbone_gw *backbone_gw;
932 struct hlist_node *node, *node_tmp;
933 struct hlist_head *head;
934 struct batadv_hashtable *hash;
935 spinlock_t *list_lock; /* protects write access to the hash lists */
938 hash = bat_priv->backbone_hash;
942 for (i = 0; i < hash->size; i++) {
943 head = &hash->table[i];
944 list_lock = &hash->list_locks[i];
946 spin_lock_bh(list_lock);
947 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
951 if (!batadv_has_timed_out(backbone_gw->lasttime,
952 BATADV_BLA_BACKBONE_TIMEOUT))
955 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
956 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
960 /* don't wait for the pending request anymore */
961 if (atomic_read(&backbone_gw->request_sent))
962 atomic_dec(&bat_priv->bla_num_requests);
964 batadv_bla_del_backbone_claims(backbone_gw);
967 batadv_backbone_gw_free_ref(backbone_gw);
969 spin_unlock_bh(list_lock);
973 /* @bat_priv: the bat priv with all the soft interface information
974 * @primary_if: the selected primary interface, may be NULL if now is set
975 * @now: whether the whole hash shall be wiped now
977 * Check when we heard last time from our own claims, and remove them in case of
978 * a time out, or clean all claims if now is set
980 static void batadv_bla_purge_claims(struct bat_priv *bat_priv,
981 struct hard_iface *primary_if, int now)
984 struct hlist_node *node;
985 struct hlist_head *head;
986 struct batadv_hashtable *hash;
989 hash = bat_priv->claim_hash;
993 for (i = 0; i < hash->size; i++) {
994 head = &hash->table[i];
997 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1000 if (!batadv_compare_eth(claim->backbone_gw->orig,
1001 primary_if->net_dev->dev_addr))
1003 if (!batadv_has_timed_out(claim->lasttime,
1004 BATADV_BLA_CLAIM_TIMEOUT))
1007 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1008 "bla_purge_claims(): %pM, vid %d, time out\n",
1009 claim->addr, claim->vid);
1012 batadv_handle_unclaim(bat_priv, primary_if,
1013 claim->backbone_gw->orig,
1014 claim->addr, claim->vid);
1020 /* @bat_priv: the bat priv with all the soft interface information
1021 * @primary_if: the new selected primary_if
1022 * @oldif: the old primary interface, may be NULL
1024 * Update the backbone gateways when the own orig address changes.
1026 void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1027 struct hard_iface *primary_if,
1028 struct hard_iface *oldif)
1030 struct backbone_gw *backbone_gw;
1031 struct hlist_node *node;
1032 struct hlist_head *head;
1033 struct batadv_hashtable *hash;
1036 /* reset bridge loop avoidance group id */
1037 bat_priv->claim_dest.group =
1038 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1041 batadv_bla_purge_claims(bat_priv, NULL, 1);
1042 batadv_bla_purge_backbone_gw(bat_priv, 1);
1046 hash = bat_priv->backbone_hash;
1050 for (i = 0; i < hash->size; i++) {
1051 head = &hash->table[i];
1054 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1055 /* own orig still holds the old value. */
1056 if (!batadv_compare_eth(backbone_gw->orig,
1057 oldif->net_dev->dev_addr))
1060 memcpy(backbone_gw->orig,
1061 primary_if->net_dev->dev_addr, ETH_ALEN);
1062 /* send an announce frame so others will ask for our
1063 * claims and update their tables.
1065 batadv_bla_send_announce(bat_priv, backbone_gw);
1073 /* (re)start the timer */
1074 static void batadv_bla_start_timer(struct bat_priv *bat_priv)
1076 INIT_DELAYED_WORK(&bat_priv->bla_work, batadv_bla_periodic_work);
1077 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
1078 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1081 /* periodic work to do:
1082 * * purge structures when they are too old
1083 * * send announcements
1085 static void batadv_bla_periodic_work(struct work_struct *work)
1087 struct delayed_work *delayed_work =
1088 container_of(work, struct delayed_work, work);
1089 struct bat_priv *bat_priv =
1090 container_of(delayed_work, struct bat_priv, bla_work);
1091 struct hlist_node *node;
1092 struct hlist_head *head;
1093 struct backbone_gw *backbone_gw;
1094 struct batadv_hashtable *hash;
1095 struct hard_iface *primary_if;
1098 primary_if = batadv_primary_if_get_selected(bat_priv);
1102 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1103 batadv_bla_purge_backbone_gw(bat_priv, 0);
1105 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1108 hash = bat_priv->backbone_hash;
1112 for (i = 0; i < hash->size; i++) {
1113 head = &hash->table[i];
1116 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1117 if (!batadv_compare_eth(backbone_gw->orig,
1118 primary_if->net_dev->dev_addr))
1121 backbone_gw->lasttime = jiffies;
1123 batadv_bla_send_announce(bat_priv, backbone_gw);
1129 batadv_hardif_free_ref(primary_if);
1131 batadv_bla_start_timer(bat_priv);
1134 /* The hash for claim and backbone hash receive the same key because they
1135 * are getting initialized by hash_new with the same key. Reinitializing
1136 * them with to different keys to allow nested locking without generating
1139 static struct lock_class_key batadv_claim_hash_lock_class_key;
1140 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1142 /* initialize all bla structures */
1143 int batadv_bla_init(struct bat_priv *bat_priv)
1146 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1147 struct hard_iface *primary_if;
1149 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1151 /* setting claim destination address */
1152 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1153 bat_priv->claim_dest.type = 0;
1154 primary_if = batadv_primary_if_get_selected(bat_priv);
1156 bat_priv->claim_dest.group =
1157 htons(crc16(0, primary_if->net_dev->dev_addr,
1159 batadv_hardif_free_ref(primary_if);
1161 bat_priv->claim_dest.group = 0; /* will be set later */
1164 /* initialize the duplicate list */
1165 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1166 bat_priv->bcast_duplist[i].entrytime =
1167 jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1168 bat_priv->bcast_duplist_curr = 0;
1170 if (bat_priv->claim_hash)
1173 bat_priv->claim_hash = batadv_hash_new(128);
1174 bat_priv->backbone_hash = batadv_hash_new(32);
1176 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
1179 batadv_hash_set_lock_class(bat_priv->claim_hash,
1180 &batadv_claim_hash_lock_class_key);
1181 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1182 &batadv_backbone_hash_lock_class_key);
1184 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1186 batadv_bla_start_timer(bat_priv);
1190 /* @bat_priv: the bat priv with all the soft interface information
1191 * @bcast_packet: originator mac address
1192 * @hdr_size: maximum length of the frame
1194 * check if it is on our broadcast list. Another gateway might
1195 * have sent the same packet because it is connected to the same backbone,
1196 * so we have to remove this duplicate.
1198 * This is performed by checking the CRC, which will tell us
1199 * with a good chance that it is the same packet. If it is furthermore
1200 * sent by another host, drop it. We allow equal packets from
1201 * the same host however as this might be intended.
1203 int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1204 struct batadv_bcast_packet *bcast_packet,
1207 int i, length, curr;
1210 struct bcast_duplist_entry *entry;
1212 length = hdr_size - sizeof(*bcast_packet);
1213 content = (uint8_t *)bcast_packet;
1214 content += sizeof(*bcast_packet);
1216 /* calculate the crc ... */
1217 crc = crc16(0, content, length);
1219 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1220 curr = (bat_priv->bcast_duplist_curr + i) % BATADV_DUPLIST_SIZE;
1221 entry = &bat_priv->bcast_duplist[curr];
1223 /* we can stop searching if the entry is too old ;
1224 * later entries will be even older
1226 if (batadv_has_timed_out(entry->entrytime,
1227 BATADV_DUPLIST_TIMEOUT))
1230 if (entry->crc != crc)
1233 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1236 /* this entry seems to match: same crc, not too old,
1237 * and from another gw. therefore return 1 to forbid it.
1241 /* not found, add a new entry (overwrite the oldest entry) */
1242 curr = (bat_priv->bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1243 curr %= BATADV_DUPLIST_SIZE;
1244 entry = &bat_priv->bcast_duplist[curr];
1246 entry->entrytime = jiffies;
1247 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1248 bat_priv->bcast_duplist_curr = curr;
1250 /* allow it, its the first occurence. */
1256 /* @bat_priv: the bat priv with all the soft interface information
1257 * @orig: originator mac address
1259 * check if the originator is a gateway for any VLAN ID.
1261 * returns 1 if it is found, 0 otherwise
1263 int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
1265 struct batadv_hashtable *hash = bat_priv->backbone_hash;
1266 struct hlist_head *head;
1267 struct hlist_node *node;
1268 struct backbone_gw *backbone_gw;
1271 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1277 for (i = 0; i < hash->size; i++) {
1278 head = &hash->table[i];
1281 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1282 if (batadv_compare_eth(backbone_gw->orig, orig)) {
1294 /* @skb: the frame to be checked
1295 * @orig_node: the orig_node of the frame
1296 * @hdr_size: maximum length of the frame
1298 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1299 * if the orig_node is also a gateway on the soft interface, otherwise it
1302 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1303 struct orig_node *orig_node, int hdr_size)
1305 struct ethhdr *ethhdr;
1306 struct vlan_ethhdr *vhdr;
1307 struct backbone_gw *backbone_gw;
1310 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1313 /* first, find out the vid. */
1314 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1317 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1319 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1320 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1323 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1325 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1328 /* see if this originator is a backbone gw for this VLAN */
1329 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1330 orig_node->orig, vid);
1334 batadv_backbone_gw_free_ref(backbone_gw);
1338 /* free all bla structures (for softinterface free or module unload) */
1339 void batadv_bla_free(struct bat_priv *bat_priv)
1341 struct hard_iface *primary_if;
1343 cancel_delayed_work_sync(&bat_priv->bla_work);
1344 primary_if = batadv_primary_if_get_selected(bat_priv);
1346 if (bat_priv->claim_hash) {
1347 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1348 batadv_hash_destroy(bat_priv->claim_hash);
1349 bat_priv->claim_hash = NULL;
1351 if (bat_priv->backbone_hash) {
1352 batadv_bla_purge_backbone_gw(bat_priv, 1);
1353 batadv_hash_destroy(bat_priv->backbone_hash);
1354 bat_priv->backbone_hash = NULL;
1357 batadv_hardif_free_ref(primary_if);
1360 /* @bat_priv: the bat priv with all the soft interface information
1361 * @skb: the frame to be checked
1362 * @vid: the VLAN ID of the frame
1364 * bla_rx avoidance checks if:
1365 * * we have to race for a claim
1366 * * if the frame is allowed on the LAN
1368 * in these cases, the skb is further handled by this function and
1369 * returns 1, otherwise it returns 0 and the caller shall further
1372 int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1374 struct ethhdr *ethhdr;
1375 struct claim search_claim, *claim = NULL;
1376 struct hard_iface *primary_if;
1379 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1381 primary_if = batadv_primary_if_get_selected(bat_priv);
1385 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1389 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1390 /* don't allow broadcasts while requests are in flight */
1391 if (is_multicast_ether_addr(ethhdr->h_dest))
1394 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1395 search_claim.vid = vid;
1396 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1399 /* possible optimization: race for a claim */
1400 /* No claim exists yet, claim it for us!
1402 batadv_handle_claim(bat_priv, primary_if,
1403 primary_if->net_dev->dev_addr,
1404 ethhdr->h_source, vid);
1408 /* if it is our own claim ... */
1409 if (batadv_compare_eth(claim->backbone_gw->orig,
1410 primary_if->net_dev->dev_addr)) {
1411 /* ... allow it in any case */
1412 claim->lasttime = jiffies;
1416 /* if it is a broadcast ... */
1417 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1418 /* ... drop it. the responsible gateway is in charge. */
1421 /* seems the client considers us as its best gateway.
1422 * send a claim and update the claim table
1425 batadv_handle_claim(bat_priv, primary_if,
1426 primary_if->net_dev->dev_addr,
1427 ethhdr->h_source, vid);
1431 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1441 batadv_hardif_free_ref(primary_if);
1443 batadv_claim_free_ref(claim);
1447 /* @bat_priv: the bat priv with all the soft interface information
1448 * @skb: the frame to be checked
1449 * @vid: the VLAN ID of the frame
1452 * * a claim was received which has to be processed
1453 * * the frame is allowed on the mesh
1455 * in these cases, the skb is further handled by this function and
1456 * returns 1, otherwise it returns 0 and the caller shall further
1459 int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1461 struct ethhdr *ethhdr;
1462 struct claim search_claim, *claim = NULL;
1463 struct hard_iface *primary_if;
1466 primary_if = batadv_primary_if_get_selected(bat_priv);
1470 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1473 /* in VLAN case, the mac header might not be set. */
1474 skb_reset_mac_header(skb);
1476 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1479 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1481 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1482 /* don't allow broadcasts while requests are in flight */
1483 if (is_multicast_ether_addr(ethhdr->h_dest))
1486 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1487 search_claim.vid = vid;
1489 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1491 /* if no claim exists, allow it. */
1495 /* check if we are responsible. */
1496 if (batadv_compare_eth(claim->backbone_gw->orig,
1497 primary_if->net_dev->dev_addr)) {
1498 /* if yes, the client has roamed and we have
1501 batadv_handle_unclaim(bat_priv, primary_if,
1502 primary_if->net_dev->dev_addr,
1503 ethhdr->h_source, vid);
1507 /* check if it is a multicast/broadcast frame */
1508 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1509 /* drop it. the responsible gateway has forwarded it into
1510 * the backbone network.
1514 /* we must allow it. at least if we are
1515 * responsible for the DESTINATION.
1520 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1527 batadv_hardif_free_ref(primary_if);
1529 batadv_claim_free_ref(claim);
1533 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1535 struct net_device *net_dev = (struct net_device *)seq->private;
1536 struct bat_priv *bat_priv = netdev_priv(net_dev);
1537 struct batadv_hashtable *hash = bat_priv->claim_hash;
1538 struct claim *claim;
1539 struct hard_iface *primary_if;
1540 struct hlist_node *node;
1541 struct hlist_head *head;
1545 uint8_t *primary_addr;
1547 primary_if = batadv_primary_if_get_selected(bat_priv);
1549 ret = seq_printf(seq,
1550 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1555 if (primary_if->if_status != BATADV_IF_ACTIVE) {
1556 ret = seq_printf(seq,
1557 "BATMAN mesh %s disabled - primary interface not active\n",
1562 primary_addr = primary_if->net_dev->dev_addr;
1564 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1565 net_dev->name, primary_addr,
1566 ntohs(bat_priv->claim_dest.group));
1567 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1568 "Client", "VID", "Originator", "CRC");
1569 for (i = 0; i < hash->size; i++) {
1570 head = &hash->table[i];
1573 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1574 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1576 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1577 claim->addr, claim->vid,
1578 claim->backbone_gw->orig,
1579 (is_own ? 'x' : ' '),
1580 claim->backbone_gw->crc);
1586 batadv_hardif_free_ref(primary_if);