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
33 #include <sys/ioctl.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <net/ethernet.h>
39 #include <net/if_arp.h>
40 #include <linux/if_tun.h>
42 static int inet_ifup(const char *ifname)
47 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
51 memset(&ifr, 0, sizeof(ifr));
52 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
54 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
59 if (ifr.ifr_flags & IFF_UP) {
64 ifr.ifr_flags |= IFF_UP;
66 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
79 static int create_tap(const char *ifname)
84 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
86 perror("Failed to open TUN/TAP device");
90 memset(&ifr, 0, sizeof(ifr));
91 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
92 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
94 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
95 perror("Failed to set TUN/TAP interface");
102 if (ioctl(fd, TUNSETLINK, (unsigned long) val) < 0)
103 perror("Failed to set TUN/TAP link type");
108 static void dump_packet(unsigned char *buf, int len)
113 for (i = 0; i < len; i++) {
114 printf(" %02x", buf[i]);
115 if ((i + 1) % 16 == 0)
117 else if ((i + 1) % 8 == 0)
123 static void decode_ip(unsigned char *buf, int len)
125 struct iphdr *ip = (void *) buf;
127 printf(" IP proto %d saddr 0x%08x daddr 0x%08x\n",
128 ip->protocol, ip->saddr, ip->daddr);
131 static void decode_packet(unsigned char *buf, int len)
133 struct ether_header *eh = (void *) buf;
134 char src[18], dst[18];
137 snprintf(dst, sizeof(dst), "%02x:%02x:%02x:%02x:%02x:%02x",
138 eh->ether_dhost[0], eh->ether_dhost[1],
139 eh->ether_dhost[2], eh->ether_dhost[3],
140 eh->ether_dhost[4], eh->ether_dhost[5]);
142 snprintf(src, sizeof(src), "%02x:%02x:%02x:%02x:%02x:%02x",
143 eh->ether_shost[0], eh->ether_shost[1],
144 eh->ether_shost[2], eh->ether_shost[3],
145 eh->ether_shost[4], eh->ether_shost[5]);
147 type = ntohs(eh->ether_type);
149 printf("> type 0x%04x src %s dst %s <\n", type, src, dst);
153 decode_ip(buf + 14, len - 14);
155 case ETHERTYPE_LOOPBACK:
156 dump_packet(buf, len);
161 int main(int argc, char *argv[])
163 const char *ifname = "xxx";
167 fd = create_tap(ifname);
171 if (inet_ifup(ifname) < 0) {
176 memset(&p, 0, sizeof(p));
178 p.events = POLLHUP | POLLIN;
181 unsigned char buf[2048];
184 len = poll(&p, 1, -1);
190 len = read(fd, buf, sizeof(buf));
194 decode_packet(buf, len);