ip_forward.c 3.3 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>

P
Patrick McHardy 已提交
42
static int ip_forward_finish(struct sk_buff *skb)
L
Linus Torvalds 已提交
43
{
D
Daniel Baluta 已提交
44
	struct ip_options *opt	= &(IPCB(skb)->opt);
L
Linus Torvalds 已提交
45

E
Eric Dumazet 已提交
46
	IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57

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

	return dst_output(skb);
}

int ip_forward(struct sk_buff *skb)
{
	struct iphdr *iph;	/* Our header */
	struct rtable *rt;	/* Route we use */
D
Daniel Baluta 已提交
58
	struct ip_options *opt	= &(IPCB(skb)->opt);
L
Linus Torvalds 已提交
59

60 61 62
	if (skb_warn_if_lro(skb))
		goto drop;

L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71
	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;

	if (skb->pkt_type != PACKET_HOST)
		goto drop;

72
	skb_forward_csum(skb);
73

L
Linus Torvalds 已提交
74 75 76 77 78
	/*
	 *	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.
	 */
79
	if (ip_hdr(skb)->ttl <= 1)
80
		goto too_many_hops;
L
Linus Torvalds 已提交
81 82 83 84

	if (!xfrm4_route_forward(skb))
		goto drop;

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

87
	if (opt->is_strictroute && opt->nexthop != rt->rt_gateway)
L
Linus Torvalds 已提交
88 89
		goto sr_failed;

90
	if (unlikely(skb->len > dst_mtu(&rt->dst) && !skb_is_gso(skb) &&
91
		     (ip_hdr(skb)->frag_off & htons(IP_DF))) && !skb->local_df) {
92
		IP_INC_STATS(dev_net(rt->dst.dev), IPSTATS_MIB_FRAGFAILS);
J
John Heffner 已提交
93
		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
94
			  htonl(dst_mtu(&rt->dst)));
J
John Heffner 已提交
95 96 97
		goto drop;
	}

L
Linus Torvalds 已提交
98
	/* We are about to mangle packet. Copy it! */
99
	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
L
Linus Torvalds 已提交
100
		goto drop;
101
	iph = ip_hdr(skb);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109

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

	/*
	 *	We now generate an ICMP HOST REDIRECT giving the route
	 *	we calculated.
	 */
110
	if (rt->rt_flags&RTCF_DOREDIRECT && !opt->srr && !skb_sec_path(skb))
L
Linus Torvalds 已提交
111 112 113 114
		ip_rt_send_redirect(skb);

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

115
	return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev,
116
		       rt->dst.dev, ip_forward_finish);
L
Linus Torvalds 已提交
117 118

sr_failed:
119
	/*
L
Linus Torvalds 已提交
120 121
	 *	Strict routing permits no gatewaying
	 */
122 123
	 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
	 goto drop;
L
Linus Torvalds 已提交
124 125

too_many_hops:
126
	/* Tell the sender its packet died... */
E
Eric Dumazet 已提交
127
	IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_INHDRERRORS);
128
	icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
L
Linus Torvalds 已提交
129 130 131 132
drop:
	kfree_skb(skb);
	return NET_RX_DROP;
}