ipifc.c 37 KB

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