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