blob: 9caff2e01d19751bbd4a05bf5e204a16dde8a779 [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
548 rcu_read_lock();
549 ptype = gro_find_complete_by_type(type);
550 if (ptype)
551 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
552
553 rcu_read_unlock();
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700554
555 skb_set_inner_mac_header(skb, nhoff + gh_len);
556
Pravin B Shelar371bd102015-08-26 23:46:54 -0700557 return err;
558}
559
560/* Create new listen socket if needed */
561static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
pravin shelar9b4437a2016-11-21 11:02:58 -0800562 bool ipv6, bool ipv6_rx_csum)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700563{
564 struct geneve_net *gn = net_generic(net, geneve_net_id);
565 struct geneve_sock *gs;
566 struct socket *sock;
567 struct udp_tunnel_sock_cfg tunnel_cfg;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700568 int h;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700569
570 gs = kzalloc(sizeof(*gs), GFP_KERNEL);
571 if (!gs)
572 return ERR_PTR(-ENOMEM);
573
pravin shelar9b4437a2016-11-21 11:02:58 -0800574 sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700575 if (IS_ERR(sock)) {
576 kfree(gs);
577 return ERR_CAST(sock);
578 }
579
580 gs->sock = sock;
581 gs->refcnt = 1;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700582 for (h = 0; h < VNI_HASH_SIZE; ++h)
583 INIT_HLIST_HEAD(&gs->vni_list[h]);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700584
585 /* Initialize the geneve udp offloads structure */
Alexander Duycke7b3db52016-06-16 12:20:52 -0700586 udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700587
588 /* Mark socket as an encapsulation socket */
Tom Herbert4a0090a2016-04-05 08:22:55 -0700589 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
Pravin B Shelar371bd102015-08-26 23:46:54 -0700590 tunnel_cfg.sk_user_data = gs;
591 tunnel_cfg.encap_type = 1;
Tom Herbert4a0090a2016-04-05 08:22:55 -0700592 tunnel_cfg.gro_receive = geneve_gro_receive;
593 tunnel_cfg.gro_complete = geneve_gro_complete;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700594 tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
Stefano Brivioa0796642018-11-08 12:19:18 +0100595 tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700596 tunnel_cfg.encap_destroy = NULL;
597 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700598 list_add(&gs->list, &gn->sock_list);
599 return gs;
600}
601
John W. Linville8ed66f02015-10-26 17:01:44 -0400602static void __geneve_sock_release(struct geneve_sock *gs)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700603{
John W. Linville8ed66f02015-10-26 17:01:44 -0400604 if (!gs || --gs->refcnt)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700605 return;
606
607 list_del(&gs->list);
Alexander Duycke7b3db52016-06-16 12:20:52 -0700608 udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700609 udp_tunnel_sock_release(gs->sock);
610 kfree_rcu(gs, rcu);
611}
612
John W. Linville8ed66f02015-10-26 17:01:44 -0400613static void geneve_sock_release(struct geneve_dev *geneve)
614{
pravin shelarfceb9c32016-10-28 09:59:16 -0700615 struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
John W. Linville8ed66f02015-10-26 17:01:44 -0400616#if IS_ENABLED(CONFIG_IPV6)
pravin shelarfceb9c32016-10-28 09:59:16 -0700617 struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
618
619 rcu_assign_pointer(geneve->sock6, NULL);
620#endif
621
622 rcu_assign_pointer(geneve->sock4, NULL);
623 synchronize_net();
624
625 __geneve_sock_release(gs4);
626#if IS_ENABLED(CONFIG_IPV6)
627 __geneve_sock_release(gs6);
John W. Linville8ed66f02015-10-26 17:01:44 -0400628#endif
629}
630
Pravin B Shelar371bd102015-08-26 23:46:54 -0700631static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
John W. Linville8ed66f02015-10-26 17:01:44 -0400632 sa_family_t family,
Pravin B Shelar371bd102015-08-26 23:46:54 -0700633 __be16 dst_port)
634{
635 struct geneve_sock *gs;
636
637 list_for_each_entry(gs, &gn->sock_list, list) {
638 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
Jiri Benc1e9f12e2016-02-18 11:22:49 +0100639 geneve_get_sk_family(gs) == family) {
Pravin B Shelar371bd102015-08-26 23:46:54 -0700640 return gs;
641 }
642 }
643 return NULL;
644}
645
John W. Linville8ed66f02015-10-26 17:01:44 -0400646static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
John W. Linville2d07dc72015-05-13 12:57:30 -0400647{
John W. Linville2d07dc72015-05-13 12:57:30 -0400648 struct net *net = geneve->net;
Pravin B Shelar371bd102015-08-26 23:46:54 -0700649 struct geneve_net *gn = net_generic(net, geneve_net_id);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200650 struct geneve_dev_node *node;
John W. Linville2d07dc72015-05-13 12:57:30 -0400651 struct geneve_sock *gs;
pravin shelar9b4437a2016-11-21 11:02:58 -0800652 __u8 vni[3];
Pravin B Shelar66d47002015-08-26 23:46:55 -0700653 __u32 hash;
John W. Linville2d07dc72015-05-13 12:57:30 -0400654
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200655 gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->cfg.info.key.tp_dst);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700656 if (gs) {
657 gs->refcnt++;
658 goto out;
659 }
660
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200661 gs = geneve_socket_create(net, geneve->cfg.info.key.tp_dst, ipv6,
662 geneve->cfg.use_udp6_rx_checksums);
John W. Linville2d07dc72015-05-13 12:57:30 -0400663 if (IS_ERR(gs))
664 return PTR_ERR(gs);
665
Pravin B Shelar371bd102015-08-26 23:46:54 -0700666out:
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200667 gs->collect_md = geneve->cfg.collect_md;
John W. Linville8ed66f02015-10-26 17:01:44 -0400668#if IS_ENABLED(CONFIG_IPV6)
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200669 if (ipv6) {
pravin shelarfceb9c32016-10-28 09:59:16 -0700670 rcu_assign_pointer(geneve->sock6, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200671 node = &geneve->hlist6;
672 } else
John W. Linville8ed66f02015-10-26 17:01:44 -0400673#endif
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200674 {
pravin shelarfceb9c32016-10-28 09:59:16 -0700675 rcu_assign_pointer(geneve->sock4, gs);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200676 node = &geneve->hlist4;
677 }
678 node->geneve = geneve;
Pravin B Shelar66d47002015-08-26 23:46:55 -0700679
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200680 tunnel_id_to_vni(geneve->cfg.info.key.tun_id, vni);
pravin shelar9b4437a2016-11-21 11:02:58 -0800681 hash = geneve_net_vni_hash(vni);
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200682 hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]);
John W. Linville2d07dc72015-05-13 12:57:30 -0400683 return 0;
684}
685
John W. Linville8ed66f02015-10-26 17:01:44 -0400686static int geneve_open(struct net_device *dev)
687{
688 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200689 bool metadata = geneve->cfg.collect_md;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100690 bool ipv4, ipv6;
John W. Linville8ed66f02015-10-26 17:01:44 -0400691 int ret = 0;
692
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200693 ipv6 = geneve->cfg.info.mode & IP_TUNNEL_INFO_IPV6 || metadata;
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100694 ipv4 = !ipv6 || metadata;
John W. Linville8ed66f02015-10-26 17:01:44 -0400695#if IS_ENABLED(CONFIG_IPV6)
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100696 if (ipv6) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400697 ret = geneve_sock_add(geneve, true);
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100698 if (ret < 0 && ret != -EAFNOSUPPORT)
699 ipv4 = false;
700 }
John W. Linville8ed66f02015-10-26 17:01:44 -0400701#endif
Jiri Benccf1c9cc2019-02-28 14:56:04 +0100702 if (ipv4)
John W. Linville8ed66f02015-10-26 17:01:44 -0400703 ret = geneve_sock_add(geneve, false);
704 if (ret < 0)
705 geneve_sock_release(geneve);
706
707 return ret;
708}
709
John W. Linville2d07dc72015-05-13 12:57:30 -0400710static int geneve_stop(struct net_device *dev)
711{
712 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -0400713
Jiri Benc4b4c21f2017-07-02 19:00:58 +0200714 hlist_del_init_rcu(&geneve->hlist4.hlist);
715#if IS_ENABLED(CONFIG_IPV6)
716 hlist_del_init_rcu(&geneve->hlist6.hlist);
717#endif
John W. Linville8ed66f02015-10-26 17:01:44 -0400718 geneve_sock_release(geneve);
John W. Linville2d07dc72015-05-13 12:57:30 -0400719 return 0;
720}
721
John W. Linville8ed66f02015-10-26 17:01:44 -0400722static void geneve_build_header(struct genevehdr *geneveh,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800723 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400724{
725 geneveh->ver = GENEVE_VER;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800726 geneveh->opt_len = info->options_len / 4;
727 geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM);
728 geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT);
John W. Linville8ed66f02015-10-26 17:01:44 -0400729 geneveh->rsvd1 = 0;
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800730 tunnel_id_to_vni(info->key.tun_id, geneveh->vni);
John W. Linville8ed66f02015-10-26 17:01:44 -0400731 geneveh->proto_type = htons(ETH_P_TEB);
732 geneveh->rsvd2 = 0;
733
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700734 if (info->key.tun_flags & TUNNEL_GENEVE_OPT)
735 ip_tunnel_info_opts_get(geneveh->options, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400736}
737
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800738static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb,
739 const struct ip_tunnel_info *info,
740 bool xnet, int ip_hdr_len)
Pravin B Shelar371bd102015-08-26 23:46:54 -0700741{
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800742 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700743 struct genevehdr *gnvh;
744 int min_headroom;
745 int err;
746
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800747 skb_reset_mac_header(skb);
John W. Linville8ed66f02015-10-26 17:01:44 -0400748 skb_scrub_packet(skb, xnet);
749
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800750 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
751 GENEVE_BASE_HLEN + info->options_len + ip_hdr_len;
John W. Linville8ed66f02015-10-26 17:01:44 -0400752 err = skb_cow_head(skb, min_headroom);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400753 if (unlikely(err))
John W. Linville8ed66f02015-10-26 17:01:44 -0400754 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400755
Alexander Duyckaed069d2016-04-14 15:33:37 -0400756 err = udp_tunnel_handle_offloads(skb, udp_sum);
Dan Carpenter1ba64fa2016-04-19 17:30:56 +0300757 if (err)
John W. Linville8ed66f02015-10-26 17:01:44 -0400758 goto free_dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400759
Johannes Bergd58ff352017-06-16 14:29:23 +0200760 gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800761 geneve_build_header(gnvh, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400762 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
763 return 0;
764
765free_dst:
766 dst_release(dst);
767 return err;
768}
John W. Linville8ed66f02015-10-26 17:01:44 -0400769
770static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
771 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700772 struct geneve_sock *gs4,
John W. Linville8ed66f02015-10-26 17:01:44 -0400773 struct flowi4 *fl4,
Mark Gray34beb212020-09-16 05:19:35 -0400774 const struct ip_tunnel_info *info,
775 __be16 dport, __be16 sport)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700776{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100777 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700778 struct geneve_dev *geneve = netdev_priv(dev);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100779 struct dst_cache *dst_cache;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700780 struct rtable *rt = NULL;
781 __u8 tos;
782
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700783 if (!gs4)
pravin shelarfceb9c32016-10-28 09:59:16 -0700784 return ERR_PTR(-EIO);
785
Pravin B Shelare305ac62015-08-26 23:46:52 -0700786 memset(fl4, 0, sizeof(*fl4));
787 fl4->flowi4_mark = skb->mark;
788 fl4->flowi4_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800789 fl4->daddr = info->key.u.ipv4.dst;
790 fl4->saddr = info->key.u.ipv4.src;
Mark Gray34beb212020-09-16 05:19:35 -0400791 fl4->fl4_dport = dport;
792 fl4->fl4_sport = sport;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700793
pravin shelar9b4437a2016-11-21 11:02:58 -0800794 tos = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200795 if ((tos == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800796 tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
797 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100798 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800799 fl4->flowi4_tos = RT_TOS(tos);
Paolo Abeni468dfff2016-02-12 15:43:58 +0100800
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800801 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100802 if (use_cache) {
803 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
804 if (rt)
805 return rt;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700806 }
Pravin B Shelare305ac62015-08-26 23:46:52 -0700807 rt = ip_route_output_key(geneve->net, fl4);
808 if (IS_ERR(rt)) {
809 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700810 return ERR_PTR(-ENETUNREACH);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700811 }
812 if (rt->dst.dev == dev) { /* is this necessary? */
813 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700814 ip_rt_put(rt);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700815 return ERR_PTR(-ELOOP);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700816 }
Paolo Abeni468dfff2016-02-12 15:43:58 +0100817 if (use_cache)
818 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
Pravin B Shelare305ac62015-08-26 23:46:52 -0700819 return rt;
820}
821
John W. Linville8ed66f02015-10-26 17:01:44 -0400822#if IS_ENABLED(CONFIG_IPV6)
823static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
824 struct net_device *dev,
Girish Moodalbail5b861f62017-07-20 22:44:20 -0700825 struct geneve_sock *gs6,
John W. Linville8ed66f02015-10-26 17:01:44 -0400826 struct flowi6 *fl6,
Mark Gray34beb212020-09-16 05:19:35 -0400827 const struct ip_tunnel_info *info,
828 __be16 dport, __be16 sport)
John W. Linville8ed66f02015-10-26 17:01:44 -0400829{
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100830 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
John W. Linville8ed66f02015-10-26 17:01:44 -0400831 struct geneve_dev *geneve = netdev_priv(dev);
John W. Linville8ed66f02015-10-26 17:01:44 -0400832 struct dst_entry *dst = NULL;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100833 struct dst_cache *dst_cache;
John W. Linville3a56f862015-10-26 17:01:45 -0400834 __u8 prio;
John W. Linville8ed66f02015-10-26 17:01:44 -0400835
pravin shelarfceb9c32016-10-28 09:59:16 -0700836 if (!gs6)
837 return ERR_PTR(-EIO);
838
John W. Linville8ed66f02015-10-26 17:01:44 -0400839 memset(fl6, 0, sizeof(*fl6));
840 fl6->flowi6_mark = skb->mark;
841 fl6->flowi6_proto = IPPROTO_UDP;
pravin shelar9b4437a2016-11-21 11:02:58 -0800842 fl6->daddr = info->key.u.ipv6.dst;
843 fl6->saddr = info->key.u.ipv6.src;
Mark Gray34beb212020-09-16 05:19:35 -0400844 fl6->fl6_dport = dport;
845 fl6->fl6_sport = sport;
846
pravin shelar9b4437a2016-11-21 11:02:58 -0800847 prio = info->key.tos;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200848 if ((prio == 1) && !geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800849 prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
850 use_cache = false;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100851 }
852
pravin shelar9b4437a2016-11-21 11:02:58 -0800853 fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
854 info->key.label);
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800855 dst_cache = (struct dst_cache *)&info->dst_cache;
Paolo Abeni468dfff2016-02-12 15:43:58 +0100856 if (use_cache) {
857 dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
858 if (dst)
859 return dst;
John W. Linville8ed66f02015-10-26 17:01:44 -0400860 }
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100861 dst = ipv6_stub->ipv6_dst_lookup_flow(geneve->net, gs6->sock->sk, fl6,
862 NULL);
863 if (IS_ERR(dst)) {
John W. Linville8ed66f02015-10-26 17:01:44 -0400864 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
865 return ERR_PTR(-ENETUNREACH);
866 }
867 if (dst->dev == dev) { /* is this necessary? */
868 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
869 dst_release(dst);
870 return ERR_PTR(-ELOOP);
871 }
872
Paolo Abeni468dfff2016-02-12 15:43:58 +0100873 if (use_cache)
874 dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
John W. Linville8ed66f02015-10-26 17:01:44 -0400875 return dst;
876}
877#endif
878
pravin shelar9b4437a2016-11-21 11:02:58 -0800879static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800880 struct geneve_dev *geneve,
881 const struct ip_tunnel_info *info)
Pravin B Shelare305ac62015-08-26 23:46:52 -0700882{
pravin shelar9b4437a2016-11-21 11:02:58 -0800883 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
884 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
885 const struct ip_tunnel_key *key = &info->key;
886 struct rtable *rt;
John W. Linville2d07dc72015-05-13 12:57:30 -0400887 struct flowi4 fl4;
John W. Linville8760ce52015-06-01 15:51:34 -0400888 __u8 tos, ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100889 __be16 df = 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700890 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800891 int err;
John W. Linville2d07dc72015-05-13 12:57:30 -0400892
Phillip Potterd13f0482021-04-23 00:49:45 +0100893 if (!pskb_inet_may_pull(skb))
Phillip Potter6628ddf2021-04-11 12:28:24 +0100894 return -EINVAL;
895
Mark Gray34beb212020-09-16 05:19:35 -0400896 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
897 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
898 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800899 if (IS_ERR(rt))
900 return PTR_ERR(rt);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700901
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200902 err = skb_tunnel_check_pmtu(skb, &rt->dst,
903 GENEVE_IPV4_HLEN + info->options_len,
904 netif_is_any_bridge_port(dev));
905 if (err < 0) {
906 dst_release(&rt->dst);
907 return err;
908 } else if (err) {
909 struct ip_tunnel_info *info;
910
911 info = skb_tunnel_info(skb);
912 if (info) {
Antoine Tenart68c1a943e2021-03-25 16:35:33 +0100913 struct ip_tunnel_info *unclone;
914
915 unclone = skb_tunnel_info_unclone(skb);
916 if (unlikely(!unclone)) {
917 dst_release(&rt->dst);
918 return -ENOMEM;
919 }
920
921 unclone->key.u.ipv4.dst = fl4.saddr;
922 unclone->key.u.ipv4.src = fl4.daddr;
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200923 }
924
925 if (!pskb_may_pull(skb, ETH_HLEN)) {
926 dst_release(&rt->dst);
927 return -EINVAL;
928 }
929
930 skb->protocol = eth_type_trans(skb, geneve->dev);
931 netif_rx(skb);
932 dst_release(&rt->dst);
933 return -EMSGSIZE;
934 }
Xin Long52a589d2017-12-25 14:43:58 +0800935
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200936 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -0800937 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
Pravin B Shelar371bd102015-08-26 23:46:54 -0700938 ttl = key->ttl;
Stefano Brivioa025fb52018-11-08 12:19:19 +0100939
940 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -0700941 } else {
pravin shelar9b4437a2016-11-21 11:02:58 -0800942 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200943 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +0800944 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
945 else
946 ttl = key->ttl;
947 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Stefano Brivioa025fb52018-11-08 12:19:19 +0100948
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200949 if (geneve->cfg.df == GENEVE_DF_SET) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100950 df = htons(IP_DF);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200951 } else if (geneve->cfg.df == GENEVE_DF_INHERIT) {
Stefano Brivioa025fb52018-11-08 12:19:19 +0100952 struct ethhdr *eth = eth_hdr(skb);
953
954 if (ntohs(eth->h_proto) == ETH_P_IPV6) {
955 df = htons(IP_DF);
956 } else if (ntohs(eth->h_proto) == ETH_P_IP) {
957 struct iphdr *iph = ip_hdr(skb);
958
959 if (iph->frag_off & htons(IP_DF))
960 df = htons(IP_DF);
961 }
962 }
John W. Linville2d07dc72015-05-13 12:57:30 -0400963 }
pravin shelar9b4437a2016-11-21 11:02:58 -0800964
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800965 err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr));
pravin shelar9b4437a2016-11-21 11:02:58 -0800966 if (unlikely(err))
967 return err;
968
Pravin B Shelar039f5062015-12-24 14:34:54 -0800969 udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +0200970 tos, ttl, df, sport, geneve->cfg.info.key.tp_dst,
Pravin B Shelar039f5062015-12-24 14:34:54 -0800971 !net_eq(geneve->net, dev_net(geneve->dev)),
pravin shelar9b4437a2016-11-21 11:02:58 -0800972 !(info->key.tun_flags & TUNNEL_CSUM));
973 return 0;
John W. Linville2d07dc72015-05-13 12:57:30 -0400974}
975
John W. Linville8ed66f02015-10-26 17:01:44 -0400976#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -0800977static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
pravin shelarc3ef5aa2016-11-21 11:02:59 -0800978 struct geneve_dev *geneve,
979 const struct ip_tunnel_info *info)
John W. Linville8ed66f02015-10-26 17:01:44 -0400980{
pravin shelar9b4437a2016-11-21 11:02:58 -0800981 bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
982 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
983 const struct ip_tunnel_key *key = &info->key;
John W. Linville8ed66f02015-10-26 17:01:44 -0400984 struct dst_entry *dst = NULL;
John W. Linville8ed66f02015-10-26 17:01:44 -0400985 struct flowi6 fl6;
John W. Linville3a56f862015-10-26 17:01:45 -0400986 __u8 prio, ttl;
John W. Linville8ed66f02015-10-26 17:01:44 -0400987 __be16 sport;
pravin shelarbcceeec2016-11-21 11:03:00 -0800988 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -0400989
Phillip Potterd13f0482021-04-23 00:49:45 +0100990 if (!pskb_inet_may_pull(skb))
Phillip Potter6628ddf2021-04-11 12:28:24 +0100991 return -EINVAL;
992
Mark Gray34beb212020-09-16 05:19:35 -0400993 sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
994 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
995 geneve->cfg.info.key.tp_dst, sport);
pravin shelar9b4437a2016-11-21 11:02:58 -0800996 if (IS_ERR(dst))
997 return PTR_ERR(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -0400998
Stefano Brivioc1a800e2020-08-04 07:53:45 +0200999 err = skb_tunnel_check_pmtu(skb, dst,
1000 GENEVE_IPV6_HLEN + info->options_len,
1001 netif_is_any_bridge_port(dev));
1002 if (err < 0) {
1003 dst_release(dst);
1004 return err;
1005 } else if (err) {
1006 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1007
1008 if (info) {
Antoine Tenart68c1a943e2021-03-25 16:35:33 +01001009 struct ip_tunnel_info *unclone;
1010
1011 unclone = skb_tunnel_info_unclone(skb);
1012 if (unlikely(!unclone)) {
1013 dst_release(dst);
1014 return -ENOMEM;
1015 }
1016
1017 unclone->key.u.ipv6.dst = fl6.saddr;
1018 unclone->key.u.ipv6.src = fl6.daddr;
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001019 }
1020
1021 if (!pskb_may_pull(skb, ETH_HLEN)) {
1022 dst_release(dst);
1023 return -EINVAL;
1024 }
1025
1026 skb->protocol = eth_type_trans(skb, geneve->dev);
1027 netif_rx(skb);
1028 dst_release(dst);
1029 return -EMSGSIZE;
1030 }
Xin Long52a589d2017-12-25 14:43:58 +08001031
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001032 if (geneve->cfg.collect_md) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001033 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
John W. Linville8ed66f02015-10-26 17:01:44 -04001034 ttl = key->ttl;
1035 } else {
Daniel Borkmann95caf6f2016-03-18 18:37:58 +01001036 prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
pravin shelar9b4437a2016-11-21 11:02:58 -08001037 ip_hdr(skb), skb);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001038 if (geneve->cfg.ttl_inherit)
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001039 ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb);
1040 else
1041 ttl = key->ttl;
1042 ttl = ttl ? : ip6_dst_hoplimit(dst);
John W. Linville8ed66f02015-10-26 17:01:44 -04001043 }
Haishuang Yan31ac1c12016-11-28 13:26:58 +08001044 err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr));
pravin shelar9b4437a2016-11-21 11:02:58 -08001045 if (unlikely(err))
1046 return err;
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001047
Pravin B Shelar039f5062015-12-24 14:34:54 -08001048 udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
pravin shelar9b4437a2016-11-21 11:02:58 -08001049 &fl6.saddr, &fl6.daddr, prio, ttl,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001050 info->key.label, sport, geneve->cfg.info.key.tp_dst,
pravin shelar9b4437a2016-11-21 11:02:58 -08001051 !(info->key.tun_flags & TUNNEL_CSUM));
1052 return 0;
John W. Linville8ed66f02015-10-26 17:01:44 -04001053}
1054#endif
1055
1056static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
1057{
1058 struct geneve_dev *geneve = netdev_priv(dev);
1059 struct ip_tunnel_info *info = NULL;
pravin shelar9b4437a2016-11-21 11:02:58 -08001060 int err;
John W. Linville8ed66f02015-10-26 17:01:44 -04001061
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001062 if (geneve->cfg.collect_md) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001063 info = skb_tunnel_info(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001064 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001065 netdev_dbg(dev, "no tunnel metadata\n");
Jiri Benc9d149042020-06-03 11:12:14 +02001066 dev_kfree_skb(skb);
1067 dev->stats.tx_dropped++;
1068 return NETDEV_TX_OK;
pravin shelar9b4437a2016-11-21 11:02:58 -08001069 }
1070 } else {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001071 info = &geneve->cfg.info;
pravin shelar9b4437a2016-11-21 11:02:58 -08001072 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001073
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001074 rcu_read_lock();
John W. Linville8ed66f02015-10-26 17:01:44 -04001075#if IS_ENABLED(CONFIG_IPV6)
pravin shelar9b4437a2016-11-21 11:02:58 -08001076 if (info->mode & IP_TUNNEL_INFO_IPV6)
1077 err = geneve6_xmit_skb(skb, dev, geneve, info);
1078 else
John W. Linville8ed66f02015-10-26 17:01:44 -04001079#endif
pravin shelar9b4437a2016-11-21 11:02:58 -08001080 err = geneve_xmit_skb(skb, dev, geneve, info);
Jakub Kicinskia717e3f2017-02-24 11:43:37 -08001081 rcu_read_unlock();
pravin shelar9b4437a2016-11-21 11:02:58 -08001082
1083 if (likely(!err))
1084 return NETDEV_TX_OK;
Jiri Benc9d149042020-06-03 11:12:14 +02001085
Stefano Brivioc1a800e2020-08-04 07:53:45 +02001086 if (err != -EMSGSIZE)
1087 dev_kfree_skb(skb);
pravin shelar9b4437a2016-11-21 11:02:58 -08001088
1089 if (err == -ELOOP)
1090 dev->stats.collisions++;
1091 else if (err == -ENETUNREACH)
1092 dev->stats.tx_carrier_errors++;
1093
1094 dev->stats.tx_errors++;
1095 return NETDEV_TX_OK;
John W. Linville8ed66f02015-10-26 17:01:44 -04001096}
1097
Jarod Wilson91572082016-10-20 13:55:20 -04001098static int geneve_change_mtu(struct net_device *dev, int new_mtu)
David Wragg55e5bfb2016-02-10 00:05:57 +00001099{
Jarod Wilson91572082016-10-20 13:55:20 -04001100 if (new_mtu > dev->max_mtu)
1101 new_mtu = dev->max_mtu;
Alexey Kodanev321acc12018-04-19 15:42:31 +03001102 else if (new_mtu < dev->min_mtu)
1103 new_mtu = dev->min_mtu;
David Wraggaeee0e62016-02-18 17:43:29 +00001104
David Wragg55e5bfb2016-02-10 00:05:57 +00001105 dev->mtu = new_mtu;
1106 return 0;
1107}
1108
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001109static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1110{
1111 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1112 struct geneve_dev *geneve = netdev_priv(dev);
Mark Gray34beb212020-09-16 05:19:35 -04001113 __be16 sport;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001114
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001115 if (ip_tunnel_info_af(info) == AF_INET) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001116 struct rtable *rt;
1117 struct flowi4 fl4;
1118
Mark Gray34beb212020-09-16 05:19:35 -04001119 struct geneve_sock *gs4 = rcu_dereference(geneve->sock4);
1120 sport = udp_flow_src_port(geneve->net, skb,
1121 1, USHRT_MAX, true);
1122
1123 rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info,
1124 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001125 if (IS_ERR(rt))
1126 return PTR_ERR(rt);
1127
1128 ip_rt_put(rt);
1129 info->key.u.ipv4.src = fl4.saddr;
1130#if IS_ENABLED(CONFIG_IPV6)
1131 } else if (ip_tunnel_info_af(info) == AF_INET6) {
pravin shelar9b4437a2016-11-21 11:02:58 -08001132 struct dst_entry *dst;
1133 struct flowi6 fl6;
1134
Mark Gray34beb212020-09-16 05:19:35 -04001135 struct geneve_sock *gs6 = rcu_dereference(geneve->sock6);
1136 sport = udp_flow_src_port(geneve->net, skb,
1137 1, USHRT_MAX, true);
1138
1139 dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info,
1140 geneve->cfg.info.key.tp_dst, sport);
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001141 if (IS_ERR(dst))
1142 return PTR_ERR(dst);
1143
1144 dst_release(dst);
1145 info->key.u.ipv6.src = fl6.saddr;
1146#endif
1147 } else {
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001148 return -EINVAL;
John W. Linvilleb8812fa2015-10-27 09:49:00 -04001149 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001150
Mark Gray34beb212020-09-16 05:19:35 -04001151 info->key.tp_src = sport;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001152 info->key.tp_dst = geneve->cfg.info.key.tp_dst;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001153 return 0;
1154}
1155
John W. Linville2d07dc72015-05-13 12:57:30 -04001156static const struct net_device_ops geneve_netdev_ops = {
1157 .ndo_init = geneve_init,
1158 .ndo_uninit = geneve_uninit,
1159 .ndo_open = geneve_open,
1160 .ndo_stop = geneve_stop,
1161 .ndo_start_xmit = geneve_xmit,
Heiner Kallweitb220a4a2020-11-07 21:52:06 +01001162 .ndo_get_stats64 = dev_get_tstats64,
David Wragg55e5bfb2016-02-10 00:05:57 +00001163 .ndo_change_mtu = geneve_change_mtu,
John W. Linville2d07dc72015-05-13 12:57:30 -04001164 .ndo_validate_addr = eth_validate_addr,
1165 .ndo_set_mac_address = eth_mac_addr,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001166 .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
John W. Linville2d07dc72015-05-13 12:57:30 -04001167};
1168
1169static void geneve_get_drvinfo(struct net_device *dev,
1170 struct ethtool_drvinfo *drvinfo)
1171{
1172 strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1173 strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1174}
1175
1176static const struct ethtool_ops geneve_ethtool_ops = {
1177 .get_drvinfo = geneve_get_drvinfo,
1178 .get_link = ethtool_op_get_link,
1179};
1180
1181/* Info for udev, that this is a virtual tunnel endpoint */
1182static struct device_type geneve_type = {
1183 .name = "geneve",
1184};
1185
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001186/* Calls the ndo_udp_tunnel_add of the caller in order to
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001187 * supply the listening GENEVE udp ports. Callers are expected
Sabrina Dubrocae5de25d2016-07-11 13:12:28 +02001188 * to implement the ndo_udp_tunnel_add.
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001189 */
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001190static void geneve_offload_rx_ports(struct net_device *dev, bool push)
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001191{
1192 struct net *net = dev_net(dev);
1193 struct geneve_net *gn = net_generic(net, geneve_net_id);
1194 struct geneve_sock *gs;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001195
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001196 rcu_read_lock();
Sabrina Dubroca2d2b13f2017-07-21 12:49:32 +02001197 list_for_each_entry_rcu(gs, &gn->sock_list, list) {
1198 if (push) {
1199 udp_tunnel_push_rx_port(dev, gs->sock,
1200 UDP_TUNNEL_TYPE_GENEVE);
1201 } else {
1202 udp_tunnel_drop_rx_port(dev, gs->sock,
1203 UDP_TUNNEL_TYPE_GENEVE);
1204 }
1205 }
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001206 rcu_read_unlock();
1207}
Singhai, Anjali05ca4022015-12-14 12:21:20 -08001208
John W. Linville2d07dc72015-05-13 12:57:30 -04001209/* Initialize the device structure. */
1210static void geneve_setup(struct net_device *dev)
1211{
1212 ether_setup(dev);
1213
1214 dev->netdev_ops = &geneve_netdev_ops;
1215 dev->ethtool_ops = &geneve_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001216 dev->needs_free_netdev = true;
John W. Linville2d07dc72015-05-13 12:57:30 -04001217
1218 SET_NETDEV_DEVTYPE(dev, &geneve_type);
1219
John W. Linville2d07dc72015-05-13 12:57:30 -04001220 dev->features |= NETIF_F_LLTX;
Xin Long18423e12021-01-15 17:47:46 +08001221 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
John W. Linville2d07dc72015-05-13 12:57:30 -04001222 dev->features |= NETIF_F_RXCSUM;
1223 dev->features |= NETIF_F_GSO_SOFTWARE;
1224
Xin Long18423e12021-01-15 17:47:46 +08001225 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
1226 dev->hw_features |= NETIF_F_RXCSUM;
John W. Linville2d07dc72015-05-13 12:57:30 -04001227 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
John W. Linville2d07dc72015-05-13 12:57:30 -04001228
Jarod Wilson91572082016-10-20 13:55:20 -04001229 /* MTU range: 68 - (something less than 65535) */
1230 dev->min_mtu = ETH_MIN_MTU;
1231 /* The max_mtu calculation does not take account of GENEVE
1232 * options, to avoid excluding potentially valid
1233 * configurations. This will be further reduced by IPvX hdr size.
1234 */
1235 dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
1236
John W. Linville2d07dc72015-05-13 12:57:30 -04001237 netif_keep_dst(dev);
Jiri Bencfc41cdb2016-02-17 15:31:35 +01001238 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Phil Suttered961ac2015-08-18 10:30:31 +02001239 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
Pravin B Shelar87cd3dc2015-08-26 23:46:48 -07001240 eth_hw_addr_random(dev);
John W. Linville2d07dc72015-05-13 12:57:30 -04001241}
1242
1243static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1244 [IFLA_GENEVE_ID] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001245 [IFLA_GENEVE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
John W. Linville8ed66f02015-10-26 17:01:44 -04001246 [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
John W. Linville8760ce52015-06-01 15:51:34 -04001247 [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
John W. Linvilled8951122015-06-01 15:51:35 -04001248 [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001249 [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001250 [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
Pravin B Shelare305ac62015-08-26 23:46:52 -07001251 [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
Tom Herbertabe492b2015-12-10 12:37:45 -08001252 [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
1253 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
1254 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001255 [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 },
Stefano Brivioa025fb52018-11-08 12:19:19 +01001256 [IFLA_GENEVE_DF] = { .type = NLA_U8 },
John W. Linville2d07dc72015-05-13 12:57:30 -04001257};
1258
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001259static int geneve_validate(struct nlattr *tb[], struct nlattr *data[],
1260 struct netlink_ext_ack *extack)
John W. Linville2d07dc72015-05-13 12:57:30 -04001261{
1262 if (tb[IFLA_ADDRESS]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001263 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1264 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1265 "Provided link layer address is not Ethernet");
John W. Linville2d07dc72015-05-13 12:57:30 -04001266 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001267 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001268
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001269 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1270 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
1271 "Provided Ethernet address is not unicast");
John W. Linville2d07dc72015-05-13 12:57:30 -04001272 return -EADDRNOTAVAIL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001273 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001274 }
1275
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001276 if (!data) {
1277 NL_SET_ERR_MSG(extack,
1278 "Not enough attributes provided to perform the operation");
John W. Linville2d07dc72015-05-13 12:57:30 -04001279 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001280 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001281
1282 if (data[IFLA_GENEVE_ID]) {
1283 __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1284
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001285 if (vni >= GENEVE_N_VID) {
1286 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID],
1287 "Geneve ID must be lower than 16777216");
John W. Linville2d07dc72015-05-13 12:57:30 -04001288 return -ERANGE;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001289 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001290 }
1291
Stefano Brivioa025fb52018-11-08 12:19:19 +01001292 if (data[IFLA_GENEVE_DF]) {
1293 enum ifla_geneve_df df = nla_get_u8(data[IFLA_GENEVE_DF]);
1294
1295 if (df < 0 || df > GENEVE_DF_MAX) {
Sabrina Dubroca9a7b5b52020-04-22 17:29:51 +02001296 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_DF],
Stefano Brivioa025fb52018-11-08 12:19:19 +01001297 "Invalid DF attribute");
1298 return -EINVAL;
1299 }
1300 }
1301
John W. Linville2d07dc72015-05-13 12:57:30 -04001302 return 0;
1303}
1304
Pravin B Shelar371bd102015-08-26 23:46:54 -07001305static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
pravin shelar9b4437a2016-11-21 11:02:58 -08001306 const struct ip_tunnel_info *info,
Pravin B Shelar371bd102015-08-26 23:46:54 -07001307 bool *tun_on_same_port,
1308 bool *tun_collect_md)
1309{
pravin shelar9b4437a2016-11-21 11:02:58 -08001310 struct geneve_dev *geneve, *t = NULL;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001311
1312 *tun_on_same_port = false;
1313 *tun_collect_md = false;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001314 list_for_each_entry(geneve, &gn->geneve_list, next) {
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001315 if (info->key.tp_dst == geneve->cfg.info.key.tp_dst) {
1316 *tun_collect_md = geneve->cfg.collect_md;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001317 *tun_on_same_port = true;
1318 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001319 if (info->key.tun_id == geneve->cfg.info.key.tun_id &&
1320 info->key.tp_dst == geneve->cfg.info.key.tp_dst &&
1321 !memcmp(&info->key.u, &geneve->cfg.info.key.u, sizeof(info->key.u)))
Pravin B Shelar371bd102015-08-26 23:46:54 -07001322 t = geneve;
1323 }
1324 return t;
1325}
1326
pravin shelar9b4437a2016-11-21 11:02:58 -08001327static bool is_tnl_info_zero(const struct ip_tunnel_info *info)
1328{
Stefano Brivio3fa5f112017-10-20 13:31:36 +02001329 return !(info->key.tun_id || info->key.tun_flags || info->key.tos ||
1330 info->key.ttl || info->key.label || info->key.tp_src ||
1331 memchr_inv(&info->key.u, 0, sizeof(info->key.u)));
pravin shelar9b4437a2016-11-21 11:02:58 -08001332}
1333
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001334static bool geneve_dst_addr_equal(struct ip_tunnel_info *a,
1335 struct ip_tunnel_info *b)
1336{
1337 if (ip_tunnel_info_af(a) == AF_INET)
1338 return a->key.u.ipv4.dst == b->key.u.ipv4.dst;
1339 else
1340 return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst);
1341}
1342
Pravin B Shelare305ac62015-08-26 23:46:52 -07001343static int geneve_configure(struct net *net, struct net_device *dev,
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001344 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001345 const struct geneve_config *cfg)
John W. Linville2d07dc72015-05-13 12:57:30 -04001346{
1347 struct geneve_net *gn = net_generic(net, geneve_net_id);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001348 struct geneve_dev *t, *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001349 const struct ip_tunnel_info *info = &cfg->info;
Pravin B Shelar371bd102015-08-26 23:46:54 -07001350 bool tun_collect_md, tun_on_same_port;
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001351 int err, encap_len;
John W. Linville2d07dc72015-05-13 12:57:30 -04001352
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001353 if (cfg->collect_md && !is_tnl_info_zero(info)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001354 NL_SET_ERR_MSG(extack,
1355 "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified");
John W. Linville8ed66f02015-10-26 17:01:44 -04001356 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001357 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001358
1359 geneve->net = net;
1360 geneve->dev = dev;
1361
pravin shelar9b4437a2016-11-21 11:02:58 -08001362 t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001363 if (t)
1364 return -EBUSY;
1365
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001366 /* make enough headroom for basic scenario */
1367 encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001368 if (!cfg->collect_md && ip_tunnel_info_af(info) == AF_INET) {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001369 encap_len += sizeof(struct iphdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001370 dev->max_mtu -= sizeof(struct iphdr);
1371 } else {
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001372 encap_len += sizeof(struct ipv6hdr);
Jarod Wilson91572082016-10-20 13:55:20 -04001373 dev->max_mtu -= sizeof(struct ipv6hdr);
1374 }
Paolo Abeni184fc8b2015-12-23 16:54:27 +01001375 dev->needed_headroom = encap_len + ETH_HLEN;
1376
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001377 if (cfg->collect_md) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001378 if (tun_on_same_port) {
1379 NL_SET_ERR_MSG(extack,
1380 "There can be only one externally controlled device on a destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001381 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001382 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001383 } else {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001384 if (tun_collect_md) {
1385 NL_SET_ERR_MSG(extack,
1386 "There already exists an externally controlled device on this destination port");
Pravin B Shelar371bd102015-08-26 23:46:54 -07001387 return -EPERM;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001388 }
Pravin B Shelar371bd102015-08-26 23:46:54 -07001389 }
1390
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001391 dst_cache_reset(&geneve->cfg.info.dst_cache);
1392 memcpy(&geneve->cfg, cfg, sizeof(*cfg));
Paolo Abeni468dfff2016-02-12 15:43:58 +01001393
John W. Linville2d07dc72015-05-13 12:57:30 -04001394 err = register_netdevice(dev);
1395 if (err)
1396 return err;
1397
1398 list_add(&geneve->next, &gn->geneve_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001399 return 0;
1400}
1401
pravin shelar9b4437a2016-11-21 11:02:58 -08001402static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port)
1403{
1404 memset(info, 0, sizeof(*info));
1405 info->key.tp_dst = htons(dst_port);
1406}
1407
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001408static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[],
1409 struct netlink_ext_ack *extack,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001410 struct geneve_config *cfg, bool changelink)
Pravin B Shelare305ac62015-08-26 23:46:52 -07001411{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001412 struct ip_tunnel_info *info = &cfg->info;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001413 int attrtype;
1414
1415 if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) {
1416 NL_SET_ERR_MSG(extack,
1417 "Cannot specify both IPv4 and IPv6 Remote addresses");
John W. Linville8ed66f02015-10-26 17:01:44 -04001418 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001419 }
John W. Linville8ed66f02015-10-26 17:01:44 -04001420
1421 if (data[IFLA_GENEVE_REMOTE]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001422 if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) {
1423 attrtype = IFLA_GENEVE_REMOTE;
1424 goto change_notsup;
1425 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001426
1427 info->key.u.ipv4.dst =
John W. Linville8ed66f02015-10-26 17:01:44 -04001428 nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
John W. Linville8ed66f02015-10-26 17:01:44 -04001429
Dave Taht842841e2019-09-02 16:29:36 -07001430 if (ipv4_is_multicast(info->key.u.ipv4.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001431 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE],
1432 "Remote IPv4 address cannot be Multicast");
John W. Linville8ed66f02015-10-26 17:01:44 -04001433 return -EINVAL;
1434 }
1435 }
1436
pravin shelar9b4437a2016-11-21 11:02:58 -08001437 if (data[IFLA_GENEVE_REMOTE6]) {
Alexey Kodanev4c52a882018-04-19 15:42:29 +03001438#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001439 if (changelink && (ip_tunnel_info_af(info) == AF_INET)) {
1440 attrtype = IFLA_GENEVE_REMOTE6;
1441 goto change_notsup;
1442 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001443
1444 info->mode = IP_TUNNEL_INFO_IPV6;
1445 info->key.u.ipv6.dst =
pravin shelar9b4437a2016-11-21 11:02:58 -08001446 nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001447
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001448 if (ipv6_addr_type(&info->key.u.ipv6.dst) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001449 IPV6_ADDR_LINKLOCAL) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001450 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1451 "Remote IPv6 address cannot be link-local");
pravin shelar9b4437a2016-11-21 11:02:58 -08001452 return -EINVAL;
1453 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001454 if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001455 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1456 "Remote IPv6 address cannot be Multicast");
pravin shelar9b4437a2016-11-21 11:02:58 -08001457 return -EINVAL;
1458 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001459 info->key.tun_flags |= TUNNEL_CSUM;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001460 cfg->use_udp6_rx_checksums = true;
pravin shelar9b4437a2016-11-21 11:02:58 -08001461#else
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001462 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6],
1463 "IPv6 support not enabled in the kernel");
pravin shelar9b4437a2016-11-21 11:02:58 -08001464 return -EPFNOSUPPORT;
1465#endif
1466 }
1467
1468 if (data[IFLA_GENEVE_ID]) {
1469 __u32 vni;
1470 __u8 tvni[3];
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001471 __be64 tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001472
1473 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1474 tvni[0] = (vni & 0x00ff0000) >> 16;
1475 tvni[1] = (vni & 0x0000ff00) >> 8;
1476 tvni[2] = vni & 0x000000ff;
1477
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001478 tunid = vni_to_tunnel_id(tvni);
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001479 if (changelink && (tunid != info->key.tun_id)) {
1480 attrtype = IFLA_GENEVE_ID;
1481 goto change_notsup;
1482 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001483 info->key.tun_id = tunid;
pravin shelar9b4437a2016-11-21 11:02:58 -08001484 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001485
Hangbin Liua97d97b2018-09-29 23:06:29 +08001486 if (data[IFLA_GENEVE_TTL_INHERIT]) {
1487 if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001488 cfg->ttl_inherit = true;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001489 else
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001490 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001491 } else if (data[IFLA_GENEVE_TTL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001492 info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001493 cfg->ttl_inherit = false;
Hangbin Liua97d97b2018-09-29 23:06:29 +08001494 }
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001495
Pravin B Shelare305ac62015-08-26 23:46:52 -07001496 if (data[IFLA_GENEVE_TOS])
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001497 info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001498
Stefano Brivioa025fb52018-11-08 12:19:19 +01001499 if (data[IFLA_GENEVE_DF])
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001500 cfg->df = nla_get_u8(data[IFLA_GENEVE_DF]);
Stefano Brivioa025fb52018-11-08 12:19:19 +01001501
pravin shelar9b4437a2016-11-21 11:02:58 -08001502 if (data[IFLA_GENEVE_LABEL]) {
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001503 info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
pravin shelar9b4437a2016-11-21 11:02:58 -08001504 IPV6_FLOWLABEL_MASK;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001505 if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) {
1506 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL],
1507 "Label attribute only applies for IPv6 Geneve devices");
pravin shelar9b4437a2016-11-21 11:02:58 -08001508 return -EINVAL;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001509 }
pravin shelar9b4437a2016-11-21 11:02:58 -08001510 }
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001511
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001512 if (data[IFLA_GENEVE_PORT]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001513 if (changelink) {
1514 attrtype = IFLA_GENEVE_PORT;
1515 goto change_notsup;
1516 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001517 info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]);
1518 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001519
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001520 if (data[IFLA_GENEVE_COLLECT_METADATA]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001521 if (changelink) {
1522 attrtype = IFLA_GENEVE_COLLECT_METADATA;
1523 goto change_notsup;
1524 }
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001525 cfg->collect_md = true;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001526 }
Pravin B Shelare305ac62015-08-26 23:46:52 -07001527
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001528 if (data[IFLA_GENEVE_UDP_CSUM]) {
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001529 if (changelink) {
1530 attrtype = IFLA_GENEVE_UDP_CSUM;
1531 goto change_notsup;
1532 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001533 if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
1534 info->key.tun_flags |= TUNNEL_CSUM;
1535 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001536
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001537 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001538#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001539 if (changelink) {
1540 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX;
1541 goto change_notsup;
1542 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001543 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
1544 info->key.tun_flags &= ~TUNNEL_CSUM;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001545#else
1546 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX],
1547 "IPv6 support not enabled in the kernel");
1548 return -EPFNOSUPPORT;
1549#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001550 }
Tom Herbertabe492b2015-12-10 12:37:45 -08001551
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001552 if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
Hangbin Liuf9094b72017-11-23 11:27:24 +08001553#if IS_ENABLED(CONFIG_IPV6)
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001554 if (changelink) {
1555 attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX;
1556 goto change_notsup;
1557 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001558 if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001559 cfg->use_udp6_rx_checksums = false;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001560#else
1561 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX],
1562 "IPv6 support not enabled in the kernel");
1563 return -EPFNOSUPPORT;
1564#endif
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001565 }
1566
1567 return 0;
Girish Moodalbailc5ebc442017-08-09 01:09:28 -07001568change_notsup:
1569 NL_SET_ERR_MSG_ATTR(extack, data[attrtype],
1570 "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported");
1571 return -EOPNOTSUPP;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001572}
1573
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001574static void geneve_link_config(struct net_device *dev,
1575 struct ip_tunnel_info *info, struct nlattr *tb[])
1576{
1577 struct geneve_dev *geneve = netdev_priv(dev);
1578 int ldev_mtu = 0;
1579
1580 if (tb[IFLA_MTU]) {
1581 geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1582 return;
1583 }
1584
1585 switch (ip_tunnel_info_af(info)) {
1586 case AF_INET: {
1587 struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst };
1588 struct rtable *rt = ip_route_output_key(geneve->net, &fl4);
1589
1590 if (!IS_ERR(rt) && rt->dst.dev) {
1591 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN;
1592 ip_rt_put(rt);
1593 }
1594 break;
1595 }
1596#if IS_ENABLED(CONFIG_IPV6)
1597 case AF_INET6: {
Hangbin Liuc0a47e42019-02-07 18:36:10 +08001598 struct rt6_info *rt;
1599
1600 if (!__in6_dev_get(dev))
1601 break;
1602
1603 rt = rt6_lookup(geneve->net, &info->key.u.ipv6.dst, NULL, 0,
1604 NULL, 0);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001605
1606 if (rt && rt->dst.dev)
1607 ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN;
1608 ip6_rt_put(rt);
1609 break;
1610 }
1611#endif
1612 }
1613
1614 if (ldev_mtu <= 0)
1615 return;
1616
1617 geneve_change_mtu(dev, ldev_mtu - info->options_len);
1618}
1619
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001620static int geneve_newlink(struct net *net, struct net_device *dev,
1621 struct nlattr *tb[], struct nlattr *data[],
1622 struct netlink_ext_ack *extack)
1623{
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001624 struct geneve_config cfg = {
1625 .df = GENEVE_DF_UNSET,
1626 .use_udp6_rx_checksums = false,
1627 .ttl_inherit = false,
1628 .collect_md = false,
1629 };
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001630 int err;
1631
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001632 init_tnl_info(&cfg.info, GENEVE_UDP_PORT);
1633 err = geneve_nl2info(tb, data, extack, &cfg, false);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001634 if (err)
1635 return err;
Tom Herbertabe492b2015-12-10 12:37:45 -08001636
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001637 err = geneve_configure(net, dev, extack, &cfg);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001638 if (err)
1639 return err;
1640
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001641 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001642
1643 return 0;
Pravin B Shelare305ac62015-08-26 23:46:52 -07001644}
1645
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001646/* Quiesces the geneve device data path for both TX and RX.
1647 *
1648 * On transmit geneve checks for non-NULL geneve_sock before it proceeds.
1649 * So, if we set that socket to NULL under RCU and wait for synchronize_net()
1650 * to complete for the existing set of in-flight packets to be transmitted,
1651 * then we would have quiesced the transmit data path. All the future packets
1652 * will get dropped until we unquiesce the data path.
1653 *
1654 * On receive geneve dereference the geneve_sock stashed in the socket. So,
1655 * if we set that to NULL under RCU and wait for synchronize_net() to
1656 * complete, then we would have quiesced the receive data path.
1657 */
1658static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4,
1659 struct geneve_sock **gs6)
1660{
1661 *gs4 = rtnl_dereference(geneve->sock4);
1662 rcu_assign_pointer(geneve->sock4, NULL);
1663 if (*gs4)
1664 rcu_assign_sk_user_data((*gs4)->sock->sk, NULL);
1665#if IS_ENABLED(CONFIG_IPV6)
1666 *gs6 = rtnl_dereference(geneve->sock6);
1667 rcu_assign_pointer(geneve->sock6, NULL);
1668 if (*gs6)
1669 rcu_assign_sk_user_data((*gs6)->sock->sk, NULL);
1670#else
1671 *gs6 = NULL;
1672#endif
1673 synchronize_net();
1674}
1675
1676/* Resumes the geneve device data path for both TX and RX. */
1677static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
1678 struct geneve_sock __maybe_unused *gs6)
1679{
1680 rcu_assign_pointer(geneve->sock4, gs4);
1681 if (gs4)
1682 rcu_assign_sk_user_data(gs4->sock->sk, gs4);
1683#if IS_ENABLED(CONFIG_IPV6)
1684 rcu_assign_pointer(geneve->sock6, gs6);
1685 if (gs6)
1686 rcu_assign_sk_user_data(gs6->sock->sk, gs6);
1687#endif
1688 synchronize_net();
1689}
1690
1691static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
1692 struct nlattr *data[],
1693 struct netlink_ext_ack *extack)
1694{
1695 struct geneve_dev *geneve = netdev_priv(dev);
1696 struct geneve_sock *gs4, *gs6;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001697 struct geneve_config cfg;
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001698 int err;
1699
1700 /* If the geneve device is configured for metadata (or externally
1701 * controlled, for example, OVS), then nothing can be changed.
1702 */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001703 if (geneve->cfg.collect_md)
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001704 return -EOPNOTSUPP;
1705
1706 /* Start with the existing info. */
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001707 memcpy(&cfg, &geneve->cfg, sizeof(cfg));
1708 err = geneve_nl2info(tb, data, extack, &cfg, true);
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001709 if (err)
1710 return err;
1711
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001712 if (!geneve_dst_addr_equal(&geneve->cfg.info, &cfg.info)) {
1713 dst_cache_reset(&cfg.info.dst_cache);
1714 geneve_link_config(dev, &cfg.info, tb);
Alexey Kodanevc40e89f2018-04-19 15:42:32 +03001715 }
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001716
1717 geneve_quiesce(geneve, &gs4, &gs6);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001718 memcpy(&geneve->cfg, &cfg, sizeof(cfg));
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001719 geneve_unquiesce(geneve, gs4, gs6);
1720
1721 return 0;
1722}
1723
John W. Linville2d07dc72015-05-13 12:57:30 -04001724static void geneve_dellink(struct net_device *dev, struct list_head *head)
1725{
1726 struct geneve_dev *geneve = netdev_priv(dev);
1727
John W. Linville2d07dc72015-05-13 12:57:30 -04001728 list_del(&geneve->next);
1729 unregister_netdevice_queue(dev, head);
1730}
1731
1732static size_t geneve_get_size(const struct net_device *dev)
1733{
1734 return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
John W. Linville8ed66f02015-10-26 17:01:44 -04001735 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
John W. Linville8760ce52015-06-01 15:51:34 -04001736 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
John W. Linvilled8951122015-06-01 15:51:35 -04001737 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
Stefano Brivioa025fb52018-11-08 12:19:19 +01001738 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_DF */
Daniel Borkmann8eb3b992016-03-09 03:00:04 +01001739 nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
John W. Linville7bbe33f2015-09-22 13:09:32 -04001740 nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
Pravin B Shelare305ac62015-08-26 23:46:52 -07001741 nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
Tom Herbertabe492b2015-12-10 12:37:45 -08001742 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
1743 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
1744 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001745 nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */
John W. Linville2d07dc72015-05-13 12:57:30 -04001746 0;
1747}
1748
1749static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1750{
1751 struct geneve_dev *geneve = netdev_priv(dev);
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001752 struct ip_tunnel_info *info = &geneve->cfg.info;
1753 bool ttl_inherit = geneve->cfg.ttl_inherit;
1754 bool metadata = geneve->cfg.collect_md;
pravin shelar9b4437a2016-11-21 11:02:58 -08001755 __u8 tmp_vni[3];
John W. Linville2d07dc72015-05-13 12:57:30 -04001756 __u32 vni;
1757
pravin shelar9b4437a2016-11-21 11:02:58 -08001758 tunnel_id_to_vni(info->key.tun_id, tmp_vni);
1759 vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2];
John W. Linville2d07dc72015-05-13 12:57:30 -04001760 if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1761 goto nla_put_failure;
1762
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001763 if (!metadata && ip_tunnel_info_af(info) == AF_INET) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001764 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
pravin shelar9b4437a2016-11-21 11:02:58 -08001765 info->key.u.ipv4.dst))
John W. Linville8ed66f02015-10-26 17:01:44 -04001766 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001767 if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
1768 !!(info->key.tun_flags & TUNNEL_CSUM)))
1769 goto nla_put_failure;
1770
John W. Linville8ed66f02015-10-26 17:01:44 -04001771#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001772 } else if (!metadata) {
John W. Linville8ed66f02015-10-26 17:01:44 -04001773 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
pravin shelar9b4437a2016-11-21 11:02:58 -08001774 &info->key.u.ipv6.dst))
1775 goto nla_put_failure;
pravin shelar9b4437a2016-11-21 11:02:58 -08001776 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
1777 !(info->key.tun_flags & TUNNEL_CSUM)))
1778 goto nla_put_failure;
Eric Garver11387fe2017-05-23 18:37:27 -04001779#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001780 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001781
pravin shelar9b4437a2016-11-21 11:02:58 -08001782 if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) ||
1783 nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) ||
1784 nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label))
John W. Linville8760ce52015-06-01 15:51:34 -04001785 goto nla_put_failure;
1786
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001787 if (nla_put_u8(skb, IFLA_GENEVE_DF, geneve->cfg.df))
Stefano Brivioa025fb52018-11-08 12:19:19 +01001788 goto nla_put_failure;
1789
pravin shelar9b4437a2016-11-21 11:02:58 -08001790 if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst))
Pravin B Shelarcd7918b2015-08-26 23:46:51 -07001791 goto nla_put_failure;
1792
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001793 if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
Hangbin Liuf9094b72017-11-23 11:27:24 +08001794 goto nla_put_failure;
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001795
Hangbin Liuf9094b72017-11-23 11:27:24 +08001796#if IS_ENABLED(CONFIG_IPV6)
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001797 if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001798 !geneve->cfg.use_udp6_rx_checksums))
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001799 goto nla_put_failure;
Hangbin Liuf9094b72017-11-23 11:27:24 +08001800#endif
Hangbin Liufd7eafd2017-11-15 09:43:09 +08001801
Hangbin Liu52d0d4042018-09-12 10:04:21 +08001802 if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit))
1803 goto nla_put_failure;
1804
John W. Linville2d07dc72015-05-13 12:57:30 -04001805 return 0;
1806
1807nla_put_failure:
1808 return -EMSGSIZE;
1809}
1810
1811static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1812 .kind = "geneve",
1813 .maxtype = IFLA_GENEVE_MAX,
1814 .policy = geneve_policy,
1815 .priv_size = sizeof(struct geneve_dev),
1816 .setup = geneve_setup,
1817 .validate = geneve_validate,
1818 .newlink = geneve_newlink,
Girish Moodalbail5b861f62017-07-20 22:44:20 -07001819 .changelink = geneve_changelink,
John W. Linville2d07dc72015-05-13 12:57:30 -04001820 .dellink = geneve_dellink,
1821 .get_size = geneve_get_size,
1822 .fill_info = geneve_fill_info,
1823};
1824
Pravin B Shelare305ac62015-08-26 23:46:52 -07001825struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1826 u8 name_assign_type, u16 dst_port)
1827{
1828 struct nlattr *tb[IFLA_MAX + 1];
1829 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001830 LIST_HEAD(list_kill);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001831 int err;
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001832 struct geneve_config cfg = {
1833 .df = GENEVE_DF_UNSET,
1834 .use_udp6_rx_checksums = true,
1835 .ttl_inherit = false,
1836 .collect_md = true,
1837 };
Pravin B Shelare305ac62015-08-26 23:46:52 -07001838
1839 memset(tb, 0, sizeof(tb));
1840 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001841 &geneve_link_ops, tb, NULL);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001842 if (IS_ERR(dev))
1843 return dev;
1844
Sabrina Dubroca9e06e852020-07-06 17:18:08 +02001845 init_tnl_info(&cfg.info, dst_port);
1846 err = geneve_configure(net, dev, NULL, &cfg);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001847 if (err) {
1848 free_netdev(dev);
1849 return ERR_PTR(err);
1850 }
David Wragg7e059152016-02-10 00:05:58 +00001851
1852 /* openvswitch users expect packet sizes to be unrestricted,
1853 * so set the largest MTU we can.
1854 */
Jarod Wilson91572082016-10-20 13:55:20 -04001855 err = geneve_change_mtu(dev, IP_MAX_MTU);
David Wragg7e059152016-02-10 00:05:58 +00001856 if (err)
1857 goto err;
1858
Nicolas Dichtel41009482016-06-13 10:31:07 +02001859 err = rtnl_configure_link(dev, NULL);
1860 if (err < 0)
1861 goto err;
1862
Pravin B Shelare305ac62015-08-26 23:46:52 -07001863 return dev;
pravin shelar9b4437a2016-11-21 11:02:58 -08001864err:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001865 geneve_dellink(dev, &list_kill);
1866 unregister_netdevice_many(&list_kill);
David Wragg7e059152016-02-10 00:05:58 +00001867 return ERR_PTR(err);
Pravin B Shelare305ac62015-08-26 23:46:52 -07001868}
1869EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1870
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001871static int geneve_netdevice_event(struct notifier_block *unused,
1872 unsigned long event, void *ptr)
1873{
1874 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1875
Jakub Kicinskidedc33e72021-01-06 13:06:35 -08001876 if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
1877 geneve_offload_rx_ports(dev, true);
1878 else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
1879 geneve_offload_rx_ports(dev, false);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001880
1881 return NOTIFY_DONE;
1882}
1883
1884static struct notifier_block geneve_notifier_block __read_mostly = {
1885 .notifier_call = geneve_netdevice_event,
1886};
1887
John W. Linville2d07dc72015-05-13 12:57:30 -04001888static __net_init int geneve_init_net(struct net *net)
1889{
1890 struct geneve_net *gn = net_generic(net, geneve_net_id);
John W. Linville2d07dc72015-05-13 12:57:30 -04001891
1892 INIT_LIST_HEAD(&gn->geneve_list);
Pravin B Shelar371bd102015-08-26 23:46:54 -07001893 INIT_LIST_HEAD(&gn->sock_list);
John W. Linville2d07dc72015-05-13 12:57:30 -04001894 return 0;
1895}
1896
Haishuang Yan2843a252017-12-16 17:54:50 +08001897static void geneve_destroy_tunnels(struct net *net, struct list_head *head)
John W. Linville2d07dc72015-05-13 12:57:30 -04001898{
1899 struct geneve_net *gn = net_generic(net, geneve_net_id);
1900 struct geneve_dev *geneve, *next;
1901 struct net_device *dev, *aux;
John W. Linville2d07dc72015-05-13 12:57:30 -04001902
1903 /* gather any geneve devices that were moved into this ns */
1904 for_each_netdev_safe(net, dev, aux)
1905 if (dev->rtnl_link_ops == &geneve_link_ops)
Haishuang Yan2843a252017-12-16 17:54:50 +08001906 unregister_netdevice_queue(dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001907
1908 /* now gather any other geneve devices that were created in this ns */
1909 list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1910 /* If geneve->dev is in the same netns, it was already added
1911 * to the list by the previous loop.
1912 */
1913 if (!net_eq(dev_net(geneve->dev), net))
Haishuang Yan2843a252017-12-16 17:54:50 +08001914 unregister_netdevice_queue(geneve->dev, head);
John W. Linville2d07dc72015-05-13 12:57:30 -04001915 }
Haishuang Yan2843a252017-12-16 17:54:50 +08001916}
1917
1918static void __net_exit geneve_exit_batch_net(struct list_head *net_list)
1919{
1920 struct net *net;
1921 LIST_HEAD(list);
1922
1923 rtnl_lock();
1924 list_for_each_entry(net, net_list, exit_list)
1925 geneve_destroy_tunnels(net, &list);
1926
John W. Linville2d07dc72015-05-13 12:57:30 -04001927 /* unregister the devices gathered above */
1928 unregister_netdevice_many(&list);
1929 rtnl_unlock();
Florian Westphal0fda7602020-03-14 08:18:42 +01001930
1931 list_for_each_entry(net, net_list, exit_list) {
1932 const struct geneve_net *gn = net_generic(net, geneve_net_id);
1933
1934 WARN_ON_ONCE(!list_empty(&gn->sock_list));
1935 }
John W. Linville2d07dc72015-05-13 12:57:30 -04001936}
1937
1938static struct pernet_operations geneve_net_ops = {
1939 .init = geneve_init_net,
Haishuang Yan2843a252017-12-16 17:54:50 +08001940 .exit_batch = geneve_exit_batch_net,
John W. Linville2d07dc72015-05-13 12:57:30 -04001941 .id = &geneve_net_id,
1942 .size = sizeof(struct geneve_net),
1943};
1944
1945static int __init geneve_init_module(void)
1946{
1947 int rc;
1948
1949 rc = register_pernet_subsys(&geneve_net_ops);
1950 if (rc)
1951 goto out1;
1952
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001953 rc = register_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001954 if (rc)
1955 goto out2;
1956
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001957 rc = rtnl_link_register(&geneve_link_ops);
1958 if (rc)
1959 goto out3;
1960
John W. Linville2d07dc72015-05-13 12:57:30 -04001961 return 0;
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001962out3:
1963 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001964out2:
1965 unregister_pernet_subsys(&geneve_net_ops);
1966out1:
1967 return rc;
1968}
1969late_initcall(geneve_init_module);
1970
1971static void __exit geneve_cleanup_module(void)
1972{
1973 rtnl_link_unregister(&geneve_link_ops);
Hannes Frederic Sowa681e6832016-04-18 21:19:48 +02001974 unregister_netdevice_notifier(&geneve_notifier_block);
John W. Linville2d07dc72015-05-13 12:57:30 -04001975 unregister_pernet_subsys(&geneve_net_ops);
1976}
1977module_exit(geneve_cleanup_module);
1978
1979MODULE_LICENSE("GPL");
1980MODULE_VERSION(GENEVE_NETDEV_VER);
1981MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1982MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1983MODULE_ALIAS_RTNL_LINK("geneve");