2 * Copyright (c) 2003-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.16.2.1 2002/12/18 21:39:40 suz Exp $ */
29 /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */
32 * Copyright (C) 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
61 * support for mandatory and extensible security protections. This notice
62 * is included in support of clause 2.2 (b) of the Apple Public License,
66 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
69 * IP multicast forwarding procedures
71 * Written by David Waitzman, BBN Labs, August 1988.
72 * Modified by Steve Deering, Stanford, February 1989.
73 * Modified by Mark J. Steiglitz, Stanford, May, 1991
74 * Modified by Van Jacobson, LBL, January 1993
75 * Modified by Ajit Thyagarajan, PARC, August 1993
76 * Modified by Bill Fenenr, PARC, April 1994
78 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/socketvar.h>
88 #include <sys/sockio.h>
89 #include <sys/protosw.h>
90 #include <sys/errno.h>
92 #include <sys/kernel.h>
93 #include <sys/syslog.h>
94 #include <kern/locks.h>
97 #include <net/route.h>
98 #include <net/raw_cb.h>
100 #include <net/net_osdep.h>
102 #include <netinet/in.h>
103 #include <netinet/in_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/scope6_var.h>
108 #include <netinet6/ip6_mroute.h>
109 #include <netinet/icmp6.h>
110 #include <netinet6/pim6.h>
111 #include <netinet6/pim6_var.h>
114 #include <security/mac.h>
118 static MALLOC_DEFINE(M_MRTABLE
, "mf6c", "multicast forwarding cache entry");
121 #define M_HASCL(m) ((m)->m_flags & M_EXT)
123 static int ip6_mdq(struct mbuf
*, struct ifnet
*, struct mf6c
*);
124 static void phyint_send(struct ip6_hdr
*, struct mif6
*, struct mbuf
*);
126 static int set_pim6(int *);
127 static int socket_send(struct socket
*, struct mbuf
*,
128 struct sockaddr_in6
*);
129 static int register_send(struct ip6_hdr
*, struct mif6
*,
133 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
134 * except for netstat or debugging purposes.
136 struct socket
*ip6_mrouter
= NULL
;
137 int ip6_mrouter_ver
= 0;
138 int ip6_mrtproto
= IPPROTO_PIM
; /* for netstat only */
142 struct mrt6stat mrt6stat
;
144 #define NO_RTE_FOUND 0x1
145 #define RTE_FOUND 0x2
147 struct mf6c
*mf6ctable
[MF6CTBLSIZ
];
148 u_char n6expire
[MF6CTBLSIZ
];
149 static struct mif6 mif6table
[MAXMIFS
];
151 u_int mrt6debug
= 0; /* debug level */
152 #define DEBUG_MFC 0x02
153 #define DEBUG_FORWARD 0x04
154 #define DEBUG_EXPIRE 0x08
155 #define DEBUG_XMIT 0x10
156 #define DEBUG_REG 0x20
157 #define DEBUG_PIM 0x40
160 static void expire_upcalls(void *);
162 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
163 #define UPCALL_EXPIRE 6 /* number of timeouts */
167 extern struct socket
*ip_mrouter
;
172 * 'Interfaces' associated with decapsulator (so we can tell
173 * packets that went through it from ones that get reflected
174 * by a broken gateway). These interfaces are never linked into
175 * the system ifnet list & no routes point to them. I.e., packets
176 * can't be sent this way. They only exist as a placeholder for
177 * multicast source verification.
179 struct ifnet multicast_register_if
;
181 #define ENCAP_HOPS 64
186 static mifi_t nummifs
= 0;
187 static mifi_t reg_mif_num
= (mifi_t
)-1;
189 static struct pim6stat pim6stat
;
193 * Hash function for a source, group entry
195 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
196 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
197 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
198 (g).s6_addr32[2] ^ (g).s6_addr32[3])
201 * Find a route for a given origin IPv6 address and Multicast group address.
202 * Quality of service parameter to be added in the future!!!
205 #define MF6CFIND(o, g, rt) do { \
206 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
208 mrt6stat.mrt6s_mfc_lookups++; \
210 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
211 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
212 (_rt->mf6c_stall == NULL)) { \
216 _rt = _rt->mf6c_next; \
219 mrt6stat.mrt6s_mfc_misses++; \
224 * Macros to compute elapsed time efficiently
225 * Borrowed from Van Jacobson's scheduling code
227 #define TV_DELTA(a, b, delta) do { \
230 delta = (a).tv_usec - (b).tv_usec; \
231 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
240 delta += (1000000 * xxs); \
245 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
246 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
249 #define UPCALL_MAX 50
250 u_int32_t upcall_data
[UPCALL_MAX
+ 1];
251 static void collate();
252 #endif /* UPCALL_TIMING */
254 static int get_sg_cnt(struct sioc_sg_req6
*);
255 static int get_mif6_cnt(void *, int);
256 static int ip6_mrouter_init(struct socket
*, int, int);
257 static int add_m6if(struct mif6ctl
*);
258 static int del_m6if(mifi_t
*);
259 static int add_m6fc(struct mf6cctl
*);
260 static int del_m6fc(struct mf6cctl
*);
263 * Handle MRT setsockopt commands to modify the multicast routing tables.
266 ip6_mrouter_set(so
, sopt
)
268 struct sockopt
*sopt
;
276 if (so
!= ip6_mrouter
&& sopt
->sopt_name
!= MRT6_INIT
)
279 switch (sopt
->sopt_name
) {
284 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
288 error
= ip6_mrouter_init(so
, optval
, sopt
->sopt_name
);
291 error
= ip6_mrouter_done();
294 error
= sooptcopyin(sopt
, &mifc
, sizeof(mifc
), sizeof(mifc
));
297 error
= add_m6if(&mifc
);
300 error
= sooptcopyin(sopt
, &mfcc
, sizeof(mfcc
), sizeof(mfcc
));
303 error
= add_m6fc(&mfcc
);
306 error
= sooptcopyin(sopt
, &mfcc
, sizeof(mfcc
), sizeof(mfcc
));
309 error
= del_m6fc(&mfcc
);
312 error
= sooptcopyin(sopt
, &mifi
, sizeof(mifi
), sizeof(mifi
));
315 error
= del_m6if(&mifi
);
318 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
322 error
= set_pim6(&optval
);
333 * Handle MRT getsockopt commands
336 ip6_mrouter_get(so
, sopt
)
338 struct sockopt
*sopt
;
342 if (so
!= ip6_mrouter
) return EACCES
;
344 switch (sopt
->sopt_name
) {
346 error
= sooptcopyout(sopt
, &pim6
, sizeof(pim6
));
353 * Handle ioctl commands to obtain information from the cache
356 mrt6_ioctl(u_long cmd
, caddr_t data
)
361 case SIOCGETSGCNT_IN6
:
362 return (get_sg_cnt((struct sioc_sg_req6
*)data
));
365 case SIOCGETMIFCNT_IN6_32
:
366 case SIOCGETMIFCNT_IN6_64
:
367 return (get_mif6_cnt(data
, cmd
== SIOCGETMIFCNT_IN6_64
));
378 * returns the packet, byte, rpf-failure count for the source group provided
382 struct sioc_sg_req6
*req
;
386 MF6CFIND(req
->src
.sin6_addr
, req
->grp
.sin6_addr
, rt
);
388 req
->pktcnt
= rt
->mf6c_pkt_cnt
;
389 req
->bytecnt
= rt
->mf6c_byte_cnt
;
390 req
->wrong_if
= rt
->mf6c_wrong_if
;
394 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
401 * returns the input and output packet and byte counts on the mif provided
404 get_mif6_cnt(void *data
, int p64
)
407 struct sioc_mif_req6_64
*req
= data
;
409 mifi_t mifi
= req
->mifi
;
414 req
->icount
= mif6table
[mifi
].m6_pkt_in
;
415 req
->ocount
= mif6table
[mifi
].m6_pkt_out
;
416 req
->ibytes
= mif6table
[mifi
].m6_bytes_in
;
417 req
->obytes
= mif6table
[mifi
].m6_bytes_out
;
419 struct sioc_mif_req6_32
*req
= data
;
421 mifi_t mifi
= req
->mifi
;
426 req
->icount
= mif6table
[mifi
].m6_pkt_in
;
427 req
->ocount
= mif6table
[mifi
].m6_pkt_out
;
428 req
->ibytes
= mif6table
[mifi
].m6_bytes_in
;
429 req
->obytes
= mif6table
[mifi
].m6_bytes_out
;
438 if ((*i
!= 1) && (*i
!= 0))
447 * Enable multicast routing
450 ip6_mrouter_init(so
, v
, cmd
)
458 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
459 so
->so_type
, so
->so_proto
->pr_protocol
);
462 if (so
->so_type
!= SOCK_RAW
||
463 so
->so_proto
->pr_protocol
!= IPPROTO_ICMPV6
)
467 return (ENOPROTOOPT
);
469 if (ip6_mrouter
!= NULL
) return EADDRINUSE
;
472 ip6_mrouter_ver
= cmd
;
474 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
475 bzero((caddr_t
)n6expire
, sizeof(n6expire
));
477 pim6
= 0;/* used for stubbing out/in pim stuff */
479 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
483 log(LOG_DEBUG
, "ip6_mrouter_init\n");
490 * Disable multicast routing
502 * For each phyint in use, disable promiscuous reception of all IPv6
508 * If there is still IPv4 multicast routing daemon,
509 * we remain interfaces to receive all muliticasted packets.
510 * XXX: there may be an interface in which the IPv4 multicast
511 * daemon is not interested...
517 for (mifi
= 0; mifi
< nummifs
; mifi
++) {
518 if (mif6table
[mifi
].m6_ifp
&&
519 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
521 if_allmulti(mif6table
[mifi
].m6_ifp
, 0);
525 struct in6_ifreq ifr
;
527 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
528 ifr
.ifr_addr
.sin6_addr
= in6addr_any
;
529 ifp
= mif6table
[mifi
].m6_ifp
;
530 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, &ifr
);
536 bzero((caddr_t
)mif6table
, sizeof(mif6table
));
539 pim6
= 0; /* used to stub out/in pim specific code */
541 untimeout(expire_upcalls
, (caddr_t
)NULL
);
544 * Free all multicast forwarding cache entries.
545 *###LD 5/27 needs locking
547 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
552 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
553 struct rtdetq
*n
= rte
->next
;
556 FREE(rte
, M_MRTABLE
);
561 FREE(frt
, M_MRTABLE
);
565 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
568 * Reset de-encapsulation cache
578 log(LOG_DEBUG
, "ip6_mrouter_done\n");
584 static struct sockaddr_in6 sin6
= { sizeof(sin6
), AF_INET6
,
585 0, 0, IN6ADDR_ANY_INIT
, 0};
588 * Add a mif to the mif table
592 struct mif6ctl
*mifcp
;
598 struct tbf
*m_tbf
= tbftable
+ mifcp
->mif6c_mifi
;
601 if (mifcp
->mif6c_mifi
>= MAXMIFS
)
603 mifp
= mif6table
+ mifcp
->mif6c_mifi
;
605 return (EADDRINUSE
); /* XXX: is it appropriate? */
606 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
> if_index
)
609 ifnet_head_lock_shared();
610 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
> if_index
) {
614 ifp
= ifindex2ifnet
[mifcp
->mif6c_pifi
];
620 if (mifcp
->mif6c_flags
& MIFF_REGISTER
) {
621 if (reg_mif_num
== (mifi_t
)-1) {
622 multicast_register_if
.if_name
= "register_mif";
623 multicast_register_if
.if_flags
|= IFF_LOOPBACK
;
624 multicast_register_if
.if_index
= mifcp
->mif6c_mifi
;
625 reg_mif_num
= mifcp
->mif6c_mifi
;
628 ifp
= &multicast_register_if
;
632 /* Make sure the interface supports multicast */
633 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
636 error
= if_allmulti(ifp
, 1);
641 mifp
->m6_flags
= mifcp
->mif6c_flags
;
644 /* initialize per mif pkt counters */
646 mifp
->m6_pkt_out
= 0;
647 mifp
->m6_bytes_in
= 0;
648 mifp
->m6_bytes_out
= 0;
650 /* Adjust nummifs up if the mifi is higher than nummifs */
651 if (nummifs
<= mifcp
->mif6c_mifi
)
652 nummifs
= mifcp
->mif6c_mifi
+ 1;
657 "add_mif #%d, phyint %s%d\n",
659 ifp
->if_name
, ifp
->if_unit
);
666 * Delete a mif from the mif table
672 struct mif6
*mifp
= mif6table
+ *mifip
;
676 if (*mifip
>= nummifs
)
678 if (mifp
->m6_ifp
== NULL
)
682 if (!(mifp
->m6_flags
& MIFF_REGISTER
)) {
684 * XXX: what if there is yet IPv4 multicast daemon
685 * using the interface?
692 bzero((caddr_t
)mifp
, sizeof(*mifp
));
694 /* Adjust nummifs down */
695 for (mifi
= nummifs
; mifi
> 0; mifi
--)
696 if (mif6table
[mifi
- 1].m6_ifp
)
703 log(LOG_DEBUG
, "del_m6if %d, nummifs %d\n", *mifip
, nummifs
);
714 struct mf6cctl
*mfccp
;
721 MF6CFIND(mfccp
->mf6cc_origin
.sin6_addr
,
722 mfccp
->mf6cc_mcastgrp
.sin6_addr
, rt
);
724 /* If an entry already exists, just update the fields */
727 if (mrt6debug
& DEBUG_MFC
)
729 "add_m6fc no upcall h %d o %s g %s p %x\n",
730 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
731 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
732 mfccp
->mf6cc_parent
);
735 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
736 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
741 * Find the entry for which the upcall was made and update
743 hash
= MF6CHASH(mfccp
->mf6cc_origin
.sin6_addr
,
744 mfccp
->mf6cc_mcastgrp
.sin6_addr
);
745 for (rt
= mf6ctable
[hash
], nstl
= 0; rt
; rt
= rt
->mf6c_next
) {
746 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
747 &mfccp
->mf6cc_origin
.sin6_addr
) &&
748 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
749 &mfccp
->mf6cc_mcastgrp
.sin6_addr
) &&
750 (rt
->mf6c_stall
!= NULL
)) {
754 "add_m6fc: %s o %s g %s p %x dbx %p\n",
755 "multiple kernel entries",
756 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
757 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
758 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
761 if (mrt6debug
& DEBUG_MFC
)
763 "add_m6fc o %s g %s p %x dbg %x\n",
764 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
765 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
766 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
769 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
770 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
771 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
772 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
773 /* initialize pkt counters per src-grp */
774 rt
->mf6c_pkt_cnt
= 0;
775 rt
->mf6c_byte_cnt
= 0;
776 rt
->mf6c_wrong_if
= 0;
778 rt
->mf6c_expire
= 0; /* Don't clean this guy up */
781 /* free packets Qed at the end of this entry */
782 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
783 struct rtdetq
*n
= rte
->next
;
784 ip6_mdq(rte
->m
, rte
->ifp
, rt
);
788 #endif /* UPCALL_TIMING */
789 FREE(rte
, M_MRTABLE
);
792 rt
->mf6c_stall
= NULL
;
797 * It is possible that an entry is being inserted without an upcall
801 if (mrt6debug
& DEBUG_MFC
)
802 log(LOG_DEBUG
,"add_mfc no upcall h %d o %s g %s p %x\n",
804 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
805 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
806 mfccp
->mf6cc_parent
);
809 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
811 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
812 &mfccp
->mf6cc_origin
.sin6_addr
)&&
813 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
814 &mfccp
->mf6cc_mcastgrp
.sin6_addr
)) {
816 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
817 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
818 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
819 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
820 /* initialize pkt counters per src-grp */
821 rt
->mf6c_pkt_cnt
= 0;
822 rt
->mf6c_byte_cnt
= 0;
823 rt
->mf6c_wrong_if
= 0;
831 /* no upcall, so make a new entry */
832 rt
= (struct mf6c
*)_MALLOC(sizeof(*rt
), M_MRTABLE
,
838 /* insert new entry at head of hash chain */
839 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
840 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
841 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
842 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
843 /* initialize pkt counters per src-grp */
844 rt
->mf6c_pkt_cnt
= 0;
845 rt
->mf6c_byte_cnt
= 0;
846 rt
->mf6c_wrong_if
= 0;
848 rt
->mf6c_stall
= NULL
;
850 /* link into table */
851 rt
->mf6c_next
= mf6ctable
[hash
];
852 mf6ctable
[hash
] = rt
;
860 * collect delay statistics on the upcalls
874 TV_DELTA(tp
, *t
, delta
);
883 #endif /* UPCALL_TIMING */
886 * Delete an mfc entry
890 struct mf6cctl
*mfccp
;
892 struct sockaddr_in6 origin
;
893 struct sockaddr_in6 mcastgrp
;
898 origin
= mfccp
->mf6cc_origin
;
899 mcastgrp
= mfccp
->mf6cc_mcastgrp
;
900 hash
= MF6CHASH(origin
.sin6_addr
, mcastgrp
.sin6_addr
);
903 if (mrt6debug
& DEBUG_MFC
)
904 log(LOG_DEBUG
,"del_m6fc orig %s mcastgrp %s\n",
905 ip6_sprintf(&origin
.sin6_addr
),
906 ip6_sprintf(&mcastgrp
.sin6_addr
));
910 nptr
= &mf6ctable
[hash
];
911 while ((rt
= *nptr
) != NULL
) {
912 if (IN6_ARE_ADDR_EQUAL(&origin
.sin6_addr
,
913 &rt
->mf6c_origin
.sin6_addr
) &&
914 IN6_ARE_ADDR_EQUAL(&mcastgrp
.sin6_addr
,
915 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
916 rt
->mf6c_stall
== NULL
)
919 nptr
= &rt
->mf6c_next
;
922 return EADDRNOTAVAIL
;
925 *nptr
= rt
->mf6c_next
;
933 socket_send(s
, mm
, src
)
936 struct sockaddr_in6
*src
;
938 //### LD 5/27/04 needs locking!
941 if (sbappendaddr(&s
->so_rcv
,
942 (struct sockaddr
*)src
,
943 mm
, (struct mbuf
*)0, NULL
) != 0) {
952 * IPv6 multicast forwarding function. This function assumes that the packet
953 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
954 * pointed to by "ifp", and the packet is to be relayed to other networks
955 * that have members of the packet's destination IPv6 multicast group.
957 * The packet is returned unscathed to the caller, unless it is
958 * erroneous, in which case a non-zero return value tells the caller to
963 ip6_mforward(ip6
, ifp
, m
)
972 struct timeval timenow
;
975 if (mrt6debug
& DEBUG_FORWARD
)
976 log(LOG_DEBUG
, "ip6_mforward: src %s, dst %s, ifindex %d\n",
977 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
),
982 * Don't forward a packet with Hop limit of zero or one,
983 * or a packet destined to a local-only group.
985 if (ip6
->ip6_hlim
<= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
) ||
986 IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
991 * Source address check: do not forward packets with unspecified
992 * source. It was discussed in July 2000, on ipngwg mailing list.
993 * This is rather more serious than unicast cases, because some
994 * MLD packets can be sent with the unspecified source address
995 * (although such packets must normally set 1 to the hop limit field).
997 getmicrotime(&timenow
);
998 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
999 ip6stat
.ip6s_cantforward
++;
1000 if (ip6_log_time
+ ip6_log_interval
< timenow
.tv_sec
) {
1001 ip6_log_time
= timenow
.tv_sec
;
1004 "from %s to %s nxt %d received on %s\n",
1005 ip6_sprintf(&ip6
->ip6_src
),
1006 ip6_sprintf(&ip6
->ip6_dst
),
1008 if_name(m
->m_pkthdr
.rcvif
));
1014 * Determine forwarding mifs from the forwarding cache table
1016 MF6CFIND(ip6
->ip6_src
, ip6
->ip6_dst
, rt
);
1018 /* Entry exists, so forward if necessary */
1020 return (ip6_mdq(m
, ifp
, rt
));
1023 * If we don't have a route for packet's origin,
1024 * Make a copy of the packet &
1025 * send message to routing daemon
1036 #endif /* UPCALL_TIMING */
1038 mrt6stat
.mrt6s_no_route
++;
1040 if (mrt6debug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1041 log(LOG_DEBUG
, "ip6_mforward: no rte s %s g %s\n",
1042 ip6_sprintf(&ip6
->ip6_src
),
1043 ip6_sprintf(&ip6
->ip6_dst
));
1047 * Allocate mbufs early so that we don't do extra work if we
1048 * are just going to fail anyway.
1050 rte
= (struct rtdetq
*)_MALLOC(sizeof(*rte
), M_MRTABLE
,
1055 mb0
= m_copy(m
, 0, M_COPYALL
);
1057 * Pullup packet header if needed before storing it,
1058 * as other references may modify it in the meantime.
1061 (M_HASCL(mb0
) || mb0
->m_len
< sizeof(struct ip6_hdr
)))
1062 mb0
= m_pullup(mb0
, sizeof(struct ip6_hdr
));
1064 FREE(rte
, M_MRTABLE
);
1068 /* is there an upcall waiting for this packet? */
1069 hash
= MF6CHASH(ip6
->ip6_src
, ip6
->ip6_dst
);
1070 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
1071 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
,
1072 &rt
->mf6c_origin
.sin6_addr
) &&
1073 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1074 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
1075 (rt
->mf6c_stall
!= NULL
))
1082 struct omrt6msg
*oim
;
1085 /* no upcall, so make a new entry */
1086 rt
= (struct mf6c
*)_MALLOC(sizeof(*rt
), M_MRTABLE
,
1089 FREE(rte
, M_MRTABLE
);
1094 * Make a copy of the header to send to the user
1097 mm
= m_copy(mb0
, 0, sizeof(struct ip6_hdr
));
1100 FREE(rte
, M_MRTABLE
);
1102 FREE(rt
, M_MRTABLE
);
1107 * Send message to routing daemon
1109 sin6
.sin6_addr
= ip6
->ip6_src
;
1115 switch (ip6_mrouter_ver
) {
1118 oim
= mtod(mm
, struct omrt6msg
*);
1119 oim
->im6_msgtype
= MRT6MSG_NOCACHE
;
1124 im
= mtod(mm
, struct mrt6msg
*);
1125 im
->im6_msgtype
= MRT6MSG_NOCACHE
;
1129 FREE(rte
, M_MRTABLE
);
1131 FREE(rt
, M_MRTABLE
);
1136 if (mrt6debug
& DEBUG_FORWARD
)
1138 "getting the iif info in the kernel\n");
1141 for (mifp
= mif6table
, mifi
= 0;
1142 mifi
< nummifs
&& mifp
->m6_ifp
!= ifp
;
1146 switch (ip6_mrouter_ver
) {
1149 oim
->im6_mif
= mifi
;
1157 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1158 log(LOG_WARNING
, "ip6_mforward: ip6_mrouter "
1159 "socket queue full\n");
1160 mrt6stat
.mrt6s_upq_sockfull
++;
1161 FREE(rte
, M_MRTABLE
);
1163 FREE(rt
, M_MRTABLE
);
1167 mrt6stat
.mrt6s_upcalls
++;
1169 /* insert new entry at head of hash chain */
1170 bzero(rt
, sizeof(*rt
));
1171 rt
->mf6c_origin
.sin6_family
= AF_INET6
;
1172 rt
->mf6c_origin
.sin6_len
= sizeof(struct sockaddr_in6
);
1173 rt
->mf6c_origin
.sin6_addr
= ip6
->ip6_src
;
1174 rt
->mf6c_mcastgrp
.sin6_family
= AF_INET6
;
1175 rt
->mf6c_mcastgrp
.sin6_len
= sizeof(struct sockaddr_in6
);
1176 rt
->mf6c_mcastgrp
.sin6_addr
= ip6
->ip6_dst
;
1177 rt
->mf6c_expire
= UPCALL_EXPIRE
;
1179 rt
->mf6c_parent
= MF6C_INCOMPLETE_PARENT
;
1181 /* link into table */
1182 rt
->mf6c_next
= mf6ctable
[hash
];
1183 mf6ctable
[hash
] = rt
;
1184 /* Add this entry to the end of the queue */
1185 rt
->mf6c_stall
= rte
;
1187 /* determine if q has overflowed */
1191 for (p
= &rt
->mf6c_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1192 if (++npkts
> MAX_UPQ6
) {
1193 mrt6stat
.mrt6s_upq_ovflw
++;
1194 FREE(rte
, M_MRTABLE
);
1199 /* Add this entry to the end of the queue */
1208 #endif /* UPCALL_TIMING */
1216 * Clean up cache entries if upcalls are not serviced
1217 * Call from the Slow Timeout mechanism, every half second.
1221 __unused
void *unused
)
1224 struct mf6c
*mfc
, **nptr
;
1227 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
1228 if (n6expire
[i
] == 0)
1230 nptr
= &mf6ctable
[i
];
1231 while ((mfc
= *nptr
) != NULL
) {
1232 rte
= mfc
->mf6c_stall
;
1234 * Skip real cache entries
1235 * Make sure it wasn't marked to not expire (shouldn't happen)
1239 mfc
->mf6c_expire
!= 0 &&
1240 --mfc
->mf6c_expire
== 0) {
1242 if (mrt6debug
& DEBUG_EXPIRE
)
1243 log(LOG_DEBUG
, "expire_upcalls: expiring (%s %s)\n",
1244 ip6_sprintf(&mfc
->mf6c_origin
.sin6_addr
),
1245 ip6_sprintf(&mfc
->mf6c_mcastgrp
.sin6_addr
));
1248 * drop all the packets
1249 * free the mbuf with the pkt, if, timing info
1252 struct rtdetq
*n
= rte
->next
;
1254 FREE(rte
, M_MRTABLE
);
1256 } while (rte
!= NULL
);
1257 mrt6stat
.mrt6s_cache_cleanups
++;
1260 *nptr
= mfc
->mf6c_next
;
1261 FREE(mfc
, M_MRTABLE
);
1263 nptr
= &mfc
->mf6c_next
;
1268 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1272 * Packet forwarding routine once entry in the cache is made
1280 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1283 int plen
= m
->m_pkthdr
.len
;
1284 struct in6_addr src0
, dst0
; /* copies for local work */
1285 u_int32_t iszone
, idzone
, oszone
, odzone
;
1289 * Macro to send packet on mif. Since RSVP packets don't get counted on
1290 * input, they shouldn't get counted on output, so statistics keeping is
1294 #define MC6_SEND(ip6, mifp, m) do { \
1295 if ((mifp)->m6_flags & MIFF_REGISTER) \
1296 register_send((ip6), (mifp), (m)); \
1298 phyint_send((ip6), (mifp), (m)); \
1302 * Don't forward if it didn't arrive from the parent mif
1305 mifi
= rt
->mf6c_parent
;
1306 if ((mifi
>= nummifs
) || (mif6table
[mifi
].m6_ifp
!= ifp
)) {
1307 /* came in the wrong interface */
1309 if (mrt6debug
& DEBUG_FORWARD
)
1311 "wrong if: ifid %d mifi %d mififid %x\n",
1312 ifp
->if_index
, mifi
,
1313 mif6table
[mifi
].m6_ifp
->if_index
);
1315 mrt6stat
.mrt6s_wrong_if
++;
1316 rt
->mf6c_wrong_if
++;
1318 * If we are doing PIM processing, and we are forwarding
1319 * packets on this interface, send a message to the
1322 /* have to make sure this is a valid mif */
1323 if (mifi
< nummifs
&& mif6table
[mifi
].m6_ifp
)
1324 if (pim6
&& (m
->m_flags
& M_LOOP
) == 0) {
1326 * Check the M_LOOP flag to avoid an
1327 * unnecessary PIM assert.
1328 * XXX: M_LOOP is an ad-hoc hack...
1330 static struct sockaddr_in6 addr
=
1331 { sizeof(addr
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0};
1336 struct omrt6msg
*oim
;
1339 mm
= m_copy(m
, 0, sizeof(struct ip6_hdr
));
1342 mm
->m_len
< sizeof(struct ip6_hdr
)))
1343 mm
= m_pullup(mm
, sizeof(struct ip6_hdr
));
1351 switch (ip6_mrouter_ver
) {
1354 oim
= mtod(mm
, struct omrt6msg
*);
1355 oim
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1360 im
= mtod(mm
, struct mrt6msg
*);
1361 im
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1369 for (mifp
= mif6table
, iif
= 0;
1370 iif
< nummifs
&& mifp
&&
1371 mifp
->m6_ifp
!= ifp
;
1375 switch (ip6_mrouter_ver
) {
1379 addr
.sin6_addr
= oim
->im6_src
;
1384 addr
.sin6_addr
= im
->im6_src
;
1388 mrt6stat
.mrt6s_upcalls
++;
1390 if (socket_send(ip6_mrouter
, mm
, &addr
) < 0) {
1393 log(LOG_WARNING
, "mdq, ip6_mrouter socket queue full\n");
1395 ++mrt6stat
.mrt6s_upq_sockfull
;
1397 } /* if socket Q full */
1400 } /* if wrong iif */
1402 /* If I sourced this packet, it counts as output, else it was input. */
1403 if (m
->m_pkthdr
.rcvif
== NULL
) {
1404 /* XXX: is rcvif really NULL when output?? */
1405 mif6table
[mifi
].m6_pkt_out
++;
1406 mif6table
[mifi
].m6_bytes_out
+= plen
;
1408 mif6table
[mifi
].m6_pkt_in
++;
1409 mif6table
[mifi
].m6_bytes_in
+= plen
;
1412 rt
->mf6c_byte_cnt
+= plen
;
1415 * For each mif, forward a copy of the packet if there are group
1416 * members downstream on the interface.
1418 src0
= ip6
->ip6_src
;
1419 dst0
= ip6
->ip6_dst
;
1420 if ((error
= in6_setscope(&src0
, ifp
, &iszone
)) != 0 ||
1421 (error
= in6_setscope(&dst0
, ifp
, &idzone
)) != 0) {
1422 ip6stat
.ip6s_badscope
++;
1425 for (mifp
= mif6table
, mifi
= 0; mifi
< nummifs
; mifp
++, mifi
++) {
1426 if (IF_ISSET(mifi
, &rt
->mf6c_ifset
)) {
1428 * check if the outgoing packet is going to break
1430 * XXX For packets through PIM register tunnel
1431 * interface, we believe a routing daemon.
1433 if (!(mif6table
[rt
->mf6c_parent
].m6_flags
&
1435 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
1436 if (in6_setscope(&src0
, mif6table
[mifi
].m6_ifp
,
1438 in6_setscope(&dst0
, mif6table
[mifi
].m6_ifp
,
1442 ip6stat
.ip6s_badscope
++;
1448 mifp
->m6_bytes_out
+= plen
;
1449 MC6_SEND(ip6
, mifp
, m
);
1456 phyint_send(ip6
, mifp
, m
)
1457 struct ip6_hdr
*ip6
;
1461 struct mbuf
*mb_copy
;
1462 struct ifnet
*ifp
= mifp
->m6_ifp
;
1464 static struct route_in6 ro
;
1465 struct in6_multi
*in6m
;
1466 struct sockaddr_in6
*dst6
;
1469 * Make a new reference to the packet; make sure that
1470 * the IPv6 header is actually copied, not just referenced,
1471 * so that ip6_output() only scribbles on the copy.
1473 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1475 (M_HASCL(mb_copy
) || mb_copy
->m_len
< sizeof(struct ip6_hdr
)))
1476 mb_copy
= m_pullup(mb_copy
, sizeof(struct ip6_hdr
));
1477 if (mb_copy
== NULL
) {
1480 /* set MCAST flag to the outgoing packet */
1481 mb_copy
->m_flags
|= M_MCAST
;
1484 * If we sourced the packet, call ip6_output since we may devide
1485 * the packet into fragments when the packet is too big for the
1486 * outgoing interface.
1487 * Otherwise, we can simply send the packet to the interface
1490 if (m
->m_pkthdr
.rcvif
== NULL
) {
1491 struct ip6_moptions
*im6o
;
1493 im6o
= ip6_allocmoptions(M_DONTWAIT
);
1499 im6o
->im6o_multicast_ifp
= ifp
;
1500 /* XXX: ip6_output will override ip6->ip6_hlim */
1501 im6o
->im6o_multicast_hlim
= ip6
->ip6_hlim
;
1502 im6o
->im6o_multicast_loop
= 1;
1503 error
= ip6_output(mb_copy
, NULL
, &ro
, IPV6_FORWARDING
,
1508 if (mrt6debug
& DEBUG_XMIT
)
1509 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1510 mifp
- mif6table
, error
);
1516 * If we belong to the destination multicast group
1517 * on the outgoing interface, loop back a copy.
1519 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
1520 in6_multihead_lock_shared();
1521 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
1522 in6_multihead_lock_done();
1525 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1526 dst6
->sin6_family
= AF_INET6
;
1527 dst6
->sin6_addr
= ip6
->ip6_dst
;
1528 ip6_mloopback(ifp
, m
, (struct sockaddr_in6
*)&ro
.ro_dst
);
1531 * Put the packet into the sending queue of the outgoing interface
1532 * if it would fit in the MTU of the interface.
1534 if (mb_copy
->m_pkthdr
.len
<= ifp
->if_mtu
|| ifp
->if_mtu
< IPV6_MMTU
) {
1535 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1536 dst6
->sin6_family
= AF_INET6
;
1537 dst6
->sin6_addr
= ip6
->ip6_dst
;
1539 * We just call if_output instead of nd6_output here, since
1540 * we need no ND for a multicast forwarded packet...right?
1543 /* Make sure the HW checksum flags are cleaned before sending the packet */
1545 mb_copy
->m_pkthdr
.rcvif
= 0;
1546 mb_copy
->m_pkthdr
.csum_data
= 0;
1547 mb_copy
->m_pkthdr
.csum_flags
= 0;
1549 error
= dlil_output(ifp
, PF_INET6
, mb_copy
,
1550 NULL
, (struct sockaddr
*)&ro
.ro_dst
, 0);
1552 error
= (*ifp
->if_output
)(ifp
, mb_copy
,
1553 (struct sockaddr
*)&ro
.ro_dst
,
1557 if (mrt6debug
& DEBUG_XMIT
)
1558 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1559 mifp
- mif6table
, error
);
1563 * pMTU discovery is intentionally disabled by default, since
1564 * various router may notify pMTU in multicast, which can be
1565 * a DDoS to a router
1568 icmp6_error(mb_copy
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
1571 if (mrt6debug
& DEBUG_XMIT
) {
1573 "phyint_send: packet too big on %s o %s "
1574 "g %s size %d(discarded)\n",
1576 ip6_sprintf(&ip6
->ip6_src
),
1577 ip6_sprintf(&ip6
->ip6_dst
),
1578 mb_copy
->m_pkthdr
.len
);
1581 #endif /* MRT6DEBUG */
1582 m_freem(mb_copy
); /* simply discard the packet */
1588 register_send(ip6
, mif
, m
)
1589 struct ip6_hdr
*ip6
;
1594 int i
, len
= m
->m_pkthdr
.len
;
1595 static struct sockaddr_in6 addr
= { sizeof(addr
), AF_INET6
,
1596 0, 0, IN6ADDR_ANY_INIT
, 0};
1597 struct mrt6msg
*im6
;
1601 log(LOG_DEBUG
, "** IPv6 register_send **\n src %s dst %s\n",
1602 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
));
1604 ++pim6stat
.pim6s_snd_registers
;
1606 /* Make a copy of the packet to send to the user level process */
1607 MGETHDR(mm
, M_DONTWAIT
, MT_HEADER
);
1610 #ifdef __darwin8_notyet
1612 mac_create_mbuf_multicast_encap(m
, mif
->m6_ifp
, mm
);
1615 mm
->m_pkthdr
.rcvif
= NULL
;
1616 mm
->m_data
+= max_linkhdr
;
1617 mm
->m_len
= sizeof(struct ip6_hdr
);
1619 if ((mm
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1623 i
= MHLEN
- M_LEADINGSPACE(mm
);
1626 mm
= m_pullup(mm
, i
);
1631 /* TODO: check it! */
1632 mm
->m_pkthdr
.len
= len
+ sizeof(struct ip6_hdr
);
1635 * Send message to routing daemon
1637 addr
.sin6_addr
= ip6
->ip6_src
;
1639 im6
= mtod(mm
, struct mrt6msg
*);
1640 im6
->im6_msgtype
= MRT6MSG_WHOLEPKT
;
1643 im6
->im6_mif
= mif
- mif6table
;
1645 /* iif info is not given for reg. encap.n */
1646 mrt6stat
.mrt6s_upcalls
++;
1648 if (socket_send(ip6_mrouter
, mm
, &addr
) < 0) {
1652 "register_send: ip6_mrouter socket queue full\n");
1654 ++mrt6stat
.mrt6s_upq_sockfull
;
1661 * PIM sparse mode hook
1662 * Receives the pim control messages, and passes them up to the listening
1663 * socket, using rip6_input.
1664 * The only message processed is the REGISTER pim message; the pim header
1665 * is stripped off, and the inner packet is passed to register_mforward.
1668 pim6_input(struct mbuf
**mp
, int *offp
, int proto
)
1670 struct pim
*pim
; /* pointer to a pim struct */
1671 struct ip6_hdr
*ip6
;
1673 struct mbuf
*m
= *mp
;
1677 ++pim6stat
.pim6s_rcv_total
;
1679 ip6
= mtod(m
, struct ip6_hdr
*);
1680 pimlen
= m
->m_pkthdr
.len
- *offp
;
1685 if (pimlen
< PIM_MINLEN
) {
1686 ++pim6stat
.pim6s_rcv_tooshort
;
1688 if (mrt6debug
& DEBUG_PIM
)
1689 log(LOG_DEBUG
,"pim6_input: PIM packet too short\n");
1692 return(IPPROTO_DONE
);
1696 * if the packet is at least as big as a REGISTER, go ahead
1697 * and grab the PIM REGISTER header size, to avoid another
1698 * possible m_pullup() later.
1700 * PIM_MINLEN == pimhdr + u_int32 == 8
1701 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1703 minlen
= (pimlen
>= PIM6_REG_MINLEN
) ? PIM6_REG_MINLEN
: PIM_MINLEN
;
1706 * Make sure that the IP6 and PIM headers in contiguous memory, and
1707 * possibly the PIM REGISTER header
1709 #ifndef PULLDOWN_TEST
1710 IP6_EXTHDR_CHECK(m
, off
, minlen
, return IPPROTO_DONE
);
1711 /* adjust pointer */
1712 ip6
= mtod(m
, struct ip6_hdr
*);
1714 /* adjust mbuf to point to the PIM header */
1715 pim
= (struct pim
*)((caddr_t
)ip6
+ off
);
1717 IP6_EXTHDR_GET(pim
, struct pim
*, m
, off
, minlen
);
1719 pim6stat
.pim6s_rcv_tooshort
++;
1720 return IPPROTO_DONE
;
1724 #define PIM6_CHECKSUM
1725 #ifdef PIM6_CHECKSUM
1730 * Validate checksum.
1731 * If PIM REGISTER, exclude the data packet
1733 if (pim
->pim_type
== PIM_REGISTER
)
1734 cksumlen
= PIM_MINLEN
;
1738 if (in6_cksum(m
, IPPROTO_PIM
, off
, cksumlen
)) {
1739 ++pim6stat
.pim6s_rcv_badsum
;
1741 if (mrt6debug
& DEBUG_PIM
)
1743 "pim6_input: invalid checksum\n");
1746 return(IPPROTO_DONE
);
1749 #endif /* PIM_CHECKSUM */
1751 /* PIM version check */
1752 if (pim
->pim_ver
!= PIM_VERSION
) {
1753 ++pim6stat
.pim6s_rcv_badversion
;
1756 "pim6_input: incorrect version %d, expecting %d\n",
1757 pim
->pim_ver
, PIM_VERSION
);
1760 return(IPPROTO_DONE
);
1763 if (pim
->pim_type
== PIM_REGISTER
) {
1765 * since this is a REGISTER, we'll make a copy of the register
1766 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1769 static struct sockaddr_in6 dst
= { sizeof(dst
), AF_INET6
,
1770 0, 0, IN6ADDR_ANY_INIT
, 0 };
1773 struct ip6_hdr
*eip6
;
1776 ++pim6stat
.pim6s_rcv_registers
;
1778 if ((reg_mif_num
>= nummifs
) || (reg_mif_num
== (mifi_t
) -1)) {
1780 if (mrt6debug
& DEBUG_PIM
)
1782 "pim6_input: register mif not set: %d\n",
1786 return(IPPROTO_DONE
);
1789 reghdr
= (u_int32_t
*)(pim
+ 1);
1791 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
1792 goto pim6_input_to_daemon
;
1797 if (pimlen
< PIM6_REG_MINLEN
) {
1798 ++pim6stat
.pim6s_rcv_tooshort
;
1799 ++pim6stat
.pim6s_rcv_badregisters
;
1802 "pim6_input: register packet size too "
1803 "small %d from %s\n",
1804 pimlen
, ip6_sprintf(&ip6
->ip6_src
));
1807 return(IPPROTO_DONE
);
1810 eip6
= (struct ip6_hdr
*) (reghdr
+ 1);
1812 if (mrt6debug
& DEBUG_PIM
)
1814 "pim6_input[register], eip6: %s -> %s, "
1816 ip6_sprintf(&eip6
->ip6_src
),
1817 ip6_sprintf(&eip6
->ip6_dst
),
1818 ntohs(eip6
->ip6_plen
));
1821 /* verify the version number of the inner packet */
1822 if ((eip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1823 ++pim6stat
.pim6s_rcv_badregisters
;
1825 log(LOG_DEBUG
, "pim6_input: invalid IP version (%d) "
1826 "of the inner packet\n",
1827 (eip6
->ip6_vfc
& IPV6_VERSION
));
1830 return(IPPROTO_NONE
);
1833 /* verify the inner packet is destined to a mcast group */
1834 if (!IN6_IS_ADDR_MULTICAST(&eip6
->ip6_dst
)) {
1835 ++pim6stat
.pim6s_rcv_badregisters
;
1837 if (mrt6debug
& DEBUG_PIM
)
1839 "pim6_input: inner packet of register "
1840 "is not multicast %s\n",
1841 ip6_sprintf(&eip6
->ip6_dst
));
1844 return(IPPROTO_DONE
);
1848 * make a copy of the whole header to pass to the daemon later.
1850 mcp
= m_copy(m
, 0, off
+ PIM6_REG_MINLEN
);
1854 "pim6_input: pim register: "
1855 "could not copy register head\n");
1858 return(IPPROTO_DONE
);
1862 * forward the inner ip6 packet; point m_data at the inner ip6.
1864 m_adj(m
, off
+ PIM_MINLEN
);
1866 if (mrt6debug
& DEBUG_PIM
) {
1868 "pim6_input: forwarding decapsulated register: "
1869 "src %s, dst %s, mif %d\n",
1870 ip6_sprintf(&eip6
->ip6_src
),
1871 ip6_sprintf(&eip6
->ip6_dst
),
1879 dlil_output(lo_ifp
, PF_INET6
, m
, 0, (struct sockaddr
*)&dst
, 0);
1882 printf("Warning: pim6_input call to dlil_find_dltag failed!\n");
1886 (void) if_simloop(mif6table
[reg_mif_num
].m6_ifp
, m
,
1887 dst
.sin6_family
, NULL
);
1890 /* prepare the register head to send to the mrouting daemon */
1895 * Pass the PIM message up to the daemon; if it is a register message
1896 * pass the 'head' only up to the daemon. This includes the
1897 * encapsulator ip6 header, pim header, register header and the
1898 * encapsulated ip6 header.
1900 pim6_input_to_daemon
:
1901 rip6_input(&m
, offp
);
1902 return(IPPROTO_DONE
);