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