mirror of
https://code.semirocket.science/wrapsix
synced 2024-12-22 12:30:18 +02:00
Processing of ARP packets
This commit is contained in:
parent
845f5c541b
commit
378f34ec64
@ -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 \
|
||||
|
92
src/arp.c
Normal file
92
src/arp.c
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* WrapSix
|
||||
* Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <net/ethernet.h> /* ETHERTYPE_* */
|
||||
#include <netinet/in.h> /* htons */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> /* malloc */
|
||||
#include <string.h> /* 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;
|
||||
}
|
46
src/arp.h
Normal file
46
src/arp.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* WrapSix
|
||||
* Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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 */
|
@ -29,6 +29,7 @@
|
||||
#include <time.h> /* time */
|
||||
#include <unistd.h> /* 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));
|
||||
|
Loading…
Reference in New Issue
Block a user