2 #include "../port/lib.h"
6 #include "../port/error.h"
8 #include "../port/netif.h"
12 typedef struct Etherhdr Etherhdr;
20 static uchar ipbroadcast[IPaddrlen] = {
27 static uchar etherbroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
29 static void etherread4(void *a);
30 static void etherread6(void *a);
31 static void etherbind(Ipifc *ifc, int argc, char **argv);
32 static void etherunbind(Ipifc *ifc);
33 static void etherbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip);
34 static void etheraddmulti(Ipifc *ifc, uchar *a, uchar *ia);
35 static void etherremmulti(Ipifc *ifc, uchar *a, uchar *ia);
36 static Block* multicastarp(Fs *f, Arpent *a, Medium*, uchar *mac);
37 static void sendarp(Ipifc *ifc, Arpent *a);
38 static void sendgarp(Ipifc *ifc, uchar*);
39 static int multicastea(uchar *ea, uchar *ip);
40 static void recvarpproc(void*);
41 static void resolveaddr6(Ipifc *ifc, Arpent *a);
42 static void etherpref2addr(uchar *pref, uchar *ea);
54 .addmulti= etheraddmulti,
55 .remmulti= etherremmulti,
58 .pref2addr= etherpref2addr,
71 .addmulti= etheraddmulti,
72 .remmulti= etherremmulti,
75 .pref2addr= etherpref2addr,
78 typedef struct Etherrock Etherrock;
81 Fs *f; /* file system we belong to */
82 Proc *arpp; /* arp process */
83 Proc *read4p; /* reading process (v4)*/
84 Proc *read6p; /* reading process (v6)*/
85 Chan *mchan4; /* Data channel for v4 */
86 Chan *achan; /* Arp channel */
87 Chan *cchan4; /* Control channel for v4 */
88 Chan *mchan6; /* Data channel for v6 */
89 Chan *cchan6; /* Control channel for v6 */
93 * ethernet arp request
101 typedef struct Etherarp Etherarp;
118 static char *nbmsg = "nonblocking";
121 * called to bind an IP ifc to an ethernet device
122 * called with ifc wlock'd
125 etherbind(Ipifc *ifc, int argc, char **argv)
127 Chan *mchan4, *cchan4, *achan, *mchan6, *cchan6, *schan;
128 char addr[Maxpath]; //char addr[2*KNAMELEN];
129 char dir[Maxpath]; //char dir[2*KNAMELEN];
138 mchan4 = cchan4 = achan = mchan6 = cchan6 = nil;
157 * open ipv4 conversation
159 * the dial will fail if the type is already open on
162 snprint(addr, sizeof(addr), "%s!0x800", argv[2]); /* ETIP4 */
163 mchan4 = chandial(addr, nil, dir, &cchan4);
166 * make it non-blocking
168 devtab[cchan4->type]->write(cchan4, nbmsg, strlen(nbmsg), 0);
171 * get mac address and speed
173 snprint(addr, sizeof(addr), "%s/stats", argv[2]);
175 schan = namec(addr, Aopen, OREAD, 0);
180 n = devtab[schan->type]->read(schan, buf, 511, 0);
185 ptr = strstr(buf, "addr: ");
189 parsemac(ifc->mac, ptr, 6);
191 ptr = strstr(buf, "mbps: ");
194 ifc->mbps = atoi(ptr);
199 * open arp conversation
201 snprint(addr, sizeof(addr), "%s!0x806", argv[2]); /* ETARP */
202 achan = chandial(addr, nil, nil, nil);
205 * open ipv6 conversation
207 * the dial will fail if the type is already open on
210 snprint(addr, sizeof(addr), "%s!0x86DD", argv[2]); /* ETIP6 */
211 mchan6 = chandial(addr, nil, dir, &cchan6);
214 * make it non-blocking
216 devtab[cchan6->type]->write(cchan6, nbmsg, strlen(nbmsg), 0);
218 er = smalloc(sizeof(*er));
224 er->f = ifc->conv->p->f;
230 kproc("etherread4", etherread4, ifc);
231 kproc("recvarpproc", recvarpproc, ifc);
232 kproc("etherread6", etherread6, ifc);
236 * called with ifc wlock'd
239 etherunbind(Ipifc *ifc)
241 Etherrock *er = ifc->arg;
244 postnote(er->read4p, 1, "unbind", 0);
246 postnote(er->read6p, 1, "unbind", 0);
248 postnote(er->arpp, 1, "unbind", 0);
250 /* wait for readers to die */
251 while(er->arpp != 0 || er->read4p != 0 || er->read6p != 0)
252 tsleep(&up->sleep, return0, 0, 300);
254 if(er->mchan4 != nil)
258 if(er->cchan4 != nil)
260 if(er->mchan6 != nil)
262 if(er->cchan6 != nil)
269 * called by ipoput with a single block to write with ifc rlock'd
272 etherbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip)
277 Etherrock *er = ifc->arg;
279 /* get mac address of destination */
280 a = arpget(er->f->arp, bp, version, ifc, ip, mac);
282 /* check for broadcast or multicast */
283 bp = multicastarp(er->f, a, ifc->m, mac);
290 resolveaddr6(ifc, a);
293 panic("etherbwrite: version %d", version);
299 /* make it a single block with space for the ether header */
300 bp = padblock(bp, ifc->m->hsize);
302 bp = concatblock(bp);
303 if(BLEN(bp) < ifc->mintu)
304 bp = adjustblock(bp, ifc->mintu);
305 eh = (Etherhdr*)bp->rp;
307 /* copy in mac addresses and ether type */
308 memmove(eh->s, ifc->mac, sizeof(eh->s));
309 memmove(eh->d, mac, sizeof(eh->d));
315 devtab[er->mchan4->type]->bwrite(er->mchan4, bp, 0);
320 devtab[er->mchan6->type]->bwrite(er->mchan6, bp, 0);
323 panic("etherbwrite2: version %d", version);
330 * process to read from the ethernet
341 er->read4p = up; /* hide identity under a rock for unbind */
347 bp = devtab[er->mchan4->type]->bread(er->mchan4, ifc->maxtu, 0);
357 if(ifc->lifc == nil || BLEN(bp) <= ifc->m->hsize)
360 bp->rp += ifc->m->hsize;
361 ipiput4(er->f, ifc, bp);
370 * process to read from the ethernet, IPv6
381 er->read6p = up; /* hide identity under a rock for unbind */
387 bp = devtab[er->mchan6->type]->bread(er->mchan6, ifc->maxtu, 0);
397 if(ifc->lifc == nil || BLEN(bp) <= ifc->m->hsize)
400 bp->rp += ifc->m->hsize;
401 ipiput6(er->f, ifc, bp);
409 etheraddmulti(Ipifc *ifc, uchar *a, uchar *)
413 Etherrock *er = ifc->arg;
416 version = multicastea(mac, a);
417 sprint(buf, "addmulti %E", mac);
420 devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
423 devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
426 panic("etheraddmulti: version %d", version);
431 etherremmulti(Ipifc *ifc, uchar *a, uchar *)
435 Etherrock *er = ifc->arg;
438 version = multicastea(mac, a);
439 sprint(buf, "remmulti %E", mac);
442 devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
445 devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
448 panic("etherremmulti: version %d", version);
453 * send an ethernet arp
454 * (only v4, v6 uses the neighbor discovery, rfc1970)
457 sendarp(Ipifc *ifc, Arpent *a)
462 Etherrock *er = ifc->arg;
464 /* don't do anything if it's been less than a second since the last */
465 if(NOW - a->ctime < 1000){
466 arprelease(er->f->arp, a);
470 /* remove all but the last message */
471 while((bp = a->hold) != nil){
478 /* try to keep it around for a second more */
480 arprelease(er->f->arp, a);
482 n = sizeof(Etherarp);
483 if(n < a->type->mintu)
486 memset(bp->rp, 0, n);
487 e = (Etherarp*)bp->rp;
488 memmove(e->tpa, a->ip+IPv4off, sizeof(e->tpa));
489 ipv4local(ifc, e->spa);
490 memmove(e->sha, ifc->mac, sizeof(e->sha));
491 memset(e->d, 0xff, sizeof(e->d)); /* ethernet broadcast */
492 memmove(e->s, ifc->mac, sizeof(e->s));
494 hnputs(e->type, ETARP);
496 hnputs(e->pro, ETIP4);
497 e->hln = sizeof(e->sha);
498 e->pln = sizeof(e->spa);
499 hnputs(e->op, ARPREQUEST);
502 devtab[er->achan->type]->bwrite(er->achan, bp, 0);
506 resolveaddr6(Ipifc *ifc, Arpent *a)
510 Etherrock *er = ifc->arg;
511 uchar ipsrc[IPaddrlen];
513 /* don't do anything if it's been less than a second since the last */
514 if(NOW - a->ctime < ReTransTimer){
515 arprelease(er->f->arp, a);
519 /* remove all but the last message */
520 while((bp = a->hold) != nil){
527 /* try to keep it around for a second more */
529 a->rtime = NOW + ReTransTimer;
530 if(a->rxtsrem <= 0) {
531 arprelease(er->f->arp, a);
536 arprelease(er->f->arp, a);
538 if(sflag = ipv6anylocal(ifc, ipsrc))
539 icmpns(er->f, ipsrc, sflag, a->ip, TARG_MULTI, ifc->mac);
543 * send a gratuitous arp to refresh arp caches
546 sendgarp(Ipifc *ifc, uchar *ip)
551 Etherrock *er = ifc->arg;
553 /* don't arp for our initial non address */
554 if(ipcmp(ip, IPnoaddr) == 0)
557 n = sizeof(Etherarp);
558 if(n < ifc->m->mintu)
561 memset(bp->rp, 0, n);
562 e = (Etherarp*)bp->rp;
563 memmove(e->tpa, ip+IPv4off, sizeof(e->tpa));
564 memmove(e->spa, ip+IPv4off, sizeof(e->spa));
565 memmove(e->sha, ifc->mac, sizeof(e->sha));
566 memset(e->d, 0xff, sizeof(e->d)); /* ethernet broadcast */
567 memmove(e->s, ifc->mac, sizeof(e->s));
569 hnputs(e->type, ETARP);
571 hnputs(e->pro, ETIP4);
572 e->hln = sizeof(e->sha);
573 e->pln = sizeof(e->spa);
574 hnputs(e->op, ARPREQUEST);
577 devtab[er->achan->type]->bwrite(er->achan, bp, 0);
587 static uchar eprinted[4];
588 Etherrock *er = ifc->arg;
590 ebp = devtab[er->achan->type]->bread(er->achan, ifc->maxtu, 0);
594 e = (Etherarp*)ebp->rp;
595 switch(nhgets(e->op)) {
600 /* check for machine using my ip address */
602 if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
603 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
604 print("arprep: 0x%E/0x%E also has ip addr %V\n",
605 e->s, e->sha, e->spa);
610 /* make sure we're not entering broadcast addresses */
611 if(ipcmp(ip, ipbroadcast) == 0 ||
612 !memcmp(e->sha, etherbroadcast, sizeof(e->sha))){
613 print("arprep: 0x%E/0x%E cannot register broadcast address %I\n",
614 e->s, e->sha, e->spa);
618 arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 0);
622 /* don't answer arps till we know who we are */
626 /* check for machine using my ip or ether address */
628 if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
629 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
630 if (memcmp(eprinted, e->spa, sizeof(e->spa))){
631 /* print only once */
632 print("arpreq: 0x%E also has ip addr %V\n", e->sha, e->spa);
633 memmove(eprinted, e->spa, sizeof(e->spa));
637 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) == 0){
638 print("arpreq: %V also has ether addr %E\n", e->spa, e->sha);
643 /* refresh what we know about sender */
644 arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 1);
646 /* answer only requests for our address or systems we're proxying for */
648 if(!iplocalonifc(ifc, ip))
649 if(!ipproxyifc(er->f, ifc, ip))
652 n = sizeof(Etherarp);
656 r = (Etherarp*)rbp->rp;
657 memset(r, 0, sizeof(Etherarp));
658 hnputs(r->type, ETARP);
660 hnputs(r->pro, ETIP4);
661 r->hln = sizeof(r->sha);
662 r->pln = sizeof(r->spa);
663 hnputs(r->op, ARPREPLY);
664 memmove(r->tha, e->sha, sizeof(r->tha));
665 memmove(r->tpa, e->spa, sizeof(r->tpa));
666 memmove(r->sha, ifc->mac, sizeof(r->sha));
667 memmove(r->spa, e->tpa, sizeof(r->spa));
668 memmove(r->d, e->sha, sizeof(r->d));
669 memmove(r->s, ifc->mac, sizeof(r->s));
672 devtab[er->achan->type]->bwrite(er->achan, rbp, 0);
681 Etherrock *er = ifc->arg;
693 multicastea(uchar *ea, uchar *ip)
697 switch(x = ipismulticast(ip)){
702 ea[3] = ip[13] & 0x7f;
719 * fill in an arp entry for broadcast or multicast
720 * addresses. Return the first queued packet for the
724 multicastarp(Fs *f, Arpent *a, Medium *medium, uchar *mac)
726 /* is it broadcast? */
727 switch(ipforme(f, a->ip)){
731 memset(mac, 0xff, 6);
732 return arpresolve(f->arp, a, medium, mac);
737 /* if multicast, fill in mac */
738 switch(multicastea(mac, a->ip)){
741 return arpresolve(f->arp, a, medium, mac);
744 /* let arp take care of it */
749 ethermediumlink(void)
751 addipmedium(ðermedium);
752 addipmedium(&gbemedium);
757 etherpref2addr(uchar *pref, uchar *ea)
759 pref[8] = ea[0] | 0x2;