raw_ip6.c revision 185370
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 185370 2008-11-27 12:04:35Z bz $"); 64174510Sobrien 6555009Sshin#include "opt_ipsec.h" 6678064Sume#include "opt_inet6.h" 6755009Sshin 6853541Sshin#include <sys/param.h> 6995759Stanimura#include <sys/errno.h> 7095759Stanimura#include <sys/lock.h> 7153541Sshin#include <sys/malloc.h> 7295759Stanimura#include <sys/mbuf.h> 73170689Srwatson#include <sys/priv.h> 7453541Sshin#include <sys/proc.h> 7595759Stanimura#include <sys/protosw.h> 7695759Stanimura#include <sys/signalvar.h> 7753541Sshin#include <sys/socket.h> 7853541Sshin#include <sys/socketvar.h> 7995759Stanimura#include <sys/sx.h> 80148385Sume#include <sys/syslog.h> 81181803Sbz#include <sys/vimage.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#include <netinet6/scope6_var.h> 10053541Sshin 101171167Sgnn#ifdef IPSEC 102105199Ssam#include <netipsec/ipsec.h> 103105199Ssam#include <netipsec/ipsec6.h> 104171167Sgnn#endif /* IPSEC */ 105105199Ssam 10653541Sshin#include <machine/stdarg.h> 10753541Sshin 10853541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 10953541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11053541Sshin 11153541Sshin/* 11253541Sshin * Raw interface to IP6 protocol. 11353541Sshin */ 11453541Sshin 115185348Szec#ifdef VIMAGE_GLOBALS 11653541Sshinextern struct inpcbhead ripcb; 11753541Sshinextern struct inpcbinfo ripcbinfo; 118185348Szecstruct rip6stat rip6stat; 119185348Szec#endif 120185348Szec 12153541Sshinextern u_long rip_sendspace; 12253541Sshinextern u_long rip_recvspace; 12353541Sshin 12453541Sshin/* 125166938Sbms * Hooks for multicast forwarding. 126166938Sbms */ 127166948Sbmsstruct socket *ip6_mrouter = NULL; 128166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 129166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 130166938Sbmsint (*ip6_mrouter_done)(void); 131166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 132166938Sbmsint (*mrt6_ioctl)(int, caddr_t); 133166938Sbms 134166938Sbms/* 135180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 136180305Srwatson * pass them along with mbuf chain. 13753541Sshin */ 13853541Sshinint 139171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 14053541Sshin{ 141183550Szec INIT_VNET_INET(curvnet); 142183550Szec INIT_VNET_INET6(curvnet); 143183550Szec#ifdef IPSEC 144183550Szec INIT_VNET_IPSEC(curvnet); 145183550Szec#endif 14653541Sshin struct mbuf *m = *mp; 14753541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 14853541Sshin register struct inpcb *in6p; 14953541Sshin struct inpcb *last = 0; 15078064Sume struct mbuf *opts = NULL; 151121901Sume struct sockaddr_in6 fromsa; 15253541Sshin 153181803Sbz V_rip6stat.rip6s_ipackets++; 15478064Sume 15583934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 156180305Srwatson /* XXX Send icmp6 host/port unreach? */ 15778064Sume m_freem(m); 158180305Srwatson return (IPPROTO_DONE); 15953541Sshin } 16078064Sume 161121901Sume init_sin6(&fromsa, m); /* general init */ 16253541Sshin 163181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 164181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 165180850Smav if ((in6p->in6p_vflag & INP_IPV6) == 0) 16653541Sshin continue; 16753541Sshin if (in6p->in6p_ip6_nxt && 16853541Sshin in6p->in6p_ip6_nxt != proto) 169180850Smav continue; 17053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 17153541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 172180850Smav continue; 17353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 17453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 175180850Smav continue; 176180932Smav INP_RLOCK(in6p); 17778064Sume if (in6p->in6p_cksum != -1) { 178181803Sbz V_rip6stat.rip6s_isum++; 179151459Ssuz if (in6_cksum(m, proto, *offp, 18078064Sume m->m_pkthdr.len - *offp)) { 181180932Smav INP_RUNLOCK(in6p); 182181803Sbz V_rip6stat.rip6s_badsum++; 183180850Smav continue; 18478064Sume } 18553541Sshin } 18653541Sshin if (last) { 18753541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 18878064Sume 189171167Sgnn#ifdef IPSEC 19078064Sume /* 19178064Sume * Check AH/ESP integrity. 19278064Sume */ 193125396Sume if (n && ipsec6_in_reject(n, last)) { 19478064Sume m_freem(n); 195181803Sbz V_ipsec6stat.in_polvio++; 196180305Srwatson /* Do not inject data into pcb. */ 197105199Ssam } else 198171167Sgnn#endif /* IPSEC */ 19953541Sshin if (n) { 20097658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 20197658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 202121674Sume ip6_savecontrol(last, n, &opts); 20353541Sshin /* strip intermediate headers */ 20453541Sshin m_adj(n, *offp); 20553541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 206121901Sume (struct sockaddr *)&fromsa, 20753541Sshin n, opts) == 0) { 20853541Sshin m_freem(n); 20953541Sshin if (opts) 21053541Sshin m_freem(opts); 211181803Sbz V_rip6stat.rip6s_fullsock++; 21297658Stanimura } else 21353541Sshin sorwakeup(last->in6p_socket); 21453541Sshin opts = NULL; 21553541Sshin } 216178377Srwatson INP_RUNLOCK(last); 21753541Sshin } 21853541Sshin last = in6p; 21953541Sshin } 220181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 221171167Sgnn#ifdef IPSEC 22278064Sume /* 22378064Sume * Check AH/ESP integrity. 22478064Sume */ 225125396Sume if (last && ipsec6_in_reject(m, last)) { 22678064Sume m_freem(m); 227181803Sbz V_ipsec6stat.in_polvio++; 228181803Sbz V_ip6stat.ip6s_delivered--; 229180305Srwatson /* Do not inject data into pcb. */ 230178377Srwatson INP_RUNLOCK(last); 231105199Ssam } else 232171167Sgnn#endif /* IPSEC */ 23353541Sshin if (last) { 23497658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 23597658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 236121674Sume ip6_savecontrol(last, m, &opts); 237180305Srwatson /* Strip intermediate headers. */ 23853541Sshin m_adj(m, *offp); 23953541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 240180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 24153541Sshin m_freem(m); 24253541Sshin if (opts) 24353541Sshin m_freem(opts); 244181803Sbz V_rip6stat.rip6s_fullsock++; 24597658Stanimura } else 24653541Sshin sorwakeup(last->in6p_socket); 247178377Srwatson INP_RUNLOCK(last); 24853541Sshin } else { 249181803Sbz V_rip6stat.rip6s_nosock++; 25078064Sume if (m->m_flags & M_MCAST) 251181803Sbz V_rip6stat.rip6s_nosockmcast++; 25253541Sshin if (proto == IPPROTO_NONE) 25353541Sshin m_freem(m); 25453541Sshin else { 25553541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 25653541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 257180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 258180305Srwatson prvnxtp - mtod(m, char *)); 25953541Sshin } 260181803Sbz V_ip6stat.ip6s_delivered--; 26153541Sshin } 262180305Srwatson return (IPPROTO_DONE); 26353541Sshin} 26453541Sshin 26562587Sitojunvoid 266171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 26762587Sitojun{ 268183550Szec INIT_VNET_INET(curvnet); 26962587Sitojun struct ip6_hdr *ip6; 27062587Sitojun struct mbuf *m; 27162587Sitojun int off = 0; 27278064Sume struct ip6ctlparam *ip6cp = NULL; 27378064Sume const struct sockaddr_in6 *sa6_src = NULL; 274125776Sume void *cmdarg; 275175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 27662587Sitojun 27762587Sitojun if (sa->sa_family != AF_INET6 || 27862587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 27962587Sitojun return; 28062587Sitojun 28162587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 28262587Sitojun return; 28362587Sitojun if (PRC_IS_REDIRECT(cmd)) 28462587Sitojun notify = in6_rtchange, d = NULL; 28562587Sitojun else if (cmd == PRC_HOSTDEAD) 28662587Sitojun d = NULL; 28762587Sitojun else if (inet6ctlerrmap[cmd] == 0) 28862587Sitojun return; 28962587Sitojun 290180305Srwatson /* 291180305Srwatson * If the parameter is from icmp6, decode it. 292180305Srwatson */ 29362587Sitojun if (d != NULL) { 29478064Sume ip6cp = (struct ip6ctlparam *)d; 29562587Sitojun m = ip6cp->ip6c_m; 29662587Sitojun ip6 = ip6cp->ip6c_ip6; 29762587Sitojun off = ip6cp->ip6c_off; 298125776Sume cmdarg = ip6cp->ip6c_cmdarg; 29978064Sume sa6_src = ip6cp->ip6c_src; 30062587Sitojun } else { 30162587Sitojun m = NULL; 30262587Sitojun ip6 = NULL; 303125776Sume cmdarg = NULL; 30478064Sume sa6_src = &sa6_any; 30562587Sitojun } 30662587Sitojun 307181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 308180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 30962587Sitojun} 31062587Sitojun 31153541Sshin/* 312180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 313180305Srwatson * may have setup with control call. 31453541Sshin */ 31553541Sshinint 31653541Sshin#if __STDC__ 31753541Sshinrip6_output(struct mbuf *m, ...) 31853541Sshin#else 31953541Sshinrip6_output(m, va_alist) 32053541Sshin struct mbuf *m; 32153541Sshin va_dcl 32253541Sshin#endif 32353541Sshin{ 324183550Szec INIT_VNET_INET6(curvnet); 325120941Sume struct mbuf *control; 32653541Sshin struct socket *so; 32753541Sshin struct sockaddr_in6 *dstsock; 32853541Sshin struct in6_addr *dst; 32953541Sshin struct ip6_hdr *ip6; 33053541Sshin struct inpcb *in6p; 33153541Sshin u_int plen = m->m_pkthdr.len; 33253541Sshin int error = 0; 333148247Sume struct ip6_pktopts opt, *optp; 33453541Sshin struct ifnet *oifp = NULL; 33553541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 336148385Sume int scope_ambiguous = 0; 337121472Sume struct in6_addr *in6a; 33853541Sshin va_list ap; 33953541Sshin 34053541Sshin va_start(ap, m); 34153541Sshin so = va_arg(ap, struct socket *); 34253541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 34353541Sshin control = va_arg(ap, struct mbuf *); 34453541Sshin va_end(ap); 34553541Sshin 34653541Sshin in6p = sotoin6pcb(so); 347178285Srwatson INP_WLOCK(in6p); 34853541Sshin 34953541Sshin dst = &dstsock->sin6_addr; 35053541Sshin if (control) { 351148242Sume if ((error = ip6_setpktopts(control, &opt, 352175630Sbz in6p->in6p_outputopts, so->so_cred, 353175630Sbz so->so_proto->pr_protocol)) != 0) { 35453541Sshin goto bad; 355121472Sume } 356148247Sume optp = &opt; 357148247Sume } else 358148247Sume optp = in6p->in6p_outputopts; 35953541Sshin 36053541Sshin /* 361148385Sume * Check and convert scope zone ID into internal form. 362180305Srwatson * 363148385Sume * XXX: we may still need to determine the zone later. 364148385Sume */ 365148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 366181803Sbz if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone) 367148385Sume scope_ambiguous = 1; 368181803Sbz if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0) 369148385Sume goto bad; 370148385Sume } 371148385Sume 372148385Sume /* 373180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 374180305Srwatson * statistics. 37553541Sshin */ 37653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 37753541Sshin struct icmp6_hdr *icmp6; 37853541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 37953541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 38053541Sshin error = ENOBUFS; 38153541Sshin goto bad; 38253541Sshin } 38353541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 38453541Sshin type = icmp6->icmp6_type; 38553541Sshin code = icmp6->icmp6_code; 38653541Sshin } 38753541Sshin 388133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 389133592Srwatson if (m == NULL) { 390133592Srwatson error = ENOBUFS; 391133592Srwatson goto bad; 392133592Srwatson } 39353541Sshin ip6 = mtod(m, struct ip6_hdr *); 39453541Sshin 39553541Sshin /* 39653541Sshin * Source address selection. 39753541Sshin */ 398180371Sbz if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 399180371Sbz &oifp, &error)) == NULL) { 400121472Sume if (error == 0) 401121472Sume error = EADDRNOTAVAIL; 402121472Sume goto bad; 40353541Sshin } 404121472Sume ip6->ip6_src = *in6a; 405148385Sume 406148385Sume if (oifp && scope_ambiguous) { 407148385Sume /* 408148385Sume * Application should provide a proper zone ID or the use of 409148385Sume * default zone IDs should be enabled. Unfortunately, some 410148385Sume * applications do not behave as it should, so we need a 411148385Sume * workaround. Even if an appropriate ID is not determined 412148385Sume * (when it's required), if we can determine the outgoing 413148385Sume * interface. determine the zone ID based on the interface. 414148385Sume */ 415148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 416148385Sume if (error != 0) 417148385Sume goto bad; 418148385Sume } 419148385Sume ip6->ip6_dst = dstsock->sin6_addr; 420148385Sume 421180305Srwatson /* 422180305Srwatson * Fill in the rest of the IPv6 header fields. 423180305Srwatson */ 42455009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 425180305Srwatson (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 42655009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 427180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 428180305Srwatson 429180305Srwatson /* 430180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 431180305Srwatson */ 43253541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 43353541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 43453541Sshin 43553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 43653541Sshin in6p->in6p_cksum != -1) { 43753541Sshin struct mbuf *n; 43853541Sshin int off; 43953541Sshin u_int16_t *p; 44053541Sshin 441180305Srwatson /* Compute checksum. */ 44253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 44353541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 44453541Sshin else 44553541Sshin off = in6p->in6p_cksum; 44653541Sshin if (plen < off + 1) { 44753541Sshin error = EINVAL; 44853541Sshin goto bad; 44953541Sshin } 45053541Sshin off += sizeof(struct ip6_hdr); 45153541Sshin 45253541Sshin n = m; 45353541Sshin while (n && n->m_len <= off) { 45453541Sshin off -= n->m_len; 45553541Sshin n = n->m_next; 45653541Sshin } 45753541Sshin if (!n) 45853541Sshin goto bad; 45953541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 46053541Sshin *p = 0; 46153541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 46253541Sshin } 46353541Sshin 464148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 46553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 46653541Sshin if (oifp) 46753541Sshin icmp6_ifoutstat_inc(oifp, type, code); 468181803Sbz V_icmp6stat.icp6s_outhist[type]++; 46978064Sume } else 470181803Sbz V_rip6stat.rip6s_opackets++; 47153541Sshin 47253541Sshin goto freectl; 47353541Sshin 47453541Sshin bad: 47553541Sshin if (m) 47653541Sshin m_freem(m); 47753541Sshin 47853541Sshin freectl: 47978064Sume if (control) { 480148247Sume ip6_clearpktopts(&opt, -1); 48153541Sshin m_freem(control); 48278064Sume } 483178285Srwatson INP_WUNLOCK(in6p); 484120856Sume return (error); 48553541Sshin} 48653541Sshin 48753541Sshin/* 48853541Sshin * Raw IPv6 socket option processing. 48953541Sshin */ 49053541Sshinint 491171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 49253541Sshin{ 49353541Sshin int error; 49453541Sshin 49553541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 49653541Sshin /* 49753541Sshin * XXX: is it better to call icmp6_ctloutput() directly 49853541Sshin * from protosw? 49953541Sshin */ 500120856Sume return (icmp6_ctloutput(so, sopt)); 50153541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 50253541Sshin return (EINVAL); 50353541Sshin 50453541Sshin error = 0; 50553541Sshin 50653541Sshin switch (sopt->sopt_dir) { 50753541Sshin case SOPT_GET: 50853541Sshin switch (sopt->sopt_name) { 50956723Sshin case MRT6_INIT: 51056723Sshin case MRT6_DONE: 51156723Sshin case MRT6_ADD_MIF: 51256723Sshin case MRT6_DEL_MIF: 51356723Sshin case MRT6_ADD_MFC: 51456723Sshin case MRT6_DEL_MFC: 51556723Sshin case MRT6_PIM: 516166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 517166938Sbms EOPNOTSUPP; 51856723Sshin break; 519121578Sume case IPV6_CHECKSUM: 520121578Sume error = ip6_raw_ctloutput(so, sopt); 521121578Sume break; 52253541Sshin default: 52353541Sshin error = ip6_ctloutput(so, sopt); 52453541Sshin break; 52553541Sshin } 52653541Sshin break; 52753541Sshin 52853541Sshin case SOPT_SET: 52953541Sshin switch (sopt->sopt_name) { 53056723Sshin case MRT6_INIT: 53156723Sshin case MRT6_DONE: 53256723Sshin case MRT6_ADD_MIF: 53356723Sshin case MRT6_DEL_MIF: 53456723Sshin case MRT6_ADD_MFC: 53556723Sshin case MRT6_DEL_MFC: 53656723Sshin case MRT6_PIM: 537166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 538166938Sbms EOPNOTSUPP; 53956723Sshin break; 540121578Sume case IPV6_CHECKSUM: 541121578Sume error = ip6_raw_ctloutput(so, sopt); 542121578Sume break; 54353541Sshin default: 54453541Sshin error = ip6_ctloutput(so, sopt); 54553541Sshin break; 54653541Sshin } 54753541Sshin break; 54853541Sshin } 54953541Sshin 55053541Sshin return (error); 55153541Sshin} 55253541Sshin 55353541Sshinstatic int 55483366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 55553541Sshin{ 556183550Szec INIT_VNET_INET(so->so_vnet); 55753541Sshin struct inpcb *inp; 558144261Ssam struct icmp6_filter *filter; 559157676Srwatson int error; 56053541Sshin 56153541Sshin inp = sotoinpcb(so); 562157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 563180305Srwatson 564175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 565175630Sbz if (error) 566180305Srwatson return (error); 56755009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 568157374Srwatson if (error) 569180305Srwatson return (error); 570184214Sdes filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 571157374Srwatson if (filter == NULL) 572180305Srwatson return (ENOMEM); 573181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 574181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 575132714Srwatson if (error) { 576181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 577184205Sdes free(filter, M_PCB); 578180305Srwatson return (error); 579132714Srwatson } 58053541Sshin inp = (struct inpcb *)so->so_pcb; 581181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 58253541Sshin inp->inp_vflag |= INP_IPV6; 58353541Sshin inp->in6p_ip6_nxt = (long)proto; 58453541Sshin inp->in6p_hops = -1; /* use kernel default */ 58553541Sshin inp->in6p_cksum = -1; 586144261Ssam inp->in6p_icmp6filt = filter; 58753541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 588178285Srwatson INP_WUNLOCK(inp); 589180305Srwatson return (0); 59053541Sshin} 59153541Sshin 592157370Srwatsonstatic void 59353541Sshinrip6_detach(struct socket *so) 59453541Sshin{ 595183550Szec INIT_VNET_INET(so->so_vnet); 59653541Sshin struct inpcb *inp; 59753541Sshin 59853541Sshin inp = sotoinpcb(so); 599157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 600160549Srwatson 601166938Sbms if (so == ip6_mrouter && ip6_mrouter_done) 602166938Sbms ip6_mrouter_done(); 60353541Sshin /* xxx: RSVP */ 604181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 605178285Srwatson INP_WLOCK(inp); 606184205Sdes free(inp->in6p_icmp6filt, M_PCB); 607185344Sbz in_pcbdetach(inp); 608185370Sbz in_pcbfree(inp); 609181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 61053541Sshin} 61153541Sshin 612160549Srwatson/* XXXRW: This can't ever be called. */ 613157366Srwatsonstatic void 61453541Sshinrip6_abort(struct socket *so) 61553541Sshin{ 616160549Srwatson struct inpcb *inp; 617160549Srwatson 618160549Srwatson inp = sotoinpcb(so); 619160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 620160549Srwatson 62153541Sshin soisdisconnected(so); 62253541Sshin} 62353541Sshin 624160549Srwatsonstatic void 625160549Srwatsonrip6_close(struct socket *so) 626160549Srwatson{ 627160549Srwatson struct inpcb *inp; 628160549Srwatson 629160549Srwatson inp = sotoinpcb(so); 630160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 631160549Srwatson 632160549Srwatson soisdisconnected(so); 633160549Srwatson} 634160549Srwatson 63553541Sshinstatic int 63653541Sshinrip6_disconnect(struct socket *so) 63753541Sshin{ 638180305Srwatson struct inpcb *inp; 63953541Sshin 640180305Srwatson inp = sotoinpcb(so); 641180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 642180305Srwatson 64397658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 644180305Srwatson return (ENOTCONN); 64553541Sshin inp->in6p_faddr = in6addr_any; 646157366Srwatson rip6_abort(so); 647157374Srwatson return (0); 64853541Sshin} 64953541Sshin 65053541Sshinstatic int 65183366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 65253541Sshin{ 653183550Szec INIT_VNET_NET(so->so_vnet); 654183550Szec INIT_VNET_INET(so->so_vnet); 655183550Szec INIT_VNET_INET6(so->so_vnet); 656180305Srwatson struct inpcb *inp; 65753541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 65853541Sshin struct ifaddr *ia = NULL; 659148385Sume int error = 0; 66053541Sshin 661180305Srwatson inp = sotoinpcb(so); 662157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 663180305Srwatson 66453541Sshin if (nam->sa_len != sizeof(*addr)) 665180305Srwatson return (EINVAL); 666181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 667180305Srwatson return (EADDRNOTAVAIL); 668181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 669180305Srwatson return (error); 670148385Sume 67153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 67253541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 673180305Srwatson return (EADDRNOTAVAIL); 67453541Sshin if (ia && 67553541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 67653541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 67753541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 678120856Sume return (EADDRNOTAVAIL); 67953541Sshin } 680181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 681178285Srwatson INP_WLOCK(inp); 68253541Sshin inp->in6p_laddr = addr->sin6_addr; 683178285Srwatson INP_WUNLOCK(inp); 684181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 685180305Srwatson return (0); 68653541Sshin} 68753541Sshin 68853541Sshinstatic int 68983366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 69053541Sshin{ 691183550Szec INIT_VNET_NET(so->so_vnet); 692183550Szec INIT_VNET_INET(so->so_vnet); 693183550Szec INIT_VNET_INET6(so->so_vnet); 694180305Srwatson struct inpcb *inp; 69553541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 69653541Sshin struct in6_addr *in6a = NULL; 697148385Sume struct ifnet *ifp = NULL; 698148385Sume int error = 0, scope_ambiguous = 0; 69953541Sshin 700180305Srwatson inp = sotoinpcb(so); 701157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 702180305Srwatson 70353541Sshin if (nam->sa_len != sizeof(*addr)) 704180305Srwatson return (EINVAL); 705181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 706180305Srwatson return (EADDRNOTAVAIL); 70753541Sshin if (addr->sin6_family != AF_INET6) 708180305Srwatson return (EAFNOSUPPORT); 709148385Sume 710148385Sume /* 711180305Srwatson * Application should provide a proper zone ID or the use of default 712180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 713180305Srwatson * not behave as it should, so we need a workaround. Even if an 714180305Srwatson * appropriate ID is not determined, we'll see if we can determine 715180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 716180305Srwatson * the interface below. 717148385Sume */ 718181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 719148385Sume scope_ambiguous = 1; 720181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 721180305Srwatson return (error); 722148385Sume 723181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 724178285Srwatson INP_WLOCK(inp); 72553541Sshin /* Source address selection. XXX: need pcblookup? */ 72653541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 727180371Sbz inp, NULL, so->so_cred, 728180371Sbz &ifp, &error); 729132714Srwatson if (in6a == NULL) { 730178285Srwatson INP_WUNLOCK(inp); 731181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 73253541Sshin return (error ? error : EADDRNOTAVAIL); 733132714Srwatson } 734148385Sume 735148385Sume /* XXX: see above */ 736148385Sume if (ifp && scope_ambiguous && 737148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 738178285Srwatson INP_WUNLOCK(inp); 739181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 740180305Srwatson return (error); 741148385Sume } 742148385Sume inp->in6p_faddr = addr->sin6_addr; 74353541Sshin inp->in6p_laddr = *in6a; 74453541Sshin soisconnected(so); 745178285Srwatson INP_WUNLOCK(inp); 746181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 747180305Srwatson return (0); 74853541Sshin} 74953541Sshin 75053541Sshinstatic int 75153541Sshinrip6_shutdown(struct socket *so) 75253541Sshin{ 753132714Srwatson struct inpcb *inp; 754132714Srwatson 755132714Srwatson inp = sotoinpcb(so); 756157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 757180305Srwatson 758178285Srwatson INP_WLOCK(inp); 75953541Sshin socantsendmore(so); 760178285Srwatson INP_WUNLOCK(inp); 761180305Srwatson return (0); 76253541Sshin} 76353541Sshin 76453541Sshinstatic int 76553541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 766171260Sdelphij struct mbuf *control, struct thread *td) 76753541Sshin{ 768180305Srwatson struct inpcb *inp; 76953541Sshin struct sockaddr_in6 tmp; 77053541Sshin struct sockaddr_in6 *dst; 771132714Srwatson int ret; 77253541Sshin 773180305Srwatson inp = sotoinpcb(so); 774157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 775180305Srwatson 776180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 777132714Srwatson /* Unlocked read. */ 77853541Sshin if (so->so_state & SS_ISCONNECTED) { 77953541Sshin if (nam) { 78053541Sshin m_freem(m); 781180305Srwatson return (EISCONN); 78253541Sshin } 78353541Sshin /* XXX */ 78453541Sshin bzero(&tmp, sizeof(tmp)); 78553541Sshin tmp.sin6_family = AF_INET6; 78653541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 787180990Srwatson INP_RLOCK(inp); 78853541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 789180990Srwatson sizeof(struct in6_addr)); 790180990Srwatson INP_RUNLOCK(inp); 79153541Sshin dst = &tmp; 79253541Sshin } else { 79353541Sshin if (nam == NULL) { 79453541Sshin m_freem(m); 795180305Srwatson return (ENOTCONN); 79653541Sshin } 797148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 798148385Sume m_freem(m); 799180305Srwatson return (EINVAL); 800148385Sume } 80162587Sitojun tmp = *(struct sockaddr_in6 *)nam; 80262587Sitojun dst = &tmp; 803148385Sume 804148385Sume if (dst->sin6_family == AF_UNSPEC) { 805148385Sume /* 806148385Sume * XXX: we allow this case for backward 807148385Sume * compatibility to buggy applications that 808148385Sume * rely on old (and wrong) kernel behavior. 809148385Sume */ 810148385Sume log(LOG_INFO, "rip6 SEND: address family is " 811148385Sume "unspec. Assume AF_INET6\n"); 812148385Sume dst->sin6_family = AF_INET6; 813148385Sume } else if (dst->sin6_family != AF_INET6) { 814148385Sume m_freem(m); 815148385Sume return(EAFNOSUPPORT); 816148385Sume } 81753541Sshin } 818132714Srwatson ret = rip6_output(m, so, dst, control); 819132714Srwatson return (ret); 82053541Sshin} 82153541Sshin 82253541Sshinstruct pr_usrreqs rip6_usrreqs = { 823137386Sphk .pru_abort = rip6_abort, 824137386Sphk .pru_attach = rip6_attach, 825137386Sphk .pru_bind = rip6_bind, 826137386Sphk .pru_connect = rip6_connect, 827137386Sphk .pru_control = in6_control, 828137386Sphk .pru_detach = rip6_detach, 829137386Sphk .pru_disconnect = rip6_disconnect, 830169462Srwatson .pru_peeraddr = in6_getpeeraddr, 831137386Sphk .pru_send = rip6_send, 832137386Sphk .pru_shutdown = rip6_shutdown, 833169462Srwatson .pru_sockaddr = in6_getsockaddr, 834160549Srwatson .pru_close = rip6_close, 83553541Sshin}; 836