raw_ip6.c revision 180932
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 180932 2008-07-28 19:32:18Z mav $"); 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> 8153541Sshin 8253541Sshin#include <net/if.h> 8395759Stanimura#include <net/if_types.h> 8453541Sshin#include <net/route.h> 8553541Sshin 8653541Sshin#include <netinet/in.h> 8753541Sshin#include <netinet/in_var.h> 8853541Sshin#include <netinet/in_systm.h> 8995759Stanimura#include <netinet/icmp6.h> 9095759Stanimura#include <netinet/in_pcb.h> 9162587Sitojun#include <netinet/ip6.h> 9295759Stanimura#include <netinet6/ip6protosw.h> 9356723Sshin#include <netinet6/ip6_mroute.h> 9453541Sshin#include <netinet6/in6_pcb.h> 9595759Stanimura#include <netinet6/ip6_var.h> 9653541Sshin#include <netinet6/nd6.h> 9795759Stanimura#include <netinet6/raw_ip6.h> 9862587Sitojun#include <netinet6/scope6_var.h> 9953541Sshin 100171167Sgnn#ifdef IPSEC 101105199Ssam#include <netipsec/ipsec.h> 102105199Ssam#include <netipsec/ipsec6.h> 103171167Sgnn#endif /* IPSEC */ 104105199Ssam 10553541Sshin#include <machine/stdarg.h> 10653541Sshin 10753541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 10853541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 10953541Sshin 11053541Sshin/* 11153541Sshin * Raw interface to IP6 protocol. 11253541Sshin */ 11353541Sshin 11453541Sshinextern struct inpcbhead ripcb; 11553541Sshinextern struct inpcbinfo ripcbinfo; 11653541Sshinextern u_long rip_sendspace; 11753541Sshinextern u_long rip_recvspace; 11853541Sshin 11978064Sumestruct rip6stat rip6stat; 12078064Sume 12153541Sshin/* 122166938Sbms * Hooks for multicast forwarding. 123166938Sbms */ 124166948Sbmsstruct socket *ip6_mrouter = NULL; 125166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 126166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 127166938Sbmsint (*ip6_mrouter_done)(void); 128166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 129166938Sbmsint (*mrt6_ioctl)(int, caddr_t); 130166938Sbms 131166938Sbms/* 132180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 133180305Srwatson * pass them along with mbuf chain. 13453541Sshin */ 13553541Sshinint 136171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 13753541Sshin{ 13853541Sshin struct mbuf *m = *mp; 13953541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 14053541Sshin register struct inpcb *in6p; 14153541Sshin struct inpcb *last = 0; 14278064Sume struct mbuf *opts = NULL; 143121901Sume struct sockaddr_in6 fromsa; 14453541Sshin 14578064Sume rip6stat.rip6s_ipackets++; 14678064Sume 14783934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 148180305Srwatson /* XXX Send icmp6 host/port unreach? */ 14978064Sume m_freem(m); 150180305Srwatson return (IPPROTO_DONE); 15153541Sshin } 15278064Sume 153121901Sume init_sin6(&fromsa, m); /* general init */ 15453541Sshin 155132714Srwatson INP_INFO_RLOCK(&ripcbinfo); 15653541Sshin LIST_FOREACH(in6p, &ripcb, inp_list) { 157180850Smav if ((in6p->in6p_vflag & INP_IPV6) == 0) 15853541Sshin continue; 15953541Sshin if (in6p->in6p_ip6_nxt && 16053541Sshin in6p->in6p_ip6_nxt != proto) 161180850Smav continue; 16253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 16353541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 164180850Smav continue; 16553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 16653541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 167180850Smav continue; 168180932Smav INP_RLOCK(in6p); 16978064Sume if (in6p->in6p_cksum != -1) { 17078064Sume rip6stat.rip6s_isum++; 171151459Ssuz if (in6_cksum(m, proto, *offp, 17278064Sume m->m_pkthdr.len - *offp)) { 173180932Smav INP_RUNLOCK(in6p); 17478064Sume rip6stat.rip6s_badsum++; 175180850Smav continue; 17678064Sume } 17753541Sshin } 17853541Sshin if (last) { 17953541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 18078064Sume 181171167Sgnn#ifdef IPSEC 18278064Sume /* 18378064Sume * Check AH/ESP integrity. 18478064Sume */ 185125396Sume if (n && ipsec6_in_reject(n, last)) { 18678064Sume m_freem(n); 18778064Sume ipsec6stat.in_polvio++; 188180305Srwatson /* Do not inject data into pcb. */ 189105199Ssam } else 190171167Sgnn#endif /* IPSEC */ 19153541Sshin if (n) { 19297658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 19397658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 194121674Sume ip6_savecontrol(last, n, &opts); 19553541Sshin /* strip intermediate headers */ 19653541Sshin m_adj(n, *offp); 19753541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 198121901Sume (struct sockaddr *)&fromsa, 19953541Sshin n, opts) == 0) { 20053541Sshin m_freem(n); 20153541Sshin if (opts) 20253541Sshin m_freem(opts); 20378064Sume rip6stat.rip6s_fullsock++; 20497658Stanimura } else 20553541Sshin sorwakeup(last->in6p_socket); 20653541Sshin opts = NULL; 20753541Sshin } 208178377Srwatson INP_RUNLOCK(last); 20953541Sshin } 21053541Sshin last = in6p; 21153541Sshin } 212180850Smav INP_INFO_RUNLOCK(&ripcbinfo); 213171167Sgnn#ifdef IPSEC 21478064Sume /* 21578064Sume * Check AH/ESP integrity. 21678064Sume */ 217125396Sume if (last && ipsec6_in_reject(m, last)) { 21878064Sume m_freem(m); 21978064Sume ipsec6stat.in_polvio++; 220105199Ssam ip6stat.ip6s_delivered--; 221180305Srwatson /* Do not inject data into pcb. */ 222178377Srwatson INP_RUNLOCK(last); 223105199Ssam } else 224171167Sgnn#endif /* IPSEC */ 22553541Sshin if (last) { 22697658Stanimura if (last->in6p_flags & IN6P_CONTROLOPTS || 22797658Stanimura last->in6p_socket->so_options & SO_TIMESTAMP) 228121674Sume ip6_savecontrol(last, m, &opts); 229180305Srwatson /* Strip intermediate headers. */ 23053541Sshin m_adj(m, *offp); 23153541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 232180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 23353541Sshin m_freem(m); 23453541Sshin if (opts) 23553541Sshin m_freem(opts); 23678064Sume rip6stat.rip6s_fullsock++; 23797658Stanimura } else 23853541Sshin sorwakeup(last->in6p_socket); 239178377Srwatson INP_RUNLOCK(last); 24053541Sshin } else { 24178064Sume rip6stat.rip6s_nosock++; 24278064Sume if (m->m_flags & M_MCAST) 24378064Sume rip6stat.rip6s_nosockmcast++; 24453541Sshin if (proto == IPPROTO_NONE) 24553541Sshin m_freem(m); 24653541Sshin else { 24753541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 24853541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 249180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 250180305Srwatson prvnxtp - mtod(m, char *)); 25153541Sshin } 25253541Sshin ip6stat.ip6s_delivered--; 25353541Sshin } 254180305Srwatson return (IPPROTO_DONE); 25553541Sshin} 25653541Sshin 25762587Sitojunvoid 258171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 25962587Sitojun{ 26062587Sitojun struct ip6_hdr *ip6; 26162587Sitojun struct mbuf *m; 26262587Sitojun int off = 0; 26378064Sume struct ip6ctlparam *ip6cp = NULL; 26478064Sume const struct sockaddr_in6 *sa6_src = NULL; 265125776Sume void *cmdarg; 266175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 26762587Sitojun 26862587Sitojun if (sa->sa_family != AF_INET6 || 26962587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 27062587Sitojun return; 27162587Sitojun 27262587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 27362587Sitojun return; 27462587Sitojun if (PRC_IS_REDIRECT(cmd)) 27562587Sitojun notify = in6_rtchange, d = NULL; 27662587Sitojun else if (cmd == PRC_HOSTDEAD) 27762587Sitojun d = NULL; 27862587Sitojun else if (inet6ctlerrmap[cmd] == 0) 27962587Sitojun return; 28062587Sitojun 281180305Srwatson /* 282180305Srwatson * If the parameter is from icmp6, decode it. 283180305Srwatson */ 28462587Sitojun if (d != NULL) { 28578064Sume ip6cp = (struct ip6ctlparam *)d; 28662587Sitojun m = ip6cp->ip6c_m; 28762587Sitojun ip6 = ip6cp->ip6c_ip6; 28862587Sitojun off = ip6cp->ip6c_off; 289125776Sume cmdarg = ip6cp->ip6c_cmdarg; 29078064Sume sa6_src = ip6cp->ip6c_src; 29162587Sitojun } else { 29262587Sitojun m = NULL; 29362587Sitojun ip6 = NULL; 294125776Sume cmdarg = NULL; 29578064Sume sa6_src = &sa6_any; 29662587Sitojun } 29762587Sitojun 298133192Srwatson (void) in6_pcbnotify(&ripcbinfo, sa, 0, 299180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 30062587Sitojun} 30162587Sitojun 30253541Sshin/* 303180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 304180305Srwatson * may have setup with control call. 30553541Sshin */ 30653541Sshinint 30753541Sshin#if __STDC__ 30853541Sshinrip6_output(struct mbuf *m, ...) 30953541Sshin#else 31053541Sshinrip6_output(m, va_alist) 31153541Sshin struct mbuf *m; 31253541Sshin va_dcl 31353541Sshin#endif 31453541Sshin{ 315120941Sume struct mbuf *control; 31653541Sshin struct socket *so; 31753541Sshin struct sockaddr_in6 *dstsock; 31853541Sshin struct in6_addr *dst; 31953541Sshin struct ip6_hdr *ip6; 32053541Sshin struct inpcb *in6p; 32153541Sshin u_int plen = m->m_pkthdr.len; 32253541Sshin int error = 0; 323148247Sume struct ip6_pktopts opt, *optp; 32453541Sshin struct ifnet *oifp = NULL; 32553541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 326148385Sume int scope_ambiguous = 0; 327121472Sume struct in6_addr *in6a; 32853541Sshin va_list ap; 32953541Sshin 33053541Sshin va_start(ap, m); 33153541Sshin so = va_arg(ap, struct socket *); 33253541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 33353541Sshin control = va_arg(ap, struct mbuf *); 33453541Sshin va_end(ap); 33553541Sshin 33653541Sshin in6p = sotoin6pcb(so); 337178285Srwatson INP_WLOCK(in6p); 33853541Sshin 33953541Sshin dst = &dstsock->sin6_addr; 34053541Sshin if (control) { 341148242Sume if ((error = ip6_setpktopts(control, &opt, 342175630Sbz in6p->in6p_outputopts, so->so_cred, 343175630Sbz so->so_proto->pr_protocol)) != 0) { 34453541Sshin goto bad; 345121472Sume } 346148247Sume optp = &opt; 347148247Sume } else 348148247Sume optp = in6p->in6p_outputopts; 34953541Sshin 35053541Sshin /* 351148385Sume * Check and convert scope zone ID into internal form. 352180305Srwatson * 353148385Sume * XXX: we may still need to determine the zone later. 354148385Sume */ 355148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 356148385Sume if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone) 357148385Sume scope_ambiguous = 1; 358148385Sume if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0) 359148385Sume goto bad; 360148385Sume } 361148385Sume 362148385Sume /* 363180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 364180305Srwatson * statistics. 36553541Sshin */ 36653541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 36753541Sshin struct icmp6_hdr *icmp6; 36853541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 36953541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 37053541Sshin error = ENOBUFS; 37153541Sshin goto bad; 37253541Sshin } 37353541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 37453541Sshin type = icmp6->icmp6_type; 37553541Sshin code = icmp6->icmp6_code; 37653541Sshin } 37753541Sshin 378133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 379133592Srwatson if (m == NULL) { 380133592Srwatson error = ENOBUFS; 381133592Srwatson goto bad; 382133592Srwatson } 38353541Sshin ip6 = mtod(m, struct ip6_hdr *); 38453541Sshin 38553541Sshin /* 38653541Sshin * Source address selection. 38753541Sshin */ 388180371Sbz if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 389180371Sbz &oifp, &error)) == NULL) { 390121472Sume if (error == 0) 391121472Sume error = EADDRNOTAVAIL; 392121472Sume goto bad; 39353541Sshin } 394121472Sume ip6->ip6_src = *in6a; 395148385Sume 396148385Sume if (oifp && scope_ambiguous) { 397148385Sume /* 398148385Sume * Application should provide a proper zone ID or the use of 399148385Sume * default zone IDs should be enabled. Unfortunately, some 400148385Sume * applications do not behave as it should, so we need a 401148385Sume * workaround. Even if an appropriate ID is not determined 402148385Sume * (when it's required), if we can determine the outgoing 403148385Sume * interface. determine the zone ID based on the interface. 404148385Sume */ 405148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 406148385Sume if (error != 0) 407148385Sume goto bad; 408148385Sume } 409148385Sume ip6->ip6_dst = dstsock->sin6_addr; 410148385Sume 411180305Srwatson /* 412180305Srwatson * Fill in the rest of the IPv6 header fields. 413180305Srwatson */ 41455009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 415180305Srwatson (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 41655009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 417180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 418180305Srwatson 419180305Srwatson /* 420180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 421180305Srwatson */ 42253541Sshin ip6->ip6_nxt = in6p->in6p_ip6_nxt; 42353541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 42453541Sshin 42553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 42653541Sshin in6p->in6p_cksum != -1) { 42753541Sshin struct mbuf *n; 42853541Sshin int off; 42953541Sshin u_int16_t *p; 43053541Sshin 431180305Srwatson /* Compute checksum. */ 43253541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 43353541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 43453541Sshin else 43553541Sshin off = in6p->in6p_cksum; 43653541Sshin if (plen < off + 1) { 43753541Sshin error = EINVAL; 43853541Sshin goto bad; 43953541Sshin } 44053541Sshin off += sizeof(struct ip6_hdr); 44153541Sshin 44253541Sshin n = m; 44353541Sshin while (n && n->m_len <= off) { 44453541Sshin off -= n->m_len; 44553541Sshin n = n->m_next; 44653541Sshin } 44753541Sshin if (!n) 44853541Sshin goto bad; 44953541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 45053541Sshin *p = 0; 45153541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 45253541Sshin } 45353541Sshin 454148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 45553541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 45653541Sshin if (oifp) 45753541Sshin icmp6_ifoutstat_inc(oifp, type, code); 45853541Sshin icmp6stat.icp6s_outhist[type]++; 45978064Sume } else 46078064Sume rip6stat.rip6s_opackets++; 46153541Sshin 46253541Sshin goto freectl; 46353541Sshin 46453541Sshin bad: 46553541Sshin if (m) 46653541Sshin m_freem(m); 46753541Sshin 46853541Sshin freectl: 46978064Sume if (control) { 470148247Sume ip6_clearpktopts(&opt, -1); 47153541Sshin m_freem(control); 47278064Sume } 473178285Srwatson INP_WUNLOCK(in6p); 474120856Sume return (error); 47553541Sshin} 47653541Sshin 47753541Sshin/* 47853541Sshin * Raw IPv6 socket option processing. 47953541Sshin */ 48053541Sshinint 481171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 48253541Sshin{ 48353541Sshin int error; 48453541Sshin 48553541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 48653541Sshin /* 48753541Sshin * XXX: is it better to call icmp6_ctloutput() directly 48853541Sshin * from protosw? 48953541Sshin */ 490120856Sume return (icmp6_ctloutput(so, sopt)); 49153541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 49253541Sshin return (EINVAL); 49353541Sshin 49453541Sshin error = 0; 49553541Sshin 49653541Sshin switch (sopt->sopt_dir) { 49753541Sshin case SOPT_GET: 49853541Sshin switch (sopt->sopt_name) { 49956723Sshin case MRT6_INIT: 50056723Sshin case MRT6_DONE: 50156723Sshin case MRT6_ADD_MIF: 50256723Sshin case MRT6_DEL_MIF: 50356723Sshin case MRT6_ADD_MFC: 50456723Sshin case MRT6_DEL_MFC: 50556723Sshin case MRT6_PIM: 506166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 507166938Sbms EOPNOTSUPP; 50856723Sshin break; 509121578Sume case IPV6_CHECKSUM: 510121578Sume error = ip6_raw_ctloutput(so, sopt); 511121578Sume break; 51253541Sshin default: 51353541Sshin error = ip6_ctloutput(so, sopt); 51453541Sshin break; 51553541Sshin } 51653541Sshin break; 51753541Sshin 51853541Sshin case SOPT_SET: 51953541Sshin switch (sopt->sopt_name) { 52056723Sshin case MRT6_INIT: 52156723Sshin case MRT6_DONE: 52256723Sshin case MRT6_ADD_MIF: 52356723Sshin case MRT6_DEL_MIF: 52456723Sshin case MRT6_ADD_MFC: 52556723Sshin case MRT6_DEL_MFC: 52656723Sshin case MRT6_PIM: 527166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 528166938Sbms EOPNOTSUPP; 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 54053541Sshin return (error); 54153541Sshin} 54253541Sshin 54353541Sshinstatic int 54483366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 54553541Sshin{ 54653541Sshin struct inpcb *inp; 547144261Ssam struct icmp6_filter *filter; 548157676Srwatson int error; 54953541Sshin 55053541Sshin inp = sotoinpcb(so); 551157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 552180305Srwatson 553175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 554175630Sbz if (error) 555180305Srwatson return (error); 55655009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 557157374Srwatson if (error) 558180305Srwatson return (error); 559144261Ssam MALLOC(filter, struct icmp6_filter *, 560144261Ssam sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 561157374Srwatson if (filter == NULL) 562180305Srwatson return (ENOMEM); 563157374Srwatson INP_INFO_WLOCK(&ripcbinfo); 564160491Sups error = in_pcballoc(so, &ripcbinfo); 565132714Srwatson if (error) { 566132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 567144261Ssam FREE(filter, M_PCB); 568180305Srwatson return (error); 569132714Srwatson } 57053541Sshin inp = (struct inpcb *)so->so_pcb; 571132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 57253541Sshin inp->inp_vflag |= INP_IPV6; 57353541Sshin inp->in6p_ip6_nxt = (long)proto; 57453541Sshin inp->in6p_hops = -1; /* use kernel default */ 57553541Sshin inp->in6p_cksum = -1; 576144261Ssam inp->in6p_icmp6filt = filter; 57753541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 578178285Srwatson INP_WUNLOCK(inp); 579180305Srwatson return (0); 58053541Sshin} 58153541Sshin 582157370Srwatsonstatic void 58353541Sshinrip6_detach(struct socket *so) 58453541Sshin{ 58553541Sshin struct inpcb *inp; 58653541Sshin 58753541Sshin inp = sotoinpcb(so); 588157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 589160549Srwatson 590166938Sbms if (so == ip6_mrouter && ip6_mrouter_done) 591166938Sbms ip6_mrouter_done(); 59253541Sshin /* xxx: RSVP */ 593160549Srwatson INP_INFO_WLOCK(&ripcbinfo); 594178285Srwatson INP_WLOCK(inp); 59553541Sshin if (inp->in6p_icmp6filt) { 59653541Sshin FREE(inp->in6p_icmp6filt, M_PCB); 59753541Sshin inp->in6p_icmp6filt = NULL; 59853541Sshin } 59953541Sshin in6_pcbdetach(inp); 600157374Srwatson in6_pcbfree(inp); 601132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 60253541Sshin} 60353541Sshin 604160549Srwatson/* XXXRW: This can't ever be called. */ 605157366Srwatsonstatic void 60653541Sshinrip6_abort(struct socket *so) 60753541Sshin{ 608160549Srwatson struct inpcb *inp; 609160549Srwatson 610160549Srwatson inp = sotoinpcb(so); 611160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 612160549Srwatson 61353541Sshin soisdisconnected(so); 61453541Sshin} 61553541Sshin 616160549Srwatsonstatic void 617160549Srwatsonrip6_close(struct socket *so) 618160549Srwatson{ 619160549Srwatson struct inpcb *inp; 620160549Srwatson 621160549Srwatson inp = sotoinpcb(so); 622160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 623160549Srwatson 624160549Srwatson soisdisconnected(so); 625160549Srwatson} 626160549Srwatson 62753541Sshinstatic int 62853541Sshinrip6_disconnect(struct socket *so) 62953541Sshin{ 630180305Srwatson struct inpcb *inp; 63153541Sshin 632180305Srwatson inp = sotoinpcb(so); 633180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 634180305Srwatson 63597658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 636180305Srwatson return (ENOTCONN); 63753541Sshin inp->in6p_faddr = in6addr_any; 638157366Srwatson rip6_abort(so); 639157374Srwatson return (0); 64053541Sshin} 64153541Sshin 64253541Sshinstatic int 64383366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 64453541Sshin{ 645180305Srwatson struct inpcb *inp; 64653541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 64753541Sshin struct ifaddr *ia = NULL; 648148385Sume int error = 0; 64953541Sshin 650180305Srwatson inp = sotoinpcb(so); 651157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 652180305Srwatson 65353541Sshin if (nam->sa_len != sizeof(*addr)) 654180305Srwatson return (EINVAL); 65553541Sshin if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 656180305Srwatson return (EADDRNOTAVAIL); 657148385Sume if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 658180305Srwatson return (error); 659148385Sume 66053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 66153541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 662180305Srwatson return (EADDRNOTAVAIL); 66353541Sshin if (ia && 66453541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 66553541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 66653541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 667120856Sume return (EADDRNOTAVAIL); 66853541Sshin } 669132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 670178285Srwatson INP_WLOCK(inp); 67153541Sshin inp->in6p_laddr = addr->sin6_addr; 672178285Srwatson INP_WUNLOCK(inp); 673132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 674180305Srwatson return (0); 67553541Sshin} 67653541Sshin 67753541Sshinstatic int 67883366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 67953541Sshin{ 680180305Srwatson struct inpcb *inp; 68153541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 68253541Sshin struct in6_addr *in6a = NULL; 683148385Sume struct ifnet *ifp = NULL; 684148385Sume int error = 0, scope_ambiguous = 0; 68553541Sshin 686180305Srwatson inp = sotoinpcb(so); 687157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 688180305Srwatson 68953541Sshin if (nam->sa_len != sizeof(*addr)) 690180305Srwatson return (EINVAL); 69153541Sshin if (TAILQ_EMPTY(&ifnet)) 692180305Srwatson return (EADDRNOTAVAIL); 69353541Sshin if (addr->sin6_family != AF_INET6) 694180305Srwatson return (EAFNOSUPPORT); 695148385Sume 696148385Sume /* 697180305Srwatson * Application should provide a proper zone ID or the use of default 698180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 699180305Srwatson * not behave as it should, so we need a workaround. Even if an 700180305Srwatson * appropriate ID is not determined, we'll see if we can determine 701180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 702180305Srwatson * the interface below. 703148385Sume */ 704148385Sume if (addr->sin6_scope_id == 0 && !ip6_use_defzone) 705148385Sume scope_ambiguous = 1; 706148385Sume if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 707180305Srwatson return (error); 708148385Sume 709132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 710178285Srwatson INP_WLOCK(inp); 71153541Sshin /* Source address selection. XXX: need pcblookup? */ 71253541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 713180371Sbz inp, NULL, so->so_cred, 714180371Sbz &ifp, &error); 715132714Srwatson if (in6a == NULL) { 716178285Srwatson INP_WUNLOCK(inp); 717132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 71853541Sshin return (error ? error : EADDRNOTAVAIL); 719132714Srwatson } 720148385Sume 721148385Sume /* XXX: see above */ 722148385Sume if (ifp && scope_ambiguous && 723148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 724178285Srwatson INP_WUNLOCK(inp); 725148385Sume INP_INFO_WUNLOCK(&ripcbinfo); 726180305Srwatson return (error); 727148385Sume } 728148385Sume inp->in6p_faddr = addr->sin6_addr; 72953541Sshin inp->in6p_laddr = *in6a; 73053541Sshin soisconnected(so); 731178285Srwatson INP_WUNLOCK(inp); 732132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 733180305Srwatson return (0); 73453541Sshin} 73553541Sshin 73653541Sshinstatic int 73753541Sshinrip6_shutdown(struct socket *so) 73853541Sshin{ 739132714Srwatson struct inpcb *inp; 740132714Srwatson 741132714Srwatson inp = sotoinpcb(so); 742157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 743180305Srwatson 744178285Srwatson INP_WLOCK(inp); 74553541Sshin socantsendmore(so); 746178285Srwatson INP_WUNLOCK(inp); 747180305Srwatson return (0); 74853541Sshin} 74953541Sshin 75053541Sshinstatic int 75153541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 752171260Sdelphij struct mbuf *control, struct thread *td) 75353541Sshin{ 754180305Srwatson struct inpcb *inp; 75553541Sshin struct sockaddr_in6 tmp; 75653541Sshin struct sockaddr_in6 *dst; 757132714Srwatson int ret; 75853541Sshin 759180305Srwatson inp = sotoinpcb(so); 760157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 761180305Srwatson 762132714Srwatson INP_INFO_WLOCK(&ripcbinfo); 763180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 764132714Srwatson /* Unlocked read. */ 76553541Sshin if (so->so_state & SS_ISCONNECTED) { 76653541Sshin if (nam) { 767132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 76853541Sshin m_freem(m); 769180305Srwatson return (EISCONN); 77053541Sshin } 77153541Sshin /* XXX */ 77253541Sshin bzero(&tmp, sizeof(tmp)); 77353541Sshin tmp.sin6_family = AF_INET6; 77453541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 77553541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 77653541Sshin sizeof(struct in6_addr)); 77753541Sshin dst = &tmp; 77853541Sshin } else { 77953541Sshin if (nam == NULL) { 780132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 78153541Sshin m_freem(m); 782180305Srwatson return (ENOTCONN); 78353541Sshin } 784148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 785148385Sume INP_INFO_WUNLOCK(&ripcbinfo); 786148385Sume m_freem(m); 787180305Srwatson return (EINVAL); 788148385Sume } 78962587Sitojun tmp = *(struct sockaddr_in6 *)nam; 79062587Sitojun dst = &tmp; 791148385Sume 792148385Sume if (dst->sin6_family == AF_UNSPEC) { 793148385Sume /* 794148385Sume * XXX: we allow this case for backward 795148385Sume * compatibility to buggy applications that 796148385Sume * rely on old (and wrong) kernel behavior. 797148385Sume */ 798148385Sume log(LOG_INFO, "rip6 SEND: address family is " 799148385Sume "unspec. Assume AF_INET6\n"); 800148385Sume dst->sin6_family = AF_INET6; 801148385Sume } else if (dst->sin6_family != AF_INET6) { 802148385Sume INP_INFO_WUNLOCK(&ripcbinfo); 803148385Sume m_freem(m); 804148385Sume return(EAFNOSUPPORT); 805148385Sume } 80653541Sshin } 807132714Srwatson ret = rip6_output(m, so, dst, control); 808132714Srwatson INP_INFO_WUNLOCK(&ripcbinfo); 809132714Srwatson return (ret); 81053541Sshin} 81153541Sshin 81253541Sshinstruct pr_usrreqs rip6_usrreqs = { 813137386Sphk .pru_abort = rip6_abort, 814137386Sphk .pru_attach = rip6_attach, 815137386Sphk .pru_bind = rip6_bind, 816137386Sphk .pru_connect = rip6_connect, 817137386Sphk .pru_control = in6_control, 818137386Sphk .pru_detach = rip6_detach, 819137386Sphk .pru_disconnect = rip6_disconnect, 820169462Srwatson .pru_peeraddr = in6_getpeeraddr, 821137386Sphk .pru_send = rip6_send, 822137386Sphk .pru_shutdown = rip6_shutdown, 823169462Srwatson .pru_sockaddr = in6_getsockaddr, 824160549Srwatson .pru_close = rip6_close, 82553541Sshin}; 826