print-dvmrp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /* \summary: Distance Vector Multicast Routing Protocol printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #include "extract.h"
  28. #include "addrtoname.h"
  29. /*
  30. * DVMRP message types and flag values shamelessly stolen from
  31. * mrouted/dvmrp.h.
  32. */
  33. #define DVMRP_PROBE 1 /* for finding neighbors */
  34. #define DVMRP_REPORT 2 /* for reporting some or all routes */
  35. #define DVMRP_ASK_NEIGHBORS 3 /* sent by mapper, asking for a list */
  36. /* of this router's neighbors */
  37. #define DVMRP_NEIGHBORS 4 /* response to such a request */
  38. #define DVMRP_ASK_NEIGHBORS2 5 /* as above, want new format reply */
  39. #define DVMRP_NEIGHBORS2 6
  40. #define DVMRP_PRUNE 7 /* prune message */
  41. #define DVMRP_GRAFT 8 /* graft message */
  42. #define DVMRP_GRAFT_ACK 9 /* graft acknowledgement */
  43. /*
  44. * 'flags' byte values in DVMRP_NEIGHBORS2 reply.
  45. */
  46. #define DVMRP_NF_TUNNEL 0x01 /* neighbors reached via tunnel */
  47. #define DVMRP_NF_SRCRT 0x02 /* tunnel uses IP source routing */
  48. #define DVMRP_NF_DOWN 0x10 /* kernel state of interface */
  49. #define DVMRP_NF_DISABLED 0x20 /* administratively disabled */
  50. #define DVMRP_NF_QUERIER 0x40 /* I am the subnet's querier */
  51. static int print_probe(netdissect_options *, const u_char *, const u_char *, u_int);
  52. static int print_report(netdissect_options *, const u_char *, const u_char *, u_int);
  53. static int print_neighbors(netdissect_options *, const u_char *, const u_char *, u_int);
  54. static int print_neighbors2(netdissect_options *, const u_char *, const u_char *, u_int);
  55. static int print_prune(netdissect_options *, const u_char *);
  56. static int print_graft(netdissect_options *, const u_char *);
  57. static int print_graft_ack(netdissect_options *, const u_char *);
  58. static uint32_t target_level;
  59. void
  60. dvmrp_print(netdissect_options *ndo,
  61. register const u_char *bp, register u_int len)
  62. {
  63. register const u_char *ep;
  64. register u_char type;
  65. ep = (const u_char *)ndo->ndo_snapend;
  66. if (bp >= ep)
  67. return;
  68. ND_TCHECK(bp[1]);
  69. type = bp[1];
  70. /* Skip IGMP header */
  71. bp += 8;
  72. len -= 8;
  73. switch (type) {
  74. case DVMRP_PROBE:
  75. ND_PRINT((ndo, " Probe"));
  76. if (ndo->ndo_vflag) {
  77. if (print_probe(ndo, bp, ep, len) < 0)
  78. goto trunc;
  79. }
  80. break;
  81. case DVMRP_REPORT:
  82. ND_PRINT((ndo, " Report"));
  83. if (ndo->ndo_vflag > 1) {
  84. if (print_report(ndo, bp, ep, len) < 0)
  85. goto trunc;
  86. }
  87. break;
  88. case DVMRP_ASK_NEIGHBORS:
  89. ND_PRINT((ndo, " Ask-neighbors(old)"));
  90. break;
  91. case DVMRP_NEIGHBORS:
  92. ND_PRINT((ndo, " Neighbors(old)"));
  93. if (print_neighbors(ndo, bp, ep, len) < 0)
  94. goto trunc;
  95. break;
  96. case DVMRP_ASK_NEIGHBORS2:
  97. ND_PRINT((ndo, " Ask-neighbors2"));
  98. break;
  99. case DVMRP_NEIGHBORS2:
  100. ND_PRINT((ndo, " Neighbors2"));
  101. /*
  102. * extract version and capabilities from IGMP group
  103. * address field
  104. */
  105. bp -= 4;
  106. ND_TCHECK2(bp[0], 4);
  107. target_level = (bp[0] << 24) | (bp[1] << 16) |
  108. (bp[2] << 8) | bp[3];
  109. bp += 4;
  110. if (print_neighbors2(ndo, bp, ep, len) < 0)
  111. goto trunc;
  112. break;
  113. case DVMRP_PRUNE:
  114. ND_PRINT((ndo, " Prune"));
  115. if (print_prune(ndo, bp) < 0)
  116. goto trunc;
  117. break;
  118. case DVMRP_GRAFT:
  119. ND_PRINT((ndo, " Graft"));
  120. if (print_graft(ndo, bp) < 0)
  121. goto trunc;
  122. break;
  123. case DVMRP_GRAFT_ACK:
  124. ND_PRINT((ndo, " Graft-ACK"));
  125. if (print_graft_ack(ndo, bp) < 0)
  126. goto trunc;
  127. break;
  128. default:
  129. ND_PRINT((ndo, " [type %d]", type));
  130. break;
  131. }
  132. return;
  133. trunc:
  134. ND_PRINT((ndo, "[|dvmrp]"));
  135. return;
  136. }
  137. static int
  138. print_report(netdissect_options *ndo,
  139. register const u_char *bp, register const u_char *ep,
  140. register u_int len)
  141. {
  142. register uint32_t mask, origin;
  143. register int metric, done;
  144. register u_int i, width;
  145. while (len > 0) {
  146. if (len < 3) {
  147. ND_PRINT((ndo, " [|]"));
  148. return (0);
  149. }
  150. ND_TCHECK2(bp[0], 3);
  151. mask = (uint32_t)0xff << 24 | bp[0] << 16 | bp[1] << 8 | bp[2];
  152. width = 1;
  153. if (bp[0])
  154. width = 2;
  155. if (bp[1])
  156. width = 3;
  157. if (bp[2])
  158. width = 4;
  159. ND_PRINT((ndo, "\n\tMask %s", intoa(htonl(mask))));
  160. bp += 3;
  161. len -= 3;
  162. do {
  163. if (bp + width + 1 > ep) {
  164. ND_PRINT((ndo, " [|]"));
  165. return (0);
  166. }
  167. if (len < width + 1) {
  168. ND_PRINT((ndo, "\n\t [Truncated Report]"));
  169. return (0);
  170. }
  171. origin = 0;
  172. for (i = 0; i < width; ++i) {
  173. ND_TCHECK(*bp);
  174. origin = origin << 8 | *bp++;
  175. }
  176. for ( ; i < 4; ++i)
  177. origin <<= 8;
  178. ND_TCHECK(*bp);
  179. metric = *bp++;
  180. done = metric & 0x80;
  181. metric &= 0x7f;
  182. ND_PRINT((ndo, "\n\t %s metric %d", intoa(htonl(origin)),
  183. metric));
  184. len -= width + 1;
  185. } while (!done);
  186. }
  187. return (0);
  188. trunc:
  189. return (-1);
  190. }
  191. static int
  192. print_probe(netdissect_options *ndo,
  193. register const u_char *bp, register const u_char *ep,
  194. register u_int len)
  195. {
  196. register uint32_t genid;
  197. ND_TCHECK2(bp[0], 4);
  198. if ((len < 4) || ((bp + 4) > ep)) {
  199. /* { (ctags) */
  200. ND_PRINT((ndo, " [|}"));
  201. return (0);
  202. }
  203. genid = (bp[0] << 24) | (bp[1] << 16) | (bp[2] << 8) | bp[3];
  204. bp += 4;
  205. len -= 4;
  206. ND_PRINT((ndo, ndo->ndo_vflag > 1 ? "\n\t" : " "));
  207. ND_PRINT((ndo, "genid %u", genid));
  208. if (ndo->ndo_vflag < 2)
  209. return (0);
  210. while ((len > 0) && (bp < ep)) {
  211. ND_TCHECK2(bp[0], 4);
  212. ND_PRINT((ndo, "\n\tneighbor %s", ipaddr_string(ndo, bp)));
  213. bp += 4; len -= 4;
  214. }
  215. return (0);
  216. trunc:
  217. return (-1);
  218. }
  219. static int
  220. print_neighbors(netdissect_options *ndo,
  221. register const u_char *bp, register const u_char *ep,
  222. register u_int len)
  223. {
  224. const u_char *laddr;
  225. register u_char metric;
  226. register u_char thresh;
  227. register int ncount;
  228. while (len > 0 && bp < ep) {
  229. ND_TCHECK2(bp[0], 7);
  230. laddr = bp;
  231. bp += 4;
  232. metric = *bp++;
  233. thresh = *bp++;
  234. ncount = *bp++;
  235. len -= 7;
  236. while (--ncount >= 0) {
  237. ND_TCHECK2(bp[0], 4);
  238. ND_PRINT((ndo, " [%s ->", ipaddr_string(ndo, laddr)));
  239. ND_PRINT((ndo, " %s, (%d/%d)]",
  240. ipaddr_string(ndo, bp), metric, thresh));
  241. bp += 4;
  242. len -= 4;
  243. }
  244. }
  245. return (0);
  246. trunc:
  247. return (-1);
  248. }
  249. static int
  250. print_neighbors2(netdissect_options *ndo,
  251. register const u_char *bp, register const u_char *ep,
  252. register u_int len)
  253. {
  254. const u_char *laddr;
  255. register u_char metric, thresh, flags;
  256. register int ncount;
  257. ND_PRINT((ndo, " (v %d.%d):",
  258. (int)target_level & 0xff,
  259. (int)(target_level >> 8) & 0xff));
  260. while (len > 0 && bp < ep) {
  261. ND_TCHECK2(bp[0], 8);
  262. laddr = bp;
  263. bp += 4;
  264. metric = *bp++;
  265. thresh = *bp++;
  266. flags = *bp++;
  267. ncount = *bp++;
  268. len -= 8;
  269. while (--ncount >= 0 && (len >= 4) && (bp + 4) <= ep) {
  270. ND_PRINT((ndo, " [%s -> ", ipaddr_string(ndo, laddr)));
  271. ND_PRINT((ndo, "%s (%d/%d", ipaddr_string(ndo, bp),
  272. metric, thresh));
  273. if (flags & DVMRP_NF_TUNNEL)
  274. ND_PRINT((ndo, "/tunnel"));
  275. if (flags & DVMRP_NF_SRCRT)
  276. ND_PRINT((ndo, "/srcrt"));
  277. if (flags & DVMRP_NF_QUERIER)
  278. ND_PRINT((ndo, "/querier"));
  279. if (flags & DVMRP_NF_DISABLED)
  280. ND_PRINT((ndo, "/disabled"));
  281. if (flags & DVMRP_NF_DOWN)
  282. ND_PRINT((ndo, "/down"));
  283. ND_PRINT((ndo, ")]"));
  284. bp += 4;
  285. len -= 4;
  286. }
  287. if (ncount != -1) {
  288. ND_PRINT((ndo, " [|]"));
  289. return (0);
  290. }
  291. }
  292. return (0);
  293. trunc:
  294. return (-1);
  295. }
  296. static int
  297. print_prune(netdissect_options *ndo,
  298. register const u_char *bp)
  299. {
  300. ND_TCHECK2(bp[0], 12);
  301. ND_PRINT((ndo, " src %s grp %s", ipaddr_string(ndo, bp), ipaddr_string(ndo, bp + 4)));
  302. bp += 8;
  303. ND_PRINT((ndo, " timer "));
  304. unsigned_relts_print(ndo, EXTRACT_32BITS(bp));
  305. return (0);
  306. trunc:
  307. return (-1);
  308. }
  309. static int
  310. print_graft(netdissect_options *ndo,
  311. register const u_char *bp)
  312. {
  313. ND_TCHECK2(bp[0], 8);
  314. ND_PRINT((ndo, " src %s grp %s", ipaddr_string(ndo, bp), ipaddr_string(ndo, bp + 4)));
  315. return (0);
  316. trunc:
  317. return (-1);
  318. }
  319. static int
  320. print_graft_ack(netdissect_options *ndo,
  321. register const u_char *bp)
  322. {
  323. ND_TCHECK2(bp[0], 8);
  324. ND_PRINT((ndo, " src %s grp %s", ipaddr_string(ndo, bp), ipaddr_string(ndo, bp + 4)));
  325. return (0);
  326. trunc:
  327. return (-1);
  328. }