Path: blob/master/net/batman-adv/bridge_loop_avoidance.c
26285 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_ptr: pointer to position inside the head buffer of the skb1591* marking the start of the data to be CRC'ed1592* @orig: originator mac address, NULL if unknown1593*1594* Check if it is on our broadcast list. Another gateway might have sent the1595* same packet because it is connected to the same backbone, so we have to1596* remove this duplicate.1597*1598* This is performed by checking the CRC, which will tell us1599* with a good chance that it is the same packet. If it is furthermore1600* sent by another host, drop it. We allow equal packets from1601* the same host however as this might be intended.1602*1603* Return: true if a packet is in the duplicate list, false otherwise.1604*/1605static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,1606struct sk_buff *skb, u8 *payload_ptr,1607const u8 *orig)1608{1609struct batadv_bcast_duplist_entry *entry;1610bool ret = false;1611int i, curr;1612__be32 crc;16131614/* calculate the crc ... */1615crc = batadv_skb_crc32(skb, payload_ptr);16161617spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);16181619for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {1620curr = (bat_priv->bla.bcast_duplist_curr + i);1621curr %= BATADV_DUPLIST_SIZE;1622entry = &bat_priv->bla.bcast_duplist[curr];16231624/* we can stop searching if the entry is too old ;1625* later entries will be even older1626*/1627if (batadv_has_timed_out(entry->entrytime,1628BATADV_DUPLIST_TIMEOUT))1629break;16301631if (entry->crc != crc)1632continue;16331634/* are the originators both known and not anonymous? */1635if (orig && !is_zero_ether_addr(orig) &&1636!is_zero_ether_addr(entry->orig)) {1637/* If known, check if the new frame came from1638* the same originator:1639* We are safe to take identical frames from the1640* same orig, if known, as multiplications in1641* the mesh are detected via the (orig, seqno) pair.1642* So we can be a bit more liberal here and allow1643* identical frames from the same orig which the source1644* host might have sent multiple times on purpose.1645*/1646if (batadv_compare_eth(entry->orig, orig))1647continue;1648}16491650/* this entry seems to match: same crc, not too old,1651* and from another gw. therefore return true to forbid it.1652*/1653ret = true;1654goto out;1655}1656/* not found, add a new entry (overwrite the oldest entry)1657* and allow it, its the first occurrence.1658*/1659curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);1660curr %= BATADV_DUPLIST_SIZE;1661entry = &bat_priv->bla.bcast_duplist[curr];1662entry->crc = crc;1663entry->entrytime = jiffies;16641665/* known originator */1666if (orig)1667ether_addr_copy(entry->orig, orig);1668/* anonymous originator */1669else1670eth_zero_addr(entry->orig);16711672bat_priv->bla.bcast_duplist_curr = curr;16731674out:1675spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);16761677return ret;1678}16791680/**1681* batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.1682* @bat_priv: the bat priv with all the mesh interface information1683* @skb: contains the multicast packet to be checked, decapsulated from a1684* unicast_packet1685*1686* Check if it is on our broadcast list. Another gateway might have sent the1687* same packet because it is connected to the same backbone, so we have to1688* remove this duplicate.1689*1690* Return: true if a packet is in the duplicate list, false otherwise.1691*/1692static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,1693struct sk_buff *skb)1694{1695return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);1696}16971698/**1699* batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.1700* @bat_priv: the bat priv with all the mesh interface information1701* @skb: contains the bcast_packet to be checked1702*1703* Check if it is on our broadcast list. Another gateway might have sent the1704* same packet because it is connected to the same backbone, so we have to1705* remove this duplicate.1706*1707* Return: true if a packet is in the duplicate list, false otherwise.1708*/1709bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,1710struct sk_buff *skb)1711{1712struct batadv_bcast_packet *bcast_packet;1713u8 *payload_ptr;17141715bcast_packet = (struct batadv_bcast_packet *)skb->data;1716payload_ptr = (u8 *)(bcast_packet + 1);17171718return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,1719bcast_packet->orig);1720}17211722/**1723* batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for1724* the VLAN identified by vid.1725* @bat_priv: the bat priv with all the mesh interface information1726* @orig: originator mac address1727* @vid: VLAN identifier1728*1729* Return: true if orig is a backbone for this vid, false otherwise.1730*/1731bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,1732unsigned short vid)1733{1734struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;1735struct hlist_head *head;1736struct batadv_bla_backbone_gw *backbone_gw;1737int i;17381739if (!atomic_read(&bat_priv->bridge_loop_avoidance))1740return false;17411742if (!hash)1743return false;17441745for (i = 0; i < hash->size; i++) {1746head = &hash->table[i];17471748rcu_read_lock();1749hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {1750if (batadv_compare_eth(backbone_gw->orig, orig) &&1751backbone_gw->vid == vid) {1752rcu_read_unlock();1753return true;1754}1755}1756rcu_read_unlock();1757}17581759return false;1760}17611762/**1763* batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN1764* @skb: the frame to be checked1765* @orig_node: the orig_node of the frame1766* @hdr_size: maximum length of the frame1767*1768* Return: true if the orig_node is also a gateway on the mesh interface,1769* otherwise it returns false.1770*/1771bool batadv_bla_is_backbone_gw(struct sk_buff *skb,1772struct batadv_orig_node *orig_node, int hdr_size)1773{1774struct batadv_bla_backbone_gw *backbone_gw;1775unsigned short vid;17761777if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))1778return false;17791780/* first, find out the vid. */1781if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))1782return false;17831784vid = batadv_get_vid(skb, hdr_size);17851786/* see if this originator is a backbone gw for this VLAN */1787backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,1788orig_node->orig, vid);1789if (!backbone_gw)1790return false;17911792batadv_backbone_gw_put(backbone_gw);1793return true;1794}17951796/**1797* batadv_bla_free() - free all bla structures1798* @bat_priv: the bat priv with all the mesh interface information1799*1800* for meshinterface free or module unload1801*/1802void batadv_bla_free(struct batadv_priv *bat_priv)1803{1804struct batadv_hard_iface *primary_if;18051806cancel_delayed_work_sync(&bat_priv->bla.work);1807primary_if = batadv_primary_if_get_selected(bat_priv);18081809if (bat_priv->bla.claim_hash) {1810batadv_bla_purge_claims(bat_priv, primary_if, 1);1811batadv_hash_destroy(bat_priv->bla.claim_hash);1812bat_priv->bla.claim_hash = NULL;1813}1814if (bat_priv->bla.backbone_hash) {1815batadv_bla_purge_backbone_gw(bat_priv, 1);1816batadv_hash_destroy(bat_priv->bla.backbone_hash);1817bat_priv->bla.backbone_hash = NULL;1818}1819batadv_hardif_put(primary_if);1820}18211822/**1823* batadv_bla_loopdetect_check() - check and handle a detected loop1824* @bat_priv: the bat priv with all the mesh interface information1825* @skb: the packet to check1826* @primary_if: interface where the request came on1827* @vid: the VLAN ID of the frame1828*1829* Checks if this packet is a loop detect frame which has been sent by us,1830* throws an uevent and logs the event if that is the case.1831*1832* Return: true if it is a loop detect frame which is to be dropped, false1833* otherwise.1834*/1835static bool1836batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,1837struct batadv_hard_iface *primary_if,1838unsigned short vid)1839{1840struct batadv_bla_backbone_gw *backbone_gw;1841struct ethhdr *ethhdr;1842bool ret;18431844ethhdr = eth_hdr(skb);18451846/* Only check for the MAC address and skip more checks here for1847* performance reasons - this function is on the hotpath, after all.1848*/1849if (!batadv_compare_eth(ethhdr->h_source,1850bat_priv->bla.loopdetect_addr))1851return false;18521853/* If the packet came too late, don't forward it on the mesh1854* but don't consider that as loop. It might be a coincidence.1855*/1856if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,1857BATADV_BLA_LOOPDETECT_TIMEOUT))1858return true;18591860backbone_gw = batadv_bla_get_backbone_gw(bat_priv,1861primary_if->net_dev->dev_addr,1862vid, true);1863if (unlikely(!backbone_gw))1864return true;18651866ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);18671868/* backbone_gw is unreferenced in the report work function1869* if queue_work() call was successful1870*/1871if (!ret)1872batadv_backbone_gw_put(backbone_gw);18731874return true;1875}18761877/**1878* batadv_bla_rx() - check packets coming from the mesh.1879* @bat_priv: the bat priv with all the mesh interface information1880* @skb: the frame to be checked1881* @vid: the VLAN ID of the frame1882* @packet_type: the batman packet type this frame came in1883*1884* batadv_bla_rx avoidance checks if:1885* * we have to race for a claim1886* * if the frame is allowed on the LAN1887*1888* In these cases, the skb is further handled by this function1889*1890* Return: true if handled, otherwise it returns false and the caller shall1891* further process the skb.1892*/1893bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,1894unsigned short vid, int packet_type)1895{1896struct batadv_bla_backbone_gw *backbone_gw;1897struct ethhdr *ethhdr;1898struct batadv_bla_claim search_claim, *claim = NULL;1899struct batadv_hard_iface *primary_if;1900bool own_claim;1901bool ret;19021903ethhdr = eth_hdr(skb);19041905primary_if = batadv_primary_if_get_selected(bat_priv);1906if (!primary_if)1907goto handled;19081909if (!atomic_read(&bat_priv->bridge_loop_avoidance))1910goto allow;19111912if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))1913goto handled;19141915if (unlikely(atomic_read(&bat_priv->bla.num_requests)))1916/* don't allow multicast packets while requests are in flight */1917if (is_multicast_ether_addr(ethhdr->h_dest))1918/* Both broadcast flooding or multicast-via-unicasts1919* delivery might send to multiple backbone gateways1920* sharing the same LAN and therefore need to coordinate1921* which backbone gateway forwards into the LAN,1922* by claiming the payload source address.1923*1924* Broadcast flooding and multicast-via-unicasts1925* delivery use the following two batman packet types.1926* Note: explicitly exclude BATADV_UNICAST_4ADDR,1927* as the DHCP gateway feature will send explicitly1928* to only one BLA gateway, so the claiming process1929* should be avoided there.1930*/1931if (packet_type == BATADV_BCAST ||1932packet_type == BATADV_UNICAST)1933goto handled;19341935/* potential duplicates from foreign BLA backbone gateways via1936* multicast-in-unicast packets1937*/1938if (is_multicast_ether_addr(ethhdr->h_dest) &&1939packet_type == BATADV_UNICAST &&1940batadv_bla_check_ucast_duplist(bat_priv, skb))1941goto handled;19421943ether_addr_copy(search_claim.addr, ethhdr->h_source);1944search_claim.vid = vid;1945claim = batadv_claim_hash_find(bat_priv, &search_claim);19461947if (!claim) {1948bool local = batadv_is_my_client(bat_priv, ethhdr->h_source, vid);19491950/* possible optimization: race for a claim */1951/* No claim exists yet, claim it for us!1952*/19531954batadv_dbg(BATADV_DBG_BLA, bat_priv,1955"%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",1956__func__, ethhdr->h_source, str_yes_no(local));1957batadv_handle_claim(bat_priv, primary_if,1958primary_if->net_dev->dev_addr,1959ethhdr->h_source, vid);1960goto allow;1961}19621963/* if it is our own claim ... */1964backbone_gw = batadv_bla_claim_get_backbone_gw(claim);1965own_claim = batadv_compare_eth(backbone_gw->orig,1966primary_if->net_dev->dev_addr);1967batadv_backbone_gw_put(backbone_gw);19681969if (own_claim) {1970/* ... allow it in any case */1971claim->lasttime = jiffies;1972goto allow;1973}19741975/* if it is a multicast ... */1976if (is_multicast_ether_addr(ethhdr->h_dest) &&1977(packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {1978/* ... drop it. the responsible gateway is in charge.1979*1980* We need to check packet type because with the gateway1981* feature, broadcasts (like DHCP requests) may be sent1982* using a unicast 4 address packet type. See comment above.1983*/1984goto handled;1985} else {1986/* seems the client considers us as its best gateway.1987* send a claim and update the claim table1988* immediately.1989*/1990batadv_handle_claim(bat_priv, primary_if,1991primary_if->net_dev->dev_addr,1992ethhdr->h_source, vid);1993goto allow;1994}1995allow:1996batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);1997ret = false;1998goto out;19992000handled:2001kfree_skb(skb);2002ret = true;20032004out:2005batadv_hardif_put(primary_if);2006batadv_claim_put(claim);2007return ret;2008}20092010/**2011* batadv_bla_tx() - check packets going into the mesh2012* @bat_priv: the bat priv with all the mesh interface information2013* @skb: the frame to be checked2014* @vid: the VLAN ID of the frame2015*2016* batadv_bla_tx checks if:2017* * a claim was received which has to be processed2018* * the frame is allowed on the mesh2019*2020* in these cases, the skb is further handled by this function.2021*2022* This call might reallocate skb data.2023*2024* Return: true if handled, otherwise it returns false and the caller shall2025* further process the skb.2026*/2027bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,2028unsigned short vid)2029{2030struct ethhdr *ethhdr;2031struct batadv_bla_claim search_claim, *claim = NULL;2032struct batadv_bla_backbone_gw *backbone_gw;2033struct batadv_hard_iface *primary_if;2034bool client_roamed;2035bool ret = false;20362037primary_if = batadv_primary_if_get_selected(bat_priv);2038if (!primary_if)2039goto out;20402041if (!atomic_read(&bat_priv->bridge_loop_avoidance))2042goto allow;20432044if (batadv_bla_process_claim(bat_priv, primary_if, skb))2045goto handled;20462047ethhdr = eth_hdr(skb);20482049if (unlikely(atomic_read(&bat_priv->bla.num_requests)))2050/* don't allow broadcasts while requests are in flight */2051if (is_multicast_ether_addr(ethhdr->h_dest))2052goto handled;20532054ether_addr_copy(search_claim.addr, ethhdr->h_source);2055search_claim.vid = vid;20562057claim = batadv_claim_hash_find(bat_priv, &search_claim);20582059/* if no claim exists, allow it. */2060if (!claim)2061goto allow;20622063/* check if we are responsible. */2064backbone_gw = batadv_bla_claim_get_backbone_gw(claim);2065client_roamed = batadv_compare_eth(backbone_gw->orig,2066primary_if->net_dev->dev_addr);2067batadv_backbone_gw_put(backbone_gw);20682069if (client_roamed) {2070/* if yes, the client has roamed and we have2071* to unclaim it.2072*/2073if (batadv_has_timed_out(claim->lasttime, 100)) {2074/* only unclaim if the last claim entry is2075* older than 100 ms to make sure we really2076* have a roaming client here.2077*/2078batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",2079__func__, ethhdr->h_source);2080batadv_handle_unclaim(bat_priv, primary_if,2081primary_if->net_dev->dev_addr,2082ethhdr->h_source, vid);2083goto allow;2084} else {2085batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",2086__func__, ethhdr->h_source);2087goto handled;2088}2089}20902091/* check if it is a multicast/broadcast frame */2092if (is_multicast_ether_addr(ethhdr->h_dest)) {2093/* drop it. the responsible gateway has forwarded it into2094* the backbone network.2095*/2096goto handled;2097} else {2098/* we must allow it. at least if we are2099* responsible for the DESTINATION.2100*/2101goto allow;2102}2103allow:2104batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);2105ret = false;2106goto out;2107handled:2108ret = true;2109out:2110batadv_hardif_put(primary_if);2111batadv_claim_put(claim);2112return ret;2113}21142115/**2116* batadv_bla_claim_dump_entry() - dump one entry of the claim table2117* to a netlink socket2118* @msg: buffer for the message2119* @portid: netlink port2120* @cb: Control block containing additional options2121* @primary_if: primary interface2122* @claim: entry to dump2123*2124* Return: 0 or error code.2125*/2126static int2127batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,2128struct netlink_callback *cb,2129struct batadv_hard_iface *primary_if,2130struct batadv_bla_claim *claim)2131{2132const u8 *primary_addr = primary_if->net_dev->dev_addr;2133u16 backbone_crc;2134bool is_own;2135void *hdr;2136int ret = -EINVAL;21372138hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,2139&batadv_netlink_family, NLM_F_MULTI,2140BATADV_CMD_GET_BLA_CLAIM);2141if (!hdr) {2142ret = -ENOBUFS;2143goto out;2144}21452146genl_dump_check_consistent(cb, hdr);21472148is_own = batadv_compare_eth(claim->backbone_gw->orig,2149primary_addr);21502151spin_lock_bh(&claim->backbone_gw->crc_lock);2152backbone_crc = claim->backbone_gw->crc;2153spin_unlock_bh(&claim->backbone_gw->crc_lock);21542155if (is_own)2156if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {2157genlmsg_cancel(msg, hdr);2158goto out;2159}21602161if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||2162nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||2163nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,2164claim->backbone_gw->orig) ||2165nla_put_u16(msg, BATADV_ATTR_BLA_CRC,2166backbone_crc)) {2167genlmsg_cancel(msg, hdr);2168goto out;2169}21702171genlmsg_end(msg, hdr);2172ret = 0;21732174out:2175return ret;2176}21772178/**2179* batadv_bla_claim_dump_bucket() - dump one bucket of the claim table2180* to a netlink socket2181* @msg: buffer for the message2182* @portid: netlink port2183* @cb: Control block containing additional options2184* @primary_if: primary interface2185* @hash: hash to dump2186* @bucket: bucket index to dump2187* @idx_skip: How many entries to skip2188*2189* Return: always 0.2190*/2191static int2192batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid,2193struct netlink_callback *cb,2194struct batadv_hard_iface *primary_if,2195struct batadv_hashtable *hash, unsigned int bucket,2196int *idx_skip)2197{2198struct batadv_bla_claim *claim;2199int idx = 0;2200int ret = 0;22012202spin_lock_bh(&hash->list_locks[bucket]);2203cb->seq = atomic_read(&hash->generation) << 1 | 1;22042205hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) {2206if (idx++ < *idx_skip)2207continue;22082209ret = batadv_bla_claim_dump_entry(msg, portid, cb,2210primary_if, claim);2211if (ret) {2212*idx_skip = idx - 1;2213goto unlock;2214}2215}22162217*idx_skip = 0;2218unlock:2219spin_unlock_bh(&hash->list_locks[bucket]);2220return ret;2221}22222223/**2224* batadv_bla_claim_dump() - dump claim table to a netlink socket2225* @msg: buffer for the message2226* @cb: callback structure containing arguments2227*2228* Return: message length.2229*/2230int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)2231{2232struct batadv_hard_iface *primary_if = NULL;2233int portid = NETLINK_CB(cb->skb).portid;2234struct net_device *mesh_iface;2235struct batadv_hashtable *hash;2236struct batadv_priv *bat_priv;2237int bucket = cb->args[0];2238int idx = cb->args[1];2239int ret = 0;22402241mesh_iface = batadv_netlink_get_meshif(cb);2242if (IS_ERR(mesh_iface))2243return PTR_ERR(mesh_iface);22442245bat_priv = netdev_priv(mesh_iface);2246hash = bat_priv->bla.claim_hash;22472248primary_if = batadv_primary_if_get_selected(bat_priv);2249if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {2250ret = -ENOENT;2251goto out;2252}22532254while (bucket < hash->size) {2255if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if,2256hash, bucket, &idx))2257break;2258bucket++;2259}22602261cb->args[0] = bucket;2262cb->args[1] = idx;22632264ret = msg->len;22652266out:2267batadv_hardif_put(primary_if);22682269dev_put(mesh_iface);22702271return ret;2272}22732274/**2275* batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a2276* netlink socket2277* @msg: buffer for the message2278* @portid: netlink port2279* @cb: Control block containing additional options2280* @primary_if: primary interface2281* @backbone_gw: entry to dump2282*2283* Return: 0 or error code.2284*/2285static int2286batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,2287struct netlink_callback *cb,2288struct batadv_hard_iface *primary_if,2289struct batadv_bla_backbone_gw *backbone_gw)2290{2291const u8 *primary_addr = primary_if->net_dev->dev_addr;2292u16 backbone_crc;2293bool is_own;2294int msecs;2295void *hdr;2296int ret = -EINVAL;22972298hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,2299&batadv_netlink_family, NLM_F_MULTI,2300BATADV_CMD_GET_BLA_BACKBONE);2301if (!hdr) {2302ret = -ENOBUFS;2303goto out;2304}23052306genl_dump_check_consistent(cb, hdr);23072308is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);23092310spin_lock_bh(&backbone_gw->crc_lock);2311backbone_crc = backbone_gw->crc;2312spin_unlock_bh(&backbone_gw->crc_lock);23132314msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);23152316if (is_own)2317if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {2318genlmsg_cancel(msg, hdr);2319goto out;2320}23212322if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,2323backbone_gw->orig) ||2324nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||2325nla_put_u16(msg, BATADV_ATTR_BLA_CRC,2326backbone_crc) ||2327nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {2328genlmsg_cancel(msg, hdr);2329goto out;2330}23312332genlmsg_end(msg, hdr);2333ret = 0;23342335out:2336return ret;2337}23382339/**2340* batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to2341* a netlink socket2342* @msg: buffer for the message2343* @portid: netlink port2344* @cb: Control block containing additional options2345* @primary_if: primary interface2346* @hash: hash to dump2347* @bucket: bucket index to dump2348* @idx_skip: How many entries to skip2349*2350* Return: always 0.2351*/2352static int2353batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid,2354struct netlink_callback *cb,2355struct batadv_hard_iface *primary_if,2356struct batadv_hashtable *hash,2357unsigned int bucket, int *idx_skip)2358{2359struct batadv_bla_backbone_gw *backbone_gw;2360int idx = 0;2361int ret = 0;23622363spin_lock_bh(&hash->list_locks[bucket]);2364cb->seq = atomic_read(&hash->generation) << 1 | 1;23652366hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) {2367if (idx++ < *idx_skip)2368continue;23692370ret = batadv_bla_backbone_dump_entry(msg, portid, cb,2371primary_if, backbone_gw);2372if (ret) {2373*idx_skip = idx - 1;2374goto unlock;2375}2376}23772378*idx_skip = 0;2379unlock:2380spin_unlock_bh(&hash->list_locks[bucket]);2381return ret;2382}23832384/**2385* batadv_bla_backbone_dump() - dump backbone table to a netlink socket2386* @msg: buffer for the message2387* @cb: callback structure containing arguments2388*2389* Return: message length.2390*/2391int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)2392{2393struct batadv_hard_iface *primary_if = NULL;2394int portid = NETLINK_CB(cb->skb).portid;2395struct net_device *mesh_iface;2396struct batadv_hashtable *hash;2397struct batadv_priv *bat_priv;2398int bucket = cb->args[0];2399int idx = cb->args[1];2400int ret = 0;24012402mesh_iface = batadv_netlink_get_meshif(cb);2403if (IS_ERR(mesh_iface))2404return PTR_ERR(mesh_iface);24052406bat_priv = netdev_priv(mesh_iface);2407hash = bat_priv->bla.backbone_hash;24082409primary_if = batadv_primary_if_get_selected(bat_priv);2410if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {2411ret = -ENOENT;2412goto out;2413}24142415while (bucket < hash->size) {2416if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if,2417hash, bucket, &idx))2418break;2419bucket++;2420}24212422cb->args[0] = bucket;2423cb->args[1] = idx;24242425ret = msg->len;24262427out:2428batadv_hardif_put(primary_if);24292430dev_put(mesh_iface);24312432return ret;2433}24342435#ifdef CONFIG_BATMAN_ADV_DAT2436/**2437* batadv_bla_check_claim() - check if address is claimed2438*2439* @bat_priv: the bat priv with all the mesh interface information2440* @addr: mac address of which the claim status is checked2441* @vid: the VLAN ID2442*2443* addr is checked if this address is claimed by the local device itself.2444*2445* Return: true if bla is disabled or the mac is claimed by the device,2446* false if the device addr is already claimed by another gateway2447*/2448bool batadv_bla_check_claim(struct batadv_priv *bat_priv,2449u8 *addr, unsigned short vid)2450{2451struct batadv_bla_claim search_claim;2452struct batadv_bla_claim *claim = NULL;2453struct batadv_hard_iface *primary_if = NULL;2454bool ret = true;24552456if (!atomic_read(&bat_priv->bridge_loop_avoidance))2457return ret;24582459primary_if = batadv_primary_if_get_selected(bat_priv);2460if (!primary_if)2461return ret;24622463/* First look if the mac address is claimed */2464ether_addr_copy(search_claim.addr, addr);2465search_claim.vid = vid;24662467claim = batadv_claim_hash_find(bat_priv, &search_claim);24682469/* If there is a claim and we are not owner of the claim,2470* return false.2471*/2472if (claim) {2473if (!batadv_compare_eth(claim->backbone_gw->orig,2474primary_if->net_dev->dev_addr))2475ret = false;2476batadv_claim_put(claim);2477}24782479batadv_hardif_put(primary_if);2480return ret;2481}2482#endif248324842485