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