blob: ed51018a813e7ba6354d296e0d6c9fba3a1f76a1 [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{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800116 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800117}
118
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100119static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
120{
121 return gs->sock->sk->sk_family;
122}
123
Pravin B Shelar66d47002015-08-26 23:46:55 -0700124static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700125 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400126{
John W. Linville2d07dc72015-05-13 12:57:30 -0400127 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200128 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400129 __u32 hash;
130
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700132 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700133 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200134 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
135 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
136 addr == node->geneve->info.key.u.ipv4.dst)
137 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400138 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700139 return NULL;
140}
141
John W. Linville8ed66f02015-10-26 17:01:44 -0400142#if IS_ENABLED(CONFIG_IPV6)
143static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
144 struct in6_addr addr6, u8 vni[])
145{
146 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200147 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400148 __u32 hash;
149
150 /* Find the device for this VNI */
151 hash = geneve_net_vni_hash(vni);
152 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200153 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
154 if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) &&
155 ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst))
156 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400157 }
158 return NULL;
159}
160#endif
161
Pravin B Shelar371bd102015-08-26 23:46:54 -0700162static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
163{
164 return (struct genevehdr *)(udp_hdr(skb) + 1);
165}
166
Jiri Benc9fc47542016-02-18 11:22:50 +0100167static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
168 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700169{
John W. Linville8ed66f02015-10-26 17:01:44 -0400170 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800171 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700172
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100173 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100174 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800175 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100176
John W. Linville8ed66f02015-10-26 17:01:44 -0400177 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700178
John W. Linville8ed66f02015-10-26 17:01:44 -0400179 if (gs->collect_md) {
180 vni = zero_vni;
181 addr = 0;
182 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100183 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400184 addr = iph->saddr;
185 }
186
Jiri Benc9fc47542016-02-18 11:22:50 +0100187 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400188#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100189 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800190 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100191 struct ipv6hdr *ip6h;
192 struct in6_addr addr6;
193
John W. Linville8ed66f02015-10-26 17:01:44 -0400194 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
195
196 if (gs->collect_md) {
197 vni = zero_vni;
198 addr6 = zero_addr6;
199 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100200 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400201 addr6 = ip6h->saddr;
202 }
203
Jiri Benc9fc47542016-02-18 11:22:50 +0100204 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400205#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700206 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100207 return NULL;
208}
209
210/* geneve receive/decap routine */
211static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
212 struct sk_buff *skb)
213{
214 struct genevehdr *gnvh = geneve_hdr(skb);
215 struct metadata_dst *tun_dst = NULL;
216 struct pcpu_sw_netstats *stats;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700217 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100218 int err = 0;
219 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400220
Pravin B Shelar371bd102015-08-26 23:46:54 -0700221 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700222 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700223
224 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
225 (gnvh->oam ? TUNNEL_OAM : 0) |
226 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
227
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100228 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700229 vni_to_tunnel_id(gnvh->vni),
230 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700231 if (!tun_dst) {
232 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700234 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700235 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700236 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
237 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700238 } else {
239 /* Drop packets w/ critical options,
240 * since we don't support any...
241 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700242 if (gnvh->critical) {
243 geneve->dev->stats.rx_frame_errors++;
244 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700245 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700246 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700247 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400248
249 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400250 skb->protocol = eth_type_trans(skb, geneve->dev);
251 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
252
Pravin B Shelare305ac62015-08-26 23:46:52 -0700253 if (tun_dst)
254 skb_dst_set(skb, &tun_dst->dst);
255
John W. Linville2d07dc72015-05-13 12:57:30 -0400256 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700257 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
258 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400259 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700260 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400261
Jiri Benc9fc47542016-02-18 11:22:50 +0100262 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400263 skb_reset_network_header(skb);
264
Jiri Benc9fc47542016-02-18 11:22:50 +0100265 if (geneve_get_sk_family(gs) == AF_INET)
266 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400267#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100268 else
269 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400270#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400271
272 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400273 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100274 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400275 net_info_ratelimited("non-ECT from %pI4 "
276 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100277 &((struct iphdr *)oiph)->saddr,
278 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400279#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100280 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400281 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100282 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400283#endif
284 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400285 if (err > 1) {
286 ++geneve->dev->stats.rx_frame_errors;
287 ++geneve->dev->stats.rx_errors;
288 goto drop;
289 }
290 }
291
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700292 len = skb->len;
293 err = gro_cells_receive(&geneve->gro_cells, skb);
294 if (likely(err == NET_RX_SUCCESS)) {
295 stats = this_cpu_ptr(geneve->dev->tstats);
296 u64_stats_update_begin(&stats->syncp);
297 stats->rx_packets++;
298 stats->rx_bytes += len;
299 u64_stats_update_end(&stats->syncp);
300 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400301 return;
302drop:
303 /* Consume bad packet */
304 kfree_skb(skb);
305}
306
307/* Setup stats when device is created */
308static int geneve_init(struct net_device *dev)
309{
Jesse Gross8e816df2015-08-28 16:54:40 -0700310 struct geneve_dev *geneve = netdev_priv(dev);
311 int err;
312
John W. Linville2d07dc72015-05-13 12:57:30 -0400313 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
314 if (!dev->tstats)
315 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700316
317 err = gro_cells_init(&geneve->gro_cells, dev);
318 if (err) {
319 free_percpu(dev->tstats);
320 return err;
321 }
322
pravin shelar9b4437a2016-11-21 11:02:58 -0800323 err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100324 if (err) {
325 free_percpu(dev->tstats);
326 gro_cells_destroy(&geneve->gro_cells);
327 return err;
328 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400329 return 0;
330}
331
332static void geneve_uninit(struct net_device *dev)
333{
Jesse Gross8e816df2015-08-28 16:54:40 -0700334 struct geneve_dev *geneve = netdev_priv(dev);
335
pravin shelar9b4437a2016-11-21 11:02:58 -0800336 dst_cache_destroy(&geneve->info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700337 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400338 free_percpu(dev->tstats);
339}
340
Pravin B Shelar371bd102015-08-26 23:46:54 -0700341/* Callback from net/ipv4/udp.c to receive packets */
342static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
343{
344 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100345 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700346 struct geneve_sock *gs;
347 int opts_len;
348
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700349 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700350 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200351 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700352
353 /* Return packets with reserved bits set */
354 geneveh = geneve_hdr(skb);
355 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200356 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700357
358 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200359 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700360
Jiri Benc9fc47542016-02-18 11:22:50 +0100361 gs = rcu_dereference_sk_user_data(sk);
362 if (!gs)
363 goto drop;
364
365 geneve = geneve_lookup_skb(gs, skb);
366 if (!geneve)
367 goto drop;
368
Pravin B Shelar371bd102015-08-26 23:46:54 -0700369 opts_len = geneveh->opt_len * 4;
370 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100371 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700372 !net_eq(geneve->net, dev_net(geneve->dev)))) {
373 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700374 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700375 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700376
Jiri Benc9fc47542016-02-18 11:22:50 +0100377 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700378 return 0;
379
380drop:
381 /* Consume bad packet */
382 kfree_skb(skb);
383 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384}
385
386static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800387 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700388{
389 struct socket *sock;
390 struct udp_port_cfg udp_conf;
391 int err;
392
393 memset(&udp_conf, 0, sizeof(udp_conf));
394
395 if (ipv6) {
396 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400397 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800398 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700399 } else {
400 udp_conf.family = AF_INET;
401 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
402 }
403
404 udp_conf.local_udp_port = port;
405
406 /* Open UDP socket */
407 err = udp_sock_create(net, &udp_conf, &sock);
408 if (err < 0)
409 return ERR_PTR(err);
410
411 return sock;
412}
413
Pravin B Shelar371bd102015-08-26 23:46:54 -0700414static int geneve_hlen(struct genevehdr *gh)
415{
416 return sizeof(*gh) + gh->opt_len * 4;
417}
418
Tom Herbert4a0090a2016-04-05 08:22:55 -0700419static struct sk_buff **geneve_gro_receive(struct sock *sk,
420 struct sk_buff **head,
421 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700422{
423 struct sk_buff *p, **pp = NULL;
424 struct genevehdr *gh, *gh2;
425 unsigned int hlen, gh_len, off_gnv;
426 const struct packet_offload *ptype;
427 __be16 type;
428 int flush = 1;
429
430 off_gnv = skb_gro_offset(skb);
431 hlen = off_gnv + sizeof(*gh);
432 gh = skb_gro_header_fast(skb, off_gnv);
433 if (skb_gro_header_hard(skb, hlen)) {
434 gh = skb_gro_header_slow(skb, hlen, off_gnv);
435 if (unlikely(!gh))
436 goto out;
437 }
438
439 if (gh->ver != GENEVE_VER || gh->oam)
440 goto out;
441 gh_len = geneve_hlen(gh);
442
443 hlen = off_gnv + gh_len;
444 if (skb_gro_header_hard(skb, hlen)) {
445 gh = skb_gro_header_slow(skb, hlen, off_gnv);
446 if (unlikely(!gh))
447 goto out;
448 }
449
Pravin B Shelar371bd102015-08-26 23:46:54 -0700450 for (p = *head; p; p = p->next) {
451 if (!NAPI_GRO_CB(p)->same_flow)
452 continue;
453
454 gh2 = (struct genevehdr *)(p->data + off_gnv);
455 if (gh->opt_len != gh2->opt_len ||
456 memcmp(gh, gh2, gh_len)) {
457 NAPI_GRO_CB(p)->same_flow = 0;
458 continue;
459 }
460 }
461
462 type = gh->proto_type;
463
464 rcu_read_lock();
465 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800466 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700467 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700468
469 skb_gro_pull(skb, gh_len);
470 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200471 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800472 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700473
474out_unlock:
475 rcu_read_unlock();
476out:
477 NAPI_GRO_CB(skb)->flush |= flush;
478
479 return pp;
480}
481
Tom Herbert4a0090a2016-04-05 08:22:55 -0700482static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
483 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700484{
485 struct genevehdr *gh;
486 struct packet_offload *ptype;
487 __be16 type;
488 int gh_len;
489 int err = -ENOSYS;
490
Pravin B Shelar371bd102015-08-26 23:46:54 -0700491 gh = (struct genevehdr *)(skb->data + nhoff);
492 gh_len = geneve_hlen(gh);
493 type = gh->proto_type;
494
495 rcu_read_lock();
496 ptype = gro_find_complete_by_type(type);
497 if (ptype)
498 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
499
500 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700501
502 skb_set_inner_mac_header(skb, nhoff + gh_len);
503
Pravin B Shelar371bd102015-08-26 23:46:54 -0700504 return err;
505}
506
507/* Create new listen socket if needed */
508static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800509 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700510{
511 struct geneve_net *gn = net_generic(net, geneve_net_id);
512 struct geneve_sock *gs;
513 struct socket *sock;
514 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700515 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700516
517 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
518 if (!gs)
519 return ERR_PTR(-ENOMEM);
520
pravin shelar9b4437a2016-11-21 11:02:58 -0800521 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700522 if (IS_ERR(sock)) {
523 kfree(gs);
524 return ERR_CAST(sock);
525 }
526
527 gs->sock = sock;
528 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700529 for (h = 0; h < VNI_HASH_SIZE; ++h)
530 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700531
532 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700533 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534
535 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700536 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700537 tunnel_cfg.sk_user_data = gs;
538 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700539 tunnel_cfg.gro_receive = geneve_gro_receive;
540 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700541 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
542 tunnel_cfg.encap_destroy = NULL;
543 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700544 list_add(&gs->list, &gn->sock_list);
545 return gs;
546}
547
John W. Linville8ed66f02015-10-26 17:01:44 -0400548static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700549{
John W. Linville8ed66f02015-10-26 17:01:44 -0400550 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700551 return;
552
553 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700554 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700555 udp_tunnel_sock_release(gs->sock);
556 kfree_rcu(gs, rcu);
557}
558
John W. Linville8ed66f02015-10-26 17:01:44 -0400559static void geneve_sock_release(struct geneve_dev *geneve)
560{
pravin shelarfceb9c32016-10-28 09:59:16 -0700561 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400562#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700563 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
564
565 rcu_assign_pointer(geneve->sock6, NULL);
566#endif
567
568 rcu_assign_pointer(geneve->sock4, NULL);
569 synchronize_net();
570
571 __geneve_sock_release(gs4);
572#if IS_ENABLED(CONFIG_IPV6)
573 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400574#endif
575}
576
Pravin B Shelar371bd102015-08-26 23:46:54 -0700577static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400578 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700579 __be16 dst_port)
580{
581 struct geneve_sock *gs;
582
583 list_for_each_entry(gs, &gn->sock_list, list) {
584 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100585 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700586 return gs;
587 }
588 }
589 return NULL;
590}
591
John W. Linville8ed66f02015-10-26 17:01:44 -0400592static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400593{
John W. Linville2d07dc72015-05-13 12:57:30 -0400594 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700595 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200596 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400597 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800598 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700599 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400600
pravin shelar9b4437a2016-11-21 11:02:58 -0800601 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700602 if (gs) {
603 gs->refcnt++;
604 goto out;
605 }
606
pravin shelar9b4437a2016-11-21 11:02:58 -0800607 gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6,
608 geneve->use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400609 if (IS_ERR(gs))
610 return PTR_ERR(gs);
611
Pravin B Shelar371bd102015-08-26 23:46:54 -0700612out:
613 gs->collect_md = geneve->collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400614#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200615 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700616 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200617 node = &geneve->hlist6;
618 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400619#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200620 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700621 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200622 node = &geneve->hlist4;
623 }
624 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700625
pravin shelar9b4437a2016-11-21 11:02:58 -0800626 tunnel_id_to_vni(geneve->info.key.tun_id, vni);
627 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200628 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400629 return 0;
630}
631
John W. Linville8ed66f02015-10-26 17:01:44 -0400632static int geneve_open(struct net_device *dev)
633{
634 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -0800635 bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400636 bool metadata = geneve->collect_md;
637 int ret = 0;
638
John W. Linville8ed66f02015-10-26 17:01:44 -0400639#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400640 if (ipv6 || metadata)
641 ret = geneve_sock_add(geneve, true);
642#endif
643 if (!ret && (!ipv6 || metadata))
644 ret = geneve_sock_add(geneve, false);
645 if (ret < 0)
646 geneve_sock_release(geneve);
647
648 return ret;
649}
650
John W. Linville2d07dc72015-05-13 12:57:30 -0400651static int geneve_stop(struct net_device *dev)
652{
653 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400654
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200655 hlist_del_init_rcu(&geneve->hlist4.hlist);
656#if IS_ENABLED(CONFIG_IPV6)
657 hlist_del_init_rcu(&geneve->hlist6.hlist);
658#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400659 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400660 return 0;
661}
662
John W. Linville8ed66f02015-10-26 17:01:44 -0400663static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800664 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400665{
666 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800667 geneveh->opt_len = info->options_len / 4;
668 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
669 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400670 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800671 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400672 geneveh->proto_type = htons(ETH_P_TEB);
673 geneveh->rsvd2 = 0;
674
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800675 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400676}
677
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800678static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
679 const struct ip_tunnel_info *info,
680 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700681{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800682 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700683 struct genevehdr *gnvh;
684 int min_headroom;
685 int err;
686
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800687 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400688 skb_scrub_packet(skb, xnet);
689
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800690 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
691 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400692 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400693 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400695
Alexander Duyckaed069d2016-04-14 15:33:37 -0400696 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300697 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400698 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400699
Johannes Bergd58ff352017-06-16 14:29:23 +0200700 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800701 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400702 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
703 return 0;
704
705free_dst:
706 dst_release(dst);
707 return err;
708}
John W. Linville8ed66f02015-10-26 17:01:44 -0400709
710static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
711 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700712 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400713 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800714 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700715{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100716 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700717 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100718 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700719 struct rtable *rt = NULL;
720 __u8 tos;
721
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700722 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700723 return ERR_PTR(-EIO);
724
Pravin B Shelare305ac62015-08-26 23:46:52 -0700725 memset(fl4, 0, sizeof(*fl4));
726 fl4->flowi4_mark = skb->mark;
727 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800728 fl4->daddr = info->key.u.ipv4.dst;
729 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700730
pravin shelar9b4437a2016-11-21 11:02:58 -0800731 tos = info->key.tos;
732 if ((tos == 1) && !geneve->collect_md) {
733 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
734 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100735 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800736 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100737
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800738 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100739 if (use_cache) {
740 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
741 if (rt)
742 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700743 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700744 rt = ip_route_output_key(geneve->net, fl4);
745 if (IS_ERR(rt)) {
746 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700747 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700748 }
749 if (rt->dst.dev == dev) { /* is this necessary? */
750 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700751 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700752 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700753 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100754 if (use_cache)
755 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700756 return rt;
757}
758
John W. Linville8ed66f02015-10-26 17:01:44 -0400759#if IS_ENABLED(CONFIG_IPV6)
760static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
761 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700762 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400763 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800764 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400765{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100766 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400767 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400768 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100769 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400770 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400771
pravin shelarfceb9c32016-10-28 09:59:16 -0700772 if (!gs6)
773 return ERR_PTR(-EIO);
774
John W. Linville8ed66f02015-10-26 17:01:44 -0400775 memset(fl6, 0, sizeof(*fl6));
776 fl6->flowi6_mark = skb->mark;
777 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800778 fl6->daddr = info->key.u.ipv6.dst;
779 fl6->saddr = info->key.u.ipv6.src;
780 prio = info->key.tos;
781 if ((prio == 1) && !geneve->collect_md) {
782 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
783 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100784 }
785
pravin shelar9b4437a2016-11-21 11:02:58 -0800786 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
787 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800788 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100789 if (use_cache) {
790 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
791 if (dst)
792 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400793 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400794 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
795 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
796 return ERR_PTR(-ENETUNREACH);
797 }
798 if (dst->dev == dev) { /* is this necessary? */
799 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
800 dst_release(dst);
801 return ERR_PTR(-ELOOP);
802 }
803
Paolo Abeni468dfff2016-02-12 15:43:58 +0100804 if (use_cache)
805 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400806 return dst;
807}
808#endif
809
pravin shelar9b4437a2016-11-21 11:02:58 -0800810static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800811 struct geneve_dev *geneve,
812 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700813{
pravin shelar9b4437a2016-11-21 11:02:58 -0800814 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
815 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
816 const struct ip_tunnel_key *key = &info->key;
817 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400818 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400819 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700820 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700821 __be16 df;
pravin shelarbcceeec2016-11-21 11:03:00 -0800822 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400823
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700824 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800825 if (IS_ERR(rt))
826 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700827
828 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800829 if (geneve->collect_md) {
830 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700831 ttl = key->ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700832 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800833 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
834 ttl = key->ttl ? : ip4_dst_hoplimit(&rt->dst);
John W. Linville2d07dc72015-05-13 12:57:30 -0400835 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800836 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
837
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800838 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800839 if (unlikely(err))
840 return err;
841
Pravin B Shelar039f5062015-12-24 14:34:54 -0800842 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
pravin shelar9b4437a2016-11-21 11:02:58 -0800843 tos, ttl, df, sport, geneve->info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800844 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800845 !(info->key.tun_flags & TUNNEL_CSUM));
846 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400847}
848
John W. Linville8ed66f02015-10-26 17:01:44 -0400849#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800850static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800851 struct geneve_dev *geneve,
852 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400853{
pravin shelar9b4437a2016-11-21 11:02:58 -0800854 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
855 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
856 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400857 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400858 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400859 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400860 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800861 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400862
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700863 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
pravin shelar9b4437a2016-11-21 11:02:58 -0800864 if (IS_ERR(dst))
865 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400866
867 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800868 if (geneve->collect_md) {
869 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400870 ttl = key->ttl;
871 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100872 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -0800873 ip_hdr(skb), skb);
874 ttl = key->ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +0800876 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800877 if (unlikely(err))
878 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100879
Pravin B Shelar039f5062015-12-24 14:34:54 -0800880 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -0800881 &fl6.saddr, &fl6.daddr, prio, ttl,
882 info->key.label, sport, geneve->info.key.tp_dst,
883 !(info->key.tun_flags & TUNNEL_CSUM));
884 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -0400885}
886#endif
887
888static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
889{
890 struct geneve_dev *geneve = netdev_priv(dev);
891 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -0800892 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400893
pravin shelar9b4437a2016-11-21 11:02:58 -0800894 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400895 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
897 err = -EINVAL;
898 netdev_dbg(dev, "no tunnel metadata\n");
899 goto tx_error;
900 }
901 } else {
902 info = &geneve->info;
903 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400904
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800905 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -0400906#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800907 if (info->mode & IP_TUNNEL_INFO_IPV6)
908 err = geneve6_xmit_skb(skb, dev, geneve, info);
909 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400910#endif
pravin shelar9b4437a2016-11-21 11:02:58 -0800911 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -0800912 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -0800913
914 if (likely(!err))
915 return NETDEV_TX_OK;
916tx_error:
917 dev_kfree_skb(skb);
918
919 if (err == -ELOOP)
920 dev->stats.collisions++;
921 else if (err == -ENETUNREACH)
922 dev->stats.tx_carrier_errors++;
923
924 dev->stats.tx_errors++;
925 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -0400926}
927
Jarod Wilson91572082016-10-20 13:55:20 -0400928static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +0000929{
Jarod Wilson91572082016-10-20 13:55:20 -0400930 /* Only possible if called internally, ndo_change_mtu path's new_mtu
931 * is guaranteed to be between dev->min_mtu and dev->max_mtu.
David Wragg55e5bfb2016-02-10 00:05:57 +0000932 */
Jarod Wilson91572082016-10-20 13:55:20 -0400933 if (new_mtu > dev->max_mtu)
934 new_mtu = dev->max_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +0000935
David Wragg55e5bfb2016-02-10 00:05:57 +0000936 dev->mtu = new_mtu;
937 return 0;
938}
939
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700940static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
941{
942 struct ip_tunnel_info *info = skb_tunnel_info(skb);
943 struct geneve_dev *geneve = netdev_priv(dev);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700944
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400945 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800946 struct rtable *rt;
947 struct flowi4 fl4;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700948 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
pravin shelar9b4437a2016-11-21 11:02:58 -0800949
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700950 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400951 if (IS_ERR(rt))
952 return PTR_ERR(rt);
953
954 ip_rt_put(rt);
955 info->key.u.ipv4.src = fl4.saddr;
956#if IS_ENABLED(CONFIG_IPV6)
957 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800958 struct dst_entry *dst;
959 struct flowi6 fl6;
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700960 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
pravin shelar9b4437a2016-11-21 11:02:58 -0800961
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700962 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info);
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400963 if (IS_ERR(dst))
964 return PTR_ERR(dst);
965
966 dst_release(dst);
967 info->key.u.ipv6.src = fl6.saddr;
968#endif
969 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700970 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400971 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700972
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700973 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
974 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800975 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700976 return 0;
977}
978
John W. Linville2d07dc72015-05-13 12:57:30 -0400979static const struct net_device_ops geneve_netdev_ops = {
980 .ndo_init = geneve_init,
981 .ndo_uninit = geneve_uninit,
982 .ndo_open = geneve_open,
983 .ndo_stop = geneve_stop,
984 .ndo_start_xmit = geneve_xmit,
985 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +0000986 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -0400987 .ndo_validate_addr = eth_validate_addr,
988 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700989 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -0400990};
991
992static void geneve_get_drvinfo(struct net_device *dev,
993 struct ethtool_drvinfo *drvinfo)
994{
995 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
996 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
997}
998
999static const struct ethtool_ops geneve_ethtool_ops = {
1000 .get_drvinfo = geneve_get_drvinfo,
1001 .get_link = ethtool_op_get_link,
1002};
1003
1004/* Info for udev, that this is a virtual tunnel endpoint */
1005static struct device_type geneve_type = {
1006 .name = "geneve",
1007};
1008
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001009/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001010 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001011 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001012 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001013static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001014{
1015 struct net *net = dev_net(dev);
1016 struct geneve_net *gn = net_generic(net, geneve_net_id);
1017 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001018
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001019 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001020 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1021 if (push) {
1022 udp_tunnel_push_rx_port(dev, gs->sock,
1023 UDP_TUNNEL_TYPE_GENEVE);
1024 } else {
1025 udp_tunnel_drop_rx_port(dev, gs->sock,
1026 UDP_TUNNEL_TYPE_GENEVE);
1027 }
1028 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001029 rcu_read_unlock();
1030}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001031
John W. Linville2d07dc72015-05-13 12:57:30 -04001032/* Initialize the device structure. */
1033static void geneve_setup(struct net_device *dev)
1034{
1035 ether_setup(dev);
1036
1037 dev->netdev_ops = &geneve_netdev_ops;
1038 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001039 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001040
1041 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1042
John W. Linville2d07dc72015-05-13 12:57:30 -04001043 dev->features |= NETIF_F_LLTX;
1044 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1045 dev->features |= NETIF_F_RXCSUM;
1046 dev->features |= NETIF_F_GSO_SOFTWARE;
1047
John W. Linville2d07dc72015-05-13 12:57:30 -04001048 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1049 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001050
Jarod Wilson91572082016-10-20 13:55:20 -04001051 /* MTU range: 68 - (something less than 65535) */
1052 dev->min_mtu = ETH_MIN_MTU;
1053 /* The max_mtu calculation does not take account of GENEVE
1054 * options, to avoid excluding potentially valid
1055 * configurations. This will be further reduced by IPvX hdr size.
1056 */
1057 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1058
John W. Linville2d07dc72015-05-13 12:57:30 -04001059 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001060 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001061 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001062 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001063}
1064
1065static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1066 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1067 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001068 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001069 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001070 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001071 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001072 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001073 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001074 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1075 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1076 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001077};
1078
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001079static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1080 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001081{
1082 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001083 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1084 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1085 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001086 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001087 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001088
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001089 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1090 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1091 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001092 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001093 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001094 }
1095
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001096 if (!data) {
1097 NL_SET_ERR_MSG(extack,
1098 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001099 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001100 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001101
1102 if (data[IFLA_GENEVE_ID]) {
1103 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1104
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001105 if (vni >= GENEVE_N_VID) {
1106 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1107 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001108 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001109 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001110 }
1111
1112 return 0;
1113}
1114
Pravin B Shelar371bd102015-08-26 23:46:54 -07001115static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001116 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001117 bool *tun_on_same_port,
1118 bool *tun_collect_md)
1119{
pravin shelar9b4437a2016-11-21 11:02:58 -08001120 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001121
1122 *tun_on_same_port = false;
1123 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001124 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001125 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001126 *tun_collect_md = geneve->collect_md;
1127 *tun_on_same_port = true;
1128 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001129 if (info->key.tun_id == geneve->info.key.tun_id &&
1130 info->key.tp_dst == geneve->info.key.tp_dst &&
1131 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001132 t = geneve;
1133 }
1134 return t;
1135}
1136
pravin shelar9b4437a2016-11-21 11:02:58 -08001137static bool is_all_zero(const u8 *fp, size_t size)
1138{
1139 int i;
1140
1141 for (i = 0; i < size; i++)
1142 if (fp[i])
1143 return false;
1144 return true;
1145}
1146
1147static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1148{
1149 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1150 info->key.ttl || info->key.label || info->key.tp_src ||
1151 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1152 return false;
1153 else
1154 return true;
1155}
1156
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001157static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1158 struct ip_tunnel_info *b)
1159{
1160 if (ip_tunnel_info_af(a) == AF_INET)
1161 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1162 else
1163 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1164}
1165
Pravin B Shelare305ac62015-08-26 23:46:52 -07001166static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001167 struct netlink_ext_ack *extack,
pravin shelar9b4437a2016-11-21 11:02:58 -08001168 const struct ip_tunnel_info *info,
1169 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001170{
1171 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001172 struct geneve_dev *t, *geneve = netdev_priv(dev);
1173 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001174 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001175
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001176 if (metadata && !is_tnl_info_zero(info)) {
1177 NL_SET_ERR_MSG(extack,
1178 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001179 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001180 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001181
1182 geneve->net = net;
1183 geneve->dev = dev;
1184
pravin shelar9b4437a2016-11-21 11:02:58 -08001185 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001186 if (t)
1187 return -EBUSY;
1188
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001189 /* make enough headroom for basic scenario */
1190 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001191 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001192 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001193 dev->max_mtu -= sizeof(struct iphdr);
1194 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001195 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001196 dev->max_mtu -= sizeof(struct ipv6hdr);
1197 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001198 dev->needed_headroom = encap_len + ETH_HLEN;
1199
Pravin B Shelar371bd102015-08-26 23:46:54 -07001200 if (metadata) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001201 if (tun_on_same_port) {
1202 NL_SET_ERR_MSG(extack,
1203 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001204 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001205 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001206 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001207 if (tun_collect_md) {
1208 NL_SET_ERR_MSG(extack,
1209 "There already exists an externally controlled device on this 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 }
1213
pravin shelar9b4437a2016-11-21 11:02:58 -08001214 dst_cache_reset(&geneve->info.dst_cache);
1215 geneve->info = *info;
1216 geneve->collect_md = metadata;
1217 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001218
John W. Linville2d07dc72015-05-13 12:57:30 -04001219 err = register_netdevice(dev);
1220 if (err)
1221 return err;
1222
1223 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001224 return 0;
1225}
1226
pravin shelar9b4437a2016-11-21 11:02:58 -08001227static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1228{
1229 memset(info, 0, sizeof(*info));
1230 info->key.tp_dst = htons(dst_port);
1231}
1232
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001233static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1234 struct netlink_ext_ack *extack,
1235 struct ip_tunnel_info *info, bool *metadata,
1236 bool *use_udp6_rx_checksums, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001237{
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001238 int attrtype;
1239
1240 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1241 NL_SET_ERR_MSG(extack,
1242 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001243 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001244 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001245
1246 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001247 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1248 attrtype = IFLA_GENEVE_REMOTE;
1249 goto change_notsup;
1250 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001251
1252 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001253 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001254
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001255 if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001256 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1257 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001258 return -EINVAL;
1259 }
1260 }
1261
pravin shelar9b4437a2016-11-21 11:02:58 -08001262 if (data[IFLA_GENEVE_REMOTE6]) {
1263 #if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001264 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1265 attrtype = IFLA_GENEVE_REMOTE6;
1266 goto change_notsup;
1267 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001268
1269 info->mode = IP_TUNNEL_INFO_IPV6;
1270 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001271 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001272
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001273 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001274 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001275 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1276 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001277 return -EINVAL;
1278 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001279 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001280 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1281 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001282 return -EINVAL;
1283 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001284 info->key.tun_flags |= TUNNEL_CSUM;
1285 *use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001286#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001287 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1288 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001289 return -EPFNOSUPPORT;
1290#endif
1291 }
1292
1293 if (data[IFLA_GENEVE_ID]) {
1294 __u32 vni;
1295 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001296 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001297
1298 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1299 tvni[0] = (vni & 0x00ff0000) >> 16;
1300 tvni[1] = (vni & 0x0000ff00) >> 8;
1301 tvni[2] = vni & 0x000000ff;
1302
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001303 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001304 if (changelink && (tunid != info->key.tun_id)) {
1305 attrtype = IFLA_GENEVE_ID;
1306 goto change_notsup;
1307 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001308 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001309 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001310
Pravin B Shelare305ac62015-08-26 23:46:52 -07001311 if (data[IFLA_GENEVE_TTL])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001312 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001313
1314 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001315 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001316
pravin shelar9b4437a2016-11-21 11:02:58 -08001317 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001318 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001319 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001320 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1321 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1322 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001323 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001324 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001325 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001326
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001327 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001328 if (changelink) {
1329 attrtype = IFLA_GENEVE_PORT;
1330 goto change_notsup;
1331 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001332 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1333 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001334
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001335 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001336 if (changelink) {
1337 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1338 goto change_notsup;
1339 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001340 *metadata = true;
1341 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001342
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001343 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001344 if (changelink) {
1345 attrtype = IFLA_GENEVE_UDP_CSUM;
1346 goto change_notsup;
1347 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001348 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1349 info->key.tun_flags |= TUNNEL_CSUM;
1350 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001351
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001352 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001353 if (changelink) {
1354 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1355 goto change_notsup;
1356 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001357 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1358 info->key.tun_flags &= ~TUNNEL_CSUM;
1359 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001360
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001361 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001362 if (changelink) {
1363 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1364 goto change_notsup;
1365 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001366 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1367 *use_udp6_rx_checksums = false;
1368 }
1369
1370 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001371change_notsup:
1372 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1373 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1374 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001375}
1376
1377static int geneve_newlink(struct net *net, struct net_device *dev,
1378 struct nlattr *tb[], struct nlattr *data[],
1379 struct netlink_ext_ack *extack)
1380{
1381 bool use_udp6_rx_checksums = false;
1382 struct ip_tunnel_info info;
1383 bool metadata = false;
1384 int err;
1385
1386 init_tnl_info(&info, GENEVE_UDP_PORT);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001387 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001388 &use_udp6_rx_checksums, false);
1389 if (err)
1390 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001391
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001392 return geneve_configure(net, dev, extack, &info, metadata,
1393 use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001394}
1395
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001396/* Quiesces the geneve device data path for both TX and RX.
1397 *
1398 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1399 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1400 * to complete for the existing set of in-flight packets to be transmitted,
1401 * then we would have quiesced the transmit data path. All the future packets
1402 * will get dropped until we unquiesce the data path.
1403 *
1404 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1405 * if we set that to NULL under RCU and wait for synchronize_net() to
1406 * complete, then we would have quiesced the receive data path.
1407 */
1408static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1409 struct geneve_sock **gs6)
1410{
1411 *gs4 = rtnl_dereference(geneve->sock4);
1412 rcu_assign_pointer(geneve->sock4, NULL);
1413 if (*gs4)
1414 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1415#if IS_ENABLED(CONFIG_IPV6)
1416 *gs6 = rtnl_dereference(geneve->sock6);
1417 rcu_assign_pointer(geneve->sock6, NULL);
1418 if (*gs6)
1419 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1420#else
1421 *gs6 = NULL;
1422#endif
1423 synchronize_net();
1424}
1425
1426/* Resumes the geneve device data path for both TX and RX. */
1427static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1428 struct geneve_sock __maybe_unused *gs6)
1429{
1430 rcu_assign_pointer(geneve->sock4, gs4);
1431 if (gs4)
1432 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1433#if IS_ENABLED(CONFIG_IPV6)
1434 rcu_assign_pointer(geneve->sock6, gs6);
1435 if (gs6)
1436 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1437#endif
1438 synchronize_net();
1439}
1440
1441static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1442 struct nlattr *data[],
1443 struct netlink_ext_ack *extack)
1444{
1445 struct geneve_dev *geneve = netdev_priv(dev);
1446 struct geneve_sock *gs4, *gs6;
1447 struct ip_tunnel_info info;
1448 bool metadata;
1449 bool use_udp6_rx_checksums;
1450 int err;
1451
1452 /* If the geneve device is configured for metadata (or externally
1453 * controlled, for example, OVS), then nothing can be changed.
1454 */
1455 if (geneve->collect_md)
1456 return -EOPNOTSUPP;
1457
1458 /* Start with the existing info. */
1459 memcpy(&info, &geneve->info, sizeof(info));
1460 metadata = geneve->collect_md;
1461 use_udp6_rx_checksums = geneve->use_udp6_rx_checksums;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001462 err = geneve_nl2info(tb, data, extack, &info, &metadata,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001463 &use_udp6_rx_checksums, true);
1464 if (err)
1465 return err;
1466
1467 if (!geneve_dst_addr_equal(&geneve->info, &info))
1468 dst_cache_reset(&info.dst_cache);
1469
1470 geneve_quiesce(geneve, &gs4, &gs6);
1471 geneve->info = info;
1472 geneve->collect_md = metadata;
1473 geneve->use_udp6_rx_checksums = use_udp6_rx_checksums;
1474 geneve_unquiesce(geneve, gs4, gs6);
1475
1476 return 0;
1477}
1478
John W. Linville2d07dc72015-05-13 12:57:30 -04001479static void geneve_dellink(struct net_device *dev, struct list_head *head)
1480{
1481 struct geneve_dev *geneve = netdev_priv(dev);
1482
John W. Linville2d07dc72015-05-13 12:57:30 -04001483 list_del(&geneve->next);
1484 unregister_netdevice_queue(dev, head);
1485}
1486
1487static size_t geneve_get_size(const struct net_device *dev)
1488{
1489 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001490 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001491 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001492 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001493 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001494 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001495 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001496 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1497 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1498 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001499 0;
1500}
1501
1502static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1503{
1504 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001505 struct ip_tunnel_info *info = &geneve->info;
1506 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001507 __u32 vni;
1508
pravin shelar9b4437a2016-11-21 11:02:58 -08001509 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1510 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001511 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1512 goto nla_put_failure;
1513
Eric Garver11387fe2017-05-23 18:37:27 -04001514 if (rtnl_dereference(geneve->sock4)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001515 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001516 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001517 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001518
1519 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1520 !!(info->key.tun_flags & TUNNEL_CSUM)))
1521 goto nla_put_failure;
1522
Eric Garver11387fe2017-05-23 18:37:27 -04001523 }
1524
John W. Linville8ed66f02015-10-26 17:01:44 -04001525#if IS_ENABLED(CONFIG_IPV6)
Eric Garver11387fe2017-05-23 18:37:27 -04001526 if (rtnl_dereference(geneve->sock6)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001527 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001528 &info->key.u.ipv6.dst))
1529 goto nla_put_failure;
1530
1531 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1532 !(info->key.tun_flags & TUNNEL_CSUM)))
1533 goto nla_put_failure;
1534
1535 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1536 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001537 goto nla_put_failure;
John W. Linville8ed66f02015-10-26 17:01:44 -04001538 }
Eric Garver11387fe2017-05-23 18:37:27 -04001539#endif
John W. Linville2d07dc72015-05-13 12:57:30 -04001540
pravin shelar9b4437a2016-11-21 11:02:58 -08001541 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1542 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1543 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001544 goto nla_put_failure;
1545
pravin shelar9b4437a2016-11-21 11:02:58 -08001546 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001547 goto nla_put_failure;
1548
Pravin B Shelare305ac62015-08-26 23:46:52 -07001549 if (geneve->collect_md) {
1550 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1551 goto nla_put_failure;
1552 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001553 return 0;
1554
1555nla_put_failure:
1556 return -EMSGSIZE;
1557}
1558
1559static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1560 .kind = "geneve",
1561 .maxtype = IFLA_GENEVE_MAX,
1562 .policy = geneve_policy,
1563 .priv_size = sizeof(struct geneve_dev),
1564 .setup = geneve_setup,
1565 .validate = geneve_validate,
1566 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001567 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001568 .dellink = geneve_dellink,
1569 .get_size = geneve_get_size,
1570 .fill_info = geneve_fill_info,
1571};
1572
Pravin B Shelare305ac62015-08-26 23:46:52 -07001573struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1574 u8 name_assign_type, u16 dst_port)
1575{
1576 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001577 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001578 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001579 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001580 int err;
1581
1582 memset(tb, 0, sizeof(tb));
1583 dev = rtnl_create_link(net, name, name_assign_type,
1584 &geneve_link_ops, tb);
1585 if (IS_ERR(dev))
1586 return dev;
1587
pravin shelar9b4437a2016-11-21 11:02:58 -08001588 init_tnl_info(&info, dst_port);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001589 err = geneve_configure(net, dev, NULL, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001590 if (err) {
1591 free_netdev(dev);
1592 return ERR_PTR(err);
1593 }
David Wragg7e059152016-02-10 00:05:58 +00001594
1595 /* openvswitch users expect packet sizes to be unrestricted,
1596 * so set the largest MTU we can.
1597 */
Jarod Wilson91572082016-10-20 13:55:20 -04001598 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001599 if (err)
1600 goto err;
1601
Nicolas Dichtel41009482016-06-13 10:31:07 +02001602 err = rtnl_configure_link(dev, NULL);
1603 if (err < 0)
1604 goto err;
1605
Pravin B Shelare305ac62015-08-26 23:46:52 -07001606 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001607err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001608 geneve_dellink(dev, &list_kill);
1609 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001610 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001611}
1612EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1613
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001614static int geneve_netdevice_event(struct notifier_block *unused,
1615 unsigned long event, void *ptr)
1616{
1617 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1618
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001619 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
Sabrina Dubroca04584952017-07-21 12:49:33 +02001620 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001621 geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
Sabrina Dubroca04584952017-07-21 12:49:33 +02001622 } else if (event == NETDEV_UNREGISTER) {
1623 geneve_offload_rx_ports(dev, false);
1624 } else if (event == NETDEV_REGISTER) {
1625 geneve_offload_rx_ports(dev, true);
1626 }
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001627
1628 return NOTIFY_DONE;
1629}
1630
1631static struct notifier_block geneve_notifier_block __read_mostly = {
1632 .notifier_call = geneve_netdevice_event,
1633};
1634
John W. Linville2d07dc72015-05-13 12:57:30 -04001635static __net_init int geneve_init_net(struct net *net)
1636{
1637 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001638
1639 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001640 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001641 return 0;
1642}
1643
1644static void __net_exit geneve_exit_net(struct net *net)
1645{
1646 struct geneve_net *gn = net_generic(net, geneve_net_id);
1647 struct geneve_dev *geneve, *next;
1648 struct net_device *dev, *aux;
1649 LIST_HEAD(list);
1650
1651 rtnl_lock();
1652
1653 /* gather any geneve devices that were moved into this ns */
1654 for_each_netdev_safe(net, dev, aux)
1655 if (dev->rtnl_link_ops == &geneve_link_ops)
1656 unregister_netdevice_queue(dev, &list);
1657
1658 /* now gather any other geneve devices that were created in this ns */
1659 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1660 /* If geneve->dev is in the same netns, it was already added
1661 * to the list by the previous loop.
1662 */
1663 if (!net_eq(dev_net(geneve->dev), net))
1664 unregister_netdevice_queue(geneve->dev, &list);
1665 }
1666
1667 /* unregister the devices gathered above */
1668 unregister_netdevice_many(&list);
1669 rtnl_unlock();
1670}
1671
1672static struct pernet_operations geneve_net_ops = {
1673 .init = geneve_init_net,
1674 .exit = geneve_exit_net,
1675 .id = &geneve_net_id,
1676 .size = sizeof(struct geneve_net),
1677};
1678
1679static int __init geneve_init_module(void)
1680{
1681 int rc;
1682
1683 rc = register_pernet_subsys(&geneve_net_ops);
1684 if (rc)
1685 goto out1;
1686
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001687 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001688 if (rc)
1689 goto out2;
1690
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001691 rc = rtnl_link_register(&geneve_link_ops);
1692 if (rc)
1693 goto out3;
1694
John W. Linville2d07dc72015-05-13 12:57:30 -04001695 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001696out3:
1697 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001698out2:
1699 unregister_pernet_subsys(&geneve_net_ops);
1700out1:
1701 return rc;
1702}
1703late_initcall(geneve_init_module);
1704
1705static void __exit geneve_cleanup_module(void)
1706{
1707 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001708 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001709 unregister_pernet_subsys(&geneve_net_ops);
1710}
1711module_exit(geneve_cleanup_module);
1712
1713MODULE_LICENSE("GPL");
1714MODULE_VERSION(GENEVE_NETDEV_VER);
1715MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1716MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1717MODULE_ALIAS_RTNL_LINK("geneve");