^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 over IPv4 tunnel device - Simple Internet Transition (SIT)
^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) * Pedro Roque <roque@di.fc.ul.pt>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Roger Venning <r.venning@telstra.com>: 6to4 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Nate Thompson <nate@thebog.net>: 6to4 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Fred Templin <fred.l.templin@boeing.com>: isatap support
^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/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/snmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/transp_v6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/ip6_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <net/ndisc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <net/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <net/inet_ecn.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/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) For comments look at net/ipv4/ip_gre.c --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define IP6_SIT_HASH_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static bool log_ecn_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) module_param(log_ecn_error, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int ipip6_tunnel_init(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void ipip6_tunnel_setup(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void ipip6_dev_free(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __be32 *v4dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static struct rtnl_link_ops sit_link_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static unsigned int sit_net_id __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct sit_net {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct ip_tunnel __rcu *tunnels_wc[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct ip_tunnel __rcu **tunnels[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct net_device *fb_tunnel_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline struct sit_net *dev_to_sit_net(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return net_generic(t->net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * Must be invoked with rcu_read_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) __be32 remote, __be32 local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int sifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int h0 = HASH(remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int h1 = HASH(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int ifindex = dev ? dev->ifindex : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (local == t->parms.iph.saddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) remote == t->parms.iph.daddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (!dev || !t->parms.link || ifindex == t->parms.link ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) sifindex == t->parms.link) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) (t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (remote == t->parms.iph.daddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) (!dev || !t->parms.link || ifindex == t->parms.link ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) sifindex == t->parms.link) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) (t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (local == t->parms.iph.saddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) (!dev || !t->parms.link || ifindex == t->parms.link ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) sifindex == t->parms.link) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) (t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) t = rcu_dereference(sitn->tunnels_wc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (t && (t->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct ip_tunnel_parm *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) __be32 remote = parms->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) __be32 local = parms->iph.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int h = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int prio = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (remote) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) prio |= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) h ^= HASH(remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) prio |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) h ^= HASH(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return &sitn->tunnels[prio][h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return __ipip6_bucket(sitn, &t->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct ip_tunnel __rcu **tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct ip_tunnel *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) for (tp = ipip6_bucket(sitn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (iter = rtnl_dereference(*tp)) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) tp = &iter->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (t == iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) rcu_assign_pointer(*tp, t->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) rcu_assign_pointer(t->next, rtnl_dereference(*tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) rcu_assign_pointer(*tp, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) t->ip6rd.relay_prefix = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) t->ip6rd.prefixlen = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) t->ip6rd.relay_prefixlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int ipip6_tunnel_create(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if ((__force u16)t->parms.i_flags & SIT_ISATAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dev->priv_flags |= IFF_ISATAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dev->rtnl_link_ops = &sit_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ipip6_tunnel_clone_6rd(dev, sitn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ipip6_tunnel_link(sitn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct ip_tunnel_parm *parms, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) __be32 remote = parms->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) __be32 local = parms->iph.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct ip_tunnel *t, *nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct ip_tunnel __rcu **tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) for (tp = __ipip6_bucket(sitn, parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) (t = rtnl_dereference(*tp)) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) tp = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (local == t->parms.iph.saddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) remote == t->parms.iph.daddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) parms->link == t->parms.link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (!create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (parms->name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!dev_valid_name(parms->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) strlcpy(name, parms->name, IFNAMSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) strcpy(name, "sit%d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ipip6_tunnel_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) dev_net_set(dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) nt = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) nt->parms = *parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ipip6_tunnel_create(dev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto failed_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) failed_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #define for_each_prl_rcu(start) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) for (prl = rcu_dereference(start); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) prl; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) prl = rcu_dereference(prl->next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static struct ip_tunnel_prl_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct ip_tunnel_prl_entry *prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) for_each_prl_rcu(t->prl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (prl->addr == addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct ip_tunnel_prl __user *a = ifr->ifr_ifru.ifru_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct ip_tunnel_prl kprl, *kp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct ip_tunnel_prl_entry *prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned int cmax, c = 0, ca, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (copy_from_user(&kprl, a, sizeof(kprl)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) cmax = kprl.datalen / sizeof(kprl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) cmax = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* For simple GET or for root users,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * we try harder to allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ca = t->prl_count < cmax ? t->prl_count : cmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (!kp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* We don't try hard to allocate much memory for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * non-root users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * For root users, retry allocating enough memory for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * the answer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (!kp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) for_each_prl_rcu(t->prl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (c >= cmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) kp[c].addr = prl->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) kp[c].flags = prl->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (kprl.addr != htonl(INADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) len = sizeof(*kp) * c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) kfree(kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct ip_tunnel_prl_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (a->addr == htonl(INADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (p->addr == a->addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (chg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) p->flags = a->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^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 (chg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) goto out;
^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) p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) p->next = t->prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) p->addr = a->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) p->flags = a->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) t->prl_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) rcu_assign_pointer(t->prl, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static void prl_list_destroy_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct ip_tunnel_prl_entry *p, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) n = rcu_dereference_protected(p->next, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) p = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) } while (p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct ip_tunnel_prl_entry *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct ip_tunnel_prl_entry __rcu **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (a && a->addr != htonl(INADDR_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) for (p = &t->prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) (x = rtnl_dereference(*p)) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) p = &x->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (x->addr == a->addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) *p = x->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) kfree_rcu(x, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) t->prl_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) x = rtnl_dereference(t->prl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (x) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) t->prl_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) call_rcu(&x->rcu_head, prl_list_destroy_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) t->prl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static int ipip6_tunnel_prl_ctl(struct net_device *dev, struct ifreq *ifr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct ip_tunnel_prl prl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) case SIOCDELPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) err = ipip6_tunnel_del_prl(t, &prl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) case SIOCADDPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) case SIOCCHGPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) netdev_state_change(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct ip_tunnel_prl_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) p = __ipip6_tunnel_locate_prl(t, iph->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (p->flags & PRL_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (ipv6_addr_is_isatap(addr6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) (addr6->s6_addr32[3] == iph->saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ipv6_chk_prefix(addr6, t->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) ok = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static void ipip6_tunnel_uninit(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (dev == sitn->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ipip6_tunnel_unlink(sitn, tunnel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ipip6_tunnel_del_prl(tunnel, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dst_cache_reset(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static int ipip6_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) const int type = icmp_hdr(skb)->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) const int code = icmp_hdr(skb)->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) unsigned int data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int sifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case ICMP_PARAMETERPROB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) case ICMP_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) case ICMP_SR_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* Impossible event. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /* All others are translated to HOST_UNREACH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) rfc2003 contains "deep thoughts" about NET_UNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) I believe they are just ether pollution. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) case ICMP_TIME_EXCEEDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (code != ICMP_EXC_TTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) case ICMP_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) iph->daddr, iph->saddr, sifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) ipv4_update_pmtu(skb, dev_net(skb->dev), info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) t->parms.link, iph->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (type == ICMP_REDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) iph->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) goto out;
^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) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (__in6_dev_get(skb->dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (t->parms.iph.daddr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) t->err_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) t->err_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) t->err_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) const struct in6_addr *v6addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) __be32 v4embed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* Checks if an address matches an address on the tunnel interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * Long story:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * This function is called after we considered the packet as spoofed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * in is_spoofed_6rd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * We may have a router that is doing NAT for proto 41 packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * function will return true, dropping the packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * But, we can still check if is spoofed against the IP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * addresses associated with the interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static bool only_dnatted(const struct ip_tunnel *tunnel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) const struct in6_addr *v6dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) prefix_len = tunnel->ip6rd.prefixlen + 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) - tunnel->ip6rd.relay_prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) prefix_len = 48;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /* Returns true if a packet is spoofed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) static bool packet_is_spoofed(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) const struct iphdr *iph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct ip_tunnel *tunnel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) const struct ipv6hdr *ipv6h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (tunnel->dev->priv_flags & IFF_ISATAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (!isatap_chksrc(skb, iph, tunnel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (tunnel->dev->flags & IFF_POINTOPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) ipv6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) &iph->saddr, &ipv6h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) &iph->daddr, &ipv6h->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (only_dnatted(tunnel, &ipv6h->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) &iph->saddr, &ipv6h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) &iph->daddr, &ipv6h->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static int ipip6_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int sifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) iph->saddr, iph->daddr, sifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct pcpu_sw_netstats *tstats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) tunnel->parms.iph.protocol != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) skb->mac_header = skb->network_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) IPCB(skb)->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) skb->dev = tunnel->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (packet_is_spoofed(skb, iph, tunnel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) tunnel->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) !net_eq(tunnel->net, dev_net(tunnel->dev))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /* skb can be uncloned in iptunnel_pull_header, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * old iph is no longer valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) iph = (const struct iphdr *)skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) err = IP_ECN_decapsulate(iph, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (log_ecn_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) &iph->saddr, iph->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (err > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) ++tunnel->dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) ++tunnel->dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) tstats = this_cpu_ptr(tunnel->dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) u64_stats_update_begin(&tstats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) tstats->rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) tstats->rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) u64_stats_update_end(&tstats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) /* no tunnel matched, let upstream know, ipsec may handle it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static const struct tnl_ptk_info ipip_tpi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /* no tunnel info required for ipip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) .proto = htons(ETH_P_IP),
^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 IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static const struct tnl_ptk_info mplsip_tpi = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /* no tunnel info required for mplsip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) .proto = htons(ETH_P_MPLS_UC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) int sifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) iph->saddr, iph->daddr, sifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (tunnel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) const struct tnl_ptk_info *tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (tunnel->parms.iph.protocol != ipproto &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) tunnel->parms.iph.protocol != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (ipproto == IPPROTO_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) tpi = &mplsip_tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) tpi = &ipip_tpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (iptunnel_pull_header(skb, 0, tpi->proto, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static int ipip_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return sit_tunnel_rcv(skb, IPPROTO_IPIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int mplsip_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) return sit_tunnel_rcv(skb, IPPROTO_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * stores the embedded IPv4 address in v4dst and returns true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) __be32 *v4dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) tunnel->ip6rd.prefixlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) unsigned int pbw0, pbi0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) int pbi1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) u32 d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) pbw0 = tunnel->ip6rd.prefixlen >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) d = tunnel->ip6rd.relay_prefixlen < 32 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) tunnel->ip6rd.relay_prefixlen : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (pbi1 > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) (32 - pbi1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (v6dst->s6_addr16[0] == htons(0x2002)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) memcpy(v4dst, &v6dst->s6_addr16[1], 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static inline __be32 try_6rd(struct ip_tunnel *tunnel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) const struct in6_addr *v6dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) __be32 dst = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) check_6rd(tunnel, v6dst, &dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * This function assumes it is being called from dev_queue_xmit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * and that skb is filled properly by that function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) const struct iphdr *tiph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) const struct ipv6hdr *iph6 = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) u8 tos = tunnel->parms.iph.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) __be16 df = tiph->frag_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) struct rtable *rt; /* Route to the other host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct net_device *tdev; /* Device to other host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) unsigned int max_headroom; /* The extra header space needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) __be32 dst = tiph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) const struct in6_addr *addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) u8 ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) u8 protocol = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) int t_hlen = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (tos == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) tos = ipv6_get_dsfield(iph6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /* ISATAP (RFC4214) - must come before 6to4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (dev->priv_flags & IFF_ISATAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) struct neighbour *neigh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) bool do_tx_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (skb_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (!neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) net_dbg_ratelimited("nexthop == NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) goto tx_error;
^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) addr6 = (const struct in6_addr *)&neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if ((addr_type & IPV6_ADDR_UNICAST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) ipv6_addr_is_isatap(addr6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) dst = addr6->s6_addr32[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) do_tx_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (do_tx_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) dst = try_6rd(tunnel, &iph6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct neighbour *neigh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) bool do_tx_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (skb_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (!neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) net_dbg_ratelimited("nexthop == NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) addr6 = (const struct in6_addr *)&neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (addr_type == IPV6_ADDR_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) addr6 = &ipv6_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) addr_type = ipv6_addr_type(addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) dst = addr6->s6_addr32[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) do_tx_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (do_tx_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 0, dst, tiph->saddr, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) sock_net_uid(tunnel->net, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (rt->rt_type != RTN_UNICAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) dev->stats.tx_carrier_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) goto tx_error_icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) tdev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (tdev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (df) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) mtu = dst_mtu(&rt->dst) - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (mtu < 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) dev->stats.collisions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (mtu < IPV6_MIN_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) df = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (tunnel->parms.iph.daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) skb_dst_update_pmtu_no_confirm(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (skb->len > mtu && !skb_is_gso(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^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) if (tunnel->err_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (time_before(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) tunnel->err_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) tunnel->err_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * Okay, now see if we can stuff it in the buffer as-is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (!new_skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (skb->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) skb_set_owner_w(new_skb, skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) skb = new_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) iph6 = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ttl = tiph->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (ttl == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) ttl = iph6->hop_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) skb_set_inner_ipproto(skb, IPPROTO_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) df, !net_eq(tunnel->net, dev_net(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) tx_error_icmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return NETDEV_TX_OK;
^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) static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) struct net_device *dev, u8 ipproto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) const struct iphdr *tiph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) goto tx_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) skb_set_inner_ipproto(skb, ipproto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) ip_tunnel_xmit(skb, dev, tiph, ipproto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) tx_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (!pskb_inet_may_pull(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) switch (skb->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) ipip6_tunnel_xmit(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) case htons(ETH_P_MPLS_UC):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) goto tx_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) tx_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static void ipip6_tunnel_bind_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct net_device *tdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) struct ip_tunnel *tunnel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) if (iph->daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) iph->daddr, iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) IPPROTO_IPV6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) RT_TOS(iph->tos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) tunnel->parms.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) tdev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) dev->flags |= IFF_POINTOPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (!tdev && tunnel->parms.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (tdev && !netif_is_l3_master(tdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int t_hlen = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) dev->mtu = tdev->mtu - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (dev->mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) dev->mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) __u32 fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) ipip6_tunnel_unlink(sitn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) t->parms.iph.saddr = p->iph.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) t->parms.iph.daddr = p->iph.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) memcpy(t->dev->broadcast, &p->iph.daddr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) ipip6_tunnel_link(sitn, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) t->parms.iph.ttl = p->iph.ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) t->parms.iph.tos = p->iph.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) t->parms.iph.frag_off = p->iph.frag_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (t->parms.link != p->link || t->fwmark != fwmark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) t->parms.link = p->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) t->fwmark = fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) ipip6_tunnel_bind_dev(t->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) netdev_state_change(t->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) struct ip_tunnel_6rd *ip6rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) struct in6_addr prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) __be32 relay_prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (ip6rd->relay_prefixlen > 32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (ip6rd->relay_prefixlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) relay_prefix = ip6rd->relay_prefix &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) htonl(0xffffffffUL <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) (32 - ip6rd->relay_prefixlen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) relay_prefix = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (relay_prefix != ip6rd->relay_prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) t->ip6rd.prefix = prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) t->ip6rd.relay_prefix = relay_prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) t->ip6rd.prefixlen = ip6rd->prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) dst_cache_reset(&t->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) netdev_state_change(t->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) return 0;
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) ipip6_tunnel_get6rd(struct net_device *dev, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct ip_tunnel_6rd ip6rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) t = ipip6_tunnel_locate(t->net, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) ip6rd.prefix = t->ip6rd.prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) ip6rd.relay_prefix = t->ip6rd.relay_prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) ip6rd.prefixlen = t->ip6rd.prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd, sizeof(ip6rd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) ipip6_tunnel_6rdctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) struct ip_tunnel_6rd ip6rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data, sizeof(ip6rd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (cmd != SIOCDEL6RD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) err = ipip6_tunnel_update_6rd(t, &ip6rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) ipip6_tunnel_clone_6rd(dev, dev_to_sit_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) return 0;
^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) #endif /* CONFIG_IPV6_SIT_6RD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static bool ipip6_valid_ip_proto(u8 ipproto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) return ipproto == IPPROTO_IPV6 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) ipproto == IPPROTO_IPIP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) ipproto == IPPROTO_MPLS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) ipproto == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) __ipip6_tunnel_ioctl_validate(struct net *net, struct ip_tunnel_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (!ipip6_valid_ip_proto(p->iph.protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (p->iph.version != 4 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) if (p->iph.ttl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) p->iph.frag_off |= htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) ipip6_tunnel_get(struct net_device *dev, struct ip_tunnel_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) t = ipip6_tunnel_locate(t->net, p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) memcpy(p, &t->parms, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) ipip6_tunnel_add(struct net_device *dev, struct ip_tunnel_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) err = __ipip6_tunnel_ioctl_validate(t->net, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) t = ipip6_tunnel_locate(t->net, p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) ipip6_tunnel_change(struct net_device *dev, struct ip_tunnel_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) err = __ipip6_tunnel_ioctl_validate(t->net, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) t = ipip6_tunnel_locate(t->net, p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if (t->dev != dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) if (((dev->flags & IFF_POINTOPOINT) && !p->iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) (!(dev->flags & IFF_POINTOPOINT) && p->iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) ipip6_tunnel_update(t, p, t->fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) ipip6_tunnel_del(struct net_device *dev, struct ip_tunnel_parm *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) t = ipip6_tunnel_locate(t->net, p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (t == netdev_priv(dev_to_sit_net(dev)->fb_tunnel_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) dev = t->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) ipip6_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) case SIOCGETTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) return ipip6_tunnel_get(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) case SIOCADDTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) return ipip6_tunnel_add(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) case SIOCCHGTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) return ipip6_tunnel_change(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) case SIOCDELTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) return ipip6_tunnel_del(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) case SIOCGETTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) case SIOCADDTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) case SIOCCHGTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) case SIOCDELTUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) return ip_tunnel_ioctl(dev, ifr, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) case SIOCGETPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) return ipip6_tunnel_get_prl(dev, ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) case SIOCADDPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) case SIOCDELPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) case SIOCCHGPRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) return ipip6_tunnel_prl_ctl(dev, ifr, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) case SIOCGET6RD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) return ipip6_tunnel_get6rd(dev, ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) case SIOCADD6RD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) case SIOCCHG6RD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) case SIOCDEL6RD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) return ipip6_tunnel_6rdctl(dev, ifr, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) static const struct net_device_ops ipip6_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) .ndo_init = ipip6_tunnel_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) .ndo_uninit = ipip6_tunnel_uninit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) .ndo_start_xmit = sit_tunnel_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) .ndo_do_ioctl = ipip6_tunnel_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) .ndo_get_stats64 = ip_tunnel_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) .ndo_get_iflink = ip_tunnel_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) .ndo_tunnel_ctl = ipip6_tunnel_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) static void ipip6_dev_free(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) dst_cache_destroy(&tunnel->dst_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) #define SIT_FEATURES (NETIF_F_SG | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) NETIF_F_FRAGLIST | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) NETIF_F_HIGHDMA | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) NETIF_F_GSO_SOFTWARE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) NETIF_F_HW_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) static void ipip6_tunnel_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) int t_hlen = tunnel->hlen + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) dev->netdev_ops = &ipip6_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) dev->header_ops = &ip_tunnel_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) dev->priv_destructor = ipip6_dev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) dev->type = ARPHRD_SIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) dev->mtu = ETH_DATA_LEN - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) dev->min_mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) dev->max_mtu = IP6_MAX_MTU - t_hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) dev->addr_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) dev->features |= SIT_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) dev->hw_features |= SIT_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) static int ipip6_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) tunnel->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) tunnel->net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) strcpy(tunnel->parms.name, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) ipip6_tunnel_bind_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) if (!dev->tstats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) free_percpu(dev->tstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) dev->tstats = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct iphdr *iph = &tunnel->parms.iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) iph->version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) iph->protocol = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) iph->ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) iph->ttl = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) u8 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (!data || !data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (!ipip6_valid_ip_proto(proto))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) static void ipip6_netlink_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) struct ip_tunnel_parm *parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) __u32 *fwmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) memset(parms, 0, sizeof(*parms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) parms->iph.version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) parms->iph.protocol = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) parms->iph.ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) parms->iph.ttl = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (data[IFLA_IPTUN_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (data[IFLA_IPTUN_LOCAL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (data[IFLA_IPTUN_REMOTE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) if (data[IFLA_IPTUN_TTL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) if (parms->iph.ttl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) parms->iph.frag_off = htons(IP_DF);
^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 (data[IFLA_IPTUN_TOS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) parms->iph.frag_off = htons(IP_DF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) if (data[IFLA_IPTUN_FLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) if (data[IFLA_IPTUN_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (data[IFLA_IPTUN_FWMARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
^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) /* This function returns true when ENCAP attributes are present in the nl msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) static bool ipip6_netlink_encap_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) struct ip_tunnel_encap *ipencap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) memset(ipencap, 0, sizeof(*ipencap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (data[IFLA_IPTUN_ENCAP_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) if (data[IFLA_IPTUN_ENCAP_SPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) if (data[IFLA_IPTUN_ENCAP_DPORT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) /* This function returns true when 6RD attributes are present in the nl msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) struct ip_tunnel_6rd *ip6rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) memset(ip6rd, 0, sizeof(*ip6rd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (data[IFLA_IPTUN_6RD_PREFIX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) ip6rd->relay_prefix =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) ip6rd->relay_prefixlen =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) static int ipip6_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) struct ip_tunnel *nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) struct ip_tunnel_6rd ip6rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) nt = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) if (ipip6_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) err = ip_tunnel_encap_setup(nt, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) if (ipip6_tunnel_locate(net, &nt->parms, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) err = ipip6_tunnel_create(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) if (tb[IFLA_MTU]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) u32 mtu = nla_get_u32(tb[IFLA_MTU]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) if (mtu >= IPV6_MIN_MTU &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) mtu <= IP6_MAX_MTU - dev->hard_header_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) dev->mtu = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) if (ipip6_netlink_6rd_parms(data, &ip6rd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) err = ipip6_tunnel_update_6rd(nt, &ip6rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) unregister_netdevice_queue(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) struct ip_tunnel *t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) struct ip_tunnel_parm p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) struct ip_tunnel_encap ipencap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) struct net *net = t->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) struct ip_tunnel_6rd ip6rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) __u32 fwmark = t->fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (dev == sitn->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) if (ipip6_netlink_encap_parms(data, &ipencap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) err = ip_tunnel_encap_setup(t, &ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) ipip6_netlink_parms(data, &p, &fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) t = ipip6_tunnel_locate(net, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (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) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) t = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) ipip6_tunnel_update(t, &p, fwmark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) if (ipip6_netlink_6rd_parms(data, &ip6rd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) return ipip6_tunnel_update_6rd(t, &ip6rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) static size_t ipip6_get_size(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) /* IFLA_IPTUN_LINK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) /* IFLA_IPTUN_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) /* IFLA_IPTUN_REMOTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) /* IFLA_IPTUN_TTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) /* IFLA_IPTUN_TOS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) /* IFLA_IPTUN_PMTUDISC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) /* IFLA_IPTUN_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) /* IFLA_IPTUN_PROTO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) nla_total_size(1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) /* IFLA_IPTUN_6RD_PREFIX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) nla_total_size(sizeof(struct in6_addr)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) /* IFLA_IPTUN_6RD_RELAY_PREFIX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) /* IFLA_IPTUN_6RD_PREFIXLEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) /* IFLA_IPTUN_ENCAP_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) /* IFLA_IPTUN_ENCAP_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) /* IFLA_IPTUN_ENCAP_SPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) /* IFLA_IPTUN_ENCAP_DPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) nla_total_size(2) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) /* IFLA_IPTUN_FWMARK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) nla_total_size(4) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) struct ip_tunnel *tunnel = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) struct ip_tunnel_parm *parm = &tunnel->parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) !!(parm->iph.frag_off & htons(IP_DF))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) &tunnel->ip6rd.prefix) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) tunnel->ip6rd.relay_prefix) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) tunnel->ip6rd.prefixlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) tunnel->ip6rd.relay_prefixlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) tunnel->encap.type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) tunnel->encap.sport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) tunnel->encap.dport) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) tunnel->encap.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) #ifdef CONFIG_IPV6_SIT_6RD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) static void ipip6_dellink(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) if (dev != sitn->fb_tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) static struct rtnl_link_ops sit_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) .kind = "sit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) .maxtype = IFLA_IPTUN_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) .policy = ipip6_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) .priv_size = sizeof(struct ip_tunnel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) .setup = ipip6_tunnel_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) .validate = ipip6_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) .newlink = ipip6_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) .changelink = ipip6_changelink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) .get_size = ipip6_get_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) .fill_info = ipip6_fill_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) .dellink = ipip6_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) .get_link_net = ip_tunnel_get_link_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) static struct xfrm_tunnel sit_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) .handler = ipip6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) .err_handler = ipip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) .priority = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) static struct xfrm_tunnel ipip_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) .handler = ipip_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) .err_handler = ipip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) .priority = 2,
^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) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) static struct xfrm_tunnel mplsip_handler __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) .handler = mplsip_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) .err_handler = ipip6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) .priority = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) static void __net_exit sit_destroy_tunnels(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) struct net_device *dev, *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) int prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) for_each_netdev_safe(net, dev, aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) if (dev->rtnl_link_ops == &sit_link_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) for (prio = 0; prio < 4; prio++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) t = rtnl_dereference(sitn->tunnels[prio][h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) while (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) /* If dev is in the same netns, it has already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) * been added to the list by the previous loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) if (!net_eq(dev_net(t->dev), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) unregister_netdevice_queue(t->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) t = rtnl_dereference(t->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) static int __net_init sit_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) struct sit_net *sitn = net_generic(net, sit_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) struct ip_tunnel *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) sitn->tunnels[0] = sitn->tunnels_wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) sitn->tunnels[1] = sitn->tunnels_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) sitn->tunnels[2] = sitn->tunnels_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) sitn->tunnels[3] = sitn->tunnels_r_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) if (!net_has_fallback_tunnels(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) ipip6_tunnel_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (!sitn->fb_tunnel_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) goto err_alloc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) dev_net_set(sitn->fb_tunnel_dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) /* FB netdevice is special: we have one, and only one per netns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) * Allowing to move it to another netns is clearly unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) err = register_netdev(sitn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) goto err_reg_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) t = netdev_priv(sitn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) err_reg_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) free_netdev(sitn->fb_tunnel_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) err_alloc_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) return err;
^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 void __net_exit sit_exit_batch_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) list_for_each_entry(net, net_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) sit_destroy_tunnels(net, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) rtnl_unlock();
^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) static struct pernet_operations sit_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) .init = sit_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) .exit_batch = sit_exit_batch_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) .id = &sit_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) .size = sizeof(struct sit_net),
^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 void __exit sit_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) rtnl_link_unregister(&sit_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) unregister_pernet_device(&sit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) rcu_barrier(); /* Wait for completion of call_rcu()'s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) static int __init sit_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) err = register_pernet_device(&sit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) pr_info("%s: can't register ip6ip4\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) goto xfrm_tunnel_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) pr_info("%s: can't register ip4ip4\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) goto xfrm_tunnel4_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) pr_info("%s: can't register mplsip\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) goto xfrm_tunnel_mpls_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) err = rtnl_link_register(&sit_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) goto rtnl_link_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) rtnl_link_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) #if IS_ENABLED(CONFIG_MPLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) xfrm_tunnel_mpls_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) xfrm_tunnel4_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) xfrm_tunnel_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) unregister_pernet_device(&sit_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) module_init(sit_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) module_exit(sit_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) MODULE_ALIAS_RTNL_LINK("sit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) MODULE_ALIAS_NETDEV("sit0");