ipifc.c 33 KB

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