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