raw_ip6.c revision 181803
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 181803 2008-08-17 23:27:27Z 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 11553541Sshinextern struct inpcbhead ripcb; 11653541Sshinextern struct inpcbinfo ripcbinfo; 11753541Sshinextern u_long rip_sendspace; 11853541Sshinextern u_long rip_recvspace; 11953541Sshin 12078064Sumestruct rip6stat rip6stat; 12178064Sume 12253541Sshin/* 123166938Sbms * Hooks for multicast forwarding. 124166938Sbms */ 125166948Sbmsstruct socket *ip6_mrouter = NULL; 126166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 127166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 128166938Sbmsint (*ip6_mrouter_done)(void); 129166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 130166938Sbmsint (*mrt6_ioctl)(int, caddr_t); 131166938Sbms 132166938Sbms/* 133180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 134180305Srwatson * pass them along with mbuf chain. 13553541Sshin */ 13653541Sshinint 137171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int 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; 144121901Sume struct sockaddr_in6 fromsa; 14553541Sshin 146181803Sbz V_rip6stat.rip6s_ipackets++; 14778064Sume 14883934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 149180305Srwatson /* XXX Send icmp6 host/port unreach? */ 15078064Sume m_freem(m); 151180305Srwatson return (IPPROTO_DONE); 15253541Sshin } 15378064Sume 154121901Sume init_sin6(&fromsa, m); /* general init */ 15553541Sshin 156181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 157181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 158180850Smav if ((in6p->in6p_vflag & INP_IPV6) == 0) 15953541Sshin continue; 16053541Sshin if (in6p->in6p_ip6_nxt && 16153541Sshin in6p->in6p_ip6_nxt != proto) 162180850Smav continue; 16353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 16453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 165180850Smav continue; 16653541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 16753541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 168180850Smav continue; 169180932Smav INP_RLOCK(in6p); 17078064Sume if (in6p->in6p_cksum != -1) { 171181803Sbz V_rip6stat.rip6s_isum++; 172151459Ssuz if (in6_cksum(m, proto, *offp, 17378064Sume m->m_pkthdr.len - *offp)) { 174180932Smav INP_RUNLOCK(in6p); 175181803Sbz V_rip6stat.rip6s_badsum++; 176180850Smav continue; 17778064Sume } 17853541Sshin } 17953541Sshin if (last) { 18053541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 18178064Sume 182171167Sgnn#ifdef IPSEC 18378064Sume /* 18478064Sume * Check AH/ESP integrity. 18578064Sume */ 186125396Sume if (n && ipsec6_in_reject(n, last)) { 18778064Sume m_freem(n); 188181803Sbz V_ipsec6stat.in_polvio++; 189180305Srwatson /* Do not inject data into pcb. */ 190105199Ssam } else 191171167Sgnn#endif /* 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); 204181803Sbz V_rip6stat.rip6s_fullsock++; 20597658Stanimura } else 20653541Sshin sorwakeup(last->in6p_socket); 20753541Sshin opts = NULL; 20853541Sshin } 209178377Srwatson INP_RUNLOCK(last); 21053541Sshin } 21153541Sshin last = in6p; 21253541Sshin } 213181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 214171167Sgnn#ifdef IPSEC 21578064Sume /* 21678064Sume * Check AH/ESP integrity. 21778064Sume */ 218125396Sume if (last && ipsec6_in_reject(m, last)) { 21978064Sume m_freem(m); 220181803Sbz V_ipsec6stat.in_polvio++; 221181803Sbz V_ip6stat.ip6s_delivered--; 222180305Srwatson /* Do not inject data into pcb. */ 223178377Srwatson INP_RUNLOCK(last); 224105199Ssam } else 225171167Sgnn#endif /* 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); 230180305Srwatson /* Strip intermediate headers. */ 23153541Sshin m_adj(m, *offp); 23253541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 233180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 23453541Sshin m_freem(m); 23553541Sshin if (opts) 23653541Sshin m_freem(opts); 237181803Sbz V_rip6stat.rip6s_fullsock++; 23897658Stanimura } else 23953541Sshin sorwakeup(last->in6p_socket); 240178377Srwatson INP_RUNLOCK(last); 24153541Sshin } else { 242181803Sbz V_rip6stat.rip6s_nosock++; 24378064Sume if (m->m_flags & M_MCAST) 244181803Sbz V_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, 250180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 251180305Srwatson prvnxtp - mtod(m, char *)); 25253541Sshin } 253181803Sbz V_ip6stat.ip6s_delivered--; 25453541Sshin } 255180305Srwatson return (IPPROTO_DONE); 25653541Sshin} 25753541Sshin 25862587Sitojunvoid 259171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 26062587Sitojun{ 26162587Sitojun struct ip6_hdr *ip6; 26262587Sitojun struct mbuf *m; 26362587Sitojun int off = 0; 26478064Sume struct ip6ctlparam *ip6cp = NULL; 26578064Sume const struct sockaddr_in6 *sa6_src = NULL; 266125776Sume void *cmdarg; 267175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 26862587Sitojun 26962587Sitojun if (sa->sa_family != AF_INET6 || 27062587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 27162587Sitojun return; 27262587Sitojun 27362587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 27462587Sitojun return; 27562587Sitojun if (PRC_IS_REDIRECT(cmd)) 27662587Sitojun notify = in6_rtchange, d = NULL; 27762587Sitojun else if (cmd == PRC_HOSTDEAD) 27862587Sitojun d = NULL; 27962587Sitojun else if (inet6ctlerrmap[cmd] == 0) 28062587Sitojun return; 28162587Sitojun 282180305Srwatson /* 283180305Srwatson * If the parameter is from icmp6, decode it. 284180305Srwatson */ 28562587Sitojun if (d != NULL) { 28678064Sume ip6cp = (struct ip6ctlparam *)d; 28762587Sitojun m = ip6cp->ip6c_m; 28862587Sitojun ip6 = ip6cp->ip6c_ip6; 28962587Sitojun off = ip6cp->ip6c_off; 290125776Sume cmdarg = ip6cp->ip6c_cmdarg; 29178064Sume sa6_src = ip6cp->ip6c_src; 29262587Sitojun } else { 29362587Sitojun m = NULL; 29462587Sitojun ip6 = NULL; 295125776Sume cmdarg = NULL; 29678064Sume sa6_src = &sa6_any; 29762587Sitojun } 29862587Sitojun 299181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 300180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 30162587Sitojun} 30262587Sitojun 30353541Sshin/* 304180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 305180305Srwatson * may have setup with control call. 30653541Sshin */ 30753541Sshinint 30853541Sshin#if __STDC__ 30953541Sshinrip6_output(struct mbuf *m, ...) 31053541Sshin#else 31153541Sshinrip6_output(m, va_alist) 31253541Sshin struct mbuf *m; 31353541Sshin va_dcl 31453541Sshin#endif 31553541Sshin{ 316120941Sume struct mbuf *control; 31753541Sshin struct socket *so; 31853541Sshin struct sockaddr_in6 *dstsock; 31953541Sshin struct in6_addr *dst; 32053541Sshin struct ip6_hdr *ip6; 32153541Sshin struct inpcb *in6p; 32253541Sshin u_int plen = m->m_pkthdr.len; 32353541Sshin int error = 0; 324148247Sume struct ip6_pktopts opt, *optp; 32553541Sshin struct ifnet *oifp = NULL; 32653541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 327148385Sume int scope_ambiguous = 0; 328121472Sume struct in6_addr *in6a; 32953541Sshin va_list ap; 33053541Sshin 33153541Sshin va_start(ap, m); 33253541Sshin so = va_arg(ap, struct socket *); 33353541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 33453541Sshin control = va_arg(ap, struct mbuf *); 33553541Sshin va_end(ap); 33653541Sshin 33753541Sshin in6p = sotoin6pcb(so); 338178285Srwatson INP_WLOCK(in6p); 33953541Sshin 34053541Sshin dst = &dstsock->sin6_addr; 34153541Sshin if (control) { 342148242Sume if ((error = ip6_setpktopts(control, &opt, 343175630Sbz in6p->in6p_outputopts, so->so_cred, 344175630Sbz so->so_proto->pr_protocol)) != 0) { 34553541Sshin goto bad; 346121472Sume } 347148247Sume optp = &opt; 348148247Sume } else 349148247Sume optp = in6p->in6p_outputopts; 35053541Sshin 35153541Sshin /* 352148385Sume * Check and convert scope zone ID into internal form. 353180305Srwatson * 354148385Sume * XXX: we may still need to determine the zone later. 355148385Sume */ 356148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 357181803Sbz if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone) 358148385Sume scope_ambiguous = 1; 359181803Sbz if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0) 360148385Sume goto bad; 361148385Sume } 362148385Sume 363148385Sume /* 364180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 365180305Srwatson * statistics. 36653541Sshin */ 36753541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 36853541Sshin struct icmp6_hdr *icmp6; 36953541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 37053541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 37153541Sshin error = ENOBUFS; 37253541Sshin goto bad; 37353541Sshin } 37453541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 37553541Sshin type = icmp6->icmp6_type; 37653541Sshin code = icmp6->icmp6_code; 37753541Sshin } 37853541Sshin 379133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 380133592Srwatson if (m == NULL) { 381133592Srwatson error = ENOBUFS; 382133592Srwatson goto bad; 383133592Srwatson } 38453541Sshin ip6 = mtod(m, struct ip6_hdr *); 38553541Sshin 38653541Sshin /* 38753541Sshin * Source address selection. 38853541Sshin */ 389180371Sbz if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 390180371Sbz &oifp, &error)) == NULL) { 391121472Sume if (error == 0) 392121472Sume error = EADDRNOTAVAIL; 393121472Sume goto bad; 39453541Sshin } 395121472Sume ip6->ip6_src = *in6a; 396148385Sume 397148385Sume if (oifp && scope_ambiguous) { 398148385Sume /* 399148385Sume * Application should provide a proper zone ID or the use of 400148385Sume * default zone IDs should be enabled. Unfortunately, some 401148385Sume * applications do not behave as it should, so we need a 402148385Sume * workaround. Even if an appropriate ID is not determined 403148385Sume * (when it's required), if we can determine the outgoing 404148385Sume * interface. determine the zone ID based on the interface. 405148385Sume */ 406148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 407148385Sume if (error != 0) 408148385Sume goto bad; 409148385Sume } 410148385Sume ip6->ip6_dst = dstsock->sin6_addr; 411148385Sume 412180305Srwatson /* 413180305Srwatson * Fill in the rest of the IPv6 header fields. 414180305Srwatson */ 41555009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 416180305Srwatson (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 41755009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 418180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 419180305Srwatson 420180305Srwatson /* 421180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 422180305Srwatson */ 42353541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 42453541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 42553541Sshin 42653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 42753541Sshin in6p->in6p_cksum != -1) { 42853541Sshin struct mbuf *n; 42953541Sshin int off; 43053541Sshin u_int16_t *p; 43153541Sshin 432180305Srwatson /* Compute checksum. */ 43353541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 43453541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 43553541Sshin else 43653541Sshin off = in6p->in6p_cksum; 43753541Sshin if (plen < off + 1) { 43853541Sshin error = EINVAL; 43953541Sshin goto bad; 44053541Sshin } 44153541Sshin off += sizeof(struct ip6_hdr); 44253541Sshin 44353541Sshin n = m; 44453541Sshin while (n && n->m_len <= off) { 44553541Sshin off -= n->m_len; 44653541Sshin n = n->m_next; 44753541Sshin } 44853541Sshin if (!n) 44953541Sshin goto bad; 45053541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 45153541Sshin *p = 0; 45253541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 45353541Sshin } 45453541Sshin 455148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 45653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 45753541Sshin if (oifp) 45853541Sshin icmp6_ifoutstat_inc(oifp, type, code); 459181803Sbz V_icmp6stat.icp6s_outhist[type]++; 46078064Sume } else 461181803Sbz V_rip6stat.rip6s_opackets++; 46253541Sshin 46353541Sshin goto freectl; 46453541Sshin 46553541Sshin bad: 46653541Sshin if (m) 46753541Sshin m_freem(m); 46853541Sshin 46953541Sshin freectl: 47078064Sume if (control) { 471148247Sume ip6_clearpktopts(&opt, -1); 47253541Sshin m_freem(control); 47378064Sume } 474178285Srwatson INP_WUNLOCK(in6p); 475120856Sume return (error); 47653541Sshin} 47753541Sshin 47853541Sshin/* 47953541Sshin * Raw IPv6 socket option processing. 48053541Sshin */ 48153541Sshinint 482171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 48353541Sshin{ 48453541Sshin int error; 48553541Sshin 48653541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 48753541Sshin /* 48853541Sshin * XXX: is it better to call icmp6_ctloutput() directly 48953541Sshin * from protosw? 49053541Sshin */ 491120856Sume return (icmp6_ctloutput(so, sopt)); 49253541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 49353541Sshin return (EINVAL); 49453541Sshin 49553541Sshin error = 0; 49653541Sshin 49753541Sshin switch (sopt->sopt_dir) { 49853541Sshin case SOPT_GET: 49953541Sshin switch (sopt->sopt_name) { 50056723Sshin case MRT6_INIT: 50156723Sshin case MRT6_DONE: 50256723Sshin case MRT6_ADD_MIF: 50356723Sshin case MRT6_DEL_MIF: 50456723Sshin case MRT6_ADD_MFC: 50556723Sshin case MRT6_DEL_MFC: 50656723Sshin case MRT6_PIM: 507166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 508166938Sbms EOPNOTSUPP; 50956723Sshin break; 510121578Sume case IPV6_CHECKSUM: 511121578Sume error = ip6_raw_ctloutput(so, sopt); 512121578Sume break; 51353541Sshin default: 51453541Sshin error = ip6_ctloutput(so, sopt); 51553541Sshin break; 51653541Sshin } 51753541Sshin break; 51853541Sshin 51953541Sshin case SOPT_SET: 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: 528166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 529166938Sbms EOPNOTSUPP; 53056723Sshin break; 531121578Sume case IPV6_CHECKSUM: 532121578Sume error = ip6_raw_ctloutput(so, sopt); 533121578Sume break; 53453541Sshin default: 53553541Sshin error = ip6_ctloutput(so, sopt); 53653541Sshin break; 53753541Sshin } 53853541Sshin break; 53953541Sshin } 54053541Sshin 54153541Sshin return (error); 54253541Sshin} 54353541Sshin 54453541Sshinstatic int 54583366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 54653541Sshin{ 54753541Sshin struct inpcb *inp; 548144261Ssam struct icmp6_filter *filter; 549157676Srwatson int error; 55053541Sshin 55153541Sshin inp = sotoinpcb(so); 552157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 553180305Srwatson 554175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 555175630Sbz if (error) 556180305Srwatson return (error); 55755009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 558157374Srwatson if (error) 559180305Srwatson return (error); 560144261Ssam MALLOC(filter, struct icmp6_filter *, 561144261Ssam sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 562157374Srwatson if (filter == NULL) 563180305Srwatson return (ENOMEM); 564181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 565181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 566132714Srwatson if (error) { 567181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 568144261Ssam FREE(filter, M_PCB); 569180305Srwatson return (error); 570132714Srwatson } 57153541Sshin inp = (struct inpcb *)so->so_pcb; 572181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 57353541Sshin inp->inp_vflag |= INP_IPV6; 57453541Sshin inp->in6p_ip6_nxt = (long)proto; 57553541Sshin inp->in6p_hops = -1; /* use kernel default */ 57653541Sshin inp->in6p_cksum = -1; 577144261Ssam inp->in6p_icmp6filt = filter; 57853541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 579178285Srwatson INP_WUNLOCK(inp); 580180305Srwatson return (0); 58153541Sshin} 58253541Sshin 583157370Srwatsonstatic void 58453541Sshinrip6_detach(struct socket *so) 58553541Sshin{ 58653541Sshin struct inpcb *inp; 58753541Sshin 58853541Sshin inp = sotoinpcb(so); 589157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 590160549Srwatson 591166938Sbms if (so == ip6_mrouter && ip6_mrouter_done) 592166938Sbms ip6_mrouter_done(); 59353541Sshin /* xxx: RSVP */ 594181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 595178285Srwatson INP_WLOCK(inp); 596180965Srwatson FREE(inp->in6p_icmp6filt, M_PCB); 59753541Sshin in6_pcbdetach(inp); 598157374Srwatson in6_pcbfree(inp); 599181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 60053541Sshin} 60153541Sshin 602160549Srwatson/* XXXRW: This can't ever be called. */ 603157366Srwatsonstatic void 60453541Sshinrip6_abort(struct socket *so) 60553541Sshin{ 606160549Srwatson struct inpcb *inp; 607160549Srwatson 608160549Srwatson inp = sotoinpcb(so); 609160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 610160549Srwatson 61153541Sshin soisdisconnected(so); 61253541Sshin} 61353541Sshin 614160549Srwatsonstatic void 615160549Srwatsonrip6_close(struct socket *so) 616160549Srwatson{ 617160549Srwatson struct inpcb *inp; 618160549Srwatson 619160549Srwatson inp = sotoinpcb(so); 620160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 621160549Srwatson 622160549Srwatson soisdisconnected(so); 623160549Srwatson} 624160549Srwatson 62553541Sshinstatic int 62653541Sshinrip6_disconnect(struct socket *so) 62753541Sshin{ 628180305Srwatson struct inpcb *inp; 62953541Sshin 630180305Srwatson inp = sotoinpcb(so); 631180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 632180305Srwatson 63397658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 634180305Srwatson return (ENOTCONN); 63553541Sshin inp->in6p_faddr = in6addr_any; 636157366Srwatson rip6_abort(so); 637157374Srwatson return (0); 63853541Sshin} 63953541Sshin 64053541Sshinstatic int 64183366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 64253541Sshin{ 643180305Srwatson struct inpcb *inp; 64453541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 64553541Sshin struct ifaddr *ia = NULL; 646148385Sume int error = 0; 64753541Sshin 648180305Srwatson inp = sotoinpcb(so); 649157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 650180305Srwatson 65153541Sshin if (nam->sa_len != sizeof(*addr)) 652180305Srwatson return (EINVAL); 653181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 654180305Srwatson return (EADDRNOTAVAIL); 655181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 656180305Srwatson return (error); 657148385Sume 65853541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 65953541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 660180305Srwatson return (EADDRNOTAVAIL); 66153541Sshin if (ia && 66253541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 66353541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 66453541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 665120856Sume return (EADDRNOTAVAIL); 66653541Sshin } 667181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 668178285Srwatson INP_WLOCK(inp); 66953541Sshin inp->in6p_laddr = addr->sin6_addr; 670178285Srwatson INP_WUNLOCK(inp); 671181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 672180305Srwatson return (0); 67353541Sshin} 67453541Sshin 67553541Sshinstatic int 67683366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 67753541Sshin{ 678180305Srwatson struct inpcb *inp; 67953541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 68053541Sshin struct in6_addr *in6a = NULL; 681148385Sume struct ifnet *ifp = NULL; 682148385Sume int error = 0, scope_ambiguous = 0; 68353541Sshin 684180305Srwatson inp = sotoinpcb(so); 685157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 686180305Srwatson 68753541Sshin if (nam->sa_len != sizeof(*addr)) 688180305Srwatson return (EINVAL); 689181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 690180305Srwatson return (EADDRNOTAVAIL); 69153541Sshin if (addr->sin6_family != AF_INET6) 692180305Srwatson return (EAFNOSUPPORT); 693148385Sume 694148385Sume /* 695180305Srwatson * Application should provide a proper zone ID or the use of default 696180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 697180305Srwatson * not behave as it should, so we need a workaround. Even if an 698180305Srwatson * appropriate ID is not determined, we'll see if we can determine 699180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 700180305Srwatson * the interface below. 701148385Sume */ 702181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 703148385Sume scope_ambiguous = 1; 704181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 705180305Srwatson return (error); 706148385Sume 707181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 708178285Srwatson INP_WLOCK(inp); 70953541Sshin /* Source address selection. XXX: need pcblookup? */ 71053541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 711180371Sbz inp, NULL, so->so_cred, 712180371Sbz &ifp, &error); 713132714Srwatson if (in6a == NULL) { 714178285Srwatson INP_WUNLOCK(inp); 715181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 71653541Sshin return (error ? error : EADDRNOTAVAIL); 717132714Srwatson } 718148385Sume 719148385Sume /* XXX: see above */ 720148385Sume if (ifp && scope_ambiguous && 721148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 722178285Srwatson INP_WUNLOCK(inp); 723181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 724180305Srwatson return (error); 725148385Sume } 726148385Sume inp->in6p_faddr = addr->sin6_addr; 72753541Sshin inp->in6p_laddr = *in6a; 72853541Sshin soisconnected(so); 729178285Srwatson INP_WUNLOCK(inp); 730181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 731180305Srwatson return (0); 73253541Sshin} 73353541Sshin 73453541Sshinstatic int 73553541Sshinrip6_shutdown(struct socket *so) 73653541Sshin{ 737132714Srwatson struct inpcb *inp; 738132714Srwatson 739132714Srwatson inp = sotoinpcb(so); 740157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 741180305Srwatson 742178285Srwatson INP_WLOCK(inp); 74353541Sshin socantsendmore(so); 744178285Srwatson INP_WUNLOCK(inp); 745180305Srwatson return (0); 74653541Sshin} 74753541Sshin 74853541Sshinstatic int 74953541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 750171260Sdelphij struct mbuf *control, struct thread *td) 75153541Sshin{ 752180305Srwatson struct inpcb *inp; 75353541Sshin struct sockaddr_in6 tmp; 75453541Sshin struct sockaddr_in6 *dst; 755132714Srwatson int ret; 75653541Sshin 757180305Srwatson inp = sotoinpcb(so); 758157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 759180305Srwatson 760180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 761132714Srwatson /* Unlocked read. */ 76253541Sshin if (so->so_state & SS_ISCONNECTED) { 76353541Sshin if (nam) { 76453541Sshin m_freem(m); 765180305Srwatson return (EISCONN); 76653541Sshin } 76753541Sshin /* XXX */ 76853541Sshin bzero(&tmp, sizeof(tmp)); 76953541Sshin tmp.sin6_family = AF_INET6; 77053541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 771180990Srwatson INP_RLOCK(inp); 77253541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 773180990Srwatson sizeof(struct in6_addr)); 774180990Srwatson INP_RUNLOCK(inp); 77553541Sshin dst = &tmp; 77653541Sshin } else { 77753541Sshin if (nam == NULL) { 77853541Sshin m_freem(m); 779180305Srwatson return (ENOTCONN); 78053541Sshin } 781148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 782148385Sume m_freem(m); 783180305Srwatson return (EINVAL); 784148385Sume } 78562587Sitojun tmp = *(struct sockaddr_in6 *)nam; 78662587Sitojun dst = &tmp; 787148385Sume 788148385Sume if (dst->sin6_family == AF_UNSPEC) { 789148385Sume /* 790148385Sume * XXX: we allow this case for backward 791148385Sume * compatibility to buggy applications that 792148385Sume * rely on old (and wrong) kernel behavior. 793148385Sume */ 794148385Sume log(LOG_INFO, "rip6 SEND: address family is " 795148385Sume "unspec. Assume AF_INET6\n"); 796148385Sume dst->sin6_family = AF_INET6; 797148385Sume } else if (dst->sin6_family != AF_INET6) { 798148385Sume m_freem(m); 799148385Sume return(EAFNOSUPPORT); 800148385Sume } 80153541Sshin } 802132714Srwatson ret = rip6_output(m, so, dst, control); 803132714Srwatson return (ret); 80453541Sshin} 80553541Sshin 80653541Sshinstruct pr_usrreqs rip6_usrreqs = { 807137386Sphk .pru_abort = rip6_abort, 808137386Sphk .pru_attach = rip6_attach, 809137386Sphk .pru_bind = rip6_bind, 810137386Sphk .pru_connect = rip6_connect, 811137386Sphk .pru_control = in6_control, 812137386Sphk .pru_detach = rip6_detach, 813137386Sphk .pru_disconnect = rip6_disconnect, 814169462Srwatson .pru_peeraddr = in6_getpeeraddr, 815137386Sphk .pru_send = rip6_send, 816137386Sphk .pru_shutdown = rip6_shutdown, 817169462Srwatson .pru_sockaddr = in6_getsockaddr, 818160549Srwatson .pru_close = rip6_close, 81953541Sshin}; 820