Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *	IPv6 tunneling device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *	Linux INET6 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *	Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *	Yasuyuki Kozakai	<kozakai@linux-ipv6.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *      Based on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *      RFC 2473
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/netfilter_ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <net/ip6_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include <net/dsfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) MODULE_AUTHOR("Ville Nuorvala");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) MODULE_DESCRIPTION("IPv6 tunneling device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) MODULE_ALIAS_RTNL_LINK("ip6tnl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) MODULE_ALIAS_NETDEV("ip6tnl0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define IP6_TUNNEL_HASH_SIZE_SHIFT  5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static int ip6_tnl_dev_init(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) static void ip6_tnl_dev_setup(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) static struct rtnl_link_ops ip6_link_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) static unsigned int ip6_tnl_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) struct ip6_tnl_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	/* the IPv6 tunnel fallback device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	struct net_device *fb_tnl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	/* lists for storing tunnels in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct ip6_tnl __rcu *tnls_wc[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct ip6_tnl __rcu **tnls[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct ip6_tnl __rcu *collect_md_tun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static inline int ip6_tnl_mpls_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	return IS_ENABLED(CONFIG_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) static struct net_device_stats *ip6_get_stats(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct pcpu_sw_netstats tmp, sum = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		unsigned int start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		const struct pcpu_sw_netstats *tstats =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 						   per_cpu_ptr(dev->tstats, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			tmp.rx_packets = tstats->rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			tmp.rx_bytes = tstats->rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 			tmp.tx_packets = tstats->tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 			tmp.tx_bytes =  tstats->tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		sum.rx_packets += tmp.rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		sum.rx_bytes   += tmp.rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		sum.tx_packets += tmp.tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		sum.tx_bytes   += tmp.tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	dev->stats.rx_packets = sum.rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	dev->stats.rx_bytes   = sum.rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	dev->stats.tx_packets = sum.tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	dev->stats.tx_bytes   = sum.tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	return &dev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) #define for_each_ip6_tunnel_rcu(start) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  *   @net: network namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  *   @link: ifindex of underlying interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  *   @remote: the address of the tunnel exit-point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  *   @local: the address of the tunnel entry-point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  *   tunnel matching given end-points if found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *   else fallback tunnel if its device is up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  *   else %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static struct ip6_tnl *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) ip6_tnl_lookup(struct net *net, int link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	       const struct in6_addr *remote, const struct in6_addr *local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	unsigned int hash = HASH(remote, local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	struct ip6_tnl *t, *cand = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct in6_addr any;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		    !ipv6_addr_equal(remote, &t->parms.raddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		    !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		if (link == t->parms.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	memset(&any, 0, sizeof(any));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	hash = HASH(&any, local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		    !ipv6_addr_any(&t->parms.raddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		    !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		if (link == t->parms.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		else if (!cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	hash = HASH(remote, &any);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		    !ipv6_addr_any(&t->parms.laddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		    !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		if (link == t->parms.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		else if (!cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	if (cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		return cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	t = rcu_dereference(ip6n->collect_md_tun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	if (t && t->dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	t = rcu_dereference(ip6n->tnls_wc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (t && (t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * ip6_tnl_bucket - get head of list matching given tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  *   @p: parameters containing tunnel end-points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  *   ip6_tnl_bucket() returns the head of the list matching the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  *   &struct in6_addr entries laddr and raddr in @p.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * Return: head of IPv6 tunnel list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) static struct ip6_tnl __rcu **
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	const struct in6_addr *remote = &p->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	const struct in6_addr *local = &p->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	unsigned int h = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	int prio = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		prio = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		h = HASH(remote, local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return &ip6n->tnls[prio][h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * ip6_tnl_link - add tunnel to hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  *   @t: tunnel to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (t->parms.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		rcu_assign_pointer(ip6n->collect_md_tun, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	rcu_assign_pointer(t->next , rtnl_dereference(*tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	rcu_assign_pointer(*tp, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  * ip6_tnl_unlink - remove tunnel from hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  *   @t: tunnel to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	struct ip6_tnl __rcu **tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct ip6_tnl *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (t->parms.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		rcu_assign_pointer(ip6n->collect_md_tun, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	for (tp = ip6_tnl_bucket(ip6n, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	     (iter = rtnl_dereference(*tp)) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	     tp = &iter->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		if (t == iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			rcu_assign_pointer(*tp, t->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) static void ip6_dev_free(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	gro_cells_destroy(&t->gro_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	dst_cache_destroy(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) static int ip6_tnl_create2(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	dev->rtnl_link_ops = &ip6_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	strcpy(t->parms.name, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	ip6_tnl_link(ip6n, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  * ip6_tnl_create - create a new tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  *   @net: network namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  *   @p: tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  *   Create tunnel matching given parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  *   created tunnel or error pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	struct ip6_tnl *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	int err = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (p->name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		if (!dev_valid_name(p->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		strlcpy(name, p->name, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		sprintf(name, "ip6tnl%%d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 			   ip6_tnl_dev_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	dev_net_set(dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	t->parms = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	t->net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	err = ip6_tnl_create2(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		goto failed_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) failed_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * ip6_tnl_locate - find or create tunnel matching given parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  *   @net: network namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  *   @p: tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  *   @create: != 0 if allowed to create new tunnel if no match found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  *   ip6_tnl_locate() first tries to locate an existing tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  *   based on @parms. If this is unsuccessful, but @create is set a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  *   tunnel device is created and registered for use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  *   matching tunnel or error pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static struct ip6_tnl *ip6_tnl_locate(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		struct __ip6_tnl_parm *p, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	const struct in6_addr *remote = &p->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	const struct in6_addr *local = &p->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct ip6_tnl __rcu **tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct ip6_tnl *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	for (tp = ip6_tnl_bucket(ip6n, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	     (t = rtnl_dereference(*tp)) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	     tp = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		    ipv6_addr_equal(remote, &t->parms.raddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		    p->link == t->parms.link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			if (create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 				return ERR_PTR(-EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	if (!create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	return ip6_tnl_create(net, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * ip6_tnl_dev_uninit - tunnel device uninitializer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  *   @dev: the device to be destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  *   ip6_tnl_dev_uninit() removes tunnel from its list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) ip6_tnl_dev_uninit(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (dev == ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		ip6_tnl_unlink(ip6n, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  *   @skb: received socket buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  *   0 if none was found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  *   else index to encapsulation limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	unsigned int nhoff = raw - skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	unsigned int off = nhoff + sizeof(*ipv6h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	u8 next, nexthdr = ipv6h->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		struct ipv6_opt_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		u16 optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		if (!pskb_may_pull(skb, off + sizeof(*hdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		hdr = (struct ipv6_opt_hdr *)(skb->data + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		if (nexthdr == NEXTHDR_FRAGMENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			if (frag_hdr->frag_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			optlen = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		} else if (nexthdr == NEXTHDR_AUTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			optlen = ipv6_authlen(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			optlen = ipv6_optlen(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		/* cache hdr->nexthdr, since pskb_may_pull() might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		 * invalidate hdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		next = hdr->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		if (nexthdr == NEXTHDR_DEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 			u16 i = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			/* Remember : hdr is no longer valid at this point. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			if (!pskb_may_pull(skb, off + optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 				struct ipv6_tlv_tnl_enc_lim *tel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 				/* No more room for encapsulation limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 				if (i + sizeof(*tel) > optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 				tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 				/* return index of option if found and valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 				if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 				    tel->length == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 					return i + off - nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 				/* else jump to next option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 				if (tel->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 					i += tel->length + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 					i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		nexthdr = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		off += optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * ip6_tnl_err - tunnel error handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  *   ip6_tnl_err() should handle errors in the tunnel according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  *   to the specifications in RFC 2473.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	    u8 *type, u8 *code, int *msg, __u32 *info, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	u8 rel_type = ICMPV6_DEST_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	u8 rel_code = ICMPV6_ADDR_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	__u32 rel_info = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	struct ip6_tnl *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	int rel_msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	u8 tproto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	__u16 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	/* If the packet doesn't contain the original IPv6 header we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	   in trouble since we might need the source address for further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	   processing of the error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->daddr, &ipv6h->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	tproto = READ_ONCE(t->parms.proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	if (tproto != ipproto && tproto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	switch (*type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	case ICMPV6_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 				    t->parms.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		rel_msg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	case ICMPV6_TIME_EXCEED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		if ((*code) == ICMPV6_EXC_HOPLIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 					    t->parms.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			rel_msg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	case ICMPV6_PARAMPROB: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		struct ipv6_tlv_tnl_enc_lim *tel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		__u32 teli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		teli = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		if ((*code) == ICMPV6_HDR_FIELD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (teli && teli == *info - 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			if (tel->encap_limit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 						    t->parms.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 				rel_msg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 					    t->parms.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	case ICMPV6_PKT_TOOBIG: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		__u32 mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 				sock_net_uid(net, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		mtu = *info - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		if (mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		if (len > mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			rel_type = ICMPV6_PKT_TOOBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 			rel_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			rel_info = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			rel_msg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	case NDISC_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			     sock_net_uid(net, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	*type = rel_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	*code = rel_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	*info = rel_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	*msg = rel_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	   u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	__u32 rel_info = ntohl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	const struct iphdr *eiph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct sk_buff *skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	int err, rel_msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	u8 rel_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	u8 rel_code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			  &rel_msg, &rel_info, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (rel_msg == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	switch (rel_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	case ICMPV6_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (rel_code != ICMPV6_ADDR_UNREACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		rel_type = ICMP_DEST_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		rel_code = ICMP_HOST_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	case ICMPV6_PKT_TOOBIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		if (rel_code != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		rel_type = ICMP_DEST_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		rel_code = ICMP_FRAG_NEEDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (!skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	skb_dst_drop(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	skb_pull(skb2, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	skb_reset_network_header(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	eiph = ip_hdr(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	/* Try to guess incoming interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 				   0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	skb2->dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	/* route "incoming" packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (rt->rt_flags & RTCF_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 					   eiph->daddr, eiph->saddr, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					   IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			if (!IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 				ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		skb_dst_set(skb2, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 				   skb2->dev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		    skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	/* change mtu on this route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		if (rel_info > dst_mtu(skb_dst(skb2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		skb_dst_update_pmtu_no_confirm(skb2, rel_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	kfree_skb(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	   u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	__u32 rel_info = ntohl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	int err, rel_msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	u8 rel_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	u8 rel_code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			  &rel_msg, &rel_info, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		if (!skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		skb_dst_drop(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		skb_pull(skb2, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		skb_reset_network_header(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		/* Try to guess incoming interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 				NULL, 0, skb2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		if (rt && rt->dst.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			skb2->dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		icmpv6_send(skb2, rel_type, rel_code, rel_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		kfree_skb(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) mplsip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	    u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	__u32 rel_info = ntohl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	int err, rel_msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	u8 rel_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	u8 rel_code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	err = ip6_tnl_err(skb, IPPROTO_MPLS, opt, &rel_type, &rel_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			  &rel_msg, &rel_info, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 				       const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 				       struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	__u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	return IP6_ECN_decapsulate(ipv6h, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 				       const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				       struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	return IP6_ECN_decapsulate(ipv6h, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) static inline int mplsip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 					       const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 					       struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	/* ECN is not supported in AF_MPLS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			     const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			     const struct in6_addr *raddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	struct __ip6_tnl_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	int ltype = ipv6_addr_type(laddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	int rtype = ipv6_addr_type(raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	__u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		flags = IP6_TNL_F_CAP_PER_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	} else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		   rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		   !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		   (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		if (ltype&IPV6_ADDR_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			flags |= IP6_TNL_F_CAP_XMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		if (rtype&IPV6_ADDR_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			flags |= IP6_TNL_F_CAP_RCV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) EXPORT_SYMBOL(ip6_tnl_get_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) /* called with rcu_read_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 				  const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 				  const struct in6_addr *raddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct __ip6_tnl_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if ((p->flags & IP6_TNL_F_CAP_RCV) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		struct net_device *ldev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		if (p->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			ldev = dev_get_by_index_rcu(net, p->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if ((ipv6_addr_is_multicast(laddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		     likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 						    0, IFA_F_TENTATIVE))) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		    ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		     likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 						     0, IFA_F_TENTATIVE))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			 const struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			 struct metadata_dst *tun_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 						const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 						struct sk_buff *skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			 bool log_ecn_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	struct pcpu_sw_netstats *tstats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if ((!(tpi->flags & TUNNEL_CSUM) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	     (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	    ((tpi->flags & TUNNEL_CSUM) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	     !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		tunnel->dev->stats.rx_crc_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (tunnel->parms.i_flags & TUNNEL_SEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		if (!(tpi->flags & TUNNEL_SEQ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		    (tunnel->i_seqno &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		     (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			tunnel->dev->stats.rx_fifo_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		tunnel->i_seqno = ntohl(tpi->seq) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	skb->protocol = tpi->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	/* Warning: All skb pointers will be invalidated! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (tunnel->dev->type == ARPHRD_ETHER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		if (!pskb_may_pull(skb, ETH_HLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			tunnel->dev->stats.rx_length_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		skb->protocol = eth_type_trans(skb, tunnel->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		skb->dev = tunnel->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	__skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (log_ecn_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 					     &ipv6h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 					     ipv6_get_dsfield(ipv6h));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		if (err > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			++tunnel->dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			++tunnel->dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	tstats = this_cpu_ptr(tunnel->dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	u64_stats_update_begin(&tstats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	tstats->rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	tstats->rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	u64_stats_update_end(&tstats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		skb_dst_set(skb, (struct dst_entry *)tun_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	gro_cells_receive(&tunnel->gro_cells, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		dst_release((struct dst_entry *)tun_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		const struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		struct metadata_dst *tun_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		bool log_ecn_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				    const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 				    struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	dscp_ecn_decapsulate = ip6ip6_dscp_ecn_decapsulate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (tpi->proto == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		dscp_ecn_decapsulate = ip4ip6_dscp_ecn_decapsulate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			     log_ecn_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) EXPORT_SYMBOL(ip6_tnl_rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) static const struct tnl_ptk_info tpi_v6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	/* no tunnel info required for ipxip6. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	.proto = htons(ETH_P_IPV6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) static const struct tnl_ptk_info tpi_v4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	/* no tunnel info required for ipxip6. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	.proto = htons(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static const struct tnl_ptk_info tpi_mpls = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	/* no tunnel info required for mplsip6. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	.proto = htons(ETH_P_MPLS_UC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		      const struct tnl_ptk_info *tpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		      int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 						  const struct ipv6hdr *ipv6h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 						  struct sk_buff *skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	struct ip6_tnl *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct metadata_dst *tun_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->saddr, &ipv6h->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		u8 tproto = READ_ONCE(t->parms.proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		if (tproto != ipproto && tproto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		if (iptunnel_pull_header(skb, 0, tpi->proto, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		if (t->parms.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			if (!tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 				goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 				    log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static int ip4ip6_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			  ip4ip6_dscp_ecn_decapsulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) static int ip6ip6_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			  ip6ip6_dscp_ecn_decapsulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) static int mplsip6_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	return ipxip6_rcv(skb, IPPROTO_MPLS, &tpi_mpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			  mplsip6_dscp_ecn_decapsulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) struct ipv6_tel_txoption {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	struct ipv6_txoptions ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	__u8 dst_opt[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	memset(opt, 0, sizeof(struct ipv6_tel_txoption));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	opt->dst_opt[3] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	opt->dst_opt[4] = encap_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	opt->dst_opt[5] = IPV6_TLV_PADN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	opt->dst_opt[6] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	opt->ops.opt_nflen = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  *   @t: the outgoing tunnel device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  *   @hdr: IPv6 header from the incoming packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  *   Avoid trivial tunneling loop by checking that tunnel exit-point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  *   doesn't match source of incoming packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  *   1 if conflict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)  *   0 else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) static inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		     const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		     const struct in6_addr *raddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct __ip6_tnl_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (t->parms.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		struct net_device *ldev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		if (p->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			ldev = dev_get_by_index_rcu(net, p->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 						      0, IFA_F_TENTATIVE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			pr_warn_ratelimited("%s xmit: Local address not yet configured!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 					    p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			 !ipv6_addr_is_multicast(raddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 							  true, 0, IFA_F_TENTATIVE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			pr_warn_ratelimited("%s xmit: Routing loop! Remote address found on this node!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 					    p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)  * ip6_tnl_xmit - encapsulate packet and send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)  *   @skb: the outgoing socket buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)  *   @dev: the outgoing tunnel device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)  *   @dsfield: dscp code for outer header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)  *   @fl6: flow of tunneled packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)  *   @encap_limit: encapsulation limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)  *   @pmtu: Path MTU is stored if packet is too big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  *   @proto: next header value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  *   Build new header and do some sanity checks on the packet before sending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  *   it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  *   0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  *   -1 fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  *   %-EMSGSIZE message too big. return mtu in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		 __u8 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	struct net_device_stats *stats = &t->dev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct ipv6hdr *ipv6h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct ipv6_tel_txoption opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	struct dst_entry *dst = NULL, *ndst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct net_device *tdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	unsigned int max_headroom = psh_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	bool use_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	u8 hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (t->parms.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		hop_limit = skb_tunnel_info(skb)->key.ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		goto route_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		hop_limit = t->parms.hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	/* NBMA tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (ipv6_addr_any(&t->parms.raddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		if (skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			struct in6_addr *addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 			struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			if (!skb_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			neigh = dst_neigh_lookup(skb_dst(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 						 &ipv6_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			if (!neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 				goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			addr6 = (struct in6_addr *)&neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 			addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			if (addr_type == IPV6_ADDR_ANY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 				addr6 = &ipv6_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	} else if (t->parms.proto != 0 && !(t->parms.flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 					    (IP6_TNL_F_USE_ORIG_TCLASS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 					     IP6_TNL_F_USE_ORIG_FWMARK))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		/* enable the cache only if neither the outer protocol nor the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		 * routing decision depends on the current inner header value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		use_cache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		dst = dst_cache_get(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) route_lookup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		/* add dsfield to flowlabel for route lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		dst = ip6_route_output(net, NULL, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		if (dst->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		if (IS_ERR(dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			err = PTR_ERR(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		    ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 				       &fl6->daddr, 0, &fl6->saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			goto tx_err_link_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		ndst = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	tdev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (tdev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		stats->collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		net_warn_ratelimited("%s: Local routing loop detected!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 				     t->parms.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		goto tx_err_dst_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (encap_limit >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		max_headroom += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		mtu -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		       IPV6_MIN_MTU : IPV4_MIN_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	skb_dst_update_pmtu_no_confirm(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		*pmtu = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		goto tx_err_dst_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	if (t->err_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		if (time_before(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 				t->err_time + IP6TUNNEL_ERR_TIMEO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			t->err_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			t->err_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 * Okay, now see if we can stuff it in the buffer as-is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	max_headroom += LL_RESERVED_SPACE(tdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		struct sk_buff *new_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		new_skb = skb_realloc_headroom(skb, max_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		if (!new_skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			goto tx_err_dst_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		if (skb->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			skb_set_owner_w(new_skb, skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		skb = new_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (t->parms.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		if (t->encap.type != TUNNEL_ENCAP_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			goto tx_err_dst_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		if (use_cache && ndst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (hop_limit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			hop_limit = ip_hdr(skb)->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		else if (skb->protocol == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			hop_limit = ipv6_hdr(skb)->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			hop_limit = ip6_dst_hoplimit(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	/* Calculate max headroom for all the headers and adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	 * needed_headroom if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			+ dst->header_len + t->hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	if (max_headroom > dev->needed_headroom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		dev->needed_headroom = max_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	err = ip6_tnl_encap(skb, t, &proto, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (encap_limit >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		init_tel_txopt(&opt, encap_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		ipv6_push_frag_opts(skb, &opt.ops, &proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	skb_push(skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	ip6_flow_hdr(ipv6h, dsfield,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		     ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	ipv6h->hop_limit = hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	ipv6h->nexthdr = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	ipv6h->saddr = fl6->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	ipv6h->daddr = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	ip6tunnel_xmit(NULL, skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) tx_err_link_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	stats->tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) tx_err_dst_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) EXPORT_SYMBOL(ip6_tnl_xmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	struct ipv6hdr *ipv6h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	const struct iphdr  *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	int encap_limit = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	__u16 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	__u8 dsfield, orig_dsfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	__u32 mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	u8 tproto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	tproto = READ_ONCE(t->parms.proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	if (tproto != protocol && tproto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	if (t->parms.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		const struct ip_tunnel_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			     ip_tunnel_info_af(tun_info) != AF_INET6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		key = &tun_info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		memset(&fl6, 0, sizeof(fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		fl6.flowi6_proto = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		fl6.saddr = key->u.ipv6.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		fl6.daddr = key->u.ipv6.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		fl6.flowlabel = key->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		dsfield =  key->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		case IPPROTO_IPIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			orig_dsfield = ipv4_get_dsfield(iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		case IPPROTO_IPV6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			orig_dsfield = ipv6_get_dsfield(ipv6h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			orig_dsfield = dsfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			encap_limit = t->parms.encap_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		if (protocol == IPPROTO_IPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			offset = ip6_tnl_parse_tlv_enc_lim(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 						skb_network_header(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			/* ip6_tnl_parse_tlv_enc_lim() might have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			 * reallocated skb->head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			if (offset > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 				struct ipv6_tlv_tnl_enc_lim *tel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 				tel = (void *)&skb_network_header(skb)[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 				if (tel->encap_limit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 					icmpv6_ndo_send(skb, ICMPV6_PARAMPROB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 							ICMPV6_HDR_FIELD, offset + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 				encap_limit = tel->encap_limit - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		fl6.flowi6_proto = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			fl6.flowi6_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			fl6.flowi6_mark = t->parms.fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		case IPPROTO_IPIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			orig_dsfield = ipv4_get_dsfield(iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 				dsfield = orig_dsfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 				dsfield = ip6_tclass(t->parms.flowinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		case IPPROTO_IPV6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			orig_dsfield = ipv6_get_dsfield(ipv6h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				dsfield = orig_dsfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 				dsfield = ip6_tclass(t->parms.flowinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 				fl6.flowlabel |= ip6_flowlabel(ipv6h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			orig_dsfield = dsfield = ip6_tclass(t->parms.flowinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	dsfield = INET_ECN_encapsulate(dsfield, orig_dsfield);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	skb_set_inner_ipproto(skb, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 			   protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		/* XXX: send ICMP error even if DF is not set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		if (err == -EMSGSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 			case IPPROTO_IPIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 				icmp_ndo_send(skb, ICMP_DEST_UNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 					      ICMP_FRAG_NEEDED, htonl(mtu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			case IPPROTO_IPV6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 				icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) static netdev_tx_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	struct net_device_stats *stats = &t->dev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	u8 ipproto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	switch (skb->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		ipproto = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		if (ip6_tnl_addr_conflict(t, ipv6_hdr(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		ipproto = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	case htons(ETH_P_MPLS_UC):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		ipproto = IPPROTO_MPLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	ret = ipxip6_tnl_xmit(skb, dev, ipproto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) tx_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	stats->tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	stats->tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static void ip6_tnl_link_config(struct ip6_tnl *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	struct net_device *dev = t->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	struct net_device *tdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	struct __ip6_tnl_parm *p = &t->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	struct flowi6 *fl6 = &t->fl.u.ip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	unsigned int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	int t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	/* Set up flowi template */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	fl6->saddr = p->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	fl6->daddr = p->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	fl6->flowi6_oif = p->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	fl6->flowlabel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		dev->flags |= IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		dev->flags &= ~IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	t->tun_hlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	t->hlen = t->encap_hlen + t->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	t_hlen = t->hlen + sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		int strict = (ipv6_addr_type(&p->raddr) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		struct rt6_info *rt = rt6_lookup(t->net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 						 &p->raddr, &p->laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 						 p->link, NULL, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 			tdev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 			ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		if (!tdev && p->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 			tdev = __dev_get_by_index(t->net, p->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		if (tdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 			dev->hard_header_len = tdev->hard_header_len + t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 			dev->mtu = mtu - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 			if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 				dev->mtu -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			if (dev->mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 				dev->mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538)  * ip6_tnl_change - update the tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)  *   @t: tunnel to be changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)  *   @p: tunnel configuration parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  *   ip6_tnl_change() updates the tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	t->parms.laddr = p->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	t->parms.raddr = p->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	t->parms.flags = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	t->parms.hop_limit = p->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	t->parms.encap_limit = p->encap_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	t->parms.flowinfo = p->flowinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	t->parms.link = p->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	t->parms.proto = p->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	t->parms.fwmark = p->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	ip6_tnl_link_config(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	ip6_tnl_unlink(ip6n, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	err = ip6_tnl_change(t, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	ip6_tnl_link(ip6n, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	netdev_state_change(t->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	/* for default tnl0 device allow to change only the proto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	t->parms.proto = p->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	netdev_state_change(t->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	p->laddr = u->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	p->raddr = u->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	p->flags = u->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	p->hop_limit = u->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	p->encap_limit = u->encap_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	p->flowinfo = u->flowinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	p->link = u->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	p->proto = u->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	memcpy(p->name, u->name, sizeof(u->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	u->laddr = p->laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	u->raddr = p->raddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	u->flags = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	u->hop_limit = p->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	u->encap_limit = p->encap_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	u->flowinfo = p->flowinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	u->link = p->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	u->proto = p->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	memcpy(u->name, p->name, sizeof(u->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)  *   @dev: virtual device associated with tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)  *   @ifr: parameters passed from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)  *   @cmd: command to be performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)  *   from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)  *   The possible commands are the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)  *     %SIOCGETTUNNEL: get tunnel parameters for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625)  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)  *     %SIOCDELTUNNEL: delete tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)  *   The fallback device "ip6tnl0", created during module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)  *   initialization, can be used for creating other tunnel devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  *   0 on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)  *   %-EFAULT if unable to copy data to or from userspace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)  *   %-EINVAL if passed tunnel parameters are invalid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637)  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)  *   %-ENODEV if attempting to change or delete a nonexisting device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	struct ip6_tnl_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	struct __ip6_tnl_parm p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	memset(&p1, 0, sizeof(p1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	case SIOCGETTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		if (dev == ip6n->fb_tnl_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 				err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			ip6_tnl_parm_from_user(&p1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			t = ip6_tnl_locate(net, &p1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			if (IS_ERR(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 				t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			memset(&p, 0, sizeof(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		ip6_tnl_parm_to_user(&p, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 			err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	case SIOCADDTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	case SIOCCHGTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		    p.proto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		ip6_tnl_parm_from_user(&p1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		if (cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 			if (!IS_ERR(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 				if (t->dev != dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 					err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 				t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 			if (dev == ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 				err = ip6_tnl0_update(t, &p1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 				err = ip6_tnl_update(t, &p1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		if (!IS_ERR(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			ip6_tnl_parm_to_user(&p, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 				err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 			err = PTR_ERR(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	case SIOCDELTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		if (dev == ip6n->fb_tnl_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 			err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			ip6_tnl_parm_from_user(&p1, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 			t = ip6_tnl_locate(net, &p1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			if (IS_ERR(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			if (t->dev == ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			dev = t->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)  * ip6_tnl_change_mtu - change mtu manually for tunnel device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)  *   @dev: virtual device associated with tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)  *   @new_mtu: the new mtu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)  *   0 on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)  *   %-EINVAL if mtu too small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	struct ip6_tnl *tnl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	if (tnl->parms.proto == IPPROTO_IPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		if (new_mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		if (new_mtu < ETH_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	dev->mtu = new_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) EXPORT_SYMBOL(ip6_tnl_change_mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) int ip6_tnl_get_iflink(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	return t->parms.link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) EXPORT_SYMBOL(ip6_tnl_get_iflink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 			  unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	if (num >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	return !cmpxchg((const struct ip6_tnl_encap_ops **)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 			&ip6tun_encaps[num],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 			NULL, ops) ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			  unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	if (num >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		       &ip6tun_encaps[num],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		       ops, NULL) == ops) ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) int ip6_tnl_encap_setup(struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	int hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	memset(&t->encap, 0, sizeof(t->encap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	hlen = ip6_encap_hlen(ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (hlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		return hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	t->encap.type = ipencap->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	t->encap.sport = ipencap->sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	t->encap.dport = ipencap->dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	t->encap.flags = ipencap->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	t->encap_hlen = hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	t->hlen = t->encap_hlen + t->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) static const struct net_device_ops ip6_tnl_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	.ndo_init	= ip6_tnl_dev_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	.ndo_uninit	= ip6_tnl_dev_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	.ndo_start_xmit = ip6_tnl_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	.ndo_do_ioctl	= ip6_tnl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	.ndo_change_mtu = ip6_tnl_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	.ndo_get_stats	= ip6_get_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	.ndo_get_iflink = ip6_tnl_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) #define IPXIPX_FEATURES (NETIF_F_SG |		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 			 NETIF_F_FRAGLIST |	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			 NETIF_F_HIGHDMA |	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			 NETIF_F_GSO_SOFTWARE |	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			 NETIF_F_HW_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)  * ip6_tnl_dev_setup - setup virtual tunnel device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)  *   @dev: virtual device associated with tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)  *   Initialize function pointers and device parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) static void ip6_tnl_dev_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	dev->netdev_ops = &ip6_tnl_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	dev->header_ops = &ip_tunnel_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	dev->priv_destructor = ip6_dev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	dev->type = ARPHRD_TUNNEL6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	dev->flags |= IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	dev->addr_len = sizeof(struct in6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	dev->features		|= IPXIPX_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	dev->hw_features	|= IPXIPX_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	/* This perm addr will be used as interface identifier by IPv6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	dev->addr_assign_type = NET_ADDR_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	eth_random_addr(dev->perm_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)  *   @dev: virtual device associated with tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) ip6_tnl_dev_init_gen(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	int t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	t->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	t->net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	if (!dev->tstats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		goto free_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	ret = gro_cells_init(&t->gro_cells, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		goto destroy_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	t->tun_hlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	t->hlen = t->encap_hlen + t->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	t_hlen = t->hlen + sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	dev->type = ARPHRD_TUNNEL6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	dev->hard_header_len = LL_MAX_HEADER + t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	dev->mtu = ETH_DATA_LEN - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		dev->mtu -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	dev->min_mtu = ETH_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) destroy_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	dst_cache_destroy(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) free_stats:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	dev->tstats = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  *   @dev: virtual device associated with tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) static int ip6_tnl_dev_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	int err = ip6_tnl_dev_init_gen(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	ip6_tnl_link_config(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (t->parms.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  *   @dev: fallback device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)  * Return: 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	t->parms.proto = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	rcu_assign_pointer(ip6n->tnls_wc[0], t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 			    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	u8 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (!data || !data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	if (proto != IPPROTO_IPV6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	    proto != IPPROTO_IPIP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	    proto != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) static void ip6_tnl_netlink_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 				  struct __ip6_tnl_parm *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	memset(parms, 0, sizeof(*parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	if (data[IFLA_IPTUN_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	if (data[IFLA_IPTUN_LOCAL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (data[IFLA_IPTUN_REMOTE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	if (data[IFLA_IPTUN_TTL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	if (data[IFLA_IPTUN_ENCAP_LIMIT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	if (data[IFLA_IPTUN_FLOWINFO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	if (data[IFLA_IPTUN_FLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	if (data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	if (data[IFLA_IPTUN_COLLECT_METADATA])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		parms->collect_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	if (data[IFLA_IPTUN_FWMARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 					struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	memset(ipencap, 0, sizeof(*ipencap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	if (data[IFLA_IPTUN_ENCAP_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	if (data[IFLA_IPTUN_ENCAP_SPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	if (data[IFLA_IPTUN_ENCAP_DPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			   struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			   struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	struct ip6_tnl *nt, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	nt = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		err = ip6_tnl_encap_setup(nt, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	ip6_tnl_netlink_parms(data, &nt->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	if (nt->parms.collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		if (rtnl_dereference(ip6n->collect_md_tun))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		t = ip6_tnl_locate(net, &nt->parms, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		if (!IS_ERR(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	err = ip6_tnl_create2(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	if (!err && tb[IFLA_MTU])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			      struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			      struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	struct ip6_tnl *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	struct __ip6_tnl_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	if (dev == ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		int err = ip6_tnl_encap_setup(t, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	ip6_tnl_netlink_parms(data, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	if (p.collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	t = ip6_tnl_locate(net, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	if (!IS_ERR(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		if (t->dev != dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	return ip6_tnl_update(t, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	if (dev != ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) static size_t ip6_tnl_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		/* IFLA_IPTUN_LINK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		/* IFLA_IPTUN_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		nla_total_size(sizeof(struct in6_addr)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		/* IFLA_IPTUN_REMOTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		nla_total_size(sizeof(struct in6_addr)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		/* IFLA_IPTUN_TTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		/* IFLA_IPTUN_ENCAP_LIMIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		/* IFLA_IPTUN_FLOWINFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 		/* IFLA_IPTUN_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 		/* IFLA_IPTUN_PROTO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		/* IFLA_IPTUN_ENCAP_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		/* IFLA_IPTUN_ENCAP_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		/* IFLA_IPTUN_ENCAP_SPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		/* IFLA_IPTUN_ENCAP_DPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		/* IFLA_IPTUN_COLLECT_METADATA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		nla_total_size(0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		/* IFLA_IPTUN_FWMARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	struct ip6_tnl *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	struct __ip6_tnl_parm *parm = &tunnel->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	    nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	    nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	    nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	    nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	    nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	    nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	    nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	    nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	    nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	if (parm->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 		if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) struct net *ip6_tnl_get_link_net(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	struct ip6_tnl *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	return tunnel->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) EXPORT_SYMBOL(ip6_tnl_get_link_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	[IFLA_IPTUN_LINK]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	[IFLA_IPTUN_LOCAL]		= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	[IFLA_IPTUN_REMOTE]		= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	[IFLA_IPTUN_TTL]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	[IFLA_IPTUN_ENCAP_LIMIT]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	[IFLA_IPTUN_FLOWINFO]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	[IFLA_IPTUN_PROTO]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	[IFLA_IPTUN_ENCAP_TYPE]		= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	[IFLA_IPTUN_ENCAP_FLAGS]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	[IFLA_IPTUN_ENCAP_SPORT]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	[IFLA_IPTUN_ENCAP_DPORT]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	[IFLA_IPTUN_COLLECT_METADATA]	= { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	[IFLA_IPTUN_FWMARK]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) static struct rtnl_link_ops ip6_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	.kind		= "ip6tnl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	.maxtype	= IFLA_IPTUN_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	.policy		= ip6_tnl_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	.priv_size	= sizeof(struct ip6_tnl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	.setup		= ip6_tnl_dev_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	.validate	= ip6_tnl_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	.newlink	= ip6_tnl_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	.changelink	= ip6_tnl_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	.dellink	= ip6_tnl_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	.get_size	= ip6_tnl_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	.fill_info	= ip6_tnl_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	.get_link_net	= ip6_tnl_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	.handler	= ip4ip6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	.err_handler	= ip4ip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	.priority	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	.handler	= ip6ip6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	.err_handler	= ip6ip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	.priority	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) static struct xfrm6_tunnel mplsip6_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	.handler	= mplsip6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	.err_handler	= mplsip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	.priority	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	struct net_device *dev, *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	struct ip6_tnl *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	for_each_netdev_safe(net, dev, aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		if (dev->rtnl_link_ops == &ip6_link_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 			unregister_netdevice_queue(dev, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		t = rtnl_dereference(ip6n->tnls_r_l[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		while (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 			/* If dev is in the same netns, it has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			 * been added to the list by the previous loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 			if (!net_eq(dev_net(t->dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 				unregister_netdevice_queue(t->dev, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 			t = rtnl_dereference(t->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	t = rtnl_dereference(ip6n->tnls_wc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	while (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		/* If dev is in the same netns, it has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		 * been added to the list by the previous loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		if (!net_eq(dev_net(t->dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			unregister_netdevice_queue(t->dev, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		t = rtnl_dereference(t->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) static int __net_init ip6_tnl_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	struct ip6_tnl *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	ip6n->tnls[0] = ip6n->tnls_wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	ip6n->tnls[1] = ip6n->tnls_r_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	if (!net_has_fallback_tunnels(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 					NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (!ip6n->fb_tnl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		goto err_alloc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	dev_net_set(ip6n->fb_tnl_dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	/* FB netdevice is special: we have one, and only one per netns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	 * Allowing to move it to another netns is clearly unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 		goto err_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	err = register_netdev(ip6n->fb_tnl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 		goto err_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	t = netdev_priv(ip6n->fb_tnl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) err_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	free_netdev(ip6n->fb_tnl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) err_alloc_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	list_for_each_entry(net, net_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		ip6_tnl_destroy_tunnels(net, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) static struct pernet_operations ip6_tnl_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	.init = ip6_tnl_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	.exit_batch = ip6_tnl_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	.id   = &ip6_tnl_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	.size = sizeof(struct ip6_tnl_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352)  * ip6_tunnel_init - register protocol and reserve needed resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354)  * Return: 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) static int __init ip6_tunnel_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	int  err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	if (!ipv6_mod_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	err = register_pernet_device(&ip6_tnl_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		goto out_pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		pr_err("%s: can't register ip4ip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		goto out_ip4ip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		pr_err("%s: can't register ip6ip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		goto out_ip6ip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	if (ip6_tnl_mpls_supported()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 		err = xfrm6_tunnel_register(&mplsip6_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 			pr_err("%s: can't register mplsip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 			goto out_mplsip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	err = rtnl_link_register(&ip6_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		goto rtnl_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) rtnl_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	if (ip6_tnl_mpls_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) out_mplsip6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) out_ip6ip6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) out_ip4ip6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	unregister_pernet_device(&ip6_tnl_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) out_pernet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)  * ip6_tunnel_cleanup - free resources and unregister protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) static void __exit ip6_tunnel_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	rtnl_link_unregister(&ip6_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		pr_info("%s: can't deregister ip4ip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 		pr_info("%s: can't deregister ip6ip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	if (ip6_tnl_mpls_supported() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	    xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		pr_info("%s: can't deregister mplsip6\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	unregister_pernet_device(&ip6_tnl_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) module_init(ip6_tunnel_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) module_exit(ip6_tunnel_cleanup);