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