Lindent pass
[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  *  called by ipoput with a single block to write with ifc rlock'd
292  */
293 static void
294 etherbwrite(struct Ipifc *ifc, struct block *bp, int version, uint8_t * ip)
295 {
296         Etherhdr *eh;
297         struct arpent *a;
298         uint8_t mac[6];
299         Etherrock *er = ifc->arg;
300
301         /* get mac address of destination */
302         a = arpget(er->f->arp, bp, version, ifc, ip, mac);
303         if (a) {
304                 /* check for broadcast or multicast */
305                 bp = multicastarp(er->f, a, ifc->m, mac);
306                 if (bp == NULL) {
307                         switch (version) {
308                                 case V4:
309                                         sendarp(ifc, a);
310                                         break;
311                                 case V6:
312                                         resolveaddr6(ifc, a);
313                                         break;
314                                 default:
315                                         panic("etherbwrite: version %d", version);
316                         }
317                         return;
318                 }
319         }
320
321         /* make it a single block with space for the ether header */
322         bp = padblock(bp, ifc->m->hsize);
323         if (bp->next)
324                 bp = concatblock(bp);
325         if (BLEN(bp) < ifc->mintu)
326                 bp = adjustblock(bp, ifc->mintu);
327         eh = (Etherhdr *) bp->rp;
328
329         /* copy in mac addresses and ether type */
330         memmove(eh->s, ifc->mac, sizeof(eh->s));
331         memmove(eh->d, mac, sizeof(eh->d));
332
333         switch (version) {
334                 case V4:
335                         eh->t[0] = 0x08;
336                         eh->t[1] = 0x00;
337                         devtab[er->mchan4->type].bwrite(er->mchan4, bp, 0);
338                         break;
339                 case V6:
340                         eh->t[0] = 0x86;
341                         eh->t[1] = 0xDD;
342                         devtab[er->mchan6->type].bwrite(er->mchan6, bp, 0);
343                         break;
344                 default:
345                         panic("etherbwrite2: version %d", version);
346         }
347         ifc->out++;
348 }
349
350 /*
351  *  process to read from the ethernet
352  */
353 static void etherread4(void *a)
354 {
355         ERRSTACK(2);
356         struct Ipifc *ifc;
357         struct block *bp;
358         Etherrock *er;
359
360         ifc = a;
361         er = ifc->arg;
362         er->read4p = current;   /* hide identity under a rock for unbind */
363         if (waserror()) {
364                 er->read4p = 0;
365                 poperror();
366                 warn("etherread4 returns, probably unexpectedly\n");
367                 return;
368         }
369         for (;;) {
370                 bp = devtab[er->mchan4->type].bread(er->mchan4, ifc->maxtu, 0);
371                 if (!canrlock(&ifc->rwlock)) {
372                         freeb(bp);
373                         continue;
374                 }
375                 if (waserror()) {
376                         runlock(&ifc->rwlock);
377                         nexterror();
378                 }
379                 ifc->in++;
380                 bp->rp += ifc->m->hsize;
381                 if (ifc->lifc == NULL)
382                         freeb(bp);
383                 else
384                         ipiput4(er->f, ifc, bp);
385                 runlock(&ifc->rwlock);
386                 poperror();
387         }
388         poperror();
389 }
390
391 /*
392  *  process to read from the ethernet, IPv6
393  */
394 static void etherread6(void *a)
395 {
396         ERRSTACK(2);
397         struct Ipifc *ifc;
398         struct block *bp;
399         Etherrock *er;
400
401         ifc = a;
402         er = ifc->arg;
403         er->read6p = current;   /* hide identity under a rock for unbind */
404         if (waserror()) {
405                 er->read6p = 0;
406                 warn("etherread6 returns, probably unexpectedly\n");
407                 poperror();
408                 return;
409         }
410         for (;;) {
411                 bp = devtab[er->mchan6->type].bread(er->mchan6, ifc->maxtu, 0);
412                 if (!canrlock(&ifc->rwlock)) {
413                         freeb(bp);
414                         continue;
415                 }
416                 if (waserror()) {
417                         runlock(&ifc->rwlock);
418                         nexterror();
419                 }
420                 ifc->in++;
421                 bp->rp += ifc->m->hsize;
422                 if (ifc->lifc == NULL)
423                         freeb(bp);
424                 else
425                         ipiput6(er->f, ifc, bp);
426                 runlock(&ifc->rwlock);
427                 poperror();
428         }
429         poperror();
430 }
431
432 static void etheraddmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * unused)
433 {
434         uint8_t mac[6];
435         char buf[64];
436         Etherrock *er = ifc->arg;
437         int version;
438
439         version = multicastea(mac, a);
440         snprintf(buf, sizeof(buf), "addmulti %E", mac);
441         switch (version) {
442                 case V4:
443                         devtab[er->cchan4->type].write(er->cchan4, buf, strlen(buf), 0);
444                         break;
445                 case V6:
446                         devtab[er->cchan6->type].write(er->cchan6, buf, strlen(buf), 0);
447                         break;
448                 default:
449                         panic("etheraddmulti: version %d", version);
450         }
451 }
452
453 static void etherremmulti(struct Ipifc *ifc, uint8_t * a, uint8_t * unused)
454 {
455         uint8_t mac[6];
456         char buf[64];
457         Etherrock *er = ifc->arg;
458         int version;
459
460         version = multicastea(mac, a);
461         snprintf(buf, sizeof(buf), "remmulti %E", mac);
462         switch (version) {
463                 case V4:
464                         devtab[er->cchan4->type].write(er->cchan4, buf, strlen(buf), 0);
465                         break;
466                 case V6:
467                         devtab[er->cchan6->type].write(er->cchan6, buf, strlen(buf), 0);
468                         break;
469                 default:
470                         panic("etherremmulti: version %d", version);
471         }
472 }
473
474 /*
475  *  send an ethernet arp
476  *  (only v4, v6 uses the neighbor discovery, rfc1970)
477  */
478 static void sendarp(struct Ipifc *ifc, struct arpent *a)
479 {
480         int n;
481         struct block *bp;
482         Etherarp *e;
483         Etherrock *er = ifc->arg;
484
485         /* don't do anything if it's been less than a second since the last */
486         if (NOW - a->ctime < 1000) {
487                 arprelease(er->f->arp, a);
488                 return;
489         }
490
491         /* remove all but the last message */
492         while ((bp = a->hold) != NULL) {
493                 if (bp == a->last)
494                         break;
495                 a->hold = bp->list;
496                 freeblist(bp);
497         }
498
499         /* try to keep it around for a second more */
500         a->ctime = NOW;
501         arprelease(er->f->arp, a);
502
503         n = sizeof(Etherarp);
504         if (n < a->type->mintu)
505                 n = a->type->mintu;
506         bp = allocb(n);
507         memset(bp->rp, 0, n);
508         e = (Etherarp *) bp->rp;
509         memmove(e->tpa, a->ip + IPv4off, sizeof(e->tpa));
510         ipv4local(ifc, e->spa);
511         memmove(e->sha, ifc->mac, sizeof(e->sha));
512         memset(e->d, 0xff, sizeof(e->d));       /* ethernet broadcast */
513         memmove(e->s, ifc->mac, sizeof(e->s));
514
515         hnputs(e->type, ETARP);
516         hnputs(e->hrd, 1);
517         hnputs(e->pro, ETIP4);
518         e->hln = sizeof(e->sha);
519         e->pln = sizeof(e->spa);
520         hnputs(e->op, ARPREQUEST);
521         bp->wp += n;
522
523         n = devtab[er->achan->type].bwrite(er->achan, bp, 0);
524         if (n < 0)
525                 printd("arp: send: %r\n");
526 }
527
528 static void resolveaddr6(struct Ipifc *ifc, struct arpent *a)
529 {
530         int sflag;
531         struct block *bp;
532         Etherrock *er = ifc->arg;
533         uint8_t ipsrc[IPaddrlen];
534
535         /* don't do anything if it's been less than a second since the last */
536         if (NOW - a->ctime < ReTransTimer) {
537                 arprelease(er->f->arp, a);
538                 return;
539         }
540
541         /* remove all but the last message */
542         while ((bp = a->hold) != NULL) {
543                 if (bp == a->last)
544                         break;
545                 a->hold = bp->list;
546                 freeblist(bp);
547         }
548
549         /* try to keep it around for a second more */
550         a->ctime = NOW;
551         a->rtime = NOW + ReTransTimer;
552         if (a->rxtsrem <= 0) {
553                 arprelease(er->f->arp, a);
554                 return;
555         }
556
557         a->rxtsrem--;
558         arprelease(er->f->arp, a);
559
560         if ((sflag = ipv6anylocal(ifc, ipsrc)))
561                 icmpns(er->f, ipsrc, sflag, a->ip, TARG_MULTI, ifc->mac);
562 }
563
564 /*
565  *  send a gratuitous arp to refresh arp caches
566  */
567 static void sendgarp(struct Ipifc *ifc, uint8_t * ip)
568 {
569         int n;
570         struct block *bp;
571         Etherarp *e;
572         Etherrock *er = ifc->arg;
573
574         /* don't arp for our initial non address */
575         if (ipcmp(ip, IPnoaddr) == 0)
576                 return;
577
578         n = sizeof(Etherarp);
579         if (n < ifc->m->mintu)
580                 n = ifc->m->mintu;
581         bp = allocb(n);
582         memset(bp->rp, 0, n);
583         e = (Etherarp *) bp->rp;
584         memmove(e->tpa, ip + IPv4off, sizeof(e->tpa));
585         memmove(e->spa, ip + IPv4off, sizeof(e->spa));
586         memmove(e->sha, ifc->mac, sizeof(e->sha));
587         memset(e->d, 0xff, sizeof(e->d));       /* ethernet broadcast */
588         memmove(e->s, ifc->mac, sizeof(e->s));
589
590         hnputs(e->type, ETARP);
591         hnputs(e->hrd, 1);
592         hnputs(e->pro, ETIP4);
593         e->hln = sizeof(e->sha);
594         e->pln = sizeof(e->spa);
595         hnputs(e->op, ARPREQUEST);
596         bp->wp += n;
597
598         n = devtab[er->achan->type].bwrite(er->achan, bp, 0);
599         if (n < 0)
600                 printd("garp: send: %r\n");
601 }
602
603 static void recvarp(struct Ipifc *ifc)
604 {
605         int n;
606         struct block *ebp, *rbp;
607         Etherarp *e, *r;
608         uint8_t ip[IPaddrlen];
609         static uint8_t eprinted[4];
610         Etherrock *er = ifc->arg;
611
612         ebp = devtab[er->achan->type].bread(er->achan, ifc->maxtu, 0);
613         if (ebp == NULL) {
614                 printd("arp: rcv: %r\n");
615                 return;
616         }
617
618         e = (Etherarp *) ebp->rp;
619         switch (nhgets(e->op)) {
620                 default:
621                         break;
622
623                 case ARPREPLY:
624                         /* check for machine using my ip address */
625                         v4tov6(ip, e->spa);
626                         if (iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)) {
627                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0) {
628                                         printd("arprep: 0x%E/0x%E also has ip addr %V\n",
629                                                    e->s, e->sha, e->spa);
630                                         break;
631                                 }
632                         }
633
634                         /* make sure we're not entering broadcast addresses */
635                         if (ipcmp(ip, ipbroadcast) == 0 ||
636                                 !memcmp(e->sha, etherbroadcast, sizeof(e->sha))) {
637                                 printd
638                                         ("arprep: 0x%E/0x%E cannot register broadcast address %I\n",
639                                          e->s, e->sha, e->spa);
640                                 break;
641                         }
642
643                         arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 0);
644                         break;
645
646                 case ARPREQUEST:
647                         /* don't answer arps till we know who we are */
648                         if (ifc->lifc == 0)
649                                 break;
650
651                         /* check for machine using my ip or ether address */
652                         v4tov6(ip, e->spa);
653                         if (iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)) {
654                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0) {
655                                         if (memcmp(eprinted, e->spa, sizeof(e->spa))) {
656                                                 /* print only once */
657                                                 printd("arpreq: 0x%E also has ip addr %V\n", e->sha,
658                                                            e->spa);
659                                                 memmove(eprinted, e->spa, sizeof(e->spa));
660                                         }
661                                 }
662                         } else {
663                                 if (memcmp(e->sha, ifc->mac, sizeof(e->sha)) == 0) {
664                                         printd("arpreq: %V also has ether addr %E\n", e->spa,
665                                                    e->sha);
666                                         break;
667                                 }
668                         }
669
670                         /* refresh what we know about sender */
671                         arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 1);
672
673                         /* answer only requests for our address or systems we're proxying for */
674                         v4tov6(ip, e->tpa);
675                         if (!iplocalonifc(ifc, ip))
676                                 if (!ipproxyifc(er->f, ifc, ip))
677                                         break;
678
679                         n = sizeof(Etherarp);
680                         if (n < ifc->mintu)
681                                 n = ifc->mintu;
682                         rbp = allocb(n);
683                         r = (Etherarp *) rbp->rp;
684                         memset(r, 0, sizeof(Etherarp));
685                         hnputs(r->type, ETARP);
686                         hnputs(r->hrd, 1);
687                         hnputs(r->pro, ETIP4);
688                         r->hln = sizeof(r->sha);
689                         r->pln = sizeof(r->spa);
690                         hnputs(r->op, ARPREPLY);
691                         memmove(r->tha, e->sha, sizeof(r->tha));
692                         memmove(r->tpa, e->spa, sizeof(r->tpa));
693                         memmove(r->sha, ifc->mac, sizeof(r->sha));
694                         memmove(r->spa, e->tpa, sizeof(r->spa));
695                         memmove(r->d, e->sha, sizeof(r->d));
696                         memmove(r->s, ifc->mac, sizeof(r->s));
697                         rbp->wp += n;
698
699                         n = devtab[er->achan->type].bwrite(er->achan, rbp, 0);
700                         if (n < 0)
701                                 printd("arp: write: %r\n");
702         }
703         freeb(ebp);
704 }
705
706 static void recvarpproc(void *v)
707 {
708         ERRSTACK(1);
709         struct Ipifc *ifc = v;
710         Etherrock *er = ifc->arg;
711
712         er->arpp = current;
713         if (waserror()) {
714                 er->arpp = 0;
715                 warn("recvarpproc returns, probably unexpectedly\n");
716                 poperror();
717                 return;
718         }
719         for (;;)
720                 recvarp(ifc);
721         poperror();
722 }
723
724 static int multicastea(uint8_t * ea, uint8_t * ip)
725 {
726         int x;
727
728         switch (x = ipismulticast(ip)) {
729                 case V4:
730                         ea[0] = 0x01;
731                         ea[1] = 0x00;
732                         ea[2] = 0x5e;
733                         ea[3] = ip[13] & 0x7f;
734                         ea[4] = ip[14];
735                         ea[5] = ip[15];
736                         break;
737                 case V6:
738                         ea[0] = 0x33;
739                         ea[1] = 0x33;
740                         ea[2] = ip[12];
741                         ea[3] = ip[13];
742                         ea[4] = ip[14];
743                         ea[5] = ip[15];
744                         break;
745         }
746         return x;
747 }
748
749 /*
750  *  fill in an arp entry for broadcast or multicast
751  *  addresses.  Return the first queued packet for the
752  *  IP address.
753  */
754 static struct block *multicastarp(struct Fs *f,
755                                                                   struct arpent *a, struct medium *medium,
756                                                                   uint8_t * mac)
757 {
758         /* is it broadcast? */
759         switch (ipforme(f, a->ip)) {
760                 case Runi:
761                         return NULL;
762                 case Rbcast:
763                         memset(mac, 0xff, 6);
764                         return arpresolve(f->arp, a, medium, mac);
765                 default:
766                         break;
767         }
768
769         /* if multicast, fill in mac */
770         switch (multicastea(mac, a->ip)) {
771                 case V4:
772                 case V6:
773                         return arpresolve(f->arp, a, medium, mac);
774         }
775
776         /* let arp take care of it */
777         return NULL;
778 }
779
780 linker_func_4(ethermediumlink)
781 {
782         addipmedium(&ethermedium);
783         addipmedium(&gbemedium);
784 }
785
786 static void etherpref2addr(uint8_t * pref, uint8_t * ea)
787 {
788         pref[8] = ea[0] | 0x2;
789         pref[9] = ea[1];
790         pref[10] = ea[2];
791         pref[11] = 0xFF;
792         pref[12] = 0xFE;
793         pref[13] = ea[3];
794         pref[14] = ea[4];
795         pref[15] = ea[5];
796 }