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