blob: 75266580b586d129c02fd7732e544f0465ee3d22 [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
John W. Linville2d07dc72015-05-13 12:57:30 -040051/* Pseudo network device */
52struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020053 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
54#if IS_ENABLED(CONFIG_IPV6)
55 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
56#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040057 struct net *net; /* netns for packet i/o */
58 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelar9b4437a2016-11-21 11:02:58 -080059 struct ip_tunnel_info info;
pravin shelarfceb9c32016-10-28 09:59:16 -070060 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040061#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070062 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040063#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040064 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070065 struct gro_cells gro_cells;
pravin shelar9b4437a2016-11-21 11:02:58 -080066 bool collect_md;
67 bool use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +080068 bool ttl_inherit;
Stefano Brivioa025fb52018-11-08 12:19:19 +010069 enum ifla_geneve_df df;
John W. Linville2d07dc72015-05-13 12:57:30 -040070};
71
Pravin B Shelar371bd102015-08-26 23:46:54 -070072struct geneve_sock {
73 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070074 struct list_head list;
75 struct socket *sock;
76 struct rcu_head rcu;
77 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070078 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070079};
John W. Linville2d07dc72015-05-13 12:57:30 -040080
81static inline __u32 geneve_net_vni_hash(u8 vni[3])
82{
83 __u32 vnid;
84
85 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
86 return hash_32(vnid, VNI_HASH_BITS);
87}
88
Pravin B Shelare305ac62015-08-26 23:46:52 -070089static __be64 vni_to_tunnel_id(const __u8 *vni)
90{
91#ifdef __BIG_ENDIAN
92 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
93#else
94 return (__force __be64)(((__force u64)vni[0] << 40) |
95 ((__force u64)vni[1] << 48) |
96 ((__force u64)vni[2] << 56));
97#endif
98}
99
pravin shelar9b4437a2016-11-21 11:02:58 -0800100/* Convert 64 bit tunnel ID to 24 bit VNI. */
101static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
102{
103#ifdef __BIG_ENDIAN
104 vni[0] = (__force __u8)(tun_id >> 16);
105 vni[1] = (__force __u8)(tun_id >> 8);
106 vni[2] = (__force __u8)tun_id;
107#else
108 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
109 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
110 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
111#endif
112}
113
pravin shelar2e0b26e2016-11-21 11:03:01 -0800114static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
115{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800116 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800117}
118
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100119static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
120{
121 return gs->sock->sk->sk_family;
122}
123
Pravin B Shelar66d47002015-08-26 23:46:55 -0700124static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700125 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400126{
John W. Linville2d07dc72015-05-13 12:57:30 -0400127 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200128 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400129 __u32 hash;
130
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700132 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700133 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200134 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
135 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
136 addr == node->geneve->info.key.u.ipv4.dst)
137 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400138 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700139 return NULL;
140}
141
John W. Linville8ed66f02015-10-26 17:01:44 -0400142#if IS_ENABLED(CONFIG_IPV6)
143static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
144 struct in6_addr addr6, u8 vni[])
145{
146 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200147 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400148 __u32 hash;
149
150 /* Find the device for this VNI */
151 hash = geneve_net_vni_hash(vni);
152 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200153 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
154 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
155 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst))
156 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400157 }
158 return NULL;
159}
160#endif
161
Pravin B Shelar371bd102015-08-26 23:46:54 -0700162static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
163{
164 return (struct genevehdr *)(udp_hdr(skb) + 1);
165}
166
Jiri Benc9fc47542016-02-18 11:22:50 +0100167static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
168 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700169{
John W. Linville8ed66f02015-10-26 17:01:44 -0400170 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800171 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700172
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100173 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100174 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800175 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100176
John W. Linville8ed66f02015-10-26 17:01:44 -0400177 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700178
John W. Linville8ed66f02015-10-26 17:01:44 -0400179 if (gs->collect_md) {
180 vni = zero_vni;
181 addr = 0;
182 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100183 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400184 addr = iph->saddr;
185 }
186
Jiri Benc9fc47542016-02-18 11:22:50 +0100187 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400188#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100189 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800190 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100191 struct ipv6hdr *ip6h;
192 struct in6_addr addr6;
193
John W. Linville8ed66f02015-10-26 17:01:44 -0400194 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
195
196 if (gs->collect_md) {
197 vni = zero_vni;
198 addr6 = zero_addr6;
199 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100200 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400201 addr6 = ip6h->saddr;
202 }
203
Jiri Benc9fc47542016-02-18 11:22:50 +0100204 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400205#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700206 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100207 return NULL;
208}
209
210/* geneve receive/decap routine */
211static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
212 struct sk_buff *skb)
213{
214 struct genevehdr *gnvh = geneve_hdr(skb);
215 struct metadata_dst *tun_dst = NULL;
216 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700217 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100218 int err = 0;
219 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400220
Pravin B Shelar371bd102015-08-26 23:46:54 -0700221 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700222 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700223
224 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
225 (gnvh->oam ? TUNNEL_OAM : 0) |
226 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
227
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100228 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229 vni_to_tunnel_id(gnvh->vni),
230 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700231 if (!tun_dst) {
232 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700234 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700235 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700236 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700237 gnvh->options, gnvh->opt_len * 4,
238 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700239 } else {
240 /* Drop packets w/ critical options,
241 * since we don't support any...
242 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700243 if (gnvh->critical) {
244 geneve->dev->stats.rx_frame_errors++;
245 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700246 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700247 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700248 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400249
250 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400251 skb->protocol = eth_type_trans(skb, geneve->dev);
252 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
253
Pravin B Shelare305ac62015-08-26 23:46:52 -0700254 if (tun_dst)
255 skb_dst_set(skb, &tun_dst->dst);
256
John W. Linville2d07dc72015-05-13 12:57:30 -0400257 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700258 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
259 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400260 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700261 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400262
Jiri Benc9fc47542016-02-18 11:22:50 +0100263 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400264 skb_reset_network_header(skb);
265
Jiri Benc9fc47542016-02-18 11:22:50 +0100266 if (geneve_get_sk_family(gs) == AF_INET)
267 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400268#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 else
270 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400271#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400272
273 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400274 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100275 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400276 net_info_ratelimited("non-ECT from %pI4 "
277 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100278 &((struct iphdr *)oiph)->saddr,
279 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400280#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100281 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400282 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100283 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400284#endif
285 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400286 if (err > 1) {
287 ++geneve->dev->stats.rx_frame_errors;
288 ++geneve->dev->stats.rx_errors;
289 goto drop;
290 }
291 }
292
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700293 len = skb->len;
294 err = gro_cells_receive(&geneve->gro_cells, skb);
295 if (likely(err == NET_RX_SUCCESS)) {
296 stats = this_cpu_ptr(geneve->dev->tstats);
297 u64_stats_update_begin(&stats->syncp);
298 stats->rx_packets++;
299 stats->rx_bytes += len;
300 u64_stats_update_end(&stats->syncp);
301 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400302 return;
303drop:
304 /* Consume bad packet */
305 kfree_skb(skb);
306}
307
308/* Setup stats when device is created */
309static int geneve_init(struct net_device *dev)
310{
Jesse Gross8e816df2015-08-28 16:54:40 -0700311 struct geneve_dev *geneve = netdev_priv(dev);
312 int err;
313
John W. Linville2d07dc72015-05-13 12:57:30 -0400314 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
315 if (!dev->tstats)
316 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700317
318 err = gro_cells_init(&geneve->gro_cells, dev);
319 if (err) {
320 free_percpu(dev->tstats);
321 return err;
322 }
323
pravin shelar9b4437a2016-11-21 11:02:58 -0800324 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100325 if (err) {
326 free_percpu(dev->tstats);
327 gro_cells_destroy(&geneve->gro_cells);
328 return err;
329 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400330 return 0;
331}
332
333static void geneve_uninit(struct net_device *dev)
334{
Jesse Gross8e816df2015-08-28 16:54:40 -0700335 struct geneve_dev *geneve = netdev_priv(dev);
336
pravin shelar9b4437a2016-11-21 11:02:58 -0800337 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700338 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400339 free_percpu(dev->tstats);
340}
341
Pravin B Shelar371bd102015-08-26 23:46:54 -0700342/* Callback from net/ipv4/udp.c to receive packets */
343static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
344{
345 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100346 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700347 struct geneve_sock *gs;
348 int opts_len;
349
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700350 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700351 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200352 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700353
354 /* Return packets with reserved bits set */
355 geneveh = geneve_hdr(skb);
356 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200357 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700358
359 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200360 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700361
Jiri Benc9fc47542016-02-18 11:22:50 +0100362 gs = rcu_dereference_sk_user_data(sk);
363 if (!gs)
364 goto drop;
365
366 geneve = geneve_lookup_skb(gs, skb);
367 if (!geneve)
368 goto drop;
369
Pravin B Shelar371bd102015-08-26 23:46:54 -0700370 opts_len = geneveh->opt_len * 4;
371 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100372 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700373 !net_eq(geneve->net, dev_net(geneve->dev)))) {
374 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700375 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700376 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700377
Jiri Benc9fc47542016-02-18 11:22:50 +0100378 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700379 return 0;
380
381drop:
382 /* Consume bad packet */
383 kfree_skb(skb);
384 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700385}
386
Stefano Brivioa0796642018-11-08 12:19:18 +0100387/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
388static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
389{
390 struct genevehdr *geneveh;
391 struct geneve_sock *gs;
392 u8 zero_vni[3] = { 0 };
393 u8 *vni = zero_vni;
394
Stefano Brivioeccc73a2019-06-11 00:27:06 +0200395 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
Stefano Brivioa0796642018-11-08 12:19:18 +0100396 return -EINVAL;
397
398 geneveh = geneve_hdr(skb);
399 if (geneveh->ver != GENEVE_VER)
400 return -EINVAL;
401
402 if (geneveh->proto_type != htons(ETH_P_TEB))
403 return -EINVAL;
404
405 gs = rcu_dereference_sk_user_data(sk);
406 if (!gs)
407 return -ENOENT;
408
409 if (geneve_get_sk_family(gs) == AF_INET) {
410 struct iphdr *iph = ip_hdr(skb);
411 __be32 addr4 = 0;
412
413 if (!gs->collect_md) {
414 vni = geneve_hdr(skb)->vni;
415 addr4 = iph->daddr;
416 }
417
418 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
419 }
420
421#if IS_ENABLED(CONFIG_IPV6)
422 if (geneve_get_sk_family(gs) == AF_INET6) {
423 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Nathan Chancellor8a962c42018-11-16 18:36:27 -0700424 struct in6_addr addr6;
425
426 memset(&addr6, 0, sizeof(struct in6_addr));
Stefano Brivioa0796642018-11-08 12:19:18 +0100427
428 if (!gs->collect_md) {
429 vni = geneve_hdr(skb)->vni;
430 addr6 = ip6h->daddr;
431 }
432
433 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
434 }
435#endif
436
437 return -EPFNOSUPPORT;
438}
439
Pravin B Shelar371bd102015-08-26 23:46:54 -0700440static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800441 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700442{
443 struct socket *sock;
444 struct udp_port_cfg udp_conf;
445 int err;
446
447 memset(&udp_conf, 0, sizeof(udp_conf));
448
449 if (ipv6) {
450 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400451 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800452 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700453 } else {
454 udp_conf.family = AF_INET;
455 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
456 }
457
458 udp_conf.local_udp_port = port;
459
460 /* Open UDP socket */
461 err = udp_sock_create(net, &udp_conf, &sock);
462 if (err < 0)
463 return ERR_PTR(err);
464
465 return sock;
466}
467
Pravin B Shelar371bd102015-08-26 23:46:54 -0700468static int geneve_hlen(struct genevehdr *gh)
469{
470 return sizeof(*gh) + gh->opt_len * 4;
471}
472
David Millerd4546c22018-06-24 14:13:49 +0900473static struct sk_buff *geneve_gro_receive(struct sock *sk,
474 struct list_head *head,
475 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700476{
David Millerd4546c22018-06-24 14:13:49 +0900477 struct sk_buff *pp = NULL;
478 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700479 struct genevehdr *gh, *gh2;
480 unsigned int hlen, gh_len, off_gnv;
481 const struct packet_offload *ptype;
482 __be16 type;
483 int flush = 1;
484
485 off_gnv = skb_gro_offset(skb);
486 hlen = off_gnv + sizeof(*gh);
487 gh = skb_gro_header_fast(skb, off_gnv);
488 if (skb_gro_header_hard(skb, hlen)) {
489 gh = skb_gro_header_slow(skb, hlen, off_gnv);
490 if (unlikely(!gh))
491 goto out;
492 }
493
494 if (gh->ver != GENEVE_VER || gh->oam)
495 goto out;
496 gh_len = geneve_hlen(gh);
497
498 hlen = off_gnv + gh_len;
499 if (skb_gro_header_hard(skb, hlen)) {
500 gh = skb_gro_header_slow(skb, hlen, off_gnv);
501 if (unlikely(!gh))
502 goto out;
503 }
504
David Millerd4546c22018-06-24 14:13:49 +0900505 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700506 if (!NAPI_GRO_CB(p)->same_flow)
507 continue;
508
509 gh2 = (struct genevehdr *)(p->data + off_gnv);
510 if (gh->opt_len != gh2->opt_len ||
511 memcmp(gh, gh2, gh_len)) {
512 NAPI_GRO_CB(p)->same_flow = 0;
513 continue;
514 }
515 }
516
517 type = gh->proto_type;
518
519 rcu_read_lock();
520 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800521 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700522 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700523
524 skb_gro_pull(skb, gh_len);
525 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200526 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800527 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528
529out_unlock:
530 rcu_read_unlock();
531out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200532 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700533
534 return pp;
535}
536
Tom Herbert4a0090a2016-04-05 08:22:55 -0700537static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
538 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700539{
540 struct genevehdr *gh;
541 struct packet_offload *ptype;
542 __be16 type;
543 int gh_len;
544 int err = -ENOSYS;
545
Pravin B Shelar371bd102015-08-26 23:46:54 -0700546 gh = (struct genevehdr *)(skb->data + nhoff);
547 gh_len = geneve_hlen(gh);
548 type = gh->proto_type;
549
550 rcu_read_lock();
551 ptype = gro_find_complete_by_type(type);
552 if (ptype)
553 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
554
555 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700556
557 skb_set_inner_mac_header(skb, nhoff + gh_len);
558
Pravin B Shelar371bd102015-08-26 23:46:54 -0700559 return err;
560}
561
562/* Create new listen socket if needed */
563static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800564 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700565{
566 struct geneve_net *gn = net_generic(net, geneve_net_id);
567 struct geneve_sock *gs;
568 struct socket *sock;
569 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700570 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700571
572 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
573 if (!gs)
574 return ERR_PTR(-ENOMEM);
575
pravin shelar9b4437a2016-11-21 11:02:58 -0800576 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700577 if (IS_ERR(sock)) {
578 kfree(gs);
579 return ERR_CAST(sock);
580 }
581
582 gs->sock = sock;
583 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700584 for (h = 0; h < VNI_HASH_SIZE; ++h)
585 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700586
587 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700588 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700589
590 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700591 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700592 tunnel_cfg.sk_user_data = gs;
593 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700594 tunnel_cfg.gro_receive = geneve_gro_receive;
595 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700596 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100597 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700598 tunnel_cfg.encap_destroy = NULL;
599 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700600 list_add(&gs->list, &gn->sock_list);
601 return gs;
602}
603
John W. Linville8ed66f02015-10-26 17:01:44 -0400604static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700605{
John W. Linville8ed66f02015-10-26 17:01:44 -0400606 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700607 return;
608
609 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700610 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700611 udp_tunnel_sock_release(gs->sock);
612 kfree_rcu(gs, rcu);
613}
614
John W. Linville8ed66f02015-10-26 17:01:44 -0400615static void geneve_sock_release(struct geneve_dev *geneve)
616{
pravin shelarfceb9c32016-10-28 09:59:16 -0700617 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400618#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700619 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
620
621 rcu_assign_pointer(geneve->sock6, NULL);
622#endif
623
624 rcu_assign_pointer(geneve->sock4, NULL);
625 synchronize_net();
626
627 __geneve_sock_release(gs4);
628#if IS_ENABLED(CONFIG_IPV6)
629 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400630#endif
631}
632
Pravin B Shelar371bd102015-08-26 23:46:54 -0700633static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400634 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700635 __be16 dst_port)
636{
637 struct geneve_sock *gs;
638
639 list_for_each_entry(gs, &gn->sock_list, list) {
640 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100641 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700642 return gs;
643 }
644 }
645 return NULL;
646}
647
John W. Linville8ed66f02015-10-26 17:01:44 -0400648static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400649{
John W. Linville2d07dc72015-05-13 12:57:30 -0400650 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700651 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200652 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400653 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800654 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700655 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400656
pravin shelar9b4437a2016-11-21 11:02:58 -0800657 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700658 if (gs) {
659 gs->refcnt++;
660 goto out;
661 }
662
pravin shelar9b4437a2016-11-21 11:02:58 -0800663 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
664 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400665 if (IS_ERR(gs))
666 return PTR_ERR(gs);
667
Pravin B Shelar371bd102015-08-26 23:46:54 -0700668out:
669 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400670#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200671 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700672 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200673 node = &geneve->hlist6;
674 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400675#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200676 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700677 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200678 node = &geneve->hlist4;
679 }
680 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700681
pravin shelar9b4437a2016-11-21 11:02:58 -0800682 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
683 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200684 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400685 return 0;
686}
687
John W. Linville8ed66f02015-10-26 17:01:44 -0400688static int geneve_open(struct net_device *dev)
689{
690 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400691 bool metadata = geneve->collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100692 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400693 int ret = 0;
694
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100695 ipv6 = geneve->info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
696 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400697#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100698 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400699 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100700 if (ret < 0 && ret != -EAFNOSUPPORT)
701 ipv4 = false;
702 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400703#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100704 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400705 ret = geneve_sock_add(geneve, false);
706 if (ret < 0)
707 geneve_sock_release(geneve);
708
709 return ret;
710}
711
John W. Linville2d07dc72015-05-13 12:57:30 -0400712static int geneve_stop(struct net_device *dev)
713{
714 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400715
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200716 hlist_del_init_rcu(&geneve->hlist4.hlist);
717#if IS_ENABLED(CONFIG_IPV6)
718 hlist_del_init_rcu(&geneve->hlist6.hlist);
719#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400720 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400721 return 0;
722}
723
John W. Linville8ed66f02015-10-26 17:01:44 -0400724static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800725 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400726{
727 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800728 geneveh->opt_len = info->options_len / 4;
729 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
730 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400731 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800732 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400733 geneveh->proto_type = htons(ETH_P_TEB);
734 geneveh->rsvd2 = 0;
735
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700736 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
737 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400738}
739
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800740static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
741 const struct ip_tunnel_info *info,
742 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700743{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800744 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700745 struct genevehdr *gnvh;
746 int min_headroom;
747 int err;
748
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800749 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400750 skb_scrub_packet(skb, xnet);
751
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800752 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
753 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400754 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400755 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400756 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400757
Alexander Duyckaed069d2016-04-14 15:33:37 -0400758 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300759 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400760 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400761
Johannes Bergd58ff352017-06-16 14:29:23 +0200762 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800763 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400764 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
765 return 0;
766
767free_dst:
768 dst_release(dst);
769 return err;
770}
John W. Linville8ed66f02015-10-26 17:01:44 -0400771
772static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
773 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700774 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400775 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800776 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100778 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700779 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100780 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700781 struct rtable *rt = NULL;
782 __u8 tos;
783
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700784 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700785 return ERR_PTR(-EIO);
786
Pravin B Shelare305ac62015-08-26 23:46:52 -0700787 memset(fl4, 0, sizeof(*fl4));
788 fl4->flowi4_mark = skb->mark;
789 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800790 fl4->daddr = info->key.u.ipv4.dst;
791 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700792
pravin shelar9b4437a2016-11-21 11:02:58 -0800793 tos = info->key.tos;
794 if ((tos == 1) && !geneve->collect_md) {
795 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
796 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100797 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800798 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100799
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800800 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100801 if (use_cache) {
802 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
803 if (rt)
804 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700805 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700806 rt = ip_route_output_key(geneve->net, fl4);
807 if (IS_ERR(rt)) {
808 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700809 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700810 }
811 if (rt->dst.dev == dev) { /* is this necessary? */
812 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700813 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700814 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700815 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100816 if (use_cache)
817 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700818 return rt;
819}
820
John W. Linville8ed66f02015-10-26 17:01:44 -0400821#if IS_ENABLED(CONFIG_IPV6)
822static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
823 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700824 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400825 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800826 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400827{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100828 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400829 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400830 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100831 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400832 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400833
pravin shelarfceb9c32016-10-28 09:59:16 -0700834 if (!gs6)
835 return ERR_PTR(-EIO);
836
John W. Linville8ed66f02015-10-26 17:01:44 -0400837 memset(fl6, 0, sizeof(*fl6));
838 fl6->flowi6_mark = skb->mark;
839 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800840 fl6->daddr = info->key.u.ipv6.dst;
841 fl6->saddr = info->key.u.ipv6.src;
842 prio = info->key.tos;
843 if ((prio == 1) && !geneve->collect_md) {
844 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
845 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100846 }
847
pravin shelar9b4437a2016-11-21 11:02:58 -0800848 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
849 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800850 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100851 if (use_cache) {
852 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
853 if (dst)
854 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400855 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100856 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
857 NULL);
858 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400859 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
860 return ERR_PTR(-ENETUNREACH);
861 }
862 if (dst->dev == dev) { /* is this necessary? */
863 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
864 dst_release(dst);
865 return ERR_PTR(-ELOOP);
866 }
867
Paolo Abeni468dfff2016-02-12 15:43:58 +0100868 if (use_cache)
869 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400870 return dst;
871}
872#endif
873
pravin shelar9b4437a2016-11-21 11:02:58 -0800874static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800875 struct geneve_dev *geneve,
876 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700877{
pravin shelar9b4437a2016-11-21 11:02:58 -0800878 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
879 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
880 const struct ip_tunnel_key *key = &info->key;
881 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400882 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400883 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100884 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700885 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800886 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400887
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700888 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800889 if (IS_ERR(rt))
890 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700891
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200892 skb_tunnel_check_pmtu(skb, &rt->dst,
893 GENEVE_IPV4_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800894
Pravin B Shelar371bd102015-08-26 23:46:54 -0700895 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 if (geneve->collect_md) {
897 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700898 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100899
900 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700901 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800902 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800903 if (geneve->ttl_inherit)
904 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
905 else
906 ttl = key->ttl;
907 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100908
909 if (geneve->df == GENEVE_DF_SET) {
910 df = htons(IP_DF);
911 } else if (geneve->df == GENEVE_DF_INHERIT) {
912 struct ethhdr *eth = eth_hdr(skb);
913
914 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
915 df = htons(IP_DF);
916 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
917 struct iphdr *iph = ip_hdr(skb);
918
919 if (iph->frag_off & htons(IP_DF))
920 df = htons(IP_DF);
921 }
922 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400923 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800924
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800925 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800926 if (unlikely(err))
927 return err;
928
Pravin B Shelar039f5062015-12-24 14:34:54 -0800929 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800930 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800931 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800932 !(info->key.tun_flags & TUNNEL_CSUM));
933 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400934}
935
John W. Linville8ed66f02015-10-26 17:01:44 -0400936#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800937static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800938 struct geneve_dev *geneve,
939 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400940{
pravin shelar9b4437a2016-11-21 11:02:58 -0800941 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
942 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
943 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400944 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400945 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400946 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400947 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800948 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400949
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700950 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800951 if (IS_ERR(dst))
952 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400953
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200954 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800955
John W. Linville8ed66f02015-10-26 17:01:44 -0400956 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800957 if (geneve->collect_md) {
958 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400959 ttl = key->ttl;
960 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100961 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800962 ip_hdr(skb), skb);
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800963 if (geneve->ttl_inherit)
964 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
965 else
966 ttl = key->ttl;
967 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400968 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800969 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800970 if (unlikely(err))
971 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100972
Pravin B Shelar039f5062015-12-24 14:34:54 -0800973 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800974 &fl6.saddr, &fl6.daddr, prio, ttl,
975 info->key.label, sport, geneve->info.key.tp_dst,
976 !(info->key.tun_flags & TUNNEL_CSUM));
977 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400978}
979#endif
980
981static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
982{
983 struct geneve_dev *geneve = netdev_priv(dev);
984 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800985 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400986
pravin shelar9b4437a2016-11-21 11:02:58 -0800987 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400988 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800989 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800990 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +0200991 dev_kfree_skb(skb);
992 dev->stats.tx_dropped++;
993 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -0800994 }
995 } else {
996 info = &geneve->info;
997 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400998
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800999 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001000#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001001 if (info->mode & IP_TUNNEL_INFO_IPV6)
1002 err = geneve6_xmit_skb(skb, dev, geneve, info);
1003 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001004#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001005 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001006 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001007
1008 if (likely(!err))
1009 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001010
pravin shelar9b4437a2016-11-21 11:02:58 -08001011 dev_kfree_skb(skb);
1012
1013 if (err == -ELOOP)
1014 dev->stats.collisions++;
1015 else if (err == -ENETUNREACH)
1016 dev->stats.tx_carrier_errors++;
1017
1018 dev->stats.tx_errors++;
1019 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -04001020}
1021
Jarod Wilson91572082016-10-20 13:55:20 -04001022static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001023{
Jarod Wilson91572082016-10-20 13:55:20 -04001024 if (new_mtu > dev->max_mtu)
1025 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001026 else if (new_mtu < dev->min_mtu)
1027 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001028
David Wragg55e5bfb2016-02-10 00:05:57 +00001029 dev->mtu = new_mtu;
1030 return 0;
1031}
1032
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001033static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1034{
1035 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1036 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001037
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001038 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001039 struct rtable *rt;
1040 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001041 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -08001042
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001043 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001044 if (IS_ERR(rt))
1045 return PTR_ERR(rt);
1046
1047 ip_rt_put(rt);
1048 info->key.u.ipv4.src = fl4.saddr;
1049#if IS_ENABLED(CONFIG_IPV6)
1050 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001051 struct dst_entry *dst;
1052 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001053 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -08001054
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001055 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001056 if (IS_ERR(dst))
1057 return PTR_ERR(dst);
1058
1059 dst_release(dst);
1060 info->key.u.ipv6.src = fl6.saddr;
1061#endif
1062 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001063 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001064 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001065
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001066 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1067 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -08001068 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001069 return 0;
1070}
1071
John W. Linville2d07dc72015-05-13 12:57:30 -04001072static const struct net_device_ops geneve_netdev_ops = {
1073 .ndo_init = geneve_init,
1074 .ndo_uninit = geneve_uninit,
1075 .ndo_open = geneve_open,
1076 .ndo_stop = geneve_stop,
1077 .ndo_start_xmit = geneve_xmit,
1078 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001079 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001080 .ndo_validate_addr = eth_validate_addr,
1081 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001082 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001083};
1084
1085static void geneve_get_drvinfo(struct net_device *dev,
1086 struct ethtool_drvinfo *drvinfo)
1087{
1088 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1089 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1090}
1091
1092static const struct ethtool_ops geneve_ethtool_ops = {
1093 .get_drvinfo = geneve_get_drvinfo,
1094 .get_link = ethtool_op_get_link,
1095};
1096
1097/* Info for udev, that this is a virtual tunnel endpoint */
1098static struct device_type geneve_type = {
1099 .name = "geneve",
1100};
1101
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001102/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001103 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001104 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001105 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001106static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001107{
1108 struct net *net = dev_net(dev);
1109 struct geneve_net *gn = net_generic(net, geneve_net_id);
1110 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001111
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001112 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001113 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1114 if (push) {
1115 udp_tunnel_push_rx_port(dev, gs->sock,
1116 UDP_TUNNEL_TYPE_GENEVE);
1117 } else {
1118 udp_tunnel_drop_rx_port(dev, gs->sock,
1119 UDP_TUNNEL_TYPE_GENEVE);
1120 }
1121 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001122 rcu_read_unlock();
1123}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001124
John W. Linville2d07dc72015-05-13 12:57:30 -04001125/* Initialize the device structure. */
1126static void geneve_setup(struct net_device *dev)
1127{
1128 ether_setup(dev);
1129
1130 dev->netdev_ops = &geneve_netdev_ops;
1131 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001132 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001133
1134 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1135
John W. Linville2d07dc72015-05-13 12:57:30 -04001136 dev->features |= NETIF_F_LLTX;
1137 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1138 dev->features |= NETIF_F_RXCSUM;
1139 dev->features |= NETIF_F_GSO_SOFTWARE;
1140
John W. Linville2d07dc72015-05-13 12:57:30 -04001141 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1142 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001143
Jarod Wilson91572082016-10-20 13:55:20 -04001144 /* MTU range: 68 - (something less than 65535) */
1145 dev->min_mtu = ETH_MIN_MTU;
1146 /* The max_mtu calculation does not take account of GENEVE
1147 * options, to avoid excluding potentially valid
1148 * configurations. This will be further reduced by IPvX hdr size.
1149 */
1150 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1151
John W. Linville2d07dc72015-05-13 12:57:30 -04001152 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001153 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001154 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001155 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001156}
1157
1158static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1159 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001160 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001161 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001162 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001163 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001164 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001165 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001166 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001167 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1168 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1169 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001170 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001171 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001172};
1173
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001174static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1175 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001176{
1177 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001178 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1179 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1180 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001181 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001182 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001183
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001184 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1185 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1186 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001187 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001188 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001189 }
1190
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001191 if (!data) {
1192 NL_SET_ERR_MSG(extack,
1193 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001194 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001195 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001196
1197 if (data[IFLA_GENEVE_ID]) {
1198 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1199
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001200 if (vni >= GENEVE_N_VID) {
1201 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1202 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001203 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001204 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001205 }
1206
Stefano Brivioa025fb52018-11-08 12:19:19 +01001207 if (data[IFLA_GENEVE_DF]) {
1208 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1209
1210 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001211 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001212 "Invalid DF attribute");
1213 return -EINVAL;
1214 }
1215 }
1216
John W. Linville2d07dc72015-05-13 12:57:30 -04001217 return 0;
1218}
1219
Pravin B Shelar371bd102015-08-26 23:46:54 -07001220static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001221 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001222 bool *tun_on_same_port,
1223 bool *tun_collect_md)
1224{
pravin shelar9b4437a2016-11-21 11:02:58 -08001225 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001226
1227 *tun_on_same_port = false;
1228 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001229 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001230 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001231 *tun_collect_md = geneve->collect_md;
1232 *tun_on_same_port = true;
1233 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001234 if (info->key.tun_id == geneve->info.key.tun_id &&
1235 info->key.tp_dst == geneve->info.key.tp_dst &&
1236 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001237 t = geneve;
1238 }
1239 return t;
1240}
1241
pravin shelar9b4437a2016-11-21 11:02:58 -08001242static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1243{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001244 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1245 info->key.ttl || info->key.label || info->key.tp_src ||
1246 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001247}
1248
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001249static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1250 struct ip_tunnel_info *b)
1251{
1252 if (ip_tunnel_info_af(a) == AF_INET)
1253 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1254 else
1255 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1256}
1257
Pravin B Shelare305ac62015-08-26 23:46:52 -07001258static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001259 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001260 const struct ip_tunnel_info *info,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001261 bool metadata, bool ipv6_rx_csum,
Stefano Brivioa025fb52018-11-08 12:19:19 +01001262 bool ttl_inherit, enum ifla_geneve_df df)
John W. Linville2d07dc72015-05-13 12:57:30 -04001263{
1264 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001265 struct geneve_dev *t, *geneve = netdev_priv(dev);
1266 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001267 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001268
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001269 if (metadata && !is_tnl_info_zero(info)) {
1270 NL_SET_ERR_MSG(extack,
1271 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001272 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001273 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001274
1275 geneve->net = net;
1276 geneve->dev = dev;
1277
pravin shelar9b4437a2016-11-21 11:02:58 -08001278 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001279 if (t)
1280 return -EBUSY;
1281
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001282 /* make enough headroom for basic scenario */
1283 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001284 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001285 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001286 dev->max_mtu -= sizeof(struct iphdr);
1287 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001288 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001289 dev->max_mtu -= sizeof(struct ipv6hdr);
1290 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001291 dev->needed_headroom = encap_len + ETH_HLEN;
1292
Pravin B Shelar371bd102015-08-26 23:46:54 -07001293 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001294 if (tun_on_same_port) {
1295 NL_SET_ERR_MSG(extack,
1296 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001297 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001298 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001299 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001300 if (tun_collect_md) {
1301 NL_SET_ERR_MSG(extack,
1302 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001303 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001304 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001305 }
1306
pravin shelar9b4437a2016-11-21 11:02:58 -08001307 dst_cache_reset(&geneve->info.dst_cache);
1308 geneve->info = *info;
1309 geneve->collect_md = metadata;
1310 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001311 geneve->ttl_inherit = ttl_inherit;
Stefano Brivioa025fb52018-11-08 12:19:19 +01001312 geneve->df = df;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001313
John W. Linville2d07dc72015-05-13 12:57:30 -04001314 err = register_netdevice(dev);
1315 if (err)
1316 return err;
1317
1318 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001319 return 0;
1320}
1321
pravin shelar9b4437a2016-11-21 11:02:58 -08001322static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1323{
1324 memset(info, 0, sizeof(*info));
1325 info->key.tp_dst = htons(dst_port);
1326}
1327
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001328static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1329 struct netlink_ext_ack *extack,
1330 struct ip_tunnel_info *info, bool *metadata,
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001331 bool *use_udp6_rx_checksums, bool *ttl_inherit,
Stefano Brivioa025fb52018-11-08 12:19:19 +01001332 enum ifla_geneve_df *df, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001333{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001334 int attrtype;
1335
1336 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1337 NL_SET_ERR_MSG(extack,
1338 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001339 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001340 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001341
1342 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001343 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1344 attrtype = IFLA_GENEVE_REMOTE;
1345 goto change_notsup;
1346 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001347
1348 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001349 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001350
Dave Taht842841e2019-09-02 16:29:36 -07001351 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001352 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1353 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001354 return -EINVAL;
1355 }
1356 }
1357
pravin shelar9b4437a2016-11-21 11:02:58 -08001358 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001359#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001360 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1361 attrtype = IFLA_GENEVE_REMOTE6;
1362 goto change_notsup;
1363 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001364
1365 info->mode = IP_TUNNEL_INFO_IPV6;
1366 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001367 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001368
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001369 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001370 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001371 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1372 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001373 return -EINVAL;
1374 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001375 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001376 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1377 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001378 return -EINVAL;
1379 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001380 info->key.tun_flags |= TUNNEL_CSUM;
1381 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001382#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001383 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1384 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001385 return -EPFNOSUPPORT;
1386#endif
1387 }
1388
1389 if (data[IFLA_GENEVE_ID]) {
1390 __u32 vni;
1391 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001392 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001393
1394 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1395 tvni[0] = (vni & 0x00ff0000) >> 16;
1396 tvni[1] = (vni & 0x0000ff00) >> 8;
1397 tvni[2] = vni & 0x000000ff;
1398
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001399 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001400 if (changelink && (tunid != info->key.tun_id)) {
1401 attrtype = IFLA_GENEVE_ID;
1402 goto change_notsup;
1403 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001404 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001405 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001406
Hangbin Liua97d97b2018-09-29 23:06:29 +08001407 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1408 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
1409 *ttl_inherit = true;
1410 else
1411 *ttl_inherit = false;
1412 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001413 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Hangbin Liua97d97b2018-09-29 23:06:29 +08001414 *ttl_inherit = false;
1415 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001416
Pravin B Shelare305ac62015-08-26 23:46:52 -07001417 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001418 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001419
Stefano Brivioa025fb52018-11-08 12:19:19 +01001420 if (data[IFLA_GENEVE_DF])
1421 *df = nla_get_u8(data[IFLA_GENEVE_DF]);
1422
pravin shelar9b4437a2016-11-21 11:02:58 -08001423 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001424 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001425 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001426 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1427 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1428 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001429 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001430 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001431 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001432
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001433 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001434 if (changelink) {
1435 attrtype = IFLA_GENEVE_PORT;
1436 goto change_notsup;
1437 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001438 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1439 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001440
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001441 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001442 if (changelink) {
1443 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1444 goto change_notsup;
1445 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001446 *metadata = true;
1447 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001448
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001449 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001450 if (changelink) {
1451 attrtype = IFLA_GENEVE_UDP_CSUM;
1452 goto change_notsup;
1453 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001454 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1455 info->key.tun_flags |= TUNNEL_CSUM;
1456 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001457
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001458 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001459#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001460 if (changelink) {
1461 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1462 goto change_notsup;
1463 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001464 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1465 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001466#else
1467 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1468 "IPv6 support not enabled in the kernel");
1469 return -EPFNOSUPPORT;
1470#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001471 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001472
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001473 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001474#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001475 if (changelink) {
1476 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1477 goto change_notsup;
1478 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001479 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1480 *use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001481#else
1482 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1483 "IPv6 support not enabled in the kernel");
1484 return -EPFNOSUPPORT;
1485#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001486 }
1487
1488 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001489change_notsup:
1490 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1491 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1492 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001493}
1494
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001495static void geneve_link_config(struct net_device *dev,
1496 struct ip_tunnel_info *info, struct nlattr *tb[])
1497{
1498 struct geneve_dev *geneve = netdev_priv(dev);
1499 int ldev_mtu = 0;
1500
1501 if (tb[IFLA_MTU]) {
1502 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1503 return;
1504 }
1505
1506 switch (ip_tunnel_info_af(info)) {
1507 case AF_INET: {
1508 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1509 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1510
1511 if (!IS_ERR(rt) && rt->dst.dev) {
1512 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1513 ip_rt_put(rt);
1514 }
1515 break;
1516 }
1517#if IS_ENABLED(CONFIG_IPV6)
1518 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001519 struct rt6_info *rt;
1520
1521 if (!__in6_dev_get(dev))
1522 break;
1523
1524 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1525 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001526
1527 if (rt && rt->dst.dev)
1528 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1529 ip6_rt_put(rt);
1530 break;
1531 }
1532#endif
1533 }
1534
1535 if (ldev_mtu <= 0)
1536 return;
1537
1538 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1539}
1540
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001541static int geneve_newlink(struct net *net, struct net_device *dev,
1542 struct nlattr *tb[], struct nlattr *data[],
1543 struct netlink_ext_ack *extack)
1544{
Stefano Brivioa025fb52018-11-08 12:19:19 +01001545 enum ifla_geneve_df df = GENEVE_DF_UNSET;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001546 bool use_udp6_rx_checksums = false;
1547 struct ip_tunnel_info info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001548 bool ttl_inherit = false;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001549 bool metadata = false;
1550 int err;
1551
1552 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001553 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Stefano Brivioa025fb52018-11-08 12:19:19 +01001554 &use_udp6_rx_checksums, &ttl_inherit, &df, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001555 if (err)
1556 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001557
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001558 err = geneve_configure(net, dev, extack, &info, metadata,
Stefano Brivioa025fb52018-11-08 12:19:19 +01001559 use_udp6_rx_checksums, ttl_inherit, df);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001560 if (err)
1561 return err;
1562
1563 geneve_link_config(dev, &info, tb);
1564
1565 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001566}
1567
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001568/* Quiesces the geneve device data path for both TX and RX.
1569 *
1570 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1571 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1572 * to complete for the existing set of in-flight packets to be transmitted,
1573 * then we would have quiesced the transmit data path. All the future packets
1574 * will get dropped until we unquiesce the data path.
1575 *
1576 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1577 * if we set that to NULL under RCU and wait for synchronize_net() to
1578 * complete, then we would have quiesced the receive data path.
1579 */
1580static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1581 struct geneve_sock **gs6)
1582{
1583 *gs4 = rtnl_dereference(geneve->sock4);
1584 rcu_assign_pointer(geneve->sock4, NULL);
1585 if (*gs4)
1586 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1587#if IS_ENABLED(CONFIG_IPV6)
1588 *gs6 = rtnl_dereference(geneve->sock6);
1589 rcu_assign_pointer(geneve->sock6, NULL);
1590 if (*gs6)
1591 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1592#else
1593 *gs6 = NULL;
1594#endif
1595 synchronize_net();
1596}
1597
1598/* Resumes the geneve device data path for both TX and RX. */
1599static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1600 struct geneve_sock __maybe_unused *gs6)
1601{
1602 rcu_assign_pointer(geneve->sock4, gs4);
1603 if (gs4)
1604 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1605#if IS_ENABLED(CONFIG_IPV6)
1606 rcu_assign_pointer(geneve->sock6, gs6);
1607 if (gs6)
1608 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1609#endif
1610 synchronize_net();
1611}
1612
1613static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1614 struct nlattr *data[],
1615 struct netlink_ext_ack *extack)
1616{
1617 struct geneve_dev *geneve = netdev_priv(dev);
1618 struct geneve_sock *gs4, *gs6;
1619 struct ip_tunnel_info info;
1620 bool metadata;
1621 bool use_udp6_rx_checksums;
Stefano Brivioa025fb52018-11-08 12:19:19 +01001622 enum ifla_geneve_df df;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001623 bool ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001624 int err;
1625
1626 /* If the geneve device is configured for metadata (or externally
1627 * controlled, for example, OVS), then nothing can be changed.
1628 */
1629 if (geneve->collect_md)
1630 return -EOPNOTSUPP;
1631
1632 /* Start with the existing info. */
1633 memcpy(&info, &geneve->info, sizeof(info));
1634 metadata = geneve->collect_md;
1635 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001636 ttl_inherit = geneve->ttl_inherit;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001637 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Stefano Brivioa025fb52018-11-08 12:19:19 +01001638 &use_udp6_rx_checksums, &ttl_inherit, &df, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001639 if (err)
1640 return err;
1641
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001642 if (!geneve_dst_addr_equal(&geneve->info, &info)) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001643 dst_cache_reset(&info.dst_cache);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001644 geneve_link_config(dev, &info, tb);
1645 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001646
1647 geneve_quiesce(geneve, &gs4, &gs6);
1648 geneve->info = info;
1649 geneve->collect_md = metadata;
1650 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001651 geneve->ttl_inherit = ttl_inherit;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001652 geneve_unquiesce(geneve, gs4, gs6);
1653
1654 return 0;
1655}
1656
John W. Linville2d07dc72015-05-13 12:57:30 -04001657static void geneve_dellink(struct net_device *dev, struct list_head *head)
1658{
1659 struct geneve_dev *geneve = netdev_priv(dev);
1660
John W. Linville2d07dc72015-05-13 12:57:30 -04001661 list_del(&geneve->next);
1662 unregister_netdevice_queue(dev, head);
1663}
1664
1665static size_t geneve_get_size(const struct net_device *dev)
1666{
1667 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001668 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001669 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001670 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001671 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001672 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001673 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001674 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001675 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1676 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1677 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001678 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001679 0;
1680}
1681
1682static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1683{
1684 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001685 struct ip_tunnel_info *info = &geneve->info;
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001686 bool ttl_inherit = geneve->ttl_inherit;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001687 bool metadata = geneve->collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001688 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001689 __u32 vni;
1690
pravin shelar9b4437a2016-11-21 11:02:58 -08001691 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1692 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001693 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1694 goto nla_put_failure;
1695
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001696 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001697 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001698 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001699 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001700 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1701 !!(info->key.tun_flags & TUNNEL_CSUM)))
1702 goto nla_put_failure;
1703
John W. Linville8ed66f02015-10-26 17:01:44 -04001704#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001705 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001706 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001707 &info->key.u.ipv6.dst))
1708 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001709 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1710 !(info->key.tun_flags & TUNNEL_CSUM)))
1711 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001712#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001713 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001714
pravin shelar9b4437a2016-11-21 11:02:58 -08001715 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1716 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1717 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001718 goto nla_put_failure;
1719
Stefano Brivioa025fb52018-11-08 12:19:19 +01001720 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->df))
1721 goto nla_put_failure;
1722
pravin shelar9b4437a2016-11-21 11:02:58 -08001723 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001724 goto nla_put_failure;
1725
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001726 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001727 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001728
Hangbin Liuf9094b72017-11-23 11:27:24 +08001729#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001730 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1731 !geneve->use_udp6_rx_checksums))
1732 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001733#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001734
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001735 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1736 goto nla_put_failure;
1737
John W. Linville2d07dc72015-05-13 12:57:30 -04001738 return 0;
1739
1740nla_put_failure:
1741 return -EMSGSIZE;
1742}
1743
1744static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1745 .kind = "geneve",
1746 .maxtype = IFLA_GENEVE_MAX,
1747 .policy = geneve_policy,
1748 .priv_size = sizeof(struct geneve_dev),
1749 .setup = geneve_setup,
1750 .validate = geneve_validate,
1751 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001752 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001753 .dellink = geneve_dellink,
1754 .get_size = geneve_get_size,
1755 .fill_info = geneve_fill_info,
1756};
1757
Pravin B Shelare305ac62015-08-26 23:46:52 -07001758struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1759 u8 name_assign_type, u16 dst_port)
1760{
1761 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001762 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001763 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001764 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001765 int err;
1766
1767 memset(tb, 0, sizeof(tb));
1768 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001769 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001770 if (IS_ERR(dev))
1771 return dev;
1772
pravin shelar9b4437a2016-11-21 11:02:58 -08001773 init_tnl_info(&info, dst_port);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001774 err = geneve_configure(net, dev, NULL, &info,
1775 true, true, false, GENEVE_DF_UNSET);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001776 if (err) {
1777 free_netdev(dev);
1778 return ERR_PTR(err);
1779 }
David Wragg7e059152016-02-10 00:05:58 +00001780
1781 /* openvswitch users expect packet sizes to be unrestricted,
1782 * so set the largest MTU we can.
1783 */
Jarod Wilson91572082016-10-20 13:55:20 -04001784 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001785 if (err)
1786 goto err;
1787
Nicolas Dichtel41009482016-06-13 10:31:07 +02001788 err = rtnl_configure_link(dev, NULL);
1789 if (err < 0)
1790 goto err;
1791
Pravin B Shelare305ac62015-08-26 23:46:52 -07001792 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001793err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001794 geneve_dellink(dev, &list_kill);
1795 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001796 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001797}
1798EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1799
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001800static int geneve_netdevice_event(struct notifier_block *unused,
1801 unsigned long event, void *ptr)
1802{
1803 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1804
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001805 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001806 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001807 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001808 } else if (event == NETDEV_UNREGISTER) {
1809 geneve_offload_rx_ports(dev, false);
1810 } else if (event == NETDEV_REGISTER) {
1811 geneve_offload_rx_ports(dev, true);
1812 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001813
1814 return NOTIFY_DONE;
1815}
1816
1817static struct notifier_block geneve_notifier_block __read_mostly = {
1818 .notifier_call = geneve_netdevice_event,
1819};
1820
John W. Linville2d07dc72015-05-13 12:57:30 -04001821static __net_init int geneve_init_net(struct net *net)
1822{
1823 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001824
1825 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001826 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001827 return 0;
1828}
1829
Haishuang Yan2843a252017-12-16 17:54:50 +08001830static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001831{
1832 struct geneve_net *gn = net_generic(net, geneve_net_id);
1833 struct geneve_dev *geneve, *next;
1834 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001835
1836 /* gather any geneve devices that were moved into this ns */
1837 for_each_netdev_safe(net, dev, aux)
1838 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001839 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001840
1841 /* now gather any other geneve devices that were created in this ns */
1842 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1843 /* If geneve->dev is in the same netns, it was already added
1844 * to the list by the previous loop.
1845 */
1846 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001847 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001848 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001849}
1850
1851static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1852{
1853 struct net *net;
1854 LIST_HEAD(list);
1855
1856 rtnl_lock();
1857 list_for_each_entry(net, net_list, exit_list)
1858 geneve_destroy_tunnels(net, &list);
1859
John W. Linville2d07dc72015-05-13 12:57:30 -04001860 /* unregister the devices gathered above */
1861 unregister_netdevice_many(&list);
1862 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001863
1864 list_for_each_entry(net, net_list, exit_list) {
1865 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1866
1867 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1868 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001869}
1870
1871static struct pernet_operations geneve_net_ops = {
1872 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001873 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001874 .id = &geneve_net_id,
1875 .size = sizeof(struct geneve_net),
1876};
1877
1878static int __init geneve_init_module(void)
1879{
1880 int rc;
1881
1882 rc = register_pernet_subsys(&geneve_net_ops);
1883 if (rc)
1884 goto out1;
1885
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001886 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001887 if (rc)
1888 goto out2;
1889
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001890 rc = rtnl_link_register(&geneve_link_ops);
1891 if (rc)
1892 goto out3;
1893
John W. Linville2d07dc72015-05-13 12:57:30 -04001894 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001895out3:
1896 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001897out2:
1898 unregister_pernet_subsys(&geneve_net_ops);
1899out1:
1900 return rc;
1901}
1902late_initcall(geneve_init_module);
1903
1904static void __exit geneve_cleanup_module(void)
1905{
1906 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001907 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001908 unregister_pernet_subsys(&geneve_net_ops);
1909}
1910module_exit(geneve_cleanup_module);
1911
1912MODULE_LICENSE("GPL");
1913MODULE_VERSION(GENEVE_NETDEV_VER);
1914MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1915MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1916MODULE_ALIAS_RTNL_LINK("geneve");