raw_ip6.c revision 194581
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 194581 2009-06-21 10:29:31Z rdivacky $"); 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> 82181803Sbz#include <sys/vimage.h> 8353541Sshin 8453541Sshin#include <net/if.h> 8595759Stanimura#include <net/if_types.h> 8653541Sshin#include <net/route.h> 87185571Sbz#include <net/vnet.h> 8853541Sshin 8953541Sshin#include <netinet/in.h> 9053541Sshin#include <netinet/in_var.h> 9153541Sshin#include <netinet/in_systm.h> 92185571Sbz#include <netinet/in_pcb.h> 93185571Sbz#include <netinet/vinet.h> 94185571Sbz 9595759Stanimura#include <netinet/icmp6.h> 9662587Sitojun#include <netinet/ip6.h> 9795759Stanimura#include <netinet6/ip6protosw.h> 9856723Sshin#include <netinet6/ip6_mroute.h> 9953541Sshin#include <netinet6/in6_pcb.h> 10095759Stanimura#include <netinet6/ip6_var.h> 10153541Sshin#include <netinet6/nd6.h> 10295759Stanimura#include <netinet6/raw_ip6.h> 10362587Sitojun#include <netinet6/scope6_var.h> 104185571Sbz#include <netinet6/vinet6.h> 10553541Sshin 106171167Sgnn#ifdef IPSEC 107105199Ssam#include <netipsec/ipsec.h> 108105199Ssam#include <netipsec/ipsec6.h> 109171167Sgnn#endif /* IPSEC */ 110105199Ssam 11153541Sshin#include <machine/stdarg.h> 11253541Sshin 11353541Sshin#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11453541Sshin#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11553541Sshin 11653541Sshin/* 11753541Sshin * Raw interface to IP6 protocol. 11853541Sshin */ 11953541Sshin 120185348Szec#ifdef VIMAGE_GLOBALS 12153541Sshinextern struct inpcbhead ripcb; 12253541Sshinextern struct inpcbinfo ripcbinfo; 123185348Szecstruct rip6stat rip6stat; 124185348Szec#endif 125185348Szec 12653541Sshinextern u_long rip_sendspace; 12753541Sshinextern u_long rip_recvspace; 12853541Sshin 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 */ 137191672Sbms#ifdef VIMAGE_GLOBALS 138191672Sbmsstruct socket *ip6_mrouter; 139191672Sbms#endif 140191672Sbms 141191672Sbms/* 142191672Sbms * The various mrouter functions. 143191672Sbms */ 144166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *); 145166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *); 146166938Sbmsint (*ip6_mrouter_done)(void); 147166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 148194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t); 149166938Sbms 150166938Sbms/* 151180305Srwatson * Setup generic address and protocol structures for raw_input routine, then 152180305Srwatson * pass them along with mbuf chain. 15353541Sshin */ 15453541Sshinint 155171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto) 15653541Sshin{ 157183550Szec INIT_VNET_INET(curvnet); 158183550Szec INIT_VNET_INET6(curvnet); 159183550Szec#ifdef IPSEC 160183550Szec INIT_VNET_IPSEC(curvnet); 161183550Szec#endif 162191672Sbms struct ifnet *ifp; 16353541Sshin struct mbuf *m = *mp; 16453541Sshin register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 16553541Sshin register struct inpcb *in6p; 16653541Sshin struct inpcb *last = 0; 16778064Sume struct mbuf *opts = NULL; 168121901Sume struct sockaddr_in6 fromsa; 16953541Sshin 170181803Sbz V_rip6stat.rip6s_ipackets++; 17178064Sume 17283934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 173180305Srwatson /* XXX Send icmp6 host/port unreach? */ 17478064Sume m_freem(m); 175180305Srwatson return (IPPROTO_DONE); 17653541Sshin } 17778064Sume 178121901Sume init_sin6(&fromsa, m); /* general init */ 17953541Sshin 180191672Sbms ifp = m->m_pkthdr.rcvif; 181191672Sbms 182181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 183181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 184185435Sbz /* XXX inp locking */ 185186141Sbz if ((in6p->inp_vflag & INP_IPV6) == 0) 18653541Sshin continue; 187186141Sbz if (in6p->inp_ip_p && 188186141Sbz in6p->inp_ip_p != proto) 189180850Smav continue; 19053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 19153541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 192180850Smav continue; 19353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 19453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 195180850Smav continue; 196191672Sbms if (jailed(in6p->inp_cred)) { 197191672Sbms /* 198191672Sbms * Allow raw socket in jail to receive multicast; 199191672Sbms * assume process had PRIV_NETINET_RAW at attach, 200191672Sbms * and fall through into normal filter path if so. 201191672Sbms */ 202191672Sbms if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 203191672Sbms prison_check_ip6(in6p->inp_cred, 204191672Sbms &ip6->ip6_dst) != 0) 205191672Sbms continue; 206191672Sbms } 20778064Sume if (in6p->in6p_cksum != -1) { 208181803Sbz V_rip6stat.rip6s_isum++; 209151459Ssuz if (in6_cksum(m, proto, *offp, 21078064Sume m->m_pkthdr.len - *offp)) { 211180932Smav INP_RUNLOCK(in6p); 212181803Sbz V_rip6stat.rip6s_badsum++; 213180850Smav continue; 21478064Sume } 21553541Sshin } 216191672Sbms INP_RLOCK(in6p); 217191672Sbms /* 218191672Sbms * If this raw socket has multicast state, and we 219191672Sbms * have received a multicast, check if this socket 220191672Sbms * should receive it, as multicast filtering is now 221191672Sbms * the responsibility of the transport layer. 222191672Sbms */ 223191672Sbms if (in6p->in6p_moptions && 224191672Sbms IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 225191672Sbms struct sockaddr_in6 mcaddr; 226191672Sbms int blocked; 227191672Sbms 228191672Sbms bzero(&mcaddr, sizeof(struct sockaddr_in6)); 229191672Sbms mcaddr.sin6_len = sizeof(struct sockaddr_in6); 230191672Sbms mcaddr.sin6_family = AF_INET6; 231191672Sbms mcaddr.sin6_addr = ip6->ip6_dst; 232191672Sbms 233191672Sbms blocked = im6o_mc_filter(in6p->in6p_moptions, ifp, 234191672Sbms (struct sockaddr *)&mcaddr, 235191672Sbms (struct sockaddr *)&fromsa); 236191672Sbms if (blocked != MCAST_PASS) { 237191672Sbms IP6STAT_INC(ip6s_notmember); 238191672Sbms continue; 239191672Sbms } 240191672Sbms } 241186163Skmacy if (last != NULL) { 24253541Sshin struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 24378064Sume 244171167Sgnn#ifdef IPSEC 24578064Sume /* 24678064Sume * Check AH/ESP integrity. 24778064Sume */ 248125396Sume if (n && ipsec6_in_reject(n, last)) { 24978064Sume m_freem(n); 250181803Sbz V_ipsec6stat.in_polvio++; 251180305Srwatson /* Do not inject data into pcb. */ 252105199Ssam } else 253171167Sgnn#endif /* IPSEC */ 25453541Sshin if (n) { 255186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 256186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 257121674Sume ip6_savecontrol(last, n, &opts); 25853541Sshin /* strip intermediate headers */ 25953541Sshin m_adj(n, *offp); 260186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 261121901Sume (struct sockaddr *)&fromsa, 26253541Sshin n, opts) == 0) { 26353541Sshin m_freem(n); 26453541Sshin if (opts) 26553541Sshin m_freem(opts); 266181803Sbz V_rip6stat.rip6s_fullsock++; 26797658Stanimura } else 268186141Sbz sorwakeup(last->inp_socket); 26953541Sshin opts = NULL; 27053541Sshin } 271178377Srwatson INP_RUNLOCK(last); 27253541Sshin } 27353541Sshin last = in6p; 27453541Sshin } 275181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 276171167Sgnn#ifdef IPSEC 27778064Sume /* 27878064Sume * Check AH/ESP integrity. 27978064Sume */ 280186170Skmacy if ((last != NULL) && ipsec6_in_reject(m, last)) { 28178064Sume m_freem(m); 282181803Sbz V_ipsec6stat.in_polvio++; 283181803Sbz V_ip6stat.ip6s_delivered--; 284180305Srwatson /* Do not inject data into pcb. */ 285178377Srwatson INP_RUNLOCK(last); 286105199Ssam } else 287171167Sgnn#endif /* IPSEC */ 288186163Skmacy if (last != NULL) { 289186223Sbz if (last->inp_flags & INP_CONTROLOPTS || 290186141Sbz last->inp_socket->so_options & SO_TIMESTAMP) 291121674Sume ip6_savecontrol(last, m, &opts); 292180305Srwatson /* Strip intermediate headers. */ 29353541Sshin m_adj(m, *offp); 294186141Sbz if (sbappendaddr(&last->inp_socket->so_rcv, 295180305Srwatson (struct sockaddr *)&fromsa, m, opts) == 0) { 29653541Sshin m_freem(m); 29753541Sshin if (opts) 29853541Sshin m_freem(opts); 299181803Sbz V_rip6stat.rip6s_fullsock++; 30097658Stanimura } else 301186141Sbz sorwakeup(last->inp_socket); 302178377Srwatson INP_RUNLOCK(last); 30353541Sshin } else { 304181803Sbz V_rip6stat.rip6s_nosock++; 30578064Sume if (m->m_flags & M_MCAST) 306181803Sbz V_rip6stat.rip6s_nosockmcast++; 30753541Sshin if (proto == IPPROTO_NONE) 30853541Sshin m_freem(m); 30953541Sshin else { 31053541Sshin char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 31153541Sshin icmp6_error(m, ICMP6_PARAM_PROB, 312180305Srwatson ICMP6_PARAMPROB_NEXTHEADER, 313180305Srwatson prvnxtp - mtod(m, char *)); 31453541Sshin } 315181803Sbz V_ip6stat.ip6s_delivered--; 31653541Sshin } 317180305Srwatson return (IPPROTO_DONE); 31853541Sshin} 31953541Sshin 32062587Sitojunvoid 321171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 32262587Sitojun{ 323183550Szec INIT_VNET_INET(curvnet); 32462587Sitojun struct ip6_hdr *ip6; 32562587Sitojun struct mbuf *m; 32662587Sitojun int off = 0; 32778064Sume struct ip6ctlparam *ip6cp = NULL; 32878064Sume const struct sockaddr_in6 *sa6_src = NULL; 329125776Sume void *cmdarg; 330175162Sobrien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 33162587Sitojun 33262587Sitojun if (sa->sa_family != AF_INET6 || 33362587Sitojun sa->sa_len != sizeof(struct sockaddr_in6)) 33462587Sitojun return; 33562587Sitojun 33662587Sitojun if ((unsigned)cmd >= PRC_NCMDS) 33762587Sitojun return; 33862587Sitojun if (PRC_IS_REDIRECT(cmd)) 33962587Sitojun notify = in6_rtchange, d = NULL; 34062587Sitojun else if (cmd == PRC_HOSTDEAD) 34162587Sitojun d = NULL; 34262587Sitojun else if (inet6ctlerrmap[cmd] == 0) 34362587Sitojun return; 34462587Sitojun 345180305Srwatson /* 346180305Srwatson * If the parameter is from icmp6, decode it. 347180305Srwatson */ 34862587Sitojun if (d != NULL) { 34978064Sume ip6cp = (struct ip6ctlparam *)d; 35062587Sitojun m = ip6cp->ip6c_m; 35162587Sitojun ip6 = ip6cp->ip6c_ip6; 35262587Sitojun off = ip6cp->ip6c_off; 353125776Sume cmdarg = ip6cp->ip6c_cmdarg; 35478064Sume sa6_src = ip6cp->ip6c_src; 35562587Sitojun } else { 35662587Sitojun m = NULL; 35762587Sitojun ip6 = NULL; 358125776Sume cmdarg = NULL; 35978064Sume sa6_src = &sa6_any; 36062587Sitojun } 36162587Sitojun 362181803Sbz (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 363180305Srwatson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 36462587Sitojun} 36562587Sitojun 36653541Sshin/* 367180305Srwatson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 368180305Srwatson * may have setup with control call. 36953541Sshin */ 37053541Sshinint 37153541Sshin#if __STDC__ 37253541Sshinrip6_output(struct mbuf *m, ...) 37353541Sshin#else 37453541Sshinrip6_output(m, va_alist) 37553541Sshin struct mbuf *m; 37653541Sshin va_dcl 37753541Sshin#endif 37853541Sshin{ 379183550Szec INIT_VNET_INET6(curvnet); 380120941Sume struct mbuf *control; 38153541Sshin struct socket *so; 38253541Sshin struct sockaddr_in6 *dstsock; 38353541Sshin struct in6_addr *dst; 38453541Sshin struct ip6_hdr *ip6; 38553541Sshin struct inpcb *in6p; 38653541Sshin u_int plen = m->m_pkthdr.len; 38753541Sshin int error = 0; 388148247Sume struct ip6_pktopts opt, *optp; 38953541Sshin struct ifnet *oifp = NULL; 39053541Sshin int type = 0, code = 0; /* for ICMPv6 output statistics only */ 391148385Sume int scope_ambiguous = 0; 392121472Sume struct in6_addr *in6a; 39353541Sshin va_list ap; 39453541Sshin 39553541Sshin va_start(ap, m); 39653541Sshin so = va_arg(ap, struct socket *); 39753541Sshin dstsock = va_arg(ap, struct sockaddr_in6 *); 39853541Sshin control = va_arg(ap, struct mbuf *); 39953541Sshin va_end(ap); 40053541Sshin 401186141Sbz in6p = sotoinpcb(so); 402178285Srwatson INP_WLOCK(in6p); 40353541Sshin 40453541Sshin dst = &dstsock->sin6_addr; 405186170Skmacy if (control != NULL) { 406148242Sume if ((error = ip6_setpktopts(control, &opt, 407175630Sbz in6p->in6p_outputopts, so->so_cred, 408175630Sbz so->so_proto->pr_protocol)) != 0) { 40953541Sshin goto bad; 410121472Sume } 411148247Sume optp = &opt; 412148247Sume } else 413148247Sume optp = in6p->in6p_outputopts; 41453541Sshin 41553541Sshin /* 416148385Sume * Check and convert scope zone ID into internal form. 417180305Srwatson * 418148385Sume * XXX: we may still need to determine the zone later. 419148385Sume */ 420148385Sume if (!(so->so_state & SS_ISCONNECTED)) { 421181803Sbz if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone) 422148385Sume scope_ambiguous = 1; 423181803Sbz if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0) 424148385Sume goto bad; 425148385Sume } 426148385Sume 427148385Sume /* 428180305Srwatson * For an ICMPv6 packet, we should know its type and code to update 429180305Srwatson * statistics. 43053541Sshin */ 43153541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 43253541Sshin struct icmp6_hdr *icmp6; 43353541Sshin if (m->m_len < sizeof(struct icmp6_hdr) && 43453541Sshin (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 43553541Sshin error = ENOBUFS; 43653541Sshin goto bad; 43753541Sshin } 43853541Sshin icmp6 = mtod(m, struct icmp6_hdr *); 43953541Sshin type = icmp6->icmp6_type; 44053541Sshin code = icmp6->icmp6_code; 44153541Sshin } 44253541Sshin 443133592Srwatson M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 444133592Srwatson if (m == NULL) { 445133592Srwatson error = ENOBUFS; 446133592Srwatson goto bad; 447133592Srwatson } 44853541Sshin ip6 = mtod(m, struct ip6_hdr *); 44953541Sshin 45053541Sshin /* 45153541Sshin * Source address selection. 45253541Sshin */ 453180371Sbz if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 454180371Sbz &oifp, &error)) == NULL) { 455121472Sume if (error == 0) 456121472Sume error = EADDRNOTAVAIL; 457121472Sume goto bad; 45853541Sshin } 459188144Sjamie error = prison_get_ip6(in6p->inp_cred, in6a); 460188144Sjamie if (error != 0) 461188144Sjamie goto bad; 462121472Sume ip6->ip6_src = *in6a; 463148385Sume 464148385Sume if (oifp && scope_ambiguous) { 465148385Sume /* 466148385Sume * Application should provide a proper zone ID or the use of 467148385Sume * default zone IDs should be enabled. Unfortunately, some 468148385Sume * applications do not behave as it should, so we need a 469148385Sume * workaround. Even if an appropriate ID is not determined 470148385Sume * (when it's required), if we can determine the outgoing 471148385Sume * interface. determine the zone ID based on the interface. 472148385Sume */ 473148385Sume error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 474148385Sume if (error != 0) 475148385Sume goto bad; 476148385Sume } 477148385Sume ip6->ip6_dst = dstsock->sin6_addr; 478148385Sume 479180305Srwatson /* 480180305Srwatson * Fill in the rest of the IPv6 header fields. 481180305Srwatson */ 48255009Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 483186141Sbz (in6p->inp_flow & IPV6_FLOWINFO_MASK); 48455009Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 485180305Srwatson (IPV6_VERSION & IPV6_VERSION_MASK); 486180305Srwatson 487180305Srwatson /* 488180305Srwatson * ip6_plen will be filled in ip6_output, so not fill it here. 489180305Srwatson */ 490186141Sbz ip6->ip6_nxt = in6p->inp_ip_p; 49153541Sshin ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 49253541Sshin 49353541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 49453541Sshin in6p->in6p_cksum != -1) { 49553541Sshin struct mbuf *n; 49653541Sshin int off; 49753541Sshin u_int16_t *p; 49853541Sshin 499180305Srwatson /* Compute checksum. */ 50053541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 50153541Sshin off = offsetof(struct icmp6_hdr, icmp6_cksum); 50253541Sshin else 50353541Sshin off = in6p->in6p_cksum; 50453541Sshin if (plen < off + 1) { 50553541Sshin error = EINVAL; 50653541Sshin goto bad; 50753541Sshin } 50853541Sshin off += sizeof(struct ip6_hdr); 50953541Sshin 51053541Sshin n = m; 51153541Sshin while (n && n->m_len <= off) { 51253541Sshin off -= n->m_len; 51353541Sshin n = n->m_next; 51453541Sshin } 51553541Sshin if (!n) 51653541Sshin goto bad; 51753541Sshin p = (u_int16_t *)(mtod(n, caddr_t) + off); 51853541Sshin *p = 0; 51953541Sshin *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 52053541Sshin } 52153541Sshin 522148247Sume error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 52353541Sshin if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 52453541Sshin if (oifp) 52553541Sshin icmp6_ifoutstat_inc(oifp, type, code); 526190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 52778064Sume } else 528181803Sbz V_rip6stat.rip6s_opackets++; 52953541Sshin 53053541Sshin goto freectl; 53153541Sshin 53253541Sshin bad: 53353541Sshin if (m) 53453541Sshin m_freem(m); 53553541Sshin 53653541Sshin freectl: 537186170Skmacy if (control != NULL) { 538148247Sume ip6_clearpktopts(&opt, -1); 53953541Sshin m_freem(control); 54078064Sume } 541178285Srwatson INP_WUNLOCK(in6p); 542120856Sume return (error); 54353541Sshin} 54453541Sshin 54553541Sshin/* 54653541Sshin * Raw IPv6 socket option processing. 54753541Sshin */ 54853541Sshinint 549171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt) 55053541Sshin{ 55153541Sshin int error; 55253541Sshin 55353541Sshin if (sopt->sopt_level == IPPROTO_ICMPV6) 55453541Sshin /* 55553541Sshin * XXX: is it better to call icmp6_ctloutput() directly 55653541Sshin * from protosw? 55753541Sshin */ 558120856Sume return (icmp6_ctloutput(so, sopt)); 55953541Sshin else if (sopt->sopt_level != IPPROTO_IPV6) 56053541Sshin return (EINVAL); 56153541Sshin 56253541Sshin error = 0; 56353541Sshin 56453541Sshin switch (sopt->sopt_dir) { 56553541Sshin case SOPT_GET: 56653541Sshin switch (sopt->sopt_name) { 56756723Sshin case MRT6_INIT: 56856723Sshin case MRT6_DONE: 56956723Sshin case MRT6_ADD_MIF: 57056723Sshin case MRT6_DEL_MIF: 57156723Sshin case MRT6_ADD_MFC: 57256723Sshin case MRT6_DEL_MFC: 57356723Sshin case MRT6_PIM: 574166938Sbms error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 575166938Sbms EOPNOTSUPP; 57656723Sshin break; 577121578Sume case IPV6_CHECKSUM: 578121578Sume error = ip6_raw_ctloutput(so, sopt); 579121578Sume break; 58053541Sshin default: 58153541Sshin error = ip6_ctloutput(so, sopt); 58253541Sshin break; 58353541Sshin } 58453541Sshin break; 58553541Sshin 58653541Sshin case SOPT_SET: 58753541Sshin switch (sopt->sopt_name) { 58856723Sshin case MRT6_INIT: 58956723Sshin case MRT6_DONE: 59056723Sshin case MRT6_ADD_MIF: 59156723Sshin case MRT6_DEL_MIF: 59256723Sshin case MRT6_ADD_MFC: 59356723Sshin case MRT6_DEL_MFC: 59456723Sshin case MRT6_PIM: 595166938Sbms error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 596166938Sbms EOPNOTSUPP; 59756723Sshin break; 598121578Sume case IPV6_CHECKSUM: 599121578Sume error = ip6_raw_ctloutput(so, sopt); 600121578Sume break; 60153541Sshin default: 60253541Sshin error = ip6_ctloutput(so, sopt); 60353541Sshin break; 60453541Sshin } 60553541Sshin break; 60653541Sshin } 60753541Sshin 60853541Sshin return (error); 60953541Sshin} 61053541Sshin 61153541Sshinstatic int 61283366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td) 61353541Sshin{ 614183550Szec INIT_VNET_INET(so->so_vnet); 61553541Sshin struct inpcb *inp; 616144261Ssam struct icmp6_filter *filter; 617157676Srwatson int error; 61853541Sshin 61953541Sshin inp = sotoinpcb(so); 620157374Srwatson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 621180305Srwatson 622175630Sbz error = priv_check(td, PRIV_NETINET_RAW); 623175630Sbz if (error) 624180305Srwatson return (error); 62555009Sshin error = soreserve(so, rip_sendspace, rip_recvspace); 626157374Srwatson if (error) 627180305Srwatson return (error); 628184214Sdes filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 629157374Srwatson if (filter == NULL) 630180305Srwatson return (ENOMEM); 631181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 632181803Sbz error = in_pcballoc(so, &V_ripcbinfo); 633132714Srwatson if (error) { 634181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 635184205Sdes free(filter, M_PCB); 636180305Srwatson return (error); 637132714Srwatson } 63853541Sshin inp = (struct inpcb *)so->so_pcb; 639181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 64053541Sshin inp->inp_vflag |= INP_IPV6; 641186141Sbz inp->inp_ip_p = (long)proto; 64253541Sshin inp->in6p_hops = -1; /* use kernel default */ 64353541Sshin inp->in6p_cksum = -1; 644144261Ssam inp->in6p_icmp6filt = filter; 64553541Sshin ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 646178285Srwatson INP_WUNLOCK(inp); 647180305Srwatson return (0); 64853541Sshin} 64953541Sshin 650157370Srwatsonstatic void 65153541Sshinrip6_detach(struct socket *so) 65253541Sshin{ 653191688Szec INIT_VNET_INET(so->so_vnet); 654191672Sbms INIT_VNET_INET6(so->so_vnet); 65553541Sshin struct inpcb *inp; 65653541Sshin 65753541Sshin inp = sotoinpcb(so); 658157374Srwatson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 659160549Srwatson 660191672Sbms if (so == V_ip6_mrouter && ip6_mrouter_done) 661166938Sbms ip6_mrouter_done(); 66253541Sshin /* xxx: RSVP */ 663181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 664178285Srwatson INP_WLOCK(inp); 665184205Sdes free(inp->in6p_icmp6filt, M_PCB); 666185344Sbz in_pcbdetach(inp); 667185370Sbz in_pcbfree(inp); 668181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 66953541Sshin} 67053541Sshin 671160549Srwatson/* XXXRW: This can't ever be called. */ 672157366Srwatsonstatic void 67353541Sshinrip6_abort(struct socket *so) 67453541Sshin{ 675160549Srwatson struct inpcb *inp; 676160549Srwatson 677160549Srwatson inp = sotoinpcb(so); 678160549Srwatson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 679160549Srwatson 68053541Sshin soisdisconnected(so); 68153541Sshin} 68253541Sshin 683160549Srwatsonstatic void 684160549Srwatsonrip6_close(struct socket *so) 685160549Srwatson{ 686160549Srwatson struct inpcb *inp; 687160549Srwatson 688160549Srwatson inp = sotoinpcb(so); 689160549Srwatson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 690160549Srwatson 691160549Srwatson soisdisconnected(so); 692160549Srwatson} 693160549Srwatson 69453541Sshinstatic int 69553541Sshinrip6_disconnect(struct socket *so) 69653541Sshin{ 697180305Srwatson struct inpcb *inp; 69853541Sshin 699180305Srwatson inp = sotoinpcb(so); 700180305Srwatson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 701180305Srwatson 70297658Stanimura if ((so->so_state & SS_ISCONNECTED) == 0) 703180305Srwatson return (ENOTCONN); 70453541Sshin inp->in6p_faddr = in6addr_any; 705157366Srwatson rip6_abort(so); 706157374Srwatson return (0); 70753541Sshin} 70853541Sshin 70953541Sshinstatic int 71083366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 71153541Sshin{ 712183550Szec INIT_VNET_NET(so->so_vnet); 713183550Szec INIT_VNET_INET(so->so_vnet); 714183550Szec INIT_VNET_INET6(so->so_vnet); 715180305Srwatson struct inpcb *inp; 71653541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 71753541Sshin struct ifaddr *ia = NULL; 718148385Sume int error = 0; 71953541Sshin 720180305Srwatson inp = sotoinpcb(so); 721157374Srwatson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 722180305Srwatson 72353541Sshin if (nam->sa_len != sizeof(*addr)) 724180305Srwatson return (EINVAL); 725188144Sjamie if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 726188144Sjamie return (error); 727181803Sbz if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 728180305Srwatson return (EADDRNOTAVAIL); 729181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 730180305Srwatson return (error); 731148385Sume 73253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 73353541Sshin (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 734180305Srwatson return (EADDRNOTAVAIL); 73553541Sshin if (ia && 73653541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 73753541Sshin (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 73853541Sshin IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 739120856Sume return (EADDRNOTAVAIL); 74053541Sshin } 741181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 742178285Srwatson INP_WLOCK(inp); 74353541Sshin inp->in6p_laddr = addr->sin6_addr; 744178285Srwatson INP_WUNLOCK(inp); 745181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 746180305Srwatson return (0); 74753541Sshin} 74853541Sshin 74953541Sshinstatic int 75083366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 75153541Sshin{ 752183550Szec INIT_VNET_NET(so->so_vnet); 753183550Szec INIT_VNET_INET(so->so_vnet); 754183550Szec INIT_VNET_INET6(so->so_vnet); 755180305Srwatson struct inpcb *inp; 75653541Sshin struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 75753541Sshin struct in6_addr *in6a = NULL; 758148385Sume struct ifnet *ifp = NULL; 759148385Sume int error = 0, scope_ambiguous = 0; 76053541Sshin 761180305Srwatson inp = sotoinpcb(so); 762157374Srwatson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 763180305Srwatson 76453541Sshin if (nam->sa_len != sizeof(*addr)) 765180305Srwatson return (EINVAL); 766181803Sbz if (TAILQ_EMPTY(&V_ifnet)) 767180305Srwatson return (EADDRNOTAVAIL); 76853541Sshin if (addr->sin6_family != AF_INET6) 769180305Srwatson return (EAFNOSUPPORT); 770148385Sume 771148385Sume /* 772180305Srwatson * Application should provide a proper zone ID or the use of default 773180305Srwatson * zone IDs should be enabled. Unfortunately, some applications do 774180305Srwatson * not behave as it should, so we need a workaround. Even if an 775180305Srwatson * appropriate ID is not determined, we'll see if we can determine 776180305Srwatson * the outgoing interface. If we can, determine the zone ID based on 777180305Srwatson * the interface below. 778148385Sume */ 779181803Sbz if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 780148385Sume scope_ambiguous = 1; 781181803Sbz if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 782180305Srwatson return (error); 783148385Sume 784181803Sbz INP_INFO_WLOCK(&V_ripcbinfo); 785178285Srwatson INP_WLOCK(inp); 78653541Sshin /* Source address selection. XXX: need pcblookup? */ 78753541Sshin in6a = in6_selectsrc(addr, inp->in6p_outputopts, 788180371Sbz inp, NULL, so->so_cred, 789180371Sbz &ifp, &error); 790132714Srwatson if (in6a == NULL) { 791178285Srwatson INP_WUNLOCK(inp); 792181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 79353541Sshin return (error ? error : EADDRNOTAVAIL); 794132714Srwatson } 795148385Sume 796148385Sume /* XXX: see above */ 797148385Sume if (ifp && scope_ambiguous && 798148385Sume (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 799178285Srwatson INP_WUNLOCK(inp); 800181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 801180305Srwatson return (error); 802148385Sume } 803148385Sume inp->in6p_faddr = addr->sin6_addr; 80453541Sshin inp->in6p_laddr = *in6a; 80553541Sshin soisconnected(so); 806178285Srwatson INP_WUNLOCK(inp); 807181803Sbz INP_INFO_WUNLOCK(&V_ripcbinfo); 808180305Srwatson return (0); 80953541Sshin} 81053541Sshin 81153541Sshinstatic int 81253541Sshinrip6_shutdown(struct socket *so) 81353541Sshin{ 814132714Srwatson struct inpcb *inp; 815132714Srwatson 816132714Srwatson inp = sotoinpcb(so); 817157374Srwatson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 818180305Srwatson 819178285Srwatson INP_WLOCK(inp); 82053541Sshin socantsendmore(so); 821178285Srwatson INP_WUNLOCK(inp); 822180305Srwatson return (0); 82353541Sshin} 82453541Sshin 82553541Sshinstatic int 82653541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 827171260Sdelphij struct mbuf *control, struct thread *td) 82853541Sshin{ 829180305Srwatson struct inpcb *inp; 83053541Sshin struct sockaddr_in6 tmp; 83153541Sshin struct sockaddr_in6 *dst; 832132714Srwatson int ret; 83353541Sshin 834180305Srwatson inp = sotoinpcb(so); 835157374Srwatson KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 836180305Srwatson 837180305Srwatson /* Always copy sockaddr to avoid overwrites. */ 838132714Srwatson /* Unlocked read. */ 83953541Sshin if (so->so_state & SS_ISCONNECTED) { 84053541Sshin if (nam) { 84153541Sshin m_freem(m); 842180305Srwatson return (EISCONN); 84353541Sshin } 84453541Sshin /* XXX */ 84553541Sshin bzero(&tmp, sizeof(tmp)); 84653541Sshin tmp.sin6_family = AF_INET6; 84753541Sshin tmp.sin6_len = sizeof(struct sockaddr_in6); 848180990Srwatson INP_RLOCK(inp); 84953541Sshin bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 850180990Srwatson sizeof(struct in6_addr)); 851180990Srwatson INP_RUNLOCK(inp); 85253541Sshin dst = &tmp; 85353541Sshin } else { 85453541Sshin if (nam == NULL) { 85553541Sshin m_freem(m); 856180305Srwatson return (ENOTCONN); 85753541Sshin } 858148385Sume if (nam->sa_len != sizeof(struct sockaddr_in6)) { 859148385Sume m_freem(m); 860180305Srwatson return (EINVAL); 861148385Sume } 86262587Sitojun tmp = *(struct sockaddr_in6 *)nam; 86362587Sitojun dst = &tmp; 864148385Sume 865148385Sume if (dst->sin6_family == AF_UNSPEC) { 866148385Sume /* 867148385Sume * XXX: we allow this case for backward 868148385Sume * compatibility to buggy applications that 869148385Sume * rely on old (and wrong) kernel behavior. 870148385Sume */ 871148385Sume log(LOG_INFO, "rip6 SEND: address family is " 872148385Sume "unspec. Assume AF_INET6\n"); 873148385Sume dst->sin6_family = AF_INET6; 874148385Sume } else if (dst->sin6_family != AF_INET6) { 875148385Sume m_freem(m); 876148385Sume return(EAFNOSUPPORT); 877148385Sume } 87853541Sshin } 879132714Srwatson ret = rip6_output(m, so, dst, control); 880132714Srwatson return (ret); 88153541Sshin} 88253541Sshin 88353541Sshinstruct pr_usrreqs rip6_usrreqs = { 884137386Sphk .pru_abort = rip6_abort, 885137386Sphk .pru_attach = rip6_attach, 886137386Sphk .pru_bind = rip6_bind, 887137386Sphk .pru_connect = rip6_connect, 888137386Sphk .pru_control = in6_control, 889137386Sphk .pru_detach = rip6_detach, 890137386Sphk .pru_disconnect = rip6_disconnect, 891169462Srwatson .pru_peeraddr = in6_getpeeraddr, 892137386Sphk .pru_send = rip6_send, 893137386Sphk .pru_shutdown = rip6_shutdown, 894169462Srwatson .pru_sockaddr = in6_getsockaddr, 895160549Srwatson .pru_close = rip6_close, 89653541Sshin}; 897