Blame view

net/ipv4/ip_forward.c 3.86 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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.
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
7
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
   * Authors:	see ip.c
   *
   * Fixes:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
11
   *		Many		:	Split from ip.c , see ip_input.c for
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
   *					history.
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
13
   *		Dave Gregorich	:	NULL ip_rt_put fix for multicast
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
19
20
   *					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
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include <linux/types.h>
  #include <linux/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
  #include <linux/skbuff.h>
  #include <linux/ip.h>
  #include <linux/icmp.h>
  #include <linux/netdevice.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
27
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
36
37
38
39
  #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>
fe6cc55f3   Florian Westphal   net: ip, ipv6: ha...
40
41
  static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
  {
ca6c5d4ad   Florian Westphal   net: ipv4: ip_for...
42
  	if (skb->len <= mtu)
fe6cc55f3   Florian Westphal   net: ip, ipv6: ha...
43
  		return false;
cf8262443   Florian Westphal   ip: reject too-bi...
44
45
46
47
48
49
50
51
52
  	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;
ae7ef81ef   Marcelo Ricardo Leitner   skbuff: introduce...
53
  	if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
fe6cc55f3   Florian Westphal   net: ip, ipv6: ha...
54
55
56
57
  		return false;
  
  	return true;
  }
fe6cc55f3   Florian Westphal   net: ip, ipv6: ha...
58

0c4b51f00   Eric W. Biederman   netfilter: Pass n...
59
  static int ip_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  {
5e73ea1a3   Daniel Baluta   ipv4: fix checkpa...
61
  	struct ip_options *opt	= &(IPCB(skb)->opt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62

b45386efa   Eric Dumazet   net: rename IP_IN...
63
  	__IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
98f619957   Eric Dumazet   net: rename IP_AD...
64
  	__IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
  
  	if (unlikely(opt->optlen))
  		ip_forward_options(skb);
13206b6bf   Eric W. Biederman   net: Pass net int...
68
  	return dst_output(net, sk, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
  }
  
  int ip_forward(struct sk_buff *skb)
  {
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
73
  	u32 mtu;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  	struct iphdr *iph;	/* Our header */
  	struct rtable *rt;	/* Route we use */
5e73ea1a3   Daniel Baluta   ipv4: fix checkpa...
76
  	struct ip_options *opt	= &(IPCB(skb)->opt);
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
77
  	struct net *net;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78

d4f2fa6ad   Denis Kirjanov   ipv4: ip_forward:...
79
80
81
  	/* that should never happen */
  	if (skb->pkt_type != PACKET_HOST)
  		goto drop;
2ab957492   Sebastian Pöhn   ip_forward: Drop ...
82
83
  	if (unlikely(skb->sk))
  		goto drop;
4497b0763   Ben Hutchings   net: Discard and ...
84
85
  	if (skb_warn_if_lro(skb))
  		goto drop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
  	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;
35fc92a9d   Herbert Xu   [NET]: Allow forw...
91
  	skb_forward_csum(skb);
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
92
  	net = dev_net(skb->dev);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
93

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
  	/*
  	 *	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.
  	 */
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
99
  	if (ip_hdr(skb)->ttl <= 1)
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
100
  		goto too_many_hops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
  
  	if (!xfrm4_route_forward(skb))
  		goto drop;
511c3f92a   Eric Dumazet   net: skb->rtable ...
104
  	rt = skb_rtable(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105

155e8336c   Julian Anastasov   ipv4: introduce r...
106
  	if (opt->is_strictroute && rt->rt_uses_gateway)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  		goto sr_failed;
9ee6c5dc8   Lance Richardson   ipv4: allow local...
108
  	IPCB(skb)->flags |= IPSKB_FORWARDED;
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
109
  	mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
cf8262443   Florian Westphal   ip: reject too-bi...
110
  	if (ip_exceeds_mtu(skb, mtu)) {
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
111
  		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
9af3912ec   John Heffner   [NET] Move DF che...
112
  		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
113
  			  htonl(mtu));
9af3912ec   John Heffner   [NET] Move DF che...
114
115
  		goto drop;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  	/* We are about to mangle packet. Copy it! */
d8d1f30b9   Changli Gao   net-next: remove ...
117
  	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  		goto drop;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
119
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
125
126
127
  
  	/* Decrease ttl after skb cow done */
  	ip_decrease_ttl(iph);
  
  	/*
  	 *	We now generate an ICMP HOST REDIRECT giving the route
  	 *	we calculated.
  	 */
df4d92549   Hannes Frederic Sowa   ipv4: try to cach...
128
129
  	if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
  	    !skb_sec_path(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
  		ip_rt_send_redirect(skb);
  
  	skb->priority = rt_tos2priority(iph->tos);
29a26a568   Eric W. Biederman   netfilter: Pass s...
133
134
135
  	return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
  		       net, NULL, skb, skb->dev, rt->dst.dev,
  		       ip_forward_finish);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
  
  sr_failed:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
138
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
  	 *	Strict routing permits no gatewaying
  	 */
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
141
142
  	 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
  	 goto drop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
  
  too_many_hops:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
145
  	/* Tell the sender its packet died... */
b45386efa   Eric Dumazet   net: rename IP_IN...
146
  	__IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
147
  	icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
  drop:
  	kfree_skb(skb);
  	return NET_RX_DROP;
  }