From patchwork Thu Nov 24 22:21:16 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 1404 Return-Path: Received: from confino.investici.org (investici.nine.ch [217.150.252.179]) by open-mesh.org (Postfix) with ESMTPS id B508A600851 for ; Thu, 24 Nov 2011 23:21:50 +0100 (CET) Authentication-Results: open-mesh.org; dkim=pass (1024-bit key) header.i=@autistici.org; dkim-adsp=pass Received: from [217.150.252.179] (confino [217.150.252.179]) (Authenticated sender: ordex@autistici.org) by localhost (Postfix) with ESMTPSA id 18073C8651; Thu, 24 Nov 2011 22:21:48 +0000 (UTC) X-DKIM: Sendmail DKIM Filter v2.8.2 confino.investici.org 18073C8651 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1322173310; bh=6GA2FUIQ1x1F3d81HufXiCPgEWs0zMmHfvPBU8RRo/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=OjYVnc8U3frrK8Bp3Un6JcBqo81O7ZT4McvaJ+nJ1G/lSe+vWQMoXggr5ENUAF8Ps RhF/zvFc6mQwfaU12P2/FEsAOG0e79FZvgruvVDwHDoXSJUvN3p6m16miehmDjcB7q 7EaneDjmMtkPrp1ZzvMQMD7cgM61lEv4H/ajS9bE= From: Antonio Quartulli To: b.a.t.m.a.n@lists.open-mesh.org Date: Thu, 24 Nov 2011 23:21:16 +0100 Message-Id: <1322173279-18338-5-git-send-email-ordex@autistici.org> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: <1322173279-18338-1-git-send-email-ordex@autistici.org> References: <1322173279-18338-1-git-send-email-ordex@autistici.org> Subject: [B.A.T.M.A.N.] [PATCHv4 4/7] batman-adv: Distributed ARP Table - add ARP parsing functions X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.13 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Nov 2011 22:21:50 -0000 ARP messages are now parsed to make it possible to trigger special actions depending on their types (snooping). Signed-off-by: Antonio Quartulli --- distributed-arp-table.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ distributed-arp-table.h | 12 +++++++++ 2 files changed, 71 insertions(+), 0 deletions(-) diff --git a/distributed-arp-table.c b/distributed-arp-table.c index 4c4e064..6cb60b0 100644 --- a/distributed-arp-table.c +++ b/distributed-arp-table.c @@ -30,6 +30,22 @@ #include "types.h" #include "unicast.h" +static inline void bat_dbg_arp(struct bat_priv *bat_priv, + struct sk_buff *skb, uint16_t type) { + char buf[30]; + const char *type_str[] = { "REQUEST", "REPLY", "RREQUEST", "RREPLY", + "InREQUEST", "InREPLY", "NAK" }; + + if (type >= 1 && type <= ARRAY_SIZE(type_str)) + scnprintf(buf, sizeof(buf), "%s", type_str[type - 1]); + else + scnprintf(buf, sizeof(buf), "UNKNOWN (%hu)", type); + + bat_dbg(DBG_ARP, bat_priv, "ARP message of type %s recognised " + "[%pM-%pI4 %pM-%pI4]\n", buf, ARP_HW_SRC(skb), &ARP_IP_SRC(skb), + ARP_HW_DST(skb), &ARP_IP_DST(skb)); +} + /* Given a key, selects the candidates which the DHT message has to be sent to. * An originator O is selected if and only if its DHT_ID value is one of three * closest values (but not greater) then the hash value of the key. @@ -180,3 +196,46 @@ out: kfree(cand); return ret; } + +/* Returns arphdr->ar_op if the skb contains a valid ARP packet, otherwise + * returns 0 */ +static uint16_t arp_get_type(struct bat_priv *bat_priv, struct sk_buff *skb) +{ + struct arphdr *arphdr; + struct ethhdr *ethhdr; + uint16_t type = 0; + + if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) + goto out; + + ethhdr = (struct ethhdr *)skb_mac_header(skb); + + if (ethhdr->h_proto != htons(ETH_P_ARP)) + goto out; + + if (unlikely(!pskb_may_pull(skb, ETH_HLEN + arp_hdr_len(skb->dev)))) + goto out; + + arphdr = (struct arphdr *)(skb->data + sizeof(struct ethhdr)); + + /* Check whether the ARP packet carries a valid + * IP information */ + if (arphdr->ar_hrd != htons(ARPHRD_ETHER)) + goto out; + + if (arphdr->ar_pro != htons(ETH_P_IP)) + goto out; + + if (arphdr->ar_hln != ETH_ALEN) + goto out; + + if (arphdr->ar_pln != 4) + goto out; + + type = ntohs(arphdr->ar_op); + + bat_dbg_arp(bat_priv, skb, type); + +out: + return type; +} diff --git a/distributed-arp-table.h b/distributed-arp-table.h index cca5c6a..3e0f5c6 100644 --- a/distributed-arp-table.h +++ b/distributed-arp-table.h @@ -22,6 +22,12 @@ #ifndef _NET_BATMAN_ADV_ARP_H_ #define _NET_BATMAN_ADV_ARP_H_ +#include "main.h" + +#include + +struct bat_priv; + /* * dat_addr_t is the type used for all DHT indexes. If it is changed, * DAT_ADDR_MAX is changed as well. @@ -31,6 +37,12 @@ #define dat_addr_t uint16_t #define DAT_ADDR_MAX biggest_unsigned_int(dat_addr_t) +#define ARP_HW_SRC(skb) ((uint8_t *)(skb->data) + sizeof(struct ethhdr) + \ + sizeof(struct arphdr)) +#define ARP_IP_SRC(skb) (*(uint32_t *)(ARP_HW_SRC(skb) + ETH_ALEN)) +#define ARP_HW_DST(skb) (ARP_HW_SRC(skb) + ETH_ALEN + 4) +#define ARP_IP_DST(skb) (*(uint32_t *)(ARP_HW_SRC(skb) + ETH_ALEN * 2 + 4)) + /* hash function to choose an entry in a hash table of given size */ /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */ static inline uint32_t hash_ipv4(const void *data, uint32_t size)