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