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