iproute.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "all.h"
  2. #include "../ip/ip.h"
  3. #define DEBUG if(cons.flags&ralloc.flag)print
  4. enum
  5. {
  6. Version= 1,
  7. /*
  8. * definitions that are innately tied to BSD
  9. */
  10. AF_INET= 2,
  11. AF_UNSPEC= 0,
  12. /*
  13. * Packet types.
  14. */
  15. Request= 1,
  16. Response= 2,
  17. Traceon= 3,
  18. Traceoff= 4,
  19. Infinity= 16, /* infinite hop count */
  20. Maxpacket= 488, /* largest packet body */
  21. };
  22. /*
  23. * network info
  24. */
  25. typedef struct Rip Rip;
  26. struct Rip
  27. {
  28. uchar family[2];
  29. uchar port[2];
  30. uchar addr[Pasize];
  31. uchar pad[8];
  32. uchar metric[4];
  33. };
  34. typedef struct Ripmsg Ripmsg;
  35. struct Ripmsg
  36. {
  37. uchar type;
  38. uchar vers;
  39. uchar pad[2];
  40. Rip rip[1]; /* the rest of the packet consists of routes */
  41. };
  42. enum
  43. {
  44. Maxroutes= (Maxpacket-4)/sizeof(Ripmsg),
  45. };
  46. /*
  47. * internal route info
  48. */
  49. enum
  50. {
  51. Nroute = 2*1024,
  52. Nhash = 256, /* routing hash buckets */
  53. Nifc = 16,
  54. };
  55. typedef struct Route Route;
  56. struct Route
  57. {
  58. Route *next;
  59. uchar dest[Pasize];
  60. uchar mask[Pasize];
  61. uchar gate[Pasize];
  62. int metric;
  63. int inuse;
  64. long time;
  65. };
  66. struct
  67. {
  68. Lock;
  69. Route route[Nroute];
  70. Route *hash[Nhash];
  71. int nroute;
  72. ulong flag;
  73. int dorip;
  74. } ralloc;
  75. uchar classmask[4][4] =
  76. {
  77. 0xff, 0x00, 0x00, 0x00,
  78. 0xff, 0x00, 0x00, 0x00,
  79. 0xff, 0xff, 0x00, 0x00,
  80. 0xff, 0xff, 0xff, 0x00,
  81. };
  82. #define CLASS(p) ((*(uchar*)(p))>>6)
  83. static void considerroute(Route*);
  84. static void deleteroute(Route*);
  85. static void printroute(Route*);
  86. static void printroutes(void);
  87. static void installroute(Route*);
  88. static void getmask(uchar*, uchar*);
  89. static void maskip(uchar*, uchar*, uchar*);
  90. static int equivip(uchar*, uchar*);
  91. static void cmd_route(int, char*[]);
  92. static ulong rhash(uchar*);
  93. void
  94. iprouteinit(void)
  95. {
  96. cmd_install("route", "subcommand -- ip routes", cmd_route);
  97. ralloc.flag = flag_install("route", "-- verbose");
  98. if(!conf.ripoff)
  99. ralloc.dorip = 1;
  100. }
  101. static void
  102. cmd_route(int argc, char *argv[])
  103. {
  104. Route r;
  105. if(argc < 2) {
  106. usage:
  107. print("route add dest gate [mask] -- add a route\n");
  108. print("route delete dest -- remote a route\n");
  109. print("route print [dest] -- print routes\n");
  110. print("route ripon -- listen to RIP packets\n");
  111. print("route ripoff -- ignore RIP packets\n");
  112. return;
  113. }
  114. if(strcmp(argv[1], "ripoff") == 0)
  115. ralloc.dorip = 0;
  116. else
  117. if(strcmp(argv[1], "ripon") == 0)
  118. ralloc.dorip = 1;
  119. else
  120. if(strcmp(argv[1], "add") == 0) {
  121. switch(argc){
  122. default:
  123. goto usage;
  124. case 4:
  125. memmove(r.mask, classmask[CLASS(r.dest)], Pasize);
  126. break;
  127. case 5:
  128. if(chartoip(r.mask, argv[4]))
  129. goto usage;
  130. break;
  131. }
  132. if(chartoip(r.dest, argv[2]))
  133. goto usage;
  134. if(chartoip(r.gate, argv[3]))
  135. goto usage;
  136. r.metric = 0; /* rip can't nuke these */
  137. deleteroute(&r);
  138. considerroute(&r);
  139. } else
  140. if(strcmp(argv[1], "delete") == 0) {
  141. if(argc != 3)
  142. goto usage;
  143. if(chartoip(r.dest, argv[2]))
  144. goto usage;
  145. deleteroute(&r);
  146. } else
  147. if(strcmp(argv[1], "print") == 0) {
  148. if(argc == 3) {
  149. if(chartoip(r.dest, argv[2]))
  150. goto usage;
  151. printroute(&r);
  152. } else
  153. printroutes();
  154. }
  155. }
  156. /*
  157. * consider installing a route. Do so only if it is better than what
  158. * we have.
  159. */
  160. static void
  161. considerroute(Route *r)
  162. {
  163. ulong h, i;
  164. ulong m, nm;
  165. Route *hp, **l;
  166. r->next = 0;
  167. r->time = time();
  168. r->inuse = 1;
  169. lock(&ralloc);
  170. h = rhash(r->dest);
  171. for(hp = ralloc.hash[h]; hp; hp = hp->next) {
  172. if(equivip(hp->dest, r->dest) && equivip(hp->mask, r->mask)) {
  173. /*
  174. * found a match, replace if better (or much newer)
  175. */
  176. if(r->metric < hp->metric || time()-hp->time > 10*60) {
  177. memmove(hp->gate, r->gate, Pasize);
  178. hp->metric = r->metric;
  179. DEBUG("route: replacement %I & %I -> %I (%d)\n",
  180. hp->dest, hp->mask, hp->dest, hp->metric);
  181. }
  182. if(equivip(r->gate, hp->gate))
  183. hp->time = time();
  184. goto done;
  185. }
  186. }
  187. /*
  188. * no match, look for space
  189. */
  190. for(hp = ralloc.route; hp < &ralloc.route[Nroute]; hp++)
  191. if(hp->inuse == 0)
  192. break;
  193. if(hp == &ralloc.route[Nroute])
  194. hp = 0;
  195. /*
  196. * look for an old entry
  197. */
  198. for(i = 0; hp == 0 && i < Nhash; i++) {
  199. l = &ralloc.hash[i];
  200. for(hp = *l; hp; hp = *l) {
  201. if(time() - hp->time > 10*60 && hp->metric > 0){
  202. *l = hp->next;
  203. break;
  204. }
  205. l = &hp->next;
  206. }
  207. }
  208. if(hp == 0) {
  209. print("no more routes");
  210. goto done;
  211. }
  212. memmove(hp, r, sizeof(Route));
  213. /*
  214. * insert largest mask first
  215. */
  216. m = nhgetl(hp->mask);
  217. for(l = &ralloc.hash[h]; *l; l = &(*l)->next){
  218. nm = nhgetl((*l)->mask);
  219. if(nm < m)
  220. break;
  221. }
  222. hp->next = *l;
  223. *l = hp;
  224. DEBUG("route: new %I & %I -> %I (%d)\n", hp->dest, hp->mask,
  225. hp->dest, hp->metric);
  226. done:
  227. unlock(&ralloc);
  228. }
  229. static void
  230. deleteroute(Route *r)
  231. {
  232. int h;
  233. Route *hp, **l;
  234. lock(&ralloc);
  235. for(h = 0; h < Nhash; h++) {
  236. l = &ralloc.hash[h];
  237. for(hp = *l; hp; hp = *l){
  238. if(equivip(r->dest, hp->dest)) {
  239. *l = hp->next;
  240. hp->next = 0;
  241. hp->inuse = 0;
  242. break;
  243. }
  244. l = &hp->next;
  245. }
  246. }
  247. unlock(&ralloc);
  248. }
  249. static void
  250. printroutes(void)
  251. {
  252. Ifc *i;
  253. int h;
  254. Route *hp;
  255. uchar mask[Pasize];
  256. lock(&ralloc);
  257. for(h = 0; h < Nhash; h++)
  258. for(hp = ralloc.hash[h]; hp; hp = hp->next)
  259. print("%I & %I -> %I\n", hp->dest, hp->mask, hp->gate);
  260. unlock(&ralloc);
  261. print("\nifc's\n");
  262. for(i = enets; i; i = i->next) {
  263. hnputl(mask, i->mask);
  264. print("addr %I mask %I defgate %I\n", i->ipa, mask, i->netgate);
  265. }
  266. }
  267. static void
  268. printroute(Route *r)
  269. {
  270. int h;
  271. Route *hp;
  272. uchar net[Pasize];
  273. h = rhash(r->dest);
  274. for(hp = ralloc.hash[h]; hp; hp = hp->next){
  275. maskip(r->dest, hp->mask, net);
  276. if(equivip(hp->dest, net)){
  277. print("%I & %I -> %I\n", hp->dest, hp->mask, hp->gate);
  278. return;
  279. }
  280. }
  281. print("default * -> %I\n", enets[0].netgate);
  282. }
  283. void
  284. iproute(uchar *to, uchar *dst, uchar *def)
  285. {
  286. int h;
  287. Route *hp;
  288. uchar net[Pasize];
  289. h = rhash(dst);
  290. for(hp = ralloc.hash[h]; hp; hp = hp->next) {
  291. maskip(dst, hp->mask, net);
  292. if(equivip(hp->dest, net)) {
  293. def = hp->gate;
  294. break;
  295. }
  296. }
  297. memmove(to, def, Pasize);
  298. }
  299. void
  300. riprecv(Msgbuf *mb, Ifc*)
  301. {
  302. int n;
  303. Rip *r;
  304. Ripmsg *m;
  305. Udppkt *uh;
  306. Route route;
  307. if(ralloc.dorip == 0)
  308. goto drop;
  309. uh = (Udppkt*)mb->data;
  310. m = (Ripmsg*)(mb->data + Ensize + Ipsize + Udpsize);
  311. if(m->type != Response || m->vers != Version)
  312. goto drop;
  313. n = nhgets(uh->udplen);
  314. n -= Udpsize;
  315. n = n/sizeof(Rip);
  316. DEBUG("%d routes from %I\n", n, uh->src);
  317. memmove(route.gate, uh->src, Pasize);
  318. for(r = m->rip; r < &m->rip[n]; r++){
  319. getmask(route.mask, r->addr);
  320. maskip(r->addr, route.mask, route.dest);
  321. route.metric = nhgetl(r->metric) + 1;
  322. if(route.metric < 1)
  323. continue;
  324. considerroute(&route);
  325. }
  326. drop:
  327. mbfree(mb);
  328. }
  329. /*
  330. * route's hashed by net, not subnet
  331. */
  332. static ulong
  333. rhash(uchar *d)
  334. {
  335. ulong h;
  336. uchar net[Pasize];
  337. maskip(d, classmask[CLASS(d)], net);
  338. h = net[0] + net[1] + net[2];
  339. return h % Nhash;
  340. }
  341. /*
  342. * figure out what mask to use, if we have a direct connected network
  343. * with the same class net use its subnet mask.
  344. */
  345. static void
  346. getmask(uchar *mask, uchar *dest)
  347. {
  348. Ifc *i;
  349. long ip;
  350. ip = nhgetl(dest);
  351. for(i = enets; i; i = i->next)
  352. if((i->ipaddr & i->cmask) == (ip & i->cmask)) {
  353. hnputl(mask, i->mask);
  354. return;
  355. }
  356. memmove(mask, classmask[CLASS(dest)], Pasize);
  357. }
  358. static void
  359. maskip(uchar *a, uchar *m, uchar *n)
  360. {
  361. int i;
  362. for(i = 0; i < 4; i++)
  363. n[i] = a[i] & m[i];
  364. }
  365. static int
  366. equivip(uchar *a, uchar *b)
  367. {
  368. int i;
  369. for(i = 0; i < 4; i++)
  370. if(a[i] != b[i])
  371. return 0;
  372. return 1;
  373. }
  374. long
  375. ipclassmask(uchar *ip)
  376. {
  377. return nhgetl(classmask[CLASS(ip)]);
  378. }