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