-
Notifications
You must be signed in to change notification settings - Fork 1
/
ife-dlpi.c
499 lines (473 loc) · 14.1 KB
/
ife-dlpi.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* ======================================================================
* Copyright (c) 1997 Theo Schlossnagle
* Copyright (c) 1998-1999 The Johns Hopkins University.
* All rights reserved.
* The following code was written for use in the Backhand project at
* The Center for Networking and Distributed Systems at The Johns
* Hopkins University. See the NOTICE file in the mod_backhand
* distribution for more details.
* ======================================================================
*/
#include "ife.h"
#include "ife-icmp-support.h"
static int _if_sock=-1;
static int _if_dev=-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_dlpi_error[] = "DLPI error";
static char _if_error_dlpi_unexpected[] = "DLPI unexpected response";
static char _if_error_alias_up_failed[] = "alias up failed";
static char _if_error_alias_down_failed[] = "alias down failed";
static char *_if_error=_if_error_none;
int if_initialize() {
if((_if_sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
perror ("socket");
exit (EXIT_FAILURE);
}
_if_dev = -1;
return 0;
}
char *if_error() {
return _if_error;
}
static int
dlpi_attach(int fd, int instance) {
dl_attach_req_t req;
struct strbuf buf;
req.dl_primitive = DL_ATTACH_REQ;
req.dl_ppa = instance;
buf.len = sizeof(req);
buf.buf = (void *) &req;
return putmsg(fd, &buf, NULL, RS_HIPRI);
}
static int
dlpi_bind(int fd, u_long sap, u_long max_conind, u_long service_mode,
u_long conn_mgmt, u_long xidtest) {
dl_bind_req_t br;
struct strbuf ctl;
br.dl_primitive = DL_BIND_REQ;
br.dl_sap = sap;
br.dl_max_conind = max_conind;
br.dl_service_mode = service_mode;
br.dl_conn_mgmt = conn_mgmt;
br.dl_xidtest_flg = xidtest;
ctl.maxlen = 0;
ctl.len = sizeof(br);
ctl.buf = (char *)&br;
return putmsg(fd, &ctl, NULL, 0);
}
static int
dlpi_mac_req(int fd) {
dl_phys_addr_req_t req;
struct strbuf buf;
req.dl_primitive = DL_PHYS_ADDR_REQ;
req.dl_addr_type = DL_CURR_PHYS_ADDR;
buf.len = sizeof(req);
buf.buf = (void *) &req;
return putmsg(fd, &buf, NULL, RS_HIPRI);
}
static int
dlpi_get_reply(int fd, union DL_primitives *reply,
int expected_prim, int maxlen) {
struct strbuf buf;
int flags, n;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN | POLLPRI;
do {
n = poll(&pfd, 1, 1000);
} while(n == -1 && errno == EINTR);
if(n <= 0)
return -1;
buf.maxlen = maxlen;
buf.buf = (void *) reply;
flags = 0;
if(getmsg(fd, &buf, NULL, &flags) < 0) {
return -1;
}
if(buf.len < sizeof(ulong)) {
_if_error = _if_error_dlpi_unexpected;
return -1;
}
if(reply->dl_primitive == expected_prim)
return 0;
if (reply->dl_primitive == DL_ERROR_ACK)
_if_error = _if_error_dlpi_error;
else
_if_error = _if_error_dlpi_unexpected;
return -1;
}
static int
dlpi_open_and_attach(char *dev) {
int fd, instance;
char ifdev[24];
char *cp;
struct {
union DL_primitives prim;
char space[64];
} reply;
strcpy(ifdev, "/dev/");
cp = ifdev + 5;
while(*dev != '\0')
*cp++ = *dev++;
*cp = '\0';
cp--;
while(isdigit((int)*cp)) cp--;
instance = atoi(++cp);
*cp = '\0';
fd = open(ifdev, O_RDWR);
if(fd < 0) {
_if_error = _if_error_nosuchinterface;
return -1;
}
if(dlpi_attach(fd, instance) < 0) {
close(fd);
return -1;
}
if(dlpi_get_reply(fd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
close(fd);
return -1;
}
if(dlpi_bind(fd, DL_ETHER, 0, DL_CLDLS, 0, 0) < 0) {
close(fd);
return -1;
}
if(dlpi_get_reply(fd, &reply.prim, DL_BIND_ACK, sizeof(reply)) < 0) {
close(fd);
return -1;
}
return fd;
}
static int
if_get_mac_address(char *dev, char *mac) {
int fd;
struct {
union DL_primitives prim;
char space[64];
} reply;
if(_if_dev < 0)
_if_dev = dlpi_open_and_attach(dev);
if((fd = _if_dev) < 0) {
return 0;
}
if(dlpi_mac_req(fd) < 0) {
close(fd);
return 0;
}
if(dlpi_get_reply(fd, &reply.prim,DL_PHYS_ADDR_ACK,sizeof(reply)) < 0) {
close(fd);
return 0;
}
if(reply.prim.physaddr_ack.dl_addr_length != ETH_ALEN) {
_if_error = _if_error_dlpi_unexpected;
close(fd);
}
memcpy(mac, (char *)&reply+reply.prim.physaddr_ack.dl_addr_offset, ETH_ALEN);
return 1;
}
int
if_send_spoof_request(char *dev,
unsigned int new_ip, unsigned int r_ip,
unsigned char *remote_mac,
int count, int ping) {
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);
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(ð->ether_shost, my_mac, ETH_ALEN);
memcpy(ð->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;
if(_if_dev < 0) {
struct strioctl sioc;
_if_dev = dlpi_open_and_attach(dev);
if(_if_dev < 0) {
return 0;
}
sioc.ic_cmd = DLIOCRAW;
sioc.ic_timout = -1;
sioc.ic_len = 0;
sioc.ic_dp = 0;
if(ioctl(_if_dev, I_STR, &sioc) < 0) {
close(_if_dev);
_if_dev = -1;
}
}
for(i=0;i<count;i++)
write(_if_dev, buffer, 60);
if(remote_mac) {
memcpy(dest_mac, remote_mac, ETH_ALEN);
memcpy(ð->ether_dhost, remote_mac, ETH_ALEN);
for(i=0;i<count;i++)
write(_if_dev, buffer, 60);
if(ping) {
compose_ping(buffer, my_mac, remote_mac, new_ip, r_ip);
write(_if_dev, buffer, 42);
}
}
return i;
}
int
if_list_ips(struct interface *ifs,
int size) {
int count=0;
struct ifconf d;
struct ifreq *ifr, *end, *cur, *temp;
struct in_addr ipaddr;
char buffer[1024];
/* temporary storage for getting broadcast address */
temp= (struct ifreq *)buffer;
d.ifc_len= 4096*8;
d.ifc_buf= malloc (d.ifc_len);
if(ioctl (_if_sock, SIOCGIFCONF, &d) == -1) {
perror("ioctl (SIOCGIFCONF)");
free(d.ifc_buf);
return 0;
}
ifr=(struct ifreq *)(d.ifc_req);
end=(struct ifreq *)(((char *) ifr) + d.ifc_len);
while((ifr<end) && (count<size)) {
cur= ifr;
ifr = (struct ifreq *)(((char *)ifr)+sizeof(struct ifreq));
if(((struct sockaddr_in *)&cur->ifr_addr)->sin_family != AF_INET)
continue;
memcpy(&ipaddr, &(((struct sockaddr_in *)&cur->ifr_addr)->sin_addr),
sizeof(struct in_addr));
memcpy(temp, cur, sizeof(struct ifreq));
if(ioctl (_if_sock, SIOCGIFFLAGS, (char *) cur) < 0)
continue;
if((cur->ifr_flags & IFF_UP) && (cur->ifr_flags & IFF_BROADCAST)) {
memcpy(&ifs[count].ipaddr, &ipaddr, sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCGIFBRDADDR, (char *)temp) != -1)
memcpy(&ifs[count].bcast,
&(((struct sockaddr_in *)&temp->ifr_addr)->sin_addr),
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCGIFNETMASK, (char *)temp) != -1)
memcpy(&ifs[count].netmask,
&(((struct sockaddr_in *)&temp->ifr_addr)->sin_addr),
sizeof(struct in_addr));
strncpy(ifs[count].ifname, cur->ifr_name, IFNAMSIZ);
if_get_mac_address(ifs[count].ifname, ifs[count].mac);
count++;
}
}
free(d.ifc_buf);
return count;
}
int
if_down(struct interface *areq) {
int i, ic, isvirtual = 0;
struct interface ifs[1024];
ic = if_list_ips(ifs, 1024);
for(i=0; i<ic; i++) {
if(!memcmp(&ifs[i].ipaddr, &(areq->ipaddr), sizeof(struct in_addr))) {
areq = NULL;
if(strchr(ifs[i].ifname, ':')) isvirtual=1;
break;
}
}
if(areq) return -1;
areq = ifs + i;
#ifdef SIOCLIFREMOVEIF
if(isvirtual) {
struct lifreq todo;
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.lifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.lifr_addr)->sin_addr, &areq->ipaddr,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCLIFREMOVEIF, &todo) < 0) {
return -1;
}
return 0;
} else
#endif
{
/* Normal non-LIF code */
struct ifreq todo;
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.ifr_addr)->sin_family = AF_INET;
memset(&((struct sockaddr_in *)&todo.ifr_addr)->sin_addr, 0,
sizeof(struct in_addr));
ioctl(_if_sock, SIOCSIFADDR, &todo);
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
if(ioctl(_if_sock, SIOCGIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_down_failed;
return -1;
}
if(todo.ifr_flags & IFF_UP) {
todo.ifr_flags &= ~IFF_UP;
if(ioctl(_if_sock, SIOCSIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_down_failed;
return -1;
}
}
if(!isvirtual) {
/* FIXME: unplumb here */
}
}
return 0;
}
int
if_up(struct interface *areq) {
int i, ic, intexists=0;
struct interface ifs[1024];
ic = if_list_ips(ifs, 1024);
for(i=0; i<ic; i++) {
if(!memcmp(&ifs[i].ipaddr, &(areq->ipaddr), sizeof(struct in_addr))) {
_if_error = _if_error_exists;
return 1;
}
if(!strcmp(ifs[i].ifname, areq->ifname))
intexists = 1;
}
#ifdef SIOCLIFADDIF
if(intexists) {
/* Use LIFREQ */
struct lifreq todo;
char vdev[LIFNAMSIZ];
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, areq->ifname, LIFNAMSIZ);
((struct sockaddr_in *)&todo.lifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.lifr_addr)->sin_addr, &areq->ipaddr,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCLIFADDIF, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
return -1;
}
strncpy(vdev, todo.lifr_name, LIFNAMSIZ);
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, vdev, LIFNAMSIZ);
((struct sockaddr_in *)&todo.lifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.lifr_addr)->sin_addr, &areq->netmask,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCSLIFNETMASK, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto baillifremove;
}
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, vdev, LIFNAMSIZ);
((struct sockaddr_in *)&todo.lifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.lifr_addr)->sin_addr, &areq->bcast,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCSLIFBRDADDR, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto baillifremove;
}
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, vdev, LIFNAMSIZ);
if(ioctl(_if_sock, SIOCGLIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto baillifremove;
}
if(!(todo.lifr_flags & IFF_UP)) {
todo.lifr_flags |= IFF_UP;
if(ioctl(_if_sock, SIOCSLIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto baillifremove;
}
}
return 0;
baillifremove:
memset(&todo, 0, sizeof(todo));
strncpy(todo.lifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.lifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.lifr_addr)->sin_addr, &areq->ipaddr,
sizeof(struct in_addr));
ioctl(_if_sock, SIOCLIFREMOVEIF, &todo);
} else {
#endif
/* old style */
if(intexists) {
/* We are not capable of LIFing, so we need to manually up an alias */
} else {
/* Main interface */
struct ifreq todo;
/* FIXME: we need to plumb here if necessary */
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.ifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.ifr_addr)->sin_addr, &areq->ipaddr,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCSIFADDR, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
return -1;
}
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.ifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.ifr_addr)->sin_addr, &areq->netmask,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCSIFNETMASK, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto bailifremove;
}
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.ifr_addr)->sin_family = AF_INET;
memcpy(&((struct sockaddr_in *)&todo.ifr_addr)->sin_addr, &areq->bcast,
sizeof(struct in_addr));
if(ioctl(_if_sock, SIOCSIFBRDADDR, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto bailifremove;
}
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
if(ioctl(_if_sock, SIOCGIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto bailifremove;
}
if(!(todo.ifr_flags & IFF_UP)) {
todo.ifr_flags |= IFF_UP;
if(ioctl(_if_sock, SIOCSIFFLAGS, &todo) < 0) {
_if_error = _if_error_alias_up_failed;
goto bailifremove;
}
}
return 0;
bailifremove:
memset(&todo, 0, sizeof(todo));
strncpy(todo.ifr_name, areq->ifname, IFNAMSIZ);
((struct sockaddr_in *)&todo.ifr_addr)->sin_family = AF_INET;
memset(&((struct sockaddr_in *)&todo.ifr_addr)->sin_addr, 0,
sizeof(struct in_addr));
ioctl(_if_sock, SIOCSIFADDR, &todo);
}
#ifdef SIOCLIFADDIF
}
#endif
return -1;
}