1 /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.2.2.9 2003/01/23 21:06:47 sam Exp $ */
2 /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
36 * Copyright (c) 1989 Stephen Deering
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
40 * This code is derived from software contributed to Berkeley by
41 * Stephen Deering of Stanford University.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
71 * IP multicast forwarding procedures
73 * Written by David Waitzman, BBN Labs, August 1988.
74 * Modified by Steve Deering, Stanford, February 1989.
75 * Modified by Mark J. Steiglitz, Stanford, May, 1991
76 * Modified by Van Jacobson, LBL, January 1993
77 * Modified by Ajit Thyagarajan, PARC, August 1993
78 * Modified by Bill Fenner, PARC, April 1994
80 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
84 #include "opt_inet6.h"
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/callout.h>
89 #include <sys/malloc.h>
91 #include <sys/socket.h>
92 #include <sys/socketvar.h>
93 #include <sys/sockio.h>
94 #include <sys/protosw.h>
95 #include <sys/errno.h>
97 #include <sys/kernel.h>
98 #include <sys/syslog.h>
99 #include <sys/thread2.h>
102 #include <net/route.h>
103 #include <net/raw_cb.h>
104 #include <net/netisr2.h>
105 #include <net/netmsg2.h>
107 #include <netinet/in.h>
108 #include <netinet/in_var.h>
110 #include <netinet/ip6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/ip6_mroute.h>
113 #include <netinet6/nd6.h>
114 #include <netinet6/pim6.h>
115 #include <netinet6/pim6_var.h>
117 #include <net/net_osdep.h>
119 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
*, struct sockaddr_in6
*);
128 static int register_send(struct ip6_hdr
*, struct mif6
*, struct mbuf
*);
131 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
132 * except for netstat or debugging purposes.
134 struct socket
*ip6_mrouter
= NULL
;
135 int ip6_mrouter_ver
= 0;
136 int ip6_mrtproto
= IPPROTO_PIM
; /* for netstat only */
137 struct mrt6stat mrt6stat
;
139 #define NO_RTE_FOUND 0x1
140 #define RTE_FOUND 0x2
142 struct mf6c
*mf6ctable
[MF6CTBLSIZ
];
143 u_char n6expire
[MF6CTBLSIZ
];
144 static struct mif6 mif6table
[MAXMIFS
];
146 u_int mrt6debug
= 0; /* debug level */
147 #define DEBUG_MFC 0x02
148 #define DEBUG_FORWARD 0x04
149 #define DEBUG_EXPIRE 0x08
150 #define DEBUG_XMIT 0x10
151 #define DEBUG_REG 0x20
152 #define DEBUG_PIM 0x40
155 static void expire_upcalls (void *);
156 static void expire_upcalls_dispatch(netmsg_t
);
157 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
158 #define UPCALL_EXPIRE 6 /* number of timeouts */
162 extern struct socket
*ip_mrouter
;
167 * 'Interfaces' associated with decapsulator (so we can tell
168 * packets that went through it from ones that get reflected
169 * by a broken gateway). These interfaces are never linked into
170 * the system ifnet list & no routes point to them. I.e., packets
171 * can't be sent this way. They only exist as a placeholder for
172 * multicast source verification.
174 struct ifnet multicast_register_if
;
176 #define ENCAP_HOPS 64
181 static mifi_t nummifs
= 0;
182 static mifi_t reg_mif_num
= (mifi_t
)-1;
184 static struct pim6stat pim6stat
;
188 * Hash function for a source, group entry
190 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
191 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
192 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
193 (g).s6_addr32[2] ^ (g).s6_addr32[3])
196 * Find a route for a given origin IPv6 address and Multicast group address.
197 * Quality of service parameter to be added in the future!!!
200 #define MF6CFIND(o, g, rt) do { \
201 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
203 mrt6stat.mrt6s_mfc_lookups++; \
205 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
206 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
207 (_rt->mf6c_stall == NULL)) { \
211 _rt = _rt->mf6c_next; \
214 mrt6stat.mrt6s_mfc_misses++; \
219 * Macros to compute elapsed time efficiently
220 * Borrowed from Van Jacobson's scheduling code
222 #define TV_DELTA(a, b, delta) do { \
225 delta = (a).tv_usec - (b).tv_usec; \
226 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
235 delta += (1000000 * xxs); \
240 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
241 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
244 #define UPCALL_MAX 50
245 u_long upcall_data
[UPCALL_MAX
+ 1];
246 static void collate();
247 #endif /* UPCALL_TIMING */
249 static int get_sg_cnt (struct sioc_sg_req6
*);
250 static int get_mif6_cnt (struct sioc_mif_req6
*);
251 static int ip6_mrouter_init (struct socket
*, struct mbuf
*, int);
252 static int add_m6if (struct mif6ctl
*);
253 static int del_m6if (mifi_t
*);
254 static int add_m6fc (struct mf6cctl
*);
255 static int del_m6fc (struct mf6cctl
*);
257 static struct callout expire_upcalls_ch
;
258 static struct netmsg_base expire_upcalls_nmsg
;
261 * Handle MRT setsockopt commands to modify the multicast routing tables.
264 ip6_mrouter_set(struct socket
*so
, struct sockopt
*sopt
)
269 if (so
!= ip6_mrouter
&& sopt
->sopt_name
!= MRT6_INIT
)
272 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
274 soopt_to_mbuf(sopt
, m
); /* XXX */
276 switch (sopt
->sopt_name
) {
281 error
= ip6_mrouter_init(so
, m
, sopt
->sopt_name
);
284 error
= ip6_mrouter_done();
287 error
= add_m6if(mtod(m
, struct mif6ctl
*));
290 error
= del_m6if(mtod(m
, mifi_t
*));
293 error
= add_m6fc(mtod(m
, struct mf6cctl
*));
296 error
= del_m6fc(mtod(m
, struct mf6cctl
*));
299 error
= set_pim6(mtod(m
, int *));
311 * Handle MRT getsockopt commands
314 ip6_mrouter_get(struct socket
*so
, struct sockopt
*sopt
)
318 if (so
!= ip6_mrouter
) return EACCES
;
320 switch (sopt
->sopt_name
) {
322 soopt_from_kbuf(sopt
, &pim6
, sizeof(pim6
));
329 * Handle ioctl commands to obtain information from the cache
332 mrt6_ioctl(int cmd
, caddr_t data
)
337 case SIOCGETSGCNT_IN6
:
338 return (get_sg_cnt((struct sioc_sg_req6
*)data
));
339 break; /* for safety */
340 case SIOCGETMIFCNT_IN6
:
341 return (get_mif6_cnt((struct sioc_mif_req6
*)data
));
342 break; /* for safety */
351 * returns the packet, byte, rpf-failure count for the source group provided
354 get_sg_cnt(struct sioc_sg_req6
*req
)
359 MF6CFIND(req
->src
.sin6_addr
, req
->grp
.sin6_addr
, rt
);
362 req
->pktcnt
= rt
->mf6c_pkt_cnt
;
363 req
->bytecnt
= rt
->mf6c_byte_cnt
;
364 req
->wrong_if
= rt
->mf6c_wrong_if
;
368 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
375 * returns the input and output packet and byte counts on the mif provided
378 get_mif6_cnt(struct sioc_mif_req6
*req
)
380 mifi_t mifi
= req
->mifi
;
385 req
->icount
= mif6table
[mifi
].m6_pkt_in
;
386 req
->ocount
= mif6table
[mifi
].m6_pkt_out
;
387 req
->ibytes
= mif6table
[mifi
].m6_bytes_in
;
388 req
->obytes
= mif6table
[mifi
].m6_bytes_out
;
396 if ((*i
!= 1) && (*i
!= 0))
405 * Enable multicast routing
408 ip6_mrouter_init(struct socket
*so
, struct mbuf
*m
, int cmd
)
417 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
418 so
->so_type
, so
->so_proto
->pr_protocol
);
421 if (so
->so_type
!= SOCK_RAW
||
422 so
->so_proto
->pr_protocol
!= IPPROTO_ICMPV6
)
425 if (!m
|| (m
->m_len
!= sizeof(int *)))
432 if (ip6_mrouter
!= NULL
)
436 ip6_mrouter_ver
= cmd
;
438 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
439 bzero((caddr_t
)n6expire
, sizeof(n6expire
));
441 pim6
= 0;/* used for stubbing out/in pim stuff */
443 callout_init_mp(&expire_upcalls_ch
);
444 netmsg_init(&expire_upcalls_nmsg
, NULL
, &netisr_adone_rport
,
445 MSGF_PRIORITY
| MSGF_DROPABLE
, expire_upcalls_dispatch
);
447 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
448 expire_upcalls
, NULL
);
452 log(LOG_DEBUG
, "ip6_mrouter_init\n");
459 * Disable multicast routing
462 ip6_mrouter_done(void)
467 struct in6_ifreq ifr
;
470 struct lwkt_msg
*lmsg
= &expire_upcalls_nmsg
.lmsg
;
474 if (ip6_mrouter
== NULL
)
478 * For each phyint in use, disable promiscuous reception of all IPv6
484 * If there is still IPv4 multicast routing daemon,
485 * we remain interfaces to receive all muliticasted packets.
486 * XXX: there may be an interface in which the IPv4 multicast
487 * daemon is not interested...
493 for (mifi
= 0; mifi
< nummifs
; mifi
++) {
494 if (mif6table
[mifi
].m6_ifp
&&
495 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
496 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
497 ifr
.ifr_addr
.sin6_addr
= kin6addr_any
;
498 ifp
= mif6table
[mifi
].m6_ifp
;
499 ifnet_serialize_all(ifp
);
500 ifp
->if_ioctl(ifp
, SIOCDELMULTI
,
501 (caddr_t
)&ifr
, NULL
);
502 ifnet_deserialize_all(ifp
);
507 bzero((caddr_t
)qtable
, sizeof(qtable
));
508 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
510 bzero((caddr_t
)mif6table
, sizeof(mif6table
));
513 pim6
= 0; /* used to stub out/in pim specific code */
515 callout_stop(&expire_upcalls_ch
);
517 if ((lmsg
->ms_flags
& MSGF_DONE
) == 0)
522 * Free all multicast forwarding cache entries.
524 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
529 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
530 struct rtdetq
*n
= rte
->next
;
533 kfree(rte
, M_MRTABLE
);
538 kfree(frt
, M_MRTABLE
);
542 bzero((caddr_t
)mf6ctable
, sizeof(mf6ctable
));
545 * Reset de-encapsulation cache
554 log(LOG_DEBUG
, "ip6_mrouter_done\n");
560 static struct sockaddr_in6 sin6
= { sizeof(sin6
), AF_INET6
};
563 * Add a mif to the mif table
566 add_m6if(struct mif6ctl
*mifcp
)
572 struct tbf
*m_tbf
= tbftable
+ mifcp
->mif6c_mifi
;
575 if (mifcp
->mif6c_mifi
>= MAXMIFS
)
577 mifp
= mif6table
+ mifcp
->mif6c_mifi
;
579 return EADDRINUSE
; /* XXX: is it appropriate? */
580 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
> if_index
)
582 ifp
= ifindex2ifnet
[mifcp
->mif6c_pifi
];
584 if (mifcp
->mif6c_flags
& MIFF_REGISTER
) {
585 if (reg_mif_num
== (mifi_t
)-1) {
586 strlcpy(multicast_register_if
.if_xname
, "register_mif",
588 multicast_register_if
.if_flags
|= IFF_LOOPBACK
;
589 multicast_register_if
.if_index
= mifcp
->mif6c_mifi
;
590 reg_mif_num
= mifcp
->mif6c_mifi
;
593 ifp
= &multicast_register_if
;
597 /* Make sure the interface supports multicast */
598 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
602 error
= if_allmulti(ifp
, 1);
609 mifp
->m6_flags
= mifcp
->mif6c_flags
;
612 /* scaling up here allows division by 1024 in critical code */
613 mifp
->m6_rate_limit
= mifcp
->mif6c_rate_limit
* 1024 / 1000;
615 /* initialize per mif pkt counters */
617 mifp
->m6_pkt_out
= 0;
618 mifp
->m6_bytes_in
= 0;
619 mifp
->m6_bytes_out
= 0;
622 /* Adjust nummifs up if the mifi is higher than nummifs */
623 if (nummifs
<= mifcp
->mif6c_mifi
)
624 nummifs
= mifcp
->mif6c_mifi
+ 1;
629 "add_mif #%d, phyint %s\n",
638 * Delete a mif from the mif table
641 del_m6if(mifi_t
*mifip
)
643 struct mif6
*mifp
= mif6table
+ *mifip
;
647 if (*mifip
>= nummifs
)
649 if (mifp
->m6_ifp
== NULL
)
654 if (!(mifp
->m6_flags
& MIFF_REGISTER
)) {
656 * XXX: what if there is yet IPv4 multicast daemon
657 * using the interface?
665 bzero((caddr_t
)qtable
[*mifip
], sizeof(qtable
[*mifip
]));
666 bzero((caddr_t
)mifp
->m6_tbf
, sizeof(*(mifp
->m6_tbf
)));
668 bzero((caddr_t
)mifp
, sizeof (*mifp
));
670 /* Adjust nummifs down */
671 for (mifi
= nummifs
; mifi
> 0; mifi
--)
672 if (mif6table
[mifi
- 1].m6_ifp
)
680 log(LOG_DEBUG
, "del_m6if %d, nummifs %d\n", *mifip
, nummifs
);
690 add_m6fc(struct mf6cctl
*mfccp
)
697 MF6CFIND(mfccp
->mf6cc_origin
.sin6_addr
,
698 mfccp
->mf6cc_mcastgrp
.sin6_addr
, rt
);
700 /* If an entry already exists, just update the fields */
703 if (mrt6debug
& DEBUG_MFC
)
705 "add_m6fc no upcall h %d o %s g %s p %x\n",
706 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
707 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
708 mfccp
->mf6cc_parent
);
712 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
713 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
719 * Find the entry for which the upcall was made and update
722 hash
= MF6CHASH(mfccp
->mf6cc_origin
.sin6_addr
,
723 mfccp
->mf6cc_mcastgrp
.sin6_addr
);
724 for (rt
= mf6ctable
[hash
], nstl
= 0; rt
; rt
= rt
->mf6c_next
) {
725 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
726 &mfccp
->mf6cc_origin
.sin6_addr
) &&
727 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
728 &mfccp
->mf6cc_mcastgrp
.sin6_addr
) &&
729 (rt
->mf6c_stall
!= NULL
)) {
733 "add_m6fc: %s o %s g %s p %x dbx %p\n",
734 "multiple kernel entries",
735 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
736 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
737 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
740 if (mrt6debug
& DEBUG_MFC
)
742 "add_m6fc o %s g %s p %x dbg %x\n",
743 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
744 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
745 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
748 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
749 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
750 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
751 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
752 /* initialize pkt counters per src-grp */
753 rt
->mf6c_pkt_cnt
= 0;
754 rt
->mf6c_byte_cnt
= 0;
755 rt
->mf6c_wrong_if
= 0;
757 rt
->mf6c_expire
= 0; /* Don't clean this guy up */
760 /* free packets Qed at the end of this entry */
761 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
762 struct rtdetq
*n
= rte
->next
;
763 ip6_mdq(rte
->m
, rte
->ifp
, rt
);
767 #endif /* UPCALL_TIMING */
768 kfree(rte
, M_MRTABLE
);
771 rt
->mf6c_stall
= NULL
;
776 * It is possible that an entry is being inserted without an upcall
780 if (mrt6debug
& DEBUG_MFC
)
781 log(LOG_DEBUG
,"add_mfc no upcall h %d o %s g %s p %x\n",
783 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
784 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
785 mfccp
->mf6cc_parent
);
788 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
789 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
790 &mfccp
->mf6cc_origin
.sin6_addr
)&&
791 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
792 &mfccp
->mf6cc_mcastgrp
.sin6_addr
)) {
794 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
795 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
796 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
797 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
798 /* initialize pkt counters per src-grp */
799 rt
->mf6c_pkt_cnt
= 0;
800 rt
->mf6c_byte_cnt
= 0;
801 rt
->mf6c_wrong_if
= 0;
809 /* no upcall, so make a new entry */
810 rt
= (struct mf6c
*)kmalloc(sizeof(*rt
), M_MRTABLE
,
817 /* insert new entry at head of hash chain */
818 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
819 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
820 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
821 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
822 /* initialize pkt counters per src-grp */
823 rt
->mf6c_pkt_cnt
= 0;
824 rt
->mf6c_byte_cnt
= 0;
825 rt
->mf6c_wrong_if
= 0;
827 rt
->mf6c_stall
= NULL
;
829 /* link into table */
830 rt
->mf6c_next
= mf6ctable
[hash
];
831 mf6ctable
[hash
] = rt
;
840 * collect delay statistics on the upcalls
843 collate(struct timeval
*t
)
853 TV_DELTA(tp
, *t
, delta
);
862 #endif /* UPCALL_TIMING */
865 * Delete an mfc entry
868 del_m6fc(struct mf6cctl
*mfccp
)
870 struct sockaddr_in6 origin
;
871 struct sockaddr_in6 mcastgrp
;
876 origin
= mfccp
->mf6cc_origin
;
877 mcastgrp
= mfccp
->mf6cc_mcastgrp
;
878 hash
= MF6CHASH(origin
.sin6_addr
, mcastgrp
.sin6_addr
);
881 if (mrt6debug
& DEBUG_MFC
)
882 log(LOG_DEBUG
,"del_m6fc orig %s mcastgrp %s\n",
883 ip6_sprintf(&origin
.sin6_addr
),
884 ip6_sprintf(&mcastgrp
.sin6_addr
));
889 nptr
= &mf6ctable
[hash
];
890 while ((rt
= *nptr
) != NULL
) {
891 if (IN6_ARE_ADDR_EQUAL(&origin
.sin6_addr
,
892 &rt
->mf6c_origin
.sin6_addr
) &&
893 IN6_ARE_ADDR_EQUAL(&mcastgrp
.sin6_addr
,
894 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
895 rt
->mf6c_stall
== NULL
)
898 nptr
= &rt
->mf6c_next
;
902 return EADDRNOTAVAIL
;
905 *nptr
= rt
->mf6c_next
;
906 kfree(rt
, M_MRTABLE
);
914 socket_send(struct socket
*so
, struct mbuf
*mm
, struct sockaddr_in6
*src
)
917 lwkt_gettoken(&so
->so_rcv
.ssb_token
);
918 if (ssb_appendaddr(&so
->so_rcv
,
919 (struct sockaddr
*)src
,
922 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
925 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
932 * IPv6 multicast forwarding function. This function assumes that the packet
933 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
934 * pointed to by "ifp", and the packet is to be relayed to other networks
935 * that have members of the packet's destination IPv6 multicast group.
937 * The packet is returned unscathed to the caller, unless it is
938 * erroneous, in which case a non-zero return value tells the caller to
943 ip6_mforward(struct ip6_hdr
*ip6
, struct ifnet
*ifp
, struct mbuf
*m
)
951 if (mrt6debug
& DEBUG_FORWARD
)
952 log(LOG_DEBUG
, "ip6_mforward: src %s, dst %s, ifindex %d\n",
953 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
),
958 * Don't forward a packet with Hop limit of zero or one,
959 * or a packet destined to a local-only group.
961 if (ip6
->ip6_hlim
<= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) ||
962 IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
967 * Source address check: do not forward packets with unspecified
968 * source. It was discussed in July 2000, on ipngwg mailing list.
969 * This is rather more serious than unicast cases, because some
970 * MLD packets can be sent with the unspecified source address
971 * (although such packets must normally set 1 to the hop limit field).
973 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
974 ip6stat
.ip6s_cantforward
++;
975 if (ip6_log_time
+ ip6_log_interval
< time_uptime
) {
976 ip6_log_time
= time_uptime
;
979 "from %s to %s nxt %d received on %s\n",
980 ip6_sprintf(&ip6
->ip6_src
),
981 ip6_sprintf(&ip6
->ip6_dst
),
983 if_name(m
->m_pkthdr
.rcvif
));
989 * Determine forwarding mifs from the forwarding cache table
992 MF6CFIND(ip6
->ip6_src
, ip6
->ip6_dst
, rt
);
994 /* Entry exists, so forward if necessary */
997 return (ip6_mdq(m
, ifp
, rt
));
1000 * If we don't have a route for packet's origin,
1001 * Make a copy of the packet &
1002 * send message to routing daemon
1009 #ifdef UPCALL_TIMING
1013 #endif /* UPCALL_TIMING */
1015 mrt6stat
.mrt6s_no_route
++;
1017 if (mrt6debug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1018 log(LOG_DEBUG
, "ip6_mforward: no rte s %s g %s\n",
1019 ip6_sprintf(&ip6
->ip6_src
),
1020 ip6_sprintf(&ip6
->ip6_dst
));
1024 * Allocate mbufs early so that we don't do extra work if we
1025 * are just going to fail anyway.
1027 rte
= (struct rtdetq
*)kmalloc(sizeof(*rte
), M_MRTABLE
,
1033 mb0
= m_copy(m
, 0, M_COPYALL
);
1035 * Pullup packet header if needed before storing it,
1036 * as other references may modify it in the meantime.
1039 (M_HASCL(mb0
) || mb0
->m_len
< sizeof(struct ip6_hdr
)))
1040 mb0
= m_pullup(mb0
, sizeof(struct ip6_hdr
));
1042 kfree(rte
, M_MRTABLE
);
1047 /* is there an upcall waiting for this packet? */
1048 hash
= MF6CHASH(ip6
->ip6_src
, ip6
->ip6_dst
);
1049 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
1050 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
,
1051 &rt
->mf6c_origin
.sin6_addr
) &&
1052 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1053 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
1054 (rt
->mf6c_stall
!= NULL
))
1061 struct omrt6msg
*oim
;
1064 /* no upcall, so make a new entry */
1065 rt
= (struct mf6c
*)kmalloc(sizeof(*rt
), M_MRTABLE
,
1068 kfree(rte
, M_MRTABLE
);
1074 * Make a copy of the header to send to the user
1077 mm
= m_copy(mb0
, 0, sizeof(struct ip6_hdr
));
1080 kfree(rte
, M_MRTABLE
);
1082 kfree(rt
, M_MRTABLE
);
1088 * Send message to routing daemon
1090 sin6
.sin6_addr
= ip6
->ip6_src
;
1096 switch (ip6_mrouter_ver
) {
1099 oim
= mtod(mm
, struct omrt6msg
*);
1100 oim
->im6_msgtype
= MRT6MSG_NOCACHE
;
1105 im
= mtod(mm
, struct mrt6msg
*);
1106 im
->im6_msgtype
= MRT6MSG_NOCACHE
;
1110 kfree(rte
, M_MRTABLE
);
1112 kfree(rt
, M_MRTABLE
);
1118 if (mrt6debug
& DEBUG_FORWARD
)
1120 "getting the iif info in the kernel\n");
1123 for (mifp
= mif6table
, mifi
= 0;
1124 mifi
< nummifs
&& mifp
->m6_ifp
!= ifp
;
1128 switch (ip6_mrouter_ver
) {
1131 oim
->im6_mif
= mifi
;
1139 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1140 log(LOG_WARNING
, "ip6_mforward: ip6_mrouter "
1141 "socket queue full\n");
1142 mrt6stat
.mrt6s_upq_sockfull
++;
1143 kfree(rte
, M_MRTABLE
);
1145 kfree(rt
, M_MRTABLE
);
1150 mrt6stat
.mrt6s_upcalls
++;
1152 /* insert new entry at head of hash chain */
1153 bzero(rt
, sizeof(*rt
));
1154 rt
->mf6c_origin
.sin6_family
= AF_INET6
;
1155 rt
->mf6c_origin
.sin6_len
= sizeof(struct sockaddr_in6
);
1156 rt
->mf6c_origin
.sin6_addr
= ip6
->ip6_src
;
1157 rt
->mf6c_mcastgrp
.sin6_family
= AF_INET6
;
1158 rt
->mf6c_mcastgrp
.sin6_len
= sizeof(struct sockaddr_in6
);
1159 rt
->mf6c_mcastgrp
.sin6_addr
= ip6
->ip6_dst
;
1160 rt
->mf6c_expire
= UPCALL_EXPIRE
;
1162 rt
->mf6c_parent
= MF6C_INCOMPLETE_PARENT
;
1164 /* link into table */
1165 rt
->mf6c_next
= mf6ctable
[hash
];
1166 mf6ctable
[hash
] = rt
;
1167 /* Add this entry to the end of the queue */
1168 rt
->mf6c_stall
= rte
;
1170 /* determine if q has overflowed */
1174 for (p
= &rt
->mf6c_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1175 if (++npkts
> MAX_UPQ6
) {
1176 mrt6stat
.mrt6s_upq_ovflw
++;
1177 kfree(rte
, M_MRTABLE
);
1183 /* Add this entry to the end of the queue */
1190 #ifdef UPCALL_TIMING
1192 #endif /* UPCALL_TIMING */
1201 * Clean up cache entries if upcalls are not serviced
1202 * Call from the Slow Timeout mechanism, every half second.
1205 expire_upcalls_dispatch(netmsg_t nmsg
)
1208 struct mf6c
*mfc
, **nptr
;
1211 ASSERT_IN_NETISR(0);
1215 lwkt_replymsg(&nmsg
->lmsg
, 0);
1218 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
1219 if (n6expire
[i
] == 0)
1221 nptr
= &mf6ctable
[i
];
1222 while ((mfc
= *nptr
) != NULL
) {
1223 rte
= mfc
->mf6c_stall
;
1225 * Skip real cache entries
1226 * Make sure it wasn't marked to not expire (shouldn't happen)
1230 mfc
->mf6c_expire
!= 0 &&
1231 --mfc
->mf6c_expire
== 0) {
1233 if (mrt6debug
& DEBUG_EXPIRE
)
1234 log(LOG_DEBUG
, "expire_upcalls: expiring (%s %s)\n",
1235 ip6_sprintf(&mfc
->mf6c_origin
.sin6_addr
),
1236 ip6_sprintf(&mfc
->mf6c_mcastgrp
.sin6_addr
));
1239 * drop all the packets
1240 * free the mbuf with the pkt, if, timing info
1243 struct rtdetq
*n
= rte
->next
;
1245 kfree(rte
, M_MRTABLE
);
1247 } while (rte
!= NULL
);
1248 mrt6stat
.mrt6s_cache_cleanups
++;
1251 *nptr
= mfc
->mf6c_next
;
1252 kfree(mfc
, M_MRTABLE
);
1254 nptr
= &mfc
->mf6c_next
;
1258 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
1259 expire_upcalls
, NULL
);
1263 expire_upcalls(void *arg __unused
)
1265 struct lwkt_msg
*lmsg
= &expire_upcalls_nmsg
.lmsg
;
1267 KASSERT(mycpuid
== 0, ("expire upcalls timer not on cpu0"));
1270 if (lmsg
->ms_flags
& MSGF_DONE
)
1271 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg
);
1276 * Packet forwarding routine once entry in the cache is made
1279 ip6_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mf6c
*rt
)
1281 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1284 int plen
= m
->m_pkthdr
.len
;
1285 u_int32_t dscopein
, sscopein
;
1288 * Macro to send packet on mif. Since RSVP packets don't get counted on
1289 * input, they shouldn't get counted on output, so statistics keeping is
1293 #define MC6_SEND(ip6, mifp, m) do { \
1294 if ((mifp)->m6_flags & MIFF_REGISTER) \
1295 register_send((ip6), (mifp), (m)); \
1297 phyint_send((ip6), (mifp), (m)); \
1301 * Don't forward if it didn't arrive from the parent mif
1304 mifi
= rt
->mf6c_parent
;
1305 if ((mifi
>= nummifs
) || (mif6table
[mifi
].m6_ifp
!= ifp
)) {
1306 /* came in the wrong interface */
1308 if (mrt6debug
& DEBUG_FORWARD
)
1310 "wrong if: ifid %d mifi %d mififid %x\n",
1311 ifp
->if_index
, mifi
,
1312 mif6table
[mifi
].m6_ifp
->if_index
);
1314 mrt6stat
.mrt6s_wrong_if
++;
1315 rt
->mf6c_wrong_if
++;
1317 * If we are doing PIM processing, and we are forwarding
1318 * packets on this interface, send a message to the
1321 /* have to make sure this is a valid mif */
1322 if (mifi
< nummifs
&& mif6table
[mifi
].m6_ifp
)
1323 if (pim6
&& (m
->m_flags
& M_LOOP
) == 0) {
1325 * Check the M_LOOP flag to avoid an
1326 * unnecessary PIM assert.
1327 * XXX: M_LOOP is an ad-hoc hack...
1329 static struct sockaddr_in6 sin6
=
1330 { sizeof(sin6
), AF_INET6
};
1335 struct omrt6msg
*oim
;
1338 mm
= m_copy(m
, 0, sizeof(struct ip6_hdr
));
1341 mm
->m_len
< sizeof(struct ip6_hdr
)))
1342 mm
= m_pullup(mm
, sizeof(struct ip6_hdr
));
1350 switch (ip6_mrouter_ver
) {
1353 oim
= mtod(mm
, struct omrt6msg
*);
1354 oim
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1359 im
= mtod(mm
, struct mrt6msg
*);
1360 im
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1368 for (mifp
= mif6table
, iif
= 0;
1369 iif
< nummifs
&& mifp
&&
1370 mifp
->m6_ifp
!= ifp
;
1374 switch (ip6_mrouter_ver
) {
1378 sin6
.sin6_addr
= oim
->im6_src
;
1383 sin6
.sin6_addr
= im
->im6_src
;
1387 mrt6stat
.mrt6s_upcalls
++;
1389 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1392 log(LOG_WARNING
, "mdq, ip6_mrouter socket queue full\n");
1394 ++mrt6stat
.mrt6s_upq_sockfull
;
1396 } /* if socket Q full */
1399 } /* if wrong iif */
1401 /* If I sourced this packet, it counts as output, else it was input. */
1402 if (m
->m_pkthdr
.rcvif
== NULL
) {
1403 /* XXX: is rcvif really NULL when output?? */
1404 mif6table
[mifi
].m6_pkt_out
++;
1405 mif6table
[mifi
].m6_bytes_out
+= plen
;
1407 mif6table
[mifi
].m6_pkt_in
++;
1408 mif6table
[mifi
].m6_bytes_in
+= plen
;
1411 rt
->mf6c_byte_cnt
+= plen
;
1414 * For each mif, forward a copy of the packet if there are group
1415 * members downstream on the interface.
1417 if (in6_addr2zoneid(ifp
, &ip6
->ip6_dst
, &dscopein
) ||
1418 in6_addr2zoneid(ifp
, &ip6
->ip6_src
, &sscopein
))
1420 for (mifp
= mif6table
, mifi
= 0; mifi
< nummifs
; mifp
++, mifi
++) {
1421 if (IF_ISSET(mifi
, &rt
->mf6c_ifset
)) {
1422 u_int32_t dscopeout
, sscopeout
;
1425 * check if the outgoing packet is going to break
1427 * XXX For packets through PIM register tunnel
1428 * interface, we believe a routing daemon.
1430 if (!(mif6table
[rt
->mf6c_parent
].m6_flags
&
1432 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
1433 if (in6_addr2zoneid(mif6table
[mifi
].m6_ifp
,
1436 in6_addr2zoneid(mif6table
[mifi
].m6_ifp
,
1439 dscopein
!= dscopeout
||
1440 sscopein
!= sscopeout
) {
1441 ip6stat
.ip6s_badscope
++;
1447 mifp
->m6_bytes_out
+= plen
;
1448 MC6_SEND(ip6
, mifp
, m
);
1455 phyint_send(struct ip6_hdr
*ip6
, struct mif6
*mifp
, struct mbuf
*m
)
1457 struct mbuf
*mb_copy
;
1458 struct ifnet
*ifp
= mifp
->m6_ifp
;
1460 static struct route_in6 ro
;
1461 struct in6_multi
*in6m
;
1462 struct sockaddr_in6
*dst6
;
1465 crit_enter(); /* needs to protect static "ro" below. */
1468 * Make a new reference to the packet; make sure that
1469 * the IPv6 header is actually copied, not just referenced,
1470 * so that ip6_output() only scribbles on the copy.
1472 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1474 (M_HASCL(mb_copy
) || mb_copy
->m_len
< sizeof(struct ip6_hdr
)))
1475 mb_copy
= m_pullup(mb_copy
, sizeof(struct ip6_hdr
));
1476 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
.im6o_multicast_ifp
= ifp
;
1494 /* XXX: ip6_output will override ip6->ip6_hlim */
1495 im6o
.im6o_multicast_hlim
= ip6
->ip6_hlim
;
1496 im6o
.im6o_multicast_loop
= 1;
1497 error
= ip6_output(mb_copy
, NULL
, &ro
,
1498 IPV6_FORWARDING
, &im6o
, NULL
, NULL
);
1501 if (mrt6debug
& DEBUG_XMIT
)
1502 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1503 mifp
- mif6table
, error
);
1510 * If we belong to the destination multicast group
1511 * on the outgoing interface, loop back a copy.
1513 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
1514 in6m
= IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
);
1516 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1517 dst6
->sin6_family
= AF_INET6
;
1518 dst6
->sin6_addr
= ip6
->ip6_dst
;
1519 ip6_mloopback(ifp
, m
, (struct sockaddr_in6
*)&ro
.ro_dst
);
1522 * Put the packet into the sending queue of the outgoing interface
1523 * if it would fit in the MTU of the interface.
1525 linkmtu
= IN6_LINKMTU(ifp
);
1526 if (mb_copy
->m_pkthdr
.len
<= linkmtu
|| linkmtu
< IPV6_MMTU
) {
1527 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
1528 dst6
->sin6_family
= AF_INET6
;
1529 dst6
->sin6_addr
= ip6
->ip6_dst
;
1531 * We just call if_output instead of nd6_output here, since
1532 * we need no ND for a multicast forwarded packet...right?
1534 error
= ifp
->if_output(ifp
, mb_copy
,
1535 (struct sockaddr
*)&ro
.ro_dst
, NULL
);
1537 if (mrt6debug
& DEBUG_XMIT
)
1538 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1539 mifp
- mif6table
, error
);
1542 #ifdef MULTICAST_PMTUD
1543 icmp6_error(mb_copy
, ICMP6_PACKET_TOO_BIG
, 0, linkmtu
);
1546 if (mrt6debug
& DEBUG_XMIT
)
1548 "phyint_send: packet too big on %s o %s g %s"
1549 " size %d(discarded)\n",
1551 ip6_sprintf(&ip6
->ip6_src
),
1552 ip6_sprintf(&ip6
->ip6_dst
),
1553 mb_copy
->m_pkthdr
.len
);
1554 #endif /* MRT6DEBUG */
1555 m_freem(mb_copy
); /* simply discard the packet */
1563 register_send(struct ip6_hdr
*ip6
, struct mif6
*mif
, struct mbuf
*m
)
1566 int i
, len
= m
->m_pkthdr
.len
;
1567 static struct sockaddr_in6 sin6
= { sizeof(sin6
), AF_INET6
};
1568 struct mrt6msg
*im6
;
1572 log(LOG_DEBUG
, "** IPv6 register_send **\n src %s dst %s\n",
1573 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
));
1575 ++pim6stat
.pim6s_snd_registers
;
1577 /* Make a copy of the packet to send to the user level process */
1578 MGETHDR(mm
, M_NOWAIT
, MT_HEADER
);
1581 mm
->m_pkthdr
.rcvif
= NULL
;
1582 mm
->m_data
+= max_linkhdr
;
1583 mm
->m_len
= sizeof(struct ip6_hdr
);
1585 if ((mm
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1589 i
= MHLEN
- M_LEADINGSPACE(mm
);
1592 mm
= m_pullup(mm
, i
);
1595 /* TODO: check it! */
1596 mm
->m_pkthdr
.len
= len
+ sizeof(struct ip6_hdr
);
1599 * Send message to routing daemon
1601 sin6
.sin6_addr
= ip6
->ip6_src
;
1603 im6
= mtod(mm
, struct mrt6msg
*);
1604 im6
->im6_msgtype
= MRT6MSG_WHOLEPKT
;
1607 im6
->im6_mif
= mif
- mif6table
;
1609 /* iif info is not given for reg. encap.n */
1610 mrt6stat
.mrt6s_upcalls
++;
1612 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1616 "register_send: ip6_mrouter socket queue full\n");
1618 ++mrt6stat
.mrt6s_upq_sockfull
;
1625 * PIM sparse mode hook
1626 * Receives the pim control messages, and passes them up to the listening
1627 * socket, using rip6_input.
1628 * The only message processed is the REGISTER pim message; the pim header
1629 * is stripped off, and the inner packet is passed to register_mforward.
1632 pim6_input(struct mbuf
**mp
, int *offp
, int proto
)
1634 struct pim
*pim
; /* pointer to a pim struct */
1635 struct ip6_hdr
*ip6
;
1637 struct mbuf
*m
= *mp
;
1641 ++pim6stat
.pim6s_rcv_total
;
1643 ip6
= mtod(m
, struct ip6_hdr
*);
1644 pimlen
= m
->m_pkthdr
.len
- *offp
;
1649 if (pimlen
< PIM_MINLEN
) {
1650 ++pim6stat
.pim6s_rcv_tooshort
;
1652 if (mrt6debug
& DEBUG_PIM
)
1653 log(LOG_DEBUG
,"pim6_input: PIM packet too short\n");
1656 return (IPPROTO_DONE
);
1660 * if the packet is at least as big as a REGISTER, go ahead
1661 * and grab the PIM REGISTER header size, to avoid another
1662 * possible m_pullup() later.
1664 * PIM_MINLEN == pimhdr + u_int32 == 8
1665 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1667 minlen
= (pimlen
>= PIM6_REG_MINLEN
) ? PIM6_REG_MINLEN
: PIM_MINLEN
;
1670 * Make sure that the IP6 and PIM headers in contiguous memory, and
1671 * possibly the PIM REGISTER header
1673 #ifndef PULLDOWN_TEST
1674 IP6_EXTHDR_CHECK(m
, off
, minlen
, IPPROTO_DONE
);
1675 /* adjust pointer */
1676 ip6
= mtod(m
, struct ip6_hdr
*);
1678 /* adjust mbuf to point to the PIM header */
1679 pim
= (struct pim
*)((caddr_t
)ip6
+ off
);
1681 IP6_EXTHDR_GET(pim
, struct pim
*, m
, off
, minlen
);
1683 pim6stat
.pim6s_rcv_tooshort
++;
1684 return IPPROTO_DONE
;
1688 #define PIM6_CHECKSUM
1689 #ifdef PIM6_CHECKSUM
1694 * Validate checksum.
1695 * If PIM REGISTER, exclude the data packet
1697 if (pim
->pim_type
== PIM_REGISTER
)
1698 cksumlen
= PIM_MINLEN
;
1702 if (in6_cksum(m
, IPPROTO_PIM
, off
, cksumlen
)) {
1703 ++pim6stat
.pim6s_rcv_badsum
;
1705 if (mrt6debug
& DEBUG_PIM
)
1707 "pim6_input: invalid checksum\n");
1710 return (IPPROTO_DONE
);
1713 #endif /* PIM_CHECKSUM */
1715 /* PIM version check */
1716 if (pim
->pim_ver
!= PIM_VERSION
) {
1717 ++pim6stat
.pim6s_rcv_badversion
;
1720 "pim6_input: incorrect version %d, expecting %d\n",
1721 pim
->pim_ver
, PIM_VERSION
);
1724 return (IPPROTO_DONE
);
1727 if (pim
->pim_type
== PIM_REGISTER
) {
1729 * since this is a REGISTER, we'll make a copy of the register
1730 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1733 static struct sockaddr_in6 dst
= { sizeof(dst
), AF_INET6
};
1736 struct ip6_hdr
*eip6
;
1739 ++pim6stat
.pim6s_rcv_registers
;
1741 if ((reg_mif_num
>= nummifs
) || (reg_mif_num
== (mifi_t
) -1)) {
1743 if (mrt6debug
& DEBUG_PIM
)
1745 "pim6_input: register mif not set: %d\n",
1749 return (IPPROTO_DONE
);
1752 reghdr
= (u_int32_t
*)(pim
+ 1);
1754 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
1755 goto pim6_input_to_daemon
;
1760 if (pimlen
< PIM6_REG_MINLEN
) {
1761 ++pim6stat
.pim6s_rcv_tooshort
;
1762 ++pim6stat
.pim6s_rcv_badregisters
;
1765 "pim6_input: register packet size too "
1766 "small %d from %s\n",
1767 pimlen
, ip6_sprintf(&ip6
->ip6_src
));
1770 return (IPPROTO_DONE
);
1773 eip6
= (struct ip6_hdr
*) (reghdr
+ 1);
1775 if (mrt6debug
& DEBUG_PIM
)
1777 "pim6_input[register], eip6: %s -> %s, "
1779 ip6_sprintf(&eip6
->ip6_src
),
1780 ip6_sprintf(&eip6
->ip6_dst
),
1781 ntohs(eip6
->ip6_plen
));
1784 /* verify the version number of the inner packet */
1785 if ((eip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1786 ++pim6stat
.pim6s_rcv_badregisters
;
1788 log(LOG_DEBUG
, "pim6_input: invalid IP version (%d) "
1789 "of the inner packet\n",
1790 (eip6
->ip6_vfc
& IPV6_VERSION
));
1793 return (IPPROTO_NONE
);
1796 /* verify the inner packet is destined to a mcast group */
1797 if (!IN6_IS_ADDR_MULTICAST(&eip6
->ip6_dst
)) {
1798 ++pim6stat
.pim6s_rcv_badregisters
;
1800 if (mrt6debug
& DEBUG_PIM
)
1802 "pim6_input: inner packet of register "
1803 "is not multicast %s\n",
1804 ip6_sprintf(&eip6
->ip6_dst
));
1807 return (IPPROTO_DONE
);
1811 * make a copy of the whole header to pass to the daemon later.
1813 mcp
= m_copy(m
, 0, off
+ PIM6_REG_MINLEN
);
1817 "pim6_input: pim register: "
1818 "could not copy register head\n");
1821 return (IPPROTO_DONE
);
1825 * forward the inner ip6 packet; point m_data at the inner ip6.
1827 m_adj(m
, off
+ PIM_MINLEN
);
1829 if (mrt6debug
& DEBUG_PIM
) {
1831 "pim6_input: forwarding decapsulated register: "
1832 "src %s, dst %s, mif %d\n",
1833 ip6_sprintf(&eip6
->ip6_src
),
1834 ip6_sprintf(&eip6
->ip6_dst
),
1839 if_simloop(mif6table
[reg_mif_num
].m6_ifp
, m
,
1840 dst
.sin6_family
, 0);
1842 /* prepare the register head to send to the mrouting daemon */
1847 * Pass the PIM message up to the daemon; if it is a register message
1848 * pass the 'head' only up to the daemon. This includes the
1849 * encapsulator ip6 header, pim header, register header and the
1850 * encapsulated ip6 header.
1852 pim6_input_to_daemon
:
1853 rip6_input(&m
, offp
, proto
);
1854 return (IPPROTO_DONE
);