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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "pool.h"
#include "ureg.h"
#include "../port/error.h"
#include "../port/netif.h"
#include "../port/etherif.h"
extern int eipfmt(Fmt*);
extern ushort ipcsum(uchar *);
static Ether *etherxx[MaxEther];
static Ether *etherprobe(int cardno, int ctlrno, char *conf);
Chan*
etherattach(char* spec)
{
ulong ctlrno;
char *conf;
Chan *chan;
ctlrno = 0;
if(*spec){
ctlrno = strtoul(spec, &conf, 0);
if(ctlrno >= MaxEther)
error(Enodev);
if(conf == spec)
error(Ebadspec);
if(*conf){
if(*conf != ':')
error(Ebadspec);
*conf++ = 0;
if(!iseve())
error(Enoattach);
if(etherxx[ctlrno] != nil)
error(Einuse);
etherxx[ctlrno] = etherprobe(-1, ctlrno, conf);
}
}
if(etherxx[ctlrno] == nil)
error(Enodev);
chan = devattach('l', spec);
if(waserror()){
chanfree(chan);
nexterror();
}
chan->dev = ctlrno;
if(etherxx[ctlrno]->attach)
etherxx[ctlrno]->attach(etherxx[ctlrno]);
poperror();
return chan;
}
static Walkqid*
etherwalk(Chan* chan, Chan* nchan, char** name, int nname)
{
return netifwalk(etherxx[chan->dev], chan, nchan, name, nname);
}
static int
etherstat(Chan* chan, uchar* dp, int n)
{
return netifstat(etherxx[chan->dev], chan, dp, n);
}
static Chan*
etheropen(Chan* chan, int omode)
{
return netifopen(etherxx[chan->dev], chan, omode);
}
static Chan*
ethercreate(Chan*, char*, int, ulong)
{
error(Eperm);
return 0;
}
static void
etherclose(Chan* chan)
{
Ether *ether = etherxx[chan->dev];
if(NETTYPE(chan->qid.path) == Ndataqid && ether->f[NETID(chan->qid.path)]->bridge)
memset(ether->mactab, 0, sizeof(ether->mactab));
netifclose(ether, chan);
}
static long
etherread(Chan* chan, void* buf, long n, vlong off)
{
Ether *ether;
ulong offset = off;
ether = etherxx[chan->dev];
if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
/*
* With some controllers it is necessary to reach
* into the chip to extract statistics.
*/
if(NETTYPE(chan->qid.path) == Nifstatqid)
return ether->ifstat(ether, buf, n, offset);
else if(NETTYPE(chan->qid.path) == Nstatqid)
ether->ifstat(ether, buf, 0, offset);
}
return netifread(ether, chan, buf, n, offset);
}
static Block*
etherbread(Chan* chan, long n, ulong offset)
{
return netifbread(etherxx[chan->dev], chan, n, offset);
}
static int
etherwstat(Chan* chan, uchar* dp, int n)
{
return netifwstat(etherxx[chan->dev], chan, dp, n);
}
static void
etherrtrace(Netfile* f, Etherpkt* pkt, int len)
{
Block *bp;
if(qwindow(f->in) <= 0)
return;
bp = iallocb(64);
if(bp == nil)
return;
memmove(bp->wp, pkt, len < 64 ? len : 64);
if(f->type != -2){
u32int ms = TK2MS(MACHP(0)->ticks);
bp->wp[58] = len>>8;
bp->wp[59] = len;
bp->wp[60] = ms>>24;
bp->wp[61] = ms>>16;
bp->wp[62] = ms>>8;
bp->wp[63] = ms;
}
bp->wp += 64;
qpass(f->in, bp);
}
static Macent*
macent(Ether *ether, uchar *ea)
{
u32int h = (ea[0] | ea[1]<<8 | ea[2]<<16 | ea[3]<<24) ^ (ea[4] | ea[5]<<8);
return ðer->mactab[h % nelem(ether->mactab)];
}
/*
* Multiplex the packet to all the connections which want it.
* If the packet is not to be used subsequently (tome || from == nil),
* attempt to simply pass it into one of the connections, thereby
* saving a copy of the data (usual case hopefully).
*/
static Block*
ethermux(Ether *ether, Block *bp, Netfile **from)
{
Block *xbp;
Etherpkt *pkt;
Netfile *f, *x, **fp;
int len, multi, tome, port, type, dispose;
len = BLEN(bp);
if(len < ETHERHDRSIZE)
goto Drop;
pkt = (Etherpkt*)bp->rp;
if(!(multi = pkt->d[0] & 1)){
tome = memcmp(pkt->d, ether->ea, Eaddrlen) == 0;
if(!tome && from != nil && ether->prom == 0)
return bp;
} else {
tome = 0;
if(from == nil && ether->prom == 0
&& memcmp(pkt->d, ether->bcast, Eaddrlen) != 0
&& !activemulti(ether, pkt->d, Eaddrlen))
goto Drop;
}
port = -1;
if(ether->prom){
if((from == nil || (*from)->bridge) && (pkt->s[0] & 1) == 0){
Macent *t = macent(ether, pkt->s);
t->port = from == nil ? 0 : 1+(from - ether->f);
memmove(t->ea, pkt->s, Eaddrlen);
}
if(!tome && !multi){
Macent *t = macent(ether, pkt->d);
if(memcmp(t->ea, pkt->d, Eaddrlen) == 0)
port = t->port;
}
}
x = nil;
type = (pkt->type[0]<<8)|pkt->type[1];
dispose = tome || from == nil || port > 0;
for(fp = ether->f; fp < ðer->f[Ntypes]; fp++){
if((f = *fp) == nil)
continue;
if(f->type != type && f->type >= 0)
continue;
if(!tome && !multi && !f->prom)
continue;
if(f->bridge){
if(tome || fp == from)
continue;
if(port >= 0 && port != 1+(fp - ether->f))
continue;
}
if(f->headersonly || f->type == -2){
etherrtrace(f, pkt, len);
continue;
}
if(dispose && x == nil)
x = f;
else if((xbp = iallocb(len)) != nil){
memmove(xbp->wp, pkt, len);
xbp->wp += len;
xbp->flag = bp->flag;
if(qpass(f->in, xbp) < 0)
ether->soverflows++;
} else
ether->soverflows++;
}
if(x != nil){
if(qpass(x->in, bp) < 0)
ether->soverflows++;
return nil;
}
if(dispose){
Drop: freeb(bp);
return nil;
}
return bp;
}
void
etheriq(Ether* ether, Block* bp)
{
ether->inpackets++;
ethermux(ether, bp, nil);
}
static void
etheroq(Ether* ether, Block* bp, Netfile **from)
{
if((*from)->bridge == 0)
memmove(((Etherpkt*)bp->rp)->s, ether->ea, Eaddrlen);
bp = ethermux(ether, bp, from);
if(bp == nil)
return;
ether->outpackets++;
qbwrite(ether->oq, bp);
if(ether->transmit != nil)
ether->transmit(ether);
}
static long
etherwrite(Chan* chan, void* buf, long n, vlong)
{
Ether *ether = etherxx[chan->dev];
Block *bp;
int nn, onoff;
Cmdbuf *cb;
if(NETTYPE(chan->qid.path) != Ndataqid) {
nn = netifwrite(ether, chan, buf, n);
if(nn >= 0)
return nn;
cb = parsecmd(buf, n);
if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
if(cb->nf <= 1)
onoff = 1;
else
onoff = atoi(cb->f[1]);
qnoblock(ether->oq, onoff);
free(cb);
return n;
}
free(cb);
if(ether->ctl!=nil)
return ether->ctl(ether,buf,n);
error(Ebadctl);
}
if(n > ether->maxmtu)
error(Etoobig);
if(n < ether->minmtu)
error(Etoosmall);
bp = allocb(n);
if(waserror()){
freeb(bp);
nexterror();
}
memmove(bp->rp, buf, n);
poperror();
bp->wp += n;
etheroq(ether, bp, ðer->f[NETID(chan->qid.path)]);
return n;
}
static long
etherbwrite(Chan* chan, Block* bp, ulong)
{
Ether *ether;
long n = BLEN(bp);
if(NETTYPE(chan->qid.path) != Ndataqid){
if(waserror()) {
freeb(bp);
nexterror();
}
n = etherwrite(chan, bp->rp, n, 0);
poperror();
freeb(bp);
return n;
}
ether = etherxx[chan->dev];
if(n > ether->maxmtu){
freeb(bp);
error(Etoobig);
}
if(n < ether->minmtu){
freeb(bp);
error(Etoosmall);
}
etheroq(ether, bp, ðer->f[NETID(chan->qid.path)]);
return n;
}
static struct {
char* type;
int (*reset)(Ether*);
} cards[MaxEther+1];
void
addethercard(char* t, int (*r)(Ether*))
{
static int ncard;
if(ncard == MaxEther)
panic("too many ether cards");
cards[ncard].type = t;
cards[ncard].reset = r;
ncard++;
}
static Ether*
etherprobe(int cardno, int ctlrno, char *conf)
{
int i, lg;
ulong mb, bsz;
Ether *ether;
ether = malloc(sizeof(Ether));
if(ether == nil){
print("etherprobe: no memory for Ether\n");
return nil;
}
memset(ether, 0, sizeof(Ether));
ether->tbdf = BUSUNKNOWN;
ether->irq = -1;
ether->ctlrno = ctlrno;
ether->mbps = 10;
ether->minmtu = ETHERMINTU;
ether->maxmtu = ETHERMAXTU;
if(cardno < 0){
if(conf != nil){
kstrdup(ðer->type, conf);
ether->nopt = tokenize(ether->type, ether->opt, nelem(ether->opt));
if(ether->nopt < 1)
goto Nope;
memmove(ðer->opt[0], ðer->opt[1], --ether->nopt*sizeof(ether->opt[0]));
} else if(isaconfig("ether", ctlrno, ether) == 0)
goto Nope;
for(cardno = 0; cards[cardno].type != nil; cardno++)
if(cistrcmp(cards[cardno].type, ether->type) == 0)
break;
if(cards[cardno].type == nil)
goto Nope;
for(i = 0; i < ether->nopt; i++){
if(strncmp(ether->opt[i], "ea=", 3) == 0){
if(parseether(ether->ea, ðer->opt[i][3]))
memset(ether->ea, 0, Eaddrlen);
}
}
}
if(cardno >= MaxEther || cards[cardno].type == nil)
goto Nope;
snprint(ether->name, sizeof(ether->name), "ether%d", ctlrno);
if(cards[cardno].reset(ether) < 0){
Nope:
if(conf != nil) free(ether->type); /* see kstrdup() above */
free(ether);
return nil;
}
ether->type = cards[cardno].type;
print("#l%d: %s: %dMbps port 0x%luX irq %d ea %E\n",
ctlrno, ether->type, ether->mbps, ether->port, ether->irq, ether->ea);
/* compute log10(ether->mbps) into lg */
for(lg = 0, mb = ether->mbps; mb >= 10; lg++)
mb /= 10;
if (lg > 0)
lg--;
if (lg > 14) /* 2^(14+17) = 2³¹ */
lg = 14;
/* allocate larger output queues for higher-speed interfaces */
bsz = 1UL << (lg + 17); /* 2¹⁷ = 128K, bsz = 2ⁿ × 128K */
while (bsz > mainmem->maxsize / 8 && bsz > 128*1024)
bsz /= 2;
netifinit(ether, ether->name, Ntypes, bsz);
if(ether->oq == nil) {
ether->oq = qopen(bsz, Qmsg, 0, 0);
ether->limit = bsz;
}
if(ether->oq == nil)
panic("etherreset %s: can't allocate output queue of %ld bytes", ether->name, bsz);
ether->alen = Eaddrlen;
memmove(ether->addr, ether->ea, Eaddrlen);
memset(ether->bcast, 0xFF, Eaddrlen);
return ether;
}
static void netconsole(int);
static void
etherreset(void)
{
Ether *ether;
int cardno, ctlrno;
fmtinstall('E', eipfmt);
for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
if((ether = etherprobe(-1, ctlrno, nil)) == nil)
continue;
etherxx[ctlrno] = ether;
}
if(getconf("*noetherprobe") == nil){
cardno = ctlrno = 0;
while(cards[cardno].type != nil && ctlrno < MaxEther){
if(etherxx[ctlrno] != nil){
ctlrno++;
continue;
}
if((ether = etherprobe(cardno, ctlrno, nil)) == nil){
cardno++;
continue;
}
etherxx[ctlrno] = ether;
ctlrno++;
}
}
netconsole(1);
}
static void
ethershutdown(void)
{
Ether *ether;
int i;
netconsole(0);
for(i = 0; i < MaxEther; i++){
ether = etherxx[i];
if(ether == nil)
continue;
if(ether->shutdown == nil) {
print("#l%d: no shutdown function\n", i);
continue;
}
(*ether->shutdown)(ether);
}
}
#define POLY 0xedb88320
/* really slow 32 bit crc for ethers */
ulong
ethercrc(uchar *p, int len)
{
int i, j;
ulong crc, b;
crc = 0xffffffff;
for(i = 0; i < len; i++){
b = *p++;
for(j = 0; j < 8; j++){
crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
b >>= 1;
}
}
return crc;
}
Dev etherdevtab = {
'l',
"ether",
etherreset,
devinit,
ethershutdown,
etherattach,
etherwalk,
etherstat,
etheropen,
ethercreate,
etherclose,
etherread,
etherbread,
etherwrite,
etherbwrite,
devremove,
etherwstat,
};
enum { PktHdr = 42 };
typedef struct Netconsole Netconsole;
struct Netconsole {
char buf[512];
int n;
Lock;
Ether *ether;
};
static Netconsole *netcons;
static void
netconsputc(Uart *, int c)
{
char *p;
u16int cs;
ilock(netcons);
netcons->buf[netcons->n++] = c;
if(c != '\n' && netcons->n < sizeof(netcons->buf)){
iunlock(netcons);
return;
}
p = netcons->buf;
p[16] = netcons->n - 14 >> 8;
p[17] = netcons->n - 14;
p[24] = 0;
p[25] = 0;
cs = ipcsum((uchar*) p + 14);
p[24] = cs >> 8;
p[25] = cs;
p[38] = netcons->n - 34 >> 8;
p[39] = netcons->n - 34;
memmove(p+Eaddrlen, netcons->ether->ea, Eaddrlen);
qiwrite(netcons->ether->oq, p, netcons->n);
netcons->n = PktHdr;
iunlock(netcons);
if(netcons->ether->transmit != nil)
netcons->ether->transmit(netcons->ether);
}
static PhysUart netconsphys = { .putc = netconsputc };
static Uart netconsuart = { .phys = &netconsphys };
static void
netconsole(int on)
{
char *p;
char *r;
int i;
int srcport, devno, dstport;
u8int srcip[4], dstip[4];
u64int dstmac;
Netconsole *nc;
if(!on){
if(consuart == &netconsuart)
consuart = nil;
return;
}
if((p = getconf("console")) == nil || strncmp(p, "net ", 4) != 0)
return;
p += 4;
for(i = 0; i < 4; i++){
srcip[i] = strtol(p, &r, 0);
p = r + 1;
if(i == 3) break;
if(*r != '.') goto err;
}
if(*r == '!'){
srcport = strtol(p, &r, 0);
p = r + 1;
}else
srcport = 6665;
if(*r == '/'){
devno = strtol(p, &r, 0);
p = r + 1;
}else
devno = 0;
if(*r != ',') goto err;
for(i = 0; i < 4; i++){
dstip[i] = strtol(p, &r, 0);
p = r + 1;
if(i == 3) break;
if(*r != '.') goto err;
}
if(*r == '!'){
dstport = strtol(p, &r, 0);
p = r + 1;
}else
dstport = 6666;
if(*r == '/'){
dstmac = strtoull(p, &r, 16);
if(r - p != 12) goto err;
}else
dstmac = ((uvlong)-1) >> 16;
if(*r != 0) goto err;
if(devno >= MaxEther || etherxx[devno] == nil){
print("netconsole: no device #l%d\n", devno);
return;
}
nc = malloc(sizeof(Netconsole));
if(nc == nil){
print("netconsole: out of memory");
return;
}
memset(nc, 0, sizeof(Netconsole));
nc->ether = etherxx[devno];
uchar header[PktHdr] = {
/* 0 */ dstmac >> 40, dstmac >> 32, dstmac >> 24, dstmac >> 16, dstmac >> 8, dstmac >> 0,
/* 6 */ 0, 0, 0, 0, 0, 0,
/* 12 */ 0x08, 0x00,
/* 14 */ 0x45, 0x00,
/* 16 */ 0x00, 0x00, /* total length */
/* 18 */ 0x00, 0x00, 0x00, 0x00,
/* 22 */ 64, /* ttl */
/* 23 */ 0x11, /* protocol */
/* 24 */ 0x00, 0x00, /* checksum */
/* 26 */ srcip[0], srcip[1], srcip[2], srcip[3],
/* 30 */ dstip[0], dstip[1], dstip[2], dstip[3],
/* 34 */ srcport >> 8, srcport, dstport >> 8, dstport,
/* 38 */ 0x00, 0x00, /* length */
/* 40 */ 0x00, 0x00 /* checksum */
};
memmove(nc->buf, header, PktHdr);
nc->n = PktHdr;
netcons = nc;
consuart = &netconsuart;
return;
err:
print("netconsole: invalid string %#q\n", getconf("console"));
print("netconsole: usage: srcip[!srcport][/srcdev],dstip[!dstport][/dstmac]\n");
}
|