1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 2853541Sshin */ 2953541Sshin 30139826Simp/*- 3153541Sshin * Copyright (c) 1982, 1986, 1988, 1993 32180305Srwatson * The Regents of the University of California. 33180305Srwatson * All rights reserved. 3453541Sshin * 3553541Sshin * Redistribution and use in source and binary forms, with or without 3653541Sshin * modification, are permitted provided that the following conditions 3753541Sshin * are met: 3853541Sshin * 1. Redistributions of source code must retain the above copyright 3953541Sshin * notice, this list of conditions and the following disclaimer. 4053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4153541Sshin * notice, this list of conditions and the following disclaimer in the 4253541Sshin * documentation and/or other materials provided with the distribution. 4353541Sshin * 4. Neither the name of the University nor the names of its contributors 4453541Sshin * may be used to endorse or promote products derived from this software 4553541Sshin * without specific prior written permission. 4653541Sshin * 4753541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5753541Sshin * SUCH DAMAGE. 5853541Sshin * 5953541Sshin * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6053541Sshin */ 6153541Sshin 62174510Sobrien#include <sys/cdefs.h> 63174510Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/netinet6/raw_ip6.c 253571 2013-07-23 14:14:24Z ae $"); 64174510Sobrien 6555009Sshin#include "opt_ipsec.h" 6678064Sume#include "opt_inet6.h" 6755009Sshin 6853541Sshin#include <sys/param.h> 6995759Stanimura#include <sys/errno.h> 70185435Sbz#include <sys/jail.h> 71253085Sae#include <sys/kernel.h> 7295759Stanimura#include <sys/lock.h> 7353541Sshin#include <sys/malloc.h> 7495759Stanimura#include <sys/mbuf.h> 75170689Srwatson#include <sys/priv.h> 7653541Sshin#include <sys/proc.h> 7795759Stanimura#include <sys/protosw.h> 7895759Stanimura#include <sys/signalvar.h> 7953541Sshin#include <sys/socket.h> 8053541Sshin#include <sys/socketvar.h> 8195759Stanimura#include <sys/sx.h> 82148385Sume#include <sys/syslog.h> 8353541Sshin 8453541Sshin#include <net/if.h> 8595759Stanimura#include <net/if_types.h> 8653541Sshin#include <net/route.h> 87185571Sbz#include <net/vnet.h> 8853541Sshin 8953541Sshin#include <netinet/in.h> 9053541Sshin#include <netinet/in_var.h> 9153541Sshin#include <netinet/in_systm.h> 92185571Sbz#include <netinet/in_pcb.h> 93185571Sbz 9495759Stanimura#include <netinet/icmp6.h> 9562587Sitojun#include <netinet/ip6.h> 96211501Sanchie#include <netinet/ip_var.h> 9795759Stanimura#include <netinet6/ip6protosw.h> 9856723Sshin#include <netinet6/ip6_mroute.h> 9953541Sshin#include <netinet6/in6_pcb.h> 10095759Stanimura#include <netinet6/ip6_var.h> 10153541Sshin#include <netinet6/nd6.h> 10295759Stanimura#include <netinet6/raw_ip6.h> 10362587Sitojun#include <netinet6/scope6_var.h> 104211501Sanchie#include <netinet6/send.h> 10553541Sshin 106171167Sgnn#ifdef IPSEC 107105199Ssam#include <netipsec/ipsec.h> 108105199Ssam#include <netipsec/ipsec6.h> 109171167Sgnn#endif /* IPSEC */ 110105199Ssam 11153541Sshin#include <machine/stdarg.h> 11253541Sshin 11353541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11453541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11553541Sshin 11653541Sshin/* 11753541Sshin * Raw interface to IP6 protocol. 11853541Sshin */ 11953541Sshin 120195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb); 121195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo); 122195727Srwatson#define V_ripcb VNET(ripcb) 123195727Srwatson#define V_ripcbinfo VNET(ripcbinfo) 124185348Szec 12553541Sshinextern u_long rip_sendspace; 12653541Sshinextern u_long rip_recvspace; 12753541Sshin 128253085SaeVNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat); 129253085SaeVNET_PCPUSTAT_SYSINIT(rip6stat); 130207369Sbz 131253085Sae#ifdef VIMAGE 132253085SaeVNET_PCPUSTAT_SYSUNINIT(rip6stat); 133253085Sae#endif /* VIMAGE */ 134253085Sae 13553541Sshin/* 136191672Sbms * Hooks for multicast routing. They all default to NULL, so leave them not 137191672Sbms * initialized and rely on BSS being set to 0. 138166938Sbms */ 139191672Sbms 140191672Sbms/* 141191672Sbms * The socket used to communicate with the multicast routing daemon. 142191672Sbms */ 143195699SrwatsonVNET_DEFINE(struct socket *, ip6_mrouter); 144191672Sbms 145191672Sbms/* 146191672Sbms * The various mrouter functions. 147191672Sbms */ 148166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 149166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 150166938Sbmsint (*ip6_mrouter_done)(void); 151166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 152194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t); 153166938Sbms 154166938Sbms/* 155180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 156180305Srwatson * pass them along with mbuf chain. 15753541Sshin */ 15853541Sshinint 159171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 16053541Sshin{ 161191672Sbms struct ifnet *ifp; 16253541Sshin struct mbuf *m = *mp; 16353541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 16453541Sshin register struct inpcb *in6p; 16553541Sshin struct inpcb *last = 0; 16678064Sume struct mbuf *opts = NULL; 167121901Sume struct sockaddr_in6 fromsa; 16853541Sshin 169252007Sae RIP6STAT_INC(rip6s_ipackets); 17078064Sume 17183934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 172180305Srwatson /* XXX Send icmp6 host/port unreach? */ 17378064Sume m_freem(m); 174180305Srwatson return (IPPROTO_DONE); 17553541Sshin } 17678064Sume 177121901Sume init_sin6(&fromsa, m); /* general init */ 17853541Sshin 179191672Sbms ifp = m->m_pkthdr.rcvif; 180191672Sbms 181181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 182181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 183185435Sbz /* XXX inp locking */ 184186141Sbz if ((in6p->inp_vflag & INP_IPV6) == 0) 18553541Sshin continue; 186186141Sbz if (in6p->inp_ip_p && 187186141Sbz in6p->inp_ip_p != proto) 188180850Smav continue; 18953541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 19053541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 191180850Smav continue; 19253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 19353541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 194180850Smav continue; 195200473Sbz if (jailed_without_vnet(in6p->inp_cred)) { 196191672Sbms /* 197191672Sbms * Allow raw socket in jail to receive multicast; 198191672Sbms * assume process had PRIV_NETINET_RAW at attach, 199191672Sbms * and fall through into normal filter path if so. 200191672Sbms */ 201191672Sbms if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 202191672Sbms prison_check_ip6(in6p->inp_cred, 203191672Sbms &ip6->ip6_dst) != 0) 204191672Sbms continue; 205191672Sbms } 206248180Sae INP_RLOCK(in6p); 20778064Sume if (in6p->in6p_cksum != -1) { 208252007Sae RIP6STAT_INC(rip6s_isum); 209151459Ssuz if (in6_cksum(m, proto, *offp, 21078064Sume m->m_pkthdr.len - *offp)) { 211180932Smav INP_RUNLOCK(in6p); 212252007Sae RIP6STAT_INC(rip6s_badsum); 213180850Smav continue; 21478064Sume } 21553541Sshin } 216191672Sbms /* 217191672Sbms * If this raw socket has multicast state, and we 218191672Sbms * have received a multicast, check if this socket 219191672Sbms * should receive it, as multicast filtering is now 220191672Sbms * the responsibility of the transport layer. 221191672Sbms */ 222191672Sbms if (in6p->in6p_moptions && 223191672Sbms IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 224199518Sbms /* 225199518Sbms * If the incoming datagram is for MLD, allow it 226199518Sbms * through unconditionally to the raw socket. 227199518Sbms * 228199518Sbms * Use the M_RTALERT_MLD flag to check for MLD 229199518Sbms * traffic without having to inspect the mbuf chain 230199518Sbms * more deeply, as all MLDv1/v2 host messages MUST 231199518Sbms * contain the Router Alert option. 232199518Sbms * 233199518Sbms * In the case of MLDv1, we may not have explicitly 234199518Sbms * joined the group, and may have set IFF_ALLMULTI 235199518Sbms * on the interface. im6o_mc_filter() may discard 236199518Sbms * control traffic we actually need to see. 237199518Sbms * 238199518Sbms * Userland multicast routing daemons should continue 239199518Sbms * filter the control traffic appropriately. 240199518Sbms */ 241191672Sbms int blocked; 242191672Sbms 243199518Sbms blocked = MCAST_PASS; 244199518Sbms if ((m->m_flags & M_RTALERT_MLD) == 0) { 245199518Sbms struct sockaddr_in6 mcaddr; 246191672Sbms 247199518Sbms bzero(&mcaddr, sizeof(struct sockaddr_in6)); 248199518Sbms mcaddr.sin6_len = sizeof(struct sockaddr_in6); 249199518Sbms mcaddr.sin6_family = AF_INET6; 250199518Sbms mcaddr.sin6_addr = ip6->ip6_dst; 251199518Sbms 252199518Sbms blocked = im6o_mc_filter(in6p->in6p_moptions, 253199518Sbms ifp, 254199518Sbms (struct sockaddr *)&mcaddr, 255199518Sbms (struct sockaddr *)&fromsa); 256199518Sbms } 257191672Sbms if (blocked != MCAST_PASS) { 258191672Sbms IP6STAT_INC(ip6s_notmember); 259211301Sbz INP_RUNLOCK(in6p); 260191672Sbms continue; 261191672Sbms } 262191672Sbms } 263186163Skmacy if (last != NULL) { 26453541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 26578064Sume 266171167Sgnn#ifdef IPSEC 26778064Sume /* 26878064Sume * Check AH/ESP integrity. 26978064Sume */ 270125396Sume if (n && ipsec6_in_reject(n, last)) { 27178064Sume m_freem(n); 272253571Sae IPSEC6STAT_INC(ips_in_polvio); 273180305Srwatson /* Do not inject data into pcb. */ 274105199Ssam } else 275171167Sgnn#endif /* IPSEC */ 27653541Sshin if (n) { 277186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 278186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 279121674Sume ip6_savecontrol(last, n, &opts); 28053541Sshin /* strip intermediate headers */ 28153541Sshin m_adj(n, *offp); 282186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 283121901Sume (struct sockaddr *)&fromsa, 28453541Sshin n, opts) == 0) { 28553541Sshin m_freem(n); 28653541Sshin if (opts) 28753541Sshin m_freem(opts); 288252007Sae RIP6STAT_INC(rip6s_fullsock); 28997658Stanimura } else 290186141Sbz sorwakeup(last->inp_socket); 29153541Sshin opts = NULL; 29253541Sshin } 293178377Srwatson INP_RUNLOCK(last); 29453541Sshin } 29553541Sshin last = in6p; 29653541Sshin } 297181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 298171167Sgnn#ifdef IPSEC 29978064Sume /* 30078064Sume * Check AH/ESP integrity. 30178064Sume */ 302186170Skmacy if ((last != NULL) && ipsec6_in_reject(m, last)) { 30378064Sume m_freem(m); 304253571Sae IPSEC6STAT_INC(ips_in_polvio); 305249294Sae IP6STAT_DEC(ip6s_delivered); 306180305Srwatson /* Do not inject data into pcb. */ 307178377Srwatson INP_RUNLOCK(last); 308105199Ssam } else 309171167Sgnn#endif /* IPSEC */ 310186163Skmacy if (last != NULL) { 311186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 312186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 313121674Sume ip6_savecontrol(last, m, &opts); 314180305Srwatson /* Strip intermediate headers. */ 31553541Sshin m_adj(m, *offp); 316186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 317180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 31853541Sshin m_freem(m); 31953541Sshin if (opts) 32053541Sshin m_freem(opts); 321252007Sae RIP6STAT_INC(rip6s_fullsock); 32297658Stanimura } else 323186141Sbz sorwakeup(last->inp_socket); 324178377Srwatson INP_RUNLOCK(last); 32553541Sshin } else { 326252007Sae RIP6STAT_INC(rip6s_nosock); 32778064Sume if (m->m_flags & M_MCAST) 328252007Sae RIP6STAT_INC(rip6s_nosockmcast); 32953541Sshin if (proto == IPPROTO_NONE) 33053541Sshin m_freem(m); 33153541Sshin else { 33253541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 33353541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 334180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 335180305Srwatson prvnxtp - mtod(m, char *)); 33653541Sshin } 337249294Sae IP6STAT_DEC(ip6s_delivered); 33853541Sshin } 339180305Srwatson return (IPPROTO_DONE); 34053541Sshin} 34153541Sshin 34262587Sitojunvoid 343171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 34462587Sitojun{ 34562587Sitojun struct ip6_hdr *ip6; 34662587Sitojun struct mbuf *m; 34762587Sitojun int off = 0; 34878064Sume struct ip6ctlparam *ip6cp = NULL; 34978064Sume const struct sockaddr_in6 *sa6_src = NULL; 350125776Sume void *cmdarg; 351175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 35262587Sitojun 35362587Sitojun if (sa->sa_family != AF_INET6 || 35462587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 35562587Sitojun return; 35662587Sitojun 35762587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 35862587Sitojun return; 35962587Sitojun if (PRC_IS_REDIRECT(cmd)) 36062587Sitojun notify = in6_rtchange, d = NULL; 36162587Sitojun else if (cmd == PRC_HOSTDEAD) 36262587Sitojun d = NULL; 36362587Sitojun else if (inet6ctlerrmap[cmd] == 0) 36462587Sitojun return; 36562587Sitojun 366180305Srwatson /* 367180305Srwatson * If the parameter is from icmp6, decode it. 368180305Srwatson */ 36962587Sitojun if (d != NULL) { 37078064Sume ip6cp = (struct ip6ctlparam *)d; 37162587Sitojun m = ip6cp->ip6c_m; 37262587Sitojun ip6 = ip6cp->ip6c_ip6; 37362587Sitojun off = ip6cp->ip6c_off; 374125776Sume cmdarg = ip6cp->ip6c_cmdarg; 37578064Sume sa6_src = ip6cp->ip6c_src; 37662587Sitojun } else { 37762587Sitojun m = NULL; 37862587Sitojun ip6 = NULL; 379125776Sume cmdarg = NULL; 38078064Sume sa6_src = &sa6_any; 38162587Sitojun } 38262587Sitojun 383181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 384180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 38562587Sitojun} 38662587Sitojun 38753541Sshin/* 388180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 389180305Srwatson * may have setup with control call. 39053541Sshin */ 39153541Sshinint 39253541Sshin#if __STDC__ 39353541Sshinrip6_output(struct mbuf *m, ...) 39453541Sshin#else 39553541Sshinrip6_output(m, va_alist) 39653541Sshin struct mbuf *m; 39753541Sshin va_dcl 39853541Sshin#endif 39953541Sshin{ 400120941Sume struct mbuf *control; 401211501Sanchie struct m_tag *mtag; 40253541Sshin struct socket *so; 40353541Sshin struct sockaddr_in6 *dstsock; 40453541Sshin struct in6_addr *dst; 40553541Sshin struct ip6_hdr *ip6; 40653541Sshin struct inpcb *in6p; 40753541Sshin u_int plen = m->m_pkthdr.len; 40853541Sshin int error = 0; 409148247Sume struct ip6_pktopts opt, *optp; 41053541Sshin struct ifnet *oifp = NULL; 41153541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 412148385Sume int scope_ambiguous = 0; 413211435Sume int use_defzone = 0; 414194777Sbz struct in6_addr in6a; 41553541Sshin va_list ap; 41653541Sshin 41753541Sshin va_start(ap, m); 41853541Sshin so = va_arg(ap, struct socket *); 41953541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 42053541Sshin control = va_arg(ap, struct mbuf *); 42153541Sshin va_end(ap); 42253541Sshin 423186141Sbz in6p = sotoinpcb(so); 424178285Srwatson INP_WLOCK(in6p); 42553541Sshin 42653541Sshin dst = &dstsock->sin6_addr; 427186170Skmacy if (control != NULL) { 428148242Sume if ((error = ip6_setpktopts(control, &opt, 429175630Sbz in6p->in6p_outputopts, so->so_cred, 430175630Sbz so->so_proto->pr_protocol)) != 0) { 43153541Sshin goto bad; 432121472Sume } 433148247Sume optp = &opt; 434148247Sume } else 435148247Sume optp = in6p->in6p_outputopts; 43653541Sshin 43753541Sshin /* 438148385Sume * Check and convert scope zone ID into internal form. 439180305Srwatson * 440148385Sume * XXX: we may still need to determine the zone later. 441148385Sume */ 442148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 443211530Sume if (!optp || !optp->ip6po_pktinfo || 444211530Sume !optp->ip6po_pktinfo->ipi6_ifindex) 445211435Sume use_defzone = V_ip6_use_defzone; 446211435Sume if (dstsock->sin6_scope_id == 0 && !use_defzone) 447148385Sume scope_ambiguous = 1; 448211435Sume if ((error = sa6_embedscope(dstsock, use_defzone)) != 0) 449148385Sume goto bad; 450148385Sume } 451148385Sume 452148385Sume /* 453180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 454180305Srwatson * statistics. 45553541Sshin */ 45653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 45753541Sshin struct icmp6_hdr *icmp6; 45853541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 45953541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 46053541Sshin error = ENOBUFS; 46153541Sshin goto bad; 46253541Sshin } 46353541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 46453541Sshin type = icmp6->icmp6_type; 46553541Sshin code = icmp6->icmp6_code; 46653541Sshin } 46753541Sshin 468243882Sglebius M_PREPEND(m, sizeof(*ip6), M_NOWAIT); 469133592Srwatson if (m == NULL) { 470133592Srwatson error = ENOBUFS; 471133592Srwatson goto bad; 472133592Srwatson } 47353541Sshin ip6 = mtod(m, struct ip6_hdr *); 47453541Sshin 47553541Sshin /* 47653541Sshin * Source address selection. 47753541Sshin */ 478194777Sbz error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 479194777Sbz &oifp, &in6a); 480194777Sbz if (error) 481121472Sume goto bad; 482207277Sbz error = prison_check_ip6(in6p->inp_cred, &in6a); 483188144Sjamie if (error != 0) 484188144Sjamie goto bad; 485194777Sbz ip6->ip6_src = in6a; 486148385Sume 487148385Sume if (oifp && scope_ambiguous) { 488148385Sume /* 489148385Sume * Application should provide a proper zone ID or the use of 490148385Sume * default zone IDs should be enabled. Unfortunately, some 491148385Sume * applications do not behave as it should, so we need a 492148385Sume * workaround. Even if an appropriate ID is not determined 493148385Sume * (when it's required), if we can determine the outgoing 494148385Sume * interface. determine the zone ID based on the interface. 495148385Sume */ 496148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 497148385Sume if (error != 0) 498148385Sume goto bad; 499148385Sume } 500148385Sume ip6->ip6_dst = dstsock->sin6_addr; 501148385Sume 502180305Srwatson /* 503180305Srwatson * Fill in the rest of the IPv6 header fields. 504180305Srwatson */ 50555009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 506186141Sbz (in6p->inp_flow & IPV6_FLOWINFO_MASK); 50755009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 508180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 509180305Srwatson 510180305Srwatson /* 511180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 512180305Srwatson */ 513186141Sbz ip6->ip6_nxt = in6p->inp_ip_p; 51453541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 51553541Sshin 51653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 51753541Sshin in6p->in6p_cksum != -1) { 51853541Sshin struct mbuf *n; 51953541Sshin int off; 52053541Sshin u_int16_t *p; 52153541Sshin 522180305Srwatson /* Compute checksum. */ 52353541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 52453541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 52553541Sshin else 52653541Sshin off = in6p->in6p_cksum; 52753541Sshin if (plen < off + 1) { 52853541Sshin error = EINVAL; 52953541Sshin goto bad; 53053541Sshin } 53153541Sshin off += sizeof(struct ip6_hdr); 53253541Sshin 53353541Sshin n = m; 53453541Sshin while (n && n->m_len <= off) { 53553541Sshin off -= n->m_len; 53653541Sshin n = n->m_next; 53753541Sshin } 53853541Sshin if (!n) 53953541Sshin goto bad; 54053541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 54153541Sshin *p = 0; 54253541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 54353541Sshin } 54453541Sshin 545211501Sanchie /* 546211501Sanchie * Send RA/RS messages to user land for protection, before sending 547211501Sanchie * them to rtadvd/rtsol. 548211501Sanchie */ 549211501Sanchie if ((send_sendso_input_hook != NULL) && 550211501Sanchie so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 551211501Sanchie switch (type) { 552211501Sanchie case ND_ROUTER_ADVERT: 553211501Sanchie case ND_ROUTER_SOLICIT: 554211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 555211501Sanchie sizeof(unsigned short), M_NOWAIT); 556211501Sanchie if (mtag == NULL) 557211501Sanchie goto bad; 558211501Sanchie m_tag_prepend(m, mtag); 559211501Sanchie } 560211501Sanchie } 561211501Sanchie 562148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 56353541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 56453541Sshin if (oifp) 56553541Sshin icmp6_ifoutstat_inc(oifp, type, code); 566190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 56778064Sume } else 568252007Sae RIP6STAT_INC(rip6s_opackets); 56953541Sshin 57053541Sshin goto freectl; 57153541Sshin 57253541Sshin bad: 57353541Sshin if (m) 57453541Sshin m_freem(m); 57553541Sshin 57653541Sshin freectl: 577186170Skmacy if (control != NULL) { 578148247Sume ip6_clearpktopts(&opt, -1); 57953541Sshin m_freem(control); 58078064Sume } 581178285Srwatson INP_WUNLOCK(in6p); 582120856Sume return (error); 58353541Sshin} 58453541Sshin 58553541Sshin/* 58653541Sshin * Raw IPv6 socket option processing. 58753541Sshin */ 58853541Sshinint 589171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 59053541Sshin{ 591231852Sbz struct inpcb *inp; 59253541Sshin int error; 59353541Sshin 59453541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 59553541Sshin /* 59653541Sshin * XXX: is it better to call icmp6_ctloutput() directly 59753541Sshin * from protosw? 59853541Sshin */ 599120856Sume return (icmp6_ctloutput(so, sopt)); 600231852Sbz else if (sopt->sopt_level != IPPROTO_IPV6) { 601231852Sbz if (sopt->sopt_level == SOL_SOCKET && 602231852Sbz sopt->sopt_name == SO_SETFIB) { 603231852Sbz inp = sotoinpcb(so); 604231852Sbz INP_WLOCK(inp); 605231852Sbz inp->inp_inc.inc_fibnum = so->so_fibnum; 606231852Sbz INP_WUNLOCK(inp); 607231852Sbz return (0); 608231852Sbz } 60953541Sshin return (EINVAL); 610231852Sbz } 61153541Sshin 61253541Sshin error = 0; 61353541Sshin 61453541Sshin switch (sopt->sopt_dir) { 61553541Sshin case SOPT_GET: 61653541Sshin switch (sopt->sopt_name) { 61756723Sshin case MRT6_INIT: 61856723Sshin case MRT6_DONE: 61956723Sshin case MRT6_ADD_MIF: 62056723Sshin case MRT6_DEL_MIF: 62156723Sshin case MRT6_ADD_MFC: 62256723Sshin case MRT6_DEL_MFC: 62356723Sshin case MRT6_PIM: 624166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 625166938Sbms EOPNOTSUPP; 62656723Sshin break; 627121578Sume case IPV6_CHECKSUM: 628121578Sume error = ip6_raw_ctloutput(so, sopt); 629121578Sume break; 63053541Sshin default: 63153541Sshin error = ip6_ctloutput(so, sopt); 63253541Sshin break; 63353541Sshin } 63453541Sshin break; 63553541Sshin 63653541Sshin case SOPT_SET: 63753541Sshin switch (sopt->sopt_name) { 63856723Sshin case MRT6_INIT: 63956723Sshin case MRT6_DONE: 64056723Sshin case MRT6_ADD_MIF: 64156723Sshin case MRT6_DEL_MIF: 64256723Sshin case MRT6_ADD_MFC: 64356723Sshin case MRT6_DEL_MFC: 64456723Sshin case MRT6_PIM: 645166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 646166938Sbms EOPNOTSUPP; 64756723Sshin break; 648121578Sume case IPV6_CHECKSUM: 649121578Sume error = ip6_raw_ctloutput(so, sopt); 650121578Sume break; 65153541Sshin default: 65253541Sshin error = ip6_ctloutput(so, sopt); 65353541Sshin break; 65453541Sshin } 65553541Sshin break; 65653541Sshin } 65753541Sshin 65853541Sshin return (error); 65953541Sshin} 66053541Sshin 66153541Sshinstatic int 66283366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 66353541Sshin{ 66453541Sshin struct inpcb *inp; 665144261Ssam struct icmp6_filter *filter; 666157676Srwatson int error; 66753541Sshin 66853541Sshin inp = sotoinpcb(so); 669157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 670180305Srwatson 671175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 672175630Sbz if (error) 673180305Srwatson return (error); 67455009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 675157374Srwatson if (error) 676180305Srwatson return (error); 677184214Sdes filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 678157374Srwatson if (filter == NULL) 679180305Srwatson return (ENOMEM); 680181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 681181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 682132714Srwatson if (error) { 683181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 684184205Sdes free(filter, M_PCB); 685180305Srwatson return (error); 686132714Srwatson } 68753541Sshin inp = (struct inpcb *)so->so_pcb; 688181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 68953541Sshin inp->inp_vflag |= INP_IPV6; 690186141Sbz inp->inp_ip_p = (long)proto; 69153541Sshin inp->in6p_hops = -1; /* use kernel default */ 69253541Sshin inp->in6p_cksum = -1; 693144261Ssam inp->in6p_icmp6filt = filter; 69453541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 695178285Srwatson INP_WUNLOCK(inp); 696180305Srwatson return (0); 69753541Sshin} 69853541Sshin 699157370Srwatsonstatic void 70053541Sshinrip6_detach(struct socket *so) 70153541Sshin{ 70253541Sshin struct inpcb *inp; 70353541Sshin 70453541Sshin inp = sotoinpcb(so); 705157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 706160549Srwatson 707191672Sbms if (so == V_ip6_mrouter && ip6_mrouter_done) 708166938Sbms ip6_mrouter_done(); 70953541Sshin /* xxx: RSVP */ 710181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 711178285Srwatson INP_WLOCK(inp); 712184205Sdes free(inp->in6p_icmp6filt, M_PCB); 713185344Sbz in_pcbdetach(inp); 714185370Sbz in_pcbfree(inp); 715181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 71653541Sshin} 71753541Sshin 718160549Srwatson/* XXXRW: This can't ever be called. */ 719157366Srwatsonstatic void 72053541Sshinrip6_abort(struct socket *so) 72153541Sshin{ 722160549Srwatson struct inpcb *inp; 723160549Srwatson 724160549Srwatson inp = sotoinpcb(so); 725160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 726160549Srwatson 72753541Sshin soisdisconnected(so); 72853541Sshin} 72953541Sshin 730160549Srwatsonstatic void 731160549Srwatsonrip6_close(struct socket *so) 732160549Srwatson{ 733160549Srwatson struct inpcb *inp; 734160549Srwatson 735160549Srwatson inp = sotoinpcb(so); 736160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 737160549Srwatson 738160549Srwatson soisdisconnected(so); 739160549Srwatson} 740160549Srwatson 74153541Sshinstatic int 74253541Sshinrip6_disconnect(struct socket *so) 74353541Sshin{ 744180305Srwatson struct inpcb *inp; 74553541Sshin 746180305Srwatson inp = sotoinpcb(so); 747180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 748180305Srwatson 74997658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 750180305Srwatson return (ENOTCONN); 75153541Sshin inp->in6p_faddr = in6addr_any; 752157366Srwatson rip6_abort(so); 753157374Srwatson return (0); 75453541Sshin} 75553541Sshin 75653541Sshinstatic int 75783366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 75853541Sshin{ 759180305Srwatson struct inpcb *inp; 76053541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 761194760Srwatson struct ifaddr *ifa = NULL; 762148385Sume int error = 0; 76353541Sshin 764180305Srwatson inp = sotoinpcb(so); 765157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 766180305Srwatson 76753541Sshin if (nam->sa_len != sizeof(*addr)) 768180305Srwatson return (EINVAL); 769188144Sjamie if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 770188144Sjamie return (error); 771181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 772180305Srwatson return (EADDRNOTAVAIL); 773181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 774180305Srwatson return (error); 775148385Sume 77653541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 777194760Srwatson (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) 778180305Srwatson return (EADDRNOTAVAIL); 779194760Srwatson if (ifa != NULL && 780194760Srwatson ((struct in6_ifaddr *)ifa)->ia6_flags & 78153541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 78253541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 783194760Srwatson ifa_free(ifa); 784120856Sume return (EADDRNOTAVAIL); 78553541Sshin } 786194760Srwatson if (ifa != NULL) 787194760Srwatson ifa_free(ifa); 788181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 789178285Srwatson INP_WLOCK(inp); 79053541Sshin inp->in6p_laddr = addr->sin6_addr; 791178285Srwatson INP_WUNLOCK(inp); 792181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 793180305Srwatson return (0); 79453541Sshin} 79553541Sshin 79653541Sshinstatic int 79783366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 79853541Sshin{ 799180305Srwatson struct inpcb *inp; 80053541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 801194777Sbz struct in6_addr in6a; 802148385Sume struct ifnet *ifp = NULL; 803148385Sume int error = 0, scope_ambiguous = 0; 80453541Sshin 805180305Srwatson inp = sotoinpcb(so); 806157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 807180305Srwatson 80853541Sshin if (nam->sa_len != sizeof(*addr)) 809180305Srwatson return (EINVAL); 810181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 811180305Srwatson return (EADDRNOTAVAIL); 81253541Sshin if (addr->sin6_family != AF_INET6) 813180305Srwatson return (EAFNOSUPPORT); 814148385Sume 815148385Sume /* 816180305Srwatson * Application should provide a proper zone ID or the use of default 817180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 818180305Srwatson * not behave as it should, so we need a workaround. Even if an 819180305Srwatson * appropriate ID is not determined, we'll see if we can determine 820180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 821180305Srwatson * the interface below. 822148385Sume */ 823181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 824148385Sume scope_ambiguous = 1; 825181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 826180305Srwatson return (error); 827148385Sume 828181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 829178285Srwatson INP_WLOCK(inp); 83053541Sshin /* Source address selection. XXX: need pcblookup? */ 831194777Sbz error = in6_selectsrc(addr, inp->in6p_outputopts, 832194777Sbz inp, NULL, so->so_cred, &ifp, &in6a); 833194777Sbz if (error) { 834178285Srwatson INP_WUNLOCK(inp); 835181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 836194777Sbz return (error); 837132714Srwatson } 838148385Sume 839148385Sume /* XXX: see above */ 840148385Sume if (ifp && scope_ambiguous && 841148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 842178285Srwatson INP_WUNLOCK(inp); 843181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 844180305Srwatson return (error); 845148385Sume } 846148385Sume inp->in6p_faddr = addr->sin6_addr; 847194777Sbz inp->in6p_laddr = in6a; 84853541Sshin soisconnected(so); 849178285Srwatson INP_WUNLOCK(inp); 850181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 851180305Srwatson return (0); 85253541Sshin} 85353541Sshin 85453541Sshinstatic int 85553541Sshinrip6_shutdown(struct socket *so) 85653541Sshin{ 857132714Srwatson struct inpcb *inp; 858132714Srwatson 859132714Srwatson inp = sotoinpcb(so); 860157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 861180305Srwatson 862178285Srwatson INP_WLOCK(inp); 86353541Sshin socantsendmore(so); 864178285Srwatson INP_WUNLOCK(inp); 865180305Srwatson return (0); 86653541Sshin} 86753541Sshin 86853541Sshinstatic int 86953541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 870171260Sdelphij struct mbuf *control, struct thread *td) 87153541Sshin{ 872180305Srwatson struct inpcb *inp; 87353541Sshin struct sockaddr_in6 tmp; 87453541Sshin struct sockaddr_in6 *dst; 875132714Srwatson int ret; 87653541Sshin 877180305Srwatson inp = sotoinpcb(so); 878157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 879180305Srwatson 880180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 881132714Srwatson /* Unlocked read. */ 88253541Sshin if (so->so_state & SS_ISCONNECTED) { 88353541Sshin if (nam) { 88453541Sshin m_freem(m); 885180305Srwatson return (EISCONN); 88653541Sshin } 88753541Sshin /* XXX */ 88853541Sshin bzero(&tmp, sizeof(tmp)); 88953541Sshin tmp.sin6_family = AF_INET6; 89053541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 891180990Srwatson INP_RLOCK(inp); 89253541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 893180990Srwatson sizeof(struct in6_addr)); 894180990Srwatson INP_RUNLOCK(inp); 89553541Sshin dst = &tmp; 89653541Sshin } else { 89753541Sshin if (nam == NULL) { 89853541Sshin m_freem(m); 899180305Srwatson return (ENOTCONN); 90053541Sshin } 901148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 902148385Sume m_freem(m); 903180305Srwatson return (EINVAL); 904148385Sume } 90562587Sitojun tmp = *(struct sockaddr_in6 *)nam; 90662587Sitojun dst = &tmp; 907148385Sume 908148385Sume if (dst->sin6_family == AF_UNSPEC) { 909148385Sume /* 910148385Sume * XXX: we allow this case for backward 911148385Sume * compatibility to buggy applications that 912148385Sume * rely on old (and wrong) kernel behavior. 913148385Sume */ 914148385Sume log(LOG_INFO, "rip6 SEND: address family is " 915148385Sume "unspec. Assume AF_INET6\n"); 916148385Sume dst->sin6_family = AF_INET6; 917148385Sume } else if (dst->sin6_family != AF_INET6) { 918148385Sume m_freem(m); 919148385Sume return(EAFNOSUPPORT); 920148385Sume } 92153541Sshin } 922132714Srwatson ret = rip6_output(m, so, dst, control); 923132714Srwatson return (ret); 92453541Sshin} 92553541Sshin 92653541Sshinstruct pr_usrreqs rip6_usrreqs = { 927137386Sphk .pru_abort = rip6_abort, 928137386Sphk .pru_attach = rip6_attach, 929137386Sphk .pru_bind = rip6_bind, 930137386Sphk .pru_connect = rip6_connect, 931137386Sphk .pru_control = in6_control, 932137386Sphk .pru_detach = rip6_detach, 933137386Sphk .pru_disconnect = rip6_disconnect, 934169462Srwatson .pru_peeraddr = in6_getpeeraddr, 935137386Sphk .pru_send = rip6_send, 936137386Sphk .pru_shutdown = rip6_shutdown, 937169462Srwatson .pru_sockaddr = in6_getsockaddr, 938160549Srwatson .pru_close = rip6_close, 93953541Sshin}; 940