-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharmnet.c
345 lines (286 loc) · 6.8 KB
/
armnet.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
/* #include <linux/if.h> */
/* #include <linux/if_arp.h> */
#include <linux/if_tun.h>
static char *brif_name = "br0";
static char *hostif_name = "eth0";
static void write_proc (char *name, char *val);
void
dump (void *buf, int n)
{
int i;
int j;
int c;
for (i = 0; i < n; i += 16) {
printf ("%04x: ", i);
for (j = 0; j < 16; j++) {
if (i+j < n)
printf ("%02x ", ((unsigned char *)buf)[i+j]);
else
printf (" ");
}
printf (" ");
for (j = 0; j < 16; j++) {
c = ((unsigned char *)buf)[i+j] & 0x7f;
if (i+j >= n)
putchar (' ');
else if (c < ' ' || c == 0x7f)
putchar ('.');
else
putchar (c);
}
printf ("\n");
}
}
static void armnet_setup_bridge (void);
/*
* get a mac address assocaited with this machine, even if it is not
* the primary network adapter
*/
static int
get_hwaddr (unsigned char *hwaddr)
{
int idx;
int sock;
struct ifreq ifr;
char ifname[IFNAMSIZ+1];
if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
return (-1);
for (idx = 1; ; idx++) {
memset (&ifr, 0, sizeof ifr);
ifr.ifr_ifindex = idx;
if (ioctl (sock, SIOCGIFNAME, &ifr) < 0)
break;
strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
ifname[IFNAMSIZ] = 0;
memset (&ifr, 0, sizeof ifr);
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl (sock, SIOCGIFHWADDR, &ifr) < 0)
break;
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
continue;
memcpy (hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
close (sock);
return (0);
}
close (sock);
return (-1);
}
int tapfd;
char tapif_name[IFNAMSIZ];
void
armnet_init (void)
{
unsigned char hwaddr[6];
struct ifreq ifr;
int i;
char cmd[1000];
char filename[1000];
armnet_setup_bridge ();
if (get_hwaddr (hwaddr) < 0) {
fprintf (stderr, "can't get host hwaddr\n");
exit (1);
}
/*
* use host hwaddr, but make it different by toggling the high bit
* and setting the local bit
*/
hwaddr[0] ^= 0x80;
hwaddr[0] |= 2;
printf ("using hwaddr ");
for (i = 0; i < 6; i++)
printf ("%s%02x", i ? ":" : "", hwaddr[i]);
printf ("\n");
if ((tapfd = open ("/dev/net/tun", O_RDWR)) < 0) {
perror ("open /dev/net/tun");
exit (1);
}
memset (&ifr, 0, sizeof ifr);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl(tapfd, TUNSETIFF, &ifr) < 0) {
perror ("TUNSETIFF");
exit(1);
}
if (ioctl (tapfd, TUNGETIFF, &ifr) < 0) {
perror ("TUNGETIFF");
exit (1);
}
strcpy (tapif_name, ifr.ifr_name);
sprintf (cmd, "ifconfig %s 0.0.0.0 promisc up", tapif_name);
system (cmd);
sprintf (cmd, "brctl addif %s %s", brif_name, tapif_name);
system (cmd);
sprintf (filename, "/proc/sys/net/ipv4/conf/%s/proxy_arp", tapif_name);
write_proc (filename, "1");
sprintf (filename, "/proc/sys/net/ipv4/conf/%s/forwarding", tapif_name);
write_proc (filename, "1");
fcntl (tapfd, F_SETFL, O_NONBLOCK);
}
static void
write_proc (char *name, char *val)
{
FILE *f;
if ((f = fopen (name, "w")) == NULL) {
fprintf (stderr, "can't write to %s\n", name);
exit (1);
}
fprintf (f, "%s\n", val);
fclose (f);
}
int
armnet_get_host_params (char *ifname,
struct in_addr *addr,
struct in_addr *netmask,
struct in_addr *broadcast,
struct in_addr *gateway)
{
int sock = 0;
struct sockaddr_in *sin;
struct ifreq ifr;
FILE *inf;
int saw_gw;
char buf[1000], gwbuf[1000];
if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
return (-1);
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl (sock, SIOCGIFADDR, &ifr) < 0)
goto bad;
sin = (struct sockaddr_in *)&ifr.ifr_addr;
*addr = sin->sin_addr;
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl (sock, SIOCGIFNETMASK, &ifr) < 0)
goto bad;
sin = (struct sockaddr_in *)&ifr.ifr_addr;
*netmask = sin->sin_addr;
strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl (sock, SIOCGIFBRDADDR, &ifr) < 0)
goto bad;
sin = (struct sockaddr_in *)&ifr.ifr_addr;
*broadcast = sin->sin_addr;
close (sock);
if ((inf = popen ("netstat -rn", "r")) == NULL)
goto bad;
saw_gw = 0;
while (fgets (buf, sizeof buf, inf) != NULL) {
if (sscanf (buf, "0.0.0.0 %s", gwbuf) == 1) {
gateway->s_addr = inet_addr (gwbuf);
saw_gw = 1;
break;
}
}
pclose (inf);
if (saw_gw == 0)
goto bad;
return (0);
bad:
close (sock);
return (-1);
}
void
armnet_setup_bridge (void)
{
char filename[1000];
struct in_addr addr, netmask, broadcast, gateway;
char cmd[1000];
char *outp;
if (armnet_get_host_params (hostif_name,
&addr,
&netmask,
&broadcast,
&gateway) < 0) {
return;
}
printf ("host %s ", inet_ntoa (addr));
printf ("netmask %s ", inet_ntoa (netmask));
printf ("broadcast %s\n", inet_ntoa (broadcast));
printf ("gateway %s\n", inet_ntoa (gateway));
/* brctl addbr br0 */
sprintf (cmd, "brctl addbr %s", brif_name);
system (cmd);
sprintf (cmd, "brctl addif %s %s", brif_name, hostif_name);
system (cmd);
/* echo 1 > /proc/sys/net/ipv4/conf/eth0/forwarding */
sprintf (filename, "/proc/sys/net/ipv4/conf/%s/forwarding",
hostif_name);
write_proc (filename, "1");
/* echo 1 > /proc/sys/net/ipv4/conf/br0/forwarding */
sprintf (filename, "/proc/sys/net/ipv4/conf/%s/forwarding",
brif_name);
write_proc (filename, "1");
sprintf (cmd, "ifconfig %s promisc 0.0.0.0 promisc",
hostif_name);
system (cmd);
/* ifconfig br0 ADDR netmask MASK broadcast BRD */
outp = cmd;
outp += sprintf (outp, "ifconfig %s %s ", brif_name,
inet_ntoa (addr));
outp += sprintf (outp, "netmask %s ", inet_ntoa (netmask));
outp += sprintf (outp, "broadcast %s ", inet_ntoa (broadcast));
system (cmd);
sprintf (cmd, "route add default gw %s", inet_ntoa (gateway));
system (cmd);
sprintf (cmd, "brctl stp %s off", brif_name);
system (cmd);
sprintf (cmd, "brctl setfd %s 0", brif_name);
system (cmd);
sprintf (cmd, "brctl sethello %s 1", brif_name);
system (cmd);
write_proc ("/proc/sys/net/ipv4/ip_nonlocal_bind", "1");
}
void
armnet_soak (void)
{
char buf[10000];
int len;
fd_set rset;
struct timeval tv;
while (1) {
if (0) {
FD_ZERO (&rset);
FD_SET (tapfd, &rset);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select (tapfd + 1, &rset, NULL, NULL, &tv) < 0) {
perror ("select");
exit (1);
}
if (FD_ISSET (tapfd, &rset) == 0)
break;
}
len = read (tapfd, buf, sizeof buf);
if (len < 0) {
if (errno == EAGAIN)
break;
perror ("tap read");
exit (1);
}
printf ("got %d\n", len);
dump (buf, len);
}
}
int
main (int argc, char **argv)
{
armnet_init ();
while (1) {
armnet_soak ();
usleep (200 * 1000);
}
return (0);
}