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