print-igmp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 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: Internet Group Management Protocol (IGMP) printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #include "addrtoname.h"
  28. #include "extract.h"
  29. #ifndef IN_CLASSD
  30. #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
  31. #endif
  32. static const char tstr[] = "[|igmp]";
  33. /* (following from ipmulti/mrouted/prune.h) */
  34. /*
  35. * The packet format for a traceroute request.
  36. */
  37. struct tr_query {
  38. uint32_t tr_src; /* traceroute source */
  39. uint32_t tr_dst; /* traceroute destination */
  40. uint32_t tr_raddr; /* traceroute response address */
  41. uint32_t tr_rttlqid; /* response ttl and qid */
  42. };
  43. #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
  44. #define TR_GETQID(x) ((x) & 0x00ffffff)
  45. /*
  46. * Traceroute response format. A traceroute response has a tr_query at the
  47. * beginning, followed by one tr_resp for each hop taken.
  48. */
  49. struct tr_resp {
  50. uint32_t tr_qarr; /* query arrival time */
  51. uint32_t tr_inaddr; /* incoming interface address */
  52. uint32_t tr_outaddr; /* outgoing interface address */
  53. uint32_t tr_rmtaddr; /* parent address in source tree */
  54. uint32_t tr_vifin; /* input packet count on interface */
  55. uint32_t tr_vifout; /* output packet count on interface */
  56. uint32_t tr_pktcnt; /* total incoming packets for src-grp */
  57. uint8_t tr_rproto; /* routing proto deployed on router */
  58. uint8_t tr_fttl; /* ttl required to forward on outvif */
  59. uint8_t tr_smask; /* subnet mask for src addr */
  60. uint8_t tr_rflags; /* forwarding error codes */
  61. };
  62. /* defs within mtrace */
  63. #define TR_QUERY 1
  64. #define TR_RESP 2
  65. /* fields for tr_rflags (forwarding error codes) */
  66. #define TR_NO_ERR 0
  67. #define TR_WRONG_IF 1
  68. #define TR_PRUNED 2
  69. #define TR_OPRUNED 3
  70. #define TR_SCOPED 4
  71. #define TR_NO_RTE 5
  72. #define TR_NO_FWD 7
  73. #define TR_NO_SPACE 0x81
  74. #define TR_OLD_ROUTER 0x82
  75. /* fields for tr_rproto (routing protocol) */
  76. #define TR_PROTO_DVMRP 1
  77. #define TR_PROTO_MOSPF 2
  78. #define TR_PROTO_PIM 3
  79. #define TR_PROTO_CBT 4
  80. /* igmpv3 report types */
  81. static const struct tok igmpv3report2str[] = {
  82. { 1, "is_in" },
  83. { 2, "is_ex" },
  84. { 3, "to_in" },
  85. { 4, "to_ex" },
  86. { 5, "allow" },
  87. { 6, "block" },
  88. { 0, NULL }
  89. };
  90. static void
  91. print_mtrace(netdissect_options *ndo,
  92. register const u_char *bp, register u_int len)
  93. {
  94. register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
  95. ND_TCHECK(*tr);
  96. if (len < 8 + sizeof (struct tr_query)) {
  97. ND_PRINT((ndo, " [invalid len %d]", len));
  98. return;
  99. }
  100. ND_PRINT((ndo, "mtrace %u: %s to %s reply-to %s",
  101. TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
  102. ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
  103. ipaddr_string(ndo, &tr->tr_raddr)));
  104. if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
  105. ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
  106. return;
  107. trunc:
  108. ND_PRINT((ndo, "%s", tstr));
  109. }
  110. static void
  111. print_mresp(netdissect_options *ndo,
  112. register const u_char *bp, register u_int len)
  113. {
  114. register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
  115. ND_TCHECK(*tr);
  116. if (len < 8 + sizeof (struct tr_query)) {
  117. ND_PRINT((ndo, " [invalid len %d]", len));
  118. return;
  119. }
  120. ND_PRINT((ndo, "mresp %lu: %s to %s reply-to %s",
  121. (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
  122. ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
  123. ipaddr_string(ndo, &tr->tr_raddr)));
  124. if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
  125. ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
  126. return;
  127. trunc:
  128. ND_PRINT((ndo, "%s", tstr));
  129. }
  130. static void
  131. print_igmpv3_report(netdissect_options *ndo,
  132. register const u_char *bp, register u_int len)
  133. {
  134. u_int group, nsrcs, ngroups;
  135. register u_int i, j;
  136. /* Minimum len is 16, and should be a multiple of 4 */
  137. if (len < 16 || len & 0x03) {
  138. ND_PRINT((ndo, " [invalid len %d]", len));
  139. return;
  140. }
  141. ND_TCHECK2(bp[6], 2);
  142. ngroups = EXTRACT_16BITS(&bp[6]);
  143. ND_PRINT((ndo, ", %d group record(s)", ngroups));
  144. if (ndo->ndo_vflag > 0) {
  145. /* Print the group records */
  146. group = 8;
  147. for (i=0; i<ngroups; i++) {
  148. if (len < group+8) {
  149. ND_PRINT((ndo, " [invalid number of groups]"));
  150. return;
  151. }
  152. ND_TCHECK2(bp[group+4], 4);
  153. ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[group+4])));
  154. ND_PRINT((ndo, " %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
  155. bp[group])));
  156. nsrcs = EXTRACT_16BITS(&bp[group+2]);
  157. /* Check the number of sources and print them */
  158. if (len < group+8+(nsrcs<<2)) {
  159. ND_PRINT((ndo, " [invalid number of sources %d]", nsrcs));
  160. return;
  161. }
  162. if (ndo->ndo_vflag == 1)
  163. ND_PRINT((ndo, ", %d source(s)", nsrcs));
  164. else {
  165. /* Print the sources */
  166. ND_PRINT((ndo, " {"));
  167. for (j=0; j<nsrcs; j++) {
  168. ND_TCHECK2(bp[group+8+(j<<2)], 4);
  169. ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[group+8+(j<<2)])));
  170. }
  171. ND_PRINT((ndo, " }"));
  172. }
  173. /* Next group record */
  174. group += 8 + (nsrcs << 2);
  175. ND_PRINT((ndo, "]"));
  176. }
  177. }
  178. return;
  179. trunc:
  180. ND_PRINT((ndo, "%s", tstr));
  181. }
  182. static void
  183. print_igmpv3_query(netdissect_options *ndo,
  184. register const u_char *bp, register u_int len)
  185. {
  186. u_int mrc;
  187. u_int mrt;
  188. u_int nsrcs;
  189. register u_int i;
  190. ND_PRINT((ndo, " v3"));
  191. /* Minimum len is 12, and should be a multiple of 4 */
  192. if (len < 12 || len & 0x03) {
  193. ND_PRINT((ndo, " [invalid len %d]", len));
  194. return;
  195. }
  196. ND_TCHECK(bp[1]);
  197. mrc = bp[1];
  198. if (mrc < 128) {
  199. mrt = mrc;
  200. } else {
  201. mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
  202. }
  203. if (mrc != 100) {
  204. ND_PRINT((ndo, " [max resp time "));
  205. if (mrt < 600) {
  206. ND_PRINT((ndo, "%.1fs", mrt * 0.1));
  207. } else {
  208. unsigned_relts_print(ndo, mrt / 10);
  209. }
  210. ND_PRINT((ndo, "]"));
  211. }
  212. ND_TCHECK2(bp[4], 4);
  213. if (EXTRACT_32BITS(&bp[4]) == 0)
  214. return;
  215. ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[4])));
  216. ND_TCHECK2(bp[10], 2);
  217. nsrcs = EXTRACT_16BITS(&bp[10]);
  218. if (nsrcs > 0) {
  219. if (len < 12 + (nsrcs << 2))
  220. ND_PRINT((ndo, " [invalid number of sources]"));
  221. else if (ndo->ndo_vflag > 1) {
  222. ND_PRINT((ndo, " {"));
  223. for (i=0; i<nsrcs; i++) {
  224. ND_TCHECK2(bp[12+(i<<2)], 4);
  225. ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[12+(i<<2)])));
  226. }
  227. ND_PRINT((ndo, " }"));
  228. } else
  229. ND_PRINT((ndo, ", %d source(s)", nsrcs));
  230. }
  231. ND_PRINT((ndo, "]"));
  232. return;
  233. trunc:
  234. ND_PRINT((ndo, "%s", tstr));
  235. }
  236. void
  237. igmp_print(netdissect_options *ndo,
  238. register const u_char *bp, register u_int len)
  239. {
  240. struct cksum_vec vec[1];
  241. if (ndo->ndo_qflag) {
  242. ND_PRINT((ndo, "igmp"));
  243. return;
  244. }
  245. ND_TCHECK(bp[0]);
  246. switch (bp[0]) {
  247. case 0x11:
  248. ND_PRINT((ndo, "igmp query"));
  249. if (len >= 12)
  250. print_igmpv3_query(ndo, bp, len);
  251. else {
  252. ND_TCHECK(bp[1]);
  253. if (bp[1]) {
  254. ND_PRINT((ndo, " v2"));
  255. if (bp[1] != 100)
  256. ND_PRINT((ndo, " [max resp time %d]", bp[1]));
  257. } else
  258. ND_PRINT((ndo, " v1"));
  259. ND_TCHECK2(bp[4], 4);
  260. if (EXTRACT_32BITS(&bp[4]))
  261. ND_PRINT((ndo, " [gaddr %s]", ipaddr_string(ndo, &bp[4])));
  262. if (len != 8)
  263. ND_PRINT((ndo, " [len %d]", len));
  264. }
  265. break;
  266. case 0x12:
  267. ND_TCHECK2(bp[4], 4);
  268. ND_PRINT((ndo, "igmp v1 report %s", ipaddr_string(ndo, &bp[4])));
  269. if (len != 8)
  270. ND_PRINT((ndo, " [len %d]", len));
  271. break;
  272. case 0x16:
  273. ND_TCHECK2(bp[4], 4);
  274. ND_PRINT((ndo, "igmp v2 report %s", ipaddr_string(ndo, &bp[4])));
  275. break;
  276. case 0x22:
  277. ND_PRINT((ndo, "igmp v3 report"));
  278. print_igmpv3_report(ndo, bp, len);
  279. break;
  280. case 0x17:
  281. ND_TCHECK2(bp[4], 4);
  282. ND_PRINT((ndo, "igmp leave %s", ipaddr_string(ndo, &bp[4])));
  283. break;
  284. case 0x13:
  285. ND_PRINT((ndo, "igmp dvmrp"));
  286. if (len < 8)
  287. ND_PRINT((ndo, " [len %d]", len));
  288. else
  289. dvmrp_print(ndo, bp, len);
  290. break;
  291. case 0x14:
  292. ND_PRINT((ndo, "igmp pimv1"));
  293. pimv1_print(ndo, bp, len);
  294. break;
  295. case 0x1e:
  296. print_mresp(ndo, bp, len);
  297. break;
  298. case 0x1f:
  299. print_mtrace(ndo, bp, len);
  300. break;
  301. default:
  302. ND_PRINT((ndo, "igmp-%d", bp[0]));
  303. break;
  304. }
  305. if (ndo->ndo_vflag && len >= 4 && ND_TTEST2(bp[0], len)) {
  306. /* Check the IGMP checksum */
  307. vec[0].ptr = bp;
  308. vec[0].len = len;
  309. if (in_cksum(vec, 1))
  310. ND_PRINT((ndo, " bad igmp cksum %x!", EXTRACT_16BITS(&bp[2])));
  311. }
  312. return;
  313. trunc:
  314. ND_PRINT((ndo, "%s", tstr));
  315. }