raw_ip6.c revision 144261
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 * $FreeBSD: head/sys/netinet6/raw_ip6.c 144261 2005-03-29 01:26:27Z sam $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 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 INP_LOCK(in6p); 342121472Sume stickyopt = in6p->in6p_outputopts; 34353541Sshin 34453541Sshin priv = 0; 34553541Sshin if (so->so_cred->cr_uid == 0) 34653541Sshin priv = 1; 34753541Sshin dst = &dstsock->sin6_addr; 34853541Sshin if (control) { 349121472Sume if ((error = ip6_setpktoptions(control, &opt, 350121472Sume stickyopt, priv, 0, 351121472Sume so->so_proto->pr_protocol)) 352121472Sume != 0) { 35353541Sshin goto bad; 354121472Sume } 355121472Sume in6p->in6p_outputopts = &opt; 356121472Sume } 35753541Sshin 35853541Sshin /* 35953541Sshin * For an ICMPv6 packet, we should know its type and code 36053541Sshin * to update statistics. 36153541Sshin */ 36253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 36353541Sshin struct icmp6_hdr *icmp6; 36453541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 36553541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 36653541Sshin error = ENOBUFS; 36753541Sshin goto bad; 36853541Sshin } 36953541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 37053541Sshin type = icmp6->icmp6_type; 37153541Sshin code = icmp6->icmp6_code; 37253541Sshin } 37353541Sshin 374133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 375133592Srwatson if (m == NULL) { 376133592Srwatson error = ENOBUFS; 377133592Srwatson goto bad; 378133592Srwatson } 37953541Sshin ip6 = mtod(m, struct ip6_hdr *); 38053541Sshin 38153541Sshin /* 38253541Sshin * Next header might not be ICMP6 but use its pseudo header anyway. 38353541Sshin */ 38453541Sshin ip6->ip6_dst = *dst; 38553541Sshin 38653541Sshin /* 38753541Sshin * If the scope of the destination is link-local, embed the interface 38853541Sshin * index in the address. 38953541Sshin * 39053541Sshin * XXX advanced-api value overrides sin6_scope_id 39153541Sshin */ 39253541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 39353541Sshin struct in6_pktinfo *pi; 39453541Sshin 39553541Sshin /* 39653541Sshin * XXX Boundary check is assumed to be already done in 39753541Sshin * ip6_setpktoptions(). 39853541Sshin */ 399121472Sume if (in6p->in6p_outputopts && 400121472Sume (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 401121472Sume pi->ipi6_ifindex) { 40253541Sshin ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex); 40383130Sjlemon oifp = ifnet_byindex(pi->ipi6_ifindex); 40453541Sshin } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 40553541Sshin in6p->in6p_moptions && 40653541Sshin in6p->in6p_moptions->im6o_multicast_ifp) { 40753541Sshin oifp = in6p->in6p_moptions->im6o_multicast_ifp; 40853541Sshin ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index); 40953541Sshin } else if (dstsock->sin6_scope_id) { 41053541Sshin /* boundary check */ 411120941Sume if (dstsock->sin6_scope_id < 0 || 412120941Sume if_index < dstsock->sin6_scope_id) { 41353541Sshin error = ENXIO; /* XXX EINVAL? */ 41453541Sshin goto bad; 41553541Sshin } 416120941Sume ip6->ip6_dst.s6_addr16[1] = 417120941Sume htons(dstsock->sin6_scope_id & 0xffff); /* XXX */ 41853541Sshin } 41953541Sshin } 42053541Sshin 42153541Sshin /* 42253541Sshin * Source address selection. 42353541Sshin */ 424121472Sume if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts, 425122927Sandre in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) { 426121472Sume if (error == 0) 427121472Sume error = EADDRNOTAVAIL; 428121472Sume goto bad; 42953541Sshin } 430121472Sume ip6->ip6_src = *in6a; 43155009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 43255009Sshin (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 43355009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 43455009Sshin (IPV6_VERSION & IPV6_VERSION_MASK); 43553541Sshin /* ip6_plen will be filled in ip6_output, so not fill it here. */ 43653541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 43753541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 43853541Sshin 43953541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 44053541Sshin in6p->in6p_cksum != -1) { 44153541Sshin struct mbuf *n; 44253541Sshin int off; 44353541Sshin u_int16_t *p; 44453541Sshin 44553541Sshin /* compute checksum */ 44653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 44753541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 44853541Sshin else 44953541Sshin off = in6p->in6p_cksum; 45053541Sshin if (plen < off + 1) { 45153541Sshin error = EINVAL; 45253541Sshin goto bad; 45353541Sshin } 45453541Sshin off += sizeof(struct ip6_hdr); 45553541Sshin 45653541Sshin n = m; 45753541Sshin while (n && n->m_len <= off) { 45853541Sshin off -= n->m_len; 45953541Sshin n = n->m_next; 46053541Sshin } 46153541Sshin if (!n) 46253541Sshin goto bad; 46353541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 46453541Sshin *p = 0; 46553541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 46653541Sshin } 46753541Sshin 468122927Sandre error = ip6_output(m, in6p->in6p_outputopts, NULL, 0, 469105194Ssam in6p->in6p_moptions, &oifp, in6p); 47053541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 47153541Sshin if (oifp) 47253541Sshin icmp6_ifoutstat_inc(oifp, type, code); 47353541Sshin icmp6stat.icp6s_outhist[type]++; 47478064Sume } else 47578064Sume rip6stat.rip6s_opackets++; 47653541Sshin 47753541Sshin goto freectl; 47853541Sshin 47953541Sshin bad: 48053541Sshin if (m) 48153541Sshin m_freem(m); 48253541Sshin 48353541Sshin freectl: 48478064Sume if (control) { 485121472Sume ip6_clearpktopts(in6p->in6p_outputopts, -1); 486121472Sume in6p->in6p_outputopts = stickyopt; 48753541Sshin m_freem(control); 48878064Sume } 489132714Srwatson INP_UNLOCK(in6p); 490120856Sume return (error); 49153541Sshin} 49253541Sshin 49353541Sshin/* 49453541Sshin * Raw IPv6 socket option processing. 49553541Sshin */ 49653541Sshinint 49753541Sshinrip6_ctloutput(so, sopt) 49853541Sshin struct socket *so; 49953541Sshin struct sockopt *sopt; 50053541Sshin{ 50153541Sshin int error; 50253541Sshin 50353541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 50453541Sshin /* 50553541Sshin * XXX: is it better to call icmp6_ctloutput() directly 50653541Sshin * from protosw? 50753541Sshin */ 508120856Sume return (icmp6_ctloutput(so, sopt)); 50953541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 51053541Sshin return (EINVAL); 51153541Sshin 51253541Sshin error = 0; 51353541Sshin 51453541Sshin switch (sopt->sopt_dir) { 51553541Sshin case SOPT_GET: 51653541Sshin switch (sopt->sopt_name) { 51756723Sshin case MRT6_INIT: 51856723Sshin case MRT6_DONE: 51956723Sshin case MRT6_ADD_MIF: 52056723Sshin case MRT6_DEL_MIF: 52156723Sshin case MRT6_ADD_MFC: 52256723Sshin case MRT6_DEL_MFC: 52356723Sshin case MRT6_PIM: 52456723Sshin error = ip6_mrouter_get(so, sopt); 52556723Sshin break; 526121578Sume case IPV6_CHECKSUM: 527121578Sume error = ip6_raw_ctloutput(so, sopt); 528121578Sume break; 52953541Sshin default: 53053541Sshin error = ip6_ctloutput(so, sopt); 53153541Sshin break; 53253541Sshin } 53353541Sshin break; 53453541Sshin 53553541Sshin case SOPT_SET: 53653541Sshin switch (sopt->sopt_name) { 53756723Sshin case MRT6_INIT: 53856723Sshin case MRT6_DONE: 53956723Sshin case MRT6_ADD_MIF: 54056723Sshin case MRT6_DEL_MIF: 54156723Sshin case MRT6_ADD_MFC: 54256723Sshin case MRT6_DEL_MFC: 54356723Sshin case MRT6_PIM: 54456723Sshin error = ip6_mrouter_set(so, sopt); 54556723Sshin break; 546121578Sume case IPV6_CHECKSUM: 547121578Sume error = ip6_raw_ctloutput(so, sopt); 548121578Sume break; 54953541Sshin default: 55053541Sshin error = ip6_ctloutput(so, sopt); 55153541Sshin break; 55253541Sshin } 55353541Sshin break; 55453541Sshin } 55553541Sshin 55653541Sshin return (error); 55753541Sshin} 55853541Sshin 55953541Sshinstatic int 56083366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 56153541Sshin{ 56253541Sshin struct inpcb *inp; 563144261Ssam struct icmp6_filter *filter; 56453541Sshin int error, s; 56553541Sshin 566132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 56753541Sshin inp = sotoinpcb(so); 568132714Srwatson if (inp) { 569132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57053541Sshin panic("rip6_attach"); 571132714Srwatson } 572132714Srwatson if (td && (error = suser(td)) != 0) { 573132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57453541Sshin return error; 575132714Srwatson } 57655009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 577132714Srwatson if (error) { 578132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57955009Sshin return error; 580132714Srwatson } 581144261Ssam MALLOC(filter, struct icmp6_filter *, 582144261Ssam sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 583144261Ssam if (filter == NULL) 584144261Ssam return ENOMEM; 58553541Sshin s = splnet(); 586127504Spjd error = in_pcballoc(so, &ripcbinfo, "raw6inp"); 58753541Sshin splx(s); 588132714Srwatson if (error) { 589132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 590144261Ssam FREE(filter, M_PCB); 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; 600144261Ssam inp->in6p_icmp6filt = filter; 60153541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 602132714Srwatson INP_UNLOCK(inp); 60353541Sshin return 0; 60453541Sshin} 60553541Sshin 60653541Sshinstatic int 60753541Sshinrip6_detach(struct socket *so) 60853541Sshin{ 60953541Sshin struct inpcb *inp; 61053541Sshin 611132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 61253541Sshin inp = sotoinpcb(so); 613132714Srwatson if (inp == 0) { 614132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 61553541Sshin panic("rip6_detach"); 616132714Srwatson } 61753541Sshin /* xxx: RSVP */ 61857535Sshin if (so == ip6_mrouter) 61957535Sshin ip6_mrouter_done(); 62053541Sshin if (inp->in6p_icmp6filt) { 62153541Sshin FREE(inp->in6p_icmp6filt, M_PCB); 62253541Sshin inp->in6p_icmp6filt = NULL; 62353541Sshin } 624132714Srwatson INP_LOCK(inp); 62553541Sshin in6_pcbdetach(inp); 626132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 62753541Sshin return 0; 62853541Sshin} 62953541Sshin 63053541Sshinstatic int 63153541Sshinrip6_abort(struct socket *so) 63253541Sshin{ 63353541Sshin soisdisconnected(so); 63453541Sshin return rip6_detach(so); 63553541Sshin} 63653541Sshin 63753541Sshinstatic int 63853541Sshinrip6_disconnect(struct socket *so) 63953541Sshin{ 64053541Sshin struct inpcb *inp = sotoinpcb(so); 64153541Sshin 64297658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 64353541Sshin return ENOTCONN; 64453541Sshin inp->in6p_faddr = in6addr_any; 64553541Sshin return rip6_abort(so); 64653541Sshin} 64753541Sshin 64853541Sshinstatic int 64983366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 65053541Sshin{ 65153541Sshin struct inpcb *inp = sotoinpcb(so); 65253541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 65353541Sshin struct ifaddr *ia = NULL; 65453541Sshin 65553541Sshin if (nam->sa_len != sizeof(*addr)) 65653541Sshin return EINVAL; 65753541Sshin if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 65853541Sshin return EADDRNOTAVAIL; 65962587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 66062587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 66162587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 66262587Sitojun } 66362587Sitojun#endif 66453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 66553541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 66653541Sshin return EADDRNOTAVAIL; 66753541Sshin if (ia && 66853541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 66953541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 67053541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 671120856Sume return (EADDRNOTAVAIL); 67253541Sshin } 673132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 674132714Srwatson INP_LOCK(inp); 67553541Sshin inp->in6p_laddr = addr->sin6_addr; 676132714Srwatson INP_UNLOCK(inp); 677132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 67853541Sshin return 0; 67953541Sshin} 68053541Sshin 68153541Sshinstatic int 68283366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 68353541Sshin{ 68453541Sshin struct inpcb *inp = sotoinpcb(so); 68553541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 68653541Sshin struct in6_addr *in6a = NULL; 68753541Sshin int error = 0; 68862587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 68962587Sitojun struct sockaddr_in6 tmp; 69062587Sitojun#endif 69153541Sshin 69253541Sshin if (nam->sa_len != sizeof(*addr)) 69353541Sshin return EINVAL; 69453541Sshin if (TAILQ_EMPTY(&ifnet)) 69553541Sshin return EADDRNOTAVAIL; 69653541Sshin if (addr->sin6_family != AF_INET6) 69753541Sshin return EAFNOSUPPORT; 69862587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 69962587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 70062587Sitojun /* avoid overwrites */ 70162587Sitojun tmp = *addr; 70262587Sitojun addr = &tmp; 70362587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 70462587Sitojun } 70562587Sitojun#endif 706132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 707132714Srwatson INP_LOCK(inp); 70853541Sshin /* Source address selection. XXX: need pcblookup? */ 70953541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 710122927Sandre inp->in6p_moptions, NULL, 71153541Sshin &inp->in6p_laddr, &error); 712132714Srwatson if (in6a == NULL) { 713132714Srwatson INP_UNLOCK(inp); 714132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 71553541Sshin return (error ? error : EADDRNOTAVAIL); 716132714Srwatson } 71753541Sshin inp->in6p_laddr = *in6a; 71853541Sshin inp->in6p_faddr = addr->sin6_addr; 71953541Sshin soisconnected(so); 720132714Srwatson INP_UNLOCK(inp); 721132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 72253541Sshin return 0; 72353541Sshin} 72453541Sshin 72553541Sshinstatic int 72653541Sshinrip6_shutdown(struct socket *so) 72753541Sshin{ 728132714Srwatson struct inpcb *inp; 729132714Srwatson 730132714Srwatson INP_INFO_RLOCK(&ripcbinfo); 731132714Srwatson inp = sotoinpcb(so); 732132714Srwatson INP_LOCK(inp); 733132714Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 73453541Sshin socantsendmore(so); 735132714Srwatson INP_UNLOCK(inp); 73653541Sshin return 0; 73753541Sshin} 73853541Sshin 73953541Sshinstatic int 74053541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 74183366Sjulian struct mbuf *control, struct thread *td) 74253541Sshin{ 74353541Sshin struct inpcb *inp = sotoinpcb(so); 74453541Sshin struct sockaddr_in6 tmp; 74553541Sshin struct sockaddr_in6 *dst; 746132714Srwatson int ret; 74753541Sshin 748132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 74962587Sitojun /* always copy sockaddr to avoid overwrites */ 750132714Srwatson /* Unlocked read. */ 75153541Sshin if (so->so_state & SS_ISCONNECTED) { 75253541Sshin if (nam) { 753132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 75453541Sshin m_freem(m); 75553541Sshin return EISCONN; 75653541Sshin } 75753541Sshin /* XXX */ 75853541Sshin bzero(&tmp, sizeof(tmp)); 75953541Sshin tmp.sin6_family = AF_INET6; 76053541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 76153541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 76253541Sshin sizeof(struct in6_addr)); 76353541Sshin dst = &tmp; 76453541Sshin } else { 76553541Sshin if (nam == NULL) { 766132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 76753541Sshin m_freem(m); 76853541Sshin return ENOTCONN; 76953541Sshin } 77062587Sitojun tmp = *(struct sockaddr_in6 *)nam; 77162587Sitojun dst = &tmp; 77253541Sshin } 77362587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 77462587Sitojun if (dst->sin6_scope_id == 0) { /* not change if specified */ 77562587Sitojun dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr); 77662587Sitojun } 77762587Sitojun#endif 778132714Srwatson ret = rip6_output(m, so, dst, control); 779132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 780132714Srwatson return (ret); 78153541Sshin} 78253541Sshin 78353541Sshinstruct pr_usrreqs rip6_usrreqs = { 784137386Sphk .pru_abort = rip6_abort, 785137386Sphk .pru_attach = rip6_attach, 786137386Sphk .pru_bind = rip6_bind, 787137386Sphk .pru_connect = rip6_connect, 788137386Sphk .pru_control = in6_control, 789137386Sphk .pru_detach = rip6_detach, 790137386Sphk .pru_disconnect = rip6_disconnect, 791137386Sphk .pru_peeraddr = in6_setpeeraddr, 792137386Sphk .pru_send = rip6_send, 793137386Sphk .pru_shutdown = rip6_shutdown, 794137386Sphk .pru_sockaddr = in6_setsockaddr, 79553541Sshin}; 796