blob: 4ac0373326efd995171b1179bd7a5e7a202b89a2 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
John W. Linville2d07dc72015-05-13 12:57:30 -04002/*
3 * GENEVE: Generic Network Virtualization Encapsulation
4 *
5 * Copyright (c) 2015 Red Hat, Inc.
John W. Linville2d07dc72015-05-13 12:57:30 -04006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Jakub Kicinskicc698372020-11-20 14:50:52 -080010#include <linux/ethtool.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040011#include <linux/kernel.h>
12#include <linux/module.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040013#include <linux/etherdevice.h>
14#include <linux/hash.h>
David Ahern3616d082019-03-22 06:06:09 -070015#include <net/ipv6_stubs.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070016#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070017#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040018#include <net/rtnetlink.h>
19#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070020#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040021
22#define GENEVE_NETDEV_VER "0.6"
23
John W. Linville2d07dc72015-05-13 12:57:30 -040024#define GENEVE_N_VID (1u << 24)
25#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
26
27#define VNI_HASH_BITS 10
28#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
29
30static bool log_ecn_error = true;
31module_param(log_ecn_error, bool, 0644);
32MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
33
Pravin B Shelar371bd102015-08-26 23:46:54 -070034#define GENEVE_VER 0
35#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
Alexey Kodanev5edbea62018-04-19 15:42:30 +030036#define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
37#define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
Pravin B Shelar371bd102015-08-26 23:46:54 -070038
John W. Linville2d07dc72015-05-13 12:57:30 -040039/* per-network namespace private data for this module */
40struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070041 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070042 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040043};
44
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030045static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070046
Jiri Benc4b4c21f2017-07-02 19:00:58 +020047struct geneve_dev_node {
48 struct hlist_node hlist;
49 struct geneve_dev *geneve;
50};
51
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020052struct geneve_config {
53 struct ip_tunnel_info info;
54 bool collect_md;
55 bool use_udp6_rx_checksums;
56 bool ttl_inherit;
57 enum ifla_geneve_df df;
58};
59
John W. Linville2d07dc72015-05-13 12:57:30 -040060/* Pseudo network device */
61struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020062 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
63#if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
65#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040066 struct net *net; /* netns for packet i/o */
67 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelarfceb9c32016-10-28 09:59:16 -070068 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040069#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070070 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040071#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040072 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070073 struct gro_cells gro_cells;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020074 struct geneve_config cfg;
John W. Linville2d07dc72015-05-13 12:57:30 -040075};
76
Pravin B Shelar371bd102015-08-26 23:46:54 -070077struct geneve_sock {
78 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070079 struct list_head list;
80 struct socket *sock;
81 struct rcu_head rcu;
82 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070083 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070084};
John W. Linville2d07dc72015-05-13 12:57:30 -040085
86static inline __u32 geneve_net_vni_hash(u8 vni[3])
87{
88 __u32 vnid;
89
90 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
91 return hash_32(vnid, VNI_HASH_BITS);
92}
93
Pravin B Shelare305ac62015-08-26 23:46:52 -070094static __be64 vni_to_tunnel_id(const __u8 *vni)
95{
96#ifdef __BIG_ENDIAN
97 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
98#else
99 return (__force __be64)(((__force u64)vni[0] << 40) |
100 ((__force u64)vni[1] << 48) |
101 ((__force u64)vni[2] << 56));
102#endif
103}
104
pravin shelar9b4437a2016-11-21 11:02:58 -0800105/* Convert 64 bit tunnel ID to 24 bit VNI. */
106static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
107{
108#ifdef __BIG_ENDIAN
109 vni[0] = (__force __u8)(tun_id >> 16);
110 vni[1] = (__force __u8)(tun_id >> 8);
111 vni[2] = (__force __u8)tun_id;
112#else
113 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
114 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
115 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
116#endif
117}
118
pravin shelar2e0b26e2016-11-21 11:03:01 -0800119static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
120{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800121 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800122}
123
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100124static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
125{
126 return gs->sock->sk->sk_family;
127}
128
Pravin B Shelar66d47002015-08-26 23:46:55 -0700129static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700130 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400131{
John W. Linville2d07dc72015-05-13 12:57:30 -0400132 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200133 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400134 __u32 hash;
135
John W. Linville2d07dc72015-05-13 12:57:30 -0400136 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700137 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700138 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200139 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200140 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
141 addr == node->geneve->cfg.info.key.u.ipv4.dst)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200142 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400143 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700144 return NULL;
145}
146
John W. Linville8ed66f02015-10-26 17:01:44 -0400147#if IS_ENABLED(CONFIG_IPV6)
148static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
149 struct in6_addr addr6, u8 vni[])
150{
151 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200152 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400153 __u32 hash;
154
155 /* Find the device for this VNI */
156 hash = geneve_net_vni_hash(vni);
157 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200158 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200159 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
160 ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200161 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400162 }
163 return NULL;
164}
165#endif
166
Pravin B Shelar371bd102015-08-26 23:46:54 -0700167static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
168{
169 return (struct genevehdr *)(udp_hdr(skb) + 1);
170}
171
Jiri Benc9fc47542016-02-18 11:22:50 +0100172static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
173 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700174{
John W. Linville8ed66f02015-10-26 17:01:44 -0400175 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800176 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700177
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100178 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100179 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800180 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100181
John W. Linville8ed66f02015-10-26 17:01:44 -0400182 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700183
John W. Linville8ed66f02015-10-26 17:01:44 -0400184 if (gs->collect_md) {
185 vni = zero_vni;
186 addr = 0;
187 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100188 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400189 addr = iph->saddr;
190 }
191
Jiri Benc9fc47542016-02-18 11:22:50 +0100192 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400193#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100194 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800195 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100196 struct ipv6hdr *ip6h;
197 struct in6_addr addr6;
198
John W. Linville8ed66f02015-10-26 17:01:44 -0400199 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
200
201 if (gs->collect_md) {
202 vni = zero_vni;
203 addr6 = zero_addr6;
204 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100205 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400206 addr6 = ip6h->saddr;
207 }
208
Jiri Benc9fc47542016-02-18 11:22:50 +0100209 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400210#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700211 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100212 return NULL;
213}
214
215/* geneve receive/decap routine */
216static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
217 struct sk_buff *skb)
218{
219 struct genevehdr *gnvh = geneve_hdr(skb);
220 struct metadata_dst *tun_dst = NULL;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700221 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100222 int err = 0;
223 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400224
Pravin B Shelar371bd102015-08-26 23:46:54 -0700225 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700226 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700227
Yi-Hung Wei9c2e14b2020-11-10 16:16:40 -0800228 flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
230
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100231 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700232 vni_to_tunnel_id(gnvh->vni),
233 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700234 if (!tun_dst) {
235 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700236 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700237 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700238 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700239 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700240 gnvh->options, gnvh->opt_len * 4,
241 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700242 } else {
243 /* Drop packets w/ critical options,
244 * since we don't support any...
245 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700246 if (gnvh->critical) {
247 geneve->dev->stats.rx_frame_errors++;
248 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700249 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700250 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700251 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400252
253 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400254 skb->protocol = eth_type_trans(skb, geneve->dev);
255 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
256
Pravin B Shelare305ac62015-08-26 23:46:52 -0700257 if (tun_dst)
258 skb_dst_set(skb, &tun_dst->dst);
259
John W. Linville2d07dc72015-05-13 12:57:30 -0400260 /* Ignore packet loops (and multicast echo) */
Jakub Kicinskic02bd112020-12-09 14:39:56 -0800261 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
262 geneve->dev->stats.rx_errors++;
263 goto drop;
Eric Dumazet4179b002020-12-01 01:05:07 -0800264 }
Jakub Kicinskic02bd112020-12-09 14:39:56 -0800265
Jiri Benc9fc47542016-02-18 11:22:50 +0100266 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400267 skb_reset_network_header(skb);
268
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 if (geneve_get_sk_family(gs) == AF_INET)
270 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400271#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100272 else
273 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400274#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400275
276 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400277 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100278 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400279 net_info_ratelimited("non-ECT from %pI4 "
280 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100281 &((struct iphdr *)oiph)->saddr,
282 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400283#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100284 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400285 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100286 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400287#endif
288 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400289 if (err > 1) {
290 ++geneve->dev->stats.rx_frame_errors;
291 ++geneve->dev->stats.rx_errors;
292 goto drop;
293 }
294 }
295
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700296 len = skb->len;
297 err = gro_cells_receive(&geneve->gro_cells, skb);
Fabian Frederick1e845272020-10-05 22:34:58 +0200298 if (likely(err == NET_RX_SUCCESS))
299 dev_sw_netstats_rx_add(geneve->dev, len);
300
John W. Linville2d07dc72015-05-13 12:57:30 -0400301 return;
302drop:
303 /* Consume bad packet */
304 kfree_skb(skb);
305}
306
307/* Setup stats when device is created */
308static int geneve_init(struct net_device *dev)
309{
Jesse Gross8e816df2015-08-28 16:54:40 -0700310 struct geneve_dev *geneve = netdev_priv(dev);
311 int err;
312
John W. Linville2d07dc72015-05-13 12:57:30 -0400313 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
314 if (!dev->tstats)
315 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700316
317 err = gro_cells_init(&geneve->gro_cells, dev);
318 if (err) {
319 free_percpu(dev->tstats);
320 return err;
321 }
322
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200323 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100324 if (err) {
325 free_percpu(dev->tstats);
326 gro_cells_destroy(&geneve->gro_cells);
327 return err;
328 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400329 return 0;
330}
331
332static void geneve_uninit(struct net_device *dev)
333{
Jesse Gross8e816df2015-08-28 16:54:40 -0700334 struct geneve_dev *geneve = netdev_priv(dev);
335
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200336 dst_cache_destroy(&geneve->cfg.info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700337 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400338 free_percpu(dev->tstats);
339}
340
Pravin B Shelar371bd102015-08-26 23:46:54 -0700341/* Callback from net/ipv4/udp.c to receive packets */
342static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
343{
344 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100345 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700346 struct geneve_sock *gs;
347 int opts_len;
348
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700349 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700350 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200351 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700352
353 /* Return packets with reserved bits set */
354 geneveh = geneve_hdr(skb);
355 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200356 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700357
358 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200359 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700360
Jiri Benc9fc47542016-02-18 11:22:50 +0100361 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 Shelar371bd102015-08-26 23:46:54 -0700369 opts_len = geneveh->opt_len * 4;
370 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100371 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700372 !net_eq(geneve->net, dev_net(geneve->dev)))) {
373 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700374 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700375 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700376
Jiri Benc9fc47542016-02-18 11:22:50 +0100377 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700378 return 0;
379
380drop:
381 /* Consume bad packet */
382 kfree_skb(skb);
383 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384}
385
Stefano Brivioa0796642018-11-08 12:19:18 +0100386/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
387static 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 Brivioeccc73a2019-06-11 00:27:06 +0200394 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
Stefano Brivioa0796642018-11-08 12:19:18 +0100395 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 Chancellor8a962c42018-11-16 18:36:27 -0700423 struct in6_addr addr6;
424
425 memset(&addr6, 0, sizeof(struct in6_addr));
Stefano Brivioa0796642018-11-08 12:19:18 +0100426
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 Shelar371bd102015-08-26 23:46:54 -0700439static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800440 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700441{
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. Linville8ed66f02015-10-26 17:01:44 -0400450 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800451 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700452 } 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 Shelar371bd102015-08-26 23:46:54 -0700467static int geneve_hlen(struct genevehdr *gh)
468{
469 return sizeof(*gh) + gh->opt_len * 4;
470}
471
David Millerd4546c22018-06-24 14:13:49 +0900472static struct sk_buff *geneve_gro_receive(struct sock *sk,
473 struct list_head *head,
474 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700475{
David Millerd4546c22018-06-24 14:13:49 +0900476 struct sk_buff *pp = NULL;
477 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700478 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 Millerd4546c22018-06-24 14:13:49 +0900504 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700505 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 Duyckc194cf92016-03-09 09:24:23 -0800520 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700521 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700522
523 skb_gro_pull(skb, gh_len);
524 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200525 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800526 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700527
528out_unlock:
529 rcu_read_unlock();
530out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200531 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700532
533 return pp;
534}
535
Tom Herbert4a0090a2016-04-05 08:22:55 -0700536static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
537 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700538{
539 struct genevehdr *gh;
540 struct packet_offload *ptype;
541 __be16 type;
542 int gh_len;
543 int err = -ENOSYS;
544
Pravin B Shelar371bd102015-08-26 23:46:54 -0700545 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 Rajahalme229740c2016-05-03 16:10:21 -0700555
556 skb_set_inner_mac_header(skb, nhoff + gh_len);
557
Pravin B Shelar371bd102015-08-26 23:46:54 -0700558 return err;
559}
560
561/* Create new listen socket if needed */
562static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800563 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700564{
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 Shelar66d47002015-08-26 23:46:55 -0700569 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700570
571 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
572 if (!gs)
573 return ERR_PTR(-ENOMEM);
574
pravin shelar9b4437a2016-11-21 11:02:58 -0800575 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700576 if (IS_ERR(sock)) {
577 kfree(gs);
578 return ERR_CAST(sock);
579 }
580
581 gs->sock = sock;
582 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700583 for (h = 0; h < VNI_HASH_SIZE; ++h)
584 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700585
586 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700587 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700588
589 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700590 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700591 tunnel_cfg.sk_user_data = gs;
592 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700593 tunnel_cfg.gro_receive = geneve_gro_receive;
594 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700595 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100596 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700597 tunnel_cfg.encap_destroy = NULL;
598 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700599 list_add(&gs->list, &gn->sock_list);
600 return gs;
601}
602
John W. Linville8ed66f02015-10-26 17:01:44 -0400603static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700604{
John W. Linville8ed66f02015-10-26 17:01:44 -0400605 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606 return;
607
608 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700609 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700610 udp_tunnel_sock_release(gs->sock);
611 kfree_rcu(gs, rcu);
612}
613
John W. Linville8ed66f02015-10-26 17:01:44 -0400614static void geneve_sock_release(struct geneve_dev *geneve)
615{
pravin shelarfceb9c32016-10-28 09:59:16 -0700616 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400617#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700618 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. Linville8ed66f02015-10-26 17:01:44 -0400629#endif
630}
631
Pravin B Shelar371bd102015-08-26 23:46:54 -0700632static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400633 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700634 __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 Benc1e9f12e2016-02-18 11:22:49 +0100640 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700641 return gs;
642 }
643 }
644 return NULL;
645}
646
John W. Linville8ed66f02015-10-26 17:01:44 -0400647static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400648{
John W. Linville2d07dc72015-05-13 12:57:30 -0400649 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700650 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200651 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400652 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800653 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700654 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400655
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200656 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700657 if (gs) {
658 gs->refcnt++;
659 goto out;
660 }
661
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200662 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
663 geneve->cfg.use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400664 if (IS_ERR(gs))
665 return PTR_ERR(gs);
666
Pravin B Shelar371bd102015-08-26 23:46:54 -0700667out:
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200668 gs->collect_md = geneve->cfg.collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400669#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200670 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700671 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200672 node = &geneve->hlist6;
673 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400674#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200675 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700676 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200677 node = &geneve->hlist4;
678 }
679 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700680
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200681 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
pravin shelar9b4437a2016-11-21 11:02:58 -0800682 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200683 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400684 return 0;
685}
686
John W. Linville8ed66f02015-10-26 17:01:44 -0400687static int geneve_open(struct net_device *dev)
688{
689 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200690 bool metadata = geneve->cfg.collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100691 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400692 int ret = 0;
693
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200694 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100695 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400696#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100697 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100699 if (ret < 0 && ret != -EAFNOSUPPORT)
700 ipv4 = false;
701 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400702#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100703 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400704 ret = geneve_sock_add(geneve, false);
705 if (ret < 0)
706 geneve_sock_release(geneve);
707
708 return ret;
709}
710
John W. Linville2d07dc72015-05-13 12:57:30 -0400711static int geneve_stop(struct net_device *dev)
712{
713 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400714
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200715 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. Linville8ed66f02015-10-26 17:01:44 -0400719 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400720 return 0;
721}
722
John W. Linville8ed66f02015-10-26 17:01:44 -0400723static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800724 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400725{
726 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800727 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. Linville8ed66f02015-10-26 17:01:44 -0400730 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800731 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400732 geneveh->proto_type = htons(ETH_P_TEB);
733 geneveh->rsvd2 = 0;
734
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700735 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
736 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400737}
738
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800739static 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 Shelar371bd102015-08-26 23:46:54 -0700742{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800743 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700744 struct genevehdr *gnvh;
745 int min_headroom;
746 int err;
747
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800748 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400749 skb_scrub_packet(skb, xnet);
750
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800751 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
752 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400753 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400754 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400755 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400756
Alexander Duyckaed069d2016-04-14 15:33:37 -0400757 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300758 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400759 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400760
Johannes Bergd58ff352017-06-16 14:29:23 +0200761 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800762 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400763 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
764 return 0;
765
766free_dst:
767 dst_release(dst);
768 return err;
769}
John W. Linville8ed66f02015-10-26 17:01:44 -0400770
771static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
772 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700773 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400774 struct flowi4 *fl4,
Mark Gray34beb212020-09-16 05:19:35 -0400775 const struct ip_tunnel_info *info,
776 __be16 dport, __be16 sport)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100778 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700779 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100780 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700781 struct rtable *rt = NULL;
782 __u8 tos;
783
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700784 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700785 return ERR_PTR(-EIO);
786
Pravin B Shelare305ac62015-08-26 23:46:52 -0700787 memset(fl4, 0, sizeof(*fl4));
788 fl4->flowi4_mark = skb->mark;
789 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800790 fl4->daddr = info->key.u.ipv4.dst;
791 fl4->saddr = info->key.u.ipv4.src;
Mark Gray34beb212020-09-16 05:19:35 -0400792 fl4->fl4_dport = dport;
793 fl4->fl4_sport = sport;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700794
pravin shelar9b4437a2016-11-21 11:02:58 -0800795 tos = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200796 if ((tos == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800797 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
798 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100799 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800800 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100801
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800802 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100803 if (use_cache) {
804 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
805 if (rt)
806 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700807 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700808 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 Shelarfc4099f2015-10-22 18:17:16 -0700811 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700812 }
813 if (rt->dst.dev == dev) { /* is this necessary? */
814 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700815 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700816 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700817 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100818 if (use_cache)
819 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700820 return rt;
821}
822
John W. Linville8ed66f02015-10-26 17:01:44 -0400823#if IS_ENABLED(CONFIG_IPV6)
824static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
825 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700826 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400827 struct flowi6 *fl6,
Mark Gray34beb212020-09-16 05:19:35 -0400828 const struct ip_tunnel_info *info,
829 __be16 dport, __be16 sport)
John W. Linville8ed66f02015-10-26 17:01:44 -0400830{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100831 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400832 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400833 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100834 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400835 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400836
pravin shelarfceb9c32016-10-28 09:59:16 -0700837 if (!gs6)
838 return ERR_PTR(-EIO);
839
John W. Linville8ed66f02015-10-26 17:01:44 -0400840 memset(fl6, 0, sizeof(*fl6));
841 fl6->flowi6_mark = skb->mark;
842 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800843 fl6->daddr = info->key.u.ipv6.dst;
844 fl6->saddr = info->key.u.ipv6.src;
Mark Gray34beb212020-09-16 05:19:35 -0400845 fl6->fl6_dport = dport;
846 fl6->fl6_sport = sport;
847
pravin shelar9b4437a2016-11-21 11:02:58 -0800848 prio = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200849 if ((prio == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800850 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
851 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100852 }
853
pravin shelar9b4437a2016-11-21 11:02:58 -0800854 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
855 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800856 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100857 if (use_cache) {
858 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
859 if (dst)
860 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400861 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100862 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
863 NULL);
864 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400865 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 Abeni468dfff2016-02-12 15:43:58 +0100874 if (use_cache)
875 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400876 return dst;
877}
878#endif
879
pravin shelar9b4437a2016-11-21 11:02:58 -0800880static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800881 struct geneve_dev *geneve,
882 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700883{
pravin shelar9b4437a2016-11-21 11:02:58 -0800884 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. Linville2d07dc72015-05-13 12:57:30 -0400888 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400889 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100890 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700891 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800892 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400893
Mark Gray34beb212020-09-16 05:19:35 -0400894 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 shelar9b4437a2016-11-21 11:02:58 -0800897 if (IS_ERR(rt))
898 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700899
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200900 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 Long52a589d2017-12-25 14:43:58 +0800925
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200926 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800927 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700928 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100929
930 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700931 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800932 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200933 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800934 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
935 else
936 ttl = key->ttl;
937 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100938
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200939 if (geneve->cfg.df == GENEVE_DF_SET) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100940 df = htons(IP_DF);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200941 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100942 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. Linville2d07dc72015-05-13 12:57:30 -0400953 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800954
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800955 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800956 if (unlikely(err))
957 return err;
958
Pravin B Shelar039f5062015-12-24 14:34:54 -0800959 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200960 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800961 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800962 !(info->key.tun_flags & TUNNEL_CSUM));
963 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400964}
965
John W. Linville8ed66f02015-10-26 17:01:44 -0400966#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800967static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800968 struct geneve_dev *geneve,
969 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400970{
pravin shelar9b4437a2016-11-21 11:02:58 -0800971 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. Linville8ed66f02015-10-26 17:01:44 -0400974 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400975 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400976 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400977 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800978 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400979
Mark Gray34beb212020-09-16 05:19:35 -0400980 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 shelar9b4437a2016-11-21 11:02:58 -0800983 if (IS_ERR(dst))
984 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400985
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200986 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 Long52a589d2017-12-25 14:43:58 +08001010
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001011 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001012 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001013 ttl = key->ttl;
1014 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +01001015 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -08001016 ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001017 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001018 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1019 else
1020 ttl = key->ttl;
1021 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -04001022 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +08001023 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -08001024 if (unlikely(err))
1025 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001026
Pravin B Shelar039f5062015-12-24 14:34:54 -08001027 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001028 &fl6.saddr, &fl6.daddr, prio, ttl,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001029 info->key.label, sport, geneve->cfg.info.key.tp_dst,
pravin shelar9b4437a2016-11-21 11:02:58 -08001030 !(info->key.tun_flags & TUNNEL_CSUM));
1031 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -04001032}
1033#endif
1034
1035static 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 shelar9b4437a2016-11-21 11:02:58 -08001039 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001040
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001041 if (geneve->cfg.collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001042 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001043 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001044 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +02001045 dev_kfree_skb(skb);
1046 dev->stats.tx_dropped++;
1047 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -08001048 }
1049 } else {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001050 info = &geneve->cfg.info;
pravin shelar9b4437a2016-11-21 11:02:58 -08001051 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001052
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001053 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001054#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001055 if (info->mode & IP_TUNNEL_INFO_IPV6)
1056 err = geneve6_xmit_skb(skb, dev, geneve, info);
1057 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001058#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001059 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001060 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001061
1062 if (likely(!err))
1063 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001064
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001065 if (err != -EMSGSIZE)
1066 dev_kfree_skb(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001067
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. Linville8ed66f02015-10-26 17:01:44 -04001075}
1076
Jarod Wilson91572082016-10-20 13:55:20 -04001077static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001078{
Jarod Wilson91572082016-10-20 13:55:20 -04001079 if (new_mtu > dev->max_mtu)
1080 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001081 else if (new_mtu < dev->min_mtu)
1082 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001083
David Wragg55e5bfb2016-02-10 00:05:57 +00001084 dev->mtu = new_mtu;
1085 return 0;
1086}
1087
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001088static 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 Gray34beb212020-09-16 05:19:35 -04001092 __be16 sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001093
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001094 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001095 struct rtable *rt;
1096 struct flowi4 fl4;
1097
Mark Gray34beb212020-09-16 05:19:35 -04001098 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. Linvilleb8812fa2015-10-27 09:49:00 -04001104 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 shelar9b4437a2016-11-21 11:02:58 -08001111 struct dst_entry *dst;
1112 struct flowi6 fl6;
1113
Mark Gray34beb212020-09-16 05:19:35 -04001114 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. Linvilleb8812fa2015-10-27 09:49:00 -04001120 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 Shelarfc4099f2015-10-22 18:17:16 -07001127 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001128 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001129
Mark Gray34beb212020-09-16 05:19:35 -04001130 info->key.tp_src = sport;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001131 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001132 return 0;
1133}
1134
John W. Linville2d07dc72015-05-13 12:57:30 -04001135static 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 Kallweitb220a4a2020-11-07 21:52:06 +01001141 .ndo_get_stats64 = dev_get_tstats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001142 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001143 .ndo_validate_addr = eth_validate_addr,
1144 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001145 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001146};
1147
1148static 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
1155static 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 */
1161static struct device_type geneve_type = {
1162 .name = "geneve",
1163};
1164
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001165/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001166 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001167 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001168 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001169static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001170{
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 Sowa681e6832016-04-18 21:19:48 +02001174
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001175 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001176 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, Anjali05ca4022015-12-14 12:21:20 -08001185 rcu_read_unlock();
1186}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001187
John W. Linville2d07dc72015-05-13 12:57:30 -04001188/* Initialize the device structure. */
1189static 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. Millercf124db2017-05-08 12:52:56 -04001195 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001196
1197 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1198
John W. Linville2d07dc72015-05-13 12:57:30 -04001199 dev->features |= NETIF_F_LLTX;
Xin Long18423e12021-01-15 17:47:46 +08001200 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
John W. Linville2d07dc72015-05-13 12:57:30 -04001201 dev->features |= NETIF_F_RXCSUM;
1202 dev->features |= NETIF_F_GSO_SOFTWARE;
1203
Xin Long18423e12021-01-15 17:47:46 +08001204 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1205 dev->hw_features |= NETIF_F_RXCSUM;
John W. Linville2d07dc72015-05-13 12:57:30 -04001206 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001207
Jarod Wilson91572082016-10-20 13:55:20 -04001208 /* MTU range: 68 - (something less than 65535) */
1209 dev->min_mtu = ETH_MIN_MTU;
1210 /* The max_mtu calculation does not take account of GENEVE
1211 * options, to avoid excluding potentially valid
1212 * configurations. This will be further reduced by IPvX hdr size.
1213 */
1214 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1215
John W. Linville2d07dc72015-05-13 12:57:30 -04001216 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001217 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001218 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001219 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001220}
1221
1222static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1223 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001224 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001225 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001226 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001227 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001228 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001229 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001230 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001231 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1232 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1233 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001234 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001235 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001236};
1237
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001238static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1239 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001240{
1241 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001242 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1243 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1244 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001245 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001246 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001247
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001248 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1249 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1250 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001251 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001252 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001253 }
1254
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001255 if (!data) {
1256 NL_SET_ERR_MSG(extack,
1257 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001258 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001259 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001260
1261 if (data[IFLA_GENEVE_ID]) {
1262 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1263
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001264 if (vni >= GENEVE_N_VID) {
1265 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1266 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001267 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001268 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001269 }
1270
Stefano Brivioa025fb52018-11-08 12:19:19 +01001271 if (data[IFLA_GENEVE_DF]) {
1272 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1273
1274 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001275 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001276 "Invalid DF attribute");
1277 return -EINVAL;
1278 }
1279 }
1280
John W. Linville2d07dc72015-05-13 12:57:30 -04001281 return 0;
1282}
1283
Pravin B Shelar371bd102015-08-26 23:46:54 -07001284static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001285 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001286 bool *tun_on_same_port,
1287 bool *tun_collect_md)
1288{
pravin shelar9b4437a2016-11-21 11:02:58 -08001289 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001290
1291 *tun_on_same_port = false;
1292 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001293 list_for_each_entry(geneve, &gn->geneve_list, next) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001294 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1295 *tun_collect_md = geneve->cfg.collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001296 *tun_on_same_port = true;
1297 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001298 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1299 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1300 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001301 t = geneve;
1302 }
1303 return t;
1304}
1305
pravin shelar9b4437a2016-11-21 11:02:58 -08001306static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1307{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001308 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1309 info->key.ttl || info->key.label || info->key.tp_src ||
1310 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001311}
1312
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001313static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1314 struct ip_tunnel_info *b)
1315{
1316 if (ip_tunnel_info_af(a) == AF_INET)
1317 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1318 else
1319 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1320}
1321
Pravin B Shelare305ac62015-08-26 23:46:52 -07001322static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001323 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001324 const struct geneve_config *cfg)
John W. Linville2d07dc72015-05-13 12:57:30 -04001325{
1326 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001327 struct geneve_dev *t, *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001328 const struct ip_tunnel_info *info = &cfg->info;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001329 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001330 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001331
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001332 if (cfg->collect_md && !is_tnl_info_zero(info)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001333 NL_SET_ERR_MSG(extack,
1334 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001335 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001336 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001337
1338 geneve->net = net;
1339 geneve->dev = dev;
1340
pravin shelar9b4437a2016-11-21 11:02:58 -08001341 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001342 if (t)
1343 return -EBUSY;
1344
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001345 /* make enough headroom for basic scenario */
1346 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001347 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001348 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001349 dev->max_mtu -= sizeof(struct iphdr);
1350 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001351 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001352 dev->max_mtu -= sizeof(struct ipv6hdr);
1353 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001354 dev->needed_headroom = encap_len + ETH_HLEN;
1355
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001356 if (cfg->collect_md) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001357 if (tun_on_same_port) {
1358 NL_SET_ERR_MSG(extack,
1359 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001360 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001361 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001362 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001363 if (tun_collect_md) {
1364 NL_SET_ERR_MSG(extack,
1365 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001366 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001367 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001368 }
1369
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001370 dst_cache_reset(&geneve->cfg.info.dst_cache);
1371 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
Paolo Abeni468dfff2016-02-12 15:43:58 +01001372
John W. Linville2d07dc72015-05-13 12:57:30 -04001373 err = register_netdevice(dev);
1374 if (err)
1375 return err;
1376
1377 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001378 return 0;
1379}
1380
pravin shelar9b4437a2016-11-21 11:02:58 -08001381static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1382{
1383 memset(info, 0, sizeof(*info));
1384 info->key.tp_dst = htons(dst_port);
1385}
1386
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001387static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1388 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001389 struct geneve_config *cfg, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001390{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001391 struct ip_tunnel_info *info = &cfg->info;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001392 int attrtype;
1393
1394 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1395 NL_SET_ERR_MSG(extack,
1396 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001397 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001398 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001399
1400 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001401 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1402 attrtype = IFLA_GENEVE_REMOTE;
1403 goto change_notsup;
1404 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001405
1406 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001407 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001408
Dave Taht842841e2019-09-02 16:29:36 -07001409 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001410 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1411 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001412 return -EINVAL;
1413 }
1414 }
1415
pravin shelar9b4437a2016-11-21 11:02:58 -08001416 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001417#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001418 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1419 attrtype = IFLA_GENEVE_REMOTE6;
1420 goto change_notsup;
1421 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001422
1423 info->mode = IP_TUNNEL_INFO_IPV6;
1424 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001425 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001426
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001427 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001428 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001429 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1430 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001431 return -EINVAL;
1432 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001433 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001434 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1435 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001436 return -EINVAL;
1437 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001438 info->key.tun_flags |= TUNNEL_CSUM;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001439 cfg->use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001440#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001441 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1442 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001443 return -EPFNOSUPPORT;
1444#endif
1445 }
1446
1447 if (data[IFLA_GENEVE_ID]) {
1448 __u32 vni;
1449 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001450 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001451
1452 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1453 tvni[0] = (vni & 0x00ff0000) >> 16;
1454 tvni[1] = (vni & 0x0000ff00) >> 8;
1455 tvni[2] = vni & 0x000000ff;
1456
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001457 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001458 if (changelink && (tunid != info->key.tun_id)) {
1459 attrtype = IFLA_GENEVE_ID;
1460 goto change_notsup;
1461 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001462 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001463 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001464
Hangbin Liua97d97b2018-09-29 23:06:29 +08001465 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1466 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001467 cfg->ttl_inherit = true;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001468 else
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001469 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001470 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001471 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001472 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001473 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001474
Pravin B Shelare305ac62015-08-26 23:46:52 -07001475 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001476 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001477
Stefano Brivioa025fb52018-11-08 12:19:19 +01001478 if (data[IFLA_GENEVE_DF])
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001479 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001480
pravin shelar9b4437a2016-11-21 11:02:58 -08001481 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001482 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001483 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001484 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1485 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1486 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001487 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001488 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001489 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001490
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001491 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001492 if (changelink) {
1493 attrtype = IFLA_GENEVE_PORT;
1494 goto change_notsup;
1495 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001496 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1497 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001498
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001499 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001500 if (changelink) {
1501 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1502 goto change_notsup;
1503 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001504 cfg->collect_md = true;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001505 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001506
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001507 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001508 if (changelink) {
1509 attrtype = IFLA_GENEVE_UDP_CSUM;
1510 goto change_notsup;
1511 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001512 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1513 info->key.tun_flags |= TUNNEL_CSUM;
1514 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001515
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001516 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001517#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001518 if (changelink) {
1519 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1520 goto change_notsup;
1521 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001522 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1523 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001524#else
1525 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1526 "IPv6 support not enabled in the kernel");
1527 return -EPFNOSUPPORT;
1528#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001529 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001530
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001531 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001532#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001533 if (changelink) {
1534 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1535 goto change_notsup;
1536 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001537 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001538 cfg->use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001539#else
1540 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1541 "IPv6 support not enabled in the kernel");
1542 return -EPFNOSUPPORT;
1543#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001544 }
1545
1546 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001547change_notsup:
1548 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1549 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1550 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001551}
1552
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001553static void geneve_link_config(struct net_device *dev,
1554 struct ip_tunnel_info *info, struct nlattr *tb[])
1555{
1556 struct geneve_dev *geneve = netdev_priv(dev);
1557 int ldev_mtu = 0;
1558
1559 if (tb[IFLA_MTU]) {
1560 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1561 return;
1562 }
1563
1564 switch (ip_tunnel_info_af(info)) {
1565 case AF_INET: {
1566 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1567 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1568
1569 if (!IS_ERR(rt) && rt->dst.dev) {
1570 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1571 ip_rt_put(rt);
1572 }
1573 break;
1574 }
1575#if IS_ENABLED(CONFIG_IPV6)
1576 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001577 struct rt6_info *rt;
1578
1579 if (!__in6_dev_get(dev))
1580 break;
1581
1582 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1583 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001584
1585 if (rt && rt->dst.dev)
1586 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1587 ip6_rt_put(rt);
1588 break;
1589 }
1590#endif
1591 }
1592
1593 if (ldev_mtu <= 0)
1594 return;
1595
1596 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1597}
1598
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001599static int geneve_newlink(struct net *net, struct net_device *dev,
1600 struct nlattr *tb[], struct nlattr *data[],
1601 struct netlink_ext_ack *extack)
1602{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001603 struct geneve_config cfg = {
1604 .df = GENEVE_DF_UNSET,
1605 .use_udp6_rx_checksums = false,
1606 .ttl_inherit = false,
1607 .collect_md = false,
1608 };
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001609 int err;
1610
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001611 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1612 err = geneve_nl2info(tb, data, extack, &cfg, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001613 if (err)
1614 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001615
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001616 err = geneve_configure(net, dev, extack, &cfg);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001617 if (err)
1618 return err;
1619
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001620 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001621
1622 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001623}
1624
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001625/* Quiesces the geneve device data path for both TX and RX.
1626 *
1627 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1628 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1629 * to complete for the existing set of in-flight packets to be transmitted,
1630 * then we would have quiesced the transmit data path. All the future packets
1631 * will get dropped until we unquiesce the data path.
1632 *
1633 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1634 * if we set that to NULL under RCU and wait for synchronize_net() to
1635 * complete, then we would have quiesced the receive data path.
1636 */
1637static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1638 struct geneve_sock **gs6)
1639{
1640 *gs4 = rtnl_dereference(geneve->sock4);
1641 rcu_assign_pointer(geneve->sock4, NULL);
1642 if (*gs4)
1643 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1644#if IS_ENABLED(CONFIG_IPV6)
1645 *gs6 = rtnl_dereference(geneve->sock6);
1646 rcu_assign_pointer(geneve->sock6, NULL);
1647 if (*gs6)
1648 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1649#else
1650 *gs6 = NULL;
1651#endif
1652 synchronize_net();
1653}
1654
1655/* Resumes the geneve device data path for both TX and RX. */
1656static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1657 struct geneve_sock __maybe_unused *gs6)
1658{
1659 rcu_assign_pointer(geneve->sock4, gs4);
1660 if (gs4)
1661 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1662#if IS_ENABLED(CONFIG_IPV6)
1663 rcu_assign_pointer(geneve->sock6, gs6);
1664 if (gs6)
1665 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1666#endif
1667 synchronize_net();
1668}
1669
1670static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1671 struct nlattr *data[],
1672 struct netlink_ext_ack *extack)
1673{
1674 struct geneve_dev *geneve = netdev_priv(dev);
1675 struct geneve_sock *gs4, *gs6;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001676 struct geneve_config cfg;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001677 int err;
1678
1679 /* If the geneve device is configured for metadata (or externally
1680 * controlled, for example, OVS), then nothing can be changed.
1681 */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001682 if (geneve->cfg.collect_md)
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001683 return -EOPNOTSUPP;
1684
1685 /* Start with the existing info. */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001686 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1687 err = geneve_nl2info(tb, data, extack, &cfg, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001688 if (err)
1689 return err;
1690
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001691 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1692 dst_cache_reset(&cfg.info.dst_cache);
1693 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001694 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001695
1696 geneve_quiesce(geneve, &gs4, &gs6);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001697 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001698 geneve_unquiesce(geneve, gs4, gs6);
1699
1700 return 0;
1701}
1702
John W. Linville2d07dc72015-05-13 12:57:30 -04001703static void geneve_dellink(struct net_device *dev, struct list_head *head)
1704{
1705 struct geneve_dev *geneve = netdev_priv(dev);
1706
John W. Linville2d07dc72015-05-13 12:57:30 -04001707 list_del(&geneve->next);
1708 unregister_netdevice_queue(dev, head);
1709}
1710
1711static size_t geneve_get_size(const struct net_device *dev)
1712{
1713 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001714 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001715 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001716 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001717 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001718 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001719 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001720 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001721 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1722 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1723 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001724 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001725 0;
1726}
1727
1728static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1729{
1730 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001731 struct ip_tunnel_info *info = &geneve->cfg.info;
1732 bool ttl_inherit = geneve->cfg.ttl_inherit;
1733 bool metadata = geneve->cfg.collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001734 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001735 __u32 vni;
1736
pravin shelar9b4437a2016-11-21 11:02:58 -08001737 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1738 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001739 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1740 goto nla_put_failure;
1741
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001742 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001743 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001744 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001745 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001746 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1747 !!(info->key.tun_flags & TUNNEL_CSUM)))
1748 goto nla_put_failure;
1749
John W. Linville8ed66f02015-10-26 17:01:44 -04001750#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001751 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001752 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001753 &info->key.u.ipv6.dst))
1754 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001755 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1756 !(info->key.tun_flags & TUNNEL_CSUM)))
1757 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001758#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001759 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001760
pravin shelar9b4437a2016-11-21 11:02:58 -08001761 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1762 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1763 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001764 goto nla_put_failure;
1765
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001766 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
Stefano Brivioa025fb52018-11-08 12:19:19 +01001767 goto nla_put_failure;
1768
pravin shelar9b4437a2016-11-21 11:02:58 -08001769 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001770 goto nla_put_failure;
1771
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001772 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001773 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001774
Hangbin Liuf9094b72017-11-23 11:27:24 +08001775#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001776 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001777 !geneve->cfg.use_udp6_rx_checksums))
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001778 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001779#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001780
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001781 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1782 goto nla_put_failure;
1783
John W. Linville2d07dc72015-05-13 12:57:30 -04001784 return 0;
1785
1786nla_put_failure:
1787 return -EMSGSIZE;
1788}
1789
1790static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1791 .kind = "geneve",
1792 .maxtype = IFLA_GENEVE_MAX,
1793 .policy = geneve_policy,
1794 .priv_size = sizeof(struct geneve_dev),
1795 .setup = geneve_setup,
1796 .validate = geneve_validate,
1797 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001798 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001799 .dellink = geneve_dellink,
1800 .get_size = geneve_get_size,
1801 .fill_info = geneve_fill_info,
1802};
1803
Pravin B Shelare305ac62015-08-26 23:46:52 -07001804struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1805 u8 name_assign_type, u16 dst_port)
1806{
1807 struct nlattr *tb[IFLA_MAX + 1];
1808 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001809 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001810 int err;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001811 struct geneve_config cfg = {
1812 .df = GENEVE_DF_UNSET,
1813 .use_udp6_rx_checksums = true,
1814 .ttl_inherit = false,
1815 .collect_md = true,
1816 };
Pravin B Shelare305ac62015-08-26 23:46:52 -07001817
1818 memset(tb, 0, sizeof(tb));
1819 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001820 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001821 if (IS_ERR(dev))
1822 return dev;
1823
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001824 init_tnl_info(&cfg.info, dst_port);
1825 err = geneve_configure(net, dev, NULL, &cfg);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001826 if (err) {
1827 free_netdev(dev);
1828 return ERR_PTR(err);
1829 }
David Wragg7e059152016-02-10 00:05:58 +00001830
1831 /* openvswitch users expect packet sizes to be unrestricted,
1832 * so set the largest MTU we can.
1833 */
Jarod Wilson91572082016-10-20 13:55:20 -04001834 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001835 if (err)
1836 goto err;
1837
Nicolas Dichtel41009482016-06-13 10:31:07 +02001838 err = rtnl_configure_link(dev, NULL);
1839 if (err < 0)
1840 goto err;
1841
Pravin B Shelare305ac62015-08-26 23:46:52 -07001842 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001843err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001844 geneve_dellink(dev, &list_kill);
1845 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001846 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001847}
1848EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1849
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001850static int geneve_netdevice_event(struct notifier_block *unused,
1851 unsigned long event, void *ptr)
1852{
1853 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1854
Jakub Kicinskidedc33e72021-01-06 13:06:35 -08001855 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1856 geneve_offload_rx_ports(dev, true);
1857 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1858 geneve_offload_rx_ports(dev, false);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001859
1860 return NOTIFY_DONE;
1861}
1862
1863static struct notifier_block geneve_notifier_block __read_mostly = {
1864 .notifier_call = geneve_netdevice_event,
1865};
1866
John W. Linville2d07dc72015-05-13 12:57:30 -04001867static __net_init int geneve_init_net(struct net *net)
1868{
1869 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001870
1871 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001872 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001873 return 0;
1874}
1875
Haishuang Yan2843a252017-12-16 17:54:50 +08001876static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001877{
1878 struct geneve_net *gn = net_generic(net, geneve_net_id);
1879 struct geneve_dev *geneve, *next;
1880 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001881
1882 /* gather any geneve devices that were moved into this ns */
1883 for_each_netdev_safe(net, dev, aux)
1884 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001885 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001886
1887 /* now gather any other geneve devices that were created in this ns */
1888 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1889 /* If geneve->dev is in the same netns, it was already added
1890 * to the list by the previous loop.
1891 */
1892 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001893 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001894 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001895}
1896
1897static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1898{
1899 struct net *net;
1900 LIST_HEAD(list);
1901
1902 rtnl_lock();
1903 list_for_each_entry(net, net_list, exit_list)
1904 geneve_destroy_tunnels(net, &list);
1905
John W. Linville2d07dc72015-05-13 12:57:30 -04001906 /* unregister the devices gathered above */
1907 unregister_netdevice_many(&list);
1908 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001909
1910 list_for_each_entry(net, net_list, exit_list) {
1911 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1912
1913 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1914 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001915}
1916
1917static struct pernet_operations geneve_net_ops = {
1918 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001919 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001920 .id = &geneve_net_id,
1921 .size = sizeof(struct geneve_net),
1922};
1923
1924static int __init geneve_init_module(void)
1925{
1926 int rc;
1927
1928 rc = register_pernet_subsys(&geneve_net_ops);
1929 if (rc)
1930 goto out1;
1931
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001932 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001933 if (rc)
1934 goto out2;
1935
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001936 rc = rtnl_link_register(&geneve_link_ops);
1937 if (rc)
1938 goto out3;
1939
John W. Linville2d07dc72015-05-13 12:57:30 -04001940 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001941out3:
1942 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001943out2:
1944 unregister_pernet_subsys(&geneve_net_ops);
1945out1:
1946 return rc;
1947}
1948late_initcall(geneve_init_module);
1949
1950static void __exit geneve_cleanup_module(void)
1951{
1952 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001953 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001954 unregister_pernet_subsys(&geneve_net_ops);
1955}
1956module_exit(geneve_cleanup_module);
1957
1958MODULE_LICENSE("GPL");
1959MODULE_VERSION(GENEVE_NETDEV_VER);
1960MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1961MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1962MODULE_ALIAS_RTNL_LINK("geneve");