Tue Aug 4 22:04:23 2009 UTC ()
Use malloc(...|M_ZERO) instead of malloc(...) followed by memset(,0,).


(dyoung)
diff -r1.50 -r1.51 src/sys/netinet6/mld6.c

cvs diff -r1.50 -r1.51 src/sys/netinet6/mld6.c (switch to unified diff)

--- src/sys/netinet6/mld6.c 2009/04/18 14:58:05 1.50
+++ src/sys/netinet6/mld6.c 2009/08/04 22:04:23 1.51
@@ -1,901 +1,898 @@ @@ -1,901 +1,898 @@
1/* $NetBSD: mld6.c,v 1.50 2009/04/18 14:58:05 tsutsui Exp $ */ 1/* $NetBSD: mld6.c,v 1.51 2009/08/04 22:04:23 dyoung Exp $ */
2/* $KAME: mld6.c,v 1.25 2001/01/16 14:14:18 itojun Exp $ */ 2/* $KAME: mld6.c,v 1.25 2001/01/16 14:14:18 itojun Exp $ */
3 3
4/* 4/*
5 * Copyright (C) 1998 WIDE Project. 5 * Copyright (C) 1998 WIDE Project.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors 16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software 17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission. 18 * without specific prior written permission.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE. 30 * SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Copyright (c) 1992, 1993 34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved. 35 * The Regents of the University of California. All rights reserved.
36 * 36 *
37 * This code is derived from software contributed to Berkeley by 37 * This code is derived from software contributed to Berkeley by
38 * Stephen Deering of Stanford University. 38 * Stephen Deering of Stanford University.
39 * 39 *
40 * Redistribution and use in source and binary forms, with or without 40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions 41 * modification, are permitted provided that the following conditions
42 * are met: 42 * are met:
43 * 1. Redistributions of source code must retain the above copyright 43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer. 44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright 45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the 46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution. 47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors 48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software 49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission. 50 * without specific prior written permission.
51 * 51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE. 62 * SUCH DAMAGE.
63 * 63 *
64 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 64 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
65 */ 65 */
66 66
67/* 67/*
68 * Copyright (c) 1988 Stephen Deering. 68 * Copyright (c) 1988 Stephen Deering.
69 * 69 *
70 * This code is derived from software contributed to Berkeley by 70 * This code is derived from software contributed to Berkeley by
71 * Stephen Deering of Stanford University. 71 * Stephen Deering of Stanford University.
72 * 72 *
73 * Redistribution and use in source and binary forms, with or without 73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions 74 * modification, are permitted provided that the following conditions
75 * are met: 75 * are met:
76 * 1. Redistributions of source code must retain the above copyright 76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer. 77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright 78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the 79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution. 80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software 81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement: 82 * must display the following acknowledgement:
83 * This product includes software developed by the University of 83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors. 84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors 85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software 86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission. 87 * without specific prior written permission.
88 * 88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE. 99 * SUCH DAMAGE.
100 * 100 *
101 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 101 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
102 */ 102 */
103 103
104#include <sys/cdefs.h> 104#include <sys/cdefs.h>
105__KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.50 2009/04/18 14:58:05 tsutsui Exp $"); 105__KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.51 2009/08/04 22:04:23 dyoung Exp $");
106 106
107#include "opt_inet.h" 107#include "opt_inet.h"
108 108
109#include <sys/param.h> 109#include <sys/param.h>
110#include <sys/systm.h> 110#include <sys/systm.h>
111#include <sys/mbuf.h> 111#include <sys/mbuf.h>
112#include <sys/socket.h> 112#include <sys/socket.h>
113#include <sys/socketvar.h> 113#include <sys/socketvar.h>
114#include <sys/protosw.h> 114#include <sys/protosw.h>
115#include <sys/syslog.h> 115#include <sys/syslog.h>
116#include <sys/sysctl.h> 116#include <sys/sysctl.h>
117#include <sys/kernel.h> 117#include <sys/kernel.h>
118#include <sys/callout.h> 118#include <sys/callout.h>
119 119
120#include <net/if.h> 120#include <net/if.h>
121 121
122#include <netinet/in.h> 122#include <netinet/in.h>
123#include <netinet/in_var.h> 123#include <netinet/in_var.h>
124#include <netinet6/in6_var.h> 124#include <netinet6/in6_var.h>
125#include <netinet/ip6.h> 125#include <netinet/ip6.h>
126#include <netinet6/ip6_var.h> 126#include <netinet6/ip6_var.h>
127#include <netinet6/scope6_var.h> 127#include <netinet6/scope6_var.h>
128#include <netinet/icmp6.h> 128#include <netinet/icmp6.h>
129#include <netinet6/icmp6_private.h> 129#include <netinet6/icmp6_private.h>
130#include <netinet6/mld6_var.h> 130#include <netinet6/mld6_var.h>
131 131
132#include <net/net_osdep.h> 132#include <net/net_osdep.h>
133 133
134 134
135/* 135/*
136 * This structure is used to keep track of in6_multi chains which belong to 136 * This structure is used to keep track of in6_multi chains which belong to
137 * deleted interface addresses. 137 * deleted interface addresses.
138 */ 138 */
139static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */ 139static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
140 140
141struct multi6_kludge { 141struct multi6_kludge {
142 LIST_ENTRY(multi6_kludge) mk_entry; 142 LIST_ENTRY(multi6_kludge) mk_entry;
143 struct ifnet *mk_ifp; 143 struct ifnet *mk_ifp;
144 struct in6_multihead mk_head; 144 struct in6_multihead mk_head;
145}; 145};
146 146
147 147
148/* 148/*
149 * Protocol constants 149 * Protocol constants
150 */ 150 */
151 151
152/* 152/*
153 * time between repetitions of a node's initial report of interest in a 153 * time between repetitions of a node's initial report of interest in a
154 * multicast address(in seconds) 154 * multicast address(in seconds)
155 */ 155 */
156#define MLD_UNSOLICITED_REPORT_INTERVAL 10 156#define MLD_UNSOLICITED_REPORT_INTERVAL 10
157 157
158static struct ip6_pktopts ip6_opts; 158static struct ip6_pktopts ip6_opts;
159 159
160static void mld_start_listening(struct in6_multi *); 160static void mld_start_listening(struct in6_multi *);
161static void mld_stop_listening(struct in6_multi *); 161static void mld_stop_listening(struct in6_multi *);
162 162
163static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *, 163static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
164 int); 164 int);
165static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *); 165static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
166static void mld_starttimer(struct in6_multi *); 166static void mld_starttimer(struct in6_multi *);
167static void mld_stoptimer(struct in6_multi *); 167static void mld_stoptimer(struct in6_multi *);
168static u_long mld_timerresid(struct in6_multi *); 168static u_long mld_timerresid(struct in6_multi *);
169 169
170void 170void
171mld_init(void) 171mld_init(void)
172{ 172{
173 static u_int8_t hbh_buf[8]; 173 static u_int8_t hbh_buf[8];
174 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf; 174 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
175 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD); 175 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
176 176
177 /* ip6h_nxt will be fill in later */ 177 /* ip6h_nxt will be fill in later */
178 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */ 178 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */
179 179
180 /* XXX: grotty hard coding... */ 180 /* XXX: grotty hard coding... */
181 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */ 181 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
182 hbh_buf[3] = 0; 182 hbh_buf[3] = 0;
183 hbh_buf[4] = IP6OPT_RTALERT; 183 hbh_buf[4] = IP6OPT_RTALERT;
184 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2; 184 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
185 memcpy(&hbh_buf[6], (void *)&rtalert_code, sizeof(u_int16_t)); 185 memcpy(&hbh_buf[6], (void *)&rtalert_code, sizeof(u_int16_t));
186 186
187 ip6_opts.ip6po_hbh = hbh; 187 ip6_opts.ip6po_hbh = hbh;
188 /* We will specify the hoplimit by a multicast option. */ 188 /* We will specify the hoplimit by a multicast option. */
189 ip6_opts.ip6po_hlim = -1; 189 ip6_opts.ip6po_hlim = -1;
190} 190}
191 191
192static void 192static void
193mld_starttimer(struct in6_multi *in6m) 193mld_starttimer(struct in6_multi *in6m)
194{ 194{
195 struct timeval now; 195 struct timeval now;
196 196
197 microtime(&now); 197 microtime(&now);
198 in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz; 198 in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
199 in6m->in6m_timer_expire.tv_usec = now.tv_usec + 199 in6m->in6m_timer_expire.tv_usec = now.tv_usec +
200 (in6m->in6m_timer % hz) * (1000000 / hz); 200 (in6m->in6m_timer % hz) * (1000000 / hz);
201 if (in6m->in6m_timer_expire.tv_usec > 1000000) { 201 if (in6m->in6m_timer_expire.tv_usec > 1000000) {
202 in6m->in6m_timer_expire.tv_sec++; 202 in6m->in6m_timer_expire.tv_sec++;
203 in6m->in6m_timer_expire.tv_usec -= 1000000; 203 in6m->in6m_timer_expire.tv_usec -= 1000000;
204 } 204 }
205 205
206 /* start or restart the timer */ 206 /* start or restart the timer */
207 callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer); 207 callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer);
208} 208}
209 209
210static void 210static void
211mld_stoptimer(struct in6_multi *in6m) 211mld_stoptimer(struct in6_multi *in6m)
212{ 212{
213 if (in6m->in6m_timer == IN6M_TIMER_UNDEF) 213 if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
214 return; 214 return;
215 215
216 callout_stop(&in6m->in6m_timer_ch); 216 callout_stop(&in6m->in6m_timer_ch);
217 217
218 in6m->in6m_timer = IN6M_TIMER_UNDEF; 218 in6m->in6m_timer = IN6M_TIMER_UNDEF;
219} 219}
220 220
221static void 221static void
222mld_timeo(void *arg) 222mld_timeo(void *arg)
223{ 223{
224 struct in6_multi *in6m = arg; 224 struct in6_multi *in6m = arg;
225 225
226 mutex_enter(softnet_lock); 226 mutex_enter(softnet_lock);
227 KERNEL_LOCK(1, NULL); 227 KERNEL_LOCK(1, NULL);
228 228
229 in6m->in6m_timer = IN6M_TIMER_UNDEF; 229 in6m->in6m_timer = IN6M_TIMER_UNDEF;
230 230
231 switch (in6m->in6m_state) { 231 switch (in6m->in6m_state) {
232 case MLD_REPORTPENDING: 232 case MLD_REPORTPENDING:
233 mld_start_listening(in6m); 233 mld_start_listening(in6m);
234 break; 234 break;
235 default: 235 default:
236 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 236 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
237 break; 237 break;
238 } 238 }
239 239
240 KERNEL_UNLOCK_ONE(NULL); 240 KERNEL_UNLOCK_ONE(NULL);
241 mutex_exit(softnet_lock); 241 mutex_exit(softnet_lock);
242} 242}
243 243
244static u_long 244static u_long
245mld_timerresid(struct in6_multi *in6m) 245mld_timerresid(struct in6_multi *in6m)
246{ 246{
247 struct timeval now, diff; 247 struct timeval now, diff;
248 248
249 microtime(&now); 249 microtime(&now);
250 250
251 if (now.tv_sec > in6m->in6m_timer_expire.tv_sec || 251 if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
252 (now.tv_sec == in6m->in6m_timer_expire.tv_sec && 252 (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
253 now.tv_usec > in6m->in6m_timer_expire.tv_usec)) { 253 now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
254 return (0); 254 return (0);
255 } 255 }
256 diff = in6m->in6m_timer_expire; 256 diff = in6m->in6m_timer_expire;
257 diff.tv_sec -= now.tv_sec; 257 diff.tv_sec -= now.tv_sec;
258 diff.tv_usec -= now.tv_usec; 258 diff.tv_usec -= now.tv_usec;
259 if (diff.tv_usec < 0) { 259 if (diff.tv_usec < 0) {
260 diff.tv_sec--; 260 diff.tv_sec--;
261 diff.tv_usec += 1000000; 261 diff.tv_usec += 1000000;
262 } 262 }
263 263
264 /* return the remaining time in milliseconds */ 264 /* return the remaining time in milliseconds */
265 return diff.tv_sec * 1000 + diff.tv_usec / 1000; 265 return diff.tv_sec * 1000 + diff.tv_usec / 1000;
266} 266}
267 267
268static void 268static void
269mld_start_listening(struct in6_multi *in6m) 269mld_start_listening(struct in6_multi *in6m)
270{ 270{
271 struct in6_addr all_in6; 271 struct in6_addr all_in6;
272 272
273 /* 273 /*
274 * RFC2710 page 10: 274 * RFC2710 page 10:
275 * The node never sends a Report or Done for the link-scope all-nodes 275 * The node never sends a Report or Done for the link-scope all-nodes
276 * address. 276 * address.
277 * MLD messages are never sent for multicast addresses whose scope is 0 277 * MLD messages are never sent for multicast addresses whose scope is 0
278 * (reserved) or 1 (node-local). 278 * (reserved) or 1 (node-local).
279 */ 279 */
280 all_in6 = in6addr_linklocal_allnodes; 280 all_in6 = in6addr_linklocal_allnodes;
281 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) { 281 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
282 /* XXX: this should not happen! */ 282 /* XXX: this should not happen! */
283 in6m->in6m_timer = 0; 283 in6m->in6m_timer = 0;
284 in6m->in6m_state = MLD_OTHERLISTENER; 284 in6m->in6m_state = MLD_OTHERLISTENER;
285 } 285 }
286 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) || 286 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
287 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) { 287 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
288 in6m->in6m_timer = IN6M_TIMER_UNDEF; 288 in6m->in6m_timer = IN6M_TIMER_UNDEF;
289 in6m->in6m_state = MLD_OTHERLISTENER; 289 in6m->in6m_state = MLD_OTHERLISTENER;
290 } else { 290 } else {
291 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 291 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
292 in6m->in6m_timer = arc4random() % 292 in6m->in6m_timer = arc4random() %
293 (MLD_UNSOLICITED_REPORT_INTERVAL * hz); 293 (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
294 in6m->in6m_state = MLD_IREPORTEDLAST; 294 in6m->in6m_state = MLD_IREPORTEDLAST;
295 295
296 mld_starttimer(in6m); 296 mld_starttimer(in6m);
297 } 297 }
298} 298}
299 299
300static void 300static void
301mld_stop_listening(struct in6_multi *in6m) 301mld_stop_listening(struct in6_multi *in6m)
302{ 302{
303 struct in6_addr allnode, allrouter; 303 struct in6_addr allnode, allrouter;
304 304
305 allnode = in6addr_linklocal_allnodes; 305 allnode = in6addr_linklocal_allnodes;
306 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) { 306 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
307 /* XXX: this should not happen! */ 307 /* XXX: this should not happen! */
308 return; 308 return;
309 } 309 }
310 allrouter = in6addr_linklocal_allrouters; 310 allrouter = in6addr_linklocal_allrouters;
311 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) { 311 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
312 /* XXX impossible */ 312 /* XXX impossible */
313 return; 313 return;
314 } 314 }
315 315
316 if (in6m->in6m_state == MLD_IREPORTEDLAST && 316 if (in6m->in6m_state == MLD_IREPORTEDLAST &&
317 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) && 317 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
318 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > 318 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
319 IPV6_ADDR_SCOPE_INTFACELOCAL) { 319 IPV6_ADDR_SCOPE_INTFACELOCAL) {
320 mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter); 320 mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
321 } 321 }
322} 322}
323 323
324void 324void
325mld_input(struct mbuf *m, int off) 325mld_input(struct mbuf *m, int off)
326{ 326{
327 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 327 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
328 struct mld_hdr *mldh; 328 struct mld_hdr *mldh;
329 struct ifnet *ifp = m->m_pkthdr.rcvif; 329 struct ifnet *ifp = m->m_pkthdr.rcvif;
330 struct in6_multi *in6m = NULL; 330 struct in6_multi *in6m = NULL;
331 struct in6_addr mld_addr, all_in6; 331 struct in6_addr mld_addr, all_in6;
332 struct in6_ifaddr *ia; 332 struct in6_ifaddr *ia;
333 u_long timer = 0; /* timer value in the MLD query header */ 333 u_long timer = 0; /* timer value in the MLD query header */
334 334
335 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh)); 335 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
336 if (mldh == NULL) { 336 if (mldh == NULL) {
337 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 337 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
338 return; 338 return;
339 } 339 }
340 340
341 /* source address validation */ 341 /* source address validation */
342 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */ 342 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
343 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) { 343 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
344 /* 344 /*
345 * RFC3590 allows the IPv6 unspecified address as the source 345 * RFC3590 allows the IPv6 unspecified address as the source
346 * address of MLD report and done messages. However, as this 346 * address of MLD report and done messages. However, as this
347 * same document says, this special rule is for snooping 347 * same document says, this special rule is for snooping
348 * switches and the RFC requires routers to discard MLD packets 348 * switches and the RFC requires routers to discard MLD packets
349 * with the unspecified source address. The RFC only talks 349 * with the unspecified source address. The RFC only talks
350 * about hosts receiving an MLD query or report in Security 350 * about hosts receiving an MLD query or report in Security
351 * Considerations, but this is probably the correct intention. 351 * Considerations, but this is probably the correct intention.
352 * RFC3590 does not talk about other cases than link-local and 352 * RFC3590 does not talk about other cases than link-local and
353 * the unspecified source addresses, but we believe the same 353 * the unspecified source addresses, but we believe the same
354 * rule should be applied. 354 * rule should be applied.
355 * As a result, we only allow link-local addresses as the 355 * As a result, we only allow link-local addresses as the
356 * source address; otherwise, simply discard the packet. 356 * source address; otherwise, simply discard the packet.
357 */ 357 */
358#if 0 358#if 0
359 /* 359 /*
360 * XXX: do not log in an input path to avoid log flooding, 360 * XXX: do not log in an input path to avoid log flooding,
361 * though RFC3590 says "SHOULD log" if the source of a query 361 * though RFC3590 says "SHOULD log" if the source of a query
362 * is the unspecified address. 362 * is the unspecified address.
363 */ 363 */
364 log(LOG_INFO, 364 log(LOG_INFO,
365 "mld_input: src %s is not link-local (grp=%s)\n", 365 "mld_input: src %s is not link-local (grp=%s)\n",
366 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr)); 366 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
367#endif 367#endif
368 m_freem(m); 368 m_freem(m);
369 return; 369 return;
370 } 370 }
371 371
372 /* 372 /*
373 * make a copy for local work (in6_setscope() may modify the 1st arg) 373 * make a copy for local work (in6_setscope() may modify the 1st arg)
374 */ 374 */
375 mld_addr = mldh->mld_addr; 375 mld_addr = mldh->mld_addr;
376 if (in6_setscope(&mld_addr, ifp, NULL)) { 376 if (in6_setscope(&mld_addr, ifp, NULL)) {
377 /* XXX: this should not happen! */ 377 /* XXX: this should not happen! */
378 m_free(m); 378 m_free(m);
379 return; 379 return;
380 } 380 }
381 381
382 /* 382 /*
383 * In the MLD specification, there are 3 states and a flag. 383 * In the MLD specification, there are 3 states and a flag.
384 * 384 *
385 * In Non-Listener state, we simply don't have a membership record. 385 * In Non-Listener state, we simply don't have a membership record.
386 * In Delaying Listener state, our timer is running (in6m->in6m_timer) 386 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
387 * In Idle Listener state, our timer is not running  387 * In Idle Listener state, our timer is not running
388 * (in6m->in6m_timer==IN6M_TIMER_UNDEF) 388 * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
389 * 389 *
390 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if 390 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
391 * we have heard a report from another member, or MLD_IREPORTEDLAST 391 * we have heard a report from another member, or MLD_IREPORTEDLAST
392 * if we sent the last report. 392 * if we sent the last report.
393 */ 393 */
394 switch (mldh->mld_type) { 394 switch (mldh->mld_type) {
395 case MLD_LISTENER_QUERY: 395 case MLD_LISTENER_QUERY:
396 if (ifp->if_flags & IFF_LOOPBACK) 396 if (ifp->if_flags & IFF_LOOPBACK)
397 break; 397 break;
398 398
399 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) && 399 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
400 !IN6_IS_ADDR_MULTICAST(&mld_addr)) 400 !IN6_IS_ADDR_MULTICAST(&mld_addr))
401 break; /* print error or log stat? */ 401 break; /* print error or log stat? */
402 402
403 all_in6 = in6addr_linklocal_allnodes; 403 all_in6 = in6addr_linklocal_allnodes;
404 if (in6_setscope(&all_in6, ifp, NULL)) { 404 if (in6_setscope(&all_in6, ifp, NULL)) {
405 /* XXX: this should not happen! */ 405 /* XXX: this should not happen! */
406 break; 406 break;
407 } 407 }
408 408
409 /* 409 /*
410 * - Start the timers in all of our membership records 410 * - Start the timers in all of our membership records
411 * that the query applies to for the interface on 411 * that the query applies to for the interface on
412 * which the query arrived excl. those that belong 412 * which the query arrived excl. those that belong
413 * to the "all-nodes" group (ff02::1). 413 * to the "all-nodes" group (ff02::1).
414 * - Restart any timer that is already running but has 414 * - Restart any timer that is already running but has
415 * a value longer than the requested timeout. 415 * a value longer than the requested timeout.
416 * - Use the value specified in the query message as 416 * - Use the value specified in the query message as
417 * the maximum timeout. 417 * the maximum timeout.
418 */ 418 */
419 timer = ntohs(mldh->mld_maxdelay); 419 timer = ntohs(mldh->mld_maxdelay);
420 420
421 IFP_TO_IA6(ifp, ia); 421 IFP_TO_IA6(ifp, ia);
422 if (ia == NULL) 422 if (ia == NULL)
423 break; 423 break;
424 424
425 LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) { 425 LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
426 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) || 426 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
427 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < 427 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
428 IPV6_ADDR_SCOPE_LINKLOCAL) 428 IPV6_ADDR_SCOPE_LINKLOCAL)
429 continue; 429 continue;
430 430
431 if (in6m->in6m_state == MLD_REPORTPENDING) 431 if (in6m->in6m_state == MLD_REPORTPENDING)
432 continue; /* we are not yet ready */ 432 continue; /* we are not yet ready */
433 433
434 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) && 434 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
435 !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr)) 435 !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
436 continue; 436 continue;
437 437
438 if (timer == 0) { 438 if (timer == 0) {
439 /* send a report immediately */ 439 /* send a report immediately */
440 mld_stoptimer(in6m); 440 mld_stoptimer(in6m);
441 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL); 441 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
442 in6m->in6m_state = MLD_IREPORTEDLAST; 442 in6m->in6m_state = MLD_IREPORTEDLAST;
443 } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF || 443 } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
444 mld_timerresid(in6m) > timer) { 444 mld_timerresid(in6m) > timer) {
445 in6m->in6m_timer = 445 in6m->in6m_timer =
446 1 + (arc4random() % timer) * hz / 1000; 446 1 + (arc4random() % timer) * hz / 1000;
447 mld_starttimer(in6m); 447 mld_starttimer(in6m);
448 } 448 }
449 } 449 }
450 break; 450 break;
451 451
452 case MLD_LISTENER_REPORT: 452 case MLD_LISTENER_REPORT:
453 /* 453 /*
454 * For fast leave to work, we have to know that we are the 454 * For fast leave to work, we have to know that we are the
455 * last person to send a report for this group. Reports 455 * last person to send a report for this group. Reports
456 * can potentially get looped back if we are a multicast 456 * can potentially get looped back if we are a multicast
457 * router, so discard reports sourced by me. 457 * router, so discard reports sourced by me.
458 * Note that it is impossible to check IFF_LOOPBACK flag of 458 * Note that it is impossible to check IFF_LOOPBACK flag of
459 * ifp for this purpose, since ip6_mloopback pass the physical 459 * ifp for this purpose, since ip6_mloopback pass the physical
460 * interface to looutput. 460 * interface to looutput.
461 */ 461 */
462 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */ 462 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
463 break; 463 break;
464 464
465 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr)) 465 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
466 break; 466 break;
467 467
468 /* 468 /*
469 * If we belong to the group being reported, stop 469 * If we belong to the group being reported, stop
470 * our timer for that group. 470 * our timer for that group.
471 */ 471 */
472 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m); 472 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
473 if (in6m) { 473 if (in6m) {
474 mld_stoptimer(in6m); /* transit to idle state */ 474 mld_stoptimer(in6m); /* transit to idle state */
475 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */ 475 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
476 } 476 }
477 break; 477 break;
478 default: /* this is impossible */ 478 default: /* this is impossible */
479#if 0 479#if 0
480 /* 480 /*
481 * this case should be impossible because of filtering in 481 * this case should be impossible because of filtering in
482 * icmp6_input(). But we explicitly disabled this part 482 * icmp6_input(). But we explicitly disabled this part
483 * just in case. 483 * just in case.
484 */ 484 */
485 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type); 485 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
486#endif 486#endif
487 break; 487 break;
488 } 488 }
489 489
490 m_freem(m); 490 m_freem(m);
491} 491}
492 492
493static void 493static void
494mld_sendpkt(struct in6_multi *in6m, int type,  494mld_sendpkt(struct in6_multi *in6m, int type,
495 const struct in6_addr *dst) 495 const struct in6_addr *dst)
496{ 496{
497 struct mbuf *mh; 497 struct mbuf *mh;
498 struct mld_hdr *mldh; 498 struct mld_hdr *mldh;
499 struct ip6_hdr *ip6 = NULL; 499 struct ip6_hdr *ip6 = NULL;
500 struct ip6_moptions im6o; 500 struct ip6_moptions im6o;
501 struct in6_ifaddr *ia = NULL; 501 struct in6_ifaddr *ia = NULL;
502 struct ifnet *ifp = in6m->in6m_ifp; 502 struct ifnet *ifp = in6m->in6m_ifp;
503 int ignflags; 503 int ignflags;
504 504
505 /* 505 /*
506 * At first, find a link local address on the outgoing interface 506 * At first, find a link local address on the outgoing interface
507 * to use as the source address of the MLD packet. 507 * to use as the source address of the MLD packet.
508 * We do not reject tentative addresses for MLD report to deal with 508 * We do not reject tentative addresses for MLD report to deal with
509 * the case where we first join a link-local address. 509 * the case where we first join a link-local address.
510 */ 510 */
511 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE; 511 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
512 if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL) 512 if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
513 return; 513 return;
514 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) 514 if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
515 ia = NULL; 515 ia = NULL;
516 516
517 /* Allocate two mbufs to store IPv6 header and MLD header */ 517 /* Allocate two mbufs to store IPv6 header and MLD header */
518 mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type); 518 mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
519 if (mldh == NULL) 519 if (mldh == NULL)
520 return; 520 return;
521 521
522 /* fill src/dst here */ 522 /* fill src/dst here */
523 ip6 = mtod(mh, struct ip6_hdr *); 523 ip6 = mtod(mh, struct ip6_hdr *);
524 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any; 524 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
525 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr; 525 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
526 526
527 mldh->mld_addr = in6m->in6m_addr; 527 mldh->mld_addr = in6m->in6m_addr;
528 in6_clearscope(&mldh->mld_addr); /* XXX */ 528 in6_clearscope(&mldh->mld_addr); /* XXX */
529 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), 529 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
530 sizeof(struct mld_hdr)); 530 sizeof(struct mld_hdr));
531 531
532 /* construct multicast option */ 532 /* construct multicast option */
533 memset(&im6o, 0, sizeof(im6o)); 533 memset(&im6o, 0, sizeof(im6o));
534 im6o.im6o_multicast_ifp = ifp; 534 im6o.im6o_multicast_ifp = ifp;
535 im6o.im6o_multicast_hlim = 1; 535 im6o.im6o_multicast_hlim = 1;
536 536
537 /* 537 /*
538 * Request loopback of the report if we are acting as a multicast 538 * Request loopback of the report if we are acting as a multicast
539 * router, so that the process-level routing daemon can hear it. 539 * router, so that the process-level routing daemon can hear it.
540 */ 540 */
541 im6o.im6o_multicast_loop = (ip6_mrouter != NULL); 541 im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
542 542
543 /* increment output statictics */ 543 /* increment output statictics */
544 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type); 544 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
545 icmp6_ifstat_inc(ifp, ifs6_out_msg); 545 icmp6_ifstat_inc(ifp, ifs6_out_msg);
546 switch (type) { 546 switch (type) {
547 case MLD_LISTENER_QUERY: 547 case MLD_LISTENER_QUERY:
548 icmp6_ifstat_inc(ifp, ifs6_out_mldquery); 548 icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
549 break; 549 break;
550 case MLD_LISTENER_REPORT: 550 case MLD_LISTENER_REPORT:
551 icmp6_ifstat_inc(ifp, ifs6_out_mldreport); 551 icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
552 break; 552 break;
553 case MLD_LISTENER_DONE: 553 case MLD_LISTENER_DONE:
554 icmp6_ifstat_inc(ifp, ifs6_out_mlddone); 554 icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
555 break; 555 break;
556 } 556 }
557 557
558 ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC, 558 ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
559 &im6o, (struct socket *)NULL, NULL); 559 &im6o, (struct socket *)NULL, NULL);
560} 560}
561 561
562static struct mld_hdr * 562static struct mld_hdr *
563mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m, 563mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m,
564 int type) 564 int type)
565{ 565{
566 struct mbuf *md; 566 struct mbuf *md;
567 struct mld_hdr *mldh; 567 struct mld_hdr *mldh;
568 struct ip6_hdr *ip6; 568 struct ip6_hdr *ip6;
569 569
570 /* 570 /*
571 * Allocate mbufs to store ip6 header and MLD header. 571 * Allocate mbufs to store ip6 header and MLD header.
572 * We allocate 2 mbufs and make chain in advance because 572 * We allocate 2 mbufs and make chain in advance because
573 * it is more convenient when inserting the hop-by-hop option later. 573 * it is more convenient when inserting the hop-by-hop option later.
574 */ 574 */
575 MGETHDR(*mh, M_DONTWAIT, MT_HEADER); 575 MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
576 if (*mh == NULL) 576 if (*mh == NULL)
577 return NULL; 577 return NULL;
578 MGET(md, M_DONTWAIT, MT_DATA); 578 MGET(md, M_DONTWAIT, MT_DATA);
579 if (md == NULL) { 579 if (md == NULL) {
580 m_free(*mh); 580 m_free(*mh);
581 *mh = NULL; 581 *mh = NULL;
582 return NULL; 582 return NULL;
583 } 583 }
584 (*mh)->m_next = md; 584 (*mh)->m_next = md;
585 md->m_next = NULL; 585 md->m_next = NULL;
586 586
587 (*mh)->m_pkthdr.rcvif = NULL; 587 (*mh)->m_pkthdr.rcvif = NULL;
588 (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len; 588 (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
589 (*mh)->m_len = sizeof(struct ip6_hdr); 589 (*mh)->m_len = sizeof(struct ip6_hdr);
590 MH_ALIGN(*mh, sizeof(struct ip6_hdr)); 590 MH_ALIGN(*mh, sizeof(struct ip6_hdr));
591 591
592 /* fill in the ip6 header */ 592 /* fill in the ip6 header */
593 ip6 = mtod(*mh, struct ip6_hdr *); 593 ip6 = mtod(*mh, struct ip6_hdr *);
594 memset(ip6, 0, sizeof(*ip6)); 594 memset(ip6, 0, sizeof(*ip6));
595 ip6->ip6_flow = 0; 595 ip6->ip6_flow = 0;
596 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 596 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
597 ip6->ip6_vfc |= IPV6_VERSION; 597 ip6->ip6_vfc |= IPV6_VERSION;
598 /* ip6_plen will be set later */ 598 /* ip6_plen will be set later */
599 ip6->ip6_nxt = IPPROTO_ICMPV6; 599 ip6->ip6_nxt = IPPROTO_ICMPV6;
600 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */ 600 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
601 /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */ 601 /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
602 602
603 /* fill in the MLD header as much as possible */ 603 /* fill in the MLD header as much as possible */
604 md->m_len = len; 604 md->m_len = len;
605 mldh = mtod(md, struct mld_hdr *); 605 mldh = mtod(md, struct mld_hdr *);
606 memset(mldh, 0, len); 606 memset(mldh, 0, len);
607 mldh->mld_type = type; 607 mldh->mld_type = type;
608 return mldh; 608 return mldh;
609} 609}
610 610
611/* 611/*
612 * Add an address to the list of IP6 multicast addresses for a given interface. 612 * Add an address to the list of IP6 multicast addresses for a given interface.
613 */ 613 */
614struct in6_multi * 614struct in6_multi *
615in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,  615in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
616 int *errorp, int timer) 616 int *errorp, int timer)
617{ 617{
618 struct in6_ifaddr *ia; 618 struct in6_ifaddr *ia;
619 struct in6_ifreq ifr; 619 struct in6_ifreq ifr;
620 struct in6_multi *in6m; 620 struct in6_multi *in6m;
621 int s = splsoftnet(); 621 int s = splsoftnet();
622 622
623 *errorp = 0; 623 *errorp = 0;
624 624
625 /* 625 /*
626 * See if address already in list. 626 * See if address already in list.
627 */ 627 */
628 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m); 628 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
629 if (in6m != NULL) { 629 if (in6m != NULL) {
630 /* 630 /*
631 * Found it; just increment the refrence count. 631 * Found it; just increment the refrence count.
632 */ 632 */
633 in6m->in6m_refcount++; 633 in6m->in6m_refcount++;
634 } else { 634 } else {
635 /* 635 /*
636 * New address; allocate a new multicast record 636 * New address; allocate a new multicast record
637 * and link it into the interface's multicast list. 637 * and link it into the interface's multicast list.
638 */ 638 */
639 in6m = (struct in6_multi *) 639 in6m = (struct in6_multi *)
640 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT); 640 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT|M_ZERO);
641 if (in6m == NULL) { 641 if (in6m == NULL) {
642 splx(s); 642 splx(s);
643 *errorp = ENOBUFS; 643 *errorp = ENOBUFS;
644 return (NULL); 644 return (NULL);
645 } 645 }
646 646
647 memset(in6m, 0, sizeof(*in6m)); 
648 in6m->in6m_addr = *maddr6; 647 in6m->in6m_addr = *maddr6;
649 in6m->in6m_ifp = ifp; 648 in6m->in6m_ifp = ifp;
650 in6m->in6m_refcount = 1; 649 in6m->in6m_refcount = 1;
651 in6m->in6m_timer = IN6M_TIMER_UNDEF; 650 in6m->in6m_timer = IN6M_TIMER_UNDEF;
652 IFP_TO_IA6(ifp, ia); 651 IFP_TO_IA6(ifp, ia);
653 if (ia == NULL) { 652 if (ia == NULL) {
654 free(in6m, M_IPMADDR); 653 free(in6m, M_IPMADDR);
655 splx(s); 654 splx(s);
656 *errorp = EADDRNOTAVAIL; /* appropriate? */ 655 *errorp = EADDRNOTAVAIL; /* appropriate? */
657 return (NULL); 656 return (NULL);
658 } 657 }
659 in6m->in6m_ia = ia; 658 in6m->in6m_ia = ia;
660 IFAREF(&ia->ia_ifa); /* gain a reference */ 659 IFAREF(&ia->ia_ifa); /* gain a reference */
661 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 660 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
662 661
663 /* 662 /*
664 * Ask the network driver to update its multicast reception 663 * Ask the network driver to update its multicast reception
665 * filter appropriately for the new address. 664 * filter appropriately for the new address.
666 */ 665 */
667 sockaddr_in6_init(&ifr.ifr_addr, maddr6, 0, 0, 0); 666 sockaddr_in6_init(&ifr.ifr_addr, maddr6, 0, 0, 0);
668 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, &ifr); 667 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, &ifr);
669 if (*errorp) { 668 if (*errorp) {
670 LIST_REMOVE(in6m, in6m_entry); 669 LIST_REMOVE(in6m, in6m_entry);
671 free(in6m, M_IPMADDR); 670 free(in6m, M_IPMADDR);
672 IFAFREE(&ia->ia_ifa); 671 IFAFREE(&ia->ia_ifa);
673 splx(s); 672 splx(s);
674 return (NULL); 673 return (NULL);
675 } 674 }
676 675
677 callout_init(&in6m->in6m_timer_ch, CALLOUT_MPSAFE); 676 callout_init(&in6m->in6m_timer_ch, CALLOUT_MPSAFE);
678 callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m); 677 callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m);
679 in6m->in6m_timer = timer; 678 in6m->in6m_timer = timer;
680 if (in6m->in6m_timer > 0) { 679 if (in6m->in6m_timer > 0) {
681 in6m->in6m_state = MLD_REPORTPENDING; 680 in6m->in6m_state = MLD_REPORTPENDING;
682 mld_starttimer(in6m); 681 mld_starttimer(in6m);
683 682
684 splx(s); 683 splx(s);
685 return (in6m); 684 return (in6m);
686 } 685 }
687 686
688 /* 687 /*
689 * Let MLD6 know that we have joined a new IP6 multicast 688 * Let MLD6 know that we have joined a new IP6 multicast
690 * group. 689 * group.
691 */ 690 */
692 mld_start_listening(in6m); 691 mld_start_listening(in6m);
693 } 692 }
694 splx(s); 693 splx(s);
695 return (in6m); 694 return (in6m);
696} 695}
697 696
698/* 697/*
699 * Delete a multicast address record. 698 * Delete a multicast address record.
700 */ 699 */
701void 700void
702in6_delmulti(struct in6_multi *in6m) 701in6_delmulti(struct in6_multi *in6m)
703{ 702{
704 struct in6_ifreq ifr; 703 struct in6_ifreq ifr;
705 struct in6_ifaddr *ia; 704 struct in6_ifaddr *ia;
706 int s = splsoftnet(); 705 int s = splsoftnet();
707 706
708 mld_stoptimer(in6m); 707 mld_stoptimer(in6m);
709 708
710 if (--in6m->in6m_refcount == 0) { 709 if (--in6m->in6m_refcount == 0) {
711 /* 710 /*
712 * No remaining claims to this record; let MLD6 know 711 * No remaining claims to this record; let MLD6 know
713 * that we are leaving the multicast group. 712 * that we are leaving the multicast group.
714 */ 713 */
715 mld_stop_listening(in6m); 714 mld_stop_listening(in6m);
716 715
717 /* 716 /*
718 * Unlink from list. 717 * Unlink from list.
719 */ 718 */
720 LIST_REMOVE(in6m, in6m_entry); 719 LIST_REMOVE(in6m, in6m_entry);
721 if (in6m->in6m_ia != NULL) { 720 if (in6m->in6m_ia != NULL) {
722 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ 721 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
723 in6m->in6m_ia = NULL; 722 in6m->in6m_ia = NULL;
724 } 723 }
725 724
726 /* 725 /*
727 * Delete all references of this multicasting group from 726 * Delete all references of this multicasting group from
728 * the membership arrays 727 * the membership arrays
729 */ 728 */
730 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 729 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
731 struct in6_multi_mship *imm; 730 struct in6_multi_mship *imm;
732 LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) { 731 LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) {
733 if (imm->i6mm_maddr == in6m) 732 if (imm->i6mm_maddr == in6m)
734 imm->i6mm_maddr = NULL; 733 imm->i6mm_maddr = NULL;
735 } 734 }
736 } 735 }
737 736
738 /* 737 /*
739 * Notify the network driver to update its multicast 738 * Notify the network driver to update its multicast
740 * reception filter. 739 * reception filter.
741 */ 740 */
742 sockaddr_in6_init(&ifr.ifr_addr, &in6m->in6m_addr, 0, 0, 0); 741 sockaddr_in6_init(&ifr.ifr_addr, &in6m->in6m_addr, 0, 0, 0);
743 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp, SIOCDELMULTI, &ifr); 742 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp, SIOCDELMULTI, &ifr);
744 callout_destroy(&in6m->in6m_timer_ch); 743 callout_destroy(&in6m->in6m_timer_ch);
745 free(in6m, M_IPMADDR); 744 free(in6m, M_IPMADDR);
746 } 745 }
747 splx(s); 746 splx(s);
748} 747}
749 748
750 749
751struct in6_multi_mship * 750struct in6_multi_mship *
752in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,  751in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
753 int *errorp, int timer) 752 int *errorp, int timer)
754{ 753{
755 struct in6_multi_mship *imm; 754 struct in6_multi_mship *imm;
756 755
757 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT); 756 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT|M_ZERO);
758 if (!imm) { 757 if (imm == NULL) {
759 *errorp = ENOBUFS; 758 *errorp = ENOBUFS;
760 return NULL; 759 return NULL;
761 } 760 }
762 761
763 memset(imm, 0, sizeof(*imm)); 
764 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer); 762 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
765 if (!imm->i6mm_maddr) { 763 if (!imm->i6mm_maddr) {
766 /* *errorp is already set */ 764 /* *errorp is already set */
767 free(imm, M_IPMADDR); 765 free(imm, M_IPMADDR);
768 return NULL; 766 return NULL;
769 } 767 }
770 return imm; 768 return imm;
771} 769}
772 770
773int 771int
774in6_leavegroup(struct in6_multi_mship *imm) 772in6_leavegroup(struct in6_multi_mship *imm)
775{ 773{
776 774
777 if (imm->i6mm_maddr) { 775 if (imm->i6mm_maddr) {
778 in6_delmulti(imm->i6mm_maddr); 776 in6_delmulti(imm->i6mm_maddr);
779 } 777 }
780 free(imm, M_IPMADDR); 778 free(imm, M_IPMADDR);
781 return 0; 779 return 0;
782} 780}
783 781
784 782
785/* 783/*
786 * Multicast address kludge: 784 * Multicast address kludge:
787 * If there were any multicast addresses attached to this interface address, 785 * If there were any multicast addresses attached to this interface address,
788 * either move them to another address on this interface, or save them until 786 * either move them to another address on this interface, or save them until
789 * such time as this interface is reconfigured for IPv6. 787 * such time as this interface is reconfigured for IPv6.
790 */ 788 */
791void 789void
792in6_savemkludge(struct in6_ifaddr *oia) 790in6_savemkludge(struct in6_ifaddr *oia)
793{ 791{
794 struct in6_ifaddr *ia; 792 struct in6_ifaddr *ia;
795 struct in6_multi *in6m; 793 struct in6_multi *in6m;
796 794
797 IFP_TO_IA6(oia->ia_ifp, ia); 795 IFP_TO_IA6(oia->ia_ifp, ia);
798 if (ia) { /* there is another address */ 796 if (ia) { /* there is another address */
799 KASSERT(ia != oia); 797 KASSERT(ia != oia);
800 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) { 798 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
801 LIST_REMOVE(in6m, in6m_entry); 799 LIST_REMOVE(in6m, in6m_entry);
802 IFAREF(&ia->ia_ifa); 800 IFAREF(&ia->ia_ifa);
803 IFAFREE(&in6m->in6m_ia->ia_ifa); 801 IFAFREE(&in6m->in6m_ia->ia_ifa);
804 in6m->in6m_ia = ia; 802 in6m->in6m_ia = ia;
805 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 803 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
806 } 804 }
807 } else { /* last address on this if deleted, save */ 805 } else { /* last address on this if deleted, save */
808 struct multi6_kludge *mk; 806 struct multi6_kludge *mk;
809 807
810 LIST_FOREACH(mk, &in6_mk, mk_entry) { 808 LIST_FOREACH(mk, &in6_mk, mk_entry) {
811 if (mk->mk_ifp == oia->ia_ifp) 809 if (mk->mk_ifp == oia->ia_ifp)
812 break; 810 break;
813 } 811 }
814 if (mk == NULL) /* this should not happen! */ 812 if (mk == NULL) /* this should not happen! */
815 panic("in6_savemkludge: no kludge space"); 813 panic("in6_savemkludge: no kludge space");
816 814
817 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) { 815 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
818 LIST_REMOVE(in6m, in6m_entry); 816 LIST_REMOVE(in6m, in6m_entry);
819 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ 817 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
820 in6m->in6m_ia = NULL; 818 in6m->in6m_ia = NULL;
821 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry); 819 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
822 } 820 }
823 } 821 }
824} 822}
825 823
826/* 824/*
827 * Continuation of multicast address hack: 825 * Continuation of multicast address hack:
828 * If there was a multicast group list previously saved for this interface, 826 * If there was a multicast group list previously saved for this interface,
829 * then we re-attach it to the first address configured on the i/f. 827 * then we re-attach it to the first address configured on the i/f.
830 */ 828 */
831void 829void
832in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp) 830in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp)
833{ 831{
834 struct multi6_kludge *mk; 832 struct multi6_kludge *mk;
835 struct in6_multi *in6m; 833 struct in6_multi *in6m;
836 834
837 LIST_FOREACH(mk, &in6_mk, mk_entry) { 835 LIST_FOREACH(mk, &in6_mk, mk_entry) {
838 if (mk->mk_ifp == ifp) 836 if (mk->mk_ifp == ifp)
839 break; 837 break;
840 } 838 }
841 if (mk == NULL) 839 if (mk == NULL)
842 return; 840 return;
843 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) { 841 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
844 LIST_REMOVE(in6m, in6m_entry); 842 LIST_REMOVE(in6m, in6m_entry);
845 in6m->in6m_ia = ia; 843 in6m->in6m_ia = ia;
846 IFAREF(&ia->ia_ifa); 844 IFAREF(&ia->ia_ifa);
847 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 845 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
848 } 846 }
849} 847}
850 848
851/* 849/*
852 * Allocate space for the kludge at interface initialization time. 850 * Allocate space for the kludge at interface initialization time.
853 * Formerly, we dynamically allocated the space in in6_savemkludge() with 851 * Formerly, we dynamically allocated the space in in6_savemkludge() with
854 * malloc(M_WAITOK). However, it was wrong since the function could be called 852 * malloc(M_WAITOK). However, it was wrong since the function could be called
855 * under an interrupt context (software timer on address lifetime expiration). 853 * under an interrupt context (software timer on address lifetime expiration).
856 * Also, we cannot just give up allocating the strucutre, since the group 854 * Also, we cannot just give up allocating the strucutre, since the group
857 * membership structure is very complex and we need to keep it anyway. 855 * membership structure is very complex and we need to keep it anyway.
858 * Of course, this function MUST NOT be called under an interrupt context. 856 * Of course, this function MUST NOT be called under an interrupt context.
859 * Specifically, it is expected to be called only from in6_ifattach(), though 857 * Specifically, it is expected to be called only from in6_ifattach(), though
860 * it is a global function. 858 * it is a global function.
861 */ 859 */
862void 860void
863in6_createmkludge(struct ifnet *ifp) 861in6_createmkludge(struct ifnet *ifp)
864{ 862{
865 struct multi6_kludge *mk; 863 struct multi6_kludge *mk;
866 864
867 LIST_FOREACH(mk, &in6_mk, mk_entry) { 865 LIST_FOREACH(mk, &in6_mk, mk_entry) {
868 /* If we've already had one, do not allocate. */ 866 /* If we've already had one, do not allocate. */
869 if (mk->mk_ifp == ifp) 867 if (mk->mk_ifp == ifp)
870 return; 868 return;
871 } 869 }
872 870
873 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK); 871 mk = malloc(sizeof(*mk), M_IPMADDR, M_ZERO|M_WAITOK);
874 872
875 memset(mk, 0, sizeof(*mk)); 
876 LIST_INIT(&mk->mk_head); 873 LIST_INIT(&mk->mk_head);
877 mk->mk_ifp = ifp; 874 mk->mk_ifp = ifp;
878 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry); 875 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
879} 876}
880 877
881void 878void
882in6_purgemkludge(struct ifnet *ifp) 879in6_purgemkludge(struct ifnet *ifp)
883{ 880{
884 struct multi6_kludge *mk; 881 struct multi6_kludge *mk;
885 struct in6_multi *in6m, *next; 882 struct in6_multi *in6m, *next;
886 883
887 LIST_FOREACH(mk, &in6_mk, mk_entry) { 884 LIST_FOREACH(mk, &in6_mk, mk_entry) {
888 if (mk->mk_ifp == ifp) 885 if (mk->mk_ifp == ifp)
889 break; 886 break;
890 } 887 }
891 if (mk == NULL) 888 if (mk == NULL)
892 return; 889 return;
893 890
894 /* leave from all multicast groups joined */ 891 /* leave from all multicast groups joined */
895 for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) { 892 for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) {
896 next = LIST_NEXT(in6m, in6m_entry); 893 next = LIST_NEXT(in6m, in6m_entry);
897 in6_delmulti(in6m); 894 in6_delmulti(in6m);
898 } 895 }
899 LIST_REMOVE(mk, mk_entry); 896 LIST_REMOVE(mk, mk_entry);
900 free(mk, M_IPMADDR); 897 free(mk, M_IPMADDR);
901} 898}