raw_ip6.c revision 121578
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 121578 2003-10-26 18:17:01Z ume $ 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 * 3. All advertising materials mentioning features or use of this software 4553541Sshin * must display the following acknowledgement: 4653541Sshin * This product includes software developed by the University of 4753541Sshin * California, Berkeley and its contributors. 4853541Sshin * 4. Neither the name of the University nor the names of its contributors 4953541Sshin * may be used to endorse or promote products derived from this software 5053541Sshin * without specific prior written permission. 5153541Sshin * 5253541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5353541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5453541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5553541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5653541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5753541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5853541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5953541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6053541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6153541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6253541Sshin * SUCH DAMAGE. 6353541Sshin * 6453541Sshin * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6553541Sshin */ 6653541Sshin 6755009Sshin#include "opt_ipsec.h" 6878064Sume#include "opt_inet6.h" 6955009Sshin 7053541Sshin#include <sys/param.h> 7195759Stanimura#include <sys/errno.h> 7295759Stanimura#include <sys/lock.h> 7353541Sshin#include <sys/malloc.h> 7495759Stanimura#include <sys/mbuf.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> 8153541Sshin#include <sys/systm.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8495759Stanimura#include <net/if_types.h> 8553541Sshin#include <net/route.h> 8653541Sshin 8753541Sshin#include <netinet/in.h> 8853541Sshin#include <netinet/in_var.h> 8953541Sshin#include <netinet/in_systm.h> 9095759Stanimura#include <netinet/icmp6.h> 9195759Stanimura#include <netinet/in_pcb.h> 9262587Sitojun#include <netinet/ip6.h> 9395759Stanimura#include <netinet6/ip6protosw.h> 9456723Sshin#include <netinet6/ip6_mroute.h> 9553541Sshin#include <netinet6/in6_pcb.h> 9695759Stanimura#include <netinet6/ip6_var.h> 9753541Sshin#include <netinet6/nd6.h> 9895759Stanimura#include <netinet6/raw_ip6.h> 9962587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 10062587Sitojun#include <netinet6/scope6_var.h> 10162587Sitojun#endif 10253541Sshin 10353541Sshin#ifdef IPSEC 10453541Sshin#include <netinet6/ipsec.h> 10553541Sshin#include <netinet6/ipsec6.h> 10653541Sshin#endif /*IPSEC*/ 10753541Sshin 108105199Ssam#ifdef FAST_IPSEC 109105199Ssam#include <netipsec/ipsec.h> 110105199Ssam#include <netipsec/ipsec6.h> 111105199Ssam#endif /* FAST_IPSEC */ 112105199Ssam 11353541Sshin#include <machine/stdarg.h> 11453541Sshin 11553541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11653541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11753541Sshin 11853541Sshin/* 11953541Sshin * Raw interface to IP6 protocol. 12053541Sshin */ 12153541Sshin 12253541Sshinextern struct inpcbhead ripcb; 12353541Sshinextern struct inpcbinfo ripcbinfo; 12453541Sshinextern u_long rip_sendspace; 12553541Sshinextern u_long rip_recvspace; 12653541Sshin 12778064Sumestruct rip6stat rip6stat; 12878064Sume 12953541Sshin/* 13053541Sshin * Setup generic address and protocol structures 13153541Sshin * for raw_input routine, then pass them along with 13253541Sshin * mbuf chain. 13353541Sshin */ 13453541Sshinint 13553541Sshinrip6_input(mp, offp, proto) 13653541Sshin struct mbuf **mp; 13753541Sshin int *offp, proto; 13853541Sshin{ 13953541Sshin struct mbuf *m = *mp; 14053541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 14153541Sshin register struct inpcb *in6p; 14253541Sshin struct inpcb *last = 0; 14378064Sume struct mbuf *opts = NULL; 14453541Sshin struct sockaddr_in6 rip6src; 14553541Sshin 14678064Sume rip6stat.rip6s_ipackets++; 14778064Sume 14883934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 14978064Sume /* XXX send icmp6 host/port unreach? */ 15078064Sume m_freem(m); 15178064Sume return IPPROTO_DONE; 15253541Sshin } 15378064Sume 15453541Sshin init_sin6(&rip6src, m); /* general init */ 15553541Sshin 15653541Sshin LIST_FOREACH(in6p, &ripcb, inp_list) { 15755009Sshin if ((in6p->in6p_vflag & INP_IPV6) == 0) 15853541Sshin continue; 15953541Sshin if (in6p->in6p_ip6_nxt && 16053541Sshin in6p->in6p_ip6_nxt != proto) 16153541Sshin continue; 16253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 16353541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 16453541Sshin continue; 16553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 16653541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 16753541Sshin continue; 16878064Sume if (in6p->in6p_cksum != -1) { 16978064Sume rip6stat.rip6s_isum++; 17078064Sume if (in6_cksum(m, ip6->ip6_nxt, *offp, 17178064Sume m->m_pkthdr.len - *offp)) { 17278064Sume rip6stat.rip6s_badsum++; 17378064Sume continue; 17478064Sume } 17553541Sshin } 17653541Sshin if (last) { 17753541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 17878064Sume 17978064Sume#ifdef IPSEC 18078064Sume /* 18178064Sume * Check AH/ESP integrity. 18278064Sume */ 183111145Sjlemon if (n && ipsec6_in_reject(n, last)) { 18478064Sume m_freem(n); 18578064Sume ipsec6stat.in_polvio++; 18678064Sume /* do not inject data into pcb */ 18778064Sume } else 18878064Sume#endif /*IPSEC*/ 189105199Ssam#ifdef FAST_IPSEC 190105199Ssam /* 191105199Ssam * Check AH/ESP integrity. 192105199Ssam */ 193105199Ssam if (n && ipsec6_in_reject(n, last)) { 194105199Ssam m_freem(n); 195105199Ssam /* do not inject data into pcb */ 196105199Ssam } else 197105199Ssam#endif /*FAST_IPSEC*/ 19853541Sshin if (n) { 19997658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 20097658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 20153541Sshin ip6_savecontrol(last, &opts, ip6, n); 20253541Sshin /* strip intermediate headers */ 20353541Sshin m_adj(n, *offp); 20453541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 20553541Sshin (struct sockaddr *)&rip6src, 20653541Sshin n, opts) == 0) { 20753541Sshin m_freem(n); 20853541Sshin if (opts) 20953541Sshin m_freem(opts); 21078064Sume rip6stat.rip6s_fullsock++; 21197658Stanimura } else 21253541Sshin sorwakeup(last->in6p_socket); 21353541Sshin opts = NULL; 21453541Sshin } 21553541Sshin } 21653541Sshin last = in6p; 21753541Sshin } 21878064Sume#ifdef IPSEC 21978064Sume /* 22078064Sume * Check AH/ESP integrity. 22178064Sume */ 222111145Sjlemon if (last && ipsec6_in_reject(m, last)) { 22378064Sume m_freem(m); 22478064Sume ipsec6stat.in_polvio++; 22578064Sume ip6stat.ip6s_delivered--; 22678064Sume /* do not inject data into pcb */ 22778064Sume } else 22878064Sume#endif /*IPSEC*/ 229105199Ssam#ifdef FAST_IPSEC 230105199Ssam /* 231105199Ssam * Check AH/ESP integrity. 232105199Ssam */ 233105199Ssam if (last && ipsec6_in_reject(m, last)) { 234105199Ssam m_freem(m); 235105199Ssam ip6stat.ip6s_delivered--; 236105199Ssam /* do not inject data into pcb */ 237105199Ssam } else 238105199Ssam#endif /*FAST_IPSEC*/ 23953541Sshin if (last) { 24097658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 24197658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 24253541Sshin ip6_savecontrol(last, &opts, ip6, m); 24353541Sshin /* strip intermediate headers */ 24453541Sshin m_adj(m, *offp); 24553541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 24653541Sshin (struct sockaddr *)&rip6src, m, opts) == 0) { 24753541Sshin m_freem(m); 24853541Sshin if (opts) 24953541Sshin m_freem(opts); 25078064Sume rip6stat.rip6s_fullsock++; 25197658Stanimura } else 25253541Sshin sorwakeup(last->in6p_socket); 25353541Sshin } else { 25478064Sume rip6stat.rip6s_nosock++; 25578064Sume if (m->m_flags & M_MCAST) 25678064Sume rip6stat.rip6s_nosockmcast++; 25753541Sshin if (proto == IPPROTO_NONE) 25853541Sshin m_freem(m); 25953541Sshin else { 26053541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 26153541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 26253541Sshin ICMP6_PARAMPROB_NEXTHEADER, 26353541Sshin prvnxtp - mtod(m, char *)); 26453541Sshin } 26553541Sshin ip6stat.ip6s_delivered--; 26653541Sshin } 26753541Sshin return IPPROTO_DONE; 26853541Sshin} 26953541Sshin 27062587Sitojunvoid 27162587Sitojunrip6_ctlinput(cmd, sa, d) 27262587Sitojun int cmd; 27362587Sitojun struct sockaddr *sa; 27462587Sitojun void *d; 27562587Sitojun{ 27662587Sitojun struct ip6_hdr *ip6; 27762587Sitojun struct mbuf *m; 27862587Sitojun int off = 0; 27978064Sume struct ip6ctlparam *ip6cp = NULL; 28078064Sume const struct sockaddr_in6 *sa6_src = NULL; 28198211Shsu struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange; 28262587Sitojun 28362587Sitojun if (sa->sa_family != AF_INET6 || 28462587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 28562587Sitojun return; 28662587Sitojun 28762587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 28862587Sitojun return; 28962587Sitojun if (PRC_IS_REDIRECT(cmd)) 29062587Sitojun notify = in6_rtchange, d = NULL; 29162587Sitojun else if (cmd == PRC_HOSTDEAD) 29262587Sitojun d = NULL; 29362587Sitojun else if (inet6ctlerrmap[cmd] == 0) 29462587Sitojun return; 29562587Sitojun 29662587Sitojun /* if the parameter is from icmp6, decode it. */ 29762587Sitojun if (d != NULL) { 29878064Sume ip6cp = (struct ip6ctlparam *)d; 29962587Sitojun m = ip6cp->ip6c_m; 30062587Sitojun ip6 = ip6cp->ip6c_ip6; 30162587Sitojun off = ip6cp->ip6c_off; 30278064Sume sa6_src = ip6cp->ip6c_src; 30362587Sitojun } else { 30462587Sitojun m = NULL; 30562587Sitojun ip6 = NULL; 30678064Sume sa6_src = &sa6_any; 30762587Sitojun } 30862587Sitojun 30991346Salfred (void) in6_pcbnotify(&ripcb, sa, 0, (const struct sockaddr *)sa6_src, 31078064Sume 0, cmd, notify); 31162587Sitojun} 31262587Sitojun 31353541Sshin/* 31453541Sshin * Generate IPv6 header and pass packet to ip6_output. 31553541Sshin * Tack on options user may have setup with control call. 31653541Sshin */ 31753541Sshinint 31853541Sshin#if __STDC__ 31953541Sshinrip6_output(struct mbuf *m, ...) 32053541Sshin#else 32153541Sshinrip6_output(m, va_alist) 32253541Sshin struct mbuf *m; 32353541Sshin va_dcl 32453541Sshin#endif 32553541Sshin{ 326120941Sume struct mbuf *control; 32753541Sshin struct socket *so; 32853541Sshin struct sockaddr_in6 *dstsock; 32953541Sshin struct in6_addr *dst; 33053541Sshin struct ip6_hdr *ip6; 33153541Sshin struct inpcb *in6p; 33253541Sshin u_int plen = m->m_pkthdr.len; 33353541Sshin int error = 0; 334121472Sume struct ip6_pktopts opt, *stickyopt = NULL; 33553541Sshin struct ifnet *oifp = NULL; 33653541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 33753541Sshin int priv = 0; 338121472Sume struct in6_addr *in6a; 33953541Sshin va_list ap; 34053541Sshin 34153541Sshin va_start(ap, m); 34253541Sshin so = va_arg(ap, struct socket *); 34353541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 34453541Sshin control = va_arg(ap, struct mbuf *); 34553541Sshin va_end(ap); 34653541Sshin 34753541Sshin in6p = sotoin6pcb(so); 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 380111119Simp M_PREPEND(m, sizeof(*ip6), M_TRYWAIT); 38153541Sshin ip6 = mtod(m, struct ip6_hdr *); 38253541Sshin 38353541Sshin /* 38453541Sshin * Next header might not be ICMP6 but use its pseudo header anyway. 38553541Sshin */ 38653541Sshin ip6->ip6_dst = *dst; 38753541Sshin 38853541Sshin /* 38953541Sshin * If the scope of the destination is link-local, embed the interface 39053541Sshin * index in the address. 39153541Sshin * 39253541Sshin * XXX advanced-api value overrides sin6_scope_id 39353541Sshin */ 39453541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 39553541Sshin struct in6_pktinfo *pi; 39653541Sshin 39753541Sshin /* 39853541Sshin * XXX Boundary check is assumed to be already done in 39953541Sshin * ip6_setpktoptions(). 40053541Sshin */ 401121472Sume if (in6p->in6p_outputopts && 402121472Sume (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 403121472Sume pi->ipi6_ifindex) { 40453541Sshin ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex); 40583130Sjlemon oifp = ifnet_byindex(pi->ipi6_ifindex); 40653541Sshin } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 40753541Sshin in6p->in6p_moptions && 40853541Sshin in6p->in6p_moptions->im6o_multicast_ifp) { 40953541Sshin oifp = in6p->in6p_moptions->im6o_multicast_ifp; 41053541Sshin ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index); 41153541Sshin } else if (dstsock->sin6_scope_id) { 41253541Sshin /* boundary check */ 413120941Sume if (dstsock->sin6_scope_id < 0 || 414120941Sume if_index < dstsock->sin6_scope_id) { 41553541Sshin error = ENXIO; /* XXX EINVAL? */ 41653541Sshin goto bad; 41753541Sshin } 418120941Sume ip6->ip6_dst.s6_addr16[1] = 419120941Sume htons(dstsock->sin6_scope_id & 0xffff); /* XXX */ 42053541Sshin } 42153541Sshin } 42253541Sshin 42353541Sshin /* 42453541Sshin * Source address selection. 42553541Sshin */ 426121472Sume if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts, 427121472Sume in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr, 428121472Sume &error)) == 0) { 429121472Sume if (error == 0) 430121472Sume error = EADDRNOTAVAIL; 431121472Sume goto bad; 43253541Sshin } 433121472Sume ip6->ip6_src = *in6a; 434121472Sume if (in6p->in6p_route.ro_rt) 435121472Sume oifp = ifnet_byindex(in6p->in6p_route.ro_rt->rt_ifp->if_index); 43655009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 43755009Sshin (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 43855009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 43955009Sshin (IPV6_VERSION & IPV6_VERSION_MASK); 44053541Sshin /* ip6_plen will be filled in ip6_output, so not fill it here. */ 44153541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 44253541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 44353541Sshin 44453541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 44553541Sshin in6p->in6p_cksum != -1) { 44653541Sshin struct mbuf *n; 44753541Sshin int off; 44853541Sshin u_int16_t *p; 44953541Sshin 45053541Sshin /* compute checksum */ 45153541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 45253541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 45353541Sshin else 45453541Sshin off = in6p->in6p_cksum; 45553541Sshin if (plen < off + 1) { 45653541Sshin error = EINVAL; 45753541Sshin goto bad; 45853541Sshin } 45953541Sshin off += sizeof(struct ip6_hdr); 46053541Sshin 46153541Sshin n = m; 46253541Sshin while (n && n->m_len <= off) { 46353541Sshin off -= n->m_len; 46453541Sshin n = n->m_next; 46553541Sshin } 46653541Sshin if (!n) 46753541Sshin goto bad; 46853541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 46953541Sshin *p = 0; 47053541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 47153541Sshin } 47253541Sshin 473121472Sume error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 0, 474105194Ssam in6p->in6p_moptions, &oifp, in6p); 47553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 47653541Sshin if (oifp) 47753541Sshin icmp6_ifoutstat_inc(oifp, type, code); 47853541Sshin icmp6stat.icp6s_outhist[type]++; 47978064Sume } else 48078064Sume rip6stat.rip6s_opackets++; 48153541Sshin 48253541Sshin goto freectl; 48353541Sshin 48453541Sshin bad: 48553541Sshin if (m) 48653541Sshin m_freem(m); 48753541Sshin 48853541Sshin freectl: 48978064Sume if (control) { 490121472Sume ip6_clearpktopts(in6p->in6p_outputopts, -1); 491121472Sume in6p->in6p_outputopts = stickyopt; 49253541Sshin m_freem(control); 49378064Sume } 494120856Sume return (error); 49553541Sshin} 49653541Sshin 49753541Sshin/* 49853541Sshin * Raw IPv6 socket option processing. 49953541Sshin */ 50053541Sshinint 50153541Sshinrip6_ctloutput(so, sopt) 50253541Sshin struct socket *so; 50353541Sshin struct sockopt *sopt; 50453541Sshin{ 50553541Sshin int error; 50653541Sshin 50753541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 50853541Sshin /* 50953541Sshin * XXX: is it better to call icmp6_ctloutput() directly 51053541Sshin * from protosw? 51153541Sshin */ 512120856Sume return (icmp6_ctloutput(so, sopt)); 51353541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 51453541Sshin return (EINVAL); 51553541Sshin 51653541Sshin error = 0; 51753541Sshin 51853541Sshin switch (sopt->sopt_dir) { 51953541Sshin case SOPT_GET: 52053541Sshin switch (sopt->sopt_name) { 52156723Sshin case MRT6_INIT: 52256723Sshin case MRT6_DONE: 52356723Sshin case MRT6_ADD_MIF: 52456723Sshin case MRT6_DEL_MIF: 52556723Sshin case MRT6_ADD_MFC: 52656723Sshin case MRT6_DEL_MFC: 52756723Sshin case MRT6_PIM: 52856723Sshin error = ip6_mrouter_get(so, sopt); 52956723Sshin break; 530121578Sume case IPV6_CHECKSUM: 531121578Sume error = ip6_raw_ctloutput(so, sopt); 532121578Sume break; 53353541Sshin default: 53453541Sshin error = ip6_ctloutput(so, sopt); 53553541Sshin break; 53653541Sshin } 53753541Sshin break; 53853541Sshin 53953541Sshin case SOPT_SET: 54053541Sshin switch (sopt->sopt_name) { 54156723Sshin case MRT6_INIT: 54256723Sshin case MRT6_DONE: 54356723Sshin case MRT6_ADD_MIF: 54456723Sshin case MRT6_DEL_MIF: 54556723Sshin case MRT6_ADD_MFC: 54656723Sshin case MRT6_DEL_MFC: 54756723Sshin case MRT6_PIM: 54856723Sshin error = ip6_mrouter_set(so, sopt); 54956723Sshin break; 550121578Sume case IPV6_CHECKSUM: 551121578Sume error = ip6_raw_ctloutput(so, sopt); 552121578Sume break; 55353541Sshin default: 55453541Sshin error = ip6_ctloutput(so, sopt); 55553541Sshin break; 55653541Sshin } 55753541Sshin break; 55853541Sshin } 55953541Sshin 56053541Sshin return (error); 56153541Sshin} 56253541Sshin 56353541Sshinstatic int 56483366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 56553541Sshin{ 56653541Sshin struct inpcb *inp; 56753541Sshin int error, s; 56853541Sshin 56953541Sshin inp = sotoinpcb(so); 57053541Sshin if (inp) 57153541Sshin panic("rip6_attach"); 57293593Sjhb if (td && (error = suser(td)) != 0) 57353541Sshin return error; 57453541Sshin 57555009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 57655009Sshin if (error) 57755009Sshin return error; 57853541Sshin s = splnet(); 57983366Sjulian error = in_pcballoc(so, &ripcbinfo, td); 58053541Sshin splx(s); 58153541Sshin if (error) 58253541Sshin return error; 58353541Sshin inp = (struct inpcb *)so->so_pcb; 58453541Sshin inp->inp_vflag |= INP_IPV6; 58553541Sshin inp->in6p_ip6_nxt = (long)proto; 58653541Sshin inp->in6p_hops = -1; /* use kernel default */ 58753541Sshin inp->in6p_cksum = -1; 58853541Sshin MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *, 58953541Sshin sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 59053541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 59153541Sshin return 0; 59253541Sshin} 59353541Sshin 59453541Sshinstatic int 59553541Sshinrip6_detach(struct socket *so) 59653541Sshin{ 59753541Sshin struct inpcb *inp; 59853541Sshin 59953541Sshin inp = sotoinpcb(so); 60053541Sshin if (inp == 0) 60153541Sshin panic("rip6_detach"); 60253541Sshin /* xxx: RSVP */ 60357535Sshin if (so == ip6_mrouter) 60457535Sshin ip6_mrouter_done(); 60553541Sshin if (inp->in6p_icmp6filt) { 60653541Sshin FREE(inp->in6p_icmp6filt, M_PCB); 60753541Sshin inp->in6p_icmp6filt = NULL; 60853541Sshin } 60953541Sshin in6_pcbdetach(inp); 61053541Sshin return 0; 61153541Sshin} 61253541Sshin 61353541Sshinstatic int 61453541Sshinrip6_abort(struct socket *so) 61553541Sshin{ 61653541Sshin soisdisconnected(so); 61753541Sshin return rip6_detach(so); 61853541Sshin} 61953541Sshin 62053541Sshinstatic int 62153541Sshinrip6_disconnect(struct socket *so) 62253541Sshin{ 62353541Sshin struct inpcb *inp = sotoinpcb(so); 62453541Sshin 62597658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 62653541Sshin return ENOTCONN; 62753541Sshin inp->in6p_faddr = in6addr_any; 62853541Sshin return rip6_abort(so); 62953541Sshin} 63053541Sshin 63153541Sshinstatic int 63283366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 63353541Sshin{ 63453541Sshin struct inpcb *inp = sotoinpcb(so); 63553541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 63653541Sshin struct ifaddr *ia = NULL; 63753541Sshin 63853541Sshin if (nam->sa_len != sizeof(*addr)) 63953541Sshin return EINVAL; 64053541Sshin if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 64153541Sshin return EADDRNOTAVAIL; 64262587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 64362587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 64462587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 64562587Sitojun } 64662587Sitojun#endif 64753541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 64853541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 64953541Sshin return EADDRNOTAVAIL; 65053541Sshin if (ia && 65153541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 65253541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 65353541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 654120856Sume return (EADDRNOTAVAIL); 65553541Sshin } 65653541Sshin inp->in6p_laddr = addr->sin6_addr; 65753541Sshin return 0; 65853541Sshin} 65953541Sshin 66053541Sshinstatic int 66183366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 66253541Sshin{ 66353541Sshin struct inpcb *inp = sotoinpcb(so); 66453541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 66553541Sshin struct in6_addr *in6a = NULL; 66653541Sshin int error = 0; 66762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 66862587Sitojun struct sockaddr_in6 tmp; 66962587Sitojun#endif 67053541Sshin 67153541Sshin if (nam->sa_len != sizeof(*addr)) 67253541Sshin return EINVAL; 67353541Sshin if (TAILQ_EMPTY(&ifnet)) 67453541Sshin return EADDRNOTAVAIL; 67553541Sshin if (addr->sin6_family != AF_INET6) 67653541Sshin return EAFNOSUPPORT; 67762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 67862587Sitojun if (addr->sin6_scope_id == 0) { /* not change if specified */ 67962587Sitojun /* avoid overwrites */ 68062587Sitojun tmp = *addr; 68162587Sitojun addr = &tmp; 68262587Sitojun addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr); 68362587Sitojun } 68462587Sitojun#endif 68553541Sshin /* Source address selection. XXX: need pcblookup? */ 68653541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 68753541Sshin inp->in6p_moptions, &inp->in6p_route, 68853541Sshin &inp->in6p_laddr, &error); 68953541Sshin if (in6a == NULL) 69053541Sshin return (error ? error : EADDRNOTAVAIL); 69153541Sshin inp->in6p_laddr = *in6a; 69253541Sshin inp->in6p_faddr = addr->sin6_addr; 69353541Sshin soisconnected(so); 69453541Sshin return 0; 69553541Sshin} 69653541Sshin 69753541Sshinstatic int 69853541Sshinrip6_shutdown(struct socket *so) 69953541Sshin{ 70053541Sshin socantsendmore(so); 70153541Sshin return 0; 70253541Sshin} 70353541Sshin 70453541Sshinstatic int 70553541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 70683366Sjulian struct mbuf *control, struct thread *td) 70753541Sshin{ 70853541Sshin struct inpcb *inp = sotoinpcb(so); 70953541Sshin struct sockaddr_in6 tmp; 71053541Sshin struct sockaddr_in6 *dst; 71153541Sshin 71262587Sitojun /* always copy sockaddr to avoid overwrites */ 71353541Sshin if (so->so_state & SS_ISCONNECTED) { 71453541Sshin if (nam) { 71553541Sshin m_freem(m); 71653541Sshin return EISCONN; 71753541Sshin } 71853541Sshin /* XXX */ 71953541Sshin bzero(&tmp, sizeof(tmp)); 72053541Sshin tmp.sin6_family = AF_INET6; 72153541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 72253541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 72353541Sshin sizeof(struct in6_addr)); 72453541Sshin dst = &tmp; 72553541Sshin } else { 72653541Sshin if (nam == NULL) { 72753541Sshin m_freem(m); 72853541Sshin return ENOTCONN; 72953541Sshin } 73062587Sitojun tmp = *(struct sockaddr_in6 *)nam; 73162587Sitojun dst = &tmp; 73253541Sshin } 73362587Sitojun#ifdef ENABLE_DEFAULT_SCOPE 73462587Sitojun if (dst->sin6_scope_id == 0) { /* not change if specified */ 73562587Sitojun dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr); 73662587Sitojun } 73762587Sitojun#endif 73853541Sshin return rip6_output(m, so, dst, control); 73953541Sshin} 74053541Sshin 74153541Sshinstruct pr_usrreqs rip6_usrreqs = { 74253541Sshin rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect, 74353541Sshin pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect, 74453541Sshin pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp, 74553541Sshin pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown, 74653541Sshin in6_setsockaddr, sosend, soreceive, sopoll 74753541Sshin}; 748