ipifc.c 37 KB

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