Files
linux-apfs/net/ipv4/ip_forward.c
T

166 lines
3.9 KiB
C
Raw Normal View History

2005-04-16 15:20:36 -07:00
/*
* INET An implementation of the TCP/IP protocol suite for the LINUX
* operating system. INET is implemented using the BSD Socket
* interface as the means of communication with the user level.
*
* The IP forwarding functionality.
2007-02-09 23:24:47 +09:00
*
2005-04-16 15:20:36 -07:00
* Authors: see ip.c
*
* Fixes:
2007-02-09 23:24:47 +09:00
* Many : Split from ip.c , see ip_input.c for
2005-04-16 15:20:36 -07:00
* history.
2007-02-09 23:24:47 +09:00
* Dave Gregorich : NULL ip_rt_put fix for multicast
2005-04-16 15:20:36 -07:00
* routing.
* Jos Vos : Add call_out_firewall before sending,
* use output device for accounting.
* Jos Vos : Call forward firewall after routing
* (always use output device).
* Mike McLagan : Routing by source
*/
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
2005-04-16 15:20:36 -07:00
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/netfilter_ipv4.h>
#include <net/checksum.h>
#include <linux/route.h>
#include <net/route.h>
#include <net/xfrm.h>
static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
{
if (skb->len <= mtu)
return false;
if (unlikely((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0))
return false;
/* original fragment exceeds mtu and DF is set */
if (unlikely(IPCB(skb)->frag_max_size > mtu))
return true;
if (skb->ignore_df)
return false;
2016-06-02 15:05:41 -03:00
if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
return false;
return true;
}
2015-09-15 20:04:18 -05:00
static int ip_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
2005-04-16 15:20:36 -07:00
{
2012-04-15 01:34:41 +00:00
struct ip_options *opt = &(IPCB(skb)->opt);
2005-04-16 15:20:36 -07:00
2016-04-27 16:44:35 -07:00
__IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
2016-04-27 16:44:37 -07:00
__IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
2005-04-16 15:20:36 -07:00
if (unlikely(opt->optlen))
ip_forward_options(skb);
return dst_output(net, sk, skb);
2005-04-16 15:20:36 -07:00
}
int ip_forward(struct sk_buff *skb)
{
u32 mtu;
2005-04-16 15:20:36 -07:00
struct iphdr *iph; /* Our header */
struct rtable *rt; /* Route we use */
2012-04-15 01:34:41 +00:00
struct ip_options *opt = &(IPCB(skb)->opt);
2015-09-15 20:03:54 -05:00
struct net *net;
2005-04-16 15:20:36 -07:00
/* that should never happen */
if (skb->pkt_type != PACKET_HOST)
goto drop;
if (unlikely(skb->sk))
goto drop;
if (skb_warn_if_lro(skb))
goto drop;
2005-04-16 15:20:36 -07:00
if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb))
goto drop;
if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
return NET_RX_SUCCESS;
skb_forward_csum(skb);
2015-09-15 20:03:54 -05:00
net = dev_net(skb->dev);
2007-02-09 23:24:47 +09:00
2005-04-16 15:20:36 -07:00
/*
* According to the RFC, we must first decrease the TTL field. If
* that reaches zero, we must reply an ICMP control message telling
* that the packet's lifetime expired.
*/
if (ip_hdr(skb)->ttl <= 1)
2007-02-09 23:24:47 +09:00
goto too_many_hops;
2005-04-16 15:20:36 -07:00
if (!xfrm4_route_forward(skb))
goto drop;
2009-06-02 05:14:27 +00:00
rt = skb_rtable(skb);
2005-04-16 15:20:36 -07:00
2012-10-08 11:41:18 +00:00
if (opt->is_strictroute && rt->rt_uses_gateway)
2005-04-16 15:20:36 -07:00
goto sr_failed;
IPCB(skb)->flags |= IPSKB_FORWARDED;
mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
if (ip_exceeds_mtu(skb, mtu)) {
2015-09-15 20:03:54 -05:00
IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
2007-03-25 23:32:29 -07:00
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
htonl(mtu));
2007-03-25 23:32:29 -07:00
goto drop;
}
2005-04-16 15:20:36 -07:00
/* We are about to mangle packet. Copy it! */
2010-06-10 23:31:35 -07:00
if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
2005-04-16 15:20:36 -07:00
goto drop;
iph = ip_hdr(skb);
2005-04-16 15:20:36 -07:00
/* Decrease ttl after skb cow done */
ip_decrease_ttl(iph);
/*
* We now generate an ICMP HOST REDIRECT giving the route
* we calculated.
*/
if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
!skb_sec_path(skb))
2005-04-16 15:20:36 -07:00
ip_rt_send_redirect(skb);
skb->priority = rt_tos2priority(iph->tos);
return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
net, NULL, skb, skb->dev, rt->dst.dev,
ip_forward_finish);
2005-04-16 15:20:36 -07:00
sr_failed:
2007-02-09 23:24:47 +09:00
/*
2005-04-16 15:20:36 -07:00
* Strict routing permits no gatewaying
*/
2007-02-09 23:24:47 +09:00
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
goto drop;
2005-04-16 15:20:36 -07:00
too_many_hops:
2007-02-09 23:24:47 +09:00
/* Tell the sender its packet died... */
2016-04-27 16:44:35 -07:00
__IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
2007-02-09 23:24:47 +09:00
icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
2005-04-16 15:20:36 -07:00
drop:
kfree_skb(skb);
return NET_RX_DROP;
}