blob: de8156c6b2925741534a45a6c3a28a3afe9d1ad6 [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,
718 struct flowi4 *fl4,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800719 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700720{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100721 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700722 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100723 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700724 struct rtable *rt = NULL;
725 __u8 tos;
726
pravin shelarfceb9c32016-10-28 09:59:16 -0700727 if (!rcu_dereference(geneve->sock4))
728 return ERR_PTR(-EIO);
729
Pravin B Shelare305ac62015-08-26 23:46:52 -0700730 memset(fl4, 0, sizeof(*fl4));
731 fl4->flowi4_mark = skb->mark;
732 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800733 fl4->daddr = info->key.u.ipv4.dst;
734 fl4->saddr = info->key.u.ipv4.src;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700735
pravin shelar9b4437a2016-11-21 11:02:58 -0800736 tos = info->key.tos;
737 if ((tos == 1) && !geneve->collect_md) {
738 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
739 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100740 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800741 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100742
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800743 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100744 if (use_cache) {
745 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
746 if (rt)
747 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700748 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700749 rt = ip_route_output_key(geneve->net, fl4);
750 if (IS_ERR(rt)) {
751 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700752 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700753 }
754 if (rt->dst.dev == dev) { /* is this necessary? */
755 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700756 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700757 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700758 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100759 if (use_cache)
760 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700761 return rt;
762}
763
John W. Linville8ed66f02015-10-26 17:01:44 -0400764#if IS_ENABLED(CONFIG_IPV6)
765static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
766 struct net_device *dev,
767 struct flowi6 *fl6,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800768 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400769{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100770 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400771 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400772 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100773 struct dst_cache *dst_cache;
pravin shelarfceb9c32016-10-28 09:59:16 -0700774 struct geneve_sock *gs6;
John W. Linville3a56f862015-10-26 17:01:45 -0400775 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400776
pravin shelarfceb9c32016-10-28 09:59:16 -0700777 gs6 = rcu_dereference(geneve->sock6);
778 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
John W. Linville8ed66f02015-10-26 17:01:44 -0400830 rt = geneve_get_v4_rt(skb, dev, &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
869 dst = geneve_get_v6_dst(skb, dev, &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;
954
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400955 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
956 if (IS_ERR(rt))
957 return PTR_ERR(rt);
958
959 ip_rt_put(rt);
960 info->key.u.ipv4.src = fl4.saddr;
961#if IS_ENABLED(CONFIG_IPV6)
962 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800963 struct dst_entry *dst;
964 struct flowi6 fl6;
965
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400966 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
967 if (IS_ERR(dst))
968 return PTR_ERR(dst);
969
970 dst_release(dst);
971 info->key.u.ipv6.src = fl6.saddr;
972#endif
973 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700974 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -0400975 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700976
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700977 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
978 1, USHRT_MAX, true);
pravin shelar9b4437a2016-11-21 11:02:58 -0800979 info->key.tp_dst = geneve->info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700980 return 0;
981}
982
John W. Linville2d07dc72015-05-13 12:57:30 -0400983static const struct net_device_ops geneve_netdev_ops = {
984 .ndo_init = geneve_init,
985 .ndo_uninit = geneve_uninit,
986 .ndo_open = geneve_open,
987 .ndo_stop = geneve_stop,
988 .ndo_start_xmit = geneve_xmit,
989 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +0000990 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -0400991 .ndo_validate_addr = eth_validate_addr,
992 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700993 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -0400994};
995
996static void geneve_get_drvinfo(struct net_device *dev,
997 struct ethtool_drvinfo *drvinfo)
998{
999 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1000 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1001}
1002
1003static const struct ethtool_ops geneve_ethtool_ops = {
1004 .get_drvinfo = geneve_get_drvinfo,
1005 .get_link = ethtool_op_get_link,
1006};
1007
1008/* Info for udev, that this is a virtual tunnel endpoint */
1009static struct device_type geneve_type = {
1010 .name = "geneve",
1011};
1012
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001013/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001014 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001015 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001016 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001017static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001018{
1019 struct net *net = dev_net(dev);
1020 struct geneve_net *gn = net_generic(net, geneve_net_id);
1021 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001022
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001023 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -07001024 list_for_each_entry_rcu(gs, &gn->sock_list, list)
1025 udp_tunnel_push_rx_port(dev, gs->sock,
1026 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001027 rcu_read_unlock();
1028}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001029
John W. Linville2d07dc72015-05-13 12:57:30 -04001030/* Initialize the device structure. */
1031static void geneve_setup(struct net_device *dev)
1032{
1033 ether_setup(dev);
1034
1035 dev->netdev_ops = &geneve_netdev_ops;
1036 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001037 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001038
1039 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1040
John W. Linville2d07dc72015-05-13 12:57:30 -04001041 dev->features |= NETIF_F_LLTX;
1042 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1043 dev->features |= NETIF_F_RXCSUM;
1044 dev->features |= NETIF_F_GSO_SOFTWARE;
1045
John W. Linville2d07dc72015-05-13 12:57:30 -04001046 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1047 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001048
Jarod Wilson91572082016-10-20 13:55:20 -04001049 /* MTU range: 68 - (something less than 65535) */
1050 dev->min_mtu = ETH_MIN_MTU;
1051 /* The max_mtu calculation does not take account of GENEVE
1052 * options, to avoid excluding potentially valid
1053 * configurations. This will be further reduced by IPvX hdr size.
1054 */
1055 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1056
John W. Linville2d07dc72015-05-13 12:57:30 -04001057 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001058 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001059 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001060 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001061}
1062
1063static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1064 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1065 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001066 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001067 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001068 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001069 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001070 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001071 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001072 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1073 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1074 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001075};
1076
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001077static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1078 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001079{
1080 if (tb[IFLA_ADDRESS]) {
1081 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1082 return -EINVAL;
1083
1084 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1085 return -EADDRNOTAVAIL;
1086 }
1087
1088 if (!data)
1089 return -EINVAL;
1090
1091 if (data[IFLA_GENEVE_ID]) {
1092 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1093
1094 if (vni >= GENEVE_VID_MASK)
1095 return -ERANGE;
1096 }
1097
1098 return 0;
1099}
1100
Pravin B Shelar371bd102015-08-26 23:46:54 -07001101static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001102 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001103 bool *tun_on_same_port,
1104 bool *tun_collect_md)
1105{
pravin shelar9b4437a2016-11-21 11:02:58 -08001106 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001107
1108 *tun_on_same_port = false;
1109 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001110 list_for_each_entry(geneve, &gn->geneve_list, next) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001111 if (info->key.tp_dst == geneve->info.key.tp_dst) {
Pravin B Shelar371bd102015-08-26 23:46:54 -07001112 *tun_collect_md = geneve->collect_md;
1113 *tun_on_same_port = true;
1114 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001115 if (info->key.tun_id == geneve->info.key.tun_id &&
1116 info->key.tp_dst == geneve->info.key.tp_dst &&
1117 !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001118 t = geneve;
1119 }
1120 return t;
1121}
1122
pravin shelar9b4437a2016-11-21 11:02:58 -08001123static bool is_all_zero(const u8 *fp, size_t size)
1124{
1125 int i;
1126
1127 for (i = 0; i < size; i++)
1128 if (fp[i])
1129 return false;
1130 return true;
1131}
1132
1133static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1134{
1135 if (info->key.tun_id || info->key.tun_flags || info->key.tos ||
1136 info->key.ttl || info->key.label || info->key.tp_src ||
1137 !is_all_zero((const u8 *)&info->key.u, sizeof(info->key.u)))
1138 return false;
1139 else
1140 return true;
1141}
1142
Pravin B Shelare305ac62015-08-26 23:46:52 -07001143static int geneve_configure(struct net *net, struct net_device *dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001144 const struct ip_tunnel_info *info,
1145 bool metadata, bool ipv6_rx_csum)
John W. Linville2d07dc72015-05-13 12:57:30 -04001146{
1147 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001148 struct geneve_dev *t, *geneve = netdev_priv(dev);
1149 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001150 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001151
pravin shelar9b4437a2016-11-21 11:02:58 -08001152 if (metadata && !is_tnl_info_zero(info))
John W. Linville8ed66f02015-10-26 17:01:44 -04001153 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001154
1155 geneve->net = net;
1156 geneve->dev = dev;
1157
pravin shelar9b4437a2016-11-21 11:02:58 -08001158 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001159 if (t)
1160 return -EBUSY;
1161
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001162 /* make enough headroom for basic scenario */
1163 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Eric Garver9a1c44d2017-06-02 14:54:10 -04001164 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001165 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001166 dev->max_mtu -= sizeof(struct iphdr);
1167 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001168 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001169 dev->max_mtu -= sizeof(struct ipv6hdr);
1170 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001171 dev->needed_headroom = encap_len + ETH_HLEN;
1172
Pravin B Shelar371bd102015-08-26 23:46:54 -07001173 if (metadata) {
1174 if (tun_on_same_port)
1175 return -EPERM;
1176 } else {
1177 if (tun_collect_md)
1178 return -EPERM;
1179 }
1180
pravin shelar9b4437a2016-11-21 11:02:58 -08001181 dst_cache_reset(&geneve->info.dst_cache);
1182 geneve->info = *info;
1183 geneve->collect_md = metadata;
1184 geneve->use_udp6_rx_checksums = ipv6_rx_csum;
Paolo Abeni468dfff2016-02-12 15:43:58 +01001185
John W. Linville2d07dc72015-05-13 12:57:30 -04001186 err = register_netdevice(dev);
1187 if (err)
1188 return err;
1189
1190 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001191 return 0;
1192}
1193
pravin shelar9b4437a2016-11-21 11:02:58 -08001194static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1195{
1196 memset(info, 0, sizeof(*info));
1197 info->key.tp_dst = htons(dst_port);
1198}
1199
Pravin B Shelare305ac62015-08-26 23:46:52 -07001200static int geneve_newlink(struct net *net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001201 struct nlattr *tb[], struct nlattr *data[],
1202 struct netlink_ext_ack *extack)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001203{
pravin shelar9b4437a2016-11-21 11:02:58 -08001204 bool use_udp6_rx_checksums = false;
1205 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001206 bool metadata = false;
pravin shelar9b4437a2016-11-21 11:02:58 -08001207
1208 init_tnl_info(&info, GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001209
John W. Linville8ed66f02015-10-26 17:01:44 -04001210 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1211 return -EINVAL;
1212
1213 if (data[IFLA_GENEVE_REMOTE]) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001214 info.key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001215 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001216
pravin shelar9b4437a2016-11-21 11:02:58 -08001217 if (IN_MULTICAST(ntohl(info.key.u.ipv4.dst))) {
1218 netdev_dbg(dev, "multicast remote is unsupported\n");
John W. Linville8ed66f02015-10-26 17:01:44 -04001219 return -EINVAL;
1220 }
1221 }
1222
pravin shelar9b4437a2016-11-21 11:02:58 -08001223 if (data[IFLA_GENEVE_REMOTE6]) {
1224 #if IS_ENABLED(CONFIG_IPV6)
1225 info.mode = IP_TUNNEL_INFO_IPV6;
1226 info.key.u.ipv6.dst =
1227 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001228
pravin shelar9b4437a2016-11-21 11:02:58 -08001229 if (ipv6_addr_type(&info.key.u.ipv6.dst) &
1230 IPV6_ADDR_LINKLOCAL) {
1231 netdev_dbg(dev, "link-local remote is unsupported\n");
1232 return -EINVAL;
1233 }
1234 if (ipv6_addr_is_multicast(&info.key.u.ipv6.dst)) {
1235 netdev_dbg(dev, "multicast remote is unsupported\n");
1236 return -EINVAL;
1237 }
1238 info.key.tun_flags |= TUNNEL_CSUM;
1239 use_udp6_rx_checksums = true;
1240#else
1241 return -EPFNOSUPPORT;
1242#endif
1243 }
1244
1245 if (data[IFLA_GENEVE_ID]) {
1246 __u32 vni;
1247 __u8 tvni[3];
1248
1249 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1250 tvni[0] = (vni & 0x00ff0000) >> 16;
1251 tvni[1] = (vni & 0x0000ff00) >> 8;
1252 tvni[2] = vni & 0x000000ff;
1253
1254 info.key.tun_id = vni_to_tunnel_id(tvni);
1255 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001256 if (data[IFLA_GENEVE_TTL])
pravin shelar9b4437a2016-11-21 11:02:58 -08001257 info.key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001258
1259 if (data[IFLA_GENEVE_TOS])
pravin shelar9b4437a2016-11-21 11:02:58 -08001260 info.key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001261
pravin shelar9b4437a2016-11-21 11:02:58 -08001262 if (data[IFLA_GENEVE_LABEL]) {
1263 info.key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1264 IPV6_FLOWLABEL_MASK;
1265 if (info.key.label && (!(info.mode & IP_TUNNEL_INFO_IPV6)))
1266 return -EINVAL;
1267 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001268
Pravin B Shelare305ac62015-08-26 23:46:52 -07001269 if (data[IFLA_GENEVE_PORT])
pravin shelar9b4437a2016-11-21 11:02:58 -08001270 info.key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001271
1272 if (data[IFLA_GENEVE_COLLECT_METADATA])
1273 metadata = true;
1274
Tom Herbertabe492b2015-12-10 12:37:45 -08001275 if (data[IFLA_GENEVE_UDP_CSUM] &&
Girish Moodalbail5e0740c2017-04-27 14:11:53 -07001276 nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001277 info.key.tun_flags |= TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001278
1279 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1280 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001281 info.key.tun_flags &= ~TUNNEL_CSUM;
Tom Herbertabe492b2015-12-10 12:37:45 -08001282
1283 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1284 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
pravin shelar9b4437a2016-11-21 11:02:58 -08001285 use_udp6_rx_checksums = false;
Tom Herbertabe492b2015-12-10 12:37:45 -08001286
pravin shelar9b4437a2016-11-21 11:02:58 -08001287 return geneve_configure(net, dev, &info, metadata, use_udp6_rx_checksums);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001288}
1289
John W. Linville2d07dc72015-05-13 12:57:30 -04001290static void geneve_dellink(struct net_device *dev, struct list_head *head)
1291{
1292 struct geneve_dev *geneve = netdev_priv(dev);
1293
John W. Linville2d07dc72015-05-13 12:57:30 -04001294 list_del(&geneve->next);
1295 unregister_netdevice_queue(dev, head);
1296}
1297
1298static size_t geneve_get_size(const struct net_device *dev)
1299{
1300 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001301 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001302 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001303 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001304 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001305 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001306 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001307 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1308 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1309 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001310 0;
1311}
1312
1313static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1314{
1315 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelar9b4437a2016-11-21 11:02:58 -08001316 struct ip_tunnel_info *info = &geneve->info;
1317 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001318 __u32 vni;
1319
pravin shelar9b4437a2016-11-21 11:02:58 -08001320 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1321 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001322 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1323 goto nla_put_failure;
1324
Eric Garver11387fe2017-05-23 18:37:27 -04001325 if (rtnl_dereference(geneve->sock4)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001326 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001327 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001328 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001329
1330 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1331 !!(info->key.tun_flags & TUNNEL_CSUM)))
1332 goto nla_put_failure;
1333
Eric Garver11387fe2017-05-23 18:37:27 -04001334 }
1335
John W. Linville8ed66f02015-10-26 17:01:44 -04001336#if IS_ENABLED(CONFIG_IPV6)
Eric Garver11387fe2017-05-23 18:37:27 -04001337 if (rtnl_dereference(geneve->sock6)) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001338 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001339 &info->key.u.ipv6.dst))
1340 goto nla_put_failure;
1341
1342 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1343 !(info->key.tun_flags & TUNNEL_CSUM)))
1344 goto nla_put_failure;
1345
1346 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1347 !geneve->use_udp6_rx_checksums))
John W. Linville8ed66f02015-10-26 17:01:44 -04001348 goto nla_put_failure;
John W. Linville8ed66f02015-10-26 17:01:44 -04001349 }
Eric Garver11387fe2017-05-23 18:37:27 -04001350#endif
John W. Linville2d07dc72015-05-13 12:57:30 -04001351
pravin shelar9b4437a2016-11-21 11:02:58 -08001352 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1353 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1354 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001355 goto nla_put_failure;
1356
pravin shelar9b4437a2016-11-21 11:02:58 -08001357 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001358 goto nla_put_failure;
1359
Pravin B Shelare305ac62015-08-26 23:46:52 -07001360 if (geneve->collect_md) {
1361 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1362 goto nla_put_failure;
1363 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001364 return 0;
1365
1366nla_put_failure:
1367 return -EMSGSIZE;
1368}
1369
1370static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1371 .kind = "geneve",
1372 .maxtype = IFLA_GENEVE_MAX,
1373 .policy = geneve_policy,
1374 .priv_size = sizeof(struct geneve_dev),
1375 .setup = geneve_setup,
1376 .validate = geneve_validate,
1377 .newlink = geneve_newlink,
1378 .dellink = geneve_dellink,
1379 .get_size = geneve_get_size,
1380 .fill_info = geneve_fill_info,
1381};
1382
Pravin B Shelare305ac62015-08-26 23:46:52 -07001383struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1384 u8 name_assign_type, u16 dst_port)
1385{
1386 struct nlattr *tb[IFLA_MAX + 1];
pravin shelar9b4437a2016-11-21 11:02:58 -08001387 struct ip_tunnel_info info;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001388 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001389 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001390 int err;
1391
1392 memset(tb, 0, sizeof(tb));
1393 dev = rtnl_create_link(net, name, name_assign_type,
1394 &geneve_link_ops, tb);
1395 if (IS_ERR(dev))
1396 return dev;
1397
pravin shelar9b4437a2016-11-21 11:02:58 -08001398 init_tnl_info(&info, dst_port);
1399 err = geneve_configure(net, dev, &info, true, true);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001400 if (err) {
1401 free_netdev(dev);
1402 return ERR_PTR(err);
1403 }
David Wragg7e059152016-02-10 00:05:58 +00001404
1405 /* openvswitch users expect packet sizes to be unrestricted,
1406 * so set the largest MTU we can.
1407 */
Jarod Wilson91572082016-10-20 13:55:20 -04001408 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001409 if (err)
1410 goto err;
1411
Nicolas Dichtel41009482016-06-13 10:31:07 +02001412 err = rtnl_configure_link(dev, NULL);
1413 if (err < 0)
1414 goto err;
1415
Pravin B Shelare305ac62015-08-26 23:46:52 -07001416 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001417err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001418 geneve_dellink(dev, &list_kill);
1419 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001420 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001421}
1422EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1423
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001424static int geneve_netdevice_event(struct notifier_block *unused,
1425 unsigned long event, void *ptr)
1426{
1427 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1428
Alexander Duyck7c46a642016-06-16 12:21:00 -07001429 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001430 geneve_push_rx_ports(dev);
1431
1432 return NOTIFY_DONE;
1433}
1434
1435static struct notifier_block geneve_notifier_block __read_mostly = {
1436 .notifier_call = geneve_netdevice_event,
1437};
1438
John W. Linville2d07dc72015-05-13 12:57:30 -04001439static __net_init int geneve_init_net(struct net *net)
1440{
1441 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001442
1443 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001444 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001445 return 0;
1446}
1447
1448static void __net_exit geneve_exit_net(struct net *net)
1449{
1450 struct geneve_net *gn = net_generic(net, geneve_net_id);
1451 struct geneve_dev *geneve, *next;
1452 struct net_device *dev, *aux;
1453 LIST_HEAD(list);
1454
1455 rtnl_lock();
1456
1457 /* gather any geneve devices that were moved into this ns */
1458 for_each_netdev_safe(net, dev, aux)
1459 if (dev->rtnl_link_ops == &geneve_link_ops)
1460 unregister_netdevice_queue(dev, &list);
1461
1462 /* now gather any other geneve devices that were created in this ns */
1463 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1464 /* If geneve->dev is in the same netns, it was already added
1465 * to the list by the previous loop.
1466 */
1467 if (!net_eq(dev_net(geneve->dev), net))
1468 unregister_netdevice_queue(geneve->dev, &list);
1469 }
1470
1471 /* unregister the devices gathered above */
1472 unregister_netdevice_many(&list);
1473 rtnl_unlock();
1474}
1475
1476static struct pernet_operations geneve_net_ops = {
1477 .init = geneve_init_net,
1478 .exit = geneve_exit_net,
1479 .id = &geneve_net_id,
1480 .size = sizeof(struct geneve_net),
1481};
1482
1483static int __init geneve_init_module(void)
1484{
1485 int rc;
1486
1487 rc = register_pernet_subsys(&geneve_net_ops);
1488 if (rc)
1489 goto out1;
1490
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001491 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001492 if (rc)
1493 goto out2;
1494
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001495 rc = rtnl_link_register(&geneve_link_ops);
1496 if (rc)
1497 goto out3;
1498
John W. Linville2d07dc72015-05-13 12:57:30 -04001499 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001500out3:
1501 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001502out2:
1503 unregister_pernet_subsys(&geneve_net_ops);
1504out1:
1505 return rc;
1506}
1507late_initcall(geneve_init_module);
1508
1509static void __exit geneve_cleanup_module(void)
1510{
1511 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001512 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001513 unregister_pernet_subsys(&geneve_net_ops);
1514}
1515module_exit(geneve_cleanup_module);
1516
1517MODULE_LICENSE("GPL");
1518MODULE_VERSION(GENEVE_NETDEV_VER);
1519MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1520MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1521MODULE_ALIAS_RTNL_LINK("geneve");