ipifc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "ip.h"
  8. #include "ipv6.h"
  9. #define DPRINT if(0)print
  10. enum {
  11. Maxmedia = 32,
  12. Nself = Maxmedia*5,
  13. NHASH = 1<<6,
  14. NCACHE = 256,
  15. QMAX = 64*1024-1,
  16. };
  17. Medium *media[Maxmedia] = { 0 };
  18. /*
  19. * cache of local addresses (addresses we answer to)
  20. */
  21. struct Ipself
  22. {
  23. uchar a[IPaddrlen];
  24. Ipself *hnext; /* next address in the hash table */
  25. Iplink *link; /* binding twixt Ipself and Ipifc */
  26. ulong expire;
  27. uchar type; /* type of address */
  28. int ref;
  29. Ipself *next; /* free list */
  30. };
  31. struct Ipselftab
  32. {
  33. QLock;
  34. int inited;
  35. int acceptall; /* true if an interface has the null address */
  36. Ipself *hash[NHASH]; /* hash chains */
  37. };
  38. /*
  39. * Multicast addresses are chained onto a Chan so that
  40. * we can remove them when the Chan is closed.
  41. */
  42. typedef struct Ipmcast Ipmcast;
  43. struct Ipmcast
  44. {
  45. Ipmcast *next;
  46. uchar ma[IPaddrlen]; /* multicast address */
  47. uchar ia[IPaddrlen]; /* interface address */
  48. };
  49. /* quick hash for ip addresses */
  50. #define hashipa(a) ( ( ((a)[IPaddrlen-2]<<8) | (a)[IPaddrlen-1] )%NHASH )
  51. static char tifc[] = "ifc ";
  52. static void addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type);
  53. static void remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a);
  54. static char* ipifcjoinmulti(Ipifc *ifc, char **argv, int argc);
  55. static char* ipifcleavemulti(Ipifc *ifc, char **argv, int argc);
  56. static void ipifcregisterproxy(Fs*, Ipifc*, uchar*);
  57. static char* ipifcremlifc(Ipifc*, Iplifc*);
  58. /*
  59. * link in a new medium
  60. */
  61. void
  62. addipmedium(Medium *med)
  63. {
  64. int i;
  65. for(i = 0; i < nelem(media)-1; i++)
  66. if(media[i] == nil){
  67. media[i] = med;
  68. break;
  69. }
  70. }
  71. /*
  72. * find the medium with this name
  73. */
  74. Medium*
  75. ipfindmedium(char *name)
  76. {
  77. Medium **mp;
  78. for(mp = media; *mp != nil; mp++)
  79. if(strcmp((*mp)->name, name) == 0)
  80. break;
  81. return *mp;
  82. }
  83. /*
  84. * attach a device (or pkt driver) to the interface.
  85. * called with c locked
  86. */
  87. static char*
  88. ipifcbind(Conv *c, char **argv, int argc)
  89. {
  90. Ipifc *ifc;
  91. Medium *m;
  92. if(argc < 2)
  93. return Ebadarg;
  94. ifc = (Ipifc*)c->ptcl;
  95. /* bind the device to the interface */
  96. m = ipfindmedium(argv[1]);
  97. if(m == nil)
  98. return "unknown interface type";
  99. wlock(ifc);
  100. if(ifc->m != nil){
  101. wunlock(ifc);
  102. return "interface already bound";
  103. }
  104. if(waserror()){
  105. wunlock(ifc);
  106. nexterror();
  107. }
  108. /* do medium specific binding */
  109. (*m->bind)(ifc, argc, argv);
  110. /* set the bound device name */
  111. if(argc > 2)
  112. strncpy(ifc->dev, argv[2], sizeof(ifc->dev));
  113. else
  114. snprint(ifc->dev, sizeof ifc->dev, "%s%d", m->name, c->x);
  115. ifc->dev[sizeof(ifc->dev)-1] = 0;
  116. /* set up parameters */
  117. ifc->m = m;
  118. ifc->mintu = ifc->m->mintu;
  119. ifc->maxtu = ifc->m->maxtu;
  120. if(ifc->m->unbindonclose == 0)
  121. ifc->conv->inuse++;
  122. ifc->rp.mflag = 0; /* default not managed */
  123. ifc->rp.oflag = 0;
  124. ifc->rp.maxraint = 600000; /* millisecs */
  125. ifc->rp.minraint = 200000;
  126. ifc->rp.linkmtu = 0; /* no mtu sent */
  127. ifc->rp.reachtime = 0;
  128. ifc->rp.rxmitra = 0;
  129. ifc->rp.ttl = MAXTTL;
  130. ifc->rp.routerlt = 3 * ifc->rp.maxraint;
  131. /* any ancillary structures (like routes) no longer pertain */
  132. ifc->ifcid++;
  133. /* reopen all the queues closed by a previous unbind */
  134. qreopen(c->rq);
  135. qreopen(c->eq);
  136. qreopen(c->sq);
  137. wunlock(ifc);
  138. poperror();
  139. return nil;
  140. }
  141. /*
  142. * detach a device from an interface, close the interface
  143. * called with ifc->conv closed
  144. */
  145. static char*
  146. ipifcunbind(Ipifc *ifc)
  147. {
  148. char *err;
  149. if(waserror()){
  150. wunlock(ifc);
  151. nexterror();
  152. }
  153. wlock(ifc);
  154. /* dissociate routes */
  155. if(ifc->m != nil && ifc->m->unbindonclose == 0)
  156. ifc->conv->inuse--;
  157. ifc->ifcid++;
  158. /* disassociate logical interfaces (before zeroing ifc->arg) */
  159. while(ifc->lifc){
  160. err = ipifcremlifc(ifc, ifc->lifc);
  161. /*
  162. * note: err non-zero means lifc not found,
  163. * which can't happen in this case.
  164. */
  165. if(err)
  166. error(err);
  167. }
  168. /* disassociate device */
  169. if(ifc->m && ifc->m->unbind)
  170. (*ifc->m->unbind)(ifc);
  171. memset(ifc->dev, 0, sizeof(ifc->dev));
  172. ifc->arg = nil;
  173. ifc->reassemble = 0;
  174. /* close queues to stop queuing of packets */
  175. qclose(ifc->conv->rq);
  176. qclose(ifc->conv->wq);
  177. qclose(ifc->conv->sq);
  178. ifc->m = nil;
  179. wunlock(ifc);
  180. poperror();
  181. return nil;
  182. }
  183. char sfixedformat[] = "device %s maxtu %d sendra %d recvra %d mflag %d oflag"
  184. " %d maxraint %d minraint %d linkmtu %d reachtime %d rxmitra %d ttl %d routerlt"
  185. " %d pktin %lud pktout %lud errin %lud errout %lud\n";
  186. char slineformat[] = " %-40I %-10M %-40I %-12lud %-12lud\n";
  187. static int
  188. ipifcstate(Conv *c, char *state, int n)
  189. {
  190. Ipifc *ifc;
  191. Iplifc *lifc;
  192. int m;
  193. ifc = (Ipifc*)c->ptcl;
  194. m = snprint(state, n, sfixedformat,
  195. ifc->dev, ifc->maxtu, ifc->sendra6, ifc->recvra6,
  196. ifc->rp.mflag, ifc->rp.oflag, ifc->rp.maxraint,
  197. ifc->rp.minraint, ifc->rp.linkmtu, ifc->rp.reachtime,
  198. ifc->rp.rxmitra, ifc->rp.ttl, ifc->rp.routerlt,
  199. ifc->in, ifc->out, ifc->inerr, ifc->outerr);
  200. rlock(ifc);
  201. for(lifc = ifc->lifc; lifc && n > m; lifc = lifc->next)
  202. m += snprint(state+m, n - m, slineformat, lifc->local,
  203. lifc->mask, lifc->remote, lifc->validlt, lifc->preflt);
  204. if(ifc->lifc == nil)
  205. m += snprint(state+m, n - m, "\n");
  206. runlock(ifc);
  207. return m;
  208. }
  209. static int
  210. ipifclocal(Conv *c, char *state, int n)
  211. {
  212. Ipifc *ifc;
  213. Iplifc *lifc;
  214. Iplink *link;
  215. int m;
  216. ifc = (Ipifc*)c->ptcl;
  217. m = 0;
  218. rlock(ifc);
  219. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  220. m += snprint(state+m, n - m, "%-40.40I ->", lifc->local);
  221. for(link = lifc->link; link; link = link->lifclink)
  222. m += snprint(state+m, n - m, " %-40.40I", link->self->a);
  223. m += snprint(state+m, n - m, "\n");
  224. }
  225. runlock(ifc);
  226. return m;
  227. }
  228. static int
  229. ipifcinuse(Conv *c)
  230. {
  231. Ipifc *ifc;
  232. ifc = (Ipifc*)c->ptcl;
  233. return ifc->m != nil;
  234. }
  235. /*
  236. * called when a process writes to an interface's 'data'
  237. */
  238. static void
  239. ipifckick(void *x)
  240. {
  241. Conv *c = x;
  242. Block *bp;
  243. Ipifc *ifc;
  244. bp = qget(c->wq);
  245. if(bp == nil)
  246. return;
  247. ifc = (Ipifc*)c->ptcl;
  248. if(!canrlock(ifc)){
  249. freeb(bp);
  250. return;
  251. }
  252. if(waserror()){
  253. runlock(ifc);
  254. nexterror();
  255. }
  256. if(ifc->m == nil || ifc->m->pktin == nil)
  257. freeb(bp);
  258. else
  259. (*ifc->m->pktin)(c->p->f, ifc, bp);
  260. runlock(ifc);
  261. poperror();
  262. }
  263. /*
  264. * called when a new ipifc structure is created
  265. */
  266. static void
  267. ipifccreate(Conv *c)
  268. {
  269. Ipifc *ifc;
  270. c->rq = qopen(QMAX, 0, 0, 0);
  271. c->sq = qopen(2*QMAX, 0, 0, 0);
  272. c->wq = qopen(QMAX, Qkick, ipifckick, c);
  273. ifc = (Ipifc*)c->ptcl;
  274. ifc->conv = c;
  275. ifc->unbinding = 0;
  276. ifc->m = nil;
  277. ifc->reassemble = 0;
  278. }
  279. /*
  280. * called after last close of ipifc data or ctl
  281. * called with c locked, we must unlock
  282. */
  283. static void
  284. ipifcclose(Conv *c)
  285. {
  286. Ipifc *ifc;
  287. Medium *m;
  288. ifc = (Ipifc*)c->ptcl;
  289. m = ifc->m;
  290. if(m && m->unbindonclose)
  291. ipifcunbind(ifc);
  292. }
  293. /*
  294. * change an interface's mtu
  295. */
  296. char*
  297. ipifcsetmtu(Ipifc *ifc, char **argv, int argc)
  298. {
  299. int mtu;
  300. if(argc < 2 || ifc->m == nil)
  301. return Ebadarg;
  302. mtu = strtoul(argv[1], 0, 0);
  303. if(mtu < ifc->m->mintu || mtu > ifc->m->maxtu)
  304. return Ebadarg;
  305. ifc->maxtu = mtu;
  306. return nil;
  307. }
  308. /*
  309. * add an address to an interface.
  310. */
  311. char*
  312. ipifcadd(Ipifc *ifc, char **argv, int argc, int tentative, Iplifc *lifcp)
  313. {
  314. int i, type, mtu, sendnbrdisc = 0;
  315. uchar ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
  316. uchar bcast[IPaddrlen], net[IPaddrlen];
  317. Iplifc *lifc, **l;
  318. Fs *f;
  319. if(ifc->m == nil)
  320. return "ipifc not yet bound to device";
  321. f = ifc->conv->p->f;
  322. type = Rifc;
  323. memset(ip, 0, IPaddrlen);
  324. memset(mask, 0, IPaddrlen);
  325. memset(rem, 0, IPaddrlen);
  326. switch(argc){
  327. case 6:
  328. if(strcmp(argv[5], "proxy") == 0)
  329. type |= Rproxy;
  330. /* fall through */
  331. case 5:
  332. mtu = strtoul(argv[4], 0, 0);
  333. if(mtu >= ifc->m->mintu && mtu <= ifc->m->maxtu)
  334. ifc->maxtu = mtu;
  335. /* fall through */
  336. case 4:
  337. parseip(ip, argv[1]);
  338. parseipmask(mask, argv[2]);
  339. parseip(rem, argv[3]);
  340. maskip(rem, mask, net);
  341. break;
  342. case 3:
  343. parseip(ip, argv[1]);
  344. parseipmask(mask, argv[2]);
  345. maskip(ip, mask, rem);
  346. maskip(rem, mask, net);
  347. break;
  348. case 2:
  349. parseip(ip, argv[1]);
  350. memmove(mask, defmask(ip), IPaddrlen);
  351. maskip(ip, mask, rem);
  352. maskip(rem, mask, net);
  353. break;
  354. default:
  355. return Ebadarg;
  356. }
  357. if(isv4(ip))
  358. tentative = 0;
  359. wlock(ifc);
  360. /* ignore if this is already a local address for this ifc */
  361. for(lifc = ifc->lifc; lifc; lifc = lifc->next) {
  362. if(ipcmp(lifc->local, ip) == 0) {
  363. if(lifc->tentative != tentative)
  364. lifc->tentative = tentative;
  365. if(lifcp) {
  366. lifc->onlink = lifcp->onlink;
  367. lifc->autoflag = lifcp->autoflag;
  368. lifc->validlt = lifcp->validlt;
  369. lifc->preflt = lifcp->preflt;
  370. lifc->origint = lifcp->origint;
  371. }
  372. goto out;
  373. }
  374. }
  375. /* add the address to the list of logical ifc's for this ifc */
  376. lifc = smalloc(sizeof(Iplifc));
  377. ipmove(lifc->local, ip);
  378. ipmove(lifc->mask, mask);
  379. ipmove(lifc->remote, rem);
  380. ipmove(lifc->net, net);
  381. lifc->tentative = tentative;
  382. if(lifcp) {
  383. lifc->onlink = lifcp->onlink;
  384. lifc->autoflag = lifcp->autoflag;
  385. lifc->validlt = lifcp->validlt;
  386. lifc->preflt = lifcp->preflt;
  387. lifc->origint = lifcp->origint;
  388. } else { /* default values */
  389. lifc->onlink = lifc->autoflag = 1;
  390. lifc->validlt = lifc->preflt = ~0L;
  391. lifc->origint = NOW / 1000;
  392. }
  393. lifc->next = nil;
  394. for(l = &ifc->lifc; *l; l = &(*l)->next)
  395. ;
  396. *l = lifc;
  397. /* check for point-to-point interface */
  398. if(ipcmp(ip, v6loopback)) /* skip v6 loopback, it's a special address */
  399. if(ipcmp(mask, IPallbits) == 0)
  400. type |= Rptpt;
  401. /* add local routes */
  402. if(isv4(ip))
  403. v4addroute(f, tifc, rem+IPv4off, mask+IPv4off, rem+IPv4off, type);
  404. else
  405. v6addroute(f, tifc, rem, mask, rem, type);
  406. addselfcache(f, ifc, lifc, ip, Runi);
  407. if((type & (Rproxy|Rptpt)) == (Rproxy|Rptpt)){
  408. ipifcregisterproxy(f, ifc, rem);
  409. goto out;
  410. }
  411. if(isv4(ip) || ipcmp(ip, IPnoaddr) == 0) {
  412. /* add subnet directed broadcast address to the self cache */
  413. for(i = 0; i < IPaddrlen; i++)
  414. bcast[i] = (ip[i] & mask[i]) | ~mask[i];
  415. addselfcache(f, ifc, lifc, bcast, Rbcast);
  416. /* add subnet directed network address to the self cache */
  417. for(i = 0; i < IPaddrlen; i++)
  418. bcast[i] = (ip[i] & mask[i]) & mask[i];
  419. addselfcache(f, ifc, lifc, bcast, Rbcast);
  420. /* add network directed broadcast address to the self cache */
  421. memmove(mask, defmask(ip), IPaddrlen);
  422. for(i = 0; i < IPaddrlen; i++)
  423. bcast[i] = (ip[i] & mask[i]) | ~mask[i];
  424. addselfcache(f, ifc, lifc, bcast, Rbcast);
  425. /* add network directed network address to the self cache */
  426. memmove(mask, defmask(ip), IPaddrlen);
  427. for(i = 0; i < IPaddrlen; i++)
  428. bcast[i] = (ip[i] & mask[i]) & mask[i];
  429. addselfcache(f, ifc, lifc, bcast, Rbcast);
  430. addselfcache(f, ifc, lifc, IPv4bcast, Rbcast);
  431. }
  432. else {
  433. if(ipcmp(ip, v6loopback) == 0) {
  434. /* add node-local mcast address */
  435. addselfcache(f, ifc, lifc, v6allnodesN, Rmulti);
  436. /* add route for all node multicast */
  437. v6addroute(f, tifc, v6allnodesN, v6allnodesNmask,
  438. v6allnodesN, Rmulti);
  439. }
  440. /* add all nodes multicast address */
  441. addselfcache(f, ifc, lifc, v6allnodesL, Rmulti);
  442. /* add route for all nodes multicast */
  443. v6addroute(f, tifc, v6allnodesL, v6allnodesLmask, v6allnodesL,
  444. Rmulti);
  445. /* add solicited-node multicast address */
  446. ipv62smcast(bcast, ip);
  447. addselfcache(f, ifc, lifc, bcast, Rmulti);
  448. sendnbrdisc = 1;
  449. }
  450. /* register the address on this network for address resolution */
  451. if(isv4(ip) && ifc->m->areg != nil)
  452. (*ifc->m->areg)(ifc, ip);
  453. out:
  454. wunlock(ifc);
  455. if(tentative && sendnbrdisc)
  456. icmpns(f, 0, SRC_UNSPEC, ip, TARG_MULTI, ifc->mac);
  457. return nil;
  458. }
  459. /*
  460. * remove a logical interface from an ifc
  461. * always called with ifc wlock'd
  462. */
  463. static char*
  464. ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
  465. {
  466. Iplifc **l;
  467. Fs *f;
  468. f = ifc->conv->p->f;
  469. /*
  470. * find address on this interface and remove from chain.
  471. * for pt to pt we actually specify the remote address as the
  472. * addresss to remove.
  473. */
  474. for(l = &ifc->lifc; *l != nil && *l != lifc; l = &(*l)->next)
  475. ;
  476. if(*l == nil)
  477. return "address not on this interface";
  478. *l = lifc->next;
  479. /* disassociate any addresses */
  480. while(lifc->link)
  481. remselfcache(f, ifc, lifc, lifc->link->self->a);
  482. /* remove the route for this logical interface */
  483. if(isv4(lifc->local))
  484. v4delroute(f, lifc->remote+IPv4off, lifc->mask+IPv4off, 1);
  485. else {
  486. v6delroute(f, lifc->remote, lifc->mask, 1);
  487. if(ipcmp(lifc->local, v6loopback) == 0)
  488. /* remove route for all node multicast */
  489. v6delroute(f, v6allnodesN, v6allnodesNmask, 1);
  490. else if(memcmp(lifc->local, v6linklocal, v6llpreflen) == 0)
  491. /* remove route for all link multicast */
  492. v6delroute(f, v6allnodesL, v6allnodesLmask, 1);
  493. }
  494. free(lifc);
  495. return nil;
  496. }
  497. /*
  498. * remove an address from an interface.
  499. * called with c->car locked
  500. */
  501. char*
  502. ipifcrem(Ipifc *ifc, char **argv, int argc)
  503. {
  504. char *rv;
  505. uchar ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
  506. Iplifc *lifc;
  507. if(argc < 3)
  508. return Ebadarg;
  509. parseip(ip, argv[1]);
  510. parseipmask(mask, argv[2]);
  511. if(argc < 4)
  512. maskip(ip, mask, rem);
  513. else
  514. parseip(rem, argv[3]);
  515. wlock(ifc);
  516. /*
  517. * find address on this interface and remove from chain.
  518. * for pt to pt we actually specify the remote address as the
  519. * addresss to remove.
  520. */
  521. for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next) {
  522. if (memcmp(ip, lifc->local, IPaddrlen) == 0
  523. && memcmp(mask, lifc->mask, IPaddrlen) == 0
  524. && memcmp(rem, lifc->remote, IPaddrlen) == 0)
  525. break;
  526. }
  527. rv = ipifcremlifc(ifc, lifc);
  528. wunlock(ifc);
  529. return rv;
  530. }
  531. /*
  532. * distribute routes to active interfaces like the
  533. * TRIP linecards
  534. */
  535. void
  536. ipifcaddroute(Fs *f, int vers, uchar *addr, uchar *mask, uchar *gate, int type)
  537. {
  538. Medium *m;
  539. Conv **cp, **e;
  540. Ipifc *ifc;
  541. e = &f->ipifc->conv[f->ipifc->nc];
  542. for(cp = f->ipifc->conv; cp < e; cp++){
  543. if(*cp != nil) {
  544. ifc = (Ipifc*)(*cp)->ptcl;
  545. m = ifc->m;
  546. if(m && m->addroute)
  547. m->addroute(ifc, vers, addr, mask, gate, type);
  548. }
  549. }
  550. }
  551. void
  552. ipifcremroute(Fs *f, int vers, uchar *addr, uchar *mask)
  553. {
  554. Medium *m;
  555. Conv **cp, **e;
  556. Ipifc *ifc;
  557. e = &f->ipifc->conv[f->ipifc->nc];
  558. for(cp = f->ipifc->conv; cp < e; cp++){
  559. if(*cp != nil) {
  560. ifc = (Ipifc*)(*cp)->ptcl;
  561. m = ifc->m;
  562. if(m && m->remroute)
  563. m->remroute(ifc, vers, addr, mask);
  564. }
  565. }
  566. }
  567. /*
  568. * associate an address with the interface. This wipes out any previous
  569. * addresses. This is a macro that means, remove all the old interfaces
  570. * and add a new one.
  571. */
  572. static char*
  573. ipifcconnect(Conv* c, char **argv, int argc)
  574. {
  575. char *err;
  576. Ipifc *ifc;
  577. ifc = (Ipifc*)c->ptcl;
  578. if(ifc->m == nil)
  579. return "ipifc not yet bound to device";
  580. if(waserror()){
  581. wunlock(ifc);
  582. nexterror();
  583. }
  584. wlock(ifc);
  585. while(ifc->lifc){
  586. err = ipifcremlifc(ifc, ifc->lifc);
  587. if(err)
  588. error(err);
  589. }
  590. wunlock(ifc);
  591. poperror();
  592. err = ipifcadd(ifc, argv, argc, 0, nil);
  593. if(err)
  594. return err;
  595. Fsconnected(c, nil);
  596. return nil;
  597. }
  598. char*
  599. ipifcra6(Ipifc *ifc, char **argv, int argc)
  600. {
  601. int i, argsleft, vmax = ifc->rp.maxraint, vmin = ifc->rp.minraint;
  602. argsleft = argc - 1;
  603. i = 1;
  604. if(argsleft % 2 != 0)
  605. return Ebadarg;
  606. while (argsleft > 1) {
  607. if(strcmp(argv[i], "recvra") == 0)
  608. ifc->recvra6 = (atoi(argv[i+1]) != 0);
  609. else if(strcmp(argv[i], "sendra") == 0)
  610. ifc->sendra6 = (atoi(argv[i+1]) != 0);
  611. else if(strcmp(argv[i], "mflag") == 0)
  612. ifc->rp.mflag = (atoi(argv[i+1]) != 0);
  613. else if(strcmp(argv[i], "oflag") == 0)
  614. ifc->rp.oflag = (atoi(argv[i+1]) != 0);
  615. else if(strcmp(argv[i], "maxraint") == 0)
  616. ifc->rp.maxraint = atoi(argv[i+1]);
  617. else if(strcmp(argv[i], "minraint") == 0)
  618. ifc->rp.minraint = atoi(argv[i+1]);
  619. else if(strcmp(argv[i], "linkmtu") == 0)
  620. ifc->rp.linkmtu = atoi(argv[i+1]);
  621. else if(strcmp(argv[i], "reachtime") == 0)
  622. ifc->rp.reachtime = atoi(argv[i+1]);
  623. else if(strcmp(argv[i], "rxmitra") == 0)
  624. ifc->rp.rxmitra = atoi(argv[i+1]);
  625. else if(strcmp(argv[i], "ttl") == 0)
  626. ifc->rp.ttl = atoi(argv[i+1]);
  627. else if(strcmp(argv[i], "routerlt") == 0)
  628. ifc->rp.routerlt = atoi(argv[i+1]);
  629. else
  630. return Ebadarg;
  631. argsleft -= 2;
  632. i += 2;
  633. }
  634. /* consistency check */
  635. if(ifc->rp.maxraint < ifc->rp.minraint) {
  636. ifc->rp.maxraint = vmax;
  637. ifc->rp.minraint = vmin;
  638. return Ebadarg;
  639. }
  640. return nil;
  641. }
  642. /*
  643. * non-standard control messages.
  644. * called with c->car locked.
  645. */
  646. static char*
  647. ipifcctl(Conv* c, char**argv, int argc)
  648. {
  649. Ipifc *ifc;
  650. int i;
  651. ifc = (Ipifc*)c->ptcl;
  652. if(strcmp(argv[0], "add") == 0)
  653. return ipifcadd(ifc, argv, argc, 0, nil);
  654. else if(strcmp(argv[0], "try") == 0)
  655. return ipifcadd(ifc, argv, argc, 1, nil);
  656. else if(strcmp(argv[0], "remove") == 0)
  657. return ipifcrem(ifc, argv, argc);
  658. else if(strcmp(argv[0], "unbind") == 0)
  659. return ipifcunbind(ifc);
  660. else if(strcmp(argv[0], "joinmulti") == 0)
  661. return ipifcjoinmulti(ifc, argv, argc);
  662. else if(strcmp(argv[0], "leavemulti") == 0)
  663. return ipifcleavemulti(ifc, argv, argc);
  664. else if(strcmp(argv[0], "mtu") == 0)
  665. return ipifcsetmtu(ifc, argv, argc);
  666. else if(strcmp(argv[0], "reassemble") == 0){
  667. ifc->reassemble = 1;
  668. return nil;
  669. }
  670. else if(strcmp(argv[0], "iprouting") == 0){
  671. i = 1;
  672. if(argc > 1)
  673. i = atoi(argv[1]);
  674. iprouting(c->p->f, i);
  675. return nil;
  676. }
  677. else if(strcmp(argv[0], "add6") == 0)
  678. return ipifcadd6(ifc, argv, argc);
  679. else if(strcmp(argv[0], "ra6") == 0)
  680. return ipifcra6(ifc, argv, argc);
  681. return "unsupported ctl";
  682. }
  683. int
  684. ipifcstats(Proto *ipifc, char *buf, int len)
  685. {
  686. return ipstats(ipifc->f, buf, len);
  687. }
  688. void
  689. ipifcinit(Fs *f)
  690. {
  691. Proto *ipifc;
  692. ipifc = smalloc(sizeof(Proto));
  693. ipifc->name = "ipifc";
  694. ipifc->connect = ipifcconnect;
  695. ipifc->announce = nil;
  696. ipifc->bind = ipifcbind;
  697. ipifc->state = ipifcstate;
  698. ipifc->create = ipifccreate;
  699. ipifc->close = ipifcclose;
  700. ipifc->rcv = nil;
  701. ipifc->ctl = ipifcctl;
  702. ipifc->advise = nil;
  703. ipifc->stats = ipifcstats;
  704. ipifc->inuse = ipifcinuse;
  705. ipifc->local = ipifclocal;
  706. ipifc->ipproto = -1;
  707. ipifc->nc = Maxmedia;
  708. ipifc->ptclsize = sizeof(Ipifc);
  709. f->ipifc = ipifc; /* hack for ipifcremroute, findipifc, ... */
  710. f->self = smalloc(sizeof(Ipselftab)); /* hack for ipforme */
  711. Fsproto(f, ipifc);
  712. }
  713. /*
  714. * add to self routing cache
  715. * called with c->car locked
  716. */
  717. static void
  718. addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
  719. {
  720. Ipself *p;
  721. Iplink *lp;
  722. int h;
  723. qlock(f->self);
  724. /* see if the address already exists */
  725. h = hashipa(a);
  726. for(p = f->self->hash[h]; p; p = p->next)
  727. if(memcmp(a, p->a, IPaddrlen) == 0)
  728. break;
  729. /* allocate a local address and add to hash chain */
  730. if(p == nil){
  731. p = smalloc(sizeof(*p));
  732. ipmove(p->a, a);
  733. p->type = type;
  734. p->next = f->self->hash[h];
  735. f->self->hash[h] = p;
  736. /* if the null address, accept all packets */
  737. if(ipcmp(a, v4prefix) == 0 || ipcmp(a, IPnoaddr) == 0)
  738. f->self->acceptall = 1;
  739. }
  740. /* look for a link for this lifc */
  741. for(lp = p->link; lp; lp = lp->selflink)
  742. if(lp->lifc == lifc)
  743. break;
  744. /* allocate a lifc-to-local link and link to both */
  745. if(lp == nil){
  746. lp = smalloc(sizeof(*lp));
  747. lp->ref = 1;
  748. lp->lifc = lifc;
  749. lp->self = p;
  750. lp->selflink = p->link;
  751. p->link = lp;
  752. lp->lifclink = lifc->link;
  753. lifc->link = lp;
  754. /* add to routing table */
  755. if(isv4(a))
  756. v4addroute(f, tifc, a+IPv4off, IPallbits+IPv4off,
  757. a+IPv4off, type);
  758. else
  759. v6addroute(f, tifc, a, IPallbits, a, type);
  760. if((type & Rmulti) && ifc->m->addmulti != nil)
  761. (*ifc->m->addmulti)(ifc, a, lifc->local);
  762. } else
  763. lp->ref++;
  764. qunlock(f->self);
  765. }
  766. /*
  767. * These structures are unlinked from their chains while
  768. * other threads may be using them. To avoid excessive locking,
  769. * just put them aside for a while before freeing them.
  770. * called with f->self locked
  771. */
  772. static Iplink *freeiplink;
  773. static Ipself *freeipself;
  774. static void
  775. iplinkfree(Iplink *p)
  776. {
  777. Iplink **l, *np;
  778. ulong now = NOW;
  779. l = &freeiplink;
  780. for(np = *l; np; np = *l){
  781. if(np->expire > now){
  782. *l = np->next;
  783. free(np);
  784. continue;
  785. }
  786. l = &np->next;
  787. }
  788. p->expire = now + 5000; /* give other threads 5 secs to get out */
  789. p->next = nil;
  790. *l = p;
  791. }
  792. static void
  793. ipselffree(Ipself *p)
  794. {
  795. Ipself **l, *np;
  796. ulong now = NOW;
  797. l = &freeipself;
  798. for(np = *l; np; np = *l){
  799. if(np->expire > now){
  800. *l = np->next;
  801. free(np);
  802. continue;
  803. }
  804. l = &np->next;
  805. }
  806. p->expire = now + 5000; /* give other threads 5 secs to get out */
  807. p->next = nil;
  808. *l = p;
  809. }
  810. /*
  811. * Decrement reference for this address on this link.
  812. * Unlink from selftab if this is the last ref.
  813. * called with c->car locked
  814. */
  815. static void
  816. remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
  817. {
  818. Ipself *p, **l;
  819. Iplink *link, **l_self, **l_lifc;
  820. qlock(f->self);
  821. /* find the unique selftab entry */
  822. l = &f->self->hash[hashipa(a)];
  823. for(p = *l; p; p = *l){
  824. if(ipcmp(p->a, a) == 0)
  825. break;
  826. l = &p->next;
  827. }
  828. if(p == nil)
  829. goto out;
  830. /*
  831. * walk down links from an ifc looking for one
  832. * that matches the selftab entry
  833. */
  834. l_lifc = &lifc->link;
  835. for(link = *l_lifc; link; link = *l_lifc){
  836. if(link->self == p)
  837. break;
  838. l_lifc = &link->lifclink;
  839. }
  840. if(link == nil)
  841. goto out;
  842. /*
  843. * walk down the links from the selftab looking for
  844. * the one we just found
  845. */
  846. l_self = &p->link;
  847. for(link = *l_self; link; link = *l_self){
  848. if(link == *l_lifc)
  849. break;
  850. l_self = &link->selflink;
  851. }
  852. if(link == nil)
  853. panic("remselfcache");
  854. if(--(link->ref) != 0)
  855. goto out;
  856. if((p->type & Rmulti) && ifc->m->remmulti != nil)
  857. (*ifc->m->remmulti)(ifc, a, lifc->local);
  858. /* ref == 0, remove from both chains and free the link */
  859. *l_lifc = link->lifclink;
  860. *l_self = link->selflink;
  861. iplinkfree(link);
  862. if(p->link != nil)
  863. goto out;
  864. /* remove from routing table */
  865. if(isv4(a))
  866. v4delroute(f, a+IPv4off, IPallbits+IPv4off, 1);
  867. else
  868. v6delroute(f, a, IPallbits, 1);
  869. /* no more links, remove from hash and free */
  870. *l = p->next;
  871. ipselffree(p);
  872. /* if IPnoaddr, forget */
  873. if(ipcmp(a, v4prefix) == 0 || ipcmp(a, IPnoaddr) == 0)
  874. f->self->acceptall = 0;
  875. out:
  876. qunlock(f->self);
  877. }
  878. static char *stformat = "%-44.44I %2.2d %4.4s\n";
  879. enum
  880. {
  881. Nstformat= 41,
  882. };
  883. long
  884. ipselftabread(Fs *f, char *cp, ulong offset, int n)
  885. {
  886. int i, m, nifc, off;
  887. Ipself *p;
  888. Iplink *link;
  889. char state[8];
  890. m = 0;
  891. off = offset;
  892. qlock(f->self);
  893. for(i = 0; i < NHASH && m < n; i++){
  894. for(p = f->self->hash[i]; p != nil && m < n; p = p->next){
  895. nifc = 0;
  896. for(link = p->link; link; link = link->selflink)
  897. nifc++;
  898. routetype(p->type, state);
  899. m += snprint(cp + m, n - m, stformat, p->a, nifc, state);
  900. if(off > 0){
  901. off -= m;
  902. m = 0;
  903. }
  904. }
  905. }
  906. qunlock(f->self);
  907. return m;
  908. }
  909. int
  910. iptentative(Fs *f, uchar *addr)
  911. {
  912. Ipself *p;
  913. p = f->self->hash[hashipa(addr)];
  914. for(; p; p = p->next){
  915. if(ipcmp(addr, p->a) == 0)
  916. return p->link->lifc->tentative;
  917. }
  918. return 0;
  919. }
  920. /*
  921. * returns
  922. * 0 - no match
  923. * Runi
  924. * Rbcast
  925. * Rmcast
  926. */
  927. int
  928. ipforme(Fs *f, uchar *addr)
  929. {
  930. Ipself *p;
  931. p = f->self->hash[hashipa(addr)];
  932. for(; p; p = p->next){
  933. if(ipcmp(addr, p->a) == 0)
  934. return p->type;
  935. }
  936. /* hack to say accept anything */
  937. if(f->self->acceptall)
  938. return Runi;
  939. return 0;
  940. }
  941. /*
  942. * find the ifc on same net as the remote system. If none,
  943. * return nil.
  944. */
  945. Ipifc*
  946. findipifc(Fs *f, uchar *remote, int type)
  947. {
  948. Ipifc *ifc, *x;
  949. Iplifc *lifc;
  950. Conv **cp, **e;
  951. uchar gnet[IPaddrlen], xmask[IPaddrlen];
  952. x = nil;
  953. memset(xmask, 0, IPaddrlen);
  954. /* find most specific match */
  955. e = &f->ipifc->conv[f->ipifc->nc];
  956. for(cp = f->ipifc->conv; cp < e; cp++){
  957. if(*cp == 0)
  958. continue;
  959. ifc = (Ipifc*)(*cp)->ptcl;
  960. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  961. maskip(remote, lifc->mask, gnet);
  962. if(ipcmp(gnet, lifc->net) == 0){
  963. if(x == nil || ipcmp(lifc->mask, xmask) > 0){
  964. x = ifc;
  965. ipmove(xmask, lifc->mask);
  966. }
  967. }
  968. }
  969. }
  970. if(x != nil)
  971. return x;
  972. /* for now for broadcast and multicast, just use first interface */
  973. if(type & (Rbcast|Rmulti)){
  974. for(cp = f->ipifc->conv; cp < e; cp++){
  975. if(*cp == 0)
  976. continue;
  977. ifc = (Ipifc*)(*cp)->ptcl;
  978. if(ifc->lifc != nil)
  979. return ifc;
  980. }
  981. }
  982. return nil;
  983. }
  984. enum {
  985. unknownv6, /* UGH */
  986. // multicastv6,
  987. unspecifiedv6,
  988. linklocalv6,
  989. globalv6,
  990. };
  991. int
  992. v6addrtype(uchar *addr)
  993. {
  994. if(islinklocal(addr) ||
  995. isv6mcast(addr) && (addr[1] & 0xF) <= Link_local_scop)
  996. return linklocalv6;
  997. else
  998. return globalv6;
  999. }
  1000. #define v6addrcurr(lifc) ((lifc)->preflt == ~0L || \
  1001. (lifc)->origint + (lifc)->preflt >= NOW/1000)
  1002. static void
  1003. findprimaryipv6(Fs *f, uchar *local)
  1004. {
  1005. int atype, atypel;
  1006. Conv **cp, **e;
  1007. Ipifc *ifc;
  1008. Iplifc *lifc;
  1009. ipmove(local, v6Unspecified);
  1010. atype = unspecifiedv6;
  1011. /*
  1012. * find "best" (global > link local > unspecified)
  1013. * local address; address must be current.
  1014. */
  1015. e = &f->ipifc->conv[f->ipifc->nc];
  1016. for(cp = f->ipifc->conv; cp < e; cp++){
  1017. if(*cp == 0)
  1018. continue;
  1019. ifc = (Ipifc*)(*cp)->ptcl;
  1020. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1021. atypel = v6addrtype(lifc->local);
  1022. if(atypel > atype && v6addrcurr(lifc)) {
  1023. ipmove(local, lifc->local);
  1024. atype = atypel;
  1025. if(atype == globalv6)
  1026. return;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. /*
  1032. * returns first ip address configured
  1033. */
  1034. static void
  1035. findprimaryipv4(Fs *f, uchar *local)
  1036. {
  1037. Conv **cp, **e;
  1038. Ipifc *ifc;
  1039. Iplifc *lifc;
  1040. /* find first ifc local address */
  1041. e = &f->ipifc->conv[f->ipifc->nc];
  1042. for(cp = f->ipifc->conv; cp < e; cp++){
  1043. if(*cp == 0)
  1044. continue;
  1045. ifc = (Ipifc*)(*cp)->ptcl;
  1046. if((lifc = ifc->lifc) != nil){
  1047. ipmove(local, lifc->local);
  1048. return;
  1049. }
  1050. }
  1051. }
  1052. /*
  1053. * find the local address 'closest' to the remote system, copy it to
  1054. * local and return the ifc for that address
  1055. */
  1056. void
  1057. findlocalip(Fs *f, uchar *local, uchar *remote)
  1058. {
  1059. int version, atype = unspecifiedv6, atypel = unknownv6;
  1060. int atyper, deprecated;
  1061. uchar gate[IPaddrlen], gnet[IPaddrlen];
  1062. Ipifc *ifc;
  1063. Iplifc *lifc;
  1064. Route *r;
  1065. USED(atype);
  1066. USED(atypel);
  1067. qlock(f->ipifc);
  1068. r = v6lookup(f, remote, nil);
  1069. version = (memcmp(remote, v4prefix, IPv4off) == 0)? V4: V6;
  1070. if(r != nil){
  1071. ifc = r->ifc;
  1072. if(r->type & Rv4)
  1073. v4tov6(gate, r->v4.gate);
  1074. else {
  1075. ipmove(gate, r->v6.gate);
  1076. ipmove(local, v6Unspecified);
  1077. }
  1078. switch(version) {
  1079. case V4:
  1080. /* find ifc address closest to the gateway to use */
  1081. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1082. maskip(gate, lifc->mask, gnet);
  1083. if(ipcmp(gnet, lifc->net) == 0){
  1084. ipmove(local, lifc->local);
  1085. goto out;
  1086. }
  1087. }
  1088. break;
  1089. case V6:
  1090. /* find ifc address with scope matching the destination */
  1091. atyper = v6addrtype(remote);
  1092. deprecated = 0;
  1093. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1094. atypel = v6addrtype(lifc->local);
  1095. /* prefer appropriate scope */
  1096. if(atypel > atype && atype < atyper ||
  1097. atypel < atype && atype > atyper){
  1098. ipmove(local, lifc->local);
  1099. deprecated = !v6addrcurr(lifc);
  1100. atype = atypel;
  1101. } else if(atypel == atype){
  1102. /* avoid deprecated addresses */
  1103. if(deprecated && v6addrcurr(lifc)){
  1104. ipmove(local, lifc->local);
  1105. atype = atypel;
  1106. deprecated = 0;
  1107. }
  1108. }
  1109. if(atype == atyper && !deprecated)
  1110. goto out;
  1111. }
  1112. if(atype >= atyper)
  1113. goto out;
  1114. break;
  1115. default:
  1116. panic("findlocalip: version %d", version);
  1117. }
  1118. }
  1119. switch(version){
  1120. case V4:
  1121. findprimaryipv4(f, local);
  1122. break;
  1123. case V6:
  1124. findprimaryipv6(f, local);
  1125. break;
  1126. default:
  1127. panic("findlocalip2: version %d", version);
  1128. }
  1129. out:
  1130. qunlock(f->ipifc);
  1131. }
  1132. /*
  1133. * return first v4 address associated with an interface
  1134. */
  1135. int
  1136. ipv4local(Ipifc *ifc, uchar *addr)
  1137. {
  1138. Iplifc *lifc;
  1139. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1140. if(isv4(lifc->local)){
  1141. memmove(addr, lifc->local+IPv4off, IPv4addrlen);
  1142. return 1;
  1143. }
  1144. }
  1145. return 0;
  1146. }
  1147. /*
  1148. * return first v6 address associated with an interface
  1149. */
  1150. int
  1151. ipv6local(Ipifc *ifc, uchar *addr)
  1152. {
  1153. Iplifc *lifc;
  1154. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1155. if(!isv4(lifc->local) && !(lifc->tentative)){
  1156. ipmove(addr, lifc->local);
  1157. return 1;
  1158. }
  1159. }
  1160. return 0;
  1161. }
  1162. int
  1163. ipv6anylocal(Ipifc *ifc, uchar *addr)
  1164. {
  1165. Iplifc *lifc;
  1166. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1167. if(!isv4(lifc->local)){
  1168. ipmove(addr, lifc->local);
  1169. return SRC_UNI;
  1170. }
  1171. }
  1172. return SRC_UNSPEC;
  1173. }
  1174. /*
  1175. * see if this address is bound to the interface
  1176. */
  1177. Iplifc*
  1178. iplocalonifc(Ipifc *ifc, uchar *ip)
  1179. {
  1180. Iplifc *lifc;
  1181. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1182. if(ipcmp(ip, lifc->local) == 0)
  1183. return lifc;
  1184. return nil;
  1185. }
  1186. /*
  1187. * See if we're proxying for this address on this interface
  1188. */
  1189. int
  1190. ipproxyifc(Fs *f, Ipifc *ifc, uchar *ip)
  1191. {
  1192. Route *r;
  1193. uchar net[IPaddrlen];
  1194. Iplifc *lifc;
  1195. /* see if this is a direct connected pt to pt address */
  1196. r = v6lookup(f, ip, nil);
  1197. if(r == nil || (r->type & (Rifc|Rproxy)) != (Rifc|Rproxy))
  1198. return 0;
  1199. /* see if this is on the right interface */
  1200. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1201. maskip(ip, lifc->mask, net);
  1202. if(ipcmp(net, lifc->remote) == 0)
  1203. return 1;
  1204. }
  1205. return 0;
  1206. }
  1207. /*
  1208. * return multicast version if any
  1209. */
  1210. int
  1211. ipismulticast(uchar *ip)
  1212. {
  1213. if(isv4(ip)){
  1214. if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
  1215. return V4;
  1216. }
  1217. else if(ip[0] == 0xff)
  1218. return V6;
  1219. return 0;
  1220. }
  1221. int
  1222. ipisbm(uchar *ip)
  1223. {
  1224. if(isv4(ip)){
  1225. if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
  1226. return V4;
  1227. else if(ipcmp(ip, IPv4bcast) == 0)
  1228. return V4;
  1229. }
  1230. else if(ip[0] == 0xff)
  1231. return V6;
  1232. return 0;
  1233. }
  1234. /*
  1235. * add a multicast address to an interface, called with c->car locked
  1236. */
  1237. void
  1238. ipifcaddmulti(Conv *c, uchar *ma, uchar *ia)
  1239. {
  1240. Ipifc *ifc;
  1241. Iplifc *lifc;
  1242. Conv **p;
  1243. Ipmulti *multi, **l;
  1244. Fs *f;
  1245. f = c->p->f;
  1246. for(l = &c->multi; *l; l = &(*l)->next)
  1247. if(ipcmp(ma, (*l)->ma) == 0 && ipcmp(ia, (*l)->ia) == 0)
  1248. return; /* it's already there */
  1249. multi = *l = smalloc(sizeof(*multi));
  1250. ipmove(multi->ma, ma);
  1251. ipmove(multi->ia, ia);
  1252. multi->next = nil;
  1253. for(p = f->ipifc->conv; *p; p++){
  1254. if((*p)->inuse == 0)
  1255. continue;
  1256. ifc = (Ipifc*)(*p)->ptcl;
  1257. if(waserror()){
  1258. wunlock(ifc);
  1259. nexterror();
  1260. }
  1261. wlock(ifc);
  1262. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1263. if(ipcmp(ia, lifc->local) == 0)
  1264. addselfcache(f, ifc, lifc, ma, Rmulti);
  1265. wunlock(ifc);
  1266. poperror();
  1267. }
  1268. }
  1269. /*
  1270. * remove a multicast address from an interface, called with c->car locked
  1271. */
  1272. void
  1273. ipifcremmulti(Conv *c, uchar *ma, uchar *ia)
  1274. {
  1275. Ipmulti *multi, **l;
  1276. Iplifc *lifc;
  1277. Conv **p;
  1278. Ipifc *ifc;
  1279. Fs *f;
  1280. f = c->p->f;
  1281. for(l = &c->multi; *l; l = &(*l)->next)
  1282. if(ipcmp(ma, (*l)->ma) == 0 && ipcmp(ia, (*l)->ia) == 0)
  1283. break;
  1284. multi = *l;
  1285. if(multi == nil)
  1286. return; /* we don't have it open */
  1287. *l = multi->next;
  1288. for(p = f->ipifc->conv; *p; p++){
  1289. if((*p)->inuse == 0)
  1290. continue;
  1291. ifc = (Ipifc*)(*p)->ptcl;
  1292. if(waserror()){
  1293. wunlock(ifc);
  1294. nexterror();
  1295. }
  1296. wlock(ifc);
  1297. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1298. if(ipcmp(ia, lifc->local) == 0)
  1299. remselfcache(f, ifc, lifc, ma);
  1300. wunlock(ifc);
  1301. poperror();
  1302. }
  1303. free(multi);
  1304. }
  1305. /*
  1306. * make lifc's join and leave multicast groups
  1307. */
  1308. static char*
  1309. ipifcjoinmulti(Ipifc *ifc, char **argv, int argc)
  1310. {
  1311. USED(ifc, argv, argc);
  1312. return nil;
  1313. }
  1314. static char*
  1315. ipifcleavemulti(Ipifc *ifc, char **argv, int argc)
  1316. {
  1317. USED(ifc, argv, argc);
  1318. return nil;
  1319. }
  1320. static void
  1321. ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
  1322. {
  1323. Conv **cp, **e;
  1324. Ipifc *nifc;
  1325. Iplifc *lifc;
  1326. Medium *m;
  1327. uchar net[IPaddrlen];
  1328. /* register the address on any network that will proxy for us */
  1329. e = &f->ipifc->conv[f->ipifc->nc];
  1330. if(!isv4(ip)) { /* V6 */
  1331. for(cp = f->ipifc->conv; cp < e; cp++){
  1332. if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc)
  1333. continue;
  1334. rlock(nifc);
  1335. m = nifc->m;
  1336. if(m == nil || m->addmulti == nil) {
  1337. runlock(nifc);
  1338. continue;
  1339. }
  1340. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  1341. maskip(ip, lifc->mask, net);
  1342. if(ipcmp(net, lifc->remote) == 0) {
  1343. /* add solicited-node multicast addr */
  1344. ipv62smcast(net, ip);
  1345. addselfcache(f, nifc, lifc, net, Rmulti);
  1346. arpenter(f, V6, ip, nifc->mac, 6, 0);
  1347. // (*m->addmulti)(nifc, net, ip);
  1348. break;
  1349. }
  1350. }
  1351. runlock(nifc);
  1352. }
  1353. }
  1354. else { /* V4 */
  1355. for(cp = f->ipifc->conv; cp < e; cp++){
  1356. if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc)
  1357. continue;
  1358. rlock(nifc);
  1359. m = nifc->m;
  1360. if(m == nil || m->areg == nil){
  1361. runlock(nifc);
  1362. continue;
  1363. }
  1364. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  1365. maskip(ip, lifc->mask, net);
  1366. if(ipcmp(net, lifc->remote) == 0){
  1367. (*m->areg)(nifc, ip);
  1368. break;
  1369. }
  1370. }
  1371. runlock(nifc);
  1372. }
  1373. }
  1374. }
  1375. /* added for new v6 mesg types */
  1376. static void
  1377. adddefroute6(Fs *f, uchar *gate, int force)
  1378. {
  1379. Route *r;
  1380. r = v6lookup(f, v6Unspecified, nil);
  1381. /*
  1382. * route entries generated by all other means take precedence
  1383. * over router announcements.
  1384. */
  1385. if (r && !force && strcmp(r->tag, "ra") != 0)
  1386. return;
  1387. v6delroute(f, v6Unspecified, v6Unspecified, 1);
  1388. v6addroute(f, "ra", v6Unspecified, v6Unspecified, gate, 0);
  1389. }
  1390. enum {
  1391. Ngates = 3,
  1392. };
  1393. char*
  1394. ipifcadd6(Ipifc *ifc, char**argv, int argc)
  1395. {
  1396. int plen = 64;
  1397. long origint = NOW / 1000, preflt = ~0L, validlt = ~0L;
  1398. char addr[40], preflen[6];
  1399. char *params[3];
  1400. uchar autoflag = 1, onlink = 1;
  1401. uchar prefix[IPaddrlen];
  1402. Iplifc *lifc;
  1403. switch(argc) {
  1404. case 7:
  1405. preflt = atoi(argv[6]);
  1406. /* fall through */
  1407. case 6:
  1408. validlt = atoi(argv[5]);
  1409. /* fall through */
  1410. case 5:
  1411. autoflag = atoi(argv[4]);
  1412. /* fall through */
  1413. case 4:
  1414. onlink = atoi(argv[3]);
  1415. /* fall through */
  1416. case 3:
  1417. plen = atoi(argv[2]);
  1418. /* fall through */
  1419. case 2:
  1420. break;
  1421. default:
  1422. return Ebadarg;
  1423. }
  1424. if (parseip(prefix, argv[1]) != 6 || validlt < preflt || plen < 0 ||
  1425. plen > 64 || islinklocal(prefix))
  1426. return Ebadarg;
  1427. lifc = smalloc(sizeof(Iplifc));
  1428. lifc->onlink = (onlink != 0);
  1429. lifc->autoflag = (autoflag != 0);
  1430. lifc->validlt = validlt;
  1431. lifc->preflt = preflt;
  1432. lifc->origint = origint;
  1433. /* issue "add" ctl msg for v6 link-local addr and prefix len */
  1434. if(!ifc->m->pref2addr)
  1435. return Ebadarg;
  1436. ifc->m->pref2addr(prefix, ifc->mac); /* mac → v6 link-local addr */
  1437. sprint(addr, "%I", prefix);
  1438. sprint(preflen, "/%d", plen);
  1439. params[0] = "add";
  1440. params[1] = addr;
  1441. params[2] = preflen;
  1442. return ipifcadd(ifc, params, 3, 0, lifc);
  1443. }