blob: 49b00def2eef5893efb7bea98d51f7bd3ee71b85 [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;
220 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700221 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100222 int err = 0;
223 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400224
Pravin B Shelar371bd102015-08-26 23:46:54 -0700225 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700226 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700227
228 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
229 (gnvh->oam ? TUNNEL_OAM : 0) |
230 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
231
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100232 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 vni_to_tunnel_id(gnvh->vni),
234 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700235 if (!tun_dst) {
236 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700237 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700238 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700239 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700240 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700241 gnvh->options, gnvh->opt_len * 4,
242 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700243 } else {
244 /* Drop packets w/ critical options,
245 * since we don't support any...
246 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700247 if (gnvh->critical) {
248 geneve->dev->stats.rx_frame_errors++;
249 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700250 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700251 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700252 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400253
254 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400255 skb->protocol = eth_type_trans(skb, geneve->dev);
256 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
257
Pravin B Shelare305ac62015-08-26 23:46:52 -0700258 if (tun_dst)
259 skb_dst_set(skb, &tun_dst->dst);
260
John W. Linville2d07dc72015-05-13 12:57:30 -0400261 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700262 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
263 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400264 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700265 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400266
Jiri Benc9fc47542016-02-18 11:22:50 +0100267 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400268 skb_reset_network_header(skb);
269
Jiri Benc9fc47542016-02-18 11:22:50 +0100270 if (geneve_get_sk_family(gs) == AF_INET)
271 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400272#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100273 else
274 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400275#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400276
277 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400278 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100279 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400280 net_info_ratelimited("non-ECT from %pI4 "
281 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100282 &((struct iphdr *)oiph)->saddr,
283 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400284#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100285 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400286 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100287 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400288#endif
289 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400290 if (err > 1) {
291 ++geneve->dev->stats.rx_frame_errors;
292 ++geneve->dev->stats.rx_errors;
293 goto drop;
294 }
295 }
296
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700297 len = skb->len;
298 err = gro_cells_receive(&geneve->gro_cells, skb);
299 if (likely(err == NET_RX_SUCCESS)) {
300 stats = this_cpu_ptr(geneve->dev->tstats);
301 u64_stats_update_begin(&stats->syncp);
302 stats->rx_packets++;
303 stats->rx_bytes += len;
304 u64_stats_update_end(&stats->syncp);
305 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400306 return;
307drop:
308 /* Consume bad packet */
309 kfree_skb(skb);
310}
311
312/* Setup stats when device is created */
313static int geneve_init(struct net_device *dev)
314{
Jesse Gross8e816df2015-08-28 16:54:40 -0700315 struct geneve_dev *geneve = netdev_priv(dev);
316 int err;
317
John W. Linville2d07dc72015-05-13 12:57:30 -0400318 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
319 if (!dev->tstats)
320 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700321
322 err = gro_cells_init(&geneve->gro_cells, dev);
323 if (err) {
324 free_percpu(dev->tstats);
325 return err;
326 }
327
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200328 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100329 if (err) {
330 free_percpu(dev->tstats);
331 gro_cells_destroy(&geneve->gro_cells);
332 return err;
333 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400334 return 0;
335}
336
337static void geneve_uninit(struct net_device *dev)
338{
Jesse Gross8e816df2015-08-28 16:54:40 -0700339 struct geneve_dev *geneve = netdev_priv(dev);
340
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200341 dst_cache_destroy(&geneve->cfg.info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700342 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400343 free_percpu(dev->tstats);
344}
345
Pravin B Shelar371bd102015-08-26 23:46:54 -0700346/* Callback from net/ipv4/udp.c to receive packets */
347static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
348{
349 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100350 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700351 struct geneve_sock *gs;
352 int opts_len;
353
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700354 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700355 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200356 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700357
358 /* Return packets with reserved bits set */
359 geneveh = geneve_hdr(skb);
360 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200361 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700362
363 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200364 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700365
Jiri Benc9fc47542016-02-18 11:22:50 +0100366 gs = rcu_dereference_sk_user_data(sk);
367 if (!gs)
368 goto drop;
369
370 geneve = geneve_lookup_skb(gs, skb);
371 if (!geneve)
372 goto drop;
373
Pravin B Shelar371bd102015-08-26 23:46:54 -0700374 opts_len = geneveh->opt_len * 4;
375 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100376 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700377 !net_eq(geneve->net, dev_net(geneve->dev)))) {
378 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700379 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700380 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700381
Jiri Benc9fc47542016-02-18 11:22:50 +0100382 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700383 return 0;
384
385drop:
386 /* Consume bad packet */
387 kfree_skb(skb);
388 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700389}
390
Stefano Brivioa0796642018-11-08 12:19:18 +0100391/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
392static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
393{
394 struct genevehdr *geneveh;
395 struct geneve_sock *gs;
396 u8 zero_vni[3] = { 0 };
397 u8 *vni = zero_vni;
398
Stefano Brivioeccc73a2019-06-11 00:27:06 +0200399 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
Stefano Brivioa0796642018-11-08 12:19:18 +0100400 return -EINVAL;
401
402 geneveh = geneve_hdr(skb);
403 if (geneveh->ver != GENEVE_VER)
404 return -EINVAL;
405
406 if (geneveh->proto_type != htons(ETH_P_TEB))
407 return -EINVAL;
408
409 gs = rcu_dereference_sk_user_data(sk);
410 if (!gs)
411 return -ENOENT;
412
413 if (geneve_get_sk_family(gs) == AF_INET) {
414 struct iphdr *iph = ip_hdr(skb);
415 __be32 addr4 = 0;
416
417 if (!gs->collect_md) {
418 vni = geneve_hdr(skb)->vni;
419 addr4 = iph->daddr;
420 }
421
422 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
423 }
424
425#if IS_ENABLED(CONFIG_IPV6)
426 if (geneve_get_sk_family(gs) == AF_INET6) {
427 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Nathan Chancellor8a962c42018-11-16 18:36:27 -0700428 struct in6_addr addr6;
429
430 memset(&addr6, 0, sizeof(struct in6_addr));
Stefano Brivioa0796642018-11-08 12:19:18 +0100431
432 if (!gs->collect_md) {
433 vni = geneve_hdr(skb)->vni;
434 addr6 = ip6h->daddr;
435 }
436
437 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
438 }
439#endif
440
441 return -EPFNOSUPPORT;
442}
443
Pravin B Shelar371bd102015-08-26 23:46:54 -0700444static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800445 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700446{
447 struct socket *sock;
448 struct udp_port_cfg udp_conf;
449 int err;
450
451 memset(&udp_conf, 0, sizeof(udp_conf));
452
453 if (ipv6) {
454 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400455 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800456 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700457 } else {
458 udp_conf.family = AF_INET;
459 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
460 }
461
462 udp_conf.local_udp_port = port;
463
464 /* Open UDP socket */
465 err = udp_sock_create(net, &udp_conf, &sock);
466 if (err < 0)
467 return ERR_PTR(err);
468
469 return sock;
470}
471
Pravin B Shelar371bd102015-08-26 23:46:54 -0700472static int geneve_hlen(struct genevehdr *gh)
473{
474 return sizeof(*gh) + gh->opt_len * 4;
475}
476
David Millerd4546c22018-06-24 14:13:49 +0900477static struct sk_buff *geneve_gro_receive(struct sock *sk,
478 struct list_head *head,
479 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700480{
David Millerd4546c22018-06-24 14:13:49 +0900481 struct sk_buff *pp = NULL;
482 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700483 struct genevehdr *gh, *gh2;
484 unsigned int hlen, gh_len, off_gnv;
485 const struct packet_offload *ptype;
486 __be16 type;
487 int flush = 1;
488
489 off_gnv = skb_gro_offset(skb);
490 hlen = off_gnv + sizeof(*gh);
491 gh = skb_gro_header_fast(skb, off_gnv);
492 if (skb_gro_header_hard(skb, hlen)) {
493 gh = skb_gro_header_slow(skb, hlen, off_gnv);
494 if (unlikely(!gh))
495 goto out;
496 }
497
498 if (gh->ver != GENEVE_VER || gh->oam)
499 goto out;
500 gh_len = geneve_hlen(gh);
501
502 hlen = off_gnv + gh_len;
503 if (skb_gro_header_hard(skb, hlen)) {
504 gh = skb_gro_header_slow(skb, hlen, off_gnv);
505 if (unlikely(!gh))
506 goto out;
507 }
508
David Millerd4546c22018-06-24 14:13:49 +0900509 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700510 if (!NAPI_GRO_CB(p)->same_flow)
511 continue;
512
513 gh2 = (struct genevehdr *)(p->data + off_gnv);
514 if (gh->opt_len != gh2->opt_len ||
515 memcmp(gh, gh2, gh_len)) {
516 NAPI_GRO_CB(p)->same_flow = 0;
517 continue;
518 }
519 }
520
521 type = gh->proto_type;
522
523 rcu_read_lock();
524 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800525 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700526 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700527
528 skb_gro_pull(skb, gh_len);
529 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200530 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800531 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700532
533out_unlock:
534 rcu_read_unlock();
535out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200536 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700537
538 return pp;
539}
540
Tom Herbert4a0090a2016-04-05 08:22:55 -0700541static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
542 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700543{
544 struct genevehdr *gh;
545 struct packet_offload *ptype;
546 __be16 type;
547 int gh_len;
548 int err = -ENOSYS;
549
Pravin B Shelar371bd102015-08-26 23:46:54 -0700550 gh = (struct genevehdr *)(skb->data + nhoff);
551 gh_len = geneve_hlen(gh);
552 type = gh->proto_type;
553
554 rcu_read_lock();
555 ptype = gro_find_complete_by_type(type);
556 if (ptype)
557 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
558
559 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700560
561 skb_set_inner_mac_header(skb, nhoff + gh_len);
562
Pravin B Shelar371bd102015-08-26 23:46:54 -0700563 return err;
564}
565
566/* Create new listen socket if needed */
567static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800568 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700569{
570 struct geneve_net *gn = net_generic(net, geneve_net_id);
571 struct geneve_sock *gs;
572 struct socket *sock;
573 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700574 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700575
576 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
577 if (!gs)
578 return ERR_PTR(-ENOMEM);
579
pravin shelar9b4437a2016-11-21 11:02:58 -0800580 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700581 if (IS_ERR(sock)) {
582 kfree(gs);
583 return ERR_CAST(sock);
584 }
585
586 gs->sock = sock;
587 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700588 for (h = 0; h < VNI_HASH_SIZE; ++h)
589 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590
591 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700592 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700593
594 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700595 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700596 tunnel_cfg.sk_user_data = gs;
597 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700598 tunnel_cfg.gro_receive = geneve_gro_receive;
599 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700600 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100601 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700602 tunnel_cfg.encap_destroy = NULL;
603 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700604 list_add(&gs->list, &gn->sock_list);
605 return gs;
606}
607
John W. Linville8ed66f02015-10-26 17:01:44 -0400608static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700609{
John W. Linville8ed66f02015-10-26 17:01:44 -0400610 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700611 return;
612
613 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700614 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700615 udp_tunnel_sock_release(gs->sock);
616 kfree_rcu(gs, rcu);
617}
618
John W. Linville8ed66f02015-10-26 17:01:44 -0400619static void geneve_sock_release(struct geneve_dev *geneve)
620{
pravin shelarfceb9c32016-10-28 09:59:16 -0700621 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400622#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700623 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
624
625 rcu_assign_pointer(geneve->sock6, NULL);
626#endif
627
628 rcu_assign_pointer(geneve->sock4, NULL);
629 synchronize_net();
630
631 __geneve_sock_release(gs4);
632#if IS_ENABLED(CONFIG_IPV6)
633 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400634#endif
635}
636
Pravin B Shelar371bd102015-08-26 23:46:54 -0700637static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400638 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700639 __be16 dst_port)
640{
641 struct geneve_sock *gs;
642
643 list_for_each_entry(gs, &gn->sock_list, list) {
644 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100645 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700646 return gs;
647 }
648 }
649 return NULL;
650}
651
John W. Linville8ed66f02015-10-26 17:01:44 -0400652static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400653{
John W. Linville2d07dc72015-05-13 12:57:30 -0400654 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700655 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200656 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400657 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800658 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700659 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400660
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200661 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700662 if (gs) {
663 gs->refcnt++;
664 goto out;
665 }
666
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200667 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
668 geneve->cfg.use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400669 if (IS_ERR(gs))
670 return PTR_ERR(gs);
671
Pravin B Shelar371bd102015-08-26 23:46:54 -0700672out:
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200673 gs->collect_md = geneve->cfg.collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400674#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200675 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700676 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200677 node = &geneve->hlist6;
678 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400679#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200680 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700681 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200682 node = &geneve->hlist4;
683 }
684 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700685
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200686 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
pravin shelar9b4437a2016-11-21 11:02:58 -0800687 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200688 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400689 return 0;
690}
691
John W. Linville8ed66f02015-10-26 17:01:44 -0400692static int geneve_open(struct net_device *dev)
693{
694 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200695 bool metadata = geneve->cfg.collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100696 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400697 int ret = 0;
698
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200699 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100700 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400701#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100702 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400703 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100704 if (ret < 0 && ret != -EAFNOSUPPORT)
705 ipv4 = false;
706 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400707#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100708 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400709 ret = geneve_sock_add(geneve, false);
710 if (ret < 0)
711 geneve_sock_release(geneve);
712
713 return ret;
714}
715
John W. Linville2d07dc72015-05-13 12:57:30 -0400716static int geneve_stop(struct net_device *dev)
717{
718 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400719
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200720 hlist_del_init_rcu(&geneve->hlist4.hlist);
721#if IS_ENABLED(CONFIG_IPV6)
722 hlist_del_init_rcu(&geneve->hlist6.hlist);
723#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400724 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400725 return 0;
726}
727
John W. Linville8ed66f02015-10-26 17:01:44 -0400728static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800729 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400730{
731 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800732 geneveh->opt_len = info->options_len / 4;
733 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
734 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400735 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800736 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400737 geneveh->proto_type = htons(ETH_P_TEB);
738 geneveh->rsvd2 = 0;
739
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700740 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
741 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400742}
743
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800744static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
745 const struct ip_tunnel_info *info,
746 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700747{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800748 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700749 struct genevehdr *gnvh;
750 int min_headroom;
751 int err;
752
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800753 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400754 skb_scrub_packet(skb, xnet);
755
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800756 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
757 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400758 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400759 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400760 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400761
Alexander Duyckaed069d2016-04-14 15:33:37 -0400762 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300763 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400764 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400765
Johannes Bergd58ff352017-06-16 14:29:23 +0200766 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800767 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400768 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
769 return 0;
770
771free_dst:
772 dst_release(dst);
773 return err;
774}
John W. Linville8ed66f02015-10-26 17:01:44 -0400775
776static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
777 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700778 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400779 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800780 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700781{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100782 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700783 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100784 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700785 struct rtable *rt = NULL;
786 __u8 tos;
787
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700788 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700789 return ERR_PTR(-EIO);
790
Pravin B Shelare305ac62015-08-26 23:46:52 -0700791 memset(fl4, 0, sizeof(*fl4));
792 fl4->flowi4_mark = skb->mark;
793 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800794 fl4->daddr = info->key.u.ipv4.dst;
795 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700796
pravin shelar9b4437a2016-11-21 11:02:58 -0800797 tos = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200798 if ((tos == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800799 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
800 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100801 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800802 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100803
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800804 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100805 if (use_cache) {
806 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
807 if (rt)
808 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700809 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700810 rt = ip_route_output_key(geneve->net, fl4);
811 if (IS_ERR(rt)) {
812 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700813 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700814 }
815 if (rt->dst.dev == dev) { /* is this necessary? */
816 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700817 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700818 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700819 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100820 if (use_cache)
821 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700822 return rt;
823}
824
John W. Linville8ed66f02015-10-26 17:01:44 -0400825#if IS_ENABLED(CONFIG_IPV6)
826static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
827 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700828 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400829 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800830 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400831{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100832 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400833 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400834 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100835 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400836 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400837
pravin shelarfceb9c32016-10-28 09:59:16 -0700838 if (!gs6)
839 return ERR_PTR(-EIO);
840
John W. Linville8ed66f02015-10-26 17:01:44 -0400841 memset(fl6, 0, sizeof(*fl6));
842 fl6->flowi6_mark = skb->mark;
843 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 fl6->daddr = info->key.u.ipv6.dst;
845 fl6->saddr = info->key.u.ipv6.src;
846 prio = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200847 if ((prio == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800848 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
849 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100850 }
851
pravin shelar9b4437a2016-11-21 11:02:58 -0800852 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
853 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800854 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100855 if (use_cache) {
856 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
857 if (dst)
858 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400859 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100860 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
861 NULL);
862 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400863 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
864 return ERR_PTR(-ENETUNREACH);
865 }
866 if (dst->dev == dev) { /* is this necessary? */
867 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
868 dst_release(dst);
869 return ERR_PTR(-ELOOP);
870 }
871
Paolo Abeni468dfff2016-02-12 15:43:58 +0100872 if (use_cache)
873 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400874 return dst;
875}
876#endif
877
pravin shelar9b4437a2016-11-21 11:02:58 -0800878static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800879 struct geneve_dev *geneve,
880 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700881{
pravin shelar9b4437a2016-11-21 11:02:58 -0800882 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
883 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
884 const struct ip_tunnel_key *key = &info->key;
885 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400886 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400887 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100888 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700889 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800890 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400891
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700892 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800893 if (IS_ERR(rt))
894 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700895
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200896 skb_tunnel_check_pmtu(skb, &rt->dst,
897 GENEVE_IPV4_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800898
Pravin B Shelar371bd102015-08-26 23:46:54 -0700899 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200900 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800901 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700902 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100903
904 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700905 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800906 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200907 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800908 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
909 else
910 ttl = key->ttl;
911 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100912
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200913 if (geneve->cfg.df == GENEVE_DF_SET) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100914 df = htons(IP_DF);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200915 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100916 struct ethhdr *eth = eth_hdr(skb);
917
918 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
919 df = htons(IP_DF);
920 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
921 struct iphdr *iph = ip_hdr(skb);
922
923 if (iph->frag_off & htons(IP_DF))
924 df = htons(IP_DF);
925 }
926 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400927 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800928
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800929 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800930 if (unlikely(err))
931 return err;
932
Pravin B Shelar039f5062015-12-24 14:34:54 -0800933 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200934 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800935 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800936 !(info->key.tun_flags & TUNNEL_CSUM));
937 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400938}
939
John W. Linville8ed66f02015-10-26 17:01:44 -0400940#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800941static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800942 struct geneve_dev *geneve,
943 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400944{
pravin shelar9b4437a2016-11-21 11:02:58 -0800945 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
946 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
947 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400948 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400949 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400950 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400951 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800952 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400953
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700954 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800955 if (IS_ERR(dst))
956 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400957
Stefano Brivio6b4f92a2018-10-12 23:53:59 +0200958 skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len);
Xin Long52a589d2017-12-25 14:43:58 +0800959
John W. Linville8ed66f02015-10-26 17:01:44 -0400960 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200961 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800962 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400963 ttl = key->ttl;
964 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100965 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800966 ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200967 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800968 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
969 else
970 ttl = key->ttl;
971 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400972 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800973 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800974 if (unlikely(err))
975 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100976
Pravin B Shelar039f5062015-12-24 14:34:54 -0800977 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800978 &fl6.saddr, &fl6.daddr, prio, ttl,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200979 info->key.label, sport, geneve->cfg.info.key.tp_dst,
pravin shelar9b4437a2016-11-21 11:02:58 -0800980 !(info->key.tun_flags & TUNNEL_CSUM));
981 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400982}
983#endif
984
985static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
986{
987 struct geneve_dev *geneve = netdev_priv(dev);
988 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800989 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400990
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200991 if (geneve->cfg.collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400992 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800993 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800994 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +0200995 dev_kfree_skb(skb);
996 dev->stats.tx_dropped++;
997 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -0800998 }
999 } else {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001000 info = &geneve->cfg.info;
pravin shelar9b4437a2016-11-21 11:02:58 -08001001 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001002
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001003 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001004#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001005 if (info->mode & IP_TUNNEL_INFO_IPV6)
1006 err = geneve6_xmit_skb(skb, dev, geneve, info);
1007 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001008#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001009 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001010 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001011
1012 if (likely(!err))
1013 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001014
pravin shelar9b4437a2016-11-21 11:02:58 -08001015 dev_kfree_skb(skb);
1016
1017 if (err == -ELOOP)
1018 dev->stats.collisions++;
1019 else if (err == -ENETUNREACH)
1020 dev->stats.tx_carrier_errors++;
1021
1022 dev->stats.tx_errors++;
1023 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -04001024}
1025
Jarod Wilson91572082016-10-20 13:55:20 -04001026static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001027{
Jarod Wilson91572082016-10-20 13:55:20 -04001028 if (new_mtu > dev->max_mtu)
1029 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001030 else if (new_mtu < dev->min_mtu)
1031 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001032
David Wragg55e5bfb2016-02-10 00:05:57 +00001033 dev->mtu = new_mtu;
1034 return 0;
1035}
1036
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001037static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1038{
1039 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1040 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001041
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001042 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001043 struct rtable *rt;
1044 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001045 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -08001046
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001047 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001048 if (IS_ERR(rt))
1049 return PTR_ERR(rt);
1050
1051 ip_rt_put(rt);
1052 info->key.u.ipv4.src = fl4.saddr;
1053#if IS_ENABLED(CONFIG_IPV6)
1054 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001055 struct dst_entry *dst;
1056 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001057 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -08001058
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001059 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001060 if (IS_ERR(dst))
1061 return PTR_ERR(dst);
1062
1063 dst_release(dst);
1064 info->key.u.ipv6.src = fl6.saddr;
1065#endif
1066 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001067 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001068 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001069
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001070 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1071 1, USHRT_MAX, true);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001072 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001073 return 0;
1074}
1075
John W. Linville2d07dc72015-05-13 12:57:30 -04001076static const struct net_device_ops geneve_netdev_ops = {
1077 .ndo_init = geneve_init,
1078 .ndo_uninit = geneve_uninit,
1079 .ndo_open = geneve_open,
1080 .ndo_stop = geneve_stop,
1081 .ndo_start_xmit = geneve_xmit,
1082 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001083 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001084 .ndo_validate_addr = eth_validate_addr,
1085 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001086 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001087};
1088
1089static void geneve_get_drvinfo(struct net_device *dev,
1090 struct ethtool_drvinfo *drvinfo)
1091{
1092 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1093 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1094}
1095
1096static const struct ethtool_ops geneve_ethtool_ops = {
1097 .get_drvinfo = geneve_get_drvinfo,
1098 .get_link = ethtool_op_get_link,
1099};
1100
1101/* Info for udev, that this is a virtual tunnel endpoint */
1102static struct device_type geneve_type = {
1103 .name = "geneve",
1104};
1105
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001106/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001107 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001108 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001109 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001110static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001111{
1112 struct net *net = dev_net(dev);
1113 struct geneve_net *gn = net_generic(net, geneve_net_id);
1114 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001115
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001116 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001117 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1118 if (push) {
1119 udp_tunnel_push_rx_port(dev, gs->sock,
1120 UDP_TUNNEL_TYPE_GENEVE);
1121 } else {
1122 udp_tunnel_drop_rx_port(dev, gs->sock,
1123 UDP_TUNNEL_TYPE_GENEVE);
1124 }
1125 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001126 rcu_read_unlock();
1127}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001128
John W. Linville2d07dc72015-05-13 12:57:30 -04001129/* Initialize the device structure. */
1130static void geneve_setup(struct net_device *dev)
1131{
1132 ether_setup(dev);
1133
1134 dev->netdev_ops = &geneve_netdev_ops;
1135 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001136 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001137
1138 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1139
John W. Linville2d07dc72015-05-13 12:57:30 -04001140 dev->features |= NETIF_F_LLTX;
1141 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1142 dev->features |= NETIF_F_RXCSUM;
1143 dev->features |= NETIF_F_GSO_SOFTWARE;
1144
John W. Linville2d07dc72015-05-13 12:57:30 -04001145 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1146 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001147
Jarod Wilson91572082016-10-20 13:55:20 -04001148 /* MTU range: 68 - (something less than 65535) */
1149 dev->min_mtu = ETH_MIN_MTU;
1150 /* The max_mtu calculation does not take account of GENEVE
1151 * options, to avoid excluding potentially valid
1152 * configurations. This will be further reduced by IPvX hdr size.
1153 */
1154 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1155
John W. Linville2d07dc72015-05-13 12:57:30 -04001156 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001157 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001158 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001159 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001160}
1161
1162static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1163 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001164 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001165 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001166 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001167 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001168 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001169 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001170 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001171 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1172 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1173 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001174 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001175 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001176};
1177
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001178static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1179 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001180{
1181 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001182 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1183 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1184 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001185 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001186 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001187
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001188 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1189 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1190 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001191 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001192 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001193 }
1194
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001195 if (!data) {
1196 NL_SET_ERR_MSG(extack,
1197 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001198 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001199 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001200
1201 if (data[IFLA_GENEVE_ID]) {
1202 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1203
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001204 if (vni >= GENEVE_N_VID) {
1205 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1206 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001207 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001208 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001209 }
1210
Stefano Brivioa025fb52018-11-08 12:19:19 +01001211 if (data[IFLA_GENEVE_DF]) {
1212 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1213
1214 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001215 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001216 "Invalid DF attribute");
1217 return -EINVAL;
1218 }
1219 }
1220
John W. Linville2d07dc72015-05-13 12:57:30 -04001221 return 0;
1222}
1223
Pravin B Shelar371bd102015-08-26 23:46:54 -07001224static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001225 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001226 bool *tun_on_same_port,
1227 bool *tun_collect_md)
1228{
pravin shelar9b4437a2016-11-21 11:02:58 -08001229 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001230
1231 *tun_on_same_port = false;
1232 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001233 list_for_each_entry(geneve, &gn->geneve_list, next) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001234 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1235 *tun_collect_md = geneve->cfg.collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001236 *tun_on_same_port = true;
1237 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001238 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1239 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1240 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001241 t = geneve;
1242 }
1243 return t;
1244}
1245
pravin shelar9b4437a2016-11-21 11:02:58 -08001246static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1247{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001248 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1249 info->key.ttl || info->key.label || info->key.tp_src ||
1250 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001251}
1252
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001253static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1254 struct ip_tunnel_info *b)
1255{
1256 if (ip_tunnel_info_af(a) == AF_INET)
1257 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1258 else
1259 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1260}
1261
Pravin B Shelare305ac62015-08-26 23:46:52 -07001262static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001263 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001264 const struct geneve_config *cfg)
John W. Linville2d07dc72015-05-13 12:57:30 -04001265{
1266 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001267 struct geneve_dev *t, *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001268 const struct ip_tunnel_info *info = &cfg->info;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001269 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001270 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001271
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001272 if (cfg->collect_md && !is_tnl_info_zero(info)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001273 NL_SET_ERR_MSG(extack,
1274 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001275 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001276 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001277
1278 geneve->net = net;
1279 geneve->dev = dev;
1280
pravin shelar9b4437a2016-11-21 11:02:58 -08001281 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001282 if (t)
1283 return -EBUSY;
1284
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001285 /* make enough headroom for basic scenario */
1286 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001287 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001288 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001289 dev->max_mtu -= sizeof(struct iphdr);
1290 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001291 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001292 dev->max_mtu -= sizeof(struct ipv6hdr);
1293 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001294 dev->needed_headroom = encap_len + ETH_HLEN;
1295
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001296 if (cfg->collect_md) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001297 if (tun_on_same_port) {
1298 NL_SET_ERR_MSG(extack,
1299 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001300 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001301 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001302 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001303 if (tun_collect_md) {
1304 NL_SET_ERR_MSG(extack,
1305 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001306 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001307 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001308 }
1309
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001310 dst_cache_reset(&geneve->cfg.info.dst_cache);
1311 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
Paolo Abeni468dfff2016-02-12 15:43:58 +01001312
John W. Linville2d07dc72015-05-13 12:57:30 -04001313 err = register_netdevice(dev);
1314 if (err)
1315 return err;
1316
1317 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001318 return 0;
1319}
1320
pravin shelar9b4437a2016-11-21 11:02:58 -08001321static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1322{
1323 memset(info, 0, sizeof(*info));
1324 info->key.tp_dst = htons(dst_port);
1325}
1326
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001327static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1328 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001329 struct geneve_config *cfg, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001330{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001331 int attrtype;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001332 struct ip_tunnel_info *info = &cfg->info;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001333
1334 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1335 NL_SET_ERR_MSG(extack,
1336 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001337 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001338 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001339
1340 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001341 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1342 attrtype = IFLA_GENEVE_REMOTE;
1343 goto change_notsup;
1344 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001345
1346 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001347 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001348
Dave Taht842841e2019-09-02 16:29:36 -07001349 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001350 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1351 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001352 return -EINVAL;
1353 }
1354 }
1355
pravin shelar9b4437a2016-11-21 11:02:58 -08001356 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001357#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001358 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1359 attrtype = IFLA_GENEVE_REMOTE6;
1360 goto change_notsup;
1361 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001362
1363 info->mode = IP_TUNNEL_INFO_IPV6;
1364 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001365 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001366
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001367 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001368 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001369 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1370 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001371 return -EINVAL;
1372 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001373 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001374 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1375 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001376 return -EINVAL;
1377 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001378 info->key.tun_flags |= TUNNEL_CSUM;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001379 cfg->use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001380#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001381 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1382 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001383 return -EPFNOSUPPORT;
1384#endif
1385 }
1386
1387 if (data[IFLA_GENEVE_ID]) {
1388 __u32 vni;
1389 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001390 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001391
1392 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1393 tvni[0] = (vni & 0x00ff0000) >> 16;
1394 tvni[1] = (vni & 0x0000ff00) >> 8;
1395 tvni[2] = vni & 0x000000ff;
1396
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001397 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001398 if (changelink && (tunid != info->key.tun_id)) {
1399 attrtype = IFLA_GENEVE_ID;
1400 goto change_notsup;
1401 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001402 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001403 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001404
Hangbin Liua97d97b2018-09-29 23:06:29 +08001405 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1406 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001407 cfg->ttl_inherit = true;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001408 else
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001409 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001410 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001411 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001412 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001413 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001414
Pravin B Shelare305ac62015-08-26 23:46:52 -07001415 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001416 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001417
Stefano Brivioa025fb52018-11-08 12:19:19 +01001418 if (data[IFLA_GENEVE_DF])
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001419 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001420
pravin shelar9b4437a2016-11-21 11:02:58 -08001421 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001422 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001423 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001424 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1425 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1426 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001427 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001428 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001429 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001430
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001431 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001432 if (changelink) {
1433 attrtype = IFLA_GENEVE_PORT;
1434 goto change_notsup;
1435 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001436 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1437 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001438
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001439 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001440 if (changelink) {
1441 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1442 goto change_notsup;
1443 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001444 cfg->collect_md = true;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001445 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001446
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001447 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001448 if (changelink) {
1449 attrtype = IFLA_GENEVE_UDP_CSUM;
1450 goto change_notsup;
1451 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001452 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1453 info->key.tun_flags |= TUNNEL_CSUM;
1454 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001455
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001456 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001457#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001458 if (changelink) {
1459 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1460 goto change_notsup;
1461 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001462 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1463 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001464#else
1465 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1466 "IPv6 support not enabled in the kernel");
1467 return -EPFNOSUPPORT;
1468#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001469 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001470
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001471 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001472#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001473 if (changelink) {
1474 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1475 goto change_notsup;
1476 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001477 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001478 cfg->use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001479#else
1480 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1481 "IPv6 support not enabled in the kernel");
1482 return -EPFNOSUPPORT;
1483#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001484 }
1485
1486 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001487change_notsup:
1488 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1489 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1490 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001491}
1492
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001493static void geneve_link_config(struct net_device *dev,
1494 struct ip_tunnel_info *info, struct nlattr *tb[])
1495{
1496 struct geneve_dev *geneve = netdev_priv(dev);
1497 int ldev_mtu = 0;
1498
1499 if (tb[IFLA_MTU]) {
1500 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1501 return;
1502 }
1503
1504 switch (ip_tunnel_info_af(info)) {
1505 case AF_INET: {
1506 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1507 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1508
1509 if (!IS_ERR(rt) && rt->dst.dev) {
1510 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1511 ip_rt_put(rt);
1512 }
1513 break;
1514 }
1515#if IS_ENABLED(CONFIG_IPV6)
1516 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001517 struct rt6_info *rt;
1518
1519 if (!__in6_dev_get(dev))
1520 break;
1521
1522 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1523 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001524
1525 if (rt && rt->dst.dev)
1526 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1527 ip6_rt_put(rt);
1528 break;
1529 }
1530#endif
1531 }
1532
1533 if (ldev_mtu <= 0)
1534 return;
1535
1536 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1537}
1538
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001539static int geneve_newlink(struct net *net, struct net_device *dev,
1540 struct nlattr *tb[], struct nlattr *data[],
1541 struct netlink_ext_ack *extack)
1542{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001543 struct geneve_config cfg = {
1544 .df = GENEVE_DF_UNSET,
1545 .use_udp6_rx_checksums = false,
1546 .ttl_inherit = false,
1547 .collect_md = false,
1548 };
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001549 int err;
1550
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001551 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1552 err = geneve_nl2info(tb, data, extack, &cfg, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001553 if (err)
1554 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001555
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001556 err = geneve_configure(net, dev, extack, &cfg);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001557 if (err)
1558 return err;
1559
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001560 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001561
1562 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001563}
1564
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001565/* Quiesces the geneve device data path for both TX and RX.
1566 *
1567 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1568 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1569 * to complete for the existing set of in-flight packets to be transmitted,
1570 * then we would have quiesced the transmit data path. All the future packets
1571 * will get dropped until we unquiesce the data path.
1572 *
1573 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1574 * if we set that to NULL under RCU and wait for synchronize_net() to
1575 * complete, then we would have quiesced the receive data path.
1576 */
1577static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1578 struct geneve_sock **gs6)
1579{
1580 *gs4 = rtnl_dereference(geneve->sock4);
1581 rcu_assign_pointer(geneve->sock4, NULL);
1582 if (*gs4)
1583 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1584#if IS_ENABLED(CONFIG_IPV6)
1585 *gs6 = rtnl_dereference(geneve->sock6);
1586 rcu_assign_pointer(geneve->sock6, NULL);
1587 if (*gs6)
1588 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1589#else
1590 *gs6 = NULL;
1591#endif
1592 synchronize_net();
1593}
1594
1595/* Resumes the geneve device data path for both TX and RX. */
1596static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1597 struct geneve_sock __maybe_unused *gs6)
1598{
1599 rcu_assign_pointer(geneve->sock4, gs4);
1600 if (gs4)
1601 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1602#if IS_ENABLED(CONFIG_IPV6)
1603 rcu_assign_pointer(geneve->sock6, gs6);
1604 if (gs6)
1605 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1606#endif
1607 synchronize_net();
1608}
1609
1610static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1611 struct nlattr *data[],
1612 struct netlink_ext_ack *extack)
1613{
1614 struct geneve_dev *geneve = netdev_priv(dev);
1615 struct geneve_sock *gs4, *gs6;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001616 struct geneve_config cfg;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001617 int err;
1618
1619 /* If the geneve device is configured for metadata (or externally
1620 * controlled, for example, OVS), then nothing can be changed.
1621 */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001622 if (geneve->cfg.collect_md)
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001623 return -EOPNOTSUPP;
1624
1625 /* Start with the existing info. */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001626 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1627 err = geneve_nl2info(tb, data, extack, &cfg, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001628 if (err)
1629 return err;
1630
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001631 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1632 dst_cache_reset(&cfg.info.dst_cache);
1633 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001634 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001635
1636 geneve_quiesce(geneve, &gs4, &gs6);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001637 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001638 geneve_unquiesce(geneve, gs4, gs6);
1639
1640 return 0;
1641}
1642
John W. Linville2d07dc72015-05-13 12:57:30 -04001643static void geneve_dellink(struct net_device *dev, struct list_head *head)
1644{
1645 struct geneve_dev *geneve = netdev_priv(dev);
1646
John W. Linville2d07dc72015-05-13 12:57:30 -04001647 list_del(&geneve->next);
1648 unregister_netdevice_queue(dev, head);
1649}
1650
1651static size_t geneve_get_size(const struct net_device *dev)
1652{
1653 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001654 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001655 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001656 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001657 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001658 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001659 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001660 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001661 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1662 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1663 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001664 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001665 0;
1666}
1667
1668static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1669{
1670 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001671 struct ip_tunnel_info *info = &geneve->cfg.info;
1672 bool ttl_inherit = geneve->cfg.ttl_inherit;
1673 bool metadata = geneve->cfg.collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001674 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001675 __u32 vni;
1676
pravin shelar9b4437a2016-11-21 11:02:58 -08001677 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1678 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001679 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1680 goto nla_put_failure;
1681
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001682 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001683 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001684 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001685 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001686 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1687 !!(info->key.tun_flags & TUNNEL_CSUM)))
1688 goto nla_put_failure;
1689
John W. Linville8ed66f02015-10-26 17:01:44 -04001690#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001691 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001692 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001693 &info->key.u.ipv6.dst))
1694 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001695 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1696 !(info->key.tun_flags & TUNNEL_CSUM)))
1697 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001698#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001699 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001700
pravin shelar9b4437a2016-11-21 11:02:58 -08001701 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1702 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1703 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001704 goto nla_put_failure;
1705
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001706 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
Stefano Brivioa025fb52018-11-08 12:19:19 +01001707 goto nla_put_failure;
1708
pravin shelar9b4437a2016-11-21 11:02:58 -08001709 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001710 goto nla_put_failure;
1711
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001712 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001713 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001714
Hangbin Liuf9094b72017-11-23 11:27:24 +08001715#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001716 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001717 !geneve->cfg.use_udp6_rx_checksums))
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001718 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001719#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001720
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001721 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1722 goto nla_put_failure;
1723
John W. Linville2d07dc72015-05-13 12:57:30 -04001724 return 0;
1725
1726nla_put_failure:
1727 return -EMSGSIZE;
1728}
1729
1730static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1731 .kind = "geneve",
1732 .maxtype = IFLA_GENEVE_MAX,
1733 .policy = geneve_policy,
1734 .priv_size = sizeof(struct geneve_dev),
1735 .setup = geneve_setup,
1736 .validate = geneve_validate,
1737 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001738 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001739 .dellink = geneve_dellink,
1740 .get_size = geneve_get_size,
1741 .fill_info = geneve_fill_info,
1742};
1743
Pravin B Shelare305ac62015-08-26 23:46:52 -07001744struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1745 u8 name_assign_type, u16 dst_port)
1746{
1747 struct nlattr *tb[IFLA_MAX + 1];
1748 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001749 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001750 int err;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001751 struct geneve_config cfg = {
1752 .df = GENEVE_DF_UNSET,
1753 .use_udp6_rx_checksums = true,
1754 .ttl_inherit = false,
1755 .collect_md = true,
1756 };
Pravin B Shelare305ac62015-08-26 23:46:52 -07001757
1758 memset(tb, 0, sizeof(tb));
1759 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001760 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001761 if (IS_ERR(dev))
1762 return dev;
1763
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001764 init_tnl_info(&cfg.info, dst_port);
1765 err = geneve_configure(net, dev, NULL, &cfg);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001766 if (err) {
1767 free_netdev(dev);
1768 return ERR_PTR(err);
1769 }
David Wragg7e059152016-02-10 00:05:58 +00001770
1771 /* openvswitch users expect packet sizes to be unrestricted,
1772 * so set the largest MTU we can.
1773 */
Jarod Wilson91572082016-10-20 13:55:20 -04001774 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001775 if (err)
1776 goto err;
1777
Nicolas Dichtel41009482016-06-13 10:31:07 +02001778 err = rtnl_configure_link(dev, NULL);
1779 if (err < 0)
1780 goto err;
1781
Pravin B Shelare305ac62015-08-26 23:46:52 -07001782 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001783err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001784 geneve_dellink(dev, &list_kill);
1785 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001786 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001787}
1788EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1789
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001790static int geneve_netdevice_event(struct notifier_block *unused,
1791 unsigned long event, void *ptr)
1792{
1793 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1794
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001795 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001796 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001797 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001798 } else if (event == NETDEV_UNREGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07001799 if (!dev->udp_tunnel_nic_info)
1800 geneve_offload_rx_ports(dev, false);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001801 } else if (event == NETDEV_REGISTER) {
Jakub Kicinskicc4e3832020-07-09 17:42:46 -07001802 if (!dev->udp_tunnel_nic_info)
1803 geneve_offload_rx_ports(dev, true);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001804 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001805
1806 return NOTIFY_DONE;
1807}
1808
1809static struct notifier_block geneve_notifier_block __read_mostly = {
1810 .notifier_call = geneve_netdevice_event,
1811};
1812
John W. Linville2d07dc72015-05-13 12:57:30 -04001813static __net_init int geneve_init_net(struct net *net)
1814{
1815 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001816
1817 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001818 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001819 return 0;
1820}
1821
Haishuang Yan2843a252017-12-16 17:54:50 +08001822static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001823{
1824 struct geneve_net *gn = net_generic(net, geneve_net_id);
1825 struct geneve_dev *geneve, *next;
1826 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001827
1828 /* gather any geneve devices that were moved into this ns */
1829 for_each_netdev_safe(net, dev, aux)
1830 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001831 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001832
1833 /* now gather any other geneve devices that were created in this ns */
1834 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1835 /* If geneve->dev is in the same netns, it was already added
1836 * to the list by the previous loop.
1837 */
1838 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001839 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001840 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001841}
1842
1843static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1844{
1845 struct net *net;
1846 LIST_HEAD(list);
1847
1848 rtnl_lock();
1849 list_for_each_entry(net, net_list, exit_list)
1850 geneve_destroy_tunnels(net, &list);
1851
John W. Linville2d07dc72015-05-13 12:57:30 -04001852 /* unregister the devices gathered above */
1853 unregister_netdevice_many(&list);
1854 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001855
1856 list_for_each_entry(net, net_list, exit_list) {
1857 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1858
1859 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1860 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001861}
1862
1863static struct pernet_operations geneve_net_ops = {
1864 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001865 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001866 .id = &geneve_net_id,
1867 .size = sizeof(struct geneve_net),
1868};
1869
1870static int __init geneve_init_module(void)
1871{
1872 int rc;
1873
1874 rc = register_pernet_subsys(&geneve_net_ops);
1875 if (rc)
1876 goto out1;
1877
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001878 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001879 if (rc)
1880 goto out2;
1881
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001882 rc = rtnl_link_register(&geneve_link_ops);
1883 if (rc)
1884 goto out3;
1885
John W. Linville2d07dc72015-05-13 12:57:30 -04001886 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001887out3:
1888 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001889out2:
1890 unregister_pernet_subsys(&geneve_net_ops);
1891out1:
1892 return rc;
1893}
1894late_initcall(geneve_init_module);
1895
1896static void __exit geneve_cleanup_module(void)
1897{
1898 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001899 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001900 unregister_pernet_subsys(&geneve_net_ops);
1901}
1902module_exit(geneve_cleanup_module);
1903
1904MODULE_LICENSE("GPL");
1905MODULE_VERSION(GENEVE_NETDEV_VER);
1906MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1907MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1908MODULE_ALIAS_RTNL_LINK("geneve");