]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ip6_mroute.c
This commit was generated by cvs2svn to compensate for changes in r155429,
[FreeBSD/FreeBSD.git] / sys / netinet6 / ip6_mroute.c
1 /*      $FreeBSD$       */
2 /*      $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $     */
3
4 /*-
5  * Copyright (C) 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*      BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp     */
34
35 /*-
36  * Copyright (c) 1989 Stephen Deering
37  * Copyright (c) 1992, 1993
38  *      The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Stephen Deering of Stanford University.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
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  * 4. 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.
54  *
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
65  * SUCH DAMAGE.
66  *
67  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
68  */
69
70 /*
71  * IP multicast forwarding procedures
72  *
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
79  *
80  * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
81  */
82
83 #include "opt_inet.h"
84 #include "opt_inet6.h"
85
86 #include <sys/param.h>
87 #include <sys/callout.h>
88 #include <sys/errno.h>
89 #include <sys/kernel.h>
90 #include <sys/lock.h>
91 #include <sys/malloc.h>
92 #include <sys/mbuf.h>
93 #include <sys/protosw.h>
94 #include <sys/signalvar.h>
95 #include <sys/socket.h>
96 #include <sys/socketvar.h>
97 #include <sys/sockio.h>
98 #include <sys/sx.h>
99 #include <sys/syslog.h>
100 #include <sys/systm.h>
101 #include <sys/time.h>
102
103 #include <net/if.h>
104 #include <net/if_types.h>
105 #include <net/raw_cb.h>
106 #include <net/route.h>
107
108 #include <netinet/in.h>
109 #include <netinet/in_var.h>
110 #include <netinet/icmp6.h>
111
112 #include <netinet/ip6.h>
113 #include <netinet6/ip6_var.h>
114 #include <netinet6/scope6_var.h>
115 #include <netinet6/nd6.h>
116 #include <netinet6/ip6_mroute.h>
117 #include <netinet6/pim6.h>
118 #include <netinet6/pim6_var.h>
119
120 #include <net/net_osdep.h>
121
122 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
123
124 #define M_HASCL(m) ((m)->m_flags & M_EXT)
125
126 static int ip6_mdq __P((struct mbuf *, struct ifnet *, struct mf6c *));
127 static void phyint_send __P((struct ip6_hdr *, struct mif6 *, struct mbuf *));
128
129 static int set_pim6 __P((int *));
130 static int socket_send __P((struct socket *, struct mbuf *,
131             struct sockaddr_in6 *));
132 static int register_send __P((struct ip6_hdr *, struct mif6 *,
133             struct mbuf *));
134
135 /*
136  * Globals.  All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
137  * except for netstat or debugging purposes.
138  */
139 struct socket  *ip6_mrouter = NULL;
140 int             ip6_mrouter_ver = 0;
141 int             ip6_mrtproto = IPPROTO_PIM;    /* for netstat only */
142 struct mrt6stat mrt6stat;
143
144 #define NO_RTE_FOUND    0x1
145 #define RTE_FOUND       0x2
146
147 struct mf6c     *mf6ctable[MF6CTBLSIZ];
148 u_char          n6expire[MF6CTBLSIZ];
149 static struct mif6 mif6table[MAXMIFS];
150 #ifdef MRT6DEBUG
151 u_int           mrt6debug = 0;    /* debug level        */
152 #define DEBUG_MFC       0x02
153 #define DEBUG_FORWARD   0x04
154 #define DEBUG_EXPIRE    0x08
155 #define DEBUG_XMIT      0x10
156 #define DEBUG_REG       0x20
157 #define DEBUG_PIM       0x40
158 #endif
159
160 static void     expire_upcalls __P((void *));
161 #define EXPIRE_TIMEOUT  (hz / 4)        /* 4x / second */
162 #define UPCALL_EXPIRE   6               /* number of timeouts */
163
164 #ifdef INET
165 #ifdef MROUTING
166 extern struct socket *ip_mrouter;
167 #endif
168 #endif
169
170 /*
171  * 'Interfaces' associated with decapsulator (so we can tell
172  * packets that went through it from ones that get reflected
173  * by a broken gateway).  Different from IPv4 register_if,
174  * these interfaces are linked into the system ifnet list,
175  * because per-interface IPv6 statistics are maintained in
176  * ifp->if_afdata.  But it does not have any routes point 
177  * to them.  I.e., packets can't be sent this way.  They
178  * only exist as a placeholder for multicast source 
179  * verification.
180  */
181 static struct ifnet *multicast_register_if6;
182
183 #define ENCAP_HOPS 64
184
185 /*
186  * Private variables.
187  */
188 static mifi_t nummifs = 0;
189 static mifi_t reg_mif_num = (mifi_t)-1;
190
191 static struct pim6stat pim6stat;
192 static int pim6;
193
194 /*
195  * Hash function for a source, group entry
196  */
197 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
198                                    (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
199                                    (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
200                                    (g).s6_addr32[2] ^ (g).s6_addr32[3])
201
202 /*
203  * Find a route for a given origin IPv6 address and Multicast group address.
204  * Quality of service parameter to be added in the future!!!
205  */
206
207 #define MF6CFIND(o, g, rt) do { \
208         struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
209         rt = NULL; \
210         mrt6stat.mrt6s_mfc_lookups++; \
211         while (_rt) { \
212                 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
213                     IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
214                     (_rt->mf6c_stall == NULL)) { \
215                         rt = _rt; \
216                         break; \
217                 } \
218                 _rt = _rt->mf6c_next; \
219         } \
220         if (rt == NULL) { \
221                 mrt6stat.mrt6s_mfc_misses++; \
222         } \
223 } while (/*CONSTCOND*/ 0)
224
225 /*
226  * Macros to compute elapsed time efficiently
227  * Borrowed from Van Jacobson's scheduling code
228  */
229 #define TV_DELTA(a, b, delta) do { \
230             int xxs; \
231                 \
232             delta = (a).tv_usec - (b).tv_usec; \
233             if ((xxs = (a).tv_sec - (b).tv_sec)) { \
234                switch (xxs) { \
235                       case 2: \
236                           delta += 1000000; \
237                               /* FALLTHROUGH */ \
238                       case 1: \
239                           delta += 1000000; \
240                           break; \
241                       default: \
242                           delta += (1000000 * xxs); \
243                } \
244             } \
245 } while (/*CONSTCOND*/ 0)
246
247 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
248               (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
249
250 #ifdef UPCALL_TIMING
251 #define UPCALL_MAX      50
252 u_long upcall_data[UPCALL_MAX + 1];
253 static void collate();
254 #endif /* UPCALL_TIMING */
255
256 static int get_sg_cnt __P((struct sioc_sg_req6 *));
257 static int get_mif6_cnt __P((struct sioc_mif_req6 *));
258 static int ip6_mrouter_init __P((struct socket *, int, int));
259 static int add_m6if __P((struct mif6ctl *));
260 static int del_m6if __P((mifi_t *));
261 static int add_m6fc __P((struct mf6cctl *));
262 static int del_m6fc __P((struct mf6cctl *));
263
264 static struct callout expire_upcalls_ch;
265
266 /*
267  * Handle MRT setsockopt commands to modify the multicast routing tables.
268  */
269 int
270 ip6_mrouter_set(so, sopt)
271         struct socket *so;
272         struct sockopt *sopt;
273 {
274         int error = 0;
275         int optval;
276         struct mif6ctl mifc;
277         struct mf6cctl mfcc;
278         mifi_t mifi;
279
280         if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
281                 return (EACCES);
282
283         switch (sopt->sopt_name) {
284         case MRT6_INIT:
285 #ifdef MRT6_OINIT
286         case MRT6_OINIT:
287 #endif
288                 error = sooptcopyin(sopt, &optval, sizeof(optval),
289                     sizeof(optval));
290                 if (error)
291                         break;
292                 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
293                 break;
294         case MRT6_DONE:
295                 error = ip6_mrouter_done();
296                 break;
297         case MRT6_ADD_MIF:
298                 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
299                 if (error)
300                         break;
301                 error = add_m6if(&mifc);
302                 break;
303         case MRT6_ADD_MFC:
304                 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
305                 if (error)
306                         break;
307                 error = add_m6fc(&mfcc);
308                 break;
309         case MRT6_DEL_MFC:
310                 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
311                 if (error)
312                         break;
313                 error = del_m6fc(&mfcc);
314                 break;
315         case MRT6_DEL_MIF:
316                 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
317                 if (error)
318                         break;
319                 error = del_m6if(&mifi);
320                 break;
321         case MRT6_PIM:
322                 error = sooptcopyin(sopt, &optval, sizeof(optval),
323                     sizeof(optval));
324                 if (error)
325                         break;
326                 error = set_pim6(&optval);
327                 break;
328         default:
329                 error = EOPNOTSUPP;
330                 break;
331         }
332
333         return (error);
334 }
335
336 /*
337  * Handle MRT getsockopt commands
338  */
339 int
340 ip6_mrouter_get(so, sopt)
341         struct socket *so;
342         struct sockopt *sopt;
343 {
344         int error = 0;
345
346         if (so != ip6_mrouter)
347                 return (EACCES);
348
349         switch (sopt->sopt_name) {
350                 case MRT6_PIM:
351                         error = sooptcopyout(sopt, &pim6, sizeof(pim6));
352                         break;
353         }
354         return (error);
355 }
356
357 /*
358  * Handle ioctl commands to obtain information from the cache
359  */
360 int
361 mrt6_ioctl(cmd, data)
362         int cmd;
363         caddr_t data;
364 {
365         switch (cmd) {
366         case SIOCGETSGCNT_IN6:
367                 return (get_sg_cnt((struct sioc_sg_req6 *)data));
368         case SIOCGETMIFCNT_IN6:
369                 return (get_mif6_cnt((struct sioc_mif_req6 *)data));
370         default:
371                 return (EINVAL);
372         }
373 }
374
375 /*
376  * returns the packet, byte, rpf-failure count for the source group provided
377  */
378 static int
379 get_sg_cnt(req)
380         struct sioc_sg_req6 *req;
381 {
382         struct mf6c *rt;
383         int s;
384
385         s = splnet();
386         MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
387         splx(s);
388         if (rt != NULL) {
389                 req->pktcnt = rt->mf6c_pkt_cnt;
390                 req->bytecnt = rt->mf6c_byte_cnt;
391                 req->wrong_if = rt->mf6c_wrong_if;
392         } else
393                 return (ESRCH);
394 #if 0
395                 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
396 #endif
397
398         return (0);
399 }
400
401 /*
402  * returns the input and output packet and byte counts on the mif provided
403  */
404 static int
405 get_mif6_cnt(req)
406         struct sioc_mif_req6 *req;
407 {
408         mifi_t mifi = req->mifi;
409
410         if (mifi >= nummifs)
411                 return (EINVAL);
412
413         req->icount = mif6table[mifi].m6_pkt_in;
414         req->ocount = mif6table[mifi].m6_pkt_out;
415         req->ibytes = mif6table[mifi].m6_bytes_in;
416         req->obytes = mif6table[mifi].m6_bytes_out;
417
418         return (0);
419 }
420
421 static int
422 set_pim6(i)
423         int *i;
424 {
425         if ((*i != 1) && (*i != 0))
426                 return (EINVAL);
427
428         pim6 = *i;
429
430         return (0);
431 }
432
433 /*
434  * Enable multicast routing
435  */
436 static int
437 ip6_mrouter_init(so, v, cmd)
438         struct socket *so;
439         int v;
440         int cmd;
441 {
442 #ifdef MRT6DEBUG
443         if (mrt6debug)
444                 log(LOG_DEBUG,
445                     "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
446                     so->so_type, so->so_proto->pr_protocol);
447 #endif
448
449         if (so->so_type != SOCK_RAW ||
450             so->so_proto->pr_protocol != IPPROTO_ICMPV6)
451                 return (EOPNOTSUPP);
452
453         if (v != 1)
454                 return (ENOPROTOOPT);
455
456         if (ip6_mrouter != NULL)
457                 return (EADDRINUSE);
458
459         ip6_mrouter = so;
460         ip6_mrouter_ver = cmd;
461
462         bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
463         bzero((caddr_t)n6expire, sizeof(n6expire));
464
465         pim6 = 0;/* used for stubbing out/in pim stuff */
466
467         callout_init(&expire_upcalls_ch, 0);
468         callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
469             expire_upcalls, NULL);
470
471 #ifdef MRT6DEBUG
472         if (mrt6debug)
473                 log(LOG_DEBUG, "ip6_mrouter_init\n");
474 #endif
475
476         return (0);
477 }
478
479 /*
480  * Disable multicast routing
481  */
482 int
483 ip6_mrouter_done()
484 {
485         mifi_t mifi;
486         int i;
487         struct ifnet *ifp;
488         struct in6_ifreq ifr;
489         struct mf6c *rt;
490         struct rtdetq *rte;
491         int s;
492
493         s = splnet();
494
495         /*
496          * For each phyint in use, disable promiscuous reception of all IPv6
497          * multicasts.
498          */
499 #ifdef INET
500 #ifdef MROUTING
501         /*
502          * If there is still IPv4 multicast routing daemon,
503          * we remain interfaces to receive all muliticasted packets.
504          * XXX: there may be an interface in which the IPv4 multicast
505          * daemon is not interested...
506          */
507         if (!ip_mrouter)
508 #endif
509 #endif
510         {
511                 for (mifi = 0; mifi < nummifs; mifi++) {
512                         if (mif6table[mifi].m6_ifp &&
513                             !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
514                                 ifr.ifr_addr.sin6_family = AF_INET6;
515                                 ifr.ifr_addr.sin6_addr = in6addr_any;
516                                 ifp = mif6table[mifi].m6_ifp;
517                                 (*ifp->if_ioctl)(ifp, SIOCDELMULTI,
518                                                  (caddr_t)&ifr);
519                         }
520                 }
521         }
522 #ifdef notyet
523         bzero((caddr_t)qtable, sizeof(qtable));
524         bzero((caddr_t)tbftable, sizeof(tbftable));
525 #endif
526         bzero((caddr_t)mif6table, sizeof(mif6table));
527         nummifs = 0;
528
529         pim6 = 0; /* used to stub out/in pim specific code */
530
531         callout_stop(&expire_upcalls_ch);
532
533         /*
534          * Free all multicast forwarding cache entries.
535          */
536         for (i = 0; i < MF6CTBLSIZ; i++) {
537                 rt = mf6ctable[i];
538                 while (rt) {
539                         struct mf6c *frt;
540
541                         for (rte = rt->mf6c_stall; rte != NULL; ) {
542                                 struct rtdetq *n = rte->next;
543
544                                 m_free(rte->m);
545                                 free(rte, M_MRTABLE6);
546                                 rte = n;
547                         }
548                         frt = rt;
549                         rt = rt->mf6c_next;
550                         free(frt, M_MRTABLE6);
551                 }
552         }
553
554         bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
555
556         /*
557          * Reset register interface
558          */
559         if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
560                 if_detach(multicast_register_if6);
561                 if_free(multicast_register_if6);
562                 reg_mif_num = (mifi_t)-1;
563                 multicast_register_if6 = NULL;
564         }
565
566         ip6_mrouter = NULL;
567         ip6_mrouter_ver = 0;
568
569         splx(s);
570
571 #ifdef MRT6DEBUG
572         if (mrt6debug)
573                 log(LOG_DEBUG, "ip6_mrouter_done\n");
574 #endif
575
576         return (0);
577 }
578
579 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
580
581 /*
582  * Add a mif to the mif table
583  */
584 static int
585 add_m6if(mifcp)
586         struct mif6ctl *mifcp;
587 {
588         struct mif6 *mifp;
589         struct ifnet *ifp;
590         int error, s;
591 #ifdef notyet
592         struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
593 #endif
594
595         if (mifcp->mif6c_mifi >= MAXMIFS)
596                 return (EINVAL);
597         mifp = mif6table + mifcp->mif6c_mifi;
598         if (mifp->m6_ifp)
599                 return (EADDRINUSE); /* XXX: is it appropriate? */
600         if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
601                 return (ENXIO);
602         ifp = ifnet_byindex(mifcp->mif6c_pifi);
603
604         if (mifcp->mif6c_flags & MIFF_REGISTER) {
605                 if (reg_mif_num == (mifi_t)-1) {
606                         ifp = if_alloc(IFT_OTHER);
607
608                         if_initname(ifp, "register_mif", 0);
609                         ifp->if_flags |= IFF_LOOPBACK;
610                         if_attach(ifp);
611                         multicast_register_if6 = ifp;
612                         reg_mif_num = mifcp->mif6c_mifi;
613                         /* 
614                          * it is impossible to guess the ifindex of the 
615                          * register interface.  So mif6c_pifi is automatically
616                          * calculated.
617                          */
618                         mifcp->mif6c_pifi = ifp->if_index;
619                 } else {
620                         ifp = multicast_register_if6;
621                 }
622
623         } /* if REGISTER */
624         else {
625                 /* Make sure the interface supports multicast */
626                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
627                         return (EOPNOTSUPP);
628
629                 s = splnet();
630                 error = if_allmulti(ifp, 1);
631                 splx(s);
632                 if (error)
633                         return (error);
634         }
635
636         s = splnet();
637         mifp->m6_flags     = mifcp->mif6c_flags;
638         mifp->m6_ifp       = ifp;
639 #ifdef notyet
640         /* scaling up here allows division by 1024 in critical code */
641         mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
642 #endif
643         /* initialize per mif pkt counters */
644         mifp->m6_pkt_in    = 0;
645         mifp->m6_pkt_out   = 0;
646         mifp->m6_bytes_in  = 0;
647         mifp->m6_bytes_out = 0;
648         splx(s);
649
650         /* Adjust nummifs up if the mifi is higher than nummifs */
651         if (nummifs <= mifcp->mif6c_mifi)
652                 nummifs = mifcp->mif6c_mifi + 1;
653
654 #ifdef MRT6DEBUG
655         if (mrt6debug)
656                 log(LOG_DEBUG,
657                     "add_mif #%d, phyint %s\n",
658                     mifcp->mif6c_mifi,
659                     ifp->if_xname);
660 #endif
661
662         return (0);
663 }
664
665 /*
666  * Delete a mif from the mif table
667  */
668 static int
669 del_m6if(mifip)
670         mifi_t *mifip;
671 {
672         struct mif6 *mifp = mif6table + *mifip;
673         mifi_t mifi;
674         struct ifnet *ifp;
675         int s;
676
677         if (*mifip >= nummifs)
678                 return (EINVAL);
679         if (mifp->m6_ifp == NULL)
680                 return (EINVAL);
681
682         s = splnet();
683
684         if (!(mifp->m6_flags & MIFF_REGISTER)) {
685                 /*
686                  * XXX: what if there is yet IPv4 multicast daemon
687                  *      using the interface?
688                  */
689                 ifp = mifp->m6_ifp;
690
691                 if_allmulti(ifp, 0);
692         } else {
693                 if (reg_mif_num != (mifi_t)-1 &&
694                     multicast_register_if6 != NULL) {
695                         if_detach(multicast_register_if6);
696                         if_free(multicast_register_if6);
697                         reg_mif_num = (mifi_t)-1;
698                         multicast_register_if6 = NULL;
699                 }
700         }
701
702 #ifdef notyet
703         bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
704         bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
705 #endif
706         bzero((caddr_t)mifp, sizeof(*mifp));
707
708         /* Adjust nummifs down */
709         for (mifi = nummifs; mifi > 0; mifi--)
710                 if (mif6table[mifi - 1].m6_ifp)
711                         break;
712         nummifs = mifi;
713
714         splx(s);
715
716 #ifdef MRT6DEBUG
717         if (mrt6debug)
718                 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
719 #endif
720
721         return (0);
722 }
723
724 /*
725  * Add an mfc entry
726  */
727 static int
728 add_m6fc(mfccp)
729         struct mf6cctl *mfccp;
730 {
731         struct mf6c *rt;
732         u_long hash;
733         struct rtdetq *rte;
734         u_short nstl;
735         int s;
736
737         MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
738                  mfccp->mf6cc_mcastgrp.sin6_addr, rt);
739
740         /* If an entry already exists, just update the fields */
741         if (rt) {
742 #ifdef MRT6DEBUG
743                 if (mrt6debug & DEBUG_MFC)
744                         log(LOG_DEBUG,
745                             "add_m6fc no upcall h %d o %s g %s p %x\n",
746                             ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
747                             ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
748                             mfccp->mf6cc_parent);
749 #endif
750
751                 s = splnet();
752                 rt->mf6c_parent = mfccp->mf6cc_parent;
753                 rt->mf6c_ifset = mfccp->mf6cc_ifset;
754                 splx(s);
755                 return (0);
756         }
757
758         /*
759          * Find the entry for which the upcall was made and update
760          */
761         s = splnet();
762         hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
763                         mfccp->mf6cc_mcastgrp.sin6_addr);
764         for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
765                 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
766                                        &mfccp->mf6cc_origin.sin6_addr) &&
767                     IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
768                                        &mfccp->mf6cc_mcastgrp.sin6_addr) &&
769                     (rt->mf6c_stall != NULL)) {
770
771                         if (nstl++)
772                                 log(LOG_ERR,
773                                     "add_m6fc: %s o %s g %s p %x dbx %p\n",
774                                     "multiple kernel entries",
775                                     ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
776                                     ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
777                                     mfccp->mf6cc_parent, rt->mf6c_stall);
778
779 #ifdef MRT6DEBUG
780                         if (mrt6debug & DEBUG_MFC)
781                                 log(LOG_DEBUG,
782                                     "add_m6fc o %s g %s p %x dbg %x\n",
783                                     ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
784                                     ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
785                                     mfccp->mf6cc_parent, rt->mf6c_stall);
786 #endif
787
788                         rt->mf6c_origin     = mfccp->mf6cc_origin;
789                         rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
790                         rt->mf6c_parent     = mfccp->mf6cc_parent;
791                         rt->mf6c_ifset      = mfccp->mf6cc_ifset;
792                         /* initialize pkt counters per src-grp */
793                         rt->mf6c_pkt_cnt    = 0;
794                         rt->mf6c_byte_cnt   = 0;
795                         rt->mf6c_wrong_if   = 0;
796
797                         rt->mf6c_expire = 0;    /* Don't clean this guy up */
798                         n6expire[hash]--;
799
800                         /* free packets Qed at the end of this entry */
801                         for (rte = rt->mf6c_stall; rte != NULL; ) {
802                                 struct rtdetq *n = rte->next;
803                                 ip6_mdq(rte->m, rte->ifp, rt);
804                                 m_freem(rte->m);
805 #ifdef UPCALL_TIMING
806                                 collate(&(rte->t));
807 #endif /* UPCALL_TIMING */
808                                 free(rte, M_MRTABLE6);
809                                 rte = n;
810                         }
811                         rt->mf6c_stall = NULL;
812                 }
813         }
814
815         /*
816          * It is possible that an entry is being inserted without an upcall
817          */
818         if (nstl == 0) {
819 #ifdef MRT6DEBUG
820                 if (mrt6debug & DEBUG_MFC)
821                         log(LOG_DEBUG,
822                             "add_mfc no upcall h %d o %s g %s p %x\n",
823                             hash,
824                             ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
825                             ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
826                             mfccp->mf6cc_parent);
827 #endif
828
829                 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
830
831                         if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
832                                                &mfccp->mf6cc_origin.sin6_addr)&&
833                             IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
834                                                &mfccp->mf6cc_mcastgrp.sin6_addr)) {
835
836                                 rt->mf6c_origin     = mfccp->mf6cc_origin;
837                                 rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
838                                 rt->mf6c_parent     = mfccp->mf6cc_parent;
839                                 rt->mf6c_ifset      = mfccp->mf6cc_ifset;
840                                 /* initialize pkt counters per src-grp */
841                                 rt->mf6c_pkt_cnt    = 0;
842                                 rt->mf6c_byte_cnt   = 0;
843                                 rt->mf6c_wrong_if   = 0;
844
845                                 if (rt->mf6c_expire)
846                                         n6expire[hash]--;
847                                 rt->mf6c_expire    = 0;
848                         }
849                 }
850                 if (rt == NULL) {
851                         /* no upcall, so make a new entry */
852                         rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
853                                                   M_NOWAIT);
854                         if (rt == NULL) {
855                                 splx(s);
856                                 return (ENOBUFS);
857                         }
858
859                         /* insert new entry at head of hash chain */
860                         rt->mf6c_origin     = mfccp->mf6cc_origin;
861                         rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
862                         rt->mf6c_parent     = mfccp->mf6cc_parent;
863                         rt->mf6c_ifset      = mfccp->mf6cc_ifset;
864                         /* initialize pkt counters per src-grp */
865                         rt->mf6c_pkt_cnt    = 0;
866                         rt->mf6c_byte_cnt   = 0;
867                         rt->mf6c_wrong_if   = 0;
868                         rt->mf6c_expire     = 0;
869                         rt->mf6c_stall = NULL;
870
871                         /* link into table */
872                         rt->mf6c_next  = mf6ctable[hash];
873                         mf6ctable[hash] = rt;
874                 }
875         }
876         splx(s);
877         return (0);
878 }
879
880 #ifdef UPCALL_TIMING
881 /*
882  * collect delay statistics on the upcalls
883  */
884 static void
885 collate(t)
886         struct timeval *t;
887 {
888         u_long d;
889         struct timeval tp;
890         u_long delta;
891
892         GET_TIME(tp);
893
894         if (TV_LT(*t, tp))
895         {
896                 TV_DELTA(tp, *t, delta);
897
898                 d = delta >> 10;
899                 if (d > UPCALL_MAX)
900                         d = UPCALL_MAX;
901
902                 ++upcall_data[d];
903         }
904 }
905 #endif /* UPCALL_TIMING */
906
907 /*
908  * Delete an mfc entry
909  */
910 static int
911 del_m6fc(mfccp)
912         struct mf6cctl *mfccp;
913 {
914         struct sockaddr_in6     origin;
915         struct sockaddr_in6     mcastgrp;
916         struct mf6c             *rt;
917         struct mf6c             **nptr;
918         u_long          hash;
919         int s;
920
921         origin = mfccp->mf6cc_origin;
922         mcastgrp = mfccp->mf6cc_mcastgrp;
923         hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
924
925 #ifdef MRT6DEBUG
926         if (mrt6debug & DEBUG_MFC)
927                 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
928                     ip6_sprintf(&origin.sin6_addr),
929                     ip6_sprintf(&mcastgrp.sin6_addr));
930 #endif
931
932         s = splnet();
933
934         nptr = &mf6ctable[hash];
935         while ((rt = *nptr) != NULL) {
936                 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
937                                        &rt->mf6c_origin.sin6_addr) &&
938                     IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
939                                        &rt->mf6c_mcastgrp.sin6_addr) &&
940                     rt->mf6c_stall == NULL)
941                         break;
942
943                 nptr = &rt->mf6c_next;
944         }
945         if (rt == NULL) {
946                 splx(s);
947                 return (EADDRNOTAVAIL);
948         }
949
950         *nptr = rt->mf6c_next;
951         free(rt, M_MRTABLE6);
952
953         splx(s);
954
955         return (0);
956 }
957
958 static int
959 socket_send(s, mm, src)
960         struct socket *s;
961         struct mbuf *mm;
962         struct sockaddr_in6 *src;
963 {
964         if (s) {
965                 if (sbappendaddr(&s->so_rcv,
966                                  (struct sockaddr *)src,
967                                  mm, (struct mbuf *)0) != 0) {
968                         sorwakeup(s);
969                         return (0);
970                 }
971         }
972         m_freem(mm);
973         return (-1);
974 }
975
976 /*
977  * IPv6 multicast forwarding function. This function assumes that the packet
978  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
979  * pointed to by "ifp", and the packet is to be relayed to other networks
980  * that have members of the packet's destination IPv6 multicast group.
981  *
982  * The packet is returned unscathed to the caller, unless it is
983  * erroneous, in which case a non-zero return value tells the caller to
984  * discard it.
985  *
986  * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
987  * this function is called in the originating context (i.e., not when
988  * forwarding a packet from other node).  ip6_output(), which is currently the
989  * only function that calls this function is called in the originating context,
990  * explicitly ensures this condition.  It is caller's responsibility to ensure
991  * that if this function is called from somewhere else in the originating
992  * context in the future.
993  */
994
995 int
996 ip6_mforward(ip6, ifp, m)
997         struct ip6_hdr *ip6;
998         struct ifnet *ifp;
999         struct mbuf *m;
1000 {
1001         struct mf6c *rt;
1002         struct mif6 *mifp;
1003         struct mbuf *mm;
1004         int s;
1005         mifi_t mifi;
1006
1007 #ifdef MRT6DEBUG
1008         if (mrt6debug & DEBUG_FORWARD)
1009                 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1010                     ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
1011                     ifp->if_index);
1012 #endif
1013
1014         /*
1015          * Don't forward a packet with Hop limit of zero or one,
1016          * or a packet destined to a local-only group.
1017          */
1018         if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1019             IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1020                 return (0);
1021         ip6->ip6_hlim--;
1022
1023         /*
1024          * Source address check: do not forward packets with unspecified
1025          * source. It was discussed in July 2000, on ipngwg mailing list.
1026          * This is rather more serious than unicast cases, because some
1027          * MLD packets can be sent with the unspecified source address
1028          * (although such packets must normally set 1 to the hop limit field).
1029          */
1030         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1031                 ip6stat.ip6s_cantforward++;
1032                 if (ip6_log_time + ip6_log_interval < time_second) {
1033                         ip6_log_time = time_second;
1034                         log(LOG_DEBUG,
1035                             "cannot forward "
1036                             "from %s to %s nxt %d received on %s\n",
1037                             ip6_sprintf(&ip6->ip6_src),
1038                             ip6_sprintf(&ip6->ip6_dst),
1039                             ip6->ip6_nxt,
1040                             if_name(m->m_pkthdr.rcvif));
1041                 }
1042                 return (0);
1043         }
1044
1045         /*
1046          * Determine forwarding mifs from the forwarding cache table
1047          */
1048         s = splnet();
1049         MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1050
1051         /* Entry exists, so forward if necessary */
1052         if (rt) {
1053                 splx(s);
1054                 return (ip6_mdq(m, ifp, rt));
1055         } else {
1056                 /*
1057                  * If we don't have a route for packet's origin,
1058                  * Make a copy of the packet &
1059                  * send message to routing daemon
1060                  */
1061
1062                 struct mbuf *mb0;
1063                 struct rtdetq *rte;
1064                 u_long hash;
1065 /*              int i, npkts;*/
1066 #ifdef UPCALL_TIMING
1067                 struct timeval tp;
1068
1069                 GET_TIME(tp);
1070 #endif /* UPCALL_TIMING */
1071
1072                 mrt6stat.mrt6s_no_route++;
1073 #ifdef MRT6DEBUG
1074                 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1075                         log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1076                             ip6_sprintf(&ip6->ip6_src),
1077                             ip6_sprintf(&ip6->ip6_dst));
1078 #endif
1079
1080                 /*
1081                  * Allocate mbufs early so that we don't do extra work if we
1082                  * are just going to fail anyway.
1083                  */
1084                 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
1085                                               M_NOWAIT);
1086                 if (rte == NULL) {
1087                         splx(s);
1088                         return (ENOBUFS);
1089                 }
1090                 mb0 = m_copy(m, 0, M_COPYALL);
1091                 /*
1092                  * Pullup packet header if needed before storing it,
1093                  * as other references may modify it in the meantime.
1094                  */
1095                 if (mb0 &&
1096                     (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1097                         mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1098                 if (mb0 == NULL) {
1099                         free(rte, M_MRTABLE6);
1100                         splx(s);
1101                         return (ENOBUFS);
1102                 }
1103
1104                 /* is there an upcall waiting for this packet? */
1105                 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1106                 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1107                         if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1108                                                &rt->mf6c_origin.sin6_addr) &&
1109                             IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1110                                                &rt->mf6c_mcastgrp.sin6_addr) &&
1111                             (rt->mf6c_stall != NULL))
1112                                 break;
1113                 }
1114
1115                 if (rt == NULL) {
1116                         struct mrt6msg *im;
1117 #ifdef MRT6_OINIT
1118                         struct omrt6msg *oim;
1119 #endif
1120
1121                         /* no upcall, so make a new entry */
1122                         rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
1123                                                   M_NOWAIT);
1124                         if (rt == NULL) {
1125                                 free(rte, M_MRTABLE6);
1126                                 m_freem(mb0);
1127                                 splx(s);
1128                                 return (ENOBUFS);
1129                         }
1130                         /*
1131                          * Make a copy of the header to send to the user
1132                          * level process
1133                          */
1134                         mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1135
1136                         if (mm == NULL) {
1137                                 free(rte, M_MRTABLE6);
1138                                 m_freem(mb0);
1139                                 free(rt, M_MRTABLE6);
1140                                 splx(s);
1141                                 return (ENOBUFS);
1142                         }
1143
1144                         /*
1145                          * Send message to routing daemon
1146                          */
1147                         sin6.sin6_addr = ip6->ip6_src;
1148
1149                         im = NULL;
1150 #ifdef MRT6_OINIT
1151                         oim = NULL;
1152 #endif
1153                         switch (ip6_mrouter_ver) {
1154 #ifdef MRT6_OINIT
1155                         case MRT6_OINIT:
1156                                 oim = mtod(mm, struct omrt6msg *);
1157                                 oim->im6_msgtype = MRT6MSG_NOCACHE;
1158                                 oim->im6_mbz = 0;
1159                                 break;
1160 #endif
1161                         case MRT6_INIT:
1162                                 im = mtod(mm, struct mrt6msg *);
1163                                 im->im6_msgtype = MRT6MSG_NOCACHE;
1164                                 im->im6_mbz = 0;
1165                                 break;
1166                         default:
1167                                 free(rte, M_MRTABLE6);
1168                                 m_freem(mb0);
1169                                 free(rt, M_MRTABLE6);
1170                                 splx(s);
1171                                 return (EINVAL);
1172                         }
1173
1174 #ifdef MRT6DEBUG
1175                         if (mrt6debug & DEBUG_FORWARD)
1176                                 log(LOG_DEBUG,
1177                                     "getting the iif info in the kernel\n");
1178 #endif
1179
1180                         for (mifp = mif6table, mifi = 0;
1181                              mifi < nummifs && mifp->m6_ifp != ifp;
1182                              mifp++, mifi++)
1183                                 ;
1184
1185                         switch (ip6_mrouter_ver) {
1186 #ifdef MRT6_OINIT
1187                         case MRT6_OINIT:
1188                                 oim->im6_mif = mifi;
1189                                 break;
1190 #endif
1191                         case MRT6_INIT:
1192                                 im->im6_mif = mifi;
1193                                 break;
1194                         }
1195
1196                         if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1197                                 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1198                                     "socket queue full\n");
1199                                 mrt6stat.mrt6s_upq_sockfull++;
1200                                 free(rte, M_MRTABLE6);
1201                                 m_freem(mb0);
1202                                 free(rt, M_MRTABLE6);
1203                                 splx(s);
1204                                 return (ENOBUFS);
1205                         }
1206
1207                         mrt6stat.mrt6s_upcalls++;
1208
1209                         /* insert new entry at head of hash chain */
1210                         bzero(rt, sizeof(*rt));
1211                         rt->mf6c_origin.sin6_family = AF_INET6;
1212                         rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1213                         rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1214                         rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1215                         rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1216                         rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1217                         rt->mf6c_expire = UPCALL_EXPIRE;
1218                         n6expire[hash]++;
1219                         rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1220
1221                         /* link into table */
1222                         rt->mf6c_next  = mf6ctable[hash];
1223                         mf6ctable[hash] = rt;
1224                         /* Add this entry to the end of the queue */
1225                         rt->mf6c_stall = rte;
1226                 } else {
1227                         /* determine if q has overflowed */
1228                         struct rtdetq **p;
1229                         int npkts = 0;
1230
1231                         for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1232                                 if (++npkts > MAX_UPQ6) {
1233                                         mrt6stat.mrt6s_upq_ovflw++;
1234                                         free(rte, M_MRTABLE6);
1235                                         m_freem(mb0);
1236                                         splx(s);
1237                                         return (0);
1238                                 }
1239
1240                         /* Add this entry to the end of the queue */
1241                         *p = rte;
1242                 }
1243
1244                 rte->next = NULL;
1245                 rte->m = mb0;
1246                 rte->ifp = ifp;
1247 #ifdef UPCALL_TIMING
1248                 rte->t = tp;
1249 #endif /* UPCALL_TIMING */
1250
1251                 splx(s);
1252
1253                 return (0);
1254         }
1255 }
1256
1257 /*
1258  * Clean up cache entries if upcalls are not serviced
1259  * Call from the Slow Timeout mechanism, every half second.
1260  */
1261 static void
1262 expire_upcalls(unused)
1263         void *unused;
1264 {
1265         struct rtdetq *rte;
1266         struct mf6c *mfc, **nptr;
1267         int i;
1268         int s;
1269
1270         s = splnet();
1271         for (i = 0; i < MF6CTBLSIZ; i++) {
1272                 if (n6expire[i] == 0)
1273                         continue;
1274                 nptr = &mf6ctable[i];
1275                 while ((mfc = *nptr) != NULL) {
1276                         rte = mfc->mf6c_stall;
1277                         /*
1278                          * Skip real cache entries
1279                          * Make sure it wasn't marked to not expire (shouldn't happen)
1280                          * If it expires now
1281                          */
1282                         if (rte != NULL &&
1283                             mfc->mf6c_expire != 0 &&
1284                             --mfc->mf6c_expire == 0) {
1285 #ifdef MRT6DEBUG
1286                                 if (mrt6debug & DEBUG_EXPIRE)
1287                                         log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1288                                             ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1289                                             ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1290 #endif
1291                                 /*
1292                                  * drop all the packets
1293                                  * free the mbuf with the pkt, if, timing info
1294                                  */
1295                                 do {
1296                                         struct rtdetq *n = rte->next;
1297                                         m_freem(rte->m);
1298                                         free(rte, M_MRTABLE6);
1299                                         rte = n;
1300                                 } while (rte != NULL);
1301                                 mrt6stat.mrt6s_cache_cleanups++;
1302                                 n6expire[i]--;
1303
1304                                 *nptr = mfc->mf6c_next;
1305                                 free(mfc, M_MRTABLE6);
1306                         } else {
1307                                 nptr = &mfc->mf6c_next;
1308                         }
1309                 }
1310         }
1311         splx(s);
1312         callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1313             expire_upcalls, NULL);
1314 }
1315
1316 /*
1317  * Packet forwarding routine once entry in the cache is made
1318  */
1319 static int
1320 ip6_mdq(m, ifp, rt)
1321         struct mbuf *m;
1322         struct ifnet *ifp;
1323         struct mf6c *rt;
1324 {
1325         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1326         mifi_t mifi, iif;
1327         struct mif6 *mifp;
1328         int plen = m->m_pkthdr.len;
1329         struct in6_addr src0, dst0; /* copies for local work */
1330         u_int32_t iszone, idzone, oszone, odzone;
1331         int error = 0;
1332
1333 /*
1334  * Macro to send packet on mif.  Since RSVP packets don't get counted on
1335  * input, they shouldn't get counted on output, so statistics keeping is
1336  * separate.
1337  */
1338
1339 #define MC6_SEND(ip6, mifp, m) do {                             \
1340         if ((mifp)->m6_flags & MIFF_REGISTER)                   \
1341                 register_send((ip6), (mifp), (m));              \
1342         else                                                    \
1343                 phyint_send((ip6), (mifp), (m));                \
1344 } while (/*CONSTCOND*/ 0)
1345
1346         /*
1347          * Don't forward if it didn't arrive from the parent mif
1348          * for its origin.
1349          */
1350         mifi = rt->mf6c_parent;
1351         if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1352                 /* came in the wrong interface */
1353 #ifdef MRT6DEBUG
1354                 if (mrt6debug & DEBUG_FORWARD)
1355                         log(LOG_DEBUG,
1356                             "wrong if: ifid %d mifi %d mififid %x\n",
1357                             ifp->if_index, mifi,
1358                             mif6table[mifi].m6_ifp->if_index);
1359 #endif
1360                 mrt6stat.mrt6s_wrong_if++;
1361                 rt->mf6c_wrong_if++;
1362                 /*
1363                  * If we are doing PIM processing, and we are forwarding
1364                  * packets on this interface, send a message to the
1365                  * routing daemon.
1366                  */
1367                 /* have to make sure this is a valid mif */
1368                 if (mifi < nummifs && mif6table[mifi].m6_ifp)
1369                         if (pim6 && (m->m_flags & M_LOOP) == 0) {
1370                                 /*
1371                                  * Check the M_LOOP flag to avoid an
1372                                  * unnecessary PIM assert.
1373                                  * XXX: M_LOOP is an ad-hoc hack...
1374                                  */
1375                                 static struct sockaddr_in6 sin6 =
1376                                 { sizeof(sin6), AF_INET6 };
1377
1378                                 struct mbuf *mm;
1379                                 struct mrt6msg *im;
1380 #ifdef MRT6_OINIT
1381                                 struct omrt6msg *oim;
1382 #endif
1383
1384                                 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1385                                 if (mm &&
1386                                     (M_HASCL(mm) ||
1387                                      mm->m_len < sizeof(struct ip6_hdr)))
1388                                         mm = m_pullup(mm, sizeof(struct ip6_hdr));
1389                                 if (mm == NULL)
1390                                         return (ENOBUFS);
1391
1392 #ifdef MRT6_OINIT
1393                                 oim = NULL;
1394 #endif
1395                                 im = NULL;
1396                                 switch (ip6_mrouter_ver) {
1397 #ifdef MRT6_OINIT
1398                                 case MRT6_OINIT:
1399                                         oim = mtod(mm, struct omrt6msg *);
1400                                         oim->im6_msgtype = MRT6MSG_WRONGMIF;
1401                                         oim->im6_mbz = 0;
1402                                         break;
1403 #endif
1404                                 case MRT6_INIT:
1405                                         im = mtod(mm, struct mrt6msg *);
1406                                         im->im6_msgtype = MRT6MSG_WRONGMIF;
1407                                         im->im6_mbz = 0;
1408                                         break;
1409                                 default:
1410                                         m_freem(mm);
1411                                         return (EINVAL);
1412                                 }
1413
1414                                 for (mifp = mif6table, iif = 0;
1415                                      iif < nummifs && mifp &&
1416                                              mifp->m6_ifp != ifp;
1417                                      mifp++, iif++)
1418                                         ;
1419
1420                                 switch (ip6_mrouter_ver) {
1421 #ifdef MRT6_OINIT
1422                                 case MRT6_OINIT:
1423                                         oim->im6_mif = iif;
1424                                         sin6.sin6_addr = oim->im6_src;
1425                                         break;
1426 #endif
1427                                 case MRT6_INIT:
1428                                         im->im6_mif = iif;
1429                                         sin6.sin6_addr = im->im6_src;
1430                                         break;
1431                                 }
1432
1433                                 mrt6stat.mrt6s_upcalls++;
1434
1435                                 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1436 #ifdef MRT6DEBUG
1437                                         if (mrt6debug)
1438                                                 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1439 #endif
1440                                         ++mrt6stat.mrt6s_upq_sockfull;
1441                                         return (ENOBUFS);
1442                                 }       /* if socket Q full */
1443                         }               /* if PIM */
1444                 return (0);
1445         }                       /* if wrong iif */
1446
1447         /* If I sourced this packet, it counts as output, else it was input. */
1448         if (m->m_pkthdr.rcvif == NULL) {
1449                 /* XXX: is rcvif really NULL when output?? */
1450                 mif6table[mifi].m6_pkt_out++;
1451                 mif6table[mifi].m6_bytes_out += plen;
1452         } else {
1453                 mif6table[mifi].m6_pkt_in++;
1454                 mif6table[mifi].m6_bytes_in += plen;
1455         }
1456         rt->mf6c_pkt_cnt++;
1457         rt->mf6c_byte_cnt += plen;
1458
1459         /*
1460          * For each mif, forward a copy of the packet if there are group
1461          * members downstream on the interface.
1462          */
1463         src0 = ip6->ip6_src;
1464         dst0 = ip6->ip6_dst;
1465         if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1466             (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1467                 ip6stat.ip6s_badscope++;
1468                 return (error);
1469         }
1470         for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1471                 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1472                         /*
1473                          * check if the outgoing packet is going to break
1474                          * a scope boundary.
1475                          * XXX For packets through PIM register tunnel
1476                          * interface, we believe a routing daemon.
1477                          */
1478                         if (!(mif6table[rt->mf6c_parent].m6_flags &
1479                               MIFF_REGISTER) &&
1480                             !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1481                                 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1482                                     &oszone) ||
1483                                     in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1484                                     &odzone) ||
1485                                     iszone != oszone ||
1486                                     idzone != odzone) {
1487                                         ip6stat.ip6s_badscope++;
1488                                         continue;
1489                                 }
1490                         }
1491
1492                         mifp->m6_pkt_out++;
1493                         mifp->m6_bytes_out += plen;
1494                         MC6_SEND(ip6, mifp, m);
1495                 }
1496         }
1497         return (0);
1498 }
1499
1500 static void
1501 phyint_send(ip6, mifp, m)
1502     struct ip6_hdr *ip6;
1503     struct mif6 *mifp;
1504     struct mbuf *m;
1505 {
1506         struct mbuf *mb_copy;
1507         struct ifnet *ifp = mifp->m6_ifp;
1508         int error = 0;
1509         int s = splnet();       /* needs to protect static "ro" below. */
1510         static struct route_in6 ro;
1511         struct  in6_multi *in6m;
1512         struct sockaddr_in6 *dst6;
1513         u_long linkmtu;
1514
1515         /*
1516          * Make a new reference to the packet; make sure that
1517          * the IPv6 header is actually copied, not just referenced,
1518          * so that ip6_output() only scribbles on the copy.
1519          */
1520         mb_copy = m_copy(m, 0, M_COPYALL);
1521         if (mb_copy &&
1522             (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1523                 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1524         if (mb_copy == NULL) {
1525                 splx(s);
1526                 return;
1527         }
1528         /* set MCAST flag to the outgoing packet */
1529         mb_copy->m_flags |= M_MCAST;
1530
1531         /*
1532          * If we sourced the packet, call ip6_output since we may devide
1533          * the packet into fragments when the packet is too big for the
1534          * outgoing interface.
1535          * Otherwise, we can simply send the packet to the interface
1536          * sending queue.
1537          */
1538         if (m->m_pkthdr.rcvif == NULL) {
1539                 struct ip6_moptions im6o;
1540
1541                 im6o.im6o_multicast_ifp = ifp;
1542                 /* XXX: ip6_output will override ip6->ip6_hlim */
1543                 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1544                 im6o.im6o_multicast_loop = 1;
1545                 error = ip6_output(mb_copy, NULL, &ro,
1546                                    IPV6_FORWARDING, &im6o, NULL, NULL);
1547
1548 #ifdef MRT6DEBUG
1549                 if (mrt6debug & DEBUG_XMIT)
1550                         log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1551                             mifp - mif6table, error);
1552 #endif
1553                 splx(s);
1554                 return;
1555         }
1556
1557         /*
1558          * If we belong to the destination multicast group
1559          * on the outgoing interface, loop back a copy.
1560          */
1561         dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1562         IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1563         if (in6m != NULL) {
1564                 dst6->sin6_len = sizeof(struct sockaddr_in6);
1565                 dst6->sin6_family = AF_INET6;
1566                 dst6->sin6_addr = ip6->ip6_dst;
1567                 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1568         }
1569         /*
1570          * Put the packet into the sending queue of the outgoing interface
1571          * if it would fit in the MTU of the interface.
1572          */
1573         linkmtu = IN6_LINKMTU(ifp);
1574         if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1575                 dst6->sin6_len = sizeof(struct sockaddr_in6);
1576                 dst6->sin6_family = AF_INET6;
1577                 dst6->sin6_addr = ip6->ip6_dst;
1578                 /*
1579                  * We just call if_output instead of nd6_output here, since
1580                  * we need no ND for a multicast forwarded packet...right?
1581                  */
1582                 error = (*ifp->if_output)(ifp, mb_copy,
1583                     (struct sockaddr *)&ro.ro_dst, NULL);
1584 #ifdef MRT6DEBUG
1585                 if (mrt6debug & DEBUG_XMIT)
1586                         log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1587                             mifp - mif6table, error);
1588 #endif
1589         } else {
1590                 /*
1591                  * pMTU discovery is intentionally disabled by default, since
1592                  * various router may notify pMTU in multicast, which can be
1593                  * a DDoS to a router
1594                  */
1595                 if (ip6_mcast_pmtu)
1596                         icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1597                 else {
1598 #ifdef MRT6DEBUG
1599                         if (mrt6debug & DEBUG_XMIT)
1600                                 log(LOG_DEBUG,
1601                                     "phyint_send: packet too big on %s o %s "
1602                                     "g %s size %d(discarded)\n",
1603                                     if_name(ifp),
1604                                     ip6_sprintf(&ip6->ip6_src),
1605                                     ip6_sprintf(&ip6->ip6_dst),
1606                                     mb_copy->m_pkthdr.len);
1607 #endif /* MRT6DEBUG */
1608                         m_freem(mb_copy); /* simply discard the packet */
1609                 }
1610         }
1611
1612         splx(s);
1613 }
1614
1615 static int
1616 register_send(ip6, mif, m)
1617         struct ip6_hdr *ip6;
1618         struct mif6 *mif;
1619         struct mbuf *m;
1620 {
1621         struct mbuf *mm;
1622         int i, len = m->m_pkthdr.len;
1623         static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1624         struct mrt6msg *im6;
1625
1626 #ifdef MRT6DEBUG
1627         if (mrt6debug)
1628                 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1629                     ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1630 #endif
1631         ++pim6stat.pim6s_snd_registers;
1632
1633         /* Make a copy of the packet to send to the user level process */
1634         MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1635         if (mm == NULL)
1636                 return (ENOBUFS);
1637         mm->m_pkthdr.rcvif = NULL;
1638         mm->m_data += max_linkhdr;
1639         mm->m_len = sizeof(struct ip6_hdr);
1640
1641         if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1642                 m_freem(mm);
1643                 return (ENOBUFS);
1644         }
1645         i = MHLEN - M_LEADINGSPACE(mm);
1646         if (i > len)
1647                 i = len;
1648         mm = m_pullup(mm, i);
1649         if (mm == NULL)
1650                 return (ENOBUFS);
1651 /* TODO: check it! */
1652         mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1653
1654         /*
1655          * Send message to routing daemon
1656          */
1657         sin6.sin6_addr = ip6->ip6_src;
1658
1659         im6 = mtod(mm, struct mrt6msg *);
1660         im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
1661         im6->im6_mbz          = 0;
1662
1663         im6->im6_mif = mif - mif6table;
1664
1665         /* iif info is not given for reg. encap.n */
1666         mrt6stat.mrt6s_upcalls++;
1667
1668         if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1669 #ifdef MRT6DEBUG
1670                 if (mrt6debug)
1671                         log(LOG_WARNING,
1672                             "register_send: ip6_mrouter socket queue full\n");
1673 #endif
1674                 ++mrt6stat.mrt6s_upq_sockfull;
1675                 return (ENOBUFS);
1676         }
1677         return (0);
1678 }
1679
1680 /*
1681  * PIM sparse mode hook
1682  * Receives the pim control messages, and passes them up to the listening
1683  * socket, using rip6_input.
1684  * The only message processed is the REGISTER pim message; the pim header
1685  * is stripped off, and the inner packet is passed to register_mforward.
1686  */
1687 int
1688 pim6_input(mp, offp, proto)
1689         struct mbuf **mp;
1690         int *offp, proto;
1691 {
1692         struct pim *pim; /* pointer to a pim struct */
1693         struct ip6_hdr *ip6;
1694         int pimlen;
1695         struct mbuf *m = *mp;
1696         int minlen;
1697         int off = *offp;
1698
1699         ++pim6stat.pim6s_rcv_total;
1700
1701         ip6 = mtod(m, struct ip6_hdr *);
1702         pimlen = m->m_pkthdr.len - *offp;
1703
1704         /*
1705          * Validate lengths
1706          */
1707         if (pimlen < PIM_MINLEN) {
1708                 ++pim6stat.pim6s_rcv_tooshort;
1709 #ifdef MRT6DEBUG
1710                 if (mrt6debug & DEBUG_PIM)
1711                         log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1712 #endif
1713                 m_freem(m);
1714                 return (IPPROTO_DONE);
1715         }
1716
1717         /*
1718          * if the packet is at least as big as a REGISTER, go ahead
1719          * and grab the PIM REGISTER header size, to avoid another
1720          * possible m_pullup() later.
1721          *
1722          * PIM_MINLEN       == pimhdr + u_int32 == 8
1723          * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1724          */
1725         minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1726
1727         /*
1728          * Make sure that the IP6 and PIM headers in contiguous memory, and
1729          * possibly the PIM REGISTER header
1730          */
1731 #ifndef PULLDOWN_TEST
1732         IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1733         /* adjust pointer */
1734         ip6 = mtod(m, struct ip6_hdr *);
1735
1736         /* adjust mbuf to point to the PIM header */
1737         pim = (struct pim *)((caddr_t)ip6 + off);
1738 #else
1739         IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1740         if (pim == NULL) {
1741                 pim6stat.pim6s_rcv_tooshort++;
1742                 return (IPPROTO_DONE);
1743         }
1744 #endif
1745
1746 #define PIM6_CHECKSUM
1747 #ifdef PIM6_CHECKSUM
1748         {
1749                 int cksumlen;
1750
1751                 /*
1752                  * Validate checksum.
1753                  * If PIM REGISTER, exclude the data packet
1754                  */
1755                 if (pim->pim_type == PIM_REGISTER)
1756                         cksumlen = PIM_MINLEN;
1757                 else
1758                         cksumlen = pimlen;
1759
1760                 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1761                         ++pim6stat.pim6s_rcv_badsum;
1762 #ifdef MRT6DEBUG
1763                         if (mrt6debug & DEBUG_PIM)
1764                                 log(LOG_DEBUG,
1765                                     "pim6_input: invalid checksum\n");
1766 #endif
1767                         m_freem(m);
1768                         return (IPPROTO_DONE);
1769                 }
1770         }
1771 #endif /* PIM_CHECKSUM */
1772
1773         /* PIM version check */
1774         if (pim->pim_ver != PIM_VERSION) {
1775                 ++pim6stat.pim6s_rcv_badversion;
1776 #ifdef MRT6DEBUG
1777                 log(LOG_ERR,
1778                     "pim6_input: incorrect version %d, expecting %d\n",
1779                     pim->pim_ver, PIM_VERSION);
1780 #endif
1781                 m_freem(m);
1782                 return (IPPROTO_DONE);
1783         }
1784
1785         if (pim->pim_type == PIM_REGISTER) {
1786                 /*
1787                  * since this is a REGISTER, we'll make a copy of the register
1788                  * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1789                  * routing daemon.
1790                  */
1791                 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1792
1793                 struct mbuf *mcp;
1794                 struct ip6_hdr *eip6;
1795                 u_int32_t *reghdr;
1796                 int rc;
1797
1798                 ++pim6stat.pim6s_rcv_registers;
1799
1800                 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1801 #ifdef MRT6DEBUG
1802                         if (mrt6debug & DEBUG_PIM)
1803                                 log(LOG_DEBUG,
1804                                     "pim6_input: register mif not set: %d\n",
1805                                     reg_mif_num);
1806 #endif
1807                         m_freem(m);
1808                         return (IPPROTO_DONE);
1809                 }
1810
1811                 reghdr = (u_int32_t *)(pim + 1);
1812
1813                 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1814                         goto pim6_input_to_daemon;
1815
1816                 /*
1817                  * Validate length
1818                  */
1819                 if (pimlen < PIM6_REG_MINLEN) {
1820                         ++pim6stat.pim6s_rcv_tooshort;
1821                         ++pim6stat.pim6s_rcv_badregisters;
1822 #ifdef MRT6DEBUG
1823                         log(LOG_ERR,
1824                             "pim6_input: register packet size too "
1825                             "small %d from %s\n",
1826                             pimlen, ip6_sprintf(&ip6->ip6_src));
1827 #endif
1828                         m_freem(m);
1829                         return (IPPROTO_DONE);
1830                 }
1831
1832                 eip6 = (struct ip6_hdr *) (reghdr + 1);
1833 #ifdef MRT6DEBUG
1834                 if (mrt6debug & DEBUG_PIM)
1835                         log(LOG_DEBUG,
1836                             "pim6_input[register], eip6: %s -> %s, "
1837                             "eip6 plen %d\n",
1838                             ip6_sprintf(&eip6->ip6_src),
1839                             ip6_sprintf(&eip6->ip6_dst),
1840                             ntohs(eip6->ip6_plen));
1841 #endif
1842
1843                 /* verify the version number of the inner packet */
1844                 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1845                         ++pim6stat.pim6s_rcv_badregisters;
1846 #ifdef MRT6DEBUG
1847                         log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1848                             "of the inner packet\n",
1849                             (eip6->ip6_vfc & IPV6_VERSION));
1850 #endif
1851                         m_freem(m);
1852                         return (IPPROTO_NONE);
1853                 }
1854
1855                 /* verify the inner packet is destined to a mcast group */
1856                 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1857                         ++pim6stat.pim6s_rcv_badregisters;
1858 #ifdef MRT6DEBUG
1859                         if (mrt6debug & DEBUG_PIM)
1860                                 log(LOG_DEBUG,
1861                                     "pim6_input: inner packet of register "
1862                                     "is not multicast %s\n",
1863                                     ip6_sprintf(&eip6->ip6_dst));
1864 #endif
1865                         m_freem(m);
1866                         return (IPPROTO_DONE);
1867                 }
1868
1869                 /*
1870                  * make a copy of the whole header to pass to the daemon later.
1871                  */
1872                 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1873                 if (mcp == NULL) {
1874 #ifdef MRT6DEBUG
1875                         log(LOG_ERR,
1876                             "pim6_input: pim register: "
1877                             "could not copy register head\n");
1878 #endif
1879                         m_freem(m);
1880                         return (IPPROTO_DONE);
1881                 }
1882
1883                 /*
1884                  * forward the inner ip6 packet; point m_data at the inner ip6.
1885                  */
1886                 m_adj(m, off + PIM_MINLEN);
1887 #ifdef MRT6DEBUG
1888                 if (mrt6debug & DEBUG_PIM) {
1889                         log(LOG_DEBUG,
1890                             "pim6_input: forwarding decapsulated register: "
1891                             "src %s, dst %s, mif %d\n",
1892                             ip6_sprintf(&eip6->ip6_src),
1893                             ip6_sprintf(&eip6->ip6_dst),
1894                             reg_mif_num);
1895                 }
1896 #endif
1897
1898                 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1899                                 dst.sin6_family, 0);
1900
1901                 /* prepare the register head to send to the mrouting daemon */
1902                 m = mcp;
1903         }
1904
1905         /*
1906          * Pass the PIM message up to the daemon; if it is a register message
1907          * pass the 'head' only up to the daemon. This includes the
1908          * encapsulator ip6 header, pim header, register header and the
1909          * encapsulated ip6 header.
1910          */
1911   pim6_input_to_daemon:
1912         rip6_input(&m, offp, proto);
1913         return (IPPROTO_DONE);
1914 }