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