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