Path: blob/master/net/batman-adv/bridge_loop_avoidance.c
49880 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (C) B.A.T.M.A.N. contributors:2*3* Simon Wunderlich4*/56#include "bridge_loop_avoidance.h"7#include "main.h"89#include <linux/atomic.h>10#include <linux/byteorder/generic.h>11#include <linux/compiler.h>12#include <linux/container_of.h>13#include <linux/crc16.h>14#include <linux/err.h>15#include <linux/errno.h>16#include <linux/etherdevice.h>17#include <linux/gfp.h>18#include <linux/if_arp.h>19#include <linux/if_ether.h>20#include <linux/if_vlan.h>21#include <linux/jhash.h>22#include <linux/jiffies.h>23#include <linux/kref.h>24#include <linux/list.h>25#include <linux/lockdep.h>26#include <linux/netdevice.h>27#include <linux/netlink.h>28#include <linux/rculist.h>29#include <linux/rcupdate.h>30#include <linux/skbuff.h>31#include <linux/slab.h>32#include <linux/spinlock.h>33#include <linux/sprintf.h>34#include <linux/stddef.h>35#include <linux/string.h>36#include <linux/string_choices.h>37#include <linux/workqueue.h>38#include <net/arp.h>39#include <net/genetlink.h>40#include <net/netlink.h>41#include <uapi/linux/batadv_packet.h>42#include <uapi/linux/batman_adv.h>4344#include "hard-interface.h"45#include "hash.h"46#include "log.h"47#include "netlink.h"48#include "originator.h"49#include "translation-table.h"5051static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};5253static void batadv_bla_periodic_work(struct work_struct *work);54static void55batadv_bla_send_announce(struct batadv_priv *bat_priv,56struct batadv_bla_backbone_gw *backbone_gw);5758/**59* batadv_choose_claim() - choose the right bucket for a claim.60* @data: data to hash61* @size: size of the hash table62*63* Return: the hash index of the claim64*/65static inline u32 batadv_choose_claim(const void *data, u32 size)66{67const struct batadv_bla_claim *claim = data;68u32 hash = 0;6970hash = jhash(&claim->addr, sizeof(claim->addr), hash);71hash = jhash(&claim->vid, sizeof(claim->vid), hash);7273return hash % size;74}7576/**77* batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway.78* @data: data to hash79* @size: size of the hash table80*81* Return: the hash index of the backbone gateway82*/83static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)84{85const struct batadv_bla_backbone_gw *gw;86u32 hash = 0;8788gw = data;89hash = jhash(&gw->orig, sizeof(gw->orig), hash);90hash = jhash(&gw->vid, sizeof(gw->vid), hash);9192return hash % size;93}9495/**96* batadv_compare_backbone_gw() - compare address and vid of two backbone gws97* @node: list node of the first entry to compare98* @data2: pointer to the second backbone gateway99*100* Return: true if the backbones have the same data, false otherwise101*/102static bool batadv_compare_backbone_gw(const struct hlist_node *node,103const void *data2)104{105const void *data1 = container_of(node, struct batadv_bla_backbone_gw,106hash_entry);107const struct batadv_bla_backbone_gw *gw1 = data1;108const struct batadv_bla_backbone_gw *gw2 = data2;109110if (!batadv_compare_eth(gw1->orig, gw2->orig))111return false;112113if (gw1->vid != gw2->vid)114return false;115116return true;117}118119/**120* batadv_compare_claim() - compare address and vid of two claims121* @node: list node of the first entry to compare122* @data2: pointer to the second claims123*124* Return: true if the claim have the same data, 0 otherwise125*/126static bool batadv_compare_claim(const struct hlist_node *node,127const void *data2)128{129const void *data1 = container_of(node, struct batadv_bla_claim,130hash_entry);131const struct batadv_bla_claim *cl1 = data1;132const struct batadv_bla_claim *cl2 = data2;133134if (!batadv_compare_eth(cl1->addr, cl2->addr))135return false;136137if (cl1->vid != cl2->vid)138return false;139140return true;141}142143/**144* batadv_backbone_gw_release() - release backbone gw from lists and queue for145* free after rcu grace period146* @ref: kref pointer of the backbone gw147*/148static void batadv_backbone_gw_release(struct kref *ref)149{150struct batadv_bla_backbone_gw *backbone_gw;151152backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,153refcount);154155kfree_rcu(backbone_gw, rcu);156}157158/**159* batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly160* release it161* @backbone_gw: backbone gateway to be free'd162*/163static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)164{165if (!backbone_gw)166return;167168kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);169}170171/**172* batadv_claim_release() - release claim from lists and queue for free after173* rcu grace period174* @ref: kref pointer of the claim175*/176static void batadv_claim_release(struct kref *ref)177{178struct batadv_bla_claim *claim;179struct batadv_bla_backbone_gw *old_backbone_gw;180181claim = container_of(ref, struct batadv_bla_claim, refcount);182183spin_lock_bh(&claim->backbone_lock);184old_backbone_gw = claim->backbone_gw;185claim->backbone_gw = NULL;186spin_unlock_bh(&claim->backbone_lock);187188spin_lock_bh(&old_backbone_gw->crc_lock);189old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);190spin_unlock_bh(&old_backbone_gw->crc_lock);191192batadv_backbone_gw_put(old_backbone_gw);193194kfree_rcu(claim, rcu);195}196197/**198* batadv_claim_put() - decrement the claim refcounter and possibly release it199* @claim: claim to be free'd200*/201static void batadv_claim_put(struct batadv_bla_claim *claim)202{203if (!claim)204return;205206kref_put(&claim->refcount, batadv_claim_release);207}208209/**210* batadv_claim_hash_find() - looks for a claim in the claim hash211* @bat_priv: the bat priv with all the mesh interface information212* @data: search data (may be local/static data)213*214* Return: claim if found or NULL otherwise.215*/216static struct batadv_bla_claim *217batadv_claim_hash_find(struct batadv_priv *bat_priv,218struct batadv_bla_claim *data)219{220struct batadv_hashtable *hash = bat_priv->bla.claim_hash;221struct hlist_head *head;222struct batadv_bla_claim *claim;223struct batadv_bla_claim *claim_tmp = NULL;224int index;225226if (!hash)227return NULL;228229index = batadv_choose_claim(data, hash->size);230head = &hash->table[index];231232rcu_read_lock();233hlist_for_each_entry_rcu(claim, head, hash_entry) {234if (!batadv_compare_claim(&claim->hash_entry, data))235continue;236237if (!kref_get_unless_zero(&claim->refcount))238continue;239240claim_tmp = claim;241break;242}243rcu_read_unlock();244245return claim_tmp;246}247248/**249* batadv_backbone_hash_find() - looks for a backbone gateway in the hash250* @bat_priv: the bat priv with all the mesh interface information251* @addr: the address of the originator252* @vid: the VLAN ID253*254* Return: backbone gateway if found or NULL otherwise255*/256static struct batadv_bla_backbone_gw *257batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr,258unsigned short vid)259{260struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;261struct hlist_head *head;262struct batadv_bla_backbone_gw search_entry, *backbone_gw;263struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;264int index;265266if (!hash)267return NULL;268269ether_addr_copy(search_entry.orig, addr);270search_entry.vid = vid;271272index = batadv_choose_backbone_gw(&search_entry, hash->size);273head = &hash->table[index];274275rcu_read_lock();276hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {277if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,278&search_entry))279continue;280281if (!kref_get_unless_zero(&backbone_gw->refcount))282continue;283284backbone_gw_tmp = backbone_gw;285break;286}287rcu_read_unlock();288289return backbone_gw_tmp;290}291292/**293* batadv_bla_del_backbone_claims() - delete all claims for a backbone294* @backbone_gw: backbone gateway where the claims should be removed295*/296static void297batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)298{299struct batadv_hashtable *hash;300struct hlist_node *node_tmp;301struct hlist_head *head;302struct batadv_bla_claim *claim;303int i;304spinlock_t *list_lock; /* protects write access to the hash lists */305306hash = backbone_gw->bat_priv->bla.claim_hash;307if (!hash)308return;309310for (i = 0; i < hash->size; i++) {311head = &hash->table[i];312list_lock = &hash->list_locks[i];313314spin_lock_bh(list_lock);315hlist_for_each_entry_safe(claim, node_tmp,316head, hash_entry) {317if (claim->backbone_gw != backbone_gw)318continue;319320batadv_claim_put(claim);321hlist_del_rcu(&claim->hash_entry);322}323spin_unlock_bh(list_lock);324}325326/* all claims gone, initialize CRC */327spin_lock_bh(&backbone_gw->crc_lock);328backbone_gw->crc = BATADV_BLA_CRC_INIT;329spin_unlock_bh(&backbone_gw->crc_lock);330}331332/**333* batadv_bla_send_claim() - sends a claim frame according to the provided info334* @bat_priv: the bat priv with all the mesh interface information335* @mac: the mac address to be announced within the claim336* @vid: the VLAN ID337* @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)338*/339static void batadv_bla_send_claim(struct batadv_priv *bat_priv, const u8 *mac,340unsigned short vid, int claimtype)341{342struct sk_buff *skb;343struct ethhdr *ethhdr;344struct batadv_hard_iface *primary_if;345struct net_device *mesh_iface;346u8 *hw_src;347struct batadv_bla_claim_dst local_claim_dest;348__be32 zeroip = 0;349350primary_if = batadv_primary_if_get_selected(bat_priv);351if (!primary_if)352return;353354memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,355sizeof(local_claim_dest));356local_claim_dest.type = claimtype;357358mesh_iface = primary_if->mesh_iface;359360skb = arp_create(ARPOP_REPLY, ETH_P_ARP,361/* IP DST: 0.0.0.0 */362zeroip,363primary_if->mesh_iface,364/* IP SRC: 0.0.0.0 */365zeroip,366/* Ethernet DST: Broadcast */367NULL,368/* Ethernet SRC/HW SRC: originator mac */369primary_if->net_dev->dev_addr,370/* HW DST: FF:43:05:XX:YY:YY371* with XX = claim type372* and YY:YY = group id373*/374(u8 *)&local_claim_dest);375376if (!skb)377goto out;378379ethhdr = (struct ethhdr *)skb->data;380hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);381382/* now we pretend that the client would have sent this ... */383switch (claimtype) {384case BATADV_CLAIM_TYPE_CLAIM:385/* normal claim frame386* set Ethernet SRC to the clients mac387*/388ether_addr_copy(ethhdr->h_source, mac);389batadv_dbg(BATADV_DBG_BLA, bat_priv,390"%s(): CLAIM %pM on vid %d\n", __func__, mac,391batadv_print_vid(vid));392break;393case BATADV_CLAIM_TYPE_UNCLAIM:394/* unclaim frame395* set HW SRC to the clients mac396*/397ether_addr_copy(hw_src, mac);398batadv_dbg(BATADV_DBG_BLA, bat_priv,399"%s(): UNCLAIM %pM on vid %d\n", __func__, mac,400batadv_print_vid(vid));401break;402case BATADV_CLAIM_TYPE_ANNOUNCE:403/* announcement frame404* set HW SRC to the special mac containing the crc405*/406ether_addr_copy(hw_src, mac);407batadv_dbg(BATADV_DBG_BLA, bat_priv,408"%s(): ANNOUNCE of %pM on vid %d\n", __func__,409ethhdr->h_source, batadv_print_vid(vid));410break;411case BATADV_CLAIM_TYPE_REQUEST:412/* request frame413* set HW SRC and header destination to the receiving backbone414* gws mac415*/416ether_addr_copy(hw_src, mac);417ether_addr_copy(ethhdr->h_dest, mac);418batadv_dbg(BATADV_DBG_BLA, bat_priv,419"%s(): REQUEST of %pM to %pM on vid %d\n", __func__,420ethhdr->h_source, ethhdr->h_dest,421batadv_print_vid(vid));422break;423case BATADV_CLAIM_TYPE_LOOPDETECT:424ether_addr_copy(ethhdr->h_source, mac);425batadv_dbg(BATADV_DBG_BLA, bat_priv,426"%s(): LOOPDETECT of %pM to %pM on vid %d\n",427__func__, ethhdr->h_source, ethhdr->h_dest,428batadv_print_vid(vid));429430break;431}432433if (vid & BATADV_VLAN_HAS_TAG) {434skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),435vid & VLAN_VID_MASK);436if (!skb)437goto out;438}439440skb_reset_mac_header(skb);441skb->protocol = eth_type_trans(skb, mesh_iface);442batadv_inc_counter(bat_priv, BATADV_CNT_RX);443batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,444skb->len + ETH_HLEN);445446netif_rx(skb);447out:448batadv_hardif_put(primary_if);449}450451/**452* batadv_bla_loopdetect_report() - worker for reporting the loop453* @work: work queue item454*455* Throws an uevent, as the loopdetect check function can't do that itself456* since the kernel may sleep while throwing uevents.457*/458static void batadv_bla_loopdetect_report(struct work_struct *work)459{460struct batadv_bla_backbone_gw *backbone_gw;461struct batadv_priv *bat_priv;462char vid_str[6] = { '\0' };463464backbone_gw = container_of(work, struct batadv_bla_backbone_gw,465report_work);466bat_priv = backbone_gw->bat_priv;467468batadv_info(bat_priv->mesh_iface,469"Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",470batadv_print_vid(backbone_gw->vid));471snprintf(vid_str, sizeof(vid_str), "%d",472batadv_print_vid(backbone_gw->vid));473vid_str[sizeof(vid_str) - 1] = 0;474475batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,476vid_str);477478batadv_backbone_gw_put(backbone_gw);479}480481/**482* batadv_bla_get_backbone_gw() - finds or creates a backbone gateway483* @bat_priv: the bat priv with all the mesh interface information484* @orig: the mac address of the originator485* @vid: the VLAN ID486* @own_backbone: set if the requested backbone is local487*488* Return: the (possibly created) backbone gateway or NULL on error489*/490static struct batadv_bla_backbone_gw *491batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,492unsigned short vid, bool own_backbone)493{494struct batadv_bla_backbone_gw *entry;495struct batadv_orig_node *orig_node;496int hash_added;497498entry = batadv_backbone_hash_find(bat_priv, orig, vid);499500if (entry)501return entry;502503batadv_dbg(BATADV_DBG_BLA, bat_priv,504"%s(): not found (%pM, %d), creating new entry\n", __func__,505orig, batadv_print_vid(vid));506507entry = kzalloc(sizeof(*entry), GFP_ATOMIC);508if (!entry)509return NULL;510511entry->vid = vid;512entry->lasttime = jiffies;513entry->crc = BATADV_BLA_CRC_INIT;514entry->bat_priv = bat_priv;515spin_lock_init(&entry->crc_lock);516atomic_set(&entry->request_sent, 0);517atomic_set(&entry->wait_periods, 0);518ether_addr_copy(entry->orig, orig);519INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);520kref_init(&entry->refcount);521522kref_get(&entry->refcount);523hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,524batadv_compare_backbone_gw,525batadv_choose_backbone_gw, entry,526&entry->hash_entry);527528if (unlikely(hash_added != 0)) {529/* hash failed, free the structure */530kfree(entry);531return NULL;532}533534/* this is a gateway now, remove any TT entry on this VLAN */535orig_node = batadv_orig_hash_find(bat_priv, orig);536if (orig_node) {537batadv_tt_global_del_orig(bat_priv, orig_node, vid,538"became a backbone gateway");539batadv_orig_node_put(orig_node);540}541542if (own_backbone) {543batadv_bla_send_announce(bat_priv, entry);544545/* this will be decreased in the worker thread */546atomic_inc(&entry->request_sent);547atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);548atomic_inc(&bat_priv->bla.num_requests);549}550551return entry;552}553554/**555* batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN556* @bat_priv: the bat priv with all the mesh interface information557* @primary_if: the selected primary interface558* @vid: VLAN identifier559*560* update or add the own backbone gw to make sure we announce561* where we receive other backbone gws562*/563static void564batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,565struct batadv_hard_iface *primary_if,566unsigned short vid)567{568struct batadv_bla_backbone_gw *backbone_gw;569570backbone_gw = batadv_bla_get_backbone_gw(bat_priv,571primary_if->net_dev->dev_addr,572vid, true);573if (unlikely(!backbone_gw))574return;575576backbone_gw->lasttime = jiffies;577batadv_backbone_gw_put(backbone_gw);578}579580/**581* batadv_bla_answer_request() - answer a bla request by sending own claims582* @bat_priv: the bat priv with all the mesh interface information583* @primary_if: interface where the request came on584* @vid: the vid where the request came on585*586* Repeat all of our own claims, and finally send an ANNOUNCE frame587* to allow the requester another check if the CRC is correct now.588*/589static void batadv_bla_answer_request(struct batadv_priv *bat_priv,590struct batadv_hard_iface *primary_if,591unsigned short vid)592{593struct hlist_head *head;594struct batadv_hashtable *hash;595struct batadv_bla_claim *claim;596struct batadv_bla_backbone_gw *backbone_gw;597int i;598599batadv_dbg(BATADV_DBG_BLA, bat_priv,600"%s(): received a claim request, send all of our own claims again\n",601__func__);602603backbone_gw = batadv_backbone_hash_find(bat_priv,604primary_if->net_dev->dev_addr,605vid);606if (!backbone_gw)607return;608609hash = bat_priv->bla.claim_hash;610for (i = 0; i < hash->size; i++) {611head = &hash->table[i];612613rcu_read_lock();614hlist_for_each_entry_rcu(claim, head, hash_entry) {615/* only own claims are interesting */616if (claim->backbone_gw != backbone_gw)617continue;618619batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,620BATADV_CLAIM_TYPE_CLAIM);621}622rcu_read_unlock();623}624625/* finally, send an announcement frame */626batadv_bla_send_announce(bat_priv, backbone_gw);627batadv_backbone_gw_put(backbone_gw);628}629630/**631* batadv_bla_send_request() - send a request to repeat claims632* @backbone_gw: the backbone gateway from whom we are out of sync633*634* When the crc is wrong, ask the backbone gateway for a full table update.635* After the request, it will repeat all of his own claims and finally636* send an announcement claim with which we can check again.637*/638static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)639{640/* first, remove all old entries */641batadv_bla_del_backbone_claims(backbone_gw);642643batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,644"Sending REQUEST to %pM\n", backbone_gw->orig);645646/* send request */647batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,648backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);649650/* no local broadcasts should be sent or received, for now. */651if (!atomic_read(&backbone_gw->request_sent)) {652atomic_inc(&backbone_gw->bat_priv->bla.num_requests);653atomic_set(&backbone_gw->request_sent, 1);654}655}656657/**658* batadv_bla_send_announce() - Send an announcement frame659* @bat_priv: the bat priv with all the mesh interface information660* @backbone_gw: our backbone gateway which should be announced661*/662static void batadv_bla_send_announce(struct batadv_priv *bat_priv,663struct batadv_bla_backbone_gw *backbone_gw)664{665u8 mac[ETH_ALEN];666__be16 crc;667668memcpy(mac, batadv_announce_mac, 4);669spin_lock_bh(&backbone_gw->crc_lock);670crc = htons(backbone_gw->crc);671spin_unlock_bh(&backbone_gw->crc_lock);672memcpy(&mac[4], &crc, 2);673674batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,675BATADV_CLAIM_TYPE_ANNOUNCE);676}677678/**679* batadv_bla_add_claim() - Adds a claim in the claim hash680* @bat_priv: the bat priv with all the mesh interface information681* @mac: the mac address of the claim682* @vid: the VLAN ID of the frame683* @backbone_gw: the backbone gateway which claims it684*/685static void batadv_bla_add_claim(struct batadv_priv *bat_priv,686const u8 *mac, const unsigned short vid,687struct batadv_bla_backbone_gw *backbone_gw)688{689struct batadv_bla_backbone_gw *old_backbone_gw;690struct batadv_bla_claim *claim;691struct batadv_bla_claim search_claim;692bool remove_crc = false;693int hash_added;694695ether_addr_copy(search_claim.addr, mac);696search_claim.vid = vid;697claim = batadv_claim_hash_find(bat_priv, &search_claim);698699/* create a new claim entry if it does not exist yet. */700if (!claim) {701claim = kzalloc(sizeof(*claim), GFP_ATOMIC);702if (!claim)703return;704705ether_addr_copy(claim->addr, mac);706spin_lock_init(&claim->backbone_lock);707claim->vid = vid;708claim->lasttime = jiffies;709kref_get(&backbone_gw->refcount);710claim->backbone_gw = backbone_gw;711kref_init(&claim->refcount);712713batadv_dbg(BATADV_DBG_BLA, bat_priv,714"%s(): adding new entry %pM, vid %d to hash ...\n",715__func__, mac, batadv_print_vid(vid));716717kref_get(&claim->refcount);718hash_added = batadv_hash_add(bat_priv->bla.claim_hash,719batadv_compare_claim,720batadv_choose_claim, claim,721&claim->hash_entry);722723if (unlikely(hash_added != 0)) {724/* only local changes happened. */725kfree(claim);726return;727}728} else {729claim->lasttime = jiffies;730if (claim->backbone_gw == backbone_gw)731/* no need to register a new backbone */732goto claim_free_ref;733734batadv_dbg(BATADV_DBG_BLA, bat_priv,735"%s(): changing ownership for %pM, vid %d to gw %pM\n",736__func__, mac, batadv_print_vid(vid),737backbone_gw->orig);738739remove_crc = true;740}741742/* replace backbone_gw atomically and adjust reference counters */743spin_lock_bh(&claim->backbone_lock);744old_backbone_gw = claim->backbone_gw;745kref_get(&backbone_gw->refcount);746claim->backbone_gw = backbone_gw;747spin_unlock_bh(&claim->backbone_lock);748749if (remove_crc) {750/* remove claim address from old backbone_gw */751spin_lock_bh(&old_backbone_gw->crc_lock);752old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);753spin_unlock_bh(&old_backbone_gw->crc_lock);754}755756batadv_backbone_gw_put(old_backbone_gw);757758/* add claim address to new backbone_gw */759spin_lock_bh(&backbone_gw->crc_lock);760backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);761spin_unlock_bh(&backbone_gw->crc_lock);762backbone_gw->lasttime = jiffies;763764claim_free_ref:765batadv_claim_put(claim);766}767768/**769* batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of770* claim771* @claim: claim whose backbone_gw should be returned772*773* Return: valid reference to claim::backbone_gw774*/775static struct batadv_bla_backbone_gw *776batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)777{778struct batadv_bla_backbone_gw *backbone_gw;779780spin_lock_bh(&claim->backbone_lock);781backbone_gw = claim->backbone_gw;782kref_get(&backbone_gw->refcount);783spin_unlock_bh(&claim->backbone_lock);784785return backbone_gw;786}787788/**789* batadv_bla_del_claim() - delete a claim from the claim hash790* @bat_priv: the bat priv with all the mesh interface information791* @mac: mac address of the claim to be removed792* @vid: VLAN id for the claim to be removed793*/794static void batadv_bla_del_claim(struct batadv_priv *bat_priv,795const u8 *mac, const unsigned short vid)796{797struct batadv_bla_claim search_claim, *claim;798struct batadv_bla_claim *claim_removed_entry;799struct hlist_node *claim_removed_node;800801ether_addr_copy(search_claim.addr, mac);802search_claim.vid = vid;803claim = batadv_claim_hash_find(bat_priv, &search_claim);804if (!claim)805return;806807batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,808mac, batadv_print_vid(vid));809810claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,811batadv_compare_claim,812batadv_choose_claim, claim);813if (!claim_removed_node)814goto free_claim;815816/* reference from the hash is gone */817claim_removed_entry = hlist_entry(claim_removed_node,818struct batadv_bla_claim, hash_entry);819batadv_claim_put(claim_removed_entry);820821free_claim:822/* don't need the reference from hash_find() anymore */823batadv_claim_put(claim);824}825826/**827* batadv_handle_announce() - check for ANNOUNCE frame828* @bat_priv: the bat priv with all the mesh interface information829* @an_addr: announcement mac address (ARP Sender HW address)830* @backbone_addr: originator address of the sender (Ethernet source MAC)831* @vid: the VLAN ID of the frame832*833* Return: true if handled834*/835static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,836u8 *backbone_addr, unsigned short vid)837{838struct batadv_bla_backbone_gw *backbone_gw;839u16 backbone_crc, crc;840841if (memcmp(an_addr, batadv_announce_mac, 4) != 0)842return false;843844backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,845false);846847if (unlikely(!backbone_gw))848return true;849850/* handle as ANNOUNCE frame */851backbone_gw->lasttime = jiffies;852crc = ntohs(*((__force __be16 *)(&an_addr[4])));853854batadv_dbg(BATADV_DBG_BLA, bat_priv,855"%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",856__func__, batadv_print_vid(vid), backbone_gw->orig, crc);857858spin_lock_bh(&backbone_gw->crc_lock);859backbone_crc = backbone_gw->crc;860spin_unlock_bh(&backbone_gw->crc_lock);861862if (backbone_crc != crc) {863batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,864"%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",865__func__, backbone_gw->orig,866batadv_print_vid(backbone_gw->vid),867backbone_crc, crc);868869batadv_bla_send_request(backbone_gw);870} else {871/* if we have sent a request and the crc was OK,872* we can allow traffic again.873*/874if (atomic_read(&backbone_gw->request_sent)) {875atomic_dec(&backbone_gw->bat_priv->bla.num_requests);876atomic_set(&backbone_gw->request_sent, 0);877}878}879880batadv_backbone_gw_put(backbone_gw);881return true;882}883884/**885* batadv_handle_request() - check for REQUEST frame886* @bat_priv: the bat priv with all the mesh interface information887* @primary_if: the primary hard interface of this batman mesh interface888* @backbone_addr: backbone address to be requested (ARP sender HW MAC)889* @ethhdr: ethernet header of a packet890* @vid: the VLAN ID of the frame891*892* Return: true if handled893*/894static bool batadv_handle_request(struct batadv_priv *bat_priv,895struct batadv_hard_iface *primary_if,896u8 *backbone_addr, struct ethhdr *ethhdr,897unsigned short vid)898{899/* check for REQUEST frame */900if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))901return false;902903/* sanity check, this should not happen on a normal switch,904* we ignore it in this case.905*/906if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))907return true;908909batadv_dbg(BATADV_DBG_BLA, bat_priv,910"%s(): REQUEST vid %d (sent by %pM)...\n",911__func__, batadv_print_vid(vid), ethhdr->h_source);912913batadv_bla_answer_request(bat_priv, primary_if, vid);914return true;915}916917/**918* batadv_handle_unclaim() - check for UNCLAIM frame919* @bat_priv: the bat priv with all the mesh interface information920* @primary_if: the primary hard interface of this batman mesh interface921* @backbone_addr: originator address of the backbone (Ethernet source)922* @claim_addr: Client to be unclaimed (ARP sender HW MAC)923* @vid: the VLAN ID of the frame924*925* Return: true if handled926*/927static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,928struct batadv_hard_iface *primary_if,929const u8 *backbone_addr, const u8 *claim_addr,930unsigned short vid)931{932struct batadv_bla_backbone_gw *backbone_gw;933934/* unclaim in any case if it is our own */935if (primary_if && batadv_compare_eth(backbone_addr,936primary_if->net_dev->dev_addr))937batadv_bla_send_claim(bat_priv, claim_addr, vid,938BATADV_CLAIM_TYPE_UNCLAIM);939940backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);941942if (!backbone_gw)943return true;944945/* this must be an UNCLAIM frame */946batadv_dbg(BATADV_DBG_BLA, bat_priv,947"%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,948claim_addr, batadv_print_vid(vid), backbone_gw->orig);949950batadv_bla_del_claim(bat_priv, claim_addr, vid);951batadv_backbone_gw_put(backbone_gw);952return true;953}954955/**956* batadv_handle_claim() - check for CLAIM frame957* @bat_priv: the bat priv with all the mesh interface information958* @primary_if: the primary hard interface of this batman mesh interface959* @backbone_addr: originator address of the backbone (Ethernet Source)960* @claim_addr: client mac address to be claimed (ARP sender HW MAC)961* @vid: the VLAN ID of the frame962*963* Return: true if handled964*/965static bool batadv_handle_claim(struct batadv_priv *bat_priv,966struct batadv_hard_iface *primary_if,967const u8 *backbone_addr, const u8 *claim_addr,968unsigned short vid)969{970struct batadv_bla_backbone_gw *backbone_gw;971972/* register the gateway if not yet available, and add the claim. */973974backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,975false);976977if (unlikely(!backbone_gw))978return true;979980/* this must be a CLAIM frame */981batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);982if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))983batadv_bla_send_claim(bat_priv, claim_addr, vid,984BATADV_CLAIM_TYPE_CLAIM);985986/* TODO: we could call something like tt_local_del() here. */987988batadv_backbone_gw_put(backbone_gw);989return true;990}991992/**993* batadv_check_claim_group() - check for claim group membership994* @bat_priv: the bat priv with all the mesh interface information995* @primary_if: the primary interface of this batman interface996* @hw_src: the Hardware source in the ARP Header997* @hw_dst: the Hardware destination in the ARP Header998* @ethhdr: pointer to the Ethernet header of the claim frame999*1000* checks if it is a claim packet and if it's on the same group.1001* This function also applies the group ID of the sender1002* if it is in the same mesh.1003*1004* Return:1005* 2 - if it is a claim packet and on the same group1006* 1 - if is a claim packet from another group1007* 0 - if it is not a claim packet1008*/1009static int batadv_check_claim_group(struct batadv_priv *bat_priv,1010struct batadv_hard_iface *primary_if,1011u8 *hw_src, u8 *hw_dst,1012struct ethhdr *ethhdr)1013{1014u8 *backbone_addr;1015struct batadv_orig_node *orig_node;1016struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;10171018bla_dst = (struct batadv_bla_claim_dst *)hw_dst;1019bla_dst_own = &bat_priv->bla.claim_dest;10201021/* if announcement packet, use the source,1022* otherwise assume it is in the hw_src1023*/1024switch (bla_dst->type) {1025case BATADV_CLAIM_TYPE_CLAIM:1026backbone_addr = hw_src;1027break;1028case BATADV_CLAIM_TYPE_REQUEST:1029case BATADV_CLAIM_TYPE_ANNOUNCE:1030case BATADV_CLAIM_TYPE_UNCLAIM:1031backbone_addr = ethhdr->h_source;1032break;1033default:1034return 0;1035}10361037/* don't accept claim frames from ourselves */1038if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))1039return 0;10401041/* if its already the same group, it is fine. */1042if (bla_dst->group == bla_dst_own->group)1043return 2;10441045/* lets see if this originator is in our mesh */1046orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);10471048/* don't accept claims from gateways which are not in1049* the same mesh or group.1050*/1051if (!orig_node)1052return 1;10531054/* if our mesh friends mac is bigger, use it for ourselves. */1055if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {1056batadv_dbg(BATADV_DBG_BLA, bat_priv,1057"taking other backbones claim group: %#.4x\n",1058ntohs(bla_dst->group));1059bla_dst_own->group = bla_dst->group;1060}10611062batadv_orig_node_put(orig_node);10631064return 2;1065}10661067/**1068* batadv_bla_process_claim() - Check if this is a claim frame, and process it1069* @bat_priv: the bat priv with all the mesh interface information1070* @primary_if: the primary hard interface of this batman mesh interface1071* @skb: the frame to be checked1072*1073* Return: true if it was a claim frame, otherwise return false to1074* tell the callee that it can use the frame on its own.1075*/1076static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,1077struct batadv_hard_iface *primary_if,1078struct sk_buff *skb)1079{1080struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;1081u8 *hw_src, *hw_dst;1082struct vlan_hdr *vhdr, vhdr_buf;1083struct ethhdr *ethhdr;1084struct arphdr *arphdr;1085unsigned short vid;1086int vlan_depth = 0;1087__be16 proto;1088int headlen;1089int ret;10901091vid = batadv_get_vid(skb, 0);1092ethhdr = eth_hdr(skb);10931094proto = ethhdr->h_proto;1095headlen = ETH_HLEN;1096if (vid & BATADV_VLAN_HAS_TAG) {1097/* Traverse the VLAN/Ethertypes.1098*1099* At this point it is known that the first protocol is a VLAN1100* header, so start checking at the encapsulated protocol.1101*1102* The depth of the VLAN headers is recorded to drop BLA claim1103* frames encapsulated into multiple VLAN headers (QinQ).1104*/1105do {1106vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,1107&vhdr_buf);1108if (!vhdr)1109return false;11101111proto = vhdr->h_vlan_encapsulated_proto;1112headlen += VLAN_HLEN;1113vlan_depth++;1114} while (proto == htons(ETH_P_8021Q));1115}11161117if (proto != htons(ETH_P_ARP))1118return false; /* not a claim frame */11191120/* this must be a ARP frame. check if it is a claim. */11211122if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))1123return false;11241125/* pskb_may_pull() may have modified the pointers, get ethhdr again */1126ethhdr = eth_hdr(skb);1127arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);11281129/* Check whether the ARP frame carries a valid1130* IP information1131*/1132if (arphdr->ar_hrd != htons(ARPHRD_ETHER))1133return false;1134if (arphdr->ar_pro != htons(ETH_P_IP))1135return false;1136if (arphdr->ar_hln != ETH_ALEN)1137return false;1138if (arphdr->ar_pln != 4)1139return false;11401141hw_src = (u8 *)arphdr + sizeof(struct arphdr);1142hw_dst = hw_src + ETH_ALEN + 4;1143bla_dst = (struct batadv_bla_claim_dst *)hw_dst;1144bla_dst_own = &bat_priv->bla.claim_dest;11451146/* check if it is a claim frame in general */1147if (memcmp(bla_dst->magic, bla_dst_own->magic,1148sizeof(bla_dst->magic)) != 0)1149return false;11501151/* check if there is a claim frame encapsulated deeper in (QinQ) and1152* drop that, as this is not supported by BLA but should also not be1153* sent via the mesh.1154*/1155if (vlan_depth > 1)1156return true;11571158/* Let the loopdetect frames on the mesh in any case. */1159if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)1160return false;11611162/* check if it is a claim frame. */1163ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,1164ethhdr);1165if (ret == 1)1166batadv_dbg(BATADV_DBG_BLA, bat_priv,1167"%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",1168__func__, ethhdr->h_source, batadv_print_vid(vid),1169hw_src, hw_dst);11701171if (ret < 2)1172return !!ret;11731174/* become a backbone gw ourselves on this vlan if not happened yet */1175batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);11761177/* check for the different types of claim frames ... */1178switch (bla_dst->type) {1179case BATADV_CLAIM_TYPE_CLAIM:1180if (batadv_handle_claim(bat_priv, primary_if, hw_src,1181ethhdr->h_source, vid))1182return true;1183break;1184case BATADV_CLAIM_TYPE_UNCLAIM:1185if (batadv_handle_unclaim(bat_priv, primary_if,1186ethhdr->h_source, hw_src, vid))1187return true;1188break;11891190case BATADV_CLAIM_TYPE_ANNOUNCE:1191if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,1192vid))1193return true;1194break;1195case BATADV_CLAIM_TYPE_REQUEST:1196if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,1197vid))1198return true;1199break;1200}12011202batadv_dbg(BATADV_DBG_BLA, bat_priv,1203"%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",1204__func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,1205hw_dst);1206return true;1207}12081209/**1210* batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or1211* immediately1212* @bat_priv: the bat priv with all the mesh interface information1213* @now: whether the whole hash shall be wiped now1214*1215* Check when we last heard from other nodes, and remove them in case of1216* a time out, or clean all backbone gws if now is set.1217*/1218static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)1219{1220struct batadv_bla_backbone_gw *backbone_gw;1221struct hlist_node *node_tmp;1222struct hlist_head *head;1223struct batadv_hashtable *hash;1224spinlock_t *list_lock; /* protects write access to the hash lists */1225int i;12261227hash = bat_priv->bla.backbone_hash;1228if (!hash)1229return;12301231for (i = 0; i < hash->size; i++) {1232head = &hash->table[i];1233list_lock = &hash->list_locks[i];12341235spin_lock_bh(list_lock);1236hlist_for_each_entry_safe(backbone_gw, node_tmp,1237head, hash_entry) {1238if (now)1239goto purge_now;1240if (!batadv_has_timed_out(backbone_gw->lasttime,1241BATADV_BLA_BACKBONE_TIMEOUT))1242continue;12431244batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,1245"%s(): backbone gw %pM timed out\n",1246__func__, backbone_gw->orig);12471248purge_now:1249/* don't wait for the pending request anymore */1250if (atomic_read(&backbone_gw->request_sent))1251atomic_dec(&bat_priv->bla.num_requests);12521253batadv_bla_del_backbone_claims(backbone_gw);12541255hlist_del_rcu(&backbone_gw->hash_entry);1256batadv_backbone_gw_put(backbone_gw);1257}1258spin_unlock_bh(list_lock);1259}1260}12611262/**1263* batadv_bla_purge_claims() - Remove claims after a timeout or immediately1264* @bat_priv: the bat priv with all the mesh interface information1265* @primary_if: the selected primary interface, may be NULL if now is set1266* @now: whether the whole hash shall be wiped now1267*1268* Check when we heard last time from our own claims, and remove them in case of1269* a time out, or clean all claims if now is set1270*/1271static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,1272struct batadv_hard_iface *primary_if,1273int now)1274{1275struct batadv_bla_backbone_gw *backbone_gw;1276struct batadv_bla_claim *claim;1277struct hlist_head *head;1278struct batadv_hashtable *hash;1279int i;12801281hash = bat_priv->bla.claim_hash;1282if (!hash)1283return;12841285for (i = 0; i < hash->size; i++) {1286head = &hash->table[i];12871288rcu_read_lock();1289hlist_for_each_entry_rcu(claim, head, hash_entry) {1290backbone_gw = batadv_bla_claim_get_backbone_gw(claim);1291if (now)1292goto purge_now;12931294if (!batadv_compare_eth(backbone_gw->orig,1295primary_if->net_dev->dev_addr))1296goto skip;12971298if (!batadv_has_timed_out(claim->lasttime,1299BATADV_BLA_CLAIM_TIMEOUT))1300goto skip;13011302batadv_dbg(BATADV_DBG_BLA, bat_priv,1303"%s(): timed out.\n", __func__);13041305purge_now:1306batadv_dbg(BATADV_DBG_BLA, bat_priv,1307"%s(): %pM, vid %d\n", __func__,1308claim->addr, claim->vid);13091310batadv_handle_unclaim(bat_priv, primary_if,1311backbone_gw->orig,1312claim->addr, claim->vid);1313skip:1314batadv_backbone_gw_put(backbone_gw);1315}1316rcu_read_unlock();1317}1318}13191320/**1321* batadv_bla_update_orig_address() - Update the backbone gateways when the own1322* originator address changes1323* @bat_priv: the bat priv with all the mesh interface information1324* @primary_if: the new selected primary_if1325* @oldif: the old primary interface, may be NULL1326*/1327void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,1328struct batadv_hard_iface *primary_if,1329struct batadv_hard_iface *oldif)1330{1331struct batadv_bla_backbone_gw *backbone_gw;1332struct hlist_head *head;1333struct batadv_hashtable *hash;1334__be16 group;1335int i;13361337/* reset bridge loop avoidance group id */1338group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));1339bat_priv->bla.claim_dest.group = group;13401341/* purge everything when bridge loop avoidance is turned off */1342if (!atomic_read(&bat_priv->bridge_loop_avoidance))1343oldif = NULL;13441345if (!oldif) {1346batadv_bla_purge_claims(bat_priv, NULL, 1);1347batadv_bla_purge_backbone_gw(bat_priv, 1);1348return;1349}13501351hash = bat_priv->bla.backbone_hash;1352if (!hash)1353return;13541355for (i = 0; i < hash->size; i++) {1356head = &hash->table[i];13571358rcu_read_lock();1359hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {1360/* own orig still holds the old value. */1361if (!batadv_compare_eth(backbone_gw->orig,1362oldif->net_dev->dev_addr))1363continue;13641365ether_addr_copy(backbone_gw->orig,1366primary_if->net_dev->dev_addr);1367/* send an announce frame so others will ask for our1368* claims and update their tables.1369*/1370batadv_bla_send_announce(bat_priv, backbone_gw);1371}1372rcu_read_unlock();1373}1374}13751376/**1377* batadv_bla_send_loopdetect() - send a loopdetect frame1378* @bat_priv: the bat priv with all the mesh interface information1379* @backbone_gw: the backbone gateway for which a loop should be detected1380*1381* To detect loops that the bridge loop avoidance can't handle, send a loop1382* detection packet on the backbone. Unlike other BLA frames, this frame will1383* be allowed on the mesh by other nodes. If it is received on the mesh, this1384* indicates that there is a loop.1385*/1386static void1387batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,1388struct batadv_bla_backbone_gw *backbone_gw)1389{1390batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",1391backbone_gw->vid);1392batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,1393backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);1394}13951396/**1397* batadv_bla_status_update() - purge bla interfaces if necessary1398* @net_dev: the mesh interface net device1399*/1400void batadv_bla_status_update(struct net_device *net_dev)1401{1402struct batadv_priv *bat_priv = netdev_priv(net_dev);1403struct batadv_hard_iface *primary_if;14041405primary_if = batadv_primary_if_get_selected(bat_priv);1406if (!primary_if)1407return;14081409/* this function already purges everything when bla is disabled,1410* so just call that one.1411*/1412batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);1413batadv_hardif_put(primary_if);1414}14151416/**1417* batadv_bla_periodic_work() - performs periodic bla work1418* @work: kernel work struct1419*1420* periodic work to do:1421* * purge structures when they are too old1422* * send announcements1423*/1424static void batadv_bla_periodic_work(struct work_struct *work)1425{1426struct delayed_work *delayed_work;1427struct batadv_priv *bat_priv;1428struct batadv_priv_bla *priv_bla;1429struct hlist_head *head;1430struct batadv_bla_backbone_gw *backbone_gw;1431struct batadv_hashtable *hash;1432struct batadv_hard_iface *primary_if;1433bool send_loopdetect = false;1434int i;14351436delayed_work = to_delayed_work(work);1437priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);1438bat_priv = container_of(priv_bla, struct batadv_priv, bla);1439primary_if = batadv_primary_if_get_selected(bat_priv);1440if (!primary_if)1441goto out;14421443batadv_bla_purge_claims(bat_priv, primary_if, 0);1444batadv_bla_purge_backbone_gw(bat_priv, 0);14451446if (!atomic_read(&bat_priv->bridge_loop_avoidance))1447goto out;14481449if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {1450/* set a new random mac address for the next bridge loop1451* detection frames. Set the locally administered bit to avoid1452* collisions with users mac addresses.1453*/1454eth_random_addr(bat_priv->bla.loopdetect_addr);1455bat_priv->bla.loopdetect_addr[0] = 0xba;1456bat_priv->bla.loopdetect_addr[1] = 0xbe;1457bat_priv->bla.loopdetect_lasttime = jiffies;1458atomic_set(&bat_priv->bla.loopdetect_next,1459BATADV_BLA_LOOPDETECT_PERIODS);14601461/* mark for sending loop detect on all VLANs */1462send_loopdetect = true;1463}14641465hash = bat_priv->bla.backbone_hash;1466if (!hash)1467goto out;14681469for (i = 0; i < hash->size; i++) {1470head = &hash->table[i];14711472rcu_read_lock();1473hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {1474if (!batadv_compare_eth(backbone_gw->orig,1475primary_if->net_dev->dev_addr))1476continue;14771478backbone_gw->lasttime = jiffies;14791480batadv_bla_send_announce(bat_priv, backbone_gw);1481if (send_loopdetect)1482batadv_bla_send_loopdetect(bat_priv,1483backbone_gw);14841485/* request_sent is only set after creation to avoid1486* problems when we are not yet known as backbone gw1487* in the backbone.1488*1489* We can reset this now after we waited some periods1490* to give bridge forward delays and bla group forming1491* some grace time.1492*/14931494if (atomic_read(&backbone_gw->request_sent) == 0)1495continue;14961497if (!atomic_dec_and_test(&backbone_gw->wait_periods))1498continue;14991500atomic_dec(&backbone_gw->bat_priv->bla.num_requests);1501atomic_set(&backbone_gw->request_sent, 0);1502}1503rcu_read_unlock();1504}1505out:1506batadv_hardif_put(primary_if);15071508queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,1509msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));1510}15111512/* The hash for claim and backbone hash receive the same key because they1513* are getting initialized by hash_new with the same key. Reinitializing1514* them with to different keys to allow nested locking without generating1515* lockdep warnings1516*/1517static struct lock_class_key batadv_claim_hash_lock_class_key;1518static struct lock_class_key batadv_backbone_hash_lock_class_key;15191520/**1521* batadv_bla_init() - initialize all bla structures1522* @bat_priv: the bat priv with all the mesh interface information1523*1524* Return: 0 on success, < 0 on error.1525*/1526int batadv_bla_init(struct batadv_priv *bat_priv)1527{1528int i;1529u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};1530struct batadv_hard_iface *primary_if;1531u16 crc;1532unsigned long entrytime;15331534spin_lock_init(&bat_priv->bla.bcast_duplist_lock);15351536batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");15371538/* setting claim destination address */1539memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);1540bat_priv->bla.claim_dest.type = 0;1541primary_if = batadv_primary_if_get_selected(bat_priv);1542if (primary_if) {1543crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);1544bat_priv->bla.claim_dest.group = htons(crc);1545batadv_hardif_put(primary_if);1546} else {1547bat_priv->bla.claim_dest.group = 0; /* will be set later */1548}15491550/* initialize the duplicate list */1551entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);1552for (i = 0; i < BATADV_DUPLIST_SIZE; i++)1553bat_priv->bla.bcast_duplist[i].entrytime = entrytime;1554bat_priv->bla.bcast_duplist_curr = 0;15551556atomic_set(&bat_priv->bla.loopdetect_next,1557BATADV_BLA_LOOPDETECT_PERIODS);15581559if (bat_priv->bla.claim_hash)1560return 0;15611562bat_priv->bla.claim_hash = batadv_hash_new(128);1563if (!bat_priv->bla.claim_hash)1564return -ENOMEM;15651566bat_priv->bla.backbone_hash = batadv_hash_new(32);1567if (!bat_priv->bla.backbone_hash) {1568batadv_hash_destroy(bat_priv->bla.claim_hash);1569return -ENOMEM;1570}15711572batadv_hash_set_lock_class(bat_priv->bla.claim_hash,1573&batadv_claim_hash_lock_class_key);1574batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,1575&batadv_backbone_hash_lock_class_key);15761577batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");15781579INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);15801581queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,1582msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));1583return 0;1584}15851586/**1587* batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.1588* @bat_priv: the bat priv with all the mesh interface information1589* @skb: contains the multicast packet to be checked1590* @payload_offset: offset in the skb, marking the start of the data to be CRC'ed1591* @orig: originator mac address, NULL if unknown1592*1593* Check if it is on our broadcast list. Another gateway might have sent the1594* same packet because it is connected to the same backbone, so we have to1595* remove this duplicate.1596*1597* This is performed by checking the CRC, which will tell us1598* with a good chance that it is the same packet. If it is furthermore1599* sent by another host, drop it. We allow equal packets from1600* the same host however as this might be intended.1601*1602* Return: true if a packet is in the duplicate list, false otherwise.1603*/1604static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,1605struct sk_buff *skb, int payload_offset,1606const u8 *orig)1607{1608struct batadv_bcast_duplist_entry *entry;1609bool ret = false;1610int payload_len;1611int i, curr;1612u32 crc;16131614/* calculate the crc ... */1615payload_len = skb->len - payload_offset;1616crc = skb_crc32c(skb, payload_offset, payload_len, 0);16171618spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);16191620for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {1621curr = (bat_priv->bla.bcast_duplist_curr + i);1622curr %= BATADV_DUPLIST_SIZE;1623entry = &bat_priv->bla.bcast_duplist[curr];16241625/* we can stop searching if the entry is too old ;1626* later entries will be even older1627*/1628if (batadv_has_timed_out(entry->entrytime,1629BATADV_DUPLIST_TIMEOUT))1630break;16311632if (entry->crc != crc)1633continue;16341635/* are the originators both known and not anonymous? */1636if (orig && !is_zero_ether_addr(orig) &&1637!is_zero_ether_addr(entry->orig)) {1638/* If known, check if the new frame came from1639* the same originator:1640* We are safe to take identical frames from the1641* same orig, if known, as multiplications in1642* the mesh are detected via the (orig, seqno) pair.1643* So we can be a bit more liberal here and allow1644* identical frames from the same orig which the source1645* host might have sent multiple times on purpose.1646*/1647if (batadv_compare_eth(entry->orig, orig))1648continue;1649}16501651/* this entry seems to match: same crc, not too old,1652* and from another gw. therefore return true to forbid it.1653*/1654ret = true;1655goto out;1656}1657/* not found, add a new entry (overwrite the oldest entry)1658* and allow it, its the first occurrence.1659*/1660curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);1661curr %= BATADV_DUPLIST_SIZE;1662entry = &bat_priv->bla.bcast_duplist[curr];1663entry->crc = crc;1664entry->entrytime = jiffies;16651666/* known originator */1667if (orig)1668ether_addr_copy(entry->orig, orig);1669/* anonymous originator */1670else1671eth_zero_addr(entry->orig);16721673bat_priv->bla.bcast_duplist_curr = curr;16741675out:1676spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);16771678return ret;1679}16801681/**1682* batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.1683* @bat_priv: the bat priv with all the mesh interface information1684* @skb: contains the multicast packet to be checked, decapsulated from a1685* unicast_packet1686*1687* Check if it is on our broadcast list. Another gateway might have sent the1688* same packet because it is connected to the same backbone, so we have to1689* remove this duplicate.1690*1691* Return: true if a packet is in the duplicate list, false otherwise.1692*/1693static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,1694struct sk_buff *skb)1695{1696return batadv_bla_check_duplist(bat_priv, skb, 0, NULL);1697}16981699/**1700* batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.1701* @bat_priv: the bat priv with all the mesh interface information1702* @skb: contains the bcast_packet to be checked1703*1704* Check if it is on our broadcast list. Another gateway might have sent the1705* same packet because it is connected to the same backbone, so we have to1706* remove this duplicate.1707*1708* Return: true if a packet is in the duplicate list, false otherwise.1709*/1710bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,1711struct sk_buff *skb)1712{1713struct batadv_bcast_packet *bcast_packet;17141715bcast_packet = (struct batadv_bcast_packet *)skb->data;17161717return batadv_bla_check_duplist(bat_priv, skb, sizeof(*bcast_packet),1718bcast_packet->orig);1719}17201721/**1722* batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for1723* the VLAN identified by vid.1724* @bat_priv: the bat priv with all the mesh interface information1725* @orig: originator mac address1726* @vid: VLAN identifier1727*1728* Return: true if orig is a backbone for this vid, false otherwise.1729*/1730bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,1731unsigned short vid)1732{1733struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;1734struct hlist_head *head;1735struct batadv_bla_backbone_gw *backbone_gw;1736int i;17371738if (!atomic_read(&bat_priv->bridge_loop_avoidance))1739return false;17401741if (!hash)1742return false;17431744for (i = 0; i < hash->size; i++) {1745head = &hash->table[i];17461747rcu_read_lock();1748hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {1749if (batadv_compare_eth(backbone_gw->orig, orig) &&1750backbone_gw->vid == vid) {1751rcu_read_unlock();1752return true;1753}1754}1755rcu_read_unlock();1756}17571758return false;1759}17601761/**1762* batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN1763* @skb: the frame to be checked1764* @orig_node: the orig_node of the frame1765* @hdr_size: maximum length of the frame1766*1767* Return: true if the orig_node is also a gateway on the mesh interface,1768* otherwise it returns false.1769*/1770bool batadv_bla_is_backbone_gw(struct sk_buff *skb,1771struct batadv_orig_node *orig_node, int hdr_size)1772{1773struct batadv_bla_backbone_gw *backbone_gw;1774unsigned short vid;17751776if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))1777return false;17781779/* first, find out the vid. */1780if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))1781return false;17821783vid = batadv_get_vid(skb, hdr_size);17841785/* see if this originator is a backbone gw for this VLAN */1786backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,1787orig_node->orig, vid);1788if (!backbone_gw)1789return false;17901791batadv_backbone_gw_put(backbone_gw);1792return true;1793}17941795/**1796* batadv_bla_free() - free all bla structures1797* @bat_priv: the bat priv with all the mesh interface information1798*1799* for meshinterface free or module unload1800*/1801void batadv_bla_free(struct batadv_priv *bat_priv)1802{1803struct batadv_hard_iface *primary_if;18041805cancel_delayed_work_sync(&bat_priv->bla.work);1806primary_if = batadv_primary_if_get_selected(bat_priv);18071808if (bat_priv->bla.claim_hash) {1809batadv_bla_purge_claims(bat_priv, primary_if, 1);1810batadv_hash_destroy(bat_priv->bla.claim_hash);1811bat_priv->bla.claim_hash = NULL;1812}1813if (bat_priv->bla.backbone_hash) {1814batadv_bla_purge_backbone_gw(bat_priv, 1);1815batadv_hash_destroy(bat_priv->bla.backbone_hash);1816bat_priv->bla.backbone_hash = NULL;1817}1818batadv_hardif_put(primary_if);1819}18201821/**1822* batadv_bla_loopdetect_check() - check and handle a detected loop1823* @bat_priv: the bat priv with all the mesh interface information1824* @skb: the packet to check1825* @primary_if: interface where the request came on1826* @vid: the VLAN ID of the frame1827*1828* Checks if this packet is a loop detect frame which has been sent by us,1829* throws an uevent and logs the event if that is the case.1830*1831* Return: true if it is a loop detect frame which is to be dropped, false1832* otherwise.1833*/1834static bool1835batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,1836struct batadv_hard_iface *primary_if,1837unsigned short vid)1838{1839struct batadv_bla_backbone_gw *backbone_gw;1840struct ethhdr *ethhdr;1841bool ret;18421843ethhdr = eth_hdr(skb);18441845/* Only check for the MAC address and skip more checks here for1846* performance reasons - this function is on the hotpath, after all.1847*/1848if (!batadv_compare_eth(ethhdr->h_source,1849bat_priv->bla.loopdetect_addr))1850return false;18511852/* If the packet came too late, don't forward it on the mesh1853* but don't consider that as loop. It might be a coincidence.1854*/1855if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,1856BATADV_BLA_LOOPDETECT_TIMEOUT))1857return true;18581859backbone_gw = batadv_bla_get_backbone_gw(bat_priv,1860primary_if->net_dev->dev_addr,1861vid, true);1862if (unlikely(!backbone_gw))1863return true;18641865ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);18661867/* backbone_gw is unreferenced in the report work function1868* if queue_work() call was successful1869*/1870if (!ret)1871batadv_backbone_gw_put(backbone_gw);18721873return true;1874}18751876/**1877* batadv_bla_rx() - check packets coming from the mesh.1878* @bat_priv: the bat priv with all the mesh interface information1879* @skb: the frame to be checked1880* @vid: the VLAN ID of the frame1881* @packet_type: the batman packet type this frame came in1882*1883* batadv_bla_rx avoidance checks if:1884* * we have to race for a claim1885* * if the frame is allowed on the LAN1886*1887* In these cases, the skb is further handled by this function1888*1889* Return: true if handled, otherwise it returns false and the caller shall1890* further process the skb.1891*/1892bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,1893unsigned short vid, int packet_type)1894{1895struct batadv_bla_backbone_gw *backbone_gw;1896struct ethhdr *ethhdr;1897struct batadv_bla_claim search_claim, *claim = NULL;1898struct batadv_hard_iface *primary_if;1899bool own_claim;1900bool ret;19011902ethhdr = eth_hdr(skb);19031904primary_if = batadv_primary_if_get_selected(bat_priv);1905if (!primary_if)1906goto handled;19071908if (!atomic_read(&bat_priv->bridge_loop_avoidance))1909goto allow;19101911if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))1912goto handled;19131914if (unlikely(atomic_read(&bat_priv->bla.num_requests)))1915/* don't allow multicast packets while requests are in flight */1916if (is_multicast_ether_addr(ethhdr->h_dest))1917/* Both broadcast flooding or multicast-via-unicasts1918* delivery might send to multiple backbone gateways1919* sharing the same LAN and therefore need to coordinate1920* which backbone gateway forwards into the LAN,1921* by claiming the payload source address.1922*1923* Broadcast flooding and multicast-via-unicasts1924* delivery use the following two batman packet types.1925* Note: explicitly exclude BATADV_UNICAST_4ADDR,1926* as the DHCP gateway feature will send explicitly1927* to only one BLA gateway, so the claiming process1928* should be avoided there.1929*/1930if (packet_type == BATADV_BCAST ||1931packet_type == BATADV_UNICAST)1932goto handled;19331934/* potential duplicates from foreign BLA backbone gateways via1935* multicast-in-unicast packets1936*/1937if (is_multicast_ether_addr(ethhdr->h_dest) &&1938packet_type == BATADV_UNICAST &&1939batadv_bla_check_ucast_duplist(bat_priv, skb))1940goto handled;19411942ether_addr_copy(search_claim.addr, ethhdr->h_source);1943search_claim.vid = vid;1944claim = batadv_claim_hash_find(bat_priv, &search_claim);19451946if (!claim) {1947bool local = batadv_is_my_client(bat_priv, ethhdr->h_source, vid);19481949/* possible optimization: race for a claim */1950/* No claim exists yet, claim it for us!1951*/19521953batadv_dbg(BATADV_DBG_BLA, bat_priv,1954"%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",1955__func__, ethhdr->h_source, str_yes_no(local));1956batadv_handle_claim(bat_priv, primary_if,1957primary_if->net_dev->dev_addr,1958ethhdr->h_source, vid);1959goto allow;1960}19611962/* if it is our own claim ... */1963backbone_gw = batadv_bla_claim_get_backbone_gw(claim);1964own_claim = batadv_compare_eth(backbone_gw->orig,1965primary_if->net_dev->dev_addr);1966batadv_backbone_gw_put(backbone_gw);19671968if (own_claim) {1969/* ... allow it in any case */1970claim->lasttime = jiffies;1971goto allow;1972}19731974/* if it is a multicast ... */1975if (is_multicast_ether_addr(ethhdr->h_dest) &&1976(packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {1977/* ... drop it. the responsible gateway is in charge.1978*1979* We need to check packet type because with the gateway1980* feature, broadcasts (like DHCP requests) may be sent1981* using a unicast 4 address packet type. See comment above.1982*/1983goto handled;1984} else {1985/* seems the client considers us as its best gateway.1986* send a claim and update the claim table1987* immediately.1988*/1989batadv_handle_claim(bat_priv, primary_if,1990primary_if->net_dev->dev_addr,1991ethhdr->h_source, vid);1992goto allow;1993}1994allow:1995batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);1996ret = false;1997goto out;19981999handled:2000kfree_skb(skb);2001ret = true;20022003out:2004batadv_hardif_put(primary_if);2005batadv_claim_put(claim);2006return ret;2007}20082009/**2010* batadv_bla_tx() - check packets going into the mesh2011* @bat_priv: the bat priv with all the mesh interface information2012* @skb: the frame to be checked2013* @vid: the VLAN ID of the frame2014*2015* batadv_bla_tx checks if:2016* * a claim was received which has to be processed2017* * the frame is allowed on the mesh2018*2019* in these cases, the skb is further handled by this function.2020*2021* This call might reallocate skb data.2022*2023* Return: true if handled, otherwise it returns false and the caller shall2024* further process the skb.2025*/2026bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,2027unsigned short vid)2028{2029struct ethhdr *ethhdr;2030struct batadv_bla_claim search_claim, *claim = NULL;2031struct batadv_bla_backbone_gw *backbone_gw;2032struct batadv_hard_iface *primary_if;2033bool client_roamed;2034bool ret = false;20352036primary_if = batadv_primary_if_get_selected(bat_priv);2037if (!primary_if)2038goto out;20392040if (!atomic_read(&bat_priv->bridge_loop_avoidance))2041goto allow;20422043if (batadv_bla_process_claim(bat_priv, primary_if, skb))2044goto handled;20452046ethhdr = eth_hdr(skb);20472048if (unlikely(atomic_read(&bat_priv->bla.num_requests)))2049/* don't allow broadcasts while requests are in flight */2050if (is_multicast_ether_addr(ethhdr->h_dest))2051goto handled;20522053ether_addr_copy(search_claim.addr, ethhdr->h_source);2054search_claim.vid = vid;20552056claim = batadv_claim_hash_find(bat_priv, &search_claim);20572058/* if no claim exists, allow it. */2059if (!claim)2060goto allow;20612062/* check if we are responsible. */2063backbone_gw = batadv_bla_claim_get_backbone_gw(claim);2064client_roamed = batadv_compare_eth(backbone_gw->orig,2065primary_if->net_dev->dev_addr);2066batadv_backbone_gw_put(backbone_gw);20672068if (client_roamed) {2069/* if yes, the client has roamed and we have2070* to unclaim it.2071*/2072if (batadv_has_timed_out(claim->lasttime, 100)) {2073/* only unclaim if the last claim entry is2074* older than 100 ms to make sure we really2075* have a roaming client here.2076*/2077batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",2078__func__, ethhdr->h_source);2079batadv_handle_unclaim(bat_priv, primary_if,2080primary_if->net_dev->dev_addr,2081ethhdr->h_source, vid);2082goto allow;2083} else {2084batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",2085__func__, ethhdr->h_source);2086goto handled;2087}2088}20892090/* check if it is a multicast/broadcast frame */2091if (is_multicast_ether_addr(ethhdr->h_dest)) {2092/* drop it. the responsible gateway has forwarded it into2093* the backbone network.2094*/2095goto handled;2096} else {2097/* we must allow it. at least if we are2098* responsible for the DESTINATION.2099*/2100goto allow;2101}2102allow:2103batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);2104ret = false;2105goto out;2106handled:2107ret = true;2108out:2109batadv_hardif_put(primary_if);2110batadv_claim_put(claim);2111return ret;2112}21132114/**2115* batadv_bla_claim_dump_entry() - dump one entry of the claim table2116* to a netlink socket2117* @msg: buffer for the message2118* @portid: netlink port2119* @cb: Control block containing additional options2120* @primary_if: primary interface2121* @claim: entry to dump2122*2123* Return: 0 or error code.2124*/2125static int2126batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,2127struct netlink_callback *cb,2128struct batadv_hard_iface *primary_if,2129struct batadv_bla_claim *claim)2130{2131const u8 *primary_addr = primary_if->net_dev->dev_addr;2132u16 backbone_crc;2133bool is_own;2134void *hdr;2135int ret = -EINVAL;21362137hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,2138&batadv_netlink_family, NLM_F_MULTI,2139BATADV_CMD_GET_BLA_CLAIM);2140if (!hdr) {2141ret = -ENOBUFS;2142goto out;2143}21442145genl_dump_check_consistent(cb, hdr);21462147is_own = batadv_compare_eth(claim->backbone_gw->orig,2148primary_addr);21492150spin_lock_bh(&claim->backbone_gw->crc_lock);2151backbone_crc = claim->backbone_gw->crc;2152spin_unlock_bh(&claim->backbone_gw->crc_lock);21532154if (is_own)2155if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {2156genlmsg_cancel(msg, hdr);2157goto out;2158}21592160if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||2161nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||2162nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,2163claim->backbone_gw->orig) ||2164nla_put_u16(msg, BATADV_ATTR_BLA_CRC,2165backbone_crc)) {2166genlmsg_cancel(msg, hdr);2167goto out;2168}21692170genlmsg_end(msg, hdr);2171ret = 0;21722173out:2174return ret;2175}21762177/**2178* batadv_bla_claim_dump_bucket() - dump one bucket of the claim table2179* to a netlink socket2180* @msg: buffer for the message2181* @portid: netlink port2182* @cb: Control block containing additional options2183* @primary_if: primary interface2184* @hash: hash to dump2185* @bucket: bucket index to dump2186* @idx_skip: How many entries to skip2187*2188* Return: always 0.2189*/2190static int2191batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid,2192struct netlink_callback *cb,2193struct batadv_hard_iface *primary_if,2194struct batadv_hashtable *hash, unsigned int bucket,2195int *idx_skip)2196{2197struct batadv_bla_claim *claim;2198int idx = 0;2199int ret = 0;22002201spin_lock_bh(&hash->list_locks[bucket]);2202cb->seq = atomic_read(&hash->generation) << 1 | 1;22032204hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) {2205if (idx++ < *idx_skip)2206continue;22072208ret = batadv_bla_claim_dump_entry(msg, portid, cb,2209primary_if, claim);2210if (ret) {2211*idx_skip = idx - 1;2212goto unlock;2213}2214}22152216*idx_skip = 0;2217unlock:2218spin_unlock_bh(&hash->list_locks[bucket]);2219return ret;2220}22212222/**2223* batadv_bla_claim_dump() - dump claim table to a netlink socket2224* @msg: buffer for the message2225* @cb: callback structure containing arguments2226*2227* Return: message length.2228*/2229int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)2230{2231struct batadv_hard_iface *primary_if = NULL;2232int portid = NETLINK_CB(cb->skb).portid;2233struct net_device *mesh_iface;2234struct batadv_hashtable *hash;2235struct batadv_priv *bat_priv;2236int bucket = cb->args[0];2237int idx = cb->args[1];2238int ret = 0;22392240mesh_iface = batadv_netlink_get_meshif(cb);2241if (IS_ERR(mesh_iface))2242return PTR_ERR(mesh_iface);22432244bat_priv = netdev_priv(mesh_iface);2245hash = bat_priv->bla.claim_hash;22462247primary_if = batadv_primary_if_get_selected(bat_priv);2248if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {2249ret = -ENOENT;2250goto out;2251}22522253while (bucket < hash->size) {2254if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if,2255hash, bucket, &idx))2256break;2257bucket++;2258}22592260cb->args[0] = bucket;2261cb->args[1] = idx;22622263ret = msg->len;22642265out:2266batadv_hardif_put(primary_if);22672268dev_put(mesh_iface);22692270return ret;2271}22722273/**2274* batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a2275* netlink socket2276* @msg: buffer for the message2277* @portid: netlink port2278* @cb: Control block containing additional options2279* @primary_if: primary interface2280* @backbone_gw: entry to dump2281*2282* Return: 0 or error code.2283*/2284static int2285batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,2286struct netlink_callback *cb,2287struct batadv_hard_iface *primary_if,2288struct batadv_bla_backbone_gw *backbone_gw)2289{2290const u8 *primary_addr = primary_if->net_dev->dev_addr;2291u16 backbone_crc;2292bool is_own;2293int msecs;2294void *hdr;2295int ret = -EINVAL;22962297hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,2298&batadv_netlink_family, NLM_F_MULTI,2299BATADV_CMD_GET_BLA_BACKBONE);2300if (!hdr) {2301ret = -ENOBUFS;2302goto out;2303}23042305genl_dump_check_consistent(cb, hdr);23062307is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);23082309spin_lock_bh(&backbone_gw->crc_lock);2310backbone_crc = backbone_gw->crc;2311spin_unlock_bh(&backbone_gw->crc_lock);23122313msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);23142315if (is_own)2316if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {2317genlmsg_cancel(msg, hdr);2318goto out;2319}23202321if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,2322backbone_gw->orig) ||2323nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||2324nla_put_u16(msg, BATADV_ATTR_BLA_CRC,2325backbone_crc) ||2326nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {2327genlmsg_cancel(msg, hdr);2328goto out;2329}23302331genlmsg_end(msg, hdr);2332ret = 0;23332334out:2335return ret;2336}23372338/**2339* batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to2340* a netlink socket2341* @msg: buffer for the message2342* @portid: netlink port2343* @cb: Control block containing additional options2344* @primary_if: primary interface2345* @hash: hash to dump2346* @bucket: bucket index to dump2347* @idx_skip: How many entries to skip2348*2349* Return: always 0.2350*/2351static int2352batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid,2353struct netlink_callback *cb,2354struct batadv_hard_iface *primary_if,2355struct batadv_hashtable *hash,2356unsigned int bucket, int *idx_skip)2357{2358struct batadv_bla_backbone_gw *backbone_gw;2359int idx = 0;2360int ret = 0;23612362spin_lock_bh(&hash->list_locks[bucket]);2363cb->seq = atomic_read(&hash->generation) << 1 | 1;23642365hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) {2366if (idx++ < *idx_skip)2367continue;23682369ret = batadv_bla_backbone_dump_entry(msg, portid, cb,2370primary_if, backbone_gw);2371if (ret) {2372*idx_skip = idx - 1;2373goto unlock;2374}2375}23762377*idx_skip = 0;2378unlock:2379spin_unlock_bh(&hash->list_locks[bucket]);2380return ret;2381}23822383/**2384* batadv_bla_backbone_dump() - dump backbone table to a netlink socket2385* @msg: buffer for the message2386* @cb: callback structure containing arguments2387*2388* Return: message length.2389*/2390int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)2391{2392struct batadv_hard_iface *primary_if = NULL;2393int portid = NETLINK_CB(cb->skb).portid;2394struct net_device *mesh_iface;2395struct batadv_hashtable *hash;2396struct batadv_priv *bat_priv;2397int bucket = cb->args[0];2398int idx = cb->args[1];2399int ret = 0;24002401mesh_iface = batadv_netlink_get_meshif(cb);2402if (IS_ERR(mesh_iface))2403return PTR_ERR(mesh_iface);24042405bat_priv = netdev_priv(mesh_iface);2406hash = bat_priv->bla.backbone_hash;24072408primary_if = batadv_primary_if_get_selected(bat_priv);2409if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {2410ret = -ENOENT;2411goto out;2412}24132414while (bucket < hash->size) {2415if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if,2416hash, bucket, &idx))2417break;2418bucket++;2419}24202421cb->args[0] = bucket;2422cb->args[1] = idx;24232424ret = msg->len;24252426out:2427batadv_hardif_put(primary_if);24282429dev_put(mesh_iface);24302431return ret;2432}24332434#ifdef CONFIG_BATMAN_ADV_DAT2435/**2436* batadv_bla_check_claim() - check if address is claimed2437*2438* @bat_priv: the bat priv with all the mesh interface information2439* @addr: mac address of which the claim status is checked2440* @vid: the VLAN ID2441*2442* addr is checked if this address is claimed by the local device itself.2443*2444* Return: true if bla is disabled or the mac is claimed by the device,2445* false if the device addr is already claimed by another gateway2446*/2447bool batadv_bla_check_claim(struct batadv_priv *bat_priv,2448u8 *addr, unsigned short vid)2449{2450struct batadv_bla_claim search_claim;2451struct batadv_bla_claim *claim = NULL;2452struct batadv_hard_iface *primary_if = NULL;2453bool ret = true;24542455if (!atomic_read(&bat_priv->bridge_loop_avoidance))2456return ret;24572458primary_if = batadv_primary_if_get_selected(bat_priv);2459if (!primary_if)2460return ret;24612462/* First look if the mac address is claimed */2463ether_addr_copy(search_claim.addr, addr);2464search_claim.vid = vid;24652466claim = batadv_claim_hash_find(bat_priv, &search_claim);24672468/* If there is a claim and we are not owner of the claim,2469* return false.2470*/2471if (claim) {2472if (!batadv_compare_eth(claim->backbone_gw->orig,2473primary_if->net_dev->dev_addr))2474ret = false;2475batadv_claim_put(claim);2476}24772478batadv_hardif_put(primary_if);2479return ret;2480}2481#endif248224832484