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);
192 * open arp conversation
194 snprint(addr, sizeof(addr), "%s!0x806", argv[2]); /* ETARP */
195 achan = chandial(addr, nil, nil, nil);
198 * open ipv6 conversation
200 * the dial will fail if the type is already open on
203 snprint(addr, sizeof(addr), "%s!0x86DD", argv[2]); /* ETIP6 */
204 mchan6 = chandial(addr, nil, dir, &cchan6);
207 * make it non-blocking
209 devtab[cchan6->type]->write(cchan6, nbmsg, strlen(nbmsg), 0);
211 er = smalloc(sizeof(*er));
217 er->f = ifc->conv->p->f;
223 kproc("etherread4", etherread4, ifc);
224 kproc("recvarpproc", recvarpproc, ifc);
225 kproc("etherread6", etherread6, ifc);
229 * called with ifc wlock'd
232 etherunbind(Ipifc *ifc)
234 Etherrock *er = ifc->arg;
237 postnote(er->read4p, 1, "unbind", 0);
239 postnote(er->read6p, 1, "unbind", 0);
241 postnote(er->arpp, 1, "unbind", 0);
243 /* wait for readers to die */
244 while(er->arpp != 0 || er->read4p != 0 || er->read6p != 0)
245 tsleep(&up->sleep, return0, 0, 300);
247 if(er->mchan4 != nil)
251 if(er->cchan4 != nil)
253 if(er->mchan6 != nil)
255 if(er->cchan6 != nil)
262 * called by ipoput with a single block to write with ifc rlock'd
265 etherbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip)
270 Etherrock *er = ifc->arg;
272 /* get mac address of destination */
273 a = arpget(er->f->arp, bp, version, ifc, ip, mac);
275 /* check for broadcast or multicast */
276 bp = multicastarp(er->f, a, ifc->m, mac);
283 resolveaddr6(ifc, a);
286 panic("etherbwrite: version %d", version);
292 /* make it a single block with space for the ether header */
293 bp = padblock(bp, ifc->m->hsize);
294 if(BLEN(bp) < ifc->mintu)
295 bp = adjustblock(bp, ifc->mintu);
296 eh = (Etherhdr*)bp->rp;
298 /* copy in mac addresses and ether type */
299 memmove(eh->s, ifc->mac, sizeof(eh->s));
300 memmove(eh->d, mac, sizeof(eh->d));
306 devtab[er->mchan4->type]->bwrite(er->mchan4, bp, 0);
311 devtab[er->mchan6->type]->bwrite(er->mchan6, bp, 0);
314 panic("etherbwrite2: version %d", version);
321 * process to read from the ethernet
332 er->read4p = up; /* hide identity under a rock for unbind */
338 bp = devtab[er->mchan4->type]->bread(er->mchan4, ifc->maxtu, 0);
348 if(ifc->lifc == nil || BLEN(bp) <= ifc->m->hsize)
351 bp->rp += ifc->m->hsize;
352 ipiput4(er->f, ifc, bp);
361 * process to read from the ethernet, IPv6
372 er->read6p = up; /* hide identity under a rock for unbind */
378 bp = devtab[er->mchan6->type]->bread(er->mchan6, ifc->maxtu, 0);
388 if(ifc->lifc == nil || BLEN(bp) <= ifc->m->hsize)
391 bp->rp += ifc->m->hsize;
392 ipiput6(er->f, ifc, bp);
400 etheraddmulti(Ipifc *ifc, uchar *a, uchar *)
404 Etherrock *er = ifc->arg;
407 version = multicastea(mac, a);
408 sprint(buf, "addmulti %E", mac);
411 devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
414 devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
417 panic("etheraddmulti: version %d", version);
422 etherremmulti(Ipifc *ifc, uchar *a, uchar *)
426 Etherrock *er = ifc->arg;
429 version = multicastea(mac, a);
430 sprint(buf, "remmulti %E", mac);
433 devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
436 devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
439 panic("etherremmulti: version %d", version);
444 * send an ethernet arp
445 * (only v4, v6 uses the neighbor discovery, rfc1970)
448 sendarp(Ipifc *ifc, Arpent *a)
453 Etherrock *er = ifc->arg;
455 /* don't do anything if it's been less than a second since the last */
456 if(NOW - a->ctime < 1000){
457 arprelease(er->f->arp, a);
461 /* remove all but the last message */
462 while((bp = a->hold) != nil){
469 /* try to keep it around for a second more */
471 arprelease(er->f->arp, a);
473 n = sizeof(Etherarp);
474 if(n < a->type->mintu)
477 memset(bp->rp, 0, n);
478 e = (Etherarp*)bp->rp;
479 memmove(e->tpa, a->ip+IPv4off, sizeof(e->tpa));
480 ipv4local(ifc, e->spa);
481 memmove(e->sha, ifc->mac, sizeof(e->sha));
482 memset(e->d, 0xff, sizeof(e->d)); /* ethernet broadcast */
483 memmove(e->s, ifc->mac, sizeof(e->s));
485 hnputs(e->type, ETARP);
487 hnputs(e->pro, ETIP4);
488 e->hln = sizeof(e->sha);
489 e->pln = sizeof(e->spa);
490 hnputs(e->op, ARPREQUEST);
493 devtab[er->achan->type]->bwrite(er->achan, bp, 0);
497 resolveaddr6(Ipifc *ifc, Arpent *a)
501 Etherrock *er = ifc->arg;
502 uchar ipsrc[IPaddrlen];
504 /* don't do anything if it's been less than a second since the last */
505 if(NOW - a->ctime < ReTransTimer){
506 arprelease(er->f->arp, a);
510 /* remove all but the last message */
511 while((bp = a->hold) != nil){
518 /* try to keep it around for a second more */
520 a->rtime = NOW + ReTransTimer;
521 if(a->rxtsrem <= 0) {
522 arprelease(er->f->arp, a);
527 arprelease(er->f->arp, a);
529 if(sflag = ipv6anylocal(ifc, ipsrc))
530 icmpns(er->f, ipsrc, sflag, a->ip, TARG_MULTI, ifc->mac);
534 * send a gratuitous arp to refresh arp caches
537 sendgarp(Ipifc *ifc, uchar *ip)
542 Etherrock *er = ifc->arg;
544 /* don't arp for our initial non address */
545 if(ipcmp(ip, IPnoaddr) == 0)
548 n = sizeof(Etherarp);
549 if(n < ifc->m->mintu)
552 memset(bp->rp, 0, n);
553 e = (Etherarp*)bp->rp;
554 memmove(e->tpa, ip+IPv4off, sizeof(e->tpa));
555 memmove(e->spa, ip+IPv4off, sizeof(e->spa));
556 memmove(e->sha, ifc->mac, sizeof(e->sha));
557 memset(e->d, 0xff, sizeof(e->d)); /* ethernet broadcast */
558 memmove(e->s, ifc->mac, sizeof(e->s));
560 hnputs(e->type, ETARP);
562 hnputs(e->pro, ETIP4);
563 e->hln = sizeof(e->sha);
564 e->pln = sizeof(e->spa);
565 hnputs(e->op, ARPREQUEST);
568 devtab[er->achan->type]->bwrite(er->achan, bp, 0);
578 static uchar eprinted[4];
579 Etherrock *er = ifc->arg;
581 ebp = devtab[er->achan->type]->bread(er->achan, ifc->maxtu, 0);
585 e = (Etherarp*)ebp->rp;
586 switch(nhgets(e->op)) {
591 /* check for machine using my ip address */
593 if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
594 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
595 print("arprep: 0x%E/0x%E also has ip addr %V\n",
596 e->s, e->sha, e->spa);
601 /* make sure we're not entering broadcast addresses */
602 if(ipcmp(ip, ipbroadcast) == 0 ||
603 !memcmp(e->sha, etherbroadcast, sizeof(e->sha))){
604 print("arprep: 0x%E/0x%E cannot register broadcast address %I\n",
605 e->s, e->sha, e->spa);
609 arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 0);
613 /* don't answer arps till we know who we are */
617 /* check for machine using my ip or ether address */
619 if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
620 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
621 if (memcmp(eprinted, e->spa, sizeof(e->spa))){
622 /* print only once */
623 print("arpreq: 0x%E also has ip addr %V\n", e->sha, e->spa);
624 memmove(eprinted, e->spa, sizeof(e->spa));
628 if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) == 0){
629 print("arpreq: %V also has ether addr %E\n", e->spa, e->sha);
634 /* refresh what we know about sender */
635 arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 1);
637 /* answer only requests for our address or systems we're proxying for */
639 if(!iplocalonifc(ifc, ip))
640 if(!ipproxyifc(er->f, ifc, ip))
643 n = sizeof(Etherarp);
647 r = (Etherarp*)rbp->rp;
648 memset(r, 0, sizeof(Etherarp));
649 hnputs(r->type, ETARP);
651 hnputs(r->pro, ETIP4);
652 r->hln = sizeof(r->sha);
653 r->pln = sizeof(r->spa);
654 hnputs(r->op, ARPREPLY);
655 memmove(r->tha, e->sha, sizeof(r->tha));
656 memmove(r->tpa, e->spa, sizeof(r->tpa));
657 memmove(r->sha, ifc->mac, sizeof(r->sha));
658 memmove(r->spa, e->tpa, sizeof(r->spa));
659 memmove(r->d, e->sha, sizeof(r->d));
660 memmove(r->s, ifc->mac, sizeof(r->s));
663 devtab[er->achan->type]->bwrite(er->achan, rbp, 0);
672 Etherrock *er = ifc->arg;
684 multicastea(uchar *ea, uchar *ip)
688 switch(x = ipismulticast(ip)){
693 ea[3] = ip[13] & 0x7f;
710 * fill in an arp entry for broadcast or multicast
711 * addresses. Return the first queued packet for the
715 multicastarp(Fs *f, Arpent *a, Medium *medium, uchar *mac)
717 /* is it broadcast? */
718 switch(ipforme(f, a->ip)){
722 memset(mac, 0xff, 6);
723 return arpresolve(f->arp, a, medium, mac);
728 /* if multicast, fill in mac */
729 switch(multicastea(mac, a->ip)){
732 return arpresolve(f->arp, a, medium, mac);
735 /* let arp take care of it */
740 ethermediumlink(void)
742 addipmedium(ðermedium);
743 addipmedium(&gbemedium);
748 etherpref2addr(uchar *pref, uchar *ea)
750 pref[8] = ea[0] | 0x2;