xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_igmp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <sys/socket.h>
33*0Sstevel@tonic-gate #include <net/if.h>
34*0Sstevel@tonic-gate #include <sys/stropts.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <netinet/in_systm.h>
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <netinet/ip.h>
39*0Sstevel@tonic-gate #include <netinet/igmp.h>
40*0Sstevel@tonic-gate #include <inet/ip.h>
41*0Sstevel@tonic-gate #include <arpa/inet.h>
42*0Sstevel@tonic-gate #include <netdb.h>
43*0Sstevel@tonic-gate #include "snoop.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static void interpret_igmpv3qry(struct igmp *, int);
46*0Sstevel@tonic-gate static void interpret_igmpv3rpt(struct igmp *, int);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*ARGSUSED*/
50*0Sstevel@tonic-gate void
interpret_igmp(int flags,char * data,int iplen,int ilen)51*0Sstevel@tonic-gate interpret_igmp(int flags, char *data, int iplen, int ilen)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	const char *pt;
54*0Sstevel@tonic-gate 	char *line;
55*0Sstevel@tonic-gate 	struct igmp *igmp = (struct igmp *)data;
56*0Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	if (ilen < IGMP_MINLEN) {
59*0Sstevel@tonic-gate 		/* incomplete header */
60*0Sstevel@tonic-gate 		line = get_sum_line();
61*0Sstevel@tonic-gate 		(void) snprintf(line, MAXLINE, "Malformed IGMP packet");
62*0Sstevel@tonic-gate 		return;
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	switch (igmp->igmp_type) {
66*0Sstevel@tonic-gate 	case IGMP_MEMBERSHIP_QUERY:
67*0Sstevel@tonic-gate 		if (ilen == IGMP_MINLEN) {
68*0Sstevel@tonic-gate 			if (igmp->igmp_code == 0)
69*0Sstevel@tonic-gate 				pt = "v1 membership query";
70*0Sstevel@tonic-gate 			else
71*0Sstevel@tonic-gate 				pt = "v2 membership query";
72*0Sstevel@tonic-gate 		} else if (ilen >= IGMP_V3_QUERY_MINLEN) {
73*0Sstevel@tonic-gate 			pt = "v3 membership query";
74*0Sstevel@tonic-gate 		} else {
75*0Sstevel@tonic-gate 			pt = "Unknown membership query";
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 		break;
78*0Sstevel@tonic-gate 	case IGMP_V1_MEMBERSHIP_REPORT:
79*0Sstevel@tonic-gate 		pt = "v1 membership report";
80*0Sstevel@tonic-gate 		break;
81*0Sstevel@tonic-gate 	case IGMP_V2_MEMBERSHIP_REPORT:
82*0Sstevel@tonic-gate 		pt = "v2 membership report";
83*0Sstevel@tonic-gate 		break;
84*0Sstevel@tonic-gate 	case IGMP_V3_MEMBERSHIP_REPORT:
85*0Sstevel@tonic-gate 		pt = "v3 membership report";
86*0Sstevel@tonic-gate 		break;
87*0Sstevel@tonic-gate 	case IGMP_V2_LEAVE_GROUP:
88*0Sstevel@tonic-gate 		pt = "v2 leave group";
89*0Sstevel@tonic-gate 		break;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	default:
92*0Sstevel@tonic-gate 		pt = "Unknown";
93*0Sstevel@tonic-gate 		break;
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if (flags & F_SUM) {
97*0Sstevel@tonic-gate 		line = get_sum_line();
98*0Sstevel@tonic-gate 		(void) snprintf(line, MAXLINE, "IGMP %s", pt);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if (flags & F_DTAIL) {
102*0Sstevel@tonic-gate 		show_header("IGMP:  ", "IGMP Header", ilen);
103*0Sstevel@tonic-gate 		show_space();
104*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
105*0Sstevel@tonic-gate 		    "Type = %d (%s)", igmp->igmp_type, pt);
106*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
107*0Sstevel@tonic-gate 		    "Max Response Time = %d", igmp->igmp_code);
108*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
109*0Sstevel@tonic-gate 		    "Checksum = %x", ntohs(igmp->igmp_cksum));
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		if (igmp->igmp_type == IGMP_MEMBERSHIP_QUERY &&
112*0Sstevel@tonic-gate 		    ilen >= IGMP_V3_QUERY_MINLEN) {
113*0Sstevel@tonic-gate 			interpret_igmpv3qry(igmp, ilen);
114*0Sstevel@tonic-gate 		} else if (igmp->igmp_type == IGMP_V3_MEMBERSHIP_REPORT) {
115*0Sstevel@tonic-gate 			interpret_igmpv3rpt(igmp, ilen);
116*0Sstevel@tonic-gate 		} else {
117*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
118*0Sstevel@tonic-gate 			    "Group = %s",
119*0Sstevel@tonic-gate 			    inet_ntop(AF_INET, &igmp->igmp_group.s_addr,
120*0Sstevel@tonic-gate 			    addrstr, INET_ADDRSTRLEN));
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		show_space();
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate static void
interpret_igmpv3qry(struct igmp * igmp,int ilen)128*0Sstevel@tonic-gate interpret_igmpv3qry(struct igmp *igmp, int ilen)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	struct igmp3q *qry;
131*0Sstevel@tonic-gate 	struct in_addr *src;
132*0Sstevel@tonic-gate 	int rem = ilen;
133*0Sstevel@tonic-gate 	int srccnt;
134*0Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (ilen < sizeof (*qry)) {
137*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
138*0Sstevel@tonic-gate 		    "Malformed IGMP Query");
139*0Sstevel@tonic-gate 		return;
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	qry = (struct igmp3q *)igmp;
143*0Sstevel@tonic-gate 	rem -= sizeof (*qry);
144*0Sstevel@tonic-gate 	srccnt = ntohs(qry->igmp3q_numsrc);
145*0Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
146*0Sstevel@tonic-gate 	    "Group = %s", inet_ntop(AF_INET, &qry->igmp3q_group, addrstr,
147*0Sstevel@tonic-gate 	    INET_ADDRSTRLEN));
148*0Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
149*0Sstevel@tonic-gate 	    "%d Source Address%s:", srccnt, (srccnt == 1) ? "" : "es");
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	src = (struct in_addr *)&qry[1];
152*0Sstevel@tonic-gate 	while (srccnt > 0 && rem >= sizeof (*src)) {
153*0Sstevel@tonic-gate 		rem -= sizeof (*src);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(), "    %s",
156*0Sstevel@tonic-gate 		    inet_ntop(AF_INET, &src->s_addr, addrstr, INET_ADDRSTRLEN));
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		srccnt--;
159*0Sstevel@tonic-gate 		src++;
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate #define	MAX_IGMPV3_REPORT_TYPE	6
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate const char *igmpv3rpt_types[] = {
166*0Sstevel@tonic-gate 	"<unknown>",
167*0Sstevel@tonic-gate 	"MODE_IS_INCLUDE",
168*0Sstevel@tonic-gate 	"MODE_IS_EXCLUDE",
169*0Sstevel@tonic-gate 	"CHANGE_TO_INCLUDE",
170*0Sstevel@tonic-gate 	"CHANGE_TO_EXCLUDE",
171*0Sstevel@tonic-gate 	"ALLOW_NEW_SOURCES",
172*0Sstevel@tonic-gate 	"BLOCK_OLD_SOURCES",
173*0Sstevel@tonic-gate };
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate static void
interpret_igmpv3rpt(struct igmp * igmp,int ilen)176*0Sstevel@tonic-gate interpret_igmpv3rpt(struct igmp *igmp, int ilen)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	struct igmp3r *rpt;
179*0Sstevel@tonic-gate 	struct grphdr *grh;
180*0Sstevel@tonic-gate 	struct in_addr *src;
181*0Sstevel@tonic-gate 	int rem = ilen, auxlen;
182*0Sstevel@tonic-gate 	uint16_t grhcnt, srccnt;
183*0Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (ilen < sizeof (*rpt)) {
186*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
187*0Sstevel@tonic-gate 		    "Malformed IGMPv3 Report");
188*0Sstevel@tonic-gate 		return;
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	rpt = (struct igmp3r *)igmp;
192*0Sstevel@tonic-gate 	grh = (struct grphdr *)&rpt[1];
193*0Sstevel@tonic-gate 	grhcnt = ntohs(rpt->igmp3r_numrec);
194*0Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
195*0Sstevel@tonic-gate 	    "%d Group Record%s:", grhcnt, (grhcnt == 1) ? "" : "s");
196*0Sstevel@tonic-gate 	rem -= sizeof (*rpt);
197*0Sstevel@tonic-gate 	while (grhcnt > 0 && rem >= sizeof (*grh)) {
198*0Sstevel@tonic-gate 		rem -= sizeof (*grh);
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
201*0Sstevel@tonic-gate 		    "Group = %s  type = %s", inet_ntop(AF_INET,
202*0Sstevel@tonic-gate 		    &grh->grphdr_group.s_addr, addrstr, INET_ADDRSTRLEN),
203*0Sstevel@tonic-gate 		    (grh->grphdr_type > MAX_IGMPV3_REPORT_TYPE) ?
204*0Sstevel@tonic-gate 		    "<unknown>" : igmpv3rpt_types[grh->grphdr_type]);
205*0Sstevel@tonic-gate 		srccnt = ntohs(grh->grphdr_numsrc);
206*0Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
207*0Sstevel@tonic-gate 		    "%d Source Address%s:", srccnt, (srccnt == 1) ? "" : "es");
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		src = (struct in_addr *)&grh[1];
210*0Sstevel@tonic-gate 		while (srccnt > 0 && rem >= sizeof (*src)) {
211*0Sstevel@tonic-gate 			rem -= sizeof (*src);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
214*0Sstevel@tonic-gate 			    "    %s", inet_ntop(AF_INET, &src->s_addr, addrstr,
215*0Sstevel@tonic-gate 			    INET_ADDRSTRLEN));
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 			srccnt--;
218*0Sstevel@tonic-gate 			src++;
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		grhcnt--;
222*0Sstevel@tonic-gate 		auxlen = grh->grphdr_auxlen * 4;
223*0Sstevel@tonic-gate 		rem -= auxlen;
224*0Sstevel@tonic-gate 		grh = (struct grphdr *)((uint8_t *)src + auxlen);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate }
227