1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <linux/if_tun.h>
static int inet_ifup(const char *ifname)
{
struct ifreq ifr;
int sk, err;
sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sk < 0)
return -errno;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
err = -errno;
goto done;
}
if (ifr.ifr_flags & IFF_UP) {
err = -EALREADY;
goto done;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
err = -errno;
goto done;
}
err = 0;
done:
close(sk);
return err;
}
static int create_tap(const char *ifname)
{
struct ifreq ifr;
int fd, val;
fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
if (fd < 0) {
perror("Failed to open TUN/TAP device");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
perror("Failed to set TUN/TAP interface");
close(fd);
return -1;
}
val = ARPHRD_ETHER;
if (ioctl(fd, TUNSETLINK, (unsigned long) val) < 0)
perror("Failed to set TUN/TAP link type");
return fd;
}
static void dump_packet(unsigned char *buf, int len)
{
int i;
printf("[");
for (i = 0; i < len; i++) {
printf(" %02x", buf[i]);
if ((i + 1) % 16 == 0)
printf("\n ");
else if ((i + 1) % 8 == 0)
printf(" ");
}
printf(" ]\n");
}
static void decode_ip(unsigned char *buf, int len)
{
struct iphdr *ip = (void *) buf;
printf(" IP proto %d saddr 0x%08x daddr 0x%08x\n",
ip->protocol, ip->saddr, ip->daddr);
}
static void decode_packet(unsigned char *buf, int len)
{
struct ether_header *eh = (void *) buf;
char src[18], dst[18];
uint16_t type;
snprintf(dst, sizeof(dst), "%02x:%02x:%02x:%02x:%02x:%02x",
eh->ether_dhost[0], eh->ether_dhost[1],
eh->ether_dhost[2], eh->ether_dhost[3],
eh->ether_dhost[4], eh->ether_dhost[5]);
snprintf(src, sizeof(src), "%02x:%02x:%02x:%02x:%02x:%02x",
eh->ether_shost[0], eh->ether_shost[1],
eh->ether_shost[2], eh->ether_shost[3],
eh->ether_shost[4], eh->ether_shost[5]);
type = ntohs(eh->ether_type);
printf("> type 0x%04x src %s dst %s <\n", type, src, dst);
switch (type) {
case ETHERTYPE_IP:
decode_ip(buf + 14, len - 14);
break;
case ETHERTYPE_LOOPBACK:
dump_packet(buf, len);
break;
}
}
int main(int argc, char *argv[])
{
const char *ifname = "xxx";
struct pollfd p;
int fd;
fd = create_tap(ifname);
if (fd < 0)
return 1;
if (inet_ifup(ifname) < 0) {
close(fd);
return 1;
}
memset(&p, 0, sizeof(p));
p.fd = fd;
p.events = POLLHUP | POLLIN;
while (1) {
unsigned char buf[2048];
int len;
len = poll(&p, 1, -1);
if (len < 0)
break;
if (len == 0)
continue;
len = read(fd, buf, sizeof(buf));
if (len < 0)
break;
decode_packet(buf, len);
}
return 0;
}
|