rip.c 12 KB

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