rip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ip.h>
  13. enum
  14. {
  15. Version= 1,
  16. Pasize= 4,
  17. /*
  18. * definitions that are innately tied to BSD
  19. */
  20. AF_INET= 2,
  21. AF_UNSPEC= 0,
  22. /*
  23. * Packet types.
  24. */
  25. Request= 1,
  26. Response= 2,
  27. Traceon= 3,
  28. Traceoff= 4,
  29. Infinity= 16, /* infinite hop count */
  30. Maxpacket= 488, /* largest packet body */
  31. };
  32. /*
  33. * network info
  34. */
  35. typedef struct Rip Rip;
  36. struct Rip
  37. {
  38. uint8_t family[2];
  39. uint8_t port[2];
  40. uint8_t addr[Pasize];
  41. uint8_t pad[8];
  42. uint8_t metric[4];
  43. };
  44. typedef struct Ripmsg Ripmsg;
  45. struct Ripmsg
  46. {
  47. uint8_t type;
  48. uint8_t vers;
  49. uint8_t pad[2];
  50. Rip rip[1]; /* the rest of the packet consists of routes */
  51. };
  52. enum
  53. {
  54. Maxroutes= (Maxpacket-4)/sizeof(Ripmsg),
  55. };
  56. /*
  57. * internal route info
  58. */
  59. enum
  60. {
  61. Nroute= 2048, /* this has to be smaller than what /ip has */
  62. Nhash= 256, /* routing hash buckets */
  63. Nifc= 16,
  64. };
  65. typedef struct Route Route;
  66. struct Route
  67. {
  68. Route *next;
  69. uint8_t dest[Pasize];
  70. uint8_t mask[Pasize];
  71. uint8_t gate[Pasize];
  72. int metric;
  73. int inuse;
  74. int32_t time;
  75. };
  76. struct {
  77. Route route[Nroute];
  78. Route *hash[Nhash];
  79. int nroute;
  80. Route def; /* default route (immutable by us) */
  81. } ralloc;
  82. typedef struct Ifc Ifc;
  83. struct Ifc
  84. {
  85. int bcast;
  86. uint8_t addr[Pasize]; /* my address */
  87. uint8_t mask[Pasize]; /* subnet mask */
  88. uint8_t net[Pasize]; /* subnet */
  89. uint8_t *cmask; /* class mask */
  90. uint8_t cnet[Pasize]; /* class net */
  91. };
  92. struct {
  93. Ifc ifc[Nifc];
  94. int nifc;
  95. } ialloc;
  96. /*
  97. * specific networks to broadcast on
  98. */
  99. typedef struct Bnet Bnet;
  100. struct Bnet
  101. {
  102. Bnet *next;
  103. uint8_t addr[Pasize];
  104. };
  105. Bnet *bnets;
  106. int ripfd;
  107. int32_t now;
  108. int debug;
  109. int readonly;
  110. char routefile[256];
  111. char netdir[256];
  112. int openport(void);
  113. void readroutes(void);
  114. void readifcs(void);
  115. void considerroute(Route*);
  116. void installroute(Route*);
  117. void removeroute(Route*);
  118. uint8_t *getmask(uint8_t*);
  119. void broadcast(void);
  120. void timeoutroutes(void);
  121. void
  122. fatal(int syserr, char *fmt, ...)
  123. {
  124. char buf[ERRMAX], sysbuf[ERRMAX];
  125. va_list arg;
  126. va_start(arg, fmt);
  127. vseprint(buf, buf+sizeof(buf), fmt, arg);
  128. va_end(arg);
  129. if(syserr) {
  130. errstr(sysbuf, sizeof sysbuf);
  131. fprint(2, "routed: %s: %s\n", buf, sysbuf);
  132. }
  133. else
  134. fprint(2, "routed: %s\n", buf);
  135. exits(buf);
  136. }
  137. uint32_t
  138. v4parseipmask(uint8_t *ip, char *p)
  139. {
  140. uint32_t x;
  141. uint8_t v6ip[IPaddrlen];
  142. x = parseipmask(v6ip, p);
  143. memmove(ip, v6ip+IPv4off, 4);
  144. return x;
  145. }
  146. uint8_t*
  147. v4defmask(uint8_t *ip)
  148. {
  149. uint8_t v6ip[IPaddrlen];
  150. v4tov6(v6ip, ip);
  151. ip = defmask(v6ip);
  152. return ip+IPv4off;
  153. }
  154. void
  155. v4maskip(uint8_t *from, uint8_t *mask, uint8_t *to)
  156. {
  157. int i;
  158. for(i = 0; i < Pasize; i++)
  159. *to++ = *from++ & *mask++;
  160. }
  161. void
  162. v6tov4mask(uint8_t *v4, uint8_t *v6)
  163. {
  164. memmove(v4, v6+IPv4off, 4);
  165. }
  166. #define equivip(a, b) (memcmp((a), (b), Pasize) == 0)
  167. void
  168. ding(void *u, char *msg)
  169. {
  170. USED(u);
  171. if(strstr(msg, "alarm"))
  172. noted(NCONT);
  173. noted(NDFLT);
  174. }
  175. void
  176. usage(void)
  177. {
  178. fprint(2, "usage: %s [-bnd] [-x netmtpt]\n", argv0);
  179. exits("usage");
  180. }
  181. void
  182. main(int argc, char *argv[])
  183. {
  184. int dobroadcast, i, n;
  185. long diff;
  186. char *p;
  187. char buf[2*1024];
  188. uint8_t raddr[Pasize];
  189. Bnet *bn, **l;
  190. Udphdr *up;
  191. Rip *r;
  192. Ripmsg *m;
  193. Route route;
  194. static long btime;
  195. setnetmtpt(netdir, sizeof(netdir), nil);
  196. dobroadcast = 0;
  197. ARGBEGIN{
  198. case 'b':
  199. dobroadcast++;
  200. break;
  201. case 'd':
  202. debug++;
  203. break;
  204. case 'n':
  205. readonly++;
  206. break;
  207. case 'x':
  208. p = ARGF();
  209. if(p == nil)
  210. usage();
  211. setnetmtpt(netdir, sizeof(netdir), p);
  212. break;
  213. default:
  214. usage();
  215. }ARGEND
  216. /* specific broadcast nets */
  217. l = &bnets;
  218. while(argc > 0){
  219. bn = (Bnet*)malloc(sizeof(Bnet));
  220. if(bn == 0)
  221. fatal(1, "out of mem");
  222. v4parseip(bn->addr, *argv);
  223. *l = bn;
  224. l = &bn->next;
  225. argc--;
  226. argv++;
  227. dobroadcast++;
  228. }
  229. /* command returns */
  230. if(!debug)
  231. switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT)) {
  232. case -1:
  233. fatal(1, "fork");
  234. case 0:
  235. break;
  236. default:
  237. exits(0);
  238. }
  239. fmtinstall('E', eipfmt);
  240. fmtinstall('V', eipfmt);
  241. snprint(routefile, sizeof(routefile), "%s/iproute", netdir);
  242. snprint(buf, sizeof(buf), "%s/iproute", netdir);
  243. now = time(0);
  244. readifcs();
  245. readroutes();
  246. notify(ding);
  247. ripfd = openport();
  248. for(;;) {
  249. diff = btime - time(0);
  250. if(diff <= 0){
  251. if(dobroadcast)
  252. broadcast();
  253. timeoutroutes();
  254. btime = time(0) + 2*60;
  255. diff = 2*60;
  256. }
  257. alarm(diff*1000);
  258. n = read(ripfd, buf, sizeof(buf));
  259. alarm(0);
  260. if(n <= 0)
  261. continue;
  262. n = (n - Udphdrsize - 4) / sizeof(Rip);
  263. if(n <= 0)
  264. continue;
  265. up = (Udphdr*)buf;
  266. m = (Ripmsg*)(buf+Udphdrsize);
  267. if(m->type != Response || m->vers != Version)
  268. continue;
  269. v6tov4(raddr, up->raddr);
  270. /* ignore our own messages */
  271. for(i = 0; i < ialloc.nifc; i++)
  272. if(equivip(ialloc.ifc[i].addr, raddr))
  273. continue;
  274. now = time(0);
  275. for(r = m->rip; r < &m->rip[n]; r++){
  276. memmove(route.gate, raddr, Pasize);
  277. memmove(route.mask, getmask(r->addr), Pasize);
  278. v4maskip(r->addr, route.mask, route.dest);
  279. route.metric = nhgetl(r->metric) + 1;
  280. if(route.metric < 1)
  281. continue;
  282. considerroute(&route);
  283. }
  284. }
  285. /* not reached */
  286. }
  287. int
  288. openport(void)
  289. {
  290. int ripctl, rip;
  291. char data[128], devdir[40];
  292. snprint(data, sizeof(data), "%s/udp!*!rip", netdir);
  293. ripctl = announce(data, devdir);
  294. if(ripctl < 0)
  295. fatal(1, "can't announce");
  296. if(fprint(ripctl, "headers") < 0)
  297. fatal(1, "can't set header mode");
  298. sprint(data, "%s/data", devdir);
  299. rip = open(data, ORDWR);
  300. if(rip < 0)
  301. fatal(1, "open udp data");
  302. return rip;
  303. }
  304. Ipifc *ifcs;
  305. void
  306. readifcs(void)
  307. {
  308. Ipifc *ifc;
  309. Iplifc *lifc;
  310. Ifc *ip;
  311. Bnet *bn;
  312. Route route;
  313. int i;
  314. ifcs = readipifc(netdir, ifcs, -1);
  315. i = 0;
  316. for(ifc = ifcs; ifc != nil; ifc = ifc->next){
  317. for(lifc = ifc->lifc; lifc != nil && i < Nifc; lifc = lifc->next){
  318. // ignore any interfaces that aren't v4
  319. if(memcmp(lifc->ip, v4prefix, IPaddrlen-IPv4addrlen) != 0)
  320. continue;
  321. ip = &ialloc.ifc[i++];
  322. v6tov4(ip->addr, lifc->ip);
  323. v6tov4mask(ip->mask, lifc->mask);
  324. v6tov4(ip->net, lifc->net);
  325. ip->cmask = v4defmask(ip->net);
  326. v4maskip(ip->net, ip->cmask, ip->cnet);
  327. ip->bcast = 0;
  328. /* add as a route */
  329. memmove(route.mask, ip->mask, Pasize);
  330. memmove(route.dest, ip->net, Pasize);
  331. memset(route.gate, 0, Pasize);
  332. route.metric = 0;
  333. considerroute(&route);
  334. /* mark as broadcast */
  335. if(bnets == 0)
  336. ip->bcast = 1;
  337. else for(bn = bnets; bn; bn = bn->next)
  338. if(memcmp(bn->addr, ip->net, Pasize) == 0){
  339. ip->bcast = 1;
  340. break;
  341. }
  342. }
  343. }
  344. ialloc.nifc = i;
  345. }
  346. void
  347. readroutes(void)
  348. {
  349. int n;
  350. char *p;
  351. Biobuf *b;
  352. char *f[6];
  353. Route route;
  354. b = Bopen(routefile, OREAD);
  355. if(b == 0)
  356. return;
  357. while(p = Brdline(b, '\n')){
  358. p[Blinelen(b)-1] = 0;
  359. n = getfields(p, f, 6, 1, " \t");
  360. if(n < 5)
  361. continue;
  362. v4parseip(route.dest, f[0]);
  363. v4parseipmask(route.mask, f[1]);
  364. v4parseip(route.gate, f[2]);
  365. route.metric = Infinity;
  366. if(equivip(route.dest, ralloc.def.dest)
  367. && equivip(route.mask, ralloc.def.mask))
  368. memmove(ralloc.def.gate, route.gate, Pasize);
  369. else if(!equivip(route.dest, route.gate) && strchr(f[3], 'i') == 0)
  370. considerroute(&route);
  371. }
  372. Bterm(b);
  373. }
  374. /*
  375. * route's hashed by net, not subnet
  376. */
  377. uint32_t
  378. rhash(uint8_t *d)
  379. {
  380. uint32_t h;
  381. uint8_t net[Pasize];
  382. v4maskip(d, v4defmask(d), net);
  383. h = net[0] + net[1] + net[2];
  384. return h % Nhash;
  385. }
  386. /*
  387. * consider installing a route. Do so only if it is better than what
  388. * we have.
  389. */
  390. void
  391. considerroute(Route *r)
  392. {
  393. uint32_t h;
  394. Route *hp;
  395. if(debug)
  396. fprint(2, "consider %16V & %16V -> %16V %d\n", r->dest, r->mask, r->gate, r->metric);
  397. r->next = 0;
  398. r->time = now;
  399. r->inuse = 1;
  400. /* don't allow our default route to be highjacked */
  401. if(equivip(r->dest, ralloc.def.dest) || equivip(r->mask, ralloc.def.mask))
  402. return;
  403. h = rhash(r->dest);
  404. for(hp = ralloc.hash[h]; hp; hp = hp->next){
  405. if(equivip(hp->dest, r->dest)){
  406. /*
  407. * found a match, replace if better (or much newer)
  408. */
  409. if(r->metric < hp->metric || now-hp->time > 5*60){
  410. removeroute(hp);
  411. memmove(hp->mask, r->mask, Pasize);
  412. memmove(hp->gate, r->gate, Pasize);
  413. hp->metric = r->metric;
  414. installroute(hp);
  415. }
  416. if(equivip(hp->gate, r->gate))
  417. hp->time = now;
  418. return;
  419. }
  420. }
  421. /*
  422. * no match, look for space
  423. */
  424. for(hp = ralloc.route; hp < &ralloc.route[Nroute]; hp++)
  425. if(hp->inuse == 0)
  426. break;
  427. if(hp == 0)
  428. fatal(0, "no more routes");
  429. memmove(hp, r, sizeof(Route));
  430. hp->next = ralloc.hash[h];
  431. ralloc.hash[h] = hp;
  432. installroute(hp);
  433. }
  434. void
  435. removeroute(Route *r)
  436. {
  437. int fd;
  438. fd = open(routefile, ORDWR);
  439. if(fd < 0){
  440. fprint(2, "can't open oproute\n");
  441. return;
  442. }
  443. if(!readonly)
  444. fprint(fd, "delete %V", r->dest);
  445. if(debug)
  446. fprint(2, "removeroute %V\n", r->dest);
  447. close(fd);
  448. }
  449. /*
  450. * pass a route to the kernel or /ip. Don't bother if it is just the default
  451. * gateway.
  452. */
  453. void
  454. installroute(Route *r)
  455. {
  456. int fd;
  457. uint32_t h;
  458. Route *hp;
  459. uint8_t net[Pasize];
  460. /*
  461. * don't install routes whose gateway is 00000000
  462. */
  463. if(equivip(r->gate, ralloc.def.dest))
  464. return;
  465. fd = open(routefile, ORDWR);
  466. if(fd < 0){
  467. fprint(2, "can't open oproute\n");
  468. return;
  469. }
  470. h = rhash(r->dest);
  471. /*
  472. * if the gateway is the same as the default gateway
  473. * we may be able to avoid a entry in the kernel
  474. */
  475. if(equivip(r->gate, ralloc.def.gate)){
  476. /*
  477. * look for a less specific match
  478. */
  479. for(hp = ralloc.hash[h]; hp; hp = hp->next){
  480. v4maskip(hp->mask, r->dest, net);
  481. if(equivip(net, hp->dest) && !equivip(hp->gate, ralloc.def.gate))
  482. break;
  483. }
  484. /*
  485. * if no less specific match, just use the default
  486. */
  487. if(hp == 0){
  488. if(!readonly)
  489. fprint(fd, "delete %V", r->dest);
  490. if(debug)
  491. fprint(2, "delete %V\n", r->dest);
  492. close(fd);
  493. return;
  494. }
  495. }
  496. if(!readonly)
  497. fprint(fd, "add %V %V %V", r->dest, r->mask, r->gate);
  498. if(debug)
  499. fprint(2, "add %V & %V -> %V\n", r->dest, r->mask, r->gate);
  500. close(fd);
  501. }
  502. /*
  503. * return true of dest is on net
  504. */
  505. int
  506. onnet(uint8_t *dest, uint8_t *net, uint8_t *netmask)
  507. {
  508. uint8_t dnet[Pasize];
  509. v4maskip(dest, netmask, dnet);
  510. return equivip(dnet, net);
  511. }
  512. /*
  513. * figure out what mask to use, if we have a direct connected network
  514. * with the same class net use its subnet mask.
  515. */
  516. uint8_t*
  517. getmask(uint8_t *dest)
  518. {
  519. int i;
  520. Ifc *ip;
  521. uint32_t mask, nmask;
  522. uint8_t *m;
  523. m = 0;
  524. mask = 0xffffffff;
  525. for(i = 0; i < ialloc.nifc; i++){
  526. ip = &ialloc.ifc[i];
  527. if(onnet(dest, ip->cnet, ip->cmask)){
  528. nmask = nhgetl(ip->mask);
  529. if(nmask < mask){
  530. mask = nmask;
  531. m = ip->mask;
  532. }
  533. }
  534. }
  535. if(m == 0)
  536. m = v4defmask(dest);
  537. return m;
  538. }
  539. /*
  540. * broadcast routes onto all networks
  541. */
  542. void
  543. sendto(Ifc *ip)
  544. {
  545. int h, n;
  546. uint8_t raddr[Pasize], mbuf[Udphdrsize+512];
  547. Ripmsg *m;
  548. Route *r;
  549. Udphdr *u;
  550. u = (Udphdr*)mbuf;
  551. for(n = 0; n < Pasize; n++)
  552. raddr[n] = ip->net[n] | ~(ip->mask[n]);
  553. v4tov6(u->raddr, raddr);
  554. hnputs(u->rport, 520);
  555. m = (Ripmsg*)(mbuf+Udphdrsize);
  556. m->type = Response;
  557. m->vers = Version;
  558. if(debug)
  559. fprint(2, "to %V\n", u->raddr);
  560. n = 0;
  561. for(h = 0; h < Nhash; h++){
  562. for(r = ralloc.hash[h]; r; r = r->next){
  563. /*
  564. * don't send any route back to the net
  565. * it came from
  566. */
  567. if(onnet(r->gate, ip->net, ip->mask))
  568. continue;
  569. /*
  570. * don't tell a network about itself
  571. */
  572. if(equivip(r->dest, ip->net))
  573. continue;
  574. /*
  575. * don't tell nets about other net's subnets
  576. */
  577. if(!equivip(r->mask, v4defmask(r->dest))
  578. && !equivip(ip->cmask, v4defmask(r->dest)))
  579. continue;
  580. memset(&m->rip[n], 0, sizeof(m->rip[n]));
  581. memmove(m->rip[n].addr, r->dest, Pasize);
  582. if(r->metric < 1)
  583. hnputl(m->rip[n].metric, 1);
  584. else
  585. hnputl(m->rip[n].metric, r->metric);
  586. hnputs(m->rip[n].family, AF_INET);
  587. if(debug)
  588. fprint(2, " %16V & %16V -> %16V %2d\n",
  589. r->dest, r->mask, r->gate, r->metric);
  590. if(++n == Maxroutes && !readonly){
  591. write(ripfd, mbuf, Udphdrsize + 4 + n*20);
  592. n = 0;
  593. }
  594. }
  595. }
  596. if(n && !readonly)
  597. write(ripfd, mbuf, Udphdrsize+4+n*20);
  598. }
  599. void
  600. broadcast(void)
  601. {
  602. int i;
  603. readifcs();
  604. for(i = 0; i < ialloc.nifc; i++){
  605. if(ialloc.ifc[i].bcast)
  606. sendto(&ialloc.ifc[i]);
  607. }
  608. }
  609. /*
  610. * timeout any routes that haven't been refreshed and aren't wired
  611. */
  612. void
  613. timeoutroutes(void)
  614. {
  615. int h;
  616. int32_t now;
  617. Route *r, **l;
  618. now = time(0);
  619. for(h = 0; h < Nhash; h++){
  620. l = &ralloc.hash[h];
  621. for(r = *l; r; r = *l){
  622. if(r->metric < Infinity && now - r->time > 10*60){
  623. removeroute(r);
  624. r->inuse = 0;
  625. *l = r->next;
  626. continue;
  627. }
  628. l = &r->next;
  629. }
  630. }
  631. }