blob: 1426bfc009bc39a61e2cf039c57b56e48abc3fdc [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
10#include <linux/kernel.h>
11#include <linux/module.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040012#include <linux/etherdevice.h>
13#include <linux/hash.h>
David Ahern3616d082019-03-22 06:06:09 -070014#include <net/ipv6_stubs.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070015#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070016#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040017#include <net/rtnetlink.h>
18#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070019#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040020
21#define GENEVE_NETDEV_VER "0.6"
22
John W. Linville2d07dc72015-05-13 12:57:30 -040023#define GENEVE_N_VID (1u << 24)
24#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
25
26#define VNI_HASH_BITS 10
27#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
28
29static bool log_ecn_error = true;
30module_param(log_ecn_error, bool, 0644);
31MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
32
Pravin B Shelar371bd102015-08-26 23:46:54 -070033#define GENEVE_VER 0
34#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
Alexey Kodanev5edbea62018-04-19 15:42:30 +030035#define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
36#define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
Pravin B Shelar371bd102015-08-26 23:46:54 -070037
John W. Linville2d07dc72015-05-13 12:57:30 -040038/* per-network namespace private data for this module */
39struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070040 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070041 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040042};
43
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030044static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070045
Jiri Benc4b4c21f2017-07-02 19:00:58 +020046struct geneve_dev_node {
47 struct hlist_node hlist;
48 struct geneve_dev *geneve;
49};
50
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020051struct geneve_config {
52 struct ip_tunnel_info info;
53 bool collect_md;
54 bool use_udp6_rx_checksums;
55 bool ttl_inherit;
56 enum ifla_geneve_df df;
57};
58
John W. Linville2d07dc72015-05-13 12:57:30 -040059/* Pseudo network device */
60struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020061 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
62#if IS_ENABLED(CONFIG_IPV6)
63 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
64#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040065 struct net *net; /* netns for packet i/o */
66 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelarfceb9c32016-10-28 09:59:16 -070067 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040068#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070069 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040070#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040071 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070072 struct gro_cells gro_cells;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020073 struct geneve_config cfg;
John W. Linville2d07dc72015-05-13 12:57:30 -040074};
75
Pravin B Shelar371bd102015-08-26 23:46:54 -070076struct geneve_sock {
77 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070078 struct list_head list;
79 struct socket *sock;
80 struct rcu_head rcu;
81 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070082 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070083};
John W. Linville2d07dc72015-05-13 12:57:30 -040084
85static inline __u32 geneve_net_vni_hash(u8 vni[3])
86{
87 __u32 vnid;
88
89 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
90 return hash_32(vnid, VNI_HASH_BITS);
91}
92
Pravin B Shelare305ac62015-08-26 23:46:52 -070093static __be64 vni_to_tunnel_id(const __u8 *vni)
94{
95#ifdef __BIG_ENDIAN
96 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
97#else
98 return (__force __be64)(((__force u64)vni[0] << 40) |
99 ((__force u64)vni[1] << 48) |
100 ((__force u64)vni[2] << 56));
101#endif
102}
103
pravin shelar9b4437a2016-11-21 11:02:58 -0800104/* Convert 64 bit tunnel ID to 24 bit VNI. */
105static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
106{
107#ifdef __BIG_ENDIAN
108 vni[0] = (__force __u8)(tun_id >> 16);
109 vni[1] = (__force __u8)(tun_id >> 8);
110 vni[2] = (__force __u8)tun_id;
111#else
112 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
113 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
114 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
115#endif
116}
117
pravin shelar2e0b26e2016-11-21 11:03:01 -0800118static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
119{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800120 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800121}
122
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100123static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
124{
125 return gs->sock->sk->sk_family;
126}
127
Pravin B Shelar66d47002015-08-26 23:46:55 -0700128static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700129 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400130{
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200132 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400133 __u32 hash;
134
John W. Linville2d07dc72015-05-13 12:57:30 -0400135 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700136 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700137 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200138 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200139 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
140 addr == node->geneve->cfg.info.key.u.ipv4.dst)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200141 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400142 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700143 return NULL;
144}
145
John W. Linville8ed66f02015-10-26 17:01:44 -0400146#if IS_ENABLED(CONFIG_IPV6)
147static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
148 struct in6_addr addr6, u8 vni[])
149{
150 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200151 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400152 __u32 hash;
153
154 /* Find the device for this VNI */
155 hash = geneve_net_vni_hash(vni);
156 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200157 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200158 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
159 ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200160 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400161 }
162 return NULL;
163}
164#endif
165
Pravin B Shelar371bd102015-08-26 23:46:54 -0700166static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
167{
168 return (struct genevehdr *)(udp_hdr(skb) + 1);
169}
170
Jiri Benc9fc47542016-02-18 11:22:50 +0100171static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
172 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700173{
John W. Linville8ed66f02015-10-26 17:01:44 -0400174 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800175 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700176
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100177 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100178 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800179 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100180
John W. Linville8ed66f02015-10-26 17:01:44 -0400181 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700182
John W. Linville8ed66f02015-10-26 17:01:44 -0400183 if (gs->collect_md) {
184 vni = zero_vni;
185 addr = 0;
186 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100187 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400188 addr = iph->saddr;
189 }
190
Jiri Benc9fc47542016-02-18 11:22:50 +0100191 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400192#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100193 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800194 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100195 struct ipv6hdr *ip6h;
196 struct in6_addr addr6;
197
John W. Linville8ed66f02015-10-26 17:01:44 -0400198 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
199
200 if (gs->collect_md) {
201 vni = zero_vni;
202 addr6 = zero_addr6;
203 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100204 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400205 addr6 = ip6h->saddr;
206 }
207
Jiri Benc9fc47542016-02-18 11:22:50 +0100208 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400209#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700210 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100211 return NULL;
212}
213
214/* geneve receive/decap routine */
215static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
216 struct sk_buff *skb)
217{
218 struct genevehdr *gnvh = geneve_hdr(skb);
219 struct metadata_dst *tun_dst = NULL;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700220 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100221 int err = 0;
222 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400223
Pravin B Shelar371bd102015-08-26 23:46:54 -0700224 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700225 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700226
Yi-Hung Wei9c2e14b2020-11-10 16:16:40 -0800227 flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
Pravin B Shelare305ac62015-08-26 23:46:52 -0700228 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
229
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100230 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700231 vni_to_tunnel_id(gnvh->vni),
232 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700233 if (!tun_dst) {
234 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700235 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700236 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700237 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700238 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700239 gnvh->options, gnvh->opt_len * 4,
240 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700241 } else {
242 /* Drop packets w/ critical options,
243 * since we don't support any...
244 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700245 if (gnvh->critical) {
246 geneve->dev->stats.rx_frame_errors++;
247 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700248 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700249 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700250 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400251
252 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400253 skb->protocol = eth_type_trans(skb, geneve->dev);
254 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
255
Pravin B Shelare305ac62015-08-26 23:46:52 -0700256 if (tun_dst)
257 skb_dst_set(skb, &tun_dst->dst);
258
John W. Linville2d07dc72015-05-13 12:57:30 -0400259 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700260 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
261 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400262 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700263 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400264
Jiri Benc9fc47542016-02-18 11:22:50 +0100265 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400266 skb_reset_network_header(skb);
267
Jiri Benc9fc47542016-02-18 11:22:50 +0100268 if (geneve_get_sk_family(gs) == AF_INET)
269 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400270#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100271 else
272 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400273#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400274
275 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400276 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100277 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400278 net_info_ratelimited("non-ECT from %pI4 "
279 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100280 &((struct iphdr *)oiph)->saddr,
281 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400282#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100283 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400284 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100285 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400286#endif
287 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400288 if (err > 1) {
289 ++geneve->dev->stats.rx_frame_errors;
290 ++geneve->dev->stats.rx_errors;
291 goto drop;
292 }
293 }
294
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700295 len = skb->len;
296 err = gro_cells_receive(&geneve->gro_cells, skb);
Fabian Frederick1e845272020-10-05 22:34:58 +0200297 if (likely(err == NET_RX_SUCCESS))
298 dev_sw_netstats_rx_add(geneve->dev, len);
299
John W. Linville2d07dc72015-05-13 12:57:30 -0400300 return;
301drop:
302 /* Consume bad packet */
303 kfree_skb(skb);
304}
305
306/* Setup stats when device is created */
307static int geneve_init(struct net_device *dev)
308{
Jesse Gross8e816df2015-08-28 16:54:40 -0700309 struct geneve_dev *geneve = netdev_priv(dev);
310 int err;
311
John W. Linville2d07dc72015-05-13 12:57:30 -0400312 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
313 if (!dev->tstats)
314 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700315
316 err = gro_cells_init(&geneve->gro_cells, dev);
317 if (err) {
318 free_percpu(dev->tstats);
319 return err;
320 }
321
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200322 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100323 if (err) {
324 free_percpu(dev->tstats);
325 gro_cells_destroy(&geneve->gro_cells);
326 return err;
327 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400328 return 0;
329}
330
331static void geneve_uninit(struct net_device *dev)
332{
Jesse Gross8e816df2015-08-28 16:54:40 -0700333 struct geneve_dev *geneve = netdev_priv(dev);
334
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200335 dst_cache_destroy(&geneve->cfg.info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700336 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400337 free_percpu(dev->tstats);
338}
339
Pravin B Shelar371bd102015-08-26 23:46:54 -0700340/* Callback from net/ipv4/udp.c to receive packets */
341static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
342{
343 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100344 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700345 struct geneve_sock *gs;
346 int opts_len;
347
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700348 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700349 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200350 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700351
352 /* Return packets with reserved bits set */
353 geneveh = geneve_hdr(skb);
354 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200355 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700356
357 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200358 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700359
Jiri Benc9fc47542016-02-18 11:22:50 +0100360 gs = rcu_dereference_sk_user_data(sk);
361 if (!gs)
362 goto drop;
363
364 geneve = geneve_lookup_skb(gs, skb);
365 if (!geneve)
366 goto drop;
367
Pravin B Shelar371bd102015-08-26 23:46:54 -0700368 opts_len = geneveh->opt_len * 4;
369 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100370 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700371 !net_eq(geneve->net, dev_net(geneve->dev)))) {
372 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700373 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700374 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700375
Jiri Benc9fc47542016-02-18 11:22:50 +0100376 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700377 return 0;
378
379drop:
380 /* Consume bad packet */
381 kfree_skb(skb);
382 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700383}
384
Stefano Brivioa0796642018-11-08 12:19:18 +0100385/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
386static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
387{
388 struct genevehdr *geneveh;
389 struct geneve_sock *gs;
390 u8 zero_vni[3] = { 0 };
391 u8 *vni = zero_vni;
392
Stefano Brivioeccc73a2019-06-11 00:27:06 +0200393 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
Stefano Brivioa0796642018-11-08 12:19:18 +0100394 return -EINVAL;
395
396 geneveh = geneve_hdr(skb);
397 if (geneveh->ver != GENEVE_VER)
398 return -EINVAL;
399
400 if (geneveh->proto_type != htons(ETH_P_TEB))
401 return -EINVAL;
402
403 gs = rcu_dereference_sk_user_data(sk);
404 if (!gs)
405 return -ENOENT;
406
407 if (geneve_get_sk_family(gs) == AF_INET) {
408 struct iphdr *iph = ip_hdr(skb);
409 __be32 addr4 = 0;
410
411 if (!gs->collect_md) {
412 vni = geneve_hdr(skb)->vni;
413 addr4 = iph->daddr;
414 }
415
416 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
417 }
418
419#if IS_ENABLED(CONFIG_IPV6)
420 if (geneve_get_sk_family(gs) == AF_INET6) {
421 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Nathan Chancellor8a962c42018-11-16 18:36:27 -0700422 struct in6_addr addr6;
423
424 memset(&addr6, 0, sizeof(struct in6_addr));
Stefano Brivioa0796642018-11-08 12:19:18 +0100425
426 if (!gs->collect_md) {
427 vni = geneve_hdr(skb)->vni;
428 addr6 = ip6h->daddr;
429 }
430
431 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
432 }
433#endif
434
435 return -EPFNOSUPPORT;
436}
437
Pravin B Shelar371bd102015-08-26 23:46:54 -0700438static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800439 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700440{
441 struct socket *sock;
442 struct udp_port_cfg udp_conf;
443 int err;
444
445 memset(&udp_conf, 0, sizeof(udp_conf));
446
447 if (ipv6) {
448 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400449 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800450 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700451 } else {
452 udp_conf.family = AF_INET;
453 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
454 }
455
456 udp_conf.local_udp_port = port;
457
458 /* Open UDP socket */
459 err = udp_sock_create(net, &udp_conf, &sock);
460 if (err < 0)
461 return ERR_PTR(err);
462
463 return sock;
464}
465
Pravin B Shelar371bd102015-08-26 23:46:54 -0700466static int geneve_hlen(struct genevehdr *gh)
467{
468 return sizeof(*gh) + gh->opt_len * 4;
469}
470
David Millerd4546c22018-06-24 14:13:49 +0900471static struct sk_buff *geneve_gro_receive(struct sock *sk,
472 struct list_head *head,
473 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700474{
David Millerd4546c22018-06-24 14:13:49 +0900475 struct sk_buff *pp = NULL;
476 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700477 struct genevehdr *gh, *gh2;
478 unsigned int hlen, gh_len, off_gnv;
479 const struct packet_offload *ptype;
480 __be16 type;
481 int flush = 1;
482
483 off_gnv = skb_gro_offset(skb);
484 hlen = off_gnv + sizeof(*gh);
485 gh = skb_gro_header_fast(skb, off_gnv);
486 if (skb_gro_header_hard(skb, hlen)) {
487 gh = skb_gro_header_slow(skb, hlen, off_gnv);
488 if (unlikely(!gh))
489 goto out;
490 }
491
492 if (gh->ver != GENEVE_VER || gh->oam)
493 goto out;
494 gh_len = geneve_hlen(gh);
495
496 hlen = off_gnv + gh_len;
497 if (skb_gro_header_hard(skb, hlen)) {
498 gh = skb_gro_header_slow(skb, hlen, off_gnv);
499 if (unlikely(!gh))
500 goto out;
501 }
502
David Millerd4546c22018-06-24 14:13:49 +0900503 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700504 if (!NAPI_GRO_CB(p)->same_flow)
505 continue;
506
507 gh2 = (struct genevehdr *)(p->data + off_gnv);
508 if (gh->opt_len != gh2->opt_len ||
509 memcmp(gh, gh2, gh_len)) {
510 NAPI_GRO_CB(p)->same_flow = 0;
511 continue;
512 }
513 }
514
515 type = gh->proto_type;
516
517 rcu_read_lock();
518 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800519 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700520 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700521
522 skb_gro_pull(skb, gh_len);
523 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200524 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800525 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700526
527out_unlock:
528 rcu_read_unlock();
529out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200530 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700531
532 return pp;
533}
534
Tom Herbert4a0090a2016-04-05 08:22:55 -0700535static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
536 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700537{
538 struct genevehdr *gh;
539 struct packet_offload *ptype;
540 __be16 type;
541 int gh_len;
542 int err = -ENOSYS;
543
Pravin B Shelar371bd102015-08-26 23:46:54 -0700544 gh = (struct genevehdr *)(skb->data + nhoff);
545 gh_len = geneve_hlen(gh);
546 type = gh->proto_type;
547
548 rcu_read_lock();
549 ptype = gro_find_complete_by_type(type);
550 if (ptype)
551 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
552
553 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700554
555 skb_set_inner_mac_header(skb, nhoff + gh_len);
556
Pravin B Shelar371bd102015-08-26 23:46:54 -0700557 return err;
558}
559
560/* Create new listen socket if needed */
561static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800562 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700563{
564 struct geneve_net *gn = net_generic(net, geneve_net_id);
565 struct geneve_sock *gs;
566 struct socket *sock;
567 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700568 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700569
570 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
571 if (!gs)
572 return ERR_PTR(-ENOMEM);
573
pravin shelar9b4437a2016-11-21 11:02:58 -0800574 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700575 if (IS_ERR(sock)) {
576 kfree(gs);
577 return ERR_CAST(sock);
578 }
579
580 gs->sock = sock;
581 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700582 for (h = 0; h < VNI_HASH_SIZE; ++h)
583 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584
585 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700586 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700587
588 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700589 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590 tunnel_cfg.sk_user_data = gs;
591 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700592 tunnel_cfg.gro_receive = geneve_gro_receive;
593 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700594 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100595 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700596 tunnel_cfg.encap_destroy = NULL;
597 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700598 list_add(&gs->list, &gn->sock_list);
599 return gs;
600}
601
John W. Linville8ed66f02015-10-26 17:01:44 -0400602static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700603{
John W. Linville8ed66f02015-10-26 17:01:44 -0400604 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700605 return;
606
607 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700608 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700609 udp_tunnel_sock_release(gs->sock);
610 kfree_rcu(gs, rcu);
611}
612
John W. Linville8ed66f02015-10-26 17:01:44 -0400613static void geneve_sock_release(struct geneve_dev *geneve)
614{
pravin shelarfceb9c32016-10-28 09:59:16 -0700615 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400616#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700617 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
618
619 rcu_assign_pointer(geneve->sock6, NULL);
620#endif
621
622 rcu_assign_pointer(geneve->sock4, NULL);
623 synchronize_net();
624
625 __geneve_sock_release(gs4);
626#if IS_ENABLED(CONFIG_IPV6)
627 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400628#endif
629}
630
Pravin B Shelar371bd102015-08-26 23:46:54 -0700631static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400632 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700633 __be16 dst_port)
634{
635 struct geneve_sock *gs;
636
637 list_for_each_entry(gs, &gn->sock_list, list) {
638 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100639 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700640 return gs;
641 }
642 }
643 return NULL;
644}
645
John W. Linville8ed66f02015-10-26 17:01:44 -0400646static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400647{
John W. Linville2d07dc72015-05-13 12:57:30 -0400648 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700649 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200650 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400651 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800652 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700653 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400654
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200655 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700656 if (gs) {
657 gs->refcnt++;
658 goto out;
659 }
660
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200661 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
662 geneve->cfg.use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400663 if (IS_ERR(gs))
664 return PTR_ERR(gs);
665
Pravin B Shelar371bd102015-08-26 23:46:54 -0700666out:
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200667 gs->collect_md = geneve->cfg.collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400668#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200669 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700670 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200671 node = &geneve->hlist6;
672 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400673#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200674 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700675 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200676 node = &geneve->hlist4;
677 }
678 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700679
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200680 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
pravin shelar9b4437a2016-11-21 11:02:58 -0800681 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200682 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400683 return 0;
684}
685
John W. Linville8ed66f02015-10-26 17:01:44 -0400686static int geneve_open(struct net_device *dev)
687{
688 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200689 bool metadata = geneve->cfg.collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100690 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400691 int ret = 0;
692
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200693 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100694 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400695#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100696 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400697 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100698 if (ret < 0 && ret != -EAFNOSUPPORT)
699 ipv4 = false;
700 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400701#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100702 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400703 ret = geneve_sock_add(geneve, false);
704 if (ret < 0)
705 geneve_sock_release(geneve);
706
707 return ret;
708}
709
John W. Linville2d07dc72015-05-13 12:57:30 -0400710static int geneve_stop(struct net_device *dev)
711{
712 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400713
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200714 hlist_del_init_rcu(&geneve->hlist4.hlist);
715#if IS_ENABLED(CONFIG_IPV6)
716 hlist_del_init_rcu(&geneve->hlist6.hlist);
717#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400718 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400719 return 0;
720}
721
John W. Linville8ed66f02015-10-26 17:01:44 -0400722static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800723 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400724{
725 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800726 geneveh->opt_len = info->options_len / 4;
727 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
728 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400729 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800730 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400731 geneveh->proto_type = htons(ETH_P_TEB);
732 geneveh->rsvd2 = 0;
733
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700734 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
735 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400736}
737
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800738static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
739 const struct ip_tunnel_info *info,
740 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700741{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800742 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700743 struct genevehdr *gnvh;
744 int min_headroom;
745 int err;
746
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800747 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400748 skb_scrub_packet(skb, xnet);
749
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800750 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
751 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400752 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400753 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400754 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400755
Alexander Duyckaed069d2016-04-14 15:33:37 -0400756 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300757 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400758 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400759
Johannes Bergd58ff352017-06-16 14:29:23 +0200760 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800761 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400762 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
763 return 0;
764
765free_dst:
766 dst_release(dst);
767 return err;
768}
John W. Linville8ed66f02015-10-26 17:01:44 -0400769
770static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
771 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700772 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400773 struct flowi4 *fl4,
Mark Gray34beb212020-09-16 05:19:35 -0400774 const struct ip_tunnel_info *info,
775 __be16 dport, __be16 sport)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700776{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100777 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700778 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100779 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700780 struct rtable *rt = NULL;
781 __u8 tos;
782
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700783 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700784 return ERR_PTR(-EIO);
785
Pravin B Shelare305ac62015-08-26 23:46:52 -0700786 memset(fl4, 0, sizeof(*fl4));
787 fl4->flowi4_mark = skb->mark;
788 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800789 fl4->daddr = info->key.u.ipv4.dst;
790 fl4->saddr = info->key.u.ipv4.src;
Mark Gray34beb212020-09-16 05:19:35 -0400791 fl4->fl4_dport = dport;
792 fl4->fl4_sport = sport;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700793
pravin shelar9b4437a2016-11-21 11:02:58 -0800794 tos = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200795 if ((tos == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800796 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
797 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100798 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800799 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100800
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800801 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100802 if (use_cache) {
803 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
804 if (rt)
805 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700806 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700807 rt = ip_route_output_key(geneve->net, fl4);
808 if (IS_ERR(rt)) {
809 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700810 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700811 }
812 if (rt->dst.dev == dev) { /* is this necessary? */
813 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700814 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700815 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700816 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100817 if (use_cache)
818 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700819 return rt;
820}
821
John W. Linville8ed66f02015-10-26 17:01:44 -0400822#if IS_ENABLED(CONFIG_IPV6)
823static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
824 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700825 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400826 struct flowi6 *fl6,
Mark Gray34beb212020-09-16 05:19:35 -0400827 const struct ip_tunnel_info *info,
828 __be16 dport, __be16 sport)
John W. Linville8ed66f02015-10-26 17:01:44 -0400829{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100830 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400831 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400832 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100833 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400834 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400835
pravin shelarfceb9c32016-10-28 09:59:16 -0700836 if (!gs6)
837 return ERR_PTR(-EIO);
838
John W. Linville8ed66f02015-10-26 17:01:44 -0400839 memset(fl6, 0, sizeof(*fl6));
840 fl6->flowi6_mark = skb->mark;
841 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800842 fl6->daddr = info->key.u.ipv6.dst;
843 fl6->saddr = info->key.u.ipv6.src;
Mark Gray34beb212020-09-16 05:19:35 -0400844 fl6->fl6_dport = dport;
845 fl6->fl6_sport = sport;
846
pravin shelar9b4437a2016-11-21 11:02:58 -0800847 prio = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200848 if ((prio == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800849 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
850 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100851 }
852
pravin shelar9b4437a2016-11-21 11:02:58 -0800853 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
854 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800855 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100856 if (use_cache) {
857 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
858 if (dst)
859 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400860 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100861 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
862 NULL);
863 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400864 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
865 return ERR_PTR(-ENETUNREACH);
866 }
867 if (dst->dev == dev) { /* is this necessary? */
868 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
869 dst_release(dst);
870 return ERR_PTR(-ELOOP);
871 }
872
Paolo Abeni468dfff2016-02-12 15:43:58 +0100873 if (use_cache)
874 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 return dst;
876}
877#endif
878
pravin shelar9b4437a2016-11-21 11:02:58 -0800879static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800880 struct geneve_dev *geneve,
881 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700882{
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
884 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
885 const struct ip_tunnel_key *key = &info->key;
886 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400887 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400888 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100889 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700890 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800891 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400892
Mark Gray34beb212020-09-16 05:19:35 -0400893 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
894 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
895 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 if (IS_ERR(rt))
897 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700898
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200899 err = skb_tunnel_check_pmtu(skb, &rt->dst,
900 GENEVE_IPV4_HLEN + info->options_len,
901 netif_is_any_bridge_port(dev));
902 if (err < 0) {
903 dst_release(&rt->dst);
904 return err;
905 } else if (err) {
906 struct ip_tunnel_info *info;
907
908 info = skb_tunnel_info(skb);
909 if (info) {
910 info->key.u.ipv4.dst = fl4.saddr;
911 info->key.u.ipv4.src = fl4.daddr;
912 }
913
914 if (!pskb_may_pull(skb, ETH_HLEN)) {
915 dst_release(&rt->dst);
916 return -EINVAL;
917 }
918
919 skb->protocol = eth_type_trans(skb, geneve->dev);
920 netif_rx(skb);
921 dst_release(&rt->dst);
922 return -EMSGSIZE;
923 }
Xin Long52a589d2017-12-25 14:43:58 +0800924
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200925 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800926 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700927 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100928
929 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700930 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800931 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200932 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800933 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
934 else
935 ttl = key->ttl;
936 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100937
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200938 if (geneve->cfg.df == GENEVE_DF_SET) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100939 df = htons(IP_DF);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200940 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100941 struct ethhdr *eth = eth_hdr(skb);
942
943 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
944 df = htons(IP_DF);
945 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
946 struct iphdr *iph = ip_hdr(skb);
947
948 if (iph->frag_off & htons(IP_DF))
949 df = htons(IP_DF);
950 }
951 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400952 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800953
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800954 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800955 if (unlikely(err))
956 return err;
957
Pravin B Shelar039f5062015-12-24 14:34:54 -0800958 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200959 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800960 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800961 !(info->key.tun_flags & TUNNEL_CSUM));
962 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400963}
964
John W. Linville8ed66f02015-10-26 17:01:44 -0400965#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800966static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800967 struct geneve_dev *geneve,
968 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400969{
pravin shelar9b4437a2016-11-21 11:02:58 -0800970 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
971 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
972 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400973 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400974 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400975 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400976 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800977 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400978
Mark Gray34beb212020-09-16 05:19:35 -0400979 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
980 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
981 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800982 if (IS_ERR(dst))
983 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400984
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200985 err = skb_tunnel_check_pmtu(skb, dst,
986 GENEVE_IPV6_HLEN + info->options_len,
987 netif_is_any_bridge_port(dev));
988 if (err < 0) {
989 dst_release(dst);
990 return err;
991 } else if (err) {
992 struct ip_tunnel_info *info = skb_tunnel_info(skb);
993
994 if (info) {
995 info->key.u.ipv6.dst = fl6.saddr;
996 info->key.u.ipv6.src = fl6.daddr;
997 }
998
999 if (!pskb_may_pull(skb, ETH_HLEN)) {
1000 dst_release(dst);
1001 return -EINVAL;
1002 }
1003
1004 skb->protocol = eth_type_trans(skb, geneve->dev);
1005 netif_rx(skb);
1006 dst_release(dst);
1007 return -EMSGSIZE;
1008 }
Xin Long52a589d2017-12-25 14:43:58 +08001009
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001010 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001011 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001012 ttl = key->ttl;
1013 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +01001014 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -08001015 ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001016 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001017 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1018 else
1019 ttl = key->ttl;
1020 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -04001021 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +08001022 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -08001023 if (unlikely(err))
1024 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001025
Pravin B Shelar039f5062015-12-24 14:34:54 -08001026 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001027 &fl6.saddr, &fl6.daddr, prio, ttl,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001028 info->key.label, sport, geneve->cfg.info.key.tp_dst,
pravin shelar9b4437a2016-11-21 11:02:58 -08001029 !(info->key.tun_flags & TUNNEL_CSUM));
1030 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -04001031}
1032#endif
1033
1034static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1035{
1036 struct geneve_dev *geneve = netdev_priv(dev);
1037 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -08001038 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001039
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001040 if (geneve->cfg.collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001041 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001042 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001043 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +02001044 dev_kfree_skb(skb);
1045 dev->stats.tx_dropped++;
1046 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -08001047 }
1048 } else {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001049 info = &geneve->cfg.info;
pravin shelar9b4437a2016-11-21 11:02:58 -08001050 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001051
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001052 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001053#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001054 if (info->mode & IP_TUNNEL_INFO_IPV6)
1055 err = geneve6_xmit_skb(skb, dev, geneve, info);
1056 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001057#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001058 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001059 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001060
1061 if (likely(!err))
1062 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001063
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001064 if (err != -EMSGSIZE)
1065 dev_kfree_skb(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001066
1067 if (err == -ELOOP)
1068 dev->stats.collisions++;
1069 else if (err == -ENETUNREACH)
1070 dev->stats.tx_carrier_errors++;
1071
1072 dev->stats.tx_errors++;
1073 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -04001074}
1075
Jarod Wilson91572082016-10-20 13:55:20 -04001076static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001077{
Jarod Wilson91572082016-10-20 13:55:20 -04001078 if (new_mtu > dev->max_mtu)
1079 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001080 else if (new_mtu < dev->min_mtu)
1081 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001082
David Wragg55e5bfb2016-02-10 00:05:57 +00001083 dev->mtu = new_mtu;
1084 return 0;
1085}
1086
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001087static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1088{
1089 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1090 struct geneve_dev *geneve = netdev_priv(dev);
Mark Gray34beb212020-09-16 05:19:35 -04001091 __be16 sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001092
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001093 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001094 struct rtable *rt;
1095 struct flowi4 fl4;
1096
Mark Gray34beb212020-09-16 05:19:35 -04001097 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1098 sport = udp_flow_src_port(geneve->net, skb,
1099 1, USHRT_MAX, true);
1100
1101 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1102 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001103 if (IS_ERR(rt))
1104 return PTR_ERR(rt);
1105
1106 ip_rt_put(rt);
1107 info->key.u.ipv4.src = fl4.saddr;
1108#if IS_ENABLED(CONFIG_IPV6)
1109 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001110 struct dst_entry *dst;
1111 struct flowi6 fl6;
1112
Mark Gray34beb212020-09-16 05:19:35 -04001113 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1114 sport = udp_flow_src_port(geneve->net, skb,
1115 1, USHRT_MAX, true);
1116
1117 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1118 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001119 if (IS_ERR(dst))
1120 return PTR_ERR(dst);
1121
1122 dst_release(dst);
1123 info->key.u.ipv6.src = fl6.saddr;
1124#endif
1125 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001126 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001127 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001128
Mark Gray34beb212020-09-16 05:19:35 -04001129 info->key.tp_src = sport;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001130 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001131 return 0;
1132}
1133
John W. Linville2d07dc72015-05-13 12:57:30 -04001134static const struct net_device_ops geneve_netdev_ops = {
1135 .ndo_init = geneve_init,
1136 .ndo_uninit = geneve_uninit,
1137 .ndo_open = geneve_open,
1138 .ndo_stop = geneve_stop,
1139 .ndo_start_xmit = geneve_xmit,
1140 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001141 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001142 .ndo_validate_addr = eth_validate_addr,
1143 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001144 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001145};
1146
1147static void geneve_get_drvinfo(struct net_device *dev,
1148 struct ethtool_drvinfo *drvinfo)
1149{
1150 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1151 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1152}
1153
1154static const struct ethtool_ops geneve_ethtool_ops = {
1155 .get_drvinfo = geneve_get_drvinfo,
1156 .get_link = ethtool_op_get_link,
1157};
1158
1159/* Info for udev, that this is a virtual tunnel endpoint */
1160static struct device_type geneve_type = {
1161 .name = "geneve",
1162};
1163
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001164/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001165 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001166 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001167 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001168static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001169{
1170 struct net *net = dev_net(dev);
1171 struct geneve_net *gn = net_generic(net, geneve_net_id);
1172 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001173
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001174 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001175 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1176 if (push) {
1177 udp_tunnel_push_rx_port(dev, gs->sock,
1178 UDP_TUNNEL_TYPE_GENEVE);
1179 } else {
1180 udp_tunnel_drop_rx_port(dev, gs->sock,
1181 UDP_TUNNEL_TYPE_GENEVE);
1182 }
1183 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001184 rcu_read_unlock();
1185}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001186
John W. Linville2d07dc72015-05-13 12:57:30 -04001187/* Initialize the device structure. */
1188static void geneve_setup(struct net_device *dev)
1189{
1190 ether_setup(dev);
1191
1192 dev->netdev_ops = &geneve_netdev_ops;
1193 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001194 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001195
1196 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1197
John W. Linville2d07dc72015-05-13 12:57:30 -04001198 dev->features |= NETIF_F_LLTX;
1199 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1200 dev->features |= NETIF_F_RXCSUM;
1201 dev->features |= NETIF_F_GSO_SOFTWARE;
1202
John W. Linville2d07dc72015-05-13 12:57:30 -04001203 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1204 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001205
Jarod Wilson91572082016-10-20 13:55:20 -04001206 /* MTU range: 68 - (something less than 65535) */
1207 dev->min_mtu = ETH_MIN_MTU;
1208 /* The max_mtu calculation does not take account of GENEVE
1209 * options, to avoid excluding potentially valid
1210 * configurations. This will be further reduced by IPvX hdr size.
1211 */
1212 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1213
John W. Linville2d07dc72015-05-13 12:57:30 -04001214 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001215 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001216 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001217 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001218}
1219
1220static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1221 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001222 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001223 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001224 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001225 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001226 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001227 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001228 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001229 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1230 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1231 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001232 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001233 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001234};
1235
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001236static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1237 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001238{
1239 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001240 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1241 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1242 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001243 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001244 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001245
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001246 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1247 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1248 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001249 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001250 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001251 }
1252
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001253 if (!data) {
1254 NL_SET_ERR_MSG(extack,
1255 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001256 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001257 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001258
1259 if (data[IFLA_GENEVE_ID]) {
1260 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1261
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001262 if (vni >= GENEVE_N_VID) {
1263 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1264 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001265 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001266 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001267 }
1268
Stefano Brivioa025fb52018-11-08 12:19:19 +01001269 if (data[IFLA_GENEVE_DF]) {
1270 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1271
1272 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001273 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001274 "Invalid DF attribute");
1275 return -EINVAL;
1276 }
1277 }
1278
John W. Linville2d07dc72015-05-13 12:57:30 -04001279 return 0;
1280}
1281
Pravin B Shelar371bd102015-08-26 23:46:54 -07001282static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001283 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001284 bool *tun_on_same_port,
1285 bool *tun_collect_md)
1286{
pravin shelar9b4437a2016-11-21 11:02:58 -08001287 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001288
1289 *tun_on_same_port = false;
1290 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001291 list_for_each_entry(geneve, &gn->geneve_list, next) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001292 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1293 *tun_collect_md = geneve->cfg.collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001294 *tun_on_same_port = true;
1295 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001296 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1297 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1298 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001299 t = geneve;
1300 }
1301 return t;
1302}
1303
pravin shelar9b4437a2016-11-21 11:02:58 -08001304static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1305{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001306 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1307 info->key.ttl || info->key.label || info->key.tp_src ||
1308 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001309}
1310
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001311static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1312 struct ip_tunnel_info *b)
1313{
1314 if (ip_tunnel_info_af(a) == AF_INET)
1315 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1316 else
1317 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1318}
1319
Pravin B Shelare305ac62015-08-26 23:46:52 -07001320static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001321 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001322 const struct geneve_config *cfg)
John W. Linville2d07dc72015-05-13 12:57:30 -04001323{
1324 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001325 struct geneve_dev *t, *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001326 const struct ip_tunnel_info *info = &cfg->info;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001327 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001328 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001329
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001330 if (cfg->collect_md && !is_tnl_info_zero(info)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001331 NL_SET_ERR_MSG(extack,
1332 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001333 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001334 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001335
1336 geneve->net = net;
1337 geneve->dev = dev;
1338
pravin shelar9b4437a2016-11-21 11:02:58 -08001339 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001340 if (t)
1341 return -EBUSY;
1342
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001343 /* make enough headroom for basic scenario */
1344 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001345 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001346 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001347 dev->max_mtu -= sizeof(struct iphdr);
1348 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001349 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001350 dev->max_mtu -= sizeof(struct ipv6hdr);
1351 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001352 dev->needed_headroom = encap_len + ETH_HLEN;
1353
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001354 if (cfg->collect_md) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001355 if (tun_on_same_port) {
1356 NL_SET_ERR_MSG(extack,
1357 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001358 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001359 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001360 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001361 if (tun_collect_md) {
1362 NL_SET_ERR_MSG(extack,
1363 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001364 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001365 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001366 }
1367
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001368 dst_cache_reset(&geneve->cfg.info.dst_cache);
1369 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
Paolo Abeni468dfff2016-02-12 15:43:58 +01001370
John W. Linville2d07dc72015-05-13 12:57:30 -04001371 err = register_netdevice(dev);
1372 if (err)
1373 return err;
1374
1375 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001376 return 0;
1377}
1378
pravin shelar9b4437a2016-11-21 11:02:58 -08001379static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1380{
1381 memset(info, 0, sizeof(*info));
1382 info->key.tp_dst = htons(dst_port);
1383}
1384
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001385static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1386 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001387 struct geneve_config *cfg, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001388{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001389 struct ip_tunnel_info *info = &cfg->info;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001390 int attrtype;
1391
1392 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1393 NL_SET_ERR_MSG(extack,
1394 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001395 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001396 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001397
1398 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001399 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1400 attrtype = IFLA_GENEVE_REMOTE;
1401 goto change_notsup;
1402 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001403
1404 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001405 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001406
Dave Taht842841e2019-09-02 16:29:36 -07001407 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001408 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1409 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001410 return -EINVAL;
1411 }
1412 }
1413
pravin shelar9b4437a2016-11-21 11:02:58 -08001414 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001415#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001416 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1417 attrtype = IFLA_GENEVE_REMOTE6;
1418 goto change_notsup;
1419 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001420
1421 info->mode = IP_TUNNEL_INFO_IPV6;
1422 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001423 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001424
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001425 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001426 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001427 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1428 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001429 return -EINVAL;
1430 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001431 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001432 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1433 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001434 return -EINVAL;
1435 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001436 info->key.tun_flags |= TUNNEL_CSUM;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001437 cfg->use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001438#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001439 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1440 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001441 return -EPFNOSUPPORT;
1442#endif
1443 }
1444
1445 if (data[IFLA_GENEVE_ID]) {
1446 __u32 vni;
1447 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001448 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001449
1450 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1451 tvni[0] = (vni & 0x00ff0000) >> 16;
1452 tvni[1] = (vni & 0x0000ff00) >> 8;
1453 tvni[2] = vni & 0x000000ff;
1454
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001455 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001456 if (changelink && (tunid != info->key.tun_id)) {
1457 attrtype = IFLA_GENEVE_ID;
1458 goto change_notsup;
1459 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001460 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001461 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001462
Hangbin Liua97d97b2018-09-29 23:06:29 +08001463 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1464 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001465 cfg->ttl_inherit = true;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001466 else
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001467 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001468 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001469 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001470 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001471 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001472
Pravin B Shelare305ac62015-08-26 23:46:52 -07001473 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001474 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001475
Stefano Brivioa025fb52018-11-08 12:19:19 +01001476 if (data[IFLA_GENEVE_DF])
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001477 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001478
pravin shelar9b4437a2016-11-21 11:02:58 -08001479 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001480 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001481 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001482 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1483 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1484 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001485 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001486 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001487 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001488
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001489 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001490 if (changelink) {
1491 attrtype = IFLA_GENEVE_PORT;
1492 goto change_notsup;
1493 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001494 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1495 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001496
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001497 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001498 if (changelink) {
1499 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1500 goto change_notsup;
1501 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001502 cfg->collect_md = true;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001503 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001504
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001505 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001506 if (changelink) {
1507 attrtype = IFLA_GENEVE_UDP_CSUM;
1508 goto change_notsup;
1509 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001510 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1511 info->key.tun_flags |= TUNNEL_CSUM;
1512 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001513
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001514 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001515#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001516 if (changelink) {
1517 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1518 goto change_notsup;
1519 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001520 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1521 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001522#else
1523 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1524 "IPv6 support not enabled in the kernel");
1525 return -EPFNOSUPPORT;
1526#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001527 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001528
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001529 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001530#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001531 if (changelink) {
1532 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1533 goto change_notsup;
1534 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001535 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001536 cfg->use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001537#else
1538 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1539 "IPv6 support not enabled in the kernel");
1540 return -EPFNOSUPPORT;
1541#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001542 }
1543
1544 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001545change_notsup:
1546 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1547 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1548 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001549}
1550
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001551static void geneve_link_config(struct net_device *dev,
1552 struct ip_tunnel_info *info, struct nlattr *tb[])
1553{
1554 struct geneve_dev *geneve = netdev_priv(dev);
1555 int ldev_mtu = 0;
1556
1557 if (tb[IFLA_MTU]) {
1558 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1559 return;
1560 }
1561
1562 switch (ip_tunnel_info_af(info)) {
1563 case AF_INET: {
1564 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1565 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1566
1567 if (!IS_ERR(rt) && rt->dst.dev) {
1568 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1569 ip_rt_put(rt);
1570 }
1571 break;
1572 }
1573#if IS_ENABLED(CONFIG_IPV6)
1574 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001575 struct rt6_info *rt;
1576
1577 if (!__in6_dev_get(dev))
1578 break;
1579
1580 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1581 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001582
1583 if (rt && rt->dst.dev)
1584 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1585 ip6_rt_put(rt);
1586 break;
1587 }
1588#endif
1589 }
1590
1591 if (ldev_mtu <= 0)
1592 return;
1593
1594 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1595}
1596
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001597static int geneve_newlink(struct net *net, struct net_device *dev,
1598 struct nlattr *tb[], struct nlattr *data[],
1599 struct netlink_ext_ack *extack)
1600{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001601 struct geneve_config cfg = {
1602 .df = GENEVE_DF_UNSET,
1603 .use_udp6_rx_checksums = false,
1604 .ttl_inherit = false,
1605 .collect_md = false,
1606 };
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001607 int err;
1608
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001609 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1610 err = geneve_nl2info(tb, data, extack, &cfg, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001611 if (err)
1612 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001613
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001614 err = geneve_configure(net, dev, extack, &cfg);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001615 if (err)
1616 return err;
1617
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001618 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001619
1620 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001621}
1622
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001623/* Quiesces the geneve device data path for both TX and RX.
1624 *
1625 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1626 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1627 * to complete for the existing set of in-flight packets to be transmitted,
1628 * then we would have quiesced the transmit data path. All the future packets
1629 * will get dropped until we unquiesce the data path.
1630 *
1631 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1632 * if we set that to NULL under RCU and wait for synchronize_net() to
1633 * complete, then we would have quiesced the receive data path.
1634 */
1635static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1636 struct geneve_sock **gs6)
1637{
1638 *gs4 = rtnl_dereference(geneve->sock4);
1639 rcu_assign_pointer(geneve->sock4, NULL);
1640 if (*gs4)
1641 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1642#if IS_ENABLED(CONFIG_IPV6)
1643 *gs6 = rtnl_dereference(geneve->sock6);
1644 rcu_assign_pointer(geneve->sock6, NULL);
1645 if (*gs6)
1646 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1647#else
1648 *gs6 = NULL;
1649#endif
1650 synchronize_net();
1651}
1652
1653/* Resumes the geneve device data path for both TX and RX. */
1654static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1655 struct geneve_sock __maybe_unused *gs6)
1656{
1657 rcu_assign_pointer(geneve->sock4, gs4);
1658 if (gs4)
1659 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1660#if IS_ENABLED(CONFIG_IPV6)
1661 rcu_assign_pointer(geneve->sock6, gs6);
1662 if (gs6)
1663 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1664#endif
1665 synchronize_net();
1666}
1667
1668static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1669 struct nlattr *data[],
1670 struct netlink_ext_ack *extack)
1671{
1672 struct geneve_dev *geneve = netdev_priv(dev);
1673 struct geneve_sock *gs4, *gs6;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001674 struct geneve_config cfg;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001675 int err;
1676
1677 /* If the geneve device is configured for metadata (or externally
1678 * controlled, for example, OVS), then nothing can be changed.
1679 */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001680 if (geneve->cfg.collect_md)
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001681 return -EOPNOTSUPP;
1682
1683 /* Start with the existing info. */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001684 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1685 err = geneve_nl2info(tb, data, extack, &cfg, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001686 if (err)
1687 return err;
1688
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001689 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1690 dst_cache_reset(&cfg.info.dst_cache);
1691 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001692 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001693
1694 geneve_quiesce(geneve, &gs4, &gs6);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001695 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001696 geneve_unquiesce(geneve, gs4, gs6);
1697
1698 return 0;
1699}
1700
John W. Linville2d07dc72015-05-13 12:57:30 -04001701static void geneve_dellink(struct net_device *dev, struct list_head *head)
1702{
1703 struct geneve_dev *geneve = netdev_priv(dev);
1704
John W. Linville2d07dc72015-05-13 12:57:30 -04001705 list_del(&geneve->next);
1706 unregister_netdevice_queue(dev, head);
1707}
1708
1709static size_t geneve_get_size(const struct net_device *dev)
1710{
1711 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001712 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001713 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001714 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001715 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001716 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001717 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001718 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001719 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1720 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1721 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001722 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001723 0;
1724}
1725
1726static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1727{
1728 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001729 struct ip_tunnel_info *info = &geneve->cfg.info;
1730 bool ttl_inherit = geneve->cfg.ttl_inherit;
1731 bool metadata = geneve->cfg.collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001732 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001733 __u32 vni;
1734
pravin shelar9b4437a2016-11-21 11:02:58 -08001735 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1736 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001737 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1738 goto nla_put_failure;
1739
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001740 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001741 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001742 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001743 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001744 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1745 !!(info->key.tun_flags & TUNNEL_CSUM)))
1746 goto nla_put_failure;
1747
John W. Linville8ed66f02015-10-26 17:01:44 -04001748#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001749 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001750 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001751 &info->key.u.ipv6.dst))
1752 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001753 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1754 !(info->key.tun_flags & TUNNEL_CSUM)))
1755 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001756#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001757 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001758
pravin shelar9b4437a2016-11-21 11:02:58 -08001759 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1760 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1761 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001762 goto nla_put_failure;
1763
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001764 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
Stefano Brivioa025fb52018-11-08 12:19:19 +01001765 goto nla_put_failure;
1766
pravin shelar9b4437a2016-11-21 11:02:58 -08001767 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001768 goto nla_put_failure;
1769
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001770 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001771 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001772
Hangbin Liuf9094b72017-11-23 11:27:24 +08001773#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001774 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001775 !geneve->cfg.use_udp6_rx_checksums))
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001776 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001777#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001778
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001779 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1780 goto nla_put_failure;
1781
John W. Linville2d07dc72015-05-13 12:57:30 -04001782 return 0;
1783
1784nla_put_failure:
1785 return -EMSGSIZE;
1786}
1787
1788static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1789 .kind = "geneve",
1790 .maxtype = IFLA_GENEVE_MAX,
1791 .policy = geneve_policy,
1792 .priv_size = sizeof(struct geneve_dev),
1793 .setup = geneve_setup,
1794 .validate = geneve_validate,
1795 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001796 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001797 .dellink = geneve_dellink,
1798 .get_size = geneve_get_size,
1799 .fill_info = geneve_fill_info,
1800};
1801
Pravin B Shelare305ac62015-08-26 23:46:52 -07001802struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1803 u8 name_assign_type, u16 dst_port)
1804{
1805 struct nlattr *tb[IFLA_MAX + 1];
1806 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001807 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001808 int err;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001809 struct geneve_config cfg = {
1810 .df = GENEVE_DF_UNSET,
1811 .use_udp6_rx_checksums = true,
1812 .ttl_inherit = false,
1813 .collect_md = true,
1814 };
Pravin B Shelare305ac62015-08-26 23:46:52 -07001815
1816 memset(tb, 0, sizeof(tb));
1817 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001818 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001819 if (IS_ERR(dev))
1820 return dev;
1821
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001822 init_tnl_info(&cfg.info, dst_port);
1823 err = geneve_configure(net, dev, NULL, &cfg);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001824 if (err) {
1825 free_netdev(dev);
1826 return ERR_PTR(err);
1827 }
David Wragg7e059152016-02-10 00:05:58 +00001828
1829 /* openvswitch users expect packet sizes to be unrestricted,
1830 * so set the largest MTU we can.
1831 */
Jarod Wilson91572082016-10-20 13:55:20 -04001832 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001833 if (err)
1834 goto err;
1835
Nicolas Dichtel41009482016-06-13 10:31:07 +02001836 err = rtnl_configure_link(dev, NULL);
1837 if (err < 0)
1838 goto err;
1839
Pravin B Shelare305ac62015-08-26 23:46:52 -07001840 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001841err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001842 geneve_dellink(dev, &list_kill);
1843 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001844 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001845}
1846EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1847
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001848static int geneve_netdevice_event(struct notifier_block *unused,
1849 unsigned long event, void *ptr)
1850{
1851 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1852
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001853 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001854 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001855 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001856 } else if (event == NETDEV_UNREGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07001857 if (!dev->udp_tunnel_nic_info)
1858 geneve_offload_rx_ports(dev, false);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001859 } else if (event == NETDEV_REGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07001860 if (!dev->udp_tunnel_nic_info)
1861 geneve_offload_rx_ports(dev, true);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001862 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001863
1864 return NOTIFY_DONE;
1865}
1866
1867static struct notifier_block geneve_notifier_block __read_mostly = {
1868 .notifier_call = geneve_netdevice_event,
1869};
1870
John W. Linville2d07dc72015-05-13 12:57:30 -04001871static __net_init int geneve_init_net(struct net *net)
1872{
1873 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001874
1875 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001876 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001877 return 0;
1878}
1879
Haishuang Yan2843a252017-12-16 17:54:50 +08001880static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001881{
1882 struct geneve_net *gn = net_generic(net, geneve_net_id);
1883 struct geneve_dev *geneve, *next;
1884 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001885
1886 /* gather any geneve devices that were moved into this ns */
1887 for_each_netdev_safe(net, dev, aux)
1888 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001889 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001890
1891 /* now gather any other geneve devices that were created in this ns */
1892 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1893 /* If geneve->dev is in the same netns, it was already added
1894 * to the list by the previous loop.
1895 */
1896 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001897 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001898 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001899}
1900
1901static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1902{
1903 struct net *net;
1904 LIST_HEAD(list);
1905
1906 rtnl_lock();
1907 list_for_each_entry(net, net_list, exit_list)
1908 geneve_destroy_tunnels(net, &list);
1909
John W. Linville2d07dc72015-05-13 12:57:30 -04001910 /* unregister the devices gathered above */
1911 unregister_netdevice_many(&list);
1912 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001913
1914 list_for_each_entry(net, net_list, exit_list) {
1915 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1916
1917 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1918 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001919}
1920
1921static struct pernet_operations geneve_net_ops = {
1922 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001923 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001924 .id = &geneve_net_id,
1925 .size = sizeof(struct geneve_net),
1926};
1927
1928static int __init geneve_init_module(void)
1929{
1930 int rc;
1931
1932 rc = register_pernet_subsys(&geneve_net_ops);
1933 if (rc)
1934 goto out1;
1935
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001936 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001937 if (rc)
1938 goto out2;
1939
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001940 rc = rtnl_link_register(&geneve_link_ops);
1941 if (rc)
1942 goto out3;
1943
John W. Linville2d07dc72015-05-13 12:57:30 -04001944 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001945out3:
1946 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001947out2:
1948 unregister_pernet_subsys(&geneve_net_ops);
1949out1:
1950 return rc;
1951}
1952late_initcall(geneve_init_module);
1953
1954static void __exit geneve_cleanup_module(void)
1955{
1956 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001957 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001958 unregister_pernet_subsys(&geneve_net_ops);
1959}
1960module_exit(geneve_cleanup_module);
1961
1962MODULE_LICENSE("GPL");
1963MODULE_VERSION(GENEVE_NETDEV_VER);
1964MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1965MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1966MODULE_ALIAS_RTNL_LINK("geneve");