ipifc.c 37 KB

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