raw_ip6.c revision 249294
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 249294 2013-04-09 07:11:22Z ae $"); 64174510Sobrien 6555009Sshin#include "opt_ipsec.h" 6678064Sume#include "opt_inet6.h" 6755009Sshin 6853541Sshin#include <sys/param.h> 6995759Stanimura#include <sys/errno.h> 70185435Sbz#include <sys/jail.h> 7195759Stanimura#include <sys/lock.h> 7253541Sshin#include <sys/malloc.h> 7395759Stanimura#include <sys/mbuf.h> 74170689Srwatson#include <sys/priv.h> 7553541Sshin#include <sys/proc.h> 7695759Stanimura#include <sys/protosw.h> 7795759Stanimura#include <sys/signalvar.h> 7853541Sshin#include <sys/socket.h> 7953541Sshin#include <sys/socketvar.h> 8095759Stanimura#include <sys/sx.h> 81148385Sume#include <sys/syslog.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8495759Stanimura#include <net/if_types.h> 8553541Sshin#include <net/route.h> 86185571Sbz#include <net/vnet.h> 8753541Sshin 8853541Sshin#include <netinet/in.h> 8953541Sshin#include <netinet/in_var.h> 9053541Sshin#include <netinet/in_systm.h> 91185571Sbz#include <netinet/in_pcb.h> 92185571Sbz 9395759Stanimura#include <netinet/icmp6.h> 9462587Sitojun#include <netinet/ip6.h> 95211501Sanchie#include <netinet/ip_var.h> 9695759Stanimura#include <netinet6/ip6protosw.h> 9756723Sshin#include <netinet6/ip6_mroute.h> 9853541Sshin#include <netinet6/in6_pcb.h> 9995759Stanimura#include <netinet6/ip6_var.h> 10053541Sshin#include <netinet6/nd6.h> 10195759Stanimura#include <netinet6/raw_ip6.h> 10262587Sitojun#include <netinet6/scope6_var.h> 103211501Sanchie#include <netinet6/send.h> 10453541Sshin 105171167Sgnn#ifdef IPSEC 106105199Ssam#include <netipsec/ipsec.h> 107105199Ssam#include <netipsec/ipsec6.h> 108171167Sgnn#endif /* IPSEC */ 109105199Ssam 11053541Sshin#include <machine/stdarg.h> 11153541Sshin 11253541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11353541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11453541Sshin 11553541Sshin/* 11653541Sshin * Raw interface to IP6 protocol. 11753541Sshin */ 11853541Sshin 119195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb); 120195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo); 121195727Srwatson#define V_ripcb VNET(ripcb) 122195727Srwatson#define V_ripcbinfo VNET(ripcbinfo) 123185348Szec 12453541Sshinextern u_long rip_sendspace; 12553541Sshinextern u_long rip_recvspace; 12653541Sshin 127207369SbzVNET_DEFINE(struct rip6stat, rip6stat); 128207369Sbz 12953541Sshin/* 130191672Sbms * Hooks for multicast routing. They all default to NULL, so leave them not 131191672Sbms * initialized and rely on BSS being set to 0. 132166938Sbms */ 133191672Sbms 134191672Sbms/* 135191672Sbms * The socket used to communicate with the multicast routing daemon. 136191672Sbms */ 137195699SrwatsonVNET_DEFINE(struct socket *, ip6_mrouter); 138191672Sbms 139191672Sbms/* 140191672Sbms * The various mrouter functions. 141191672Sbms */ 142166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 143166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 144166938Sbmsint (*ip6_mrouter_done)(void); 145166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 146194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t); 147166938Sbms 148166938Sbms/* 149180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 150180305Srwatson * pass them along with mbuf chain. 15153541Sshin */ 15253541Sshinint 153171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 15453541Sshin{ 155191672Sbms struct ifnet *ifp; 15653541Sshin struct mbuf *m = *mp; 15753541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 15853541Sshin register struct inpcb *in6p; 15953541Sshin struct inpcb *last = 0; 16078064Sume struct mbuf *opts = NULL; 161121901Sume struct sockaddr_in6 fromsa; 16253541Sshin 163181803Sbz V_rip6stat.rip6s_ipackets++; 16478064Sume 16583934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 166180305Srwatson /* XXX Send icmp6 host/port unreach? */ 16778064Sume m_freem(m); 168180305Srwatson return (IPPROTO_DONE); 16953541Sshin } 17078064Sume 171121901Sume init_sin6(&fromsa, m); /* general init */ 17253541Sshin 173191672Sbms ifp = m->m_pkthdr.rcvif; 174191672Sbms 175181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 176181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 177185435Sbz /* XXX inp locking */ 178186141Sbz if ((in6p->inp_vflag & INP_IPV6) == 0) 17953541Sshin continue; 180186141Sbz if (in6p->inp_ip_p && 181186141Sbz in6p->inp_ip_p != proto) 182180850Smav continue; 18353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 18453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 185180850Smav continue; 18653541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 18753541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 188180850Smav continue; 189200473Sbz if (jailed_without_vnet(in6p->inp_cred)) { 190191672Sbms /* 191191672Sbms * Allow raw socket in jail to receive multicast; 192191672Sbms * assume process had PRIV_NETINET_RAW at attach, 193191672Sbms * and fall through into normal filter path if so. 194191672Sbms */ 195191672Sbms if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 196191672Sbms prison_check_ip6(in6p->inp_cred, 197191672Sbms &ip6->ip6_dst) != 0) 198191672Sbms continue; 199191672Sbms } 200248180Sae INP_RLOCK(in6p); 20178064Sume if (in6p->in6p_cksum != -1) { 202181803Sbz V_rip6stat.rip6s_isum++; 203151459Ssuz if (in6_cksum(m, proto, *offp, 20478064Sume m->m_pkthdr.len - *offp)) { 205180932Smav INP_RUNLOCK(in6p); 206181803Sbz V_rip6stat.rip6s_badsum++; 207180850Smav continue; 20878064Sume } 20953541Sshin } 210191672Sbms /* 211191672Sbms * If this raw socket has multicast state, and we 212191672Sbms * have received a multicast, check if this socket 213191672Sbms * should receive it, as multicast filtering is now 214191672Sbms * the responsibility of the transport layer. 215191672Sbms */ 216191672Sbms if (in6p->in6p_moptions && 217191672Sbms IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 218199518Sbms /* 219199518Sbms * If the incoming datagram is for MLD, allow it 220199518Sbms * through unconditionally to the raw socket. 221199518Sbms * 222199518Sbms * Use the M_RTALERT_MLD flag to check for MLD 223199518Sbms * traffic without having to inspect the mbuf chain 224199518Sbms * more deeply, as all MLDv1/v2 host messages MUST 225199518Sbms * contain the Router Alert option. 226199518Sbms * 227199518Sbms * In the case of MLDv1, we may not have explicitly 228199518Sbms * joined the group, and may have set IFF_ALLMULTI 229199518Sbms * on the interface. im6o_mc_filter() may discard 230199518Sbms * control traffic we actually need to see. 231199518Sbms * 232199518Sbms * Userland multicast routing daemons should continue 233199518Sbms * filter the control traffic appropriately. 234199518Sbms */ 235191672Sbms int blocked; 236191672Sbms 237199518Sbms blocked = MCAST_PASS; 238199518Sbms if ((m->m_flags & M_RTALERT_MLD) == 0) { 239199518Sbms struct sockaddr_in6 mcaddr; 240191672Sbms 241199518Sbms bzero(&mcaddr, sizeof(struct sockaddr_in6)); 242199518Sbms mcaddr.sin6_len = sizeof(struct sockaddr_in6); 243199518Sbms mcaddr.sin6_family = AF_INET6; 244199518Sbms mcaddr.sin6_addr = ip6->ip6_dst; 245199518Sbms 246199518Sbms blocked = im6o_mc_filter(in6p->in6p_moptions, 247199518Sbms ifp, 248199518Sbms (struct sockaddr *)&mcaddr, 249199518Sbms (struct sockaddr *)&fromsa); 250199518Sbms } 251191672Sbms if (blocked != MCAST_PASS) { 252191672Sbms IP6STAT_INC(ip6s_notmember); 253211301Sbz INP_RUNLOCK(in6p); 254191672Sbms continue; 255191672Sbms } 256191672Sbms } 257186163Skmacy if (last != NULL) { 25853541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 25978064Sume 260171167Sgnn#ifdef IPSEC 26178064Sume /* 26278064Sume * Check AH/ESP integrity. 26378064Sume */ 264125396Sume if (n && ipsec6_in_reject(n, last)) { 26578064Sume m_freem(n); 266181803Sbz V_ipsec6stat.in_polvio++; 267180305Srwatson /* Do not inject data into pcb. */ 268105199Ssam } else 269171167Sgnn#endif /* IPSEC */ 27053541Sshin if (n) { 271186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 272186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 273121674Sume ip6_savecontrol(last, n, &opts); 27453541Sshin /* strip intermediate headers */ 27553541Sshin m_adj(n, *offp); 276186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 277121901Sume (struct sockaddr *)&fromsa, 27853541Sshin n, opts) == 0) { 27953541Sshin m_freem(n); 28053541Sshin if (opts) 28153541Sshin m_freem(opts); 282181803Sbz V_rip6stat.rip6s_fullsock++; 28397658Stanimura } else 284186141Sbz sorwakeup(last->inp_socket); 28553541Sshin opts = NULL; 28653541Sshin } 287178377Srwatson INP_RUNLOCK(last); 28853541Sshin } 28953541Sshin last = in6p; 29053541Sshin } 291181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 292171167Sgnn#ifdef IPSEC 29378064Sume /* 29478064Sume * Check AH/ESP integrity. 29578064Sume */ 296186170Skmacy if ((last != NULL) && ipsec6_in_reject(m, last)) { 29778064Sume m_freem(m); 298181803Sbz V_ipsec6stat.in_polvio++; 299249294Sae IP6STAT_DEC(ip6s_delivered); 300180305Srwatson /* Do not inject data into pcb. */ 301178377Srwatson INP_RUNLOCK(last); 302105199Ssam } else 303171167Sgnn#endif /* IPSEC */ 304186163Skmacy if (last != NULL) { 305186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 306186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 307121674Sume ip6_savecontrol(last, m, &opts); 308180305Srwatson /* Strip intermediate headers. */ 30953541Sshin m_adj(m, *offp); 310186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 311180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 31253541Sshin m_freem(m); 31353541Sshin if (opts) 31453541Sshin m_freem(opts); 315181803Sbz V_rip6stat.rip6s_fullsock++; 31697658Stanimura } else 317186141Sbz sorwakeup(last->inp_socket); 318178377Srwatson INP_RUNLOCK(last); 31953541Sshin } else { 320181803Sbz V_rip6stat.rip6s_nosock++; 32178064Sume if (m->m_flags & M_MCAST) 322181803Sbz V_rip6stat.rip6s_nosockmcast++; 32353541Sshin if (proto == IPPROTO_NONE) 32453541Sshin m_freem(m); 32553541Sshin else { 32653541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 32753541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 328180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 329180305Srwatson prvnxtp - mtod(m, char *)); 33053541Sshin } 331249294Sae IP6STAT_DEC(ip6s_delivered); 33253541Sshin } 333180305Srwatson return (IPPROTO_DONE); 33453541Sshin} 33553541Sshin 33662587Sitojunvoid 337171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 33862587Sitojun{ 33962587Sitojun struct ip6_hdr *ip6; 34062587Sitojun struct mbuf *m; 34162587Sitojun int off = 0; 34278064Sume struct ip6ctlparam *ip6cp = NULL; 34378064Sume const struct sockaddr_in6 *sa6_src = NULL; 344125776Sume void *cmdarg; 345175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 34662587Sitojun 34762587Sitojun if (sa->sa_family != AF_INET6 || 34862587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 34962587Sitojun return; 35062587Sitojun 35162587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 35262587Sitojun return; 35362587Sitojun if (PRC_IS_REDIRECT(cmd)) 35462587Sitojun notify = in6_rtchange, d = NULL; 35562587Sitojun else if (cmd == PRC_HOSTDEAD) 35662587Sitojun d = NULL; 35762587Sitojun else if (inet6ctlerrmap[cmd] == 0) 35862587Sitojun return; 35962587Sitojun 360180305Srwatson /* 361180305Srwatson * If the parameter is from icmp6, decode it. 362180305Srwatson */ 36362587Sitojun if (d != NULL) { 36478064Sume ip6cp = (struct ip6ctlparam *)d; 36562587Sitojun m = ip6cp->ip6c_m; 36662587Sitojun ip6 = ip6cp->ip6c_ip6; 36762587Sitojun off = ip6cp->ip6c_off; 368125776Sume cmdarg = ip6cp->ip6c_cmdarg; 36978064Sume sa6_src = ip6cp->ip6c_src; 37062587Sitojun } else { 37162587Sitojun m = NULL; 37262587Sitojun ip6 = NULL; 373125776Sume cmdarg = NULL; 37478064Sume sa6_src = &sa6_any; 37562587Sitojun } 37662587Sitojun 377181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 378180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 37962587Sitojun} 38062587Sitojun 38153541Sshin/* 382180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 383180305Srwatson * may have setup with control call. 38453541Sshin */ 38553541Sshinint 38653541Sshin#if __STDC__ 38753541Sshinrip6_output(struct mbuf *m, ...) 38853541Sshin#else 38953541Sshinrip6_output(m, va_alist) 39053541Sshin struct mbuf *m; 39153541Sshin va_dcl 39253541Sshin#endif 39353541Sshin{ 394120941Sume struct mbuf *control; 395211501Sanchie struct m_tag *mtag; 39653541Sshin struct socket *so; 39753541Sshin struct sockaddr_in6 *dstsock; 39853541Sshin struct in6_addr *dst; 39953541Sshin struct ip6_hdr *ip6; 40053541Sshin struct inpcb *in6p; 40153541Sshin u_int plen = m->m_pkthdr.len; 40253541Sshin int error = 0; 403148247Sume struct ip6_pktopts opt, *optp; 40453541Sshin struct ifnet *oifp = NULL; 40553541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 406148385Sume int scope_ambiguous = 0; 407211435Sume int use_defzone = 0; 408194777Sbz struct in6_addr in6a; 40953541Sshin va_list ap; 41053541Sshin 41153541Sshin va_start(ap, m); 41253541Sshin so = va_arg(ap, struct socket *); 41353541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 41453541Sshin control = va_arg(ap, struct mbuf *); 41553541Sshin va_end(ap); 41653541Sshin 417186141Sbz in6p = sotoinpcb(so); 418178285Srwatson INP_WLOCK(in6p); 41953541Sshin 42053541Sshin dst = &dstsock->sin6_addr; 421186170Skmacy if (control != NULL) { 422148242Sume if ((error = ip6_setpktopts(control, &opt, 423175630Sbz in6p->in6p_outputopts, so->so_cred, 424175630Sbz so->so_proto->pr_protocol)) != 0) { 42553541Sshin goto bad; 426121472Sume } 427148247Sume optp = &opt; 428148247Sume } else 429148247Sume optp = in6p->in6p_outputopts; 43053541Sshin 43153541Sshin /* 432148385Sume * Check and convert scope zone ID into internal form. 433180305Srwatson * 434148385Sume * XXX: we may still need to determine the zone later. 435148385Sume */ 436148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 437211530Sume if (!optp || !optp->ip6po_pktinfo || 438211530Sume !optp->ip6po_pktinfo->ipi6_ifindex) 439211435Sume use_defzone = V_ip6_use_defzone; 440211435Sume if (dstsock->sin6_scope_id == 0 && !use_defzone) 441148385Sume scope_ambiguous = 1; 442211435Sume if ((error = sa6_embedscope(dstsock, use_defzone)) != 0) 443148385Sume goto bad; 444148385Sume } 445148385Sume 446148385Sume /* 447180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 448180305Srwatson * statistics. 44953541Sshin */ 45053541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 45153541Sshin struct icmp6_hdr *icmp6; 45253541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 45353541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 45453541Sshin error = ENOBUFS; 45553541Sshin goto bad; 45653541Sshin } 45753541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 45853541Sshin type = icmp6->icmp6_type; 45953541Sshin code = icmp6->icmp6_code; 46053541Sshin } 46153541Sshin 462243882Sglebius M_PREPEND(m, sizeof(*ip6), M_NOWAIT); 463133592Srwatson if (m == NULL) { 464133592Srwatson error = ENOBUFS; 465133592Srwatson goto bad; 466133592Srwatson } 46753541Sshin ip6 = mtod(m, struct ip6_hdr *); 46853541Sshin 46953541Sshin /* 47053541Sshin * Source address selection. 47153541Sshin */ 472194777Sbz error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 473194777Sbz &oifp, &in6a); 474194777Sbz if (error) 475121472Sume goto bad; 476207277Sbz error = prison_check_ip6(in6p->inp_cred, &in6a); 477188144Sjamie if (error != 0) 478188144Sjamie goto bad; 479194777Sbz ip6->ip6_src = in6a; 480148385Sume 481148385Sume if (oifp && scope_ambiguous) { 482148385Sume /* 483148385Sume * Application should provide a proper zone ID or the use of 484148385Sume * default zone IDs should be enabled. Unfortunately, some 485148385Sume * applications do not behave as it should, so we need a 486148385Sume * workaround. Even if an appropriate ID is not determined 487148385Sume * (when it's required), if we can determine the outgoing 488148385Sume * interface. determine the zone ID based on the interface. 489148385Sume */ 490148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 491148385Sume if (error != 0) 492148385Sume goto bad; 493148385Sume } 494148385Sume ip6->ip6_dst = dstsock->sin6_addr; 495148385Sume 496180305Srwatson /* 497180305Srwatson * Fill in the rest of the IPv6 header fields. 498180305Srwatson */ 49955009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 500186141Sbz (in6p->inp_flow & IPV6_FLOWINFO_MASK); 50155009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 502180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 503180305Srwatson 504180305Srwatson /* 505180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 506180305Srwatson */ 507186141Sbz ip6->ip6_nxt = in6p->inp_ip_p; 50853541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 50953541Sshin 51053541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 51153541Sshin in6p->in6p_cksum != -1) { 51253541Sshin struct mbuf *n; 51353541Sshin int off; 51453541Sshin u_int16_t *p; 51553541Sshin 516180305Srwatson /* Compute checksum. */ 51753541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 51853541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 51953541Sshin else 52053541Sshin off = in6p->in6p_cksum; 52153541Sshin if (plen < off + 1) { 52253541Sshin error = EINVAL; 52353541Sshin goto bad; 52453541Sshin } 52553541Sshin off += sizeof(struct ip6_hdr); 52653541Sshin 52753541Sshin n = m; 52853541Sshin while (n && n->m_len <= off) { 52953541Sshin off -= n->m_len; 53053541Sshin n = n->m_next; 53153541Sshin } 53253541Sshin if (!n) 53353541Sshin goto bad; 53453541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 53553541Sshin *p = 0; 53653541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 53753541Sshin } 53853541Sshin 539211501Sanchie /* 540211501Sanchie * Send RA/RS messages to user land for protection, before sending 541211501Sanchie * them to rtadvd/rtsol. 542211501Sanchie */ 543211501Sanchie if ((send_sendso_input_hook != NULL) && 544211501Sanchie so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 545211501Sanchie switch (type) { 546211501Sanchie case ND_ROUTER_ADVERT: 547211501Sanchie case ND_ROUTER_SOLICIT: 548211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 549211501Sanchie sizeof(unsigned short), M_NOWAIT); 550211501Sanchie if (mtag == NULL) 551211501Sanchie goto bad; 552211501Sanchie m_tag_prepend(m, mtag); 553211501Sanchie } 554211501Sanchie } 555211501Sanchie 556148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 55753541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 55853541Sshin if (oifp) 55953541Sshin icmp6_ifoutstat_inc(oifp, type, code); 560190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 56178064Sume } else 562181803Sbz V_rip6stat.rip6s_opackets++; 56353541Sshin 56453541Sshin goto freectl; 56553541Sshin 56653541Sshin bad: 56753541Sshin if (m) 56853541Sshin m_freem(m); 56953541Sshin 57053541Sshin freectl: 571186170Skmacy if (control != NULL) { 572148247Sume ip6_clearpktopts(&opt, -1); 57353541Sshin m_freem(control); 57478064Sume } 575178285Srwatson INP_WUNLOCK(in6p); 576120856Sume return (error); 57753541Sshin} 57853541Sshin 57953541Sshin/* 58053541Sshin * Raw IPv6 socket option processing. 58153541Sshin */ 58253541Sshinint 583171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 58453541Sshin{ 585231852Sbz struct inpcb *inp; 58653541Sshin int error; 58753541Sshin 58853541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 58953541Sshin /* 59053541Sshin * XXX: is it better to call icmp6_ctloutput() directly 59153541Sshin * from protosw? 59253541Sshin */ 593120856Sume return (icmp6_ctloutput(so, sopt)); 594231852Sbz else if (sopt->sopt_level != IPPROTO_IPV6) { 595231852Sbz if (sopt->sopt_level == SOL_SOCKET && 596231852Sbz sopt->sopt_name == SO_SETFIB) { 597231852Sbz inp = sotoinpcb(so); 598231852Sbz INP_WLOCK(inp); 599231852Sbz inp->inp_inc.inc_fibnum = so->so_fibnum; 600231852Sbz INP_WUNLOCK(inp); 601231852Sbz return (0); 602231852Sbz } 60353541Sshin return (EINVAL); 604231852Sbz } 60553541Sshin 60653541Sshin error = 0; 60753541Sshin 60853541Sshin switch (sopt->sopt_dir) { 60953541Sshin case SOPT_GET: 61053541Sshin switch (sopt->sopt_name) { 61156723Sshin case MRT6_INIT: 61256723Sshin case MRT6_DONE: 61356723Sshin case MRT6_ADD_MIF: 61456723Sshin case MRT6_DEL_MIF: 61556723Sshin case MRT6_ADD_MFC: 61656723Sshin case MRT6_DEL_MFC: 61756723Sshin case MRT6_PIM: 618166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 619166938Sbms EOPNOTSUPP; 62056723Sshin break; 621121578Sume case IPV6_CHECKSUM: 622121578Sume error = ip6_raw_ctloutput(so, sopt); 623121578Sume break; 62453541Sshin default: 62553541Sshin error = ip6_ctloutput(so, sopt); 62653541Sshin break; 62753541Sshin } 62853541Sshin break; 62953541Sshin 63053541Sshin case SOPT_SET: 63153541Sshin switch (sopt->sopt_name) { 63256723Sshin case MRT6_INIT: 63356723Sshin case MRT6_DONE: 63456723Sshin case MRT6_ADD_MIF: 63556723Sshin case MRT6_DEL_MIF: 63656723Sshin case MRT6_ADD_MFC: 63756723Sshin case MRT6_DEL_MFC: 63856723Sshin case MRT6_PIM: 639166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 640166938Sbms EOPNOTSUPP; 64156723Sshin break; 642121578Sume case IPV6_CHECKSUM: 643121578Sume error = ip6_raw_ctloutput(so, sopt); 644121578Sume break; 64553541Sshin default: 64653541Sshin error = ip6_ctloutput(so, sopt); 64753541Sshin break; 64853541Sshin } 64953541Sshin break; 65053541Sshin } 65153541Sshin 65253541Sshin return (error); 65353541Sshin} 65453541Sshin 65553541Sshinstatic int 65683366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 65753541Sshin{ 65853541Sshin struct inpcb *inp; 659144261Ssam struct icmp6_filter *filter; 660157676Srwatson int error; 66153541Sshin 66253541Sshin inp = sotoinpcb(so); 663157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 664180305Srwatson 665175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 666175630Sbz if (error) 667180305Srwatson return (error); 66855009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 669157374Srwatson if (error) 670180305Srwatson return (error); 671184214Sdes filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 672157374Srwatson if (filter == NULL) 673180305Srwatson return (ENOMEM); 674181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 675181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 676132714Srwatson if (error) { 677181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 678184205Sdes free(filter, M_PCB); 679180305Srwatson return (error); 680132714Srwatson } 68153541Sshin inp = (struct inpcb *)so->so_pcb; 682181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 68353541Sshin inp->inp_vflag |= INP_IPV6; 684186141Sbz inp->inp_ip_p = (long)proto; 68553541Sshin inp->in6p_hops = -1; /* use kernel default */ 68653541Sshin inp->in6p_cksum = -1; 687144261Ssam inp->in6p_icmp6filt = filter; 68853541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 689178285Srwatson INP_WUNLOCK(inp); 690180305Srwatson return (0); 69153541Sshin} 69253541Sshin 693157370Srwatsonstatic void 69453541Sshinrip6_detach(struct socket *so) 69553541Sshin{ 69653541Sshin struct inpcb *inp; 69753541Sshin 69853541Sshin inp = sotoinpcb(so); 699157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 700160549Srwatson 701191672Sbms if (so == V_ip6_mrouter && ip6_mrouter_done) 702166938Sbms ip6_mrouter_done(); 70353541Sshin /* xxx: RSVP */ 704181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 705178285Srwatson INP_WLOCK(inp); 706184205Sdes free(inp->in6p_icmp6filt, M_PCB); 707185344Sbz in_pcbdetach(inp); 708185370Sbz in_pcbfree(inp); 709181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 71053541Sshin} 71153541Sshin 712160549Srwatson/* XXXRW: This can't ever be called. */ 713157366Srwatsonstatic void 71453541Sshinrip6_abort(struct socket *so) 71553541Sshin{ 716160549Srwatson struct inpcb *inp; 717160549Srwatson 718160549Srwatson inp = sotoinpcb(so); 719160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 720160549Srwatson 72153541Sshin soisdisconnected(so); 72253541Sshin} 72353541Sshin 724160549Srwatsonstatic void 725160549Srwatsonrip6_close(struct socket *so) 726160549Srwatson{ 727160549Srwatson struct inpcb *inp; 728160549Srwatson 729160549Srwatson inp = sotoinpcb(so); 730160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 731160549Srwatson 732160549Srwatson soisdisconnected(so); 733160549Srwatson} 734160549Srwatson 73553541Sshinstatic int 73653541Sshinrip6_disconnect(struct socket *so) 73753541Sshin{ 738180305Srwatson struct inpcb *inp; 73953541Sshin 740180305Srwatson inp = sotoinpcb(so); 741180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 742180305Srwatson 74397658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 744180305Srwatson return (ENOTCONN); 74553541Sshin inp->in6p_faddr = in6addr_any; 746157366Srwatson rip6_abort(so); 747157374Srwatson return (0); 74853541Sshin} 74953541Sshin 75053541Sshinstatic int 75183366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 75253541Sshin{ 753180305Srwatson struct inpcb *inp; 75453541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 755194760Srwatson struct ifaddr *ifa = NULL; 756148385Sume int error = 0; 75753541Sshin 758180305Srwatson inp = sotoinpcb(so); 759157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 760180305Srwatson 76153541Sshin if (nam->sa_len != sizeof(*addr)) 762180305Srwatson return (EINVAL); 763188144Sjamie if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 764188144Sjamie return (error); 765181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 766180305Srwatson return (EADDRNOTAVAIL); 767181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 768180305Srwatson return (error); 769148385Sume 77053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 771194760Srwatson (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) 772180305Srwatson return (EADDRNOTAVAIL); 773194760Srwatson if (ifa != NULL && 774194760Srwatson ((struct in6_ifaddr *)ifa)->ia6_flags & 77553541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 77653541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 777194760Srwatson ifa_free(ifa); 778120856Sume return (EADDRNOTAVAIL); 77953541Sshin } 780194760Srwatson if (ifa != NULL) 781194760Srwatson ifa_free(ifa); 782181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 783178285Srwatson INP_WLOCK(inp); 78453541Sshin inp->in6p_laddr = addr->sin6_addr; 785178285Srwatson INP_WUNLOCK(inp); 786181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 787180305Srwatson return (0); 78853541Sshin} 78953541Sshin 79053541Sshinstatic int 79183366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 79253541Sshin{ 793180305Srwatson struct inpcb *inp; 79453541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 795194777Sbz struct in6_addr in6a; 796148385Sume struct ifnet *ifp = NULL; 797148385Sume int error = 0, scope_ambiguous = 0; 79853541Sshin 799180305Srwatson inp = sotoinpcb(so); 800157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 801180305Srwatson 80253541Sshin if (nam->sa_len != sizeof(*addr)) 803180305Srwatson return (EINVAL); 804181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 805180305Srwatson return (EADDRNOTAVAIL); 80653541Sshin if (addr->sin6_family != AF_INET6) 807180305Srwatson return (EAFNOSUPPORT); 808148385Sume 809148385Sume /* 810180305Srwatson * Application should provide a proper zone ID or the use of default 811180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 812180305Srwatson * not behave as it should, so we need a workaround. Even if an 813180305Srwatson * appropriate ID is not determined, we'll see if we can determine 814180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 815180305Srwatson * the interface below. 816148385Sume */ 817181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 818148385Sume scope_ambiguous = 1; 819181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 820180305Srwatson return (error); 821148385Sume 822181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 823178285Srwatson INP_WLOCK(inp); 82453541Sshin /* Source address selection. XXX: need pcblookup? */ 825194777Sbz error = in6_selectsrc(addr, inp->in6p_outputopts, 826194777Sbz inp, NULL, so->so_cred, &ifp, &in6a); 827194777Sbz if (error) { 828178285Srwatson INP_WUNLOCK(inp); 829181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 830194777Sbz return (error); 831132714Srwatson } 832148385Sume 833148385Sume /* XXX: see above */ 834148385Sume if (ifp && scope_ambiguous && 835148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 836178285Srwatson INP_WUNLOCK(inp); 837181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 838180305Srwatson return (error); 839148385Sume } 840148385Sume inp->in6p_faddr = addr->sin6_addr; 841194777Sbz inp->in6p_laddr = in6a; 84253541Sshin soisconnected(so); 843178285Srwatson INP_WUNLOCK(inp); 844181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 845180305Srwatson return (0); 84653541Sshin} 84753541Sshin 84853541Sshinstatic int 84953541Sshinrip6_shutdown(struct socket *so) 85053541Sshin{ 851132714Srwatson struct inpcb *inp; 852132714Srwatson 853132714Srwatson inp = sotoinpcb(so); 854157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 855180305Srwatson 856178285Srwatson INP_WLOCK(inp); 85753541Sshin socantsendmore(so); 858178285Srwatson INP_WUNLOCK(inp); 859180305Srwatson return (0); 86053541Sshin} 86153541Sshin 86253541Sshinstatic int 86353541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 864171260Sdelphij struct mbuf *control, struct thread *td) 86553541Sshin{ 866180305Srwatson struct inpcb *inp; 86753541Sshin struct sockaddr_in6 tmp; 86853541Sshin struct sockaddr_in6 *dst; 869132714Srwatson int ret; 87053541Sshin 871180305Srwatson inp = sotoinpcb(so); 872157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 873180305Srwatson 874180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 875132714Srwatson /* Unlocked read. */ 87653541Sshin if (so->so_state & SS_ISCONNECTED) { 87753541Sshin if (nam) { 87853541Sshin m_freem(m); 879180305Srwatson return (EISCONN); 88053541Sshin } 88153541Sshin /* XXX */ 88253541Sshin bzero(&tmp, sizeof(tmp)); 88353541Sshin tmp.sin6_family = AF_INET6; 88453541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 885180990Srwatson INP_RLOCK(inp); 88653541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 887180990Srwatson sizeof(struct in6_addr)); 888180990Srwatson INP_RUNLOCK(inp); 88953541Sshin dst = &tmp; 89053541Sshin } else { 89153541Sshin if (nam == NULL) { 89253541Sshin m_freem(m); 893180305Srwatson return (ENOTCONN); 89453541Sshin } 895148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 896148385Sume m_freem(m); 897180305Srwatson return (EINVAL); 898148385Sume } 89962587Sitojun tmp = *(struct sockaddr_in6 *)nam; 90062587Sitojun dst = &tmp; 901148385Sume 902148385Sume if (dst->sin6_family == AF_UNSPEC) { 903148385Sume /* 904148385Sume * XXX: we allow this case for backward 905148385Sume * compatibility to buggy applications that 906148385Sume * rely on old (and wrong) kernel behavior. 907148385Sume */ 908148385Sume log(LOG_INFO, "rip6 SEND: address family is " 909148385Sume "unspec. Assume AF_INET6\n"); 910148385Sume dst->sin6_family = AF_INET6; 911148385Sume } else if (dst->sin6_family != AF_INET6) { 912148385Sume m_freem(m); 913148385Sume return(EAFNOSUPPORT); 914148385Sume } 91553541Sshin } 916132714Srwatson ret = rip6_output(m, so, dst, control); 917132714Srwatson return (ret); 91853541Sshin} 91953541Sshin 92053541Sshinstruct pr_usrreqs rip6_usrreqs = { 921137386Sphk .pru_abort = rip6_abort, 922137386Sphk .pru_attach = rip6_attach, 923137386Sphk .pru_bind = rip6_bind, 924137386Sphk .pru_connect = rip6_connect, 925137386Sphk .pru_control = in6_control, 926137386Sphk .pru_detach = rip6_detach, 927137386Sphk .pru_disconnect = rip6_disconnect, 928169462Srwatson .pru_peeraddr = in6_getpeeraddr, 929137386Sphk .pru_send = rip6_send, 930137386Sphk .pru_shutdown = rip6_shutdown, 931169462Srwatson .pru_sockaddr = in6_getsockaddr, 932160549Srwatson .pru_close = rip6_close, 93353541Sshin}; 934