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