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 #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <string.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
31*7c478bd9Sstevel@tonic-gate #include <net/if.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
34*7c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
35*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
36*7c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
37*7c478bd9Sstevel@tonic-gate #include <netinet/igmp.h>
38*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
39*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
40*7c478bd9Sstevel@tonic-gate #include <netdb.h>
41*7c478bd9Sstevel@tonic-gate #include "snoop.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static void interpret_igmpv3qry(struct igmp *, int);
44*7c478bd9Sstevel@tonic-gate static void interpret_igmpv3rpt(struct igmp *, int);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48*7c478bd9Sstevel@tonic-gate void
interpret_igmp(int flags,char * data,int iplen,int ilen)49*7c478bd9Sstevel@tonic-gate interpret_igmp(int flags, char *data, int iplen, int ilen)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	const char *pt;
52*7c478bd9Sstevel@tonic-gate 	char *line;
53*7c478bd9Sstevel@tonic-gate 	struct igmp *igmp = (struct igmp *)data;
54*7c478bd9Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (ilen < IGMP_MINLEN) {
57*7c478bd9Sstevel@tonic-gate 		/* incomplete header */
58*7c478bd9Sstevel@tonic-gate 		line = get_sum_line();
59*7c478bd9Sstevel@tonic-gate 		(void) snprintf(line, MAXLINE, "Malformed IGMP packet");
60*7c478bd9Sstevel@tonic-gate 		return;
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	switch (igmp->igmp_type) {
64*7c478bd9Sstevel@tonic-gate 	case IGMP_MEMBERSHIP_QUERY:
65*7c478bd9Sstevel@tonic-gate 		if (ilen == IGMP_MINLEN) {
66*7c478bd9Sstevel@tonic-gate 			if (igmp->igmp_code == 0)
67*7c478bd9Sstevel@tonic-gate 				pt = "v1 membership query";
68*7c478bd9Sstevel@tonic-gate 			else
69*7c478bd9Sstevel@tonic-gate 				pt = "v2 membership query";
70*7c478bd9Sstevel@tonic-gate 		} else if (ilen >= IGMP_V3_QUERY_MINLEN) {
71*7c478bd9Sstevel@tonic-gate 			pt = "v3 membership query";
72*7c478bd9Sstevel@tonic-gate 		} else {
73*7c478bd9Sstevel@tonic-gate 			pt = "Unknown membership query";
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 		break;
76*7c478bd9Sstevel@tonic-gate 	case IGMP_V1_MEMBERSHIP_REPORT:
77*7c478bd9Sstevel@tonic-gate 		pt = "v1 membership report";
78*7c478bd9Sstevel@tonic-gate 		break;
79*7c478bd9Sstevel@tonic-gate 	case IGMP_V2_MEMBERSHIP_REPORT:
80*7c478bd9Sstevel@tonic-gate 		pt = "v2 membership report";
81*7c478bd9Sstevel@tonic-gate 		break;
82*7c478bd9Sstevel@tonic-gate 	case IGMP_V3_MEMBERSHIP_REPORT:
83*7c478bd9Sstevel@tonic-gate 		pt = "v3 membership report";
84*7c478bd9Sstevel@tonic-gate 		break;
85*7c478bd9Sstevel@tonic-gate 	case IGMP_V2_LEAVE_GROUP:
86*7c478bd9Sstevel@tonic-gate 		pt = "v2 leave group";
87*7c478bd9Sstevel@tonic-gate 		break;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	default:
90*7c478bd9Sstevel@tonic-gate 		pt = "Unknown";
91*7c478bd9Sstevel@tonic-gate 		break;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
95*7c478bd9Sstevel@tonic-gate 		line = get_sum_line();
96*7c478bd9Sstevel@tonic-gate 		(void) snprintf(line, MAXLINE, "IGMP %s", pt);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
100*7c478bd9Sstevel@tonic-gate 		show_header("IGMP:  ", "IGMP Header", ilen);
101*7c478bd9Sstevel@tonic-gate 		show_space();
102*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
103*7c478bd9Sstevel@tonic-gate 		    "Type = %d (%s)", igmp->igmp_type, pt);
104*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
105*7c478bd9Sstevel@tonic-gate 		    "Max Response Time = %d", igmp->igmp_code);
106*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
107*7c478bd9Sstevel@tonic-gate 		    "Checksum = %x", ntohs(igmp->igmp_cksum));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		if (igmp->igmp_type == IGMP_MEMBERSHIP_QUERY &&
110*7c478bd9Sstevel@tonic-gate 		    ilen >= IGMP_V3_QUERY_MINLEN) {
111*7c478bd9Sstevel@tonic-gate 			interpret_igmpv3qry(igmp, ilen);
112*7c478bd9Sstevel@tonic-gate 		} else if (igmp->igmp_type == IGMP_V3_MEMBERSHIP_REPORT) {
113*7c478bd9Sstevel@tonic-gate 			interpret_igmpv3rpt(igmp, ilen);
114*7c478bd9Sstevel@tonic-gate 		} else {
115*7c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
116*7c478bd9Sstevel@tonic-gate 			    "Group = %s",
117*7c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET, &igmp->igmp_group.s_addr,
118*7c478bd9Sstevel@tonic-gate 			    addrstr, INET_ADDRSTRLEN));
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		show_space();
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate static void
interpret_igmpv3qry(struct igmp * igmp,int ilen)126*7c478bd9Sstevel@tonic-gate interpret_igmpv3qry(struct igmp *igmp, int ilen)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	struct igmp3q *qry;
129*7c478bd9Sstevel@tonic-gate 	struct in_addr *src;
130*7c478bd9Sstevel@tonic-gate 	int rem = ilen;
131*7c478bd9Sstevel@tonic-gate 	int srccnt;
132*7c478bd9Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (ilen < sizeof (*qry)) {
135*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
136*7c478bd9Sstevel@tonic-gate 		    "Malformed IGMP Query");
137*7c478bd9Sstevel@tonic-gate 		return;
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	qry = (struct igmp3q *)igmp;
141*7c478bd9Sstevel@tonic-gate 	rem -= sizeof (*qry);
142*7c478bd9Sstevel@tonic-gate 	srccnt = ntohs(qry->igmp3q_numsrc);
143*7c478bd9Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
144*7c478bd9Sstevel@tonic-gate 	    "Group = %s", inet_ntop(AF_INET, &qry->igmp3q_group, addrstr,
145*7c478bd9Sstevel@tonic-gate 	    INET_ADDRSTRLEN));
146*7c478bd9Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
147*7c478bd9Sstevel@tonic-gate 	    "%d Source Address%s:", srccnt, (srccnt == 1) ? "" : "es");
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	src = (struct in_addr *)&qry[1];
150*7c478bd9Sstevel@tonic-gate 	while (srccnt > 0 && rem >= sizeof (*src)) {
151*7c478bd9Sstevel@tonic-gate 		rem -= sizeof (*src);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(), "    %s",
154*7c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET, &src->s_addr, addrstr, INET_ADDRSTRLEN));
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		srccnt--;
157*7c478bd9Sstevel@tonic-gate 		src++;
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate #define	MAX_IGMPV3_REPORT_TYPE	6
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate const char *igmpv3rpt_types[] = {
164*7c478bd9Sstevel@tonic-gate 	"<unknown>",
165*7c478bd9Sstevel@tonic-gate 	"MODE_IS_INCLUDE",
166*7c478bd9Sstevel@tonic-gate 	"MODE_IS_EXCLUDE",
167*7c478bd9Sstevel@tonic-gate 	"CHANGE_TO_INCLUDE",
168*7c478bd9Sstevel@tonic-gate 	"CHANGE_TO_EXCLUDE",
169*7c478bd9Sstevel@tonic-gate 	"ALLOW_NEW_SOURCES",
170*7c478bd9Sstevel@tonic-gate 	"BLOCK_OLD_SOURCES",
171*7c478bd9Sstevel@tonic-gate };
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate static void
interpret_igmpv3rpt(struct igmp * igmp,int ilen)174*7c478bd9Sstevel@tonic-gate interpret_igmpv3rpt(struct igmp *igmp, int ilen)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	struct igmp3r *rpt;
177*7c478bd9Sstevel@tonic-gate 	struct grphdr *grh;
178*7c478bd9Sstevel@tonic-gate 	struct in_addr *src;
179*7c478bd9Sstevel@tonic-gate 	int rem = ilen, auxlen;
180*7c478bd9Sstevel@tonic-gate 	uint16_t grhcnt, srccnt;
181*7c478bd9Sstevel@tonic-gate 	char addrstr[INET_ADDRSTRLEN];
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	if (ilen < sizeof (*rpt)) {
184*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
185*7c478bd9Sstevel@tonic-gate 		    "Malformed IGMPv3 Report");
186*7c478bd9Sstevel@tonic-gate 		return;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	rpt = (struct igmp3r *)igmp;
190*7c478bd9Sstevel@tonic-gate 	grh = (struct grphdr *)&rpt[1];
191*7c478bd9Sstevel@tonic-gate 	grhcnt = ntohs(rpt->igmp3r_numrec);
192*7c478bd9Sstevel@tonic-gate 	(void) snprintf(get_line(0, 0), get_line_remain(),
193*7c478bd9Sstevel@tonic-gate 	    "%d Group Record%s:", grhcnt, (grhcnt == 1) ? "" : "s");
194*7c478bd9Sstevel@tonic-gate 	rem -= sizeof (*rpt);
195*7c478bd9Sstevel@tonic-gate 	while (grhcnt > 0 && rem >= sizeof (*grh)) {
196*7c478bd9Sstevel@tonic-gate 		rem -= sizeof (*grh);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
199*7c478bd9Sstevel@tonic-gate 		    "Group = %s  type = %s", inet_ntop(AF_INET,
200*7c478bd9Sstevel@tonic-gate 		    &grh->grphdr_group.s_addr, addrstr, INET_ADDRSTRLEN),
201*7c478bd9Sstevel@tonic-gate 		    (grh->grphdr_type > MAX_IGMPV3_REPORT_TYPE) ?
202*7c478bd9Sstevel@tonic-gate 		    "<unknown>" : igmpv3rpt_types[grh->grphdr_type]);
203*7c478bd9Sstevel@tonic-gate 		srccnt = ntohs(grh->grphdr_numsrc);
204*7c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
205*7c478bd9Sstevel@tonic-gate 		    "%d Source Address%s:", srccnt, (srccnt == 1) ? "" : "es");
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 		src = (struct in_addr *)&grh[1];
208*7c478bd9Sstevel@tonic-gate 		while (srccnt > 0 && rem >= sizeof (*src)) {
209*7c478bd9Sstevel@tonic-gate 			rem -= sizeof (*src);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
212*7c478bd9Sstevel@tonic-gate 			    "    %s", inet_ntop(AF_INET, &src->s_addr, addrstr,
213*7c478bd9Sstevel@tonic-gate 			    INET_ADDRSTRLEN));
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 			srccnt--;
216*7c478bd9Sstevel@tonic-gate 			src++;
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		grhcnt--;
220*7c478bd9Sstevel@tonic-gate 		auxlen = grh->grphdr_auxlen * 4;
221*7c478bd9Sstevel@tonic-gate 		rem -= auxlen;
222*7c478bd9Sstevel@tonic-gate 		grh = (struct grphdr *)((uint8_t *)src + auxlen);
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate }
225