From 378f34ec644a8575750ae107a886347804115c67 Mon Sep 17 00:00:00 2001 From: Michal Zima Date: Sat, 31 Mar 2012 18:47:27 +0200 Subject: [PATCH] Processing of ARP packets --- src/Makefile.am | 1 + src/arp.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ src/arp.h | 46 +++++++++++++++++++++++++ src/wrapper.c | 3 +- 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/arp.c create mode 100644 src/arp.h diff --git a/src/Makefile.am b/src/Makefile.am index 87e24a4..a895769 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,7 @@ sbin_PROGRAMS = wrapsix-dnsproxy wrapsix-wrapper wrapsix_dnsproxy_SOURCES = dnsproxy.c wrapsix_wrapper_SOURCES = \ + arp.c arp.h \ ipv6.c ipv6.h \ ipv4.h \ nat.c nat.h \ diff --git a/src/arp.c b/src/arp.c new file mode 100644 index 0000000..0c0a060 --- /dev/null +++ b/src/arp.c @@ -0,0 +1,92 @@ +/* + * WrapSix + * Copyright (C) 2008-2012 Michal Zima + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include /* ETHERTYPE_* */ +#include /* htons */ +#include +#include /* malloc */ +#include /* memcmp, memset */ + +#include "arp.h" +#include "transmitter.h" +#include "wrapper.h" + +/** + * Process ARP packets and reply to them. + * + * @param ethq Ethernet header of the packet + * @param payload Data of the packet + * + * @return 0 for success + * @return 1 for failure + */ +int arp(struct s_ethernet *ethq, char *payload) +{ + struct s_arp *arpq, *arpr; /* request and reply */ + struct s_ethernet *ethr; + unsigned char *packet; + + arpq = (struct s_arp *) payload; + + /* process only requests */ + if (htons(arpq->opcode) != ARP_OP_REQUEST) { + /* not an ARP request */ + return 1; + } + + /* test if this packet belongs to us */ + if (memcmp(&wrapsix_ipv4_addr, &arpq->ip_dest, 4)) { + printf("[Debug] This is unfamiliar packet\n"); + return 1; + } + + /* compute the packet size */ + #define ARP_PACKET_SIZE sizeof(struct s_ethernet) + sizeof(struct s_arp) + + /* allocate enough memory */ + if ((packet = (unsigned char *) malloc(ARP_PACKET_SIZE)) == NULL) { + fprintf(stderr, "[Error] Lack of free memory\n"); + return 1; + } + memset(packet, 0x0, ARP_PACKET_SIZE); + + /* define ethernet header and ARP offsets */ + ethr = (struct s_ethernet *) packet; + arpr = (struct s_arp *) (packet + sizeof(struct s_ethernet)); + + /* assemble the ethernet header */ + ethr->dest = ethq->src; + ethr->src = mac; + ethr->type = htons(ETHERTYPE_ARP); + + /* assemble the ARP reply part */ + arpr->hw = htons(ARP_HDR_ETHER); + arpr->proto = htons(ETHERTYPE_IP); + arpr->hw_len = 0x06; + arpr->proto_len = 0x04; + arpr->opcode = htons(ARP_OP_REPLY); + arpr->mac_src = ethr->src; + arpr->mac_dest = ethr->dest; + arpr->ip_src = wrapsix_ipv4_addr; + arpr->ip_dest = arpq->ip_src; + + /* send ARP reply */ + transmit_raw(packet, ARP_PACKET_SIZE); + + return 0; +} diff --git a/src/arp.h b/src/arp.h new file mode 100644 index 0000000..daa00b9 --- /dev/null +++ b/src/arp.h @@ -0,0 +1,46 @@ +/* + * WrapSix + * Copyright (C) 2008-2012 Michal Zima + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef ARP_H +#define ARP_H + +#include "ipv4.h" +#include "ipv6.h" + +/* ARP opcodes */ +#define ARP_OP_REQUEST 0x0001 +#define ARP_OP_REPLY 0x0002 + +#define ARP_HDR_ETHER 0x0001 + +/* ARP structure */ +struct s_arp { + unsigned short hw; /* 16 b; hardware type [0x0001] */ + unsigned short proto; /* 16 b; protocol type [0x0800] */ + unsigned char hw_len; /* 8 b; length of hardware address in bytes [0x06] */ + unsigned char proto_len; /* 8 b; length of protocol address in bytes [0x04] */ + unsigned short opcode; /* 16 b; operation code: [0x0001] or [0x0002] */ + struct s_mac_addr mac_src; /* 48 b; sender hardware address */ + struct s_ipv4_addr ip_src; /* 32 b; sender protocol address */ + struct s_mac_addr mac_dest; /* 48 b; target hardware address */ + struct s_ipv4_addr ip_dest; /* 32 b; target protocol address */ +}__attribute__((__packed__)); + +int arp(struct s_ethernet *ethq, char *payload); + +#endif /* ARP_H */ diff --git a/src/wrapper.c b/src/wrapper.c index 99f810e..6b9eef4 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -29,6 +29,7 @@ #include /* time */ #include /* close */ +#include "arp.h" #include "ethernet.h" #include "ipv4.h" #include "ipv6.h" @@ -166,7 +167,7 @@ int process(char *packet) return ipv6(eth, payload); case ETHERTYPE_ARP: printf("[Debug] HW Protocol: ARP\n"); - return -1; + return arp(eth, payload); default: printf("[Debug] HW Protocol: unknown [%d/0x%04x]\n", htons(eth->type), htons(eth->type));