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