2 * Copyright (c) 2003-2013 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>
101 #include <netinet/in.h>
102 #include <netinet/in_var.h>
104 #include <netinet/ip6.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/ip6_mroute.h>
108 #include <netinet/icmp6.h>
109 #include <netinet6/pim6.h>
110 #include <netinet6/pim6_var.h>
113 #include <security/mac.h>
117 static MALLOC_DEFINE(M_MRTABLE
, "mf6c", "multicast forwarding cache entry");
120 #define M_HASCL(m) ((m)->m_flags & M_EXT)
122 static int ip6_mdq(struct mbuf
*, struct ifnet
*, struct mf6c
*);
123 static void phyint_send(struct ip6_hdr
*, struct mif6
*, struct mbuf
*);
125 static int set_pim6(int *);
126 static int socket_send(struct socket
*, struct mbuf
*,
127 struct sockaddr_in6
*);
128 static int register_send(struct ip6_hdr
*, struct mif6
*,
132 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
133 * except for netstat or debugging purposes.
135 struct socket
*ip6_mrouter
= NULL
;
136 int ip6_mrouter_ver
= 0;
137 int ip6_mrtproto
= IPPROTO_PIM
; /* for netstat only */
141 struct mrt6stat mrt6stat
;
143 #define NO_RTE_FOUND 0x1
144 #define RTE_FOUND 0x2
146 struct mf6c
*mf6ctable
[MF6CTBLSIZ
];
147 u_char n6expire
[MF6CTBLSIZ
];
148 static struct mif6 mif6table
[MAXMIFS
];
150 u_int mrt6debug
= 0; /* debug level */
151 #define DEBUG_MFC 0x02
152 #define DEBUG_FORWARD 0x04
153 #define DEBUG_EXPIRE 0x08
154 #define DEBUG_XMIT 0x10
155 #define DEBUG_REG 0x20
156 #define DEBUG_PIM 0x40
159 static void expire_upcalls(void *);
161 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
162 #define UPCALL_EXPIRE 6 /* number of timeouts */
166 extern struct socket
*ip_mrouter
;
171 * 'Interfaces' associated with decapsulator (so we can tell
172 * packets that went through it from ones that get reflected
173 * by a broken gateway). These interfaces are never linked into
174 * the system ifnet list & no routes point to them. I.e., packets
175 * can't be sent this way. They only exist as a placeholder for
176 * multicast source verification.
178 struct ifnet multicast_register_if
;
180 #define ENCAP_HOPS 64
185 static mifi_t nummifs
= 0;
186 static mifi_t reg_mif_num
= (mifi_t
)-1;
188 static struct pim6stat pim6stat
;
192 * Hash function for a source, group entry
194 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
195 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
196 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
197 (g).s6_addr32[2] ^ (g).s6_addr32[3])
200 * Find a route for a given origin IPv6 address and Multicast group address.
201 * Quality of service parameter to be added in the future!!!
204 #define MF6CFIND(o, g, rt) do { \
205 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
207 mrt6stat.mrt6s_mfc_lookups++; \
209 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
210 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
211 (_rt->mf6c_stall == NULL)) { \
215 _rt = _rt->mf6c_next; \
218 mrt6stat.mrt6s_mfc_misses++; \
223 * Macros to compute elapsed time efficiently
224 * Borrowed from Van Jacobson's scheduling code
226 #define TV_DELTA(a, b, delta) do { \
229 delta = (a).tv_usec - (b).tv_usec; \
230 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
239 delta += (1000000 * xxs); \
244 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
245 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
248 #define UPCALL_MAX 50
249 u_int32_t upcall_data
[UPCALL_MAX
+ 1];
250 static void collate();
251 #endif /* UPCALL_TIMING */
253 static int get_sg_cnt(struct sioc_sg_req6
*);
254 static int get_mif6_cnt(void *, int);
255 static int ip6_mrouter_init(struct socket
*, int, int);
256 static int add_m6if(struct mif6ctl
*);
257 static int del_m6if(mifi_t
*);
258 static int add_m6fc(struct mf6cctl
*);
259 static int del_m6fc(struct mf6cctl
*);
262 * Handle MRT setsockopt commands to modify the multicast routing tables.
265 ip6_mrouter_set(so
, sopt
)
267 struct sockopt
*sopt
;
275 if (so
!= ip6_mrouter
&& sopt
->sopt_name
!= MRT6_INIT
)
278 switch (sopt
->sopt_name
) {
283 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
287 error
= ip6_mrouter_init(so
, optval
, sopt
->sopt_name
);
290 error
= ip6_mrouter_done();
293 error
= sooptcopyin(sopt
, &mifc
, sizeof(mifc
), sizeof(mifc
));
296 error
= add_m6if(&mifc
);
299 error
= sooptcopyin(sopt
, &mfcc
, sizeof(mfcc
), sizeof(mfcc
));
302 error
= add_m6fc(&mfcc
);
305 error
= sooptcopyin(sopt
, &mfcc
, sizeof(mfcc
), sizeof(mfcc
));
308 error
= del_m6fc(&mfcc
);
311 error
= sooptcopyin(sopt
, &mifi
, sizeof(mifi
), sizeof(mifi
));
314 error
= del_m6if(&mifi
);
317 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
321 error
= set_pim6(&optval
);
332 * Handle MRT getsockopt commands
335 ip6_mrouter_get(so
, sopt
)
337 struct sockopt
*sopt
;
341 if (so
!= ip6_mrouter
) return EACCES
;
343 switch (sopt
->sopt_name
) {
345 error
= sooptcopyout(sopt
, &pim6
, sizeof(pim6
));
352 * Handle ioctl commands to obtain information from the cache
355 mrt6_ioctl(u_long cmd
, caddr_t data
)
360 case SIOCGETSGCNT_IN6
: { /* struct sioc_sg_req6 */
361 struct sioc_sg_req6 req
;
363 bcopy(data
, &req
, sizeof (req
));
364 error
= get_sg_cnt(®
);
365 bcopy(&req
, data
, sizeof (req
));
369 case SIOCGETMIFCNT_IN6_32
: /* struct sioc_mif_req6_32 */
370 case SIOCGETMIFCNT_IN6_64
: /* struct sioc_mif_req6_64 */
371 return (get_mif6_cnt(data
, cmd
== SIOCGETMIFCNT_IN6_64
));
382 * returns the packet, byte, rpf-failure count for the source group provided
386 struct sioc_sg_req6
*req
;
390 MF6CFIND(req
->src
.sin6_addr
, req
->grp
.sin6_addr
, rt
);
392 req
->pktcnt
= rt
->mf6c_pkt_cnt
;
393 req
->bytecnt
= rt
->mf6c_byte_cnt
;
394 req
->wrong_if
= rt
->mf6c_wrong_if
;
398 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
405 * returns the input and output packet and byte counts on the mif provided
408 get_mif6_cnt(void *data
, int p64
)
411 struct sioc_mif_req6_64
*req
= data
;
414 bcopy(&req
->mifi
, &mifi
, sizeof (mifi
));
418 bcopy(&mif6table
[mifi
].m6_pkt_in
, &req
->icount
,
419 sizeof (req
->icount
));
420 bcopy(&mif6table
[mifi
].m6_pkt_out
, &req
->ocount
,
421 sizeof (req
->ocount
));
422 bcopy(&mif6table
[mifi
].m6_bytes_in
, &req
->ibytes
,
423 sizeof (req
->ibytes
));
424 bcopy(&mif6table
[mifi
].m6_bytes_out
, &req
->obytes
,
425 sizeof (req
->obytes
));
427 struct sioc_mif_req6_32
*req
= data
;
430 bcopy(&req
->mifi
, &mifi
, sizeof (mifi
));
434 bcopy(&mif6table
[mifi
].m6_pkt_in
, &req
->icount
,
435 sizeof (req
->icount
));
436 bcopy(&mif6table
[mifi
].m6_pkt_out
, &req
->ocount
,
437 sizeof (req
->ocount
));
438 bcopy(&mif6table
[mifi
].m6_bytes_in
, &req
->ibytes
,
439 sizeof (req
->ibytes
));
440 bcopy(&mif6table
[mifi
].m6_bytes_out
, &req
->obytes
,
441 sizeof (req
->obytes
));
450 if ((*i
!= 1) && (*i
!= 0))
459 * Enable multicast routing
462 ip6_mrouter_init(so
, v
, cmd
)
470 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
471 so
->so_type
, so
->so_proto
->pr_protocol
);
474 if (so
->so_type
!= SOCK_RAW
||
475 so
->so_proto
->pr_protocol
!= IPPROTO_ICMPV6
)
479 return (ENOPROTOOPT
);
481 if (ip6_mrouter
!= NULL
) return EADDRINUSE
;
484 ip6_mrouter_ver
= cmd
;
486 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
487 bzero((caddr_t
)n6expire
, sizeof(n6expire
));
489 pim6
= 0;/* used for stubbing out/in pim stuff */
491 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
495 log(LOG_DEBUG
, "ip6_mrouter_init\n");
502 * Disable multicast routing
514 * For each phyint in use, disable promiscuous reception of all IPv6
520 * If there is still IPv4 multicast routing daemon,
521 * we remain interfaces to receive all muliticasted packets.
522 * XXX: there may be an interface in which the IPv4 multicast
523 * daemon is not interested...
529 for (mifi
= 0; mifi
< nummifs
; mifi
++) {
530 if (mif6table
[mifi
].m6_ifp
&&
531 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
533 if_allmulti(mif6table
[mifi
].m6_ifp
, 0);
537 struct in6_ifreq ifr
;
539 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
540 ifr
.ifr_addr
.sin6_addr
= in6addr_any
;
541 ifp
= mif6table
[mifi
].m6_ifp
;
542 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, &ifr
);
548 bzero((caddr_t
)mif6table
, sizeof(mif6table
));
551 pim6
= 0; /* used to stub out/in pim specific code */
553 untimeout(expire_upcalls
, (caddr_t
)NULL
);
556 * Free all multicast forwarding cache entries.
557 *###LD 5/27 needs locking
559 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
564 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
565 struct rtdetq
*n
= rte
->next
;
568 FREE(rte
, M_MRTABLE
);
573 FREE(frt
, M_MRTABLE
);
577 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
580 * Reset de-encapsulation cache
590 log(LOG_DEBUG
, "ip6_mrouter_done\n");
596 static struct sockaddr_in6 sin6
= { sizeof(sin6
), AF_INET6
,
597 0, 0, IN6ADDR_ANY_INIT
, 0};
600 * Add a mif to the mif table
604 struct mif6ctl
*mifcp
;
610 struct tbf
*m_tbf
= tbftable
+ mifcp
->mif6c_mifi
;
613 if (mifcp
->mif6c_mifi
>= MAXMIFS
)
615 mifp
= mif6table
+ mifcp
->mif6c_mifi
;
617 return (EADDRINUSE
); /* XXX: is it appropriate? */
618 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
> if_index
)
621 ifnet_head_lock_shared();
622 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
> if_index
) {
626 ifp
= ifindex2ifnet
[mifcp
->mif6c_pifi
];
632 if (mifcp
->mif6c_flags
& MIFF_REGISTER
) {
633 if (reg_mif_num
== (mifi_t
)-1) {
634 multicast_register_if
.if_name
= "register_mif";
635 multicast_register_if
.if_flags
|= IFF_LOOPBACK
;
636 multicast_register_if
.if_index
= mifcp
->mif6c_mifi
;
637 reg_mif_num
= mifcp
->mif6c_mifi
;
640 ifp
= &multicast_register_if
;
644 /* Make sure the interface supports multicast */
645 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
648 error
= if_allmulti(ifp
, 1);
653 mifp
->m6_flags
= mifcp
->mif6c_flags
;
656 /* initialize per mif pkt counters */
658 mifp
->m6_pkt_out
= 0;
659 mifp
->m6_bytes_in
= 0;
660 mifp
->m6_bytes_out
= 0;
662 /* Adjust nummifs up if the mifi is higher than nummifs */
663 if (nummifs
<= mifcp
->mif6c_mifi
)
664 nummifs
= mifcp
->mif6c_mifi
+ 1;
669 "add_mif #%d, phyint %s\n",
678 * Delete a mif from the mif table
684 struct mif6
*mifp
= mif6table
+ *mifip
;
688 if (*mifip
>= nummifs
)
690 if (mifp
->m6_ifp
== NULL
)
694 if (!(mifp
->m6_flags
& MIFF_REGISTER
)) {
696 * XXX: what if there is yet IPv4 multicast daemon
697 * using the interface?
704 bzero((caddr_t
)mifp
, sizeof(*mifp
));
706 /* Adjust nummifs down */
707 for (mifi
= nummifs
; mifi
> 0; mifi
--)
708 if (mif6table
[mifi
- 1].m6_ifp
)
715 log(LOG_DEBUG
, "del_m6if %d, nummifs %d\n", *mifip
, nummifs
);
726 struct mf6cctl
*mfccp
;
733 MF6CFIND(mfccp
->mf6cc_origin
.sin6_addr
,
734 mfccp
->mf6cc_mcastgrp
.sin6_addr
, rt
);
736 /* If an entry already exists, just update the fields */
739 if (mrt6debug
& DEBUG_MFC
)
741 "add_m6fc no upcall h %d o %s g %s p %x\n",
742 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
743 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
744 mfccp
->mf6cc_parent
);
747 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
748 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
753 * Find the entry for which the upcall was made and update
755 hash
= MF6CHASH(mfccp
->mf6cc_origin
.sin6_addr
,
756 mfccp
->mf6cc_mcastgrp
.sin6_addr
);
757 for (rt
= mf6ctable
[hash
], nstl
= 0; rt
; rt
= rt
->mf6c_next
) {
758 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
759 &mfccp
->mf6cc_origin
.sin6_addr
) &&
760 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
761 &mfccp
->mf6cc_mcastgrp
.sin6_addr
) &&
762 (rt
->mf6c_stall
!= NULL
)) {
766 "add_m6fc: %s o %s g %s p %x dbx %p\n",
767 "multiple kernel entries",
768 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
769 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
770 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
773 if (mrt6debug
& DEBUG_MFC
)
775 "add_m6fc o %s g %s p %x dbg %x\n",
776 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
777 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
778 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
781 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
782 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
783 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
784 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
785 /* initialize pkt counters per src-grp */
786 rt
->mf6c_pkt_cnt
= 0;
787 rt
->mf6c_byte_cnt
= 0;
788 rt
->mf6c_wrong_if
= 0;
790 rt
->mf6c_expire
= 0; /* Don't clean this guy up */
793 /* free packets Qed at the end of this entry */
794 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
795 struct rtdetq
*n
= rte
->next
;
796 ip6_mdq(rte
->m
, rte
->ifp
, rt
);
800 #endif /* UPCALL_TIMING */
801 FREE(rte
, M_MRTABLE
);
804 rt
->mf6c_stall
= NULL
;
809 * It is possible that an entry is being inserted without an upcall
813 if (mrt6debug
& DEBUG_MFC
)
814 log(LOG_DEBUG
,"add_mfc no upcall h %d o %s g %s p %x\n",
816 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
817 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
818 mfccp
->mf6cc_parent
);
821 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
823 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
824 &mfccp
->mf6cc_origin
.sin6_addr
)&&
825 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
826 &mfccp
->mf6cc_mcastgrp
.sin6_addr
)) {
828 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
829 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
830 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
831 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
832 /* initialize pkt counters per src-grp */
833 rt
->mf6c_pkt_cnt
= 0;
834 rt
->mf6c_byte_cnt
= 0;
835 rt
->mf6c_wrong_if
= 0;
843 /* no upcall, so make a new entry */
844 rt
= (struct mf6c
*)_MALLOC(sizeof(*rt
), M_MRTABLE
,
850 /* insert new entry at head of hash chain */
851 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
852 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
853 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
854 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
855 /* initialize pkt counters per src-grp */
856 rt
->mf6c_pkt_cnt
= 0;
857 rt
->mf6c_byte_cnt
= 0;
858 rt
->mf6c_wrong_if
= 0;
860 rt
->mf6c_stall
= NULL
;
862 /* link into table */
863 rt
->mf6c_next
= mf6ctable
[hash
];
864 mf6ctable
[hash
] = rt
;
872 * collect delay statistics on the upcalls
886 TV_DELTA(tp
, *t
, delta
);
895 #endif /* UPCALL_TIMING */
898 * Delete an mfc entry
902 struct mf6cctl
*mfccp
;
904 struct sockaddr_in6 origin
;
905 struct sockaddr_in6 mcastgrp
;
910 origin
= mfccp
->mf6cc_origin
;
911 mcastgrp
= mfccp
->mf6cc_mcastgrp
;
912 hash
= MF6CHASH(origin
.sin6_addr
, mcastgrp
.sin6_addr
);
915 if (mrt6debug
& DEBUG_MFC
)
916 log(LOG_DEBUG
,"del_m6fc orig %s mcastgrp %s\n",
917 ip6_sprintf(&origin
.sin6_addr
),
918 ip6_sprintf(&mcastgrp
.sin6_addr
));
922 nptr
= &mf6ctable
[hash
];
923 while ((rt
= *nptr
) != NULL
) {
924 if (IN6_ARE_ADDR_EQUAL(&origin
.sin6_addr
,
925 &rt
->mf6c_origin
.sin6_addr
) &&
926 IN6_ARE_ADDR_EQUAL(&mcastgrp
.sin6_addr
,
927 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
928 rt
->mf6c_stall
== NULL
)
931 nptr
= &rt
->mf6c_next
;
934 return EADDRNOTAVAIL
;
937 *nptr
= rt
->mf6c_next
;
945 socket_send(s
, mm
, src
)
948 struct sockaddr_in6
*src
;
950 //### LD 5/27/04 needs locking!
953 if (sbappendaddr(&s
->so_rcv
,
954 (struct sockaddr
*)src
,
955 mm
, (struct mbuf
*)0, NULL
) != 0) {
964 * IPv6 multicast forwarding function. This function assumes that the packet
965 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
966 * pointed to by "ifp", and the packet is to be relayed to other networks
967 * that have members of the packet's destination IPv6 multicast group.
969 * The packet is returned unscathed to the caller, unless it is
970 * erroneous, in which case a non-zero return value tells the caller to
975 ip6_mforward(ip6
, ifp
, m
)
984 uint64_t curtime
= net_uptime();
987 if (mrt6debug
& DEBUG_FORWARD
)
988 log(LOG_DEBUG
, "ip6_mforward: src %s, dst %s, ifindex %d\n",
989 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
),
994 * Don't forward a packet with Hop limit of zero or one,
995 * or a packet destined to a local-only group.
997 if (ip6
->ip6_hlim
<= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
) ||
998 IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
1003 * Source address check: do not forward packets with unspecified
1004 * source. It was discussed in July 2000, on ipngwg mailing list.
1005 * This is rather more serious than unicast cases, because some
1006 * MLD packets can be sent with the unspecified source address
1007 * (although such packets must normally set 1 to the hop limit field).
1009 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1010 ip6stat
.ip6s_cantforward
++;
1011 if (ip6_log_time
+ ip6_log_interval
< curtime
) {
1012 ip6_log_time
= curtime
;
1015 "from %s to %s nxt %d received on %s\n",
1016 ip6_sprintf(&ip6
->ip6_src
),
1017 ip6_sprintf(&ip6
->ip6_dst
),
1019 if_name(m
->m_pkthdr
.rcvif
));
1025 * Determine forwarding mifs from the forwarding cache table
1027 MF6CFIND(ip6
->ip6_src
, ip6
->ip6_dst
, rt
);
1029 /* Entry exists, so forward if necessary */
1031 return (ip6_mdq(m
, ifp
, rt
));
1034 * If we don't have a route for packet's origin,
1035 * Make a copy of the packet &
1036 * send message to routing daemon
1047 #endif /* UPCALL_TIMING */
1049 mrt6stat
.mrt6s_no_route
++;
1051 if (mrt6debug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1052 log(LOG_DEBUG
, "ip6_mforward: no rte s %s g %s\n",
1053 ip6_sprintf(&ip6
->ip6_src
),
1054 ip6_sprintf(&ip6
->ip6_dst
));
1058 * Allocate mbufs early so that we don't do extra work if we
1059 * are just going to fail anyway.
1061 rte
= (struct rtdetq
*)_MALLOC(sizeof(*rte
), M_MRTABLE
,
1066 mb0
= m_copy(m
, 0, M_COPYALL
);
1068 * Pullup packet header if needed before storing it,
1069 * as other references may modify it in the meantime.
1072 (M_HASCL(mb0
) || mb0
->m_len
< sizeof(struct ip6_hdr
)))
1073 mb0
= m_pullup(mb0
, sizeof(struct ip6_hdr
));
1075 FREE(rte
, M_MRTABLE
);
1079 /* is there an upcall waiting for this packet? */
1080 hash
= MF6CHASH(ip6
->ip6_src
, ip6
->ip6_dst
);
1081 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
1082 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
,
1083 &rt
->mf6c_origin
.sin6_addr
) &&
1084 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1085 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
1086 (rt
->mf6c_stall
!= NULL
))
1093 struct omrt6msg
*oim
;
1096 /* no upcall, so make a new entry */
1097 rt
= (struct mf6c
*)_MALLOC(sizeof(*rt
), M_MRTABLE
,
1100 FREE(rte
, M_MRTABLE
);
1105 * Make a copy of the header to send to the user
1108 mm
= m_copy(mb0
, 0, sizeof(struct ip6_hdr
));
1111 FREE(rte
, M_MRTABLE
);
1113 FREE(rt
, M_MRTABLE
);
1118 * Send message to routing daemon
1120 sin6
.sin6_addr
= ip6
->ip6_src
;
1126 switch (ip6_mrouter_ver
) {
1129 oim
= mtod(mm
, struct omrt6msg
*);
1130 oim
->im6_msgtype
= MRT6MSG_NOCACHE
;
1135 im
= mtod(mm
, struct mrt6msg
*);
1136 im
->im6_msgtype
= MRT6MSG_NOCACHE
;
1140 FREE(rte
, M_MRTABLE
);
1142 FREE(rt
, M_MRTABLE
);
1147 if (mrt6debug
& DEBUG_FORWARD
)
1149 "getting the iif info in the kernel\n");
1152 for (mifp
= mif6table
, mifi
= 0;
1153 mifi
< nummifs
&& mifp
->m6_ifp
!= ifp
;
1157 switch (ip6_mrouter_ver
) {
1160 oim
->im6_mif
= mifi
;
1168 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1169 log(LOG_WARNING
, "ip6_mforward: ip6_mrouter "
1170 "socket queue full\n");
1171 mrt6stat
.mrt6s_upq_sockfull
++;
1172 FREE(rte
, M_MRTABLE
);
1174 FREE(rt
, M_MRTABLE
);
1178 mrt6stat
.mrt6s_upcalls
++;
1180 /* insert new entry at head of hash chain */
1181 bzero(rt
, sizeof(*rt
));
1182 rt
->mf6c_origin
.sin6_family
= AF_INET6
;
1183 rt
->mf6c_origin
.sin6_len
= sizeof(struct sockaddr_in6
);
1184 rt
->mf6c_origin
.sin6_addr
= ip6
->ip6_src
;
1185 rt
->mf6c_mcastgrp
.sin6_family
= AF_INET6
;
1186 rt
->mf6c_mcastgrp
.sin6_len
= sizeof(struct sockaddr_in6
);
1187 rt
->mf6c_mcastgrp
.sin6_addr
= ip6
->ip6_dst
;
1188 rt
->mf6c_expire
= UPCALL_EXPIRE
;
1190 rt
->mf6c_parent
= MF6C_INCOMPLETE_PARENT
;
1192 /* link into table */
1193 rt
->mf6c_next
= mf6ctable
[hash
];
1194 mf6ctable
[hash
] = rt
;
1195 /* Add this entry to the end of the queue */
1196 rt
->mf6c_stall
= rte
;
1198 /* determine if q has overflowed */
1202 for (p
= &rt
->mf6c_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1203 if (++npkts
> MAX_UPQ6
) {
1204 mrt6stat
.mrt6s_upq_ovflw
++;
1205 FREE(rte
, M_MRTABLE
);
1210 /* Add this entry to the end of the queue */
1219 #endif /* UPCALL_TIMING */
1227 * Clean up cache entries if upcalls are not serviced
1228 * Call from the Slow Timeout mechanism, every half second.
1232 __unused
void *unused
)
1235 struct mf6c
*mfc
, **nptr
;
1238 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
1239 if (n6expire
[i
] == 0)
1241 nptr
= &mf6ctable
[i
];
1242 while ((mfc
= *nptr
) != NULL
) {
1243 rte
= mfc
->mf6c_stall
;
1245 * Skip real cache entries
1246 * Make sure it wasn't marked to not expire (shouldn't happen)
1250 mfc
->mf6c_expire
!= 0 &&
1251 --mfc
->mf6c_expire
== 0) {
1253 if (mrt6debug
& DEBUG_EXPIRE
)
1254 log(LOG_DEBUG
, "expire_upcalls: expiring (%s %s)\n",
1255 ip6_sprintf(&mfc
->mf6c_origin
.sin6_addr
),
1256 ip6_sprintf(&mfc
->mf6c_mcastgrp
.sin6_addr
));
1259 * drop all the packets
1260 * free the mbuf with the pkt, if, timing info
1263 struct rtdetq
*n
= rte
->next
;
1265 FREE(rte
, M_MRTABLE
);
1267 } while (rte
!= NULL
);
1268 mrt6stat
.mrt6s_cache_cleanups
++;
1271 *nptr
= mfc
->mf6c_next
;
1272 FREE(mfc
, M_MRTABLE
);
1274 nptr
= &mfc
->mf6c_next
;
1279 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1283 * Packet forwarding routine once entry in the cache is made
1291 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1294 int plen
= m
->m_pkthdr
.len
;
1295 struct in6_addr src0
, dst0
; /* copies for local work */
1296 u_int32_t iszone
, idzone
, oszone
, odzone
;
1300 * Macro to send packet on mif. Since RSVP packets don't get counted on
1301 * input, they shouldn't get counted on output, so statistics keeping is
1305 #define MC6_SEND(ip6, mifp, m) do { \
1306 if ((mifp)->m6_flags & MIFF_REGISTER) \
1307 register_send((ip6), (mifp), (m)); \
1309 phyint_send((ip6), (mifp), (m)); \
1313 * Don't forward if it didn't arrive from the parent mif
1316 mifi
= rt
->mf6c_parent
;
1317 if ((mifi
>= nummifs
) || (mif6table
[mifi
].m6_ifp
!= ifp
)) {
1318 /* came in the wrong interface */
1320 if (mrt6debug
& DEBUG_FORWARD
)
1322 "wrong if: ifid %d mifi %d mififid %x\n",
1323 ifp
->if_index
, mifi
,
1324 mif6table
[mifi
].m6_ifp
->if_index
);
1326 mrt6stat
.mrt6s_wrong_if
++;
1327 rt
->mf6c_wrong_if
++;
1329 * If we are doing PIM processing, and we are forwarding
1330 * packets on this interface, send a message to the
1333 /* have to make sure this is a valid mif */
1334 if (mifi
< nummifs
&& mif6table
[mifi
].m6_ifp
)
1335 if (pim6
&& (m
->m_flags
& M_LOOP
) == 0) {
1337 * Check the M_LOOP flag to avoid an
1338 * unnecessary PIM assert.
1339 * XXX: M_LOOP is an ad-hoc hack...
1341 static struct sockaddr_in6 addr
=
1342 { sizeof(addr
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0};
1347 struct omrt6msg
*oim
;
1350 mm
= m_copy(m
, 0, sizeof(struct ip6_hdr
));
1353 mm
->m_len
< sizeof(struct ip6_hdr
)))
1354 mm
= m_pullup(mm
, sizeof(struct ip6_hdr
));
1362 switch (ip6_mrouter_ver
) {
1365 oim
= mtod(mm
, struct omrt6msg
*);
1366 oim
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1371 im
= mtod(mm
, struct mrt6msg
*);
1372 im
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1380 for (mifp
= mif6table
, iif
= 0;
1381 iif
< nummifs
&& mifp
&&
1382 mifp
->m6_ifp
!= ifp
;
1386 switch (ip6_mrouter_ver
) {
1390 addr
.sin6_addr
= oim
->im6_src
;
1395 addr
.sin6_addr
= im
->im6_src
;
1399 mrt6stat
.mrt6s_upcalls
++;
1401 if (socket_send(ip6_mrouter
, mm
, &addr
) < 0) {
1404 log(LOG_WARNING
, "mdq, ip6_mrouter socket queue full\n");
1406 ++mrt6stat
.mrt6s_upq_sockfull
;
1408 } /* if socket Q full */
1411 } /* if wrong iif */
1413 /* If I sourced this packet, it counts as output, else it was input. */
1414 if (m
->m_pkthdr
.rcvif
== NULL
) {
1415 /* XXX: is rcvif really NULL when output?? */
1416 mif6table
[mifi
].m6_pkt_out
++;
1417 mif6table
[mifi
].m6_bytes_out
+= plen
;
1419 mif6table
[mifi
].m6_pkt_in
++;
1420 mif6table
[mifi
].m6_bytes_in
+= plen
;
1423 rt
->mf6c_byte_cnt
+= plen
;
1426 * For each mif, forward a copy of the packet if there are group
1427 * members downstream on the interface.
1429 src0
= ip6
->ip6_src
;
1430 dst0
= ip6
->ip6_dst
;
1431 if ((error
= in6_setscope(&src0
, ifp
, &iszone
)) != 0 ||
1432 (error
= in6_setscope(&dst0
, ifp
, &idzone
)) != 0) {
1433 ip6stat
.ip6s_badscope
++;
1436 for (mifp
= mif6table
, mifi
= 0; mifi
< nummifs
; mifp
++, mifi
++) {
1437 if (IF_ISSET(mifi
, &rt
->mf6c_ifset
)) {
1439 * check if the outgoing packet is going to break
1441 * XXX For packets through PIM register tunnel
1442 * interface, we believe a routing daemon.
1444 if (!(mif6table
[rt
->mf6c_parent
].m6_flags
&
1446 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
1447 if (in6_setscope(&src0
, mif6table
[mifi
].m6_ifp
,
1449 in6_setscope(&dst0
, mif6table
[mifi
].m6_ifp
,
1453 ip6stat
.ip6s_badscope
++;
1459 mifp
->m6_bytes_out
+= plen
;
1460 MC6_SEND(ip6
, mifp
, m
);
1467 phyint_send(ip6
, mifp
, m
)
1468 struct ip6_hdr
*ip6
;
1472 struct mbuf
*mb_copy
;
1473 struct ifnet
*ifp
= mifp
->m6_ifp
;
1475 static struct route_in6 ro
;
1476 struct in6_multi
*in6m
;
1477 struct sockaddr_in6
*dst6
;
1480 * Make a new reference to the packet; make sure that
1481 * the IPv6 header is actually copied, not just referenced,
1482 * so that ip6_output() only scribbles on the copy.
1484 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1486 (M_HASCL(mb_copy
) || mb_copy
->m_len
< sizeof(struct ip6_hdr
)))
1487 mb_copy
= m_pullup(mb_copy
, sizeof(struct ip6_hdr
));
1488 if (mb_copy
== NULL
) {
1491 /* set MCAST flag to the outgoing packet */
1492 mb_copy
->m_flags
|= M_MCAST
;
1495 * If we sourced the packet, call ip6_output since we may devide
1496 * the packet into fragments when the packet is too big for the
1497 * outgoing interface.
1498 * Otherwise, we can simply send the packet to the interface
1501 if (m
->m_pkthdr
.rcvif
== NULL
) {
1502 struct ip6_moptions
*im6o
;
1504 im6o
= ip6_allocmoptions(M_DONTWAIT
);
1510 im6o
->im6o_multicast_ifp
= ifp
;
1511 /* XXX: ip6_output will override ip6->ip6_hlim */
1512 im6o
->im6o_multicast_hlim
= ip6
->ip6_hlim
;
1513 im6o
->im6o_multicast_loop
= 1;
1514 error
= ip6_output(mb_copy
, NULL
, &ro
, IPV6_FORWARDING
,
1519 if (mrt6debug
& DEBUG_XMIT
)
1520 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1521 mifp
- mif6table
, error
);
1527 * If we belong to the destination multicast group
1528 * on the outgoing interface, loop back a copy.
1530 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
1531 in6_multihead_lock_shared();
1532 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
1533 in6_multihead_lock_done();
1536 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1537 dst6
->sin6_family
= AF_INET6
;
1538 dst6
->sin6_addr
= ip6
->ip6_dst
;
1539 ip6_mloopback(NULL
, ifp
, m
, (struct sockaddr_in6
*)&ro
.ro_dst
,
1543 * Put the packet into the sending queue of the outgoing interface
1544 * if it would fit in the MTU of the interface.
1546 if (mb_copy
->m_pkthdr
.len
<= ifp
->if_mtu
|| ifp
->if_mtu
< IPV6_MMTU
) {
1547 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1548 dst6
->sin6_family
= AF_INET6
;
1549 dst6
->sin6_addr
= ip6
->ip6_dst
;
1551 * We just call if_output instead of nd6_output here, since
1552 * we need no ND for a multicast forwarded packet...right?
1555 /* Make sure the HW checksum flags are cleaned before sending the packet */
1557 mb_copy
->m_pkthdr
.rcvif
= 0;
1558 mb_copy
->m_pkthdr
.csum_data
= 0;
1559 mb_copy
->m_pkthdr
.csum_flags
= 0;
1561 error
= dlil_output(ifp
, PF_INET6
, mb_copy
,
1562 NULL
, (struct sockaddr
*)&ro
.ro_dst
, 0, NULL
);
1564 error
= (*ifp
->if_output
)(ifp
, mb_copy
,
1565 (struct sockaddr
*)&ro
.ro_dst
,
1569 if (mrt6debug
& DEBUG_XMIT
)
1570 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1571 mifp
- mif6table
, error
);
1575 * pMTU discovery is intentionally disabled by default, since
1576 * various router may notify pMTU in multicast, which can be
1577 * a DDoS to a router
1580 icmp6_error(mb_copy
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
1583 if (mrt6debug
& DEBUG_XMIT
) {
1585 "phyint_send: packet too big on %s o %s "
1586 "g %s size %d(discarded)\n",
1588 ip6_sprintf(&ip6
->ip6_src
),
1589 ip6_sprintf(&ip6
->ip6_dst
),
1590 mb_copy
->m_pkthdr
.len
);
1593 #endif /* MRT6DEBUG */
1594 m_freem(mb_copy
); /* simply discard the packet */
1600 register_send(ip6
, mif
, m
)
1601 struct ip6_hdr
*ip6
;
1606 int i
, len
= m
->m_pkthdr
.len
;
1607 static struct sockaddr_in6 addr
= { sizeof(addr
), AF_INET6
,
1608 0, 0, IN6ADDR_ANY_INIT
, 0};
1609 struct mrt6msg
*im6
;
1613 log(LOG_DEBUG
, "** IPv6 register_send **\n src %s dst %s\n",
1614 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
));
1616 ++pim6stat
.pim6s_snd_registers
;
1618 /* Make a copy of the packet to send to the user level process */
1619 MGETHDR(mm
, M_DONTWAIT
, MT_HEADER
);
1624 mac_create_mbuf_multicast_encap(m
, mif
->m6_ifp
, mm
);
1627 mm
->m_pkthdr
.rcvif
= NULL
;
1628 mm
->m_data
+= max_linkhdr
;
1629 mm
->m_len
= sizeof(struct ip6_hdr
);
1631 if ((mm
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1635 i
= MHLEN
- M_LEADINGSPACE(mm
);
1638 mm
= m_pullup(mm
, i
);
1643 /* TODO: check it! */
1644 mm
->m_pkthdr
.len
= len
+ sizeof(struct ip6_hdr
);
1647 * Send message to routing daemon
1649 addr
.sin6_addr
= ip6
->ip6_src
;
1651 im6
= mtod(mm
, struct mrt6msg
*);
1652 im6
->im6_msgtype
= MRT6MSG_WHOLEPKT
;
1655 im6
->im6_mif
= mif
- mif6table
;
1657 /* iif info is not given for reg. encap.n */
1658 mrt6stat
.mrt6s_upcalls
++;
1660 if (socket_send(ip6_mrouter
, mm
, &addr
) < 0) {
1664 "register_send: ip6_mrouter socket queue full\n");
1666 ++mrt6stat
.mrt6s_upq_sockfull
;
1673 * PIM sparse mode hook
1674 * Receives the pim control messages, and passes them up to the listening
1675 * socket, using rip6_input.
1676 * The only message processed is the REGISTER pim message; the pim header
1677 * is stripped off, and the inner packet is passed to register_mforward.
1680 pim6_input(struct mbuf
**mp
, int *offp
, int proto
)
1682 struct pim
*pim
; /* pointer to a pim struct */
1683 struct ip6_hdr
*ip6
;
1685 struct mbuf
*m
= *mp
;
1689 ++pim6stat
.pim6s_rcv_total
;
1691 /* Expect 32-bit aligned data pointer on strict-align platforms */
1692 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1694 ip6
= mtod(m
, struct ip6_hdr
*);
1695 pimlen
= m
->m_pkthdr
.len
- *offp
;
1700 if (pimlen
< PIM_MINLEN
) {
1701 ++pim6stat
.pim6s_rcv_tooshort
;
1703 if (mrt6debug
& DEBUG_PIM
)
1704 log(LOG_DEBUG
,"pim6_input: PIM packet too short\n");
1707 return(IPPROTO_DONE
);
1711 * if the packet is at least as big as a REGISTER, go ahead
1712 * and grab the PIM REGISTER header size, to avoid another
1713 * possible m_pullup() later.
1715 * PIM_MINLEN == pimhdr + u_int32 == 8
1716 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1718 minlen
= (pimlen
>= PIM6_REG_MINLEN
) ? PIM6_REG_MINLEN
: PIM_MINLEN
;
1721 * Make sure that the IP6 and PIM headers in contiguous memory, and
1722 * possibly the PIM REGISTER header
1724 #ifndef PULLDOWN_TEST
1725 IP6_EXTHDR_CHECK(m
, off
, minlen
, return IPPROTO_DONE
);
1726 /* adjust pointer */
1727 ip6
= mtod(m
, struct ip6_hdr
*);
1729 /* adjust mbuf to point to the PIM header */
1730 pim
= (struct pim
*)((caddr_t
)ip6
+ off
);
1732 IP6_EXTHDR_GET(pim
, struct pim
*, m
, off
, minlen
);
1734 pim6stat
.pim6s_rcv_tooshort
++;
1735 return IPPROTO_DONE
;
1739 #define PIM6_CHECKSUM
1740 #ifdef PIM6_CHECKSUM
1745 * Validate checksum.
1746 * If PIM REGISTER, exclude the data packet
1748 if (pim
->pim_type
== PIM_REGISTER
)
1749 cksumlen
= PIM_MINLEN
;
1753 if (in6_cksum(m
, IPPROTO_PIM
, off
, cksumlen
)) {
1754 ++pim6stat
.pim6s_rcv_badsum
;
1756 if (mrt6debug
& DEBUG_PIM
)
1758 "pim6_input: invalid checksum\n");
1761 return(IPPROTO_DONE
);
1764 #endif /* PIM_CHECKSUM */
1766 /* PIM version check */
1767 if (pim
->pim_ver
!= PIM_VERSION
) {
1768 ++pim6stat
.pim6s_rcv_badversion
;
1771 "pim6_input: incorrect version %d, expecting %d\n",
1772 pim
->pim_ver
, PIM_VERSION
);
1775 return(IPPROTO_DONE
);
1778 if (pim
->pim_type
== PIM_REGISTER
) {
1780 * since this is a REGISTER, we'll make a copy of the register
1781 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1784 static struct sockaddr_in6 dst
= { sizeof(dst
), AF_INET6
,
1785 0, 0, IN6ADDR_ANY_INIT
, 0 };
1788 struct ip6_hdr
*eip6
;
1791 ++pim6stat
.pim6s_rcv_registers
;
1793 if ((reg_mif_num
>= nummifs
) || (reg_mif_num
== (mifi_t
) -1)) {
1795 if (mrt6debug
& DEBUG_PIM
)
1797 "pim6_input: register mif not set: %d\n",
1801 return(IPPROTO_DONE
);
1804 reghdr
= (u_int32_t
*)(pim
+ 1);
1806 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
1807 goto pim6_input_to_daemon
;
1812 if (pimlen
< PIM6_REG_MINLEN
) {
1813 ++pim6stat
.pim6s_rcv_tooshort
;
1814 ++pim6stat
.pim6s_rcv_badregisters
;
1817 "pim6_input: register packet size too "
1818 "small %d from %s\n",
1819 pimlen
, ip6_sprintf(&ip6
->ip6_src
));
1822 return(IPPROTO_DONE
);
1825 eip6
= (struct ip6_hdr
*) (reghdr
+ 1);
1827 if (mrt6debug
& DEBUG_PIM
)
1829 "pim6_input[register], eip6: %s -> %s, "
1831 ip6_sprintf(&eip6
->ip6_src
),
1832 ip6_sprintf(&eip6
->ip6_dst
),
1833 ntohs(eip6
->ip6_plen
));
1836 /* verify the version number of the inner packet */
1837 if ((eip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1838 ++pim6stat
.pim6s_rcv_badregisters
;
1840 log(LOG_DEBUG
, "pim6_input: invalid IP version (%d) "
1841 "of the inner packet\n",
1842 (eip6
->ip6_vfc
& IPV6_VERSION
));
1845 return(IPPROTO_NONE
);
1848 /* verify the inner packet is destined to a mcast group */
1849 if (!IN6_IS_ADDR_MULTICAST(&eip6
->ip6_dst
)) {
1850 ++pim6stat
.pim6s_rcv_badregisters
;
1852 if (mrt6debug
& DEBUG_PIM
)
1854 "pim6_input: inner packet of register "
1855 "is not multicast %s\n",
1856 ip6_sprintf(&eip6
->ip6_dst
));
1859 return(IPPROTO_DONE
);
1863 * make a copy of the whole header to pass to the daemon later.
1865 mcp
= m_copy(m
, 0, off
+ PIM6_REG_MINLEN
);
1869 "pim6_input: pim register: "
1870 "could not copy register head\n");
1873 return(IPPROTO_DONE
);
1877 * forward the inner ip6 packet; point m_data at the inner ip6.
1879 m_adj(m
, off
+ PIM_MINLEN
);
1881 if (mrt6debug
& DEBUG_PIM
) {
1883 "pim6_input: forwarding decapsulated register: "
1884 "src %s, dst %s, mif %d\n",
1885 ip6_sprintf(&eip6
->ip6_src
),
1886 ip6_sprintf(&eip6
->ip6_dst
),
1894 dlil_output(lo_ifp
, PF_INET6
, m
, 0, (struct sockaddr
*)&dst
, 0, NULL
);
1897 printf("Warning: pim6_input call to dlil_find_dltag failed!\n");
1901 (void) if_simloop(mif6table
[reg_mif_num
].m6_ifp
, m
,
1902 dst
.sin6_family
, NULL
);
1905 /* prepare the register head to send to the mrouting daemon */
1910 * Pass the PIM message up to the daemon; if it is a register message
1911 * pass the 'head' only up to the daemon. This includes the
1912 * encapsulator ip6 header, pim header, register header and the
1913 * encapsulated ip6 header.
1915 pim6_input_to_daemon
:
1916 rip6_input(&m
, offp
);
1917 return(IPPROTO_DONE
);