linux/net/ipv4/ip_gre.c

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *	Linux NET3:	GRE over IP protocol decoder.
 *
 *	Authors: Alexey Kuznetsov ([email protected])
 */

#define pr_fmt(fmt)

#include <linux/capability.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/if_arp.h>
#include <linux/if_vlan.h>
#include <linux/init.h>
#include <linux/in6.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#include <linux/netfilter_ipv4.h>
#include <linux/etherdevice.h>
#include <linux/if_ether.h>

#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/protocol.h>
#include <net/ip_tunnels.h>
#include <net/arp.h>
#include <net/checksum.h>
#include <net/dsfield.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/rtnetlink.h>
#include <net/gre.h>
#include <net/dst_metadata.h>
#include <net/erspan.h>

/*
   Problems & solutions
   --------------------

   1. The most important issue is detecting local dead loops.
   They would cause complete host lockup in transmit, which
   would be "resolved" by stack overflow or, if queueing is enabled,
   with infinite looping in net_bh.

   We cannot track such dead loops during route installation,
   it is infeasible task. The most general solutions would be
   to keep skb->encapsulation counter (sort of local ttl),
   and silently drop packet when it expires. It is a good
   solution, but it supposes maintaining new variable in ALL
   skb, even if no tunneling is used.

   Current solution: xmit_recursion breaks dead loops. This is a percpu
   counter, since when we enter the first ndo_xmit(), cpu migration is
   forbidden. We force an exit if this counter reaches RECURSION_LIMIT

   2. Networking dead loops would not kill routers, but would really
   kill network. IP hop limit plays role of "t->recursion" in this case,
   if we copy it from packet being encapsulated to upper header.
   It is very good solution, but it introduces two problems:

   - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
     do not work over tunnels.
   - traceroute does not work. I planned to relay ICMP from tunnel,
     so that this problem would be solved and traceroute output
     would even more informative. This idea appeared to be wrong:
     only Linux complies to rfc1812 now (yes, guys, Linux is the only
     true router now :-)), all routers (at least, in neighbourhood of mine)
     return only 8 bytes of payload. It is the end.

   Hence, if we want that OSPF worked or traceroute said something reasonable,
   we should search for another solution.

   One of them is to parse packet trying to detect inner encapsulation
   made by our node. It is difficult or even impossible, especially,
   taking into account fragmentation. TO be short, ttl is not solution at all.

   Current solution: The solution was UNEXPECTEDLY SIMPLE.
   We force DF flag on tunnels with preconfigured hop limit,
   that is ALL. :-) Well, it does not remove the problem completely,
   but exponential growth of network traffic is changed to linear
   (branches, that exceed pmtu are pruned) and tunnel mtu
   rapidly degrades to value <68, where looping stops.
   Yes, it is not good if there exists a router in the loop,
   which does not force DF, even when encapsulating packets have DF set.
   But it is not our problem! Nobody could accuse us, we made
   all that we could make. Even if it is your gated who injected
   fatal route to network, even if it were you who configured
   fatal static route: you are innocent. :-)

   Alexey Kuznetsov.
 */

static bool log_ecn_error =;
module_param(log_ecn_error, bool, 0644);
MODULE_PARM_DESC();

static struct rtnl_link_ops ipgre_link_ops __read_mostly;
static const struct header_ops ipgre_header_ops;

static int ipgre_tunnel_init(struct net_device *dev);
static void erspan_build_header(struct sk_buff *skb,
				u32 id, u32 index,
				bool truncate, bool is_ipv4);

static unsigned int ipgre_net_id __read_mostly;
static unsigned int gre_tap_net_id __read_mostly;
static unsigned int erspan_net_id __read_mostly;

static int ipgre_err(struct sk_buff *skb, u32 info,
		     const struct tnl_ptk_info *tpi)
{}

static void gre_err(struct sk_buff *skb, u32 info)
{}

static bool is_erspan_type1(int gre_hdr_len)
{}

static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
		      int gre_hdr_len)
{}

static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
		       struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
{}

static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
		     int hdr_len)
{}

static int gre_rcv(struct sk_buff *skb)
{}

static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
		       const struct iphdr *tnl_params,
		       __be16 proto)
{}

static int gre_handle_offloads(struct sk_buff *skb, bool csum)
{}

static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
			__be16 proto)
{}

static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
{}

static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
{}

static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
			      struct net_device *dev)
{}

static netdev_tx_t erspan_xmit(struct sk_buff *skb,
			       struct net_device *dev)
{}

static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
				struct net_device *dev)
{}

static void ipgre_link_update(struct net_device *dev, bool set_mtu)
{}

static int ipgre_tunnel_ctl(struct net_device *dev,
			    struct ip_tunnel_parm_kern *p,
			    int cmd)
{}

/* Nice toy. Unfortunately, useless in real life :-)
   It allows to construct virtual multiprotocol broadcast "LAN"
   over the Internet, provided multicast routing is tuned.


   I have no idea was this bicycle invented before me,
   so that I had to set ARPHRD_IPGRE to a random value.
   I have an impression, that Cisco could make something similar,
   but this feature is apparently missing in IOS<=11.2(8).

   I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)

   ping -t 255 224.66.66.66

   If nobody answers, mbone does not work.

   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
   ip addr add 10.66.66.<somewhat>/24 dev Universe
   ifconfig Universe up
   ifconfig Universe add fe80::<Your_real_addr>/10
   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
   ftp 10.66.66.66
   ...
   ftp fec0:6666:6666::193.233.7.65
   ...
 */
static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
			unsigned short type,
			const void *daddr, const void *saddr, unsigned int len)
{}

static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{}

static const struct header_ops ipgre_header_ops =;

#ifdef CONFIG_NET_IPGRE_BROADCAST
static int ipgre_open(struct net_device *dev)
{}

static int ipgre_close(struct net_device *dev)
{}
#endif

static const struct net_device_ops ipgre_netdev_ops =;

#define GRE_FEATURES

static void ipgre_tunnel_setup(struct net_device *dev)
{}

static void __gre_tunnel_init(struct net_device *dev)
{}

static int ipgre_tunnel_init(struct net_device *dev)
{}

static const struct gre_protocol ipgre_protocol =;

static int __net_init ipgre_init_net(struct net *net)
{}

static void __net_exit ipgre_exit_batch_rtnl(struct list_head *list_net,
					     struct list_head *dev_to_kill)
{}

static struct pernet_operations ipgre_net_ops =;

static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
				 struct netlink_ext_ack *extack)
{}

static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
			      struct netlink_ext_ack *extack)
{}

static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
			   struct netlink_ext_ack *extack)
{}

static int ipgre_netlink_parms(struct net_device *dev,
				struct nlattr *data[],
				struct nlattr *tb[],
				struct ip_tunnel_parm_kern *parms,
				__u32 *fwmark)
{}

static int erspan_netlink_parms(struct net_device *dev,
				struct nlattr *data[],
				struct nlattr *tb[],
				struct ip_tunnel_parm_kern *parms,
				__u32 *fwmark)
{}

/* This function returns true when ENCAP attributes are present in the nl msg */
static bool ipgre_netlink_encap_parms(struct nlattr *data[],
				      struct ip_tunnel_encap *ipencap)
{}

static int gre_tap_init(struct net_device *dev)
{}

static const struct net_device_ops gre_tap_netdev_ops =;

static int erspan_tunnel_init(struct net_device *dev)
{}

static const struct net_device_ops erspan_netdev_ops =;

static void ipgre_tap_setup(struct net_device *dev)
{}

static int
ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
{}

static int ipgre_newlink(struct net *src_net, struct net_device *dev,
			 struct nlattr *tb[], struct nlattr *data[],
			 struct netlink_ext_ack *extack)
{}

static int erspan_newlink(struct net *src_net, struct net_device *dev,
			  struct nlattr *tb[], struct nlattr *data[],
			  struct netlink_ext_ack *extack)
{}

static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
			    struct nlattr *data[],
			    struct netlink_ext_ack *extack)
{}

static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
			     struct nlattr *data[],
			     struct netlink_ext_ack *extack)
{}

static size_t ipgre_get_size(const struct net_device *dev)
{}

static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
{}

static int erspan_fill_info(struct sk_buff *skb, const struct net_device *dev)
{}

static void erspan_setup(struct net_device *dev)
{}

static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] =;

static struct rtnl_link_ops ipgre_link_ops __read_mostly =;

static struct rtnl_link_ops ipgre_tap_ops __read_mostly =;

static struct rtnl_link_ops erspan_link_ops __read_mostly =;

struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
					u8 name_assign_type)
{}
EXPORT_SYMBOL_GPL();

static int __net_init ipgre_tap_init_net(struct net *net)
{}

static void __net_exit ipgre_tap_exit_batch_rtnl(struct list_head *list_net,
						 struct list_head *dev_to_kill)
{}

static struct pernet_operations ipgre_tap_net_ops =;

static int __net_init erspan_init_net(struct net *net)
{}

static void __net_exit erspan_exit_batch_rtnl(struct list_head *net_list,
					      struct list_head *dev_to_kill)
{}

static struct pernet_operations erspan_net_ops =;

static int __init ipgre_init(void)
{}

static void __exit ipgre_fini(void)
{}

module_init();
module_exit(ipgre_fini);
MODULE_DESCRIPTION();
MODULE_LICENSE();
MODULE_ALIAS_RTNL_LINK();
MODULE_ALIAS_RTNL_LINK();
MODULE_ALIAS_RTNL_LINK();
MODULE_ALIAS_NETDEV();
MODULE_ALIAS_NETDEV();
MODULE_ALIAS_NETDEV();