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