blob: d89995f4bd433faefa10b7dfd54e6f04bf262f8a [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
Pravin B Shelar371bd102015-08-26 23:46:54 -070046static int geneve_net_id;
47
John W. Linville8ed66f02015-10-26 17:01:44 -040048union geneve_addr {
49 struct sockaddr_in sin;
50 struct sockaddr_in6 sin6;
51 struct sockaddr sa;
52};
53
54static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
55
John W. Linville2d07dc72015-05-13 12:57:30 -040056/* Pseudo network device */
57struct geneve_dev {
58 struct hlist_node hlist; /* vni hash table */
59 struct net *net; /* netns for packet i/o */
60 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelarfceb9c32016-10-28 09:59:16 -070061 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040062#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070063 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040064#endif
John W. Linville8760ce52015-06-01 15:51:34 -040065 u8 vni[3]; /* virtual network ID for tunnel */
66 u8 ttl; /* TTL override */
John W. Linvilled8951122015-06-01 15:51:35 -040067 u8 tos; /* TOS override */
John W. Linville8ed66f02015-10-26 17:01:44 -040068 union geneve_addr remote; /* IP address for link partner */
John W. Linville2d07dc72015-05-13 12:57:30 -040069 struct list_head next; /* geneve's per namespace list */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +010070 __be32 label; /* IPv6 flowlabel override */
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;
Pravin B Shelar66d47002015-08-26 23:46:55 -070089 struct hlist_head vni_list[VNI_HASH_SIZE];
Tom Herbertabe492b2015-12-10 12:37:45 -080090 u32 flags;
Pravin B Shelar371bd102015-08-26 23:46:54 -070091};
John W. Linville2d07dc72015-05-13 12:57:30 -040092
93static inline __u32 geneve_net_vni_hash(u8 vni[3])
94{
95 __u32 vnid;
96
97 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
98 return hash_32(vnid, VNI_HASH_BITS);
99}
100
Pravin B Shelare305ac62015-08-26 23:46:52 -0700101static __be64 vni_to_tunnel_id(const __u8 *vni)
102{
103#ifdef __BIG_ENDIAN
104 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
105#else
106 return (__force __be64)(((__force u64)vni[0] << 40) |
107 ((__force u64)vni[1] << 48) |
108 ((__force u64)vni[2] << 56));
109#endif
110}
111
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100112static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
113{
114 return gs->sock->sk->sk_family;
115}
116
Pravin B Shelar66d47002015-08-26 23:46:55 -0700117static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700118 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400119{
John W. Linville2d07dc72015-05-13 12:57:30 -0400120 struct hlist_head *vni_list_head;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700121 struct geneve_dev *geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400122 __u32 hash;
123
John W. Linville2d07dc72015-05-13 12:57:30 -0400124 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700125 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700126 vni_list_head = &gs->vni_list[hash];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700127 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700128 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -0400129 addr == geneve->remote.sin.sin_addr.s_addr)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700130 return geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400131 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700132 return NULL;
133}
134
John W. Linville8ed66f02015-10-26 17:01:44 -0400135#if IS_ENABLED(CONFIG_IPV6)
136static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
137 struct in6_addr addr6, u8 vni[])
138{
139 struct hlist_head *vni_list_head;
140 struct geneve_dev *geneve;
141 __u32 hash;
142
143 /* Find the device for this VNI */
144 hash = geneve_net_vni_hash(vni);
145 vni_list_head = &gs->vni_list[hash];
146 hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
147 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
148 ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
149 return geneve;
150 }
151 return NULL;
152}
153#endif
154
Pravin B Shelar371bd102015-08-26 23:46:54 -0700155static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
156{
157 return (struct genevehdr *)(udp_hdr(skb) + 1);
158}
159
Jiri Benc9fc47542016-02-18 11:22:50 +0100160static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
161 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700162{
Jiri Benc9fc47542016-02-18 11:22:50 +0100163 u8 *vni;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700164 __be32 addr;
John W. Linville8ed66f02015-10-26 17:01:44 -0400165 static u8 zero_vni[3];
John W. Linville8ed66f02015-10-26 17:01:44 -0400166#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400167 static struct in6_addr zero_addr6;
168#endif
Pravin B Shelare305ac62015-08-26 23:46:52 -0700169
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100170 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100171 struct iphdr *iph;
172
John W. Linville8ed66f02015-10-26 17:01:44 -0400173 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700174
John W. Linville8ed66f02015-10-26 17:01:44 -0400175 if (gs->collect_md) {
176 vni = zero_vni;
177 addr = 0;
178 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100179 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400180 addr = iph->saddr;
181 }
182
Jiri Benc9fc47542016-02-18 11:22:50 +0100183 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400184#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100185 } else if (geneve_get_sk_family(gs) == AF_INET6) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100186 struct ipv6hdr *ip6h;
187 struct in6_addr addr6;
188
John W. Linville8ed66f02015-10-26 17:01:44 -0400189 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
190
191 if (gs->collect_md) {
192 vni = zero_vni;
193 addr6 = zero_addr6;
194 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100195 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400196 addr6 = ip6h->saddr;
197 }
198
Jiri Benc9fc47542016-02-18 11:22:50 +0100199 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400200#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700201 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100202 return NULL;
203}
204
205/* geneve receive/decap routine */
206static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
207 struct sk_buff *skb)
208{
209 struct genevehdr *gnvh = geneve_hdr(skb);
210 struct metadata_dst *tun_dst = NULL;
211 struct pcpu_sw_netstats *stats;
Girish Moodalbailfb968222017-06-08 17:07:48 -0700212 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100213 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);
Girish Moodalbailfb968222017-06-08 17:07:48 -0700226 if (!tun_dst) {
227 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700228 goto drop;
Girish Moodalbailfb968222017-06-08 17:07:48 -0700229 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700230 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700231 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
232 gnvh->options, gnvh->opt_len * 4);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 } else {
234 /* Drop packets w/ critical options,
235 * since we don't support any...
236 */
Girish Moodalbailfb968222017-06-08 17:07:48 -0700237 if (gnvh->critical) {
238 geneve->dev->stats.rx_frame_errors++;
239 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700240 goto drop;
Girish Moodalbailfb968222017-06-08 17:07:48 -0700241 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700242 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400243
244 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400245 skb->protocol = eth_type_trans(skb, geneve->dev);
246 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
247
Pravin B Shelare305ac62015-08-26 23:46:52 -0700248 if (tun_dst)
249 skb_dst_set(skb, &tun_dst->dst);
250
John W. Linville2d07dc72015-05-13 12:57:30 -0400251 /* Ignore packet loops (and multicast echo) */
Girish Moodalbailfb968222017-06-08 17:07:48 -0700252 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
253 geneve->dev->stats.rx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400254 goto drop;
Girish Moodalbailfb968222017-06-08 17:07:48 -0700255 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400256
Jiri Benc9fc47542016-02-18 11:22:50 +0100257 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400258 skb_reset_network_header(skb);
259
Jiri Benc9fc47542016-02-18 11:22:50 +0100260 if (geneve_get_sk_family(gs) == AF_INET)
261 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400262#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100263 else
264 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400265#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400266
267 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400268 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100269 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400270 net_info_ratelimited("non-ECT from %pI4 "
271 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100272 &((struct iphdr *)oiph)->saddr,
273 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400274#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100275 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400276 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100277 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400278#endif
279 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400280 if (err > 1) {
281 ++geneve->dev->stats.rx_frame_errors;
282 ++geneve->dev->stats.rx_errors;
283 goto drop;
284 }
285 }
286
Girish Moodalbailfb968222017-06-08 17:07:48 -0700287 len = skb->len;
288 err = gro_cells_receive(&geneve->gro_cells, skb);
289 if (likely(err == NET_RX_SUCCESS)) {
290 stats = this_cpu_ptr(geneve->dev->tstats);
291 u64_stats_update_begin(&stats->syncp);
292 stats->rx_packets++;
293 stats->rx_bytes += len;
294 u64_stats_update_end(&stats->syncp);
295 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400296 return;
297drop:
298 /* Consume bad packet */
299 kfree_skb(skb);
300}
301
302/* Setup stats when device is created */
303static int geneve_init(struct net_device *dev)
304{
Jesse Gross8e816df2015-08-28 16:54:40 -0700305 struct geneve_dev *geneve = netdev_priv(dev);
306 int err;
307
John W. Linville2d07dc72015-05-13 12:57:30 -0400308 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
309 if (!dev->tstats)
310 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700311
312 err = gro_cells_init(&geneve->gro_cells, dev);
313 if (err) {
314 free_percpu(dev->tstats);
315 return err;
316 }
317
Paolo Abeni468dfff2016-02-12 15:43:58 +0100318 err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL);
319 if (err) {
320 free_percpu(dev->tstats);
321 gro_cells_destroy(&geneve->gro_cells);
322 return err;
323 }
324
John W. Linville2d07dc72015-05-13 12:57:30 -0400325 return 0;
326}
327
328static void geneve_uninit(struct net_device *dev)
329{
Jesse Gross8e816df2015-08-28 16:54:40 -0700330 struct geneve_dev *geneve = netdev_priv(dev);
331
Paolo Abeni468dfff2016-02-12 15:43:58 +0100332 dst_cache_destroy(&geneve->dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700333 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400334 free_percpu(dev->tstats);
335}
336
Pravin B Shelar371bd102015-08-26 23:46:54 -0700337/* Callback from net/ipv4/udp.c to receive packets */
338static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
339{
340 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100341 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700342 struct geneve_sock *gs;
343 int opts_len;
344
Girish Moodalbailfb968222017-06-08 17:07:48 -0700345 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700346 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200347 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700348
349 /* Return packets with reserved bits set */
350 geneveh = geneve_hdr(skb);
351 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200352 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700353
354 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200355 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700356
Jiri Benc9fc47542016-02-18 11:22:50 +0100357 gs = rcu_dereference_sk_user_data(sk);
358 if (!gs)
359 goto drop;
360
361 geneve = geneve_lookup_skb(gs, skb);
362 if (!geneve)
363 goto drop;
364
Pravin B Shelar371bd102015-08-26 23:46:54 -0700365 opts_len = geneveh->opt_len * 4;
366 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100367 htons(ETH_P_TEB),
Girish Moodalbailfb968222017-06-08 17:07:48 -0700368 !net_eq(geneve->net, dev_net(geneve->dev)))) {
369 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700370 goto drop;
Girish Moodalbailfb968222017-06-08 17:07:48 -0700371 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700372
Jiri Benc9fc47542016-02-18 11:22:50 +0100373 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700374 return 0;
375
376drop:
377 /* Consume bad packet */
378 kfree_skb(skb);
379 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700380}
381
382static struct socket *geneve_create_sock(struct net *net, bool ipv6,
Tom Herbertabe492b2015-12-10 12:37:45 -0800383 __be16 port, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700384{
385 struct socket *sock;
386 struct udp_port_cfg udp_conf;
387 int err;
388
389 memset(&udp_conf, 0, sizeof(udp_conf));
390
391 if (ipv6) {
392 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400393 udp_conf.ipv6_v6only = 1;
Tom Herbertabe492b2015-12-10 12:37:45 -0800394 udp_conf.use_udp6_rx_checksums =
395 !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700396 } else {
397 udp_conf.family = AF_INET;
398 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
399 }
400
401 udp_conf.local_udp_port = port;
402
403 /* Open UDP socket */
404 err = udp_sock_create(net, &udp_conf, &sock);
405 if (err < 0)
406 return ERR_PTR(err);
407
408 return sock;
409}
410
Pravin B Shelar371bd102015-08-26 23:46:54 -0700411static int geneve_hlen(struct genevehdr *gh)
412{
413 return sizeof(*gh) + gh->opt_len * 4;
414}
415
Tom Herbert4a0090a2016-04-05 08:22:55 -0700416static struct sk_buff **geneve_gro_receive(struct sock *sk,
417 struct sk_buff **head,
418 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700419{
420 struct sk_buff *p, **pp = NULL;
421 struct genevehdr *gh, *gh2;
422 unsigned int hlen, gh_len, off_gnv;
423 const struct packet_offload *ptype;
424 __be16 type;
425 int flush = 1;
426
427 off_gnv = skb_gro_offset(skb);
428 hlen = off_gnv + sizeof(*gh);
429 gh = skb_gro_header_fast(skb, off_gnv);
430 if (skb_gro_header_hard(skb, hlen)) {
431 gh = skb_gro_header_slow(skb, hlen, off_gnv);
432 if (unlikely(!gh))
433 goto out;
434 }
435
436 if (gh->ver != GENEVE_VER || gh->oam)
437 goto out;
438 gh_len = geneve_hlen(gh);
439
440 hlen = off_gnv + gh_len;
441 if (skb_gro_header_hard(skb, hlen)) {
442 gh = skb_gro_header_slow(skb, hlen, off_gnv);
443 if (unlikely(!gh))
444 goto out;
445 }
446
Pravin B Shelar371bd102015-08-26 23:46:54 -0700447 for (p = *head; p; p = p->next) {
448 if (!NAPI_GRO_CB(p)->same_flow)
449 continue;
450
451 gh2 = (struct genevehdr *)(p->data + off_gnv);
452 if (gh->opt_len != gh2->opt_len ||
453 memcmp(gh, gh2, gh_len)) {
454 NAPI_GRO_CB(p)->same_flow = 0;
455 continue;
456 }
457 }
458
459 type = gh->proto_type;
460
461 rcu_read_lock();
462 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800463 if (!ptype)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700464 goto out_unlock;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700465
466 skb_gro_pull(skb, gh_len);
467 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200468 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800469 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700470
471out_unlock:
472 rcu_read_unlock();
473out:
474 NAPI_GRO_CB(skb)->flush |= flush;
475
476 return pp;
477}
478
Tom Herbert4a0090a2016-04-05 08:22:55 -0700479static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
480 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700481{
482 struct genevehdr *gh;
483 struct packet_offload *ptype;
484 __be16 type;
485 int gh_len;
486 int err = -ENOSYS;
487
Pravin B Shelar371bd102015-08-26 23:46:54 -0700488 gh = (struct genevehdr *)(skb->data + nhoff);
489 gh_len = geneve_hlen(gh);
490 type = gh->proto_type;
491
492 rcu_read_lock();
493 ptype = gro_find_complete_by_type(type);
494 if (ptype)
495 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
496
497 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700498
499 skb_set_inner_mac_header(skb, nhoff + gh_len);
500
Pravin B Shelar371bd102015-08-26 23:46:54 -0700501 return err;
502}
503
504/* Create new listen socket if needed */
505static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
Tom Herbertabe492b2015-12-10 12:37:45 -0800506 bool ipv6, u32 flags)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700507{
508 struct geneve_net *gn = net_generic(net, geneve_net_id);
509 struct geneve_sock *gs;
510 struct socket *sock;
511 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700512 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700513
514 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
515 if (!gs)
516 return ERR_PTR(-ENOMEM);
517
Tom Herbertabe492b2015-12-10 12:37:45 -0800518 sock = geneve_create_sock(net, ipv6, port, flags);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700519 if (IS_ERR(sock)) {
520 kfree(gs);
521 return ERR_CAST(sock);
522 }
523
524 gs->sock = sock;
525 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700526 for (h = 0; h < VNI_HASH_SIZE; ++h)
527 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528
529 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700530 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700531
532 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700533 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700534 tunnel_cfg.sk_user_data = gs;
535 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700536 tunnel_cfg.gro_receive = geneve_gro_receive;
537 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700538 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
539 tunnel_cfg.encap_destroy = NULL;
540 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700541 list_add(&gs->list, &gn->sock_list);
542 return gs;
543}
544
John W. Linville8ed66f02015-10-26 17:01:44 -0400545static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700546{
John W. Linville8ed66f02015-10-26 17:01:44 -0400547 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700548 return;
549
550 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700551 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700552 udp_tunnel_sock_release(gs->sock);
553 kfree_rcu(gs, rcu);
554}
555
John W. Linville8ed66f02015-10-26 17:01:44 -0400556static void geneve_sock_release(struct geneve_dev *geneve)
557{
pravin shelarfceb9c32016-10-28 09:59:16 -0700558 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400559#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700560 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
561
562 rcu_assign_pointer(geneve->sock6, NULL);
563#endif
564
565 rcu_assign_pointer(geneve->sock4, NULL);
566 synchronize_net();
567
568 __geneve_sock_release(gs4);
569#if IS_ENABLED(CONFIG_IPV6)
570 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400571#endif
572}
573
Pravin B Shelar371bd102015-08-26 23:46:54 -0700574static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400575 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700576 __be16 dst_port)
577{
578 struct geneve_sock *gs;
579
580 list_for_each_entry(gs, &gn->sock_list, list) {
581 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100582 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700583 return gs;
584 }
585 }
586 return NULL;
587}
588
John W. Linville8ed66f02015-10-26 17:01:44 -0400589static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400590{
John W. Linville2d07dc72015-05-13 12:57:30 -0400591 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700592 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -0400593 struct geneve_sock *gs;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700594 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400595
John W. Linville8ed66f02015-10-26 17:01:44 -0400596 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700597 if (gs) {
598 gs->refcnt++;
599 goto out;
600 }
601
Tom Herbertabe492b2015-12-10 12:37:45 -0800602 gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
John W. Linville2d07dc72015-05-13 12:57:30 -0400603 if (IS_ERR(gs))
604 return PTR_ERR(gs);
605
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606out:
607 gs->collect_md = geneve->collect_md;
Tom Herbertabe492b2015-12-10 12:37:45 -0800608 gs->flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400609#if IS_ENABLED(CONFIG_IPV6)
610 if (ipv6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700611 rcu_assign_pointer(geneve->sock6, gs);
John W. Linville8ed66f02015-10-26 17:01:44 -0400612 else
613#endif
pravin shelarfceb9c32016-10-28 09:59:16 -0700614 rcu_assign_pointer(geneve->sock4, gs);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700615
616 hash = geneve_net_vni_hash(geneve->vni);
617 hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400618 return 0;
619}
620
John W. Linville8ed66f02015-10-26 17:01:44 -0400621static int geneve_open(struct net_device *dev)
622{
623 struct geneve_dev *geneve = netdev_priv(dev);
624 bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
625 bool metadata = geneve->collect_md;
626 int ret = 0;
627
John W. Linville8ed66f02015-10-26 17:01:44 -0400628#if IS_ENABLED(CONFIG_IPV6)
John W. Linville8ed66f02015-10-26 17:01:44 -0400629 if (ipv6 || metadata)
630 ret = geneve_sock_add(geneve, true);
631#endif
632 if (!ret && (!ipv6 || metadata))
633 ret = geneve_sock_add(geneve, false);
634 if (ret < 0)
635 geneve_sock_release(geneve);
636
637 return ret;
638}
639
John W. Linville2d07dc72015-05-13 12:57:30 -0400640static int geneve_stop(struct net_device *dev)
641{
642 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400643
Pravin B Shelar66d47002015-08-26 23:46:55 -0700644 if (!hlist_unhashed(&geneve->hlist))
645 hlist_del_rcu(&geneve->hlist);
John W. Linville8ed66f02015-10-26 17:01:44 -0400646 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400647 return 0;
648}
649
John W. Linville8ed66f02015-10-26 17:01:44 -0400650static void geneve_build_header(struct genevehdr *geneveh,
651 __be16 tun_flags, u8 vni[3],
652 u8 options_len, u8 *options)
653{
654 geneveh->ver = GENEVE_VER;
655 geneveh->opt_len = options_len / 4;
656 geneveh->oam = !!(tun_flags & TUNNEL_OAM);
657 geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
658 geneveh->rsvd1 = 0;
659 memcpy(geneveh->vni, vni, 3);
660 geneveh->proto_type = htons(ETH_P_TEB);
661 geneveh->rsvd2 = 0;
662
663 memcpy(geneveh->options, options, options_len);
664}
665
Pravin B Shelar371bd102015-08-26 23:46:54 -0700666static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
667 __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
Tom Herbertabe492b2015-12-10 12:37:45 -0800668 u32 flags, bool xnet)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700669{
670 struct genevehdr *gnvh;
671 int min_headroom;
672 int err;
Alexander Duyck14f1f722016-02-19 11:26:24 -0800673 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700674
John W. Linville8ed66f02015-10-26 17:01:44 -0400675 skb_scrub_packet(skb, xnet);
676
Pravin B Shelar371bd102015-08-26 23:46:54 -0700677 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
678 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
679 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400680 if (unlikely(err))
Pravin B Shelar371bd102015-08-26 23:46:54 -0700681 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700682
Alexander Duyckaed069d2016-04-14 15:33:37 -0400683 err = udp_tunnel_handle_offloads(skb, udp_sum);
684 if (err)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700685 goto free_rt;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700686
687 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
John W. Linville8ed66f02015-10-26 17:01:44 -0400688 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700689
690 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
691 return 0;
692
693free_rt:
694 ip_rt_put(rt);
695 return err;
696}
697
John W. Linville8ed66f02015-10-26 17:01:44 -0400698#if IS_ENABLED(CONFIG_IPV6)
699static int geneve6_build_skb(struct dst_entry *dst, 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)
John W. Linville8ed66f02015-10-26 17:01:44 -0400702{
703 struct genevehdr *gnvh;
704 int min_headroom;
705 int err;
Tom Herbertabe492b2015-12-10 12:37:45 -0800706 bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
John W. Linville8ed66f02015-10-26 17:01:44 -0400707
708 skb_scrub_packet(skb, xnet);
709
710 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
711 + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
712 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400713 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400714 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400715
Alexander Duyckaed069d2016-04-14 15:33:37 -0400716 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300717 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400718 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400719
720 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
721 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
722
723 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
724 return 0;
725
726free_dst:
727 dst_release(dst);
728 return err;
729}
730#endif
731
732static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
733 struct net_device *dev,
734 struct flowi4 *fl4,
Mark Gray1200ebb2020-09-16 05:19:35 -0400735 struct ip_tunnel_info *info,
736 __be16 dport, __be16 sport)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700737{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100738 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700739 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100740 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700741 struct rtable *rt = NULL;
742 __u8 tos;
743
pravin shelarfceb9c32016-10-28 09:59:16 -0700744 if (!rcu_dereference(geneve->sock4))
745 return ERR_PTR(-EIO);
746
Pravin B Shelare305ac62015-08-26 23:46:52 -0700747 memset(fl4, 0, sizeof(*fl4));
748 fl4->flowi4_mark = skb->mark;
749 fl4->flowi4_proto = IPPROTO_UDP;
Mark Gray1200ebb2020-09-16 05:19:35 -0400750 fl4->fl4_dport = dport;
751 fl4->fl4_sport = sport;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700752
753 if (info) {
754 fl4->daddr = info->key.u.ipv4.dst;
755 fl4->saddr = info->key.u.ipv4.src;
756 fl4->flowi4_tos = RT_TOS(info->key.tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100757 dst_cache = &info->dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700758 } else {
759 tos = geneve->tos;
760 if (tos == 1) {
761 const struct iphdr *iip = ip_hdr(skb);
762
763 tos = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100764 use_cache = false;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700765 }
766
767 fl4->flowi4_tos = RT_TOS(tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400768 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100769 dst_cache = &geneve->dst_cache;
770 }
771
Paolo Abeni468dfff2016-02-12 15:43:58 +0100772 if (use_cache) {
773 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
774 if (rt)
775 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700776 }
777
778 rt = ip_route_output_key(geneve->net, fl4);
779 if (IS_ERR(rt)) {
780 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700781 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700782 }
783 if (rt->dst.dev == dev) { /* is this necessary? */
784 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700785 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700786 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700787 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100788 if (use_cache)
789 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700790 return rt;
791}
792
John W. Linville8ed66f02015-10-26 17:01:44 -0400793#if IS_ENABLED(CONFIG_IPV6)
794static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
795 struct net_device *dev,
796 struct flowi6 *fl6,
Mark Gray1200ebb2020-09-16 05:19:35 -0400797 struct ip_tunnel_info *info,
798 __be16 dport, __be16 sport)
John W. Linville8ed66f02015-10-26 17:01:44 -0400799{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100800 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400801 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400802 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100803 struct dst_cache *dst_cache;
pravin shelarfceb9c32016-10-28 09:59:16 -0700804 struct geneve_sock *gs6;
John W. Linville3a56f862015-10-26 17:01:45 -0400805 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400806
pravin shelarfceb9c32016-10-28 09:59:16 -0700807 gs6 = rcu_dereference(geneve->sock6);
808 if (!gs6)
809 return ERR_PTR(-EIO);
810
John W. Linville8ed66f02015-10-26 17:01:44 -0400811 memset(fl6, 0, sizeof(*fl6));
812 fl6->flowi6_mark = skb->mark;
813 fl6->flowi6_proto = IPPROTO_UDP;
Mark Gray1200ebb2020-09-16 05:19:35 -0400814 fl6->fl6_dport = dport;
815 fl6->fl6_sport = sport;
John W. Linville8ed66f02015-10-26 17:01:44 -0400816
817 if (info) {
818 fl6->daddr = info->key.u.ipv6.dst;
819 fl6->saddr = info->key.u.ipv6.src;
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100820 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(info->key.tos),
821 info->key.label);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100822 dst_cache = &info->dst_cache;
John W. Linville8ed66f02015-10-26 17:01:44 -0400823 } else {
John W. Linville3a56f862015-10-26 17:01:45 -0400824 prio = geneve->tos;
825 if (prio == 1) {
826 const struct iphdr *iip = ip_hdr(skb);
827
828 prio = ip_tunnel_get_dsfield(iip, skb);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100829 use_cache = false;
John W. Linville3a56f862015-10-26 17:01:45 -0400830 }
831
Daniel Borkmann95caf6f2016-03-18 18:37:58 +0100832 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
833 geneve->label);
John W. Linville8ed66f02015-10-26 17:01:44 -0400834 fl6->daddr = geneve->remote.sin6.sin6_addr;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100835 dst_cache = &geneve->dst_cache;
836 }
837
Paolo Abeni468dfff2016-02-12 15:43:58 +0100838 if (use_cache) {
839 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
840 if (dst)
841 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400842 }
843
Sabrina Dubrocabadbe562019-12-04 15:35:53 +0100844 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
845 NULL);
846 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400847 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
848 return ERR_PTR(-ENETUNREACH);
849 }
850 if (dst->dev == dev) { /* is this necessary? */
851 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
852 dst_release(dst);
853 return ERR_PTR(-ELOOP);
854 }
855
Paolo Abeni468dfff2016-02-12 15:43:58 +0100856 if (use_cache)
857 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400858 return dst;
859}
860#endif
861
Pravin B Shelare305ac62015-08-26 23:46:52 -0700862/* Convert 64 bit tunnel ID to 24 bit VNI. */
863static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
864{
865#ifdef __BIG_ENDIAN
866 vni[0] = (__force __u8)(tun_id >> 16);
867 vni[1] = (__force __u8)(tun_id >> 8);
868 vni[2] = (__force __u8)tun_id;
869#else
870 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
871 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
872 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
873#endif
874}
875
John W. Linville8ed66f02015-10-26 17:01:44 -0400876static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
877 struct ip_tunnel_info *info)
John W. Linville2d07dc72015-05-13 12:57:30 -0400878{
879 struct geneve_dev *geneve = netdev_priv(dev);
pravin shelarfceb9c32016-10-28 09:59:16 -0700880 struct geneve_sock *gs4;
John W. Linville2d07dc72015-05-13 12:57:30 -0400881 struct rtable *rt = NULL;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700882 int err = -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -0400883 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400884 __u8 tos, ttl;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700885 __be16 sport;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700886 __be16 df;
John W. Linville8ed66f02015-10-26 17:01:44 -0400887 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800888 u32 flags = geneve->flags;
John W. Linville2d07dc72015-05-13 12:57:30 -0400889
pravin shelarfceb9c32016-10-28 09:59:16 -0700890 gs4 = rcu_dereference(geneve->sock4);
891 if (!gs4)
892 goto tx_error;
893
Pravin B Shelare305ac62015-08-26 23:46:52 -0700894 if (geneve->collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400895 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700896 netdev_dbg(dev, "no tunnel metadata\n");
897 goto tx_error;
898 }
Jiri Benc7f9562a2015-08-28 20:48:20 +0200899 if (info && ip_tunnel_info_af(info) != AF_INET)
900 goto tx_error;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700901 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400902
Mark Gray1200ebb2020-09-16 05:19:35 -0400903 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
904 rt = geneve_get_v4_rt(skb, dev, &fl4, info,
905 geneve->dst_port, sport);
John W. Linville2d07dc72015-05-13 12:57:30 -0400906 if (IS_ERR(rt)) {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700907 err = PTR_ERR(rt);
John W. Linville2d07dc72015-05-13 12:57:30 -0400908 goto tx_error;
909 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700910
Pravin B Shelare305ac62015-08-26 23:46:52 -0700911 skb_reset_mac_header(skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700912
913 if (info) {
914 const struct ip_tunnel_key *key = &info->key;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700915 u8 *opts = NULL;
916 u8 vni[3];
Pravin B Shelare305ac62015-08-26 23:46:52 -0700917
918 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +0100919 if (info->options_len)
Pravin B Shelar4c222792015-08-30 18:09:38 -0700920 opts = ip_tunnel_info_opts(info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700921
Tom Herbertabe492b2015-12-10 12:37:45 -0800922 if (key->tun_flags & TUNNEL_CSUM)
Alexander Duyck14f1f722016-02-19 11:26:24 -0800923 flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800924 else
Alexander Duyck14f1f722016-02-19 11:26:24 -0800925 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -0800926
Pravin B Shelar371bd102015-08-26 23:46:54 -0700927 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800928 info->options_len, opts, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700929 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400930 goto tx_error;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700931
Sabrina Dubroca5b010142016-12-02 16:49:29 +0100932 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700933 ttl = key->ttl;
934 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700935 } else {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700936 err = geneve_build_skb(rt, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -0800937 0, NULL, flags, xnet);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700938 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -0400939 goto tx_error;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700940
Sabrina Dubroca5b010142016-12-02 16:49:29 +0100941 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700942 ttl = geneve->ttl;
943 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
944 ttl = 1;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700945 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700946 df = 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400947 }
Pravin B Shelar039f5062015-12-24 14:34:54 -0800948 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
949 tos, ttl, df, sport, geneve->dst_port,
950 !net_eq(geneve->net, dev_net(geneve->dev)),
Alexander Duyck14f1f722016-02-19 11:26:24 -0800951 !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX));
John W. Linville2d07dc72015-05-13 12:57:30 -0400952
John W. Linville2d07dc72015-05-13 12:57:30 -0400953 return NETDEV_TX_OK;
954
John W. Linville2d07dc72015-05-13 12:57:30 -0400955tx_error:
John W. Linville2d07dc72015-05-13 12:57:30 -0400956 dev_kfree_skb(skb);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400957
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700958 if (err == -ELOOP)
959 dev->stats.collisions++;
960 else if (err == -ENETUNREACH)
961 dev->stats.tx_carrier_errors++;
Haishuang Yanefeb2262016-06-21 16:26:49 +0800962
963 dev->stats.tx_errors++;
John W. Linville2d07dc72015-05-13 12:57:30 -0400964 return NETDEV_TX_OK;
965}
966
John W. Linville8ed66f02015-10-26 17:01:44 -0400967#if IS_ENABLED(CONFIG_IPV6)
968static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
969 struct ip_tunnel_info *info)
970{
971 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400972 struct dst_entry *dst = NULL;
pravin shelarfceb9c32016-10-28 09:59:16 -0700973 struct geneve_sock *gs6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400974 int err = -EINVAL;
975 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400976 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400977 __be16 sport;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +0100978 __be32 label;
John W. Linville8ed66f02015-10-26 17:01:44 -0400979 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
Tom Herbertabe492b2015-12-10 12:37:45 -0800980 u32 flags = geneve->flags;
John W. Linville8ed66f02015-10-26 17:01:44 -0400981
pravin shelarfceb9c32016-10-28 09:59:16 -0700982 gs6 = rcu_dereference(geneve->sock6);
983 if (!gs6)
984 goto tx_error;
985
John W. Linville8ed66f02015-10-26 17:01:44 -0400986 if (geneve->collect_md) {
987 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
988 netdev_dbg(dev, "no tunnel metadata\n");
989 goto tx_error;
990 }
991 }
992
Mark Gray1200ebb2020-09-16 05:19:35 -0400993 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
994 dst = geneve_get_v6_dst(skb, dev, &fl6, info,
995 geneve->dst_port, sport);
John W. Linville8ed66f02015-10-26 17:01:44 -0400996 if (IS_ERR(dst)) {
997 err = PTR_ERR(dst);
998 goto tx_error;
999 }
1000
John W. Linville8ed66f02015-10-26 17:01:44 -04001001 skb_reset_mac_header(skb);
1002
1003 if (info) {
1004 const struct ip_tunnel_key *key = &info->key;
1005 u8 *opts = NULL;
1006 u8 vni[3];
1007
1008 tunnel_id_to_vni(key->tun_id, vni);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001009 if (info->options_len)
John W. Linville8ed66f02015-10-26 17:01:44 -04001010 opts = ip_tunnel_info_opts(info);
1011
Tom Herbertabe492b2015-12-10 12:37:45 -08001012 if (key->tun_flags & TUNNEL_CSUM)
Jesse Gross35e2d112016-01-20 16:22:47 -08001013 flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001014 else
Jesse Gross35e2d112016-01-20 16:22:47 -08001015 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001016
John W. Linville8ed66f02015-10-26 17:01:44 -04001017 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
1018 info->options_len, opts,
Tom Herbertabe492b2015-12-10 12:37:45 -08001019 flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001020 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -04001021 goto tx_error;
John W. Linville8ed66f02015-10-26 17:01:44 -04001022
Sabrina Dubroca5b010142016-12-02 16:49:29 +01001023 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001024 ttl = key->ttl;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001025 label = info->key.label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001026 } else {
John W. Linville8ed66f02015-10-26 17:01:44 -04001027 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
Tom Herbertabe492b2015-12-10 12:37:45 -08001028 0, NULL, flags, xnet);
John W. Linville8ed66f02015-10-26 17:01:44 -04001029 if (unlikely(err))
Alexander Duyckaed069d2016-04-14 15:33:37 -04001030 goto tx_error;
John W. Linville8ed66f02015-10-26 17:01:44 -04001031
Daniel Borkmann95caf6f2016-03-18 18:37:58 +01001032 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
Sabrina Dubroca5b010142016-12-02 16:49:29 +01001033 ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001034 ttl = geneve->ttl;
1035 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
1036 ttl = 1;
1037 ttl = ttl ? : ip6_dst_hoplimit(dst);
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001038 label = geneve->label;
John W. Linville8ed66f02015-10-26 17:01:44 -04001039 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001040
Pravin B Shelar039f5062015-12-24 14:34:54 -08001041 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001042 &fl6.saddr, &fl6.daddr, prio, ttl, label,
Pravin B Shelar039f5062015-12-24 14:34:54 -08001043 sport, geneve->dst_port,
1044 !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
John W. Linville8ed66f02015-10-26 17:01:44 -04001045 return NETDEV_TX_OK;
1046
1047tx_error:
1048 dev_kfree_skb(skb);
Alexander Duyckaed069d2016-04-14 15:33:37 -04001049
John W. Linville8ed66f02015-10-26 17:01:44 -04001050 if (err == -ELOOP)
1051 dev->stats.collisions++;
1052 else if (err == -ENETUNREACH)
1053 dev->stats.tx_carrier_errors++;
Haishuang Yanefeb2262016-06-21 16:26:49 +08001054
1055 dev->stats.tx_errors++;
John W. Linville8ed66f02015-10-26 17:01:44 -04001056 return NETDEV_TX_OK;
1057}
1058#endif
1059
1060static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1061{
1062 struct geneve_dev *geneve = netdev_priv(dev);
1063 struct ip_tunnel_info *info = NULL;
Jakub Kicinski02595f42017-02-24 11:43:37 -08001064 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001065
1066 if (geneve->collect_md)
1067 info = skb_tunnel_info(skb);
1068
Jakub Kicinski02595f42017-02-24 11:43:37 -08001069 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001070#if IS_ENABLED(CONFIG_IPV6)
1071 if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
1072 (!info && geneve->remote.sa.sa_family == AF_INET6))
Jakub Kicinski02595f42017-02-24 11:43:37 -08001073 err = geneve6_xmit_skb(skb, dev, info);
1074 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001075#endif
Jakub Kicinski02595f42017-02-24 11:43:37 -08001076 err = geneve_xmit_skb(skb, dev, info);
1077 rcu_read_unlock();
1078
1079 return err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001080}
1081
David Wraggaeee0e62016-02-18 17:43:29 +00001082static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict)
David Wragg55e5bfb2016-02-10 00:05:57 +00001083{
Haishuang Yand5d5e8d2016-07-02 15:02:48 +08001084 struct geneve_dev *geneve = netdev_priv(dev);
David Wraggaeee0e62016-02-18 17:43:29 +00001085 /* The max_mtu calculation does not take account of GENEVE
1086 * options, to avoid excluding potentially valid
1087 * configurations.
David Wragg55e5bfb2016-02-10 00:05:57 +00001088 */
Haishuang Yand5d5e8d2016-07-02 15:02:48 +08001089 int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1090
1091 if (geneve->remote.sa.sa_family == AF_INET6)
1092 max_mtu -= sizeof(struct ipv6hdr);
1093 else
1094 max_mtu -= sizeof(struct iphdr);
David Wraggaeee0e62016-02-18 17:43:29 +00001095
1096 if (new_mtu < 68)
David Wragg55e5bfb2016-02-10 00:05:57 +00001097 return -EINVAL;
David Wraggaeee0e62016-02-18 17:43:29 +00001098
1099 if (new_mtu > max_mtu) {
1100 if (strict)
1101 return -EINVAL;
1102
1103 new_mtu = max_mtu;
1104 }
1105
David Wragg55e5bfb2016-02-10 00:05:57 +00001106 dev->mtu = new_mtu;
1107 return 0;
1108}
1109
David Wraggaeee0e62016-02-18 17:43:29 +00001110static int geneve_change_mtu(struct net_device *dev, int new_mtu)
1111{
1112 return __geneve_change_mtu(dev, new_mtu, true);
1113}
1114
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001115static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1116{
1117 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1118 struct geneve_dev *geneve = netdev_priv(dev);
1119 struct rtable *rt;
1120 struct flowi4 fl4;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001121#if IS_ENABLED(CONFIG_IPV6)
1122 struct dst_entry *dst;
1123 struct flowi6 fl6;
1124#endif
Mark Gray1200ebb2020-09-16 05:19:35 -04001125 __be16 sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001126
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001127 if (ip_tunnel_info_af(info) == AF_INET) {
Mark Gray1200ebb2020-09-16 05:19:35 -04001128 sport = udp_flow_src_port(geneve->net, skb,
1129 1, USHRT_MAX, true);
1130
1131 rt = geneve_get_v4_rt(skb, dev, &fl4, info,
1132 geneve->dst_port, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001133 if (IS_ERR(rt))
1134 return PTR_ERR(rt);
1135
1136 ip_rt_put(rt);
1137 info->key.u.ipv4.src = fl4.saddr;
1138#if IS_ENABLED(CONFIG_IPV6)
1139 } else if (ip_tunnel_info_af(info) == AF_INET6) {
Mark Gray1200ebb2020-09-16 05:19:35 -04001140 sport = udp_flow_src_port(geneve->net, skb,
1141 1, USHRT_MAX, true);
1142
1143 dst = geneve_get_v6_dst(skb, dev, &fl6, info,
1144 geneve->dst_port, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001145 if (IS_ERR(dst))
1146 return PTR_ERR(dst);
1147
1148 dst_release(dst);
1149 info->key.u.ipv6.src = fl6.saddr;
1150#endif
1151 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001152 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001153 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001154
Mark Gray1200ebb2020-09-16 05:19:35 -04001155 info->key.tp_src = sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001156 info->key.tp_dst = geneve->dst_port;
1157 return 0;
1158}
1159
John W. Linville2d07dc72015-05-13 12:57:30 -04001160static const struct net_device_ops geneve_netdev_ops = {
1161 .ndo_init = geneve_init,
1162 .ndo_uninit = geneve_uninit,
1163 .ndo_open = geneve_open,
1164 .ndo_stop = geneve_stop,
1165 .ndo_start_xmit = geneve_xmit,
1166 .ndo_get_stats64 = ip_tunnel_get_stats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001167 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001168 .ndo_validate_addr = eth_validate_addr,
1169 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001170 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001171};
1172
1173static void geneve_get_drvinfo(struct net_device *dev,
1174 struct ethtool_drvinfo *drvinfo)
1175{
1176 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1177 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1178}
1179
1180static const struct ethtool_ops geneve_ethtool_ops = {
1181 .get_drvinfo = geneve_get_drvinfo,
1182 .get_link = ethtool_op_get_link,
1183};
1184
1185/* Info for udev, that this is a virtual tunnel endpoint */
1186static struct device_type geneve_type = {
1187 .name = "geneve",
1188};
1189
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001190/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001191 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001192 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001193 */
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001194static void geneve_push_rx_ports(struct net_device *dev)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001195{
1196 struct net *net = dev_net(dev);
1197 struct geneve_net *gn = net_generic(net, geneve_net_id);
1198 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001199
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001200 rcu_read_lock();
Alexander Duycke7b3db52016-06-16 12:20:52 -07001201 list_for_each_entry_rcu(gs, &gn->sock_list, list)
1202 udp_tunnel_push_rx_port(dev, gs->sock,
1203 UDP_TUNNEL_TYPE_GENEVE);
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001204 rcu_read_unlock();
1205}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001206
John W. Linville2d07dc72015-05-13 12:57:30 -04001207/* Initialize the device structure. */
1208static void geneve_setup(struct net_device *dev)
1209{
1210 ether_setup(dev);
1211
1212 dev->netdev_ops = &geneve_netdev_ops;
1213 dev->ethtool_ops = &geneve_ethtool_ops;
1214 dev->destructor = free_netdev;
1215
1216 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1217
John W. Linville2d07dc72015-05-13 12:57:30 -04001218 dev->features |= NETIF_F_LLTX;
1219 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1220 dev->features |= NETIF_F_RXCSUM;
1221 dev->features |= NETIF_F_GSO_SOFTWARE;
1222
John W. Linville2d07dc72015-05-13 12:57:30 -04001223 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1224 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001225
1226 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001227 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001228 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001229 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001230}
1231
1232static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1233 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
1234 [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001235 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001236 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001237 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001238 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001239 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001240 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001241 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1242 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1243 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001244};
1245
1246static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1247{
1248 if (tb[IFLA_ADDRESS]) {
1249 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1250 return -EINVAL;
1251
1252 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1253 return -EADDRNOTAVAIL;
1254 }
1255
1256 if (!data)
1257 return -EINVAL;
1258
1259 if (data[IFLA_GENEVE_ID]) {
1260 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1261
1262 if (vni >= GENEVE_VID_MASK)
1263 return -ERANGE;
1264 }
1265
1266 return 0;
1267}
1268
Pravin B Shelar371bd102015-08-26 23:46:54 -07001269static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1270 __be16 dst_port,
John W. Linville8ed66f02015-10-26 17:01:44 -04001271 union geneve_addr *remote,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001272 u8 vni[],
1273 bool *tun_on_same_port,
1274 bool *tun_collect_md)
1275{
1276 struct geneve_dev *geneve, *t;
1277
1278 *tun_on_same_port = false;
1279 *tun_collect_md = false;
1280 t = NULL;
1281 list_for_each_entry(geneve, &gn->geneve_list, next) {
1282 if (geneve->dst_port == dst_port) {
1283 *tun_collect_md = geneve->collect_md;
1284 *tun_on_same_port = true;
1285 }
1286 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
John W. Linville8ed66f02015-10-26 17:01:44 -04001287 !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
Pravin B Shelar371bd102015-08-26 23:46:54 -07001288 dst_port == geneve->dst_port)
1289 t = geneve;
1290 }
1291 return t;
1292}
1293
Pravin B Shelare305ac62015-08-26 23:46:52 -07001294static int geneve_configure(struct net *net, struct net_device *dev,
John W. Linville8ed66f02015-10-26 17:01:44 -04001295 union geneve_addr *remote,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001296 __u32 vni, __u8 ttl, __u8 tos, __be32 label,
1297 __be16 dst_port, bool metadata, u32 flags)
John W. Linville2d07dc72015-05-13 12:57:30 -04001298{
1299 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001300 struct geneve_dev *t, *geneve = netdev_priv(dev);
1301 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001302 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001303
John W. Linville8ed66f02015-10-26 17:01:44 -04001304 if (!remote)
1305 return -EINVAL;
1306 if (metadata &&
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001307 (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl || label))
John W. Linville8ed66f02015-10-26 17:01:44 -04001308 return -EINVAL;
John W. Linville2d07dc72015-05-13 12:57:30 -04001309
1310 geneve->net = net;
1311 geneve->dev = dev;
1312
John W. Linville2d07dc72015-05-13 12:57:30 -04001313 geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1314 geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1315 geneve->vni[2] = vni & 0x000000ff;
1316
John W. Linville8ed66f02015-10-26 17:01:44 -04001317 if ((remote->sa.sa_family == AF_INET &&
1318 IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1319 (remote->sa.sa_family == AF_INET6 &&
1320 ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
John W. Linville2d07dc72015-05-13 12:57:30 -04001321 return -EINVAL;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001322 if (label && remote->sa.sa_family != AF_INET6)
1323 return -EINVAL;
1324
John W. Linville8ed66f02015-10-26 17:01:44 -04001325 geneve->remote = *remote;
John W. Linville2d07dc72015-05-13 12:57:30 -04001326
Pravin B Shelare305ac62015-08-26 23:46:52 -07001327 geneve->ttl = ttl;
1328 geneve->tos = tos;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001329 geneve->label = label;
John W. Linville7bbe33f2015-09-22 13:09:32 -04001330 geneve->dst_port = dst_port;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001331 geneve->collect_md = metadata;
Tom Herbertabe492b2015-12-10 12:37:45 -08001332 geneve->flags = flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001333
John W. Linville8ed66f02015-10-26 17:01:44 -04001334 t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001335 &tun_on_same_port, &tun_collect_md);
1336 if (t)
1337 return -EBUSY;
1338
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001339 /* make enough headroom for basic scenario */
1340 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1341 if (remote->sa.sa_family == AF_INET)
1342 encap_len += sizeof(struct iphdr);
1343 else
1344 encap_len += sizeof(struct ipv6hdr);
1345 dev->needed_headroom = encap_len + ETH_HLEN;
1346
Pravin B Shelar371bd102015-08-26 23:46:54 -07001347 if (metadata) {
1348 if (tun_on_same_port)
1349 return -EPERM;
1350 } else {
1351 if (tun_collect_md)
1352 return -EPERM;
1353 }
1354
Paolo Abeni468dfff2016-02-12 15:43:58 +01001355 dst_cache_reset(&geneve->dst_cache);
1356
John W. Linville2d07dc72015-05-13 12:57:30 -04001357 err = register_netdevice(dev);
1358 if (err)
1359 return err;
1360
1361 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001362 return 0;
1363}
1364
Pravin B Shelare305ac62015-08-26 23:46:52 -07001365static int geneve_newlink(struct net *net, struct net_device *dev,
1366 struct nlattr *tb[], struct nlattr *data[])
1367{
John W. Linville7bbe33f2015-09-22 13:09:32 -04001368 __be16 dst_port = htons(GENEVE_UDP_PORT);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001369 __u8 ttl = 0, tos = 0;
1370 bool metadata = false;
John W. Linville8ed66f02015-10-26 17:01:44 -04001371 union geneve_addr remote = geneve_remote_unspec;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001372 __be32 label = 0;
Jesse Grosse277de52015-10-16 16:36:00 -07001373 __u32 vni = 0;
Tom Herbertabe492b2015-12-10 12:37:45 -08001374 u32 flags = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001375
John W. Linville8ed66f02015-10-26 17:01:44 -04001376 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1377 return -EINVAL;
1378
1379 if (data[IFLA_GENEVE_REMOTE]) {
1380 remote.sa.sa_family = AF_INET;
1381 remote.sin.sin_addr.s_addr =
1382 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1383 }
1384
1385 if (data[IFLA_GENEVE_REMOTE6]) {
1386 if (!IS_ENABLED(CONFIG_IPV6))
1387 return -EPFNOSUPPORT;
1388
1389 remote.sa.sa_family = AF_INET6;
1390 remote.sin6.sin6_addr =
1391 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1392
1393 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1394 IPV6_ADDR_LINKLOCAL) {
1395 netdev_dbg(dev, "link-local remote is unsupported\n");
1396 return -EINVAL;
1397 }
1398 }
1399
Jesse Grosse277de52015-10-16 16:36:00 -07001400 if (data[IFLA_GENEVE_ID])
1401 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001402
Pravin B Shelare305ac62015-08-26 23:46:52 -07001403 if (data[IFLA_GENEVE_TTL])
1404 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1405
1406 if (data[IFLA_GENEVE_TOS])
1407 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1408
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001409 if (data[IFLA_GENEVE_LABEL])
1410 label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
1411 IPV6_FLOWLABEL_MASK;
1412
Pravin B Shelare305ac62015-08-26 23:46:52 -07001413 if (data[IFLA_GENEVE_PORT])
John W. Linville7bbe33f2015-09-22 13:09:32 -04001414 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001415
1416 if (data[IFLA_GENEVE_COLLECT_METADATA])
1417 metadata = true;
1418
Tom Herbertabe492b2015-12-10 12:37:45 -08001419 if (data[IFLA_GENEVE_UDP_CSUM] &&
Alexander Duyck14f1f722016-02-19 11:26:24 -08001420 !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1421 flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
Tom Herbertabe492b2015-12-10 12:37:45 -08001422
1423 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
1424 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1425 flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
1426
1427 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
1428 nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
1429 flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
1430
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001431 return geneve_configure(net, dev, &remote, vni, ttl, tos, label,
1432 dst_port, metadata, flags);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001433}
1434
John W. Linville2d07dc72015-05-13 12:57:30 -04001435static void geneve_dellink(struct net_device *dev, struct list_head *head)
1436{
1437 struct geneve_dev *geneve = netdev_priv(dev);
1438
John W. Linville2d07dc72015-05-13 12:57:30 -04001439 list_del(&geneve->next);
1440 unregister_netdevice_queue(dev, head);
1441}
1442
1443static size_t geneve_get_size(const struct net_device *dev)
1444{
1445 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001446 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001447 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001448 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001449 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001450 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001451 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001452 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1453 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1454 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
John W. Linville2d07dc72015-05-13 12:57:30 -04001455 0;
1456}
1457
1458static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1459{
1460 struct geneve_dev *geneve = netdev_priv(dev);
1461 __u32 vni;
1462
1463 vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1464 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1465 goto nla_put_failure;
1466
John W. Linville8ed66f02015-10-26 17:01:44 -04001467 if (geneve->remote.sa.sa_family == AF_INET) {
1468 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1469 geneve->remote.sin.sin_addr.s_addr))
1470 goto nla_put_failure;
1471#if IS_ENABLED(CONFIG_IPV6)
1472 } else {
1473 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1474 &geneve->remote.sin6.sin6_addr))
1475 goto nla_put_failure;
1476#endif
1477 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001478
John W. Linvilled8951122015-06-01 15:51:35 -04001479 if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001480 nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos) ||
1481 nla_put_be32(skb, IFLA_GENEVE_LABEL, geneve->label))
John W. Linville8760ce52015-06-01 15:51:34 -04001482 goto nla_put_failure;
1483
John W. Linville7bbe33f2015-09-22 13:09:32 -04001484 if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001485 goto nla_put_failure;
1486
Pravin B Shelare305ac62015-08-26 23:46:52 -07001487 if (geneve->collect_md) {
1488 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1489 goto nla_put_failure;
1490 }
1491
Tom Herbertabe492b2015-12-10 12:37:45 -08001492 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
Alexander Duyck14f1f722016-02-19 11:26:24 -08001493 !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) ||
Tom Herbertabe492b2015-12-10 12:37:45 -08001494 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1495 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
1496 nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
1497 !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
1498 goto nla_put_failure;
1499
John W. Linville2d07dc72015-05-13 12:57:30 -04001500 return 0;
1501
1502nla_put_failure:
1503 return -EMSGSIZE;
1504}
1505
1506static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1507 .kind = "geneve",
1508 .maxtype = IFLA_GENEVE_MAX,
1509 .policy = geneve_policy,
1510 .priv_size = sizeof(struct geneve_dev),
1511 .setup = geneve_setup,
1512 .validate = geneve_validate,
1513 .newlink = geneve_newlink,
1514 .dellink = geneve_dellink,
1515 .get_size = geneve_get_size,
1516 .fill_info = geneve_fill_info,
1517};
1518
Pravin B Shelare305ac62015-08-26 23:46:52 -07001519struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1520 u8 name_assign_type, u16 dst_port)
1521{
1522 struct nlattr *tb[IFLA_MAX + 1];
1523 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001524 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001525 int err;
1526
1527 memset(tb, 0, sizeof(tb));
1528 dev = rtnl_create_link(net, name, name_assign_type,
1529 &geneve_link_ops, tb);
1530 if (IS_ERR(dev))
1531 return dev;
1532
John W. Linville8ed66f02015-10-26 17:01:44 -04001533 err = geneve_configure(net, dev, &geneve_remote_unspec,
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001534 0, 0, 0, 0, htons(dst_port), true,
Paolo Abenic868ee72016-02-17 19:30:01 +01001535 GENEVE_F_UDP_ZERO_CSUM6_RX);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001536 if (err) {
1537 free_netdev(dev);
1538 return ERR_PTR(err);
1539 }
David Wragg7e059152016-02-10 00:05:58 +00001540
1541 /* openvswitch users expect packet sizes to be unrestricted,
1542 * so set the largest MTU we can.
1543 */
David Wraggaeee0e62016-02-18 17:43:29 +00001544 err = __geneve_change_mtu(dev, IP_MAX_MTU, false);
David Wragg7e059152016-02-10 00:05:58 +00001545 if (err)
1546 goto err;
1547
Nicolas Dichtel41009482016-06-13 10:31:07 +02001548 err = rtnl_configure_link(dev, NULL);
1549 if (err < 0)
1550 goto err;
1551
Pravin B Shelare305ac62015-08-26 23:46:52 -07001552 return dev;
David Wragg7e059152016-02-10 00:05:58 +00001553
1554 err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001555 geneve_dellink(dev, &list_kill);
1556 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001557 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001558}
1559EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1560
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001561static int geneve_netdevice_event(struct notifier_block *unused,
1562 unsigned long event, void *ptr)
1563{
1564 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1565
Alexander Duyck7c46a642016-06-16 12:21:00 -07001566 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001567 geneve_push_rx_ports(dev);
1568
1569 return NOTIFY_DONE;
1570}
1571
1572static struct notifier_block geneve_notifier_block __read_mostly = {
1573 .notifier_call = geneve_netdevice_event,
1574};
1575
John W. Linville2d07dc72015-05-13 12:57:30 -04001576static __net_init int geneve_init_net(struct net *net)
1577{
1578 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001579
1580 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001581 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001582 return 0;
1583}
1584
1585static void __net_exit geneve_exit_net(struct net *net)
1586{
1587 struct geneve_net *gn = net_generic(net, geneve_net_id);
1588 struct geneve_dev *geneve, *next;
1589 struct net_device *dev, *aux;
1590 LIST_HEAD(list);
1591
1592 rtnl_lock();
1593
1594 /* gather any geneve devices that were moved into this ns */
1595 for_each_netdev_safe(net, dev, aux)
1596 if (dev->rtnl_link_ops == &geneve_link_ops)
1597 unregister_netdevice_queue(dev, &list);
1598
1599 /* now gather any other geneve devices that were created in this ns */
1600 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1601 /* If geneve->dev is in the same netns, it was already added
1602 * to the list by the previous loop.
1603 */
1604 if (!net_eq(dev_net(geneve->dev), net))
1605 unregister_netdevice_queue(geneve->dev, &list);
1606 }
1607
1608 /* unregister the devices gathered above */
1609 unregister_netdevice_many(&list);
1610 rtnl_unlock();
1611}
1612
1613static struct pernet_operations geneve_net_ops = {
1614 .init = geneve_init_net,
1615 .exit = geneve_exit_net,
1616 .id = &geneve_net_id,
1617 .size = sizeof(struct geneve_net),
1618};
1619
1620static int __init geneve_init_module(void)
1621{
1622 int rc;
1623
1624 rc = register_pernet_subsys(&geneve_net_ops);
1625 if (rc)
1626 goto out1;
1627
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001628 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001629 if (rc)
1630 goto out2;
1631
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001632 rc = rtnl_link_register(&geneve_link_ops);
1633 if (rc)
1634 goto out3;
1635
John W. Linville2d07dc72015-05-13 12:57:30 -04001636 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001637
1638out3:
1639 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001640out2:
1641 unregister_pernet_subsys(&geneve_net_ops);
1642out1:
1643 return rc;
1644}
1645late_initcall(geneve_init_module);
1646
1647static void __exit geneve_cleanup_module(void)
1648{
1649 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001650 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001651 unregister_pernet_subsys(&geneve_net_ops);
1652}
1653module_exit(geneve_cleanup_module);
1654
1655MODULE_LICENSE("GPL");
1656MODULE_VERSION(GENEVE_NETDEV_VER);
1657MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1658MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1659MODULE_ALIAS_RTNL_LINK("geneve");