ifnet: Properly protect if_multiaddrs using ifnet serializers
[dragonfly.git] / sys / netinet6 / mld6.c
1 /*      $FreeBSD: src/sys/netinet6/mld6.c,v 1.4.2.4 2003/01/23 21:06:47 sam Exp $       */
2 /*      $DragonFly: src/sys/netinet6/mld6.c,v 1.6 2005/06/03 19:56:08 eirikn Exp $      */
3 /*      $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $   */
4
5 /*
6  * Copyright (C) 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1988 Stephen Deering.
36  * Copyright (c) 1992, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Stephen Deering of Stanford University.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      @(#)igmp.c      8.1 (Berkeley) 7/19/93
67  */
68
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/protosw.h>
77 #include <sys/syslog.h>
78 #include <sys/thread2.h>
79
80 #include <net/if.h>
81
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet/icmp6.h>
87 #include <netinet6/mld6_var.h>
88
89 #include <net/net_osdep.h>
90
91 /*
92  * Protocol constants
93  */
94
95 /* denotes that the MLD max response delay field specifies time in milliseconds */
96 #define MLD6_TIMER_SCALE        1000
97 /*
98  * time between repetitions of a node's initial report of interest in a
99  * multicast address(in seconds)
100  */
101 #define MLD6_UNSOLICITED_REPORT_INTERVAL        10
102
103 static struct ip6_pktopts ip6_opts;
104 static int mld6_timers_are_running;
105 /* XXX: These are necessary for KAME's link-local hack */
106 static struct in6_addr mld6_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
107 static struct in6_addr mld6_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
108
109 static void mld6_sendpkt (struct in6_multi *, int, const struct in6_addr *);
110
111 static struct lwkt_token mld6_token = LWKT_TOKEN_INITIALIZER(mp_token);
112
113
114 void
115 mld6_init(void)
116 {
117         static u_int8_t hbh_buf[8];
118         struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
119         u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
120
121         mld6_timers_are_running = 0;
122
123         /* ip6h_nxt will be fill in later */
124         hbh->ip6h_len = 0;      /* (8 >> 3) - 1 */
125
126         /* XXX: grotty hard coding... */
127         hbh_buf[2] = IP6OPT_PADN;       /* 2 byte padding */
128         hbh_buf[3] = 0;
129         hbh_buf[4] = IP6OPT_RTALERT;
130         hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
131         bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
132
133         init_ip6pktopts(&ip6_opts);
134         ip6_opts.ip6po_hbh = hbh;
135 }
136
137 void
138 mld6_start_listening(struct in6_multi *in6m)
139 {
140         /*
141          * RFC2710 page 10:
142          * The node never sends a Report or Done for the link-scope all-nodes
143          * address.
144          * MLD messages are never sent for multicast addresses whose scope is 0
145          * (reserved) or 1 (node-local).
146          */
147         lwkt_gettoken(&mld6_token);
148         mld6_all_nodes_linklocal.s6_addr16[1] =
149                 htons(in6m->in6m_ifp->if_index); /* XXX */
150         if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal) ||
151             IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
152                 in6m->in6m_timer = 0;
153                 in6m->in6m_state = MLD6_OTHERLISTENER;
154         } else {
155                 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
156                 in6m->in6m_timer = MLD6_RANDOM_DELAY(
157                         MLD6_UNSOLICITED_REPORT_INTERVAL * PR_FASTHZ);
158                 in6m->in6m_state = MLD6_IREPORTEDLAST;
159                 mld6_timers_are_running = 1;
160         }
161         lwkt_reltoken(&mld6_token);
162 }
163
164 void
165 mld6_stop_listening(struct in6_multi *in6m)
166 {
167         mld6_all_nodes_linklocal.s6_addr16[1] =
168                 htons(in6m->in6m_ifp->if_index); /* XXX */
169         mld6_all_routers_linklocal.s6_addr16[1] =
170                 htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */
171
172         if (in6m->in6m_state == MLD6_IREPORTEDLAST &&
173             (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal)) &&
174             IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_NODELOCAL)
175                 mld6_sendpkt(in6m, MLD_LISTENER_DONE,
176                              &mld6_all_routers_linklocal);
177 }
178
179 void
180 mld6_input(struct mbuf *m, int off)
181 {
182         struct ip6_hdr *ip6;
183         struct mld_hdr *mldh;
184         struct ifnet *ifp = m->m_pkthdr.rcvif;
185         struct in6_multi *in6m;
186         struct in6_ifaddr *ia;
187         struct ifmultiaddr *ifma, mark;
188         struct sockaddr sa;
189         int timer;              /* timer value in the MLD query header */
190
191 #ifndef PULLDOWN_TEST
192         IP6_EXTHDR_CHECK(m, off, sizeof(*mldh),);
193         mldh = (struct mld_hdr *)(mtod(m, caddr_t) + off);
194 #else
195         IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
196         if (mldh == NULL) {
197                 icmp6stat.icp6s_tooshort++;
198                 return;
199         }
200 #endif
201
202         lwkt_gettoken(&mld6_token);
203         /* source address validation */
204         ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
205         if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
206                 log(LOG_ERR,
207                     "mld6_input: src %s is not link-local (grp=%s)\n",
208                     ip6_sprintf(&ip6->ip6_src),
209                     ip6_sprintf(&mldh->mld_addr));
210                 /*
211                  * spec (RFC2710) does not explicitly
212                  * specify to discard the packet from a non link-local
213                  * source address. But we believe it's expected to do so.
214                  * XXX: do we have to allow :: as source?
215                  */
216                 m_freem(m);
217                 lwkt_reltoken(&mld6_token);
218                 return;
219         }
220
221         /*
222          * In the MLD6 specification, there are 3 states and a flag.
223          *
224          * In Non-Listener state, we simply don't have a membership record.
225          * In Delaying Listener state, our timer is running (in6m->in6m_timer)
226          * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
227          *
228          * The flag is in6m->in6m_state, it is set to MLD6_OTHERLISTENER if
229          * we have heard a report from another member, or MLD6_IREPORTEDLAST
230          * if we sent the last report.
231          */
232         switch(mldh->mld_type) {
233         case MLD_LISTENER_QUERY:
234                 if (ifp->if_flags & IFF_LOOPBACK)
235                         break;
236
237                 if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
238                     !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
239                         break;  /* print error or log stat? */
240                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
241                         mldh->mld_addr.s6_addr16[1] =
242                                 htons(ifp->if_index); /* XXX */
243
244                 /*
245                  * - Start the timers in all of our membership records
246                  *   that the query applies to for the interface on
247                  *   which the query arrived excl. those that belong
248                  *   to the "all-nodes" group (ff02::1).
249                  * - Restart any timer that is already running but has
250                  *   A value longer than the requested timeout.
251                  * - Use the value specified in the query message as
252                  *   the maximum timeout.
253                  */
254                 IFP_TO_IA6(ifp, ia);
255                 if (ia == NULL)
256                         break;
257
258                 /*
259                  * XXX: System timer resolution is too low to handle Max
260                  * Response Delay, so set 1 to the internal timer even if
261                  * the calculated value equals to zero when Max Response
262                  * Delay is positive.
263                  */
264                 timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD6_TIMER_SCALE;
265                 if (timer == 0 && mldh->mld_maxdelay)
266                         timer = 1;
267                 mld6_all_nodes_linklocal.s6_addr16[1] =
268                         htons(ifp->if_index); /* XXX */
269
270                 bzero(&sa, sizeof(sa));
271                 sa.sa_family = AF_UNSPEC;
272                 sa.sa_len = sizeof(sa);
273
274                 bzero(&mark, sizeof(mark));
275                 mark.ifma_addr = &sa;
276
277                 /* TODO: need ifnet_serialize_main */
278                 ifnet_serialize_all(ifp);
279
280                 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
281                 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
282                         TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
283                         TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
284                             ifma_link);
285
286                         if (ifma->ifma_addr->sa_family != AF_INET6)
287                                 continue;
288                         in6m = (struct in6_multi *)ifma->ifma_protospec;
289                         if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
290                                         &mld6_all_nodes_linklocal) ||
291                             IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
292                             IPV6_ADDR_SCOPE_LINKLOCAL)
293                                 continue;
294
295                         if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||
296                             IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,
297                                                 &in6m->in6m_addr))
298                         {
299                                 if (timer == 0) {
300                                         /*
301                                          * Release serializer(s) temporarily,
302                                          * before sending report.
303                                          */
304                                         ifnet_deserialize_all(ifp);
305                                         /* send a report immediately */
306                                         mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
307                                                 NULL);
308                                         ifnet_serialize_all(ifp);
309                                         in6m->in6m_timer = 0; /* reset timer */
310                                         in6m->in6m_state = MLD6_IREPORTEDLAST;
311                                 }
312                                 else if (in6m->in6m_timer == 0 || /*idle state*/
313                                         in6m->in6m_timer > timer) {
314                                         in6m->in6m_timer =
315                                                 MLD6_RANDOM_DELAY(timer);
316                                         mld6_timers_are_running = 1;
317                                 }
318                         }
319                 }
320
321                 ifnet_deserialize_all(ifp);
322
323                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
324                         mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
325                 break;
326         case MLD_LISTENER_REPORT:
327                 /*
328                  * For fast leave to work, we have to know that we are the
329                  * last person to send a report for this group.  Reports
330                  * can potentially get looped back if we are a multicast
331                  * router, so discard reports sourced by me.
332                  * Note that it is impossible to check IFF_LOOPBACK flag of
333                  * ifp for this purpose, since ip6_mloopback pass the physical
334                  * interface to looutput.
335                  */
336                 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
337                         break;
338
339                 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
340                         break;
341
342                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
343                         mldh->mld_addr.s6_addr16[1] =
344                                 htons(ifp->if_index); /* XXX */
345                 /*
346                  * If we belong to the group being reported, stop
347                  * our timer for that group.
348                  */
349                 in6m = IN6_LOOKUP_MULTI(&mldh->mld_addr, ifp);
350                 if (in6m) {
351                         in6m->in6m_timer = 0; /* transit to idle state */
352                         in6m->in6m_state = MLD6_OTHERLISTENER; /* clear flag */
353                 }
354
355                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
356                         mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
357                 break;
358         default:                /* this is impossible */
359                 log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld_type);
360                 break;
361         }
362
363         m_freem(m);
364         lwkt_reltoken(&mld6_token);
365 }
366
367 void
368 mld6_fasttimeo(void)
369 {
370         struct in6_multi *in6m;
371         struct in6_multistep step;
372
373         /*
374          * Quick check to see if any work needs to be done, in order
375          * to minimize the overhead of fasttimo processing.
376          */
377         lwkt_gettoken(&mld6_token);
378         if (!mld6_timers_are_running) {
379                 lwkt_reltoken(&mld6_token);
380                 return;
381         }
382
383         mld6_timers_are_running = 0;
384         IN6_FIRST_MULTI(step, in6m);
385         while (in6m != NULL) {
386                 if (in6m->in6m_timer == 0) {
387                         /* do nothing */
388                 } else if (--in6m->in6m_timer == 0) {
389                         mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
390                         in6m->in6m_state = MLD6_IREPORTEDLAST;
391                 } else {
392                         mld6_timers_are_running = 1;
393                 }
394                 IN6_NEXT_MULTI(step, in6m);
395         }
396         lwkt_reltoken(&mld6_token);
397 }
398
399 static void
400 mld6_sendpkt(struct in6_multi *in6m, int type, const struct in6_addr *dst)
401 {
402         struct mbuf *mh, *md;
403         struct mld_hdr *mldh;
404         struct ip6_hdr *ip6;
405         struct ip6_moptions im6o;
406         struct in6_ifaddr *ia;
407         struct ifnet *ifp = in6m->in6m_ifp;
408         struct ifnet *outif = NULL;
409
410         /*
411          * At first, find a link local address on the outgoing interface
412          * to use as the source address of the MLD packet.
413          */
414         if ((ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST))
415             == NULL)
416                 return;
417
418         /*
419          * Allocate mbufs to store ip6 header and MLD header.
420          * We allocate 2 mbufs and make chain in advance because
421          * it is more convenient when inserting the hop-by-hop option later.
422          */
423         MGETHDR(mh, MB_DONTWAIT, MT_HEADER);
424         if (mh == NULL)
425                 return;
426         MGET(md, MB_DONTWAIT, MT_DATA);
427         if (md == NULL) {
428                 m_free(mh);
429                 return;
430         }
431         mh->m_next = md;
432
433         mh->m_pkthdr.rcvif = NULL;
434         mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
435         mh->m_len = sizeof(struct ip6_hdr);
436         MH_ALIGN(mh, sizeof(struct ip6_hdr));
437
438         /* fill in the ip6 header */
439         ip6 = mtod(mh, struct ip6_hdr *);
440         ip6->ip6_flow = 0;
441         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
442         ip6->ip6_vfc |= IPV6_VERSION;
443         /* ip6_plen will be set later */
444         ip6->ip6_nxt = IPPROTO_ICMPV6;
445         /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
446         ip6->ip6_src = ia->ia_addr.sin6_addr;
447         ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
448
449         /* fill in the MLD header */
450         md->m_len = sizeof(struct mld_hdr);
451         mldh = mtod(md, struct mld_hdr *);
452         mldh->mld_type = type;
453         mldh->mld_code = 0;
454         mldh->mld_cksum = 0;
455         /* XXX: we assume the function will not be called for query messages */
456         mldh->mld_maxdelay = 0;
457         mldh->mld_reserved = 0;
458         mldh->mld_addr = in6m->in6m_addr;
459         if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
460                 mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
461         mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
462                                      sizeof(struct ip6_hdr),
463                                      sizeof(struct mld_hdr));
464
465         /* construct multicast option */
466         bzero(&im6o, sizeof(im6o));
467         im6o.im6o_multicast_ifp = ifp;
468         im6o.im6o_multicast_hlim = 1;
469
470         /*
471          * Request loopback of the report if we are acting as a multicast
472          * router, so that the process-level routing daemon can hear it.
473          */
474         im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
475
476         /* increment output statictics */
477         icmp6stat.icp6s_outhist[type]++;
478
479         ip6_output(mh, &ip6_opts, NULL, 0, &im6o, &outif, NULL);
480         if (outif) {
481                 icmp6_ifstat_inc(outif, ifs6_out_msg);
482                 switch (type) {
483                 case MLD_LISTENER_QUERY:
484                         icmp6_ifstat_inc(outif, ifs6_out_mldquery);
485                         break;
486                 case MLD_LISTENER_REPORT:
487                         icmp6_ifstat_inc(outif, ifs6_out_mldreport);
488                         break;
489                 case MLD_LISTENER_DONE:
490                         icmp6_ifstat_inc(outif, ifs6_out_mlddone);
491                         break;
492                 }
493         }
494 }