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