2000-07-04 16:35:15 +00:00
|
|
|
/* $FreeBSD$ */
|
2001-06-11 12:39:29 +00:00
|
|
|
/* $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ */
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)udp_var.h 8.1 (Berkeley) 6/10/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_ipsec.h"
|
|
|
|
#include "opt_inet.h"
|
2001-08-31 13:49:03 +00:00
|
|
|
#include "opt_inet6.h"
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <sys/malloc.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/udp_var.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#include <netinet6/in6_pcb.h>
|
|
|
|
#include <netinet6/udp6_var.h>
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
#include <netinet6/ip6protosw.h>
|
|
|
|
|
|
|
|
#ifdef IPSEC
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ipsec6.h>
|
|
|
|
#endif
|
2002-04-19 04:46:24 +00:00
|
|
|
#endif /* IPSEC */
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
#include <net/net_osdep.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UDP protocol inplementation.
|
|
|
|
* Per RFC 768, August, 1980.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define in6pcb inpcb
|
|
|
|
#define udp6stat udpstat
|
|
|
|
#define udp6s_opackets udps_opackets
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
udp6_output(in6p, m, addr6, control, td)
|
2001-06-11 12:39:29 +00:00
|
|
|
struct in6pcb *in6p;
|
|
|
|
struct mbuf *m;
|
2000-07-04 16:35:15 +00:00
|
|
|
struct mbuf *control;
|
|
|
|
struct sockaddr *addr6;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
2000-07-04 16:35:15 +00:00
|
|
|
{
|
2001-06-11 12:39:29 +00:00
|
|
|
u_int32_t ulen = m->m_pkthdr.len;
|
2000-07-04 16:35:15 +00:00
|
|
|
u_int32_t plen = sizeof(struct udphdr) + ulen;
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
struct udphdr *udp6;
|
2001-06-11 12:39:29 +00:00
|
|
|
struct in6_addr *laddr, *faddr;
|
2000-07-04 16:35:15 +00:00
|
|
|
u_short fport;
|
|
|
|
int error = 0;
|
|
|
|
struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
|
|
|
|
int priv;
|
2001-06-11 12:39:29 +00:00
|
|
|
int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
|
2000-07-04 16:35:15 +00:00
|
|
|
int flags;
|
|
|
|
struct sockaddr_in6 tmp;
|
|
|
|
|
|
|
|
priv = 0;
|
2002-04-01 21:31:13 +00:00
|
|
|
if (td && !suser(td))
|
2000-07-04 16:35:15 +00:00
|
|
|
priv = 1;
|
|
|
|
if (control) {
|
2003-10-24 18:26:30 +00:00
|
|
|
if ((error = ip6_setpktoptions(control, &opt, stickyopt, priv,
|
|
|
|
0, IPPROTO_UDP)) != 0)
|
2000-07-04 16:35:15 +00:00
|
|
|
goto release;
|
|
|
|
in6p->in6p_outputopts = &opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr6) {
|
|
|
|
/*
|
|
|
|
* IPv4 version of udp_output calls in_pcbconnect in this case,
|
|
|
|
* which needs splnet and affects performance.
|
|
|
|
* Since we saw no essential reason for calling in_pcbconnect,
|
|
|
|
* we get rid of such kind of logic, and call in6_selectsrc
|
|
|
|
* and in6_pcbsetport in order to fill in the local address
|
|
|
|
* and the local port.
|
|
|
|
*/
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
|
|
|
|
if (sin6->sin6_port == 0) {
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
|
2001-06-11 12:39:29 +00:00
|
|
|
/* how about ::ffff:0.0.0.0 case? */
|
2000-07-04 16:35:15 +00:00
|
|
|
error = EISCONN;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protect *sin6 from overwrites */
|
|
|
|
tmp = *sin6;
|
|
|
|
sin6 = &tmp;
|
|
|
|
|
|
|
|
faddr = &sin6->sin6_addr;
|
|
|
|
fport = sin6->sin6_port; /* allow 0 port */
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(faddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
|
|
|
|
/*
|
|
|
|
* I believe we should explicitly discard the
|
|
|
|
* packet when mapped addresses are disabled,
|
|
|
|
* rather than send the packet as an IPv6 one.
|
|
|
|
* If we chose the latter approach, the packet
|
|
|
|
* might be sent out on the wire based on the
|
|
|
|
* default route, the situation which we'd
|
|
|
|
* probably want to avoid.
|
|
|
|
* (20010421 jinmei@kame.net)
|
|
|
|
*/
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
} else
|
|
|
|
af = AF_INET;
|
|
|
|
}
|
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
/* KAME hack: embed scopeid */
|
|
|
|
if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
|
|
|
|
laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
|
2003-11-20 20:07:39 +00:00
|
|
|
in6p->in6p_moptions, NULL,
|
2000-07-04 16:35:15 +00:00
|
|
|
&in6p->in6p_laddr, &error);
|
|
|
|
} else
|
2001-06-11 12:39:29 +00:00
|
|
|
laddr = &in6p->in6p_laddr; /* XXX */
|
2000-07-04 16:35:15 +00:00
|
|
|
if (laddr == NULL) {
|
|
|
|
if (error == 0)
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
if (in6p->in6p_lport == 0 &&
|
2001-09-12 08:38:13 +00:00
|
|
|
(error = in6_pcbsetport(laddr, in6p, td)) != 0)
|
2000-07-04 16:35:15 +00:00
|
|
|
goto release;
|
|
|
|
} else {
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
|
|
|
|
error = ENOTCONN;
|
|
|
|
goto release;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
|
|
|
|
/*
|
|
|
|
* XXX: this case would happen when the
|
|
|
|
* application sets the V6ONLY flag after
|
|
|
|
* connecting the foreign address.
|
|
|
|
* Such applications should be fixed,
|
|
|
|
* so we bark here.
|
|
|
|
*/
|
|
|
|
log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
|
|
|
|
"option was set for a connected socket\n");
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
} else
|
|
|
|
af = AF_INET;
|
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
laddr = &in6p->in6p_laddr;
|
|
|
|
faddr = &in6p->in6p_faddr;
|
|
|
|
fport = in6p->in6p_fport;
|
|
|
|
}
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if (af == AF_INET)
|
2000-07-04 16:35:15 +00:00
|
|
|
hlen = sizeof(struct ip);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate data length and get a mbuf
|
|
|
|
* for UDP and IP6 headers.
|
|
|
|
*/
|
2003-02-19 05:47:46 +00:00
|
|
|
M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
|
2000-07-04 16:35:15 +00:00
|
|
|
if (m == 0) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff checksum and output datagram.
|
|
|
|
*/
|
|
|
|
udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
|
|
|
|
udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
|
|
|
|
udp6->uh_dport = fport;
|
|
|
|
if (plen <= 0xffff)
|
|
|
|
udp6->uh_ulen = htons((u_short)plen);
|
|
|
|
else
|
|
|
|
udp6->uh_ulen = 0;
|
|
|
|
udp6->uh_sum = 0;
|
|
|
|
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET6:
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
|
|
|
|
ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
|
|
|
|
ip6->ip6_vfc |= IPV6_VERSION;
|
|
|
|
#if 0 /* ip6_plen will be filled in ip6_output. */
|
|
|
|
ip6->ip6_plen = htons((u_short)plen);
|
|
|
|
#endif
|
|
|
|
ip6->ip6_nxt = IPPROTO_UDP;
|
2003-11-20 20:07:39 +00:00
|
|
|
ip6->ip6_hlim = in6_selecthlim(in6p, NULL);
|
2000-07-04 16:35:15 +00:00
|
|
|
ip6->ip6_src = *laddr;
|
|
|
|
ip6->ip6_dst = *faddr;
|
|
|
|
|
|
|
|
if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
|
|
|
|
sizeof(struct ip6_hdr), plen)) == 0) {
|
|
|
|
udp6->uh_sum = 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
udp6stat.udp6s_opackets++;
|
- cleanup SP refcnt issue.
- share policy-on-socket for listening socket.
- don't copy policy-on-socket at all. secpolicy no longer contain
spidx, which saves a lot of memory.
- deep-copy pcb policy if it is an ipsec policy. assign ID field to
all SPD entries. make it possible for racoon to grab SPD entry on
pcb.
- fixed the order of searching SA table for packets.
- fixed to get a security association header. a mode is always needed
to compare them.
- fixed that the incorrect time was set to
sadb_comb_{hard|soft}_usetime.
- disallow port spec for tunnel mode policy (as we don't reassemble).
- an user can define a policy-id.
- clear enc/auth key before freeing.
- fixed that the kernel crashed when key_spdacquire() was called
because key_spdacquire() had been implemented imcopletely.
- preparation for 64bit sequence number.
- maintain ordered list of SA, based on SA id.
- cleanup secasvar management; refcnt is key.c responsibility;
alloc/free is keydb.c responsibility.
- cleanup, avoid double-loop.
- use hash for spi-based lookup.
- mark persistent SP "persistent".
XXX in theory refcnt should do the right thing, however, we have
"spdflush" which would touch all SPs. another solution would be to
de-register persistent SPs from sptree.
- u_short -> u_int16_t
- reduce kernel stack usage by auto variable secasindex.
- clarify function name confusion. ipsec_*_policy ->
ipsec_*_pcbpolicy.
- avoid variable name confusion.
(struct inpcbpolicy *)pcb_sp, spp (struct secpolicy **), sp (struct
secpolicy *)
- count number of ipsec encapsulations on ipsec4_output, so that we
can tell ip_output() how to handle the packet further.
- When the value of the ul_proto is ICMP or ICMPV6, the port field in
"src" of the spidx specifies ICMP type, and the port field in "dst"
of the spidx specifies ICMP code.
- avoid from applying IPsec transport mode to the packets when the
kernel forwards the packets.
Tested by: nork
Obtained from: KAME
2003-11-04 16:02:05 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
if (ipsec_setsocket(m, in6p->in6p_socket) != 0) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
#endif /* IPSEC */
|
2003-11-20 20:07:39 +00:00
|
|
|
error = ip6_output(m, in6p->in6p_outputopts, NULL,
|
2002-10-16 01:54:46 +00:00
|
|
|
flags, in6p->in6p_moptions, NULL, in6p);
|
2000-07-04 16:35:15 +00:00
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
error = EAFNOSUPPORT;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
goto releaseopt;
|
|
|
|
|
|
|
|
release:
|
|
|
|
m_freem(m);
|
|
|
|
|
|
|
|
releaseopt:
|
|
|
|
if (control) {
|
2003-10-24 18:26:30 +00:00
|
|
|
ip6_clearpktopts(in6p->in6p_outputopts, -1);
|
2000-07-04 16:35:15 +00:00
|
|
|
in6p->in6p_outputopts = stickyopt;
|
|
|
|
m_freem(control);
|
|
|
|
}
|
2003-10-06 14:02:09 +00:00
|
|
|
return (error);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|