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