2010-02-06 12:17:19 +02:00
|
|
|
/*
|
|
|
|
* WrapSix
|
2012-03-26 13:53:58 +03:00
|
|
|
* Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
|
2010-02-06 12:17:19 +02:00
|
|
|
*
|
|
|
|
* 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 <arpa/inet.h> /* inet_pton */
|
|
|
|
#include <linux/if_ether.h> /* ETH_P_ALL */
|
|
|
|
#include <net/if.h> /* struct ifreq */
|
|
|
|
#include <netpacket/packet.h> /* struct packet_mreq, struct sockaddr_ll */
|
|
|
|
#include <netinet/in.h> /* htons */
|
|
|
|
#include <net/ethernet.h> /* ETHERTYPE_* */
|
|
|
|
#include <stdio.h>
|
2011-11-08 22:15:36 +02:00
|
|
|
#include <stdlib.h> /* srand */
|
2010-02-06 12:17:19 +02:00
|
|
|
#include <string.h> /* strncpy */
|
|
|
|
#include <sys/ioctl.h> /* ioctl, SIOCGIFINDEX */
|
2011-11-08 22:15:36 +02:00
|
|
|
#include <time.h> /* time */
|
2010-02-06 12:17:19 +02:00
|
|
|
#include <unistd.h> /* close */
|
|
|
|
|
2012-03-31 19:47:27 +03:00
|
|
|
#include "arp.h"
|
2011-11-08 22:15:36 +02:00
|
|
|
#include "ethernet.h"
|
2010-02-22 16:00:14 +02:00
|
|
|
#include "ipv4.h"
|
2010-02-06 12:31:29 +02:00
|
|
|
#include "ipv6.h"
|
2010-02-22 16:00:14 +02:00
|
|
|
#include "nat.h"
|
2012-03-26 13:53:58 +03:00
|
|
|
#include "transmitter.h"
|
2010-02-22 16:00:14 +02:00
|
|
|
#include "wrapper.h"
|
2010-02-06 12:17:19 +02:00
|
|
|
|
|
|
|
#define INTERFACE "eth0"
|
|
|
|
#define BUFFER_SIZE 65536
|
2012-03-31 15:10:35 +03:00
|
|
|
#define PREFIX "fd77::"
|
|
|
|
#define IPV4_ADDR "192.168.0.111"
|
2010-02-06 12:31:29 +02:00
|
|
|
|
2012-03-26 13:53:58 +03:00
|
|
|
struct ifreq interface;
|
2012-03-31 15:10:35 +03:00
|
|
|
struct s_mac_addr mac;
|
2010-02-06 12:31:29 +02:00
|
|
|
struct s_ipv6_addr ndp_multicast_addr;
|
|
|
|
struct s_ipv6_addr wrapsix_ipv6_prefix;
|
2012-03-31 15:10:35 +03:00
|
|
|
struct s_ipv4_addr wrapsix_ipv4_addr;
|
2010-02-06 12:17:19 +02:00
|
|
|
|
|
|
|
int process(char *packet);
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct packet_mreq pmr;
|
|
|
|
|
|
|
|
struct sockaddr_ll addr;
|
|
|
|
size_t addr_size;
|
|
|
|
|
|
|
|
int sniff_sock;
|
|
|
|
int length;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
|
|
|
|
/* initialize the socket for sniffing */
|
|
|
|
if ((sniff_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to create listening socket\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the interface */
|
|
|
|
strncpy(interface.ifr_name, INTERFACE, IFNAMSIZ);
|
|
|
|
if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to get the interface\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-31 15:10:35 +03:00
|
|
|
/* get interface's HW address (i.e. MAC) */
|
|
|
|
if (ioctl(sniff_sock, SIOCGIFHWADDR, &interface) == 0) {
|
|
|
|
memcpy(&mac, &interface.ifr_hwaddr.sa_data, sizeof(struct s_mac_addr));
|
|
|
|
|
|
|
|
/* reinitialize the interface */
|
|
|
|
if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to reinitialize the interface\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "[Error] Unable to get the interface's HW address\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-06 12:17:19 +02:00
|
|
|
/* set the promiscuous mode */
|
|
|
|
memset(&pmr, 0x0, sizeof(pmr));
|
|
|
|
pmr.mr_ifindex = interface.ifr_ifindex;
|
|
|
|
pmr.mr_type = PACKET_MR_PROMISC;
|
|
|
|
if (setsockopt(sniff_sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to set the promiscuous mode on the interface\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-06 12:31:29 +02:00
|
|
|
/* some preparations */
|
|
|
|
/* compute binary IPv6 address of NDP multicast */
|
|
|
|
inet_pton(AF_INET6, "ff02::1:ff00:0", &ndp_multicast_addr);
|
|
|
|
|
|
|
|
/* compute binary IPv6 address of WrapSix prefix */
|
|
|
|
inet_pton(AF_INET6, PREFIX, &wrapsix_ipv6_prefix);
|
|
|
|
|
2012-03-31 15:10:35 +03:00
|
|
|
/* compute binary IPv4 address of WrapSix */
|
|
|
|
inet_pton(AF_INET, IPV4_ADDR, &wrapsix_ipv4_addr);
|
|
|
|
|
2012-03-26 13:53:58 +03:00
|
|
|
/* initiate sending socket */
|
|
|
|
if (transmission_init()) {
|
|
|
|
fprintf(stderr, "[Error] Unable to initiate sending socket\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-22 16:00:14 +02:00
|
|
|
/* initiate NAT tables */
|
|
|
|
nat_init();
|
|
|
|
|
2011-11-08 22:15:36 +02:00
|
|
|
/* initiate random numbers generator */
|
|
|
|
srand((unsigned int) time(NULL));
|
|
|
|
|
2010-02-06 12:17:19 +02:00
|
|
|
/* sniff! :c) */
|
|
|
|
for (;;) {
|
|
|
|
addr_size = sizeof(addr);
|
|
|
|
if ((length = recv(sniff_sock, buffer, BUFFER_SIZE, 0)) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to retrieve data from socket\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
process((char *) &buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean-up */
|
2012-03-26 13:53:58 +03:00
|
|
|
/* close sending socket */
|
|
|
|
transmission_quit();
|
|
|
|
|
2010-02-22 16:00:14 +02:00
|
|
|
/* empty NAT tables */
|
|
|
|
nat_quit();
|
|
|
|
|
2010-02-06 12:17:19 +02:00
|
|
|
/* unset the promiscuous mode */
|
|
|
|
if (setsockopt(sniff_sock, SOL_PACKET, PACKET_DROP_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
|
|
|
|
fprintf(stderr, "[Error] Unable to unset the promiscuous mode on the interface\n");
|
|
|
|
/* do not call `return` here as we want to close the socket too */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close the socket */
|
|
|
|
close(sniff_sock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int process(char *packet)
|
|
|
|
{
|
|
|
|
struct s_ethernet *eth; /* the ethernet header */
|
|
|
|
char *payload; /* the IP header + packet payload */
|
|
|
|
|
|
|
|
/* parse ethernet header */
|
|
|
|
eth = (struct s_ethernet *) (packet);
|
|
|
|
payload = packet + sizeof(struct s_ethernet);
|
|
|
|
|
|
|
|
switch (htons(eth->type)) {
|
|
|
|
case ETHERTYPE_IP:
|
|
|
|
printf("[Debug] HW Protocol: IPv4\n");
|
2012-04-02 14:44:14 +03:00
|
|
|
return ipv4(eth, payload);
|
2010-02-06 12:17:19 +02:00
|
|
|
case ETHERTYPE_IPV6:
|
|
|
|
printf("[Debug] HW Protocol: IPv6\n");
|
2010-02-06 12:31:29 +02:00
|
|
|
return ipv6(eth, payload);
|
2010-02-06 12:17:19 +02:00
|
|
|
case ETHERTYPE_ARP:
|
|
|
|
printf("[Debug] HW Protocol: ARP\n");
|
2012-03-31 19:47:27 +03:00
|
|
|
return arp(eth, payload);
|
2010-02-06 12:17:19 +02:00
|
|
|
default:
|
|
|
|
printf("[Debug] HW Protocol: unknown [%d/0x%04x]\n",
|
|
|
|
htons(eth->type), htons(eth->type));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-22 16:00:14 +02:00
|
|
|
|
2012-04-01 09:24:25 +03:00
|
|
|
/**
|
|
|
|
* Translator of IPv6 address with embedded IPv4 address to that IPv4 address.
|
|
|
|
*
|
|
|
|
* @param ipv6_addr IPv6 address (as data source)
|
|
|
|
* @param ipv4_addr Where to put final IPv4 address
|
|
|
|
*/
|
2011-11-08 22:15:36 +02:00
|
|
|
void ipv6_to_ipv4(struct s_ipv6_addr *ipv6_addr, struct s_ipv4_addr *ipv4_addr)
|
2010-02-22 16:00:14 +02:00
|
|
|
{
|
2012-04-01 09:24:25 +03:00
|
|
|
memcpy(ipv4_addr, ipv6_addr->addr + 12, 4);
|
2010-02-22 16:00:14 +02:00
|
|
|
}
|
|
|
|
|
2012-04-01 09:24:25 +03:00
|
|
|
/**
|
|
|
|
* Translator of IPv4 address to IPv6 address with WrapSix' prefix.
|
|
|
|
*
|
|
|
|
* @param ipv4_addr IPv4 address (as data source)
|
|
|
|
* @param ipv6_addr Where to put final IPv6 address
|
|
|
|
*/
|
2011-11-08 22:15:36 +02:00
|
|
|
void ipv4_to_ipv6(struct s_ipv4_addr *ipv4_addr, struct s_ipv6_addr *ipv6_addr)
|
2010-02-22 16:00:14 +02:00
|
|
|
{
|
2012-04-01 09:24:25 +03:00
|
|
|
memcpy(ipv6_addr, &wrapsix_ipv6_prefix, 12);
|
|
|
|
memcpy(ipv6_addr->addr + 12, ipv4_addr, 4);
|
2010-02-22 16:00:14 +02:00
|
|
|
}
|