From: Antonio Quartulli <ordex@autistici.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCHv7 4/7] batman-adv: Distributed ARP Table - add ARP parsing functions
Date: Sun, 26 Feb 2012 19:23:20 +0100 [thread overview]
Message-ID: <1330280603-25510-5-git-send-email-ordex@autistici.org> (raw)
In-Reply-To: <1330280603-25510-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>
---
distributed-arp-table.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++
distributed-arp-table.h | 10 ++++
packet.h | 5 ++-
3 files changed, 130 insertions(+), 1 deletions(-)
diff --git a/distributed-arp-table.c b/distributed-arp-table.c
index a0fccb7..a436e5d 100644
--- a/distributed-arp-table.c
+++ b/distributed-arp-table.c
@@ -30,6 +30,70 @@
#include "types.h"
#include "unicast.h"
+#ifdef CONFIG_BATMAN_ADV_DEBUG
+
+static void bat_dbg_arp(struct bat_priv *bat_priv, struct sk_buff *skb,
+ uint16_t type, int hdr_size, char *msg) {
+ struct unicast_4addr_packet *unicast_4addr_packet;
+
+ if (msg)
+ bat_dbg(DBG_ARP, bat_priv, "%s\n", msg);
+
+ bat_dbg(DBG_ARP, bat_priv, "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
+ ARP_HW_SRC(skb, hdr_size), &ARP_IP_SRC(skb, hdr_size),
+ ARP_HW_DST(skb, hdr_size), &ARP_IP_DST(skb, hdr_size));
+
+ if (hdr_size == 0)
+ return;
+
+ /* if the AP packet is encapsulated in a batman packet, let's print some
+ * debug messages */
+ unicast_4addr_packet = (struct unicast_4addr_packet *)skb->data;
+
+ switch (unicast_4addr_packet->u.header.packet_type) {
+ case BAT_UNICAST:
+ bat_dbg(DBG_ARP, bat_priv, "* encapsulated within a UNICAST "
+ "packet\n");
+ break;
+ case BAT_UNICAST_4ADDR:
+ bat_dbg(DBG_ARP, bat_priv, "* encapsulated within a "
+ "UNICAST_4ADDR packet (src: %pM)\n",
+ unicast_4addr_packet->src);
+ switch (unicast_4addr_packet->subtype) {
+ case BAT_P_DAT_DHT_PUT:
+ bat_dbg(DBG_ARP, bat_priv, "* type: DAT_DHT_PUT\n");
+ break;
+ case BAT_P_DAT_DHT_GET:
+ bat_dbg(DBG_ARP, bat_priv, "* type: DAT_DHT_GET\n");
+ break;
+ case BAT_P_DAT_CACHE_REPLY:
+ bat_dbg(DBG_ARP, bat_priv, "* type: DAT_CACHE_REPLY\n");
+ break;
+ case BAT_P_DATA:
+ bat_dbg(DBG_ARP, bat_priv, "* type: DATA\n");
+ break;
+ default:
+ bat_dbg(DBG_ARP, bat_priv, "* type: Unknown!\n");
+ }
+ break;
+ case BAT_BCAST:
+ bat_dbg(DBG_ARP, bat_priv, "* encapsulated within a BCAST "
+ "packet (src: %pM)\n",
+ ((struct bcast_packet *)unicast_4addr_packet)->orig);
+ break;
+ default:
+ bat_dbg(DBG_ARP, bat_priv, "* encapsulated within an unknown "
+ "packet type (0x%x)\n",
+ unicast_4addr_packet->u.header.packet_type);
+ }
+}
+
+#else
+
+#define bat_dbg_arp(...)
+
+#endif /* CONFIG_BATMAN_ADV_DEBUG */
+
static bool is_orig_node_eligible(struct dht_candidate *res, int select,
dat_addr_t tmp_max, dat_addr_t max,
dat_addr_t last_max,
@@ -197,3 +261,55 @@ 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,
+ int hdr_size)
+{
+ struct arphdr *arphdr;
+ struct ethhdr *ethhdr;
+ uint16_t type = 0;
+
+ /* pull the ethernet header */
+ if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
+ goto out;
+
+ ethhdr = (struct ethhdr *)(skb->data + hdr_size);
+
+ if (ethhdr->h_proto != htons(ETH_P_ARP))
+ goto out;
+
+ /* pull the ARP payload */
+ if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
+ arp_hdr_len(skb->dev))))
+ goto out;
+
+ arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
+
+ /* 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;
+
+ /* Check for bad reply/request. If the ARP message is not sane, DAT
+ * will simply ignore it */
+ if (ipv4_is_loopback(ARP_IP_SRC(skb, hdr_size)) ||
+ ipv4_is_multicast(ARP_IP_SRC(skb, hdr_size)) ||
+ ipv4_is_loopback(ARP_IP_DST(skb, hdr_size)) ||
+ ipv4_is_multicast(ARP_IP_DST(skb, hdr_size)))
+ goto out;
+
+ type = ntohs(arphdr->ar_op);
+out:
+ return type;
+}
diff --git a/distributed-arp-table.h b/distributed-arp-table.h
index 89d52c8..b5bf2d1 100644
--- a/distributed-arp-table.h
+++ b/distributed-arp-table.h
@@ -25,8 +25,18 @@
#include "types.h"
#include "originator.h"
+#include <linux/if_arp.h>
+
#define DAT_ADDR_MAX biggest_unsigned_int(dat_addr_t)
+#define ARP_HW_SRC(skb, hdr_size) ((uint8_t *)(skb->data + hdr_size) + \
+ ETH_HLEN + sizeof(struct arphdr))
+#define ARP_IP_SRC(skb, hdr_size) (*(uint32_t *)(ARP_HW_SRC(skb, hdr_size) + \
+ ETH_ALEN))
+#define ARP_HW_DST(skb, hdr_size) (ARP_HW_SRC(skb, hdr_size) + ETH_ALEN + 4)
+#define ARP_IP_DST(skb, hdr_size) (*(uint32_t *)(ARP_HW_SRC(skb, hdr_size) + \
+ 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)
diff --git a/packet.h b/packet.h
index f704c51..02b0c87 100644
--- a/packet.h
+++ b/packet.h
@@ -37,7 +37,10 @@ enum bat_packettype {
};
enum bat_subtype {
- BAT_P_DATA = 0x01
+ BAT_P_DATA = 0x01,
+ BAT_P_DAT_DHT_GET = 0x02,
+ BAT_P_DAT_DHT_PUT = 0x03,
+ BAT_P_DAT_CACHE_REPLY = 0x04
};
/* this file is included by batctl which needs these defines */
--
1.7.3.4
next prev parent reply other threads:[~2012-02-26 18:23 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-26 18:23 [B.A.T.M.A.N.] [PATCHv7 0/7] Distributed ARP Table Antonio Quartulli
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 1/7] batman-adv: add UNICAST_4ADDR packet type Antonio Quartulli
2012-02-27 11:07 ` Marek Lindner
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 2/7] batman-adv: add a new log level for DAT/ARP debugging Antonio Quartulli
2012-02-27 11:10 ` Marek Lindner
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 3/7] batman-adv: Distributed ARP Table - create DHT helper functions Antonio Quartulli
2012-02-27 11:12 ` Marek Lindner
2012-02-26 18:23 ` Antonio Quartulli [this message]
2012-02-27 11:16 ` [B.A.T.M.A.N.] [PATCHv7 4/7] batman-adv: Distributed ARP Table - add ARP parsing functions Marek Lindner
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 5/7] batman-adv: Distributed ARP Table - add snooping functions for ARP messages Antonio Quartulli
2012-02-27 11:19 ` Marek Lindner
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 6/7] batman-adv: Distributed ARP Table - increase default soft_iface ARP table timeout Antonio Quartulli
2012-02-27 11:22 ` Marek Lindner
2012-02-26 18:23 ` [B.A.T.M.A.N.] [PATCHv7 7/7] batman-adv: Distributed ARP Table - add compile option Antonio Quartulli
2012-02-27 11:28 ` Marek Lindner
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=1330280603-25510-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).