/*
* 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) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Public Access Networks Corporation ("Panix"). It was developed under
* contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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, 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. 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.
*
* @(#)ip_input.c 8.2 (Berkeley) 1/4/94
*/
#ifndef IPFORWARDING
#ifdef GATEWAY
#define IPFORWARDING 1 /* forward IP packets not for us */
#else
#define IPFORWARDING 0 /* don't forward IP packets not for us */
#endif
#endif
#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
int ipforwarding = IPFORWARDING;
int ipsendredirects = 1;
int ip_defttl = IPDEFTTL;
int ip_forwsrcrt = 0;
int ip_directedbcast = 0;
int ip_allowsrcrt = 0;
int ip_mtudisc = 1;
int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
int ip_do_randomid = 1;
/*
* XXX - Setting ip_checkinterface mostly implements the receive side of
* the Strong ES model described in RFC 1122, but since the routing table
* and transmit implementation do not implement the Strong ES model,
* setting this to 1 results in an odd hybrid.
*
* XXX - ip_checkinterface currently must be disabled if you use NAT
* to translate the destination address to another local interface.
*
* XXX - ip_checkinterface must be disabled if you add IP aliases
* to the loopback interface instead of the interface where the
* packets for those addresses are received.
*/
static int ip_checkinterface __read_mostly = 0;
/*
* Used to save the IP options in case a protocol wants to respond
* to an incoming packet over the same route if the packet got here
* using IP source routing. This allows connection establishment and
* maintenance when the remote end is on a network that is not known
* to us.
*/
struct ip_srcrt {
int isr_nhops; /* number of hops */
struct in_addr isr_dst; /* final destination */
char isr_nop; /* one NOP to align */
char isr_hdr[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN & OFFSET */
struct in_addr isr_routes[MAX_IPOPTLEN/sizeof(struct in_addr)];
};
/*
* IP initialization: fill in IP protocol switch table.
* All protocols not implemented in kernel go to raw IP protocol handler.
*/
void
ip_init(void)
{
const struct protosw *pr;
static struct in_ifaddr *
ip_match_our_address(struct ifnet *ifp, struct ip *ip, int *downmatch)
{
struct in_ifaddr *ia = NULL;
int checkif;
/*
* Enable a consistency check between the destination address
* and the arrival interface for a unicast packet (the RFC 1122
* strong ES model) if IP forwarding is disabled and the packet
* is not locally generated.
*
* XXX - We need to add a per ifaddr flag for this so that
* we get finer grain control.
*/
checkif = ip_checkinterface && (ipforwarding == 0) &&
(ifp->if_flags & IFF_LOOPBACK) == 0;
IN_ADDRHASH_READER_FOREACH(ia, ip->ip_dst.s_addr) {
if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
if (ia->ia4_flags & IN_IFF_NOTREADY)
continue;
if (checkif && ia->ia_ifp != ifp)
continue;
if ((ia->ia_ifp->if_flags & IFF_UP) == 0) {
(*downmatch)++;
continue;
}
if (ia->ia4_flags & IN_IFF_DETACHED &&
(ifp->if_flags & IFF_LOOPBACK) == 0)
continue;
break;
}
}
IFADDR_READER_FOREACH(ifa, ifp) {
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
ia = ifatoia(ifa);
if (ia->ia4_flags & IN_IFF_NOTREADY)
continue;
if (ia->ia4_flags & IN_IFF_DETACHED &&
(ifp->if_flags & IFF_LOOPBACK) == 0)
continue;
if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
/*
* Look for all-0's host part (old broadcast addr),
* either for subnet or net.
*/
ip->ip_dst.s_addr == ia->ia_subnet ||
ip->ip_dst.s_addr == ia->ia_net)
goto matched;
/*
* An interface with IP address zero accepts
* all packets that arrive on that interface.
*/
if (in_nullhost(ia->ia_addr.sin_addr))
goto matched;
}
ia = NULL;
/*
* IP input routine. Checksum and byte swap header. If fragmented
* try to reassemble. Process options. Pass to next level.
*/
static void
ip_input(struct mbuf *m, struct ifnet *ifp)
{
struct ip *ip = NULL;
struct in_ifaddr *ia = NULL;
int hlen = 0, len;
int downmatch;
int srcrt = 0;
int s;
KASSERTMSG(cpu_softintr_p(), "ip_input: not in the software "
"interrupt handler; synchronization assumptions violated");
/*
* If no IP addresses have been set yet but the interfaces
* are receiving, can't do anything with incoming packets yet.
* Note: we pre-check without locks held.
*/
if (IN_ADDRLIST_READER_EMPTY()) {
IP_STATINC(IP_STAT_IFDROP);
goto out;
}
IP_STATINC(IP_STAT_TOTAL);
/*
* If the IP header is not aligned, slurp it up into a new
* mbuf with space for link headers, in the event we forward
* it. Otherwise, if it is aligned, make sure the entire
* base IP header is in the first mbuf of the chain.
*/
if (M_GET_ALIGNED_HDR(&m, struct ip, true) != 0) {
/* XXXJRT new stat, please */
IP_STATINC(IP_STAT_TOOSMALL);
goto out;
}
ip = mtod(m, struct ip *);
if (ip->ip_v != IPVERSION) {
IP_STATINC(IP_STAT_BADVERS);
goto out;
}
hlen = ip->ip_hl << 2;
if (hlen < sizeof(struct ip)) { /* minimum header length */
IP_STATINC(IP_STAT_BADHLEN);
goto out;
}
if (hlen > m->m_len) {
if ((m = m_pullup(m, hlen)) == NULL) {
IP_STATINC(IP_STAT_BADHLEN);
goto out;
}
ip = mtod(m, struct ip *);
}
/*
* RFC1122: packets with a multicast source address are
* not allowed.
*/
if (IN_MULTICAST(ip->ip_src.s_addr)) {
IP_STATINC(IP_STAT_BADADDR);
goto out;
}
/* 127/8 must not appear on wire - RFC1122 */
if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
(ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
IP_STATINC(IP_STAT_BADADDR);
goto out;
}
}
case M_CSUM_IPv4:
/* Checksum was okay. */
INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
break;
default:
/*
* Must compute it ourselves. Maybe skip checksum on
* loopback interfaces.
*/
if (__predict_true(!(ifp->if_flags & IFF_LOOPBACK) ||
ip_do_loopback_cksum)) {
INET_CSUM_COUNTER_INCR(&ip_swcsum);
if (in_cksum(m, hlen) != 0) {
IP_STATINC(IP_STAT_BADSUM);
goto out;
}
}
break;
}
/* Retrieve the packet length. */
len = ntohs(ip->ip_len);
/*
* Check for additional length bogosity
*/
if (len < hlen) {
IP_STATINC(IP_STAT_BADLEN);
goto out;
}
/*
* Check that the amount of data in the buffers is at least as much
* as the IP header would have us expect. Trim mbufs if longer than
* we expect. Drop packet if shorter than we expect.
*/
if (m->m_pkthdr.len < len) {
IP_STATINC(IP_STAT_TOOSHORT);
goto out;
}
if (m->m_pkthdr.len > len) {
if (m->m_len == m->m_pkthdr.len) {
m->m_len = len;
m->m_pkthdr.len = len;
} else
m_adj(m, len - m->m_pkthdr.len);
}
/*
* Assume that we can create a fast-forward IP flow entry
* based on this packet.
*/
m->m_flags |= M_CANFASTFWD;
/*
* Run through list of hooks for input packets. If there are any
* filters which require that additional packets in the flow are
* not fast-forwarded, they must clear the M_CANFASTFWD flag.
* Note that filters must _never_ set this flag, as another filter
* in the list may have previously cleared it.
*
* Don't call hooks if the packet has already been processed by
* IPsec (encapsulated, tunnel mode).
*/
#if defined(IPSEC)
if (!ipsec_used || !ipsec_skip_pfil(m))
#else
if (1)
#endif
{
struct in_addr odst = ip->ip_dst;
bool freed;
freed = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_IN) != 0;
if (freed || m == NULL) {
m = NULL;
IP_STATINC(IP_STAT_PFILDROP_IN);
goto out;
}
if (__predict_false(m->m_len < sizeof(struct ip))) {
if ((m = m_pullup(m, sizeof(struct ip))) == NULL) {
IP_STATINC(IP_STAT_TOOSMALL);
goto out;
}
}
ip = mtod(m, struct ip *);
hlen = ip->ip_hl << 2;
if (hlen < sizeof(struct ip)) { /* minimum header length */
IP_STATINC(IP_STAT_BADHLEN);
goto out;
}
if (hlen > m->m_len) {
if ((m = m_pullup(m, hlen)) == NULL) {
IP_STATINC(IP_STAT_BADHLEN);
goto out;
}
ip = mtod(m, struct ip *);
}
/*
* XXX The setting of "srcrt" here is to prevent ip_forward()
* from generating ICMP redirects for packets that have
* been redirected by a hook back out on to the same LAN that
* they came from and is not an indication that the packet
* is being influenced by source routing options. This
* allows things like
* "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
* where tlp0 is both on the 1.1.1.0/24 network and is the
* default route for hosts on 1.1.1.0/24. Of course this
* also requires a "map tlp0 ..." to complete the story.
* One might argue whether or not this kind of network config.
* should be supported in this manner...
*/
srcrt = (odst.s_addr != ip->ip_dst.s_addr);
}
#ifdef ALTQ
/* XXX Temporary until ALTQ is changed to use a pfil hook */
if (altq_input) {
SOFTNET_LOCK();
if ((*altq_input)(m, AF_INET) == 0) {
/* Packet dropped by traffic conditioner. */
SOFTNET_UNLOCK();
m = NULL;
goto out;
}
SOFTNET_UNLOCK();
}
#endif
/*
* Process options and, if not destined for us,
* ship it on. ip_dooptions returns 1 when an
* error was detected (causing an icmp message
* to be sent and the original packet to be freed).
*/
if (hlen > sizeof(struct ip) && ip_dooptions(m)) {
m = NULL;
goto out;
}
/*
* Check our list of addresses, to see if the packet is for us.
*
* Traditional 4.4BSD did not consult IFF_UP at all.
* The behavior here is to treat addresses on !IFF_UP interface
* or IN_IFF_NOTREADY addresses as not mine.
*/
downmatch = 0;
s = pserialize_read_enter();
ia = ip_match_our_address(ifp, ip, &downmatch);
if (ia != NULL) {
pserialize_read_exit(s);
goto ours;
}
if (ifp->if_flags & IFF_BROADCAST) {
ia = ip_match_our_address_broadcast(ifp, ip);
if (ia != NULL) {
pserialize_read_exit(s);
goto ours;
}
}
pserialize_read_exit(s);
if (IN_MULTICAST(ip->ip_dst.s_addr)) {
#ifdef MROUTING
extern struct socket *ip_mrouter;
if (ip_mrouter) {
/*
* If we are acting as a multicast router, all
* incoming multicast packets are passed to the
* kernel-level multicast forwarding function.
* The packet is returned (relatively) intact; if
* ip_mforward() returns a non-zero value, the packet
* must be discarded, else it may be accepted below.
*
* (The IP ident field is put in the same byte order
* as expected when ip_mforward() is called from
* ip_output().)
*/
SOFTNET_LOCK();
if (ip_mforward(m, ifp) != 0) {
SOFTNET_UNLOCK();
IP_STATINC(IP_STAT_CANTFORWARD);
goto out;
}
SOFTNET_UNLOCK();
/*
* The process-level routing demon needs to receive
* all multicast IGMP packets, whether or not this
* host belongs to their destination groups.
*/
if (ip->ip_p == IPPROTO_IGMP) {
goto ours;
}
IP_STATINC(IP_STAT_CANTFORWARD);
}
#endif
/*
* See if we belong to the destination multicast group on the
* arrival interface.
*/
if (!in_multi_group(ip->ip_dst, ifp, 0)) {
IP_STATINC(IP_STAT_CANTFORWARD);
goto out;
}
goto ours;
}
if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
in_nullhost(ip->ip_dst))
goto ours;
/*
* Not for us; forward if possible and desirable.
*/
if (ipforwarding == 0) {
IP_STATINC(IP_STAT_CANTFORWARD);
m_freem(m);
} else {
/*
* If ip_dst matched any of my address on !IFF_UP interface,
* and there's no IFF_UP interface that matches ip_dst,
* send icmp unreach. Forwarding it will result in in-kernel
* forwarding loop till TTL goes to 0.
*/
if (downmatch) {
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
IP_STATINC(IP_STAT_CANTFORWARD);
return;
}
#ifdef IPSEC
/* Check the security policy (SP) for the packet */
if (ipsec_used) {
if (ipsec_ip_input_checkpolicy(m, true) != 0) {
IP_STATINC(IP_STAT_IPSECDROP_IN);
goto out;
}
}
#endif
ip_forward(m, srcrt, ifp);
}
return;
ours:
/*
* If offset or IP_MF are set, must reassemble.
*/
if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
/*
* Pass to IP reassembly mechanism.
*/
if (ip_reass_packet(&m) != 0) {
/* Failed; invalid fragment(s) or packet. */
goto out;
}
if (m == NULL) {
/* More fragments should come; silently return. */
goto out;
}
/*
* Reassembly is done, we have the final packet.
* Update cached data in local variable(s).
*/
ip = mtod(m, struct ip *);
hlen = ip->ip_hl << 2;
}
M_VERIFY_PACKET(m);
#ifdef IPSEC
/*
* Enforce IPsec policy checking if we are seeing last header.
* Note that we do not visit this with protocols with PCB layer
* code - like UDP/TCP/raw IP.
*/
if (ipsec_used &&
(inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
if (ipsec_ip_input_checkpolicy(m, false) != 0) {
IP_STATINC(IP_STAT_IPSECDROP_IN);
goto out;
}
}
#endif
/*
* Switch out to protocol's input routine.
*/
#if IFA_STATS
if (ia) {
struct in_ifaddr *_ia;
/*
* Keep a reference from ip_match_our_address with psref
* is expensive, so explore ia here again.
*/
s = pserialize_read_enter();
_ia = in_get_ia(ip->ip_dst);
_ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
pserialize_read_exit(s);
}
#endif
IP_STATINC(IP_STAT_DELIVERED);
/*
* ip_dooptions: perform option processing on a datagram, possibly discarding
* it if bad options are encountered, or forwarding it if source-routed.
*
* => Returns true if packet has been forwarded/freed.
* => Returns false if the packet should be processed further.
*/
static bool
ip_dooptions(struct mbuf *m)
{
struct ip *ip = mtod(m, struct ip *);
u_char *cp, *cp0;
struct ip_timestamp *ipt;
struct in_ifaddr *ia;
int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
int srr_present, rr_present, ts_present;
struct in_addr dst;
n_time ntime;
struct ifaddr *ifa = NULL;
int s;
/*
* Source routing with record.
* Find interface with current destination address.
* If none on this machine then drop if strictly routed,
* or do nothing if loosely routed.
* Record interface address and bring up next address
* component. If strictly routed make sure next
* address is on directly accessible net.
*/
case IPOPT_LSRR:
case IPOPT_SSRR: {
struct psref psref;
struct sockaddr_in ipaddr = {
.sin_len = sizeof(ipaddr),
.sin_family = AF_INET,
};
s = pserialize_read_enter();
ifa = ifa_ifwithaddr(sintosa(&ipaddr));
if (ifa == NULL) {
pserialize_read_exit(s);
if (opt == IPOPT_SSRR) {
type = ICMP_UNREACH;
code = ICMP_UNREACH_SRCFAIL;
goto bad;
}
/*
* Loose routing, and not at next destination
* yet; nothing to do except forward.
*/
break;
}
pserialize_read_exit(s);
off--; /* 0 origin */
if ((off + sizeof(struct in_addr)) > optlen) {
/*
* End of source route. Should be for us.
*/
save_rte(m, cp, ip->ip_src);
break;
}
/*
* locate outgoing interface
*/
memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
sizeof(ipaddr.sin_addr));
if (opt == IPOPT_SSRR) {
ifa = ifa_ifwithladdr_psref(sintosa(&ipaddr),
&psref);
if (ifa != NULL)
ia = ifatoia(ifa);
else
ia = NULL;
} else {
ia = ip_rtaddr(ipaddr.sin_addr, &psref);
}
if (ia == NULL) {
type = ICMP_UNREACH;
code = ICMP_UNREACH_SRCFAIL;
goto bad;
}
ip->ip_dst = ipaddr.sin_addr;
memcpy(cp + off, &ia->ia_addr.sin_addr,
sizeof(struct in_addr));
ia4_release(ia, &psref);
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
/*
* Let ip_intr's mcast routing check handle mcast pkts
*/
forward = !IN_MULTICAST(ip->ip_dst.s_addr);
break;
}
/*
* ip_rtaddr: given address of next destination (final or next hop),
* return internet address info of interface to be used to get there.
*/
static struct in_ifaddr *
ip_rtaddr(struct in_addr dst, struct psref *psref)
{
struct rtentry *rt;
union {
struct sockaddr dst;
struct sockaddr_in dst4;
} u;
struct route *ro;
sockaddr_in_init(&u.dst4, &dst, 0);
ro = rtcache_percpu_getref(ipforward_rt_percpu);
rt = rtcache_lookup(ro, &u.dst);
if (rt == NULL) {
rtcache_percpu_putref(ipforward_rt_percpu);
return NULL;
}
/*
* save_rte: save incoming source route for use in replies, to be picked
* up later by ip_srcroute if the receiver is interested.
*/
static void
save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
{
struct ip_srcrt *isr;
struct m_tag *mtag;
unsigned olen;
olen = option[IPOPT_OLEN];
if (olen > sizeof(isr->isr_hdr) + sizeof(isr->isr_routes))
return;
/*
* Retrieve incoming source route for use in replies,
* in the same form used by setsockopt.
* The first hop is placed before the options, will be removed later.
*/
struct mbuf *
ip_srcroute(struct mbuf *m0)
{
struct in_addr *p, *q;
struct mbuf *m;
struct ip_srcrt *isr;
struct m_tag *mtag;
/*
* Forward a packet. If some error occurs return the sender
* an icmp packet. Note we can't always generate a meaningful
* icmp message because icmp doesn't have a large enough repertoire
* of codes and types.
*
* If not forwarding, just drop the packet. This could be confusing
* if ipforwarding was zero but some routing protocol was advancing
* us as a gateway to somewhere. However, we must let the routing
* protocol deal with that.
*
* The srcrt parameter indicates whether the packet is being forwarded
* via a source route.
*/
static void
ip_forward(struct mbuf *m, int srcrt, struct ifnet *rcvif)
{
struct ip *ip = mtod(m, struct ip *);
struct rtentry *rt;
int error, type = 0, code = 0, destmtu = 0;
struct mbuf *mcopy;
n_long dest;
union {
struct sockaddr dst;
struct sockaddr_in dst4;
} u;
net_stat_ref_t ips;
struct route *ro;
KASSERTMSG(cpu_softintr_p(), "ip_forward: not in the software "
"interrupt handler; synchronization assumptions violated");
/*
* We are now in the output path.
*/
MCLAIM(m, &ip_tx_mowner);
/*
* Clear any in-bound checksum flags for this packet.
*/
m->m_pkthdr.csum_flags = 0;
dest = 0;
if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
IP_STATINC(IP_STAT_CANTFORWARD);
m_freem(m);
return;
}
ro = rtcache_percpu_getref(ipforward_rt_percpu);
rt = rtcache_lookup(ro, &u.dst);
if (rt == NULL) {
rtcache_percpu_putref(ipforward_rt_percpu);
IP_STATINC(IP_STAT_NOROUTE);
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
return;
}
#ifdef NET_MPSAFE
/*
* XXX workaround an inconsistency issue between address and route on
* address initialization to avoid packet looping. See doc/TODO.smpnet.
*/
if (__predict_false(rt->rt_ifp->if_type == IFT_LOOP)) {
rtcache_unref(rt, ro);
rtcache_percpu_putref(ipforward_rt_percpu);
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
IP_STATINC(IP_STAT_CANTFORWARD);
return;
}
#endif
/*
* Save at most 68 bytes of the packet in case
* we need to generate an ICMP message to the src.
* Pullup to avoid sharing mbuf cluster between m and mcopy.
*/
mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
if (mcopy)
mcopy = m_pullup(mcopy, ip->ip_hl << 2);
ip->ip_ttl -= IPTTLDEC;
/*
* If forwarding packet using same interface that it came in on,
* perhaps should send a redirect to sender to shortcut a hop.
* Only send redirect if source is sending directly to us,
* and if packet was not source routed (or has any options).
* Also, don't send redirect if forwarding using a default route
* or a route modified by a redirect.
*/
if (rt->rt_ifp == rcvif &&
(rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
!in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
ipsendredirects && !srcrt) {
if ((ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
ifatoia(rt->rt_ifa)->ia_subnet) {
if (rt->rt_flags & RTF_GATEWAY)
dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
else
dest = ip->ip_dst.s_addr;
/*
* Router requirements says to only send host
* redirects.
*/
type = ICMP_REDIRECT;
code = ICMP_REDIRECT_HOST;
}
}
rtcache_unref(rt, ro);
redirect:
error:
if (mcopy == NULL) {
rtcache_percpu_putref(ipforward_rt_percpu);
return;
}
switch (error) {
case 0: /* forwarded, but need redirect */
/* type, code set above */
break;
case ENETUNREACH: /* shouldn't happen, checked above */
case EHOSTUNREACH:
case ENETDOWN:
case EHOSTDOWN:
default:
type = ICMP_UNREACH;
code = ICMP_UNREACH_HOST;
break;
case EMSGSIZE:
type = ICMP_UNREACH;
code = ICMP_UNREACH_NEEDFRAG;
case ENOBUFS:
/*
* Do not generate ICMP_SOURCEQUENCH as required in RFC 1812,
* Requirements for IP Version 4 Routers. Source quench can
* be a big problem under DoS attacks or if the underlying
* interface is rate-limited.
*/
m_freem(mcopy);
rtcache_percpu_putref(ipforward_rt_percpu);
return;
}
icmp_error(mcopy, type, code, dest, destmtu);
rtcache_percpu_putref(ipforward_rt_percpu);
}
/*
* sysctl helper routine for net.inet.ip.mtudisctimeout. checks the
* range of the new value and tweaks timers if it changes.
*/
static int
sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
{
int error, tmp;
struct sysctlnode node;