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