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