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