-
Notifications
You must be signed in to change notification settings - Fork 8
/
iface.c
602 lines (546 loc) · 13.8 KB
/
iface.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* IP interface control and configuration routines
* Copyright 1991 Phil Karn, KA9Q
*/
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "proc.h"
#include "iface.h"
#include "ip.h"
#include "icmp.h"
#include "netuser.h"
#include "ax25.h"
#include "enet.h"
#include "pktdrvr.h"
#include "cmdparse.h"
#include "commands.h"
#include "trace.h"
static void showiface(struct iface *ifp);
static int mask2width(int32 mask);
static int ifipaddr(int argc,char *argv[],void *p);
static int iflinkadr(int argc,char *argv[],void *p);
static int ifbroad(int argc,char *argv[],void *p);
static int ifnetmsk(int argc,char *argv[],void *p);
static int ifrxbuf(int argc,char *argv[],void *p);
static int ifmtu(int argc,char *argv[],void *p);
static int ifforw(int argc,char *argv[],void *p);
static int ifencap(int argc,char *argv[],void *p);
static int iftxqlen(int argc,char *argv[],void *p);
/* Interface list header */
struct iface *Ifaces = &Loopback;
/* Loopback pseudo-interface */
struct iface Loopback = {
&Encap, /* Link to next entry */
"loopback", /* name */
0x7f000001L, /* addr 127.0.0.1 */
0xffffffffL, /* broadcast 255.255.255.255 */
0xffffffffL, /* netmask 255.255.255.255 */
MAXINT16, /* mtu No limit */
0, /* trace */
NULL, /* trfp */
NULL, /* forw */
NULL, /* rxproc */
NULL, /* txproc */
NULL, /* supv */
NULL, /* outq */
0, /* outlim */
0, /* txbusy */
NULL, /* dstate */
NULL, /* dtickle */
NULL, /* dstatus */
0, /* dev */
NULL, /* (*ioctl) */
NULL, /* (*iostatus) */
NULL, /* (*stop) */
NULL, /* hwaddr */
NULL, /* extension */
0, /* xdev */
&Iftypes[0], /* iftype */
NULL, /* (*send) */
NULL, /* (*output) */
NULL, /* (*raw) */
NULL, /* (*status) */
NULL, /* (*discard) */
NULL, /* (*echo) */
0, /* ipsndcnt */
0, /* rawsndcnt */
0, /* iprecvcnt */
0, /* rawrcvcnt */
0, /* lastsent */
0, /* lastrecv */
};
/* Encapsulation pseudo-interface */
struct iface Encap = {
NULL,
"encap", /* name */
INADDR_ANY, /* addr 0.0.0.0 */
0xffffffffL, /* broadcast 255.255.255.255 */
0xffffffffL, /* netmask 255.255.255.255 */
MAXINT16, /* mtu No limit */
0, /* trace */
NULL, /* trfp */
NULL, /* forw */
NULL, /* rxproc */
NULL, /* txproc */
NULL, /* supv */
NULL, /* outq */
0, /* outlim */
0, /* txbusy */
NULL, /* dstate */
NULL, /* dtickle */
NULL, /* dstatus */
0, /* dev */
NULL, /* (*ioctl) */
NULL, /* (*iostatus) */
NULL, /* (*stop) */
NULL, /* hwaddr */
NULL, /* extension */
0, /* xdev */
&Iftypes[0], /* iftype */
ip_encap, /* (*send) */
NULL, /* (*output) */
NULL, /* (*raw) */
NULL, /* (*status) */
NULL, /* (*discard) */
NULL, /* (*echo) */
0, /* ipsndcnt */
0, /* rawsndcnt */
0, /* iprecvcnt */
0, /* rawrcvcnt */
0, /* lastsent */
0, /* lastrecv */
};
char Noipaddr[] = "IP address field missing, and ip address not set\n";
struct cmds Ifcmds[] = {
"broadcast", ifbroad, 0, 2, NULL,
"encapsulation", ifencap, 0, 2, NULL,
"forward", ifforw, 0, 2, NULL,
"ipaddress", ifipaddr, 0, 2, NULL,
"linkaddress", iflinkadr, 0, 2, NULL,
"mtu", ifmtu, 0, 2, NULL,
"netmask", ifnetmsk, 0, 2, NULL,
"txqlen", iftxqlen, 0, 2, NULL,
"rxbuf", ifrxbuf, 0, 2, NULL,
NULL,
};
/*
* General purpose interface transmit task, one for each device that can
* send IP datagrams. It waits on the interface's IP output queue (outq),
* extracts IP datagrams placed there in priority order by ip_route(),
* and sends them to the device's send routine.
*/
void
if_tx(int dev,void *arg1,void *unused)
{
struct mbuf *bp; /* Buffer to send */
struct iface *iface; /* Pointer to interface control block */
struct qhdr qhdr;
iface = arg1;
for(;;){
while(iface->outq == NULL)
kwait(&iface->outq);
iface->txbusy = 1;
bp = dequeue(&iface->outq);
pullup(&bp,&qhdr,sizeof(qhdr));
if(iface->dtickle != NULL && (*iface->dtickle)(iface) == -1){
#ifdef notdef /* Confuses some non-compliant hosts */
struct ip ip;
/* Link redial failed; bounce with unreachable */
ntohip(&ip,&bp);
icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,
NULL);
#endif
free_p(&bp);
} else {
(*iface->send)(&bp,iface,qhdr.gateway,qhdr.tos);
}
iface->txbusy = 0;
/* Let other tasks run, just in case send didn't block */
kwait(NULL);
}
}
/* Process packets in the Hopper */
void
network(int i,void *v1,void *v2)
{
struct mbuf *bp;
char i_state;
struct iftype *ift;
struct iface *ifp;
loop:
for(;;){
i_state = dirps();
bp = Hopper;
if(bp != NULL){
bp = dequeue(&Hopper);
restore(i_state);
break;
}
restore(i_state);
kwait(&Hopper);
}
/* Process the input packet */
pullup(&bp,&ifp,sizeof(ifp));
if(ifp != NULL){
ifp->rawrecvcnt++;
ifp->lastrecv = secclock();
ift = ifp->iftype;
} else {
ift = &Iftypes[0];
}
dump(ifp,IF_TRACE_IN,bp);
if(ift->rcvf != NULL)
(*ift->rcvf)(ifp,&bp);
else
free_p(&bp); /* Nowhere to send it */
/* Let everything else run - this keeps the system from wedging
* when we're hit by a big burst of packets
*/
kwait(NULL);
goto loop;
}
/* put mbuf into Hopper for network task
* returns 0 if OK
*/
int
net_route(struct iface *ifp,struct mbuf **bpp)
{
pushdown(bpp,&ifp,sizeof(ifp));
enqueue(&Hopper,bpp);
return 0;
}
/* Null send and output routines for interfaces without link level protocols */
int
nu_send(struct mbuf **bpp,struct iface *ifp,int32 gateway,uint8 tos)
{
return (*ifp->raw)(ifp,bpp);
}
int
nu_output(struct iface *ifp,uint8 *dest,uint8 *src,uint16 type,struct mbuf **bpp)
{
return (*ifp->raw)(ifp,bpp);
}
/* Set interface parameters */
int
doifconfig(int argc,char *argv[],void *p)
{
struct iface *ifp;
int i;
if(argc < 2){
for(ifp = Ifaces;ifp != NULL;ifp = ifp->next)
showiface(ifp);
return 0;
}
if((ifp = if_lookup(argv[1])) == NULL){
printf("Interface %s unknown\n",argv[1]);
return 1;
}
if(argc == 2){
showiface(ifp);
if(ifp->show != NULL){
(*ifp->show)(ifp);
}
return 0;
}
if(argc == 3){
printf("Argument missing\n");
return 1;
}
for(i=2;i<argc-1;i+=2)
subcmd(Ifcmds,3,&argv[i-1],ifp);
return 0;
}
/* Set interface IP address */
static int
ifipaddr(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
ifp->addr = resolve(argv[1]);
return 0;
}
/* Set link (hardware) address */
static int
iflinkadr(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
if(ifp->iftype == NULL || ifp->iftype->scan == NULL){
printf("Can't set link address\n");
return 1;
}
if(ifp->hwaddr != NULL)
free(ifp->hwaddr);
ifp->hwaddr = mallocw(ifp->iftype->hwalen);
(*ifp->iftype->scan)(ifp->hwaddr,argv[1]);
return 0;
}
/* Set interface broadcast address. This is actually done
* by installing a private entry in the routing table.
*/
static int
ifbroad(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
struct route *rp;
rp = rt_blookup(ifp->broadcast,32);
if(rp != NULL && rp->iface == ifp)
rt_drop(ifp->broadcast,32);
ifp->broadcast = resolve(argv[1]);
rt_add(ifp->broadcast,32,0L,ifp,1L,0L,1);
return 0;
}
/* Set the network mask. This is actually done by installing
* a routing entry.
*/
static int
ifnetmsk(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
struct route *rp;
/* Remove old entry if it exists */
rp = rt_blookup(ifp->addr & ifp->netmask,mask2width(ifp->netmask));
if(rp != NULL)
rt_drop(rp->target,rp->bits);
ifp->netmask = htol(argv[1]);
rt_add(ifp->addr,mask2width(ifp->netmask),0L,ifp,0L,0L,0);
return 0;
}
/* Command to set interface encapsulation mode */
static int
ifencap(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
if(setencap(ifp,argv[1]) != 0){
printf("Encapsulation mode '%s' unknown\n",argv[1]);
return 1;
}
return 0;
}
/* Function to set encapsulation mode */
int
setencap(struct iface *ifp,char *mode)
{
struct iftype *ift;
for(ift = &Iftypes[0];ift->name != NULL;ift++)
if(strnicmp(ift->name,mode,strlen(mode)) == 0)
break;
if(ift->name == NULL)
return -1;
if(ifp != NULL){
ifp->iftype = ift;
ifp->send = ift->send;
ifp->output = ift->output;
}
return 0;
}
/* Set interface receive buffer size */
static int
ifrxbuf(int argc,char *argv[],void *p)
{
return 0; /* To be written */
}
/* Set interface Maximum Transmission Unit */
static int
ifmtu(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
ifp->mtu = atoi(argv[1]);
return 0;
}
/* Set interface forwarding */
static int
ifforw(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
ifp->forw = if_lookup(argv[1]);
if(ifp->forw == ifp)
ifp->forw = NULL;
return 0;
}
/* Display the parameters for a specified interface */
static void
showiface(struct iface *ifp)
{
char tmp[25];
printf("%-10s IP addr %s MTU %u Link encap %s\n",ifp->name,
inet_ntoa(ifp->addr),(int)ifp->mtu,
ifp->iftype != NULL ? ifp->iftype->name : "not set");
if(ifp->iftype != NULL && ifp->iftype->format != NULL && ifp->hwaddr != NULL){
printf(" Link addr %s\n",
(*ifp->iftype->format)(tmp,ifp->hwaddr));
}
printf(" trace 0x%x netmask 0x%08lx broadcast %s\n",
ifp->trace,ifp->netmask,inet_ntoa(ifp->broadcast));
if(ifp->forw != NULL)
printf(" output forward to %s\n",ifp->forw->name);
printf(" sent: ip %lu tot %lu idle %s qlen %u",
ifp->ipsndcnt,ifp->rawsndcnt,tformat(secclock() - ifp->lastsent),
len_q(ifp->outq));
if(ifp->outlim != 0)
printf("/%u",ifp->outlim);
if(ifp->txbusy)
printf(" BUSY");
printf("\n");
printf(" recv: ip %lu tot %lu idle %s\n",
ifp->iprecvcnt,ifp->rawrecvcnt,tformat(secclock() - ifp->lastrecv));
}
/* Command to detach an interface */
int
dodetach(int argc,char *argv[],void *p)
{
register struct iface *ifp;
if((ifp = if_lookup(argv[1])) == NULL){
printf("Interface %s unknown\n",argv[1]);
return 1;
}
if(if_detach(ifp) == -1)
printf("Can't detach loopback or encap interface\n");
return 0;
}
/* Detach a specified interface */
int
if_detach(struct iface *ifp)
{
struct iface *iftmp;
struct route *rp,*rptmp;
int i,j;
if(ifp == &Loopback || ifp == &Encap)
return -1;
/* Drop all routes that point to this interface */
if(R_default.iface == ifp)
rt_drop(0L,0); /* Drop default route */
for(i=0;i<HASHMOD;i++){
for(j=0;j<32;j++){
for(rp = Routes[j][i];rp != NULL;rp = rptmp){
/* Save next pointer in case we delete this entry */
rptmp = rp->next;
if(rp->iface == ifp)
rt_drop(rp->target,rp->bits);
}
}
}
/* Unforward any other interfaces forwarding to this one */
for(iftmp = Ifaces;iftmp != NULL;iftmp = iftmp->next){
if(iftmp->forw == ifp)
iftmp->forw = NULL;
}
/* Call device shutdown routine, if any */
if(ifp->stop != NULL)
(*ifp->stop)(ifp);
killproc(ifp->rxproc);
killproc(ifp->txproc);
killproc(ifp->supv);
/* Free allocated memory associated with this interface */
if(ifp->name != NULL)
free(ifp->name);
if(ifp->hwaddr != NULL)
free(ifp->hwaddr);
/* Remove from interface list */
if(ifp == Ifaces){
Ifaces = ifp->next;
} else {
/* Search for entry just before this one
* (necessary because list is only singly-linked.)
*/
for(iftmp = Ifaces;iftmp != NULL ;iftmp = iftmp->next)
if(iftmp->next == ifp)
break;
if(iftmp != NULL && iftmp->next == ifp)
iftmp->next = ifp->next;
}
/* Finally free the structure itself */
free(ifp);
return 0;
}
static int
iftxqlen(int argc,char *argv[],void *p)
{
struct iface *ifp = p;
setint(&ifp->outlim,"TX queue limit",argc,argv);
return 0;
}
/* Given the ascii name of an interface, return a pointer to the structure,
* or NULL if it doesn't exist
*/
struct iface *
if_lookup(char *name)
{
register struct iface *ifp;
for(ifp = Ifaces; ifp != NULL; ifp = ifp->next)
if(strcmp(ifp->name,name) == 0)
break;
return ifp;
}
/* Return iface pointer if 'addr' belongs to one of our interfaces,
* NULL otherwise.
* This is used to tell if an incoming IP datagram is for us, or if it
* has to be routed.
*/
struct iface *
ismyaddr(int32 addr)
{
register struct iface *ifp;
if(addr == INADDR_ANY)
return &Loopback;
for(ifp = Ifaces; ifp != NULL; ifp = ifp->next)
if(addr == ifp->addr)
break;
return ifp;
}
/* Given a network mask, return the number of contiguous 1-bits starting
* from the most significant bit.
*/
static int
mask2width(int32 mask)
{
int width,i;
width = 0;
for(i = 31;i >= 0;i--){
if(!(mask & (1L << i)))
break;
width++;
}
return width;
}
/* return buffer with name + comment */
char *
if_name(struct iface *ifp,char *comment)
{
char *result;
result = mallocw(strlen(ifp->name) + strlen(comment) + 1);
strcpy(result,ifp->name);
strcat(result,comment);
return result;
}
/* Raw output routine that tosses all packets. Used by dialer, tip, etc */
int
bitbucket(struct iface *ifp,struct mbuf **bpp)
{
free_p(bpp);
return 0;
}
/*
* dial <iface> <seconds> [device dependent args] (begin autodialing)
* dial <iface> 0 (stop autodialing)
* dial <iface> (display status)
*/
int
dodialer(int argc,char *argv[],void *p)
{
struct iface *ifp;
int32 timeout;
if((ifp = if_lookup(argv[1])) == NULL){
printf("Interface %s unknown\n",argv[1]);
return 1;
}
if(argc < 3){
if(ifp->iftype->dstat != NULL)
(*ifp->iftype->dstat)(ifp);
return 0;
}
if(ifp->iftype->dinit == NULL){
printf("Dialing not supported on %s\n",argv[1]);
return 1;
}
timeout = atol(argv[2]) * 1000L;
return (*ifp->iftype->dinit)(ifp,timeout,argc-3,argv+3);
}