From d6f93792cd88e2e007c4faa10d2a514907b5b3ac Mon Sep 17 00:00:00 2001 From: Michal Zima Date: Fri, 6 Apr 2012 17:02:40 +0200 Subject: [PATCH] Full processing of UDP packets --- src/Makefile.am | 1 + src/ipv4.c | 3 +- src/ipv6.c | 3 +- src/udp.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++ src/udp.h | 34 ++++++++ 5 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 src/udp.c create mode 100644 src/udp.h diff --git a/src/Makefile.am b/src/Makefile.am index 2f1a6d7..64d2053 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,4 +9,5 @@ wrapsix_wrapper_SOURCES = \ nat.c nat.h \ radixtree.c radixtree.h \ transmitter.c transmitter.h \ + udp.c udp.h \ wrapper.c wrapper.h diff --git a/src/ipv4.c b/src/ipv4.c index d84e18e..812d4d7 100644 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -22,6 +22,7 @@ #include "icmp.h" #include "ipv4.h" +#include "udp.h" #include "wrapper.h" int ipv4(struct s_ethernet *eth, char *packet) @@ -54,7 +55,7 @@ int ipv4(struct s_ethernet *eth, char *packet) break; case IPPROTO_UDP: printf("[Debug] IPv4 Protocol: UDP\n"); - /*ipv4_udp(eth, ip, payload, data_size);*/ + udp_ipv4(eth, ip, payload, data_size); break; case IPPROTO_ICMP: printf("[Debug] IPv4 Protocol: ICMP\n"); diff --git a/src/ipv6.c b/src/ipv6.c index 96d6610..7e11a98 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -22,6 +22,7 @@ #include "icmp.h" #include "ipv6.h" +#include "udp.h" #include "wrapper.h" int ipv6(struct s_ethernet *eth, char *packet) @@ -47,7 +48,7 @@ int ipv6(struct s_ethernet *eth, char *packet) break; case IPPROTO_UDP: printf("[Debug] IPv6 Protocol: UDP\n"); - /*ipv6_udp(eth, ip, payload);*/ + udp_ipv6(eth, ip, payload); break; case IPPROTO_ICMPV6: printf("[Debug] IPv6 Protocol: ICMP\n"); diff --git a/src/udp.c b/src/udp.c new file mode 100644 index 0000000..cc8f424 --- /dev/null +++ b/src/udp.c @@ -0,0 +1,207 @@ +/* + * 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 /* 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; +} diff --git a/src/udp.h b/src/udp.h new file mode 100644 index 0000000..3513f18 --- /dev/null +++ b/src/udp.h @@ -0,0 +1,34 @@ +/* + * 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 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 */