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