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