5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <net/ethernet.h>
38 #include <net/if_arp.h>
39 #include <linux/if_tun.h>
41 static int inet_ifup(const char *ifname)
46 sk = socket(PF_INET, SOCK_DGRAM, 0);
50 memset(&ifr, 0, sizeof(ifr));
51 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
53 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
58 if (ifr.ifr_flags & IFF_UP) {
63 ifr.ifr_flags |= IFF_UP;
65 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
78 static int create_tap(const char *ifname)
83 fd = open("/dev/net/tun", O_RDWR);
85 perror("Failed to open TUN/TAP device");
89 memset(&ifr, 0, sizeof(ifr));
90 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
91 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
93 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
94 perror("Failed to set TUN/TAP interface");
101 if (ioctl(fd, TUNSETLINK, (unsigned long) val) < 0)
102 perror("Failed to set TUN/TAP link type");
107 static void dump_packet(unsigned char *buf, int len)
112 for (i = 0; i < len; i++) {
113 printf(" %02x", buf[i]);
114 if ((i + 1) % 16 == 0)
116 else if ((i + 1) % 8 == 0)
122 static void decode_ip(unsigned char *buf, int len)
124 struct iphdr *ip = (void *) buf;
126 printf(" IP proto %d saddr 0x%08x daddr 0x%08x\n",
127 ip->protocol, ip->saddr, ip->daddr);
130 static void decode_packet(unsigned char *buf, int len)
132 struct ether_header *eh = (void *) buf;
133 char src[18], dst[18];
136 snprintf(dst, sizeof(dst), "%02x:%02x:%02x:%02x:%02x:%02x",
137 eh->ether_dhost[0], eh->ether_dhost[1],
138 eh->ether_dhost[2], eh->ether_dhost[3],
139 eh->ether_dhost[4], eh->ether_dhost[5]);
141 snprintf(src, sizeof(src), "%02x:%02x:%02x:%02x:%02x:%02x",
142 eh->ether_shost[0], eh->ether_shost[1],
143 eh->ether_shost[2], eh->ether_shost[3],
144 eh->ether_shost[4], eh->ether_shost[5]);
146 type = ntohs(eh->ether_type);
148 printf("> type 0x%04x src %s dst %s <\n", type, src, dst);
152 decode_ip(buf + 14, len - 14);
154 case ETHERTYPE_LOOPBACK:
155 dump_packet(buf, len);
160 int main(int argc, char *argv[])
162 const char *ifname = "xxx";
166 fd = create_tap(ifname);
170 if (inet_ifup(ifname) < 0) {
175 memset(&p, 0, sizeof(p));
177 p.events = POLLHUP | POLLIN;
180 unsigned char buf[2048];
183 len = poll(&p, 1, -1);
189 len = read(fd, buf, sizeof(buf));
193 decode_packet(buf, len);