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