ipifc.c 33 KB

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