1 /* $NetBSD: ip6_mroute.c,v 1.97 2009/03/18 16:00:23 cegger Exp $ */
2 /* $KAME: ip6_mroute.c,v 1.49 2001/07/25 09:21:18 jinmei 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) 1992, 1993
37 * The Regents of the University of California. All rights reserved.
39 * This code is derived from software contributed to Berkeley by
40 * Stephen Deering of Stanford University.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
70 * Copyright (c) 1989 Stephen Deering
72 * This code is derived from software contributed to Berkeley by
73 * Stephen Deering of Stanford University.
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. All advertising materials mentioning features or use of this software
84 * must display the following acknowledgement:
85 * This product includes software developed by the University of
86 * California, Berkeley and its contributors.
87 * 4. Neither the name of the University nor the names of its contributors
88 * may be used to endorse or promote products derived from this software
89 * without specific prior written permission.
91 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
107 * IP multicast forwarding procedures
109 * Written by David Waitzman, BBN Labs, August 1988.
110 * Modified by Steve Deering, Stanford, February 1989.
111 * Modified by Mark J. Steiglitz, Stanford, May, 1991
112 * Modified by Van Jacobson, LBL, January 1993
113 * Modified by Ajit Thyagarajan, PARC, August 1993
114 * Modified by Bill Fenner, PARC, April 1994
116 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
119 #include <sys/cdefs.h>
120 __KERNEL_RCSID(0, "$NetBSD: ip6_mroute.c,v 1.97 2009/03/18 16:00:23 cegger Exp $");
122 #include "opt_inet.h"
123 #include "opt_mrouting.h"
125 #include <sys/param.h>
126 #include <sys/systm.h>
127 #include <sys/callout.h>
128 #include <sys/mbuf.h>
129 #include <sys/socket.h>
130 #include <sys/socketvar.h>
131 #include <sys/sockio.h>
132 #include <sys/protosw.h>
133 #include <sys/errno.h>
134 #include <sys/time.h>
135 #include <sys/kernel.h>
136 #include <sys/ioctl.h>
137 #include <sys/sysctl.h>
138 #include <sys/syslog.h>
141 #include <net/route.h>
142 #include <net/raw_cb.h>
143 #include <net/net_stats.h>
145 #include <netinet/in.h>
146 #include <netinet/in_var.h>
147 #include <netinet/icmp6.h>
149 #include <netinet/ip6.h>
150 #include <netinet6/ip6_var.h>
151 #include <netinet6/ip6_private.h>
152 #include <netinet6/ip6_mroute.h>
153 #include <netinet6/scope6_var.h>
154 #include <netinet6/pim6.h>
155 #include <netinet6/pim6_var.h>
156 #include <netinet6/nd6.h>
158 #include <net/net_osdep.h>
160 static int ip6_mdq(struct mbuf
*, struct ifnet
*, struct mf6c
*);
161 static void phyint_send(struct ip6_hdr
*, struct mif6
*, struct mbuf
*);
163 static int set_pim6(int *);
164 static int socket_send(struct socket
*, struct mbuf
*,
165 struct sockaddr_in6
*);
166 static int register_send(struct ip6_hdr
*, struct mif6
*, struct mbuf
*);
169 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
170 * except for netstat or debugging purposes.
172 struct socket
*ip6_mrouter
= NULL
;
173 int ip6_mrouter_ver
= 0;
174 int ip6_mrtproto
= IPPROTO_PIM
; /* for netstat only */
175 struct mrt6stat mrt6stat
;
177 #define NO_RTE_FOUND 0x1
178 #define RTE_FOUND 0x2
180 struct mf6c
*mf6ctable
[MF6CTBLSIZ
];
181 u_char n6expire
[MF6CTBLSIZ
];
182 struct mif6 mif6table
[MAXMIFS
];
184 u_int mrt6debug
= 0; /* debug level */
185 #define DEBUG_MFC 0x02
186 #define DEBUG_FORWARD 0x04
187 #define DEBUG_EXPIRE 0x08
188 #define DEBUG_XMIT 0x10
189 #define DEBUG_REG 0x20
190 #define DEBUG_PIM 0x40
193 static void expire_upcalls(void *);
194 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
195 #define UPCALL_EXPIRE 6 /* number of timeouts */
199 extern struct socket
*ip_mrouter
;
204 * 'Interfaces' associated with decapsulator (so we can tell
205 * packets that went through it from ones that get reflected
206 * by a broken gateway). These interfaces are never linked into
207 * the system ifnet list & no routes point to them. I.e., packets
208 * can't be sent this way. They only exist as a placeholder for
209 * multicast source verification.
211 struct ifnet multicast_register_if6
;
213 #define ENCAP_HOPS 64
218 static mifi_t nummifs
= 0;
219 static mifi_t reg_mif_num
= (mifi_t
)-1;
221 static percpu_t
*pim6stat_percpu
;
223 #define PIM6_STATINC(x) _NET_STATINC(pim6stat_percpu, x)
228 * Hash function for a source, group entry
230 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
231 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
232 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
233 (g).s6_addr32[2] ^ (g).s6_addr32[3])
236 * Find a route for a given origin IPv6 address and Multicast group address.
237 * Quality of service parameter to be added in the future!!!
240 #define MF6CFIND(o, g, rt) do { \
241 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
243 mrt6stat.mrt6s_mfc_lookups++; \
245 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
246 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
247 (_rt->mf6c_stall == NULL)) { \
251 _rt = _rt->mf6c_next; \
254 mrt6stat.mrt6s_mfc_misses++; \
256 } while (/*CONSTCOND*/ 0)
259 * Macros to compute elapsed time efficiently
260 * Borrowed from Van Jacobson's scheduling code
262 #define TV_DELTA(a, b, delta) do { \
265 delta = (a).tv_usec - (b).tv_usec; \
266 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
275 delta += (1000000 * xxs); \
278 } while (/*CONSTCOND*/ 0)
280 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
281 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
284 #define UPCALL_MAX 50
285 u_long upcall_data
[UPCALL_MAX
+ 1];
286 static void collate();
287 #endif /* UPCALL_TIMING */
289 static int get_sg_cnt(struct sioc_sg_req6
*);
290 static int get_mif6_cnt(struct sioc_mif_req6
*);
291 static int ip6_mrouter_init(struct socket
*, int, int);
292 static int add_m6if(struct mif6ctl
*);
293 static int del_m6if(mifi_t
*);
294 static int add_m6fc(struct mf6cctl
*);
295 static int del_m6fc(struct mf6cctl
*);
296 static void sysctl_net_inet6_pim6_setup(struct sysctllog
**);
298 static callout_t expire_upcalls_ch
;
304 sysctl_net_inet6_pim6_setup(NULL
);
305 pim6stat_percpu
= percpu_alloc(sizeof(uint64_t) * PIM6_NSTATS
);
309 * Handle MRT setsockopt commands to modify the multicast routing tables.
312 ip6_mrouter_set(struct socket
*so
, struct sockopt
*sopt
)
319 if (sopt
->sopt_name
!= MRT6_INIT
&& so
!= ip6_mrouter
)
324 switch (sopt
->sopt_name
) {
329 error
= sockopt_getint(sopt
, &optval
);
332 return (ip6_mrouter_init(so
, optval
, sopt
->sopt_name
));
334 return (ip6_mrouter_done());
336 error
= sockopt_get(sopt
, &mifc
, sizeof(mifc
));
339 return (add_m6if(&mifc
));
341 error
= sockopt_get(sopt
, &mifi
, sizeof(mifi
));
344 return (del_m6if(&mifi
));
346 error
= sockopt_get(sopt
, &mfcc
, sizeof(mfcc
));
349 return (add_m6fc(&mfcc
));
351 error
= sockopt_get(sopt
, &mfcc
, sizeof(mfcc
));
354 return (del_m6fc(&mfcc
));
356 error
= sockopt_getint(sopt
, &optval
);
359 return (set_pim6(&optval
));
368 * Handle MRT getsockopt commands
371 ip6_mrouter_get(struct socket
*so
, struct sockopt
*sopt
)
375 if (so
!= ip6_mrouter
) return EACCES
;
379 switch (sopt
->sopt_name
) {
381 error
= sockopt_set(sopt
, &pim6
, sizeof(pim6
));
392 * Handle ioctl commands to obtain information from the cache
395 mrt6_ioctl(u_long cmd
, void *data
)
399 case SIOCGETSGCNT_IN6
:
400 return (get_sg_cnt((struct sioc_sg_req6
*)data
));
401 case SIOCGETMIFCNT_IN6
:
402 return (get_mif6_cnt((struct sioc_mif_req6
*)data
));
409 * returns the packet, byte, rpf-failure count for the source group provided
412 get_sg_cnt(struct sioc_sg_req6
*req
)
418 MF6CFIND(req
->src
.sin6_addr
, req
->grp
.sin6_addr
, rt
);
421 req
->pktcnt
= rt
->mf6c_pkt_cnt
;
422 req
->bytecnt
= rt
->mf6c_byte_cnt
;
423 req
->wrong_if
= rt
->mf6c_wrong_if
;
427 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
434 * returns the input and output packet and byte counts on the mif provided
437 get_mif6_cnt(struct sioc_mif_req6
*req
)
439 mifi_t mifi
= req
->mifi
;
444 req
->icount
= mif6table
[mifi
].m6_pkt_in
;
445 req
->ocount
= mif6table
[mifi
].m6_pkt_out
;
446 req
->ibytes
= mif6table
[mifi
].m6_bytes_in
;
447 req
->obytes
= mif6table
[mifi
].m6_bytes_out
;
455 if ((*i
!= 1) && (*i
!= 0))
464 * Enable multicast routing
467 ip6_mrouter_init(struct socket
*so
, int v
, int cmd
)
472 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
473 so
->so_type
, so
->so_proto
->pr_protocol
);
476 if (so
->so_type
!= SOCK_RAW
||
477 so
->so_proto
->pr_protocol
!= IPPROTO_ICMPV6
)
481 return (ENOPROTOOPT
);
483 if (ip6_mrouter
!= NULL
)
487 ip6_mrouter_ver
= cmd
;
489 memset((void *)mf6ctable
, 0, sizeof(mf6ctable
));
490 memset((void *)n6expire
, 0, sizeof(n6expire
));
492 pim6
= 0;/* used for stubbing out/in pim stuff */
494 callout_init(&expire_upcalls_ch
, CALLOUT_MPSAFE
);
495 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
496 expire_upcalls
, NULL
);
500 log(LOG_DEBUG
, "ip6_mrouter_init\n");
507 * Disable multicast routing
510 ip6_mrouter_done(void)
515 struct in6_ifreq ifr
;
523 * For each phyint in use, disable promiscuous reception of all IPv6
529 * If there is still IPv4 multicast routing daemon,
530 * we remain interfaces to receive all muliticasted packets.
531 * XXX: there may be an interface in which the IPv4 multicast
532 * daemon is not interested...
538 for (mifi
= 0; mifi
< nummifs
; mifi
++) {
539 if (mif6table
[mifi
].m6_ifp
&&
540 !(mif6table
[mifi
].m6_flags
& MIFF_REGISTER
)) {
541 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
542 ifr
.ifr_addr
.sin6_addr
= in6addr_any
;
543 ifp
= mif6table
[mifi
].m6_ifp
;
544 (*ifp
->if_ioctl
)(ifp
, SIOCDELMULTI
, &ifr
);
549 memset((void *)qtable
, 0, sizeof(qtable
));
550 memset((void *)tbftable
, 0, sizeof(tbftable
));
552 memset((void *)mif6table
, 0, sizeof(mif6table
));
555 pim6
= 0; /* used to stub out/in pim specific code */
557 callout_stop(&expire_upcalls_ch
);
560 * Free all multicast forwarding cache entries.
562 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
567 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
568 struct rtdetq
*n
= rte
->next
;
571 free(rte
, M_MRTABLE
);
576 free(frt
, M_MRTABLE
);
580 memset((void *)mf6ctable
, 0, sizeof(mf6ctable
));
583 * Reset register interface
585 if (reg_mif_num
!= (mifi_t
)-1) {
586 if_detach(&multicast_register_if6
);
587 reg_mif_num
= (mifi_t
)-1;
597 log(LOG_DEBUG
, "ip6_mrouter_done\n");
604 ip6_mrouter_detach(struct ifnet
*ifp
)
611 if (ip6_mrouter
== NULL
)
615 * Delete a mif which points to ifp.
617 for (mifi
= 0; mifi
< nummifs
; mifi
++)
618 if (mif6table
[mifi
].m6_ifp
== ifp
)
622 * Clear rte->ifp of cache entries received on ifp.
624 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
625 if (n6expire
[i
] == 0)
628 for (mfc
= mf6ctable
[i
]; mfc
!= NULL
; mfc
= mfc
->mf6c_next
) {
629 for (rte
= mfc
->mf6c_stall
; rte
!= NULL
; rte
= rte
->next
) {
639 * Add a mif to the mif table
642 add_m6if(struct mif6ctl
*mifcp
)
646 struct in6_ifreq ifr
;
649 struct tbf
*m_tbf
= tbftable
+ mifcp
->mif6c_mifi
;
652 if (mifcp
->mif6c_mifi
>= MAXMIFS
)
654 mifp
= mif6table
+ mifcp
->mif6c_mifi
;
656 return EADDRINUSE
; /* XXX: is it appropriate? */
657 if (mifcp
->mif6c_pifi
== 0 || mifcp
->mif6c_pifi
>= if_indexlim
)
660 * XXX: some OSes can remove ifp and clear ifindex2ifnet[id]
661 * even for id between 0 and if_index.
663 if ((ifp
= ifindex2ifnet
[mifcp
->mif6c_pifi
]) == NULL
)
666 if (mifcp
->mif6c_flags
& MIFF_REGISTER
) {
667 ifp
= &multicast_register_if6
;
669 if (reg_mif_num
== (mifi_t
)-1) {
670 strlcpy(ifp
->if_xname
, "register_mif",
671 sizeof(ifp
->if_xname
));
672 ifp
->if_flags
|= IFF_LOOPBACK
;
673 ifp
->if_index
= mifcp
->mif6c_mifi
;
674 reg_mif_num
= mifcp
->mif6c_mifi
;
680 /* Make sure the interface supports multicast */
681 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
686 * Enable promiscuous reception of all IPv6 multicasts
687 * from the interface.
689 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
690 ifr
.ifr_addr
.sin6_addr
= in6addr_any
;
691 error
= (*ifp
->if_ioctl
)(ifp
, SIOCADDMULTI
, &ifr
);
698 mifp
->m6_flags
= mifcp
->mif6c_flags
;
701 /* scaling up here allows division by 1024 in critical code */
702 mifp
->m6_rate_limit
= mifcp
->mif6c_rate_limit
* 1024 / 1000;
704 /* initialize per mif pkt counters */
706 mifp
->m6_pkt_out
= 0;
707 mifp
->m6_bytes_in
= 0;
708 mifp
->m6_bytes_out
= 0;
711 /* Adjust nummifs up if the mifi is higher than nummifs */
712 if (nummifs
<= mifcp
->mif6c_mifi
)
713 nummifs
= mifcp
->mif6c_mifi
+ 1;
718 "add_mif #%d, phyint %s\n",
719 mifcp
->mif6c_mifi
, ifp
->if_xname
);
726 * Delete a mif from the mif table
729 del_m6if(mifi_t
*mifip
)
731 struct mif6
*mifp
= mif6table
+ *mifip
;
734 struct in6_ifreq ifr
;
737 if (*mifip
>= nummifs
)
739 if (mifp
->m6_ifp
== NULL
)
744 if (!(mifp
->m6_flags
& MIFF_REGISTER
)) {
746 * XXX: what if there is yet IPv4 multicast daemon
747 * using the interface?
751 ifr
.ifr_addr
.sin6_family
= AF_INET6
;
752 ifr
.ifr_addr
.sin6_addr
= in6addr_any
;
753 (*ifp
->if_ioctl
)(ifp
, SIOCDELMULTI
, &ifr
);
755 if (reg_mif_num
!= (mifi_t
)-1) {
756 if_detach(&multicast_register_if6
);
757 reg_mif_num
= (mifi_t
)-1;
762 memset((void *)qtable
[*mifip
], 0, sizeof(qtable
[*mifip
]));
763 memset((void *)mifp
->m6_tbf
, 0, sizeof(*(mifp
->m6_tbf
)));
765 memset((void *)mifp
, 0, sizeof (*mifp
));
767 /* Adjust nummifs down */
768 for (mifi
= nummifs
; mifi
> 0; mifi
--)
769 if (mif6table
[mifi
- 1].m6_ifp
)
777 log(LOG_DEBUG
, "del_m6if %d, nummifs %d\n", *mifip
, nummifs
);
787 add_m6fc(struct mf6cctl
*mfccp
)
795 MF6CFIND(mfccp
->mf6cc_origin
.sin6_addr
,
796 mfccp
->mf6cc_mcastgrp
.sin6_addr
, rt
);
798 /* If an entry already exists, just update the fields */
801 if (mrt6debug
& DEBUG_MFC
)
802 log(LOG_DEBUG
,"add_m6fc update o %s g %s p %x\n",
803 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
804 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
805 mfccp
->mf6cc_parent
);
809 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
810 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
816 * Find the entry for which the upcall was made and update
819 hash
= MF6CHASH(mfccp
->mf6cc_origin
.sin6_addr
,
820 mfccp
->mf6cc_mcastgrp
.sin6_addr
);
821 for (rt
= mf6ctable
[hash
], nstl
= 0; rt
; rt
= rt
->mf6c_next
) {
822 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
823 &mfccp
->mf6cc_origin
.sin6_addr
) &&
824 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
825 &mfccp
->mf6cc_mcastgrp
.sin6_addr
) &&
826 (rt
->mf6c_stall
!= NULL
)) {
830 "add_m6fc: %s o %s g %s p %x dbx %p\n",
831 "multiple kernel entries",
832 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
833 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
834 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
837 if (mrt6debug
& DEBUG_MFC
)
839 "add_m6fc o %s g %s p %x dbg %p\n",
840 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
841 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
842 mfccp
->mf6cc_parent
, rt
->mf6c_stall
);
845 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
846 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
847 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
848 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
849 /* initialize pkt counters per src-grp */
850 rt
->mf6c_pkt_cnt
= 0;
851 rt
->mf6c_byte_cnt
= 0;
852 rt
->mf6c_wrong_if
= 0;
854 rt
->mf6c_expire
= 0; /* Don't clean this guy up */
857 /* free packets Qed at the end of this entry */
858 for (rte
= rt
->mf6c_stall
; rte
!= NULL
; ) {
859 struct rtdetq
*n
= rte
->next
;
861 ip6_mdq(rte
->m
, rte
->ifp
, rt
);
866 #endif /* UPCALL_TIMING */
867 free(rte
, M_MRTABLE
);
870 rt
->mf6c_stall
= NULL
;
875 * It is possible that an entry is being inserted without an upcall
879 if (mrt6debug
& DEBUG_MFC
)
881 "add_mfc no upcall h %ld o %s g %s p %x\n",
883 ip6_sprintf(&mfccp
->mf6cc_origin
.sin6_addr
),
884 ip6_sprintf(&mfccp
->mf6cc_mcastgrp
.sin6_addr
),
885 mfccp
->mf6cc_parent
);
888 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
890 if (IN6_ARE_ADDR_EQUAL(&rt
->mf6c_origin
.sin6_addr
,
891 &mfccp
->mf6cc_origin
.sin6_addr
)&&
892 IN6_ARE_ADDR_EQUAL(&rt
->mf6c_mcastgrp
.sin6_addr
,
893 &mfccp
->mf6cc_mcastgrp
.sin6_addr
)) {
895 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
896 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
897 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
898 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
899 /* initialize pkt counters per src-grp */
900 rt
->mf6c_pkt_cnt
= 0;
901 rt
->mf6c_byte_cnt
= 0;
902 rt
->mf6c_wrong_if
= 0;
910 /* no upcall, so make a new entry */
911 rt
= (struct mf6c
*)malloc(sizeof(*rt
), M_MRTABLE
,
918 /* insert new entry at head of hash chain */
919 rt
->mf6c_origin
= mfccp
->mf6cc_origin
;
920 rt
->mf6c_mcastgrp
= mfccp
->mf6cc_mcastgrp
;
921 rt
->mf6c_parent
= mfccp
->mf6cc_parent
;
922 rt
->mf6c_ifset
= mfccp
->mf6cc_ifset
;
923 /* initialize pkt counters per src-grp */
924 rt
->mf6c_pkt_cnt
= 0;
925 rt
->mf6c_byte_cnt
= 0;
926 rt
->mf6c_wrong_if
= 0;
928 rt
->mf6c_stall
= NULL
;
930 /* link into table */
931 rt
->mf6c_next
= mf6ctable
[hash
];
932 mf6ctable
[hash
] = rt
;
941 * collect delay statistics on the upcalls
944 collate(struct timeval
*t
)
954 TV_DELTA(tp
, *t
, delta
);
963 #endif /* UPCALL_TIMING */
966 * Delete an mfc entry
969 del_m6fc(struct mf6cctl
*mfccp
)
971 struct sockaddr_in6 origin
;
972 struct sockaddr_in6 mcastgrp
;
978 origin
= mfccp
->mf6cc_origin
;
979 mcastgrp
= mfccp
->mf6cc_mcastgrp
;
980 hash
= MF6CHASH(origin
.sin6_addr
, mcastgrp
.sin6_addr
);
983 if (mrt6debug
& DEBUG_MFC
)
984 log(LOG_DEBUG
,"del_m6fc orig %s mcastgrp %s\n",
985 ip6_sprintf(&origin
.sin6_addr
),
986 ip6_sprintf(&mcastgrp
.sin6_addr
));
991 nptr
= &mf6ctable
[hash
];
992 while ((rt
= *nptr
) != NULL
) {
993 if (IN6_ARE_ADDR_EQUAL(&origin
.sin6_addr
,
994 &rt
->mf6c_origin
.sin6_addr
) &&
995 IN6_ARE_ADDR_EQUAL(&mcastgrp
.sin6_addr
,
996 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
997 rt
->mf6c_stall
== NULL
)
1000 nptr
= &rt
->mf6c_next
;
1004 return EADDRNOTAVAIL
;
1007 *nptr
= rt
->mf6c_next
;
1008 free(rt
, M_MRTABLE
);
1016 socket_send(struct socket
*s
, struct mbuf
*mm
, struct sockaddr_in6
*src
)
1019 if (sbappendaddr(&s
->so_rcv
,
1020 (struct sockaddr
*)src
,
1021 mm
, (struct mbuf
*)0) != 0) {
1031 * IPv6 multicast forwarding function. This function assumes that the packet
1032 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1033 * pointed to by "ifp", and the packet is to be relayed to other networks
1034 * that have members of the packet's destination IPv6 multicast group.
1036 * The packet is returned unscathed to the caller, unless it is
1037 * erroneous, in which case a non-zero return value tells the caller to
1042 ip6_mforward(struct ip6_hdr
*ip6
, struct ifnet
*ifp
, struct mbuf
*m
)
1049 struct sockaddr_in6 sin6
;
1052 if (mrt6debug
& DEBUG_FORWARD
)
1053 log(LOG_DEBUG
, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1054 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
),
1059 * Don't forward a packet with Hop limit of zero or one,
1060 * or a packet destined to a local-only group.
1062 if (ip6
->ip6_hlim
<= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
) ||
1063 IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
1068 * Source address check: do not forward packets with unspecified
1069 * source. It was discussed in July 2000, on ipngwg mailing list.
1070 * This is rather more serious than unicast cases, because some
1071 * MLD packets can be sent with the unspecified source address
1072 * (although such packets must normally set the hop limit field to 1).
1074 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1075 IP6_STATINC(IP6_STAT_CANTFORWARD
);
1076 if (ip6_log_time
+ ip6_log_interval
< time_second
) {
1077 ip6_log_time
= time_second
;
1080 "from %s to %s nxt %d received on %s\n",
1081 ip6_sprintf(&ip6
->ip6_src
),
1082 ip6_sprintf(&ip6
->ip6_dst
),
1085 if_name(m
->m_pkthdr
.rcvif
) : "?");
1091 * Determine forwarding mifs from the forwarding cache table
1094 MF6CFIND(ip6
->ip6_src
, ip6
->ip6_dst
, rt
);
1096 /* Entry exists, so forward if necessary */
1099 return (ip6_mdq(m
, ifp
, rt
));
1102 * If we don't have a route for packet's origin,
1103 * Make a copy of the packet &
1104 * send message to routing daemon
1111 #ifdef UPCALL_TIMING
1115 #endif /* UPCALL_TIMING */
1117 mrt6stat
.mrt6s_no_route
++;
1119 if (mrt6debug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1120 log(LOG_DEBUG
, "ip6_mforward: no rte s %s g %s\n",
1121 ip6_sprintf(&ip6
->ip6_src
),
1122 ip6_sprintf(&ip6
->ip6_dst
));
1126 * Allocate mbufs early so that we don't do extra work if we
1127 * are just going to fail anyway.
1129 rte
= (struct rtdetq
*)malloc(sizeof(*rte
), M_MRTABLE
,
1135 mb0
= m_copy(m
, 0, M_COPYALL
);
1137 * Pullup packet header if needed before storing it,
1138 * as other references may modify it in the meantime.
1141 (M_READONLY(mb0
) || mb0
->m_len
< sizeof(struct ip6_hdr
)))
1142 mb0
= m_pullup(mb0
, sizeof(struct ip6_hdr
));
1144 free(rte
, M_MRTABLE
);
1149 /* is there an upcall waiting for this packet? */
1150 hash
= MF6CHASH(ip6
->ip6_src
, ip6
->ip6_dst
);
1151 for (rt
= mf6ctable
[hash
]; rt
; rt
= rt
->mf6c_next
) {
1152 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
,
1153 &rt
->mf6c_origin
.sin6_addr
) &&
1154 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1155 &rt
->mf6c_mcastgrp
.sin6_addr
) &&
1156 (rt
->mf6c_stall
!= NULL
))
1162 struct omrt6msg
*oim
;
1164 /* no upcall, so make a new entry */
1165 rt
= (struct mf6c
*)malloc(sizeof(*rt
), M_MRTABLE
,
1168 free(rte
, M_MRTABLE
);
1174 * Make a copy of the header to send to the user
1177 mm
= m_copy(mb0
, 0, sizeof(struct ip6_hdr
));
1180 free(rte
, M_MRTABLE
);
1182 free(rt
, M_MRTABLE
);
1188 * Send message to routing daemon
1190 sockaddr_in6_init(&sin6
, &ip6
->ip6_src
, 0, 0, 0);
1194 switch (ip6_mrouter_ver
) {
1196 oim
= mtod(mm
, struct omrt6msg
*);
1197 oim
->im6_msgtype
= MRT6MSG_NOCACHE
;
1201 im
= mtod(mm
, struct mrt6msg
*);
1202 im
->im6_msgtype
= MRT6MSG_NOCACHE
;
1206 free(rte
, M_MRTABLE
);
1208 free(rt
, M_MRTABLE
);
1214 if (mrt6debug
& DEBUG_FORWARD
)
1216 "getting the iif info in the kernel\n");
1219 for (mifp
= mif6table
, mifi
= 0;
1220 mifi
< nummifs
&& mifp
->m6_ifp
!= ifp
;
1224 switch (ip6_mrouter_ver
) {
1226 oim
->im6_mif
= mifi
;
1233 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1234 log(LOG_WARNING
, "ip6_mforward: ip6_mrouter "
1235 "socket queue full\n");
1236 mrt6stat
.mrt6s_upq_sockfull
++;
1237 free(rte
, M_MRTABLE
);
1239 free(rt
, M_MRTABLE
);
1244 mrt6stat
.mrt6s_upcalls
++;
1246 /* insert new entry at head of hash chain */
1247 memset(rt
, 0, sizeof(*rt
));
1248 sockaddr_in6_init(&rt
->mf6c_origin
, &ip6
->ip6_src
,
1250 sockaddr_in6_init(&rt
->mf6c_mcastgrp
, &ip6
->ip6_dst
,
1252 rt
->mf6c_expire
= UPCALL_EXPIRE
;
1254 rt
->mf6c_parent
= MF6C_INCOMPLETE_PARENT
;
1256 /* link into table */
1257 rt
->mf6c_next
= mf6ctable
[hash
];
1258 mf6ctable
[hash
] = rt
;
1259 /* Add this entry to the end of the queue */
1260 rt
->mf6c_stall
= rte
;
1262 /* determine if q has overflowed */
1266 for (p
= &rt
->mf6c_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1267 if (++npkts
> MAX_UPQ6
) {
1268 mrt6stat
.mrt6s_upq_ovflw
++;
1269 free(rte
, M_MRTABLE
);
1275 /* Add this entry to the end of the queue */
1282 #ifdef UPCALL_TIMING
1284 #endif /* UPCALL_TIMING */
1293 * Clean up cache entries if upcalls are not serviced
1294 * Call from the Slow Timeout mechanism, every 0.25 seconds.
1297 expire_upcalls(void *unused
)
1300 struct mf6c
*mfc
, **nptr
;
1303 mutex_enter(softnet_lock
);
1304 KERNEL_LOCK(1, NULL
);
1306 for (i
= 0; i
< MF6CTBLSIZ
; i
++) {
1307 if (n6expire
[i
] == 0)
1309 nptr
= &mf6ctable
[i
];
1310 while ((mfc
= *nptr
) != NULL
) {
1311 rte
= mfc
->mf6c_stall
;
1313 * Skip real cache entries
1314 * Make sure it wasn't marked to not expire (shouldn't happen)
1318 mfc
->mf6c_expire
!= 0 &&
1319 --mfc
->mf6c_expire
== 0) {
1321 if (mrt6debug
& DEBUG_EXPIRE
)
1322 log(LOG_DEBUG
, "expire_upcalls: expiring (%s %s)\n",
1323 ip6_sprintf(&mfc
->mf6c_origin
.sin6_addr
),
1324 ip6_sprintf(&mfc
->mf6c_mcastgrp
.sin6_addr
));
1327 * drop all the packets
1328 * free the mbuf with the pkt, if, timing info
1331 struct rtdetq
*n
= rte
->next
;
1333 free(rte
, M_MRTABLE
);
1335 } while (rte
!= NULL
);
1336 mrt6stat
.mrt6s_cache_cleanups
++;
1339 *nptr
= mfc
->mf6c_next
;
1340 free(mfc
, M_MRTABLE
);
1342 nptr
= &mfc
->mf6c_next
;
1346 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
1347 expire_upcalls
, NULL
);
1349 KERNEL_UNLOCK_ONE(NULL
);
1350 mutex_exit(softnet_lock
);
1354 * Packet forwarding routine once entry in the cache is made
1357 ip6_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mf6c
*rt
)
1359 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1362 int plen
= m
->m_pkthdr
.len
;
1363 struct in6_addr src0
, dst0
; /* copies for local work */
1364 u_int32_t iszone
, idzone
, oszone
, odzone
;
1368 * Macro to send packet on mif. Since RSVP packets don't get counted on
1369 * input, they shouldn't get counted on output, so statistics keeping is
1373 #define MC6_SEND(ip6, mifp, m) do { \
1374 if ((mifp)->m6_flags & MIFF_REGISTER) \
1375 register_send((ip6), (mifp), (m)); \
1377 phyint_send((ip6), (mifp), (m)); \
1378 } while (/*CONSTCOND*/ 0)
1381 * Don't forward if it didn't arrive from the parent mif
1384 mifi
= rt
->mf6c_parent
;
1385 if ((mifi
>= nummifs
) || (mif6table
[mifi
].m6_ifp
!= ifp
)) {
1386 /* came in the wrong interface */
1388 if (mrt6debug
& DEBUG_FORWARD
)
1390 "wrong if: ifid %d mifi %d mififid %x\n",
1391 ifp
->if_index
, mifi
,
1392 mif6table
[mifi
].m6_ifp
?
1393 mif6table
[mifi
].m6_ifp
->if_index
: -1);
1395 mrt6stat
.mrt6s_wrong_if
++;
1396 rt
->mf6c_wrong_if
++;
1398 * If we are doing PIM processing, and we are forwarding
1399 * packets on this interface, send a message to the
1402 /* have to make sure this is a valid mif */
1403 if (mifi
< nummifs
&& mif6table
[mifi
].m6_ifp
)
1404 if (pim6
&& (m
->m_flags
& M_LOOP
) == 0) {
1406 * Check the M_LOOP flag to avoid an
1407 * unnecessary PIM assert.
1408 * XXX: M_LOOP is an ad-hoc hack...
1410 struct sockaddr_in6 sin6
;
1414 struct omrt6msg
*oim
;
1416 mm
= m_copy(m
, 0, sizeof(struct ip6_hdr
));
1419 mm
->m_len
< sizeof(struct ip6_hdr
)))
1420 mm
= m_pullup(mm
, sizeof(struct ip6_hdr
));
1426 switch (ip6_mrouter_ver
) {
1428 oim
= mtod(mm
, struct omrt6msg
*);
1429 oim
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1433 im
= mtod(mm
, struct mrt6msg
*);
1434 im
->im6_msgtype
= MRT6MSG_WRONGMIF
;
1442 for (mifp
= mif6table
, iif
= 0;
1443 iif
< nummifs
&& mifp
&&
1444 mifp
->m6_ifp
!= ifp
;
1448 memset(&sin6
, 0, sizeof(sin6
));
1449 sin6
.sin6_len
= sizeof(sin6
);
1450 sin6
.sin6_family
= AF_INET6
;
1451 switch (ip6_mrouter_ver
) {
1454 sin6
.sin6_addr
= oim
->im6_src
;
1458 sin6
.sin6_addr
= im
->im6_src
;
1462 mrt6stat
.mrt6s_upcalls
++;
1464 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1467 log(LOG_WARNING
, "mdq, ip6_mrouter socket queue full\n");
1469 ++mrt6stat
.mrt6s_upq_sockfull
;
1471 } /* if socket Q full */
1474 } /* if wrong iif */
1476 /* If I sourced this packet, it counts as output, else it was input. */
1477 if (m
->m_pkthdr
.rcvif
== NULL
) {
1478 /* XXX: is rcvif really NULL when output?? */
1479 mif6table
[mifi
].m6_pkt_out
++;
1480 mif6table
[mifi
].m6_bytes_out
+= plen
;
1482 mif6table
[mifi
].m6_pkt_in
++;
1483 mif6table
[mifi
].m6_bytes_in
+= plen
;
1486 rt
->mf6c_byte_cnt
+= plen
;
1489 * For each mif, forward a copy of the packet if there are group
1490 * members downstream on the interface.
1492 src0
= ip6
->ip6_src
;
1493 dst0
= ip6
->ip6_dst
;
1494 if ((error
= in6_setscope(&src0
, ifp
, &iszone
)) != 0 ||
1495 (error
= in6_setscope(&dst0
, ifp
, &idzone
)) != 0) {
1496 IP6_STATINC(IP6_STAT_BADSCOPE
);
1499 for (mifp
= mif6table
, mifi
= 0; mifi
< nummifs
; mifp
++, mifi
++)
1500 if (IF_ISSET(mifi
, &rt
->mf6c_ifset
)) {
1501 if (mif6table
[mifi
].m6_ifp
== NULL
)
1504 * check if the outgoing packet is going to break
1506 * XXX: For packets through PIM register tunnel
1507 * interface, we believe the routing daemon.
1509 if ((mif6table
[rt
->mf6c_parent
].m6_flags
&
1510 MIFF_REGISTER
) == 0 &&
1511 (mif6table
[mifi
].m6_flags
& MIFF_REGISTER
) == 0) {
1512 if (in6_setscope(&src0
, mif6table
[mifi
].m6_ifp
,
1514 in6_setscope(&dst0
, mif6table
[mifi
].m6_ifp
,
1516 iszone
!= oszone
|| idzone
!= odzone
) {
1517 IP6_STATINC(IP6_STAT_BADSCOPE
);
1523 mifp
->m6_bytes_out
+= plen
;
1524 MC6_SEND(ip6
, mifp
, m
);
1530 phyint_send(struct ip6_hdr
*ip6
, struct mif6
*mifp
, struct mbuf
*m
)
1532 struct mbuf
*mb_copy
;
1533 struct ifnet
*ifp
= mifp
->m6_ifp
;
1536 static struct route ro
;
1537 struct in6_multi
*in6m
;
1538 struct sockaddr_in6 dst6
;
1543 * Make a new reference to the packet; make sure that
1544 * the IPv6 header is actually copied, not just referenced,
1545 * so that ip6_output() only scribbles on the copy.
1547 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1549 (M_READONLY(mb_copy
) || mb_copy
->m_len
< sizeof(struct ip6_hdr
)))
1550 mb_copy
= m_pullup(mb_copy
, sizeof(struct ip6_hdr
));
1551 if (mb_copy
== NULL
) {
1555 /* set MCAST flag to the outgoing packet */
1556 mb_copy
->m_flags
|= M_MCAST
;
1559 * If we sourced the packet, call ip6_output since we may divide
1560 * the packet into fragments when the packet is too big for the
1561 * outgoing interface.
1562 * Otherwise, we can simply send the packet to the interface
1565 if (m
->m_pkthdr
.rcvif
== NULL
) {
1566 struct ip6_moptions im6o
;
1568 im6o
.im6o_multicast_ifp
= ifp
;
1569 /* XXX: ip6_output will override ip6->ip6_hlim */
1570 im6o
.im6o_multicast_hlim
= ip6
->ip6_hlim
;
1571 im6o
.im6o_multicast_loop
= 1;
1572 error
= ip6_output(mb_copy
, NULL
, &ro
, IPV6_FORWARDING
,
1576 if (mrt6debug
& DEBUG_XMIT
)
1577 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1578 mifp
- mif6table
, error
);
1585 * If we belong to the destination multicast group
1586 * on the outgoing interface, loop back a copy.
1589 * Does not have to check source info, as it's alreay covered by
1592 sockaddr_in6_init(&dst6
, &ip6
->ip6_dst
, 0, 0, 0);
1594 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
1596 ip6_mloopback(ifp
, m
,
1597 satocsin6(rtcache_getdst(&ro
)));
1601 * Put the packet into the sending queue of the outgoing interface
1602 * if it would fit in the MTU of the interface.
1604 linkmtu
= IN6_LINKMTU(ifp
);
1605 if (mb_copy
->m_pkthdr
.len
<= linkmtu
|| linkmtu
< IPV6_MMTU
) {
1607 * We could call if_output directly here, but we use
1608 * nd6_output on purpose to see if IPv6 operation is allowed
1611 error
= nd6_output(ifp
, ifp
, mb_copy
, &dst6
, NULL
);
1613 if (mrt6debug
& DEBUG_XMIT
)
1614 log(LOG_DEBUG
, "phyint_send on mif %d err %d\n",
1615 mifp
- mif6table
, error
);
1619 * pMTU discovery is intentionally disabled by default, since
1620 * various router may notify pMTU in multicast, which can be
1621 * a DDoS to a router
1624 icmp6_error(mb_copy
, ICMP6_PACKET_TOO_BIG
, 0, linkmtu
);
1627 if (mrt6debug
& DEBUG_XMIT
)
1629 "phyint_send: packet too big on %s o %s g %s"
1630 " size %d(discarded)\n",
1632 ip6_sprintf(&ip6
->ip6_src
),
1633 ip6_sprintf(&ip6
->ip6_dst
),
1634 mb_copy
->m_pkthdr
.len
);
1635 #endif /* MRT6DEBUG */
1636 m_freem(mb_copy
); /* simply discard the packet */
1644 register_send(struct ip6_hdr
*ip6
, struct mif6
*mif
, struct mbuf
*m
)
1647 int i
, len
= m
->m_pkthdr
.len
;
1648 struct sockaddr_in6 sin6
;
1649 struct mrt6msg
*im6
;
1653 log(LOG_DEBUG
, "** IPv6 register_send **\n src %s dst %s\n",
1654 ip6_sprintf(&ip6
->ip6_src
), ip6_sprintf(&ip6
->ip6_dst
));
1656 PIM6_STATINC(PIM6_STAT_SND_REGISTERS
);
1658 /* Make a copy of the packet to send to the user level process */
1659 MGETHDR(mm
, M_DONTWAIT
, MT_HEADER
);
1662 mm
->m_data
+= max_linkhdr
;
1663 mm
->m_len
= sizeof(struct ip6_hdr
);
1665 if ((mm
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1669 i
= MHLEN
- M_LEADINGSPACE(mm
);
1672 mm
= m_pullup(mm
, i
);
1675 /* TODO: check it! */
1676 mm
->m_pkthdr
.len
= len
+ sizeof(struct ip6_hdr
);
1679 * Send message to routing daemon
1681 sockaddr_in6_init(&sin6
, &ip6
->ip6_src
, 0, 0, 0);
1683 im6
= mtod(mm
, struct mrt6msg
*);
1684 im6
->im6_msgtype
= MRT6MSG_WHOLEPKT
;
1687 im6
->im6_mif
= mif
- mif6table
;
1689 /* iif info is not given for reg. encap.n */
1690 mrt6stat
.mrt6s_upcalls
++;
1692 if (socket_send(ip6_mrouter
, mm
, &sin6
) < 0) {
1696 "register_send: ip6_mrouter socket queue full\n");
1698 ++mrt6stat
.mrt6s_upq_sockfull
;
1705 * PIM sparse mode hook
1706 * Receives the pim control messages, and passes them up to the listening
1707 * socket, using rip6_input.
1708 * The only message processed is the REGISTER pim message; the pim header
1709 * is stripped off, and the inner packet is passed to register_mforward.
1712 pim6_input(struct mbuf
**mp
, int *offp
, int proto
)
1714 struct pim
*pim
; /* pointer to a pim struct */
1715 struct ip6_hdr
*ip6
;
1717 struct mbuf
*m
= *mp
;
1721 PIM6_STATINC(PIM6_STAT_RCV_TOTAL
);
1723 ip6
= mtod(m
, struct ip6_hdr
*);
1724 pimlen
= m
->m_pkthdr
.len
- *offp
;
1729 if (pimlen
< PIM_MINLEN
) {
1730 PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT
);
1732 if (mrt6debug
& DEBUG_PIM
)
1733 log(LOG_DEBUG
,"pim6_input: PIM packet too short\n");
1736 return (IPPROTO_DONE
);
1740 * if the packet is at least as big as a REGISTER, go ahead
1741 * and grab the PIM REGISTER header size, to avoid another
1742 * possible m_pullup() later.
1744 * PIM_MINLEN == pimhdr + u_int32 == 8
1745 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1747 minlen
= (pimlen
>= PIM6_REG_MINLEN
) ? PIM6_REG_MINLEN
: PIM_MINLEN
;
1750 * Make sure that the IP6 and PIM headers in contiguous memory, and
1751 * possibly the PIM REGISTER header
1753 IP6_EXTHDR_GET(pim
, struct pim
*, m
, off
, minlen
);
1755 PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT
);
1756 return IPPROTO_DONE
;
1759 /* PIM version check */
1760 if (pim
->pim_ver
!= PIM_VERSION
) {
1761 PIM6_STATINC(PIM6_STAT_RCV_BADVERSION
);
1764 "pim6_input: incorrect version %d, expecting %d\n",
1765 pim
->pim_ver
, PIM_VERSION
);
1768 return (IPPROTO_DONE
);
1771 #define PIM6_CHECKSUM
1772 #ifdef PIM6_CHECKSUM
1777 * Validate checksum.
1778 * If PIM REGISTER, exclude the data packet
1780 if (pim
->pim_type
== PIM_REGISTER
)
1781 cksumlen
= PIM_MINLEN
;
1785 if (in6_cksum(m
, IPPROTO_PIM
, off
, cksumlen
)) {
1786 PIM6_STATINC(PIM6_STAT_RCV_BADSUM
);
1788 if (mrt6debug
& DEBUG_PIM
)
1790 "pim6_input: invalid checksum\n");
1793 return (IPPROTO_DONE
);
1796 #endif /* PIM_CHECKSUM */
1798 if (pim
->pim_type
== PIM_REGISTER
) {
1800 * since this is a REGISTER, we'll make a copy of the register
1801 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1804 static const struct sockaddr_in6 dst
= {
1805 .sin6_len
= sizeof(dst
),
1806 .sin6_family
= AF_INET6
,
1810 struct ip6_hdr
*eip6
;
1813 PIM6_STATINC(PIM6_STAT_RCV_REGISTERS
);
1815 if ((reg_mif_num
>= nummifs
) || (reg_mif_num
== (mifi_t
) -1)) {
1817 if (mrt6debug
& DEBUG_PIM
)
1819 "pim6_input: register mif not set: %d\n",
1823 return (IPPROTO_DONE
);
1826 reghdr
= (u_int32_t
*)(pim
+ 1);
1828 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
1829 goto pim6_input_to_daemon
;
1834 if (pimlen
< PIM6_REG_MINLEN
) {
1835 PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT
);
1836 PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS
);
1839 "pim6_input: register packet size too "
1840 "small %d from %s\n",
1841 pimlen
, ip6_sprintf(&ip6
->ip6_src
));
1844 return (IPPROTO_DONE
);
1847 eip6
= (struct ip6_hdr
*) (reghdr
+ 1);
1849 if (mrt6debug
& DEBUG_PIM
)
1851 "pim6_input[register], eip6: %s -> %s, "
1853 ip6_sprintf(&eip6
->ip6_src
),
1854 ip6_sprintf(&eip6
->ip6_dst
),
1855 ntohs(eip6
->ip6_plen
));
1858 /* verify the version number of the inner packet */
1859 if ((eip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1860 PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS
);
1862 log(LOG_DEBUG
, "pim6_input: invalid IP version (%d) "
1863 "of the inner packet\n",
1864 (eip6
->ip6_vfc
& IPV6_VERSION
));
1867 return (IPPROTO_NONE
);
1870 /* verify the inner packet is destined to a mcast group */
1871 if (!IN6_IS_ADDR_MULTICAST(&eip6
->ip6_dst
)) {
1872 PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS
);
1874 if (mrt6debug
& DEBUG_PIM
)
1876 "pim6_input: inner packet of register "
1877 "is not multicast %s\n",
1878 ip6_sprintf(&eip6
->ip6_dst
));
1881 return (IPPROTO_DONE
);
1885 * make a copy of the whole header to pass to the daemon later.
1887 mcp
= m_copy(m
, 0, off
+ PIM6_REG_MINLEN
);
1891 "pim6_input: pim register: "
1892 "could not copy register head\n");
1895 return (IPPROTO_DONE
);
1899 * forward the inner ip6 packet; point m_data at the inner ip6.
1901 m_adj(m
, off
+ PIM_MINLEN
);
1903 if (mrt6debug
& DEBUG_PIM
) {
1905 "pim6_input: forwarding decapsulated register: "
1906 "src %s, dst %s, mif %d\n",
1907 ip6_sprintf(&eip6
->ip6_src
),
1908 ip6_sprintf(&eip6
->ip6_dst
),
1913 looutput(mif6table
[reg_mif_num
].m6_ifp
, m
,
1914 (struct sockaddr
*)__UNCONST(&dst
),
1915 (struct rtentry
*) NULL
);
1917 /* prepare the register head to send to the mrouting daemon */
1922 * Pass the PIM message up to the daemon; if it is a register message
1923 * pass the 'head' only up to the daemon. This includes the
1924 * encapsulator ip6 header, pim header, register header and the
1925 * encapsulated ip6 header.
1927 pim6_input_to_daemon
:
1928 rip6_input(&m
, offp
, proto
);
1929 return (IPPROTO_DONE
);
1933 sysctl_net_inet6_pim6_stats(SYSCTLFN_ARGS
)
1936 return (NETSTAT_SYSCTL(pim6stat_percpu
, PIM6_NSTATS
));
1940 sysctl_net_inet6_pim6_setup(struct sysctllog
**clog
)
1942 sysctl_createv(clog
, 0, NULL
, NULL
,
1944 CTLTYPE_NODE
, "net", NULL
,
1947 sysctl_createv(clog
, 0, NULL
, NULL
,
1949 CTLTYPE_NODE
, "inet6", NULL
,
1951 CTL_NET
, PF_INET6
, CTL_EOL
);
1952 sysctl_createv(clog
, 0, NULL
, NULL
,
1954 CTLTYPE_NODE
, "pim6",
1955 SYSCTL_DESCR("PIMv6 settings"),
1957 CTL_NET
, PF_INET6
, IPPROTO_PIM
, CTL_EOL
);
1959 sysctl_createv(clog
, 0, NULL
, NULL
,
1961 CTLTYPE_STRUCT
, "stats",
1962 SYSCTL_DESCR("PIMv6 statistics"),
1963 sysctl_net_inet6_pim6_stats
, 0, NULL
, 0,
1964 CTL_NET
, PF_INET6
, IPPROTO_PIM
, PIM6CTL_STATS
,