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