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