1
0
mirror of https://code.semirocket.science/wrapsix synced 2024-11-22 09:44:05 +02:00

Full processing of UDP packets

This commit is contained in:
Michal Zima 2012-04-06 17:02:40 +02:00
parent 0157236357
commit d6f93792cd
5 changed files with 246 additions and 2 deletions

View File

@ -9,4 +9,5 @@ wrapsix_wrapper_SOURCES = \
nat.c nat.h \ nat.c nat.h \
radixtree.c radixtree.h \ radixtree.c radixtree.h \
transmitter.c transmitter.h \ transmitter.c transmitter.h \
udp.c udp.h \
wrapper.c wrapper.h wrapper.c wrapper.h

View File

@ -22,6 +22,7 @@
#include "icmp.h" #include "icmp.h"
#include "ipv4.h" #include "ipv4.h"
#include "udp.h"
#include "wrapper.h" #include "wrapper.h"
int ipv4(struct s_ethernet *eth, char *packet) int ipv4(struct s_ethernet *eth, char *packet)
@ -54,7 +55,7 @@ int ipv4(struct s_ethernet *eth, char *packet)
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
printf("[Debug] IPv4 Protocol: UDP\n"); printf("[Debug] IPv4 Protocol: UDP\n");
/*ipv4_udp(eth, ip, payload, data_size);*/ udp_ipv4(eth, ip, payload, data_size);
break; break;
case IPPROTO_ICMP: case IPPROTO_ICMP:
printf("[Debug] IPv4 Protocol: ICMP\n"); printf("[Debug] IPv4 Protocol: ICMP\n");

View File

@ -22,6 +22,7 @@
#include "icmp.h" #include "icmp.h"
#include "ipv6.h" #include "ipv6.h"
#include "udp.h"
#include "wrapper.h" #include "wrapper.h"
int ipv6(struct s_ethernet *eth, char *packet) int ipv6(struct s_ethernet *eth, char *packet)
@ -47,7 +48,7 @@ int ipv6(struct s_ethernet *eth, char *packet)
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
printf("[Debug] IPv6 Protocol: UDP\n"); printf("[Debug] IPv6 Protocol: UDP\n");
/*ipv6_udp(eth, ip, payload);*/ udp_ipv6(eth, ip, payload);
break; break;
case IPPROTO_ICMPV6: case IPPROTO_ICMPV6:
printf("[Debug] IPv6 Protocol: ICMP\n"); printf("[Debug] IPv6 Protocol: ICMP\n");

207
src/udp.c Normal file
View File

@ -0,0 +1,207 @@
/*
* 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> /* memcpy */
#include "checksum.h"
#include "ethernet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "nat.h"
#include "transmitter.h"
#include "udp.h"
#include "wrapper.h"
/**
* Processing of incoming UDPv4 packets. Directly sends translated UDPv6
* packets.
*
* @param eth4 Ethernet header
* @param ip4 IPv4 header
* @param payload UDPv4 data
* @param payload_size Size of payload; needed because IPv4 header has
* dynamic length
*
* @return 0 for success
* @return 1 for failure
*/
int udp_ipv4(struct s_ethernet *eth, struct s_ipv4 *ip4, char *payload,
unsigned short payload_size)
{
struct s_udp *udp;
struct s_nat *connection;
unsigned short orig_checksum;
unsigned char *packet;
struct s_ethernet *eth6;
struct s_ipv6 *ip6;
/* parse UDP header */
udp = (struct s_udp *) payload;
/* TODO: checksum recheck */
/* find connection in NAT */
connection = nat_in(nat4_udp, ip4->ip_src, udp->port_src, udp->port_dest);
if (connection == NULL) {
printf("[Debug] Incoming connection wasn't found in NAT\n");
return 1;
}
/* allocate memory for translated packet */
if ((packet = (unsigned char *) malloc(sizeof(struct s_ethernet) +
sizeof(struct s_ipv6) +
payload_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
return 1;
}
eth6 = (struct s_ethernet *) packet;
ip6 = (struct s_ipv6 *) (packet + sizeof(struct s_ethernet));
/* build ethernet header */
eth6->dest = connection->mac;
eth6->src = mac;
eth6->type = htons(ETHERTYPE_IPV6);
/* build IPv6 packet */
ip6->ver = 0x60;
ip6->traffic_class = 0x0;
ip6->flow_label = 0x0;
ip6->len = htons(payload_size);
ip6->next_header = IPPROTO_UDP;
ip6->hop_limit = ip4->ttl;
ipv4_to_ipv6(&ip4->ip_src, &ip6->ip_src);
memcpy(&ip6->ip_dest, &connection->ipv6, sizeof(struct s_ipv6_addr));
/* set incoming source port */
udp->port_dest = connection->ipv6_port_src;
/* compute UDP checksum */
udp->checksum = 0x0;
udp->checksum = checksum_ipv6(ip6->ip_src, ip6->ip_dest, payload_size,
IPPROTO_UDP, (unsigned char *) udp);
/* copy the payload data (with new checksum) */
memcpy(packet + sizeof(struct s_ethernet) + sizeof(struct s_ipv6),
payload, payload_size);
/* send translated packet */
transmit_raw(packet, sizeof(struct s_ethernet) + sizeof(struct s_ipv6) +
payload_size);
/* clean-up */
free(packet);
return 0;
}
/**
* Processing of outgoing UDPv6 packets. Directly sends translated UDPv4
* packets.
*
* @param eth6 Ethernet header
* @param ip6 IPv6 header
* @param payload UDPv6 data
*
* @return 0 for success
* @return 1 for failure
*/
int udp_ipv6(struct s_ethernet *eth, struct s_ipv6 *ip6, char *payload)
{
struct s_udp *udp;
struct s_nat *connection;
unsigned short orig_checksum;
struct s_ipv4 *ip4;
unsigned char *packet;
unsigned int packet_size;
/* parse UDP header */
udp = (struct s_udp *) payload;
/* checksum recheck */
orig_checksum = udp->checksum;
udp->checksum = 0;
udp->checksum = checksum_ipv6(ip6->ip_src, ip6->ip_dest,
htons(ip6->len), IPPROTO_UDP,
(unsigned char *) payload);
if (udp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
return 1;
}
/* find connection in NAT */
connection = nat_out(nat6_udp, nat4_udp, eth->src,
ip6->ip_src, ip6->ip_dest,
udp->port_src, udp->port_dest);
if (connection == NULL) {
printf("[Debug] Error! Outgoing connection wasn't "
"found/created in NAT!\n");
return 1;
}
/* allocate memory for translated packet */
packet_size = sizeof(struct s_ipv4) + ip6->len;
if ((packet = (unsigned char *) malloc(packet_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
return 1;
}
ip4 = (struct s_ipv4 *) packet;
/* build IPv4 packet */
ip4->ver_hdrlen = 0x45; /* ver 4, header length 20 B */
ip4->tos = 0x0;
ip4->len = htons(sizeof(struct s_ipv4) + htons(ip6->len));
ip4->id = 0x0;
ip4->flags_offset = htons(IPV4_FLAG_DONT_FRAGMENT);
ip4->ttl = ip6->hop_limit;
ip4->proto = IPPROTO_UDP;
ipv6_to_ipv4(&ip6->ip_dest, &ip4->ip_dest);
memcpy(&ip4->ip_src, &wrapsix_ipv4_addr, sizeof(struct s_ipv4_addr));
/* set outgoing source port */
udp->port_src = connection->ipv4_port_src;
/* compute UDP checksum */
udp->checksum = 0;
/* TODO: checksum computation; in IPv4 it's optional in UDP */
/* copy the payload data (with new checksum) */
memcpy(packet + sizeof(struct s_ipv4), payload, htons(ip6->len));
/* compute IPv4 checksum */
ip4->checksum = checksum_ipv4(ip4->ip_src, ip4->ip_dest,
htons(ip4->len), IPPROTO_UDP,
(unsigned char *) udp);
/* send translated packet */
printf("[Debug] transmitting\n");
transmit_ipv4(&ip4->ip_dest, packet, htons(ip4->len));
/* clean-up */
free(packet);
return 0;
}

34
src/udp.h Normal file
View File

@ -0,0 +1,34 @@
/*
* 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 UDP_H
#define UDP_H
/* UDP header structure */
struct s_udp {
unsigned short port_src; /* 16 b; source port */
unsigned short port_dest; /* 16 b; destination port */
unsigned short len; /* 16 b; header + data length */
unsigned short checksum; /* 16 b; optional checksum */
} __attribute__ ((__packed__));
int udp_ipv4(struct s_ethernet *eth, struct s_ipv4 *ip4, char *payload,
unsigned short payload_size);
int udp_ipv6(struct s_ethernet *eth, struct s_ipv6 *ip6, char *payload);
#endif /* UDP_H */