xref: /linux/net/ipv4/ip_forward.c (revision 036b9e7caeb09598afb297a6d4fb36b477a4f6b2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		The IP forwarding functionality.
8  *
9  * Authors:	see ip.c
10  *
11  * Fixes:
12  *		Many		:	Split from ip.c , see ip_input.c for
13  *					history.
14  *		Dave Gregorich	:	NULL ip_rt_put fix for multicast
15  *					routing.
16  *		Jos Vos		:	Add call_out_firewall before sending,
17  *					use output device for accounting.
18  *		Jos Vos		:	Call forward firewall after routing
19  *					(always use output device).
20  *		Mike McLagan	:	Routing by source
21  */
22 
23 #include <linux/types.h>
24 #include <linux/mm.h>
25 #include <linux/skbuff.h>
26 #include <linux/ip.h>
27 #include <linux/icmp.h>
28 #include <linux/netdevice.h>
29 #include <linux/slab.h>
30 #include <net/sock.h>
31 #include <net/ip.h>
32 #include <net/tcp.h>
33 #include <net/udp.h>
34 #include <net/icmp.h>
35 #include <linux/tcp.h>
36 #include <linux/udp.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <net/checksum.h>
39 #include <linux/route.h>
40 #include <net/route.h>
41 #include <net/xfrm.h>
42 
43 static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
44 {
45 	if (skb->len <= mtu)
46 		return false;
47 
48 	if (unlikely((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0))
49 		return false;
50 
51 	/* original fragment exceeds mtu and DF is set */
52 	if (unlikely(IPCB(skb)->frag_max_size > mtu))
53 		return true;
54 
55 	if (skb->ignore_df)
56 		return false;
57 
58 	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
59 		return false;
60 
61 	return true;
62 }
63 
64 
65 static int ip_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
66 {
67 	struct ip_options *opt	= &(IPCB(skb)->opt);
68 
69 	__IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
70 	__IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
71 
72 #ifdef CONFIG_NET_SWITCHDEV
73 	if (skb->offload_l3_fwd_mark) {
74 		consume_skb(skb);
75 		return 0;
76 	}
77 #endif
78 
79 	if (unlikely(opt->optlen))
80 		ip_forward_options(skb);
81 
82 	return dst_output(net, sk, skb);
83 }
84 
85 int ip_forward(struct sk_buff *skb)
86 {
87 	u32 mtu;
88 	struct iphdr *iph;	/* Our header */
89 	struct rtable *rt;	/* Route we use */
90 	struct ip_options *opt	= &(IPCB(skb)->opt);
91 	struct net *net;
92 
93 	/* that should never happen */
94 	if (skb->pkt_type != PACKET_HOST)
95 		goto drop;
96 
97 	if (unlikely(skb->sk))
98 		goto drop;
99 
100 	if (skb_warn_if_lro(skb))
101 		goto drop;
102 
103 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb))
104 		goto drop;
105 
106 	if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
107 		return NET_RX_SUCCESS;
108 
109 	skb_forward_csum(skb);
110 	net = dev_net(skb->dev);
111 
112 	/*
113 	 *	According to the RFC, we must first decrease the TTL field. If
114 	 *	that reaches zero, we must reply an ICMP control message telling
115 	 *	that the packet's lifetime expired.
116 	 */
117 	if (ip_hdr(skb)->ttl <= 1)
118 		goto too_many_hops;
119 
120 	if (!xfrm4_route_forward(skb))
121 		goto drop;
122 
123 	rt = skb_rtable(skb);
124 
125 	if (opt->is_strictroute && rt->rt_uses_gateway)
126 		goto sr_failed;
127 
128 	IPCB(skb)->flags |= IPSKB_FORWARDED;
129 	mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
130 	if (ip_exceeds_mtu(skb, mtu)) {
131 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
132 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
133 			  htonl(mtu));
134 		goto drop;
135 	}
136 
137 	/* We are about to mangle packet. Copy it! */
138 	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
139 		goto drop;
140 	iph = ip_hdr(skb);
141 
142 	/* Decrease ttl after skb cow done */
143 	ip_decrease_ttl(iph);
144 
145 	/*
146 	 *	We now generate an ICMP HOST REDIRECT giving the route
147 	 *	we calculated.
148 	 */
149 	if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
150 	    !skb_sec_path(skb))
151 		ip_rt_send_redirect(skb);
152 
153 	if (net->ipv4.sysctl_ip_fwd_update_priority)
154 		skb->priority = rt_tos2priority(iph->tos);
155 
156 	return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
157 		       net, NULL, skb, skb->dev, rt->dst.dev,
158 		       ip_forward_finish);
159 
160 sr_failed:
161 	/*
162 	 *	Strict routing permits no gatewaying
163 	 */
164 	 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
165 	 goto drop;
166 
167 too_many_hops:
168 	/* Tell the sender its packet died... */
169 	__IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
170 	icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
171 drop:
172 	kfree_skb(skb);
173 	return NET_RX_DROP;
174 }
175