/*
* 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, 1988, 1990, 1993, 1995
* 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. 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_usrreq.c 8.6 (Berkeley) 5/23/95
*/
void
udp_input(struct mbuf *m, int off, int proto)
{
struct sockaddr_in src, dst;
struct ip *ip;
struct udphdr *uh;
int iphlen = off;
int len;
int n;
u_int16_t ip_len;
/*
* Get IP and UDP header together in first mbuf.
*/
ip = mtod(m, struct ip *);
M_REGION_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
if (uh == NULL) {
UDP_STATINC(UDP_STAT_HDROPS);
return;
}
/*
* Enforce alignment requirements that are violated in
* some cases, see kern/50766 for details.
*/
if (ACCESSIBLE_POINTER(uh, struct udphdr) == 0) {
m = m_copyup(m, iphlen + sizeof(struct udphdr), 0);
if (m == NULL) {
UDP_STATINC(UDP_STAT_HDROPS);
return;
}
ip = mtod(m, struct ip *);
uh = (struct udphdr *)(mtod(m, char *) + iphlen);
}
KASSERT(ACCESSIBLE_POINTER(uh, struct udphdr));
/* destination port of 0 is illegal, based on RFC768. */
if (uh->uh_dport == 0)
goto bad;
/*
* Make mbuf data length reflect UDP length.
* If not enough data to reflect UDP length, drop.
*/
ip_len = ntohs(ip->ip_len);
len = ntohs((u_int16_t)uh->uh_ulen);
if (len < sizeof(struct udphdr)) {
UDP_STATINC(UDP_STAT_BADLEN);
goto bad;
}
if (ip_len != iphlen + len) {
if (ip_len < iphlen + len) {
UDP_STATINC(UDP_STAT_BADLEN);
goto bad;
}
m_adj(m, iphlen + len - ip_len);
}
/*
* Checksum extended UDP header and data.
*/
if (udp4_input_checksum(m, uh, iphlen, len))
goto badcsum;
if (IN_MULTICAST(dst4->s_addr) ||
in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) {
/*
* Deliver a multicast or broadcast datagram to *all* sockets
* for which the local and remote addresses and ports match
* those of the incoming datagram. This allows more than
* one process to receive multi/broadcasts on the same port.
* (This really ought to be done for unicast datagrams as
* well, but that would cause problems with existing
* applications that open both address-specific sockets and
* a wildcard socket listening to the same port -- they would
* end up receiving duplicates of every unicast datagram.
* Those applications open the multiple sockets to overcome an
* inadequacy of the UDP socket interface, but for backwards
* compatibility we avoid the problem here rather than
* fixing the interface. Maybe 4.5BSD will remedy this?)
*/
/*
* KAME note: traditionally we dropped udpiphdr from mbuf here.
* we need udpiphdr for IPsec processing so we do that later.
*/
/*
* Locate pcb(s) for datagram.
*/
TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
if (inp->inp_af != AF_INET)
continue;
if (inp->inp_lport != *dport)
continue;
if (!in_nullhost(in4p_laddr(inp))) {
if (!in_hosteq(in4p_laddr(inp), *dst4))
continue;
}
if (!in_nullhost(in4p_faddr(inp))) {
if (!in_hosteq(in4p_faddr(inp), *src4) ||
inp->inp_fport != *sport)
continue;
}
/*
* Don't look for additional matches if this one does
* not have either the SO_REUSEPORT or SO_REUSEADDR
* socket options set. This heuristic avoids searching
* through all pcbs in the common case of a non-shared
* port. It assumes that an application will never
* clear these options after setting them.
*/
if ((inp->inp_socket->so_options &
(SO_REUSEPORT|SO_REUSEADDR)) == 0)
break;
}
} else {
/*
* Locate pcb for datagram.
*/
inp = inpcb_lookup(&udbtable, *src4, *sport, *dst4,
*dport, 0);
if (inp == 0) {
UDP_STATINC(UDP_STAT_PCBHASHMISS);
inp = inpcb_lookup_bound(&udbtable, *dst4, *dport);
if (inp == 0)
return rcvcnt;
}
#ifdef IPSEC
/* Handle ESP over UDP */
if (inp->inp_flags & INP_ESPINUDP) {
switch (udp4_espinudp(mp, off)) {
case -1: /* Error, m was freed */
KASSERT(*mp == NULL);
rcvcnt = -1;
goto bad;
case 1: /* ESP over UDP */
KASSERT(*mp == NULL);
rcvcnt++;
goto bad;
case 0: /* plain UDP */
default: /* Unexpected */
/*
* Normal UDP processing will take place,
* m may have changed.
*/
m = *mp;
break;
}
}
#endif
if (inp->inp_overudp_cb != NULL) {
int ret;
ret = inp->inp_overudp_cb(mp, off, inp->inp_socket,
sintosa(src), inp->inp_overudp_arg);
switch (ret) {
case -1: /* Error, m was freed */
KASSERT(*mp == NULL);
rcvcnt = -1;
goto bad;
case 1: /* Foo over UDP */
KASSERT(*mp == NULL);
rcvcnt++;
goto bad;
case 0: /* plain UDP */
default: /* Unexpected */
/*
* Normal UDP processing will take place,
* m may have changed.
*/
m = *mp;
break;
}
}
/*
* Check the minimum TTL for socket.
*/
if (mtod(m, struct ip *)->ip_ttl < in4p_ip_minttl(inp))
goto bad;
#ifdef INET
/*
* Notify a udp user of an asynchronous error;
* just wake up so that he can collect error status.
*/
static void
udp_notify(struct inpcb *inp, int errno)
{
inp->inp_socket->so_error = errno;
sorwakeup(inp->inp_socket);
sowwakeup(inp->inp_socket);
}
int
udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
{
int s;
int error = 0;
struct inpcb *inp;
int family;
int optval;
family = so->so_proto->pr_domain->dom_family;
s = splsoftnet();
switch (family) {
#ifdef INET
case PF_INET:
if (sopt->sopt_level != IPPROTO_UDP) {
error = ip_ctloutput(op, so, sopt);
goto end;
}
break;
#endif
#ifdef INET6
case PF_INET6:
if (sopt->sopt_level != IPPROTO_UDP) {
error = ip6_ctloutput(op, so, sopt);
goto end;
}
break;
#endif
default:
error = EAFNOSUPPORT;
goto end;
}
switch (op) {
case PRCO_SETOPT:
inp = sotoinpcb(so);
switch (sopt->sopt_name) {
case UDP_ENCAP:
error = sockopt_getint(sopt, &optval);
if (error)
break;
switch(optval) {
case 0:
inp->inp_flags &= ~INP_ESPINUDP;
break;
case UDP_ENCAP_ESPINUDP:
inp->inp_flags |= INP_ESPINUDP;
break;
default:
error = EINVAL;
break;
}
break;
default:
error = ENOPROTOOPT;
break;
}
break;
default:
error = EINVAL;
break;
}
end:
splx(s);
return error;
}
int
udp_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
struct lwp *l)
{
struct udpiphdr *ui;
struct route *ro;
struct ip_pktopts pktopts;
kauth_cred_t cred;
int len = m->m_pkthdr.len;
int error, flags = 0;
MCLAIM(m, &udp_tx_mowner);
/*
* Calculate data length and get a mbuf
* for UDP and IP headers.
*/
M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
if (m == NULL) {
error = ENOBUFS;
goto release;
}
/*
* Compute the packet length of the IP header, and
* punt if the length looks bogus.
*/
if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
error = EMSGSIZE;
goto release;
}
#if defined(INET) && defined(IPSEC)
/*
* Handle ESP-in-UDP packets (RFC3948).
*
* We need to distinguish between ESP packets and IKE packets. We do so by
* looking at the Non-ESP marker. If IKE, we process the UDP packet as usual.
* Otherwise, ESP, we invoke IPsec.
*
* Returns:
* 1 if the packet was processed
* 0 if normal UDP processing should take place
* -1 if an error occurred and m was freed
*/
static int
udp4_espinudp(struct mbuf **mp, int off)
{
const size_t skip = sizeof(struct udphdr);
size_t len;
uint8_t *data;
size_t minlen;
size_t iphdrlen;
struct ip *ip;
struct m_tag *tag;
struct udphdr *udphdr;
u_int16_t sport, dport;
struct mbuf *m = *mp;
uint32_t *marker;
minlen = off + sizeof(struct esp);
if (minlen > m->m_pkthdr.len)
minlen = m->m_pkthdr.len;
if (m->m_len < minlen) {
if ((*mp = m_pullup(m, minlen)) == NULL) {
return -1; /* dropped */
}
m = *mp;
}
len = m->m_len - off;
data = mtod(m, uint8_t *) + off;
/* Ignore keepalive packets. */
if ((len == 1) && (*data == 0xff)) {
m_freem(m);
*mp = NULL; /* avoid any further processing by caller */
return 1; /* consumed */
}
/* Handle Non-ESP marker (32bit). If zero, then IKE. */
marker = (uint32_t *)data;
if (len <= sizeof(uint32_t))
return 0; /* passthrough */
if (marker[0] == 0)
return 0; /* passthrough */
/*
* Get the UDP ports. They are handled in network order
* everywhere in the IPSEC_NAT_T code.
*/
udphdr = (struct udphdr *)((char *)data - skip);
sport = udphdr->uh_sport;
dport = udphdr->uh_dport;
/*
* Remove the UDP header, plus a possible marker. IP header
* length is iphdrlen.
*
* Before:
* <--- off --->
* +----+------+-----+
* | IP | UDP | ESP |
* +----+------+-----+
* <-skip->
* After:
* +----+-----+
* | IP | ESP |
* +----+-----+
* <-skip->
*/
iphdrlen = off - sizeof(struct udphdr);
memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
m_adj(m, skip);
ip = mtod(m, struct ip *);
ip->ip_len = htons(ntohs(ip->ip_len) - skip);
ip->ip_p = IPPROTO_ESP;
/*
* We have modified the packet - it is now ESP, so we should not
* return to UDP processing.
*
* Add a PACKET_TAG_IPSEC_NAT_T_PORTS tag to remember the source
* UDP port. This is required if we want to select the right SPD
* for multiple hosts behind same NAT.
*/
if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
m_freem(m);
*mp = NULL;
return -1; /* dropped */
}
((u_int16_t *)(tag + 1))[0] = sport;
((u_int16_t *)(tag + 1))[1] = dport;
m_tag_prepend(m, tag);
if (ipsec_used)
ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
else
m_freem(m);
/* We handled it, it shouldn't be handled by UDP */
*mp = NULL; /* avoid free by caller ... */
return 1; /* consumed */
}
#endif