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