raw_ip6.c revision 137386
153541Sshin/* 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 * $FreeBSD: head/sys/netinet6/raw_ip6.c 137386 2004-11-08 14:44:54Z phk $ 3053541Sshin */ 3153541Sshin 3253541Sshin/* 3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 6355009Sshin#include "opt_ipsec.h" 6478064Sume#include "opt_inet6.h" 6555009Sshin 6653541Sshin#include <sys/param.h> 6795759Stanimura#include <sys/errno.h> 6895759Stanimura#include <sys/lock.h> 6953541Sshin#include <sys/malloc.h> 7095759Stanimura#include <sys/mbuf.h> 7153541Sshin#include <sys/proc.h> 7295759Stanimura#include <sys/protosw.h> 7395759Stanimura#include <sys/signalvar.h> 7453541Sshin#include <sys/socket.h> 7553541Sshin#include <sys/socketvar.h> 7695759Stanimura#include <sys/sx.h> 7753541Sshin#include <sys/systm.h> 7853541Sshin 7953541Sshin#include <net/if.h> 8095759Stanimura#include <net/if_types.h> 8153541Sshin#include <net/route.h> 8253541Sshin 8353541Sshin#include <netinet/in.h> 8453541Sshin#include <netinet/in_var.h> 8553541Sshin#include <netinet/in_systm.h> 8695759Stanimura#include <netinet/icmp6.h> 8795759Stanimura#include <netinet/in_pcb.h> 8862587Sitojun#include <netinet/ip6.h> 8995759Stanimura#include <netinet6/ip6protosw.h> 9056723Sshin#include <netinet6/ip6_mroute.h> 9153541Sshin#include <netinet6/in6_pcb.h> 9295759Stanimura#include <netinet6/ip6_var.h> 9353541Sshin#include <netinet6/nd6.h> 9495759Stanimura#include <netinet6/raw_ip6.h> 9562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 9662587Sitojun#include <netinet6/scope6_var.h> 9762587Sitojun#endif 9853541Sshin 9953541Sshin#ifdef IPSEC 10053541Sshin#include <netinet6/ipsec.h> 10153541Sshin#include <netinet6/ipsec6.h> 10253541Sshin#endif /*IPSEC*/ 10353541Sshin 104105199Ssam#ifdef FAST_IPSEC 105105199Ssam#include <netipsec/ipsec.h> 106105199Ssam#include <netipsec/ipsec6.h> 107105199Ssam#endif /* FAST_IPSEC */ 108105199Ssam 10953541Sshin#include <machine/stdarg.h> 11053541Sshin 11153541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11253541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11353541Sshin 11453541Sshin/* 11553541Sshin * Raw interface to IP6 protocol. 11653541Sshin */ 11753541Sshin 11853541Sshinextern struct inpcbhead ripcb; 11953541Sshinextern struct inpcbinfo ripcbinfo; 12053541Sshinextern u_long rip_sendspace; 12153541Sshinextern u_long rip_recvspace; 12253541Sshin 12378064Sumestruct rip6stat rip6stat; 12478064Sume 12553541Sshin/* 12653541Sshin * Setup generic address and protocol structures 12753541Sshin * for raw_input routine, then pass them along with 12853541Sshin * mbuf chain. 12953541Sshin */ 13053541Sshinint 13153541Sshinrip6_input(mp, offp, proto) 13253541Sshin struct mbuf **mp; 13353541Sshin int *offp, proto; 13453541Sshin{ 13553541Sshin struct mbuf *m = *mp; 13653541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 13753541Sshin register struct inpcb *in6p; 13853541Sshin struct inpcb *last = 0; 13978064Sume struct mbuf *opts = NULL; 140121901Sume struct sockaddr_in6 fromsa; 14153541Sshin 14278064Sume rip6stat.rip6s_ipackets++; 14378064Sume 14483934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 14578064Sume /* XXX send icmp6 host/port unreach? */ 14678064Sume m_freem(m); 14778064Sume return IPPROTO_DONE; 14853541Sshin } 14978064Sume 150121901Sume init_sin6(&fromsa, m); /* general init */ 15153541Sshin 152132714Srwatson INP_INFO_RLOCK(&ripcbinfo); 15353541Sshin LIST_FOREACH(in6p, &ripcb, inp_list) { 154132714Srwatson INP_LOCK(in6p); 155132714Srwatson if ((in6p->in6p_vflag & INP_IPV6) == 0) { 156132714Srwatsondocontinue: 157132714Srwatson INP_UNLOCK(in6p); 15853541Sshin continue; 159132714Srwatson } 16053541Sshin if (in6p->in6p_ip6_nxt && 16153541Sshin in6p->in6p_ip6_nxt != proto) 162132714Srwatson goto docontinue; 16353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 16453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 165132714Srwatson goto docontinue; 16653541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 16753541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 168132714Srwatson goto docontinue; 16978064Sume if (in6p->in6p_cksum != -1) { 17078064Sume rip6stat.rip6s_isum++; 17178064Sume if (in6_cksum(m, ip6->ip6_nxt, *offp, 17278064Sume m->m_pkthdr.len - *offp)) { 17378064Sume rip6stat.rip6s_badsum++; 174132714Srwatson goto docontinue; 17578064Sume } 17653541Sshin } 17753541Sshin if (last) { 17853541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 17978064Sume 180125941Sume#if defined(IPSEC) || defined(FAST_IPSEC) 18178064Sume /* 18278064Sume * Check AH/ESP integrity. 18378064Sume */ 184125396Sume if (n && ipsec6_in_reject(n, last)) { 18578064Sume m_freem(n); 186125941Sume#ifdef IPSEC 18778064Sume ipsec6stat.in_polvio++; 18878064Sume#endif /*IPSEC*/ 189105199Ssam /* do not inject data into pcb */ 190105199Ssam } else 191125941Sume#endif /*IPSEC || FAST_IPSEC*/ 19253541Sshin if (n) { 19397658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 19497658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 195121674Sume ip6_savecontrol(last, n, &opts); 19653541Sshin /* strip intermediate headers */ 19753541Sshin m_adj(n, *offp); 19853541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 199121901Sume (struct sockaddr *)&fromsa, 20053541Sshin n, opts) == 0) { 20153541Sshin m_freem(n); 20253541Sshin if (opts) 20353541Sshin m_freem(opts); 20478064Sume rip6stat.rip6s_fullsock++; 20597658Stanimura } else 20653541Sshin sorwakeup(last->in6p_socket); 20753541Sshin opts = NULL; 20853541Sshin } 209132714Srwatson INP_UNLOCK(last); 21053541Sshin } 21153541Sshin last = in6p; 21253541Sshin } 213125941Sume#if defined(IPSEC) || defined(FAST_IPSEC) 21478064Sume /* 21578064Sume * Check AH/ESP integrity. 21678064Sume */ 217125396Sume if (last && ipsec6_in_reject(m, last)) { 21878064Sume m_freem(m); 219125941Sume#ifdef IPSEC 22078064Sume ipsec6stat.in_polvio++; 22178064Sume#endif /*IPSEC*/ 222105199Ssam ip6stat.ip6s_delivered--; 223105199Ssam /* do not inject data into pcb */ 224105199Ssam } else 225125941Sume#endif /*IPSEC || FAST_IPSEC*/ 22653541Sshin if (last) { 22797658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 22897658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 229121674Sume ip6_savecontrol(last, m, &opts); 23053541Sshin /* strip intermediate headers */ 23153541Sshin m_adj(m, *offp); 23253541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 233121901Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 23453541Sshin m_freem(m); 23553541Sshin if (opts) 23653541Sshin m_freem(opts); 23778064Sume rip6stat.rip6s_fullsock++; 23897658Stanimura } else 23953541Sshin sorwakeup(last->in6p_socket); 240132714Srwatson INP_UNLOCK(last); 24153541Sshin } else { 24278064Sume rip6stat.rip6s_nosock++; 24378064Sume if (m->m_flags & M_MCAST) 24478064Sume rip6stat.rip6s_nosockmcast++; 24553541Sshin if (proto == IPPROTO_NONE) 24653541Sshin m_freem(m); 24753541Sshin else { 24853541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 24953541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 25053541Sshin ICMP6_PARAMPROB_NEXTHEADER, 25153541Sshin prvnxtp - mtod(m, char *)); 25253541Sshin } 25353541Sshin ip6stat.ip6s_delivered--; 25453541Sshin } 255134655Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 25653541Sshin return IPPROTO_DONE; 25753541Sshin} 25853541Sshin 25962587Sitojunvoid 26062587Sitojunrip6_ctlinput(cmd, sa, d) 26162587Sitojun int cmd; 26262587Sitojun struct sockaddr *sa; 26362587Sitojun void *d; 26462587Sitojun{ 26562587Sitojun struct ip6_hdr *ip6; 26662587Sitojun struct mbuf *m; 26762587Sitojun int off = 0; 26878064Sume struct ip6ctlparam *ip6cp = NULL; 26978064Sume const struct sockaddr_in6 *sa6_src = NULL; 270125776Sume void *cmdarg; 27198211Shsu struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange; 27262587Sitojun 27362587Sitojun if (sa->sa_family != AF_INET6 || 27462587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 27562587Sitojun return; 27662587Sitojun 27762587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 27862587Sitojun return; 27962587Sitojun if (PRC_IS_REDIRECT(cmd)) 28062587Sitojun notify = in6_rtchange, d = NULL; 28162587Sitojun else if (cmd == PRC_HOSTDEAD) 28262587Sitojun d = NULL; 28362587Sitojun else if (inet6ctlerrmap[cmd] == 0) 28462587Sitojun return; 28562587Sitojun 28662587Sitojun /* if the parameter is from icmp6, decode it. */ 28762587Sitojun if (d != NULL) { 28878064Sume ip6cp = (struct ip6ctlparam *)d; 28962587Sitojun m = ip6cp->ip6c_m; 29062587Sitojun ip6 = ip6cp->ip6c_ip6; 29162587Sitojun off = ip6cp->ip6c_off; 292125776Sume cmdarg = ip6cp->ip6c_cmdarg; 29378064Sume sa6_src = ip6cp->ip6c_src; 29462587Sitojun } else { 29562587Sitojun m = NULL; 29662587Sitojun ip6 = NULL; 297125776Sume cmdarg = NULL; 29878064Sume sa6_src = &sa6_any; 29962587Sitojun } 30062587Sitojun 301133192Srwatson (void) in6_pcbnotify(&ripcbinfo, sa, 0, 302133192Srwatson (const struct sockaddr *)sa6_src, 303125776Sume 0, cmd, cmdarg, notify); 30462587Sitojun} 30562587Sitojun 30653541Sshin/* 30753541Sshin * Generate IPv6 header and pass packet to ip6_output. 30853541Sshin * Tack on options user may have setup with control call. 30953541Sshin */ 31053541Sshinint 31153541Sshin#if __STDC__ 31253541Sshinrip6_output(struct mbuf *m, ...) 31353541Sshin#else 31453541Sshinrip6_output(m, va_alist) 31553541Sshin struct mbuf *m; 31653541Sshin va_dcl 31753541Sshin#endif 31853541Sshin{ 319120941Sume struct mbuf *control; 32053541Sshin struct socket *so; 32153541Sshin struct sockaddr_in6 *dstsock; 32253541Sshin struct in6_addr *dst; 32353541Sshin struct ip6_hdr *ip6; 32453541Sshin struct inpcb *in6p; 32553541Sshin u_int plen = m->m_pkthdr.len; 32653541Sshin int error = 0; 327121472Sume struct ip6_pktopts opt, *stickyopt = NULL; 32853541Sshin struct ifnet *oifp = NULL; 32953541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 33053541Sshin int priv = 0; 331121472Sume struct in6_addr *in6a; 33253541Sshin va_list ap; 33353541Sshin 33453541Sshin va_start(ap, m); 33553541Sshin so = va_arg(ap, struct socket *); 33653541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 33753541Sshin control = va_arg(ap, struct mbuf *); 33853541Sshin va_end(ap); 33953541Sshin 34053541Sshin in6p = sotoin6pcb(so); 341132714Srwatson /* 342132714Srwatson * XXXRW: In IPv6, we don't start referencing the contents of the 343132714Srwatson * inpcb until after all M_TRYWAIT allocations have finished. We may 344132714Srwatson * want to reorder this function to provide similar guarantees here, 345132714Srwatson * so as to avoid holding a mutex over M_TRYWAIT. 346132714Srwatson */ 347132714Srwatson INP_LOCK(in6p); 348121472Sume stickyopt = in6p->in6p_outputopts; 34953541Sshin 35053541Sshin priv = 0; 35153541Sshin if (so->so_cred->cr_uid == 0) 35253541Sshin priv = 1; 35353541Sshin dst = &dstsock->sin6_addr; 35453541Sshin if (control) { 355121472Sume if ((error = ip6_setpktoptions(control, &opt, 356121472Sume stickyopt, priv, 0, 357121472Sume so->so_proto->pr_protocol)) 358121472Sume != 0) { 35953541Sshin goto bad; 360121472Sume } 361121472Sume in6p->in6p_outputopts = &opt; 362121472Sume } 36353541Sshin 36453541Sshin /* 36553541Sshin * For an ICMPv6 packet, we should know its type and code 36653541Sshin * to update statistics. 36753541Sshin */ 36853541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 36953541Sshin struct icmp6_hdr *icmp6; 37053541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 37153541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 37253541Sshin error = ENOBUFS; 37353541Sshin goto bad; 37453541Sshin } 37553541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 37653541Sshin type = icmp6->icmp6_type; 37753541Sshin code = icmp6->icmp6_code; 37853541Sshin } 37953541Sshin 380133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 381133592Srwatson if (m == NULL) { 382133592Srwatson error = ENOBUFS; 383133592Srwatson goto bad; 384133592Srwatson } 38553541Sshin ip6 = mtod(m, struct ip6_hdr *); 38653541Sshin 38753541Sshin /* 38853541Sshin * Next header might not be ICMP6 but use its pseudo header anyway. 38953541Sshin */ 39053541Sshin ip6->ip6_dst = *dst; 39153541Sshin 39253541Sshin /* 39353541Sshin * If the scope of the destination is link-local, embed the interface 39453541Sshin * index in the address. 39553541Sshin * 39653541Sshin * XXX advanced-api value overrides sin6_scope_id 39753541Sshin */ 39853541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 39953541Sshin struct in6_pktinfo *pi; 40053541Sshin 40153541Sshin /* 40253541Sshin * XXX Boundary check is assumed to be already done in 40353541Sshin * ip6_setpktoptions(). 40453541Sshin */ 405121472Sume if (in6p->in6p_outputopts && 406121472Sume (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 407121472Sume pi->ipi6_ifindex) { 40853541Sshin ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex); 40983130Sjlemon oifp = ifnet_byindex(pi->ipi6_ifindex); 41053541Sshin } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 41153541Sshin in6p->in6p_moptions && 41253541Sshin in6p->in6p_moptions->im6o_multicast_ifp) { 41353541Sshin oifp = in6p->in6p_moptions->im6o_multicast_ifp; 41453541Sshin ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index); 41553541Sshin } else if (dstsock->sin6_scope_id) { 41653541Sshin /* boundary check */ 417120941Sume if (dstsock->sin6_scope_id < 0 || 418120941Sume if_index < dstsock->sin6_scope_id) { 41953541Sshin error = ENXIO; /* XXX EINVAL? */ 42053541Sshin goto bad; 42153541Sshin } 422120941Sume ip6->ip6_dst.s6_addr16[1] = 423120941Sume htons(dstsock->sin6_scope_id & 0xffff); /* XXX */ 42453541Sshin } 42553541Sshin } 42653541Sshin 42753541Sshin /* 42853541Sshin * Source address selection. 42953541Sshin */ 430121472Sume if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts, 431122927Sandre in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) { 432121472Sume if (error == 0) 433121472Sume error = EADDRNOTAVAIL; 434121472Sume goto bad; 43553541Sshin } 436121472Sume ip6->ip6_src = *in6a; 43755009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 43855009Sshin (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 43955009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 44055009Sshin (IPV6_VERSION & IPV6_VERSION_MASK); 44153541Sshin /* ip6_plen will be filled in ip6_output, so not fill it here. */ 44253541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 44353541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 44453541Sshin 44553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 44653541Sshin in6p->in6p_cksum != -1) { 44753541Sshin struct mbuf *n; 44853541Sshin int off; 44953541Sshin u_int16_t *p; 45053541Sshin 45153541Sshin /* compute checksum */ 45253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 45353541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 45453541Sshin else 45553541Sshin off = in6p->in6p_cksum; 45653541Sshin if (plen < off + 1) { 45753541Sshin error = EINVAL; 45853541Sshin goto bad; 45953541Sshin } 46053541Sshin off += sizeof(struct ip6_hdr); 46153541Sshin 46253541Sshin n = m; 46353541Sshin while (n && n->m_len <= off) { 46453541Sshin off -= n->m_len; 46553541Sshin n = n->m_next; 46653541Sshin } 46753541Sshin if (!n) 46853541Sshin goto bad; 46953541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 47053541Sshin *p = 0; 47153541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 47253541Sshin } 47353541Sshin 474122927Sandre error = ip6_output(m, in6p->in6p_outputopts, NULL, 0, 475105194Ssam in6p->in6p_moptions, &oifp, in6p); 47653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 47753541Sshin if (oifp) 47853541Sshin icmp6_ifoutstat_inc(oifp, type, code); 47953541Sshin icmp6stat.icp6s_outhist[type]++; 48078064Sume } else 48178064Sume rip6stat.rip6s_opackets++; 48253541Sshin 48353541Sshin goto freectl; 48453541Sshin 48553541Sshin bad: 48653541Sshin if (m) 48753541Sshin m_freem(m); 48853541Sshin 48953541Sshin freectl: 49078064Sume if (control) { 491121472Sume ip6_clearpktopts(in6p->in6p_outputopts, -1); 492121472Sume in6p->in6p_outputopts = stickyopt; 49353541Sshin m_freem(control); 49478064Sume } 495132714Srwatson INP_UNLOCK(in6p); 496120856Sume return (error); 49753541Sshin} 49853541Sshin 49953541Sshin/* 50053541Sshin * Raw IPv6 socket option processing. 50153541Sshin */ 50253541Sshinint 50353541Sshinrip6_ctloutput(so, sopt) 50453541Sshin struct socket *so; 50553541Sshin struct sockopt *sopt; 50653541Sshin{ 50753541Sshin int error; 50853541Sshin 50953541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 51053541Sshin /* 51153541Sshin * XXX: is it better to call icmp6_ctloutput() directly 51253541Sshin * from protosw? 51353541Sshin */ 514120856Sume return (icmp6_ctloutput(so, sopt)); 51553541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 51653541Sshin return (EINVAL); 51753541Sshin 51853541Sshin error = 0; 51953541Sshin 52053541Sshin switch (sopt->sopt_dir) { 52153541Sshin case SOPT_GET: 52253541Sshin switch (sopt->sopt_name) { 52356723Sshin case MRT6_INIT: 52456723Sshin case MRT6_DONE: 52556723Sshin case MRT6_ADD_MIF: 52656723Sshin case MRT6_DEL_MIF: 52756723Sshin case MRT6_ADD_MFC: 52856723Sshin case MRT6_DEL_MFC: 52956723Sshin case MRT6_PIM: 53056723Sshin error = ip6_mrouter_get(so, sopt); 53156723Sshin break; 532121578Sume case IPV6_CHECKSUM: 533121578Sume error = ip6_raw_ctloutput(so, sopt); 534121578Sume break; 53553541Sshin default: 53653541Sshin error = ip6_ctloutput(so, sopt); 53753541Sshin break; 53853541Sshin } 53953541Sshin break; 54053541Sshin 54153541Sshin case SOPT_SET: 54253541Sshin switch (sopt->sopt_name) { 54356723Sshin case MRT6_INIT: 54456723Sshin case MRT6_DONE: 54556723Sshin case MRT6_ADD_MIF: 54656723Sshin case MRT6_DEL_MIF: 54756723Sshin case MRT6_ADD_MFC: 54856723Sshin case MRT6_DEL_MFC: 54956723Sshin case MRT6_PIM: 55056723Sshin error = ip6_mrouter_set(so, sopt); 55156723Sshin break; 552121578Sume case IPV6_CHECKSUM: 553121578Sume error = ip6_raw_ctloutput(so, sopt); 554121578Sume break; 55553541Sshin default: 55653541Sshin error = ip6_ctloutput(so, sopt); 55753541Sshin break; 55853541Sshin } 55953541Sshin break; 56053541Sshin } 56153541Sshin 56253541Sshin return (error); 56353541Sshin} 56453541Sshin 56553541Sshinstatic int 56683366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 56753541Sshin{ 56853541Sshin struct inpcb *inp; 56953541Sshin int error, s; 57053541Sshin 571132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 57253541Sshin inp = sotoinpcb(so); 573132714Srwatson if (inp) { 574132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57553541Sshin panic("rip6_attach"); 576132714Srwatson } 577132714Srwatson if (td && (error = suser(td)) != 0) { 578132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57953541Sshin return error; 580132714Srwatson } 58155009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 582132714Srwatson if (error) { 583132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 58455009Sshin return error; 585132714Srwatson } 58653541Sshin s = splnet(); 587127504Spjd error = in_pcballoc(so, &ripcbinfo, "raw6inp"); 58853541Sshin splx(s); 589132714Srwatson if (error) { 590132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 59153541Sshin return error; 592132714Srwatson } 59353541Sshin inp = (struct inpcb *)so->so_pcb; 594132714Srwatson INP_LOCK(inp); 595132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 59653541Sshin inp->inp_vflag |= INP_IPV6; 59753541Sshin inp->in6p_ip6_nxt = (long)proto; 59853541Sshin inp->in6p_hops = -1; /* use kernel default */ 59953541Sshin inp->in6p_cksum = -1; 60053541Sshin MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *, 60153541Sshin sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 60253541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 603132714Srwatson INP_UNLOCK(inp); 60453541Sshin return 0; 60553541Sshin} 60653541Sshin 60753541Sshinstatic int 60853541Sshinrip6_detach(struct socket *so) 60953541Sshin{ 61053541Sshin struct inpcb *inp; 61153541Sshin 612132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 61353541Sshin inp = sotoinpcb(so); 614132714Srwatson if (inp == 0) { 615132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 61653541Sshin panic("rip6_detach"); 617132714Srwatson } 61853541Sshin /* xxx: RSVP */ 61957535Sshin if (so == ip6_mrouter) 62057535Sshin ip6_mrouter_done(); 62153541Sshin if (inp->in6p_icmp6filt) { 62253541Sshin FREE(inp->in6p_icmp6filt, M_PCB); 62353541Sshin inp->in6p_icmp6filt = NULL; 62453541Sshin } 625132714Srwatson INP_LOCK(inp); 62653541Sshin in6_pcbdetach(inp); 627132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 62853541Sshin return 0; 62953541Sshin} 63053541Sshin 63153541Sshinstatic int 63253541Sshinrip6_abort(struct socket *so) 63353541Sshin{ 63453541Sshin soisdisconnected(so); 63553541Sshin return rip6_detach(so); 63653541Sshin} 63753541Sshin 63853541Sshinstatic int 63953541Sshinrip6_disconnect(struct socket *so) 64053541Sshin{ 64153541Sshin struct inpcb *inp = sotoinpcb(so); 64253541Sshin 64397658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 64453541Sshin return ENOTCONN; 64553541Sshin inp->in6p_faddr = in6addr_any; 64653541Sshin return rip6_abort(so); 64753541Sshin} 64853541Sshin 64953541Sshinstatic int 65083366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 65153541Sshin{ 65253541Sshin struct inpcb *inp = sotoinpcb(so); 65353541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 65453541Sshin struct ifaddr *ia = NULL; 65553541Sshin 65653541Sshin if (nam->sa_len != sizeof(*addr)) 65753541Sshin return EINVAL; 65853541Sshin if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 65953541Sshin return EADDRNOTAVAIL; 66062587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 66162587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 66262587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 66362587Sitojun } 66462587Sitojun#endif 66553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 66653541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 66753541Sshin return EADDRNOTAVAIL; 66853541Sshin if (ia && 66953541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 67053541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 67153541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 672120856Sume return (EADDRNOTAVAIL); 67353541Sshin } 674132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 675132714Srwatson INP_LOCK(inp); 67653541Sshin inp->in6p_laddr = addr->sin6_addr; 677132714Srwatson INP_UNLOCK(inp); 678132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 67953541Sshin return 0; 68053541Sshin} 68153541Sshin 68253541Sshinstatic int 68383366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 68453541Sshin{ 68553541Sshin struct inpcb *inp = sotoinpcb(so); 68653541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 68753541Sshin struct in6_addr *in6a = NULL; 68853541Sshin int error = 0; 68962587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 69062587Sitojun struct sockaddr_in6 tmp; 69162587Sitojun#endif 69253541Sshin 69353541Sshin if (nam->sa_len != sizeof(*addr)) 69453541Sshin return EINVAL; 69553541Sshin if (TAILQ_EMPTY(&ifnet)) 69653541Sshin return EADDRNOTAVAIL; 69753541Sshin if (addr->sin6_family != AF_INET6) 69853541Sshin return EAFNOSUPPORT; 69962587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 70062587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 70162587Sitojun /* avoid overwrites */ 70262587Sitojun tmp = *addr; 70362587Sitojun addr = &tmp; 70462587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 70562587Sitojun } 70662587Sitojun#endif 707132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 708132714Srwatson INP_LOCK(inp); 70953541Sshin /* Source address selection. XXX: need pcblookup? */ 71053541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 711122927Sandre inp->in6p_moptions, NULL, 71253541Sshin &inp->in6p_laddr, &error); 713132714Srwatson if (in6a == NULL) { 714132714Srwatson INP_UNLOCK(inp); 715132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 71653541Sshin return (error ? error : EADDRNOTAVAIL); 717132714Srwatson } 71853541Sshin inp->in6p_laddr = *in6a; 71953541Sshin inp->in6p_faddr = addr->sin6_addr; 72053541Sshin soisconnected(so); 721132714Srwatson INP_UNLOCK(inp); 722132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 72353541Sshin return 0; 72453541Sshin} 72553541Sshin 72653541Sshinstatic int 72753541Sshinrip6_shutdown(struct socket *so) 72853541Sshin{ 729132714Srwatson struct inpcb *inp; 730132714Srwatson 731132714Srwatson INP_INFO_RLOCK(&ripcbinfo); 732132714Srwatson inp = sotoinpcb(so); 733132714Srwatson INP_LOCK(inp); 734132714Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 73553541Sshin socantsendmore(so); 736132714Srwatson INP_UNLOCK(inp); 73753541Sshin return 0; 73853541Sshin} 73953541Sshin 74053541Sshinstatic int 74153541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 74283366Sjulian struct mbuf *control, struct thread *td) 74353541Sshin{ 74453541Sshin struct inpcb *inp = sotoinpcb(so); 74553541Sshin struct sockaddr_in6 tmp; 74653541Sshin struct sockaddr_in6 *dst; 747132714Srwatson int ret; 74853541Sshin 749132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 75062587Sitojun /* always copy sockaddr to avoid overwrites */ 751132714Srwatson /* Unlocked read. */ 75253541Sshin if (so->so_state & SS_ISCONNECTED) { 75353541Sshin if (nam) { 754132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 75553541Sshin m_freem(m); 75653541Sshin return EISCONN; 75753541Sshin } 75853541Sshin /* XXX */ 75953541Sshin bzero(&tmp, sizeof(tmp)); 76053541Sshin tmp.sin6_family = AF_INET6; 76153541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 76253541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 76353541Sshin sizeof(struct in6_addr)); 76453541Sshin dst = &tmp; 76553541Sshin } else { 76653541Sshin if (nam == NULL) { 767132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 76853541Sshin m_freem(m); 76953541Sshin return ENOTCONN; 77053541Sshin } 77162587Sitojun tmp = *(struct sockaddr_in6 *)nam; 77262587Sitojun dst = &tmp; 77353541Sshin } 77462587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 77562587Sitojun if (dst->sin6_scope_id == 0) { /* not change if specified */ 77662587Sitojun dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr); 77762587Sitojun } 77862587Sitojun#endif 779132714Srwatson ret = rip6_output(m, so, dst, control); 780132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 781132714Srwatson return (ret); 78253541Sshin} 78353541Sshin 78453541Sshinstruct pr_usrreqs rip6_usrreqs = { 785137386Sphk .pru_abort = rip6_abort, 786137386Sphk .pru_attach = rip6_attach, 787137386Sphk .pru_bind = rip6_bind, 788137386Sphk .pru_connect = rip6_connect, 789137386Sphk .pru_control = in6_control, 790137386Sphk .pru_detach = rip6_detach, 791137386Sphk .pru_disconnect = rip6_disconnect, 792137386Sphk .pru_peeraddr = in6_setpeeraddr, 793137386Sphk .pru_send = rip6_send, 794137386Sphk .pru_shutdown = rip6_shutdown, 795137386Sphk .pru_sockaddr = in6_setsockaddr, 79653541Sshin}; 797