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