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