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 = hash_add(bat_priv->backbone_hash, compare_backbone_gw,
383 choose_backbone_gw, entry, &entry->hash_entry);
385 if (unlikely(hash_added != 0)) {
386 /* hash failed, free the structure */
391 /* this is a gateway now, remove any tt entries */
392 orig_node = orig_hash_find(bat_priv, orig);
394 batadv_tt_global_del_orig(bat_priv, orig_node,
395 "became a backbone gateway");
396 batadv_orig_node_free_ref(orig_node);
401 /* update or add the own backbone gw to make sure we announce
402 * where we receive other backbone gws
404 static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
405 struct hard_iface *primary_if,
408 struct backbone_gw *backbone_gw;
410 backbone_gw = bla_get_backbone_gw(bat_priv,
411 primary_if->net_dev->dev_addr, vid);
412 if (unlikely(!backbone_gw))
415 backbone_gw->lasttime = jiffies;
416 backbone_gw_free_ref(backbone_gw);
419 /* @bat_priv: the bat priv with all the soft interface information
420 * @vid: the vid where the request came on
422 * Repeat all of our own claims, and finally send an ANNOUNCE frame
423 * to allow the requester another check if the CRC is correct now.
425 static void bla_answer_request(struct bat_priv *bat_priv,
426 struct hard_iface *primary_if, short vid)
428 struct hlist_node *node;
429 struct hlist_head *head;
430 struct hashtable_t *hash;
432 struct backbone_gw *backbone_gw;
435 bat_dbg(DBG_BLA, bat_priv,
436 "bla_answer_request(): received a claim request, send all of our own claims again\n");
438 backbone_gw = backbone_hash_find(bat_priv,
439 primary_if->net_dev->dev_addr, vid);
443 hash = bat_priv->claim_hash;
444 for (i = 0; i < hash->size; i++) {
445 head = &hash->table[i];
448 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
449 /* only own claims are interesting */
450 if (claim->backbone_gw != backbone_gw)
453 bla_send_claim(bat_priv, claim->addr, claim->vid,
459 /* finally, send an announcement frame */
460 bla_send_announce(bat_priv, backbone_gw);
461 backbone_gw_free_ref(backbone_gw);
464 /* @backbone_gw: the backbone gateway from whom we are out of sync
466 * When the crc is wrong, ask the backbone gateway for a full table update.
467 * After the request, it will repeat all of his own claims and finally
468 * send an announcement claim with which we can check again.
470 static void bla_send_request(struct backbone_gw *backbone_gw)
472 /* first, remove all old entries */
473 bla_del_backbone_claims(backbone_gw);
475 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
476 "Sending REQUEST to %pM\n",
480 bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
481 backbone_gw->vid, CLAIM_TYPE_REQUEST);
483 /* no local broadcasts should be sent or received, for now. */
484 if (!atomic_read(&backbone_gw->request_sent)) {
485 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
486 atomic_set(&backbone_gw->request_sent, 1);
490 /* @bat_priv: the bat priv with all the soft interface information
491 * @backbone_gw: our backbone gateway which should be announced
493 * This function sends an announcement. It is called from multiple
496 static void bla_send_announce(struct bat_priv *bat_priv,
497 struct backbone_gw *backbone_gw)
499 uint8_t mac[ETH_ALEN];
502 memcpy(mac, announce_mac, 4);
503 crc = htons(backbone_gw->crc);
504 memcpy(&mac[4], &crc, 2);
506 bla_send_claim(bat_priv, mac, backbone_gw->vid, CLAIM_TYPE_ANNOUNCE);
510 /* @bat_priv: the bat priv with all the soft interface information
511 * @mac: the mac address of the claim
512 * @vid: the VLAN ID of the frame
513 * @backbone_gw: the backbone gateway which claims it
515 * Adds a claim in the claim hash.
517 static void bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
518 const short vid, struct backbone_gw *backbone_gw)
521 struct claim search_claim;
524 memcpy(search_claim.addr, mac, ETH_ALEN);
525 search_claim.vid = vid;
526 claim = claim_hash_find(bat_priv, &search_claim);
528 /* create a new claim entry if it does not exist yet. */
530 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
534 memcpy(claim->addr, mac, ETH_ALEN);
536 claim->lasttime = jiffies;
537 claim->backbone_gw = backbone_gw;
539 atomic_set(&claim->refcount, 2);
540 bat_dbg(DBG_BLA, bat_priv,
541 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
543 hash_added = hash_add(bat_priv->claim_hash, compare_claim,
544 choose_claim, claim, &claim->hash_entry);
546 if (unlikely(hash_added != 0)) {
547 /* only local changes happened. */
552 claim->lasttime = jiffies;
553 if (claim->backbone_gw == backbone_gw)
554 /* no need to register a new backbone */
557 bat_dbg(DBG_BLA, bat_priv,
558 "bla_add_claim(): changing ownership for %pM, vid %d\n",
561 claim->backbone_gw->crc ^=
562 crc16(0, claim->addr, ETH_ALEN);
563 backbone_gw_free_ref(claim->backbone_gw);
566 /* set (new) backbone gw */
567 atomic_inc(&backbone_gw->refcount);
568 claim->backbone_gw = backbone_gw;
570 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
571 backbone_gw->lasttime = jiffies;
574 claim_free_ref(claim);
577 /* Delete a claim from the claim hash which has the
578 * given mac address and vid.
580 static void bla_del_claim(struct bat_priv *bat_priv, const uint8_t *mac,
583 struct claim search_claim, *claim;
585 memcpy(search_claim.addr, mac, ETH_ALEN);
586 search_claim.vid = vid;
587 claim = claim_hash_find(bat_priv, &search_claim);
591 bat_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac, vid);
593 hash_remove(bat_priv->claim_hash, compare_claim, choose_claim, claim);
594 claim_free_ref(claim); /* reference from the hash is gone */
596 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
598 /* don't need the reference from hash_find() anymore */
599 claim_free_ref(claim);
602 /* check for ANNOUNCE frame, return 1 if handled */
603 static int handle_announce(struct bat_priv *bat_priv,
604 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
606 struct backbone_gw *backbone_gw;
609 if (memcmp(an_addr, announce_mac, 4) != 0)
612 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
614 if (unlikely(!backbone_gw))
618 /* handle as ANNOUNCE frame */
619 backbone_gw->lasttime = jiffies;
620 crc = ntohs(*((__be16 *)(&an_addr[4])));
622 bat_dbg(DBG_BLA, bat_priv,
623 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
624 vid, backbone_gw->orig, crc);
626 if (backbone_gw->crc != crc) {
627 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
628 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
629 backbone_gw->orig, backbone_gw->vid, backbone_gw->crc,
632 bla_send_request(backbone_gw);
634 /* if we have sent a request and the crc was OK,
635 * we can allow traffic again.
637 if (atomic_read(&backbone_gw->request_sent)) {
638 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
639 atomic_set(&backbone_gw->request_sent, 0);
643 backbone_gw_free_ref(backbone_gw);
647 /* check for REQUEST frame, return 1 if handled */
648 static int handle_request(struct bat_priv *bat_priv,
649 struct hard_iface *primary_if,
650 uint8_t *backbone_addr,
651 struct ethhdr *ethhdr, short vid)
653 /* check for REQUEST frame */
654 if (!compare_eth(backbone_addr, ethhdr->h_dest))
657 /* sanity check, this should not happen on a normal switch,
658 * we ignore it in this case.
660 if (!compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
663 bat_dbg(DBG_BLA, bat_priv,
664 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
665 vid, ethhdr->h_source);
667 bla_answer_request(bat_priv, primary_if, vid);
671 /* check for UNCLAIM frame, return 1 if handled */
672 static int handle_unclaim(struct bat_priv *bat_priv,
673 struct hard_iface *primary_if,
674 uint8_t *backbone_addr,
675 uint8_t *claim_addr, short vid)
677 struct backbone_gw *backbone_gw;
679 /* unclaim in any case if it is our own */
680 if (primary_if && compare_eth(backbone_addr,
681 primary_if->net_dev->dev_addr))
682 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
684 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
689 /* this must be an UNCLAIM frame */
690 bat_dbg(DBG_BLA, bat_priv,
691 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
692 claim_addr, vid, backbone_gw->orig);
694 bla_del_claim(bat_priv, claim_addr, vid);
695 backbone_gw_free_ref(backbone_gw);
699 /* check for CLAIM frame, return 1 if handled */
700 static int handle_claim(struct bat_priv *bat_priv,
701 struct hard_iface *primary_if, uint8_t *backbone_addr,
702 uint8_t *claim_addr, short vid)
704 struct backbone_gw *backbone_gw;
706 /* register the gateway if not yet available, and add the claim. */
708 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
710 if (unlikely(!backbone_gw))
713 /* this must be a CLAIM frame */
714 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
715 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
716 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
718 /* TODO: we could call something like tt_local_del() here. */
720 backbone_gw_free_ref(backbone_gw);
724 /* @bat_priv: the bat priv with all the soft interface information
725 * @hw_src: the Hardware source in the ARP Header
726 * @hw_dst: the Hardware destination in the ARP Header
727 * @ethhdr: pointer to the Ethernet header of the claim frame
729 * checks if it is a claim packet and if its on the same group.
730 * This function also applies the group ID of the sender
731 * if it is in the same mesh.
734 * 2 - if it is a claim packet and on the same group
735 * 1 - if is a claim packet from another group
736 * 0 - if it is not a claim packet
738 static int check_claim_group(struct bat_priv *bat_priv,
739 struct hard_iface *primary_if,
740 uint8_t *hw_src, uint8_t *hw_dst,
741 struct ethhdr *ethhdr)
743 uint8_t *backbone_addr;
744 struct orig_node *orig_node;
745 struct bla_claim_dst *bla_dst, *bla_dst_own;
747 bla_dst = (struct bla_claim_dst *)hw_dst;
748 bla_dst_own = &bat_priv->claim_dest;
750 /* check if it is a claim packet in general */
751 if (memcmp(bla_dst->magic, bla_dst_own->magic,
752 sizeof(bla_dst->magic)) != 0)
755 /* if announcement packet, use the source,
756 * otherwise assume it is in the hw_src
758 switch (bla_dst->type) {
760 backbone_addr = hw_src;
762 case CLAIM_TYPE_REQUEST:
763 case CLAIM_TYPE_ANNOUNCE:
765 backbone_addr = ethhdr->h_source;
771 /* don't accept claim frames from ourselves */
772 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
775 /* if its already the same group, it is fine. */
776 if (bla_dst->group == bla_dst_own->group)
779 /* lets see if this originator is in our mesh */
780 orig_node = orig_hash_find(bat_priv, backbone_addr);
782 /* dont accept claims from gateways which are not in
783 * the same mesh or group.
788 /* if our mesh friends mac is bigger, use it for ourselves. */
789 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
790 bat_dbg(DBG_BLA, bat_priv,
791 "taking other backbones claim group: %04x\n",
792 ntohs(bla_dst->group));
793 bla_dst_own->group = bla_dst->group;
796 batadv_orig_node_free_ref(orig_node);
802 /* @bat_priv: the bat priv with all the soft interface information
803 * @skb: the frame to be checked
805 * Check if this is a claim frame, and process it accordingly.
807 * returns 1 if it was a claim frame, otherwise return 0 to
808 * tell the callee that it can use the frame on its own.
810 static int bla_process_claim(struct bat_priv *bat_priv,
811 struct hard_iface *primary_if,
814 struct ethhdr *ethhdr;
815 struct vlan_ethhdr *vhdr;
816 struct arphdr *arphdr;
817 uint8_t *hw_src, *hw_dst;
818 struct bla_claim_dst *bla_dst;
824 ethhdr = (struct ethhdr *)skb_mac_header(skb);
826 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
827 vhdr = (struct vlan_ethhdr *)ethhdr;
828 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
829 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
830 headlen = sizeof(*vhdr);
832 proto = ntohs(ethhdr->h_proto);
836 if (proto != ETH_P_ARP)
837 return 0; /* not a claim frame */
839 /* this must be a ARP frame. check if it is a claim. */
841 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
844 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
845 ethhdr = (struct ethhdr *)skb_mac_header(skb);
846 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
848 /* Check whether the ARP frame carries a valid
851 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
853 if (arphdr->ar_pro != htons(ETH_P_IP))
855 if (arphdr->ar_hln != ETH_ALEN)
857 if (arphdr->ar_pln != 4)
860 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
861 hw_dst = hw_src + ETH_ALEN + 4;
862 bla_dst = (struct bla_claim_dst *)hw_dst;
864 /* check if it is a claim frame. */
865 ret = check_claim_group(bat_priv, primary_if, hw_src, hw_dst, ethhdr);
867 bat_dbg(DBG_BLA, bat_priv,
868 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
869 ethhdr->h_source, vid, hw_src, hw_dst);
874 /* become a backbone gw ourselves on this vlan if not happened yet */
875 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
877 /* check for the different types of claim frames ... */
878 switch (bla_dst->type) {
880 if (handle_claim(bat_priv, primary_if, hw_src,
881 ethhdr->h_source, vid))
885 if (handle_unclaim(bat_priv, primary_if,
886 ethhdr->h_source, hw_src, vid))
890 case CLAIM_TYPE_ANNOUNCE:
891 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
894 case CLAIM_TYPE_REQUEST:
895 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
900 bat_dbg(DBG_BLA, bat_priv,
901 "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",
902 ethhdr->h_source, vid, hw_src, hw_dst);
906 /* Check when we last heard from other nodes, and remove them in case of
907 * a time out, or clean all backbone gws if now is set.
909 static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
911 struct backbone_gw *backbone_gw;
912 struct hlist_node *node, *node_tmp;
913 struct hlist_head *head;
914 struct hashtable_t *hash;
915 spinlock_t *list_lock; /* protects write access to the hash lists */
918 hash = bat_priv->backbone_hash;
922 for (i = 0; i < hash->size; i++) {
923 head = &hash->table[i];
924 list_lock = &hash->list_locks[i];
926 spin_lock_bh(list_lock);
927 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
931 if (!has_timed_out(backbone_gw->lasttime,
932 BLA_BACKBONE_TIMEOUT))
935 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
936 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
940 /* don't wait for the pending request anymore */
941 if (atomic_read(&backbone_gw->request_sent))
942 atomic_dec(&bat_priv->bla_num_requests);
944 bla_del_backbone_claims(backbone_gw);
947 backbone_gw_free_ref(backbone_gw);
949 spin_unlock_bh(list_lock);
953 /* @bat_priv: the bat priv with all the soft interface information
954 * @primary_if: the selected primary interface, may be NULL if now is set
955 * @now: whether the whole hash shall be wiped now
957 * Check when we heard last time from our own claims, and remove them in case of
958 * a time out, or clean all claims if now is set
960 static void bla_purge_claims(struct bat_priv *bat_priv,
961 struct hard_iface *primary_if, int now)
964 struct hlist_node *node;
965 struct hlist_head *head;
966 struct hashtable_t *hash;
969 hash = bat_priv->claim_hash;
973 for (i = 0; i < hash->size; i++) {
974 head = &hash->table[i];
977 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
980 if (!compare_eth(claim->backbone_gw->orig,
981 primary_if->net_dev->dev_addr))
983 if (!has_timed_out(claim->lasttime,
987 bat_dbg(DBG_BLA, bat_priv,
988 "bla_purge_claims(): %pM, vid %d, time out\n",
989 claim->addr, claim->vid);
992 handle_unclaim(bat_priv, primary_if,
993 claim->backbone_gw->orig,
994 claim->addr, claim->vid);
1000 /* @bat_priv: the bat priv with all the soft interface information
1001 * @primary_if: the new selected primary_if
1002 * @oldif: the old primary interface, may be NULL
1004 * Update the backbone gateways when the own orig address changes.
1006 void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1007 struct hard_iface *primary_if,
1008 struct hard_iface *oldif)
1010 struct backbone_gw *backbone_gw;
1011 struct hlist_node *node;
1012 struct hlist_head *head;
1013 struct hashtable_t *hash;
1016 /* reset bridge loop avoidance group id */
1017 bat_priv->claim_dest.group =
1018 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1021 bla_purge_claims(bat_priv, NULL, 1);
1022 bla_purge_backbone_gw(bat_priv, 1);
1026 hash = bat_priv->backbone_hash;
1030 for (i = 0; i < hash->size; i++) {
1031 head = &hash->table[i];
1034 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1035 /* own orig still holds the old value. */
1036 if (!compare_eth(backbone_gw->orig,
1037 oldif->net_dev->dev_addr))
1040 memcpy(backbone_gw->orig,
1041 primary_if->net_dev->dev_addr, ETH_ALEN);
1042 /* send an announce frame so others will ask for our
1043 * claims and update their tables.
1045 bla_send_announce(bat_priv, backbone_gw);
1053 /* (re)start the timer */
1054 static void bla_start_timer(struct bat_priv *bat_priv)
1056 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
1057 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
1058 msecs_to_jiffies(BLA_PERIOD_LENGTH));
1061 /* periodic work to do:
1062 * * purge structures when they are too old
1063 * * send announcements
1065 static void bla_periodic_work(struct work_struct *work)
1067 struct delayed_work *delayed_work =
1068 container_of(work, struct delayed_work, work);
1069 struct bat_priv *bat_priv =
1070 container_of(delayed_work, struct bat_priv, bla_work);
1071 struct hlist_node *node;
1072 struct hlist_head *head;
1073 struct backbone_gw *backbone_gw;
1074 struct hashtable_t *hash;
1075 struct hard_iface *primary_if;
1078 primary_if = batadv_primary_if_get_selected(bat_priv);
1082 bla_purge_claims(bat_priv, primary_if, 0);
1083 bla_purge_backbone_gw(bat_priv, 0);
1085 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1088 hash = bat_priv->backbone_hash;
1092 for (i = 0; i < hash->size; i++) {
1093 head = &hash->table[i];
1096 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1097 if (!compare_eth(backbone_gw->orig,
1098 primary_if->net_dev->dev_addr))
1101 backbone_gw->lasttime = jiffies;
1103 bla_send_announce(bat_priv, backbone_gw);
1109 batadv_hardif_free_ref(primary_if);
1111 bla_start_timer(bat_priv);
1114 /* The hash for claim and backbone hash receive the same key because they
1115 * are getting initialized by hash_new with the same key. Reinitializing
1116 * them with to different keys to allow nested locking without generating
1119 static struct lock_class_key claim_hash_lock_class_key;
1120 static struct lock_class_key backbone_hash_lock_class_key;
1122 /* initialize all bla structures */
1123 int batadv_bla_init(struct bat_priv *bat_priv)
1126 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1127 struct hard_iface *primary_if;
1129 bat_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
1131 /* setting claim destination address */
1132 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1133 bat_priv->claim_dest.type = 0;
1134 primary_if = batadv_primary_if_get_selected(bat_priv);
1136 bat_priv->claim_dest.group =
1137 htons(crc16(0, primary_if->net_dev->dev_addr,
1139 batadv_hardif_free_ref(primary_if);
1141 bat_priv->claim_dest.group = 0; /* will be set later */
1144 /* initialize the duplicate list */
1145 for (i = 0; i < DUPLIST_SIZE; i++)
1146 bat_priv->bcast_duplist[i].entrytime =
1147 jiffies - msecs_to_jiffies(DUPLIST_TIMEOUT);
1148 bat_priv->bcast_duplist_curr = 0;
1150 if (bat_priv->claim_hash)
1153 bat_priv->claim_hash = batadv_hash_new(128);
1154 bat_priv->backbone_hash = batadv_hash_new(32);
1156 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
1159 batadv_hash_set_lock_class(bat_priv->claim_hash,
1160 &claim_hash_lock_class_key);
1161 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1162 &backbone_hash_lock_class_key);
1164 bat_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
1166 bla_start_timer(bat_priv);
1170 /* @bat_priv: the bat priv with all the soft interface information
1171 * @bcast_packet: originator mac address
1172 * @hdr_size: maximum length of the frame
1174 * check if it is on our broadcast list. Another gateway might
1175 * have sent the same packet because it is connected to the same backbone,
1176 * so we have to remove this duplicate.
1178 * This is performed by checking the CRC, which will tell us
1179 * with a good chance that it is the same packet. If it is furthermore
1180 * sent by another host, drop it. We allow equal packets from
1181 * the same host however as this might be intended.
1183 int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1184 struct bcast_packet *bcast_packet,
1187 int i, length, curr;
1190 struct bcast_duplist_entry *entry;
1192 length = hdr_size - sizeof(*bcast_packet);
1193 content = (uint8_t *)bcast_packet;
1194 content += sizeof(*bcast_packet);
1196 /* calculate the crc ... */
1197 crc = crc16(0, content, length);
1199 for (i = 0 ; i < DUPLIST_SIZE; i++) {
1200 curr = (bat_priv->bcast_duplist_curr + i) % DUPLIST_SIZE;
1201 entry = &bat_priv->bcast_duplist[curr];
1203 /* we can stop searching if the entry is too old ;
1204 * later entries will be even older
1206 if (has_timed_out(entry->entrytime, DUPLIST_TIMEOUT))
1209 if (entry->crc != crc)
1212 if (compare_eth(entry->orig, bcast_packet->orig))
1215 /* this entry seems to match: same crc, not too old,
1216 * and from another gw. therefore return 1 to forbid it.
1220 /* not found, add a new entry (overwrite the oldest entry) */
1221 curr = (bat_priv->bcast_duplist_curr + DUPLIST_SIZE - 1) % DUPLIST_SIZE;
1222 entry = &bat_priv->bcast_duplist[curr];
1224 entry->entrytime = jiffies;
1225 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1226 bat_priv->bcast_duplist_curr = curr;
1228 /* allow it, its the first occurence. */
1234 /* @bat_priv: the bat priv with all the soft interface information
1235 * @orig: originator mac address
1237 * check if the originator is a gateway for any VLAN ID.
1239 * returns 1 if it is found, 0 otherwise
1241 int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
1243 struct hashtable_t *hash = bat_priv->backbone_hash;
1244 struct hlist_head *head;
1245 struct hlist_node *node;
1246 struct backbone_gw *backbone_gw;
1249 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1255 for (i = 0; i < hash->size; i++) {
1256 head = &hash->table[i];
1259 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1260 if (compare_eth(backbone_gw->orig, orig)) {
1272 /* @skb: the frame to be checked
1273 * @orig_node: the orig_node of the frame
1274 * @hdr_size: maximum length of the frame
1276 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1277 * if the orig_node is also a gateway on the soft interface, otherwise it
1280 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1281 struct orig_node *orig_node, int hdr_size)
1283 struct ethhdr *ethhdr;
1284 struct vlan_ethhdr *vhdr;
1285 struct backbone_gw *backbone_gw;
1288 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1291 /* first, find out the vid. */
1292 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1295 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1297 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1298 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1301 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1303 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1306 /* see if this originator is a backbone gw for this VLAN */
1307 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1308 orig_node->orig, vid);
1312 backbone_gw_free_ref(backbone_gw);
1316 /* free all bla structures (for softinterface free or module unload) */
1317 void batadv_bla_free(struct bat_priv *bat_priv)
1319 struct hard_iface *primary_if;
1321 cancel_delayed_work_sync(&bat_priv->bla_work);
1322 primary_if = batadv_primary_if_get_selected(bat_priv);
1324 if (bat_priv->claim_hash) {
1325 bla_purge_claims(bat_priv, primary_if, 1);
1326 batadv_hash_destroy(bat_priv->claim_hash);
1327 bat_priv->claim_hash = NULL;
1329 if (bat_priv->backbone_hash) {
1330 bla_purge_backbone_gw(bat_priv, 1);
1331 batadv_hash_destroy(bat_priv->backbone_hash);
1332 bat_priv->backbone_hash = NULL;
1335 batadv_hardif_free_ref(primary_if);
1338 /* @bat_priv: the bat priv with all the soft interface information
1339 * @skb: the frame to be checked
1340 * @vid: the VLAN ID of the frame
1342 * bla_rx avoidance checks if:
1343 * * we have to race for a claim
1344 * * if the frame is allowed on the LAN
1346 * in these cases, the skb is further handled by this function and
1347 * returns 1, otherwise it returns 0 and the caller shall further
1350 int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1352 struct ethhdr *ethhdr;
1353 struct claim search_claim, *claim = NULL;
1354 struct hard_iface *primary_if;
1357 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1359 primary_if = batadv_primary_if_get_selected(bat_priv);
1363 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1367 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1368 /* don't allow broadcasts while requests are in flight */
1369 if (is_multicast_ether_addr(ethhdr->h_dest))
1372 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1373 search_claim.vid = vid;
1374 claim = claim_hash_find(bat_priv, &search_claim);
1377 /* possible optimization: race for a claim */
1378 /* No claim exists yet, claim it for us!
1380 handle_claim(bat_priv, primary_if,
1381 primary_if->net_dev->dev_addr,
1382 ethhdr->h_source, vid);
1386 /* if it is our own claim ... */
1387 if (compare_eth(claim->backbone_gw->orig,
1388 primary_if->net_dev->dev_addr)) {
1389 /* ... allow it in any case */
1390 claim->lasttime = jiffies;
1394 /* if it is a broadcast ... */
1395 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1396 /* ... drop it. the responsible gateway is in charge. */
1399 /* seems the client considers us as its best gateway.
1400 * send a claim and update the claim table
1403 handle_claim(bat_priv, primary_if,
1404 primary_if->net_dev->dev_addr,
1405 ethhdr->h_source, vid);
1409 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1419 batadv_hardif_free_ref(primary_if);
1421 claim_free_ref(claim);
1425 /* @bat_priv: the bat priv with all the soft interface information
1426 * @skb: the frame to be checked
1427 * @vid: the VLAN ID of the frame
1430 * * a claim was received which has to be processed
1431 * * the frame is allowed on the mesh
1433 * in these cases, the skb is further handled by this function and
1434 * returns 1, otherwise it returns 0 and the caller shall further
1437 int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1439 struct ethhdr *ethhdr;
1440 struct claim search_claim, *claim = NULL;
1441 struct hard_iface *primary_if;
1444 primary_if = batadv_primary_if_get_selected(bat_priv);
1448 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1451 /* in VLAN case, the mac header might not be set. */
1452 skb_reset_mac_header(skb);
1454 if (bla_process_claim(bat_priv, primary_if, skb))
1457 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1459 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1460 /* don't allow broadcasts while requests are in flight */
1461 if (is_multicast_ether_addr(ethhdr->h_dest))
1464 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1465 search_claim.vid = vid;
1467 claim = claim_hash_find(bat_priv, &search_claim);
1469 /* if no claim exists, allow it. */
1473 /* check if we are responsible. */
1474 if (compare_eth(claim->backbone_gw->orig,
1475 primary_if->net_dev->dev_addr)) {
1476 /* if yes, the client has roamed and we have
1479 handle_unclaim(bat_priv, primary_if,
1480 primary_if->net_dev->dev_addr,
1481 ethhdr->h_source, vid);
1485 /* check if it is a multicast/broadcast frame */
1486 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1487 /* drop it. the responsible gateway has forwarded it into
1488 * the backbone network.
1492 /* we must allow it. at least if we are
1493 * responsible for the DESTINATION.
1498 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1505 batadv_hardif_free_ref(primary_if);
1507 claim_free_ref(claim);
1511 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1513 struct net_device *net_dev = (struct net_device *)seq->private;
1514 struct bat_priv *bat_priv = netdev_priv(net_dev);
1515 struct hashtable_t *hash = bat_priv->claim_hash;
1516 struct claim *claim;
1517 struct hard_iface *primary_if;
1518 struct hlist_node *node;
1519 struct hlist_head *head;
1524 primary_if = batadv_primary_if_get_selected(bat_priv);
1526 ret = seq_printf(seq,
1527 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1532 if (primary_if->if_status != IF_ACTIVE) {
1533 ret = seq_printf(seq,
1534 "BATMAN mesh %s disabled - primary interface not active\n",
1540 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1541 net_dev->name, primary_if->net_dev->dev_addr,
1542 ntohs(bat_priv->claim_dest.group));
1543 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1544 "Client", "VID", "Originator", "CRC");
1545 for (i = 0; i < hash->size; i++) {
1546 head = &hash->table[i];
1549 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1550 is_own = compare_eth(claim->backbone_gw->orig,
1551 primary_if->net_dev->dev_addr);
1552 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1553 claim->addr, claim->vid,
1554 claim->backbone_gw->orig,
1555 (is_own ? 'x' : ' '),
1556 claim->backbone_gw->crc);
1562 batadv_hardif_free_ref(primary_if);