15ae69b2b58215cf79d53bfa763f9eee8a57aaea
[akaros.git] / kern / src / net / ethermedium.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 typedef struct Etherhdr Etherhdr;
17 struct Etherhdr {
18         uint8_t d[6];
19         uint8_t s[6];
20         uint8_t t[2];
21 };
22
23 static uint8_t ipbroadcast[IPaddrlen] = {
24         0xff, 0xff, 0xff, 0xff,
25         0xff, 0xff, 0xff, 0xff,
26         0xff, 0xff, 0xff, 0xff,
27         0xff, 0xff, 0xff, 0xff,
28 };
29
30 static uint8_t etherbroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
31
32 static void etherread4(void *a);
33 static void etherread6(void *a);
34 static void etherbind(struct Ipifc *ifc, int argc, char **argv);
35 static void etherunbind(struct Ipifc *ifc);
36 static void etherbwrite(struct Ipifc *ifc, struct block *bp, int version,
37                                                 uint8_t * ip);
38 static void etheraddmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * ia);
39 static void etherremmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * ia);
40 static struct block *multicastarp(struct Fs *f, struct arpent *a,
41                                                                   struct medium *, uint8_t * mac);
42 static void sendarp(struct Ipifc *ifc, struct arpent *a);
43 static void sendgarp(struct Ipifc *ifc, uint8_t * unused_uint8_p_t);
44 static int multicastea(uint8_t * ea, uint8_t * ip);
45 static void recvarpproc(void *);
46 static void resolveaddr6(struct Ipifc *ifc, struct arpent *a);
47 static void etherpref2addr(uint8_t * pref, uint8_t * ea);
48
49 struct medium ethermedium = {
50         .name = "ether",
51         .hsize = 14,
52         .mintu = 60,
53         .maxtu = 1514,
54         .maclen = 6,
55         .bind = etherbind,
56         .unbind = etherunbind,
57         .bwrite = etherbwrite,
58         .addmulti = etheraddmulti,
59         .remmulti = etherremmulti,
60         .ares = arpenter,
61         .areg = sendgarp,
62         .pref2addr = etherpref2addr,
63 };
64
65 struct medium gbemedium = {
66         .name = "gbe",
67         .hsize = 14,
68         .mintu = 60,
69         .maxtu = 9014,
70         .maclen = 6,
71         .bind = etherbind,
72         .unbind = etherunbind,
73         .bwrite = etherbwrite,
74         .addmulti = etheraddmulti,
75         .remmulti = etherremmulti,
76         .ares = arpenter,
77         .areg = sendgarp,
78         .pref2addr = etherpref2addr,
79 };
80
81 typedef struct Etherrock Etherrock;
82 struct Etherrock {
83         struct Fs *f;                           /* file system we belong to */
84         struct proc *arpp;                      /* arp process */
85         struct proc *read4p;            /* reading process (v4) */
86         struct proc *read6p;            /* reading process (v6) */
87         struct chan *mchan4;            /* Data channel for v4 */
88         struct chan *achan;                     /* Arp channel */
89         struct chan *cchan4;            /* Control channel for v4 */
90         struct chan *mchan6;            /* Data channel for v6 */
91         struct chan *cchan6;            /* Control channel for v6 */
92 };
93
94 /*
95  *  ethernet arp request
96  */
97 enum {
98         ETARP = 0x0806,
99         ETIP4 = 0x0800,
100         ETIP6 = 0x86DD,
101         ARPREQUEST = 1,
102         ARPREPLY = 2,
103 };
104
105 typedef struct Etherarp Etherarp;
106 struct Etherarp {
107         uint8_t d[6];
108         uint8_t s[6];
109         uint8_t type[2];
110         uint8_t hrd[2];
111         uint8_t pro[2];
112         uint8_t hln;
113         uint8_t pln;
114         uint8_t op[2];
115         uint8_t sha[6];
116         uint8_t spa[4];
117         uint8_t tha[6];
118         uint8_t tpa[4];
119 };
120
121 static char *nbmsg = "nonblocking";
122
123 /*
124  *  called to bind an IP ifc to an ethernet device
125  *  called with ifc wlock'd
126  */
127 static void etherbind(struct Ipifc *ifc, int argc, char **argv)
128 {
129         ERRSTACK(1);
130         struct chan *mchan4, *cchan4, *achan, *mchan6, *cchan6;
131         char addr[Maxpath];                     //char addr[2*KNAMELEN];
132         char dir[Maxpath];                      //char dir[2*KNAMELEN];
133         char *buf;
134         int fd, cfd, n;
135         char *ptr;
136         Etherrock *er;
137
138         if (argc < 2)
139                 error(Ebadarg);
140
141         mchan4 = cchan4 = achan = mchan6 = cchan6 = NULL;
142         buf = NULL;
143         if (waserror()) {
144                 if (mchan4 != NULL)
145                         cclose(mchan4);
146                 if (cchan4 != NULL)
147                         cclose(cchan4);
148                 if (achan != NULL)
149                         cclose(achan);
150                 if (mchan6 != NULL)
151                         cclose(mchan6);
152                 if (cchan6 != NULL)
153                         cclose(cchan6);
154                 if (buf != NULL)
155                         kfree(buf);
156                 nexterror();
157         }
158
159         /*
160          *  open ip converstation
161          *
162          *  the dial will fail if the type is already open on
163          *  this device.
164          */
165         snprintf(addr, sizeof(addr), "%s!0x800", argv[2]);
166         fd = kdial(addr, NULL, dir, &cfd);
167         if (fd < 0)
168                 error("dial 0x800 failed: %s", get_cur_errbuf());
169         mchan4 = commonfdtochan(fd, ORDWR, 0, 1);
170         cchan4 = commonfdtochan(cfd, ORDWR, 0, 1);
171         sysclose(fd);
172         sysclose(cfd);
173
174         /*
175          *  make it non-blocking
176          */
177         devtab[cchan4->type].write(cchan4, nbmsg, strlen(nbmsg), 0);
178
179         /*
180          *  get mac address and speed
181          */
182         snprintf(addr, sizeof(addr), "%s/stats", dir);
183         fd = sysopen(addr, OREAD);
184         if (fd < 0)
185                 error("can't open ether stats: %s", get_cur_errbuf());
186
187         buf = kzmalloc(512, 0);
188         n = sysread(fd, buf, 511);
189         sysclose(fd);
190         if (n <= 0)
191                 error(Eio);
192         buf[n] = 0;
193
194         ptr = strstr(buf, "addr: ");
195         if (!ptr)
196                 error(Eio);
197         ptr += 6;
198         parsemac(ifc->mac, ptr, 6);
199
200         ptr = strstr(buf, "mbps: ");
201         if (ptr) {
202                 ptr += 6;
203                 ifc->mbps = atoi(ptr);
204         } else
205                 ifc->mbps = 100;
206
207         /*
208          *  open arp conversation
209          */
210         snprintf(addr, sizeof(addr), "%s!0x806", argv[2]);
211         fd = kdial(addr, NULL, NULL, NULL);
212         if (fd < 0)
213                 error("dial 0x806 failed: %s", get_cur_errbuf());
214         achan = commonfdtochan(fd, ORDWR, 0, 1);
215         sysclose(fd);
216
217         /*
218          *  open ip conversation
219          *
220          *  the dial will fail if the type is already open on
221          *  this device.
222          */
223         snprintf(addr, sizeof(addr), "%s!0x86DD", argv[2]);
224         fd = kdial(addr, NULL, dir, &cfd);
225         if (fd < 0)
226                 error("dial 0x86DD failed: %s", get_cur_errbuf());
227         mchan6 = commonfdtochan(fd, ORDWR, 0, 1);
228         cchan6 = commonfdtochan(cfd, ORDWR, 0, 1);
229         sysclose(fd);
230         sysclose(cfd);
231
232         /*
233          *  make it non-blocking
234          */
235         devtab[cchan6->type].write(cchan6, nbmsg, strlen(nbmsg), 0);
236
237         er = kzmalloc(sizeof(*er), 0);
238         er->mchan4 = mchan4;
239         er->cchan4 = cchan4;
240         er->achan = achan;
241         er->mchan6 = mchan6;
242         er->cchan6 = cchan6;
243         er->f = ifc->conv->p->f;
244         ifc->arg = er;
245
246         kfree(buf);
247         poperror();
248
249         ktask("etherread4", etherread4, ifc);
250         ktask("recvarpproc", recvarpproc, ifc);
251         ktask("etherread6", etherread6, ifc);
252 }
253
254 /*
255  *  called with ifc wlock'd
256  */
257 static void etherunbind(struct Ipifc *ifc)
258 {
259         Etherrock *er = ifc->arg;
260         printk("[kernel] etherunbind not supported yet!\n");
261
262         // we'll need to tell the ktasks to exit, maybe via flags and a wakeup
263 #if 0
264         if (er->read4p)
265                 postnote(er->read4p, 1, "unbind", 0);
266         if (er->read6p)
267                 postnote(er->read6p, 1, "unbind", 0);
268         if (er->arpp)
269                 postnote(er->arpp, 1, "unbind", 0);
270 #endif
271
272         /* wait for readers to die */
273         while (er->arpp != 0 || er->read4p != 0 || er->read6p != 0) ;
274         udelay_sched(300 * 1000);
275
276         if (er->mchan4 != NULL)
277                 cclose(er->mchan4);
278         if (er->achan != NULL)
279                 cclose(er->achan);
280         if (er->cchan4 != NULL)
281                 cclose(er->cchan4);
282         if (er->mchan6 != NULL)
283                 cclose(er->mchan6);
284         if (er->cchan6 != NULL)
285                 cclose(er->cchan6);
286
287         kfree(er);
288 }
289
290 /*
291  * copy ethernet address
292  */
293 static inline void etherfilladdr(uint16_t *pkt, uint16_t *dst, uint16_t *src)
294 {
295         *pkt++ = *dst++;
296         *pkt++ = *dst++;
297         *pkt++ = *dst++;
298         *pkt++ = *src++;
299         *pkt++ = *src++;
300         *pkt = *src;
301 }
302
303 /*
304  *  called by ipoput with a single block to write with ifc rlock'd
305  */
306 static void
307 etherbwrite(struct Ipifc *ifc, struct block *bp, int version, uint8_t * ip)
308 {
309         Etherhdr *eh;
310         struct arpent *a;
311         uint8_t mac[6];
312         Etherrock *er = ifc->arg;
313
314         /* get mac address of destination */
315         a = arpget(er->f->arp, bp, version, ifc, ip, mac);
316         if (a) {
317                 /* check for broadcast or multicast */
318                 bp = multicastarp(er->f, a, ifc->m, mac);
319                 if (bp == NULL) {
320                         switch (version) {
321                                 case V4:
322                                         sendarp(ifc, a);
323                                         break;
324                                 case V6:
325                                         resolveaddr6(ifc, a);
326                                         break;
327                                 default:
328                                         panic("etherbwrite: version %d", version);
329                         }
330                         return;
331                 }
332         }
333
334         /* make it a single block with space for the ether header */
335         bp = padblock(bp, ifc->m->hsize);
336         if (bp->next)
337                 bp = concatblock(bp);
338         eh = (Etherhdr *) bp->rp;
339
340         /* copy in mac addresses and ether type */
341         etherfilladdr((uint16_t *)bp->rp, (uint16_t *)mac, (uint16_t *)ifc->mac);
342
343         switch (version) {
344                 case V4:
345                         eh->t[0] = 0x08;
346                         eh->t[1] = 0x00;
347                         devtab[er->mchan4->type].bwrite(er->mchan4, bp, 0);
348                         break;
349                 case V6:
350                         eh->t[0] = 0x86;
351                         eh->t[1] = 0xDD;
352                         devtab[er->mchan6->type].bwrite(er->mchan6, bp, 0);
353                         break;
354                 default:
355                         panic("etherbwrite2: version %d", version);
356         }
357         ifc->out++;
358 }
359
360 /*
361  *  process to read from the ethernet
362  */
363 static void etherread4(void *a)
364 {
365         ERRSTACK(2);
366         struct Ipifc *ifc;
367         struct block *bp;
368         Etherrock *er;
369
370         ifc = a;
371         er = ifc->arg;
372         er->read4p = current;   /* hide identity under a rock for unbind */
373         if (waserror()) {
374                 er->read4p = 0;
375                 poperror();
376                 warn("etherread4 returns, probably unexpectedly\n");
377                 return;
378         }
379         for (;;) {
380                 bp = devtab[er->mchan4->type].bread(er->mchan4, ifc->maxtu, 0);
381                 if (!canrlock(&ifc->rwlock)) {
382                         freeb(bp);
383                         continue;
384                 }
385                 if (waserror()) {
386                         runlock(&ifc->rwlock);
387                         nexterror();
388                 }
389                 ifc->in++;
390                 bp->rp += ifc->m->hsize;
391                 if (ifc->lifc == NULL)
392                         freeb(bp);
393                 else
394                         ipiput4(er->f, ifc, bp);
395                 runlock(&ifc->rwlock);
396                 poperror();
397         }
398         poperror();
399 }
400
401 /*
402  *  process to read from the ethernet, IPv6
403  */
404 static void etherread6(void *a)
405 {
406         ERRSTACK(2);
407         struct Ipifc *ifc;
408         struct block *bp;
409         Etherrock *er;
410
411         ifc = a;
412         er = ifc->arg;
413         er->read6p = current;   /* hide identity under a rock for unbind */
414         if (waserror()) {
415                 er->read6p = 0;
416                 warn("etherread6 returns, probably unexpectedly\n");
417                 poperror();
418                 return;
419         }
420         for (;;) {
421                 bp = devtab[er->mchan6->type].bread(er->mchan6, ifc->maxtu, 0);
422                 if (!canrlock(&ifc->rwlock)) {
423                         freeb(bp);
424                         continue;
425                 }
426                 if (waserror()) {
427                         runlock(&ifc->rwlock);
428                         nexterror();
429                 }
430                 ifc->in++;
431                 bp->rp += ifc->m->hsize;
432                 if (ifc->lifc == NULL)
433                         freeb(bp);
434                 else
435                         ipiput6(er->f, ifc, bp);
436                 runlock(&ifc->rwlock);
437                 poperror();
438         }
439         poperror();
440 }
441
442 static void etheraddmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * unused)
443 {
444         uint8_t mac[6];
445         char buf[64];
446         Etherrock *er = ifc->arg;
447         int version;
448
449         version = multicastea(mac, a);
450         snprintf(buf, sizeof(buf), "addmulti %E", mac);
451         switch (version) {
452                 case V4:
453                         devtab[er->cchan4->type].write(er->cchan4, buf, strlen(buf), 0);
454                         break;
455                 case V6:
456                         devtab[er->cchan6->type].write(er->cchan6, buf, strlen(buf), 0);
457                         break;
458                 default:
459                         panic("etheraddmulti: version %d", version);
460         }
461 }
462
463 static void etherremmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * unused)
464 {
465         uint8_t mac[6];
466         char buf[64];
467         Etherrock *er = ifc->arg;
468         int version;
469
470         version = multicastea(mac, a);
471         snprintf(buf, sizeof(buf), "remmulti %E", mac);
472         switch (version) {
473                 case V4:
474                         devtab[er->cchan4->type].write(er->cchan4, buf, strlen(buf), 0);
475                         break;
476                 case V6:
477                         devtab[er->cchan6->type].write(er->cchan6, buf, strlen(buf), 0);
478                         break;
479                 default:
480                         panic("etherremmulti: version %d", version);
481         }
482 }
483
484 /*
485  *  send an ethernet arp
486  *  (only v4, v6 uses the neighbor discovery, rfc1970)
487  */
488 static void sendarp(struct Ipifc *ifc, struct arpent *a)
489 {
490         int n;
491         struct block *bp;
492         Etherarp *e;
493         Etherrock *er = ifc->arg;
494
495         /* don't do anything if it's been less than a second since the last */
496         if (NOW - a->ctime < 1000) {
497                 arprelease(er->f->arp, a);
498                 return;
499         }
500
501         /* remove all but the last message */
502         while ((bp = a->hold) != NULL) {
503                 if (bp == a->last)
504                         break;
505                 a->hold = bp->list;
506                 freeblist(bp);
507         }
508
509         /* try to keep it around for a second more */
510         a->ctime = NOW;
511         arprelease(er->f->arp, a);
512
513         n = sizeof(Etherarp);
514         if (n < a->type->mintu)
515                 n = a->type->mintu;
516         bp = allocb(n);
517         memset(bp->rp, 0, n);
518         e = (Etherarp *) bp->rp;
519         memmove(e->tpa, a->ip + IPv4off, sizeof(e->tpa));
520         ipv4local(ifc, e->spa);
521         memmove(e->sha, ifc->mac, sizeof(e->sha));
522         memset(e->d, 0xff, sizeof(e->d));       /* ethernet broadcast */
523         memmove(e->s, ifc->mac, sizeof(e->s));
524
525         hnputs(e->type, ETARP);
526         hnputs(e->hrd, 1);
527         hnputs(e->pro, ETIP4);
528         e->hln = sizeof(e->sha);
529         e->pln = sizeof(e->spa);
530         hnputs(e->op, ARPREQUEST);
531         bp->wp += n;
532
533         n = devtab[er->achan->type].bwrite(er->achan, bp, 0);
534         if (n < 0)
535                 printd("arp: send: %r\n");
536 }
537
538 static void resolveaddr6(struct Ipifc *ifc, struct arpent *a)
539 {
540         int sflag;
541         struct block *bp;
542         Etherrock *er = ifc->arg;
543         uint8_t ipsrc[IPaddrlen];
544
545         /* don't do anything if it's been less than a second since the last */
546         if (NOW - a->ctime < ReTransTimer) {
547                 arprelease(er->f->arp, a);
548                 return;
549         }
550
551         /* remove all but the last message */
552         while ((bp = a->hold) != NULL) {
553                 if (bp == a->last)
554                         break;
555                 a->hold = bp->list;
556                 freeblist(bp);
557         }
558
559         /* try to keep it around for a second more */
560         a->ctime = NOW;
561         a->rtime = NOW + ReTransTimer;
562         if (a->rxtsrem <= 0) {
563                 arprelease(er->f->arp, a);
564                 return;
565         }
566
567         a->rxtsrem--;
568         arprelease(er->f->arp, a);
569
570         if ((sflag = ipv6anylocal(ifc, ipsrc)))
571                 icmpns(er->f, ipsrc, sflag, a->ip, TARG_MULTI, ifc->mac);
572 }
573
574 /*
575  *  send a gratuitous arp to refresh arp caches
576  */
577 static void sendgarp(struct Ipifc *ifc, uint8_t * ip)
578 {
579         int n;
580         struct block *bp;
581         Etherarp *e;
582         Etherrock *er = ifc->arg;
583
584         /* don't arp for our initial non address */
585         if (ipcmp(ip, IPnoaddr) == 0)
586                 return;
587
588         n = sizeof(Etherarp);
589         if (n < ifc->m->mintu)
590                 n = ifc->m->mintu;
591         bp = allocb(n);
592         memset(bp->rp, 0, n);
593         e = (Etherarp *) bp->rp;
594         memmove(e->tpa, ip + IPv4off, sizeof(e->tpa));
595         memmove(e->spa, ip + IPv4off, sizeof(e->spa));
596         memmove(e->sha, ifc->mac, sizeof(e->sha));
597         memset(e->d, 0xff, sizeof(e->d));       /* ethernet broadcast */
598         memmove(e->s, ifc->mac, sizeof(e->s));
599
600         hnputs(e->type, ETARP);
601         hnputs(e->hrd, 1);
602         hnputs(e->pro, ETIP4);
603         e->hln = sizeof(e->sha);
604         e->pln = sizeof(e->spa);
605         hnputs(e->op, ARPREQUEST);
606         bp->wp += n;
607
608         n = devtab[er->achan->type].bwrite(er->achan, bp, 0);
609         if (n < 0)
610                 printd("garp: send: %r\n");
611 }
612
613 static void recvarp(struct Ipifc *ifc)
614 {
615         int n;
616         struct block *ebp, *rbp;
617         Etherarp *e, *r;
618         uint8_t ip[IPaddrlen];
619         static uint8_t eprinted[4];
620         Etherrock *er = ifc->arg;
621
622         ebp = devtab[er->achan->type].bread(er->achan, ifc->maxtu, 0);
623         if (ebp == NULL) {
624                 printd("arp: rcv: %r\n");
625                 return;
626         }
627
628         e = (Etherarp *) ebp->rp;
629         switch (nhgets(e->op)) {
630                 default:
631                         break;
632
633                 case ARPREPLY:
634                         /* check for machine using my ip address */
635                         v4tov6(ip, e->spa);
636                         if (iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)) {
637                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0) {
638                                         printd("arprep: 0x%E/0x%E also has ip addr %V\n",
639                                                    e->s, e->sha, e->spa);
640                                         break;
641                                 }
642                         }
643
644                         /* make sure we're not entering broadcast addresses */
645                         if (ipcmp(ip, ipbroadcast) == 0 ||
646                                 !memcmp(e->sha, etherbroadcast, sizeof(e->sha))) {
647                                 printd
648                                         ("arprep: 0x%E/0x%E cannot register broadcast address %I\n",
649                                          e->s, e->sha, e->spa);
650                                 break;
651                         }
652
653                         arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 0);
654                         break;
655
656                 case ARPREQUEST:
657                         /* don't answer arps till we know who we are */
658                         if (ifc->lifc == 0)
659                                 break;
660
661                         /* check for machine using my ip or ether address */
662                         v4tov6(ip, e->spa);
663                         if (iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)) {
664                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0) {
665                                         if (memcmp(eprinted, e->spa, sizeof(e->spa))) {
666                                                 /* print only once */
667                                                 printd("arpreq: 0x%E also has ip addr %V\n", e->sha,
668                                                            e->spa);
669                                                 memmove(eprinted, e->spa, sizeof(e->spa));
670                                         }
671                                 }
672                         } else {
673                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) == 0) {
674                                         printd("arpreq: %V also has ether addr %E\n", e->spa,
675                                                    e->sha);
676                                         break;
677                                 }
678                         }
679
680                         /* refresh what we know about sender */
681                         arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 1);
682
683                         /* answer only requests for our address or systems we're proxying for */
684                         v4tov6(ip, e->tpa);
685                         if (!iplocalonifc(ifc, ip))
686                                 if (!ipproxyifc(er->f, ifc, ip))
687                                         break;
688
689                         n = sizeof(Etherarp);
690                         if (n < ifc->mintu)
691                                 n = ifc->mintu;
692                         rbp = allocb(n);
693                         r = (Etherarp *) rbp->rp;
694                         memset(r, 0, sizeof(Etherarp));
695                         hnputs(r->type, ETARP);
696                         hnputs(r->hrd, 1);
697                         hnputs(r->pro, ETIP4);
698                         r->hln = sizeof(r->sha);
699                         r->pln = sizeof(r->spa);
700                         hnputs(r->op, ARPREPLY);
701                         memmove(r->tha, e->sha, sizeof(r->tha));
702                         memmove(r->tpa, e->spa, sizeof(r->tpa));
703                         memmove(r->sha, ifc->mac, sizeof(r->sha));
704                         memmove(r->spa, e->tpa, sizeof(r->spa));
705                         memmove(r->d, e->sha, sizeof(r->d));
706                         memmove(r->s, ifc->mac, sizeof(r->s));
707                         rbp->wp += n;
708
709                         n = devtab[er->achan->type].bwrite(er->achan, rbp, 0);
710                         if (n < 0)
711                                 printd("arp: write: %r\n");
712         }
713         freeb(ebp);
714 }
715
716 static void recvarpproc(void *v)
717 {
718         ERRSTACK(1);
719         struct Ipifc *ifc = v;
720         Etherrock *er = ifc->arg;
721
722         er->arpp = current;
723         if (waserror()) {
724                 er->arpp = 0;
725                 warn("recvarpproc returns, probably unexpectedly\n");
726                 poperror();
727                 return;
728         }
729         for (;;)
730                 recvarp(ifc);
731         poperror();
732 }
733
734 static int multicastea(uint8_t * ea, uint8_t * ip)
735 {
736         int x;
737
738         switch (x = ipismulticast(ip)) {
739                 case V4:
740                         ea[0] = 0x01;
741                         ea[1] = 0x00;
742                         ea[2] = 0x5e;
743                         ea[3] = ip[13] & 0x7f;
744                         ea[4] = ip[14];
745                         ea[5] = ip[15];
746                         break;
747                 case V6:
748                         ea[0] = 0x33;
749                         ea[1] = 0x33;
750                         ea[2] = ip[12];
751                         ea[3] = ip[13];
752                         ea[4] = ip[14];
753                         ea[5] = ip[15];
754                         break;
755         }
756         return x;
757 }
758
759 /*
760  *  fill in an arp entry for broadcast or multicast
761  *  addresses.  Return the first queued packet for the
762  *  IP address.
763  */
764 static struct block *multicastarp(struct Fs *f,
765                                                                   struct arpent *a, struct medium *medium,
766                                                                   uint8_t * mac)
767 {
768         /* is it broadcast? */
769         switch (ipforme(f, a->ip)) {
770                 case Runi:
771                         return NULL;
772                 case Rbcast:
773                         memset(mac, 0xff, 6);
774                         return arpresolve(f->arp, a, medium, mac);
775                 default:
776                         break;
777         }
778
779         /* if multicast, fill in mac */
780         switch (multicastea(mac, a->ip)) {
781                 case V4:
782                 case V6:
783                         return arpresolve(f->arp, a, medium, mac);
784         }
785
786         /* let arp take care of it */
787         return NULL;
788 }
789
790 linker_func_4(ethermediumlink)
791 {
792         addipmedium(&ethermedium);
793         addipmedium(&gbemedium);
794 }
795
796 static void etherpref2addr(uint8_t * pref, uint8_t * ea)
797 {
798         pref[8] = ea[0] | 0x2;
799         pref[9] = ea[1];
800         pref[10] = ea[2];
801         pref[11] = 0xFF;
802         pref[12] = 0xFE;
803         pref[13] = ea[3];
804         pref[14] = ea[4];
805         pref[15] = ea[5];
806 }