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