Blame view

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

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

b45386efa   Eric Dumazet   net: rename IP_IN...
64
  	__IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
98f619957   Eric Dumazet   net: rename IP_AD...
65
  	__IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66

f839a6c92   Ido Schimmel   net: Do not route...
67
68
69
70
71
72
  #ifdef CONFIG_NET_SWITCHDEV
  	if (skb->offload_l3_fwd_mark) {
  		consume_skb(skb);
  		return 0;
  	}
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
  	if (unlikely(opt->optlen))
  		ip_forward_options(skb);
8203e2d84   Eric Dumazet   net: clear skb->t...
75
  	skb->tstamp = 0;
13206b6bf   Eric W. Biederman   net: Pass net int...
76
  	return dst_output(net, sk, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
  }
  
  int ip_forward(struct sk_buff *skb)
  {
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
81
  	u32 mtu;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  	struct iphdr *iph;	/* Our header */
  	struct rtable *rt;	/* Route we use */
5e73ea1a3   Daniel Baluta   ipv4: fix checkpa...
84
  	struct ip_options *opt	= &(IPCB(skb)->opt);
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
85
  	struct net *net;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86

d4f2fa6ad   Denis Kirjanov   ipv4: ip_forward:...
87
88
89
  	/* that should never happen */
  	if (skb->pkt_type != PACKET_HOST)
  		goto drop;
2ab957492   Sebastian Pöhn   ip_forward: Drop ...
90
91
  	if (unlikely(skb->sk))
  		goto drop;
4497b0763   Ben Hutchings   net: Discard and ...
92
93
  	if (skb_warn_if_lro(skb))
  		goto drop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
  	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...
99
  	skb_forward_csum(skb);
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
100
  	net = dev_net(skb->dev);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
101

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
106
  	/*
  	 *	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...
107
  	if (ip_hdr(skb)->ttl <= 1)
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
108
  		goto too_many_hops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
  
  	if (!xfrm4_route_forward(skb))
  		goto drop;
511c3f92a   Eric Dumazet   net: skb->rtable ...
112
  	rt = skb_rtable(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113

77d5bc7e6   David Ahern   ipv4: Revert remo...
114
  	if (opt->is_strictroute && rt->rt_uses_gateway)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
  		goto sr_failed;
9ee6c5dc8   Lance Richardson   ipv4: allow local...
116
  	IPCB(skb)->flags |= IPSKB_FORWARDED;
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
117
  	mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
cf8262443   Florian Westphal   ip: reject too-bi...
118
  	if (ip_exceeds_mtu(skb, mtu)) {
fcad0ac2d   Eric W. Biederman   ipv4: Compute net...
119
  		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
9af3912ec   John Heffner   [NET] Move DF che...
120
  		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
121
  			  htonl(mtu));
9af3912ec   John Heffner   [NET] Move DF che...
122
123
  		goto drop;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  	/* We are about to mangle packet. Copy it! */
d8d1f30b9   Changli Gao   net-next: remove ...
125
  	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  		goto drop;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
127
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
132
133
134
135
  
  	/* 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...
136
137
  	if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
  	    !skb_sec_path(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
  		ip_rt_send_redirect(skb);
432e05d32   Petr Machata   net: ipv4: Contro...
139
140
  	if (net->ipv4.sysctl_ip_fwd_update_priority)
  		skb->priority = rt_tos2priority(iph->tos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141

29a26a568   Eric W. Biederman   netfilter: Pass s...
142
143
144
  	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
145
146
  
  sr_failed:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
147
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
  	 *	Strict routing permits no gatewaying
  	 */
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
150
151
  	 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
  	 goto drop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
  
  too_many_hops:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
154
  	/* Tell the sender its packet died... */
b45386efa   Eric Dumazet   net: rename IP_IN...
155
  	__IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
156
  	icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
  drop:
  	kfree_skb(skb);
  	return NET_RX_DROP;
  }