ethermedium.c 16 KB

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