ip6_mroute.c revision 1.5 1 /* $NetBSD: ip6_mroute.c,v 1.5 1999/07/06 08:55:56 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 * If the packet is loop-backed, it should be for local listeners
927 * and need not to be forwarded any more.
928 * XXX: M_LOOP is an ad-hoc hack...
929 */
930 if (m->m_flags & M_LOOP)
931 return 0;
932
933 /*
934 * Don't forward a packet with Hop limit of zero or one,
935 * or a packet destined to a local-only group.
936 */
937 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
938 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
939 return 0;
940 ip6->ip6_hlim--;
941
942 /*
943 * Determine forwarding mifs from the forwarding cache table
944 */
945 s = splsoftnet();
946 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
947
948 /* Entry exists, so forward if necessary */
949 if (rt) {
950 splx(s);
951 return (ip6_mdq(m, ifp, rt));
952 } else {
953 /*
954 * If we don't have a route for packet's origin,
955 * Make a copy of the packet &
956 * send message to routing daemon
957 */
958
959 register struct mbuf *mb0;
960 register struct rtdetq *rte;
961 register u_long hash;
962 /* register int i, npkts;*/
963 #ifdef UPCALL_TIMING
964 struct timeval tp;
965
966 GET_TIME(tp);
967 #endif /* UPCALL_TIMING */
968
969 mrt6stat.mrt6s_no_route++;
970 #ifdef MRT6DEBUG
971 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
972 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
973 ip6_sprintf(&ip6->ip6_src),
974 ip6_sprintf(&ip6->ip6_dst));
975 #endif
976
977 /*
978 * Allocate mbufs early so that we don't do extra work if we
979 * are just going to fail anyway.
980 */
981 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
982 M_NOWAIT);
983 if (rte == NULL) {
984 splx(s);
985 return ENOBUFS;
986 }
987 mb0 = m_copy(m, 0, M_COPYALL);
988 /*
989 * Pullup packet header if needed before storing it,
990 * as other references may modify it in the meantime.
991 */
992 if (mb0 &&
993 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
994 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
995 if (mb0 == NULL) {
996 free(rte, M_MRTABLE);
997 splx(s);
998 return ENOBUFS;
999 }
1000
1001 /* is there an upcall waiting for this packet? */
1002 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1003 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1004 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1005 &rt->mf6c_origin.sin6_addr) &&
1006 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1007 &rt->mf6c_mcastgrp.sin6_addr) &&
1008 (rt->mf6c_stall != NULL))
1009 break;
1010 }
1011
1012 if (rt == NULL) {
1013 struct mrt6msg *im;
1014
1015 /* no upcall, so make a new entry */
1016 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
1017 M_NOWAIT);
1018 if (rt == NULL) {
1019 free(rte, M_MRTABLE);
1020 m_freem(mb0);
1021 splx(s);
1022 return ENOBUFS;
1023 }
1024 /*
1025 * Make a copy of the header to send to the user
1026 * level process
1027 */
1028 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1029
1030 if (mm == NULL) {
1031 free(rte, M_MRTABLE);
1032 m_freem(mb0);
1033 free(rt, M_MRTABLE);
1034 splx(s);
1035 return ENOBUFS;
1036 }
1037
1038 /*
1039 * Send message to routing daemon
1040 */
1041 sin6.sin6_addr = ip6->ip6_src;
1042
1043 im = mtod(mm, struct mrt6msg *);
1044 im->im6_msgtype = MRT6MSG_NOCACHE;
1045 im->im6_mbz = 0;
1046
1047 #ifdef MRT6DEBUG
1048 if (mrt6debug & DEBUG_FORWARD)
1049 log(LOG_DEBUG,
1050 "getting the iif info in the kernel\n");
1051 #endif
1052
1053 for (mifp = mif6table, mifi = 0;
1054 mifi < nummifs && mifp->m6_ifp != ifp;
1055 mifp++, mifi++)
1056 ;
1057
1058 im->im6_mif = mifi;
1059
1060 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1061 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1062 "socket queue full\n");
1063 mrt6stat.mrt6s_upq_sockfull++;
1064 free(rte, M_MRTABLE);
1065 m_freem(mb0);
1066 free(rt, M_MRTABLE);
1067 splx(s);
1068 return ENOBUFS;
1069 }
1070
1071 mrt6stat.mrt6s_upcalls++;
1072
1073 /* insert new entry at head of hash chain */
1074 bzero(rt, sizeof(*rt));
1075 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1076 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1077 rt->mf6c_expire = UPCALL_EXPIRE;
1078 nexpire[hash]++;
1079 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1080
1081 /* link into table */
1082 rt->mf6c_next = mf6ctable[hash];
1083 mf6ctable[hash] = rt;
1084 /* Add this entry to the end of the queue */
1085 rt->mf6c_stall = rte;
1086 } else {
1087 /* determine if q has overflowed */
1088 struct rtdetq **p;
1089 register int npkts = 0;
1090
1091 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1092 if (++npkts > MAX_UPQ6) {
1093 mrt6stat.mrt6s_upq_ovflw++;
1094 free(rte, M_MRTABLE);
1095 m_freem(mb0);
1096 splx(s);
1097 return 0;
1098 }
1099
1100 /* Add this entry to the end of the queue */
1101 *p = rte;
1102 }
1103
1104 rte->next = NULL;
1105 rte->m = mb0;
1106 rte->ifp = ifp;
1107 #ifdef UPCALL_TIMING
1108 rte->t = tp;
1109 #endif /* UPCALL_TIMING */
1110
1111 splx(s);
1112
1113 return 0;
1114 }
1115 }
1116
1117 /*
1118 * Clean up cache entries if upcalls are not serviced
1119 * Call from the Slow Timeout mechanism, every half second.
1120 */
1121 static void
1122 expire_upcalls(unused)
1123 void *unused;
1124 {
1125 struct rtdetq *rte;
1126 struct mf6c *mfc, **nptr;
1127 int i;
1128 int s;
1129
1130 s = splsoftnet();
1131 for (i = 0; i < MF6CTBLSIZ; i++) {
1132 if (nexpire[i] == 0)
1133 continue;
1134 nptr = &mf6ctable[i];
1135 while ((mfc = *nptr) != NULL) {
1136 rte = mfc->mf6c_stall;
1137 /*
1138 * Skip real cache entries
1139 * Make sure it wasn't marked to not expire (shouldn't happen)
1140 * If it expires now
1141 */
1142 if (rte != NULL &&
1143 mfc->mf6c_expire != 0 &&
1144 --mfc->mf6c_expire == 0) {
1145 #ifdef MRT6DEBUG
1146 if (mrt6debug & DEBUG_EXPIRE)
1147 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1148 ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1149 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1150 #endif
1151 /*
1152 * drop all the packets
1153 * free the mbuf with the pkt, if, timing info
1154 */
1155 do {
1156 struct rtdetq *n = rte->next;
1157 m_freem(rte->m);
1158 free(rte, M_MRTABLE);
1159 rte = n;
1160 } while (rte != NULL);
1161 mrt6stat.mrt6s_cache_cleanups++;
1162 nexpire[i]--;
1163
1164 *nptr = mfc->mf6c_next;
1165 free(mfc, M_MRTABLE);
1166 } else {
1167 nptr = &mfc->mf6c_next;
1168 }
1169 }
1170 }
1171 splx(s);
1172 timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1173 }
1174
1175 /*
1176 * Packet forwarding routine once entry in the cache is made
1177 */
1178 static int
1179 ip6_mdq(m, ifp, rt)
1180 register struct mbuf *m;
1181 register struct ifnet *ifp;
1182 register struct mf6c *rt;
1183 {
1184 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1185 register mifi_t mifi, iif;
1186 register struct mif6 *mifp;
1187 register u_long plen = m->m_pkthdr.len;
1188
1189 /*
1190 * Macro to send packet on mif. Since RSVP packets don't get counted on
1191 * input, they shouldn't get counted on output, so statistics keeping is
1192 * seperate.
1193 */
1194
1195 #define MC6_SEND(ip6,mifp,m) { \
1196 if ((mifp)->m6_flags & MIFF_REGISTER) \
1197 register_send((ip6), (mifp), (m)); \
1198 else \
1199 phyint_send((ip6), (mifp), (m)); \
1200 }
1201
1202 /*
1203 * Don't forward if it didn't arrive from the parent mif
1204 * for its origin.
1205 */
1206 mifi = rt->mf6c_parent;
1207 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1208 /* came in the wrong interface */
1209 #ifdef MRT6DEBUG
1210 if (mrt6debug & DEBUG_FORWARD)
1211 log(LOG_DEBUG,
1212 "wrong if: ifid %d mifi %d mififid %x\n",
1213 ifp->if_index, mifi,
1214 mif6table[mifi].m6_ifp->if_index);
1215 #endif
1216 mrt6stat.mrt6s_wrong_if++;
1217 rt->mf6c_wrong_if++;
1218 /*
1219 * If we are doing PIM processing, and we are forwarding
1220 * packets on this interface, send a message to the
1221 * routing daemon.
1222 */
1223 if(mifi < nummifs) /* have to make sure this is a valid mif */
1224 if(mif6table[mifi].m6_ifp)
1225
1226 if (pim6) {
1227 static struct sockaddr_in6 sin6 =
1228 { sizeof(sin6), AF_INET6 };
1229
1230 register struct mbuf *mm;
1231 struct mrt6msg *im;
1232
1233 mm = m_copy(m, 0,
1234 sizeof(struct ip6_hdr));
1235 if (mm &&
1236 (M_HASCL(mm) ||
1237 mm->m_len < sizeof(struct ip6_hdr)))
1238 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1239 if (mm == NULL)
1240 return ENOBUFS;
1241
1242 im = mtod(mm, struct mrt6msg *);
1243 im->im6_msgtype = MRT6MSG_WRONGMIF;
1244 im->im6_mbz = 0;
1245
1246 for (mifp = mif6table, iif = 0;
1247 iif < nummifs && mifp &&
1248 mifp->m6_ifp != ifp;
1249 mifp++, iif++);
1250
1251 im->im6_mif = iif;
1252
1253 sin6.sin6_addr = im->im6_src;
1254
1255 mrt6stat.mrt6s_upcalls++;
1256
1257 if (socket_send(ip6_mrouter, mm,
1258 &sin6) < 0) {
1259 #ifdef MRT6DEBUG
1260 if (mrt6debug)
1261 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1262 #endif
1263 ++mrt6stat.mrt6s_upq_sockfull;
1264 return ENOBUFS;
1265 } /* if socket Q full */
1266 } /* if PIM */
1267 return 0;
1268 } /* if wrong iif */
1269
1270 /* If I sourced this packet, it counts as output, else it was input. */
1271 if (m->m_pkthdr.rcvif == NULL) {
1272 /* XXX: is rcvif really NULL when output?? */
1273 mif6table[mifi].m6_pkt_out++;
1274 mif6table[mifi].m6_bytes_out += plen;
1275 } else {
1276 mif6table[mifi].m6_pkt_in++;
1277 mif6table[mifi].m6_bytes_in += plen;
1278 }
1279 rt->mf6c_pkt_cnt++;
1280 rt->mf6c_byte_cnt += plen;
1281
1282 /*
1283 * For each mif, forward a copy of the packet if there are group
1284 * members downstream on the interface.
1285 */
1286 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1287 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1288 mifp->m6_pkt_out++;
1289 mifp->m6_bytes_out += plen;
1290 MC6_SEND(ip6, mifp, m);
1291 }
1292 return 0;
1293 }
1294
1295 static void
1296 phyint_send(ip6, mifp, m)
1297 struct ip6_hdr *ip6;
1298 struct mif6 *mifp;
1299 struct mbuf *m;
1300 {
1301 register struct mbuf *mb_copy;
1302 struct ifnet *ifp = mifp->m6_ifp;
1303 int error = 0;
1304 int s = splsoftnet();
1305 static struct route_in6 ro6;
1306 struct in6_multi *in6m;
1307
1308 /*
1309 * Make a new reference to the packet; make sure that
1310 * the IPv6 header is actually copied, not just referenced,
1311 * so that ip6_output() only scribbles on the copy.
1312 */
1313 mb_copy = m_copy(m, 0, M_COPYALL);
1314 if (mb_copy &&
1315 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1316 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1317 if (mb_copy == NULL)
1318 return;
1319 /* set MCAST flag to the outgoing packet */
1320 mb_copy->m_flags |= M_MCAST;
1321
1322 /*
1323 * If we sourced the packet, call ip6_output since we may devide
1324 * the packet into fragments when the packet is too big for the
1325 * outgoing interface.
1326 * Otherwise, we can simply send the packet to the interface
1327 * sending queue.
1328 */
1329 if (m->m_pkthdr.rcvif == NULL) {
1330 struct ip6_moptions im6o;
1331
1332 im6o.im6o_multicast_ifp = ifp;
1333 /* XXX: ip6_output will override ip6->ip6_hlim */
1334 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1335 im6o.im6o_multicast_loop = 1;
1336 error = ip6_output(mb_copy, NULL, &ro6,
1337 IPV6_FORWARDING, &im6o);
1338
1339 #ifdef MRT6DEBUG
1340 if (mrt6debug & DEBUG_XMIT)
1341 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1342 mifp - mif6table, error);
1343 #endif
1344 splx(s);
1345 return;
1346 }
1347
1348 /*
1349 * If we belong to the destination multicast group
1350 * on the outgoing interface, loop back a copy.
1351 */
1352 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1353 if (in6m != NULL) {
1354 ro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
1355 ro6.ro_dst.sin6_family = AF_INET6;
1356 ro6.ro_dst.sin6_addr = ip6->ip6_dst;
1357 ip6_mloopback(ifp, m, &ro6.ro_dst);
1358 }
1359 /*
1360 * Put the packet into the sending queue of the outgoing interface
1361 * if it would fit in the MTU of the interface.
1362 */
1363 if (mb_copy->m_pkthdr.len < ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1364 ro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
1365 ro6.ro_dst.sin6_family = AF_INET6;
1366 ro6.ro_dst.sin6_addr = ip6->ip6_dst;
1367 error = (*ifp->if_output)(ifp, mb_copy,
1368 (struct sockaddr *)&ro6.ro_dst,
1369 NULL);
1370 #ifdef MRT6DEBUG
1371 if (mrt6debug & DEBUG_XMIT)
1372 log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1373 mifp - mif6table, error);
1374 #endif
1375 }
1376 else {
1377 #ifdef MULTICAST_PMTUD
1378 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1379 return;
1380 #else
1381 #ifdef MRT6DEBUG
1382 #ifdef __NetBSD__
1383 if (mrt6debug & DEBUG_DEBUG_XMIT)
1384 log(LOG_DEBUG,
1385 "phyint_send: packet too big on %s o %s g %s"
1386 " size %d(discarded)\n",
1387 ifp->if_xname,
1388 ip6_sprintf(&ip6->ip6_src),
1389 ip6_sprintf(&ip6->ip6_dst),
1390 mb_copy->m_pkthdr.len);
1391 #else
1392 if (mrt6debug & DEBUG_XMIT)
1393 log(LOG_DEBUG,
1394 "phyint_send: packet too big on %s%u o %s g %s"
1395 " size %d(discarded)\n",
1396 ifp->if_name, ifp->if_unit,
1397 ip6_sprintf(&ip6->ip6_src),
1398 ip6_sprintf(&ip6->ip6_dst),
1399 mb_copy->m_pkthdr.len);
1400 #endif /* __NetBSD__ */
1401 #endif /* MRT6DEBUG */
1402 m_freem(mb_copy); /* simply discard the packet */
1403 return;
1404 #endif
1405 }
1406 }
1407
1408 static int
1409 register_send(ip6, mif, m)
1410 register struct ip6_hdr *ip6;
1411 struct mif6 *mif;
1412 register struct mbuf *m;
1413 {
1414 register struct mbuf *mm;
1415 register int i, len = m->m_pkthdr.len;
1416 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1417 struct mrt6msg *im6;
1418
1419 #ifdef MRT6DEBUG
1420 if (mrt6debug)
1421 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1422 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1423 #endif
1424 ++pim6stat.pim6s_snd_registers;
1425
1426 /* Make a copy of the packet to send to the user level process */
1427 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1428 if (mm == NULL)
1429 return ENOBUFS;
1430 mm->m_data += max_linkhdr;
1431 mm->m_len = sizeof(struct ip6_hdr);
1432
1433 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1434 m_freem(mm);
1435 return ENOBUFS;
1436 }
1437 i = MHLEN - M_LEADINGSPACE(mm);
1438 if (i > len)
1439 i = len;
1440 mm = m_pullup(mm, i);
1441 if (mm == NULL){
1442 m_freem(mm);
1443 return ENOBUFS;
1444 }
1445 /* TODO: check it! */
1446 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1447
1448 /*
1449 * Send message to routing daemon
1450 */
1451 sin6.sin6_addr = ip6->ip6_src;
1452
1453 im6 = mtod(mm, struct mrt6msg *);
1454 im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1455 im6->im6_mbz = 0;
1456
1457 im6->im6_mif = mif - mif6table;
1458
1459 /* iif info is not given for reg. encap.n */
1460 mrt6stat.mrt6s_upcalls++;
1461
1462 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1463 #ifdef MRT6DEBUG
1464 if (mrt6debug)
1465 log(LOG_WARNING,
1466 "register_send: ip_mrouter socket queue full\n");
1467 #endif
1468 ++mrt6stat.mrt6s_upq_sockfull;
1469 return ENOBUFS;
1470 }
1471 return 0;
1472 }
1473
1474 /*
1475 * PIM sparse mode hook
1476 * Receives the pim control messages, and passes them up to the listening
1477 * socket, using rip6_input.
1478 * The only message processed is the REGISTER pim message; the pim header
1479 * is stripped off, and the inner packet is passed to register_mforward.
1480 */
1481 int
1482 pim6_input(mp, offp, proto)
1483 struct mbuf **mp;
1484 int *offp, proto;
1485 {
1486 register struct pim *pim; /* pointer to a pim struct */
1487 register struct ip6_hdr *ip6;
1488 register int pimlen;
1489 struct mbuf *m = *mp;
1490 int minlen;
1491 int off = *offp;
1492
1493 ++pim6stat.pim6s_rcv_total;
1494
1495 ip6 = mtod(m, struct ip6_hdr *);
1496 pimlen = m->m_pkthdr.len - *offp;
1497
1498 /*
1499 * Validate lengths
1500 */
1501 if (pimlen < PIM_MINLEN) {
1502 ++pim6stat.pim6s_rcv_tooshort;
1503 #ifdef MRT6DEBUG
1504 if (mrt6debug & DEBUG_PIM)
1505 log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1506 #endif
1507 m_freem(m);
1508 return(IPPROTO_DONE);
1509 }
1510
1511 /*
1512 * if the packet is at least as big as a REGISTER, go ahead
1513 * and grab the PIM REGISTER header size, to avoid another
1514 * possible m_pullup() later.
1515 *
1516 * PIM_MINLEN == pimhdr + u_int32 == 8
1517 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1518 */
1519 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1520
1521 /*
1522 * Make sure that the IP6 and PIM headers in contiguous memory, and
1523 * possibly the PIM REGISTER header
1524 */
1525 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1526 /* adjust pointer */
1527 ip6 = mtod(m, struct ip6_hdr *);
1528
1529 /* adjust mbuf to point to the PIM header */
1530 pim = (struct pim *)((caddr_t)ip6 + off);
1531
1532 #define PIM6_CHECKSUM
1533 #ifdef PIM6_CHECKSUM
1534 {
1535 int cksumlen;
1536
1537 /*
1538 * Validate checksum.
1539 * If PIM REGISTER, exclude the data packet
1540 */
1541 if (pim->pim_type == PIM_REGISTER)
1542 cksumlen = PIM_MINLEN;
1543 else
1544 cksumlen = pimlen;
1545
1546 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1547 ++pim6stat.pim6s_rcv_badsum;
1548 #ifdef MRT6DEBUG
1549 if (mrt6debug & DEBUG_PIM)
1550 log(LOG_DEBUG,
1551 "pim6_input: invalid checksum\n");
1552 #endif
1553 m_freem(m);
1554 return(IPPROTO_DONE);
1555 }
1556 }
1557 #endif /* PIM_CHECKSUM */
1558
1559 /* PIM version check */
1560 if (pim->pim_ver != PIM_VERSION) {
1561 ++pim6stat.pim6s_rcv_badversion;
1562 #ifdef MRT6DEBUG
1563 log(LOG_ERR,
1564 "pim6_input: incorrect version %d, expecting %d\n",
1565 pim->pim_ver, PIM_VERSION);
1566 #endif
1567 m_freem(m);
1568 return(IPPROTO_DONE);
1569 }
1570
1571 if (pim->pim_type == PIM_REGISTER) {
1572 /*
1573 * since this is a REGISTER, we'll make a copy of the register
1574 * headers ip6+pim+u_long+encap_ip6, to be passed up to the
1575 * routing daemon.
1576 */
1577 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1578
1579 struct mbuf *mcp;
1580 struct ip6_hdr *eip6;
1581 u_long *reghdr;
1582 int rc;
1583
1584 ++pim6stat.pim6s_rcv_registers;
1585
1586 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1587 #ifdef MRT6DEBUG
1588 if (mrt6debug & DEBUG_PIM)
1589 log(LOG_DEBUG,
1590 "pim6_input: register mif not set: %d\n",
1591 reg_mif_num);
1592 #endif
1593 m_freem(m);
1594 return(IPPROTO_DONE);
1595 }
1596
1597 reghdr = (u_long *)(pim + 1);
1598
1599 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1600 goto pim6_input_to_daemon;
1601
1602 /*
1603 * Validate length
1604 */
1605 if (pimlen < PIM6_REG_MINLEN) {
1606 ++pim6stat.pim6s_rcv_tooshort;
1607 ++pim6stat.pim6s_rcv_badregisters;
1608 #ifdef MRT6DEBUG
1609 log(LOG_ERR,
1610 "pim6_input: register packet size too "
1611 "small %d from %s\n",
1612 pimlen, ip6_sprintf(&ip6->ip6_src));
1613 #endif
1614 m_freem(m);
1615 return(IPPROTO_DONE);
1616 }
1617
1618 eip6 = (struct ip6_hdr *) (reghdr + 1);
1619 #ifdef MRT6DEBUG
1620 if (mrt6debug & DEBUG_PIM)
1621 log(LOG_DEBUG,
1622 "pim6_input[register], eip6: %s -> %s, "
1623 "eip6 plen %d\n",
1624 ip6_sprintf(&eip6->ip6_src),
1625 ip6_sprintf(&eip6->ip6_dst),
1626 ntohs(eip6->ip6_plen));
1627 #endif
1628
1629 /* verify the inner packet is destined to a mcast group */
1630 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1631 ++pim6stat.pim6s_rcv_badregisters;
1632 #ifdef MRT6DEBUG
1633 if (mrt6debug & DEBUG_PIM)
1634 log(LOG_DEBUG,
1635 "pim6_input: inner packet of register "
1636 "is not multicast %s\n",
1637 ip6_sprintf(&eip6->ip6_dst));
1638 #endif
1639 m_freem(m);
1640 return(IPPROTO_DONE);
1641 }
1642
1643 /*
1644 * make a copy of the whole header to pass to the daemon later.
1645 */
1646 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1647 if (mcp == NULL) {
1648 #ifdef MRT6DEBUG
1649 log(LOG_ERR,
1650 "pim6_input: pim register: "
1651 "could not copy register head\n");
1652 #endif
1653 m_freem(m);
1654 return(IPPROTO_DONE);
1655 }
1656
1657 /*
1658 * forward the inner ip6 packet; point m_data at the inner ip6.
1659 */
1660 m_adj(m, off + PIM_MINLEN);
1661 #ifdef MRT6DEBUG
1662 if (mrt6debug & DEBUG_PIM) {
1663 log(LOG_DEBUG,
1664 "pim6_input: forwarding decapsulated register: "
1665 "src %s, dst %s, mif %d\n",
1666 ip6_sprintf(&eip6->ip6_src),
1667 ip6_sprintf(&eip6->ip6_dst),
1668 reg_mif_num);
1669 }
1670 #endif
1671
1672 rc = looutput(mif6table[reg_mif_num].m6_ifp, m,
1673 (struct sockaddr *) &dst,
1674 (struct rtentry *) NULL);
1675
1676 /* prepare the register head to send to the mrouting daemon */
1677 m = mcp;
1678 }
1679
1680 /*
1681 * Pass the PIM message up to the daemon; if it is a register message
1682 * pass the 'head' only up to the daemon. This includes the
1683 * encapsulator ip6 header, pim header, register header and the
1684 * encapsulated ip6 header.
1685 */
1686 pim6_input_to_daemon:
1687 rip6_input(&m, offp, proto);
1688 return(IPPROTO_DONE);
1689 }
1690