blob: 8fa8388cc5d41941ee4052e60c186eea2bb9914d [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>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/hash.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070018#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070019#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040020#include <net/rtnetlink.h>
21#include <net/geneve.h>
Pravin B Shelar371bd102015-08-26 23:46:54 -070022#include <net/protocol.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040023
24#define GENEVE_NETDEV_VER "0.6"
25
26#define GENEVE_UDP_PORT 6081
27
28#define GENEVE_N_VID (1u << 24)
29#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
30
31#define VNI_HASH_BITS 10
32#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
33
34static bool log_ecn_error = true;
35module_param(log_ecn_error, bool, 0644);
36MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
37
Pravin B Shelar371bd102015-08-26 23:46:54 -070038#define GENEVE_VER 0
39#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
40
John W. Linville2d07dc72015-05-13 12:57:30 -040041/* per-network namespace private data for this module */
42struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070044 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040045};
46
Pravin B Shelar371bd102015-08-26 23:46:54 -070047static int geneve_net_id;
48
John W. Linville8ed66f02015-10-26 17:01:44 -040049union geneve_addr {
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
52 struct sockaddr sa;
53};
54
55static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
56
John W. Linville2d07dc72015-05-13 12:57:30 -040057/* Pseudo network device */
58struct geneve_dev {
59 struct hlist_node hlist; /* vni hash table */
60 struct net *net; /* netns for packet i/o */
61 struct net_device *dev; /* netdev for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040062 struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */
63#if IS_ENABLED(CONFIG_IPV6)
64 struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */
65#endif
John W. Linville8760ce52015-06-01 15:51:34 -040066 u8 vni[3]; /* virtual network ID for tunnel */
67 u8 ttl; /* TTL override */
John W. Linvilled8951122015-06-01 15:51:35 -040068 u8 tos; /* TOS override */
John W. Linville8ed66f02015-10-26 17:01:44 -040069 union geneve_addr remote; /* IP address for link partner */
John W. Linville2d07dc72015-05-13 12:57:30 -040070 struct list_head next; /* geneve's per namespace list */
Pravin B Shelarcd7918b2015-08-26 23:46:51 -070071 __be16 dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -070072 bool collect_md;
Jesse Gross8e816df2015-08-28 16:54:40 -070073 struct gro_cells gro_cells;
Tom Herbertabe492b2015-12-10 12:37:45 -080074 u32 flags;
Paolo Abeni468dfff2016-02-12 15:43:58 +010075 struct dst_cache dst_cache;
John W. Linville2d07dc72015-05-13 12:57:30 -040076};
77
Tom Herbertabe492b2015-12-10 12:37:45 -080078/* Geneve device flags */
Alexander Duyck14f1f722016-02-19 11:26:24 -080079#define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
Tom Herbertabe492b2015-12-10 12:37:45 -080080#define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
81#define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
82
Pravin B Shelar371bd102015-08-26 23:46:54 -070083struct geneve_sock {
84 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070085 struct list_head list;
86 struct socket *sock;
87 struct rcu_head rcu;
88 int refcnt;
89 struct udp_offload udp_offloads;
Pravin B Shelar66d47002015-08-26 23:46:55 -070090 struct hlist_head vni_list[VNI_HASH_SIZE];
Tom Herbertabe492b2015-12-10 12:37:45 -080091 u32 flags;
Pravin B Shelar371bd102015-08-26 23:46:54 -070092};
John W. Linville2d07dc72015-05-13 12:57:30 -040093
94static inline __u32 geneve_net_vni_hash(u8 vni[3])
95{
96 __u32 vnid;
97
98 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
99 return hash_32(vnid, VNI_HASH_BITS);
100}
101
Pravin B Shelare305ac62015-08-26 23:46:52 -0700102static __be64 vni_to_tunnel_id(const __u8 *vni)
103{
104#ifdef __BIG_ENDIAN
105 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
106#else
107 return (__force __be64)(((__force u64)vni[0] << 40) |
108 ((__force u64)vni[1] << 48) |
109 ((__force u64)vni[2] << 56));
110#endif
111}
112
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100113static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
114{
115 return gs->sock->sk->sk_family;
116}
117
Pravin B Shelar66d47002015-08-26 23:46:55 -0700118static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700119 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400120{
John W. Linville2d07dc72015-05-13 12:57:30 -0400121 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700122 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400123 __u32 hash;
124
John W. Linville2d07dc72015-05-13 12:57:30 -0400125 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700126 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700127 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700128 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700129 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400130 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700131 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400132 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700133 return NULL;
134}
135
John W. Linville8ed66f02015-10-26 17:01:44 -0400136#if IS_ENABLED(CONFIG_IPV6)
137static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
138 struct in6_addr addr6, u8 vni[])
139{
140 struct hlist_head *vni_list_head;
141 struct geneve_dev *geneve;
142 __u32 hash;
143
144 /* Find the device for this VNI */
145 hash = geneve_net_vni_hash(vni);
146 vni_list_head = &gs->vni_list[hash];
147 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
148 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
149 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
150 return geneve;
151 }
152 return NULL;
153}
154#endif
155
Pravin B Shelar371bd102015-08-26 23:46:54 -0700156static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
157{
158 return (struct genevehdr *)(udp_hdr(skb) + 1);
159}
160
Jiri Benc9fc47542016-02-18 11:22:50 +0100161static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
162 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700163{
Jiri Benc9fc47542016-02-18 11:22:50 +0100164 u8 *vni;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700165 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400166 static u8 zero_vni[3];
John W. Linville8ed66f02015-10-26 17:01:44 -0400167#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400168 static struct in6_addr zero_addr6;
169#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700170
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100171 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100172 struct iphdr *iph;
173
John W. Linville8ed66f02015-10-26 17:01:44 -0400174 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700175
John W. Linville8ed66f02015-10-26 17:01:44 -0400176 if (gs->collect_md) {
177 vni = zero_vni;
178 addr = 0;
179 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100180 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400181 addr = iph->saddr;
182 }
183
Jiri Benc9fc47542016-02-18 11:22:50 +0100184 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400185#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100186 } else if (geneve_get_sk_family(gs) == AF_INET6) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100187 struct ipv6hdr *ip6h;
188 struct in6_addr addr6;
189
John W. Linville8ed66f02015-10-26 17:01:44 -0400190 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
191
192 if (gs->collect_md) {
193 vni = zero_vni;
194 addr6 = zero_addr6;
195 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100196 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400197 addr6 = ip6h->saddr;
198 }
199
Jiri Benc9fc47542016-02-18 11:22:50 +0100200 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400201#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700202 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100203 return NULL;
204}
205
206/* geneve receive/decap routine */
207static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
208 struct sk_buff *skb)
209{
210 struct genevehdr *gnvh = geneve_hdr(skb);
211 struct metadata_dst *tun_dst = NULL;
212 struct pcpu_sw_netstats *stats;
213 int err = 0;
214 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400215
Pravin B Shelar371bd102015-08-26 23:46:54 -0700216 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700217 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700218
219 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
220 (gnvh->oam ? TUNNEL_OAM : 0) |
221 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
222
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100223 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700224 vni_to_tunnel_id(gnvh->vni),
225 gnvh->opt_len * 4);
226 if (!tun_dst)
227 goto drop;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700228 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700229 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
230 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700231 } else {
232 /* Drop packets w/ critical options,
233 * since we don't support any...
234 */
235 if (gnvh->critical)
236 goto drop;
237 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400238
239 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400240 skb->protocol = eth_type_trans(skb, geneve->dev);
241 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
242
Pravin B Shelare305ac62015-08-26 23:46:52 -0700243 if (tun_dst)
244 skb_dst_set(skb, &tun_dst->dst);
245
John W. Linville2d07dc72015-05-13 12:57:30 -0400246 /* Ignore packet loops (and multicast echo) */
247 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
248 goto drop;
249
Jiri Benc9fc47542016-02-18 11:22:50 +0100250 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400251 skb_reset_network_header(skb);
252
Jiri Benc9fc47542016-02-18 11:22:50 +0100253 if (geneve_get_sk_family(gs) == AF_INET)
254 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400255#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100256 else
257 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400258#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400259
260 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400261 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100262 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400263 net_info_ratelimited("non-ECT from %pI4 "
264 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100265 &((struct iphdr *)oiph)->saddr,
266 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400267#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100268 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400269 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100270 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400271#endif
272 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400273 if (err > 1) {
274 ++geneve->dev->stats.rx_frame_errors;
275 ++geneve->dev->stats.rx_errors;
276 goto drop;
277 }
278 }
279
280 stats = this_cpu_ptr(geneve->dev->tstats);
281 u64_stats_update_begin(&stats->syncp);
282 stats->rx_packets++;
283 stats->rx_bytes += skb->len;
284 u64_stats_update_end(&stats->syncp);
285
Jesse Gross8e816df2015-08-28 16:54:40 -0700286 gro_cells_receive(&geneve->gro_cells, skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400287 return;
288drop:
289 /* Consume bad packet */
290 kfree_skb(skb);
291}
292
293/* Setup stats when device is created */
294static int geneve_init(struct net_device *dev)
295{
Jesse Gross8e816df2015-08-28 16:54:40 -0700296 struct geneve_dev *geneve = netdev_priv(dev);
297 int err;
298
John W. Linville2d07dc72015-05-13 12:57:30 -0400299 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
300 if (!dev->tstats)
301 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700302
303 err = gro_cells_init(&geneve->gro_cells, dev);
304 if (err) {
305 free_percpu(dev->tstats);
306 return err;
307 }
308
Paolo Abeni468dfff2016-02-12 15:43:58 +0100309 err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL);
310 if (err) {
311 free_percpu(dev->tstats);
312 gro_cells_destroy(&geneve->gro_cells);
313 return err;
314 }
315
John W. Linville2d07dc72015-05-13 12:57:30 -0400316 return 0;
317}
318
319static void geneve_uninit(struct net_device *dev)
320{
Jesse Gross8e816df2015-08-28 16:54:40 -0700321 struct geneve_dev *geneve = netdev_priv(dev);
322
Paolo Abeni468dfff2016-02-12 15:43:58 +0100323 dst_cache_destroy(&geneve->dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700324 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400325 free_percpu(dev->tstats);
326}
327
Pravin B Shelar371bd102015-08-26 23:46:54 -0700328/* Callback from net/ipv4/udp.c to receive packets */
329static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
330{
331 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100332 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700333 struct geneve_sock *gs;
334 int opts_len;
335
336 /* Need Geneve and inner Ethernet header to be present */
337 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
338 goto error;
339
340 /* Return packets with reserved bits set */
341 geneveh = geneve_hdr(skb);
342 if (unlikely(geneveh->ver != GENEVE_VER))
343 goto error;
344
345 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
346 goto error;
347
Jiri Benc9fc47542016-02-18 11:22:50 +0100348 gs = rcu_dereference_sk_user_data(sk);
349 if (!gs)
350 goto drop;
351
352 geneve = geneve_lookup_skb(gs, skb);
353 if (!geneve)
354 goto drop;
355
Pravin B Shelar371bd102015-08-26 23:46:54 -0700356 opts_len = geneveh->opt_len * 4;
357 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100358 htons(ETH_P_TEB),
359 !net_eq(geneve->net, dev_net(geneve->dev))))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700360 goto drop;
361
Jiri Benc9fc47542016-02-18 11:22:50 +0100362 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700363 return 0;
364
365drop:
366 /* Consume bad packet */
367 kfree_skb(skb);
368 return 0;
369
370error:
371 /* Let the UDP layer deal with the skb */
372 return 1;
373}
374
375static struct socket *geneve_create_sock(struct net *net, bool ipv6,
Tom Herbertabe492b2015-12-10 12:37:45 -0800376 __be16 port, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700377{
378 struct socket *sock;
379 struct udp_port_cfg udp_conf;
380 int err;
381
382 memset(&udp_conf, 0, sizeof(udp_conf));
383
384 if (ipv6) {
385 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400386 udp_conf.ipv6_v6only = 1;
Tom Herbertabe492b2015-12-10 12:37:45 -0800387 udp_conf.use_udp6_rx_checksums =
388 !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700389 } else {
390 udp_conf.family = AF_INET;
391 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
392 }
393
394 udp_conf.local_udp_port = port;
395
396 /* Open UDP socket */
397 err = udp_sock_create(net, &udp_conf, &sock);
398 if (err < 0)
399 return ERR_PTR(err);
400
401 return sock;
402}
403
404static void geneve_notify_add_rx_port(struct geneve_sock *gs)
405{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800406 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700407 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800408 struct net *net = sock_net(sk);
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100409 sa_family_t sa_family = geneve_get_sk_family(gs);
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800410 __be16 port = inet_sk(sk)->inet_sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700411 int err;
412
413 if (sa_family == AF_INET) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100414 err = udp_add_offload(sock_net(sk), &gs->udp_offloads);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700415 if (err)
416 pr_warn("geneve: udp_add_offload failed with status %d\n",
417 err);
418 }
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800419
420 rcu_read_lock();
421 for_each_netdev_rcu(net, dev) {
422 if (dev->netdev_ops->ndo_add_geneve_port)
423 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family,
424 port);
425 }
426 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700427}
428
429static int geneve_hlen(struct genevehdr *gh)
430{
431 return sizeof(*gh) + gh->opt_len * 4;
432}
433
434static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
435 struct sk_buff *skb,
436 struct udp_offload *uoff)
437{
438 struct sk_buff *p, **pp = NULL;
439 struct genevehdr *gh, *gh2;
440 unsigned int hlen, gh_len, off_gnv;
441 const struct packet_offload *ptype;
442 __be16 type;
443 int flush = 1;
444
445 off_gnv = skb_gro_offset(skb);
446 hlen = off_gnv + sizeof(*gh);
447 gh = skb_gro_header_fast(skb, off_gnv);
448 if (skb_gro_header_hard(skb, hlen)) {
449 gh = skb_gro_header_slow(skb, hlen, off_gnv);
450 if (unlikely(!gh))
451 goto out;
452 }
453
454 if (gh->ver != GENEVE_VER || gh->oam)
455 goto out;
456 gh_len = geneve_hlen(gh);
457
458 hlen = off_gnv + gh_len;
459 if (skb_gro_header_hard(skb, hlen)) {
460 gh = skb_gro_header_slow(skb, hlen, off_gnv);
461 if (unlikely(!gh))
462 goto out;
463 }
464
465 flush = 0;
466
467 for (p = *head; p; p = p->next) {
468 if (!NAPI_GRO_CB(p)->same_flow)
469 continue;
470
471 gh2 = (struct genevehdr *)(p->data + off_gnv);
472 if (gh->opt_len != gh2->opt_len ||
473 memcmp(gh, gh2, gh_len)) {
474 NAPI_GRO_CB(p)->same_flow = 0;
475 continue;
476 }
477 }
478
479 type = gh->proto_type;
480
481 rcu_read_lock();
482 ptype = gro_find_receive_by_type(type);
483 if (!ptype) {
484 flush = 1;
485 goto out_unlock;
486 }
487
488 skb_gro_pull(skb, gh_len);
489 skb_gro_postpull_rcsum(skb, gh, gh_len);
490 pp = ptype->callbacks.gro_receive(head, skb);
491
492out_unlock:
493 rcu_read_unlock();
494out:
495 NAPI_GRO_CB(skb)->flush |= flush;
496
497 return pp;
498}
499
500static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
501 struct udp_offload *uoff)
502{
503 struct genevehdr *gh;
504 struct packet_offload *ptype;
505 __be16 type;
506 int gh_len;
507 int err = -ENOSYS;
508
509 udp_tunnel_gro_complete(skb, nhoff);
510
511 gh = (struct genevehdr *)(skb->data + nhoff);
512 gh_len = geneve_hlen(gh);
513 type = gh->proto_type;
514
515 rcu_read_lock();
516 ptype = gro_find_complete_by_type(type);
517 if (ptype)
518 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
519
520 rcu_read_unlock();
521 return err;
522}
523
524/* Create new listen socket if needed */
525static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
Tom Herbertabe492b2015-12-10 12:37:45 -0800526 bool ipv6, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700527{
528 struct geneve_net *gn = net_generic(net, geneve_net_id);
529 struct geneve_sock *gs;
530 struct socket *sock;
531 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700532 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700533
534 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
535 if (!gs)
536 return ERR_PTR(-ENOMEM);
537
Tom Herbertabe492b2015-12-10 12:37:45 -0800538 sock = geneve_create_sock(net, ipv6, port, flags);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700539 if (IS_ERR(sock)) {
540 kfree(gs);
541 return ERR_CAST(sock);
542 }
543
544 gs->sock = sock;
545 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700546 for (h = 0; h < VNI_HASH_SIZE; ++h)
547 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700548
549 /* Initialize the geneve udp offloads structure */
550 gs->udp_offloads.port = port;
551 gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
552 gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
553 geneve_notify_add_rx_port(gs);
554
555 /* Mark socket as an encapsulation socket */
556 tunnel_cfg.sk_user_data = gs;
557 tunnel_cfg.encap_type = 1;
558 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
559 tunnel_cfg.encap_destroy = NULL;
560 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700561 list_add(&gs->list, &gn->sock_list);
562 return gs;
563}
564
565static void geneve_notify_del_rx_port(struct geneve_sock *gs)
566{
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800567 struct net_device *dev;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700568 struct sock *sk = gs->sock->sk;
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800569 struct net *net = sock_net(sk);
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100570 sa_family_t sa_family = geneve_get_sk_family(gs);
Singhai, Anjalia8170d22015-12-14 12:21:17 -0800571 __be16 port = inet_sk(sk)->inet_sport;
572
573 rcu_read_lock();
574 for_each_netdev_rcu(net, dev) {
575 if (dev->netdev_ops->ndo_del_geneve_port)
576 dev->netdev_ops->ndo_del_geneve_port(dev, sa_family,
577 port);
578 }
579
580 rcu_read_unlock();
Pravin B Shelar371bd102015-08-26 23:46:54 -0700581
582 if (sa_family == AF_INET)
583 udp_del_offload(&gs->udp_offloads);
584}
585
John W. Linville8ed66f02015-10-26 17:01:44 -0400586static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700587{
John W. Linville8ed66f02015-10-26 17:01:44 -0400588 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700589 return;
590
591 list_del(&gs->list);
592 geneve_notify_del_rx_port(gs);
593 udp_tunnel_sock_release(gs->sock);
594 kfree_rcu(gs, rcu);
595}
596
John W. Linville8ed66f02015-10-26 17:01:44 -0400597static void geneve_sock_release(struct geneve_dev *geneve)
598{
599 __geneve_sock_release(geneve->sock4);
600#if IS_ENABLED(CONFIG_IPV6)
601 __geneve_sock_release(geneve->sock6);
602#endif
603}
604
Pravin B Shelar371bd102015-08-26 23:46:54 -0700605static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400606 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700607 __be16 dst_port)
608{
609 struct geneve_sock *gs;
610
611 list_for_each_entry(gs, &gn->sock_list, list) {
612 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100613 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700614 return gs;
615 }
616 }
617 return NULL;
618}
619
John W. Linville8ed66f02015-10-26 17:01:44 -0400620static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400621{
John W. Linville2d07dc72015-05-13 12:57:30 -0400622 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700623 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400624 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700625 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400626
John W. Linville8ed66f02015-10-26 17:01:44 -0400627 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700628 if (gs) {
629 gs->refcnt++;
630 goto out;
631 }
632
Tom Herbertabe492b2015-12-10 12:37:45 -0800633 gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
John W. Linville2d07dc72015-05-13 12:57:30 -0400634 if (IS_ERR(gs))
635 return PTR_ERR(gs);
636
Pravin B Shelar371bd102015-08-26 23:46:54 -0700637out:
638 gs->collect_md = geneve->collect_md;
Tom Herbertabe492b2015-12-10 12:37:45 -0800639 gs->flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400640#if IS_ENABLED(CONFIG_IPV6)
641 if (ipv6)
642 geneve->sock6 = gs;
643 else
644#endif
645 geneve->sock4 = gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700646
647 hash = geneve_net_vni_hash(geneve->vni);
648 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400649 return 0;
650}
651
John W. Linville8ed66f02015-10-26 17:01:44 -0400652static int geneve_open(struct net_device *dev)
653{
654 struct geneve_dev *geneve = netdev_priv(dev);
655 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
656 bool metadata = geneve->collect_md;
657 int ret = 0;
658
659 geneve->sock4 = NULL;
660#if IS_ENABLED(CONFIG_IPV6)
661 geneve->sock6 = NULL;
662 if (ipv6 || metadata)
663 ret = geneve_sock_add(geneve, true);
664#endif
665 if (!ret && (!ipv6 || metadata))
666 ret = geneve_sock_add(geneve, false);
667 if (ret < 0)
668 geneve_sock_release(geneve);
669
670 return ret;
671}
672
John W. Linville2d07dc72015-05-13 12:57:30 -0400673static int geneve_stop(struct net_device *dev)
674{
675 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400676
Pravin B Shelar66d47002015-08-26 23:46:55 -0700677 if (!hlist_unhashed(&geneve->hlist))
678 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400679 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400680 return 0;
681}
682
John W. Linville8ed66f02015-10-26 17:01:44 -0400683static void geneve_build_header(struct genevehdr *geneveh,
684 __be16 tun_flags, u8 vni[3],
685 u8 options_len, u8 *options)
686{
687 geneveh->ver = GENEVE_VER;
688 geneveh->opt_len = options_len / 4;
689 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
690 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
691 geneveh->rsvd1 = 0;
692 memcpy(geneveh->vni, vni, 3);
693 geneveh->proto_type = htons(ETH_P_TEB);
694 geneveh->rsvd2 = 0;
695
696 memcpy(geneveh->options, options, options_len);
697}
698
Pravin B Shelar371bd102015-08-26 23:46:54 -0700699static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
700 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800701 u32 flags, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700702{
703 struct genevehdr *gnvh;
704 int min_headroom;
705 int err;
Alexander Duyck14f1f722016-02-19 11:26:24 -0800706 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700707
John W. Linville8ed66f02015-10-26 17:01:44 -0400708 skb_scrub_packet(skb, xnet);
709
Pravin B Shelar371bd102015-08-26 23:46:54 -0700710 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
711 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
712 err = skb_cow_head(skb, min_headroom);
713 if (unlikely(err)) {
714 kfree_skb(skb);
715 goto free_rt;
716 }
717
Tom Herbertabe492b2015-12-10 12:37:45 -0800718 skb = udp_tunnel_handle_offloads(skb, udp_sum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700719 if (IS_ERR(skb)) {
720 err = PTR_ERR(skb);
721 goto free_rt;
722 }
723
724 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400725 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700726
727 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
728 return 0;
729
730free_rt:
731 ip_rt_put(rt);
732 return err;
733}
734
John W. Linville8ed66f02015-10-26 17:01:44 -0400735#if IS_ENABLED(CONFIG_IPV6)
736static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
737 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800738 u32 flags, bool xnet)
John W. Linville8ed66f02015-10-26 17:01:44 -0400739{
740 struct genevehdr *gnvh;
741 int min_headroom;
742 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800743 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
John W. Linville8ed66f02015-10-26 17:01:44 -0400744
745 skb_scrub_packet(skb, xnet);
746
747 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
748 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
749 err = skb_cow_head(skb, min_headroom);
750 if (unlikely(err)) {
751 kfree_skb(skb);
752 goto free_dst;
753 }
754
Tom Herbertabe492b2015-12-10 12:37:45 -0800755 skb = udp_tunnel_handle_offloads(skb, udp_sum);
John W. Linville8ed66f02015-10-26 17:01:44 -0400756 if (IS_ERR(skb)) {
757 err = PTR_ERR(skb);
758 goto free_dst;
759 }
760
761 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
762 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
763
764 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
765 return 0;
766
767free_dst:
768 dst_release(dst);
769 return err;
770}
771#endif
772
773static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
774 struct net_device *dev,
775 struct flowi4 *fl4,
776 struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777{
778 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100779 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700780 struct rtable *rt = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100781 bool use_cache = true;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700782 __u8 tos;
783
784 memset(fl4, 0, sizeof(*fl4));
785 fl4->flowi4_mark = skb->mark;
786 fl4->flowi4_proto = IPPROTO_UDP;
787
788 if (info) {
789 fl4->daddr = info->key.u.ipv4.dst;
790 fl4->saddr = info->key.u.ipv4.src;
791 fl4->flowi4_tos = RT_TOS(info->key.tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100792 dst_cache = &info->dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700793 } else {
794 tos = geneve->tos;
795 if (tos == 1) {
796 const struct iphdr *iip = ip_hdr(skb);
797
798 tos = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100799 use_cache = false;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700800 }
801
802 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400803 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100804 dst_cache = &geneve->dst_cache;
805 }
806
807 use_cache = use_cache && !skb->mark;
808 if (use_cache) {
809 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
810 if (rt)
811 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700812 }
813
814 rt = ip_route_output_key(geneve->net, fl4);
815 if (IS_ERR(rt)) {
816 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700817 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700818 }
819 if (rt->dst.dev == dev) { /* is this necessary? */
820 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700821 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700822 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700823 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100824 if (use_cache)
825 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700826 return rt;
827}
828
John W. Linville8ed66f02015-10-26 17:01:44 -0400829#if IS_ENABLED(CONFIG_IPV6)
830static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
831 struct net_device *dev,
832 struct flowi6 *fl6,
833 struct ip_tunnel_info *info)
834{
835 struct geneve_dev *geneve = netdev_priv(dev);
836 struct geneve_sock *gs6 = geneve->sock6;
837 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100838 struct dst_cache *dst_cache;
839 bool use_cache = true;
John W. Linville3a56f862015-10-26 17:01:45 -0400840 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400841
842 memset(fl6, 0, sizeof(*fl6));
843 fl6->flowi6_mark = skb->mark;
844 fl6->flowi6_proto = IPPROTO_UDP;
845
846 if (info) {
847 fl6->daddr = info->key.u.ipv6.dst;
848 fl6->saddr = info->key.u.ipv6.src;
John W. Linville3a56f862015-10-26 17:01:45 -0400849 fl6->flowi6_tos = RT_TOS(info->key.tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100850 dst_cache = &info->dst_cache;
John W. Linville8ed66f02015-10-26 17:01:44 -0400851 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400852 prio = geneve->tos;
853 if (prio == 1) {
854 const struct iphdr *iip = ip_hdr(skb);
855
856 prio = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100857 use_cache = false;
John W. Linville3a56f862015-10-26 17:01:45 -0400858 }
859
860 fl6->flowi6_tos = RT_TOS(prio);
John W. Linville8ed66f02015-10-26 17:01:44 -0400861 fl6->daddr = geneve->remote.sin6.sin6_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100862 dst_cache = &geneve->dst_cache;
863 }
864
865 use_cache = use_cache && !skb->mark;
866 if (use_cache) {
867 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
868 if (dst)
869 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400870 }
871
872 if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
873 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
874 return ERR_PTR(-ENETUNREACH);
875 }
876 if (dst->dev == dev) { /* is this necessary? */
877 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
878 dst_release(dst);
879 return ERR_PTR(-ELOOP);
880 }
881
Paolo Abeni468dfff2016-02-12 15:43:58 +0100882 if (use_cache)
883 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400884 return dst;
885}
886#endif
887
Pravin B Shelare305ac62015-08-26 23:46:52 -0700888/* Convert 64 bit tunnel ID to 24 bit VNI. */
889static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
890{
891#ifdef __BIG_ENDIAN
892 vni[0] = (__force __u8)(tun_id >> 16);
893 vni[1] = (__force __u8)(tun_id >> 8);
894 vni[2] = (__force __u8)tun_id;
895#else
896 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
897 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
898 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
899#endif
900}
901
John W. Linville8ed66f02015-10-26 17:01:44 -0400902static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
903 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400904{
905 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400906 struct geneve_sock *gs4 = geneve->sock4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400907 struct rtable *rt = NULL;
John W. Linville08399ef2015-09-21 10:29:09 -0400908 const struct iphdr *iip; /* interior IP header */
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700909 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400910 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400911 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700912 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700913 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400914 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800915 u32 flags = geneve->flags;
John W. Linville2d07dc72015-05-13 12:57:30 -0400916
Pravin B Shelare305ac62015-08-26 23:46:52 -0700917 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400918 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700919 netdev_dbg(dev, "no tunnel metadata\n");
920 goto tx_error;
921 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200922 if (info && ip_tunnel_info_af(info) != AF_INET)
923 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700924 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400925
John W. Linville8ed66f02015-10-26 17:01:44 -0400926 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
John W. Linville2d07dc72015-05-13 12:57:30 -0400927 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700928 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400929 goto tx_error;
930 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700931
932 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700933 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700934
John W. Linville08399ef2015-09-21 10:29:09 -0400935 iip = ip_hdr(skb);
936
Pravin B Shelare305ac62015-08-26 23:46:52 -0700937 if (info) {
938 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700939 u8 *opts = NULL;
940 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700941
942 tunnel_id_to_vni(key->tun_id, vni);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700943 if (key->tun_flags & TUNNEL_GENEVE_OPT)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700944 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700945
Tom Herbertabe492b2015-12-10 12:37:45 -0800946 if (key->tun_flags & TUNNEL_CSUM)
Alexander Duyck14f1f722016-02-19 11:26:24 -0800947 flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800948 else
Alexander Duyck14f1f722016-02-19 11:26:24 -0800949 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800950
Pravin B Shelar371bd102015-08-26 23:46:54 -0700951 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800952 info->options_len, opts, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700953 if (unlikely(err))
954 goto err;
955
John W. Linville08399ef2015-09-21 10:29:09 -0400956 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700957 ttl = key->ttl;
958 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700959 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700960 err = geneve_build_skb(rt, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800961 0, NULL, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700962 if (unlikely(err))
963 goto err;
964
Pravin B Shelare305ac62015-08-26 23:46:52 -0700965 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700966 ttl = geneve->ttl;
967 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
968 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700969 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700970 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400971 }
Pravin B Shelar039f5062015-12-24 14:34:54 -0800972 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
973 tos, ttl, df, sport, geneve->dst_port,
974 !net_eq(geneve->net, dev_net(geneve->dev)),
Alexander Duyck14f1f722016-02-19 11:26:24 -0800975 !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX));
John W. Linville2d07dc72015-05-13 12:57:30 -0400976
John W. Linville2d07dc72015-05-13 12:57:30 -0400977 return NETDEV_TX_OK;
978
John W. Linville2d07dc72015-05-13 12:57:30 -0400979tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400980 dev_kfree_skb(skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700981err:
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700982 if (err == -ELOOP)
983 dev->stats.collisions++;
984 else if (err == -ENETUNREACH)
985 dev->stats.tx_carrier_errors++;
986 else
987 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400988 return NETDEV_TX_OK;
989}
990
John W. Linville8ed66f02015-10-26 17:01:44 -0400991#if IS_ENABLED(CONFIG_IPV6)
992static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
993 struct ip_tunnel_info *info)
994{
995 struct geneve_dev *geneve = netdev_priv(dev);
996 struct geneve_sock *gs6 = geneve->sock6;
997 struct dst_entry *dst = NULL;
John W. Linville3a56f862015-10-26 17:01:45 -0400998 const struct iphdr *iip; /* interior IP header */
John W. Linville8ed66f02015-10-26 17:01:44 -0400999 int err = -EINVAL;
1000 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -04001001 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -04001002 __be16 sport;
John W. Linville8ed66f02015-10-26 17:01:44 -04001003 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -08001004 u32 flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -04001005
1006 if (geneve->collect_md) {
1007 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
1008 netdev_dbg(dev, "no tunnel metadata\n");
1009 goto tx_error;
1010 }
1011 }
1012
1013 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1014 if (IS_ERR(dst)) {
1015 err = PTR_ERR(dst);
1016 goto tx_error;
1017 }
1018
1019 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
1020 skb_reset_mac_header(skb);
1021
John W. Linville3a56f862015-10-26 17:01:45 -04001022 iip = ip_hdr(skb);
1023
John W. Linville8ed66f02015-10-26 17:01:44 -04001024 if (info) {
1025 const struct ip_tunnel_key *key = &info->key;
1026 u8 *opts = NULL;
1027 u8 vni[3];
1028
1029 tunnel_id_to_vni(key->tun_id, vni);
1030 if (key->tun_flags & TUNNEL_GENEVE_OPT)
1031 opts = ip_tunnel_info_opts(info);
1032
Tom Herbertabe492b2015-12-10 12:37:45 -08001033 if (key->tun_flags & TUNNEL_CSUM)
Jesse Gross35e2d112016-01-20 16:22:47 -08001034 flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001035 else
Jesse Gross35e2d112016-01-20 16:22:47 -08001036 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001037
John W. Linville8ed66f02015-10-26 17:01:44 -04001038 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
1039 info->options_len, opts,
Tom Herbertabe492b2015-12-10 12:37:45 -08001040 flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001041 if (unlikely(err))
1042 goto err;
1043
John W. Linville3a56f862015-10-26 17:01:45 -04001044 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001045 ttl = key->ttl;
1046 } else {
John W. Linville8ed66f02015-10-26 17:01:44 -04001047 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -08001048 0, NULL, flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001049 if (unlikely(err))
1050 goto err;
1051
John W. Linville3a56f862015-10-26 17:01:45 -04001052 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001053 ttl = geneve->ttl;
1054 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
1055 ttl = 1;
1056 ttl = ttl ? : ip6_dst_hoplimit(dst);
1057 }
Pravin B Shelar039f5062015-12-24 14:34:54 -08001058 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
1059 &fl6.saddr, &fl6.daddr, prio, ttl,
1060 sport, geneve->dst_port,
1061 !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
John W. Linville8ed66f02015-10-26 17:01:44 -04001062 return NETDEV_TX_OK;
1063
1064tx_error:
1065 dev_kfree_skb(skb);
1066err:
1067 if (err == -ELOOP)
1068 dev->stats.collisions++;
1069 else if (err == -ENETUNREACH)
1070 dev->stats.tx_carrier_errors++;
1071 else
1072 dev->stats.tx_errors++;
1073 return NETDEV_TX_OK;
1074}
1075#endif
1076
1077static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1078{
1079 struct geneve_dev *geneve = netdev_priv(dev);
1080 struct ip_tunnel_info *info = NULL;
1081
1082 if (geneve->collect_md)
1083 info = skb_tunnel_info(skb);
1084
1085#if IS_ENABLED(CONFIG_IPV6)
1086 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
1087 (!info && geneve->remote.sa.sa_family == AF_INET6))
1088 return geneve6_xmit_skb(skb, dev, info);
1089#endif
1090 return geneve_xmit_skb(skb, dev, info);
1091}
1092
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001093static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1094{
1095 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1096 struct geneve_dev *geneve = netdev_priv(dev);
1097 struct rtable *rt;
1098 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001099#if IS_ENABLED(CONFIG_IPV6)
1100 struct dst_entry *dst;
1101 struct flowi6 fl6;
1102#endif
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001103
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001104 if (ip_tunnel_info_af(info) == AF_INET) {
1105 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1106 if (IS_ERR(rt))
1107 return PTR_ERR(rt);
1108
1109 ip_rt_put(rt);
1110 info->key.u.ipv4.src = fl4.saddr;
1111#if IS_ENABLED(CONFIG_IPV6)
1112 } else if (ip_tunnel_info_af(info) == AF_INET6) {
1113 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1114 if (IS_ERR(dst))
1115 return PTR_ERR(dst);
1116
1117 dst_release(dst);
1118 info->key.u.ipv6.src = fl6.saddr;
1119#endif
1120 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001121 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001122 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001123
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001124 info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1125 1, USHRT_MAX, true);
1126 info->key.tp_dst = geneve->dst_port;
1127 return 0;
1128}
1129
John W. Linville2d07dc72015-05-13 12:57:30 -04001130static const struct net_device_ops geneve_netdev_ops = {
1131 .ndo_init = geneve_init,
1132 .ndo_uninit = geneve_uninit,
1133 .ndo_open = geneve_open,
1134 .ndo_stop = geneve_stop,
1135 .ndo_start_xmit = geneve_xmit,
1136 .ndo_get_stats64 = ip_tunnel_get_stats64,
1137 .ndo_change_mtu = eth_change_mtu,
1138 .ndo_validate_addr = eth_validate_addr,
1139 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001140 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001141};
1142
1143static void geneve_get_drvinfo(struct net_device *dev,
1144 struct ethtool_drvinfo *drvinfo)
1145{
1146 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1147 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1148}
1149
1150static const struct ethtool_ops geneve_ethtool_ops = {
1151 .get_drvinfo = geneve_get_drvinfo,
1152 .get_link = ethtool_op_get_link,
1153};
1154
1155/* Info for udev, that this is a virtual tunnel endpoint */
1156static struct device_type geneve_type = {
1157 .name = "geneve",
1158};
1159
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001160/* Calls the ndo_add_geneve_port of the caller in order to
1161 * supply the listening GENEVE udp ports. Callers are expected
1162 * to implement the ndo_add_geneve_port.
1163 */
1164void geneve_get_rx_port(struct net_device *dev)
1165{
1166 struct net *net = dev_net(dev);
1167 struct geneve_net *gn = net_generic(net, geneve_net_id);
1168 struct geneve_sock *gs;
1169 sa_family_t sa_family;
1170 struct sock *sk;
1171 __be16 port;
1172
1173 rcu_read_lock();
1174 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1175 sk = gs->sock->sk;
1176 sa_family = sk->sk_family;
1177 port = inet_sk(sk)->inet_sport;
1178 dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port);
1179 }
1180 rcu_read_unlock();
1181}
1182EXPORT_SYMBOL_GPL(geneve_get_rx_port);
1183
John W. Linville2d07dc72015-05-13 12:57:30 -04001184/* Initialize the device structure. */
1185static void geneve_setup(struct net_device *dev)
1186{
1187 ether_setup(dev);
1188
1189 dev->netdev_ops = &geneve_netdev_ops;
1190 dev->ethtool_ops = &geneve_ethtool_ops;
1191 dev->destructor = free_netdev;
1192
1193 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1194
John W. Linville2d07dc72015-05-13 12:57:30 -04001195 dev->features |= NETIF_F_LLTX;
1196 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1197 dev->features |= NETIF_F_RXCSUM;
1198 dev->features |= NETIF_F_GSO_SOFTWARE;
1199
John W. Linville2d07dc72015-05-13 12:57:30 -04001200 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1201 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001202
1203 netif_keep_dst(dev);
Phil Suttered961ac2015-08-18 10:30:31 +02001204 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001205 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001206}
1207
1208static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1209 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1210 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001211 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001212 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001213 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001214 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001215 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001216 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1217 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1218 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001219};
1220
1221static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1222{
1223 if (tb[IFLA_ADDRESS]) {
1224 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1225 return -EINVAL;
1226
1227 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1228 return -EADDRNOTAVAIL;
1229 }
1230
1231 if (!data)
1232 return -EINVAL;
1233
1234 if (data[IFLA_GENEVE_ID]) {
1235 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1236
1237 if (vni >= GENEVE_VID_MASK)
1238 return -ERANGE;
1239 }
1240
1241 return 0;
1242}
1243
Pravin B Shelar371bd102015-08-26 23:46:54 -07001244static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1245 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001246 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001247 u8 vni[],
1248 bool *tun_on_same_port,
1249 bool *tun_collect_md)
1250{
1251 struct geneve_dev *geneve, *t;
1252
1253 *tun_on_same_port = false;
1254 *tun_collect_md = false;
1255 t = NULL;
1256 list_for_each_entry(geneve, &gn->geneve_list, next) {
1257 if (geneve->dst_port == dst_port) {
1258 *tun_collect_md = geneve->collect_md;
1259 *tun_on_same_port = true;
1260 }
1261 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001262 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001263 dst_port == geneve->dst_port)
1264 t = geneve;
1265 }
1266 return t;
1267}
1268
Pravin B Shelare305ac62015-08-26 23:46:52 -07001269static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001270 union geneve_addr *remote,
1271 __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001272 bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001273{
1274 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001275 struct geneve_dev *t, *geneve = netdev_priv(dev);
1276 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001277 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001278
John W. Linville8ed66f02015-10-26 17:01:44 -04001279 if (!remote)
1280 return -EINVAL;
1281 if (metadata &&
1282 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1283 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001284
1285 geneve->net = net;
1286 geneve->dev = dev;
1287
John W. Linville2d07dc72015-05-13 12:57:30 -04001288 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1289 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1290 geneve->vni[2] = vni & 0x000000ff;
1291
John W. Linville8ed66f02015-10-26 17:01:44 -04001292 if ((remote->sa.sa_family == AF_INET &&
1293 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1294 (remote->sa.sa_family == AF_INET6 &&
1295 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001296 return -EINVAL;
John W. Linville8ed66f02015-10-26 17:01:44 -04001297 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001298
Pravin B Shelare305ac62015-08-26 23:46:52 -07001299 geneve->ttl = ttl;
1300 geneve->tos = tos;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001301 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001302 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001303 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001304
John W. Linville8ed66f02015-10-26 17:01:44 -04001305 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001306 &tun_on_same_port, &tun_collect_md);
1307 if (t)
1308 return -EBUSY;
1309
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001310 /* make enough headroom for basic scenario */
1311 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1312 if (remote->sa.sa_family == AF_INET)
1313 encap_len += sizeof(struct iphdr);
1314 else
1315 encap_len += sizeof(struct ipv6hdr);
1316 dev->needed_headroom = encap_len + ETH_HLEN;
1317
Pravin B Shelar371bd102015-08-26 23:46:54 -07001318 if (metadata) {
1319 if (tun_on_same_port)
1320 return -EPERM;
1321 } else {
1322 if (tun_collect_md)
1323 return -EPERM;
1324 }
1325
Paolo Abeni468dfff2016-02-12 15:43:58 +01001326 dst_cache_reset(&geneve->dst_cache);
1327
John W. Linville2d07dc72015-05-13 12:57:30 -04001328 err = register_netdevice(dev);
1329 if (err)
1330 return err;
1331
1332 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001333 return 0;
1334}
1335
Pravin B Shelare305ac62015-08-26 23:46:52 -07001336static int geneve_newlink(struct net *net, struct net_device *dev,
1337 struct nlattr *tb[], struct nlattr *data[])
1338{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001339 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001340 __u8 ttl = 0, tos = 0;
1341 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001342 union geneve_addr remote = geneve_remote_unspec;
Jesse Grosse277de52015-10-16 16:36:00 -07001343 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001344 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001345
John W. Linville8ed66f02015-10-26 17:01:44 -04001346 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1347 return -EINVAL;
1348
1349 if (data[IFLA_GENEVE_REMOTE]) {
1350 remote.sa.sa_family = AF_INET;
1351 remote.sin.sin_addr.s_addr =
1352 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1353 }
1354
1355 if (data[IFLA_GENEVE_REMOTE6]) {
1356 if (!IS_ENABLED(CONFIG_IPV6))
1357 return -EPFNOSUPPORT;
1358
1359 remote.sa.sa_family = AF_INET6;
1360 remote.sin6.sin6_addr =
1361 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1362
1363 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1364 IPV6_ADDR_LINKLOCAL) {
1365 netdev_dbg(dev, "link-local remote is unsupported\n");
1366 return -EINVAL;
1367 }
1368 }
1369
Jesse Grosse277de52015-10-16 16:36:00 -07001370 if (data[IFLA_GENEVE_ID])
1371 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001372
Pravin B Shelare305ac62015-08-26 23:46:52 -07001373 if (data[IFLA_GENEVE_TTL])
1374 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1375
1376 if (data[IFLA_GENEVE_TOS])
1377 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1378
1379 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001380 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001381
1382 if (data[IFLA_GENEVE_COLLECT_METADATA])
1383 metadata = true;
1384
Tom Herbertabe492b2015-12-10 12:37:45 -08001385 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001386 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1387 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001388
1389 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1390 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1391 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1392
1393 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1394 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1395 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1396
John W. Linville8ed66f02015-10-26 17:01:44 -04001397 return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
Tom Herbertabe492b2015-12-10 12:37:45 -08001398 metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001399}
1400
John W. Linville2d07dc72015-05-13 12:57:30 -04001401static void geneve_dellink(struct net_device *dev, struct list_head *head)
1402{
1403 struct geneve_dev *geneve = netdev_priv(dev);
1404
John W. Linville2d07dc72015-05-13 12:57:30 -04001405 list_del(&geneve->next);
1406 unregister_netdevice_queue(dev, head);
1407}
1408
1409static size_t geneve_get_size(const struct net_device *dev)
1410{
1411 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001412 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001413 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001414 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001415 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001416 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001417 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1418 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1419 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001420 0;
1421}
1422
1423static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1424{
1425 struct geneve_dev *geneve = netdev_priv(dev);
1426 __u32 vni;
1427
1428 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1429 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1430 goto nla_put_failure;
1431
John W. Linville8ed66f02015-10-26 17:01:44 -04001432 if (geneve->remote.sa.sa_family == AF_INET) {
1433 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1434 geneve->remote.sin.sin_addr.s_addr))
1435 goto nla_put_failure;
1436#if IS_ENABLED(CONFIG_IPV6)
1437 } else {
1438 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1439 &geneve->remote.sin6.sin6_addr))
1440 goto nla_put_failure;
1441#endif
1442 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001443
John W. Linvilled8951122015-06-01 15:51:35 -04001444 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1445 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
John W. Linville8760ce52015-06-01 15:51:34 -04001446 goto nla_put_failure;
1447
John W. Linville7bbe33f2015-09-22 13:09:32 -04001448 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001449 goto nla_put_failure;
1450
Pravin B Shelare305ac62015-08-26 23:46:52 -07001451 if (geneve->collect_md) {
1452 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1453 goto nla_put_failure;
1454 }
1455
Tom Herbertabe492b2015-12-10 12:37:45 -08001456 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
Alexander Duyck14f1f722016-02-19 11:26:24 -08001457 !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbertabe492b2015-12-10 12:37:45 -08001458 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1459 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1460 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1461 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1462 goto nla_put_failure;
1463
John W. Linville2d07dc72015-05-13 12:57:30 -04001464 return 0;
1465
1466nla_put_failure:
1467 return -EMSGSIZE;
1468}
1469
1470static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1471 .kind = "geneve",
1472 .maxtype = IFLA_GENEVE_MAX,
1473 .policy = geneve_policy,
1474 .priv_size = sizeof(struct geneve_dev),
1475 .setup = geneve_setup,
1476 .validate = geneve_validate,
1477 .newlink = geneve_newlink,
1478 .dellink = geneve_dellink,
1479 .get_size = geneve_get_size,
1480 .fill_info = geneve_fill_info,
1481};
1482
Pravin B Shelare305ac62015-08-26 23:46:52 -07001483struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1484 u8 name_assign_type, u16 dst_port)
1485{
1486 struct nlattr *tb[IFLA_MAX + 1];
1487 struct net_device *dev;
1488 int err;
1489
1490 memset(tb, 0, sizeof(tb));
1491 dev = rtnl_create_link(net, name, name_assign_type,
1492 &geneve_link_ops, tb);
1493 if (IS_ERR(dev))
1494 return dev;
1495
John W. Linville8ed66f02015-10-26 17:01:44 -04001496 err = geneve_configure(net, dev, &geneve_remote_unspec,
Tom Herbertabe492b2015-12-10 12:37:45 -08001497 0, 0, 0, htons(dst_port), true, 0);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001498 if (err) {
1499 free_netdev(dev);
1500 return ERR_PTR(err);
1501 }
1502 return dev;
1503}
1504EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1505
John W. Linville2d07dc72015-05-13 12:57:30 -04001506static __net_init int geneve_init_net(struct net *net)
1507{
1508 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001509
1510 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001511 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001512 return 0;
1513}
1514
1515static void __net_exit geneve_exit_net(struct net *net)
1516{
1517 struct geneve_net *gn = net_generic(net, geneve_net_id);
1518 struct geneve_dev *geneve, *next;
1519 struct net_device *dev, *aux;
1520 LIST_HEAD(list);
1521
1522 rtnl_lock();
1523
1524 /* gather any geneve devices that were moved into this ns */
1525 for_each_netdev_safe(net, dev, aux)
1526 if (dev->rtnl_link_ops == &geneve_link_ops)
1527 unregister_netdevice_queue(dev, &list);
1528
1529 /* now gather any other geneve devices that were created in this ns */
1530 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1531 /* If geneve->dev is in the same netns, it was already added
1532 * to the list by the previous loop.
1533 */
1534 if (!net_eq(dev_net(geneve->dev), net))
1535 unregister_netdevice_queue(geneve->dev, &list);
1536 }
1537
1538 /* unregister the devices gathered above */
1539 unregister_netdevice_many(&list);
1540 rtnl_unlock();
1541}
1542
1543static struct pernet_operations geneve_net_ops = {
1544 .init = geneve_init_net,
1545 .exit = geneve_exit_net,
1546 .id = &geneve_net_id,
1547 .size = sizeof(struct geneve_net),
1548};
1549
1550static int __init geneve_init_module(void)
1551{
1552 int rc;
1553
1554 rc = register_pernet_subsys(&geneve_net_ops);
1555 if (rc)
1556 goto out1;
1557
1558 rc = rtnl_link_register(&geneve_link_ops);
1559 if (rc)
1560 goto out2;
1561
1562 return 0;
1563out2:
1564 unregister_pernet_subsys(&geneve_net_ops);
1565out1:
1566 return rc;
1567}
1568late_initcall(geneve_init_module);
1569
1570static void __exit geneve_cleanup_module(void)
1571{
1572 rtnl_link_unregister(&geneve_link_ops);
1573 unregister_pernet_subsys(&geneve_net_ops);
1574}
1575module_exit(geneve_cleanup_module);
1576
1577MODULE_LICENSE("GPL");
1578MODULE_VERSION(GENEVE_NETDEV_VER);
1579MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1580MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1581MODULE_ALIAS_RTNL_LINK("geneve");