igmp.c revision 1.1 1 1.1 hpeyerl /*
2 1.1 hpeyerl * Copyright (c) 1988 Stephen Deering.
3 1.1 hpeyerl * Copyright (c) 1992, 1993 Regents of the University of California.
4 1.1 hpeyerl * 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.1 hpeyerl * @(#)igmp.c 7.2 (Berkeley) 10/11/92
38 1.1 hpeyerl */
39 1.1 hpeyerl
40 1.1 hpeyerl /* Internet Group Management Protocol (IGMP) routines. */
41 1.1 hpeyerl
42 1.1 hpeyerl #ifdef MULTICAST
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 extern struct ifnet loif;
61 1.1 hpeyerl
62 1.1 hpeyerl static int igmp_timers_are_running = 0;
63 1.1 hpeyerl static u_long igmp_all_hosts_group;
64 1.1 hpeyerl
65 1.1 hpeyerl static void igmp_sendreport __P((struct in_multi *));
66 1.1 hpeyerl
67 1.1 hpeyerl void
68 1.1 hpeyerl igmp_init()
69 1.1 hpeyerl {
70 1.1 hpeyerl /*
71 1.1 hpeyerl * To avoid byte-swapping the same value over and over again.
72 1.1 hpeyerl */
73 1.1 hpeyerl igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
74 1.1 hpeyerl }
75 1.1 hpeyerl
76 1.1 hpeyerl void
77 1.1 hpeyerl igmp_input(m, iphlen)
78 1.1 hpeyerl register struct mbuf *m;
79 1.1 hpeyerl register int iphlen;
80 1.1 hpeyerl {
81 1.1 hpeyerl register struct igmp *igmp;
82 1.1 hpeyerl register struct ip *ip;
83 1.1 hpeyerl register int igmplen;
84 1.1 hpeyerl register struct ifnet *ifp = m->m_pkthdr.rcvif;
85 1.1 hpeyerl register int minlen;
86 1.1 hpeyerl register struct in_multi *inm;
87 1.1 hpeyerl register struct in_ifaddr *ia;
88 1.1 hpeyerl struct in_multistep step;
89 1.1 hpeyerl
90 1.1 hpeyerl ++igmpstat.igps_rcv_total;
91 1.1 hpeyerl
92 1.1 hpeyerl ip = mtod(m, struct ip *);
93 1.1 hpeyerl igmplen = ip->ip_len;
94 1.1 hpeyerl
95 1.1 hpeyerl /*
96 1.1 hpeyerl * Validate lengths
97 1.1 hpeyerl */
98 1.1 hpeyerl if (igmplen < IGMP_MINLEN) {
99 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
100 1.1 hpeyerl m_freem(m);
101 1.1 hpeyerl return;
102 1.1 hpeyerl }
103 1.1 hpeyerl minlen = iphlen + IGMP_MINLEN;
104 1.1 hpeyerl if ((m->m_flags & M_EXT || m->m_len < minlen) &&
105 1.1 hpeyerl (m = m_pullup(m, minlen)) == 0) {
106 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
107 1.1 hpeyerl return;
108 1.1 hpeyerl }
109 1.1 hpeyerl
110 1.1 hpeyerl /*
111 1.1 hpeyerl * Validate checksum
112 1.1 hpeyerl */
113 1.1 hpeyerl m->m_data += iphlen;
114 1.1 hpeyerl m->m_len -= iphlen;
115 1.1 hpeyerl igmp = mtod(m, struct igmp *);
116 1.1 hpeyerl if (in_cksum(m, igmplen)) {
117 1.1 hpeyerl ++igmpstat.igps_rcv_badsum;
118 1.1 hpeyerl m_freem(m);
119 1.1 hpeyerl return;
120 1.1 hpeyerl }
121 1.1 hpeyerl m->m_data -= iphlen;
122 1.1 hpeyerl m->m_len += iphlen;
123 1.1 hpeyerl ip = mtod(m, struct ip *);
124 1.1 hpeyerl
125 1.1 hpeyerl switch (igmp->igmp_type) {
126 1.1 hpeyerl
127 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_QUERY:
128 1.1 hpeyerl ++igmpstat.igps_rcv_queries;
129 1.1 hpeyerl
130 1.1 hpeyerl if (ifp == &loif)
131 1.1 hpeyerl break;
132 1.1 hpeyerl
133 1.1 hpeyerl if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
134 1.1 hpeyerl ++igmpstat.igps_rcv_badqueries;
135 1.1 hpeyerl m_freem(m);
136 1.1 hpeyerl return;
137 1.1 hpeyerl }
138 1.1 hpeyerl
139 1.1 hpeyerl /*
140 1.1 hpeyerl * Start the timers in all of our membership records for
141 1.1 hpeyerl * the interface on which the query arrived, except those
142 1.1 hpeyerl * that are already running and those that belong to the
143 1.1 hpeyerl * "all-hosts" group.
144 1.1 hpeyerl */
145 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
146 1.1 hpeyerl while (inm != NULL) {
147 1.1 hpeyerl if (inm->inm_ifp == ifp && inm->inm_timer == 0 &&
148 1.1 hpeyerl inm->inm_addr.s_addr != igmp_all_hosts_group) {
149 1.1 hpeyerl inm->inm_timer =
150 1.1 hpeyerl IGMP_RANDOM_DELAY(inm->inm_addr);
151 1.1 hpeyerl igmp_timers_are_running = 1;
152 1.1 hpeyerl }
153 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
154 1.1 hpeyerl }
155 1.1 hpeyerl
156 1.1 hpeyerl break;
157 1.1 hpeyerl
158 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_REPORT:
159 1.1 hpeyerl ++igmpstat.igps_rcv_reports;
160 1.1 hpeyerl
161 1.1 hpeyerl if (ifp == &loif)
162 1.1 hpeyerl break;
163 1.1 hpeyerl
164 1.1 hpeyerl if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
165 1.1 hpeyerl igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
166 1.1 hpeyerl ++igmpstat.igps_rcv_badreports;
167 1.1 hpeyerl m_freem(m);
168 1.1 hpeyerl return;
169 1.1 hpeyerl }
170 1.1 hpeyerl
171 1.1 hpeyerl /*
172 1.1 hpeyerl * KLUDGE: if the IP source address of the report has an
173 1.1 hpeyerl * unspecified (i.e., zero) subnet number, as is allowed for
174 1.1 hpeyerl * a booting host, replace it with the correct subnet number
175 1.1 hpeyerl * so that a process-level multicast routing demon can
176 1.1 hpeyerl * determine which subnet it arrived from. This is necessary
177 1.1 hpeyerl * to compensate for the lack of any way for a process to
178 1.1 hpeyerl * determine the arrival interface of an incoming packet.
179 1.1 hpeyerl */
180 1.1 hpeyerl if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
181 1.1 hpeyerl IFP_TO_IA(ifp, ia);
182 1.1 hpeyerl if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
183 1.1 hpeyerl }
184 1.1 hpeyerl
185 1.1 hpeyerl /*
186 1.1 hpeyerl * If we belong to the group being reported, stop
187 1.1 hpeyerl * our timer for that group.
188 1.1 hpeyerl */
189 1.1 hpeyerl IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
190 1.1 hpeyerl if (inm != NULL) {
191 1.1 hpeyerl inm->inm_timer = 0;
192 1.1 hpeyerl ++igmpstat.igps_rcv_ourreports;
193 1.1 hpeyerl }
194 1.1 hpeyerl
195 1.1 hpeyerl break;
196 1.1 hpeyerl }
197 1.1 hpeyerl
198 1.1 hpeyerl /*
199 1.1 hpeyerl * Pass all valid IGMP packets up to any process(es) listening
200 1.1 hpeyerl * on a raw IGMP socket.
201 1.1 hpeyerl */
202 1.1 hpeyerl rip_input(m);
203 1.1 hpeyerl }
204 1.1 hpeyerl
205 1.1 hpeyerl void
206 1.1 hpeyerl igmp_joingroup(inm)
207 1.1 hpeyerl struct in_multi *inm;
208 1.1 hpeyerl {
209 1.1 hpeyerl register int s = splnet();
210 1.1 hpeyerl
211 1.1 hpeyerl if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
212 1.1 hpeyerl inm->inm_ifp == &loif)
213 1.1 hpeyerl inm->inm_timer = 0;
214 1.1 hpeyerl else {
215 1.1 hpeyerl igmp_sendreport(inm);
216 1.1 hpeyerl inm->inm_timer = IGMP_RANDOM_DELAY(inm->inm_addr);
217 1.1 hpeyerl igmp_timers_are_running = 1;
218 1.1 hpeyerl }
219 1.1 hpeyerl splx(s);
220 1.1 hpeyerl }
221 1.1 hpeyerl
222 1.1 hpeyerl void
223 1.1 hpeyerl igmp_leavegroup(inm)
224 1.1 hpeyerl struct in_multi *inm;
225 1.1 hpeyerl {
226 1.1 hpeyerl /*
227 1.1 hpeyerl * No action required on leaving a group.
228 1.1 hpeyerl */
229 1.1 hpeyerl }
230 1.1 hpeyerl
231 1.1 hpeyerl void
232 1.1 hpeyerl igmp_fasttimo()
233 1.1 hpeyerl {
234 1.1 hpeyerl register struct in_multi *inm;
235 1.1 hpeyerl register int s;
236 1.1 hpeyerl struct in_multistep step;
237 1.1 hpeyerl
238 1.1 hpeyerl /*
239 1.1 hpeyerl * Quick check to see if any work needs to be done, in order
240 1.1 hpeyerl * to minimize the overhead of fasttimo processing.
241 1.1 hpeyerl */
242 1.1 hpeyerl if (!igmp_timers_are_running)
243 1.1 hpeyerl return;
244 1.1 hpeyerl
245 1.1 hpeyerl s = splnet();
246 1.1 hpeyerl igmp_timers_are_running = 0;
247 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
248 1.1 hpeyerl while (inm != NULL) {
249 1.1 hpeyerl if (inm->inm_timer == 0) {
250 1.1 hpeyerl /* do nothing */
251 1.1 hpeyerl } else if (--inm->inm_timer == 0) {
252 1.1 hpeyerl igmp_sendreport(inm);
253 1.1 hpeyerl } else {
254 1.1 hpeyerl igmp_timers_are_running = 1;
255 1.1 hpeyerl }
256 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
257 1.1 hpeyerl }
258 1.1 hpeyerl splx(s);
259 1.1 hpeyerl }
260 1.1 hpeyerl
261 1.1 hpeyerl static void
262 1.1 hpeyerl igmp_sendreport(inm)
263 1.1 hpeyerl register struct in_multi *inm;
264 1.1 hpeyerl {
265 1.1 hpeyerl register struct mbuf *m;
266 1.1 hpeyerl register struct igmp *igmp;
267 1.1 hpeyerl register struct ip *ip;
268 1.1 hpeyerl register struct ip_moptions *imo;
269 1.1 hpeyerl struct ip_moptions simo;
270 1.1 hpeyerl extern struct socket *ip_mrouter;
271 1.1 hpeyerl
272 1.1 hpeyerl MGETHDR(m, M_DONTWAIT, MT_HEADER);
273 1.1 hpeyerl if (m == NULL)
274 1.1 hpeyerl return;
275 1.1 hpeyerl /*
276 1.1 hpeyerl * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
277 1.1 hpeyerl * is smaller than mbuf size returned by MGETHDR.
278 1.1 hpeyerl */
279 1.1 hpeyerl m->m_data += max_linkhdr;
280 1.1 hpeyerl m->m_len = sizeof(struct ip) + IGMP_MINLEN;
281 1.1 hpeyerl m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
282 1.1 hpeyerl
283 1.1 hpeyerl ip = mtod(m, struct ip *);
284 1.1 hpeyerl ip->ip_tos = 0;
285 1.1 hpeyerl ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
286 1.1 hpeyerl ip->ip_off = 0;
287 1.1 hpeyerl ip->ip_p = IPPROTO_IGMP;
288 1.1 hpeyerl ip->ip_src.s_addr = INADDR_ANY;
289 1.1 hpeyerl ip->ip_dst = inm->inm_addr;
290 1.1 hpeyerl
291 1.1 hpeyerl m->m_data += sizeof(struct ip);
292 1.1 hpeyerl m->m_len -= sizeof(struct ip);
293 1.1 hpeyerl igmp = mtod(m, struct igmp *);
294 1.1 hpeyerl igmp->igmp_type = IGMP_HOST_MEMBERSHIP_REPORT;
295 1.1 hpeyerl igmp->igmp_code = 0;
296 1.1 hpeyerl igmp->igmp_group = inm->inm_addr;
297 1.1 hpeyerl igmp->igmp_cksum = 0;
298 1.1 hpeyerl igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
299 1.1 hpeyerl m->m_data -= sizeof(struct ip);
300 1.1 hpeyerl m->m_len += sizeof(struct ip);
301 1.1 hpeyerl
302 1.1 hpeyerl imo = &simo;
303 1.1 hpeyerl bzero((caddr_t)imo, sizeof(*imo));
304 1.1 hpeyerl imo->imo_multicast_ifp = inm->inm_ifp;
305 1.1 hpeyerl imo->imo_multicast_ttl = 1;
306 1.1 hpeyerl /*
307 1.1 hpeyerl * Request loopback of the report if we are acting as a multicast
308 1.1 hpeyerl * router, so that the process-level routing demon can hear it.
309 1.1 hpeyerl */
310 1.1 hpeyerl #ifdef MROUTING
311 1.1 hpeyerl imo->imo_multicast_loop = (ip_mrouter != NULL);
312 1.1 hpeyerl #else
313 1.1 hpeyerl imo->imo_multicast_loop = 0;
314 1.1 hpeyerl #endif
315 1.1 hpeyerl
316 1.1 hpeyerl ip_output(m, NULL, NULL, IP_MULTICASTOPTS, imo);
317 1.1 hpeyerl
318 1.1 hpeyerl ++igmpstat.igps_snd_reports;
319 1.1 hpeyerl }
320 1.1 hpeyerl #endif
321