blob: c1fdd721a730d7122a84079e7a96e6902c14f1fe [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
John W. Linville2d07dc72015-05-13 12:57:30 -04002/*
3 * GENEVE: Generic Network Virtualization Encapsulation
4 *
5 * Copyright (c) 2015 Red Hat, Inc.
John W. Linville2d07dc72015-05-13 12:57:30 -04006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Jakub Kicinskicc698372020-11-20 14:50:52 -080010#include <linux/ethtool.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040011#include <linux/kernel.h>
12#include <linux/module.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040013#include <linux/etherdevice.h>
14#include <linux/hash.h>
David Ahern3616d082019-03-22 06:06:09 -070015#include <net/ipv6_stubs.h>
Pravin B Shelare305ac62015-08-26 23:46:52 -070016#include <net/dst_metadata.h>
Jesse Gross8e816df2015-08-28 16:54:40 -070017#include <net/gro_cells.h>
John W. Linville2d07dc72015-05-13 12:57:30 -040018#include <net/rtnetlink.h>
19#include <net/geneve.h>
Eric Dumazet47210312021-11-15 09:05:51 -080020#include <net/gro.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
John W. Linville2d07dc72015-05-13 12:57:30 -040025#define GENEVE_N_VID (1u << 24)
26#define GENEVE_VID_MASK (GENEVE_N_VID - 1)
27
28#define VNI_HASH_BITS 10
29#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
30
31static bool log_ecn_error = true;
32module_param(log_ecn_error, bool, 0644);
33MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
34
Pravin B Shelar371bd102015-08-26 23:46:54 -070035#define GENEVE_VER 0
36#define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
Alexey Kodanev5edbea62018-04-19 15:42:30 +030037#define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN)
38#define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN)
Pravin B Shelar371bd102015-08-26 23:46:54 -070039
John W. Linville2d07dc72015-05-13 12:57:30 -040040/* per-network namespace private data for this module */
41struct geneve_net {
Pravin B Shelar371bd102015-08-26 23:46:54 -070042 struct list_head geneve_list;
Pravin B Shelar371bd102015-08-26 23:46:54 -070043 struct list_head sock_list;
John W. Linville2d07dc72015-05-13 12:57:30 -040044};
45
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030046static unsigned int geneve_net_id;
Pravin B Shelar371bd102015-08-26 23:46:54 -070047
Jiri Benc4b4c21f2017-07-02 19:00:58 +020048struct geneve_dev_node {
49 struct hlist_node hlist;
50 struct geneve_dev *geneve;
51};
52
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020053struct geneve_config {
54 struct ip_tunnel_info info;
55 bool collect_md;
56 bool use_udp6_rx_checksums;
57 bool ttl_inherit;
58 enum ifla_geneve_df df;
59};
60
John W. Linville2d07dc72015-05-13 12:57:30 -040061/* Pseudo network device */
62struct geneve_dev {
Jiri Benc4b4c21f2017-07-02 19:00:58 +020063 struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */
64#if IS_ENABLED(CONFIG_IPV6)
65 struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */
66#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040067 struct net *net; /* netns for packet i/o */
68 struct net_device *dev; /* netdev for geneve tunnel */
pravin shelarfceb9c32016-10-28 09:59:16 -070069 struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040070#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -070071 struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
John W. Linville8ed66f02015-10-26 17:01:44 -040072#endif
John W. Linville2d07dc72015-05-13 12:57:30 -040073 struct list_head next; /* geneve's per namespace list */
Jesse Gross8e816df2015-08-28 16:54:40 -070074 struct gro_cells gro_cells;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +020075 struct geneve_config cfg;
John W. Linville2d07dc72015-05-13 12:57:30 -040076};
77
Pravin B Shelar371bd102015-08-26 23:46:54 -070078struct geneve_sock {
79 bool collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -070080 struct list_head list;
81 struct socket *sock;
82 struct rcu_head rcu;
83 int refcnt;
Pravin B Shelar66d47002015-08-26 23:46:55 -070084 struct hlist_head vni_list[VNI_HASH_SIZE];
Pravin B Shelar371bd102015-08-26 23:46:54 -070085};
John W. Linville2d07dc72015-05-13 12:57:30 -040086
87static inline __u32 geneve_net_vni_hash(u8 vni[3])
88{
89 __u32 vnid;
90
91 vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
92 return hash_32(vnid, VNI_HASH_BITS);
93}
94
Pravin B Shelare305ac62015-08-26 23:46:52 -070095static __be64 vni_to_tunnel_id(const __u8 *vni)
96{
97#ifdef __BIG_ENDIAN
98 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
99#else
100 return (__force __be64)(((__force u64)vni[0] << 40) |
101 ((__force u64)vni[1] << 48) |
102 ((__force u64)vni[2] << 56));
103#endif
104}
105
pravin shelar9b4437a2016-11-21 11:02:58 -0800106/* Convert 64 bit tunnel ID to 24 bit VNI. */
107static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
108{
109#ifdef __BIG_ENDIAN
110 vni[0] = (__force __u8)(tun_id >> 16);
111 vni[1] = (__force __u8)(tun_id >> 8);
112 vni[2] = (__force __u8)tun_id;
113#else
114 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
115 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
116 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
117#endif
118}
119
pravin shelar2e0b26e2016-11-21 11:03:01 -0800120static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni)
121{
pravin shelar2e0b26e2016-11-21 11:03:01 -0800122 return !memcmp(vni, &tun_id[5], 3);
pravin shelar2e0b26e2016-11-21 11:03:01 -0800123}
124
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100125static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
126{
127 return gs->sock->sk->sk_family;
128}
129
Pravin B Shelar66d47002015-08-26 23:46:55 -0700130static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700131 __be32 addr, u8 vni[])
John W. Linville2d07dc72015-05-13 12:57:30 -0400132{
John W. Linville2d07dc72015-05-13 12:57:30 -0400133 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200134 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400135 __u32 hash;
136
John W. Linville2d07dc72015-05-13 12:57:30 -0400137 /* Find the device for this VNI */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700138 hash = geneve_net_vni_hash(vni);
Pravin B Shelar66d47002015-08-26 23:46:55 -0700139 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200140 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200141 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
142 addr == node->geneve->cfg.info.key.u.ipv4.dst)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200143 return node->geneve;
John W. Linville2d07dc72015-05-13 12:57:30 -0400144 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700145 return NULL;
146}
147
John W. Linville8ed66f02015-10-26 17:01:44 -0400148#if IS_ENABLED(CONFIG_IPV6)
149static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
150 struct in6_addr addr6, u8 vni[])
151{
152 struct hlist_head *vni_list_head;
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200153 struct geneve_dev_node *node;
John W. Linville8ed66f02015-10-26 17:01:44 -0400154 __u32 hash;
155
156 /* Find the device for this VNI */
157 hash = geneve_net_vni_hash(vni);
158 vni_list_head = &gs->vni_list[hash];
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200159 hlist_for_each_entry_rcu(node, vni_list_head, hlist) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200160 if (eq_tun_id_and_vni((u8 *)&node->geneve->cfg.info.key.tun_id, vni) &&
161 ipv6_addr_equal(&addr6, &node->geneve->cfg.info.key.u.ipv6.dst))
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200162 return node->geneve;
John W. Linville8ed66f02015-10-26 17:01:44 -0400163 }
164 return NULL;
165}
166#endif
167
Pravin B Shelar371bd102015-08-26 23:46:54 -0700168static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
169{
170 return (struct genevehdr *)(udp_hdr(skb) + 1);
171}
172
Jiri Benc9fc47542016-02-18 11:22:50 +0100173static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
174 struct sk_buff *skb)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700175{
John W. Linville8ed66f02015-10-26 17:01:44 -0400176 static u8 zero_vni[3];
pravin shelar9b4437a2016-11-21 11:02:58 -0800177 u8 *vni;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700178
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100179 if (geneve_get_sk_family(gs) == AF_INET) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100180 struct iphdr *iph;
pravin shelar9b4437a2016-11-21 11:02:58 -0800181 __be32 addr;
Jiri Benc9fc47542016-02-18 11:22:50 +0100182
John W. Linville8ed66f02015-10-26 17:01:44 -0400183 iph = ip_hdr(skb); /* outer IP header... */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700184
John W. Linville8ed66f02015-10-26 17:01:44 -0400185 if (gs->collect_md) {
186 vni = zero_vni;
187 addr = 0;
188 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100189 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400190 addr = iph->saddr;
191 }
192
Jiri Benc9fc47542016-02-18 11:22:50 +0100193 return geneve_lookup(gs, addr, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400194#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100195 } else if (geneve_get_sk_family(gs) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800196 static struct in6_addr zero_addr6;
Jiri Benc9fc47542016-02-18 11:22:50 +0100197 struct ipv6hdr *ip6h;
198 struct in6_addr addr6;
199
John W. Linville8ed66f02015-10-26 17:01:44 -0400200 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
201
202 if (gs->collect_md) {
203 vni = zero_vni;
204 addr6 = zero_addr6;
205 } else {
Jiri Benc9fc47542016-02-18 11:22:50 +0100206 vni = geneve_hdr(skb)->vni;
John W. Linville8ed66f02015-10-26 17:01:44 -0400207 addr6 = ip6h->saddr;
208 }
209
Jiri Benc9fc47542016-02-18 11:22:50 +0100210 return geneve6_lookup(gs, addr6, vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400211#endif
Pravin B Shelar371bd102015-08-26 23:46:54 -0700212 }
Jiri Benc9fc47542016-02-18 11:22:50 +0100213 return NULL;
214}
215
216/* geneve receive/decap routine */
217static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
218 struct sk_buff *skb)
219{
220 struct genevehdr *gnvh = geneve_hdr(skb);
221 struct metadata_dst *tun_dst = NULL;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700222 unsigned int len;
Jiri Benc9fc47542016-02-18 11:22:50 +0100223 int err = 0;
224 void *oiph;
John W. Linville2d07dc72015-05-13 12:57:30 -0400225
Pravin B Shelar371bd102015-08-26 23:46:54 -0700226 if (ip_tunnel_collect_metadata() || gs->collect_md) {
Pravin B Shelare305ac62015-08-26 23:46:52 -0700227 __be16 flags;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700228
Yi-Hung Wei9c2e14b2020-11-10 16:16:40 -0800229 flags = TUNNEL_KEY | (gnvh->oam ? TUNNEL_OAM : 0) |
Pravin B Shelare305ac62015-08-26 23:46:52 -0700230 (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
231
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100232 tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
Pravin B Shelare305ac62015-08-26 23:46:52 -0700233 vni_to_tunnel_id(gnvh->vni),
234 gnvh->opt_len * 4);
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700235 if (!tun_dst) {
236 geneve->dev->stats.rx_dropped++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700237 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700238 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700239 /* Update tunnel dst according to Geneve options. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700240 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700241 gnvh->options, gnvh->opt_len * 4,
242 TUNNEL_GENEVE_OPT);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700243 } else {
244 /* Drop packets w/ critical options,
245 * since we don't support any...
246 */
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700247 if (gnvh->critical) {
248 geneve->dev->stats.rx_frame_errors++;
249 geneve->dev->stats.rx_errors++;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700250 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700251 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700252 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400253
254 skb_reset_mac_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400255 skb->protocol = eth_type_trans(skb, geneve->dev);
256 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
257
Pravin B Shelare305ac62015-08-26 23:46:52 -0700258 if (tun_dst)
259 skb_dst_set(skb, &tun_dst->dst);
260
John W. Linville2d07dc72015-05-13 12:57:30 -0400261 /* Ignore packet loops (and multicast echo) */
Jakub Kicinskic02bd112020-12-09 14:39:56 -0800262 if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
263 geneve->dev->stats.rx_errors++;
264 goto drop;
Eric Dumazet4179b002020-12-01 01:05:07 -0800265 }
Jakub Kicinskic02bd112020-12-09 14:39:56 -0800266
Jiri Benc9fc47542016-02-18 11:22:50 +0100267 oiph = skb_network_header(skb);
John W. Linville2d07dc72015-05-13 12:57:30 -0400268 skb_reset_network_header(skb);
269
Jiri Benc9fc47542016-02-18 11:22:50 +0100270 if (geneve_get_sk_family(gs) == AF_INET)
271 err = IP_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400272#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100273 else
274 err = IP6_ECN_decapsulate(oiph, skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400275#endif
John W. Linville2d07dc72015-05-13 12:57:30 -0400276
277 if (unlikely(err)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400278 if (log_ecn_error) {
Jiri Benc9fc47542016-02-18 11:22:50 +0100279 if (geneve_get_sk_family(gs) == AF_INET)
John W. Linville8ed66f02015-10-26 17:01:44 -0400280 net_info_ratelimited("non-ECT from %pI4 "
281 "with TOS=%#x\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100282 &((struct iphdr *)oiph)->saddr,
283 ((struct iphdr *)oiph)->tos);
John W. Linville8ed66f02015-10-26 17:01:44 -0400284#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc9fc47542016-02-18 11:22:50 +0100285 else
John W. Linville8ed66f02015-10-26 17:01:44 -0400286 net_info_ratelimited("non-ECT from %pI6\n",
Jiri Benc9fc47542016-02-18 11:22:50 +0100287 &((struct ipv6hdr *)oiph)->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400288#endif
289 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400290 if (err > 1) {
291 ++geneve->dev->stats.rx_frame_errors;
292 ++geneve->dev->stats.rx_errors;
293 goto drop;
294 }
295 }
296
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700297 len = skb->len;
298 err = gro_cells_receive(&geneve->gro_cells, skb);
Fabian Frederick1e845272020-10-05 22:34:58 +0200299 if (likely(err == NET_RX_SUCCESS))
300 dev_sw_netstats_rx_add(geneve->dev, len);
301
John W. Linville2d07dc72015-05-13 12:57:30 -0400302 return;
303drop:
304 /* Consume bad packet */
305 kfree_skb(skb);
306}
307
308/* Setup stats when device is created */
309static int geneve_init(struct net_device *dev)
310{
Jesse Gross8e816df2015-08-28 16:54:40 -0700311 struct geneve_dev *geneve = netdev_priv(dev);
312 int err;
313
John W. Linville2d07dc72015-05-13 12:57:30 -0400314 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
315 if (!dev->tstats)
316 return -ENOMEM;
Jesse Gross8e816df2015-08-28 16:54:40 -0700317
318 err = gro_cells_init(&geneve->gro_cells, dev);
319 if (err) {
320 free_percpu(dev->tstats);
321 return err;
322 }
323
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200324 err = dst_cache_init(&geneve->cfg.info.dst_cache, GFP_KERNEL);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100325 if (err) {
326 free_percpu(dev->tstats);
327 gro_cells_destroy(&geneve->gro_cells);
328 return err;
329 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400330 return 0;
331}
332
333static void geneve_uninit(struct net_device *dev)
334{
Jesse Gross8e816df2015-08-28 16:54:40 -0700335 struct geneve_dev *geneve = netdev_priv(dev);
336
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200337 dst_cache_destroy(&geneve->cfg.info.dst_cache);
Jesse Gross8e816df2015-08-28 16:54:40 -0700338 gro_cells_destroy(&geneve->gro_cells);
John W. Linville2d07dc72015-05-13 12:57:30 -0400339 free_percpu(dev->tstats);
340}
341
Pravin B Shelar371bd102015-08-26 23:46:54 -0700342/* Callback from net/ipv4/udp.c to receive packets */
343static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
344{
345 struct genevehdr *geneveh;
Jiri Benc9fc47542016-02-18 11:22:50 +0100346 struct geneve_dev *geneve;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700347 struct geneve_sock *gs;
348 int opts_len;
349
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700350 /* Need UDP and Geneve header to be present */
Pravin B Shelar371bd102015-08-26 23:46:54 -0700351 if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200352 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700353
354 /* Return packets with reserved bits set */
355 geneveh = geneve_hdr(skb);
356 if (unlikely(geneveh->ver != GENEVE_VER))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200357 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700358
359 if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200360 goto drop;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700361
Jiri Benc9fc47542016-02-18 11:22:50 +0100362 gs = rcu_dereference_sk_user_data(sk);
363 if (!gs)
364 goto drop;
365
366 geneve = geneve_lookup_skb(gs, skb);
367 if (!geneve)
368 goto drop;
369
Pravin B Shelar371bd102015-08-26 23:46:54 -0700370 opts_len = geneveh->opt_len * 4;
371 if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
Jiri Benc7f290c92016-02-18 11:22:52 +0100372 htons(ETH_P_TEB),
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700373 !net_eq(geneve->net, dev_net(geneve->dev)))) {
374 geneve->dev->stats.rx_dropped++;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700375 goto drop;
Girish Moodalbailfe741e22017-06-08 17:07:48 -0700376 }
Pravin B Shelar371bd102015-08-26 23:46:54 -0700377
Jiri Benc9fc47542016-02-18 11:22:50 +0100378 geneve_rx(geneve, gs, skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700379 return 0;
380
381drop:
382 /* Consume bad packet */
383 kfree_skb(skb);
384 return 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700385}
386
Stefano Brivioa0796642018-11-08 12:19:18 +0100387/* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */
388static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb)
389{
390 struct genevehdr *geneveh;
391 struct geneve_sock *gs;
392 u8 zero_vni[3] = { 0 };
393 u8 *vni = zero_vni;
394
Stefano Brivioeccc73a2019-06-11 00:27:06 +0200395 if (!pskb_may_pull(skb, skb_transport_offset(skb) + GENEVE_BASE_HLEN))
Stefano Brivioa0796642018-11-08 12:19:18 +0100396 return -EINVAL;
397
398 geneveh = geneve_hdr(skb);
399 if (geneveh->ver != GENEVE_VER)
400 return -EINVAL;
401
402 if (geneveh->proto_type != htons(ETH_P_TEB))
403 return -EINVAL;
404
405 gs = rcu_dereference_sk_user_data(sk);
406 if (!gs)
407 return -ENOENT;
408
409 if (geneve_get_sk_family(gs) == AF_INET) {
410 struct iphdr *iph = ip_hdr(skb);
411 __be32 addr4 = 0;
412
413 if (!gs->collect_md) {
414 vni = geneve_hdr(skb)->vni;
415 addr4 = iph->daddr;
416 }
417
418 return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT;
419 }
420
421#if IS_ENABLED(CONFIG_IPV6)
422 if (geneve_get_sk_family(gs) == AF_INET6) {
423 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Nathan Chancellor8a962c42018-11-16 18:36:27 -0700424 struct in6_addr addr6;
425
426 memset(&addr6, 0, sizeof(struct in6_addr));
Stefano Brivioa0796642018-11-08 12:19:18 +0100427
428 if (!gs->collect_md) {
429 vni = geneve_hdr(skb)->vni;
430 addr6 = ip6h->daddr;
431 }
432
433 return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT;
434 }
435#endif
436
437 return -EPFNOSUPPORT;
438}
439
Pravin B Shelar371bd102015-08-26 23:46:54 -0700440static struct socket *geneve_create_sock(struct net *net, bool ipv6,
pravin shelar9b4437a2016-11-21 11:02:58 -0800441 __be16 port, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700442{
443 struct socket *sock;
444 struct udp_port_cfg udp_conf;
445 int err;
446
447 memset(&udp_conf, 0, sizeof(udp_conf));
448
449 if (ipv6) {
450 udp_conf.family = AF_INET6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400451 udp_conf.ipv6_v6only = 1;
pravin shelar9b4437a2016-11-21 11:02:58 -0800452 udp_conf.use_udp6_rx_checksums = ipv6_rx_csum;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700453 } else {
454 udp_conf.family = AF_INET;
455 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
456 }
457
458 udp_conf.local_udp_port = port;
459
460 /* Open UDP socket */
461 err = udp_sock_create(net, &udp_conf, &sock);
462 if (err < 0)
463 return ERR_PTR(err);
464
Paolo Abeni61630c42021-03-30 12:28:54 +0200465 udp_allow_gso(sock->sk);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700466 return sock;
467}
468
Pravin B Shelar371bd102015-08-26 23:46:54 -0700469static int geneve_hlen(struct genevehdr *gh)
470{
471 return sizeof(*gh) + gh->opt_len * 4;
472}
473
David Millerd4546c22018-06-24 14:13:49 +0900474static struct sk_buff *geneve_gro_receive(struct sock *sk,
475 struct list_head *head,
476 struct sk_buff *skb)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700477{
David Millerd4546c22018-06-24 14:13:49 +0900478 struct sk_buff *pp = NULL;
479 struct sk_buff *p;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700480 struct genevehdr *gh, *gh2;
481 unsigned int hlen, gh_len, off_gnv;
482 const struct packet_offload *ptype;
483 __be16 type;
484 int flush = 1;
485
486 off_gnv = skb_gro_offset(skb);
487 hlen = off_gnv + sizeof(*gh);
488 gh = skb_gro_header_fast(skb, off_gnv);
489 if (skb_gro_header_hard(skb, hlen)) {
490 gh = skb_gro_header_slow(skb, hlen, off_gnv);
491 if (unlikely(!gh))
492 goto out;
493 }
494
495 if (gh->ver != GENEVE_VER || gh->oam)
496 goto out;
497 gh_len = geneve_hlen(gh);
498
499 hlen = off_gnv + gh_len;
500 if (skb_gro_header_hard(skb, hlen)) {
501 gh = skb_gro_header_slow(skb, hlen, off_gnv);
502 if (unlikely(!gh))
503 goto out;
504 }
505
David Millerd4546c22018-06-24 14:13:49 +0900506 list_for_each_entry(p, head, list) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700507 if (!NAPI_GRO_CB(p)->same_flow)
508 continue;
509
510 gh2 = (struct genevehdr *)(p->data + off_gnv);
511 if (gh->opt_len != gh2->opt_len ||
512 memcmp(gh, gh2, gh_len)) {
513 NAPI_GRO_CB(p)->same_flow = 0;
514 continue;
515 }
516 }
517
518 type = gh->proto_type;
519
Pravin B Shelar371bd102015-08-26 23:46:54 -0700520 ptype = gro_find_receive_by_type(type);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800521 if (!ptype)
Eric Dumazetfc1ca332021-11-23 14:56:07 -0800522 goto out;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700523
524 skb_gro_pull(skb, gh_len);
525 skb_gro_postpull_rcsum(skb, gh, gh_len);
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200526 pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800527 flush = 0;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700528
Pravin B Shelar371bd102015-08-26 23:46:54 -0700529out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200530 skb_gro_flush_final(skb, pp, flush);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700531
532 return pp;
533}
534
Tom Herbert4a0090a2016-04-05 08:22:55 -0700535static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
536 int nhoff)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700537{
538 struct genevehdr *gh;
539 struct packet_offload *ptype;
540 __be16 type;
541 int gh_len;
542 int err = -ENOSYS;
543
Pravin B Shelar371bd102015-08-26 23:46:54 -0700544 gh = (struct genevehdr *)(skb->data + nhoff);
545 gh_len = geneve_hlen(gh);
546 type = gh->proto_type;
547
Pravin B Shelar371bd102015-08-26 23:46:54 -0700548 ptype = gro_find_complete_by_type(type);
549 if (ptype)
550 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
551
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700552 skb_set_inner_mac_header(skb, nhoff + gh_len);
553
Pravin B Shelar371bd102015-08-26 23:46:54 -0700554 return err;
555}
556
557/* Create new listen socket if needed */
558static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800559 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700560{
561 struct geneve_net *gn = net_generic(net, geneve_net_id);
562 struct geneve_sock *gs;
563 struct socket *sock;
564 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700565 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700566
567 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
568 if (!gs)
569 return ERR_PTR(-ENOMEM);
570
pravin shelar9b4437a2016-11-21 11:02:58 -0800571 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700572 if (IS_ERR(sock)) {
573 kfree(gs);
574 return ERR_CAST(sock);
575 }
576
577 gs->sock = sock;
578 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700579 for (h = 0; h < VNI_HASH_SIZE; ++h)
580 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700581
582 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700583 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584
585 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700586 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700587 tunnel_cfg.sk_user_data = gs;
588 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700589 tunnel_cfg.gro_receive = geneve_gro_receive;
590 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700591 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100592 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700593 tunnel_cfg.encap_destroy = NULL;
594 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700595 list_add(&gs->list, &gn->sock_list);
596 return gs;
597}
598
John W. Linville8ed66f02015-10-26 17:01:44 -0400599static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700600{
John W. Linville8ed66f02015-10-26 17:01:44 -0400601 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700602 return;
603
604 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700605 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700606 udp_tunnel_sock_release(gs->sock);
607 kfree_rcu(gs, rcu);
608}
609
John W. Linville8ed66f02015-10-26 17:01:44 -0400610static void geneve_sock_release(struct geneve_dev *geneve)
611{
pravin shelarfceb9c32016-10-28 09:59:16 -0700612 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400613#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700614 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
615
616 rcu_assign_pointer(geneve->sock6, NULL);
617#endif
618
619 rcu_assign_pointer(geneve->sock4, NULL);
620 synchronize_net();
621
622 __geneve_sock_release(gs4);
623#if IS_ENABLED(CONFIG_IPV6)
624 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400625#endif
626}
627
Pravin B Shelar371bd102015-08-26 23:46:54 -0700628static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400629 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700630 __be16 dst_port)
631{
632 struct geneve_sock *gs;
633
634 list_for_each_entry(gs, &gn->sock_list, list) {
635 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100636 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700637 return gs;
638 }
639 }
640 return NULL;
641}
642
John W. Linville8ed66f02015-10-26 17:01:44 -0400643static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400644{
John W. Linville2d07dc72015-05-13 12:57:30 -0400645 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700646 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200647 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400648 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800649 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700650 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400651
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200652 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700653 if (gs) {
654 gs->refcnt++;
655 goto out;
656 }
657
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200658 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
659 geneve->cfg.use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400660 if (IS_ERR(gs))
661 return PTR_ERR(gs);
662
Pravin B Shelar371bd102015-08-26 23:46:54 -0700663out:
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200664 gs->collect_md = geneve->cfg.collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400665#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200666 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700667 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200668 node = &geneve->hlist6;
669 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400670#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200671 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700672 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200673 node = &geneve->hlist4;
674 }
675 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700676
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200677 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
pravin shelar9b4437a2016-11-21 11:02:58 -0800678 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200679 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400680 return 0;
681}
682
John W. Linville8ed66f02015-10-26 17:01:44 -0400683static int geneve_open(struct net_device *dev)
684{
685 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200686 bool metadata = geneve->cfg.collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100687 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400688 int ret = 0;
689
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200690 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100691 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400692#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100693 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400694 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100695 if (ret < 0 && ret != -EAFNOSUPPORT)
696 ipv4 = false;
697 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400698#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100699 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400700 ret = geneve_sock_add(geneve, false);
701 if (ret < 0)
702 geneve_sock_release(geneve);
703
704 return ret;
705}
706
John W. Linville2d07dc72015-05-13 12:57:30 -0400707static int geneve_stop(struct net_device *dev)
708{
709 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400710
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200711 hlist_del_init_rcu(&geneve->hlist4.hlist);
712#if IS_ENABLED(CONFIG_IPV6)
713 hlist_del_init_rcu(&geneve->hlist6.hlist);
714#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400715 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400716 return 0;
717}
718
John W. Linville8ed66f02015-10-26 17:01:44 -0400719static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800720 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400721{
722 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800723 geneveh->opt_len = info->options_len / 4;
724 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
725 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400726 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800727 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400728 geneveh->proto_type = htons(ETH_P_TEB);
729 geneveh->rsvd2 = 0;
730
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700731 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
732 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400733}
734
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800735static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
736 const struct ip_tunnel_info *info,
737 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700738{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800739 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700740 struct genevehdr *gnvh;
741 int min_headroom;
742 int err;
743
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800744 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400745 skb_scrub_packet(skb, xnet);
746
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800747 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
748 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400749 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400750 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400751 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400752
Alexander Duyckaed069d2016-04-14 15:33:37 -0400753 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300754 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400755 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400756
Johannes Bergd58ff352017-06-16 14:29:23 +0200757 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800758 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400759 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
760 return 0;
761
762free_dst:
763 dst_release(dst);
764 return err;
765}
John W. Linville8ed66f02015-10-26 17:01:44 -0400766
767static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
768 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700769 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400770 struct flowi4 *fl4,
Mark Gray34beb212020-09-16 05:19:35 -0400771 const struct ip_tunnel_info *info,
772 __be16 dport, __be16 sport)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700773{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100774 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700775 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100776 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700777 struct rtable *rt = NULL;
778 __u8 tos;
779
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700780 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700781 return ERR_PTR(-EIO);
782
Pravin B Shelare305ac62015-08-26 23:46:52 -0700783 memset(fl4, 0, sizeof(*fl4));
784 fl4->flowi4_mark = skb->mark;
785 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800786 fl4->daddr = info->key.u.ipv4.dst;
787 fl4->saddr = info->key.u.ipv4.src;
Mark Gray34beb212020-09-16 05:19:35 -0400788 fl4->fl4_dport = dport;
789 fl4->fl4_sport = sport;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700790
pravin shelar9b4437a2016-11-21 11:02:58 -0800791 tos = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200792 if ((tos == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800793 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
794 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100795 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800796 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100797
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800798 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100799 if (use_cache) {
800 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
801 if (rt)
802 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700803 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700804 rt = ip_route_output_key(geneve->net, fl4);
805 if (IS_ERR(rt)) {
806 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700807 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700808 }
809 if (rt->dst.dev == dev) { /* is this necessary? */
810 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700811 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700812 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700813 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100814 if (use_cache)
815 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700816 return rt;
817}
818
John W. Linville8ed66f02015-10-26 17:01:44 -0400819#if IS_ENABLED(CONFIG_IPV6)
820static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
821 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700822 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400823 struct flowi6 *fl6,
Mark Gray34beb212020-09-16 05:19:35 -0400824 const struct ip_tunnel_info *info,
825 __be16 dport, __be16 sport)
John W. Linville8ed66f02015-10-26 17:01:44 -0400826{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100827 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400828 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400829 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100830 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400831 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400832
pravin shelarfceb9c32016-10-28 09:59:16 -0700833 if (!gs6)
834 return ERR_PTR(-EIO);
835
John W. Linville8ed66f02015-10-26 17:01:44 -0400836 memset(fl6, 0, sizeof(*fl6));
837 fl6->flowi6_mark = skb->mark;
838 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800839 fl6->daddr = info->key.u.ipv6.dst;
840 fl6->saddr = info->key.u.ipv6.src;
Mark Gray34beb212020-09-16 05:19:35 -0400841 fl6->fl6_dport = dport;
842 fl6->fl6_sport = sport;
843
pravin shelar9b4437a2016-11-21 11:02:58 -0800844 prio = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200845 if ((prio == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800846 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
847 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100848 }
849
pravin shelar9b4437a2016-11-21 11:02:58 -0800850 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
851 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800852 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100853 if (use_cache) {
854 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
855 if (dst)
856 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400857 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100858 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
859 NULL);
860 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400861 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
862 return ERR_PTR(-ENETUNREACH);
863 }
864 if (dst->dev == dev) { /* is this necessary? */
865 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
866 dst_release(dst);
867 return ERR_PTR(-ELOOP);
868 }
869
Paolo Abeni468dfff2016-02-12 15:43:58 +0100870 if (use_cache)
871 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400872 return dst;
873}
874#endif
875
pravin shelar9b4437a2016-11-21 11:02:58 -0800876static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800877 struct geneve_dev *geneve,
878 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700879{
pravin shelar9b4437a2016-11-21 11:02:58 -0800880 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
881 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
882 const struct ip_tunnel_key *key = &info->key;
883 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400884 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400885 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100886 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700887 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800888 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400889
Phillip Potterd13f0482021-04-23 00:49:45 +0100890 if (!pskb_inet_may_pull(skb))
Phillip Potter6628ddf2021-04-11 12:28:24 +0100891 return -EINVAL;
892
Mark Gray34beb212020-09-16 05:19:35 -0400893 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
894 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
895 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800896 if (IS_ERR(rt))
897 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700898
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200899 err = skb_tunnel_check_pmtu(skb, &rt->dst,
900 GENEVE_IPV4_HLEN + info->options_len,
901 netif_is_any_bridge_port(dev));
902 if (err < 0) {
903 dst_release(&rt->dst);
904 return err;
905 } else if (err) {
906 struct ip_tunnel_info *info;
907
908 info = skb_tunnel_info(skb);
909 if (info) {
Antoine Tenart68c1a943e2021-03-25 16:35:33 +0100910 struct ip_tunnel_info *unclone;
911
912 unclone = skb_tunnel_info_unclone(skb);
913 if (unlikely(!unclone)) {
914 dst_release(&rt->dst);
915 return -ENOMEM;
916 }
917
918 unclone->key.u.ipv4.dst = fl4.saddr;
919 unclone->key.u.ipv4.src = fl4.daddr;
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200920 }
921
922 if (!pskb_may_pull(skb, ETH_HLEN)) {
923 dst_release(&rt->dst);
924 return -EINVAL;
925 }
926
927 skb->protocol = eth_type_trans(skb, geneve->dev);
928 netif_rx(skb);
929 dst_release(&rt->dst);
930 return -EMSGSIZE;
931 }
Xin Long52a589d2017-12-25 14:43:58 +0800932
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200933 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800934 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700935 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100936
937 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700938 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800939 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200940 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800941 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
942 else
943 ttl = key->ttl;
944 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100945
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200946 if (geneve->cfg.df == GENEVE_DF_SET) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100947 df = htons(IP_DF);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200948 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100949 struct ethhdr *eth = eth_hdr(skb);
950
951 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
952 df = htons(IP_DF);
953 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
954 struct iphdr *iph = ip_hdr(skb);
955
956 if (iph->frag_off & htons(IP_DF))
957 df = htons(IP_DF);
958 }
959 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400960 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800961
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800962 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800963 if (unlikely(err))
964 return err;
965
Pravin B Shelar039f5062015-12-24 14:34:54 -0800966 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200967 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800968 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800969 !(info->key.tun_flags & TUNNEL_CSUM));
970 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400971}
972
John W. Linville8ed66f02015-10-26 17:01:44 -0400973#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800974static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800975 struct geneve_dev *geneve,
976 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400977{
pravin shelar9b4437a2016-11-21 11:02:58 -0800978 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
979 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
980 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400981 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400982 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400983 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400984 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800985 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400986
Phillip Potterd13f0482021-04-23 00:49:45 +0100987 if (!pskb_inet_may_pull(skb))
Phillip Potter6628ddf2021-04-11 12:28:24 +0100988 return -EINVAL;
989
Mark Gray34beb212020-09-16 05:19:35 -0400990 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
991 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
992 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800993 if (IS_ERR(dst))
994 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400995
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200996 err = skb_tunnel_check_pmtu(skb, dst,
997 GENEVE_IPV6_HLEN + info->options_len,
998 netif_is_any_bridge_port(dev));
999 if (err < 0) {
1000 dst_release(dst);
1001 return err;
1002 } else if (err) {
1003 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1004
1005 if (info) {
Antoine Tenart68c1a943e2021-03-25 16:35:33 +01001006 struct ip_tunnel_info *unclone;
1007
1008 unclone = skb_tunnel_info_unclone(skb);
1009 if (unlikely(!unclone)) {
1010 dst_release(dst);
1011 return -ENOMEM;
1012 }
1013
1014 unclone->key.u.ipv6.dst = fl6.saddr;
1015 unclone->key.u.ipv6.src = fl6.daddr;
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001016 }
1017
1018 if (!pskb_may_pull(skb, ETH_HLEN)) {
1019 dst_release(dst);
1020 return -EINVAL;
1021 }
1022
1023 skb->protocol = eth_type_trans(skb, geneve->dev);
1024 netif_rx(skb);
1025 dst_release(dst);
1026 return -EMSGSIZE;
1027 }
Xin Long52a589d2017-12-25 14:43:58 +08001028
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001029 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001030 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001031 ttl = key->ttl;
1032 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +01001033 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -08001034 ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001035 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001036 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1037 else
1038 ttl = key->ttl;
1039 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -04001040 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +08001041 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -08001042 if (unlikely(err))
1043 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001044
Pravin B Shelar039f5062015-12-24 14:34:54 -08001045 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001046 &fl6.saddr, &fl6.daddr, prio, ttl,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001047 info->key.label, sport, geneve->cfg.info.key.tp_dst,
pravin shelar9b4437a2016-11-21 11:02:58 -08001048 !(info->key.tun_flags & TUNNEL_CSUM));
1049 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -04001050}
1051#endif
1052
1053static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1054{
1055 struct geneve_dev *geneve = netdev_priv(dev);
1056 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -08001057 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001058
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001059 if (geneve->cfg.collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001060 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001061 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001062 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +02001063 dev_kfree_skb(skb);
1064 dev->stats.tx_dropped++;
1065 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -08001066 }
1067 } else {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001068 info = &geneve->cfg.info;
pravin shelar9b4437a2016-11-21 11:02:58 -08001069 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001070
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001071 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001072#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001073 if (info->mode & IP_TUNNEL_INFO_IPV6)
1074 err = geneve6_xmit_skb(skb, dev, geneve, info);
1075 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001076#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001077 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001078 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001079
1080 if (likely(!err))
1081 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001082
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001083 if (err != -EMSGSIZE)
1084 dev_kfree_skb(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001085
1086 if (err == -ELOOP)
1087 dev->stats.collisions++;
1088 else if (err == -ENETUNREACH)
1089 dev->stats.tx_carrier_errors++;
1090
1091 dev->stats.tx_errors++;
1092 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -04001093}
1094
Jarod Wilson91572082016-10-20 13:55:20 -04001095static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001096{
Jarod Wilson91572082016-10-20 13:55:20 -04001097 if (new_mtu > dev->max_mtu)
1098 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001099 else if (new_mtu < dev->min_mtu)
1100 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001101
David Wragg55e5bfb2016-02-10 00:05:57 +00001102 dev->mtu = new_mtu;
1103 return 0;
1104}
1105
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001106static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1107{
1108 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1109 struct geneve_dev *geneve = netdev_priv(dev);
Mark Gray34beb212020-09-16 05:19:35 -04001110 __be16 sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001111
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001112 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001113 struct rtable *rt;
1114 struct flowi4 fl4;
1115
Mark Gray34beb212020-09-16 05:19:35 -04001116 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1117 sport = udp_flow_src_port(geneve->net, skb,
1118 1, USHRT_MAX, true);
1119
1120 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1121 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001122 if (IS_ERR(rt))
1123 return PTR_ERR(rt);
1124
1125 ip_rt_put(rt);
1126 info->key.u.ipv4.src = fl4.saddr;
1127#if IS_ENABLED(CONFIG_IPV6)
1128 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001129 struct dst_entry *dst;
1130 struct flowi6 fl6;
1131
Mark Gray34beb212020-09-16 05:19:35 -04001132 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1133 sport = udp_flow_src_port(geneve->net, skb,
1134 1, USHRT_MAX, true);
1135
1136 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1137 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001138 if (IS_ERR(dst))
1139 return PTR_ERR(dst);
1140
1141 dst_release(dst);
1142 info->key.u.ipv6.src = fl6.saddr;
1143#endif
1144 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001145 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001146 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001147
Mark Gray34beb212020-09-16 05:19:35 -04001148 info->key.tp_src = sport;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001149 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001150 return 0;
1151}
1152
John W. Linville2d07dc72015-05-13 12:57:30 -04001153static const struct net_device_ops geneve_netdev_ops = {
1154 .ndo_init = geneve_init,
1155 .ndo_uninit = geneve_uninit,
1156 .ndo_open = geneve_open,
1157 .ndo_stop = geneve_stop,
1158 .ndo_start_xmit = geneve_xmit,
Heiner Kallweitb220a4a2020-11-07 21:52:06 +01001159 .ndo_get_stats64 = dev_get_tstats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001160 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001161 .ndo_validate_addr = eth_validate_addr,
1162 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001163 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001164};
1165
1166static void geneve_get_drvinfo(struct net_device *dev,
1167 struct ethtool_drvinfo *drvinfo)
1168{
1169 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1170 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1171}
1172
1173static const struct ethtool_ops geneve_ethtool_ops = {
1174 .get_drvinfo = geneve_get_drvinfo,
1175 .get_link = ethtool_op_get_link,
1176};
1177
1178/* Info for udev, that this is a virtual tunnel endpoint */
1179static struct device_type geneve_type = {
1180 .name = "geneve",
1181};
1182
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001183/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001184 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001185 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001186 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001187static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001188{
1189 struct net *net = dev_net(dev);
1190 struct geneve_net *gn = net_generic(net, geneve_net_id);
1191 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001192
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001193 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001194 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1195 if (push) {
1196 udp_tunnel_push_rx_port(dev, gs->sock,
1197 UDP_TUNNEL_TYPE_GENEVE);
1198 } else {
1199 udp_tunnel_drop_rx_port(dev, gs->sock,
1200 UDP_TUNNEL_TYPE_GENEVE);
1201 }
1202 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001203 rcu_read_unlock();
1204}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001205
John W. Linville2d07dc72015-05-13 12:57:30 -04001206/* Initialize the device structure. */
1207static void geneve_setup(struct net_device *dev)
1208{
1209 ether_setup(dev);
1210
1211 dev->netdev_ops = &geneve_netdev_ops;
1212 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001213 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001214
1215 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1216
John W. Linville2d07dc72015-05-13 12:57:30 -04001217 dev->features |= NETIF_F_LLTX;
Xin Long18423e12021-01-15 17:47:46 +08001218 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
John W. Linville2d07dc72015-05-13 12:57:30 -04001219 dev->features |= NETIF_F_RXCSUM;
1220 dev->features |= NETIF_F_GSO_SOFTWARE;
1221
Xin Long18423e12021-01-15 17:47:46 +08001222 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1223 dev->hw_features |= NETIF_F_RXCSUM;
John W. Linville2d07dc72015-05-13 12:57:30 -04001224 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001225
Jarod Wilson91572082016-10-20 13:55:20 -04001226 /* MTU range: 68 - (something less than 65535) */
1227 dev->min_mtu = ETH_MIN_MTU;
1228 /* The max_mtu calculation does not take account of GENEVE
1229 * options, to avoid excluding potentially valid
1230 * configurations. This will be further reduced by IPvX hdr size.
1231 */
1232 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1233
John W. Linville2d07dc72015-05-13 12:57:30 -04001234 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001235 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001236 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001237 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001238}
1239
1240static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1241 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001242 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001243 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001244 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001245 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001246 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001247 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001248 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001249 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1250 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1251 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001252 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001253 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001254};
1255
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001256static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1257 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001258{
1259 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001260 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1261 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1262 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001263 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001264 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001265
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001266 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1267 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1268 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001269 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001270 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001271 }
1272
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001273 if (!data) {
1274 NL_SET_ERR_MSG(extack,
1275 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001276 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001277 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001278
1279 if (data[IFLA_GENEVE_ID]) {
1280 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1281
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001282 if (vni >= GENEVE_N_VID) {
1283 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1284 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001285 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001286 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001287 }
1288
Stefano Brivioa025fb52018-11-08 12:19:19 +01001289 if (data[IFLA_GENEVE_DF]) {
1290 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1291
1292 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001293 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001294 "Invalid DF attribute");
1295 return -EINVAL;
1296 }
1297 }
1298
John W. Linville2d07dc72015-05-13 12:57:30 -04001299 return 0;
1300}
1301
Pravin B Shelar371bd102015-08-26 23:46:54 -07001302static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001303 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001304 bool *tun_on_same_port,
1305 bool *tun_collect_md)
1306{
pravin shelar9b4437a2016-11-21 11:02:58 -08001307 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001308
1309 *tun_on_same_port = false;
1310 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001311 list_for_each_entry(geneve, &gn->geneve_list, next) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001312 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1313 *tun_collect_md = geneve->cfg.collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001314 *tun_on_same_port = true;
1315 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001316 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1317 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1318 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001319 t = geneve;
1320 }
1321 return t;
1322}
1323
pravin shelar9b4437a2016-11-21 11:02:58 -08001324static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1325{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001326 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1327 info->key.ttl || info->key.label || info->key.tp_src ||
1328 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001329}
1330
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001331static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1332 struct ip_tunnel_info *b)
1333{
1334 if (ip_tunnel_info_af(a) == AF_INET)
1335 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1336 else
1337 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1338}
1339
Pravin B Shelare305ac62015-08-26 23:46:52 -07001340static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001341 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001342 const struct geneve_config *cfg)
John W. Linville2d07dc72015-05-13 12:57:30 -04001343{
1344 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001345 struct geneve_dev *t, *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001346 const struct ip_tunnel_info *info = &cfg->info;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001347 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001348 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001349
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001350 if (cfg->collect_md && !is_tnl_info_zero(info)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001351 NL_SET_ERR_MSG(extack,
1352 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001353 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001354 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001355
1356 geneve->net = net;
1357 geneve->dev = dev;
1358
pravin shelar9b4437a2016-11-21 11:02:58 -08001359 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001360 if (t)
1361 return -EBUSY;
1362
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001363 /* make enough headroom for basic scenario */
1364 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001365 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001366 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001367 dev->max_mtu -= sizeof(struct iphdr);
1368 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001369 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001370 dev->max_mtu -= sizeof(struct ipv6hdr);
1371 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001372 dev->needed_headroom = encap_len + ETH_HLEN;
1373
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001374 if (cfg->collect_md) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001375 if (tun_on_same_port) {
1376 NL_SET_ERR_MSG(extack,
1377 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001378 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001379 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001380 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001381 if (tun_collect_md) {
1382 NL_SET_ERR_MSG(extack,
1383 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001384 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001385 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001386 }
1387
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001388 dst_cache_reset(&geneve->cfg.info.dst_cache);
1389 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
Paolo Abeni468dfff2016-02-12 15:43:58 +01001390
John W. Linville2d07dc72015-05-13 12:57:30 -04001391 err = register_netdevice(dev);
1392 if (err)
1393 return err;
1394
1395 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001396 return 0;
1397}
1398
pravin shelar9b4437a2016-11-21 11:02:58 -08001399static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1400{
1401 memset(info, 0, sizeof(*info));
1402 info->key.tp_dst = htons(dst_port);
1403}
1404
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001405static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1406 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001407 struct geneve_config *cfg, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001408{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001409 struct ip_tunnel_info *info = &cfg->info;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001410 int attrtype;
1411
1412 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1413 NL_SET_ERR_MSG(extack,
1414 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001415 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001416 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001417
1418 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001419 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1420 attrtype = IFLA_GENEVE_REMOTE;
1421 goto change_notsup;
1422 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001423
1424 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001425 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001426
Dave Taht842841e2019-09-02 16:29:36 -07001427 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001428 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1429 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001430 return -EINVAL;
1431 }
1432 }
1433
pravin shelar9b4437a2016-11-21 11:02:58 -08001434 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001435#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001436 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1437 attrtype = IFLA_GENEVE_REMOTE6;
1438 goto change_notsup;
1439 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001440
1441 info->mode = IP_TUNNEL_INFO_IPV6;
1442 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001443 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001444
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001445 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001446 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001447 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1448 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001449 return -EINVAL;
1450 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001451 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001452 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1453 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001454 return -EINVAL;
1455 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001456 info->key.tun_flags |= TUNNEL_CSUM;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001457 cfg->use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001458#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001459 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1460 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001461 return -EPFNOSUPPORT;
1462#endif
1463 }
1464
1465 if (data[IFLA_GENEVE_ID]) {
1466 __u32 vni;
1467 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001468 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001469
1470 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1471 tvni[0] = (vni & 0x00ff0000) >> 16;
1472 tvni[1] = (vni & 0x0000ff00) >> 8;
1473 tvni[2] = vni & 0x000000ff;
1474
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001475 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001476 if (changelink && (tunid != info->key.tun_id)) {
1477 attrtype = IFLA_GENEVE_ID;
1478 goto change_notsup;
1479 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001480 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001481 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001482
Hangbin Liua97d97b2018-09-29 23:06:29 +08001483 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1484 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001485 cfg->ttl_inherit = true;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001486 else
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001487 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001488 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001489 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001490 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001491 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001492
Pravin B Shelare305ac62015-08-26 23:46:52 -07001493 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001494 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001495
Stefano Brivioa025fb52018-11-08 12:19:19 +01001496 if (data[IFLA_GENEVE_DF])
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001497 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001498
pravin shelar9b4437a2016-11-21 11:02:58 -08001499 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001500 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001501 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001502 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1503 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1504 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001505 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001506 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001507 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001508
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001509 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001510 if (changelink) {
1511 attrtype = IFLA_GENEVE_PORT;
1512 goto change_notsup;
1513 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001514 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1515 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001516
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001517 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001518 if (changelink) {
1519 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1520 goto change_notsup;
1521 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001522 cfg->collect_md = true;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001523 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001524
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001525 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001526 if (changelink) {
1527 attrtype = IFLA_GENEVE_UDP_CSUM;
1528 goto change_notsup;
1529 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001530 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1531 info->key.tun_flags |= TUNNEL_CSUM;
1532 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001533
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001534 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001535#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001536 if (changelink) {
1537 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1538 goto change_notsup;
1539 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001540 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1541 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001542#else
1543 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1544 "IPv6 support not enabled in the kernel");
1545 return -EPFNOSUPPORT;
1546#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001547 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001548
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001549 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001550#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001551 if (changelink) {
1552 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1553 goto change_notsup;
1554 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001555 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001556 cfg->use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001557#else
1558 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1559 "IPv6 support not enabled in the kernel");
1560 return -EPFNOSUPPORT;
1561#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001562 }
1563
1564 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001565change_notsup:
1566 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1567 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1568 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001569}
1570
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001571static void geneve_link_config(struct net_device *dev,
1572 struct ip_tunnel_info *info, struct nlattr *tb[])
1573{
1574 struct geneve_dev *geneve = netdev_priv(dev);
1575 int ldev_mtu = 0;
1576
1577 if (tb[IFLA_MTU]) {
1578 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1579 return;
1580 }
1581
1582 switch (ip_tunnel_info_af(info)) {
1583 case AF_INET: {
1584 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1585 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1586
1587 if (!IS_ERR(rt) && rt->dst.dev) {
1588 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1589 ip_rt_put(rt);
1590 }
1591 break;
1592 }
1593#if IS_ENABLED(CONFIG_IPV6)
1594 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001595 struct rt6_info *rt;
1596
1597 if (!__in6_dev_get(dev))
1598 break;
1599
1600 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1601 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001602
1603 if (rt && rt->dst.dev)
1604 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1605 ip6_rt_put(rt);
1606 break;
1607 }
1608#endif
1609 }
1610
1611 if (ldev_mtu <= 0)
1612 return;
1613
1614 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1615}
1616
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001617static int geneve_newlink(struct net *net, struct net_device *dev,
1618 struct nlattr *tb[], struct nlattr *data[],
1619 struct netlink_ext_ack *extack)
1620{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001621 struct geneve_config cfg = {
1622 .df = GENEVE_DF_UNSET,
1623 .use_udp6_rx_checksums = false,
1624 .ttl_inherit = false,
1625 .collect_md = false,
1626 };
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001627 int err;
1628
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001629 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1630 err = geneve_nl2info(tb, data, extack, &cfg, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001631 if (err)
1632 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001633
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001634 err = geneve_configure(net, dev, extack, &cfg);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001635 if (err)
1636 return err;
1637
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001638 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001639
1640 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001641}
1642
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001643/* Quiesces the geneve device data path for both TX and RX.
1644 *
1645 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1646 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1647 * to complete for the existing set of in-flight packets to be transmitted,
1648 * then we would have quiesced the transmit data path. All the future packets
1649 * will get dropped until we unquiesce the data path.
1650 *
1651 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1652 * if we set that to NULL under RCU and wait for synchronize_net() to
1653 * complete, then we would have quiesced the receive data path.
1654 */
1655static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1656 struct geneve_sock **gs6)
1657{
1658 *gs4 = rtnl_dereference(geneve->sock4);
1659 rcu_assign_pointer(geneve->sock4, NULL);
1660 if (*gs4)
1661 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1662#if IS_ENABLED(CONFIG_IPV6)
1663 *gs6 = rtnl_dereference(geneve->sock6);
1664 rcu_assign_pointer(geneve->sock6, NULL);
1665 if (*gs6)
1666 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1667#else
1668 *gs6 = NULL;
1669#endif
1670 synchronize_net();
1671}
1672
1673/* Resumes the geneve device data path for both TX and RX. */
1674static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1675 struct geneve_sock __maybe_unused *gs6)
1676{
1677 rcu_assign_pointer(geneve->sock4, gs4);
1678 if (gs4)
1679 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1680#if IS_ENABLED(CONFIG_IPV6)
1681 rcu_assign_pointer(geneve->sock6, gs6);
1682 if (gs6)
1683 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1684#endif
1685 synchronize_net();
1686}
1687
1688static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1689 struct nlattr *data[],
1690 struct netlink_ext_ack *extack)
1691{
1692 struct geneve_dev *geneve = netdev_priv(dev);
1693 struct geneve_sock *gs4, *gs6;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001694 struct geneve_config cfg;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001695 int err;
1696
1697 /* If the geneve device is configured for metadata (or externally
1698 * controlled, for example, OVS), then nothing can be changed.
1699 */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001700 if (geneve->cfg.collect_md)
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001701 return -EOPNOTSUPP;
1702
1703 /* Start with the existing info. */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001704 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1705 err = geneve_nl2info(tb, data, extack, &cfg, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001706 if (err)
1707 return err;
1708
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001709 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1710 dst_cache_reset(&cfg.info.dst_cache);
1711 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001712 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001713
1714 geneve_quiesce(geneve, &gs4, &gs6);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001715 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001716 geneve_unquiesce(geneve, gs4, gs6);
1717
1718 return 0;
1719}
1720
John W. Linville2d07dc72015-05-13 12:57:30 -04001721static void geneve_dellink(struct net_device *dev, struct list_head *head)
1722{
1723 struct geneve_dev *geneve = netdev_priv(dev);
1724
John W. Linville2d07dc72015-05-13 12:57:30 -04001725 list_del(&geneve->next);
1726 unregister_netdevice_queue(dev, head);
1727}
1728
1729static size_t geneve_get_size(const struct net_device *dev)
1730{
1731 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001732 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001733 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001734 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001735 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001736 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001737 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001738 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001739 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1740 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1741 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001742 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001743 0;
1744}
1745
1746static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1747{
1748 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001749 struct ip_tunnel_info *info = &geneve->cfg.info;
1750 bool ttl_inherit = geneve->cfg.ttl_inherit;
1751 bool metadata = geneve->cfg.collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001752 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001753 __u32 vni;
1754
pravin shelar9b4437a2016-11-21 11:02:58 -08001755 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1756 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001757 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1758 goto nla_put_failure;
1759
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001760 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001761 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001762 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001763 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001764 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1765 !!(info->key.tun_flags & TUNNEL_CSUM)))
1766 goto nla_put_failure;
1767
John W. Linville8ed66f02015-10-26 17:01:44 -04001768#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001769 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001770 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001771 &info->key.u.ipv6.dst))
1772 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001773 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1774 !(info->key.tun_flags & TUNNEL_CSUM)))
1775 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001776#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001777 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001778
pravin shelar9b4437a2016-11-21 11:02:58 -08001779 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1780 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1781 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001782 goto nla_put_failure;
1783
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001784 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
Stefano Brivioa025fb52018-11-08 12:19:19 +01001785 goto nla_put_failure;
1786
pravin shelar9b4437a2016-11-21 11:02:58 -08001787 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001788 goto nla_put_failure;
1789
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001790 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001791 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001792
Hangbin Liuf9094b72017-11-23 11:27:24 +08001793#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001794 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001795 !geneve->cfg.use_udp6_rx_checksums))
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001796 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001797#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001798
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001799 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1800 goto nla_put_failure;
1801
John W. Linville2d07dc72015-05-13 12:57:30 -04001802 return 0;
1803
1804nla_put_failure:
1805 return -EMSGSIZE;
1806}
1807
1808static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1809 .kind = "geneve",
1810 .maxtype = IFLA_GENEVE_MAX,
1811 .policy = geneve_policy,
1812 .priv_size = sizeof(struct geneve_dev),
1813 .setup = geneve_setup,
1814 .validate = geneve_validate,
1815 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001816 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001817 .dellink = geneve_dellink,
1818 .get_size = geneve_get_size,
1819 .fill_info = geneve_fill_info,
1820};
1821
Pravin B Shelare305ac62015-08-26 23:46:52 -07001822struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1823 u8 name_assign_type, u16 dst_port)
1824{
1825 struct nlattr *tb[IFLA_MAX + 1];
1826 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001827 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001828 int err;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001829 struct geneve_config cfg = {
1830 .df = GENEVE_DF_UNSET,
1831 .use_udp6_rx_checksums = true,
1832 .ttl_inherit = false,
1833 .collect_md = true,
1834 };
Pravin B Shelare305ac62015-08-26 23:46:52 -07001835
1836 memset(tb, 0, sizeof(tb));
1837 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001838 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001839 if (IS_ERR(dev))
1840 return dev;
1841
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001842 init_tnl_info(&cfg.info, dst_port);
1843 err = geneve_configure(net, dev, NULL, &cfg);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001844 if (err) {
1845 free_netdev(dev);
1846 return ERR_PTR(err);
1847 }
David Wragg7e059152016-02-10 00:05:58 +00001848
1849 /* openvswitch users expect packet sizes to be unrestricted,
1850 * so set the largest MTU we can.
1851 */
Jarod Wilson91572082016-10-20 13:55:20 -04001852 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001853 if (err)
1854 goto err;
1855
Nicolas Dichtel41009482016-06-13 10:31:07 +02001856 err = rtnl_configure_link(dev, NULL);
1857 if (err < 0)
1858 goto err;
1859
Pravin B Shelare305ac62015-08-26 23:46:52 -07001860 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001861err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001862 geneve_dellink(dev, &list_kill);
1863 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001864 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001865}
1866EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1867
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001868static int geneve_netdevice_event(struct notifier_block *unused,
1869 unsigned long event, void *ptr)
1870{
1871 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1872
Jakub Kicinskidedc33e72021-01-06 13:06:35 -08001873 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1874 geneve_offload_rx_ports(dev, true);
1875 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1876 geneve_offload_rx_ports(dev, false);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001877
1878 return NOTIFY_DONE;
1879}
1880
1881static struct notifier_block geneve_notifier_block __read_mostly = {
1882 .notifier_call = geneve_netdevice_event,
1883};
1884
John W. Linville2d07dc72015-05-13 12:57:30 -04001885static __net_init int geneve_init_net(struct net *net)
1886{
1887 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001888
1889 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001890 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001891 return 0;
1892}
1893
Haishuang Yan2843a252017-12-16 17:54:50 +08001894static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001895{
1896 struct geneve_net *gn = net_generic(net, geneve_net_id);
1897 struct geneve_dev *geneve, *next;
1898 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001899
1900 /* gather any geneve devices that were moved into this ns */
1901 for_each_netdev_safe(net, dev, aux)
1902 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001903 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001904
1905 /* now gather any other geneve devices that were created in this ns */
1906 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1907 /* If geneve->dev is in the same netns, it was already added
1908 * to the list by the previous loop.
1909 */
1910 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001911 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001912 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001913}
1914
1915static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1916{
1917 struct net *net;
1918 LIST_HEAD(list);
1919
1920 rtnl_lock();
1921 list_for_each_entry(net, net_list, exit_list)
1922 geneve_destroy_tunnels(net, &list);
1923
John W. Linville2d07dc72015-05-13 12:57:30 -04001924 /* unregister the devices gathered above */
1925 unregister_netdevice_many(&list);
1926 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001927
1928 list_for_each_entry(net, net_list, exit_list) {
1929 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1930
1931 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1932 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001933}
1934
1935static struct pernet_operations geneve_net_ops = {
1936 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001937 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001938 .id = &geneve_net_id,
1939 .size = sizeof(struct geneve_net),
1940};
1941
1942static int __init geneve_init_module(void)
1943{
1944 int rc;
1945
1946 rc = register_pernet_subsys(&geneve_net_ops);
1947 if (rc)
1948 goto out1;
1949
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001950 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001951 if (rc)
1952 goto out2;
1953
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001954 rc = rtnl_link_register(&geneve_link_ops);
1955 if (rc)
1956 goto out3;
1957
John W. Linville2d07dc72015-05-13 12:57:30 -04001958 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001959out3:
1960 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001961out2:
1962 unregister_pernet_subsys(&geneve_net_ops);
1963out1:
1964 return rc;
1965}
1966late_initcall(geneve_init_module);
1967
1968static void __exit geneve_cleanup_module(void)
1969{
1970 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001971 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001972 unregister_pernet_subsys(&geneve_net_ops);
1973}
1974module_exit(geneve_cleanup_module);
1975
1976MODULE_LICENSE("GPL");
1977MODULE_VERSION(GENEVE_NETDEV_VER);
1978MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1979MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1980MODULE_ALIAS_RTNL_LINK("geneve");