igmp.c revision 1.7 1 1.1 hpeyerl /*
2 1.1 hpeyerl * Copyright (c) 1988 Stephen Deering.
3 1.5 mycroft * Copyright (c) 1992, 1993
4 1.5 mycroft * The Regents of the University of California. All rights reserved.
5 1.1 hpeyerl *
6 1.1 hpeyerl * This code is derived from software contributed to Berkeley by
7 1.1 hpeyerl * Stephen Deering of Stanford University.
8 1.1 hpeyerl *
9 1.1 hpeyerl * Redistribution and use in source and binary forms, with or without
10 1.1 hpeyerl * modification, are permitted provided that the following conditions
11 1.1 hpeyerl * are met:
12 1.1 hpeyerl * 1. Redistributions of source code must retain the above copyright
13 1.1 hpeyerl * notice, this list of conditions and the following disclaimer.
14 1.1 hpeyerl * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 hpeyerl * notice, this list of conditions and the following disclaimer in the
16 1.1 hpeyerl * documentation and/or other materials provided with the distribution.
17 1.1 hpeyerl * 3. All advertising materials mentioning features or use of this software
18 1.1 hpeyerl * must display the following acknowledgement:
19 1.1 hpeyerl * This product includes software developed by the University of
20 1.1 hpeyerl * California, Berkeley and its contributors.
21 1.1 hpeyerl * 4. Neither the name of the University nor the names of its contributors
22 1.1 hpeyerl * may be used to endorse or promote products derived from this software
23 1.1 hpeyerl * without specific prior written permission.
24 1.1 hpeyerl *
25 1.1 hpeyerl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 hpeyerl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 hpeyerl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 hpeyerl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 hpeyerl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 hpeyerl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 hpeyerl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 hpeyerl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 hpeyerl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 hpeyerl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 hpeyerl * SUCH DAMAGE.
36 1.1 hpeyerl *
37 1.5 mycroft * from: @(#)igmp.c 8.1 (Berkeley) 7/19/93
38 1.7 brezak * $Id: igmp.c,v 1.7 1994/06/09 15:59:47 brezak Exp $
39 1.1 hpeyerl */
40 1.1 hpeyerl
41 1.1 hpeyerl /* Internet Group Management Protocol (IGMP) routines. */
42 1.1 hpeyerl
43 1.1 hpeyerl
44 1.1 hpeyerl #include <sys/param.h>
45 1.1 hpeyerl #include <sys/mbuf.h>
46 1.1 hpeyerl #include <sys/socket.h>
47 1.1 hpeyerl #include <sys/protosw.h>
48 1.1 hpeyerl
49 1.1 hpeyerl #include <net/if.h>
50 1.1 hpeyerl #include <net/route.h>
51 1.1 hpeyerl
52 1.1 hpeyerl #include <netinet/in.h>
53 1.1 hpeyerl #include <netinet/in_var.h>
54 1.1 hpeyerl #include <netinet/in_systm.h>
55 1.1 hpeyerl #include <netinet/ip.h>
56 1.1 hpeyerl #include <netinet/ip_var.h>
57 1.1 hpeyerl #include <netinet/igmp.h>
58 1.1 hpeyerl #include <netinet/igmp_var.h>
59 1.1 hpeyerl
60 1.1 hpeyerl static int igmp_timers_are_running = 0;
61 1.1 hpeyerl static u_long igmp_all_hosts_group;
62 1.1 hpeyerl
63 1.1 hpeyerl static void igmp_sendreport __P((struct in_multi *));
64 1.1 hpeyerl
65 1.1 hpeyerl void
66 1.1 hpeyerl igmp_init()
67 1.1 hpeyerl {
68 1.1 hpeyerl /*
69 1.1 hpeyerl * To avoid byte-swapping the same value over and over again.
70 1.1 hpeyerl */
71 1.1 hpeyerl igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
72 1.1 hpeyerl }
73 1.1 hpeyerl
74 1.1 hpeyerl void
75 1.1 hpeyerl igmp_input(m, iphlen)
76 1.1 hpeyerl register struct mbuf *m;
77 1.1 hpeyerl register int iphlen;
78 1.1 hpeyerl {
79 1.1 hpeyerl register struct igmp *igmp;
80 1.1 hpeyerl register struct ip *ip;
81 1.2 mycroft register int igmplen;
82 1.1 hpeyerl register struct ifnet *ifp = m->m_pkthdr.rcvif;
83 1.1 hpeyerl register int minlen;
84 1.1 hpeyerl register struct in_multi *inm;
85 1.1 hpeyerl register struct in_ifaddr *ia;
86 1.1 hpeyerl struct in_multistep step;
87 1.1 hpeyerl
88 1.1 hpeyerl ++igmpstat.igps_rcv_total;
89 1.1 hpeyerl
90 1.1 hpeyerl ip = mtod(m, struct ip *);
91 1.1 hpeyerl igmplen = ip->ip_len;
92 1.1 hpeyerl
93 1.1 hpeyerl /*
94 1.1 hpeyerl * Validate lengths
95 1.1 hpeyerl */
96 1.1 hpeyerl if (igmplen < IGMP_MINLEN) {
97 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
98 1.1 hpeyerl m_freem(m);
99 1.1 hpeyerl return;
100 1.1 hpeyerl }
101 1.1 hpeyerl minlen = iphlen + IGMP_MINLEN;
102 1.1 hpeyerl if ((m->m_flags & M_EXT || m->m_len < minlen) &&
103 1.1 hpeyerl (m = m_pullup(m, minlen)) == 0) {
104 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
105 1.1 hpeyerl return;
106 1.1 hpeyerl }
107 1.1 hpeyerl
108 1.1 hpeyerl /*
109 1.1 hpeyerl * Validate checksum
110 1.1 hpeyerl */
111 1.1 hpeyerl m->m_data += iphlen;
112 1.1 hpeyerl m->m_len -= iphlen;
113 1.1 hpeyerl igmp = mtod(m, struct igmp *);
114 1.1 hpeyerl if (in_cksum(m, igmplen)) {
115 1.1 hpeyerl ++igmpstat.igps_rcv_badsum;
116 1.1 hpeyerl m_freem(m);
117 1.1 hpeyerl return;
118 1.1 hpeyerl }
119 1.1 hpeyerl m->m_data -= iphlen;
120 1.1 hpeyerl m->m_len += iphlen;
121 1.1 hpeyerl ip = mtod(m, struct ip *);
122 1.1 hpeyerl
123 1.1 hpeyerl switch (igmp->igmp_type) {
124 1.1 hpeyerl
125 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_QUERY:
126 1.1 hpeyerl ++igmpstat.igps_rcv_queries;
127 1.1 hpeyerl
128 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
129 1.1 hpeyerl break;
130 1.1 hpeyerl
131 1.1 hpeyerl if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
132 1.1 hpeyerl ++igmpstat.igps_rcv_badqueries;
133 1.1 hpeyerl m_freem(m);
134 1.1 hpeyerl return;
135 1.1 hpeyerl }
136 1.1 hpeyerl
137 1.1 hpeyerl /*
138 1.1 hpeyerl * Start the timers in all of our membership records for
139 1.1 hpeyerl * the interface on which the query arrived, except those
140 1.1 hpeyerl * that are already running and those that belong to the
141 1.1 hpeyerl * "all-hosts" group.
142 1.1 hpeyerl */
143 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
144 1.1 hpeyerl while (inm != NULL) {
145 1.1 hpeyerl if (inm->inm_ifp == ifp && inm->inm_timer == 0 &&
146 1.1 hpeyerl inm->inm_addr.s_addr != igmp_all_hosts_group) {
147 1.1 hpeyerl inm->inm_timer =
148 1.1 hpeyerl IGMP_RANDOM_DELAY(inm->inm_addr);
149 1.1 hpeyerl igmp_timers_are_running = 1;
150 1.1 hpeyerl }
151 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
152 1.1 hpeyerl }
153 1.1 hpeyerl
154 1.1 hpeyerl break;
155 1.1 hpeyerl
156 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_REPORT:
157 1.1 hpeyerl ++igmpstat.igps_rcv_reports;
158 1.1 hpeyerl
159 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
160 1.1 hpeyerl break;
161 1.1 hpeyerl
162 1.1 hpeyerl if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
163 1.1 hpeyerl igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
164 1.1 hpeyerl ++igmpstat.igps_rcv_badreports;
165 1.1 hpeyerl m_freem(m);
166 1.1 hpeyerl return;
167 1.1 hpeyerl }
168 1.1 hpeyerl
169 1.1 hpeyerl /*
170 1.1 hpeyerl * KLUDGE: if the IP source address of the report has an
171 1.1 hpeyerl * unspecified (i.e., zero) subnet number, as is allowed for
172 1.1 hpeyerl * a booting host, replace it with the correct subnet number
173 1.1 hpeyerl * so that a process-level multicast routing demon can
174 1.1 hpeyerl * determine which subnet it arrived from. This is necessary
175 1.1 hpeyerl * to compensate for the lack of any way for a process to
176 1.1 hpeyerl * determine the arrival interface of an incoming packet.
177 1.1 hpeyerl */
178 1.1 hpeyerl if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
179 1.1 hpeyerl IFP_TO_IA(ifp, ia);
180 1.1 hpeyerl if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
181 1.1 hpeyerl }
182 1.1 hpeyerl
183 1.1 hpeyerl /*
184 1.1 hpeyerl * If we belong to the group being reported, stop
185 1.1 hpeyerl * our timer for that group.
186 1.1 hpeyerl */
187 1.1 hpeyerl IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
188 1.1 hpeyerl if (inm != NULL) {
189 1.1 hpeyerl inm->inm_timer = 0;
190 1.1 hpeyerl ++igmpstat.igps_rcv_ourreports;
191 1.1 hpeyerl }
192 1.1 hpeyerl
193 1.1 hpeyerl break;
194 1.1 hpeyerl }
195 1.1 hpeyerl
196 1.1 hpeyerl /*
197 1.1 hpeyerl * Pass all valid IGMP packets up to any process(es) listening
198 1.1 hpeyerl * on a raw IGMP socket.
199 1.1 hpeyerl */
200 1.1 hpeyerl rip_input(m);
201 1.1 hpeyerl }
202 1.1 hpeyerl
203 1.1 hpeyerl void
204 1.1 hpeyerl igmp_joingroup(inm)
205 1.1 hpeyerl struct in_multi *inm;
206 1.1 hpeyerl {
207 1.1 hpeyerl register int s = splnet();
208 1.1 hpeyerl
209 1.1 hpeyerl if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
210 1.6 mycroft (inm->inm_ifp->if_flags & IFF_LOOPBACK))
211 1.1 hpeyerl inm->inm_timer = 0;
212 1.1 hpeyerl else {
213 1.1 hpeyerl igmp_sendreport(inm);
214 1.1 hpeyerl inm->inm_timer = IGMP_RANDOM_DELAY(inm->inm_addr);
215 1.1 hpeyerl igmp_timers_are_running = 1;
216 1.1 hpeyerl }
217 1.1 hpeyerl splx(s);
218 1.1 hpeyerl }
219 1.1 hpeyerl
220 1.1 hpeyerl void
221 1.1 hpeyerl igmp_leavegroup(inm)
222 1.1 hpeyerl struct in_multi *inm;
223 1.1 hpeyerl {
224 1.1 hpeyerl /*
225 1.1 hpeyerl * No action required on leaving a group.
226 1.1 hpeyerl */
227 1.1 hpeyerl }
228 1.1 hpeyerl
229 1.1 hpeyerl void
230 1.1 hpeyerl igmp_fasttimo()
231 1.1 hpeyerl {
232 1.1 hpeyerl register struct in_multi *inm;
233 1.1 hpeyerl register int s;
234 1.1 hpeyerl struct in_multistep step;
235 1.1 hpeyerl
236 1.1 hpeyerl /*
237 1.1 hpeyerl * Quick check to see if any work needs to be done, in order
238 1.1 hpeyerl * to minimize the overhead of fasttimo processing.
239 1.1 hpeyerl */
240 1.1 hpeyerl if (!igmp_timers_are_running)
241 1.1 hpeyerl return;
242 1.1 hpeyerl
243 1.1 hpeyerl s = splnet();
244 1.1 hpeyerl igmp_timers_are_running = 0;
245 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
246 1.1 hpeyerl while (inm != NULL) {
247 1.1 hpeyerl if (inm->inm_timer == 0) {
248 1.1 hpeyerl /* do nothing */
249 1.1 hpeyerl } else if (--inm->inm_timer == 0) {
250 1.1 hpeyerl igmp_sendreport(inm);
251 1.1 hpeyerl } else {
252 1.1 hpeyerl igmp_timers_are_running = 1;
253 1.1 hpeyerl }
254 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
255 1.1 hpeyerl }
256 1.1 hpeyerl splx(s);
257 1.1 hpeyerl }
258 1.1 hpeyerl
259 1.1 hpeyerl static void
260 1.1 hpeyerl igmp_sendreport(inm)
261 1.1 hpeyerl register struct in_multi *inm;
262 1.1 hpeyerl {
263 1.1 hpeyerl register struct mbuf *m;
264 1.1 hpeyerl register struct igmp *igmp;
265 1.1 hpeyerl register struct ip *ip;
266 1.1 hpeyerl register struct ip_moptions *imo;
267 1.1 hpeyerl struct ip_moptions simo;
268 1.1 hpeyerl
269 1.1 hpeyerl MGETHDR(m, M_DONTWAIT, MT_HEADER);
270 1.1 hpeyerl if (m == NULL)
271 1.1 hpeyerl return;
272 1.1 hpeyerl /*
273 1.1 hpeyerl * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
274 1.1 hpeyerl * is smaller than mbuf size returned by MGETHDR.
275 1.1 hpeyerl */
276 1.1 hpeyerl m->m_data += max_linkhdr;
277 1.1 hpeyerl m->m_len = sizeof(struct ip) + IGMP_MINLEN;
278 1.1 hpeyerl m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
279 1.1 hpeyerl
280 1.1 hpeyerl ip = mtod(m, struct ip *);
281 1.1 hpeyerl ip->ip_tos = 0;
282 1.1 hpeyerl ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
283 1.1 hpeyerl ip->ip_off = 0;
284 1.1 hpeyerl ip->ip_p = IPPROTO_IGMP;
285 1.1 hpeyerl ip->ip_src.s_addr = INADDR_ANY;
286 1.1 hpeyerl ip->ip_dst = inm->inm_addr;
287 1.1 hpeyerl
288 1.7 brezak m->m_data += sizeof(struct ip);
289 1.7 brezak m->m_len -= sizeof(struct ip);
290 1.7 brezak igmp = mtod(m, struct igmp *);
291 1.1 hpeyerl igmp->igmp_type = IGMP_HOST_MEMBERSHIP_REPORT;
292 1.1 hpeyerl igmp->igmp_code = 0;
293 1.1 hpeyerl igmp->igmp_group = inm->inm_addr;
294 1.1 hpeyerl igmp->igmp_cksum = 0;
295 1.1 hpeyerl igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
296 1.7 brezak m->m_data -= sizeof(struct ip);
297 1.7 brezak m->m_len += sizeof(struct ip);
298 1.1 hpeyerl
299 1.1 hpeyerl imo = &simo;
300 1.1 hpeyerl bzero((caddr_t)imo, sizeof(*imo));
301 1.1 hpeyerl imo->imo_multicast_ifp = inm->inm_ifp;
302 1.1 hpeyerl imo->imo_multicast_ttl = 1;
303 1.1 hpeyerl /*
304 1.1 hpeyerl * Request loopback of the report if we are acting as a multicast
305 1.1 hpeyerl * router, so that the process-level routing demon can hear it.
306 1.1 hpeyerl */
307 1.1 hpeyerl #ifdef MROUTING
308 1.5 mycroft {
309 1.5 mycroft extern struct socket *ip_mrouter;
310 1.1 hpeyerl imo->imo_multicast_loop = (ip_mrouter != NULL);
311 1.5 mycroft }
312 1.1 hpeyerl #endif
313 1.3 mycroft ip_output(m, NULL, NULL, 0, imo);
314 1.1 hpeyerl
315 1.1 hpeyerl ++igmpstat.igps_snd_reports;
316 1.1 hpeyerl }
317