Skip to content

Commit

Permalink
Some work on the ipv6->eth sending side (not a compilable commit)
Browse files Browse the repository at this point in the history
Just initial work. Still need to get the NDP solicitations getting sent to do 
the ipv6 -> MAC translations, and then more work to get the upper layer TCP
and UDP handlers to work right without referencing ip_hdr all over the place.

Ticket #42
  • Loading branch information
PherricOxide committed Nov 14, 2012
1 parent bcc1e0c commit bb798be
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 32 deletions.
1 change: 1 addition & 0 deletions arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#include "arp.h"
#include "debug.h"

/* Time in seconds before expiration of ARP entries */
#define ARP_MAX_ACTIVE 600

/* Exported */
Expand Down
73 changes: 51 additions & 22 deletions honeyd.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,29 +594,58 @@ honeyd_ether_cb(struct arp_req *req, int success, void *arg)
void
honeyd_deliver_ethernet(struct interface *inter,
struct addr *src_pa, struct addr *src_ha,
struct addr *dst_pa, struct ip_hdr *ip, u_int iplen)
struct addr *dst_pa, u_char *frameData, u_int frameLength)
{
struct arp_req *req;
if (src_pa->addr_type == ADDR_TYPE_IP)
{
struct ip_hdr *ip = (struct ip_hdr*)frameData;
struct arp_req *req;

ip_checksum(ip, iplen);
ip_checksum(ip, frameLength);

/* Ethernet delivery if possible */
if ((req = arp_find(dst_pa)) == NULL) {
arp_request(inter, src_pa, src_ha, dst_pa, honeyd_ether_cb,ip);
} else if (req->cnt == -1) {
/*
* The source MAC of the original requestor does not help
* us here, but we can overwrite it with the MAC of this
* honeypot without causing any harm.
*/
req->src_ha = *src_ha;
honeyd_ether_cb(req, 1, ip);
} else {
/*
* Fall through in case that this packet needs
* to be dropped.
*/
pool_free(pool_pkt, ip);
/* Ethernet delivery if possible */
if ((req = arp_find(dst_pa)) == NULL) {
arp_request(inter, src_pa, src_ha, dst_pa, honeyd_ether_cb,ip);
} else if (req->cnt == -1) {
/*
* The source MAC of the original requestor does not help
* us here, but we can overwrite it with the MAC of this
* honeypot without causing any harm.
*/
req->src_ha = *src_ha;
honeyd_ether_cb(req, 1, ip);
} else {
/*
* Fall through in case that this packet needs
* to be dropped.
*/
pool_free(pool_pkt, ip);
}
} else if (src_pa->addr_type == ADDR_TYPE_IP6) {
// TODO ipv6 Do the neighbor solicitation here, send to the ether
struct ip6_hdr *ip6 = (struct ip6_hdr*)ip6;
struct ndp_req *req;

ip6_checksum(ip6, frameLength);

if ((req = ndp_find(dst_pa)) == NULL)
{
ndp_request(inter, src_pa, src_ha, dst_pa, honeyd_ether_cb,ip6);
} else if (req->cnt == -1) {
/*
* The source MAC of the original requestor does not help
* us here, but we can overwrite it with the MAC of this
* honeypot without causing any harm.
*/
req->src_ha = *src_ha;
//honeyd_ether_cb(req, 1, ip);
} else {
/*
* Fall through in case that this packet needs
* to be dropped.
*/
pool_free(pool_pkt, ip6);
}
}
}

Expand Down Expand Up @@ -721,7 +750,7 @@ honeyd_delay_cb(int fd, short which, void *arg)
/* This function computes the IP checksum for us */
honeyd_deliver_ethernet(tmpl->inter,
&src, tmpl->ethernet_addr,
&dst, ip, iplen);
&dst, (u_char*)ip, iplen);
} else {
honeyd_send_normally(ip, iplen);
}
Expand Down Expand Up @@ -761,7 +790,7 @@ honeyd_delay_cb(int fd, short which, void *arg)
/* This function computes the IP checksum for us */
honeyd_deliver_ethernet(inter,
&router->addr, &inter->if_ent.intf_link_addr,
&addr, ip, iplen);
&addr, (u_char*)ip, iplen);
} else {
struct addr addr;
uint16_t ipoff;
Expand Down
2 changes: 2 additions & 0 deletions icmpv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#define ICMPV6_HDR_LEN 4 /* base ICMPv6 header length */

#define ICMPV6_ND_PAYLOAD_LEN 32 /* length of icmpv6 neighbor discovery/advertisement with target MAC option */

#ifndef __GNUC__
#ifndef __attribute__
# define __attribute__(x)
Expand Down
59 changes: 50 additions & 9 deletions ndp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@

#include "debug.h"

#define NDP_MAX_ACTIVE 600

/* For the physical (IP) address */
static SPLAY_HEAD(ndpTree, ndp_req) pa_ndp_reqs;

/* Just for ordering the SPLAY tree */
static int
pandp_compare(struct ndp_req *a, struct ndp_req *b)
{
Expand Down Expand Up @@ -140,23 +143,63 @@ ndp_new(struct interface *inter,
return (req);
}

void ndp_send_advertisement(eth_t *eth,
/* Request resulution ofi pv6 address into a MAC via a neighbor solicitation */
void
ndp_request(struct interface *inter,
struct addr *src_pa, struct addr *src_ha,
struct addr *addr, void (*cb)(struct ndp_req *, int, void *), void *arg)
{
struct ndp_req *req;
struct addr bcast;
struct timeval tv;

if ((req = ndp_new(inter, src_pa, src_ha, addr, NULL)) == NULL) {
syslog(LOG_ERR, "calloc: %m");
return;
}

req->cb = cb;
req->arg = arg;


// TODO ipv6 !!!!

// Need to trace figure out what to do with this stuff
timerclear(&tv);
tv.tv_sec = NDP_MAX_ACTIVE;
evtimer_add(&req->active, &tv);

addr_pack(&bcast, ADDR_TYPE_ETH, ETH_ADDR_BITS,
ETH_ADDR_BROADCAST, ETH_ADDR_LEN);
arp_discover(req, &bcast);

// TODO ipv6 !!!!
}

void ndp_send(eth_t *eth, uint icmpv6MessageType,
struct addr linkLayerSource, struct addr linkLayerDestination,
struct addr ipLayerSource, struct addr ipLayerDestination,
struct addr advertisementLinkTarget, struct addr advertisementIpTarget)
struct addr linkLayerTarget, struct addr ipLayerTarget)
{

printf("Ndp advertisement details {\nipLayerSource: %s\nipLayerDestination: %s \nadvertisementLinkTarget: %s\nadvertisementIpTarget: %s\n}\n", addr_ntoa(&ipLayerSource), addr_ntoa(&ipLayerDestination), addr_ntoa(&advertisementLinkTarget), addr_ntoa(&advertisementIpTarget));
uint packetLength = ETH_HDR_LEN + IP6_HDR_LEN + ICMPV6_HDR_LEN + sizeof(struct icmpv6_msg_nd);
u_char pkt[packetLength];

eth_pack_hdr(pkt, linkLayerDestination.addr_eth, linkLayerSource.addr_eth, ETH_TYPE_IPV6);
ip6_pack_hdr(pkt + ETH_HDR_LEN, 0, 0, 32, IP_PROTO_ICMPV6, IP6_HLIM_MAX, ipLayerSource.addr_ip6, ipLayerDestination.addr_ip6);
icmpv6_pack_hdr_na_mac(pkt + ETH_HDR_LEN + IP6_HDR_LEN, advertisementIpTarget.addr_ip6, advertisementLinkTarget.addr_eth);
ip6_pack_hdr(pkt + ETH_HDR_LEN, 0, 0, ICMPV6_ND_PAYLOAD_LEN, IP_PROTO_ICMPV6, IP6_HLIM_MAX, ipLayerSource.addr_ip6, ipLayerDestination.addr_ip6);

if (icmpv6MessageType == ICMPV6_NEIGHBOR_ADVERTISEMENT) {
icmpv6_pack_hdr_na_mac(pkt + ETH_HDR_LEN + IP6_HDR_LEN, ipLayerTarget.addr_ip6, linkLayerTarget.addr_eth);
} else if (icmpv6MessageType == ICMPV6_NEIGHBOR_SOLICITATION) {
icmpv6_pack_hdr_ns_mac(pkt + ETH_HDR_LEN + IP6_HDR_LEN, ipLayerTarget.addr_ip6, linkLayerTarget.addr_eth);
} else {
syslog(LOG_ERR, "ndp_send called with unknown neighbor solicitation message type %d", icmpv6MessageType);
return;
}

ip6_checksum(pkt + ETH_HDR_LEN, packetLength - ETH_HDR_LEN);

syslog(LOG_INFO, "ndp reply %s is-at %s", addr_ntoa(&advertisementIpTarget), addr_ntoa(&advertisementLinkTarget));
syslog(LOG_INFO, "ndp reply %s is-at %s", addr_ntoa(&ipLayerTarget), addr_ntoa(&linkLayerTarget));

if (eth_send(eth, pkt, sizeof(pkt)) != sizeof(pkt))
syslog(LOG_ERR, "couldn't send packet: %m");
Expand Down Expand Up @@ -186,9 +229,7 @@ ndp_recv_cb(struct tuple *summary, const struct icmpv6_msg_nd *query)
else
linkLayerSource = tmpl->ethernet_addr;

printf("Creating reply now\n");

ndp_send_advertisement(summary->inter->if_eth,
ndp_send(summary->inter->if_eth, ICMPV6_NEIGHBOR_ADVERTISEMENT,
*linkLayerSource, summary->linkLayer_src,
queryIP, summary->address_src,
*linkLayerSource, queryIP);
Expand Down
9 changes: 8 additions & 1 deletion ndp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct ndp_req {

struct interface *inter;

// TODO: Document this
int cnt;

struct event active;
Expand Down Expand Up @@ -67,11 +68,17 @@ struct ndp_req *ndp_new(struct interface *inter,
struct addr *pa, struct addr *ha);


void ndp_send_advertisement(eth_t *eth,
void ndp_send(eth_t *eth, uint icmpv6MessageType,
struct addr linkLayerSource, struct addr linkLayerDestination,
struct addr ipLayerSource, struct addr ipLayerDestination,
struct addr advertisementLinkTarget, struct addr advertisementIpTarget);

void ndp_request(struct interface *,
struct addr *src_pa, struct addr *src_ha,
struct addr *dst, void (*)(struct ndp_req *, int, void *), void *);

struct ndp_req *ndp_find(struct addr *);

struct ndp_req * ndp_find(struct addr *addr);
void ndp_recv_cb(struct tuple *summary, const struct icmpv6_msg_nd *query);

Expand Down

0 comments on commit bb798be

Please sign in to comment.