^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2013 Nicira, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/igmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/dsfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <net/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <net/ip6_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static unsigned int ip_tunnel_hash(__be32 key, __be32 remote)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return hash_32((__force u32)key ^ (__force u32)remote,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) IP_TNL_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static bool ip_tunnel_key_match(const struct ip_tunnel_parm *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __be16 flags, __be32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (p->i_flags & TUNNEL_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (flags & TUNNEL_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return key == p->i_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* key expected, none present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return !(flags & TUNNEL_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Fallback tunnel: no source, no destination, no key, no options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) Tunnel hash table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) We require exact key match i.e. if a key is present in packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) it will match only tunnel with the same key; if it is not present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) it will match only keyless tunnel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) All keysless packets, if not matched configured keyless tunnels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) will match fallback tunnel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) Given src, dst and key, find appropriate for input tunnel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int link, __be16 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) __be32 remote, __be32 local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) __be32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct ip_tunnel *t, *cand = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct net_device *ndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned int hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) hash = ip_tunnel_hash(key, remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) head = &itn->tunnels[hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) hlist_for_each_entry_rcu(t, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (local != t->parms.iph.saddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) remote != t->parms.iph.daddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!ip_tunnel_key_match(&t->parms, flags, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (t->parms.link == link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) hlist_for_each_entry_rcu(t, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (remote != t->parms.iph.daddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) t->parms.iph.saddr != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!ip_tunnel_key_match(&t->parms, flags, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (t->parms.link == link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) else if (!cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) hash = ip_tunnel_hash(key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) head = &itn->tunnels[hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) hlist_for_each_entry_rcu(t, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if ((local != t->parms.iph.saddr || t->parms.iph.daddr != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) (local != t->parms.iph.daddr || !ipv4_is_multicast(local)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!ip_tunnel_key_match(&t->parms, flags, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (t->parms.link == link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) else if (!cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) hlist_for_each_entry_rcu(t, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if ((!(flags & TUNNEL_NO_KEY) && t->parms.i_key != key) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) t->parms.iph.saddr != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) t->parms.iph.daddr != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) !(t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (t->parms.link == link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) else if (!cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) cand = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (cand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) t = rcu_dereference(itn->collect_md_tun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (t && t->dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ndev = READ_ONCE(itn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (ndev && ndev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return netdev_priv(ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) EXPORT_SYMBOL_GPL(ip_tunnel_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct hlist_head *ip_bucket(struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct ip_tunnel_parm *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) __be32 remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __be32 i_key = parms->i_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (parms->iph.daddr && !ipv4_is_multicast(parms->iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) remote = parms->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) remote = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (!(parms->i_flags & TUNNEL_KEY) && (parms->i_flags & VTI_ISVTI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) i_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) h = ip_tunnel_hash(i_key, remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return &itn->tunnels[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void ip_tunnel_add(struct ip_tunnel_net *itn, struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct hlist_head *head = ip_bucket(itn, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (t->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rcu_assign_pointer(itn->collect_md_tun, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) hlist_add_head_rcu(&t->hash_node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void ip_tunnel_del(struct ip_tunnel_net *itn, struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (t->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) rcu_assign_pointer(itn->collect_md_tun, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) hlist_del_init_rcu(&t->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static struct ip_tunnel *ip_tunnel_find(struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct ip_tunnel_parm *parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) __be32 remote = parms->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) __be32 local = parms->iph.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) __be32 key = parms->i_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) __be16 flags = parms->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int link = parms->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct ip_tunnel *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct hlist_head *head = ip_bucket(itn, parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) hlist_for_each_entry_rcu(t, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (local == t->parms.iph.saddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) remote == t->parms.iph.daddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) link == t->parms.link &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) type == t->dev->type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ip_tunnel_key_match(&t->parms, flags, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return t;
^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) static struct net_device *__ip_tunnel_create(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) const struct rtnl_link_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct ip_tunnel_parm *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) err = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (parms->name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!dev_valid_name(parms->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) strlcpy(name, parms->name, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (strlen(ops->kind) > (IFNAMSIZ - 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) strcpy(name, ops->kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) strcat(name, "%d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) dev = alloc_netdev(ops->priv_size, name, NET_NAME_UNKNOWN, ops->setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dev_net_set(dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev->rtnl_link_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) tunnel->parms = *parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) tunnel->net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto failed_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) failed_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int ip_tunnel_bind_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct net_device *tdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int hlen = LL_MAX_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int mtu = ETH_DATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int t_hlen = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Guess output device to choose reasonable mtu and needed_headroom */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (iph->daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ip_tunnel_init_flow(&fl4, iph->protocol, iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) iph->saddr, tunnel->parms.o_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) RT_TOS(iph->tos), tunnel->parms.link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) tunnel->fwmark, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) rt = ip_route_output_key(tunnel->net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) tdev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (dev->type != ARPHRD_ETHER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) dev->flags |= IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) dst_cache_reset(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!tdev && tunnel->parms.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (tdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) hlen = tdev->hard_header_len + tdev->needed_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) mtu = min(tdev->mtu, IP_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) dev->needed_headroom = t_hlen + hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mtu -= t_hlen + (dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (mtu < IPV4_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) mtu = IPV4_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static struct ip_tunnel *ip_tunnel_create(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct ip_tunnel_parm *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct ip_tunnel *nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) dev = __ip_tunnel_create(net, itn->rtnl_link_ops, parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return ERR_CAST(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) mtu = ip_tunnel_bind_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) err = dev_set_mtu(dev, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto err_dev_set_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) nt = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) t_hlen = nt->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) dev->min_mtu = ETH_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) dev->max_mtu = IP_MAX_MTU - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (dev->type == ARPHRD_ETHER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dev->max_mtu -= dev->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ip_tunnel_add(itn, nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) err_dev_set_mtu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) bool log_ecn_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #ifdef CONFIG_NET_IPGRE_BROADCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (ipv4_is_multicast(iph->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) tunnel->dev->stats.multicast++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) skb->pkt_type = PACKET_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if ((!(tpi->flags&TUNNEL_CSUM) && (tunnel->parms.i_flags&TUNNEL_CSUM)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ((tpi->flags&TUNNEL_CSUM) && !(tunnel->parms.i_flags&TUNNEL_CSUM))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) tunnel->dev->stats.rx_crc_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (tunnel->parms.i_flags&TUNNEL_SEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!(tpi->flags&TUNNEL_SEQ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) (tunnel->i_seqno && (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) tunnel->dev->stats.rx_fifo_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) tunnel->i_seqno = ntohl(tpi->seq) + 1;
^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) skb_set_network_header(skb, (tunnel->dev->type == ARPHRD_ETHER) ? ETH_HLEN : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) err = IP_ECN_decapsulate(iph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (log_ecn_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) &iph->saddr, iph->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (err > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) ++tunnel->dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ++tunnel->dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dev_sw_netstats_rx_add(tunnel->dev, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (tunnel->dev->type == ARPHRD_ETHER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) skb->protocol = eth_type_trans(skb, tunnel->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) skb->dev = tunnel->dev;
^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) if (tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) skb_dst_set(skb, (struct dst_entry *)tun_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) gro_cells_receive(&tunnel->gro_cells, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (tun_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dst_release((struct dst_entry *)tun_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) EXPORT_SYMBOL_GPL(ip_tunnel_rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (num >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return !cmpxchg((const struct ip_tunnel_encap_ops **)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) &iptun_encaps[num],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) NULL, ops) ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) EXPORT_SYMBOL(ip_tunnel_encap_add_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) unsigned int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (num >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) ret = (cmpxchg((const struct ip_tunnel_encap_ops **)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) &iptun_encaps[num],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ops, NULL) == ops) ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) EXPORT_SYMBOL(ip_tunnel_encap_del_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int ip_tunnel_encap_setup(struct ip_tunnel *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) memset(&t->encap, 0, sizeof(t->encap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) hlen = ip_encap_hlen(ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (hlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) t->encap.type = ipencap->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) t->encap.sport = ipencap->sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) t->encap.dport = ipencap->dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) t->encap.flags = ipencap->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) t->encap_hlen = hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) t->hlen = t->encap_hlen + t->tun_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) EXPORT_SYMBOL_GPL(ip_tunnel_encap_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int tnl_update_pmtu(struct net_device *dev, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct rtable *rt, __be16 df,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) const struct iphdr *inner_iph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int tunnel_hlen, __be32 dst, bool md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) tunnel_hlen = md ? tunnel_hlen : tunnel->hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) pkt_size = skb->len - tunnel_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) pkt_size -= dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (df) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) mtu = dst_mtu(&rt->dst) - (sizeof(struct iphdr) + tunnel_hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) mtu -= dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (skb_valid_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) skb_dst_update_pmtu_no_confirm(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (!skb_is_gso(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) (inner_iph->frag_off & htons(IP_DF)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mtu < pkt_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) else if (skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct rt6_info *rt6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) __be32 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) rt6 = skb_valid_dst(skb) ? (struct rt6_info *)skb_dst(skb) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) daddr = md ? dst : tunnel->parms.iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (rt6 && mtu < dst_mtu(skb_dst(skb)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) mtu >= IPV6_MIN_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if ((daddr && !ipv4_is_multicast(daddr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) rt6->rt6i_dst.plen == 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) rt6->rt6i_flags |= RTF_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) dst_metric_set(skb_dst(skb), RTAX_MTU, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (!skb_is_gso(skb) && mtu >= IPV6_MIN_MTU &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) mtu < pkt_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) u8 proto, int tunnel_hlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) u32 headroom = sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) const struct ip_tunnel_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) const struct iphdr *inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct rtable *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) __be16 df = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) u8 tos, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) bool use_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) ip_tunnel_info_af(tun_info) != AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) key = &tun_info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) tos = key->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (tos == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) tos = inner_iph->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) else if (skb->protocol == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) ip_tunnel_init_flow(&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) tunnel_id_to_key32(key->tun_id), RT_TOS(tos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 0, skb->mark, skb_get_hash(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (tunnel->encap.type != TUNNEL_ENCAP_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) rt = ip_route_output_key(tunnel->net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (rt->dst.dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (key->tun_flags & TUNNEL_DONT_FRAGMENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) df = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, tunnel_hlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) key->u.ipv4.dst, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) ttl = key->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (ttl == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ttl = inner_iph->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) else if (skb->protocol == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) ttl = ip4_dst_hoplimit(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) headroom += LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (headroom > dev->needed_headroom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) dev->needed_headroom = headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (skb_cow_head(skb, dev->needed_headroom)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) goto tx_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, proto, tos, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) df, !net_eq(tunnel->net, dev_net(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) goto kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) tx_dropped:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) EXPORT_SYMBOL_GPL(ip_md_tunnel_xmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) const struct iphdr *tnl_params, u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct ip_tunnel_info *tun_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) const struct iphdr *inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) unsigned int max_headroom; /* The extra header space needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) struct rtable *rt = NULL; /* Route to the other host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) bool use_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) bool md = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) bool connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) u8 tos, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) __be32 dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) __be16 df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) connected = (tunnel->parms.iph.daddr != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) dst = tnl_params->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (dst == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /* NBMA tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (!skb_dst(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) dev->stats.tx_fifo_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) ip_tunnel_info_af(tun_info) == AF_INET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) tun_info->key.u.ipv4.dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) dst = tun_info->key.u.ipv4.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) else if (skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) dst = rt_nexthop(rt, inner_iph->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) else if (skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) const struct in6_addr *addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) bool do_tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) neigh = dst_neigh_lookup(skb_dst(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) &ipv6_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (!neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) addr6 = (const struct in6_addr *)&neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (addr_type == IPV6_ADDR_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) addr6 = &ipv6_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) do_tx_error_icmp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) do_tx_error_icmp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) dst = addr6->s6_addr32[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (do_tx_error_icmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) tos = tnl_params->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if (tos & 0x1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) tos &= ~0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) tos = inner_iph->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) } else if (skb->protocol == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) connected = false;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) tunnel->parms.o_key, RT_TOS(tos), tunnel->parms.link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) tunnel->fwmark, skb_get_hash(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (connected && md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) rt = dst_cache_get_ip4(&tun_info->dst_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) &fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) rt = connected ? dst_cache_get_ip4(&tunnel->dst_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) &fl4.saddr) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) rt = ip_route_output_key(tunnel->net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) else if (!md && connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (rt->dst.dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) df = tnl_params->frag_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (skb->protocol == htons(ETH_P_IP) && !tunnel->ignore_df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) df |= (inner_iph->frag_off & htons(IP_DF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (tnl_update_pmtu(dev, skb, rt, df, inner_iph, 0, 0, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (tunnel->err_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (time_before(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) tunnel->err_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) tunnel->err_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) ttl = tnl_params->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (ttl == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) ttl = inner_iph->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) else if (skb->protocol == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) ttl = ip4_dst_hoplimit(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) max_headroom = LL_RESERVED_SPACE(rt->dst.dev) + sizeof(struct iphdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) + rt->dst.header_len + ip_encap_hlen(&tunnel->encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (max_headroom > dev->needed_headroom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) dev->needed_headroom = max_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (skb_cow_head(skb, dev->needed_headroom)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) df, !net_eq(tunnel->net, dev_net(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) tx_error_icmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) EXPORT_SYMBOL_GPL(ip_tunnel_xmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) static void ip_tunnel_update(struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) struct ip_tunnel *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) struct ip_tunnel_parm *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) bool set_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) __u32 fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) ip_tunnel_del(itn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) t->parms.iph.saddr = p->iph.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) t->parms.iph.daddr = p->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) t->parms.i_key = p->i_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) t->parms.o_key = p->o_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (dev->type != ARPHRD_ETHER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) memcpy(dev->dev_addr, &p->iph.saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) memcpy(dev->broadcast, &p->iph.daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) ip_tunnel_add(itn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) t->parms.iph.ttl = p->iph.ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) t->parms.iph.tos = p->iph.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) t->parms.iph.frag_off = p->iph.frag_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (t->parms.link != p->link || t->fwmark != fwmark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) t->parms.link = p->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) t->fwmark = fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) mtu = ip_tunnel_bind_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (set_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) dev->mtu = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) netdev_state_change(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) struct ip_tunnel_net *itn = net_generic(net, t->ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) case SIOCGETTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (dev == itn->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) memcpy(p, &t->parms, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) case SIOCADDTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) case SIOCCHGTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (p->iph.ttl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) p->iph.frag_off |= htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (!(p->i_flags & VTI_ISVTI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (!(p->i_flags & TUNNEL_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) p->i_key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (!(p->o_flags & TUNNEL_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) p->o_key = 0;
^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) t = ip_tunnel_find(itn, p, itn->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (cmd == SIOCADDTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) t = ip_tunnel_create(net, itn, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) err = PTR_ERR_OR_ZERO(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (t->dev != dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) unsigned int nflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (ipv4_is_multicast(p->iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) nflags = IFF_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) else if (p->iph.daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) nflags = IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) ip_tunnel_update(itn, t, dev, p, true, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) case SIOCDELTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (dev == itn->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (t == netdev_priv(itn->fb_tunnel_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) dev = t->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) EXPORT_SYMBOL_GPL(ip_tunnel_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) int ip_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (!err && copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) EXPORT_SYMBOL_GPL(ip_tunnel_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) int t_hlen = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) int max_mtu = IP_MAX_MTU - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (dev->type == ARPHRD_ETHER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) max_mtu -= dev->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (new_mtu < ETH_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (new_mtu > max_mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) new_mtu = max_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) dev->mtu = new_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) EXPORT_SYMBOL_GPL(__ip_tunnel_change_mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return __ip_tunnel_change_mtu(dev, new_mtu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) EXPORT_SYMBOL_GPL(ip_tunnel_change_mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static void ip_tunnel_dev_free(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) gro_cells_destroy(&tunnel->gro_cells);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) dst_cache_destroy(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) void ip_tunnel_dellink(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) itn = net_generic(tunnel->net, tunnel->ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (itn->fb_tunnel_dev != dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) ip_tunnel_del(itn, netdev_priv(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) EXPORT_SYMBOL_GPL(ip_tunnel_dellink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) struct net *ip_tunnel_get_link_net(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) return tunnel->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) EXPORT_SYMBOL(ip_tunnel_get_link_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) int ip_tunnel_get_iflink(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) return tunnel->parms.link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) EXPORT_SYMBOL(ip_tunnel_get_iflink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) struct rtnl_link_ops *ops, char *devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) struct ip_tunnel_net *itn = net_generic(net, ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) struct ip_tunnel_parm parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) itn->rtnl_link_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) for (i = 0; i < IP_TNL_HASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) INIT_HLIST_HEAD(&itn->tunnels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (!ops || !net_has_fallback_tunnels(net)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) struct ip_tunnel_net *it_init_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) it_init_net = net_generic(&init_net, ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) itn->type = it_init_net->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) itn->fb_tunnel_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) memset(&parms, 0, sizeof(parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) strlcpy(parms.name, devname, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) itn->fb_tunnel_dev = __ip_tunnel_create(net, ops, &parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) /* FB netdevice is special: we have one, and only one per netns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * Allowing to move it to another netns is clearly unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (!IS_ERR(itn->fb_tunnel_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) itn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) itn->fb_tunnel_dev->mtu = ip_tunnel_bind_dev(itn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) ip_tunnel_add(itn, netdev_priv(itn->fb_tunnel_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) itn->type = itn->fb_tunnel_dev->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) return PTR_ERR_OR_ZERO(itn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) EXPORT_SYMBOL_GPL(ip_tunnel_init_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) static void ip_tunnel_destroy(struct net *net, struct ip_tunnel_net *itn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) struct rtnl_link_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) struct net_device *dev, *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) for_each_netdev_safe(net, dev, aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (dev->rtnl_link_ops == ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) for (h = 0; h < IP_TNL_HASH_SIZE; h++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) struct hlist_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct hlist_head *thead = &itn->tunnels[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) hlist_for_each_entry_safe(t, n, thead, hash_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) /* If dev is in the same netns, it has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) * been added to the list by the previous loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (!net_eq(dev_net(t->dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) unregister_netdevice_queue(t->dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) void ip_tunnel_delete_nets(struct list_head *net_list, unsigned int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) struct rtnl_link_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) list_for_each_entry(net, net_list, exit_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) itn = net_generic(net, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) ip_tunnel_destroy(net, itn, &list, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) EXPORT_SYMBOL_GPL(ip_tunnel_delete_nets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) struct ip_tunnel_parm *p, __u32 fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) struct ip_tunnel *nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) nt = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) itn = net_generic(net, nt->ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) if (nt->collect_md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) if (rtnl_dereference(itn->collect_md_tun))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (ip_tunnel_find(itn, p, dev->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) nt->net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) nt->parms = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) nt->fwmark = fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) goto err_register_netdevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) mtu = ip_tunnel_bind_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (tb[IFLA_MTU]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) unsigned int max = IP_MAX_MTU - (nt->hlen + sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (dev->type == ARPHRD_ETHER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) max -= dev->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) mtu = clamp(dev->mtu, (unsigned int)ETH_MIN_MTU, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) err = dev_set_mtu(dev, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) goto err_dev_set_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) ip_tunnel_add(itn, nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) err_dev_set_mtu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) err_register_netdevice:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) EXPORT_SYMBOL_GPL(ip_tunnel_newlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct ip_tunnel_parm *p, __u32 fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) struct net *net = tunnel->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) struct ip_tunnel_net *itn = net_generic(net, tunnel->ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) if (dev == itn->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) t = ip_tunnel_find(itn, p, dev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (t->dev != dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) t = tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (dev->type != ARPHRD_ETHER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) unsigned int nflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) if (ipv4_is_multicast(p->iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) nflags = IFF_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) else if (p->iph.daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) nflags = IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if ((dev->flags ^ nflags) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) (IFF_POINTOPOINT | IFF_BROADCAST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) ip_tunnel_update(itn, t, dev, p, !tb[IFLA_MTU], fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) EXPORT_SYMBOL_GPL(ip_tunnel_changelink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) int ip_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) struct iphdr *iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) dev->priv_destructor = ip_tunnel_dev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (!dev->tstats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) err = gro_cells_init(&tunnel->gro_cells, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) dst_cache_destroy(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) tunnel->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) tunnel->net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) strcpy(tunnel->parms.name, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) iph->version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) iph->ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (tunnel->collect_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) EXPORT_SYMBOL_GPL(ip_tunnel_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) void ip_tunnel_uninit(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) struct net *net = tunnel->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct ip_tunnel_net *itn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) itn = net_generic(net, tunnel->ip_tnl_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) ip_tunnel_del(itn, netdev_priv(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (itn->fb_tunnel_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) WRITE_ONCE(itn->fb_tunnel_dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) dst_cache_reset(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) EXPORT_SYMBOL_GPL(ip_tunnel_uninit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /* Do least required initialization, rest of init is done in tunnel_init call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) void ip_tunnel_setup(struct net_device *dev, unsigned int net_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) tunnel->ip_tnl_net_id = net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) EXPORT_SYMBOL_GPL(ip_tunnel_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) MODULE_LICENSE("GPL");