raw_ip6.c revision 200473
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: head/sys/netinet6/raw_ip6.c 200473 2009-12-13 13:57:32Z bz $"); 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> 7195759Stanimura#include <sys/lock.h> 7253541Sshin#include <sys/malloc.h> 7395759Stanimura#include <sys/mbuf.h> 74170689Srwatson#include <sys/priv.h> 7553541Sshin#include <sys/proc.h> 7695759Stanimura#include <sys/protosw.h> 7795759Stanimura#include <sys/signalvar.h> 7853541Sshin#include <sys/socket.h> 7953541Sshin#include <sys/socketvar.h> 8095759Stanimura#include <sys/sx.h> 81148385Sume#include <sys/syslog.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8495759Stanimura#include <net/if_types.h> 8553541Sshin#include <net/route.h> 86185571Sbz#include <net/vnet.h> 8753541Sshin 8853541Sshin#include <netinet/in.h> 8953541Sshin#include <netinet/in_var.h> 9053541Sshin#include <netinet/in_systm.h> 91185571Sbz#include <netinet/in_pcb.h> 92185571Sbz 9395759Stanimura#include <netinet/icmp6.h> 9462587Sitojun#include <netinet/ip6.h> 9595759Stanimura#include <netinet6/ip6protosw.h> 9656723Sshin#include <netinet6/ip6_mroute.h> 9753541Sshin#include <netinet6/in6_pcb.h> 9895759Stanimura#include <netinet6/ip6_var.h> 9953541Sshin#include <netinet6/nd6.h> 10095759Stanimura#include <netinet6/raw_ip6.h> 10162587Sitojun#include <netinet6/scope6_var.h> 10253541Sshin 103171167Sgnn#ifdef IPSEC 104105199Ssam#include <netipsec/ipsec.h> 105105199Ssam#include <netipsec/ipsec6.h> 106171167Sgnn#endif /* IPSEC */ 107105199Ssam 10853541Sshin#include <machine/stdarg.h> 10953541Sshin 11053541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11153541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11253541Sshin 11353541Sshin/* 11453541Sshin * Raw interface to IP6 protocol. 11553541Sshin */ 11653541Sshin 117195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb); 118195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo); 119195727Srwatson#define V_ripcb VNET(ripcb) 120195727Srwatson#define V_ripcbinfo VNET(ripcbinfo) 121185348Szec 122195699SrwatsonVNET_DEFINE(struct rip6stat, rip6stat); 123195699Srwatson 12453541Sshinextern u_long rip_sendspace; 12553541Sshinextern u_long rip_recvspace; 12653541Sshin 12753541Sshin/* 128191672Sbms * Hooks for multicast routing. They all default to NULL, so leave them not 129191672Sbms * initialized and rely on BSS being set to 0. 130166938Sbms */ 131191672Sbms 132191672Sbms/* 133191672Sbms * The socket used to communicate with the multicast routing daemon. 134191672Sbms */ 135195699SrwatsonVNET_DEFINE(struct socket *, ip6_mrouter); 136191672Sbms 137191672Sbms/* 138191672Sbms * The various mrouter functions. 139191672Sbms */ 140166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 141166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 142166938Sbmsint (*ip6_mrouter_done)(void); 143166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 144194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t); 145166938Sbms 146166938Sbms/* 147180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 148180305Srwatson * pass them along with mbuf chain. 14953541Sshin */ 15053541Sshinint 151171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 15253541Sshin{ 153191672Sbms struct ifnet *ifp; 15453541Sshin struct mbuf *m = *mp; 15553541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 15653541Sshin register struct inpcb *in6p; 15753541Sshin struct inpcb *last = 0; 15878064Sume struct mbuf *opts = NULL; 159121901Sume struct sockaddr_in6 fromsa; 16053541Sshin 161181803Sbz V_rip6stat.rip6s_ipackets++; 16278064Sume 16383934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 164180305Srwatson /* XXX Send icmp6 host/port unreach? */ 16578064Sume m_freem(m); 166180305Srwatson return (IPPROTO_DONE); 16753541Sshin } 16878064Sume 169121901Sume init_sin6(&fromsa, m); /* general init */ 17053541Sshin 171191672Sbms ifp = m->m_pkthdr.rcvif; 172191672Sbms 173181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 174181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 175185435Sbz /* XXX inp locking */ 176186141Sbz if ((in6p->inp_vflag & INP_IPV6) == 0) 17753541Sshin continue; 178186141Sbz if (in6p->inp_ip_p && 179186141Sbz in6p->inp_ip_p != proto) 180180850Smav continue; 18153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 18253541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 183180850Smav continue; 18453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 18553541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 186180850Smav continue; 187200473Sbz if (jailed_without_vnet(in6p->inp_cred)) { 188191672Sbms /* 189191672Sbms * Allow raw socket in jail to receive multicast; 190191672Sbms * assume process had PRIV_NETINET_RAW at attach, 191191672Sbms * and fall through into normal filter path if so. 192191672Sbms */ 193191672Sbms if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 194191672Sbms prison_check_ip6(in6p->inp_cred, 195191672Sbms &ip6->ip6_dst) != 0) 196191672Sbms continue; 197191672Sbms } 19878064Sume if (in6p->in6p_cksum != -1) { 199181803Sbz V_rip6stat.rip6s_isum++; 200151459Ssuz if (in6_cksum(m, proto, *offp, 20178064Sume m->m_pkthdr.len - *offp)) { 202180932Smav INP_RUNLOCK(in6p); 203181803Sbz V_rip6stat.rip6s_badsum++; 204180850Smav continue; 20578064Sume } 20653541Sshin } 207191672Sbms INP_RLOCK(in6p); 208191672Sbms /* 209191672Sbms * If this raw socket has multicast state, and we 210191672Sbms * have received a multicast, check if this socket 211191672Sbms * should receive it, as multicast filtering is now 212191672Sbms * the responsibility of the transport layer. 213191672Sbms */ 214191672Sbms if (in6p->in6p_moptions && 215191672Sbms IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 216199518Sbms /* 217199518Sbms * If the incoming datagram is for MLD, allow it 218199518Sbms * through unconditionally to the raw socket. 219199518Sbms * 220199518Sbms * Use the M_RTALERT_MLD flag to check for MLD 221199518Sbms * traffic without having to inspect the mbuf chain 222199518Sbms * more deeply, as all MLDv1/v2 host messages MUST 223199518Sbms * contain the Router Alert option. 224199518Sbms * 225199518Sbms * In the case of MLDv1, we may not have explicitly 226199518Sbms * joined the group, and may have set IFF_ALLMULTI 227199518Sbms * on the interface. im6o_mc_filter() may discard 228199518Sbms * control traffic we actually need to see. 229199518Sbms * 230199518Sbms * Userland multicast routing daemons should continue 231199518Sbms * filter the control traffic appropriately. 232199518Sbms */ 233191672Sbms int blocked; 234191672Sbms 235199518Sbms blocked = MCAST_PASS; 236199518Sbms if ((m->m_flags & M_RTALERT_MLD) == 0) { 237199518Sbms struct sockaddr_in6 mcaddr; 238191672Sbms 239199518Sbms bzero(&mcaddr, sizeof(struct sockaddr_in6)); 240199518Sbms mcaddr.sin6_len = sizeof(struct sockaddr_in6); 241199518Sbms mcaddr.sin6_family = AF_INET6; 242199518Sbms mcaddr.sin6_addr = ip6->ip6_dst; 243199518Sbms 244199518Sbms blocked = im6o_mc_filter(in6p->in6p_moptions, 245199518Sbms ifp, 246199518Sbms (struct sockaddr *)&mcaddr, 247199518Sbms (struct sockaddr *)&fromsa); 248199518Sbms } 249191672Sbms if (blocked != MCAST_PASS) { 250191672Sbms IP6STAT_INC(ip6s_notmember); 251191672Sbms continue; 252191672Sbms } 253191672Sbms } 254186163Skmacy if (last != NULL) { 25553541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 25678064Sume 257171167Sgnn#ifdef IPSEC 25878064Sume /* 25978064Sume * Check AH/ESP integrity. 26078064Sume */ 261125396Sume if (n && ipsec6_in_reject(n, last)) { 26278064Sume m_freem(n); 263181803Sbz V_ipsec6stat.in_polvio++; 264180305Srwatson /* Do not inject data into pcb. */ 265105199Ssam } else 266171167Sgnn#endif /* IPSEC */ 26753541Sshin if (n) { 268186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 269186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 270121674Sume ip6_savecontrol(last, n, &opts); 27153541Sshin /* strip intermediate headers */ 27253541Sshin m_adj(n, *offp); 273186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 274121901Sume (struct sockaddr *)&fromsa, 27553541Sshin n, opts) == 0) { 27653541Sshin m_freem(n); 27753541Sshin if (opts) 27853541Sshin m_freem(opts); 279181803Sbz V_rip6stat.rip6s_fullsock++; 28097658Stanimura } else 281186141Sbz sorwakeup(last->inp_socket); 28253541Sshin opts = NULL; 28353541Sshin } 284178377Srwatson INP_RUNLOCK(last); 28553541Sshin } 28653541Sshin last = in6p; 28753541Sshin } 288181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 289171167Sgnn#ifdef IPSEC 29078064Sume /* 29178064Sume * Check AH/ESP integrity. 29278064Sume */ 293186170Skmacy if ((last != NULL) && ipsec6_in_reject(m, last)) { 29478064Sume m_freem(m); 295181803Sbz V_ipsec6stat.in_polvio++; 296181803Sbz V_ip6stat.ip6s_delivered--; 297180305Srwatson /* Do not inject data into pcb. */ 298178377Srwatson INP_RUNLOCK(last); 299105199Ssam } else 300171167Sgnn#endif /* IPSEC */ 301186163Skmacy if (last != NULL) { 302186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 303186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 304121674Sume ip6_savecontrol(last, m, &opts); 305180305Srwatson /* Strip intermediate headers. */ 30653541Sshin m_adj(m, *offp); 307186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 308180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 30953541Sshin m_freem(m); 31053541Sshin if (opts) 31153541Sshin m_freem(opts); 312181803Sbz V_rip6stat.rip6s_fullsock++; 31397658Stanimura } else 314186141Sbz sorwakeup(last->inp_socket); 315178377Srwatson INP_RUNLOCK(last); 31653541Sshin } else { 317181803Sbz V_rip6stat.rip6s_nosock++; 31878064Sume if (m->m_flags & M_MCAST) 319181803Sbz V_rip6stat.rip6s_nosockmcast++; 32053541Sshin if (proto == IPPROTO_NONE) 32153541Sshin m_freem(m); 32253541Sshin else { 32353541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 32453541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 325180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 326180305Srwatson prvnxtp - mtod(m, char *)); 32753541Sshin } 328181803Sbz V_ip6stat.ip6s_delivered--; 32953541Sshin } 330180305Srwatson return (IPPROTO_DONE); 33153541Sshin} 33253541Sshin 33362587Sitojunvoid 334171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 33562587Sitojun{ 33662587Sitojun struct ip6_hdr *ip6; 33762587Sitojun struct mbuf *m; 33862587Sitojun int off = 0; 33978064Sume struct ip6ctlparam *ip6cp = NULL; 34078064Sume const struct sockaddr_in6 *sa6_src = NULL; 341125776Sume void *cmdarg; 342175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 34362587Sitojun 34462587Sitojun if (sa->sa_family != AF_INET6 || 34562587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 34662587Sitojun return; 34762587Sitojun 34862587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 34962587Sitojun return; 35062587Sitojun if (PRC_IS_REDIRECT(cmd)) 35162587Sitojun notify = in6_rtchange, d = NULL; 35262587Sitojun else if (cmd == PRC_HOSTDEAD) 35362587Sitojun d = NULL; 35462587Sitojun else if (inet6ctlerrmap[cmd] == 0) 35562587Sitojun return; 35662587Sitojun 357180305Srwatson /* 358180305Srwatson * If the parameter is from icmp6, decode it. 359180305Srwatson */ 36062587Sitojun if (d != NULL) { 36178064Sume ip6cp = (struct ip6ctlparam *)d; 36262587Sitojun m = ip6cp->ip6c_m; 36362587Sitojun ip6 = ip6cp->ip6c_ip6; 36462587Sitojun off = ip6cp->ip6c_off; 365125776Sume cmdarg = ip6cp->ip6c_cmdarg; 36678064Sume sa6_src = ip6cp->ip6c_src; 36762587Sitojun } else { 36862587Sitojun m = NULL; 36962587Sitojun ip6 = NULL; 370125776Sume cmdarg = NULL; 37178064Sume sa6_src = &sa6_any; 37262587Sitojun } 37362587Sitojun 374181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 375180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 37662587Sitojun} 37762587Sitojun 37853541Sshin/* 379180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 380180305Srwatson * may have setup with control call. 38153541Sshin */ 38253541Sshinint 38353541Sshin#if __STDC__ 38453541Sshinrip6_output(struct mbuf *m, ...) 38553541Sshin#else 38653541Sshinrip6_output(m, va_alist) 38753541Sshin struct mbuf *m; 38853541Sshin va_dcl 38953541Sshin#endif 39053541Sshin{ 391120941Sume struct mbuf *control; 39253541Sshin struct socket *so; 39353541Sshin struct sockaddr_in6 *dstsock; 39453541Sshin struct in6_addr *dst; 39553541Sshin struct ip6_hdr *ip6; 39653541Sshin struct inpcb *in6p; 39753541Sshin u_int plen = m->m_pkthdr.len; 39853541Sshin int error = 0; 399148247Sume struct ip6_pktopts opt, *optp; 40053541Sshin struct ifnet *oifp = NULL; 40153541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 402148385Sume int scope_ambiguous = 0; 403194777Sbz struct in6_addr in6a; 40453541Sshin va_list ap; 40553541Sshin 40653541Sshin va_start(ap, m); 40753541Sshin so = va_arg(ap, struct socket *); 40853541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 40953541Sshin control = va_arg(ap, struct mbuf *); 41053541Sshin va_end(ap); 41153541Sshin 412186141Sbz in6p = sotoinpcb(so); 413178285Srwatson INP_WLOCK(in6p); 41453541Sshin 41553541Sshin dst = &dstsock->sin6_addr; 416186170Skmacy if (control != NULL) { 417148242Sume if ((error = ip6_setpktopts(control, &opt, 418175630Sbz in6p->in6p_outputopts, so->so_cred, 419175630Sbz so->so_proto->pr_protocol)) != 0) { 42053541Sshin goto bad; 421121472Sume } 422148247Sume optp = &opt; 423148247Sume } else 424148247Sume optp = in6p->in6p_outputopts; 42553541Sshin 42653541Sshin /* 427148385Sume * Check and convert scope zone ID into internal form. 428180305Srwatson * 429148385Sume * XXX: we may still need to determine the zone later. 430148385Sume */ 431148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 432181803Sbz if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone) 433148385Sume scope_ambiguous = 1; 434181803Sbz if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0) 435148385Sume goto bad; 436148385Sume } 437148385Sume 438148385Sume /* 439180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 440180305Srwatson * statistics. 44153541Sshin */ 44253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 44353541Sshin struct icmp6_hdr *icmp6; 44453541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 44553541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 44653541Sshin error = ENOBUFS; 44753541Sshin goto bad; 44853541Sshin } 44953541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 45053541Sshin type = icmp6->icmp6_type; 45153541Sshin code = icmp6->icmp6_code; 45253541Sshin } 45353541Sshin 454133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 455133592Srwatson if (m == NULL) { 456133592Srwatson error = ENOBUFS; 457133592Srwatson goto bad; 458133592Srwatson } 45953541Sshin ip6 = mtod(m, struct ip6_hdr *); 46053541Sshin 46153541Sshin /* 46253541Sshin * Source address selection. 46353541Sshin */ 464194777Sbz error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 465194777Sbz &oifp, &in6a); 466194777Sbz if (error) 467121472Sume goto bad; 468194777Sbz error = prison_get_ip6(in6p->inp_cred, &in6a); 469188144Sjamie if (error != 0) 470188144Sjamie goto bad; 471194777Sbz ip6->ip6_src = in6a; 472148385Sume 473148385Sume if (oifp && scope_ambiguous) { 474148385Sume /* 475148385Sume * Application should provide a proper zone ID or the use of 476148385Sume * default zone IDs should be enabled. Unfortunately, some 477148385Sume * applications do not behave as it should, so we need a 478148385Sume * workaround. Even if an appropriate ID is not determined 479148385Sume * (when it's required), if we can determine the outgoing 480148385Sume * interface. determine the zone ID based on the interface. 481148385Sume */ 482148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 483148385Sume if (error != 0) 484148385Sume goto bad; 485148385Sume } 486148385Sume ip6->ip6_dst = dstsock->sin6_addr; 487148385Sume 488180305Srwatson /* 489180305Srwatson * Fill in the rest of the IPv6 header fields. 490180305Srwatson */ 49155009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 492186141Sbz (in6p->inp_flow & IPV6_FLOWINFO_MASK); 49355009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 494180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 495180305Srwatson 496180305Srwatson /* 497180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 498180305Srwatson */ 499186141Sbz ip6->ip6_nxt = in6p->inp_ip_p; 50053541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 50153541Sshin 50253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 50353541Sshin in6p->in6p_cksum != -1) { 50453541Sshin struct mbuf *n; 50553541Sshin int off; 50653541Sshin u_int16_t *p; 50753541Sshin 508180305Srwatson /* Compute checksum. */ 50953541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 51053541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 51153541Sshin else 51253541Sshin off = in6p->in6p_cksum; 51353541Sshin if (plen < off + 1) { 51453541Sshin error = EINVAL; 51553541Sshin goto bad; 51653541Sshin } 51753541Sshin off += sizeof(struct ip6_hdr); 51853541Sshin 51953541Sshin n = m; 52053541Sshin while (n && n->m_len <= off) { 52153541Sshin off -= n->m_len; 52253541Sshin n = n->m_next; 52353541Sshin } 52453541Sshin if (!n) 52553541Sshin goto bad; 52653541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 52753541Sshin *p = 0; 52853541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 52953541Sshin } 53053541Sshin 531148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 53253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 53353541Sshin if (oifp) 53453541Sshin icmp6_ifoutstat_inc(oifp, type, code); 535190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 53678064Sume } else 537181803Sbz V_rip6stat.rip6s_opackets++; 53853541Sshin 53953541Sshin goto freectl; 54053541Sshin 54153541Sshin bad: 54253541Sshin if (m) 54353541Sshin m_freem(m); 54453541Sshin 54553541Sshin freectl: 546186170Skmacy if (control != NULL) { 547148247Sume ip6_clearpktopts(&opt, -1); 54853541Sshin m_freem(control); 54978064Sume } 550178285Srwatson INP_WUNLOCK(in6p); 551120856Sume return (error); 55253541Sshin} 55353541Sshin 55453541Sshin/* 55553541Sshin * Raw IPv6 socket option processing. 55653541Sshin */ 55753541Sshinint 558171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 55953541Sshin{ 56053541Sshin int error; 56153541Sshin 56253541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 56353541Sshin /* 56453541Sshin * XXX: is it better to call icmp6_ctloutput() directly 56553541Sshin * from protosw? 56653541Sshin */ 567120856Sume return (icmp6_ctloutput(so, sopt)); 56853541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 56953541Sshin return (EINVAL); 57053541Sshin 57153541Sshin error = 0; 57253541Sshin 57353541Sshin switch (sopt->sopt_dir) { 57453541Sshin case SOPT_GET: 57553541Sshin switch (sopt->sopt_name) { 57656723Sshin case MRT6_INIT: 57756723Sshin case MRT6_DONE: 57856723Sshin case MRT6_ADD_MIF: 57956723Sshin case MRT6_DEL_MIF: 58056723Sshin case MRT6_ADD_MFC: 58156723Sshin case MRT6_DEL_MFC: 58256723Sshin case MRT6_PIM: 583166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 584166938Sbms EOPNOTSUPP; 58556723Sshin break; 586121578Sume case IPV6_CHECKSUM: 587121578Sume error = ip6_raw_ctloutput(so, sopt); 588121578Sume break; 58953541Sshin default: 59053541Sshin error = ip6_ctloutput(so, sopt); 59153541Sshin break; 59253541Sshin } 59353541Sshin break; 59453541Sshin 59553541Sshin case SOPT_SET: 59653541Sshin switch (sopt->sopt_name) { 59756723Sshin case MRT6_INIT: 59856723Sshin case MRT6_DONE: 59956723Sshin case MRT6_ADD_MIF: 60056723Sshin case MRT6_DEL_MIF: 60156723Sshin case MRT6_ADD_MFC: 60256723Sshin case MRT6_DEL_MFC: 60356723Sshin case MRT6_PIM: 604166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 605166938Sbms EOPNOTSUPP; 60656723Sshin break; 607121578Sume case IPV6_CHECKSUM: 608121578Sume error = ip6_raw_ctloutput(so, sopt); 609121578Sume break; 61053541Sshin default: 61153541Sshin error = ip6_ctloutput(so, sopt); 61253541Sshin break; 61353541Sshin } 61453541Sshin break; 61553541Sshin } 61653541Sshin 61753541Sshin return (error); 61853541Sshin} 61953541Sshin 62053541Sshinstatic int 62183366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 62253541Sshin{ 62353541Sshin struct inpcb *inp; 624144261Ssam struct icmp6_filter *filter; 625157676Srwatson int error; 62653541Sshin 62753541Sshin inp = sotoinpcb(so); 628157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 629180305Srwatson 630175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 631175630Sbz if (error) 632180305Srwatson return (error); 63355009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 634157374Srwatson if (error) 635180305Srwatson return (error); 636184214Sdes filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 637157374Srwatson if (filter == NULL) 638180305Srwatson return (ENOMEM); 639181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 640181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 641132714Srwatson if (error) { 642181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 643184205Sdes free(filter, M_PCB); 644180305Srwatson return (error); 645132714Srwatson } 64653541Sshin inp = (struct inpcb *)so->so_pcb; 647181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 64853541Sshin inp->inp_vflag |= INP_IPV6; 649186141Sbz inp->inp_ip_p = (long)proto; 65053541Sshin inp->in6p_hops = -1; /* use kernel default */ 65153541Sshin inp->in6p_cksum = -1; 652144261Ssam inp->in6p_icmp6filt = filter; 65353541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 654178285Srwatson INP_WUNLOCK(inp); 655180305Srwatson return (0); 65653541Sshin} 65753541Sshin 658157370Srwatsonstatic void 65953541Sshinrip6_detach(struct socket *so) 66053541Sshin{ 66153541Sshin struct inpcb *inp; 66253541Sshin 66353541Sshin inp = sotoinpcb(so); 664157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 665160549Srwatson 666191672Sbms if (so == V_ip6_mrouter && ip6_mrouter_done) 667166938Sbms ip6_mrouter_done(); 66853541Sshin /* xxx: RSVP */ 669181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 670178285Srwatson INP_WLOCK(inp); 671184205Sdes free(inp->in6p_icmp6filt, M_PCB); 672185344Sbz in_pcbdetach(inp); 673185370Sbz in_pcbfree(inp); 674181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 67553541Sshin} 67653541Sshin 677160549Srwatson/* XXXRW: This can't ever be called. */ 678157366Srwatsonstatic void 67953541Sshinrip6_abort(struct socket *so) 68053541Sshin{ 681160549Srwatson struct inpcb *inp; 682160549Srwatson 683160549Srwatson inp = sotoinpcb(so); 684160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 685160549Srwatson 68653541Sshin soisdisconnected(so); 68753541Sshin} 68853541Sshin 689160549Srwatsonstatic void 690160549Srwatsonrip6_close(struct socket *so) 691160549Srwatson{ 692160549Srwatson struct inpcb *inp; 693160549Srwatson 694160549Srwatson inp = sotoinpcb(so); 695160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 696160549Srwatson 697160549Srwatson soisdisconnected(so); 698160549Srwatson} 699160549Srwatson 70053541Sshinstatic int 70153541Sshinrip6_disconnect(struct socket *so) 70253541Sshin{ 703180305Srwatson struct inpcb *inp; 70453541Sshin 705180305Srwatson inp = sotoinpcb(so); 706180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 707180305Srwatson 70897658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 709180305Srwatson return (ENOTCONN); 71053541Sshin inp->in6p_faddr = in6addr_any; 711157366Srwatson rip6_abort(so); 712157374Srwatson return (0); 71353541Sshin} 71453541Sshin 71553541Sshinstatic int 71683366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 71753541Sshin{ 718180305Srwatson struct inpcb *inp; 71953541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 720194760Srwatson struct ifaddr *ifa = NULL; 721148385Sume int error = 0; 72253541Sshin 723180305Srwatson inp = sotoinpcb(so); 724157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 725180305Srwatson 72653541Sshin if (nam->sa_len != sizeof(*addr)) 727180305Srwatson return (EINVAL); 728188144Sjamie if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 729188144Sjamie return (error); 730181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 731180305Srwatson return (EADDRNOTAVAIL); 732181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 733180305Srwatson return (error); 734148385Sume 73553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 736194760Srwatson (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) 737180305Srwatson return (EADDRNOTAVAIL); 738194760Srwatson if (ifa != NULL && 739194760Srwatson ((struct in6_ifaddr *)ifa)->ia6_flags & 74053541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 74153541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 742194760Srwatson ifa_free(ifa); 743120856Sume return (EADDRNOTAVAIL); 74453541Sshin } 745194760Srwatson if (ifa != NULL) 746194760Srwatson ifa_free(ifa); 747181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 748178285Srwatson INP_WLOCK(inp); 74953541Sshin inp->in6p_laddr = addr->sin6_addr; 750178285Srwatson INP_WUNLOCK(inp); 751181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 752180305Srwatson return (0); 75353541Sshin} 75453541Sshin 75553541Sshinstatic int 75683366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 75753541Sshin{ 758180305Srwatson struct inpcb *inp; 75953541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 760194777Sbz struct in6_addr in6a; 761148385Sume struct ifnet *ifp = NULL; 762148385Sume int error = 0, scope_ambiguous = 0; 76353541Sshin 764180305Srwatson inp = sotoinpcb(so); 765157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 766180305Srwatson 76753541Sshin if (nam->sa_len != sizeof(*addr)) 768180305Srwatson return (EINVAL); 769181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 770180305Srwatson return (EADDRNOTAVAIL); 77153541Sshin if (addr->sin6_family != AF_INET6) 772180305Srwatson return (EAFNOSUPPORT); 773148385Sume 774148385Sume /* 775180305Srwatson * Application should provide a proper zone ID or the use of default 776180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 777180305Srwatson * not behave as it should, so we need a workaround. Even if an 778180305Srwatson * appropriate ID is not determined, we'll see if we can determine 779180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 780180305Srwatson * the interface below. 781148385Sume */ 782181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 783148385Sume scope_ambiguous = 1; 784181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 785180305Srwatson return (error); 786148385Sume 787181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 788178285Srwatson INP_WLOCK(inp); 78953541Sshin /* Source address selection. XXX: need pcblookup? */ 790194777Sbz error = in6_selectsrc(addr, inp->in6p_outputopts, 791194777Sbz inp, NULL, so->so_cred, &ifp, &in6a); 792194777Sbz if (error) { 793178285Srwatson INP_WUNLOCK(inp); 794181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 795194777Sbz return (error); 796132714Srwatson } 797148385Sume 798148385Sume /* XXX: see above */ 799148385Sume if (ifp && scope_ambiguous && 800148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 801178285Srwatson INP_WUNLOCK(inp); 802181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 803180305Srwatson return (error); 804148385Sume } 805148385Sume inp->in6p_faddr = addr->sin6_addr; 806194777Sbz inp->in6p_laddr = in6a; 80753541Sshin soisconnected(so); 808178285Srwatson INP_WUNLOCK(inp); 809181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 810180305Srwatson return (0); 81153541Sshin} 81253541Sshin 81353541Sshinstatic int 81453541Sshinrip6_shutdown(struct socket *so) 81553541Sshin{ 816132714Srwatson struct inpcb *inp; 817132714Srwatson 818132714Srwatson inp = sotoinpcb(so); 819157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 820180305Srwatson 821178285Srwatson INP_WLOCK(inp); 82253541Sshin socantsendmore(so); 823178285Srwatson INP_WUNLOCK(inp); 824180305Srwatson return (0); 82553541Sshin} 82653541Sshin 82753541Sshinstatic int 82853541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 829171260Sdelphij struct mbuf *control, struct thread *td) 83053541Sshin{ 831180305Srwatson struct inpcb *inp; 83253541Sshin struct sockaddr_in6 tmp; 83353541Sshin struct sockaddr_in6 *dst; 834132714Srwatson int ret; 83553541Sshin 836180305Srwatson inp = sotoinpcb(so); 837157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 838180305Srwatson 839180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 840132714Srwatson /* Unlocked read. */ 84153541Sshin if (so->so_state & SS_ISCONNECTED) { 84253541Sshin if (nam) { 84353541Sshin m_freem(m); 844180305Srwatson return (EISCONN); 84553541Sshin } 84653541Sshin /* XXX */ 84753541Sshin bzero(&tmp, sizeof(tmp)); 84853541Sshin tmp.sin6_family = AF_INET6; 84953541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 850180990Srwatson INP_RLOCK(inp); 85153541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 852180990Srwatson sizeof(struct in6_addr)); 853180990Srwatson INP_RUNLOCK(inp); 85453541Sshin dst = &tmp; 85553541Sshin } else { 85653541Sshin if (nam == NULL) { 85753541Sshin m_freem(m); 858180305Srwatson return (ENOTCONN); 85953541Sshin } 860148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 861148385Sume m_freem(m); 862180305Srwatson return (EINVAL); 863148385Sume } 86462587Sitojun tmp = *(struct sockaddr_in6 *)nam; 86562587Sitojun dst = &tmp; 866148385Sume 867148385Sume if (dst->sin6_family == AF_UNSPEC) { 868148385Sume /* 869148385Sume * XXX: we allow this case for backward 870148385Sume * compatibility to buggy applications that 871148385Sume * rely on old (and wrong) kernel behavior. 872148385Sume */ 873148385Sume log(LOG_INFO, "rip6 SEND: address family is " 874148385Sume "unspec. Assume AF_INET6\n"); 875148385Sume dst->sin6_family = AF_INET6; 876148385Sume } else if (dst->sin6_family != AF_INET6) { 877148385Sume m_freem(m); 878148385Sume return(EAFNOSUPPORT); 879148385Sume } 88053541Sshin } 881132714Srwatson ret = rip6_output(m, so, dst, control); 882132714Srwatson return (ret); 88353541Sshin} 88453541Sshin 88553541Sshinstruct pr_usrreqs rip6_usrreqs = { 886137386Sphk .pru_abort = rip6_abort, 887137386Sphk .pru_attach = rip6_attach, 888137386Sphk .pru_bind = rip6_bind, 889137386Sphk .pru_connect = rip6_connect, 890137386Sphk .pru_control = in6_control, 891137386Sphk .pru_detach = rip6_detach, 892137386Sphk .pru_disconnect = rip6_disconnect, 893169462Srwatson .pru_peeraddr = in6_getpeeraddr, 894137386Sphk .pru_send = rip6_send, 895137386Sphk .pru_shutdown = rip6_shutdown, 896169462Srwatson .pru_sockaddr = in6_getsockaddr, 897160549Srwatson .pru_close = rip6_close, 89853541Sshin}; 899