1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * The IP forwarding functionality.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Authors: see ip.c
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * Fixes:
121da177e4SLinus Torvalds * Many : Split from ip.c , see ip_input.c for
131da177e4SLinus Torvalds * history.
141da177e4SLinus Torvalds * Dave Gregorich : NULL ip_rt_put fix for multicast
151da177e4SLinus Torvalds * routing.
161da177e4SLinus Torvalds * Jos Vos : Add call_out_firewall before sending,
171da177e4SLinus Torvalds * use output device for accounting.
181da177e4SLinus Torvalds * Jos Vos : Call forward firewall after routing
191da177e4SLinus Torvalds * (always use output device).
201da177e4SLinus Torvalds * Mike McLagan : Routing by source
211da177e4SLinus Torvalds */
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds #include <linux/types.h>
241da177e4SLinus Torvalds #include <linux/mm.h>
251da177e4SLinus Torvalds #include <linux/skbuff.h>
261da177e4SLinus Torvalds #include <linux/ip.h>
271da177e4SLinus Torvalds #include <linux/icmp.h>
281da177e4SLinus Torvalds #include <linux/netdevice.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
301da177e4SLinus Torvalds #include <net/sock.h>
311da177e4SLinus Torvalds #include <net/ip.h>
321da177e4SLinus Torvalds #include <net/tcp.h>
331da177e4SLinus Torvalds #include <net/udp.h>
341da177e4SLinus Torvalds #include <net/icmp.h>
351da177e4SLinus Torvalds #include <linux/tcp.h>
361da177e4SLinus Torvalds #include <linux/udp.h>
371da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
381da177e4SLinus Torvalds #include <net/checksum.h>
391da177e4SLinus Torvalds #include <linux/route.h>
401da177e4SLinus Torvalds #include <net/route.h>
411da177e4SLinus Torvalds #include <net/xfrm.h>
421da177e4SLinus Torvalds
ip_exceeds_mtu(const struct sk_buff * skb,unsigned int mtu)43fe6cc55fSFlorian Westphal static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
44fe6cc55fSFlorian Westphal {
45ca6c5d4aSFlorian Westphal if (skb->len <= mtu)
46fe6cc55fSFlorian Westphal return false;
47fe6cc55fSFlorian Westphal
48cf826244SFlorian Westphal if (unlikely((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0))
49cf826244SFlorian Westphal return false;
50cf826244SFlorian Westphal
51cf826244SFlorian Westphal /* original fragment exceeds mtu and DF is set */
52cf826244SFlorian Westphal if (unlikely(IPCB(skb)->frag_max_size > mtu))
53cf826244SFlorian Westphal return true;
54cf826244SFlorian Westphal
55cf826244SFlorian Westphal if (skb->ignore_df)
56cf826244SFlorian Westphal return false;
57cf826244SFlorian Westphal
58779b7931SDaniel Axtens if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
59fe6cc55fSFlorian Westphal return false;
60fe6cc55fSFlorian Westphal
61fe6cc55fSFlorian Westphal return true;
62fe6cc55fSFlorian Westphal }
63fe6cc55fSFlorian Westphal
64fe6cc55fSFlorian Westphal
ip_forward_finish(struct net * net,struct sock * sk,struct sk_buff * skb)650c4b51f0SEric W. Biederman static int ip_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt);
681da177e4SLinus Torvalds
69f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV
70f839a6c9SIdo Schimmel if (skb->offload_l3_fwd_mark) {
71f839a6c9SIdo Schimmel consume_skb(skb);
72f839a6c9SIdo Schimmel return 0;
73f839a6c9SIdo Schimmel }
74f839a6c9SIdo Schimmel #endif
75f839a6c9SIdo Schimmel
761da177e4SLinus Torvalds if (unlikely(opt->optlen))
771da177e4SLinus Torvalds ip_forward_options(skb);
781da177e4SLinus Torvalds
79de799101SMartin KaFai Lau skb_clear_tstamp(skb);
8013206b6bSEric W. Biederman return dst_output(net, sk, skb);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds
ip_forward(struct sk_buff * skb)831da177e4SLinus Torvalds int ip_forward(struct sk_buff *skb)
841da177e4SLinus Torvalds {
85f87c10a8SHannes Frederic Sowa u32 mtu;
861da177e4SLinus Torvalds struct iphdr *iph; /* Our header */
871da177e4SLinus Torvalds struct rtable *rt; /* Route we use */
881da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt);
89fcad0ac2SEric W. Biederman struct net *net;
902edc1a38SMenglong Dong SKB_DR(reason);
911da177e4SLinus Torvalds
92d4f2fa6aSDenis Kirjanov /* that should never happen */
93d4f2fa6aSDenis Kirjanov if (skb->pkt_type != PACKET_HOST)
94d4f2fa6aSDenis Kirjanov goto drop;
95d4f2fa6aSDenis Kirjanov
962ab95749SSebastian Pöhn if (unlikely(skb->sk))
972ab95749SSebastian Pöhn goto drop;
982ab95749SSebastian Pöhn
994497b076SBen Hutchings if (skb_warn_if_lro(skb))
1004497b076SBen Hutchings goto drop;
1014497b076SBen Hutchings
1022edc1a38SMenglong Dong if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
1032edc1a38SMenglong Dong SKB_DR_SET(reason, XFRM_POLICY);
1041da177e4SLinus Torvalds goto drop;
1052edc1a38SMenglong Dong }
1061da177e4SLinus Torvalds
1071da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
1081da177e4SLinus Torvalds return NET_RX_SUCCESS;
1091da177e4SLinus Torvalds
11035fc92a9SHerbert Xu skb_forward_csum(skb);
111fcad0ac2SEric W. Biederman net = dev_net(skb->dev);
1121da177e4SLinus Torvalds
1131da177e4SLinus Torvalds /*
1141da177e4SLinus Torvalds * According to the RFC, we must first decrease the TTL field. If
1151da177e4SLinus Torvalds * that reaches zero, we must reply an ICMP control message telling
1161da177e4SLinus Torvalds * that the packet's lifetime expired.
1171da177e4SLinus Torvalds */
118eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl <= 1)
1191da177e4SLinus Torvalds goto too_many_hops;
1201da177e4SLinus Torvalds
1212edc1a38SMenglong Dong if (!xfrm4_route_forward(skb)) {
1222edc1a38SMenglong Dong SKB_DR_SET(reason, XFRM_POLICY);
1231da177e4SLinus Torvalds goto drop;
1242edc1a38SMenglong Dong }
1251da177e4SLinus Torvalds
126511c3f92SEric Dumazet rt = skb_rtable(skb);
1271da177e4SLinus Torvalds
12877d5bc7eSDavid Ahern if (opt->is_strictroute && rt->rt_uses_gateway)
1291da177e4SLinus Torvalds goto sr_failed;
1301da177e4SLinus Torvalds
131*cf8b49fbSHeng Guo __IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
132*cf8b49fbSHeng Guo
1339ee6c5dcSLance Richardson IPCB(skb)->flags |= IPSKB_FORWARDED;
134f87c10a8SHannes Frederic Sowa mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
135cf826244SFlorian Westphal if (ip_exceeds_mtu(skb, mtu)) {
136fcad0ac2SEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1379af3912eSJohn Heffner icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
138f87c10a8SHannes Frederic Sowa htonl(mtu));
1392edc1a38SMenglong Dong SKB_DR_SET(reason, PKT_TOO_BIG);
1409af3912eSJohn Heffner goto drop;
1419af3912eSJohn Heffner }
1429af3912eSJohn Heffner
1431da177e4SLinus Torvalds /* We are about to mangle packet. Copy it! */
144d8d1f30bSChangli Gao if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
1451da177e4SLinus Torvalds goto drop;
146eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb);
1471da177e4SLinus Torvalds
1481da177e4SLinus Torvalds /* Decrease ttl after skb cow done */
1491da177e4SLinus Torvalds ip_decrease_ttl(iph);
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds * We now generate an ICMP HOST REDIRECT giving the route
1531da177e4SLinus Torvalds * we calculated.
1541da177e4SLinus Torvalds */
155df4d9254SHannes Frederic Sowa if (IPCB(skb)->flags & IPSKB_DOREDIRECT && !opt->srr &&
156df4d9254SHannes Frederic Sowa !skb_sec_path(skb))
1571da177e4SLinus Torvalds ip_rt_send_redirect(skb);
1581da177e4SLinus Torvalds
1597bf9e18dSKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_ip_fwd_update_priority))
1601da177e4SLinus Torvalds skb->priority = rt_tos2priority(iph->tos);
1611da177e4SLinus Torvalds
16229a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
16329a26a56SEric W. Biederman net, NULL, skb, skb->dev, rt->dst.dev,
16429a26a56SEric W. Biederman ip_forward_finish);
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds sr_failed:
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds * Strict routing permits no gatewaying
1691da177e4SLinus Torvalds */
1701da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
1711da177e4SLinus Torvalds goto drop;
1721da177e4SLinus Torvalds
1731da177e4SLinus Torvalds too_many_hops:
1741da177e4SLinus Torvalds /* Tell the sender its packet died... */
175b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
1761da177e4SLinus Torvalds icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
1772edc1a38SMenglong Dong SKB_DR_SET(reason, IP_INHDR);
1781da177e4SLinus Torvalds drop:
1792edc1a38SMenglong Dong kfree_skb_reason(skb, reason);
1801da177e4SLinus Torvalds return NET_RX_DROP;
1811da177e4SLinus Torvalds }
182