1580 lines · 40.1 KB · C
RawDownload
/* * Copyright (C) 2011-2012 B.A.T.M.A.N. contributors: * * Simon Wunderlich * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA * */ #include "main.h"#include "hash.h"#include "hard-interface.h"#include "originator.h"#include "bridge_loop_avoidance.h"#include "translation-table.h"#include "send.h" #include <linux/etherdevice.h>#include <linux/crc16.h>#include <linux/if_arp.h>#include <net/arp.h>#include <linux/if_vlan.h> static const uint8_t announce_mac[4] = {0x43, 0x05, 0x43, 0x05}; static void bla_periodic_work(struct work_struct *work);static void bla_send_announce(struct bat_priv *bat_priv,			      struct backbone_gw *backbone_gw); /* return the index of the claim */static inline uint32_t choose_claim(const void *data, uint32_t size){	const unsigned char *key = data;	uint32_t hash = 0;	size_t i; 	for (i = 0; i < ETH_ALEN + sizeof(short); i++) {		hash += key[i];		hash += (hash << 10);		hash ^= (hash >> 6);	} 	hash += (hash << 3);	hash ^= (hash >> 11);	hash += (hash << 15); 	return hash % size;} /* return the index of the backbone gateway */static inline uint32_t choose_backbone_gw(const void *data, uint32_t size){	const unsigned char *key = data;	uint32_t hash = 0;	size_t i; 	for (i = 0; i < ETH_ALEN + sizeof(short); i++) {		hash += key[i];		hash += (hash << 10);		hash ^= (hash >> 6);	} 	hash += (hash << 3);	hash ^= (hash >> 11);	hash += (hash << 15); 	return hash % size;}  /* compares address and vid of two backbone gws */static int compare_backbone_gw(const struct hlist_node *node, const void *data2){	const void *data1 = container_of(node, struct backbone_gw,					 hash_entry); 	return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);} /* compares address and vid of two claims */static int compare_claim(const struct hlist_node *node, const void *data2){	const void *data1 = container_of(node, struct claim,					 hash_entry); 	return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);} /* free a backbone gw */static void backbone_gw_free_ref(struct backbone_gw *backbone_gw){	if (atomic_dec_and_test(&backbone_gw->refcount))		kfree_rcu(backbone_gw, rcu);} /* finally deinitialize the claim */static void claim_free_rcu(struct rcu_head *rcu){	struct claim *claim; 	claim = container_of(rcu, struct claim, rcu); 	backbone_gw_free_ref(claim->backbone_gw);	kfree(claim);} /* free a claim, call claim_free_rcu if its the last reference */static void claim_free_ref(struct claim *claim){	if (atomic_dec_and_test(&claim->refcount))		call_rcu(&claim->rcu, claim_free_rcu);} /** * @bat_priv: the bat priv with all the soft interface information * @data: search data (may be local/static data) * * looks for a claim in the hash, and returns it if found * or NULL otherwise. */static struct claim *claim_hash_find(struct bat_priv *bat_priv,				     struct claim *data){	struct hashtable_t *hash = bat_priv->claim_hash;	struct hlist_head *head;	struct hlist_node *node;	struct claim *claim;	struct claim *claim_tmp = NULL;	int index; 	if (!hash)		return NULL; 	index = choose_claim(data, hash->size);	head = &hash->table[index]; 	rcu_read_lock();	hlist_for_each_entry_rcu(claim, node, head, hash_entry) {		if (!compare_claim(&claim->hash_entry, data))			continue; 		if (!atomic_inc_not_zero(&claim->refcount))			continue; 		claim_tmp = claim;		break;	}	rcu_read_unlock(); 	return claim_tmp;} /** * @bat_priv: the bat priv with all the soft interface information * @addr: the address of the originator * @vid: the VLAN ID * * looks for a claim in the hash, and returns it if found * or NULL otherwise. */static struct backbone_gw *backbone_hash_find(struct bat_priv *bat_priv,					      uint8_t *addr, short vid){	struct hashtable_t *hash = bat_priv->backbone_hash;	struct hlist_head *head;	struct hlist_node *node;	struct backbone_gw search_entry, *backbone_gw;	struct backbone_gw *backbone_gw_tmp = NULL;	int index; 	if (!hash)		return NULL; 	memcpy(search_entry.orig, addr, ETH_ALEN);	search_entry.vid = vid; 	index = choose_backbone_gw(&search_entry, hash->size);	head = &hash->table[index]; 	rcu_read_lock();	hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {		if (!compare_backbone_gw(&backbone_gw->hash_entry,					 &search_entry))			continue; 		if (!atomic_inc_not_zero(&backbone_gw->refcount))			continue; 		backbone_gw_tmp = backbone_gw;		break;	}	rcu_read_unlock(); 	return backbone_gw_tmp;} /* delete all claims for a backbone */static void bla_del_backbone_claims(struct backbone_gw *backbone_gw){	struct hashtable_t *hash;	struct hlist_node *node, *node_tmp;	struct hlist_head *head;	struct claim *claim;	int i;	spinlock_t *list_lock;	/* protects write access to the hash lists */ 	hash = backbone_gw->bat_priv->claim_hash;	if (!hash)		return; 	for (i = 0; i < hash->size; i++) {		head = &hash->table[i];		list_lock = &hash->list_locks[i]; 		spin_lock_bh(list_lock);		hlist_for_each_entry_safe(claim, node, node_tmp,					  head, hash_entry) { 			if (claim->backbone_gw != backbone_gw)				continue; 			claim_free_ref(claim);			hlist_del_rcu(node);		}		spin_unlock_bh(list_lock);	} 	/* all claims gone, intialize CRC */	backbone_gw->crc = BLA_CRC_INIT;} /** * @bat_priv: the bat priv with all the soft interface information * @orig: the mac address to be announced within the claim * @vid: the VLAN ID * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...) * * sends a claim frame according to the provided info. */static void bla_send_claim(struct bat_priv *bat_priv, uint8_t *mac,			   short vid, int claimtype){	struct sk_buff *skb;	struct ethhdr *ethhdr;	struct hard_iface *primary_if;	struct net_device *soft_iface;	uint8_t *hw_src;	struct bla_claim_dst local_claim_dest;	uint32_t zeroip = 0; 	primary_if = primary_if_get_selected(bat_priv);	if (!primary_if)		return; 	memcpy(&local_claim_dest, &bat_priv->claim_dest,	       sizeof(local_claim_dest));	local_claim_dest.type = claimtype; 	soft_iface = primary_if->soft_iface; 	skb = arp_create(ARPOP_REPLY, ETH_P_ARP,			 /* IP DST: 0.0.0.0 */			 zeroip,			 primary_if->soft_iface,			 /* IP SRC: 0.0.0.0 */			 zeroip,			 /* Ethernet DST: Broadcast */			 NULL,			 /* Ethernet SRC/HW SRC:  originator mac */			 primary_if->net_dev->dev_addr,			 /* HW DST: FF:43:05:XX:00:00			  * with XX   = claim type			  * and YY:YY = group id			  */			 (uint8_t *)&local_claim_dest); 	if (!skb)		goto out; 	ethhdr = (struct ethhdr *)skb->data;	hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr); 	/* now we pretend that the client would have sent this ... */	switch (claimtype) {	case CLAIM_TYPE_ADD:		/* normal claim frame		 * set Ethernet SRC to the clients mac		 */		memcpy(ethhdr->h_source, mac, ETH_ALEN);		bat_dbg(DBG_BLA, bat_priv,			"bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);		break;	case CLAIM_TYPE_DEL:		/* unclaim frame		 * set HW SRC to the clients mac		 */		memcpy(hw_src, mac, ETH_ALEN);		bat_dbg(DBG_BLA, bat_priv,			"bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, vid);		break;	case CLAIM_TYPE_ANNOUNCE:		/* announcement frame		 * set HW SRC to the special mac containg the crc		 */		memcpy(hw_src, mac, ETH_ALEN);		bat_dbg(DBG_BLA, bat_priv,			"bla_send_claim(): ANNOUNCE of %pM on vid %d\n",			ethhdr->h_source, vid);		break;	case CLAIM_TYPE_REQUEST:		/* request frame		 * set HW SRC to the special mac containg the crc		 */		memcpy(hw_src, mac, ETH_ALEN);		memcpy(ethhdr->h_dest, mac, ETH_ALEN);		bat_dbg(DBG_BLA, bat_priv,			"bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",			ethhdr->h_source, ethhdr->h_dest, vid);		break; 	} 	if (vid != -1)		skb = vlan_insert_tag(skb, vid); 	skb_reset_mac_header(skb);	skb->protocol = eth_type_trans(skb, soft_iface);	bat_priv->stats.rx_packets++;	bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;	soft_iface->last_rx = jiffies; 	netif_rx(skb);out:	if (primary_if)		hardif_free_ref(primary_if);} /** * @bat_priv: the bat priv with all the soft interface information * @orig: the mac address of the originator * @vid: the VLAN ID * * searches for the backbone gw or creates a new one if it could not * be found. */static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,					       uint8_t *orig, short vid){	struct backbone_gw *entry;	struct orig_node *orig_node;	int hash_added; 	entry = backbone_hash_find(bat_priv, orig, vid); 	if (entry)		return entry; 	bat_dbg(DBG_BLA, bat_priv,		"bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",		orig, vid); 	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);	if (!entry)		return NULL; 	entry->vid = vid;	entry->lasttime = jiffies;	entry->crc = BLA_CRC_INIT;	entry->bat_priv = bat_priv;	atomic_set(&entry->request_sent, 0);	memcpy(entry->orig, orig, ETH_ALEN); 	/* one for the hash, one for returning */	atomic_set(&entry->refcount, 2); 	hash_added = hash_add(bat_priv->backbone_hash, compare_backbone_gw,			      choose_backbone_gw, entry, &entry->hash_entry); 	if (unlikely(hash_added != 0)) {		/* hash failed, free the structure */		kfree(entry);		return NULL;	} 	/* this is a gateway now, remove any tt entries */	orig_node = orig_hash_find(bat_priv, orig);	if (orig_node) {		tt_global_del_orig(bat_priv, orig_node,				   "became a backbone gateway");		orig_node_free_ref(orig_node);	}	return entry;} /* update or add the own backbone gw to make sure we announce * where we receive other backbone gws */static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,				       struct hard_iface *primary_if,				       short vid){	struct backbone_gw *backbone_gw; 	backbone_gw = bla_get_backbone_gw(bat_priv,					  primary_if->net_dev->dev_addr, vid);	if (unlikely(!backbone_gw))		return; 	backbone_gw->lasttime = jiffies;	backbone_gw_free_ref(backbone_gw);} /** * @bat_priv: the bat priv with all the soft interface information * @vid: the vid where the request came on * * Repeat all of our own claims, and finally send an ANNOUNCE frame * to allow the requester another check if the CRC is correct now. */static void bla_answer_request(struct bat_priv *bat_priv,			       struct hard_iface *primary_if, short vid){	struct hlist_node *node;	struct hlist_head *head;	struct hashtable_t *hash;	struct claim *claim;	struct backbone_gw *backbone_gw;	int i; 	bat_dbg(DBG_BLA, bat_priv,		"bla_answer_request(): received a claim request, send all of our own claims again\n"); 	backbone_gw = backbone_hash_find(bat_priv,					 primary_if->net_dev->dev_addr, vid);	if (!backbone_gw)		return; 	hash = bat_priv->claim_hash;	for (i = 0; i < hash->size; i++) {		head = &hash->table[i]; 		rcu_read_lock();		hlist_for_each_entry_rcu(claim, node, head, hash_entry) {			/* only own claims are interesting */			if (claim->backbone_gw != backbone_gw)				continue; 			bla_send_claim(bat_priv, claim->addr, claim->vid,				       CLAIM_TYPE_ADD);		}		rcu_read_unlock();	} 	/* finally, send an announcement frame */	bla_send_announce(bat_priv, backbone_gw);	backbone_gw_free_ref(backbone_gw);} /** * @backbone_gw: the backbone gateway from whom we are out of sync * * When the crc is wrong, ask the backbone gateway for a full table update. * After the request, it will repeat all of his own claims and finally * send an announcement claim with which we can check again. */static void bla_send_request(struct backbone_gw *backbone_gw){	/* first, remove all old entries */	bla_del_backbone_claims(backbone_gw); 	bat_dbg(DBG_BLA, backbone_gw->bat_priv,		"Sending REQUEST to %pM\n",		backbone_gw->orig); 	/* send request */	bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,		       backbone_gw->vid, CLAIM_TYPE_REQUEST); 	/* no local broadcasts should be sent or received, for now. */	if (!atomic_read(&backbone_gw->request_sent)) {		atomic_inc(&backbone_gw->bat_priv->bla_num_requests);		atomic_set(&backbone_gw->request_sent, 1);	}} /** * @bat_priv: the bat priv with all the soft interface information * @backbone_gw: our backbone gateway which should be announced

Lines 1–500 of 1580.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t