ipifc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. parseip(ip, argv[1]);
  338. parseipmask(mask, argv[2]);
  339. parseip(rem, argv[3]);
  340. maskip(rem, mask, net);
  341. break;
  342. case 3:
  343. parseip(ip, argv[1]);
  344. parseipmask(mask, argv[2]);
  345. maskip(ip, mask, rem);
  346. maskip(rem, mask, net);
  347. break;
  348. case 2:
  349. parseip(ip, argv[1]);
  350. memmove(mask, defmask(ip), IPaddrlen);
  351. maskip(ip, mask, rem);
  352. maskip(rem, mask, net);
  353. break;
  354. default:
  355. return Ebadarg;
  356. }
  357. if(isv4(ip))
  358. tentative = 0;
  359. wlock(ifc);
  360. /* ignore if this is already a local address for this ifc */
  361. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  362. if(ipcmp(lifc->local, ip) == 0) {
  363. if(lifc->tentative != tentative)
  364. lifc->tentative = tentative;
  365. if(lifcp) {
  366. lifc->onlink = lifcp->onlink;
  367. lifc->autoflag = lifcp->autoflag;
  368. lifc->validlt = lifcp->validlt;
  369. lifc->preflt = lifcp->preflt;
  370. lifc->origint = lifcp->origint;
  371. }
  372. goto out;
  373. }
  374. /* add the address to the list of logical ifc's for this ifc */
  375. lifc = smalloc(sizeof(Iplifc));
  376. ipmove(lifc->local, ip);
  377. ipmove(lifc->mask, mask);
  378. ipmove(lifc->remote, rem);
  379. ipmove(lifc->net, net);
  380. lifc->tentative = tentative;
  381. if(lifcp) {
  382. lifc->onlink = lifcp->onlink;
  383. lifc->autoflag = lifcp->autoflag;
  384. lifc->validlt = lifcp->validlt;
  385. lifc->preflt = lifcp->preflt;
  386. lifc->origint = lifcp->origint;
  387. } else { /* default values */
  388. lifc->onlink = lifc->autoflag = 1;
  389. lifc->validlt = lifc->preflt = ~0L;
  390. lifc->origint = NOW / 1000;
  391. }
  392. lifc->next = nil;
  393. for(l = &ifc->lifc; *l; l = &(*l)->next)
  394. ;
  395. *l = lifc;
  396. /* check for point-to-point interface */
  397. if(ipcmp(ip, v6loopback)) /* skip v6 loopback, it's a special address */
  398. if(ipcmp(mask, IPallbits) == 0)
  399. type |= Rptpt;
  400. /* add local routes */
  401. if(isv4(ip))
  402. v4addroute(f, tifc, rem+IPv4off, mask+IPv4off, rem+IPv4off, type);
  403. else
  404. v6addroute(f, tifc, rem, mask, rem, type);
  405. addselfcache(f, ifc, lifc, ip, Runi);
  406. if((type & (Rproxy|Rptpt)) == (Rproxy|Rptpt)){
  407. ipifcregisterproxy(f, ifc, rem);
  408. goto out;
  409. }
  410. if(isv4(ip) || ipcmp(ip, IPnoaddr) == 0) {
  411. /* add subnet directed broadcast address to the self cache */
  412. for(i = 0; i < IPaddrlen; i++)
  413. bcast[i] = (ip[i] & mask[i]) | ~mask[i];
  414. addselfcache(f, ifc, lifc, bcast, Rbcast);
  415. /* add subnet directed network address to the self cache */
  416. for(i = 0; i < IPaddrlen; i++)
  417. bcast[i] = (ip[i] & mask[i]) & mask[i];
  418. addselfcache(f, ifc, lifc, bcast, Rbcast);
  419. /* add network directed broadcast address to the self cache */
  420. memmove(mask, defmask(ip), IPaddrlen);
  421. for(i = 0; i < IPaddrlen; i++)
  422. bcast[i] = (ip[i] & mask[i]) | ~mask[i];
  423. addselfcache(f, ifc, lifc, bcast, Rbcast);
  424. /* add network directed network address to the self cache */
  425. memmove(mask, defmask(ip), IPaddrlen);
  426. for(i = 0; i < IPaddrlen; i++)
  427. bcast[i] = (ip[i] & mask[i]) & mask[i];
  428. addselfcache(f, ifc, lifc, bcast, Rbcast);
  429. addselfcache(f, ifc, lifc, IPv4bcast, Rbcast);
  430. } else {
  431. if(ipcmp(ip, v6loopback) == 0) {
  432. /* add node-local mcast address */
  433. addselfcache(f, ifc, lifc, v6allnodesN, Rmulti);
  434. /* add route for all node multicast */
  435. v6addroute(f, tifc, v6allnodesN, v6allnodesNmask,
  436. v6allnodesN, Rmulti);
  437. }
  438. /* add all nodes multicast address */
  439. addselfcache(f, ifc, lifc, v6allnodesL, Rmulti);
  440. /* add route for all nodes multicast */
  441. v6addroute(f, tifc, v6allnodesL, v6allnodesLmask, v6allnodesL,
  442. Rmulti);
  443. /* add solicited-node multicast address */
  444. ipv62smcast(bcast, ip);
  445. addselfcache(f, ifc, lifc, bcast, Rmulti);
  446. sendnbrdisc = 1;
  447. }
  448. /* register the address on this network for address resolution */
  449. if(isv4(ip) && ifc->m->areg != nil)
  450. (*ifc->m->areg)(ifc, ip);
  451. out:
  452. wunlock(ifc);
  453. if(tentative && sendnbrdisc)
  454. icmpns(f, 0, SRC_UNSPEC, ip, TARG_MULTI, ifc->mac);
  455. return nil;
  456. }
  457. /*
  458. * remove a logical interface from an ifc
  459. * always called with ifc wlock'd
  460. */
  461. static char*
  462. ipifcremlifc(Ipifc *ifc, Iplifc *lifc)
  463. {
  464. Iplifc **l;
  465. Fs *f;
  466. f = ifc->conv->p->f;
  467. /*
  468. * find address on this interface and remove from chain.
  469. * for pt to pt we actually specify the remote address as the
  470. * addresss to remove.
  471. */
  472. for(l = &ifc->lifc; *l != nil && *l != lifc; l = &(*l)->next)
  473. ;
  474. if(*l == nil)
  475. return "address not on this interface";
  476. *l = lifc->next;
  477. /* disassociate any addresses */
  478. while(lifc->link)
  479. remselfcache(f, ifc, lifc, lifc->link->self->a);
  480. /* remove the route for this logical interface */
  481. if(isv4(lifc->local))
  482. v4delroute(f, lifc->remote+IPv4off, lifc->mask+IPv4off, 1);
  483. else {
  484. v6delroute(f, lifc->remote, lifc->mask, 1);
  485. if(ipcmp(lifc->local, v6loopback) == 0)
  486. /* remove route for all node multicast */
  487. v6delroute(f, v6allnodesN, v6allnodesNmask, 1);
  488. else if(memcmp(lifc->local, v6linklocal, v6llpreflen) == 0)
  489. /* remove route for all link multicast */
  490. v6delroute(f, v6allnodesL, v6allnodesLmask, 1);
  491. }
  492. free(lifc);
  493. return nil;
  494. }
  495. /*
  496. * remove an address from an interface.
  497. * called with c->car locked
  498. */
  499. char*
  500. ipifcrem(Ipifc *ifc, char **argv, int argc)
  501. {
  502. char *rv;
  503. uchar ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
  504. Iplifc *lifc;
  505. if(argc < 3)
  506. return Ebadarg;
  507. parseip(ip, argv[1]);
  508. parseipmask(mask, argv[2]);
  509. if(argc < 4)
  510. maskip(ip, mask, rem);
  511. else
  512. parseip(rem, argv[3]);
  513. wlock(ifc);
  514. /*
  515. * find address on this interface and remove from chain.
  516. * for pt to pt we actually specify the remote address as the
  517. * addresss to remove.
  518. */
  519. for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next)
  520. if (memcmp(ip, lifc->local, IPaddrlen) == 0
  521. && memcmp(mask, lifc->mask, IPaddrlen) == 0
  522. && memcmp(rem, lifc->remote, IPaddrlen) == 0)
  523. break;
  524. rv = ipifcremlifc(ifc, lifc);
  525. wunlock(ifc);
  526. return rv;
  527. }
  528. /*
  529. * distribute routes to active interfaces like the
  530. * TRIP linecards
  531. */
  532. void
  533. ipifcaddroute(Fs *f, int vers, uchar *addr, uchar *mask, uchar *gate, int type)
  534. {
  535. Medium *m;
  536. Conv **cp, **e;
  537. Ipifc *ifc;
  538. e = &f->ipifc->conv[f->ipifc->nc];
  539. for(cp = f->ipifc->conv; cp < e; cp++)
  540. if(*cp != nil) {
  541. ifc = (Ipifc*)(*cp)->ptcl;
  542. m = ifc->m;
  543. if(m && m->addroute)
  544. m->addroute(ifc, vers, addr, mask, gate, type);
  545. }
  546. }
  547. void
  548. ipifcremroute(Fs *f, int vers, uchar *addr, uchar *mask)
  549. {
  550. Medium *m;
  551. Conv **cp, **e;
  552. Ipifc *ifc;
  553. e = &f->ipifc->conv[f->ipifc->nc];
  554. for(cp = f->ipifc->conv; cp < e; cp++)
  555. if(*cp != nil) {
  556. ifc = (Ipifc*)(*cp)->ptcl;
  557. m = ifc->m;
  558. if(m && m->remroute)
  559. m->remroute(ifc, vers, addr, mask);
  560. }
  561. }
  562. /*
  563. * associate an address with the interface. This wipes out any previous
  564. * addresses. This is a macro that means, remove all the old interfaces
  565. * and add a new one.
  566. */
  567. static char*
  568. ipifcconnect(Conv* c, char **argv, int argc)
  569. {
  570. char *err;
  571. Ipifc *ifc;
  572. ifc = (Ipifc*)c->ptcl;
  573. if(ifc->m == nil)
  574. return "ipifc not yet bound to device";
  575. if(waserror()){
  576. wunlock(ifc);
  577. nexterror();
  578. }
  579. wlock(ifc);
  580. while(ifc->lifc){
  581. err = ipifcremlifc(ifc, ifc->lifc);
  582. if(err)
  583. error(err);
  584. }
  585. wunlock(ifc);
  586. poperror();
  587. err = ipifcadd(ifc, argv, argc, 0, nil);
  588. if(err)
  589. return err;
  590. Fsconnected(c, nil);
  591. return nil;
  592. }
  593. char*
  594. ipifcra6(Ipifc *ifc, char **argv, int argc)
  595. {
  596. int i, argsleft, vmax = ifc->rp.maxraint, vmin = ifc->rp.minraint;
  597. argsleft = argc - 1;
  598. i = 1;
  599. if(argsleft % 2 != 0)
  600. return Ebadarg;
  601. while (argsleft > 1) {
  602. if(strcmp(argv[i], "recvra") == 0)
  603. ifc->recvra6 = (atoi(argv[i+1]) != 0);
  604. else if(strcmp(argv[i], "sendra") == 0)
  605. ifc->sendra6 = (atoi(argv[i+1]) != 0);
  606. else if(strcmp(argv[i], "mflag") == 0)
  607. ifc->rp.mflag = (atoi(argv[i+1]) != 0);
  608. else if(strcmp(argv[i], "oflag") == 0)
  609. ifc->rp.oflag = (atoi(argv[i+1]) != 0);
  610. else if(strcmp(argv[i], "maxraint") == 0)
  611. ifc->rp.maxraint = atoi(argv[i+1]);
  612. else if(strcmp(argv[i], "minraint") == 0)
  613. ifc->rp.minraint = atoi(argv[i+1]);
  614. else if(strcmp(argv[i], "linkmtu") == 0)
  615. ifc->rp.linkmtu = atoi(argv[i+1]);
  616. else if(strcmp(argv[i], "reachtime") == 0)
  617. ifc->rp.reachtime = atoi(argv[i+1]);
  618. else if(strcmp(argv[i], "rxmitra") == 0)
  619. ifc->rp.rxmitra = atoi(argv[i+1]);
  620. else if(strcmp(argv[i], "ttl") == 0)
  621. ifc->rp.ttl = atoi(argv[i+1]);
  622. else if(strcmp(argv[i], "routerlt") == 0)
  623. ifc->rp.routerlt = atoi(argv[i+1]);
  624. else
  625. return Ebadarg;
  626. argsleft -= 2;
  627. i += 2;
  628. }
  629. /* consistency check */
  630. if(ifc->rp.maxraint < ifc->rp.minraint) {
  631. ifc->rp.maxraint = vmax;
  632. ifc->rp.minraint = vmin;
  633. return Ebadarg;
  634. }
  635. return nil;
  636. }
  637. /*
  638. * non-standard control messages.
  639. * called with c->car locked.
  640. */
  641. static char*
  642. ipifcctl(Conv* c, char**argv, int argc)
  643. {
  644. Ipifc *ifc;
  645. ifc = (Ipifc*)c->ptcl;
  646. if(strcmp(argv[0], "add") == 0)
  647. return ipifcadd(ifc, argv, argc, 0, nil);
  648. else if(strcmp(argv[0], "try") == 0)
  649. return ipifcadd(ifc, argv, argc, 1, nil);
  650. else if(strcmp(argv[0], "remove") == 0)
  651. return ipifcrem(ifc, argv, argc);
  652. else if(strcmp(argv[0], "unbind") == 0)
  653. return ipifcunbind(ifc);
  654. else if(strcmp(argv[0], "joinmulti") == 0)
  655. return ipifcjoinmulti(ifc, argv, argc);
  656. else if(strcmp(argv[0], "leavemulti") == 0)
  657. return ipifcleavemulti(ifc, argv, argc);
  658. else if(strcmp(argv[0], "mtu") == 0)
  659. return ipifcsetmtu(ifc, argv, argc);
  660. else if(strcmp(argv[0], "reassemble") == 0){
  661. ifc->reassemble = 1;
  662. return nil;
  663. } else if(strcmp(argv[0], "iprouting") == 0){
  664. iprouting(c->p->f, (argc > 1? atoi(argv[1]): 1));
  665. return nil;
  666. } else if(strcmp(argv[0], "add6") == 0)
  667. return ipifcadd6(ifc, argv, argc);
  668. else if(strcmp(argv[0], "ra6") == 0)
  669. return ipifcra6(ifc, argv, argc);
  670. else
  671. return "unsupported ctl";
  672. }
  673. int
  674. ipifcstats(Proto *ipifc, char *buf, int len)
  675. {
  676. return ipstats(ipifc->f, buf, len);
  677. }
  678. void
  679. ipifcinit(Fs *f)
  680. {
  681. Proto *ipifc;
  682. ipifc = smalloc(sizeof(Proto));
  683. ipifc->name = "ipifc";
  684. ipifc->connect = ipifcconnect;
  685. ipifc->announce = nil;
  686. ipifc->bind = ipifcbind;
  687. ipifc->state = ipifcstate;
  688. ipifc->create = ipifccreate;
  689. ipifc->close = ipifcclose;
  690. ipifc->rcv = nil;
  691. ipifc->ctl = ipifcctl;
  692. ipifc->advise = nil;
  693. ipifc->stats = ipifcstats;
  694. ipifc->inuse = ipifcinuse;
  695. ipifc->local = ipifclocal;
  696. ipifc->ipproto = -1;
  697. ipifc->nc = Maxmedia;
  698. ipifc->ptclsize = sizeof(Ipifc);
  699. f->ipifc = ipifc; /* hack for ipifcremroute, findipifc, ... */
  700. f->self = smalloc(sizeof(Ipselftab)); /* hack for ipforme */
  701. Fsproto(f, ipifc);
  702. }
  703. /*
  704. * add to self routing cache
  705. * called with c->car locked
  706. */
  707. static void
  708. addselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a, int type)
  709. {
  710. Ipself *p;
  711. Iplink *lp;
  712. int h;
  713. qlock(f->self);
  714. /* see if the address already exists */
  715. h = hashipa(a);
  716. for(p = f->self->hash[h]; p; p = p->next)
  717. if(memcmp(a, p->a, IPaddrlen) == 0)
  718. break;
  719. /* allocate a local address and add to hash chain */
  720. if(p == nil){
  721. p = smalloc(sizeof(*p));
  722. ipmove(p->a, a);
  723. p->type = type;
  724. p->next = f->self->hash[h];
  725. f->self->hash[h] = p;
  726. /* if the null address, accept all packets */
  727. if(ipcmp(a, v4prefix) == 0 || ipcmp(a, IPnoaddr) == 0)
  728. f->self->acceptall = 1;
  729. }
  730. /* look for a link for this lifc */
  731. for(lp = p->link; lp; lp = lp->selflink)
  732. if(lp->lifc == lifc)
  733. break;
  734. /* allocate a lifc-to-local link and link to both */
  735. if(lp == nil){
  736. lp = smalloc(sizeof(*lp));
  737. lp->ref = 1;
  738. lp->lifc = lifc;
  739. lp->self = p;
  740. lp->selflink = p->link;
  741. p->link = lp;
  742. lp->lifclink = lifc->link;
  743. lifc->link = lp;
  744. /* add to routing table */
  745. if(isv4(a))
  746. v4addroute(f, tifc, a+IPv4off, IPallbits+IPv4off,
  747. a+IPv4off, type);
  748. else
  749. v6addroute(f, tifc, a, IPallbits, a, type);
  750. if((type & Rmulti) && ifc->m->addmulti != nil)
  751. (*ifc->m->addmulti)(ifc, a, lifc->local);
  752. } else
  753. lp->ref++;
  754. qunlock(f->self);
  755. }
  756. /*
  757. * These structures are unlinked from their chains while
  758. * other threads may be using them. To avoid excessive locking,
  759. * just put them aside for a while before freeing them.
  760. * called with f->self locked
  761. */
  762. static Iplink *freeiplink;
  763. static Ipself *freeipself;
  764. static void
  765. iplinkfree(Iplink *p)
  766. {
  767. Iplink **l, *np;
  768. ulong now = NOW;
  769. l = &freeiplink;
  770. for(np = *l; np; np = *l){
  771. if(np->expire > now){
  772. *l = np->next;
  773. free(np);
  774. continue;
  775. }
  776. l = &np->next;
  777. }
  778. p->expire = now + 5000; /* give other threads 5 secs to get out */
  779. p->next = nil;
  780. *l = p;
  781. }
  782. static void
  783. ipselffree(Ipself *p)
  784. {
  785. Ipself **l, *np;
  786. ulong now = NOW;
  787. l = &freeipself;
  788. for(np = *l; np; np = *l){
  789. if(np->expire > now){
  790. *l = np->next;
  791. free(np);
  792. continue;
  793. }
  794. l = &np->next;
  795. }
  796. p->expire = now + 5000; /* give other threads 5 secs to get out */
  797. p->next = nil;
  798. *l = p;
  799. }
  800. /*
  801. * Decrement reference for this address on this link.
  802. * Unlink from selftab if this is the last ref.
  803. * called with c->car locked
  804. */
  805. static void
  806. remselfcache(Fs *f, Ipifc *ifc, Iplifc *lifc, uchar *a)
  807. {
  808. Ipself *p, **l;
  809. Iplink *link, **l_self, **l_lifc;
  810. qlock(f->self);
  811. /* find the unique selftab entry */
  812. l = &f->self->hash[hashipa(a)];
  813. for(p = *l; p; p = *l){
  814. if(ipcmp(p->a, a) == 0)
  815. break;
  816. l = &p->next;
  817. }
  818. if(p == nil)
  819. goto out;
  820. /*
  821. * walk down links from an ifc looking for one
  822. * that matches the selftab entry
  823. */
  824. l_lifc = &lifc->link;
  825. for(link = *l_lifc; link; link = *l_lifc){
  826. if(link->self == p)
  827. break;
  828. l_lifc = &link->lifclink;
  829. }
  830. if(link == nil)
  831. goto out;
  832. /*
  833. * walk down the links from the selftab looking for
  834. * the one we just found
  835. */
  836. l_self = &p->link;
  837. for(link = *l_self; link; link = *l_self){
  838. if(link == *l_lifc)
  839. break;
  840. l_self = &link->selflink;
  841. }
  842. if(link == nil)
  843. panic("remselfcache");
  844. if(--(link->ref) != 0)
  845. goto out;
  846. if((p->type & Rmulti) && ifc->m->remmulti != nil)
  847. (*ifc->m->remmulti)(ifc, a, lifc->local);
  848. /* ref == 0, remove from both chains and free the link */
  849. *l_lifc = link->lifclink;
  850. *l_self = link->selflink;
  851. iplinkfree(link);
  852. if(p->link != nil)
  853. goto out;
  854. /* remove from routing table */
  855. if(isv4(a))
  856. v4delroute(f, a+IPv4off, IPallbits+IPv4off, 1);
  857. else
  858. v6delroute(f, a, IPallbits, 1);
  859. /* no more links, remove from hash and free */
  860. *l = p->next;
  861. ipselffree(p);
  862. /* if IPnoaddr, forget */
  863. if(ipcmp(a, v4prefix) == 0 || ipcmp(a, IPnoaddr) == 0)
  864. f->self->acceptall = 0;
  865. out:
  866. qunlock(f->self);
  867. }
  868. static char *stformat = "%-44.44I %2.2d %4.4s\n";
  869. enum
  870. {
  871. Nstformat= 41,
  872. };
  873. long
  874. ipselftabread(Fs *f, char *cp, ulong offset, int n)
  875. {
  876. int i, m, nifc, off;
  877. Ipself *p;
  878. Iplink *link;
  879. char state[8];
  880. m = 0;
  881. off = offset;
  882. qlock(f->self);
  883. for(i = 0; i < NHASH && m < n; i++)
  884. for(p = f->self->hash[i]; p != nil && m < n; p = p->next){
  885. nifc = 0;
  886. for(link = p->link; link; link = link->selflink)
  887. nifc++;
  888. routetype(p->type, state);
  889. m += snprint(cp + m, n - m, stformat, p->a, nifc, state);
  890. if(off > 0){
  891. off -= m;
  892. m = 0;
  893. }
  894. }
  895. qunlock(f->self);
  896. return m;
  897. }
  898. int
  899. iptentative(Fs *f, uchar *addr)
  900. {
  901. Ipself *p;
  902. p = f->self->hash[hashipa(addr)];
  903. for(; p; p = p->next)
  904. if(ipcmp(addr, p->a) == 0)
  905. return p->link->lifc->tentative;
  906. return 0;
  907. }
  908. /*
  909. * returns
  910. * 0 - no match
  911. * Runi
  912. * Rbcast
  913. * Rmcast
  914. */
  915. int
  916. ipforme(Fs *f, uchar *addr)
  917. {
  918. Ipself *p;
  919. p = f->self->hash[hashipa(addr)];
  920. for(; p; p = p->next)
  921. if(ipcmp(addr, p->a) == 0)
  922. return p->type;
  923. /* hack to say accept anything */
  924. if(f->self->acceptall)
  925. return Runi;
  926. return 0;
  927. }
  928. /*
  929. * find the ifc on same net as the remote system. If none,
  930. * return nil.
  931. */
  932. Ipifc*
  933. findipifc(Fs *f, uchar *remote, int type)
  934. {
  935. Ipifc *ifc, *x;
  936. Iplifc *lifc;
  937. Conv **cp, **e;
  938. uchar gnet[IPaddrlen], xmask[IPaddrlen];
  939. x = nil;
  940. memset(xmask, 0, IPaddrlen);
  941. /* find most specific match */
  942. e = &f->ipifc->conv[f->ipifc->nc];
  943. for(cp = f->ipifc->conv; cp < e; cp++){
  944. if(*cp == 0)
  945. continue;
  946. ifc = (Ipifc*)(*cp)->ptcl;
  947. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  948. maskip(remote, lifc->mask, gnet);
  949. if(ipcmp(gnet, lifc->net) == 0)
  950. if(x == nil || ipcmp(lifc->mask, xmask) > 0){
  951. x = ifc;
  952. ipmove(xmask, lifc->mask);
  953. }
  954. }
  955. }
  956. if(x != nil)
  957. return x;
  958. /* for now for broadcast and multicast, just use first interface */
  959. if(type & (Rbcast|Rmulti))
  960. for(cp = f->ipifc->conv; cp < e; cp++){
  961. if(*cp == 0)
  962. continue;
  963. ifc = (Ipifc*)(*cp)->ptcl;
  964. if(ifc->lifc != nil)
  965. return ifc;
  966. }
  967. return nil;
  968. }
  969. enum {
  970. unknownv6, /* UGH */
  971. multicastv6,
  972. unspecifiedv6,
  973. linklocalv6,
  974. sitelocalv6,
  975. globalv6,
  976. };
  977. int
  978. v6addrtype(uchar *addr)
  979. {
  980. if(isv6global(addr))
  981. return globalv6;
  982. if(islinklocal(addr))
  983. return linklocalv6;
  984. if(isv6mcast(addr))
  985. return multicastv6;
  986. if(issitelocal(addr))
  987. return sitelocalv6;
  988. return unknownv6;
  989. }
  990. #define v6addrcurr(lifc) ((lifc)->origint + (lifc)->preflt >= NOW/1000 || \
  991. (lifc)->preflt == ~0L)
  992. static void
  993. findprimaryipv6(Fs *f, uchar *local)
  994. {
  995. int atype, atypel;
  996. Conv **cp, **e;
  997. Ipifc *ifc;
  998. Iplifc *lifc;
  999. ipmove(local, v6Unspecified);
  1000. atype = unspecifiedv6;
  1001. /* find "best" (global > sitelocal > link local > unspecified)
  1002. * local address; address must be current */
  1003. e = &f->ipifc->conv[f->ipifc->nc];
  1004. for(cp = f->ipifc->conv; cp < e; cp++){
  1005. if(*cp == 0)
  1006. continue;
  1007. ifc = (Ipifc*)(*cp)->ptcl;
  1008. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1009. atypel = v6addrtype(lifc->local);
  1010. if(atypel > atype && v6addrcurr(lifc)) {
  1011. ipmove(local, lifc->local);
  1012. atype = atypel;
  1013. if(atype == globalv6)
  1014. return;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. /*
  1020. * returns first ip address configured
  1021. */
  1022. static void
  1023. findprimaryipv4(Fs *f, uchar *local)
  1024. {
  1025. Conv **cp, **e;
  1026. Ipifc *ifc;
  1027. Iplifc *lifc;
  1028. /* find first ifc local address */
  1029. e = &f->ipifc->conv[f->ipifc->nc];
  1030. for(cp = f->ipifc->conv; cp < e; cp++){
  1031. if(*cp == 0)
  1032. continue;
  1033. ifc = (Ipifc*)(*cp)->ptcl;
  1034. if((lifc = ifc->lifc) != nil){
  1035. ipmove(local, lifc->local);
  1036. return;
  1037. }
  1038. }
  1039. }
  1040. /*
  1041. * find the local address 'closest' to the remote system, copy it to
  1042. * local and return the ifc for that address
  1043. */
  1044. void
  1045. findlocalip(Fs *f, uchar *local, uchar *remote)
  1046. {
  1047. int version, atype = unspecifiedv6, atypel = unknownv6;
  1048. uchar gate[IPaddrlen], gnet[IPaddrlen];
  1049. Ipifc *ifc;
  1050. Iplifc *lifc;
  1051. Route *r;
  1052. USED(atype);
  1053. USED(atypel);
  1054. qlock(f->ipifc);
  1055. r = v6lookup(f, remote, nil);
  1056. version = (memcmp(remote, v4prefix, IPv4off) == 0)? V4: V6;
  1057. if(r != nil){
  1058. ifc = r->ifc;
  1059. if(r->type & Rv4)
  1060. v4tov6(gate, r->v4.gate);
  1061. else {
  1062. ipmove(gate, r->v6.gate);
  1063. ipmove(local, v6Unspecified);
  1064. }
  1065. /* find ifc address closest to the gateway to use */
  1066. switch(version) {
  1067. case V4:
  1068. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1069. maskip(gate, lifc->mask, gnet);
  1070. if(ipcmp(gnet, lifc->net) == 0){
  1071. ipmove(local, lifc->local);
  1072. goto out;
  1073. }
  1074. }
  1075. break;
  1076. case V6:
  1077. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1078. atypel = v6addrtype(lifc->local);
  1079. maskip(gate, lifc->mask, gnet);
  1080. if (ipcmp(gnet, lifc->net) == 0 &&
  1081. atypel > atype && v6addrcurr(lifc)) {
  1082. ipmove(local, lifc->local);
  1083. atype = atypel;
  1084. if(atype == globalv6)
  1085. break;
  1086. }
  1087. }
  1088. if(atype > unspecifiedv6)
  1089. goto out;
  1090. break;
  1091. default:
  1092. panic("findlocalip: version %d", version);
  1093. }
  1094. }
  1095. switch(version){
  1096. case V4:
  1097. findprimaryipv4(f, local);
  1098. break;
  1099. case V6:
  1100. findprimaryipv6(f, local);
  1101. break;
  1102. default:
  1103. panic("findlocalip2: version %d", version);
  1104. }
  1105. out:
  1106. qunlock(f->ipifc);
  1107. }
  1108. /*
  1109. * return first v4 address associated with an interface
  1110. */
  1111. int
  1112. ipv4local(Ipifc *ifc, uchar *addr)
  1113. {
  1114. Iplifc *lifc;
  1115. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1116. if(isv4(lifc->local)){
  1117. memmove(addr, lifc->local+IPv4off, IPv4addrlen);
  1118. return 1;
  1119. }
  1120. return 0;
  1121. }
  1122. /*
  1123. * return first v6 address associated with an interface
  1124. */
  1125. int
  1126. ipv6local(Ipifc *ifc, uchar *addr)
  1127. {
  1128. Iplifc *lifc;
  1129. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1130. if(!isv4(lifc->local) && !(lifc->tentative)){
  1131. ipmove(addr, lifc->local);
  1132. return 1;
  1133. }
  1134. return 0;
  1135. }
  1136. int
  1137. ipv6anylocal(Ipifc *ifc, uchar *addr)
  1138. {
  1139. Iplifc *lifc;
  1140. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1141. if(!isv4(lifc->local)){
  1142. ipmove(addr, lifc->local);
  1143. return SRC_UNI;
  1144. }
  1145. return SRC_UNSPEC;
  1146. }
  1147. /*
  1148. * see if this address is bound to the interface
  1149. */
  1150. Iplifc*
  1151. iplocalonifc(Ipifc *ifc, uchar *ip)
  1152. {
  1153. Iplifc *lifc;
  1154. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1155. if(ipcmp(ip, lifc->local) == 0)
  1156. return lifc;
  1157. return nil;
  1158. }
  1159. /*
  1160. * See if we're proxying for this address on this interface
  1161. */
  1162. int
  1163. ipproxyifc(Fs *f, Ipifc *ifc, uchar *ip)
  1164. {
  1165. Route *r;
  1166. uchar net[IPaddrlen];
  1167. Iplifc *lifc;
  1168. /* see if this is a direct connected pt to pt address */
  1169. r = v6lookup(f, ip, nil);
  1170. if(r == nil || (r->type & (Rifc|Rproxy)) != (Rifc|Rproxy))
  1171. return 0;
  1172. /* see if this is on the right interface */
  1173. for(lifc = ifc->lifc; lifc; lifc = lifc->next){
  1174. maskip(ip, lifc->mask, net);
  1175. if(ipcmp(net, lifc->remote) == 0)
  1176. return 1;
  1177. }
  1178. return 0;
  1179. }
  1180. /*
  1181. * return multicast version if any
  1182. */
  1183. int
  1184. ipismulticast(uchar *ip)
  1185. {
  1186. if(isv4(ip)){
  1187. if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
  1188. return V4;
  1189. } else
  1190. if(ip[0] == 0xff)
  1191. return V6;
  1192. return 0;
  1193. }
  1194. int
  1195. ipisbm(uchar *ip)
  1196. {
  1197. if(isv4(ip))
  1198. if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
  1199. return V4;
  1200. else if(ipcmp(ip, IPv4bcast) == 0)
  1201. return V4;
  1202. else
  1203. if(ip[0] == 0xff)
  1204. return V6;
  1205. return 0;
  1206. }
  1207. /*
  1208. * add a multicast address to an interface, called with c->car locked
  1209. */
  1210. void
  1211. ipifcaddmulti(Conv *c, uchar *ma, uchar *ia)
  1212. {
  1213. Ipifc *ifc;
  1214. Iplifc *lifc;
  1215. Conv **p;
  1216. Ipmulti *multi, **l;
  1217. Fs *f;
  1218. f = c->p->f;
  1219. for(l = &c->multi; *l; l = &(*l)->next)
  1220. if(ipcmp(ma, (*l)->ma) == 0 && ipcmp(ia, (*l)->ia) == 0)
  1221. return; /* it's already there */
  1222. multi = *l = smalloc(sizeof(*multi));
  1223. ipmove(multi->ma, ma);
  1224. ipmove(multi->ia, ia);
  1225. multi->next = nil;
  1226. for(p = f->ipifc->conv; *p; p++){
  1227. if((*p)->inuse == 0)
  1228. continue;
  1229. ifc = (Ipifc*)(*p)->ptcl;
  1230. if(waserror()){
  1231. wunlock(ifc);
  1232. nexterror();
  1233. }
  1234. wlock(ifc);
  1235. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1236. if(ipcmp(ia, lifc->local) == 0)
  1237. addselfcache(f, ifc, lifc, ma, Rmulti);
  1238. wunlock(ifc);
  1239. poperror();
  1240. }
  1241. }
  1242. /*
  1243. * remove a multicast address from an interface, called with c->car locked
  1244. */
  1245. void
  1246. ipifcremmulti(Conv *c, uchar *ma, uchar *ia)
  1247. {
  1248. Ipmulti *multi, **l;
  1249. Iplifc *lifc;
  1250. Conv **p;
  1251. Ipifc *ifc;
  1252. Fs *f;
  1253. f = c->p->f;
  1254. for(l = &c->multi; *l; l = &(*l)->next)
  1255. if(ipcmp(ma, (*l)->ma) == 0 && ipcmp(ia, (*l)->ia) == 0)
  1256. break;
  1257. multi = *l;
  1258. if(multi == nil)
  1259. return; /* we don't have it open */
  1260. *l = multi->next;
  1261. for(p = f->ipifc->conv; *p; p++){
  1262. if((*p)->inuse == 0)
  1263. continue;
  1264. ifc = (Ipifc*)(*p)->ptcl;
  1265. if(waserror()){
  1266. wunlock(ifc);
  1267. nexterror();
  1268. }
  1269. wlock(ifc);
  1270. for(lifc = ifc->lifc; lifc; lifc = lifc->next)
  1271. if(ipcmp(ia, lifc->local) == 0)
  1272. remselfcache(f, ifc, lifc, ma);
  1273. wunlock(ifc);
  1274. poperror();
  1275. }
  1276. free(multi);
  1277. }
  1278. /*
  1279. * make lifc's join and leave multicast groups
  1280. */
  1281. static char*
  1282. ipifcjoinmulti(Ipifc *ifc, char **argv, int argc)
  1283. {
  1284. USED(ifc, argv, argc);
  1285. return nil;
  1286. }
  1287. static char*
  1288. ipifcleavemulti(Ipifc *ifc, char **argv, int argc)
  1289. {
  1290. USED(ifc, argv, argc);
  1291. return nil;
  1292. }
  1293. static void
  1294. ipifcregisterproxy(Fs *f, Ipifc *ifc, uchar *ip)
  1295. {
  1296. Conv **cp, **e;
  1297. Ipifc *nifc;
  1298. Iplifc *lifc;
  1299. Medium *m;
  1300. uchar net[IPaddrlen];
  1301. /* register the address on any network that will proxy for us */
  1302. e = &f->ipifc->conv[f->ipifc->nc];
  1303. if(!isv4(ip)) /* V6 */
  1304. for(cp = f->ipifc->conv; cp < e; cp++){
  1305. if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc)
  1306. continue;
  1307. rlock(nifc);
  1308. m = nifc->m;
  1309. if(m == nil || m->addmulti == nil) {
  1310. runlock(nifc);
  1311. continue;
  1312. }
  1313. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  1314. maskip(ip, lifc->mask, net);
  1315. if(ipcmp(net, lifc->remote) == 0) {
  1316. /* add solicited-node multicast addr */
  1317. ipv62smcast(net, ip);
  1318. addselfcache(f, nifc, lifc, net, Rmulti);
  1319. arpenter(f, V6, ip, nifc->mac, 6, 0);
  1320. // (*m->addmulti)(nifc, net, ip);
  1321. break;
  1322. }
  1323. }
  1324. runlock(nifc);
  1325. }
  1326. else /* V4 */
  1327. for(cp = f->ipifc->conv; cp < e; cp++){
  1328. if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc)
  1329. continue;
  1330. rlock(nifc);
  1331. m = nifc->m;
  1332. if(m == nil || m->areg == nil){
  1333. runlock(nifc);
  1334. continue;
  1335. }
  1336. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  1337. maskip(ip, lifc->mask, net);
  1338. if(ipcmp(net, lifc->remote) == 0){
  1339. (*m->areg)(nifc, ip);
  1340. break;
  1341. }
  1342. }
  1343. runlock(nifc);
  1344. }
  1345. }
  1346. /* added for new v6 mesg types */
  1347. static void
  1348. adddefroute6(Fs *f, uchar *gate, int force)
  1349. {
  1350. Route *r;
  1351. r = v6lookup(f, v6Unspecified, nil);
  1352. /*
  1353. * route entries generated by all other means take precedence
  1354. * over router announcements.
  1355. */
  1356. if (r && !force && strcmp(r->tag, "ra") != 0)
  1357. return;
  1358. v6delroute(f, v6Unspecified, v6Unspecified, 1);
  1359. v6addroute(f, "ra", v6Unspecified, v6Unspecified, gate, 0);
  1360. }
  1361. enum {
  1362. Ngates = 3,
  1363. };
  1364. char*
  1365. ipifcadd6(Ipifc *ifc, char**argv, int argc)
  1366. {
  1367. int plen = 64;
  1368. long origint = NOW / 1000, preflt = ~0L, validlt = ~0L;
  1369. char addr[40], preflen[6];
  1370. char *params[3];
  1371. uchar autoflag = 1, onlink = 1;
  1372. uchar prefix[IPaddrlen];
  1373. Iplifc *lifc;
  1374. switch(argc) {
  1375. case 7:
  1376. preflt = atoi(argv[6]);
  1377. /* fall through */
  1378. case 6:
  1379. validlt = atoi(argv[5]);
  1380. /* fall through */
  1381. case 5:
  1382. autoflag = atoi(argv[4]);
  1383. /* fall through */
  1384. case 4:
  1385. onlink = atoi(argv[3]);
  1386. /* fall through */
  1387. case 3:
  1388. plen = atoi(argv[2]);
  1389. /* fall through */
  1390. case 2:
  1391. break;
  1392. default:
  1393. return Ebadarg;
  1394. }
  1395. if (parseip(prefix, argv[1]) != 6 || validlt < preflt || plen < 0 ||
  1396. plen > 64 || islinklocal(prefix))
  1397. return Ebadarg;
  1398. lifc = smalloc(sizeof(Iplifc));
  1399. lifc->onlink = (onlink != 0);
  1400. lifc->autoflag = (autoflag != 0);
  1401. lifc->validlt = validlt;
  1402. lifc->preflt = preflt;
  1403. lifc->origint = origint;
  1404. /* issue "add" ctl msg for v6 link-local addr and prefix len */
  1405. if(!ifc->m->pref2addr)
  1406. return Ebadarg;
  1407. ifc->m->pref2addr(prefix, ifc->mac); /* mac → v6 link-local addr */
  1408. sprint(addr, "%I", prefix);
  1409. sprint(preflen, "/%d", plen);
  1410. params[0] = "add";
  1411. params[1] = addr;
  1412. params[2] = preflen;
  1413. return ipifcadd(ifc, params, 3, 0, lifc);
  1414. }