Import the kernel GPT and UUID header files from FreeBSD, and bring in
[dragonfly/vkernel-mp.git] / sys / netinet6 / ip6_mroute.c
blobd6f02615311e328c49887b56d4e04e0c0c4ff2f6
1 /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.2.2.9 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_mroute.c,v 1.15 2007/04/22 01:13:14 dillon Exp $ */
3 /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */
5 /*
6 * Copyright (C) 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */
37 * IP multicast forwarding procedures
39 * Written by David Waitzman, BBN Labs, August 1988.
40 * Modified by Steve Deering, Stanford, February 1989.
41 * Modified by Mark J. Steiglitz, Stanford, May, 1991
42 * Modified by Van Jacobson, LBL, January 1993
43 * Modified by Ajit Thyagarajan, PARC, August 1993
44 * Modified by Bill Fenenr, PARC, April 1994
46 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sockio.h>
60 #include <sys/protosw.h>
61 #include <sys/errno.h>
62 #include <sys/time.h>
63 #include <sys/kernel.h>
64 #include <sys/syslog.h>
65 #include <sys/thread2.h>
67 #include <net/if.h>
68 #include <net/route.h>
69 #include <net/raw_cb.h>
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/ip6_mroute.h>
77 #include <netinet6/pim6.h>
78 #include <netinet6/pim6_var.h>
80 #include <net/net_osdep.h>
82 static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry");
84 #define M_HASCL(m) ((m)->m_flags & M_EXT)
86 static int ip6_mdq (struct mbuf *, struct ifnet *, struct mf6c *);
87 static void phyint_send (struct ip6_hdr *, struct mif6 *, struct mbuf *);
89 static int set_pim6 (int *);
90 static int socket_send (struct socket *, struct mbuf *,
91 struct sockaddr_in6 *);
92 static int register_send (struct ip6_hdr *, struct mif6 *,
93 struct mbuf *);
96 * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
97 * except for netstat or debugging purposes.
99 struct socket *ip6_mrouter = NULL;
100 int ip6_mrouter_ver = 0;
101 int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */
102 struct mrt6stat mrt6stat;
104 #define NO_RTE_FOUND 0x1
105 #define RTE_FOUND 0x2
107 struct mf6c *mf6ctable[MF6CTBLSIZ];
108 u_char n6expire[MF6CTBLSIZ];
109 static struct mif6 mif6table[MAXMIFS];
110 #ifdef MRT6DEBUG
111 u_int mrt6debug = 0; /* debug level */
112 #define DEBUG_MFC 0x02
113 #define DEBUG_FORWARD 0x04
114 #define DEBUG_EXPIRE 0x08
115 #define DEBUG_XMIT 0x10
116 #define DEBUG_REG 0x20
117 #define DEBUG_PIM 0x40
118 #endif
120 static void expire_upcalls (void *);
121 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
122 #define UPCALL_EXPIRE 6 /* number of timeouts */
124 #ifdef INET
125 #ifdef MROUTING
126 extern struct socket *ip_mrouter;
127 #endif
128 #endif
131 * 'Interfaces' associated with decapsulator (so we can tell
132 * packets that went through it from ones that get reflected
133 * by a broken gateway). These interfaces are never linked into
134 * the system ifnet list & no routes point to them. I.e., packets
135 * can't be sent this way. They only exist as a placeholder for
136 * multicast source verification.
138 struct ifnet multicast_register_if;
140 #define ENCAP_HOPS 64
143 * Private variables.
145 static mifi_t nummifs = 0;
146 static mifi_t reg_mif_num = (mifi_t)-1;
148 static struct pim6stat pim6stat;
149 static int pim6;
152 * Hash function for a source, group entry
154 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
155 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
156 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
157 (g).s6_addr32[2] ^ (g).s6_addr32[3])
160 * Find a route for a given origin IPv6 address and Multicast group address.
161 * Quality of service parameter to be added in the future!!!
164 #define MF6CFIND(o, g, rt) do { \
165 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
166 rt = NULL; \
167 mrt6stat.mrt6s_mfc_lookups++; \
168 while (_rt) { \
169 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
170 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
171 (_rt->mf6c_stall == NULL)) { \
172 rt = _rt; \
173 break; \
175 _rt = _rt->mf6c_next; \
177 if (rt == NULL) { \
178 mrt6stat.mrt6s_mfc_misses++; \
180 } while (0)
183 * Macros to compute elapsed time efficiently
184 * Borrowed from Van Jacobson's scheduling code
186 #define TV_DELTA(a, b, delta) do { \
187 int xxs; \
189 delta = (a).tv_usec - (b).tv_usec; \
190 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
191 switch (xxs) { \
192 case 2: \
193 delta += 1000000; \
194 /* fall through */ \
195 case 1: \
196 delta += 1000000; \
197 break; \
198 default: \
199 delta += (1000000 * xxs); \
202 } while (0)
204 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
205 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
207 #ifdef UPCALL_TIMING
208 #define UPCALL_MAX 50
209 u_long upcall_data[UPCALL_MAX + 1];
210 static void collate();
211 #endif /* UPCALL_TIMING */
213 static int get_sg_cnt (struct sioc_sg_req6 *);
214 static int get_mif6_cnt (struct sioc_mif_req6 *);
215 static int ip6_mrouter_init (struct socket *, struct mbuf *, int);
216 static int add_m6if (struct mif6ctl *);
217 static int del_m6if (mifi_t *);
218 static int add_m6fc (struct mf6cctl *);
219 static int del_m6fc (struct mf6cctl *);
221 static struct callout expire_upcalls_ch;
224 * Handle MRT setsockopt commands to modify the multicast routing tables.
227 ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
229 int error = 0;
230 struct mbuf *m;
232 if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
233 return (EACCES);
235 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
236 return (error);
237 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
238 return (error);
240 switch (sopt->sopt_name) {
241 case MRT6_INIT:
242 #ifdef MRT6_OINIT
243 case MRT6_OINIT:
244 #endif
245 error = ip6_mrouter_init(so, m, sopt->sopt_name);
246 break;
247 case MRT6_DONE:
248 error = ip6_mrouter_done();
249 break;
250 case MRT6_ADD_MIF:
251 error = add_m6if(mtod(m, struct mif6ctl *));
252 break;
253 case MRT6_DEL_MIF:
254 error = del_m6if(mtod(m, mifi_t *));
255 break;
256 case MRT6_ADD_MFC:
257 error = add_m6fc(mtod(m, struct mf6cctl *));
258 break;
259 case MRT6_DEL_MFC:
260 error = del_m6fc(mtod(m, struct mf6cctl *));
261 break;
262 case MRT6_PIM:
263 error = set_pim6(mtod(m, int *));
264 break;
265 default:
266 error = EOPNOTSUPP;
267 break;
270 m_freem(m);
271 return (error);
275 * Handle MRT getsockopt commands
278 ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
280 int error = 0;
282 if (so != ip6_mrouter) return EACCES;
284 switch (sopt->sopt_name) {
285 case MRT6_PIM:
286 error = sooptcopyout(sopt, &pim6, sizeof(pim6));
287 break;
289 return (error);
293 * Handle ioctl commands to obtain information from the cache
296 mrt6_ioctl(int cmd, caddr_t data)
298 int error = 0;
300 switch (cmd) {
301 case SIOCGETSGCNT_IN6:
302 return (get_sg_cnt((struct sioc_sg_req6 *)data));
303 break; /* for safety */
304 case SIOCGETMIFCNT_IN6:
305 return (get_mif6_cnt((struct sioc_mif_req6 *)data));
306 break; /* for safety */
307 default:
308 return (EINVAL);
309 break;
311 return error;
315 * returns the packet, byte, rpf-failure count for the source group provided
317 static int
318 get_sg_cnt(struct sioc_sg_req6 *req)
320 struct mf6c *rt;
322 crit_enter();
323 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
324 crit_exit();
325 if (rt != NULL) {
326 req->pktcnt = rt->mf6c_pkt_cnt;
327 req->bytecnt = rt->mf6c_byte_cnt;
328 req->wrong_if = rt->mf6c_wrong_if;
329 } else
330 return (ESRCH);
331 #if 0
332 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
333 #endif
335 return 0;
339 * returns the input and output packet and byte counts on the mif provided
341 static int
342 get_mif6_cnt(struct sioc_mif_req6 *req)
344 mifi_t mifi = req->mifi;
346 if (mifi >= nummifs)
347 return EINVAL;
349 req->icount = mif6table[mifi].m6_pkt_in;
350 req->ocount = mif6table[mifi].m6_pkt_out;
351 req->ibytes = mif6table[mifi].m6_bytes_in;
352 req->obytes = mif6table[mifi].m6_bytes_out;
354 return 0;
357 static int
358 set_pim6(int *i)
360 if ((*i != 1) && (*i != 0))
361 return EINVAL;
363 pim6 = *i;
365 return 0;
369 * Enable multicast routing
371 static int
372 ip6_mrouter_init(struct socket *so, struct mbuf *m, int cmd)
374 int *v;
376 #ifdef MRT6DEBUG
377 if (mrt6debug)
378 log(LOG_DEBUG,
379 "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
380 so->so_type, so->so_proto->pr_protocol);
381 #endif
383 if (so->so_type != SOCK_RAW ||
384 so->so_proto->pr_protocol != IPPROTO_ICMPV6)
385 return EOPNOTSUPP;
387 if (!m || (m->m_len != sizeof(int *)))
388 return ENOPROTOOPT;
390 v = mtod(m, int *);
391 if (*v != 1)
392 return ENOPROTOOPT;
394 if (ip6_mrouter != NULL) return EADDRINUSE;
396 ip6_mrouter = so;
397 ip6_mrouter_ver = cmd;
399 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
400 bzero((caddr_t)n6expire, sizeof(n6expire));
402 pim6 = 0;/* used for stubbing out/in pim stuff */
404 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
405 expire_upcalls, NULL);
407 #ifdef MRT6DEBUG
408 if (mrt6debug)
409 log(LOG_DEBUG, "ip6_mrouter_init\n");
410 #endif
412 return 0;
416 * Disable multicast routing
419 ip6_mrouter_done(void)
421 mifi_t mifi;
422 int i;
423 struct ifnet *ifp;
424 struct in6_ifreq ifr;
425 struct mf6c *rt;
426 struct rtdetq *rte;
429 * For each phyint in use, disable promiscuous reception of all IPv6
430 * multicasts.
432 #ifdef INET
433 #ifdef MROUTING
435 * If there is still IPv4 multicast routing daemon,
436 * we remain interfaces to receive all muliticasted packets.
437 * XXX: there may be an interface in which the IPv4 multicast
438 * daemon is not interested...
440 if (!ip_mrouter)
441 #endif
442 #endif
444 for (mifi = 0; mifi < nummifs; mifi++) {
445 if (mif6table[mifi].m6_ifp &&
446 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
447 ifr.ifr_addr.sin6_family = AF_INET6;
448 ifr.ifr_addr.sin6_addr= kin6addr_any;
449 ifp = mif6table[mifi].m6_ifp;
450 lwkt_serialize_enter(ifp->if_serializer);
451 ifp->if_ioctl(ifp, SIOCDELMULTI,
452 (caddr_t)&ifr, NULL);
453 lwkt_serialize_exit(ifp->if_serializer);
457 #ifdef notyet
458 bzero((caddr_t)qtable, sizeof(qtable));
459 bzero((caddr_t)tbftable, sizeof(tbftable));
460 #endif
461 bzero((caddr_t)mif6table, sizeof(mif6table));
462 nummifs = 0;
464 pim6 = 0; /* used to stub out/in pim specific code */
466 callout_stop(&expire_upcalls_ch);
469 * Free all multicast forwarding cache entries.
471 for (i = 0; i < MF6CTBLSIZ; i++) {
472 rt = mf6ctable[i];
473 while (rt) {
474 struct mf6c *frt;
476 for (rte = rt->mf6c_stall; rte != NULL; ) {
477 struct rtdetq *n = rte->next;
479 m_free(rte->m);
480 kfree(rte, M_MRTABLE);
481 rte = n;
483 frt = rt;
484 rt = rt->mf6c_next;
485 kfree(frt, M_MRTABLE);
489 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
492 * Reset de-encapsulation cache
494 reg_mif_num = -1;
496 ip6_mrouter = NULL;
497 ip6_mrouter_ver = 0;
499 #ifdef MRT6DEBUG
500 if (mrt6debug)
501 log(LOG_DEBUG, "ip6_mrouter_done\n");
502 #endif
504 return 0;
507 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
510 * Add a mif to the mif table
512 static int
513 add_m6if(struct mif6ctl *mifcp)
515 struct mif6 *mifp;
516 struct ifnet *ifp;
517 int error;
518 #ifdef notyet
519 struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
520 #endif
522 if (mifcp->mif6c_mifi >= MAXMIFS)
523 return EINVAL;
524 mifp = mif6table + mifcp->mif6c_mifi;
525 if (mifp->m6_ifp)
526 return EADDRINUSE; /* XXX: is it appropriate? */
527 if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
528 return ENXIO;
529 ifp = ifindex2ifnet[mifcp->mif6c_pifi];
531 if (mifcp->mif6c_flags & MIFF_REGISTER) {
532 if (reg_mif_num == (mifi_t)-1) {
533 strlcpy(multicast_register_if.if_xname, "register_mif",
534 IFNAMSIZ);
535 multicast_register_if.if_flags |= IFF_LOOPBACK;
536 multicast_register_if.if_index = mifcp->mif6c_mifi;
537 reg_mif_num = mifcp->mif6c_mifi;
540 ifp = &multicast_register_if;
542 } /* if REGISTER */
543 else {
544 /* Make sure the interface supports multicast */
545 if ((ifp->if_flags & IFF_MULTICAST) == 0)
546 return EOPNOTSUPP;
548 crit_enter();
549 error = if_allmulti(ifp, 1);
550 crit_exit();
551 if (error)
552 return error;
555 crit_enter();
556 mifp->m6_flags = mifcp->mif6c_flags;
557 mifp->m6_ifp = ifp;
558 #ifdef notyet
559 /* scaling up here allows division by 1024 in critical code */
560 mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
561 #endif
562 /* initialize per mif pkt counters */
563 mifp->m6_pkt_in = 0;
564 mifp->m6_pkt_out = 0;
565 mifp->m6_bytes_in = 0;
566 mifp->m6_bytes_out = 0;
567 crit_exit();
569 /* Adjust nummifs up if the mifi is higher than nummifs */
570 if (nummifs <= mifcp->mif6c_mifi)
571 nummifs = mifcp->mif6c_mifi + 1;
573 #ifdef MRT6DEBUG
574 if (mrt6debug)
575 log(LOG_DEBUG,
576 "add_mif #%d, phyint %s\n",
577 mifcp->mif6c_mifi,
578 ifp->if_xname);
579 #endif
581 return 0;
585 * Delete a mif from the mif table
587 static int
588 del_m6if(mifi_t *mifip)
590 struct mif6 *mifp = mif6table + *mifip;
591 mifi_t mifi;
592 struct ifnet *ifp;
594 if (*mifip >= nummifs)
595 return EINVAL;
596 if (mifp->m6_ifp == NULL)
597 return EINVAL;
599 crit_enter();
601 if (!(mifp->m6_flags & MIFF_REGISTER)) {
603 * XXX: what if there is yet IPv4 multicast daemon
604 * using the interface?
606 ifp = mifp->m6_ifp;
608 if_allmulti(ifp, 0);
611 #ifdef notyet
612 bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
613 bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
614 #endif
615 bzero((caddr_t)mifp, sizeof (*mifp));
617 /* Adjust nummifs down */
618 for (mifi = nummifs; mifi > 0; mifi--)
619 if (mif6table[mifi - 1].m6_ifp)
620 break;
621 nummifs = mifi;
623 crit_exit();
625 #ifdef MRT6DEBUG
626 if (mrt6debug)
627 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
628 #endif
630 return 0;
634 * Add an mfc entry
636 static int
637 add_m6fc(struct mf6cctl *mfccp)
639 struct mf6c *rt;
640 u_long hash;
641 struct rtdetq *rte;
642 u_short nstl;
644 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
645 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
647 /* If an entry already exists, just update the fields */
648 if (rt) {
649 #ifdef MRT6DEBUG
650 if (mrt6debug & DEBUG_MFC)
651 log(LOG_DEBUG,
652 "add_m6fc no upcall h %d o %s g %s p %x\n",
653 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
654 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
655 mfccp->mf6cc_parent);
656 #endif
658 crit_enter();
659 rt->mf6c_parent = mfccp->mf6cc_parent;
660 rt->mf6c_ifset = mfccp->mf6cc_ifset;
661 crit_exit();
662 return 0;
666 * Find the entry for which the upcall was made and update
668 crit_enter();
669 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
670 mfccp->mf6cc_mcastgrp.sin6_addr);
671 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
672 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
673 &mfccp->mf6cc_origin.sin6_addr) &&
674 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
675 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
676 (rt->mf6c_stall != NULL)) {
678 if (nstl++)
679 log(LOG_ERR,
680 "add_m6fc: %s o %s g %s p %x dbx %p\n",
681 "multiple kernel entries",
682 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
683 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
684 mfccp->mf6cc_parent, rt->mf6c_stall);
686 #ifdef MRT6DEBUG
687 if (mrt6debug & DEBUG_MFC)
688 log(LOG_DEBUG,
689 "add_m6fc o %s g %s p %x dbg %x\n",
690 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
691 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
692 mfccp->mf6cc_parent, rt->mf6c_stall);
693 #endif
695 rt->mf6c_origin = mfccp->mf6cc_origin;
696 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
697 rt->mf6c_parent = mfccp->mf6cc_parent;
698 rt->mf6c_ifset = mfccp->mf6cc_ifset;
699 /* initialize pkt counters per src-grp */
700 rt->mf6c_pkt_cnt = 0;
701 rt->mf6c_byte_cnt = 0;
702 rt->mf6c_wrong_if = 0;
704 rt->mf6c_expire = 0; /* Don't clean this guy up */
705 n6expire[hash]--;
707 /* free packets Qed at the end of this entry */
708 for (rte = rt->mf6c_stall; rte != NULL; ) {
709 struct rtdetq *n = rte->next;
710 ip6_mdq(rte->m, rte->ifp, rt);
711 m_freem(rte->m);
712 #ifdef UPCALL_TIMING
713 collate(&(rte->t));
714 #endif /* UPCALL_TIMING */
715 kfree(rte, M_MRTABLE);
716 rte = n;
718 rt->mf6c_stall = NULL;
723 * It is possible that an entry is being inserted without an upcall
725 if (nstl == 0) {
726 #ifdef MRT6DEBUG
727 if (mrt6debug & DEBUG_MFC)
728 log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
729 hash,
730 ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
731 ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
732 mfccp->mf6cc_parent);
733 #endif
735 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
737 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
738 &mfccp->mf6cc_origin.sin6_addr)&&
739 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
740 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
742 rt->mf6c_origin = mfccp->mf6cc_origin;
743 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
744 rt->mf6c_parent = mfccp->mf6cc_parent;
745 rt->mf6c_ifset = mfccp->mf6cc_ifset;
746 /* initialize pkt counters per src-grp */
747 rt->mf6c_pkt_cnt = 0;
748 rt->mf6c_byte_cnt = 0;
749 rt->mf6c_wrong_if = 0;
751 if (rt->mf6c_expire)
752 n6expire[hash]--;
753 rt->mf6c_expire = 0;
756 if (rt == NULL) {
757 /* no upcall, so make a new entry */
758 rt = (struct mf6c *)kmalloc(sizeof(*rt), M_MRTABLE,
759 M_NOWAIT);
760 if (rt == NULL) {
761 crit_exit();
762 return ENOBUFS;
765 /* insert new entry at head of hash chain */
766 rt->mf6c_origin = mfccp->mf6cc_origin;
767 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
768 rt->mf6c_parent = mfccp->mf6cc_parent;
769 rt->mf6c_ifset = mfccp->mf6cc_ifset;
770 /* initialize pkt counters per src-grp */
771 rt->mf6c_pkt_cnt = 0;
772 rt->mf6c_byte_cnt = 0;
773 rt->mf6c_wrong_if = 0;
774 rt->mf6c_expire = 0;
775 rt->mf6c_stall = NULL;
777 /* link into table */
778 rt->mf6c_next = mf6ctable[hash];
779 mf6ctable[hash] = rt;
782 crit_exit();
783 return 0;
786 #ifdef UPCALL_TIMING
788 * collect delay statistics on the upcalls
790 static void
791 collate(struct timeval *t)
793 u_long d;
794 struct timeval tp;
795 u_long delta;
797 GET_TIME(tp);
799 if (TV_LT(*t, tp))
801 TV_DELTA(tp, *t, delta);
803 d = delta >> 10;
804 if (d > UPCALL_MAX)
805 d = UPCALL_MAX;
807 ++upcall_data[d];
810 #endif /* UPCALL_TIMING */
813 * Delete an mfc entry
815 static int
816 del_m6fc(struct mf6cctl *mfccp)
818 struct sockaddr_in6 origin;
819 struct sockaddr_in6 mcastgrp;
820 struct mf6c *rt;
821 struct mf6c **nptr;
822 u_long hash;
824 origin = mfccp->mf6cc_origin;
825 mcastgrp = mfccp->mf6cc_mcastgrp;
826 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
828 #ifdef MRT6DEBUG
829 if (mrt6debug & DEBUG_MFC)
830 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
831 ip6_sprintf(&origin.sin6_addr),
832 ip6_sprintf(&mcastgrp.sin6_addr));
833 #endif
835 crit_enter();
837 nptr = &mf6ctable[hash];
838 while ((rt = *nptr) != NULL) {
839 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
840 &rt->mf6c_origin.sin6_addr) &&
841 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
842 &rt->mf6c_mcastgrp.sin6_addr) &&
843 rt->mf6c_stall == NULL)
844 break;
846 nptr = &rt->mf6c_next;
848 if (rt == NULL) {
849 crit_exit();
850 return EADDRNOTAVAIL;
853 *nptr = rt->mf6c_next;
854 kfree(rt, M_MRTABLE);
856 crit_exit();
858 return 0;
861 static int
862 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
864 if (s) {
865 if (ssb_appendaddr(&s->so_rcv,
866 (struct sockaddr *)src,
867 mm, (struct mbuf *)0) != 0) {
868 sorwakeup(s);
869 return 0;
872 m_freem(mm);
873 return -1;
877 * IPv6 multicast forwarding function. This function assumes that the packet
878 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
879 * pointed to by "ifp", and the packet is to be relayed to other networks
880 * that have members of the packet's destination IPv6 multicast group.
882 * The packet is returned unscathed to the caller, unless it is
883 * erroneous, in which case a non-zero return value tells the caller to
884 * discard it.
888 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
890 struct mf6c *rt;
891 struct mif6 *mifp;
892 struct mbuf *mm;
893 mifi_t mifi;
895 #ifdef MRT6DEBUG
896 if (mrt6debug & DEBUG_FORWARD)
897 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
898 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
899 ifp->if_index);
900 #endif
903 * Don't forward a packet with Hop limit of zero or one,
904 * or a packet destined to a local-only group.
906 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
907 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
908 return 0;
909 ip6->ip6_hlim--;
912 * Source address check: do not forward packets with unspecified
913 * source. It was discussed in July 2000, on ipngwg mailing list.
914 * This is rather more serious than unicast cases, because some
915 * MLD packets can be sent with the unspecified source address
916 * (although such packets must normally set 1 to the hop limit field).
918 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
919 ip6stat.ip6s_cantforward++;
920 if (ip6_log_time + ip6_log_interval < time_second) {
921 ip6_log_time = time_second;
922 log(LOG_DEBUG,
923 "cannot forward "
924 "from %s to %s nxt %d received on %s\n",
925 ip6_sprintf(&ip6->ip6_src),
926 ip6_sprintf(&ip6->ip6_dst),
927 ip6->ip6_nxt,
928 if_name(m->m_pkthdr.rcvif));
930 return 0;
934 * Determine forwarding mifs from the forwarding cache table
936 crit_enter();
937 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
939 /* Entry exists, so forward if necessary */
940 if (rt) {
941 crit_exit();
942 return (ip6_mdq(m, ifp, rt));
943 } else {
945 * If we don't have a route for packet's origin,
946 * Make a copy of the packet &
947 * send message to routing daemon
950 struct mbuf *mb0;
951 struct rtdetq *rte;
952 u_long hash;
953 /* int i, npkts;*/
954 #ifdef UPCALL_TIMING
955 struct timeval tp;
957 GET_TIME(tp);
958 #endif /* UPCALL_TIMING */
960 mrt6stat.mrt6s_no_route++;
961 #ifdef MRT6DEBUG
962 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
963 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
964 ip6_sprintf(&ip6->ip6_src),
965 ip6_sprintf(&ip6->ip6_dst));
966 #endif
969 * Allocate mbufs early so that we don't do extra work if we
970 * are just going to fail anyway.
972 rte = (struct rtdetq *)kmalloc(sizeof(*rte), M_MRTABLE,
973 M_NOWAIT);
974 if (rte == NULL) {
975 crit_exit();
976 return ENOBUFS;
978 mb0 = m_copy(m, 0, M_COPYALL);
980 * Pullup packet header if needed before storing it,
981 * as other references may modify it in the meantime.
983 if (mb0 &&
984 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
985 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
986 if (mb0 == NULL) {
987 kfree(rte, M_MRTABLE);
988 crit_exit();
989 return ENOBUFS;
992 /* is there an upcall waiting for this packet? */
993 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
994 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
995 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
996 &rt->mf6c_origin.sin6_addr) &&
997 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
998 &rt->mf6c_mcastgrp.sin6_addr) &&
999 (rt->mf6c_stall != NULL))
1000 break;
1003 if (rt == NULL) {
1004 struct mrt6msg *im;
1005 #ifdef MRT6_OINIT
1006 struct omrt6msg *oim;
1007 #endif
1009 /* no upcall, so make a new entry */
1010 rt = (struct mf6c *)kmalloc(sizeof(*rt), M_MRTABLE,
1011 M_NOWAIT);
1012 if (rt == NULL) {
1013 kfree(rte, M_MRTABLE);
1014 m_freem(mb0);
1015 crit_exit();
1016 return ENOBUFS;
1019 * Make a copy of the header to send to the user
1020 * level process
1022 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1024 if (mm == NULL) {
1025 kfree(rte, M_MRTABLE);
1026 m_freem(mb0);
1027 kfree(rt, M_MRTABLE);
1028 crit_exit();
1029 return ENOBUFS;
1033 * Send message to routing daemon
1035 sin6.sin6_addr = ip6->ip6_src;
1037 im = NULL;
1038 #ifdef MRT6_OINIT
1039 oim = NULL;
1040 #endif
1041 switch (ip6_mrouter_ver) {
1042 #ifdef MRT6_OINIT
1043 case MRT6_OINIT:
1044 oim = mtod(mm, struct omrt6msg *);
1045 oim->im6_msgtype = MRT6MSG_NOCACHE;
1046 oim->im6_mbz = 0;
1047 break;
1048 #endif
1049 case MRT6_INIT:
1050 im = mtod(mm, struct mrt6msg *);
1051 im->im6_msgtype = MRT6MSG_NOCACHE;
1052 im->im6_mbz = 0;
1053 break;
1054 default:
1055 kfree(rte, M_MRTABLE);
1056 m_freem(mb0);
1057 kfree(rt, M_MRTABLE);
1058 crit_exit();
1059 return EINVAL;
1062 #ifdef MRT6DEBUG
1063 if (mrt6debug & DEBUG_FORWARD)
1064 log(LOG_DEBUG,
1065 "getting the iif info in the kernel\n");
1066 #endif
1068 for (mifp = mif6table, mifi = 0;
1069 mifi < nummifs && mifp->m6_ifp != ifp;
1070 mifp++, mifi++)
1073 switch (ip6_mrouter_ver) {
1074 #ifdef MRT6_OINIT
1075 case MRT6_OINIT:
1076 oim->im6_mif = mifi;
1077 break;
1078 #endif
1079 case MRT6_INIT:
1080 im->im6_mif = mifi;
1081 break;
1084 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1085 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1086 "socket queue full\n");
1087 mrt6stat.mrt6s_upq_sockfull++;
1088 kfree(rte, M_MRTABLE);
1089 m_freem(mb0);
1090 kfree(rt, M_MRTABLE);
1091 crit_exit();
1092 return ENOBUFS;
1095 mrt6stat.mrt6s_upcalls++;
1097 /* insert new entry at head of hash chain */
1098 bzero(rt, sizeof(*rt));
1099 rt->mf6c_origin.sin6_family = AF_INET6;
1100 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1101 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1102 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1103 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1104 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1105 rt->mf6c_expire = UPCALL_EXPIRE;
1106 n6expire[hash]++;
1107 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1109 /* link into table */
1110 rt->mf6c_next = mf6ctable[hash];
1111 mf6ctable[hash] = rt;
1112 /* Add this entry to the end of the queue */
1113 rt->mf6c_stall = rte;
1114 } else {
1115 /* determine if q has overflowed */
1116 struct rtdetq **p;
1117 int npkts = 0;
1119 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1120 if (++npkts > MAX_UPQ6) {
1121 mrt6stat.mrt6s_upq_ovflw++;
1122 kfree(rte, M_MRTABLE);
1123 m_freem(mb0);
1124 crit_exit();
1125 return 0;
1128 /* Add this entry to the end of the queue */
1129 *p = rte;
1132 rte->next = NULL;
1133 rte->m = mb0;
1134 rte->ifp = ifp;
1135 #ifdef UPCALL_TIMING
1136 rte->t = tp;
1137 #endif /* UPCALL_TIMING */
1139 crit_exit();
1141 return 0;
1146 * Clean up cache entries if upcalls are not serviced
1147 * Call from the Slow Timeout mechanism, every half second.
1149 static void
1150 expire_upcalls(void *unused)
1152 struct rtdetq *rte;
1153 struct mf6c *mfc, **nptr;
1154 int i;
1156 crit_enter();
1157 for (i = 0; i < MF6CTBLSIZ; i++) {
1158 if (n6expire[i] == 0)
1159 continue;
1160 nptr = &mf6ctable[i];
1161 while ((mfc = *nptr) != NULL) {
1162 rte = mfc->mf6c_stall;
1164 * Skip real cache entries
1165 * Make sure it wasn't marked to not expire (shouldn't happen)
1166 * If it expires now
1168 if (rte != NULL &&
1169 mfc->mf6c_expire != 0 &&
1170 --mfc->mf6c_expire == 0) {
1171 #ifdef MRT6DEBUG
1172 if (mrt6debug & DEBUG_EXPIRE)
1173 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1174 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1175 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1176 #endif
1178 * drop all the packets
1179 * free the mbuf with the pkt, if, timing info
1181 do {
1182 struct rtdetq *n = rte->next;
1183 m_freem(rte->m);
1184 kfree(rte, M_MRTABLE);
1185 rte = n;
1186 } while (rte != NULL);
1187 mrt6stat.mrt6s_cache_cleanups++;
1188 n6expire[i]--;
1190 *nptr = mfc->mf6c_next;
1191 kfree(mfc, M_MRTABLE);
1192 } else {
1193 nptr = &mfc->mf6c_next;
1197 crit_exit();
1198 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1199 expire_upcalls, NULL);
1203 * Packet forwarding routine once entry in the cache is made
1205 static int
1206 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1208 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1209 mifi_t mifi, iif;
1210 struct mif6 *mifp;
1211 int plen = m->m_pkthdr.len;
1214 * Macro to send packet on mif. Since RSVP packets don't get counted on
1215 * input, they shouldn't get counted on output, so statistics keeping is
1216 * separate.
1219 #define MC6_SEND(ip6, mifp, m) do { \
1220 if ((mifp)->m6_flags & MIFF_REGISTER) \
1221 register_send((ip6), (mifp), (m)); \
1222 else \
1223 phyint_send((ip6), (mifp), (m)); \
1224 } while (0)
1227 * Don't forward if it didn't arrive from the parent mif
1228 * for its origin.
1230 mifi = rt->mf6c_parent;
1231 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1232 /* came in the wrong interface */
1233 #ifdef MRT6DEBUG
1234 if (mrt6debug & DEBUG_FORWARD)
1235 log(LOG_DEBUG,
1236 "wrong if: ifid %d mifi %d mififid %x\n",
1237 ifp->if_index, mifi,
1238 mif6table[mifi].m6_ifp->if_index);
1239 #endif
1240 mrt6stat.mrt6s_wrong_if++;
1241 rt->mf6c_wrong_if++;
1243 * If we are doing PIM processing, and we are forwarding
1244 * packets on this interface, send a message to the
1245 * routing daemon.
1247 /* have to make sure this is a valid mif */
1248 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1249 if (pim6 && (m->m_flags & M_LOOP) == 0) {
1251 * Check the M_LOOP flag to avoid an
1252 * unnecessary PIM assert.
1253 * XXX: M_LOOP is an ad-hoc hack...
1255 static struct sockaddr_in6 sin6 =
1256 { sizeof(sin6), AF_INET6 };
1258 struct mbuf *mm;
1259 struct mrt6msg *im;
1260 #ifdef MRT6_OINIT
1261 struct omrt6msg *oim;
1262 #endif
1264 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1265 if (mm &&
1266 (M_HASCL(mm) ||
1267 mm->m_len < sizeof(struct ip6_hdr)))
1268 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1269 if (mm == NULL)
1270 return ENOBUFS;
1272 #ifdef MRT6_OINIT
1273 oim = NULL;
1274 #endif
1275 im = NULL;
1276 switch (ip6_mrouter_ver) {
1277 #ifdef MRT6_OINIT
1278 case MRT6_OINIT:
1279 oim = mtod(mm, struct omrt6msg *);
1280 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1281 oim->im6_mbz = 0;
1282 break;
1283 #endif
1284 case MRT6_INIT:
1285 im = mtod(mm, struct mrt6msg *);
1286 im->im6_msgtype = MRT6MSG_WRONGMIF;
1287 im->im6_mbz = 0;
1288 break;
1289 default:
1290 m_freem(mm);
1291 return EINVAL;
1294 for (mifp = mif6table, iif = 0;
1295 iif < nummifs && mifp &&
1296 mifp->m6_ifp != ifp;
1297 mifp++, iif++)
1300 switch (ip6_mrouter_ver) {
1301 #ifdef MRT6_OINIT
1302 case MRT6_OINIT:
1303 oim->im6_mif = iif;
1304 sin6.sin6_addr = oim->im6_src;
1305 break;
1306 #endif
1307 case MRT6_INIT:
1308 im->im6_mif = iif;
1309 sin6.sin6_addr = im->im6_src;
1310 break;
1313 mrt6stat.mrt6s_upcalls++;
1315 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1316 #ifdef MRT6DEBUG
1317 if (mrt6debug)
1318 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1319 #endif
1320 ++mrt6stat.mrt6s_upq_sockfull;
1321 return ENOBUFS;
1322 } /* if socket Q full */
1323 } /* if PIM */
1324 return 0;
1325 } /* if wrong iif */
1327 /* If I sourced this packet, it counts as output, else it was input. */
1328 if (m->m_pkthdr.rcvif == NULL) {
1329 /* XXX: is rcvif really NULL when output?? */
1330 mif6table[mifi].m6_pkt_out++;
1331 mif6table[mifi].m6_bytes_out += plen;
1332 } else {
1333 mif6table[mifi].m6_pkt_in++;
1334 mif6table[mifi].m6_bytes_in += plen;
1336 rt->mf6c_pkt_cnt++;
1337 rt->mf6c_byte_cnt += plen;
1340 * For each mif, forward a copy of the packet if there are group
1341 * members downstream on the interface.
1343 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1344 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1346 * check if the outgoing packet is going to break
1347 * a scope boundary.
1348 * XXX For packets through PIM register tunnel
1349 * interface, we believe a routing daemon.
1351 if ((mif6table[rt->mf6c_parent].m6_flags &
1352 MIFF_REGISTER) == 0 &&
1353 (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 &&
1354 (in6_addr2scopeid(ifp, &ip6->ip6_dst) !=
1355 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1356 &ip6->ip6_dst) ||
1357 in6_addr2scopeid(ifp, &ip6->ip6_src) !=
1358 in6_addr2scopeid(mif6table[mifi].m6_ifp,
1359 &ip6->ip6_src))) {
1360 ip6stat.ip6s_badscope++;
1361 continue;
1364 mifp->m6_pkt_out++;
1365 mifp->m6_bytes_out += plen;
1366 MC6_SEND(ip6, mifp, m);
1368 return 0;
1371 static void
1372 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1374 struct mbuf *mb_copy;
1375 struct ifnet *ifp = mifp->m6_ifp;
1376 int error = 0;
1377 static struct route_in6 ro;
1378 struct in6_multi *in6m;
1379 struct sockaddr_in6 *dst6;
1381 crit_enter(); /* needs to protect static "ro" below. */
1384 * Make a new reference to the packet; make sure that
1385 * the IPv6 header is actually copied, not just referenced,
1386 * so that ip6_output() only scribbles on the copy.
1388 mb_copy = m_copy(m, 0, M_COPYALL);
1389 if (mb_copy &&
1390 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1391 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1392 if (mb_copy == NULL) {
1393 crit_exit();
1394 return;
1396 /* set MCAST flag to the outgoing packet */
1397 mb_copy->m_flags |= M_MCAST;
1400 * If we sourced the packet, call ip6_output since we may devide
1401 * the packet into fragments when the packet is too big for the
1402 * outgoing interface.
1403 * Otherwise, we can simply send the packet to the interface
1404 * sending queue.
1406 if (m->m_pkthdr.rcvif == NULL) {
1407 struct ip6_moptions im6o;
1409 im6o.im6o_multicast_ifp = ifp;
1410 /* XXX: ip6_output will override ip6->ip6_hlim */
1411 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1412 im6o.im6o_multicast_loop = 1;
1413 error = ip6_output(mb_copy, NULL, &ro,
1414 IPV6_FORWARDING, &im6o, NULL, NULL);
1416 #ifdef MRT6DEBUG
1417 if (mrt6debug & DEBUG_XMIT)
1418 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1419 mifp - mif6table, error);
1420 #endif
1421 crit_exit();
1422 return;
1426 * If we belong to the destination multicast group
1427 * on the outgoing interface, loop back a copy.
1429 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1430 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1431 if (in6m != NULL) {
1432 dst6->sin6_len = sizeof(struct sockaddr_in6);
1433 dst6->sin6_family = AF_INET6;
1434 dst6->sin6_addr = ip6->ip6_dst;
1435 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1438 * Put the packet into the sending queue of the outgoing interface
1439 * if it would fit in the MTU of the interface.
1441 if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1442 dst6->sin6_len = sizeof(struct sockaddr_in6);
1443 dst6->sin6_family = AF_INET6;
1444 dst6->sin6_addr = ip6->ip6_dst;
1446 * We just call if_output instead of nd6_output here, since
1447 * we need no ND for a multicast forwarded packet...right?
1449 lwkt_serialize_enter(ifp->if_serializer);
1450 error = (*ifp->if_output)(ifp, mb_copy,
1451 (struct sockaddr *)&ro.ro_dst, NULL);
1452 lwkt_serialize_exit(ifp->if_serializer);
1453 #ifdef MRT6DEBUG
1454 if (mrt6debug & DEBUG_XMIT)
1455 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1456 mifp - mif6table, error);
1457 #endif
1458 } else {
1459 #ifdef MULTICAST_PMTUD
1460 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1461 #else
1462 #ifdef MRT6DEBUG
1463 if (mrt6debug & DEBUG_XMIT)
1464 log(LOG_DEBUG,
1465 "phyint_send: packet too big on %s o %s g %s"
1466 " size %d(discarded)\n",
1467 if_name(ifp),
1468 ip6_sprintf(&ip6->ip6_src),
1469 ip6_sprintf(&ip6->ip6_dst),
1470 mb_copy->m_pkthdr.len);
1471 #endif /* MRT6DEBUG */
1472 m_freem(mb_copy); /* simply discard the packet */
1473 #endif
1476 crit_exit();
1479 static int
1480 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1482 struct mbuf *mm;
1483 int i, len = m->m_pkthdr.len;
1484 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1485 struct mrt6msg *im6;
1487 #ifdef MRT6DEBUG
1488 if (mrt6debug)
1489 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1490 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1491 #endif
1492 ++pim6stat.pim6s_snd_registers;
1494 /* Make a copy of the packet to send to the user level process */
1495 MGETHDR(mm, MB_DONTWAIT, MT_HEADER);
1496 if (mm == NULL)
1497 return ENOBUFS;
1498 mm->m_pkthdr.rcvif = NULL;
1499 mm->m_data += max_linkhdr;
1500 mm->m_len = sizeof(struct ip6_hdr);
1502 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1503 m_freem(mm);
1504 return ENOBUFS;
1506 i = MHLEN - M_LEADINGSPACE(mm);
1507 if (i > len)
1508 i = len;
1509 mm = m_pullup(mm, i);
1510 if (mm == NULL)
1511 return ENOBUFS;
1512 /* TODO: check it! */
1513 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1516 * Send message to routing daemon
1518 sin6.sin6_addr = ip6->ip6_src;
1520 im6 = mtod(mm, struct mrt6msg *);
1521 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1522 im6->im6_mbz = 0;
1524 im6->im6_mif = mif - mif6table;
1526 /* iif info is not given for reg. encap.n */
1527 mrt6stat.mrt6s_upcalls++;
1529 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1530 #ifdef MRT6DEBUG
1531 if (mrt6debug)
1532 log(LOG_WARNING,
1533 "register_send: ip6_mrouter socket queue full\n");
1534 #endif
1535 ++mrt6stat.mrt6s_upq_sockfull;
1536 return ENOBUFS;
1538 return 0;
1542 * PIM sparse mode hook
1543 * Receives the pim control messages, and passes them up to the listening
1544 * socket, using rip6_input.
1545 * The only message processed is the REGISTER pim message; the pim header
1546 * is stripped off, and the inner packet is passed to register_mforward.
1549 pim6_input(struct mbuf **mp, int *offp, int proto)
1551 struct pim *pim; /* pointer to a pim struct */
1552 struct ip6_hdr *ip6;
1553 int pimlen;
1554 struct mbuf *m = *mp;
1555 int minlen;
1556 int off = *offp;
1558 ++pim6stat.pim6s_rcv_total;
1560 ip6 = mtod(m, struct ip6_hdr *);
1561 pimlen = m->m_pkthdr.len - *offp;
1564 * Validate lengths
1566 if (pimlen < PIM_MINLEN) {
1567 ++pim6stat.pim6s_rcv_tooshort;
1568 #ifdef MRT6DEBUG
1569 if (mrt6debug & DEBUG_PIM)
1570 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1571 #endif
1572 m_freem(m);
1573 return (IPPROTO_DONE);
1577 * if the packet is at least as big as a REGISTER, go ahead
1578 * and grab the PIM REGISTER header size, to avoid another
1579 * possible m_pullup() later.
1581 * PIM_MINLEN == pimhdr + u_int32 == 8
1582 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1584 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1587 * Make sure that the IP6 and PIM headers in contiguous memory, and
1588 * possibly the PIM REGISTER header
1590 #ifndef PULLDOWN_TEST
1591 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1592 /* adjust pointer */
1593 ip6 = mtod(m, struct ip6_hdr *);
1595 /* adjust mbuf to point to the PIM header */
1596 pim = (struct pim *)((caddr_t)ip6 + off);
1597 #else
1598 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1599 if (pim == NULL) {
1600 pim6stat.pim6s_rcv_tooshort++;
1601 return IPPROTO_DONE;
1603 #endif
1605 #define PIM6_CHECKSUM
1606 #ifdef PIM6_CHECKSUM
1608 int cksumlen;
1611 * Validate checksum.
1612 * If PIM REGISTER, exclude the data packet
1614 if (pim->pim_type == PIM_REGISTER)
1615 cksumlen = PIM_MINLEN;
1616 else
1617 cksumlen = pimlen;
1619 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1620 ++pim6stat.pim6s_rcv_badsum;
1621 #ifdef MRT6DEBUG
1622 if (mrt6debug & DEBUG_PIM)
1623 log(LOG_DEBUG,
1624 "pim6_input: invalid checksum\n");
1625 #endif
1626 m_freem(m);
1627 return (IPPROTO_DONE);
1630 #endif /* PIM_CHECKSUM */
1632 /* PIM version check */
1633 if (pim->pim_ver != PIM_VERSION) {
1634 ++pim6stat.pim6s_rcv_badversion;
1635 #ifdef MRT6DEBUG
1636 log(LOG_ERR,
1637 "pim6_input: incorrect version %d, expecting %d\n",
1638 pim->pim_ver, PIM_VERSION);
1639 #endif
1640 m_freem(m);
1641 return (IPPROTO_DONE);
1644 if (pim->pim_type == PIM_REGISTER) {
1646 * since this is a REGISTER, we'll make a copy of the register
1647 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1648 * routing daemon.
1650 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1652 struct mbuf *mcp;
1653 struct ip6_hdr *eip6;
1654 u_int32_t *reghdr;
1655 int rc;
1657 ++pim6stat.pim6s_rcv_registers;
1659 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1660 #ifdef MRT6DEBUG
1661 if (mrt6debug & DEBUG_PIM)
1662 log(LOG_DEBUG,
1663 "pim6_input: register mif not set: %d\n",
1664 reg_mif_num);
1665 #endif
1666 m_freem(m);
1667 return (IPPROTO_DONE);
1670 reghdr = (u_int32_t *)(pim + 1);
1672 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1673 goto pim6_input_to_daemon;
1676 * Validate length
1678 if (pimlen < PIM6_REG_MINLEN) {
1679 ++pim6stat.pim6s_rcv_tooshort;
1680 ++pim6stat.pim6s_rcv_badregisters;
1681 #ifdef MRT6DEBUG
1682 log(LOG_ERR,
1683 "pim6_input: register packet size too "
1684 "small %d from %s\n",
1685 pimlen, ip6_sprintf(&ip6->ip6_src));
1686 #endif
1687 m_freem(m);
1688 return (IPPROTO_DONE);
1691 eip6 = (struct ip6_hdr *) (reghdr + 1);
1692 #ifdef MRT6DEBUG
1693 if (mrt6debug & DEBUG_PIM)
1694 log(LOG_DEBUG,
1695 "pim6_input[register], eip6: %s -> %s, "
1696 "eip6 plen %d\n",
1697 ip6_sprintf(&eip6->ip6_src),
1698 ip6_sprintf(&eip6->ip6_dst),
1699 ntohs(eip6->ip6_plen));
1700 #endif
1702 /* verify the version number of the inner packet */
1703 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1704 ++pim6stat.pim6s_rcv_badregisters;
1705 #ifdef MRT6DEBUG
1706 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1707 "of the inner packet\n",
1708 (eip6->ip6_vfc & IPV6_VERSION));
1709 #endif
1710 m_freem(m);
1711 return (IPPROTO_NONE);
1714 /* verify the inner packet is destined to a mcast group */
1715 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1716 ++pim6stat.pim6s_rcv_badregisters;
1717 #ifdef MRT6DEBUG
1718 if (mrt6debug & DEBUG_PIM)
1719 log(LOG_DEBUG,
1720 "pim6_input: inner packet of register "
1721 "is not multicast %s\n",
1722 ip6_sprintf(&eip6->ip6_dst));
1723 #endif
1724 m_freem(m);
1725 return (IPPROTO_DONE);
1729 * make a copy of the whole header to pass to the daemon later.
1731 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1732 if (mcp == NULL) {
1733 #ifdef MRT6DEBUG
1734 log(LOG_ERR,
1735 "pim6_input: pim register: "
1736 "could not copy register head\n");
1737 #endif
1738 m_freem(m);
1739 return (IPPROTO_DONE);
1743 * forward the inner ip6 packet; point m_data at the inner ip6.
1745 m_adj(m, off + PIM_MINLEN);
1746 #ifdef MRT6DEBUG
1747 if (mrt6debug & DEBUG_PIM) {
1748 log(LOG_DEBUG,
1749 "pim6_input: forwarding decapsulated register: "
1750 "src %s, dst %s, mif %d\n",
1751 ip6_sprintf(&eip6->ip6_src),
1752 ip6_sprintf(&eip6->ip6_dst),
1753 reg_mif_num);
1755 #endif
1757 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1758 dst.sin6_family, NULL);
1760 /* prepare the register head to send to the mrouting daemon */
1761 m = mcp;
1765 * Pass the PIM message up to the daemon; if it is a register message
1766 * pass the 'head' only up to the daemon. This includes the
1767 * encapsulator ip6 header, pim header, register header and the
1768 * encapsulated ip6 header.
1770 pim6_input_to_daemon:
1771 rip6_input(&m, offp, proto);
1772 return (IPPROTO_DONE);