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