ipifc.c 37 KB

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