ipifc.c 33 KB

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