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