ip6_mroute.c revision 157633
11556Srgrimes/* $FreeBSD: head/sys/netinet6/ip6_mroute.c 157633 2006-04-10 14:33:22Z suz $ */ 21556Srgrimes/* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */ 31556Srgrimes 41556Srgrimes/*- 51556Srgrimes * Copyright (C) 1998 WIDE Project. 61556Srgrimes * All rights reserved. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. Neither the name of the project nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3090143Smarkm * SUCH DAMAGE. 311556Srgrimes */ 3236049Scharnier 3390143Smarkm/* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */ 3436049Scharnier 35110391Scharnier/*- 3699110Sobrien * Copyright (c) 1989 Stephen Deering 3799110Sobrien * Copyright (c) 1992, 1993 381556Srgrimes * The Regents of the University of California. All rights reserved. 391556Srgrimes * 401556Srgrimes * This code is derived from software contributed to Berkeley by 411556Srgrimes * Stephen Deering of Stanford University. 421556Srgrimes * 431556Srgrimes * Redistribution and use in source and binary forms, with or without 441556Srgrimes * modification, are permitted provided that the following conditions 45105831Srwatson * are met: 4611890Sphk * 1. Redistributions of source code must retain the above copyright 471556Srgrimes * notice, this list of conditions and the following disclaimer. 481556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 491556Srgrimes * notice, this list of conditions and the following disclaimer in the 5090878Simp * documentation and/or other materials provided with the distribution. 5173369Sache * 4. Neither the name of the University nor the names of its contributors 5272343Sache * may be used to endorse or promote products derived from this software 531556Srgrimes * without specific prior written permission. 541556Srgrimes * 5590878Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 561556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 571556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 581556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5990143Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 6016835Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 611556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 621556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 631556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 641556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65103274Speter * SUCH DAMAGE. 6690143Smarkm * 671556Srgrimes * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93 6890110Simp */ 691556Srgrimes 701556Srgrimes/* 711556Srgrimes * IP multicast forwarding procedures 721556Srgrimes * 73130999Sgad * Written by David Waitzman, BBN Labs, August 1988. 74130999Sgad * Modified by Steve Deering, Stanford, February 1989. 75106251Stjr * Modified by Mark J. Steiglitz, Stanford, May, 1991 76130999Sgad * Modified by Van Jacobson, LBL, January 1993 77106251Stjr * Modified by Ajit Thyagarajan, PARC, August 1993 78130999Sgad * Modified by Bill Fenner, PARC, April 1994 79130999Sgad * 801556Srgrimes * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support 811556Srgrimes */ 82130999Sgad 83109504Sjmallett#include "opt_inet.h" 841556Srgrimes#include "opt_inet6.h" 85109504Sjmallett 861556Srgrimes#include <sys/param.h> 87109504Sjmallett#include <sys/callout.h> 88130999Sgad#include <sys/errno.h> 891556Srgrimes#include <sys/kernel.h> 901556Srgrimes#include <sys/lock.h> 911556Srgrimes#include <sys/malloc.h> 921556Srgrimes#include <sys/mbuf.h> 931556Srgrimes#include <sys/protosw.h> 941556Srgrimes#include <sys/signalvar.h> 9597958Sjmallett#include <sys/socket.h> 9697958Sjmallett#include <sys/socketvar.h> 9797958Sjmallett#include <sys/sockio.h> 9897958Sjmallett#include <sys/sx.h> 9997958Sjmallett#include <sys/syslog.h> 10097958Sjmallett#include <sys/systm.h> 10197958Sjmallett#include <sys/time.h> 10297958Sjmallett 10397958Sjmallett#include <net/if.h> 10497958Sjmallett#include <net/if_types.h> 105130999Sgad#include <net/raw_cb.h> 10697958Sjmallett#include <net/route.h> 10797958Sjmallett 10897958Sjmallett#include <netinet/in.h> 10997958Sjmallett#include <netinet/in_var.h> 11097958Sjmallett#include <netinet/icmp6.h> 11197958Sjmallett 11297958Sjmallett#include <netinet/ip6.h> 11397958Sjmallett#include <netinet6/ip6_var.h> 11497958Sjmallett#include <netinet6/scope6_var.h> 11597958Sjmallett#include <netinet6/nd6.h> 11697958Sjmallett#include <netinet6/ip6_mroute.h> 11797958Sjmallett#include <netinet6/pim6.h> 11897958Sjmallett#include <netinet6/pim6_var.h> 11997958Sjmallett 12097958Sjmallett#include <net/net_osdep.h> 12197958Sjmallett 12297958Sjmallettstatic MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry"); 12390110Simp 1241556Srgrimes#define M_HASCL(m) ((m)->m_flags & M_EXT) 1251556Srgrimes 1261556Srgrimesstatic int ip6_mdq __P((struct mbuf *, struct ifnet *, struct mf6c *)); 1271556Srgrimesstatic void phyint_send __P((struct ip6_hdr *, struct mif6 *, struct mbuf *)); 1281556Srgrimes 12919068Speterstatic int set_pim6 __P((int *)); 13019068Speterstatic int socket_send __P((struct socket *, struct mbuf *, 131130999Sgad struct sockaddr_in6 *)); 132130999Sgadstatic int register_send __P((struct ip6_hdr *, struct mif6 *, 13369896Smckusick struct mbuf *)); 13424983Sjdp 13569896Smckusick/* 13619068Speter * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static, 13719068Speter * except for netstat or debugging purposes. 1381556Srgrimes */ 13997877Sjmallettstruct socket *ip6_mrouter = NULL; 1401556Srgrimesint ip6_mrouter_ver = 0; 1411556Srgrimesint ip6_mrtproto = IPPROTO_PIM; /* for netstat only */ 1421556Srgrimesstruct mrt6stat mrt6stat; 14397877Sjmallett 1441556Srgrimes#define NO_RTE_FOUND 0x1 1451556Srgrimes#define RTE_FOUND 0x2 1461556Srgrimes 1471556Srgrimesstruct mf6c *mf6ctable[MF6CTBLSIZ]; 148130999Sgadu_char n6expire[MF6CTBLSIZ]; 1491556Srgrimesstatic struct mif6 mif6table[MAXMIFS]; 1501556Srgrimes#ifdef MRT6DEBUG 1511556Srgrimesu_int mrt6debug = 0; /* debug level */ 1521556Srgrimes#define DEBUG_MFC 0x02 1531556Srgrimes#define DEBUG_FORWARD 0x04 1541556Srgrimes#define DEBUG_EXPIRE 0x08 1551556Srgrimes#define DEBUG_XMIT 0x10 1561556Srgrimes#define DEBUG_REG 0x20 1571556Srgrimes#define DEBUG_PIM 0x40 1581556Srgrimes#endif 1591556Srgrimes 1601556Srgrimesstatic void expire_upcalls __P((void *)); 1611556Srgrimes#define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */ 1621556Srgrimes#define UPCALL_EXPIRE 6 /* number of timeouts */ 1631556Srgrimes 1641556Srgrimes#ifdef INET 1651556Srgrimes#ifdef MROUTING 1661556Srgrimesextern struct socket *ip_mrouter; 1671556Srgrimes#endif 1681556Srgrimes#endif 1691556Srgrimes 1701556Srgrimes/* 1711556Srgrimes * 'Interfaces' associated with decapsulator (so we can tell 1721556Srgrimes * packets that went through it from ones that get reflected 1731556Srgrimes * by a broken gateway). Different from IPv4 register_if, 1741556Srgrimes * these interfaces are linked into the system ifnet list, 1751556Srgrimes * because per-interface IPv6 statistics are maintained in 17690110Simp * ifp->if_afdata. But it does not have any routes point 1771556Srgrimes * to them. I.e., packets can't be sent this way. They 1781556Srgrimes * only exist as a placeholder for multicast source 1791556Srgrimes * verification. 1801556Srgrimes */ 181130999Sgadstatic struct ifnet *multicast_register_if6; 182130975Sgad 183130975Sgad#define ENCAP_HOPS 64 184130975Sgad 1851556Srgrimes/* 1861556Srgrimes * Private variables. 1871556Srgrimes */ 18890110Simpstatic mifi_t nummifs = 0; 1891556Srgrimesstatic mifi_t reg_mif_num = (mifi_t)-1; 1901556Srgrimes 19136352Sstevestatic struct pim6stat pim6stat; 1921556Srgrimesstatic int pim6; 1931556Srgrimes 19469896Smckusick/* 1951556Srgrimes * Hash function for a source, group entry 1961556Srgrimes */ 1971556Srgrimes#define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \ 19890110Simp (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \ 1991556Srgrimes (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \ 200172207Sjeff (g).s6_addr32[2] ^ (g).s6_addr32[3]) 2011556Srgrimes 2021556Srgrimes/* 2031556Srgrimes * Find a route for a given origin IPv6 address and Multicast group address. 2041556Srgrimes * Quality of service parameter to be added in the future!!! 2051556Srgrimes */ 20669896Smckusick 20783366Sjulian#define MF6CFIND(o, g, rt) do { \ 2081556Srgrimes struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \ 2091556Srgrimes rt = NULL; \ 21069896Smckusick mrt6stat.mrt6s_mfc_lookups++; \ 2111556Srgrimes while (_rt) { \ 2121556Srgrimes if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \ 2131556Srgrimes IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \ 2141556Srgrimes (_rt->mf6c_stall == NULL)) { \ 2151556Srgrimes rt = _rt; \ 2161556Srgrimes break; \ 21783366Sjulian } \ 21869896Smckusick _rt = _rt->mf6c_next; \ 2191556Srgrimes } \ 2201556Srgrimes if (rt == NULL) { \ 2211556Srgrimes mrt6stat.mrt6s_mfc_misses++; \ 2221556Srgrimes } \ 2231556Srgrimes} while (/*CONSTCOND*/ 0) 2241556Srgrimes 2251556Srgrimes/* 2261556Srgrimes * Macros to compute elapsed time efficiently 2271556Srgrimes * Borrowed from Van Jacobson's scheduling code 22865557Sjasone */ 22965557Sjasone#define TV_DELTA(a, b, delta) do { \ 23065557Sjasone int xxs; \ 23165557Sjasone \ 232104388Sjhb delta = (a).tv_usec - (b).tv_usec; \ 233104388Sjhb if ((xxs = (a).tv_sec - (b).tv_sec)) { \ 23465557Sjasone switch (xxs) { \ 23565557Sjasone case 2: \ 2361556Srgrimes delta += 1000000; \ 2371556Srgrimes /* FALLTHROUGH */ \ 2381556Srgrimes case 1: \ 2391556Srgrimes delta += 1000000; \ 2401556Srgrimes break; \ 2411556Srgrimes default: \ 2421556Srgrimes delta += (1000000 * xxs); \ 2431556Srgrimes } \ 244172207Sjeff } \ 2451556Srgrimes} while (/*CONSTCOND*/ 0) 24669896Smckusick 2471556Srgrimes#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \ 24869896Smckusick (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec) 2491556Srgrimes 2501556Srgrimes#ifdef UPCALL_TIMING 2511556Srgrimes#define UPCALL_MAX 50 25269896Smckusicku_long upcall_data[UPCALL_MAX + 1]; 2531556Srgrimesstatic void collate(); 2541556Srgrimes#endif /* UPCALL_TIMING */ 2551556Srgrimes 25669896Smckusickstatic int get_sg_cnt __P((struct sioc_sg_req6 *)); 2571556Srgrimesstatic int get_mif6_cnt __P((struct sioc_mif_req6 *)); 25869896Smckusickstatic int ip6_mrouter_init __P((struct socket *, int, int)); 2591556Srgrimesstatic int add_m6if __P((struct mif6ctl *)); 26069896Smckusickstatic int del_m6if __P((mifi_t *)); 2611556Srgrimesstatic int add_m6fc __P((struct mf6cctl *)); 26246155Sphkstatic int del_m6fc __P((struct mf6cctl *)); 26346155Sphk 2641556Srgrimesstatic struct callout expire_upcalls_ch; 2651556Srgrimes 2661556Srgrimes/* 2671556Srgrimes * Handle MRT setsockopt commands to modify the multicast routing tables. 268130974Sgad */ 269130974Sgadint 2701556Srgrimesip6_mrouter_set(so, sopt) 27190110Simp struct socket *so; 2721556Srgrimes struct sockopt *sopt; 2731556Srgrimes{ 2741556Srgrimes int error = 0; 2751556Srgrimes int optval; 276130974Sgad struct mif6ctl mifc; 2771556Srgrimes struct mf6cctl mfcc; 2781556Srgrimes mifi_t mifi; 2791556Srgrimes 280130974Sgad if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT) 281130974Sgad return (EACCES); 282130974Sgad 283130974Sgad switch (sopt->sopt_name) { 284130974Sgad case MRT6_INIT: 285130974Sgad#ifdef MRT6_OINIT 286130974Sgad case MRT6_OINIT: 287130974Sgad#endif 288130974Sgad error = sooptcopyin(sopt, &optval, sizeof(optval), 289130974Sgad sizeof(optval)); 29090110Simp if (error) 2911556Srgrimes break; 2921556Srgrimes error = ip6_mrouter_init(so, optval, sopt->sopt_name); 2931556Srgrimes break; 2941556Srgrimes case MRT6_DONE: 29597961Sjmallett error = ip6_mrouter_done(); 2961556Srgrimes break; 2971556Srgrimes case MRT6_ADD_MIF: 29825271Sjkh error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc)); 29990110Simp if (error) 30025271Sjkh break; 301113485Scharnier error = add_m6if(&mifc); 30225271Sjkh break; 30325271Sjkh case MRT6_ADD_MFC: 3041556Srgrimes error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc)); 30597961Sjmallett if (error) 30697961Sjmallett break; 30797961Sjmallett error = add_m6fc(&mfcc); 30897961Sjmallett break; 30997961Sjmallett case MRT6_DEL_MFC: 31097961Sjmallett error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc)); 31197961Sjmallett if (error) 31297961Sjmallett break; 31397961Sjmallett error = del_m6fc(&mfcc); 31497961Sjmallett break; 31597961Sjmallett case MRT6_DEL_MIF: 31697961Sjmallett error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi)); 31797961Sjmallett if (error) 31897961Sjmallett break; 31997961Sjmallett error = del_m6if(&mifi); 32090110Simp break; 3211556Srgrimes case MRT6_PIM: 3221556Srgrimes error = sooptcopyin(sopt, &optval, sizeof(optval), 3231556Srgrimes sizeof(optval)); 3241556Srgrimes if (error) 32597961Sjmallett break; 3261556Srgrimes error = set_pim6(&optval); 3271556Srgrimes break; 32825271Sjkh default: 32990110Simp error = EOPNOTSUPP; 33025271Sjkh break; 331113485Scharnier } 33225271Sjkh 33325271Sjkh return (error); 334103422Sjmallett} 3351556Srgrimes 33690110Simp/* 3371556Srgrimes * Handle MRT getsockopt commands 3381556Srgrimes */ 3391556Srgrimesint 3401556Srgrimesip6_mrouter_get(so, sopt) 3411556Srgrimes struct socket *so; 3421556Srgrimes struct sockopt *sopt; 34369896Smckusick{ 3441556Srgrimes int error = 0; 3451556Srgrimes 3461556Srgrimes if (so != ip6_mrouter) 3471556Srgrimes return (EACCES); 3481556Srgrimes 3491556Srgrimes switch (sopt->sopt_name) { 3501556Srgrimes case MRT6_PIM: 3511556Srgrimes error = sooptcopyout(sopt, &pim6, sizeof(pim6)); 3521556Srgrimes break; 3531556Srgrimes } 35490110Simp return (error); 3551556Srgrimes} 3561556Srgrimes 3571556Srgrimes/* 3581556Srgrimes * Handle ioctl commands to obtain information from the cache 3591556Srgrimes */ 3601556Srgrimesint 36169896Smckusickmrt6_ioctl(cmd, data) 3621556Srgrimes int cmd; 363113485Scharnier caddr_t data; 3641556Srgrimes{ 3653301Sache switch (cmd) { 3663301Sache case SIOCGETSGCNT_IN6: 3671556Srgrimes return (get_sg_cnt((struct sioc_sg_req6 *)data)); 368155876Scognet case SIOCGETMIFCNT_IN6: 369155876Scognet return (get_mif6_cnt((struct sioc_mif_req6 *)data)); 370113485Scharnier default: 371113485Scharnier return (EINVAL); 3721556Srgrimes } 3731556Srgrimes} 3741556Srgrimes 3751556Srgrimes/* 37690110Simp * returns the packet, byte, rpf-failure count for the source group provided 3771556Srgrimes */ 3781556Srgrimesstatic int 3791556Srgrimesget_sg_cnt(req) 3801556Srgrimes struct sioc_sg_req6 *req; 3811556Srgrimes{ 3821556Srgrimes struct mf6c *rt; 38369896Smckusick int s; 3841556Srgrimes 3851556Srgrimes s = splnet(); 3861556Srgrimes MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt); 3871556Srgrimes splx(s); 3881556Srgrimes if (rt != NULL) { 3891556Srgrimes req->pktcnt = rt->mf6c_pkt_cnt; 3901556Srgrimes req->bytecnt = rt->mf6c_byte_cnt; 39190110Simp req->wrong_if = rt->mf6c_wrong_if; 3921556Srgrimes } else 3931556Srgrimes return (ESRCH); 39437231Sbde#if 0 3951556Srgrimes req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; 396113485Scharnier#endif 3971556Srgrimes 3981556Srgrimes return (0); 3991556Srgrimes} 40069896Smckusick 4011556Srgrimes/* 4021556Srgrimes * returns the input and output packet and byte counts on the mif provided 4031556Srgrimes */ 40473369Sachestatic int 40573369Sacheget_mif6_cnt(req) 40669896Smckusick struct sioc_mif_req6 *req; 40737231Sbde{ 40869896Smckusick mifi_t mifi = req->mifi; 409113485Scharnier 410113485Scharnier if (mifi >= nummifs) 41169896Smckusick return (EINVAL); 412113485Scharnier 413113485Scharnier req->icount = mif6table[mifi].m6_pkt_in; 4141556Srgrimes req->ocount = mif6table[mifi].m6_pkt_out; 415113485Scharnier req->ibytes = mif6table[mifi].m6_bytes_in; 4161556Srgrimes req->obytes = mif6table[mifi].m6_bytes_out; 4171556Srgrimes 4181556Srgrimes return (0); 4191556Srgrimes} 42090110Simp 4211556Srgrimesstatic int 4221556Srgrimesset_pim6(i) 42337231Sbde int *i; 4241556Srgrimes{ 4251556Srgrimes if ((*i != 1) && (*i != 0)) 4261556Srgrimes return (EINVAL); 42769896Smckusick 4281556Srgrimes pim6 = *i; 4291556Srgrimes 4301556Srgrimes return (0); 43169896Smckusick} 432113485Scharnier 4331556Srgrimes/* 4341556Srgrimes * Enable multicast routing 4351556Srgrimes */ 4361556Srgrimesstatic int 437104388Sjhbip6_mrouter_init(so, v, cmd) 43869372Sjhb struct socket *so; 43969372Sjhb int v; 44069372Sjhb int cmd; 44169372Sjhb{ 442104388Sjhb#ifdef MRT6DEBUG 443104388Sjhb if (mrt6debug) 44469372Sjhb log(LOG_DEBUG, 445113485Scharnier "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n", 44669372Sjhb so->so_type, so->so_proto->pr_protocol); 44769372Sjhb#endif 44869372Sjhb 44969372Sjhb if (so->so_type != SOCK_RAW || 45069372Sjhb so->so_proto->pr_protocol != IPPROTO_ICMPV6) 45169372Sjhb return (EOPNOTSUPP); 45269372Sjhb 45390110Simp if (v != 1) 4541556Srgrimes return (ENOPROTOOPT); 4551556Srgrimes 4561556Srgrimes if (ip6_mrouter != NULL) 4571556Srgrimes return (EADDRINUSE); 45869896Smckusick 45969896Smckusick ip6_mrouter = so; 4608855Srgrimes ip6_mrouter_ver = cmd; 461113485Scharnier 4621556Srgrimes bzero((caddr_t)mf6ctable, sizeof(mf6ctable)); 46339160Sdfr bzero((caddr_t)n6expire, sizeof(n6expire)); 46482268Speter 465113485Scharnier pim6 = 0;/* used for stubbing out/in pim stuff */ 46691028Sdillon 46791028Sdillon callout_init(&expire_upcalls_ch, 0); 46891028Sdillon callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, 46991028Sdillon expire_upcalls, NULL); 470118857Sharti 471118857Sharti#ifdef MRT6DEBUG 472118857Sharti if (mrt6debug) 473118857Sharti log(LOG_DEBUG, "ip6_mrouter_init\n"); 474118857Sharti#endif 475118857Sharti 476118857Sharti return (0); 477118857Sharti} 478118857Sharti 479118857Sharti/* 480118857Sharti * Disable multicast routing 481118857Sharti */ 482118857Shartiint 48391028Sdillonip6_mrouter_done() 48491028Sdillon{ 48591028Sdillon mifi_t mifi; 48691028Sdillon int i; 48791028Sdillon struct mf6c *rt; 48891028Sdillon struct rtdetq *rte; 48991028Sdillon int s; 49091028Sdillon 491113485Scharnier s = splnet(); 49291028Sdillon 49391028Sdillon /* 49491028Sdillon * For each phyint in use, disable promiscuous reception of all IPv6 495104388Sjhb * multicasts. 496104388Sjhb */ 49791028Sdillon#ifdef INET 498104388Sjhb#ifdef MROUTING 499113485Scharnier /* 50091028Sdillon * If there is still IPv4 multicast routing daemon, 501113485Scharnier * we remain interfaces to receive all muliticasted packets. 5021556Srgrimes * XXX: there may be an interface in which the IPv4 multicast 5031556Srgrimes * daemon is not interested... 5041556Srgrimes */ 5051556Srgrimes if (!ip_mrouter) 50690110Simp#endif 5071556Srgrimes#endif 5081556Srgrimes { 5091556Srgrimes for (mifi = 0; mifi < nummifs; mifi++) { 5101556Srgrimes if (mif6table[mifi].m6_ifp && 51199785Sbde !(mif6table[mifi].m6_flags & MIFF_REGISTER)) { 5121556Srgrimes if_allmulti(mif6table[mifi].m6_ifp, 0); 5131556Srgrimes } 5141556Srgrimes } 51590110Simp } 5161556Srgrimes#ifdef notyet 5171556Srgrimes bzero((caddr_t)qtable, sizeof(qtable)); 5181556Srgrimes bzero((caddr_t)tbftable, sizeof(tbftable)); 5191556Srgrimes#endif 5201556Srgrimes bzero((caddr_t)mif6table, sizeof(mif6table)); 521113485Scharnier nummifs = 0; 5221556Srgrimes 523113485Scharnier pim6 = 0; /* used to stub out/in pim specific code */ 52472343Sache 5251556Srgrimes callout_stop(&expire_upcalls_ch); 526130856Sgad 52736497Sbde /* 52836497Sbde * Free all multicast forwarding cache entries. 5291556Srgrimes */ 5301556Srgrimes for (i = 0; i < MF6CTBLSIZ; i++) { 5311556Srgrimes rt = mf6ctable[i]; 5321556Srgrimes while (rt) { 5331556Srgrimes struct mf6c *frt; 5341556Srgrimes 53569896Smckusick for (rte = rt->mf6c_stall; rte != NULL; ) { 53669896Smckusick struct rtdetq *n = rte->next; 5371556Srgrimes 53869896Smckusick m_free(rte->m); 53969896Smckusick free(rte, M_MRTABLE6); 5401556Srgrimes rte = n; 54136497Sbde } 54236497Sbde frt = rt; 54336497Sbde rt = rt->mf6c_next; 54436497Sbde free(frt, M_MRTABLE6); 54536497Sbde } 54636497Sbde } 5471556Srgrimes 548113485Scharnier bzero((caddr_t)mf6ctable, sizeof(mf6ctable)); 549113485Scharnier 5501556Srgrimes /* 5511556Srgrimes * Reset register interface 5521556Srgrimes */ 55397965Sjmallett if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) { 55497965Sjmallett if_detach(multicast_register_if6); 55597965Sjmallett if_free(multicast_register_if6); 55697965Sjmallett reg_mif_num = (mifi_t)-1; 557113485Scharnier multicast_register_if6 = NULL; 558113485Scharnier } 55997965Sjmallett 56097965Sjmallett ip6_mrouter = NULL; 56197965Sjmallett ip6_mrouter_ver = 0; 562113485Scharnier 563113485Scharnier splx(s); 564113485Scharnier 565113485Scharnier#ifdef MRT6DEBUG 566113485Scharnier if (mrt6debug) 567113485Scharnier log(LOG_DEBUG, "ip6_mrouter_done\n"); 568113485Scharnier#endif 569113485Scharnier 570113485Scharnier return (0); 571113485Scharnier} 572113485Scharnier 573113485Scharnierstatic struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 }; 574113485Scharnier 575113485Scharnier/* 576113485Scharnier * Add a mif to the mif table 57797965Sjmallett */ 57897965Sjmallettstatic int 57997965Sjmallettadd_m6if(mifcp) 5801556Srgrimes struct mif6ctl *mifcp; 58190143Smarkm{ 5821556Srgrimes struct mif6 *mifp; 5831556Srgrimes struct ifnet *ifp; 5841556Srgrimes int error, s; 5851556Srgrimes#ifdef notyet 5861556Srgrimes struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi; 5871556Srgrimes#endif 5881556Srgrimes 5891556Srgrimes if (mifcp->mif6c_mifi >= MAXMIFS) 5901556Srgrimes return (EINVAL); 5911556Srgrimes mifp = mif6table + mifcp->mif6c_mifi; 5921556Srgrimes if (mifp->m6_ifp) 593172207Sjeff return (EADDRINUSE); /* XXX: is it appropriate? */ 5941556Srgrimes if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index) 5951556Srgrimes return (ENXIO); 59669896Smckusick ifp = ifnet_byindex(mifcp->mif6c_pifi); 59769896Smckusick 59869896Smckusick if (mifcp->mif6c_flags & MIFF_REGISTER) { 5991556Srgrimes if (reg_mif_num == (mifi_t)-1) { 6001556Srgrimes ifp = if_alloc(IFT_OTHER); 6011556Srgrimes 60290110Simp if_initname(ifp, "register_mif", 0); 6031556Srgrimes ifp->if_flags |= IFF_LOOPBACK; 6041556Srgrimes if_attach(ifp); 6051556Srgrimes multicast_register_if6 = ifp; 6061556Srgrimes reg_mif_num = mifcp->mif6c_mifi; 6071556Srgrimes /* 6081556Srgrimes * it is impossible to guess the ifindex of the 6091556Srgrimes * register interface. So mif6c_pifi is automatically 61090143Smarkm * calculated. 61190110Simp */ 6121556Srgrimes mifcp->mif6c_pifi = ifp->if_index; 6131556Srgrimes } else { 6141556Srgrimes ifp = multicast_register_if6; 6151556Srgrimes } 6161556Srgrimes 6171556Srgrimes } /* if REGISTER */ 6181556Srgrimes else { 6191556Srgrimes /* Make sure the interface supports multicast */ 6201556Srgrimes if ((ifp->if_flags & IFF_MULTICAST) == 0) 621172207Sjeff return (EOPNOTSUPP); 6221556Srgrimes 6231556Srgrimes s = splnet(); 6241556Srgrimes error = if_allmulti(ifp, 1); 625113485Scharnier splx(s); 6261556Srgrimes if (error) 6271556Srgrimes return (error); 6281556Srgrimes } 6291556Srgrimes 63090110Simp s = splnet(); 6311556Srgrimes mifp->m6_flags = mifcp->mif6c_flags; 6321556Srgrimes mifp->m6_ifp = ifp; 6331556Srgrimes#ifdef notyet 6341556Srgrimes /* scaling up here allows division by 1024 in critical code */ 6351556Srgrimes mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000; 6361556Srgrimes#endif 6371556Srgrimes /* initialize per mif pkt counters */ 6381556Srgrimes mifp->m6_pkt_in = 0; 63990110Simp mifp->m6_pkt_out = 0; 6401556Srgrimes mifp->m6_bytes_in = 0; 6411556Srgrimes mifp->m6_bytes_out = 0; 6421556Srgrimes splx(s); 6431556Srgrimes 6448855Srgrimes /* Adjust nummifs up if the mifi is higher than nummifs */ 64569896Smckusick if (nummifs <= mifcp->mif6c_mifi) 6461556Srgrimes nummifs = mifcp->mif6c_mifi + 1; 6471556Srgrimes 64890143Smarkm#ifdef MRT6DEBUG 6491556Srgrimes if (mrt6debug) 65090143Smarkm log(LOG_DEBUG, 6511556Srgrimes "add_mif #%d, phyint %s\n", 6521556Srgrimes mifcp->mif6c_mifi, 6531556Srgrimes ifp->if_xname); 6541556Srgrimes#endif 65527856Speter 65627856Speter return (0); 6571556Srgrimes} 6581556Srgrimes 6591556Srgrimes/* 66090110Simp * Delete a mif from the mif table 66136352Ssteve */ 66236352Sstevestatic int 66390143Smarkmdel_m6if(mifip) 66436352Ssteve mifi_t *mifip; 66572377Sjake{ 666127512Sgad struct mif6 *mifp = mif6table + *mifip; 66736352Ssteve mifi_t mifi; 668127512Sgad struct ifnet *ifp; 66990143Smarkm int s; 67090143Smarkm 67172377Sjake if (*mifip >= nummifs) 672131215Sgad return (EINVAL); 673131215Sgad if (mifp->m6_ifp == NULL) 674131215Sgad return (EINVAL); 67572377Sjake 67672377Sjake s = splnet(); 67736352Ssteve 67872377Sjake if (!(mifp->m6_flags & MIFF_REGISTER)) { 67936352Ssteve /* 68036352Ssteve * XXX: what if there is yet IPv4 multicast daemon 68172377Sjake * using the interface? 68272377Sjake */ 68336352Ssteve ifp = mifp->m6_ifp; 68436352Ssteve 68572377Sjake if_allmulti(ifp, 0); 68636352Ssteve } else { 68736352Ssteve if (reg_mif_num != (mifi_t)-1 && 68836352Ssteve multicast_register_if6 != NULL) { 68936352Ssteve if_detach(multicast_register_if6); 69036352Ssteve if_free(multicast_register_if6); 69136352Ssteve reg_mif_num = (mifi_t)-1; 6921556Srgrimes multicast_register_if6 = NULL; 6931556Srgrimes } 6941556Srgrimes } 6951556Srgrimes 6961556Srgrimes#ifdef notyet 69799547Sjmallett bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip])); 6981556Srgrimes bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf))); 6991556Srgrimes#endif 70097843Sjmallett bzero((caddr_t)mifp, sizeof(*mifp)); 70197843Sjmallett 7021556Srgrimes /* Adjust nummifs down */ 7031556Srgrimes for (mifi = nummifs; mifi > 0; mifi--) 7041556Srgrimes if (mif6table[mifi - 1].m6_ifp) 7051556Srgrimes break; 7061556Srgrimes nummifs = mifi; 7071556Srgrimes 7087165Sjoerg splx(s); 7091556Srgrimes 710113395Stjr#ifdef MRT6DEBUG 711113395Stjr if (mrt6debug) 7121556Srgrimes log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs); 7131556Srgrimes#endif 7141556Srgrimes 7151556Srgrimes return (0); 7161556Srgrimes} 7171556Srgrimes 7181556Srgrimes/* 7191556Srgrimes * Add an mfc entry 7201556Srgrimes */ 7211556Srgrimesstatic int 7221556Srgrimesadd_m6fc(mfccp) 7231556Srgrimes struct mf6cctl *mfccp; 7241556Srgrimes{ 72539160Sdfr struct mf6c *rt; 72639160Sdfr u_long hash; 72739160Sdfr struct rtdetq *rte; 72839160Sdfr u_short nstl; 729113395Stjr int s; 73039160Sdfr 7311556Srgrimes MF6CFIND(mfccp->mf6cc_origin.sin6_addr, 7321556Srgrimes mfccp->mf6cc_mcastgrp.sin6_addr, rt); 7331556Srgrimes 7341556Srgrimes /* If an entry already exists, just update the fields */ 7351556Srgrimes if (rt) { 7361556Srgrimes#ifdef MRT6DEBUG 7371556Srgrimes if (mrt6debug & DEBUG_MFC) 73882268Speter log(LOG_DEBUG, 7391556Srgrimes "add_m6fc no upcall h %d o %s g %s p %x\n", 740103497Sjmallett ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), 741103438Sjmallett ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), 742103438Sjmallett mfccp->mf6cc_parent); 7431556Srgrimes#endif 7441556Srgrimes 7451556Srgrimes s = splnet(); 7461556Srgrimes rt->mf6c_parent = mfccp->mf6cc_parent; 7471556Srgrimes rt->mf6c_ifset = mfccp->mf6cc_ifset; 7481556Srgrimes splx(s); 74990110Simp return (0); 7501556Srgrimes } 7511556Srgrimes 7521556Srgrimes /* 7531556Srgrimes * Find the entry for which the upcall was made and update 75469896Smckusick */ 7551556Srgrimes s = splnet(); 7561556Srgrimes hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr, 7571556Srgrimes mfccp->mf6cc_mcastgrp.sin6_addr); 75890110Simp for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) { 7591556Srgrimes if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr, 7601556Srgrimes &mfccp->mf6cc_origin.sin6_addr) && 7611556Srgrimes IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr, 7621556Srgrimes &mfccp->mf6cc_mcastgrp.sin6_addr) && 76369896Smckusick (rt->mf6c_stall != NULL)) { 76469896Smckusick 7651556Srgrimes if (nstl++) 7661556Srgrimes log(LOG_ERR, 7671556Srgrimes "add_m6fc: %s o %s g %s p %x dbx %p\n", 76886922Sgreen "multiple kernel entries", 76986922Sgreen ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), 770130830Sgad ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), 771130830Sgad mfccp->mf6cc_parent, rt->mf6c_stall); 772130830Sgad 773130830Sgad#ifdef MRT6DEBUG 774130830Sgad if (mrt6debug & DEBUG_MFC) 775130830Sgad log(LOG_DEBUG, 776130830Sgad "add_m6fc o %s g %s p %x dbg %x\n", 777130830Sgad ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), 778130830Sgad ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), 779105831Srwatson mfccp->mf6cc_parent, rt->mf6c_stall); 78086922Sgreen#endif 781105831Srwatson 782113485Scharnier rt->mf6c_origin = mfccp->mf6cc_origin; 783109460Sjmallett rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; 784105831Srwatson rt->mf6c_parent = mfccp->mf6cc_parent; 78586922Sgreen rt->mf6c_ifset = mfccp->mf6cc_ifset; 78686922Sgreen /* initialize pkt counters per src-grp */ 787105831Srwatson rt->mf6c_pkt_cnt = 0; 788109460Sjmallett rt->mf6c_byte_cnt = 0; 789132433Stjr rt->mf6c_wrong_if = 0; 790105831Srwatson 791105831Srwatson rt->mf6c_expire = 0; /* Don't clean this guy up */ 792109460Sjmallett n6expire[hash]--; 793105831Srwatson 794109460Sjmallett /* free packets Qed at the end of this entry */ 795105831Srwatson for (rte = rt->mf6c_stall; rte != NULL; ) { 796105831Srwatson struct rtdetq *n = rte->next; 797109460Sjmallett ip6_mdq(rte->m, rte->ifp, rt); 798105831Srwatson m_freem(rte->m); 799105831Srwatson#ifdef UPCALL_TIMING 800105831Srwatson collate(&(rte->t)); 801105831Srwatson#endif /* UPCALL_TIMING */ 802105831Srwatson free(rte, M_MRTABLE6); 803130828Sgad rte = n; 804105831Srwatson } 80586922Sgreen rt->mf6c_stall = NULL; 806105831Srwatson } 807105831Srwatson } 808105831Srwatson 809105831Srwatson /* 810105831Srwatson * It is possible that an entry is being inserted without an upcall 811109460Sjmallett */ 812105831Srwatson if (nstl == 0) { 813105831Srwatson#ifdef MRT6DEBUG 814109460Sjmallett if (mrt6debug & DEBUG_MFC) 815132433Stjr log(LOG_DEBUG, 816105831Srwatson "add_mfc no upcall h %d o %s g %s p %x\n", 817105831Srwatson hash, 818109460Sjmallett ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), 819109460Sjmallett ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), 820105831Srwatson mfccp->mf6cc_parent); 821105831Srwatson#endif 822105831Srwatson 823109460Sjmallett for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) { 824105831Srwatson 825105831Srwatson if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr, 826 &mfccp->mf6cc_origin.sin6_addr)&& 827 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr, 828 &mfccp->mf6cc_mcastgrp.sin6_addr)) { 829 830 rt->mf6c_origin = mfccp->mf6cc_origin; 831 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; 832 rt->mf6c_parent = mfccp->mf6cc_parent; 833 rt->mf6c_ifset = mfccp->mf6cc_ifset; 834 /* initialize pkt counters per src-grp */ 835 rt->mf6c_pkt_cnt = 0; 836 rt->mf6c_byte_cnt = 0; 837 rt->mf6c_wrong_if = 0; 838 839 if (rt->mf6c_expire) 840 n6expire[hash]--; 841 rt->mf6c_expire = 0; 842 } 843 } 844 if (rt == NULL) { 845 /* no upcall, so make a new entry */ 846 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6, 847 M_NOWAIT); 848 if (rt == NULL) { 849 splx(s); 850 return (ENOBUFS); 851 } 852 853 /* insert new entry at head of hash chain */ 854 rt->mf6c_origin = mfccp->mf6cc_origin; 855 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; 856 rt->mf6c_parent = mfccp->mf6cc_parent; 857 rt->mf6c_ifset = mfccp->mf6cc_ifset; 858 /* initialize pkt counters per src-grp */ 859 rt->mf6c_pkt_cnt = 0; 860 rt->mf6c_byte_cnt = 0; 861 rt->mf6c_wrong_if = 0; 862 rt->mf6c_expire = 0; 863 rt->mf6c_stall = NULL; 864 865 /* link into table */ 866 rt->mf6c_next = mf6ctable[hash]; 867 mf6ctable[hash] = rt; 868 } 869 } 870 splx(s); 871 return (0); 872} 873 874#ifdef UPCALL_TIMING 875/* 876 * collect delay statistics on the upcalls 877 */ 878static void 879collate(t) 880 struct timeval *t; 881{ 882 u_long d; 883 struct timeval tp; 884 u_long delta; 885 886 GET_TIME(tp); 887 888 if (TV_LT(*t, tp)) 889 { 890 TV_DELTA(tp, *t, delta); 891 892 d = delta >> 10; 893 if (d > UPCALL_MAX) 894 d = UPCALL_MAX; 895 896 ++upcall_data[d]; 897 } 898} 899#endif /* UPCALL_TIMING */ 900 901/* 902 * Delete an mfc entry 903 */ 904static int 905del_m6fc(mfccp) 906 struct mf6cctl *mfccp; 907{ 908 struct sockaddr_in6 origin; 909 struct sockaddr_in6 mcastgrp; 910 struct mf6c *rt; 911 struct mf6c **nptr; 912 u_long hash; 913 int s; 914 915 origin = mfccp->mf6cc_origin; 916 mcastgrp = mfccp->mf6cc_mcastgrp; 917 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr); 918 919#ifdef MRT6DEBUG 920 if (mrt6debug & DEBUG_MFC) 921 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n", 922 ip6_sprintf(&origin.sin6_addr), 923 ip6_sprintf(&mcastgrp.sin6_addr)); 924#endif 925 926 s = splnet(); 927 928 nptr = &mf6ctable[hash]; 929 while ((rt = *nptr) != NULL) { 930 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr, 931 &rt->mf6c_origin.sin6_addr) && 932 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr, 933 &rt->mf6c_mcastgrp.sin6_addr) && 934 rt->mf6c_stall == NULL) 935 break; 936 937 nptr = &rt->mf6c_next; 938 } 939 if (rt == NULL) { 940 splx(s); 941 return (EADDRNOTAVAIL); 942 } 943 944 *nptr = rt->mf6c_next; 945 free(rt, M_MRTABLE6); 946 947 splx(s); 948 949 return (0); 950} 951 952static int 953socket_send(s, mm, src) 954 struct socket *s; 955 struct mbuf *mm; 956 struct sockaddr_in6 *src; 957{ 958 if (s) { 959 if (sbappendaddr(&s->so_rcv, 960 (struct sockaddr *)src, 961 mm, (struct mbuf *)0) != 0) { 962 sorwakeup(s); 963 return (0); 964 } 965 } 966 m_freem(mm); 967 return (-1); 968} 969 970/* 971 * IPv6 multicast forwarding function. This function assumes that the packet 972 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface 973 * pointed to by "ifp", and the packet is to be relayed to other networks 974 * that have members of the packet's destination IPv6 multicast group. 975 * 976 * The packet is returned unscathed to the caller, unless it is 977 * erroneous, in which case a non-zero return value tells the caller to 978 * discard it. 979 * 980 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff 981 * this function is called in the originating context (i.e., not when 982 * forwarding a packet from other node). ip6_output(), which is currently the 983 * only function that calls this function is called in the originating context, 984 * explicitly ensures this condition. It is caller's responsibility to ensure 985 * that if this function is called from somewhere else in the originating 986 * context in the future. 987 */ 988 989int 990ip6_mforward(ip6, ifp, m) 991 struct ip6_hdr *ip6; 992 struct ifnet *ifp; 993 struct mbuf *m; 994{ 995 struct mf6c *rt; 996 struct mif6 *mifp; 997 struct mbuf *mm; 998 int s; 999 mifi_t mifi; 1000 1001#ifdef MRT6DEBUG 1002 if (mrt6debug & DEBUG_FORWARD) 1003 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n", 1004 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst), 1005 ifp->if_index); 1006#endif 1007 1008 /* 1009 * Don't forward a packet with Hop limit of zero or one, 1010 * or a packet destined to a local-only group. 1011 */ 1012 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) || 1013 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1014 return (0); 1015 ip6->ip6_hlim--; 1016 1017 /* 1018 * Source address check: do not forward packets with unspecified 1019 * source. It was discussed in July 2000, on ipngwg mailing list. 1020 * This is rather more serious than unicast cases, because some 1021 * MLD packets can be sent with the unspecified source address 1022 * (although such packets must normally set 1 to the hop limit field). 1023 */ 1024 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 1025 ip6stat.ip6s_cantforward++; 1026 if (ip6_log_time + ip6_log_interval < time_second) { 1027 ip6_log_time = time_second; 1028 log(LOG_DEBUG, 1029 "cannot forward " 1030 "from %s to %s nxt %d received on %s\n", 1031 ip6_sprintf(&ip6->ip6_src), 1032 ip6_sprintf(&ip6->ip6_dst), 1033 ip6->ip6_nxt, 1034 if_name(m->m_pkthdr.rcvif)); 1035 } 1036 return (0); 1037 } 1038 1039 /* 1040 * Determine forwarding mifs from the forwarding cache table 1041 */ 1042 s = splnet(); 1043 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt); 1044 1045 /* Entry exists, so forward if necessary */ 1046 if (rt) { 1047 splx(s); 1048 return (ip6_mdq(m, ifp, rt)); 1049 } else { 1050 /* 1051 * If we don't have a route for packet's origin, 1052 * Make a copy of the packet & 1053 * send message to routing daemon 1054 */ 1055 1056 struct mbuf *mb0; 1057 struct rtdetq *rte; 1058 u_long hash; 1059/* int i, npkts;*/ 1060#ifdef UPCALL_TIMING 1061 struct timeval tp; 1062 1063 GET_TIME(tp); 1064#endif /* UPCALL_TIMING */ 1065 1066 mrt6stat.mrt6s_no_route++; 1067#ifdef MRT6DEBUG 1068 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC)) 1069 log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n", 1070 ip6_sprintf(&ip6->ip6_src), 1071 ip6_sprintf(&ip6->ip6_dst)); 1072#endif 1073 1074 /* 1075 * Allocate mbufs early so that we don't do extra work if we 1076 * are just going to fail anyway. 1077 */ 1078 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6, 1079 M_NOWAIT); 1080 if (rte == NULL) { 1081 splx(s); 1082 return (ENOBUFS); 1083 } 1084 mb0 = m_copy(m, 0, M_COPYALL); 1085 /* 1086 * Pullup packet header if needed before storing it, 1087 * as other references may modify it in the meantime. 1088 */ 1089 if (mb0 && 1090 (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr))) 1091 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr)); 1092 if (mb0 == NULL) { 1093 free(rte, M_MRTABLE6); 1094 splx(s); 1095 return (ENOBUFS); 1096 } 1097 1098 /* is there an upcall waiting for this packet? */ 1099 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst); 1100 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) { 1101 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, 1102 &rt->mf6c_origin.sin6_addr) && 1103 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1104 &rt->mf6c_mcastgrp.sin6_addr) && 1105 (rt->mf6c_stall != NULL)) 1106 break; 1107 } 1108 1109 if (rt == NULL) { 1110 struct mrt6msg *im; 1111#ifdef MRT6_OINIT 1112 struct omrt6msg *oim; 1113#endif 1114 1115 /* no upcall, so make a new entry */ 1116 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6, 1117 M_NOWAIT); 1118 if (rt == NULL) { 1119 free(rte, M_MRTABLE6); 1120 m_freem(mb0); 1121 splx(s); 1122 return (ENOBUFS); 1123 } 1124 /* 1125 * Make a copy of the header to send to the user 1126 * level process 1127 */ 1128 mm = m_copy(mb0, 0, sizeof(struct ip6_hdr)); 1129 1130 if (mm == NULL) { 1131 free(rte, M_MRTABLE6); 1132 m_freem(mb0); 1133 free(rt, M_MRTABLE6); 1134 splx(s); 1135 return (ENOBUFS); 1136 } 1137 1138 /* 1139 * Send message to routing daemon 1140 */ 1141 sin6.sin6_addr = ip6->ip6_src; 1142 1143 im = NULL; 1144#ifdef MRT6_OINIT 1145 oim = NULL; 1146#endif 1147 switch (ip6_mrouter_ver) { 1148#ifdef MRT6_OINIT 1149 case MRT6_OINIT: 1150 oim = mtod(mm, struct omrt6msg *); 1151 oim->im6_msgtype = MRT6MSG_NOCACHE; 1152 oim->im6_mbz = 0; 1153 break; 1154#endif 1155 case MRT6_INIT: 1156 im = mtod(mm, struct mrt6msg *); 1157 im->im6_msgtype = MRT6MSG_NOCACHE; 1158 im->im6_mbz = 0; 1159 break; 1160 default: 1161 free(rte, M_MRTABLE6); 1162 m_freem(mb0); 1163 free(rt, M_MRTABLE6); 1164 splx(s); 1165 return (EINVAL); 1166 } 1167 1168#ifdef MRT6DEBUG 1169 if (mrt6debug & DEBUG_FORWARD) 1170 log(LOG_DEBUG, 1171 "getting the iif info in the kernel\n"); 1172#endif 1173 1174 for (mifp = mif6table, mifi = 0; 1175 mifi < nummifs && mifp->m6_ifp != ifp; 1176 mifp++, mifi++) 1177 ; 1178 1179 switch (ip6_mrouter_ver) { 1180#ifdef MRT6_OINIT 1181 case MRT6_OINIT: 1182 oim->im6_mif = mifi; 1183 break; 1184#endif 1185 case MRT6_INIT: 1186 im->im6_mif = mifi; 1187 break; 1188 } 1189 1190 if (socket_send(ip6_mrouter, mm, &sin6) < 0) { 1191 log(LOG_WARNING, "ip6_mforward: ip6_mrouter " 1192 "socket queue full\n"); 1193 mrt6stat.mrt6s_upq_sockfull++; 1194 free(rte, M_MRTABLE6); 1195 m_freem(mb0); 1196 free(rt, M_MRTABLE6); 1197 splx(s); 1198 return (ENOBUFS); 1199 } 1200 1201 mrt6stat.mrt6s_upcalls++; 1202 1203 /* insert new entry at head of hash chain */ 1204 bzero(rt, sizeof(*rt)); 1205 rt->mf6c_origin.sin6_family = AF_INET6; 1206 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6); 1207 rt->mf6c_origin.sin6_addr = ip6->ip6_src; 1208 rt->mf6c_mcastgrp.sin6_family = AF_INET6; 1209 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6); 1210 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst; 1211 rt->mf6c_expire = UPCALL_EXPIRE; 1212 n6expire[hash]++; 1213 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT; 1214 1215 /* link into table */ 1216 rt->mf6c_next = mf6ctable[hash]; 1217 mf6ctable[hash] = rt; 1218 /* Add this entry to the end of the queue */ 1219 rt->mf6c_stall = rte; 1220 } else { 1221 /* determine if q has overflowed */ 1222 struct rtdetq **p; 1223 int npkts = 0; 1224 1225 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next) 1226 if (++npkts > MAX_UPQ6) { 1227 mrt6stat.mrt6s_upq_ovflw++; 1228 free(rte, M_MRTABLE6); 1229 m_freem(mb0); 1230 splx(s); 1231 return (0); 1232 } 1233 1234 /* Add this entry to the end of the queue */ 1235 *p = rte; 1236 } 1237 1238 rte->next = NULL; 1239 rte->m = mb0; 1240 rte->ifp = ifp; 1241#ifdef UPCALL_TIMING 1242 rte->t = tp; 1243#endif /* UPCALL_TIMING */ 1244 1245 splx(s); 1246 1247 return (0); 1248 } 1249} 1250 1251/* 1252 * Clean up cache entries if upcalls are not serviced 1253 * Call from the Slow Timeout mechanism, every half second. 1254 */ 1255static void 1256expire_upcalls(unused) 1257 void *unused; 1258{ 1259 struct rtdetq *rte; 1260 struct mf6c *mfc, **nptr; 1261 int i; 1262 int s; 1263 1264 s = splnet(); 1265 for (i = 0; i < MF6CTBLSIZ; i++) { 1266 if (n6expire[i] == 0) 1267 continue; 1268 nptr = &mf6ctable[i]; 1269 while ((mfc = *nptr) != NULL) { 1270 rte = mfc->mf6c_stall; 1271 /* 1272 * Skip real cache entries 1273 * Make sure it wasn't marked to not expire (shouldn't happen) 1274 * If it expires now 1275 */ 1276 if (rte != NULL && 1277 mfc->mf6c_expire != 0 && 1278 --mfc->mf6c_expire == 0) { 1279#ifdef MRT6DEBUG 1280 if (mrt6debug & DEBUG_EXPIRE) 1281 log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n", 1282 ip6_sprintf(&mfc->mf6c_origin.sin6_addr), 1283 ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr)); 1284#endif 1285 /* 1286 * drop all the packets 1287 * free the mbuf with the pkt, if, timing info 1288 */ 1289 do { 1290 struct rtdetq *n = rte->next; 1291 m_freem(rte->m); 1292 free(rte, M_MRTABLE6); 1293 rte = n; 1294 } while (rte != NULL); 1295 mrt6stat.mrt6s_cache_cleanups++; 1296 n6expire[i]--; 1297 1298 *nptr = mfc->mf6c_next; 1299 free(mfc, M_MRTABLE6); 1300 } else { 1301 nptr = &mfc->mf6c_next; 1302 } 1303 } 1304 } 1305 splx(s); 1306 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, 1307 expire_upcalls, NULL); 1308} 1309 1310/* 1311 * Packet forwarding routine once entry in the cache is made 1312 */ 1313static int 1314ip6_mdq(m, ifp, rt) 1315 struct mbuf *m; 1316 struct ifnet *ifp; 1317 struct mf6c *rt; 1318{ 1319 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1320 mifi_t mifi, iif; 1321 struct mif6 *mifp; 1322 int plen = m->m_pkthdr.len; 1323 struct in6_addr src0, dst0; /* copies for local work */ 1324 u_int32_t iszone, idzone, oszone, odzone; 1325 int error = 0; 1326 1327/* 1328 * Macro to send packet on mif. Since RSVP packets don't get counted on 1329 * input, they shouldn't get counted on output, so statistics keeping is 1330 * separate. 1331 */ 1332 1333#define MC6_SEND(ip6, mifp, m) do { \ 1334 if ((mifp)->m6_flags & MIFF_REGISTER) \ 1335 register_send((ip6), (mifp), (m)); \ 1336 else \ 1337 phyint_send((ip6), (mifp), (m)); \ 1338} while (/*CONSTCOND*/ 0) 1339 1340 /* 1341 * Don't forward if it didn't arrive from the parent mif 1342 * for its origin. 1343 */ 1344 mifi = rt->mf6c_parent; 1345 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) { 1346 /* came in the wrong interface */ 1347#ifdef MRT6DEBUG 1348 if (mrt6debug & DEBUG_FORWARD) 1349 log(LOG_DEBUG, 1350 "wrong if: ifid %d mifi %d mififid %x\n", 1351 ifp->if_index, mifi, 1352 mif6table[mifi].m6_ifp->if_index); 1353#endif 1354 mrt6stat.mrt6s_wrong_if++; 1355 rt->mf6c_wrong_if++; 1356 /* 1357 * If we are doing PIM processing, and we are forwarding 1358 * packets on this interface, send a message to the 1359 * routing daemon. 1360 */ 1361 /* have to make sure this is a valid mif */ 1362 if (mifi < nummifs && mif6table[mifi].m6_ifp) 1363 if (pim6 && (m->m_flags & M_LOOP) == 0) { 1364 /* 1365 * Check the M_LOOP flag to avoid an 1366 * unnecessary PIM assert. 1367 * XXX: M_LOOP is an ad-hoc hack... 1368 */ 1369 static struct sockaddr_in6 sin6 = 1370 { sizeof(sin6), AF_INET6 }; 1371 1372 struct mbuf *mm; 1373 struct mrt6msg *im; 1374#ifdef MRT6_OINIT 1375 struct omrt6msg *oim; 1376#endif 1377 1378 mm = m_copy(m, 0, sizeof(struct ip6_hdr)); 1379 if (mm && 1380 (M_HASCL(mm) || 1381 mm->m_len < sizeof(struct ip6_hdr))) 1382 mm = m_pullup(mm, sizeof(struct ip6_hdr)); 1383 if (mm == NULL) 1384 return (ENOBUFS); 1385 1386#ifdef MRT6_OINIT 1387 oim = NULL; 1388#endif 1389 im = NULL; 1390 switch (ip6_mrouter_ver) { 1391#ifdef MRT6_OINIT 1392 case MRT6_OINIT: 1393 oim = mtod(mm, struct omrt6msg *); 1394 oim->im6_msgtype = MRT6MSG_WRONGMIF; 1395 oim->im6_mbz = 0; 1396 break; 1397#endif 1398 case MRT6_INIT: 1399 im = mtod(mm, struct mrt6msg *); 1400 im->im6_msgtype = MRT6MSG_WRONGMIF; 1401 im->im6_mbz = 0; 1402 break; 1403 default: 1404 m_freem(mm); 1405 return (EINVAL); 1406 } 1407 1408 for (mifp = mif6table, iif = 0; 1409 iif < nummifs && mifp && 1410 mifp->m6_ifp != ifp; 1411 mifp++, iif++) 1412 ; 1413 1414 switch (ip6_mrouter_ver) { 1415#ifdef MRT6_OINIT 1416 case MRT6_OINIT: 1417 oim->im6_mif = iif; 1418 sin6.sin6_addr = oim->im6_src; 1419 break; 1420#endif 1421 case MRT6_INIT: 1422 im->im6_mif = iif; 1423 sin6.sin6_addr = im->im6_src; 1424 break; 1425 } 1426 1427 mrt6stat.mrt6s_upcalls++; 1428 1429 if (socket_send(ip6_mrouter, mm, &sin6) < 0) { 1430#ifdef MRT6DEBUG 1431 if (mrt6debug) 1432 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n"); 1433#endif 1434 ++mrt6stat.mrt6s_upq_sockfull; 1435 return (ENOBUFS); 1436 } /* if socket Q full */ 1437 } /* if PIM */ 1438 return (0); 1439 } /* if wrong iif */ 1440 1441 /* If I sourced this packet, it counts as output, else it was input. */ 1442 if (m->m_pkthdr.rcvif == NULL) { 1443 /* XXX: is rcvif really NULL when output?? */ 1444 mif6table[mifi].m6_pkt_out++; 1445 mif6table[mifi].m6_bytes_out += plen; 1446 } else { 1447 mif6table[mifi].m6_pkt_in++; 1448 mif6table[mifi].m6_bytes_in += plen; 1449 } 1450 rt->mf6c_pkt_cnt++; 1451 rt->mf6c_byte_cnt += plen; 1452 1453 /* 1454 * For each mif, forward a copy of the packet if there are group 1455 * members downstream on the interface. 1456 */ 1457 src0 = ip6->ip6_src; 1458 dst0 = ip6->ip6_dst; 1459 if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 || 1460 (error = in6_setscope(&dst0, ifp, &idzone)) != 0) { 1461 ip6stat.ip6s_badscope++; 1462 return (error); 1463 } 1464 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) { 1465 if (IF_ISSET(mifi, &rt->mf6c_ifset)) { 1466 /* 1467 * check if the outgoing packet is going to break 1468 * a scope boundary. 1469 * XXX For packets through PIM register tunnel 1470 * interface, we believe a routing daemon. 1471 */ 1472 if (!(mif6table[rt->mf6c_parent].m6_flags & 1473 MIFF_REGISTER) && 1474 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) { 1475 if (in6_setscope(&src0, mif6table[mifi].m6_ifp, 1476 &oszone) || 1477 in6_setscope(&dst0, mif6table[mifi].m6_ifp, 1478 &odzone) || 1479 iszone != oszone || 1480 idzone != odzone) { 1481 ip6stat.ip6s_badscope++; 1482 continue; 1483 } 1484 } 1485 1486 mifp->m6_pkt_out++; 1487 mifp->m6_bytes_out += plen; 1488 MC6_SEND(ip6, mifp, m); 1489 } 1490 } 1491 return (0); 1492} 1493 1494static void 1495phyint_send(ip6, mifp, m) 1496 struct ip6_hdr *ip6; 1497 struct mif6 *mifp; 1498 struct mbuf *m; 1499{ 1500 struct mbuf *mb_copy; 1501 struct ifnet *ifp = mifp->m6_ifp; 1502 int error = 0; 1503 int s = splnet(); /* needs to protect static "ro" below. */ 1504 static struct route_in6 ro; 1505 struct in6_multi *in6m; 1506 struct sockaddr_in6 *dst6; 1507 u_long linkmtu; 1508 1509 /* 1510 * Make a new reference to the packet; make sure that 1511 * the IPv6 header is actually copied, not just referenced, 1512 * so that ip6_output() only scribbles on the copy. 1513 */ 1514 mb_copy = m_copy(m, 0, M_COPYALL); 1515 if (mb_copy && 1516 (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr))) 1517 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr)); 1518 if (mb_copy == NULL) { 1519 splx(s); 1520 return; 1521 } 1522 /* set MCAST flag to the outgoing packet */ 1523 mb_copy->m_flags |= M_MCAST; 1524 1525 /* 1526 * If we sourced the packet, call ip6_output since we may devide 1527 * the packet into fragments when the packet is too big for the 1528 * outgoing interface. 1529 * Otherwise, we can simply send the packet to the interface 1530 * sending queue. 1531 */ 1532 if (m->m_pkthdr.rcvif == NULL) { 1533 struct ip6_moptions im6o; 1534 1535 im6o.im6o_multicast_ifp = ifp; 1536 /* XXX: ip6_output will override ip6->ip6_hlim */ 1537 im6o.im6o_multicast_hlim = ip6->ip6_hlim; 1538 im6o.im6o_multicast_loop = 1; 1539 error = ip6_output(mb_copy, NULL, &ro, 1540 IPV6_FORWARDING, &im6o, NULL, NULL); 1541 1542#ifdef MRT6DEBUG 1543 if (mrt6debug & DEBUG_XMIT) 1544 log(LOG_DEBUG, "phyint_send on mif %d err %d\n", 1545 mifp - mif6table, error); 1546#endif 1547 splx(s); 1548 return; 1549 } 1550 1551 /* 1552 * If we belong to the destination multicast group 1553 * on the outgoing interface, loop back a copy. 1554 */ 1555 dst6 = (struct sockaddr_in6 *)&ro.ro_dst; 1556 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 1557 if (in6m != NULL) { 1558 dst6->sin6_len = sizeof(struct sockaddr_in6); 1559 dst6->sin6_family = AF_INET6; 1560 dst6->sin6_addr = ip6->ip6_dst; 1561 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst); 1562 } 1563 /* 1564 * Put the packet into the sending queue of the outgoing interface 1565 * if it would fit in the MTU of the interface. 1566 */ 1567 linkmtu = IN6_LINKMTU(ifp); 1568 if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) { 1569 dst6->sin6_len = sizeof(struct sockaddr_in6); 1570 dst6->sin6_family = AF_INET6; 1571 dst6->sin6_addr = ip6->ip6_dst; 1572 /* 1573 * We just call if_output instead of nd6_output here, since 1574 * we need no ND for a multicast forwarded packet...right? 1575 */ 1576 error = (*ifp->if_output)(ifp, mb_copy, 1577 (struct sockaddr *)&ro.ro_dst, NULL); 1578#ifdef MRT6DEBUG 1579 if (mrt6debug & DEBUG_XMIT) 1580 log(LOG_DEBUG, "phyint_send on mif %d err %d\n", 1581 mifp - mif6table, error); 1582#endif 1583 } else { 1584 /* 1585 * pMTU discovery is intentionally disabled by default, since 1586 * various router may notify pMTU in multicast, which can be 1587 * a DDoS to a router 1588 */ 1589 if (ip6_mcast_pmtu) 1590 icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu); 1591 else { 1592#ifdef MRT6DEBUG 1593 if (mrt6debug & DEBUG_XMIT) 1594 log(LOG_DEBUG, 1595 "phyint_send: packet too big on %s o %s " 1596 "g %s size %d(discarded)\n", 1597 if_name(ifp), 1598 ip6_sprintf(&ip6->ip6_src), 1599 ip6_sprintf(&ip6->ip6_dst), 1600 mb_copy->m_pkthdr.len); 1601#endif /* MRT6DEBUG */ 1602 m_freem(mb_copy); /* simply discard the packet */ 1603 } 1604 } 1605 1606 splx(s); 1607} 1608 1609static int 1610register_send(ip6, mif, m) 1611 struct ip6_hdr *ip6; 1612 struct mif6 *mif; 1613 struct mbuf *m; 1614{ 1615 struct mbuf *mm; 1616 int i, len = m->m_pkthdr.len; 1617 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 }; 1618 struct mrt6msg *im6; 1619 1620#ifdef MRT6DEBUG 1621 if (mrt6debug) 1622 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n", 1623 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst)); 1624#endif 1625 ++pim6stat.pim6s_snd_registers; 1626 1627 /* Make a copy of the packet to send to the user level process */ 1628 MGETHDR(mm, M_DONTWAIT, MT_HEADER); 1629 if (mm == NULL) 1630 return (ENOBUFS); 1631 mm->m_pkthdr.rcvif = NULL; 1632 mm->m_data += max_linkhdr; 1633 mm->m_len = sizeof(struct ip6_hdr); 1634 1635 if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) { 1636 m_freem(mm); 1637 return (ENOBUFS); 1638 } 1639 i = MHLEN - M_LEADINGSPACE(mm); 1640 if (i > len) 1641 i = len; 1642 mm = m_pullup(mm, i); 1643 if (mm == NULL) 1644 return (ENOBUFS); 1645/* TODO: check it! */ 1646 mm->m_pkthdr.len = len + sizeof(struct ip6_hdr); 1647 1648 /* 1649 * Send message to routing daemon 1650 */ 1651 sin6.sin6_addr = ip6->ip6_src; 1652 1653 im6 = mtod(mm, struct mrt6msg *); 1654 im6->im6_msgtype = MRT6MSG_WHOLEPKT; 1655 im6->im6_mbz = 0; 1656 1657 im6->im6_mif = mif - mif6table; 1658 1659 /* iif info is not given for reg. encap.n */ 1660 mrt6stat.mrt6s_upcalls++; 1661 1662 if (socket_send(ip6_mrouter, mm, &sin6) < 0) { 1663#ifdef MRT6DEBUG 1664 if (mrt6debug) 1665 log(LOG_WARNING, 1666 "register_send: ip6_mrouter socket queue full\n"); 1667#endif 1668 ++mrt6stat.mrt6s_upq_sockfull; 1669 return (ENOBUFS); 1670 } 1671 return (0); 1672} 1673 1674/* 1675 * PIM sparse mode hook 1676 * Receives the pim control messages, and passes them up to the listening 1677 * socket, using rip6_input. 1678 * The only message processed is the REGISTER pim message; the pim header 1679 * is stripped off, and the inner packet is passed to register_mforward. 1680 */ 1681int 1682pim6_input(mp, offp, proto) 1683 struct mbuf **mp; 1684 int *offp, proto; 1685{ 1686 struct pim *pim; /* pointer to a pim struct */ 1687 struct ip6_hdr *ip6; 1688 int pimlen; 1689 struct mbuf *m = *mp; 1690 int minlen; 1691 int off = *offp; 1692 1693 ++pim6stat.pim6s_rcv_total; 1694 1695 ip6 = mtod(m, struct ip6_hdr *); 1696 pimlen = m->m_pkthdr.len - *offp; 1697 1698 /* 1699 * Validate lengths 1700 */ 1701 if (pimlen < PIM_MINLEN) { 1702 ++pim6stat.pim6s_rcv_tooshort; 1703#ifdef MRT6DEBUG 1704 if (mrt6debug & DEBUG_PIM) 1705 log(LOG_DEBUG,"pim6_input: PIM packet too short\n"); 1706#endif 1707 m_freem(m); 1708 return (IPPROTO_DONE); 1709 } 1710 1711 /* 1712 * if the packet is at least as big as a REGISTER, go ahead 1713 * and grab the PIM REGISTER header size, to avoid another 1714 * possible m_pullup() later. 1715 * 1716 * PIM_MINLEN == pimhdr + u_int32 == 8 1717 * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40 1718 */ 1719 minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN; 1720 1721 /* 1722 * Make sure that the IP6 and PIM headers in contiguous memory, and 1723 * possibly the PIM REGISTER header 1724 */ 1725#ifndef PULLDOWN_TEST 1726 IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE); 1727 /* adjust pointer */ 1728 ip6 = mtod(m, struct ip6_hdr *); 1729 1730 /* adjust mbuf to point to the PIM header */ 1731 pim = (struct pim *)((caddr_t)ip6 + off); 1732#else 1733 IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen); 1734 if (pim == NULL) { 1735 pim6stat.pim6s_rcv_tooshort++; 1736 return (IPPROTO_DONE); 1737 } 1738#endif 1739 1740#define PIM6_CHECKSUM 1741#ifdef PIM6_CHECKSUM 1742 { 1743 int cksumlen; 1744 1745 /* 1746 * Validate checksum. 1747 * If PIM REGISTER, exclude the data packet 1748 */ 1749 if (pim->pim_type == PIM_REGISTER) 1750 cksumlen = PIM_MINLEN; 1751 else 1752 cksumlen = pimlen; 1753 1754 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) { 1755 ++pim6stat.pim6s_rcv_badsum; 1756#ifdef MRT6DEBUG 1757 if (mrt6debug & DEBUG_PIM) 1758 log(LOG_DEBUG, 1759 "pim6_input: invalid checksum\n"); 1760#endif 1761 m_freem(m); 1762 return (IPPROTO_DONE); 1763 } 1764 } 1765#endif /* PIM_CHECKSUM */ 1766 1767 /* PIM version check */ 1768 if (pim->pim_ver != PIM_VERSION) { 1769 ++pim6stat.pim6s_rcv_badversion; 1770#ifdef MRT6DEBUG 1771 log(LOG_ERR, 1772 "pim6_input: incorrect version %d, expecting %d\n", 1773 pim->pim_ver, PIM_VERSION); 1774#endif 1775 m_freem(m); 1776 return (IPPROTO_DONE); 1777 } 1778 1779 if (pim->pim_type == PIM_REGISTER) { 1780 /* 1781 * since this is a REGISTER, we'll make a copy of the register 1782 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the 1783 * routing daemon. 1784 */ 1785 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 }; 1786 1787 struct mbuf *mcp; 1788 struct ip6_hdr *eip6; 1789 u_int32_t *reghdr; 1790 int rc; 1791 1792 ++pim6stat.pim6s_rcv_registers; 1793 1794 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) { 1795#ifdef MRT6DEBUG 1796 if (mrt6debug & DEBUG_PIM) 1797 log(LOG_DEBUG, 1798 "pim6_input: register mif not set: %d\n", 1799 reg_mif_num); 1800#endif 1801 m_freem(m); 1802 return (IPPROTO_DONE); 1803 } 1804 1805 reghdr = (u_int32_t *)(pim + 1); 1806 1807 if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) 1808 goto pim6_input_to_daemon; 1809 1810 /* 1811 * Validate length 1812 */ 1813 if (pimlen < PIM6_REG_MINLEN) { 1814 ++pim6stat.pim6s_rcv_tooshort; 1815 ++pim6stat.pim6s_rcv_badregisters; 1816#ifdef MRT6DEBUG 1817 log(LOG_ERR, 1818 "pim6_input: register packet size too " 1819 "small %d from %s\n", 1820 pimlen, ip6_sprintf(&ip6->ip6_src)); 1821#endif 1822 m_freem(m); 1823 return (IPPROTO_DONE); 1824 } 1825 1826 eip6 = (struct ip6_hdr *) (reghdr + 1); 1827#ifdef MRT6DEBUG 1828 if (mrt6debug & DEBUG_PIM) 1829 log(LOG_DEBUG, 1830 "pim6_input[register], eip6: %s -> %s, " 1831 "eip6 plen %d\n", 1832 ip6_sprintf(&eip6->ip6_src), 1833 ip6_sprintf(&eip6->ip6_dst), 1834 ntohs(eip6->ip6_plen)); 1835#endif 1836 1837 /* verify the version number of the inner packet */ 1838 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1839 ++pim6stat.pim6s_rcv_badregisters; 1840#ifdef MRT6DEBUG 1841 log(LOG_DEBUG, "pim6_input: invalid IP version (%d) " 1842 "of the inner packet\n", 1843 (eip6->ip6_vfc & IPV6_VERSION)); 1844#endif 1845 m_freem(m); 1846 return (IPPROTO_NONE); 1847 } 1848 1849 /* verify the inner packet is destined to a mcast group */ 1850 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) { 1851 ++pim6stat.pim6s_rcv_badregisters; 1852#ifdef MRT6DEBUG 1853 if (mrt6debug & DEBUG_PIM) 1854 log(LOG_DEBUG, 1855 "pim6_input: inner packet of register " 1856 "is not multicast %s\n", 1857 ip6_sprintf(&eip6->ip6_dst)); 1858#endif 1859 m_freem(m); 1860 return (IPPROTO_DONE); 1861 } 1862 1863 /* 1864 * make a copy of the whole header to pass to the daemon later. 1865 */ 1866 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN); 1867 if (mcp == NULL) { 1868#ifdef MRT6DEBUG 1869 log(LOG_ERR, 1870 "pim6_input: pim register: " 1871 "could not copy register head\n"); 1872#endif 1873 m_freem(m); 1874 return (IPPROTO_DONE); 1875 } 1876 1877 /* 1878 * forward the inner ip6 packet; point m_data at the inner ip6. 1879 */ 1880 m_adj(m, off + PIM_MINLEN); 1881#ifdef MRT6DEBUG 1882 if (mrt6debug & DEBUG_PIM) { 1883 log(LOG_DEBUG, 1884 "pim6_input: forwarding decapsulated register: " 1885 "src %s, dst %s, mif %d\n", 1886 ip6_sprintf(&eip6->ip6_src), 1887 ip6_sprintf(&eip6->ip6_dst), 1888 reg_mif_num); 1889 } 1890#endif 1891 1892 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m, 1893 dst.sin6_family, 0); 1894 1895 /* prepare the register head to send to the mrouting daemon */ 1896 m = mcp; 1897 } 1898 1899 /* 1900 * Pass the PIM message up to the daemon; if it is a register message 1901 * pass the 'head' only up to the daemon. This includes the 1902 * encapsulator ip6 header, pim header, register header and the 1903 * encapsulated ip6 header. 1904 */ 1905 pim6_input_to_daemon: 1906 rip6_input(&m, offp, proto); 1907 return (IPPROTO_DONE); 1908} 1909