ip_forward.c 3.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * 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.
7
 *
L
Linus Torvalds 已提交
8 9 10
 * Authors:	see ip.c
 *
 * Fixes:
11
 *		Many		:	Split from ip.c , see ip_input.c for
L
Linus Torvalds 已提交
12
 *					history.
13
 *		Dave Gregorich	:	NULL ip_rt_put fix for multicast
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *					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>
28
#include <linux/slab.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
#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>

42 43
static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
{
44
	if (skb->len <= mtu)
45 46
		return false;

47 48 49 50 51 52 53 54 55 56
	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;

57 58 59 60 61 62 63
	if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
		return false;

	return true;
}


64
static int ip_forward_finish(struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
65
{
66
	struct net *net = dev_net(skb_dst(skb)->dev);
D
Daniel Baluta 已提交
67
	struct ip_options *opt	= &(IPCB(skb)->opt);
L
Linus Torvalds 已提交
68

69 70
	IP_INC_STATS_BH(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
	IP_ADD_STATS_BH(net, IPSTATS_MIB_OUTOCTETS, skb->len);
L
Linus Torvalds 已提交
71 72 73 74

	if (unlikely(opt->optlen))
		ip_forward_options(skb);

75
	skb_sender_cpu_clear(skb);
76
	return dst_output(sk, skb);
L
Linus Torvalds 已提交
77 78 79 80
}

int ip_forward(struct sk_buff *skb)
{
81
	u32 mtu;
L
Linus Torvalds 已提交
82 83
	struct iphdr *iph;	/* Our header */
	struct rtable *rt;	/* Route we use */
D
Daniel Baluta 已提交
84
	struct ip_options *opt	= &(IPCB(skb)->opt);
85
	struct net *net;
L
Linus Torvalds 已提交
86

87 88 89 90
	/* that should never happen */
	if (skb->pkt_type != PACKET_HOST)
		goto drop;

91 92 93
	if (unlikely(skb->sk))
		goto drop;

94 95 96
	if (skb_warn_if_lro(skb))
		goto drop;

L
Linus Torvalds 已提交
97 98 99 100 101 102
	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;

103
	skb_forward_csum(skb);
104
	net = dev_net(skb->dev);
105

L
Linus Torvalds 已提交
106 107 108 109 110
	/*
	 *	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.
	 */
111
	if (ip_hdr(skb)->ttl <= 1)
112
		goto too_many_hops;
L
Linus Torvalds 已提交
113 114 115 116

	if (!xfrm4_route_forward(skb))
		goto drop;

E
Eric Dumazet 已提交
117
	rt = skb_rtable(skb);
L
Linus Torvalds 已提交
118

J
Julian Anastasov 已提交
119
	if (opt->is_strictroute && rt->rt_uses_gateway)
L
Linus Torvalds 已提交
120 121
		goto sr_failed;

122 123
	IPCB(skb)->flags |= IPSKB_FORWARDED;
	mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
124
	if (ip_exceeds_mtu(skb, mtu)) {
125
		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
J
John Heffner 已提交
126
		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
127
			  htonl(mtu));
J
John Heffner 已提交
128 129 130
		goto drop;
	}

L
Linus Torvalds 已提交
131
	/* We are about to mangle packet. Copy it! */
132
	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
L
Linus Torvalds 已提交
133
		goto drop;
134
	iph = ip_hdr(skb);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142

	/* Decrease ttl after skb cow done */
	ip_decrease_ttl(iph);

	/*
	 *	We now generate an ICMP HOST REDIRECT giving the route
	 *	we calculated.
	 */
143 144
	if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
	    !skb_sec_path(skb))
L
Linus Torvalds 已提交
145 146 147 148
		ip_rt_send_redirect(skb);

	skb->priority = rt_tos2priority(iph->tos);

149 150 151
	return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
		       net, NULL, skb, skb->dev, rt->dst.dev,
		       ip_forward_finish);
L
Linus Torvalds 已提交
152 153

sr_failed:
154
	/*
L
Linus Torvalds 已提交
155 156
	 *	Strict routing permits no gatewaying
	 */
157 158
	 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
	 goto drop;
L
Linus Torvalds 已提交
159 160

too_many_hops:
161
	/* Tell the sender its packet died... */
162
	IP_INC_STATS_BH(net, IPSTATS_MIB_INHDRERRORS);
163
	icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
L
Linus Torvalds 已提交
164 165 166 167
drop:
	kfree_skb(skb);
	return NET_RX_DROP;
}