xref: /freebsd/contrib/tcpdump/print-igmp.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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 #ifndef lint
23 static const char rcsid[] =
24     "@(#) $Header: /tcpdump/master/tcpdump/print-igmp.c,v 1.3 2001/01/09 08:01:18 fenner Exp $ (LBL)";
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.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  */
54 struct tr_query {
55     u_int  tr_src;          /* traceroute source */
56     u_int  tr_dst;          /* traceroute destination */
57     u_int  tr_raddr;        /* traceroute response address */
58     u_int  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  */
68 struct tr_resp {
69     u_int tr_qarr;          /* query arrival time */
70     u_int tr_inaddr;        /* incoming interface address */
71     u_int tr_outaddr;       /* outgoing interface address */
72     u_int tr_rmtaddr;       /* parent address in source tree */
73     u_int tr_vifin;         /* input packet count on interface */
74     u_int tr_vifout;        /* output packet count on interface */
75     u_int tr_pktcnt;        /* total incoming packets for src-grp */
76     u_char  tr_rproto;      /* routing proto deployed on router */
77     u_char  tr_fttl;        /* ttl required to forward on outvif */
78     u_char  tr_smask;       /* subnet mask for src addr */
79     u_char  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 */
104 static 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 
114 static void
115 print_mtrace(register const u_char *bp, register u_int len)
116 {
117     register struct tr_query *tr = (struct tr_query *)(bp + 8);
118 
119     printf("mtrace %lu: %s to %s reply-to %s",
120         (u_long)TR_GETQID(ntohl(tr->tr_rttlqid)),
121         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
122         ipaddr_string(&tr->tr_raddr));
123     if (IN_CLASSD(ntohl(tr->tr_raddr)))
124         printf(" with-ttl %d", TR_GETTTL(ntohl(tr->tr_rttlqid)));
125 }
126 
127 static void
128 print_mresp(register const u_char *bp, register u_int len)
129 {
130     register struct tr_query *tr = (struct tr_query *)(bp + 8);
131 
132     printf("mresp %lu: %s to %s reply-to %s",
133         (u_long)TR_GETQID(ntohl(tr->tr_rttlqid)),
134         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
135         ipaddr_string(&tr->tr_raddr));
136     if (IN_CLASSD(ntohl(tr->tr_raddr)))
137         printf(" with-ttl %d", TR_GETTTL(ntohl(tr->tr_rttlqid)));
138 }
139 
140 static void
141 print_igmpv3_report(register const u_char *bp, register u_int len,
142        register const u_char *bp2)
143 {
144     int group, nsrcs, ngroups;
145     register int i, j;
146 
147     /* Minimum len is 16, and should be a multiple of 4 */
148     if (len < 16 || len & 0x03) {
149     	(void)printf(" [invalid len %d]", len);
150     	return;
151     }
152     TCHECK2(bp[6], 2);
153     ngroups = EXTRACT_16BITS(&bp[6]);
154     (void)printf(", %d group record(s)", ngroups);
155     if (vflag > 0) {
156 	/* Print the group records */
157     	group = 8;
158         for (i=0; i<ngroups; i++) {
159 	    if (len < group+8) {
160 		(void)printf(" [invalid number of groups]");
161 		return;
162 	    }
163 	    TCHECK2(bp[group+4], 4);
164             (void)printf(" [gaddr %s", ipaddr_string(&bp[group+4]));
165 	    (void)printf(" %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
166 								bp[group]));
167             nsrcs = EXTRACT_16BITS(&bp[group+2]);
168 	    /* Check the number of sources and print them */
169 	    if (len < group+8+(nsrcs<<2)) {
170 		(void)printf(" [invalid number of sources %d]", nsrcs);
171 		return;
172 	    }
173             if (vflag == 1)
174                 (void)printf(", %d source(s)", nsrcs);
175             else {
176 		/* Print the sources */
177                 (void)printf(" {");
178                 for (j=0; j<nsrcs; j++) {
179 		    TCHECK2(bp[group+8+(j<<2)], 4);
180 		    (void)printf(" %s", ipaddr_string(&bp[group+8+(j<<2)]));
181 		}
182                 (void)printf(" }");
183             }
184 	    /* Next group record */
185             group += 8 + (nsrcs << 2);
186 	    (void)printf("]");
187         }
188     }
189     return;
190 trunc:
191     (void)printf("[|igmp]");
192     return;
193 }
194 
195 static void
196 print_igmpv3_query(register const u_char *bp, register u_int len,
197        register const u_char *bp2)
198 {
199     int nsrcs;
200     register int i;
201 
202     (void)printf(" v3");
203     /* Minimum len is 12, and should be a multiple of 4 */
204     if (len < 12 || len & 0x03) {
205     	(void)printf(" [invalid len %d]", len);
206     	return;
207     }
208     TCHECK2(bp[4], 4);
209     if (EXTRACT_32BITS(&bp[4]) == 0)
210 	return;
211     (void)printf(" [gaddr %s", ipaddr_string(&bp[4]));
212     TCHECK2(bp[10], 2);
213     nsrcs = EXTRACT_16BITS(&bp[10]);
214     if (nsrcs > 0) {
215 	if (len < 12 + (nsrcs << 2))
216 	    (void)printf(" [invalid number of sources]");
217 	else if (vflag > 1) {
218 	    (void)printf(" {");
219 	    for (i=0; i<nsrcs; i++) {
220 		TCHECK2(bp[12+(i<<2)], 4);
221 		(void)printf(" %s", ipaddr_string(&bp[12+(i<<2)]));
222 	    }
223 	    (void)printf(" }");
224 	} else
225 	    (void)printf(", %d source(s)", nsrcs);
226     }
227     (void)printf("]");
228     return;
229 trunc:
230     (void)printf("[|igmp]");
231     return;
232 }
233 
234 void
235 igmp_print(register const u_char *bp, register u_int len,
236        register const u_char *bp2)
237 {
238     if (qflag) {
239         (void)printf("igmp");
240         return;
241     }
242 
243     TCHECK2(bp[0], 8);
244     switch (bp[0]) {
245     case 0x11:
246         (void)printf("igmp query");
247 	if (len >= 12)
248 	    print_igmpv3_query(bp, len, bp2);
249 	else {
250 	    if (bp[1]) {
251 		(void)printf(" v2");
252 		if (bp[1] != 100)
253 		    (void)printf(" [max resp time %d]", bp[1]);
254 	    } else
255 		(void)printf(" v1");
256        	    if (EXTRACT_32BITS(&bp[4]))
257                 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
258             if (len != 8)
259                 (void)printf(" [len %d]", len);
260 	}
261         break;
262     case 0x12:
263         (void)printf("igmp v1 report %s", ipaddr_string(&bp[4]));
264         if (len != 8)
265             (void)printf(" [len %d]", len);
266         break;
267     case 0x16:
268         (void)printf("igmp v2 report %s", ipaddr_string(&bp[4]));
269         break;
270     case 0x22:
271         (void)printf("igmp v3 report");
272 	print_igmpv3_report(bp, len, bp2);
273         break;
274     case 0x17:
275         (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
276         break;
277     case 0x13:
278         (void)printf("igmp dvmrp");
279         if (len < 8)
280             (void)printf(" [len %d]", len);
281         else
282             dvmrp_print(bp, len);
283         break;
284     case 0x14:
285         (void)printf("igmp pimv1");
286         pimv1_print(bp, len);
287         break;
288     case 0x1e:
289         print_mresp(bp, len);
290         break;
291     case 0x1f:
292         print_mtrace(bp, len);
293         break;
294     default:
295         (void)printf("igmp-%d", bp[0]);
296         break;
297     }
298 
299     if (vflag && TTEST2(bp[0], len)) {
300         /* Check the IGMP checksum */
301         if (in_cksum((const u_short*)bp, len, 0))
302             printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
303     }
304     return;
305 trunc:
306     fputs("[|igmp]", stdout);
307 }
308