blob: f6404074b7b053e2af682c7e25ad36cec21ebe3b [file] [log] [blame]
John W. Linville2d07dc72015-05-13 12:57:30 -04001/*
2 * GENEVE: Generic Network Virtualization Encapsulation
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040015#include <linux/etherdevice.h>
16#include <linux/hash.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070017#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070018#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040019#include <net/rtnetlink.h>
20#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070021#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040022
23#define GENEVE_NETDEV_VER "0.6"
24
25#define GENEVE_UDP_PORT 6081
26
27#define GENEVE_N_VID (1u << 24)
28#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
29
30#define VNI_HASH_BITS 10
31#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
32
33static bool log_ecn_error = true;
34module_param(log_ecn_error, bool, 0644);
35MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
36
Pravin B Shelar371bd102015-08-26 23:46:54 -070037#define GENEVE_VER 0
38#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
39
John W. Linville2d07dc72015-05-13 12:57:30 -040040/* per-network namespace private data for this module */
41struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070042 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040044};
45
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030046static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070047
Jiri Benc4b4c21f2017-07-02 19:00:58 +020048struct geneve_dev_node {
49 struct hlist_node hlist;
50 struct geneve_dev *geneve;
51};
52
John W. Linville2d07dc72015-05-13 12:57:30 -040053/* Pseudo network device */
54struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020055 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
56#if IS_ENABLED(CONFIG_IPV6)
57 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
58#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040059 struct net *net; /* netns for packet i/o */
60 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelar9b4437a2016-11-21 11:02:58 -080061 struct ip_tunnel_info info;
pravin shelarfceb9c32016-10-28 09:59:16 -070062 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040063#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070064 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040065#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040066 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070067 struct gro_cells gro_cells;
pravin shelar9b4437a2016-11-21 11:02:58 -080068 bool collect_md;
69 bool use_udp6_rx_checksums;
John W. Linville2d07dc72015-05-13 12:57:30 -040070};
71
Pravin B Shelar371bd102015-08-26 23:46:54 -070072struct geneve_sock {
73 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070074 struct list_head list;
75 struct socket *sock;
76 struct rcu_head rcu;
77 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070078 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070079};
John W. Linville2d07dc72015-05-13 12:57:30 -040080
81static inline __u32 geneve_net_vni_hash(u8 vni[3])
82{
83 __u32 vnid;
84
85 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
86 return hash_32(vnid, VNI_HASH_BITS);
87}
88
Pravin B Shelare305ac62015-08-26 23:46:52 -070089static __be64 vni_to_tunnel_id(const __u8 *vni)
90{
91#ifdef __BIG_ENDIAN
92 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
93#else
94 return (__force __be64)(((__force u64)vni[0] << 40) |
95 ((__force u64)vni[1] << 48) |
96 ((__force u64)vni[2] << 56));
97#endif
98}
99
pravin shelar9b4437a2016-11-21 11:02:58 -0800100/* Convert 64 bit tunnel ID to 24 bit VNI. */
101static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
102{
103#ifdef __BIG_ENDIAN
104 vni[0] = (__force __u8)(tun_id >> 16);
105 vni[1] = (__force __u8)(tun_id >> 8);
106 vni[2] = (__force __u8)tun_id;
107#else
108 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
109 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
110 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
111#endif
112}
113
pravin shelar2e0b26e2016-11-21 11:03:01 -0800114static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
115{
116#ifdef __BIG_ENDIAN
117 return (vni[0] == tun_id[2]) &&
118 (vni[1] == tun_id[1]) &&
119 (vni[2] == tun_id[0]);
120#else
121 return !memcmp(vni, &tun_id[5], 3);
122#endif
123}
124
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100125static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
126{
127 return gs->sock->sk->sk_family;
128}
129
Pravin B Shelar66d47002015-08-26 23:46:55 -0700130static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700131 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400132{
John W. Linville2d07dc72015-05-13 12:57:30 -0400133 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200134 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400135 __u32 hash;
136
John W. Linville2d07dc72015-05-13 12:57:30 -0400137 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700138 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700139 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200140 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
141 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
142 addr == node->geneve->info.key.u.ipv4.dst)
143 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400144 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700145 return NULL;
146}
147
John W. Linville8ed66f02015-10-26 17:01:44 -0400148#if IS_ENABLED(CONFIG_IPV6)
149static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
150 struct in6_addr addr6, u8 vni[])
151{
152 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200153 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400154 __u32 hash;
155
156 /* Find the device for this VNI */
157 hash = geneve_net_vni_hash(vni);
158 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200159 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
160 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
161 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst))
162 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400163 }
164 return NULL;
165}
166#endif
167
Pravin B Shelar371bd102015-08-26 23:46:54 -0700168static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
169{
170 return (struct genevehdr *)(udp_hdr(skb) + 1);
171}
172
Jiri Benc9fc47542016-02-18 11:22:50 +0100173static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
174 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700175{
John W. Linville8ed66f02015-10-26 17:01:44 -0400176 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800177 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700178
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100179 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100180 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800181 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100182
John W. Linville8ed66f02015-10-26 17:01:44 -0400183 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700184
John W. Linville8ed66f02015-10-26 17:01:44 -0400185 if (gs->collect_md) {
186 vni = zero_vni;
187 addr = 0;
188 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100189 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400190 addr = iph->saddr;
191 }
192
Jiri Benc9fc47542016-02-18 11:22:50 +0100193 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400194#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100195 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800196 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100197 struct ipv6hdr *ip6h;
198 struct in6_addr addr6;
199
John W. Linville8ed66f02015-10-26 17:01:44 -0400200 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
201
202 if (gs->collect_md) {
203 vni = zero_vni;
204 addr6 = zero_addr6;
205 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100206 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400207 addr6 = ip6h->saddr;
208 }
209
Jiri Benc9fc47542016-02-18 11:22:50 +0100210 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400211#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700212 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100213 return NULL;
214}
215
216/* geneve receive/decap routine */
217static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
218 struct sk_buff *skb)
219{
220 struct genevehdr *gnvh = geneve_hdr(skb);
221 struct metadata_dst *tun_dst = NULL;
222 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700223 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100224 int err = 0;
225 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400226
Pravin B Shelar371bd102015-08-26 23:46:54 -0700227 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700228 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229
230 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
231 (gnvh->oam ? TUNNEL_OAM : 0) |
232 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
233
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100234 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700235 vni_to_tunnel_id(gnvh->vni),
236 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700237 if (!tun_dst) {
238 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700239 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700240 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700241 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700242 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
243 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700244 } else {
245 /* Drop packets w/ critical options,
246 * since we don't support any...
247 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700248 if (gnvh->critical) {
249 geneve->dev->stats.rx_frame_errors++;
250 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700251 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700252 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700253 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400254
255 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400256 skb->protocol = eth_type_trans(skb, geneve->dev);
257 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
258
Pravin B Shelare305ac62015-08-26 23:46:52 -0700259 if (tun_dst)
260 skb_dst_set(skb, &tun_dst->dst);
261
John W. Linville2d07dc72015-05-13 12:57:30 -0400262 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700263 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
264 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400265 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700266 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400267
Jiri Benc9fc47542016-02-18 11:22:50 +0100268 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400269 skb_reset_network_header(skb);
270
Jiri Benc9fc47542016-02-18 11:22:50 +0100271 if (geneve_get_sk_family(gs) == AF_INET)
272 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400273#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100274 else
275 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400276#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400277
278 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400279 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100280 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400281 net_info_ratelimited("non-ECT from %pI4 "
282 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100283 &((struct iphdr *)oiph)->saddr,
284 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400285#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100286 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400287 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100288 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400289#endif
290 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400291 if (err > 1) {
292 ++geneve->dev->stats.rx_frame_errors;
293 ++geneve->dev->stats.rx_errors;
294 goto drop;
295 }
296 }
297
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700298 len = skb->len;
299 err = gro_cells_receive(&geneve->gro_cells, skb);
300 if (likely(err == NET_RX_SUCCESS)) {
301 stats = this_cpu_ptr(geneve->dev->tstats);
302 u64_stats_update_begin(&stats->syncp);
303 stats->rx_packets++;
304 stats->rx_bytes += len;
305 u64_stats_update_end(&stats->syncp);
306 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400307 return;
308drop:
309 /* Consume bad packet */
310 kfree_skb(skb);
311}
312
313/* Setup stats when device is created */
314static int geneve_init(struct net_device *dev)
315{
Jesse Gross8e816df2015-08-28 16:54:40 -0700316 struct geneve_dev *geneve = netdev_priv(dev);
317 int err;
318
John W. Linville2d07dc72015-05-13 12:57:30 -0400319 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
320 if (!dev->tstats)
321 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700322
323 err = gro_cells_init(&geneve->gro_cells, dev);
324 if (err) {
325 free_percpu(dev->tstats);
326 return err;
327 }
328
pravin shelar9b4437a2016-11-21 11:02:58 -0800329 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100330 if (err) {
331 free_percpu(dev->tstats);
332 gro_cells_destroy(&geneve->gro_cells);
333 return err;
334 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400335 return 0;
336}
337
338static void geneve_uninit(struct net_device *dev)
339{
Jesse Gross8e816df2015-08-28 16:54:40 -0700340 struct geneve_dev *geneve = netdev_priv(dev);
341
pravin shelar9b4437a2016-11-21 11:02:58 -0800342 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700343 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400344 free_percpu(dev->tstats);
345}
346
Pravin B Shelar371bd102015-08-26 23:46:54 -0700347/* Callback from net/ipv4/udp.c to receive packets */
348static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
349{
350 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100351 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700352 struct geneve_sock *gs;
353 int opts_len;
354
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700355 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700356 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200357 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700358
359 /* Return packets with reserved bits set */
360 geneveh = geneve_hdr(skb);
361 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200362 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700363
364 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200365 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700366
Jiri Benc9fc47542016-02-18 11:22:50 +0100367 gs = rcu_dereference_sk_user_data(sk);
368 if (!gs)
369 goto drop;
370
371 geneve = geneve_lookup_skb(gs, skb);
372 if (!geneve)
373 goto drop;
374
Pravin B Shelar371bd102015-08-26 23:46:54 -0700375 opts_len = geneveh->opt_len * 4;
376 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100377 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700378 !net_eq(geneve->net, dev_net(geneve->dev)))) {
379 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700380 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700381 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700382
Jiri Benc9fc47542016-02-18 11:22:50 +0100383 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384 return 0;
385
386drop:
387 /* Consume bad packet */
388 kfree_skb(skb);
389 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700390}
391
392static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800393 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700394{
395 struct socket *sock;
396 struct udp_port_cfg udp_conf;
397 int err;
398
399 memset(&udp_conf, 0, sizeof(udp_conf));
400
401 if (ipv6) {
402 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400403 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800404 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700405 } else {
406 udp_conf.family = AF_INET;
407 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
408 }
409
410 udp_conf.local_udp_port = port;
411
412 /* Open UDP socket */
413 err = udp_sock_create(net, &udp_conf, &sock);
414 if (err < 0)
415 return ERR_PTR(err);
416
417 return sock;
418}
419
Pravin B Shelar371bd102015-08-26 23:46:54 -0700420static int geneve_hlen(struct genevehdr *gh)
421{
422 return sizeof(*gh) + gh->opt_len * 4;
423}
424
Tom Herbert4a0090a2016-04-05 08:22:55 -0700425static struct sk_buff **geneve_gro_receive(struct sock *sk,
426 struct sk_buff **head,
427 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700428{
429 struct sk_buff *p, **pp = NULL;
430 struct genevehdr *gh, *gh2;
431 unsigned int hlen, gh_len, off_gnv;
432 const struct packet_offload *ptype;
433 __be16 type;
434 int flush = 1;
435
436 off_gnv = skb_gro_offset(skb);
437 hlen = off_gnv + sizeof(*gh);
438 gh = skb_gro_header_fast(skb, off_gnv);
439 if (skb_gro_header_hard(skb, hlen)) {
440 gh = skb_gro_header_slow(skb, hlen, off_gnv);
441 if (unlikely(!gh))
442 goto out;
443 }
444
445 if (gh->ver != GENEVE_VER || gh->oam)
446 goto out;
447 gh_len = geneve_hlen(gh);
448
449 hlen = off_gnv + gh_len;
450 if (skb_gro_header_hard(skb, hlen)) {
451 gh = skb_gro_header_slow(skb, hlen, off_gnv);
452 if (unlikely(!gh))
453 goto out;
454 }
455
Pravin B Shelar371bd102015-08-26 23:46:54 -0700456 for (p = *head; p; p = p->next) {
457 if (!NAPI_GRO_CB(p)->same_flow)
458 continue;
459
460 gh2 = (struct genevehdr *)(p->data + off_gnv);
461 if (gh->opt_len != gh2->opt_len ||
462 memcmp(gh, gh2, gh_len)) {
463 NAPI_GRO_CB(p)->same_flow = 0;
464 continue;
465 }
466 }
467
468 type = gh->proto_type;
469
470 rcu_read_lock();
471 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800472 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700473 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700474
475 skb_gro_pull(skb, gh_len);
476 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200477 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800478 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700479
480out_unlock:
481 rcu_read_unlock();
482out:
483 NAPI_GRO_CB(skb)->flush |= flush;
484
485 return pp;
486}
487
Tom Herbert4a0090a2016-04-05 08:22:55 -0700488static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
489 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700490{
491 struct genevehdr *gh;
492 struct packet_offload *ptype;
493 __be16 type;
494 int gh_len;
495 int err = -ENOSYS;
496
Pravin B Shelar371bd102015-08-26 23:46:54 -0700497 gh = (struct genevehdr *)(skb->data + nhoff);
498 gh_len = geneve_hlen(gh);
499 type = gh->proto_type;
500
501 rcu_read_lock();
502 ptype = gro_find_complete_by_type(type);
503 if (ptype)
504 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
505
506 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700507
508 skb_set_inner_mac_header(skb, nhoff + gh_len);
509
Pravin B Shelar371bd102015-08-26 23:46:54 -0700510 return err;
511}
512
513/* Create new listen socket if needed */
514static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800515 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700516{
517 struct geneve_net *gn = net_generic(net, geneve_net_id);
518 struct geneve_sock *gs;
519 struct socket *sock;
520 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700521 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700522
523 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
524 if (!gs)
525 return ERR_PTR(-ENOMEM);
526
pravin shelar9b4437a2016-11-21 11:02:58 -0800527 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528 if (IS_ERR(sock)) {
529 kfree(gs);
530 return ERR_CAST(sock);
531 }
532
533 gs->sock = sock;
534 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700535 for (h = 0; h < VNI_HASH_SIZE; ++h)
536 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700537
538 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700539 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700540
541 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700542 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700543 tunnel_cfg.sk_user_data = gs;
544 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700545 tunnel_cfg.gro_receive = geneve_gro_receive;
546 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700547 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
548 tunnel_cfg.encap_destroy = NULL;
549 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700550 list_add(&gs->list, &gn->sock_list);
551 return gs;
552}
553
John W. Linville8ed66f02015-10-26 17:01:44 -0400554static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700555{
John W. Linville8ed66f02015-10-26 17:01:44 -0400556 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700557 return;
558
559 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700560 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700561 udp_tunnel_sock_release(gs->sock);
562 kfree_rcu(gs, rcu);
563}
564
John W. Linville8ed66f02015-10-26 17:01:44 -0400565static void geneve_sock_release(struct geneve_dev *geneve)
566{
pravin shelarfceb9c32016-10-28 09:59:16 -0700567 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400568#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700569 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
570
571 rcu_assign_pointer(geneve->sock6, NULL);
572#endif
573
574 rcu_assign_pointer(geneve->sock4, NULL);
575 synchronize_net();
576
577 __geneve_sock_release(gs4);
578#if IS_ENABLED(CONFIG_IPV6)
579 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400580#endif
581}
582
Pravin B Shelar371bd102015-08-26 23:46:54 -0700583static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400584 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700585 __be16 dst_port)
586{
587 struct geneve_sock *gs;
588
589 list_for_each_entry(gs, &gn->sock_list, list) {
590 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100591 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700592 return gs;
593 }
594 }
595 return NULL;
596}
597
John W. Linville8ed66f02015-10-26 17:01:44 -0400598static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400599{
John W. Linville2d07dc72015-05-13 12:57:30 -0400600 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700601 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200602 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400603 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800604 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700605 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400606
pravin shelar9b4437a2016-11-21 11:02:58 -0800607 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700608 if (gs) {
609 gs->refcnt++;
610 goto out;
611 }
612
pravin shelar9b4437a2016-11-21 11:02:58 -0800613 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
614 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400615 if (IS_ERR(gs))
616 return PTR_ERR(gs);
617
Pravin B Shelar371bd102015-08-26 23:46:54 -0700618out:
619 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400620#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200621 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700622 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200623 node = &geneve->hlist6;
624 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400625#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200626 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700627 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200628 node = &geneve->hlist4;
629 }
630 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700631
pravin shelar9b4437a2016-11-21 11:02:58 -0800632 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
633 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200634 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400635 return 0;
636}
637
John W. Linville8ed66f02015-10-26 17:01:44 -0400638static int geneve_open(struct net_device *dev)
639{
640 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800641 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400642 bool metadata = geneve->collect_md;
643 int ret = 0;
644
John W. Linville8ed66f02015-10-26 17:01:44 -0400645#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400646 if (ipv6 || metadata)
647 ret = geneve_sock_add(geneve, true);
648#endif
649 if (!ret && (!ipv6 || metadata))
650 ret = geneve_sock_add(geneve, false);
651 if (ret < 0)
652 geneve_sock_release(geneve);
653
654 return ret;
655}
656
John W. Linville2d07dc72015-05-13 12:57:30 -0400657static int geneve_stop(struct net_device *dev)
658{
659 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400660
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200661 hlist_del_init_rcu(&geneve->hlist4.hlist);
662#if IS_ENABLED(CONFIG_IPV6)
663 hlist_del_init_rcu(&geneve->hlist6.hlist);
664#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400665 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400666 return 0;
667}
668
John W. Linville8ed66f02015-10-26 17:01:44 -0400669static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800670 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400671{
672 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800673 geneveh->opt_len = info->options_len / 4;
674 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
675 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400676 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800677 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400678 geneveh->proto_type = htons(ETH_P_TEB);
679 geneveh->rsvd2 = 0;
680
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800681 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400682}
683
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800684static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
685 const struct ip_tunnel_info *info,
686 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700687{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800688 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700689 struct genevehdr *gnvh;
690 int min_headroom;
691 int err;
692
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800693 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 skb_scrub_packet(skb, xnet);
695
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800696 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
697 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400699 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400700 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400701
Alexander Duyckaed069d2016-04-14 15:33:37 -0400702 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300703 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400704 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400705
Johannes Bergd58ff352017-06-16 14:29:23 +0200706 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800707 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400708 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
709 return 0;
710
711free_dst:
712 dst_release(dst);
713 return err;
714}
John W. Linville8ed66f02015-10-26 17:01:44 -0400715
716static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
717 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700718 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400719 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800720 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700721{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100722 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700723 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100724 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700725 struct rtable *rt = NULL;
726 __u8 tos;
727
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700728 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700729 return ERR_PTR(-EIO);
730
Pravin B Shelare305ac62015-08-26 23:46:52 -0700731 memset(fl4, 0, sizeof(*fl4));
732 fl4->flowi4_mark = skb->mark;
733 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800734 fl4->daddr = info->key.u.ipv4.dst;
735 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700736
pravin shelar9b4437a2016-11-21 11:02:58 -0800737 tos = info->key.tos;
738 if ((tos == 1) && !geneve->collect_md) {
739 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
740 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100741 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800742 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100743
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800744 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100745 if (use_cache) {
746 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
747 if (rt)
748 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700749 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700750 rt = ip_route_output_key(geneve->net, fl4);
751 if (IS_ERR(rt)) {
752 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700753 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700754 }
755 if (rt->dst.dev == dev) { /* is this necessary? */
756 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700757 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700758 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700759 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100760 if (use_cache)
761 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700762 return rt;
763}
764
John W. Linville8ed66f02015-10-26 17:01:44 -0400765#if IS_ENABLED(CONFIG_IPV6)
766static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
767 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700768 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400769 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800770 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400771{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100772 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400773 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400774 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100775 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400776 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400777
pravin shelarfceb9c32016-10-28 09:59:16 -0700778 if (!gs6)
779 return ERR_PTR(-EIO);
780
John W. Linville8ed66f02015-10-26 17:01:44 -0400781 memset(fl6, 0, sizeof(*fl6));
782 fl6->flowi6_mark = skb->mark;
783 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800784 fl6->daddr = info->key.u.ipv6.dst;
785 fl6->saddr = info->key.u.ipv6.src;
786 prio = info->key.tos;
787 if ((prio == 1) && !geneve->collect_md) {
788 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
789 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100790 }
791
pravin shelar9b4437a2016-11-21 11:02:58 -0800792 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
793 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800794 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100795 if (use_cache) {
796 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
797 if (dst)
798 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400799 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400800 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
801 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
802 return ERR_PTR(-ENETUNREACH);
803 }
804 if (dst->dev == dev) { /* is this necessary? */
805 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
806 dst_release(dst);
807 return ERR_PTR(-ELOOP);
808 }
809
Paolo Abeni468dfff2016-02-12 15:43:58 +0100810 if (use_cache)
811 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400812 return dst;
813}
814#endif
815
pravin shelar9b4437a2016-11-21 11:02:58 -0800816static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800817 struct geneve_dev *geneve,
818 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700819{
pravin shelar9b4437a2016-11-21 11:02:58 -0800820 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
821 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
822 const struct ip_tunnel_key *key = &info->key;
823 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400824 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400825 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700826 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700827 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800828 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400829
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700830 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800831 if (IS_ERR(rt))
832 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700833
834 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800835 if (geneve->collect_md) {
836 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700837 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700838 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800839 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
840 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400841 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800842 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
843
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800844 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800845 if (unlikely(err))
846 return err;
847
Pravin B Shelar039f5062015-12-24 14:34:54 -0800848 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800849 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800850 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800851 !(info->key.tun_flags & TUNNEL_CSUM));
852 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400853}
854
John W. Linville8ed66f02015-10-26 17:01:44 -0400855#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800856static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800857 struct geneve_dev *geneve,
858 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400859{
pravin shelar9b4437a2016-11-21 11:02:58 -0800860 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
861 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
862 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400863 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400864 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400865 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400866 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800867 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400868
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700869 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800870 if (IS_ERR(dst))
871 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400872
873 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800874 if (geneve->collect_md) {
875 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400876 ttl = key->ttl;
877 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100878 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800879 ip_hdr(skb), skb);
880 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400881 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800882 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 if (unlikely(err))
884 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100885
Pravin B Shelar039f5062015-12-24 14:34:54 -0800886 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800887 &fl6.saddr, &fl6.daddr, prio, ttl,
888 info->key.label, sport, geneve->info.key.tp_dst,
889 !(info->key.tun_flags & TUNNEL_CSUM));
890 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400891}
892#endif
893
894static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
895{
896 struct geneve_dev *geneve = netdev_priv(dev);
897 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800898 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400899
pravin shelar9b4437a2016-11-21 11:02:58 -0800900 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400901 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800902 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
903 err = -EINVAL;
904 netdev_dbg(dev, "no tunnel metadata\n");
905 goto tx_error;
906 }
907 } else {
908 info = &geneve->info;
909 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400910
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800911 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400912#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800913 if (info->mode & IP_TUNNEL_INFO_IPV6)
914 err = geneve6_xmit_skb(skb, dev, geneve, info);
915 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400916#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800917 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800918 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800919
920 if (likely(!err))
921 return NETDEV_TX_OK;
922tx_error:
923 dev_kfree_skb(skb);
924
925 if (err == -ELOOP)
926 dev->stats.collisions++;
927 else if (err == -ENETUNREACH)
928 dev->stats.tx_carrier_errors++;
929
930 dev->stats.tx_errors++;
931 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400932}
933
Jarod Wilson91572082016-10-20 13:55:20 -0400934static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000935{
Jarod Wilson91572082016-10-20 13:55:20 -0400936 /* Only possible if called internally, ndo_change_mtu path's new_mtu
937 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
David Wragg55e5bfb2016-02-10 00:05:57 +0000938 */
Jarod Wilson91572082016-10-20 13:55:20 -0400939 if (new_mtu > dev->max_mtu)
940 new_mtu = dev->max_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000941
David Wragg55e5bfb2016-02-10 00:05:57 +0000942 dev->mtu = new_mtu;
943 return 0;
944}
945
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700946static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
947{
948 struct ip_tunnel_info *info = skb_tunnel_info(skb);
949 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700950
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400951 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800952 struct rtable *rt;
953 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700954 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -0800955
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700956 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400957 if (IS_ERR(rt))
958 return PTR_ERR(rt);
959
960 ip_rt_put(rt);
961 info->key.u.ipv4.src = fl4.saddr;
962#if IS_ENABLED(CONFIG_IPV6)
963 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800964 struct dst_entry *dst;
965 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700966 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -0800967
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700968 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400969 if (IS_ERR(dst))
970 return PTR_ERR(dst);
971
972 dst_release(dst);
973 info->key.u.ipv6.src = fl6.saddr;
974#endif
975 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700976 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400977 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700978
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700979 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
980 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800981 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700982 return 0;
983}
984
John W. Linville2d07dc72015-05-13 12:57:30 -0400985static const struct net_device_ops geneve_netdev_ops = {
986 .ndo_init = geneve_init,
987 .ndo_uninit = geneve_uninit,
988 .ndo_open = geneve_open,
989 .ndo_stop = geneve_stop,
990 .ndo_start_xmit = geneve_xmit,
991 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +0000992 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -0400993 .ndo_validate_addr = eth_validate_addr,
994 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700995 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -0400996};
997
998static void geneve_get_drvinfo(struct net_device *dev,
999 struct ethtool_drvinfo *drvinfo)
1000{
1001 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1002 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1003}
1004
1005static const struct ethtool_ops geneve_ethtool_ops = {
1006 .get_drvinfo = geneve_get_drvinfo,
1007 .get_link = ethtool_op_get_link,
1008};
1009
1010/* Info for udev, that this is a virtual tunnel endpoint */
1011static struct device_type geneve_type = {
1012 .name = "geneve",
1013};
1014
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001015/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001016 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001017 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001018 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001019static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001020{
1021 struct net *net = dev_net(dev);
1022 struct geneve_net *gn = net_generic(net, geneve_net_id);
1023 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001024
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001025 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001026 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1027 if (push) {
1028 udp_tunnel_push_rx_port(dev, gs->sock,
1029 UDP_TUNNEL_TYPE_GENEVE);
1030 } else {
1031 udp_tunnel_drop_rx_port(dev, gs->sock,
1032 UDP_TUNNEL_TYPE_GENEVE);
1033 }
1034 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001035 rcu_read_unlock();
1036}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001037
John W. Linville2d07dc72015-05-13 12:57:30 -04001038/* Initialize the device structure. */
1039static void geneve_setup(struct net_device *dev)
1040{
1041 ether_setup(dev);
1042
1043 dev->netdev_ops = &geneve_netdev_ops;
1044 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001045 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001046
1047 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1048
John W. Linville2d07dc72015-05-13 12:57:30 -04001049 dev->features |= NETIF_F_LLTX;
1050 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1051 dev->features |= NETIF_F_RXCSUM;
1052 dev->features |= NETIF_F_GSO_SOFTWARE;
1053
John W. Linville2d07dc72015-05-13 12:57:30 -04001054 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1055 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001056
Jarod Wilson91572082016-10-20 13:55:20 -04001057 /* MTU range: 68 - (something less than 65535) */
1058 dev->min_mtu = ETH_MIN_MTU;
1059 /* The max_mtu calculation does not take account of GENEVE
1060 * options, to avoid excluding potentially valid
1061 * configurations. This will be further reduced by IPvX hdr size.
1062 */
1063 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1064
John W. Linville2d07dc72015-05-13 12:57:30 -04001065 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001066 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001067 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001068 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001069}
1070
1071static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1072 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1073 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001074 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001075 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001076 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001077 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001078 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001079 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001080 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1081 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1082 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001083};
1084
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001085static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1086 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001087{
1088 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001089 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1090 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1091 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001092 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001093 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001094
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001095 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1096 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1097 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001098 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001099 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001100 }
1101
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001102 if (!data) {
1103 NL_SET_ERR_MSG(extack,
1104 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001105 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001106 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001107
1108 if (data[IFLA_GENEVE_ID]) {
1109 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1110
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001111 if (vni >= GENEVE_N_VID) {
1112 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1113 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001114 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001115 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001116 }
1117
1118 return 0;
1119}
1120
Pravin B Shelar371bd102015-08-26 23:46:54 -07001121static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001122 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001123 bool *tun_on_same_port,
1124 bool *tun_collect_md)
1125{
pravin shelar9b4437a2016-11-21 11:02:58 -08001126 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001127
1128 *tun_on_same_port = false;
1129 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001130 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001131 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001132 *tun_collect_md = geneve->collect_md;
1133 *tun_on_same_port = true;
1134 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001135 if (info->key.tun_id == geneve->info.key.tun_id &&
1136 info->key.tp_dst == geneve->info.key.tp_dst &&
1137 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001138 t = geneve;
1139 }
1140 return t;
1141}
1142
pravin shelar9b4437a2016-11-21 11:02:58 -08001143static bool is_all_zero(const u8 *fp, size_t size)
1144{
1145 int i;
1146
1147 for (i = 0; i < size; i++)
1148 if (fp[i])
1149 return false;
1150 return true;
1151}
1152
1153static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1154{
1155 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1156 info->key.ttl || info->key.label || info->key.tp_src ||
1157 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1158 return false;
1159 else
1160 return true;
1161}
1162
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001163static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1164 struct ip_tunnel_info *b)
1165{
1166 if (ip_tunnel_info_af(a) == AF_INET)
1167 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1168 else
1169 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1170}
1171
Pravin B Shelare305ac62015-08-26 23:46:52 -07001172static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001173 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001174 const struct ip_tunnel_info *info,
1175 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001176{
1177 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001178 struct geneve_dev *t, *geneve = netdev_priv(dev);
1179 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001180 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001181
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001182 if (metadata && !is_tnl_info_zero(info)) {
1183 NL_SET_ERR_MSG(extack,
1184 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001185 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001186 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001187
1188 geneve->net = net;
1189 geneve->dev = dev;
1190
pravin shelar9b4437a2016-11-21 11:02:58 -08001191 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001192 if (t)
1193 return -EBUSY;
1194
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001195 /* make enough headroom for basic scenario */
1196 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001197 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001198 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001199 dev->max_mtu -= sizeof(struct iphdr);
1200 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001201 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001202 dev->max_mtu -= sizeof(struct ipv6hdr);
1203 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001204 dev->needed_headroom = encap_len + ETH_HLEN;
1205
Pravin B Shelar371bd102015-08-26 23:46:54 -07001206 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001207 if (tun_on_same_port) {
1208 NL_SET_ERR_MSG(extack,
1209 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001210 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001211 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001212 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001213 if (tun_collect_md) {
1214 NL_SET_ERR_MSG(extack,
1215 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001216 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001217 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001218 }
1219
pravin shelar9b4437a2016-11-21 11:02:58 -08001220 dst_cache_reset(&geneve->info.dst_cache);
1221 geneve->info = *info;
1222 geneve->collect_md = metadata;
1223 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001224
John W. Linville2d07dc72015-05-13 12:57:30 -04001225 err = register_netdevice(dev);
1226 if (err)
1227 return err;
1228
1229 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001230 return 0;
1231}
1232
pravin shelar9b4437a2016-11-21 11:02:58 -08001233static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1234{
1235 memset(info, 0, sizeof(*info));
1236 info->key.tp_dst = htons(dst_port);
1237}
1238
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001239static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1240 struct netlink_ext_ack *extack,
1241 struct ip_tunnel_info *info, bool *metadata,
1242 bool *use_udp6_rx_checksums, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001243{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001244 int attrtype;
1245
1246 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1247 NL_SET_ERR_MSG(extack,
1248 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001249 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001250 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001251
1252 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001253 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1254 attrtype = IFLA_GENEVE_REMOTE;
1255 goto change_notsup;
1256 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001257
1258 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001259 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001260
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001261 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001262 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1263 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001264 return -EINVAL;
1265 }
1266 }
1267
pravin shelar9b4437a2016-11-21 11:02:58 -08001268 if (data[IFLA_GENEVE_REMOTE6]) {
1269 #if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001270 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1271 attrtype = IFLA_GENEVE_REMOTE6;
1272 goto change_notsup;
1273 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001274
1275 info->mode = IP_TUNNEL_INFO_IPV6;
1276 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001277 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001278
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001279 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001280 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001281 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1282 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001283 return -EINVAL;
1284 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001285 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001286 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1287 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001288 return -EINVAL;
1289 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001290 info->key.tun_flags |= TUNNEL_CSUM;
1291 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001292#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001293 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1294 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001295 return -EPFNOSUPPORT;
1296#endif
1297 }
1298
1299 if (data[IFLA_GENEVE_ID]) {
1300 __u32 vni;
1301 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001302 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001303
1304 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1305 tvni[0] = (vni & 0x00ff0000) >> 16;
1306 tvni[1] = (vni & 0x0000ff00) >> 8;
1307 tvni[2] = vni & 0x000000ff;
1308
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001309 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001310 if (changelink && (tunid != info->key.tun_id)) {
1311 attrtype = IFLA_GENEVE_ID;
1312 goto change_notsup;
1313 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001314 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001315 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001316
Pravin B Shelare305ac62015-08-26 23:46:52 -07001317 if (data[IFLA_GENEVE_TTL])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001318 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001319
1320 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001321 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001322
pravin shelar9b4437a2016-11-21 11:02:58 -08001323 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001324 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001325 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001326 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1327 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1328 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001329 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001330 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001331 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001332
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001333 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001334 if (changelink) {
1335 attrtype = IFLA_GENEVE_PORT;
1336 goto change_notsup;
1337 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001338 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1339 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001340
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001341 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001342 if (changelink) {
1343 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1344 goto change_notsup;
1345 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001346 *metadata = true;
1347 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001348
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001349 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001350 if (changelink) {
1351 attrtype = IFLA_GENEVE_UDP_CSUM;
1352 goto change_notsup;
1353 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001354 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1355 info->key.tun_flags |= TUNNEL_CSUM;
1356 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001357
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001358 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001359 if (changelink) {
1360 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1361 goto change_notsup;
1362 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001363 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1364 info->key.tun_flags &= ~TUNNEL_CSUM;
1365 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001366
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001367 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001368 if (changelink) {
1369 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1370 goto change_notsup;
1371 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001372 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1373 *use_udp6_rx_checksums = false;
1374 }
1375
1376 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001377change_notsup:
1378 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1379 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1380 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001381}
1382
1383static int geneve_newlink(struct net *net, struct net_device *dev,
1384 struct nlattr *tb[], struct nlattr *data[],
1385 struct netlink_ext_ack *extack)
1386{
1387 bool use_udp6_rx_checksums = false;
1388 struct ip_tunnel_info info;
1389 bool metadata = false;
1390 int err;
1391
1392 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001393 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001394 &use_udp6_rx_checksums, false);
1395 if (err)
1396 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001397
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001398 return geneve_configure(net, dev, extack, &info, metadata,
1399 use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001400}
1401
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001402/* Quiesces the geneve device data path for both TX and RX.
1403 *
1404 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1405 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1406 * to complete for the existing set of in-flight packets to be transmitted,
1407 * then we would have quiesced the transmit data path. All the future packets
1408 * will get dropped until we unquiesce the data path.
1409 *
1410 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1411 * if we set that to NULL under RCU and wait for synchronize_net() to
1412 * complete, then we would have quiesced the receive data path.
1413 */
1414static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1415 struct geneve_sock **gs6)
1416{
1417 *gs4 = rtnl_dereference(geneve->sock4);
1418 rcu_assign_pointer(geneve->sock4, NULL);
1419 if (*gs4)
1420 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1421#if IS_ENABLED(CONFIG_IPV6)
1422 *gs6 = rtnl_dereference(geneve->sock6);
1423 rcu_assign_pointer(geneve->sock6, NULL);
1424 if (*gs6)
1425 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1426#else
1427 *gs6 = NULL;
1428#endif
1429 synchronize_net();
1430}
1431
1432/* Resumes the geneve device data path for both TX and RX. */
1433static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1434 struct geneve_sock __maybe_unused *gs6)
1435{
1436 rcu_assign_pointer(geneve->sock4, gs4);
1437 if (gs4)
1438 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1439#if IS_ENABLED(CONFIG_IPV6)
1440 rcu_assign_pointer(geneve->sock6, gs6);
1441 if (gs6)
1442 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1443#endif
1444 synchronize_net();
1445}
1446
1447static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1448 struct nlattr *data[],
1449 struct netlink_ext_ack *extack)
1450{
1451 struct geneve_dev *geneve = netdev_priv(dev);
1452 struct geneve_sock *gs4, *gs6;
1453 struct ip_tunnel_info info;
1454 bool metadata;
1455 bool use_udp6_rx_checksums;
1456 int err;
1457
1458 /* If the geneve device is configured for metadata (or externally
1459 * controlled, for example, OVS), then nothing can be changed.
1460 */
1461 if (geneve->collect_md)
1462 return -EOPNOTSUPP;
1463
1464 /* Start with the existing info. */
1465 memcpy(&info, &geneve->info, sizeof(info));
1466 metadata = geneve->collect_md;
1467 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001468 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001469 &use_udp6_rx_checksums, true);
1470 if (err)
1471 return err;
1472
1473 if (!geneve_dst_addr_equal(&geneve->info, &info))
1474 dst_cache_reset(&info.dst_cache);
1475
1476 geneve_quiesce(geneve, &gs4, &gs6);
1477 geneve->info = info;
1478 geneve->collect_md = metadata;
1479 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
1480 geneve_unquiesce(geneve, gs4, gs6);
1481
1482 return 0;
1483}
1484
John W. Linville2d07dc72015-05-13 12:57:30 -04001485static void geneve_dellink(struct net_device *dev, struct list_head *head)
1486{
1487 struct geneve_dev *geneve = netdev_priv(dev);
1488
John W. Linville2d07dc72015-05-13 12:57:30 -04001489 list_del(&geneve->next);
1490 unregister_netdevice_queue(dev, head);
1491}
1492
1493static size_t geneve_get_size(const struct net_device *dev)
1494{
1495 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001496 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001497 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001498 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001499 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001500 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001501 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001502 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1503 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1504 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001505 0;
1506}
1507
1508static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1509{
1510 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001511 struct ip_tunnel_info *info = &geneve->info;
1512 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001513 __u32 vni;
1514
pravin shelar9b4437a2016-11-21 11:02:58 -08001515 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1516 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001517 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1518 goto nla_put_failure;
1519
Eric Garver11387fe2017-05-23 18:37:27 -04001520 if (rtnl_dereference(geneve->sock4)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001521 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001522 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001523 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001524
1525 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1526 !!(info->key.tun_flags & TUNNEL_CSUM)))
1527 goto nla_put_failure;
1528
Eric Garver11387fe2017-05-23 18:37:27 -04001529 }
1530
John W. Linville8ed66f02015-10-26 17:01:44 -04001531#if IS_ENABLED(CONFIG_IPV6)
Eric Garver11387fe2017-05-23 18:37:27 -04001532 if (rtnl_dereference(geneve->sock6)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001533 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001534 &info->key.u.ipv6.dst))
1535 goto nla_put_failure;
1536
1537 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1538 !(info->key.tun_flags & TUNNEL_CSUM)))
1539 goto nla_put_failure;
1540
1541 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1542 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001543 goto nla_put_failure;
John W. Linville8ed66f02015-10-26 17:01:44 -04001544 }
Eric Garver11387fe2017-05-23 18:37:27 -04001545#endif
John W. Linville2d07dc72015-05-13 12:57:30 -04001546
pravin shelar9b4437a2016-11-21 11:02:58 -08001547 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1548 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1549 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001550 goto nla_put_failure;
1551
pravin shelar9b4437a2016-11-21 11:02:58 -08001552 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001553 goto nla_put_failure;
1554
Pravin B Shelare305ac62015-08-26 23:46:52 -07001555 if (geneve->collect_md) {
1556 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1557 goto nla_put_failure;
1558 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001559 return 0;
1560
1561nla_put_failure:
1562 return -EMSGSIZE;
1563}
1564
1565static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1566 .kind = "geneve",
1567 .maxtype = IFLA_GENEVE_MAX,
1568 .policy = geneve_policy,
1569 .priv_size = sizeof(struct geneve_dev),
1570 .setup = geneve_setup,
1571 .validate = geneve_validate,
1572 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001573 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001574 .dellink = geneve_dellink,
1575 .get_size = geneve_get_size,
1576 .fill_info = geneve_fill_info,
1577};
1578
Pravin B Shelare305ac62015-08-26 23:46:52 -07001579struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1580 u8 name_assign_type, u16 dst_port)
1581{
1582 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001583 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001584 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001585 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001586 int err;
1587
1588 memset(tb, 0, sizeof(tb));
1589 dev = rtnl_create_link(net, name, name_assign_type,
1590 &geneve_link_ops, tb);
1591 if (IS_ERR(dev))
1592 return dev;
1593
pravin shelar9b4437a2016-11-21 11:02:58 -08001594 init_tnl_info(&info, dst_port);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001595 err = geneve_configure(net, dev, NULL, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001596 if (err) {
1597 free_netdev(dev);
1598 return ERR_PTR(err);
1599 }
David Wragg7e059152016-02-10 00:05:58 +00001600
1601 /* openvswitch users expect packet sizes to be unrestricted,
1602 * so set the largest MTU we can.
1603 */
Jarod Wilson91572082016-10-20 13:55:20 -04001604 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001605 if (err)
1606 goto err;
1607
Nicolas Dichtel41009482016-06-13 10:31:07 +02001608 err = rtnl_configure_link(dev, NULL);
1609 if (err < 0)
1610 goto err;
1611
Pravin B Shelare305ac62015-08-26 23:46:52 -07001612 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001613err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001614 geneve_dellink(dev, &list_kill);
1615 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001616 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001617}
1618EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1619
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001620static int geneve_netdevice_event(struct notifier_block *unused,
1621 unsigned long event, void *ptr)
1622{
1623 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1624
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001625 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001626 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001627 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001628 } else if (event == NETDEV_UNREGISTER) {
1629 geneve_offload_rx_ports(dev, false);
1630 } else if (event == NETDEV_REGISTER) {
1631 geneve_offload_rx_ports(dev, true);
1632 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001633
1634 return NOTIFY_DONE;
1635}
1636
1637static struct notifier_block geneve_notifier_block __read_mostly = {
1638 .notifier_call = geneve_netdevice_event,
1639};
1640
John W. Linville2d07dc72015-05-13 12:57:30 -04001641static __net_init int geneve_init_net(struct net *net)
1642{
1643 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001644
1645 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001646 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001647 return 0;
1648}
1649
1650static void __net_exit geneve_exit_net(struct net *net)
1651{
1652 struct geneve_net *gn = net_generic(net, geneve_net_id);
1653 struct geneve_dev *geneve, *next;
1654 struct net_device *dev, *aux;
1655 LIST_HEAD(list);
1656
1657 rtnl_lock();
1658
1659 /* gather any geneve devices that were moved into this ns */
1660 for_each_netdev_safe(net, dev, aux)
1661 if (dev->rtnl_link_ops == &geneve_link_ops)
1662 unregister_netdevice_queue(dev, &list);
1663
1664 /* now gather any other geneve devices that were created in this ns */
1665 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1666 /* If geneve->dev is in the same netns, it was already added
1667 * to the list by the previous loop.
1668 */
1669 if (!net_eq(dev_net(geneve->dev), net))
1670 unregister_netdevice_queue(geneve->dev, &list);
1671 }
1672
1673 /* unregister the devices gathered above */
1674 unregister_netdevice_many(&list);
1675 rtnl_unlock();
1676}
1677
1678static struct pernet_operations geneve_net_ops = {
1679 .init = geneve_init_net,
1680 .exit = geneve_exit_net,
1681 .id = &geneve_net_id,
1682 .size = sizeof(struct geneve_net),
1683};
1684
1685static int __init geneve_init_module(void)
1686{
1687 int rc;
1688
1689 rc = register_pernet_subsys(&geneve_net_ops);
1690 if (rc)
1691 goto out1;
1692
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001693 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001694 if (rc)
1695 goto out2;
1696
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001697 rc = rtnl_link_register(&geneve_link_ops);
1698 if (rc)
1699 goto out3;
1700
John W. Linville2d07dc72015-05-13 12:57:30 -04001701 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001702out3:
1703 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001704out2:
1705 unregister_pernet_subsys(&geneve_net_ops);
1706out1:
1707 return rc;
1708}
1709late_initcall(geneve_init_module);
1710
1711static void __exit geneve_cleanup_module(void)
1712{
1713 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001714 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001715 unregister_pernet_subsys(&geneve_net_ops);
1716}
1717module_exit(geneve_cleanup_module);
1718
1719MODULE_LICENSE("GPL");
1720MODULE_VERSION(GENEVE_NETDEV_VER);
1721MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1722MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1723MODULE_ALIAS_RTNL_LINK("geneve");