b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Antonio Quartulli <ordex@autistici.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCHv2 4/7] batman-adv: Distributed ARP Table - add ARP parsing functions
Date: Tue,  8 Nov 2011 12:34:19 +0100	[thread overview]
Message-ID: <1320752062-21776-5-git-send-email-ordex@autistici.org> (raw)
In-Reply-To: <1320752062-21776-1-git-send-email-ordex@autistici.org>

ARP messages are now parsed to make it possible to trigger special actions
depending on their types (snooping).

Signed-off-by: Antonio Quartulli <ordex@autistici.org>
---

* now this patch is checkpatch --strict clean
* corrected use of arp_hdr_len()
* added print of ARP type as string (uses scnprintf())


 distributed-arp-table.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++
 distributed-arp-table.h |    8 +++++++
 2 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/distributed-arp-table.c b/distributed-arp-table.c
index 5bc3004..5fad513 100644
--- a/distributed-arp-table.c
+++ b/distributed-arp-table.c
@@ -179,3 +179,54 @@ out:
 	kfree(cand);
 	return ret;
 }
+
+/* Returns arphdr->ar_op if the skb contains a valid ARP packet, otherwise
+ * returns 0 */
+uint16_t arp_get_type(struct bat_priv *bat_priv, struct sk_buff *skb)
+{
+	struct arphdr *arphdr;
+	struct ethhdr *ethhdr;
+	uint16_t type = 0;
+	char buf[30], *type_str[] = { "REQUEST", "REPLY", "RREQUEST", "RREPLY",
+				      "InREQUEST", "InREPLY", "NAK" };
+
+	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);
+
+	if (type >= 1 && type <= 10)
+		scnprintf(buf, 30, "%s", type_str[type - 1]);
+	else
+		scnprintf(buf, 30, "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));
+out:
+	return type;
+}
diff --git a/distributed-arp-table.h b/distributed-arp-table.h
index ee1a8b3..439af22 100644
--- a/distributed-arp-table.h
+++ b/distributed-arp-table.h
@@ -22,6 +22,14 @@
 #ifndef _NET_BATMAN_ADV_ARP_H_
 #define _NET_BATMAN_ADV_ARP_H_
 
+#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))
+
+uint16_t arp_get_type(struct bat_priv *bat_priv, struct sk_buff *skb);
+
 /* 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)
-- 
1.7.3.4


  parent reply	other threads:[~2011-11-08 11:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-08 11:34 [B.A.T.M.A.N.] [PATCHv2 0/7] DAT: Distributed ARP Table Antonio Quartulli
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 1/7] batman-adv: implement an helper function to forge unicast packets Antonio Quartulli
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 2/7] batman-adv: add a new log level for DAT-ARP debugging Antonio Quartulli
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 3/7] batman-adv: Distributed ARP Table - create the DHT helper functions Antonio Quartulli
2011-11-08 11:34 ` Antonio Quartulli [this message]
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 5/7] batman-adv: Distributed ARP Table - add snooping functions for ARP messages Antonio Quartulli
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 6/7] batman-adv: Distributed ARP Table - increase default soft_iface ARP table timeout Antonio Quartulli
2011-11-08 11:34 ` [B.A.T.M.A.N.] [PATCHv2 7/7] batman-adv: add Distributed ARP Table compile option Antonio Quartulli
2011-11-15 20:27   ` Simon Wunderlich
2011-11-15 20:32     ` Antonio Quartulli
2011-11-21 21:37 ` [B.A.T.M.A.N.] [PATCH] batman-adv: fix two issues in DAT Simon Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1320752062-21776-5-git-send-email-ordex@autistici.org \
    --to=ordex@autistici.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).