ipifc.c 33 KB

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