-
Notifications
You must be signed in to change notification settings - Fork 7
/
ife-bpf.cc
313 lines (295 loc) · 9.85 KB
/
ife-bpf.cc
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
/* Copyright (c) 1995, Theo Schlossnagle. All rights reserved. */
/* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "ife.h"
#include "ife-icmp-support.h"
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in_var.h>
#include <netinet6/in6_var.h>
#include <sys/sockio.h>
#include <ifaddrs.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <arpa/inet.h>
#define IFLISTSIZE 1024
static int _if_sock=-1;
static int _if_sock6=-1;
static int _if_bpf=-1;
static char _if_error_none[] = "";
static char _if_error_exists[] = "IP alias exists";
static char _if_error_nosuchinterface[] = "No such interface";
static char *_if_error=_if_error_none;
int if_initialize() {
int socket_type, v, n=0;
char device[sizeof "/dev/bpf000"];
socket_type = SOCK_RAW;
if((_if_sock = socket (AF_INET, socket_type, 0)) == -1) {
return -1;
}
if((_if_sock6 = socket (AF_INET6, socket_type, 0)) == -1) {
return -1;
}
do {
(void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
_if_bpf = open(device, O_WRONLY);
} while (_if_bpf < 0 && errno == EBUSY && n < 999);
if(_if_bpf < 0) {
return -1;
}
v = 32768;
(void) ioctl(_if_bpf, BIOCSBLEN, (caddr_t)&v);
return 0;
}
char *if_error() {
return _if_error;
}
int
if_send_spoof_request(const char *dev,
unsigned int new_ip, unsigned int r_ip,
const unsigned char *remote_mac,
int count,int icmp) {
int i,ic;
struct ifreq ifr;
struct ether_header *eth;
struct arphdr *arp;
struct interface ifs[1024];
unsigned char *cp, *dest_mac;
static unsigned char buffer[60];
static unsigned char my_mac[ETH_ALEN];
static unsigned char bc_mac[ETH_ALEN] =
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
memset(&ifr, sizeof(struct ifreq), 0);
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(_if_bpf, BIOCSETIF, (caddr_t)&ifr) < 0) {
return -1;
}
ic = if_list_ips(ifs, 1024);
for(i=0; i<ic; i++) {
if(!strncmp(ifs[i].ifname, ifr.ifr_name, strlen(ifr.ifr_name)) &&
(ifs[i].mac[0] || ifs[i].mac[1] || ifs[i].mac[2] ||
ifs[i].mac[3] || ifs[i].mac[4] || ifs[i].mac[5]) ) {
memcpy(my_mac, ifs[i].mac, ETH_ALEN);
break;
}
}
memset(buffer, 0, 60);
eth = (struct ether_header *)buffer;
memcpy(eth->ether_shost, my_mac, ETH_ALEN);
memcpy(eth->ether_dhost, bc_mac, ETH_ALEN);
eth->ether_type = htons(ETH_P_ARP);
arp = (struct arphdr *)(eth+1);
arp->ar_hrd = htons(ARPHRD_ETHER);
arp->ar_pro = htons(ETH_P_IP);
arp->ar_hln = ETH_ALEN;
arp->ar_pln = 4;
arp->ar_op = htons(ARPOP_REPLY);
cp = (unsigned char *)(arp+1);
memcpy(cp, my_mac, ETH_ALEN); cp+=ETH_ALEN;
memcpy(cp, &new_ip, 4); cp+=4;
dest_mac = cp;
memcpy(cp, bc_mac, ETH_ALEN); cp+=ETH_ALEN;
memcpy(cp, &r_ip, 4); cp+=4;
for(i=0;i<count;i++)
write(_if_bpf, buffer, 60);
if(remote_mac) {
memcpy(dest_mac, remote_mac, ETH_ALEN);
memcpy(eth->ether_dhost, remote_mac, ETH_ALEN);
for(i=0;i<count;i++)
write(_if_bpf, buffer, 60);
if(icmp) {
compose_ping(buffer, my_mac, remote_mac, new_ip, r_ip);
write(_if_bpf, buffer, 42);
}
}
return i;
}
#ifndef _SIZEOF_ADDR_IFREQ
#define _SIZEOF_ADDR_IFREQ(ifr) ((ifr).ifr_addr.sa_len + IFNAMSIZ)
#endif
/* Build a list of interfaces/IP-addresses/MAC adresses this
machine currently has configured.
ifs points to a buffer large enough to hold size entries */
int
if_list_ips(struct interface *ifs,
int size) {
int count=0;
struct ifaddrs *ifap, *ifa;
memset(ifs, 0, size * (&ifs[1] - &ifs[0]));
if(getifaddrs(&ifap)) return 0;
for(ifa = ifap; ifa; ifa = ifa->ifa_next) {
if(ifa->ifa_addr == NULL) continue;
/* Handle LL adresses (MAC adress) */
if(ifa->ifa_addr->sa_family == AF_LINK) {
struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
if(sdl->sdl_alen != ETH_ALEN) continue;
memset(&ifs[count], sizeof(struct interface), 0);
strncpy(ifs[count].ifname, sdl->sdl_data, sdl->sdl_nlen);
ifs[count].ifname[sdl->sdl_nlen] = '\0';
memcpy(ifs[count].mac, sdl->sdl_data+sdl->sdl_nlen, ETH_ALEN);
continue;
}
/* Not AF_INET or AF_LINK, then ignore it */
if(ifa->ifa_addr->sa_family == AF_INET6) {
if((ifa->ifa_flags & IFF_UP) && (ifa->ifa_flags & IFF_BROADCAST)) {
ifs[count].family = AF_INET6;
memcpy(&ifs[count].ip6addr, &(((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
sizeof(struct in6_addr));
memcpy(&ifs[count].netmask6, &(((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr),
sizeof(struct in6_addr));
strncpy(ifs[count].ifname, ifa->ifa_name, IFNAMSIZ);
count++;
memcpy(&ifs[count], &ifs[count-1], sizeof(struct interface));
}
}
else if(ifa->ifa_addr->sa_family == AF_INET) {
if((ifa->ifa_flags & IFF_UP) && (ifa->ifa_flags & IFF_BROADCAST)) {
ifs[count].family = AF_INET;
memcpy(&ifs[count].ipaddr, &(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr),
sizeof(struct in_addr));
memcpy(&ifs[count].netmask,
&(((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr),
sizeof(struct in_addr));
ifs[count].bcast.s_addr = ifs[count].ipaddr.s_addr | ~ifs[count].netmask.s_addr;
strncpy(ifs[count].ifname, ifa->ifa_name, IFNAMSIZ);
count++;
memcpy(&ifs[count], &ifs[count-1], sizeof(struct interface));
}
}
}
freeifaddrs(ifap);
return count;
}
int
if_down(struct interface *areq) {
int i, ic;
struct interface ifs[IFLISTSIZE];
ic = if_list_ips(ifs, IFLISTSIZE);
for(i=0; i<ic; i++) {
if((areq->family == AF_INET &&
!memcmp(&ifs[i].ipaddr, &(areq->ipaddr), sizeof(struct in_addr))) ||
(areq->family == AF_INET6 &&
!memcmp(&ifs[i].ip6addr, &(areq->ip6addr), sizeof(struct in6_addr)))) {
areq = NULL;
break;
}
}
if(areq) return -1;
areq = &ifs[i];
if(areq->family == AF_INET) {
struct sockaddr_in *a;
struct ifaliasreq toup;
memset(&toup, 0, sizeof(toup));
memcpy(&toup.ifra_name, areq->ifname, IFNAMSIZ);
a = ((struct sockaddr_in *)&toup.ifra_addr);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
memcpy(&a->sin_addr.s_addr, &areq->ipaddr, sizeof(struct in_addr));
a = ((struct sockaddr_in *)&toup.ifra_broadaddr);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
memcpy(&a->sin_addr.s_addr, &areq->bcast, sizeof(struct in_addr));
a = ((struct sockaddr_in *)&toup.ifra_mask);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
memcpy(&a->sin_addr.s_addr, &areq->netmask, sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCDIFADDR, &toup) < 0) {
_if_error = _if_error_nosuchinterface;
} else {
return 0;
}
}
else if(areq->family == AF_INET6) {
struct sockaddr_in6 *a;
struct in6_aliasreq toup = {
.ifra_flags = 0,
.ifra_lifetime = { 0, 0, 0xffffffff, 0xffffffff }
};
memcpy(&toup.ifra_name, areq->ifname, IFNAMSIZ);
a = &toup.ifra_addr;
a->sin6_len = sizeof(struct sockaddr_in6);
a->sin6_family = AF_INET6;
memcpy((void *)&a->sin6_addr, (void *)&areq->ip6addr, sizeof(struct in6_addr));
a = &toup.ifra_prefixmask;
a->sin6_len = sizeof(struct sockaddr_in6);
a->sin6_family = AF_INET6;
memcpy((void *)&a->sin6_addr, (void *)&areq->netmask6, sizeof(struct in6_addr));
if(ioctl(_if_sock6, SIOCDIFADDR_IN6, &toup) < 0) {
perror("AIFADDR");
} else {
return 0;
}
}
return -1;
}
int
if_up(struct interface *areq) {
int i, ic;
struct interface ifs[IFLISTSIZE];
ic = if_list_ips(ifs, IFLISTSIZE);
for(i=0; i<ic; i++) {
if((areq->family == AF_INET &&
!memcmp(&ifs[i].ipaddr, &(areq->ipaddr), sizeof(struct in_addr))) ||
(areq->family == AF_INET6 &&
!memcmp(&ifs[i].ip6addr, &(areq->ip6addr), sizeof(struct in6_addr)))) {
_if_error = _if_error_exists;
return 1;
}
}
if(areq->family == AF_INET) {
struct sockaddr_in *a;
struct ifaliasreq toup;
memset(&toup, 0, sizeof(toup));
memcpy(&toup.ifra_name, areq->ifname, IFNAMSIZ);
a = ((struct sockaddr_in *)&toup.ifra_addr);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
memcpy(&a->sin_addr.s_addr, &areq->ipaddr.s_addr, sizeof(struct in_addr));
a = ((struct sockaddr_in *)&toup.ifra_broadaddr);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
memcpy(&a->sin_addr.s_addr, &areq->bcast.s_addr, sizeof(struct in_addr));
a = ((struct sockaddr_in *)&toup.ifra_mask);
a->sin_len = sizeof(struct sockaddr_in);
a->sin_family = AF_INET;
/* VIFs on BSD use netmask 0xffffffff as opposed to something sane */
/* memcpy(&a->sin_addr.s_addr, &areq->netmask.s_addr, sizeof(struct in_addr)); */
memset(&a->sin_addr.s_addr, 0xff, 4);
if(ioctl(_if_sock, SIOCAIFADDR, &toup) < 0) {
perror("AIFADDR");
} else {
return 0;
}
}
else if(areq->family == AF_INET6) {
struct sockaddr_in6 *a;
struct in6_aliasreq toup = {
.ifra_flags = 0,
.ifra_lifetime = { 0, 0, 0xffffffff, 0xffffffff }
};
memcpy(&toup.ifra_name, areq->ifname, IFNAMSIZ);
a = &toup.ifra_addr;
a->sin6_len = sizeof(struct sockaddr_in6);
a->sin6_family = AF_INET6;
memcpy((void *)&a->sin6_addr, (void *)&areq->ip6addr, sizeof(struct in6_addr));
a = &toup.ifra_prefixmask;
a->sin6_len = sizeof(struct sockaddr_in6);
a->sin6_family = AF_INET6;
memcpy((void *)&a->sin6_addr, (void *)&areq->netmask6, sizeof(struct in6_addr));
if(ioctl(_if_sock6, SIOCAIFADDR_IN6, &toup) < 0) {
perror("AIFADDR");
} else {
return 0;
}
}
else return -1;
return -1;
}