rip.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 i, n;
  177. OUdphdr *up;
  178. Ripmsg *m;
  179. Rip *r;
  180. Route route;
  181. Bnet *bn, **l;
  182. int dobroadcast;
  183. char buf[2*1024];
  184. long diff;
  185. char *p;
  186. static long btime;
  187. uchar raddr[Pasize];
  188. setnetmtpt(netdir, sizeof(netdir), nil);
  189. dobroadcast = 0;
  190. ARGBEGIN{
  191. case 'b':
  192. dobroadcast++;
  193. break;
  194. case 'd':
  195. debug++;
  196. break;
  197. case 'n':
  198. readonly++;
  199. break;
  200. case 'x':
  201. p = ARGF();
  202. if(p == nil)
  203. usage();
  204. setnetmtpt(netdir, sizeof(netdir), p);
  205. break;
  206. default:
  207. usage();
  208. }ARGEND
  209. /* specific broadcast nets */
  210. l = &bnets;
  211. while(argc > 0){
  212. bn = (Bnet*)malloc(sizeof(Bnet));
  213. if(bn == 0)
  214. fatal(1, "out of mem");
  215. v4parseip(bn->addr, *argv);
  216. *l = bn;
  217. l = &bn->next;
  218. argc--;
  219. argv++;
  220. dobroadcast++;
  221. }
  222. /* command returns */
  223. if(!debug)
  224. switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT)) {
  225. case -1:
  226. fatal(1, "fork");
  227. case 0:
  228. break;
  229. default:
  230. exits(0);
  231. }
  232. fmtinstall('E', eipfmt);
  233. fmtinstall('V', eipfmt);
  234. snprint(routefile, sizeof(routefile), "%s/iproute", netdir);
  235. snprint(buf, sizeof(buf), "%s/iproute", netdir);
  236. now = time(0);
  237. readifcs();
  238. readroutes();
  239. notify(ding);
  240. ripfd = openport();
  241. for(;;) {
  242. diff = btime - time(0);
  243. if(diff <= 0){
  244. if(dobroadcast)
  245. broadcast();
  246. timeoutroutes();
  247. btime = time(0) + 2*60;
  248. diff = 2*60;
  249. }
  250. alarm(diff*1000);
  251. n = read(ripfd, buf, sizeof(buf));
  252. alarm(0);
  253. if(n <= 0)
  254. continue;
  255. n = (n-OUdphdrsize-4)/sizeof(Rip);
  256. if(n <= 0)
  257. continue;
  258. up = (OUdphdr*)buf;
  259. m = (Ripmsg*)(buf+OUdphdrsize);
  260. if(m->type != Response || m->vers != Version)
  261. continue;
  262. v6tov4(raddr, up->raddr);
  263. /* ignore our own messages */
  264. for(i = 0; i < ialloc.nifc; i++)
  265. if(equivip(ialloc.ifc[i].addr, raddr))
  266. continue;
  267. now = time(0);
  268. for(r = m->rip; r < &m->rip[n]; r++){
  269. memmove(route.gate, raddr, Pasize);
  270. memmove(route.mask, getmask(r->addr), Pasize);
  271. v4maskip(r->addr, route.mask, route.dest);
  272. route.metric = nhgetl(r->metric) + 1;
  273. if(route.metric < 1)
  274. continue;
  275. considerroute(&route);
  276. }
  277. }
  278. exits(0);
  279. }
  280. int
  281. openport(void)
  282. {
  283. char data[128];
  284. char devdir[40];
  285. int ripctl, rip;
  286. snprint(data, sizeof(data), "%s/udp!*!rip", netdir);
  287. ripctl = announce(data, devdir);
  288. if(ripctl < 0)
  289. fatal(1, "can't announce");
  290. if(fprint(ripctl, "headers") < 0)
  291. fatal(1, "can't set header mode");
  292. fprint(ripctl, "oldheaders");
  293. sprint(data, "%s/data", devdir);
  294. rip = open(data, ORDWR);
  295. if(rip < 0)
  296. fatal(1, "open udp data");
  297. return rip;
  298. }
  299. Ipifc *ifcs;
  300. void
  301. readifcs(void)
  302. {
  303. Ipifc *ifc;
  304. Iplifc *lifc;
  305. Ifc *ip;
  306. Bnet *bn;
  307. Route route;
  308. int i;
  309. ifcs = readipifc(netdir, ifcs, -1);
  310. i = 0;
  311. for(ifc = ifcs; ifc != nil; ifc = ifc->next){
  312. for(lifc = ifc->lifc; lifc != nil && i < Nifc; lifc = lifc->next){
  313. // ignore any interfaces that aren't v4
  314. if(memcmp(lifc->ip, v4prefix, IPaddrlen-IPv4addrlen) != 0)
  315. continue;
  316. ip = &ialloc.ifc[i++];
  317. v6tov4(ip->addr, lifc->ip);
  318. v6tov4mask(ip->mask, lifc->mask);
  319. v6tov4(ip->net, lifc->net);
  320. ip->cmask = v4defmask(ip->net);
  321. v4maskip(ip->net, ip->cmask, ip->cnet);
  322. ip->bcast = 0;
  323. /* add as a route */
  324. memmove(route.mask, ip->mask, Pasize);
  325. memmove(route.dest, ip->net, Pasize);
  326. memset(route.gate, 0, Pasize);
  327. route.metric = 0;
  328. considerroute(&route);
  329. /* mark as broadcast */
  330. if(bnets == 0)
  331. ip->bcast = 1;
  332. else for(bn = bnets; bn; bn = bn->next)
  333. if(memcmp(bn->addr, ip->net, Pasize) == 0){
  334. ip->bcast = 1;
  335. break;
  336. }
  337. }
  338. }
  339. ialloc.nifc = i;
  340. }
  341. void
  342. readroutes(void)
  343. {
  344. int n;
  345. char *p;
  346. Biobuf *b;
  347. char *f[6];
  348. Route route;
  349. b = Bopen(routefile, OREAD);
  350. if(b == 0)
  351. return;
  352. while(p = Brdline(b, '\n')){
  353. p[Blinelen(b)-1] = 0;
  354. n = getfields(p, f, 6, 1, " \t");
  355. if(n < 5)
  356. continue;
  357. v4parseip(route.dest, f[0]);
  358. v4parseipmask(route.mask, f[1]);
  359. v4parseip(route.gate, f[2]);
  360. route.metric = Infinity;
  361. if(equivip(route.dest, ralloc.def.dest)
  362. && equivip(route.mask, ralloc.def.mask))
  363. memmove(ralloc.def.gate, route.gate, Pasize);
  364. else if(!equivip(route.dest, route.gate) && strchr(f[3], 'i') == 0)
  365. considerroute(&route);
  366. }
  367. Bterm(b);
  368. }
  369. /*
  370. * route's hashed by net, not subnet
  371. */
  372. ulong
  373. rhash(uchar *d)
  374. {
  375. ulong h;
  376. uchar net[Pasize];
  377. v4maskip(d, v4defmask(d), net);
  378. h = net[0] + net[1] + net[2];
  379. return h % Nhash;
  380. }
  381. /*
  382. * consider installing a route. Do so only if it is better than what
  383. * we have.
  384. */
  385. void
  386. considerroute(Route *r)
  387. {
  388. ulong h;
  389. Route *hp;
  390. if(debug)
  391. fprint(2, "consider %16V & %16V -> %16V %d\n", r->dest, r->mask, r->gate, r->metric);
  392. r->next = 0;
  393. r->time = now;
  394. r->inuse = 1;
  395. /* don't allow our default route to be highjacked */
  396. if(equivip(r->dest, ralloc.def.dest) || equivip(r->mask, ralloc.def.mask))
  397. return;
  398. h = rhash(r->dest);
  399. for(hp = ralloc.hash[h]; hp; hp = hp->next){
  400. if(equivip(hp->dest, r->dest)){
  401. /*
  402. * found a match, replace if better (or much newer)
  403. */
  404. if(r->metric < hp->metric || now-hp->time > 5*60){
  405. removeroute(hp);
  406. memmove(hp->mask, r->mask, Pasize);
  407. memmove(hp->gate, r->gate, Pasize);
  408. hp->metric = r->metric;
  409. installroute(hp);
  410. }
  411. if(equivip(hp->gate, r->gate))
  412. hp->time = now;
  413. return;
  414. }
  415. }
  416. /*
  417. * no match, look for space
  418. */
  419. for(hp = ralloc.route; hp < &ralloc.route[Nroute]; hp++)
  420. if(hp->inuse == 0)
  421. break;
  422. if(hp == 0)
  423. fatal(0, "no more routes");
  424. memmove(hp, r, sizeof(Route));
  425. hp->next = ralloc.hash[h];
  426. ralloc.hash[h] = hp;
  427. installroute(hp);
  428. }
  429. void
  430. removeroute(Route *r)
  431. {
  432. int fd;
  433. fd = open(routefile, ORDWR);
  434. if(fd < 0){
  435. fprint(2, "can't open oproute\n");
  436. return;
  437. }
  438. if(!readonly)
  439. fprint(fd, "delete %V", r->dest);
  440. if(debug)
  441. fprint(2, "removeroute %V\n", r->dest);
  442. close(fd);
  443. }
  444. /*
  445. * pass a route to the kernel or /ip. Don't bother if it is just the default
  446. * gateway.
  447. */
  448. void
  449. installroute(Route *r)
  450. {
  451. int fd;
  452. ulong h;
  453. Route *hp;
  454. uchar net[Pasize];
  455. /*
  456. * don't install routes whose gateway is 00000000
  457. */
  458. if(equivip(r->gate, ralloc.def.dest))
  459. return;
  460. fd = open(routefile, ORDWR);
  461. if(fd < 0){
  462. fprint(2, "can't open oproute\n");
  463. return;
  464. }
  465. h = rhash(r->dest);
  466. /*
  467. * if the gateway is the same as the default gateway
  468. * we may be able to avoid a entry in the kernel
  469. */
  470. if(equivip(r->gate, ralloc.def.gate)){
  471. /*
  472. * look for a less specific match
  473. */
  474. for(hp = ralloc.hash[h]; hp; hp = hp->next){
  475. v4maskip(hp->mask, r->dest, net);
  476. if(equivip(net, hp->dest) && !equivip(hp->gate, ralloc.def.gate))
  477. break;
  478. }
  479. /*
  480. * if no less specific match, just use the default
  481. */
  482. if(hp == 0){
  483. if(!readonly)
  484. fprint(fd, "delete %V", r->dest);
  485. if(debug)
  486. fprint(2, "delete %V\n", r->dest);
  487. close(fd);
  488. return;
  489. }
  490. }
  491. if(!readonly)
  492. fprint(fd, "add %V %V %V", r->dest, r->mask, r->gate);
  493. if(debug)
  494. fprint(2, "add %V & %V -> %V\n", r->dest, r->mask, r->gate);
  495. close(fd);
  496. }
  497. /*
  498. * return true of dest is on net
  499. */
  500. int
  501. onnet(uchar *dest, uchar *net, uchar *netmask)
  502. {
  503. uchar dnet[Pasize];
  504. v4maskip(dest, netmask, dnet);
  505. return equivip(dnet, net);
  506. }
  507. /*
  508. * figure out what mask to use, if we have a direct connected network
  509. * with the same class net use its subnet mask.
  510. */
  511. uchar*
  512. getmask(uchar *dest)
  513. {
  514. int i;
  515. Ifc *ip;
  516. ulong mask, nmask;
  517. uchar *m;
  518. m = 0;
  519. mask = 0xffffffff;
  520. for(i = 0; i < ialloc.nifc; i++){
  521. ip = &ialloc.ifc[i];
  522. if(onnet(dest, ip->cnet, ip->cmask)){
  523. nmask = nhgetl(ip->mask);
  524. if(nmask < mask){
  525. mask = nmask;
  526. m = ip->mask;
  527. }
  528. }
  529. }
  530. if(m == 0)
  531. m = v4defmask(dest);
  532. return m;
  533. }
  534. /*
  535. * broadcast routes onto all networks
  536. */
  537. void
  538. sendto(Ifc *ip)
  539. {
  540. int h, n;
  541. Route *r;
  542. uchar mbuf[OUdphdrsize+512];
  543. Ripmsg *m;
  544. OUdphdr *u;
  545. uchar raddr[Pasize];
  546. u = (OUdphdr*)mbuf;
  547. for(n = 0; n < Pasize; n++)
  548. raddr[n] = ip->net[n] | ~(ip->mask[n]);
  549. v4tov6(u->raddr, raddr);
  550. hnputs(u->rport, 520);
  551. m = (Ripmsg*)(mbuf+OUdphdrsize);
  552. m->type = Response;
  553. m->vers = Version;
  554. if(debug)
  555. fprint(2, "to %V\n", u->raddr);
  556. n = 0;
  557. for(h = 0; h < Nhash; h++){
  558. for(r = ralloc.hash[h]; r; r = r->next){
  559. /*
  560. * don't send any route back to the net
  561. * it came from
  562. */
  563. if(onnet(r->gate, ip->net, ip->mask))
  564. continue;
  565. /*
  566. * don't tell a network about itself
  567. */
  568. if(equivip(r->dest, ip->net))
  569. continue;
  570. /*
  571. * don't tell nets about other net's subnets
  572. */
  573. if(!equivip(r->mask, v4defmask(r->dest))
  574. && !equivip(ip->cmask, v4defmask(r->dest)))
  575. continue;
  576. memset(&m->rip[n], 0, sizeof(m->rip[n]));
  577. memmove(m->rip[n].addr, r->dest, Pasize);
  578. if(r->metric < 1)
  579. hnputl(m->rip[n].metric, 1);
  580. else
  581. hnputl(m->rip[n].metric, r->metric);
  582. hnputs(m->rip[n].family, AF_INET);
  583. if(debug)
  584. fprint(2, " %16V & %16V -> %16V %2d\n", r->dest, r->mask, r->gate, r->metric);
  585. if(++n == Maxroutes && !readonly){
  586. write(ripfd, mbuf, OUdphdrsize+4+n*20);
  587. n = 0;
  588. }
  589. }
  590. }
  591. if(n && !readonly)
  592. write(ripfd, mbuf, OUdphdrsize+4+n*20);
  593. }
  594. void
  595. broadcast(void)
  596. {
  597. int i;
  598. readifcs();
  599. for(i = 0; i < ialloc.nifc; i++){
  600. if(ialloc.ifc[i].bcast)
  601. sendto(&ialloc.ifc[i]);
  602. }
  603. }
  604. /*
  605. * timeout any routes that haven't been refreshed and aren't wired
  606. */
  607. void
  608. timeoutroutes(void)
  609. {
  610. int h;
  611. long now;
  612. Route *r, **l;
  613. now = time(0);
  614. for(h = 0; h < Nhash; h++){
  615. l = &ralloc.hash[h];
  616. for(r = *l; r; r = *l){
  617. if(r->metric < Infinity && now - r->time > 10*60){
  618. removeroute(r);
  619. r->inuse = 0;
  620. *l = r->next;
  621. continue;
  622. }
  623. l = &r->next;
  624. }
  625. }
  626. }