1833 lines · 47.5 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* *	Linux NET3:	GRE over IP protocol decoder. * *	Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) */ #define pr_fmt(fmt) KBUILD_MODNAME ": " 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/flow.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 = true;module_param(log_ecn_error, bool, 0644);MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 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){ 	/* All the routers (except for Linux) return only	   8 bytes of packet payload. It means, that precise relaying of	   ICMP in the real Internet is absolutely infeasible. 	   Moreover, Cisco "wise men" put GRE key to the third word	   in GRE header. It makes impossible maintaining even soft	   state for keyed GRE tunnels with enabled checksum. Tell	   them "thank you". 	   Well, I wonder, rfc1812 was written by Cisco employee,	   what the hell these idiots break standards established	   by themselves???	   */	struct net *net = dev_net(skb->dev);	struct ip_tunnel_net *itn;	const struct iphdr *iph;	const int type = icmp_hdr(skb)->type;	const int code = icmp_hdr(skb)->code;	struct ip_tunnel *t; 	if (tpi->proto == htons(ETH_P_TEB))		itn = net_generic(net, gre_tap_net_id);	else if (tpi->proto == htons(ETH_P_ERSPAN) ||		 tpi->proto == htons(ETH_P_ERSPAN2))		itn = net_generic(net, erspan_net_id);	else		itn = net_generic(net, ipgre_net_id); 	iph = (const struct iphdr *)(icmp_hdr(skb) + 1);	t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,			     iph->daddr, iph->saddr, tpi->key); 	if (!t)		return -ENOENT; 	switch (type) {	default:	case ICMP_PARAMETERPROB:		return 0; 	case ICMP_DEST_UNREACH:		switch (code) {		case ICMP_SR_FAILED:		case ICMP_PORT_UNREACH:			/* Impossible event. */			return 0;		default:			/* All others are translated to HOST_UNREACH.			   rfc2003 contains "deep thoughts" about NET_UNREACH,			   I believe they are just ether pollution. --ANK			 */			break;		}		break; 	case ICMP_TIME_EXCEEDED:		if (code != ICMP_EXC_TTL)			return 0;		break; 	case ICMP_REDIRECT:		break;	} #if IS_ENABLED(CONFIG_IPV6)	if (tpi->proto == htons(ETH_P_IPV6)) {		unsigned int data_len = 0; 		if (type == ICMP_TIME_EXCEEDED)			data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ 		if (!ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,						type, data_len))			return 0;	}#endif 	if (t->parms.iph.daddr == 0 ||	    ipv4_is_multicast(t->parms.iph.daddr))		return 0; 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)		return 0; 	if (time_before(jiffies, READ_ONCE(t->err_time) + IPTUNNEL_ERR_TIMEO))		WRITE_ONCE(t->err_count, READ_ONCE(t->err_count) + 1);	else		WRITE_ONCE(t->err_count, 1);	WRITE_ONCE(t->err_time, jiffies); 	return 0;} static void gre_err(struct sk_buff *skb, u32 info){	/* All the routers (except for Linux) return only	 * 8 bytes of packet payload. It means, that precise relaying of	 * ICMP in the real Internet is absolutely infeasible.	 *	 * Moreover, Cisco "wise men" put GRE key to the third word	 * in GRE header. It makes impossible maintaining even soft	 * state for keyed	 * GRE tunnels with enabled checksum. Tell them "thank you".	 *	 * Well, I wonder, rfc1812 was written by Cisco employee,	 * what the hell these idiots break standards established	 * by themselves???	 */ 	const struct iphdr *iph = (struct iphdr *)skb->data;	const int type = icmp_hdr(skb)->type;	const int code = icmp_hdr(skb)->code;	struct tnl_ptk_info tpi; 	if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),			     iph->ihl * 4) < 0)		return; 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {		ipv4_update_pmtu(skb, dev_net(skb->dev), info,				 skb->dev->ifindex, IPPROTO_GRE);		return;	}	if (type == ICMP_REDIRECT) {		ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,			      IPPROTO_GRE);		return;	} 	ipgre_err(skb, info, &tpi);} static bool is_erspan_type1(int gre_hdr_len){	/* Both ERSPAN type I (version 0) and type II (version 1) use	 * protocol 0x88BE, but the type I has only 4-byte GRE header,	 * while type II has 8-byte.	 */	return gre_hdr_len == 4;} static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,		      int gre_hdr_len){	struct net *net = dev_net(skb->dev);	struct metadata_dst *tun_dst = NULL;	struct erspan_base_hdr *ershdr;	IP_TUNNEL_DECLARE_FLAGS(flags);	struct ip_tunnel_net *itn;	struct ip_tunnel *tunnel;	const struct iphdr *iph;	struct erspan_md2 *md2;	int ver;	int len; 	ip_tunnel_flags_copy(flags, tpi->flags); 	itn = net_generic(net, erspan_net_id);	iph = ip_hdr(skb);	if (is_erspan_type1(gre_hdr_len)) {		ver = 0;		__set_bit(IP_TUNNEL_NO_KEY_BIT, flags);		tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,					  iph->saddr, iph->daddr, 0);	} else {		if (unlikely(!pskb_may_pull(skb,					    gre_hdr_len + sizeof(*ershdr))))			return PACKET_REJECT; 		ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);		ver = ershdr->ver;		iph = ip_hdr(skb);		__set_bit(IP_TUNNEL_KEY_BIT, flags);		tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,					  iph->saddr, iph->daddr, tpi->key);	} 	if (tunnel) {		if (is_erspan_type1(gre_hdr_len))			len = gre_hdr_len;		else			len = gre_hdr_len + erspan_hdr_len(ver); 		if (unlikely(!pskb_may_pull(skb, len)))			return PACKET_REJECT; 		if (__iptunnel_pull_header(skb,					   len,					   htons(ETH_P_TEB),					   false, false) < 0)			goto drop; 		if (tunnel->collect_md) {			struct erspan_metadata *pkt_md, *md;			struct ip_tunnel_info *info;			unsigned char *gh;			__be64 tun_id; 			__set_bit(IP_TUNNEL_KEY_BIT, tpi->flags);			ip_tunnel_flags_copy(flags, tpi->flags);			tun_id = key32_to_tunnel_id(tpi->key); 			tun_dst = ip_tun_rx_dst(skb, flags,						tun_id, sizeof(*md));			if (!tun_dst)				return PACKET_REJECT; 			/* MUST set options_len before referencing options */			info = &tun_dst->u.tun_info;			info->options_len = sizeof(*md); 			/* skb can be uncloned in __iptunnel_pull_header, so			 * old pkt_md is no longer valid and we need to reset			 * it			 */			gh = skb_network_header(skb) +			     skb_network_header_len(skb);			pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +							    sizeof(*ershdr));			md = ip_tunnel_info_opts(&tun_dst->u.tun_info);			md->version = ver;			md2 = &md->u.md2;			memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :						       ERSPAN_V2_MDSIZE); 			__set_bit(IP_TUNNEL_ERSPAN_OPT_BIT,				  info->key.tun_flags);		} 		skb_reset_mac_header(skb);		ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);		return PACKET_RCVD;	}	return PACKET_REJECT; drop:	kfree_skb(skb);	return PACKET_RCVD;} 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){	struct metadata_dst *tun_dst = NULL;	const struct iphdr *iph;	struct ip_tunnel *tunnel; 	iph = ip_hdr(skb);	tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,				  iph->saddr, iph->daddr, tpi->key); 	if (tunnel) {		const struct iphdr *tnl_params; 		if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,					   raw_proto, false) < 0)			goto drop; 		/* Special case for ipgre_header_parse(), which expects the		 * mac_header to point to the outer IP header.		 */		if (tunnel->dev->header_ops == &ipgre_header_ops)			skb_pop_mac_header(skb);		else			skb_reset_mac_header(skb); 		tnl_params = &tunnel->parms.iph;		if (tunnel->collect_md || tnl_params->daddr == 0) {			IP_TUNNEL_DECLARE_FLAGS(flags) = { };			__be64 tun_id; 			__set_bit(IP_TUNNEL_CSUM_BIT, flags);			__set_bit(IP_TUNNEL_KEY_BIT, flags);			ip_tunnel_flags_and(flags, tpi->flags, flags); 			tun_id = key32_to_tunnel_id(tpi->key);			tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);			if (!tun_dst)				return PACKET_REJECT;		} 		ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);		return PACKET_RCVD;	}	return PACKET_NEXT; drop:	kfree_skb(skb);	return PACKET_RCVD;} static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,		     int hdr_len){	struct net *net = dev_net(skb->dev);	struct ip_tunnel_net *itn;	int res; 	if (tpi->proto == htons(ETH_P_TEB))		itn = net_generic(net, gre_tap_net_id);	else		itn = net_generic(net, ipgre_net_id); 	res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);	if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {		/* ipgre tunnels in collect metadata mode should receive		 * also ETH_P_TEB traffic.		 */		itn = net_generic(net, ipgre_net_id);		res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);	}	return res;} static int gre_rcv(struct sk_buff *skb){	struct tnl_ptk_info tpi;	bool csum_err = false;	int hdr_len; #ifdef CONFIG_NET_IPGRE_BROADCAST	if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {		/* Looped back packet, drop it! */		if (rt_is_output_route(skb_rtable(skb)))			goto drop;	}#endif 	hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);	if (hdr_len < 0)		goto drop; 	if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||		     tpi.proto == htons(ETH_P_ERSPAN2))) {		if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)			return 0;		goto out;	} 	if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)		return 0; out:	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);drop:	dev_core_stats_rx_dropped_inc(skb->dev);	kfree_skb(skb);	return 0;} static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,		       const struct iphdr *tnl_params,		       __be16 proto, const unsigned long *flags){	struct ip_tunnel *tunnel = netdev_priv(dev); 	/* Push GRE header. */	gre_build_header(skb, tunnel->tun_hlen,			 flags, proto, tunnel->parms.o_key,			 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?			 htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0); 	ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);} static int gre_handle_offloads(struct sk_buff *skb, bool csum){	return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);} static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,			__be16 proto){	struct ip_tunnel *tunnel = netdev_priv(dev);	IP_TUNNEL_DECLARE_FLAGS(flags) = { };

Lines 1–500 of 1833.