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