Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 2 | /* |
| 3 | * GENEVE: Generic Network Virtualization Encapsulation |
| 4 | * |
| 5 | * Copyright (c) 2015 Red Hat, Inc. |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 12 | #include <linux/etherdevice.h> |
| 13 | #include <linux/hash.h> |
David Ahern | 3616d08 | 2019-03-22 06:06:09 -0700 | [diff] [blame] | 14 | #include <net/ipv6_stubs.h> |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 15 | #include <net/dst_metadata.h> |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 16 | #include <net/gro_cells.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 17 | #include <net/rtnetlink.h> |
| 18 | #include <net/geneve.h> |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 19 | #include <net/protocol.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 20 | |
| 21 | #define GENEVE_NETDEV_VER "0.6" |
| 22 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 23 | #define GENEVE_N_VID (1u << 24) |
| 24 | #define GENEVE_VID_MASK (GENEVE_N_VID - 1) |
| 25 | |
| 26 | #define VNI_HASH_BITS 10 |
| 27 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 28 | |
| 29 | static bool log_ecn_error = true; |
| 30 | module_param(log_ecn_error, bool, 0644); |
| 31 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 32 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 33 | #define GENEVE_VER 0 |
| 34 | #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) |
Alexey Kodanev | 5edbea6 | 2018-04-19 15:42:30 +0300 | [diff] [blame] | 35 | #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN) |
| 36 | #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 37 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 38 | /* per-network namespace private data for this module */ |
| 39 | struct geneve_net { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 40 | struct list_head geneve_list; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 41 | struct list_head sock_list; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 42 | }; |
| 43 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 44 | static unsigned int geneve_net_id; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 45 | |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 46 | struct geneve_dev_node { |
| 47 | struct hlist_node hlist; |
| 48 | struct geneve_dev *geneve; |
| 49 | }; |
| 50 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 51 | struct geneve_config { |
| 52 | struct ip_tunnel_info info; |
| 53 | bool collect_md; |
| 54 | bool use_udp6_rx_checksums; |
| 55 | bool ttl_inherit; |
| 56 | enum ifla_geneve_df df; |
| 57 | }; |
| 58 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 59 | /* Pseudo network device */ |
| 60 | struct geneve_dev { |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 61 | struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */ |
| 62 | #if IS_ENABLED(CONFIG_IPV6) |
| 63 | struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */ |
| 64 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 65 | struct net *net; /* netns for packet i/o */ |
| 66 | struct net_device *dev; /* netdev for geneve tunnel */ |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 67 | struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 68 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 69 | struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 70 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 71 | struct list_head next; /* geneve's per namespace list */ |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 72 | struct gro_cells gro_cells; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 73 | struct geneve_config cfg; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 74 | }; |
| 75 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 76 | struct geneve_sock { |
| 77 | bool collect_md; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 78 | struct list_head list; |
| 79 | struct socket *sock; |
| 80 | struct rcu_head rcu; |
| 81 | int refcnt; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 82 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 83 | }; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 84 | |
| 85 | static inline __u32 geneve_net_vni_hash(u8 vni[3]) |
| 86 | { |
| 87 | __u32 vnid; |
| 88 | |
| 89 | vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 90 | return hash_32(vnid, VNI_HASH_BITS); |
| 91 | } |
| 92 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 93 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
| 94 | { |
| 95 | #ifdef __BIG_ENDIAN |
| 96 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 97 | #else |
| 98 | return (__force __be64)(((__force u64)vni[0] << 40) | |
| 99 | ((__force u64)vni[1] << 48) | |
| 100 | ((__force u64)vni[2] << 56)); |
| 101 | #endif |
| 102 | } |
| 103 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 104 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
| 105 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) |
| 106 | { |
| 107 | #ifdef __BIG_ENDIAN |
| 108 | vni[0] = (__force __u8)(tun_id >> 16); |
| 109 | vni[1] = (__force __u8)(tun_id >> 8); |
| 110 | vni[2] = (__force __u8)tun_id; |
| 111 | #else |
| 112 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); |
| 113 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); |
| 114 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); |
| 115 | #endif |
| 116 | } |
| 117 | |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 118 | static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni) |
| 119 | { |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 120 | return !memcmp(vni, &tun_id[5], 3); |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 123 | static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) |
| 124 | { |
| 125 | return gs->sock->sk->sk_family; |
| 126 | } |
| 127 | |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 128 | static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 129 | __be32 addr, u8 vni[]) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 130 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 131 | struct hlist_head *vni_list_head; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 132 | struct geneve_dev_node *node; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 133 | __u32 hash; |
| 134 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 135 | /* Find the device for this VNI */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 136 | hash = geneve_net_vni_hash(vni); |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 137 | vni_list_head = &gs->vni_list[hash]; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 138 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 139 | if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) && |
| 140 | addr == node->geneve->cfg.info.key.u.ipv4.dst) |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 141 | return node->geneve; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 142 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 146 | #if IS_ENABLED(CONFIG_IPV6) |
| 147 | static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, |
| 148 | struct in6_addr addr6, u8 vni[]) |
| 149 | { |
| 150 | struct hlist_head *vni_list_head; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 151 | struct geneve_dev_node *node; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 152 | __u32 hash; |
| 153 | |
| 154 | /* Find the device for this VNI */ |
| 155 | hash = geneve_net_vni_hash(vni); |
| 156 | vni_list_head = &gs->vni_list[hash]; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 157 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 158 | if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) && |
| 159 | ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst)) |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 160 | return node->geneve; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | #endif |
| 165 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 166 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
| 167 | { |
| 168 | return (struct genevehdr *)(udp_hdr(skb) + 1); |
| 169 | } |
| 170 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 171 | static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, |
| 172 | struct sk_buff *skb) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 173 | { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 174 | static u8 zero_vni[3]; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 175 | u8 *vni; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 176 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 177 | if (geneve_get_sk_family(gs) == AF_INET) { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 178 | struct iphdr *iph; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 179 | __be32 addr; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 180 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 181 | iph = ip_hdr(skb); /* outer IP header... */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 182 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 183 | if (gs->collect_md) { |
| 184 | vni = zero_vni; |
| 185 | addr = 0; |
| 186 | } else { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 187 | vni = geneve_hdr(skb)->vni; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 188 | addr = iph->saddr; |
| 189 | } |
| 190 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 191 | return geneve_lookup(gs, addr, vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 192 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 193 | } else if (geneve_get_sk_family(gs) == AF_INET6) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 194 | static struct in6_addr zero_addr6; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 195 | struct ipv6hdr *ip6h; |
| 196 | struct in6_addr addr6; |
| 197 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 198 | ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ |
| 199 | |
| 200 | if (gs->collect_md) { |
| 201 | vni = zero_vni; |
| 202 | addr6 = zero_addr6; |
| 203 | } else { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 204 | vni = geneve_hdr(skb)->vni; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 205 | addr6 = ip6h->saddr; |
| 206 | } |
| 207 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 208 | return geneve6_lookup(gs, addr6, vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 209 | #endif |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 210 | } |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | /* geneve receive/decap routine */ |
| 215 | static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, |
| 216 | struct sk_buff *skb) |
| 217 | { |
| 218 | struct genevehdr *gnvh = geneve_hdr(skb); |
| 219 | struct metadata_dst *tun_dst = NULL; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 220 | unsigned int len; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 221 | int err = 0; |
| 222 | void *oiph; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 223 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 224 | if (ip_tunnel_collect_metadata() || gs->collect_md) { |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 225 | __be16 flags; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 226 | |
| 227 | flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | |
| 228 | (gnvh->oam ? TUNNEL_OAM : 0) | |
| 229 | (gnvh->critical ? TUNNEL_CRIT_OPT : 0); |
| 230 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 231 | tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 232 | vni_to_tunnel_id(gnvh->vni), |
| 233 | gnvh->opt_len * 4); |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 234 | if (!tun_dst) { |
| 235 | geneve->dev->stats.rx_dropped++; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 236 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 237 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 238 | /* Update tunnel dst according to Geneve options. */ |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 239 | ip_tunnel_info_opts_set(&tun_dst->u.tun_info, |
Pieter Jansen van Vuuren | 256c87c | 2018-06-26 21:39:36 -0700 | [diff] [blame] | 240 | gnvh->options, gnvh->opt_len * 4, |
| 241 | TUNNEL_GENEVE_OPT); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 242 | } else { |
| 243 | /* Drop packets w/ critical options, |
| 244 | * since we don't support any... |
| 245 | */ |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 246 | if (gnvh->critical) { |
| 247 | geneve->dev->stats.rx_frame_errors++; |
| 248 | geneve->dev->stats.rx_errors++; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 249 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 250 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 251 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 252 | |
| 253 | skb_reset_mac_header(skb); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 254 | skb->protocol = eth_type_trans(skb, geneve->dev); |
| 255 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 256 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 257 | if (tun_dst) |
| 258 | skb_dst_set(skb, &tun_dst->dst); |
| 259 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 260 | /* Ignore packet loops (and multicast echo) */ |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 261 | if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) { |
| 262 | geneve->dev->stats.rx_errors++; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 263 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 264 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 265 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 266 | oiph = skb_network_header(skb); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 267 | skb_reset_network_header(skb); |
| 268 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 269 | if (geneve_get_sk_family(gs) == AF_INET) |
| 270 | err = IP_ECN_decapsulate(oiph, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 271 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 272 | else |
| 273 | err = IP6_ECN_decapsulate(oiph, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 274 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 275 | |
| 276 | if (unlikely(err)) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 277 | if (log_ecn_error) { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 278 | if (geneve_get_sk_family(gs) == AF_INET) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 279 | net_info_ratelimited("non-ECT from %pI4 " |
| 280 | "with TOS=%#x\n", |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 281 | &((struct iphdr *)oiph)->saddr, |
| 282 | ((struct iphdr *)oiph)->tos); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 283 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 284 | else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 285 | net_info_ratelimited("non-ECT from %pI6\n", |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 286 | &((struct ipv6hdr *)oiph)->saddr); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 287 | #endif |
| 288 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 289 | if (err > 1) { |
| 290 | ++geneve->dev->stats.rx_frame_errors; |
| 291 | ++geneve->dev->stats.rx_errors; |
| 292 | goto drop; |
| 293 | } |
| 294 | } |
| 295 | |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 296 | len = skb->len; |
| 297 | err = gro_cells_receive(&geneve->gro_cells, skb); |
Fabian Frederick | 1e84527 | 2020-10-05 22:34:58 +0200 | [diff] [blame] | 298 | if (likely(err == NET_RX_SUCCESS)) |
| 299 | dev_sw_netstats_rx_add(geneve->dev, len); |
| 300 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 301 | return; |
| 302 | drop: |
| 303 | /* Consume bad packet */ |
| 304 | kfree_skb(skb); |
| 305 | } |
| 306 | |
| 307 | /* Setup stats when device is created */ |
| 308 | static int geneve_init(struct net_device *dev) |
| 309 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 310 | struct geneve_dev *geneve = netdev_priv(dev); |
| 311 | int err; |
| 312 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 313 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 314 | if (!dev->tstats) |
| 315 | return -ENOMEM; |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 316 | |
| 317 | err = gro_cells_init(&geneve->gro_cells, dev); |
| 318 | if (err) { |
| 319 | free_percpu(dev->tstats); |
| 320 | return err; |
| 321 | } |
| 322 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 323 | err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 324 | if (err) { |
| 325 | free_percpu(dev->tstats); |
| 326 | gro_cells_destroy(&geneve->gro_cells); |
| 327 | return err; |
| 328 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void geneve_uninit(struct net_device *dev) |
| 333 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 334 | struct geneve_dev *geneve = netdev_priv(dev); |
| 335 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 336 | dst_cache_destroy(&geneve->cfg.info.dst_cache); |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 337 | gro_cells_destroy(&geneve->gro_cells); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 338 | free_percpu(dev->tstats); |
| 339 | } |
| 340 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 341 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 342 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 343 | { |
| 344 | struct genevehdr *geneveh; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 345 | struct geneve_dev *geneve; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 346 | struct geneve_sock *gs; |
| 347 | int opts_len; |
| 348 | |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 349 | /* Need UDP and Geneve header to be present */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 350 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 351 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 352 | |
| 353 | /* Return packets with reserved bits set */ |
| 354 | geneveh = geneve_hdr(skb); |
| 355 | if (unlikely(geneveh->ver != GENEVE_VER)) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 356 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 357 | |
| 358 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 359 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 360 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 361 | gs = rcu_dereference_sk_user_data(sk); |
| 362 | if (!gs) |
| 363 | goto drop; |
| 364 | |
| 365 | geneve = geneve_lookup_skb(gs, skb); |
| 366 | if (!geneve) |
| 367 | goto drop; |
| 368 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 369 | opts_len = geneveh->opt_len * 4; |
| 370 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, |
Jiri Benc | 7f290c9 | 2016-02-18 11:22:52 +0100 | [diff] [blame] | 371 | htons(ETH_P_TEB), |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 372 | !net_eq(geneve->net, dev_net(geneve->dev)))) { |
| 373 | geneve->dev->stats.rx_dropped++; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 374 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 375 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 376 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 377 | geneve_rx(geneve, gs, skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | |
| 380 | drop: |
| 381 | /* Consume bad packet */ |
| 382 | kfree_skb(skb); |
| 383 | return 0; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame] | 386 | /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */ |
| 387 | static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb) |
| 388 | { |
| 389 | struct genevehdr *geneveh; |
| 390 | struct geneve_sock *gs; |
| 391 | u8 zero_vni[3] = { 0 }; |
| 392 | u8 *vni = zero_vni; |
| 393 | |
Stefano Brivio | eccc73a | 2019-06-11 00:27:06 +0200 | [diff] [blame] | 394 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN)) |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame] | 395 | return -EINVAL; |
| 396 | |
| 397 | geneveh = geneve_hdr(skb); |
| 398 | if (geneveh->ver != GENEVE_VER) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | if (geneveh->proto_type != htons(ETH_P_TEB)) |
| 402 | return -EINVAL; |
| 403 | |
| 404 | gs = rcu_dereference_sk_user_data(sk); |
| 405 | if (!gs) |
| 406 | return -ENOENT; |
| 407 | |
| 408 | if (geneve_get_sk_family(gs) == AF_INET) { |
| 409 | struct iphdr *iph = ip_hdr(skb); |
| 410 | __be32 addr4 = 0; |
| 411 | |
| 412 | if (!gs->collect_md) { |
| 413 | vni = geneve_hdr(skb)->vni; |
| 414 | addr4 = iph->daddr; |
| 415 | } |
| 416 | |
| 417 | return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT; |
| 418 | } |
| 419 | |
| 420 | #if IS_ENABLED(CONFIG_IPV6) |
| 421 | if (geneve_get_sk_family(gs) == AF_INET6) { |
| 422 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
Nathan Chancellor | 8a962c4 | 2018-11-16 18:36:27 -0700 | [diff] [blame] | 423 | struct in6_addr addr6; |
| 424 | |
| 425 | memset(&addr6, 0, sizeof(struct in6_addr)); |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame] | 426 | |
| 427 | if (!gs->collect_md) { |
| 428 | vni = geneve_hdr(skb)->vni; |
| 429 | addr6 = ip6h->daddr; |
| 430 | } |
| 431 | |
| 432 | return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT; |
| 433 | } |
| 434 | #endif |
| 435 | |
| 436 | return -EPFNOSUPPORT; |
| 437 | } |
| 438 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 439 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 440 | __be16 port, bool ipv6_rx_csum) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 441 | { |
| 442 | struct socket *sock; |
| 443 | struct udp_port_cfg udp_conf; |
| 444 | int err; |
| 445 | |
| 446 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 447 | |
| 448 | if (ipv6) { |
| 449 | udp_conf.family = AF_INET6; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 450 | udp_conf.ipv6_v6only = 1; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 451 | udp_conf.use_udp6_rx_checksums = ipv6_rx_csum; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 452 | } else { |
| 453 | udp_conf.family = AF_INET; |
| 454 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 455 | } |
| 456 | |
| 457 | udp_conf.local_udp_port = port; |
| 458 | |
| 459 | /* Open UDP socket */ |
| 460 | err = udp_sock_create(net, &udp_conf, &sock); |
| 461 | if (err < 0) |
| 462 | return ERR_PTR(err); |
| 463 | |
| 464 | return sock; |
| 465 | } |
| 466 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 467 | static int geneve_hlen(struct genevehdr *gh) |
| 468 | { |
| 469 | return sizeof(*gh) + gh->opt_len * 4; |
| 470 | } |
| 471 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 472 | static struct sk_buff *geneve_gro_receive(struct sock *sk, |
| 473 | struct list_head *head, |
| 474 | struct sk_buff *skb) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 475 | { |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 476 | struct sk_buff *pp = NULL; |
| 477 | struct sk_buff *p; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 478 | struct genevehdr *gh, *gh2; |
| 479 | unsigned int hlen, gh_len, off_gnv; |
| 480 | const struct packet_offload *ptype; |
| 481 | __be16 type; |
| 482 | int flush = 1; |
| 483 | |
| 484 | off_gnv = skb_gro_offset(skb); |
| 485 | hlen = off_gnv + sizeof(*gh); |
| 486 | gh = skb_gro_header_fast(skb, off_gnv); |
| 487 | if (skb_gro_header_hard(skb, hlen)) { |
| 488 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 489 | if (unlikely(!gh)) |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | if (gh->ver != GENEVE_VER || gh->oam) |
| 494 | goto out; |
| 495 | gh_len = geneve_hlen(gh); |
| 496 | |
| 497 | hlen = off_gnv + gh_len; |
| 498 | if (skb_gro_header_hard(skb, hlen)) { |
| 499 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 500 | if (unlikely(!gh)) |
| 501 | goto out; |
| 502 | } |
| 503 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 504 | list_for_each_entry(p, head, list) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 505 | if (!NAPI_GRO_CB(p)->same_flow) |
| 506 | continue; |
| 507 | |
| 508 | gh2 = (struct genevehdr *)(p->data + off_gnv); |
| 509 | if (gh->opt_len != gh2->opt_len || |
| 510 | memcmp(gh, gh2, gh_len)) { |
| 511 | NAPI_GRO_CB(p)->same_flow = 0; |
| 512 | continue; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | type = gh->proto_type; |
| 517 | |
| 518 | rcu_read_lock(); |
| 519 | ptype = gro_find_receive_by_type(type); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 520 | if (!ptype) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 521 | goto out_unlock; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 522 | |
| 523 | skb_gro_pull(skb, gh_len); |
| 524 | skb_gro_postpull_rcsum(skb, gh, gh_len); |
Sabrina Dubroca | fcd91dd | 2016-10-20 15:58:02 +0200 | [diff] [blame] | 525 | pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 526 | flush = 0; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 527 | |
| 528 | out_unlock: |
| 529 | rcu_read_unlock(); |
| 530 | out: |
Sabrina Dubroca | 603d4cf | 2018-06-30 17:38:55 +0200 | [diff] [blame] | 531 | skb_gro_flush_final(skb, pp, flush); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 532 | |
| 533 | return pp; |
| 534 | } |
| 535 | |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 536 | static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, |
| 537 | int nhoff) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 538 | { |
| 539 | struct genevehdr *gh; |
| 540 | struct packet_offload *ptype; |
| 541 | __be16 type; |
| 542 | int gh_len; |
| 543 | int err = -ENOSYS; |
| 544 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 545 | gh = (struct genevehdr *)(skb->data + nhoff); |
| 546 | gh_len = geneve_hlen(gh); |
| 547 | type = gh->proto_type; |
| 548 | |
| 549 | rcu_read_lock(); |
| 550 | ptype = gro_find_complete_by_type(type); |
| 551 | if (ptype) |
| 552 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); |
| 553 | |
| 554 | rcu_read_unlock(); |
Jarno Rajahalme | 229740c | 2016-05-03 16:10:21 -0700 | [diff] [blame] | 555 | |
| 556 | skb_set_inner_mac_header(skb, nhoff + gh_len); |
| 557 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 558 | return err; |
| 559 | } |
| 560 | |
| 561 | /* Create new listen socket if needed */ |
| 562 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 563 | bool ipv6, bool ipv6_rx_csum) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 564 | { |
| 565 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 566 | struct geneve_sock *gs; |
| 567 | struct socket *sock; |
| 568 | struct udp_tunnel_sock_cfg tunnel_cfg; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 569 | int h; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 570 | |
| 571 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); |
| 572 | if (!gs) |
| 573 | return ERR_PTR(-ENOMEM); |
| 574 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 575 | sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 576 | if (IS_ERR(sock)) { |
| 577 | kfree(gs); |
| 578 | return ERR_CAST(sock); |
| 579 | } |
| 580 | |
| 581 | gs->sock = sock; |
| 582 | gs->refcnt = 1; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 583 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 584 | INIT_HLIST_HEAD(&gs->vni_list[h]); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 585 | |
| 586 | /* Initialize the geneve udp offloads structure */ |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 587 | udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 588 | |
| 589 | /* Mark socket as an encapsulation socket */ |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 590 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 591 | tunnel_cfg.sk_user_data = gs; |
| 592 | tunnel_cfg.encap_type = 1; |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 593 | tunnel_cfg.gro_receive = geneve_gro_receive; |
| 594 | tunnel_cfg.gro_complete = geneve_gro_complete; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 595 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame] | 596 | tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 597 | tunnel_cfg.encap_destroy = NULL; |
| 598 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 599 | list_add(&gs->list, &gn->sock_list); |
| 600 | return gs; |
| 601 | } |
| 602 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 603 | static void __geneve_sock_release(struct geneve_sock *gs) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 604 | { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 605 | if (!gs || --gs->refcnt) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 606 | return; |
| 607 | |
| 608 | list_del(&gs->list); |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 609 | udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 610 | udp_tunnel_sock_release(gs->sock); |
| 611 | kfree_rcu(gs, rcu); |
| 612 | } |
| 613 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 614 | static void geneve_sock_release(struct geneve_dev *geneve) |
| 615 | { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 616 | struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 617 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 618 | struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6); |
| 619 | |
| 620 | rcu_assign_pointer(geneve->sock6, NULL); |
| 621 | #endif |
| 622 | |
| 623 | rcu_assign_pointer(geneve->sock4, NULL); |
| 624 | synchronize_net(); |
| 625 | |
| 626 | __geneve_sock_release(gs4); |
| 627 | #if IS_ENABLED(CONFIG_IPV6) |
| 628 | __geneve_sock_release(gs6); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 629 | #endif |
| 630 | } |
| 631 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 632 | static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 633 | sa_family_t family, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 634 | __be16 dst_port) |
| 635 | { |
| 636 | struct geneve_sock *gs; |
| 637 | |
| 638 | list_for_each_entry(gs, &gn->sock_list, list) { |
| 639 | if (inet_sk(gs->sock->sk)->inet_sport == dst_port && |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 640 | geneve_get_sk_family(gs) == family) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 641 | return gs; |
| 642 | } |
| 643 | } |
| 644 | return NULL; |
| 645 | } |
| 646 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 647 | static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 648 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 649 | struct net *net = geneve->net; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 650 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 651 | struct geneve_dev_node *node; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 652 | struct geneve_sock *gs; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 653 | __u8 vni[3]; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 654 | __u32 hash; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 655 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 656 | gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 657 | if (gs) { |
| 658 | gs->refcnt++; |
| 659 | goto out; |
| 660 | } |
| 661 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 662 | gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6, |
| 663 | geneve->cfg.use_udp6_rx_checksums); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 664 | if (IS_ERR(gs)) |
| 665 | return PTR_ERR(gs); |
| 666 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 667 | out: |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 668 | gs->collect_md = geneve->cfg.collect_md; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 669 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 670 | if (ipv6) { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 671 | rcu_assign_pointer(geneve->sock6, gs); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 672 | node = &geneve->hlist6; |
| 673 | } else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 674 | #endif |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 675 | { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 676 | rcu_assign_pointer(geneve->sock4, gs); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 677 | node = &geneve->hlist4; |
| 678 | } |
| 679 | node->geneve = geneve; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 680 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 681 | tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 682 | hash = geneve_net_vni_hash(vni); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 683 | hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 687 | static int geneve_open(struct net_device *dev) |
| 688 | { |
| 689 | struct geneve_dev *geneve = netdev_priv(dev); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 690 | bool metadata = geneve->cfg.collect_md; |
Jiri Benc | cf1c9cc | 2019-02-28 14:56:04 +0100 | [diff] [blame] | 691 | bool ipv4, ipv6; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 692 | int ret = 0; |
| 693 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 694 | ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata; |
Jiri Benc | cf1c9cc | 2019-02-28 14:56:04 +0100 | [diff] [blame] | 695 | ipv4 = !ipv6 || metadata; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 696 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | cf1c9cc | 2019-02-28 14:56:04 +0100 | [diff] [blame] | 697 | if (ipv6) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 698 | ret = geneve_sock_add(geneve, true); |
Jiri Benc | cf1c9cc | 2019-02-28 14:56:04 +0100 | [diff] [blame] | 699 | if (ret < 0 && ret != -EAFNOSUPPORT) |
| 700 | ipv4 = false; |
| 701 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 702 | #endif |
Jiri Benc | cf1c9cc | 2019-02-28 14:56:04 +0100 | [diff] [blame] | 703 | if (ipv4) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 704 | ret = geneve_sock_add(geneve, false); |
| 705 | if (ret < 0) |
| 706 | geneve_sock_release(geneve); |
| 707 | |
| 708 | return ret; |
| 709 | } |
| 710 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 711 | static int geneve_stop(struct net_device *dev) |
| 712 | { |
| 713 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 714 | |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 715 | hlist_del_init_rcu(&geneve->hlist4.hlist); |
| 716 | #if IS_ENABLED(CONFIG_IPV6) |
| 717 | hlist_del_init_rcu(&geneve->hlist6.hlist); |
| 718 | #endif |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 719 | geneve_sock_release(geneve); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 723 | static void geneve_build_header(struct genevehdr *geneveh, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 724 | const struct ip_tunnel_info *info) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 725 | { |
| 726 | geneveh->ver = GENEVE_VER; |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 727 | geneveh->opt_len = info->options_len / 4; |
| 728 | geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM); |
| 729 | geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 730 | geneveh->rsvd1 = 0; |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 731 | tunnel_id_to_vni(info->key.tun_id, geneveh->vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 732 | geneveh->proto_type = htons(ETH_P_TEB); |
| 733 | geneveh->rsvd2 = 0; |
| 734 | |
Pieter Jansen van Vuuren | 256c87c | 2018-06-26 21:39:36 -0700 | [diff] [blame] | 735 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) |
| 736 | ip_tunnel_info_opts_get(geneveh->options, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 737 | } |
| 738 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 739 | static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, |
| 740 | const struct ip_tunnel_info *info, |
| 741 | bool xnet, int ip_hdr_len) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 742 | { |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 743 | bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 744 | struct genevehdr *gnvh; |
| 745 | int min_headroom; |
| 746 | int err; |
| 747 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 748 | skb_reset_mac_header(skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 749 | skb_scrub_packet(skb, xnet); |
| 750 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 751 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + |
| 752 | GENEVE_BASE_HLEN + info->options_len + ip_hdr_len; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 753 | err = skb_cow_head(skb, min_headroom); |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 754 | if (unlikely(err)) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 755 | goto free_dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 756 | |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 757 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
Dan Carpenter | 1ba64fa | 2016-04-19 17:30:56 +0300 | [diff] [blame] | 758 | if (err) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 759 | goto free_dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 760 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 761 | gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len); |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 762 | geneve_build_header(gnvh, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 763 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 764 | return 0; |
| 765 | |
| 766 | free_dst: |
| 767 | dst_release(dst); |
| 768 | return err; |
| 769 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 770 | |
| 771 | static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, |
| 772 | struct net_device *dev, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 773 | struct geneve_sock *gs4, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 774 | struct flowi4 *fl4, |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 775 | const struct ip_tunnel_info *info, |
| 776 | __be16 dport, __be16 sport) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 777 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 778 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 779 | struct geneve_dev *geneve = netdev_priv(dev); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 780 | struct dst_cache *dst_cache; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 781 | struct rtable *rt = NULL; |
| 782 | __u8 tos; |
| 783 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 784 | if (!gs4) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 785 | return ERR_PTR(-EIO); |
| 786 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 787 | memset(fl4, 0, sizeof(*fl4)); |
| 788 | fl4->flowi4_mark = skb->mark; |
| 789 | fl4->flowi4_proto = IPPROTO_UDP; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 790 | fl4->daddr = info->key.u.ipv4.dst; |
| 791 | fl4->saddr = info->key.u.ipv4.src; |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 792 | fl4->fl4_dport = dport; |
| 793 | fl4->fl4_sport = sport; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 794 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 795 | tos = info->key.tos; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 796 | if ((tos == 1) && !geneve->cfg.collect_md) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 797 | tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
| 798 | use_cache = false; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 799 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 800 | fl4->flowi4_tos = RT_TOS(tos); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 801 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 802 | dst_cache = (struct dst_cache *)&info->dst_cache; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 803 | if (use_cache) { |
| 804 | rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); |
| 805 | if (rt) |
| 806 | return rt; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 807 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 808 | rt = ip_route_output_key(geneve->net, fl4); |
| 809 | if (IS_ERR(rt)) { |
| 810 | netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 811 | return ERR_PTR(-ENETUNREACH); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 812 | } |
| 813 | if (rt->dst.dev == dev) { /* is this necessary? */ |
| 814 | netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 815 | ip_rt_put(rt); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 816 | return ERR_PTR(-ELOOP); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 817 | } |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 818 | if (use_cache) |
| 819 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 820 | return rt; |
| 821 | } |
| 822 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 823 | #if IS_ENABLED(CONFIG_IPV6) |
| 824 | static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, |
| 825 | struct net_device *dev, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 826 | struct geneve_sock *gs6, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 827 | struct flowi6 *fl6, |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 828 | const struct ip_tunnel_info *info, |
| 829 | __be16 dport, __be16 sport) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 830 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 831 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 832 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 833 | struct dst_entry *dst = NULL; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 834 | struct dst_cache *dst_cache; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 835 | __u8 prio; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 836 | |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 837 | if (!gs6) |
| 838 | return ERR_PTR(-EIO); |
| 839 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 840 | memset(fl6, 0, sizeof(*fl6)); |
| 841 | fl6->flowi6_mark = skb->mark; |
| 842 | fl6->flowi6_proto = IPPROTO_UDP; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 843 | fl6->daddr = info->key.u.ipv6.dst; |
| 844 | fl6->saddr = info->key.u.ipv6.src; |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 845 | fl6->fl6_dport = dport; |
| 846 | fl6->fl6_sport = sport; |
| 847 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 848 | prio = info->key.tos; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 849 | if ((prio == 1) && !geneve->cfg.collect_md) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 850 | prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
| 851 | use_cache = false; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 852 | } |
| 853 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 854 | fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio), |
| 855 | info->key.label); |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 856 | dst_cache = (struct dst_cache *)&info->dst_cache; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 857 | if (use_cache) { |
| 858 | dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); |
| 859 | if (dst) |
| 860 | return dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 861 | } |
Sabrina Dubroca | 6c8991f | 2019-12-04 15:35:53 +0100 | [diff] [blame] | 862 | dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6, |
| 863 | NULL); |
| 864 | if (IS_ERR(dst)) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 865 | netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); |
| 866 | return ERR_PTR(-ENETUNREACH); |
| 867 | } |
| 868 | if (dst->dev == dev) { /* is this necessary? */ |
| 869 | netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); |
| 870 | dst_release(dst); |
| 871 | return ERR_PTR(-ELOOP); |
| 872 | } |
| 873 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 874 | if (use_cache) |
| 875 | dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 876 | return dst; |
| 877 | } |
| 878 | #endif |
| 879 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 880 | static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 881 | struct geneve_dev *geneve, |
| 882 | const struct ip_tunnel_info *info) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 883 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 884 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
| 885 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); |
| 886 | const struct ip_tunnel_key *key = &info->key; |
| 887 | struct rtable *rt; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 888 | struct flowi4 fl4; |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 889 | __u8 tos, ttl; |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 890 | __be16 df = 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 891 | __be16 sport; |
pravin shelar | bcceeec | 2016-11-21 11:03:00 -0800 | [diff] [blame] | 892 | int err; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 893 | |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 894 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
| 895 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info, |
| 896 | geneve->cfg.info.key.tp_dst, sport); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 897 | if (IS_ERR(rt)) |
| 898 | return PTR_ERR(rt); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 899 | |
Stefano Brivio | c1a800e | 2020-08-04 07:53:45 +0200 | [diff] [blame] | 900 | err = skb_tunnel_check_pmtu(skb, &rt->dst, |
| 901 | GENEVE_IPV4_HLEN + info->options_len, |
| 902 | netif_is_any_bridge_port(dev)); |
| 903 | if (err < 0) { |
| 904 | dst_release(&rt->dst); |
| 905 | return err; |
| 906 | } else if (err) { |
| 907 | struct ip_tunnel_info *info; |
| 908 | |
| 909 | info = skb_tunnel_info(skb); |
| 910 | if (info) { |
| 911 | info->key.u.ipv4.dst = fl4.saddr; |
| 912 | info->key.u.ipv4.src = fl4.daddr; |
| 913 | } |
| 914 | |
| 915 | if (!pskb_may_pull(skb, ETH_HLEN)) { |
| 916 | dst_release(&rt->dst); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
| 920 | skb->protocol = eth_type_trans(skb, geneve->dev); |
| 921 | netif_rx(skb); |
| 922 | dst_release(&rt->dst); |
| 923 | return -EMSGSIZE; |
| 924 | } |
Xin Long | 52a589d | 2017-12-25 14:43:58 +0800 | [diff] [blame] | 925 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 926 | if (geneve->cfg.collect_md) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 927 | tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 928 | ttl = key->ttl; |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 929 | |
| 930 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 931 | } else { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 932 | tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 933 | if (geneve->cfg.ttl_inherit) |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 934 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
| 935 | else |
| 936 | ttl = key->ttl; |
| 937 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 938 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 939 | if (geneve->cfg.df == GENEVE_DF_SET) { |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 940 | df = htons(IP_DF); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 941 | } else if (geneve->cfg.df == GENEVE_DF_INHERIT) { |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 942 | struct ethhdr *eth = eth_hdr(skb); |
| 943 | |
| 944 | if (ntohs(eth->h_proto) == ETH_P_IPV6) { |
| 945 | df = htons(IP_DF); |
| 946 | } else if (ntohs(eth->h_proto) == ETH_P_IP) { |
| 947 | struct iphdr *iph = ip_hdr(skb); |
| 948 | |
| 949 | if (iph->frag_off & htons(IP_DF)) |
| 950 | df = htons(IP_DF); |
| 951 | } |
| 952 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 953 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 954 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 955 | err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr)); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 956 | if (unlikely(err)) |
| 957 | return err; |
| 958 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 959 | udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 960 | tos, ttl, df, sport, geneve->cfg.info.key.tp_dst, |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 961 | !net_eq(geneve->net, dev_net(geneve->dev)), |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 962 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 963 | return 0; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 964 | } |
| 965 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 966 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 967 | static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 968 | struct geneve_dev *geneve, |
| 969 | const struct ip_tunnel_info *info) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 970 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 971 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
| 972 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); |
| 973 | const struct ip_tunnel_key *key = &info->key; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 974 | struct dst_entry *dst = NULL; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 975 | struct flowi6 fl6; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 976 | __u8 prio, ttl; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 977 | __be16 sport; |
pravin shelar | bcceeec | 2016-11-21 11:03:00 -0800 | [diff] [blame] | 978 | int err; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 979 | |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 980 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
| 981 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info, |
| 982 | geneve->cfg.info.key.tp_dst, sport); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 983 | if (IS_ERR(dst)) |
| 984 | return PTR_ERR(dst); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 985 | |
Stefano Brivio | c1a800e | 2020-08-04 07:53:45 +0200 | [diff] [blame] | 986 | err = skb_tunnel_check_pmtu(skb, dst, |
| 987 | GENEVE_IPV6_HLEN + info->options_len, |
| 988 | netif_is_any_bridge_port(dev)); |
| 989 | if (err < 0) { |
| 990 | dst_release(dst); |
| 991 | return err; |
| 992 | } else if (err) { |
| 993 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 994 | |
| 995 | if (info) { |
| 996 | info->key.u.ipv6.dst = fl6.saddr; |
| 997 | info->key.u.ipv6.src = fl6.daddr; |
| 998 | } |
| 999 | |
| 1000 | if (!pskb_may_pull(skb, ETH_HLEN)) { |
| 1001 | dst_release(dst); |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | skb->protocol = eth_type_trans(skb, geneve->dev); |
| 1006 | netif_rx(skb); |
| 1007 | dst_release(dst); |
| 1008 | return -EMSGSIZE; |
| 1009 | } |
Xin Long | 52a589d | 2017-12-25 14:43:58 +0800 | [diff] [blame] | 1010 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1011 | if (geneve->cfg.collect_md) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1012 | prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1013 | ttl = key->ttl; |
| 1014 | } else { |
Daniel Borkmann | 95caf6f | 2016-03-18 18:37:58 +0100 | [diff] [blame] | 1015 | prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel), |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1016 | ip_hdr(skb), skb); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1017 | if (geneve->cfg.ttl_inherit) |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1018 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
| 1019 | else |
| 1020 | ttl = key->ttl; |
| 1021 | ttl = ttl ? : ip6_dst_hoplimit(dst); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1022 | } |
Haishuang Yan | 31ac1c1 | 2016-11-28 13:26:58 +0800 | [diff] [blame] | 1023 | err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr)); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1024 | if (unlikely(err)) |
| 1025 | return err; |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1026 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 1027 | udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1028 | &fl6.saddr, &fl6.daddr, prio, ttl, |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1029 | info->key.label, sport, geneve->cfg.info.key.tp_dst, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1030 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 1031 | return 0; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1032 | } |
| 1033 | #endif |
| 1034 | |
| 1035 | static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1036 | { |
| 1037 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1038 | struct ip_tunnel_info *info = NULL; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1039 | int err; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1040 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1041 | if (geneve->cfg.collect_md) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1042 | info = skb_tunnel_info(skb); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1043 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1044 | netdev_dbg(dev, "no tunnel metadata\n"); |
Jiri Benc | 9d14904 | 2020-06-03 11:12:14 +0200 | [diff] [blame] | 1045 | dev_kfree_skb(skb); |
| 1046 | dev->stats.tx_dropped++; |
| 1047 | return NETDEV_TX_OK; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1048 | } |
| 1049 | } else { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1050 | info = &geneve->cfg.info; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1051 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1052 | |
Jakub Kicinski | a717e3f | 2017-02-24 11:43:37 -0800 | [diff] [blame] | 1053 | rcu_read_lock(); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1054 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1055 | if (info->mode & IP_TUNNEL_INFO_IPV6) |
| 1056 | err = geneve6_xmit_skb(skb, dev, geneve, info); |
| 1057 | else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1058 | #endif |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1059 | err = geneve_xmit_skb(skb, dev, geneve, info); |
Jakub Kicinski | a717e3f | 2017-02-24 11:43:37 -0800 | [diff] [blame] | 1060 | rcu_read_unlock(); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1061 | |
| 1062 | if (likely(!err)) |
| 1063 | return NETDEV_TX_OK; |
Jiri Benc | 9d14904 | 2020-06-03 11:12:14 +0200 | [diff] [blame] | 1064 | |
Stefano Brivio | c1a800e | 2020-08-04 07:53:45 +0200 | [diff] [blame] | 1065 | if (err != -EMSGSIZE) |
| 1066 | dev_kfree_skb(skb); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1067 | |
| 1068 | if (err == -ELOOP) |
| 1069 | dev->stats.collisions++; |
| 1070 | else if (err == -ENETUNREACH) |
| 1071 | dev->stats.tx_carrier_errors++; |
| 1072 | |
| 1073 | dev->stats.tx_errors++; |
| 1074 | return NETDEV_TX_OK; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1077 | static int geneve_change_mtu(struct net_device *dev, int new_mtu) |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1078 | { |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1079 | if (new_mtu > dev->max_mtu) |
| 1080 | new_mtu = dev->max_mtu; |
Alexey Kodanev | 321acc1 | 2018-04-19 15:42:31 +0300 | [diff] [blame] | 1081 | else if (new_mtu < dev->min_mtu) |
| 1082 | new_mtu = dev->min_mtu; |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1083 | |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1084 | dev->mtu = new_mtu; |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1088 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 1089 | { |
| 1090 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1091 | struct geneve_dev *geneve = netdev_priv(dev); |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 1092 | __be16 sport; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1093 | |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1094 | if (ip_tunnel_info_af(info) == AF_INET) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1095 | struct rtable *rt; |
| 1096 | struct flowi4 fl4; |
| 1097 | |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 1098 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); |
| 1099 | sport = udp_flow_src_port(geneve->net, skb, |
| 1100 | 1, USHRT_MAX, true); |
| 1101 | |
| 1102 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info, |
| 1103 | geneve->cfg.info.key.tp_dst, sport); |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1104 | if (IS_ERR(rt)) |
| 1105 | return PTR_ERR(rt); |
| 1106 | |
| 1107 | ip_rt_put(rt); |
| 1108 | info->key.u.ipv4.src = fl4.saddr; |
| 1109 | #if IS_ENABLED(CONFIG_IPV6) |
| 1110 | } else if (ip_tunnel_info_af(info) == AF_INET6) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1111 | struct dst_entry *dst; |
| 1112 | struct flowi6 fl6; |
| 1113 | |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 1114 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); |
| 1115 | sport = udp_flow_src_port(geneve->net, skb, |
| 1116 | 1, USHRT_MAX, true); |
| 1117 | |
| 1118 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info, |
| 1119 | geneve->cfg.info.key.tp_dst, sport); |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1120 | if (IS_ERR(dst)) |
| 1121 | return PTR_ERR(dst); |
| 1122 | |
| 1123 | dst_release(dst); |
| 1124 | info->key.u.ipv6.src = fl6.saddr; |
| 1125 | #endif |
| 1126 | } else { |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1127 | return -EINVAL; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1128 | } |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1129 | |
Mark Gray | 34beb21 | 2020-09-16 05:19:35 -0400 | [diff] [blame] | 1130 | info->key.tp_src = sport; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1131 | info->key.tp_dst = geneve->cfg.info.key.tp_dst; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |
| 1134 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1135 | static const struct net_device_ops geneve_netdev_ops = { |
| 1136 | .ndo_init = geneve_init, |
| 1137 | .ndo_uninit = geneve_uninit, |
| 1138 | .ndo_open = geneve_open, |
| 1139 | .ndo_stop = geneve_stop, |
| 1140 | .ndo_start_xmit = geneve_xmit, |
Heiner Kallweit | b220a4a | 2020-11-07 21:52:06 +0100 | [diff] [blame^] | 1141 | .ndo_get_stats64 = dev_get_tstats64, |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1142 | .ndo_change_mtu = geneve_change_mtu, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1143 | .ndo_validate_addr = eth_validate_addr, |
| 1144 | .ndo_set_mac_address = eth_mac_addr, |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1145 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1146 | }; |
| 1147 | |
| 1148 | static void geneve_get_drvinfo(struct net_device *dev, |
| 1149 | struct ethtool_drvinfo *drvinfo) |
| 1150 | { |
| 1151 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); |
| 1152 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); |
| 1153 | } |
| 1154 | |
| 1155 | static const struct ethtool_ops geneve_ethtool_ops = { |
| 1156 | .get_drvinfo = geneve_get_drvinfo, |
| 1157 | .get_link = ethtool_op_get_link, |
| 1158 | }; |
| 1159 | |
| 1160 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1161 | static struct device_type geneve_type = { |
| 1162 | .name = "geneve", |
| 1163 | }; |
| 1164 | |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 1165 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1166 | * supply the listening GENEVE udp ports. Callers are expected |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 1167 | * to implement the ndo_udp_tunnel_add. |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1168 | */ |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1169 | static void geneve_offload_rx_ports(struct net_device *dev, bool push) |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1170 | { |
| 1171 | struct net *net = dev_net(dev); |
| 1172 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1173 | struct geneve_sock *gs; |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1174 | |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1175 | rcu_read_lock(); |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1176 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { |
| 1177 | if (push) { |
| 1178 | udp_tunnel_push_rx_port(dev, gs->sock, |
| 1179 | UDP_TUNNEL_TYPE_GENEVE); |
| 1180 | } else { |
| 1181 | udp_tunnel_drop_rx_port(dev, gs->sock, |
| 1182 | UDP_TUNNEL_TYPE_GENEVE); |
| 1183 | } |
| 1184 | } |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1185 | rcu_read_unlock(); |
| 1186 | } |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1187 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1188 | /* Initialize the device structure. */ |
| 1189 | static void geneve_setup(struct net_device *dev) |
| 1190 | { |
| 1191 | ether_setup(dev); |
| 1192 | |
| 1193 | dev->netdev_ops = &geneve_netdev_ops; |
| 1194 | dev->ethtool_ops = &geneve_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1195 | dev->needs_free_netdev = true; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1196 | |
| 1197 | SET_NETDEV_DEVTYPE(dev, &geneve_type); |
| 1198 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1199 | dev->features |= NETIF_F_LLTX; |
| 1200 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
| 1201 | dev->features |= NETIF_F_RXCSUM; |
| 1202 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1203 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1204 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
| 1205 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1206 | |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1207 | /* MTU range: 68 - (something less than 65535) */ |
| 1208 | dev->min_mtu = ETH_MIN_MTU; |
| 1209 | /* The max_mtu calculation does not take account of GENEVE |
| 1210 | * options, to avoid excluding potentially valid |
| 1211 | * configurations. This will be further reduced by IPvX hdr size. |
| 1212 | */ |
| 1213 | dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len; |
| 1214 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1215 | netif_keep_dst(dev); |
Jiri Benc | fc41cdb | 2016-02-17 15:31:35 +0100 | [diff] [blame] | 1216 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Phil Sutter | ed961ac | 2015-08-18 10:30:31 +0200 | [diff] [blame] | 1217 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Pravin B Shelar | 87cd3dc | 2015-08-26 23:46:48 -0700 | [diff] [blame] | 1218 | eth_hw_addr_random(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { |
| 1222 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 1223 | [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) }, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1224 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1225 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1226 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1227 | [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1228 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1229 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1230 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
| 1231 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 1232 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1233 | [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 }, |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1234 | [IFLA_GENEVE_DF] = { .type = NLA_U8 }, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1235 | }; |
| 1236 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1237 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1238 | struct netlink_ext_ack *extack) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1239 | { |
| 1240 | if (tb[IFLA_ADDRESS]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1241 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 1242 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 1243 | "Provided link layer address is not Ethernet"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1244 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1245 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1246 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1247 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 1248 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 1249 | "Provided Ethernet address is not unicast"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1250 | return -EADDRNOTAVAIL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1251 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1254 | if (!data) { |
| 1255 | NL_SET_ERR_MSG(extack, |
| 1256 | "Not enough attributes provided to perform the operation"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1257 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1258 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1259 | |
| 1260 | if (data[IFLA_GENEVE_ID]) { |
| 1261 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1262 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1263 | if (vni >= GENEVE_N_VID) { |
| 1264 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID], |
| 1265 | "Geneve ID must be lower than 16777216"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1266 | return -ERANGE; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1267 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1270 | if (data[IFLA_GENEVE_DF]) { |
| 1271 | enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]); |
| 1272 | |
| 1273 | if (df < 0 || df > GENEVE_DF_MAX) { |
Sabrina Dubroca | 9a7b5b5 | 2020-04-22 17:29:51 +0200 | [diff] [blame] | 1274 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF], |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1275 | "Invalid DF attribute"); |
| 1276 | return -EINVAL; |
| 1277 | } |
| 1278 | } |
| 1279 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1280 | return 0; |
| 1281 | } |
| 1282 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1283 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1284 | const struct ip_tunnel_info *info, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1285 | bool *tun_on_same_port, |
| 1286 | bool *tun_collect_md) |
| 1287 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1288 | struct geneve_dev *geneve, *t = NULL; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1289 | |
| 1290 | *tun_on_same_port = false; |
| 1291 | *tun_collect_md = false; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1292 | list_for_each_entry(geneve, &gn->geneve_list, next) { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1293 | if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) { |
| 1294 | *tun_collect_md = geneve->cfg.collect_md; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1295 | *tun_on_same_port = true; |
| 1296 | } |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1297 | if (info->key.tun_id == geneve->cfg.info.key.tun_id && |
| 1298 | info->key.tp_dst == geneve->cfg.info.key.tp_dst && |
| 1299 | !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u))) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1300 | t = geneve; |
| 1301 | } |
| 1302 | return t; |
| 1303 | } |
| 1304 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1305 | static bool is_tnl_info_zero(const struct ip_tunnel_info *info) |
| 1306 | { |
Stefano Brivio | 3fa5f11 | 2017-10-20 13:31:36 +0200 | [diff] [blame] | 1307 | return !(info->key.tun_id || info->key.tun_flags || info->key.tos || |
| 1308 | info->key.ttl || info->key.label || info->key.tp_src || |
| 1309 | memchr_inv(&info->key.u, 0, sizeof(info->key.u))); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1312 | static bool geneve_dst_addr_equal(struct ip_tunnel_info *a, |
| 1313 | struct ip_tunnel_info *b) |
| 1314 | { |
| 1315 | if (ip_tunnel_info_af(a) == AF_INET) |
| 1316 | return a->key.u.ipv4.dst == b->key.u.ipv4.dst; |
| 1317 | else |
| 1318 | return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst); |
| 1319 | } |
| 1320 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1321 | static int geneve_configure(struct net *net, struct net_device *dev, |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1322 | struct netlink_ext_ack *extack, |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1323 | const struct geneve_config *cfg) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1324 | { |
| 1325 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1326 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1327 | const struct ip_tunnel_info *info = &cfg->info; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1328 | bool tun_collect_md, tun_on_same_port; |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1329 | int err, encap_len; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1330 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1331 | if (cfg->collect_md && !is_tnl_info_zero(info)) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1332 | NL_SET_ERR_MSG(extack, |
| 1333 | "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1334 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1335 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1336 | |
| 1337 | geneve->net = net; |
| 1338 | geneve->dev = dev; |
| 1339 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1340 | t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1341 | if (t) |
| 1342 | return -EBUSY; |
| 1343 | |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1344 | /* make enough headroom for basic scenario */ |
| 1345 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1346 | if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) { |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1347 | encap_len += sizeof(struct iphdr); |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1348 | dev->max_mtu -= sizeof(struct iphdr); |
| 1349 | } else { |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1350 | encap_len += sizeof(struct ipv6hdr); |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1351 | dev->max_mtu -= sizeof(struct ipv6hdr); |
| 1352 | } |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1353 | dev->needed_headroom = encap_len + ETH_HLEN; |
| 1354 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1355 | if (cfg->collect_md) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1356 | if (tun_on_same_port) { |
| 1357 | NL_SET_ERR_MSG(extack, |
| 1358 | "There can be only one externally controlled device on a destination port"); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1359 | return -EPERM; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1360 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1361 | } else { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1362 | if (tun_collect_md) { |
| 1363 | NL_SET_ERR_MSG(extack, |
| 1364 | "There already exists an externally controlled device on this destination port"); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1365 | return -EPERM; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1366 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1369 | dst_cache_reset(&geneve->cfg.info.dst_cache); |
| 1370 | memcpy(&geneve->cfg, cfg, sizeof(*cfg)); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 1371 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1372 | err = register_netdevice(dev); |
| 1373 | if (err) |
| 1374 | return err; |
| 1375 | |
| 1376 | list_add(&geneve->next, &gn->geneve_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1380 | static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port) |
| 1381 | { |
| 1382 | memset(info, 0, sizeof(*info)); |
| 1383 | info->key.tp_dst = htons(dst_port); |
| 1384 | } |
| 1385 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1386 | static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[], |
| 1387 | struct netlink_ext_ack *extack, |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1388 | struct geneve_config *cfg, bool changelink) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1389 | { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1390 | struct ip_tunnel_info *info = &cfg->info; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1391 | int attrtype; |
| 1392 | |
| 1393 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) { |
| 1394 | NL_SET_ERR_MSG(extack, |
| 1395 | "Cannot specify both IPv4 and IPv6 Remote addresses"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1396 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1397 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1398 | |
| 1399 | if (data[IFLA_GENEVE_REMOTE]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1400 | if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) { |
| 1401 | attrtype = IFLA_GENEVE_REMOTE; |
| 1402 | goto change_notsup; |
| 1403 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1404 | |
| 1405 | info->key.u.ipv4.dst = |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1406 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1407 | |
Dave Taht | 842841e | 2019-09-02 16:29:36 -0700 | [diff] [blame] | 1408 | if (ipv4_is_multicast(info->key.u.ipv4.dst)) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1409 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE], |
| 1410 | "Remote IPv4 address cannot be Multicast"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1411 | return -EINVAL; |
| 1412 | } |
| 1413 | } |
| 1414 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1415 | if (data[IFLA_GENEVE_REMOTE6]) { |
Alexey Kodanev | 4c52a88 | 2018-04-19 15:42:29 +0300 | [diff] [blame] | 1416 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1417 | if (changelink && (ip_tunnel_info_af(info) == AF_INET)) { |
| 1418 | attrtype = IFLA_GENEVE_REMOTE6; |
| 1419 | goto change_notsup; |
| 1420 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1421 | |
| 1422 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 1423 | info->key.u.ipv6.dst = |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1424 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1425 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1426 | if (ipv6_addr_type(&info->key.u.ipv6.dst) & |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1427 | IPV6_ADDR_LINKLOCAL) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1428 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1429 | "Remote IPv6 address cannot be link-local"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1430 | return -EINVAL; |
| 1431 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1432 | if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1433 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1434 | "Remote IPv6 address cannot be Multicast"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1435 | return -EINVAL; |
| 1436 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1437 | info->key.tun_flags |= TUNNEL_CSUM; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1438 | cfg->use_udp6_rx_checksums = true; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1439 | #else |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1440 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1441 | "IPv6 support not enabled in the kernel"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1442 | return -EPFNOSUPPORT; |
| 1443 | #endif |
| 1444 | } |
| 1445 | |
| 1446 | if (data[IFLA_GENEVE_ID]) { |
| 1447 | __u32 vni; |
| 1448 | __u8 tvni[3]; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1449 | __be64 tunid; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1450 | |
| 1451 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1452 | tvni[0] = (vni & 0x00ff0000) >> 16; |
| 1453 | tvni[1] = (vni & 0x0000ff00) >> 8; |
| 1454 | tvni[2] = vni & 0x000000ff; |
| 1455 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1456 | tunid = vni_to_tunnel_id(tvni); |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1457 | if (changelink && (tunid != info->key.tun_id)) { |
| 1458 | attrtype = IFLA_GENEVE_ID; |
| 1459 | goto change_notsup; |
| 1460 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1461 | info->key.tun_id = tunid; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1462 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1463 | |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1464 | if (data[IFLA_GENEVE_TTL_INHERIT]) { |
| 1465 | if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT])) |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1466 | cfg->ttl_inherit = true; |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1467 | else |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1468 | cfg->ttl_inherit = false; |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1469 | } else if (data[IFLA_GENEVE_TTL]) { |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1470 | info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1471 | cfg->ttl_inherit = false; |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1472 | } |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1473 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1474 | if (data[IFLA_GENEVE_TOS]) |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1475 | info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1476 | |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1477 | if (data[IFLA_GENEVE_DF]) |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1478 | cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]); |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1479 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1480 | if (data[IFLA_GENEVE_LABEL]) { |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1481 | info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1482 | IPV6_FLOWLABEL_MASK; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1483 | if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) { |
| 1484 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL], |
| 1485 | "Label attribute only applies for IPv6 Geneve devices"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1486 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1487 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1488 | } |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1489 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1490 | if (data[IFLA_GENEVE_PORT]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1491 | if (changelink) { |
| 1492 | attrtype = IFLA_GENEVE_PORT; |
| 1493 | goto change_notsup; |
| 1494 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1495 | info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]); |
| 1496 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1497 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1498 | if (data[IFLA_GENEVE_COLLECT_METADATA]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1499 | if (changelink) { |
| 1500 | attrtype = IFLA_GENEVE_COLLECT_METADATA; |
| 1501 | goto change_notsup; |
| 1502 | } |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1503 | cfg->collect_md = true; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1504 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1505 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1506 | if (data[IFLA_GENEVE_UDP_CSUM]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1507 | if (changelink) { |
| 1508 | attrtype = IFLA_GENEVE_UDP_CSUM; |
| 1509 | goto change_notsup; |
| 1510 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1511 | if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
| 1512 | info->key.tun_flags |= TUNNEL_CSUM; |
| 1513 | } |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1514 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1515 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) { |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1516 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1517 | if (changelink) { |
| 1518 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX; |
| 1519 | goto change_notsup; |
| 1520 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1521 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) |
| 1522 | info->key.tun_flags &= ~TUNNEL_CSUM; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1523 | #else |
| 1524 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX], |
| 1525 | "IPv6 support not enabled in the kernel"); |
| 1526 | return -EPFNOSUPPORT; |
| 1527 | #endif |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1528 | } |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1529 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1530 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) { |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1531 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1532 | if (changelink) { |
| 1533 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX; |
| 1534 | goto change_notsup; |
| 1535 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1536 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1537 | cfg->use_udp6_rx_checksums = false; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1538 | #else |
| 1539 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX], |
| 1540 | "IPv6 support not enabled in the kernel"); |
| 1541 | return -EPFNOSUPPORT; |
| 1542 | #endif |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | return 0; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1546 | change_notsup: |
| 1547 | NL_SET_ERR_MSG_ATTR(extack, data[attrtype], |
| 1548 | "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported"); |
| 1549 | return -EOPNOTSUPP; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1552 | static void geneve_link_config(struct net_device *dev, |
| 1553 | struct ip_tunnel_info *info, struct nlattr *tb[]) |
| 1554 | { |
| 1555 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1556 | int ldev_mtu = 0; |
| 1557 | |
| 1558 | if (tb[IFLA_MTU]) { |
| 1559 | geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | switch (ip_tunnel_info_af(info)) { |
| 1564 | case AF_INET: { |
| 1565 | struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst }; |
| 1566 | struct rtable *rt = ip_route_output_key(geneve->net, &fl4); |
| 1567 | |
| 1568 | if (!IS_ERR(rt) && rt->dst.dev) { |
| 1569 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN; |
| 1570 | ip_rt_put(rt); |
| 1571 | } |
| 1572 | break; |
| 1573 | } |
| 1574 | #if IS_ENABLED(CONFIG_IPV6) |
| 1575 | case AF_INET6: { |
Hangbin Liu | c0a47e4 | 2019-02-07 18:36:10 +0800 | [diff] [blame] | 1576 | struct rt6_info *rt; |
| 1577 | |
| 1578 | if (!__in6_dev_get(dev)) |
| 1579 | break; |
| 1580 | |
| 1581 | rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0, |
| 1582 | NULL, 0); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1583 | |
| 1584 | if (rt && rt->dst.dev) |
| 1585 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN; |
| 1586 | ip6_rt_put(rt); |
| 1587 | break; |
| 1588 | } |
| 1589 | #endif |
| 1590 | } |
| 1591 | |
| 1592 | if (ldev_mtu <= 0) |
| 1593 | return; |
| 1594 | |
| 1595 | geneve_change_mtu(dev, ldev_mtu - info->options_len); |
| 1596 | } |
| 1597 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1598 | static int geneve_newlink(struct net *net, struct net_device *dev, |
| 1599 | struct nlattr *tb[], struct nlattr *data[], |
| 1600 | struct netlink_ext_ack *extack) |
| 1601 | { |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1602 | struct geneve_config cfg = { |
| 1603 | .df = GENEVE_DF_UNSET, |
| 1604 | .use_udp6_rx_checksums = false, |
| 1605 | .ttl_inherit = false, |
| 1606 | .collect_md = false, |
| 1607 | }; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1608 | int err; |
| 1609 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1610 | init_tnl_info(&cfg.info, GENEVE_UDP_PORT); |
| 1611 | err = geneve_nl2info(tb, data, extack, &cfg, false); |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1612 | if (err) |
| 1613 | return err; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1614 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1615 | err = geneve_configure(net, dev, extack, &cfg); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1616 | if (err) |
| 1617 | return err; |
| 1618 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1619 | geneve_link_config(dev, &cfg.info, tb); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1620 | |
| 1621 | return 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1624 | /* Quiesces the geneve device data path for both TX and RX. |
| 1625 | * |
| 1626 | * On transmit geneve checks for non-NULL geneve_sock before it proceeds. |
| 1627 | * So, if we set that socket to NULL under RCU and wait for synchronize_net() |
| 1628 | * to complete for the existing set of in-flight packets to be transmitted, |
| 1629 | * then we would have quiesced the transmit data path. All the future packets |
| 1630 | * will get dropped until we unquiesce the data path. |
| 1631 | * |
| 1632 | * On receive geneve dereference the geneve_sock stashed in the socket. So, |
| 1633 | * if we set that to NULL under RCU and wait for synchronize_net() to |
| 1634 | * complete, then we would have quiesced the receive data path. |
| 1635 | */ |
| 1636 | static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4, |
| 1637 | struct geneve_sock **gs6) |
| 1638 | { |
| 1639 | *gs4 = rtnl_dereference(geneve->sock4); |
| 1640 | rcu_assign_pointer(geneve->sock4, NULL); |
| 1641 | if (*gs4) |
| 1642 | rcu_assign_sk_user_data((*gs4)->sock->sk, NULL); |
| 1643 | #if IS_ENABLED(CONFIG_IPV6) |
| 1644 | *gs6 = rtnl_dereference(geneve->sock6); |
| 1645 | rcu_assign_pointer(geneve->sock6, NULL); |
| 1646 | if (*gs6) |
| 1647 | rcu_assign_sk_user_data((*gs6)->sock->sk, NULL); |
| 1648 | #else |
| 1649 | *gs6 = NULL; |
| 1650 | #endif |
| 1651 | synchronize_net(); |
| 1652 | } |
| 1653 | |
| 1654 | /* Resumes the geneve device data path for both TX and RX. */ |
| 1655 | static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4, |
| 1656 | struct geneve_sock __maybe_unused *gs6) |
| 1657 | { |
| 1658 | rcu_assign_pointer(geneve->sock4, gs4); |
| 1659 | if (gs4) |
| 1660 | rcu_assign_sk_user_data(gs4->sock->sk, gs4); |
| 1661 | #if IS_ENABLED(CONFIG_IPV6) |
| 1662 | rcu_assign_pointer(geneve->sock6, gs6); |
| 1663 | if (gs6) |
| 1664 | rcu_assign_sk_user_data(gs6->sock->sk, gs6); |
| 1665 | #endif |
| 1666 | synchronize_net(); |
| 1667 | } |
| 1668 | |
| 1669 | static int geneve_changelink(struct net_device *dev, struct nlattr *tb[], |
| 1670 | struct nlattr *data[], |
| 1671 | struct netlink_ext_ack *extack) |
| 1672 | { |
| 1673 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1674 | struct geneve_sock *gs4, *gs6; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1675 | struct geneve_config cfg; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1676 | int err; |
| 1677 | |
| 1678 | /* If the geneve device is configured for metadata (or externally |
| 1679 | * controlled, for example, OVS), then nothing can be changed. |
| 1680 | */ |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1681 | if (geneve->cfg.collect_md) |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1682 | return -EOPNOTSUPP; |
| 1683 | |
| 1684 | /* Start with the existing info. */ |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1685 | memcpy(&cfg, &geneve->cfg, sizeof(cfg)); |
| 1686 | err = geneve_nl2info(tb, data, extack, &cfg, true); |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1687 | if (err) |
| 1688 | return err; |
| 1689 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1690 | if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) { |
| 1691 | dst_cache_reset(&cfg.info.dst_cache); |
| 1692 | geneve_link_config(dev, &cfg.info, tb); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1693 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1694 | |
| 1695 | geneve_quiesce(geneve, &gs4, &gs6); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1696 | memcpy(&geneve->cfg, &cfg, sizeof(cfg)); |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1697 | geneve_unquiesce(geneve, gs4, gs6); |
| 1698 | |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1702 | static void geneve_dellink(struct net_device *dev, struct list_head *head) |
| 1703 | { |
| 1704 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1705 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1706 | list_del(&geneve->next); |
| 1707 | unregister_netdevice_queue(dev, head); |
| 1708 | } |
| 1709 | |
| 1710 | static size_t geneve_get_size(const struct net_device *dev) |
| 1711 | { |
| 1712 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1713 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1714 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1715 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1716 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */ |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1717 | nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1718 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1719 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1720 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
| 1721 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ |
| 1722 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1723 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */ |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1724 | 0; |
| 1725 | } |
| 1726 | |
| 1727 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1728 | { |
| 1729 | struct geneve_dev *geneve = netdev_priv(dev); |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1730 | struct ip_tunnel_info *info = &geneve->cfg.info; |
| 1731 | bool ttl_inherit = geneve->cfg.ttl_inherit; |
| 1732 | bool metadata = geneve->cfg.collect_md; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1733 | __u8 tmp_vni[3]; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1734 | __u32 vni; |
| 1735 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1736 | tunnel_id_to_vni(info->key.tun_id, tmp_vni); |
| 1737 | vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2]; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1738 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) |
| 1739 | goto nla_put_failure; |
| 1740 | |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1741 | if (!metadata && ip_tunnel_info_af(info) == AF_INET) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1742 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1743 | info->key.u.ipv4.dst)) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1744 | goto nla_put_failure; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1745 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
| 1746 | !!(info->key.tun_flags & TUNNEL_CSUM))) |
| 1747 | goto nla_put_failure; |
| 1748 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1749 | #if IS_ENABLED(CONFIG_IPV6) |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1750 | } else if (!metadata) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1751 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1752 | &info->key.u.ipv6.dst)) |
| 1753 | goto nla_put_failure; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1754 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
| 1755 | !(info->key.tun_flags & TUNNEL_CSUM))) |
| 1756 | goto nla_put_failure; |
Eric Garver | 11387fe | 2017-05-23 18:37:27 -0400 | [diff] [blame] | 1757 | #endif |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1758 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1759 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1760 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) || |
| 1761 | nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) || |
| 1762 | nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label)) |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1763 | goto nla_put_failure; |
| 1764 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1765 | if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df)) |
Stefano Brivio | a025fb5 | 2018-11-08 12:19:19 +0100 | [diff] [blame] | 1766 | goto nla_put_failure; |
| 1767 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1768 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst)) |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1769 | goto nla_put_failure; |
| 1770 | |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1771 | if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1772 | goto nla_put_failure; |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1773 | |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1774 | #if IS_ENABLED(CONFIG_IPV6) |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1775 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1776 | !geneve->cfg.use_udp6_rx_checksums)) |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1777 | goto nla_put_failure; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1778 | #endif |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1779 | |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1780 | if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit)) |
| 1781 | goto nla_put_failure; |
| 1782 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1783 | return 0; |
| 1784 | |
| 1785 | nla_put_failure: |
| 1786 | return -EMSGSIZE; |
| 1787 | } |
| 1788 | |
| 1789 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { |
| 1790 | .kind = "geneve", |
| 1791 | .maxtype = IFLA_GENEVE_MAX, |
| 1792 | .policy = geneve_policy, |
| 1793 | .priv_size = sizeof(struct geneve_dev), |
| 1794 | .setup = geneve_setup, |
| 1795 | .validate = geneve_validate, |
| 1796 | .newlink = geneve_newlink, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1797 | .changelink = geneve_changelink, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1798 | .dellink = geneve_dellink, |
| 1799 | .get_size = geneve_get_size, |
| 1800 | .fill_info = geneve_fill_info, |
| 1801 | }; |
| 1802 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1803 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
| 1804 | u8 name_assign_type, u16 dst_port) |
| 1805 | { |
| 1806 | struct nlattr *tb[IFLA_MAX + 1]; |
| 1807 | struct net_device *dev; |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1808 | LIST_HEAD(list_kill); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1809 | int err; |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1810 | struct geneve_config cfg = { |
| 1811 | .df = GENEVE_DF_UNSET, |
| 1812 | .use_udp6_rx_checksums = true, |
| 1813 | .ttl_inherit = false, |
| 1814 | .collect_md = true, |
| 1815 | }; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1816 | |
| 1817 | memset(tb, 0, sizeof(tb)); |
| 1818 | dev = rtnl_create_link(net, name, name_assign_type, |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 1819 | &geneve_link_ops, tb, NULL); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1820 | if (IS_ERR(dev)) |
| 1821 | return dev; |
| 1822 | |
Sabrina Dubroca | 9e06e85 | 2020-07-06 17:18:08 +0200 | [diff] [blame] | 1823 | init_tnl_info(&cfg.info, dst_port); |
| 1824 | err = geneve_configure(net, dev, NULL, &cfg); |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1825 | if (err) { |
| 1826 | free_netdev(dev); |
| 1827 | return ERR_PTR(err); |
| 1828 | } |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1829 | |
| 1830 | /* openvswitch users expect packet sizes to be unrestricted, |
| 1831 | * so set the largest MTU we can. |
| 1832 | */ |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1833 | err = geneve_change_mtu(dev, IP_MAX_MTU); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1834 | if (err) |
| 1835 | goto err; |
| 1836 | |
Nicolas Dichtel | 4100948 | 2016-06-13 10:31:07 +0200 | [diff] [blame] | 1837 | err = rtnl_configure_link(dev, NULL); |
| 1838 | if (err < 0) |
| 1839 | goto err; |
| 1840 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1841 | return dev; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1842 | err: |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1843 | geneve_dellink(dev, &list_kill); |
| 1844 | unregister_netdevice_many(&list_kill); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1845 | return ERR_PTR(err); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1846 | } |
| 1847 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); |
| 1848 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1849 | static int geneve_netdevice_event(struct notifier_block *unused, |
| 1850 | unsigned long event, void *ptr) |
| 1851 | { |
| 1852 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1853 | |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1854 | if (event == NETDEV_UDP_TUNNEL_PUSH_INFO || |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1855 | event == NETDEV_UDP_TUNNEL_DROP_INFO) { |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1856 | geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1857 | } else if (event == NETDEV_UNREGISTER) { |
Jakub Kicinski | cc4e383 | 2020-07-09 17:42:46 -0700 | [diff] [blame] | 1858 | if (!dev->udp_tunnel_nic_info) |
| 1859 | geneve_offload_rx_ports(dev, false); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1860 | } else if (event == NETDEV_REGISTER) { |
Jakub Kicinski | cc4e383 | 2020-07-09 17:42:46 -0700 | [diff] [blame] | 1861 | if (!dev->udp_tunnel_nic_info) |
| 1862 | geneve_offload_rx_ports(dev, true); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1863 | } |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1864 | |
| 1865 | return NOTIFY_DONE; |
| 1866 | } |
| 1867 | |
| 1868 | static struct notifier_block geneve_notifier_block __read_mostly = { |
| 1869 | .notifier_call = geneve_netdevice_event, |
| 1870 | }; |
| 1871 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1872 | static __net_init int geneve_init_net(struct net *net) |
| 1873 | { |
| 1874 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1875 | |
| 1876 | INIT_LIST_HEAD(&gn->geneve_list); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1877 | INIT_LIST_HEAD(&gn->sock_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1878 | return 0; |
| 1879 | } |
| 1880 | |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1881 | static void geneve_destroy_tunnels(struct net *net, struct list_head *head) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1882 | { |
| 1883 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1884 | struct geneve_dev *geneve, *next; |
| 1885 | struct net_device *dev, *aux; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1886 | |
| 1887 | /* gather any geneve devices that were moved into this ns */ |
| 1888 | for_each_netdev_safe(net, dev, aux) |
| 1889 | if (dev->rtnl_link_ops == &geneve_link_ops) |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1890 | unregister_netdevice_queue(dev, head); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1891 | |
| 1892 | /* now gather any other geneve devices that were created in this ns */ |
| 1893 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { |
| 1894 | /* If geneve->dev is in the same netns, it was already added |
| 1895 | * to the list by the previous loop. |
| 1896 | */ |
| 1897 | if (!net_eq(dev_net(geneve->dev), net)) |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1898 | unregister_netdevice_queue(geneve->dev, head); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1899 | } |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | static void __net_exit geneve_exit_batch_net(struct list_head *net_list) |
| 1903 | { |
| 1904 | struct net *net; |
| 1905 | LIST_HEAD(list); |
| 1906 | |
| 1907 | rtnl_lock(); |
| 1908 | list_for_each_entry(net, net_list, exit_list) |
| 1909 | geneve_destroy_tunnels(net, &list); |
| 1910 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1911 | /* unregister the devices gathered above */ |
| 1912 | unregister_netdevice_many(&list); |
| 1913 | rtnl_unlock(); |
Florian Westphal | 0fda760 | 2020-03-14 08:18:42 +0100 | [diff] [blame] | 1914 | |
| 1915 | list_for_each_entry(net, net_list, exit_list) { |
| 1916 | const struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1917 | |
| 1918 | WARN_ON_ONCE(!list_empty(&gn->sock_list)); |
| 1919 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | static struct pernet_operations geneve_net_ops = { |
| 1923 | .init = geneve_init_net, |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1924 | .exit_batch = geneve_exit_batch_net, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1925 | .id = &geneve_net_id, |
| 1926 | .size = sizeof(struct geneve_net), |
| 1927 | }; |
| 1928 | |
| 1929 | static int __init geneve_init_module(void) |
| 1930 | { |
| 1931 | int rc; |
| 1932 | |
| 1933 | rc = register_pernet_subsys(&geneve_net_ops); |
| 1934 | if (rc) |
| 1935 | goto out1; |
| 1936 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1937 | rc = register_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1938 | if (rc) |
| 1939 | goto out2; |
| 1940 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1941 | rc = rtnl_link_register(&geneve_link_ops); |
| 1942 | if (rc) |
| 1943 | goto out3; |
| 1944 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1945 | return 0; |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1946 | out3: |
| 1947 | unregister_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1948 | out2: |
| 1949 | unregister_pernet_subsys(&geneve_net_ops); |
| 1950 | out1: |
| 1951 | return rc; |
| 1952 | } |
| 1953 | late_initcall(geneve_init_module); |
| 1954 | |
| 1955 | static void __exit geneve_cleanup_module(void) |
| 1956 | { |
| 1957 | rtnl_link_unregister(&geneve_link_ops); |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1958 | unregister_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1959 | unregister_pernet_subsys(&geneve_net_ops); |
| 1960 | } |
| 1961 | module_exit(geneve_cleanup_module); |
| 1962 | |
| 1963 | MODULE_LICENSE("GPL"); |
| 1964 | MODULE_VERSION(GENEVE_NETDEV_VER); |
| 1965 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); |
| 1966 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); |
| 1967 | MODULE_ALIAS_RTNL_LINK("geneve"); |