John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * GENEVE: Generic Network Virtualization Encapsulation |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 15 | #include <linux/etherdevice.h> |
| 16 | #include <linux/hash.h> |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 17 | #include <net/dst_metadata.h> |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 18 | #include <net/gro_cells.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 19 | #include <net/rtnetlink.h> |
| 20 | #include <net/geneve.h> |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 21 | #include <net/protocol.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 22 | |
| 23 | #define GENEVE_NETDEV_VER "0.6" |
| 24 | |
| 25 | #define GENEVE_UDP_PORT 6081 |
| 26 | |
| 27 | #define GENEVE_N_VID (1u << 24) |
| 28 | #define GENEVE_VID_MASK (GENEVE_N_VID - 1) |
| 29 | |
| 30 | #define VNI_HASH_BITS 10 |
| 31 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 32 | |
| 33 | static bool log_ecn_error = true; |
| 34 | module_param(log_ecn_error, bool, 0644); |
| 35 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 36 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 37 | #define GENEVE_VER 0 |
| 38 | #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) |
Alexey Kodanev | 5edbea6 | 2018-04-19 15:42:30 +0300 | [diff] [blame] | 39 | #define GENEVE_IPV4_HLEN (ETH_HLEN + sizeof(struct iphdr) + GENEVE_BASE_HLEN) |
| 40 | #define GENEVE_IPV6_HLEN (ETH_HLEN + sizeof(struct ipv6hdr) + GENEVE_BASE_HLEN) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 41 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 42 | /* per-network namespace private data for this module */ |
| 43 | struct geneve_net { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 44 | struct list_head geneve_list; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 45 | struct list_head sock_list; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 48 | static unsigned int geneve_net_id; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 49 | |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 50 | struct geneve_dev_node { |
| 51 | struct hlist_node hlist; |
| 52 | struct geneve_dev *geneve; |
| 53 | }; |
| 54 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 55 | /* Pseudo network device */ |
| 56 | struct geneve_dev { |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 57 | struct geneve_dev_node hlist4; /* vni hash table for IPv4 socket */ |
| 58 | #if IS_ENABLED(CONFIG_IPV6) |
| 59 | struct geneve_dev_node hlist6; /* vni hash table for IPv6 socket */ |
| 60 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 61 | struct net *net; /* netns for packet i/o */ |
| 62 | struct net_device *dev; /* netdev for geneve tunnel */ |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 63 | struct ip_tunnel_info info; |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 64 | struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 65 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 66 | struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 67 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 68 | struct list_head next; /* geneve's per namespace list */ |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 69 | struct gro_cells gro_cells; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 70 | bool collect_md; |
| 71 | bool use_udp6_rx_checksums; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 72 | bool ttl_inherit; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 73 | }; |
| 74 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 75 | struct geneve_sock { |
| 76 | bool collect_md; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 77 | struct list_head list; |
| 78 | struct socket *sock; |
| 79 | struct rcu_head rcu; |
| 80 | int refcnt; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 81 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 82 | }; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 83 | |
| 84 | static inline __u32 geneve_net_vni_hash(u8 vni[3]) |
| 85 | { |
| 86 | __u32 vnid; |
| 87 | |
| 88 | vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 89 | return hash_32(vnid, VNI_HASH_BITS); |
| 90 | } |
| 91 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 92 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
| 93 | { |
| 94 | #ifdef __BIG_ENDIAN |
| 95 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 96 | #else |
| 97 | return (__force __be64)(((__force u64)vni[0] << 40) | |
| 98 | ((__force u64)vni[1] << 48) | |
| 99 | ((__force u64)vni[2] << 56)); |
| 100 | #endif |
| 101 | } |
| 102 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 103 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
| 104 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) |
| 105 | { |
| 106 | #ifdef __BIG_ENDIAN |
| 107 | vni[0] = (__force __u8)(tun_id >> 16); |
| 108 | vni[1] = (__force __u8)(tun_id >> 8); |
| 109 | vni[2] = (__force __u8)tun_id; |
| 110 | #else |
| 111 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); |
| 112 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); |
| 113 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); |
| 114 | #endif |
| 115 | } |
| 116 | |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 117 | static bool eq_tun_id_and_vni(u8 *tun_id, u8 *vni) |
| 118 | { |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 119 | return !memcmp(vni, &tun_id[5], 3); |
pravin shelar | 2e0b26e | 2016-11-21 11:03:01 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 122 | static sa_family_t geneve_get_sk_family(struct geneve_sock *gs) |
| 123 | { |
| 124 | return gs->sock->sk->sk_family; |
| 125 | } |
| 126 | |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 127 | static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 128 | __be32 addr, u8 vni[]) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 129 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 130 | struct hlist_head *vni_list_head; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 131 | struct geneve_dev_node *node; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 132 | __u32 hash; |
| 133 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 134 | /* Find the device for this VNI */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 135 | hash = geneve_net_vni_hash(vni); |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 136 | vni_list_head = &gs->vni_list[hash]; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 137 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
| 138 | if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) && |
| 139 | addr == node->geneve->info.key.u.ipv4.dst) |
| 140 | return node->geneve; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 141 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
| 144 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 145 | #if IS_ENABLED(CONFIG_IPV6) |
| 146 | static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, |
| 147 | struct in6_addr addr6, u8 vni[]) |
| 148 | { |
| 149 | struct hlist_head *vni_list_head; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 150 | struct geneve_dev_node *node; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 151 | __u32 hash; |
| 152 | |
| 153 | /* Find the device for this VNI */ |
| 154 | hash = geneve_net_vni_hash(vni); |
| 155 | vni_list_head = &gs->vni_list[hash]; |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 156 | hlist_for_each_entry_rcu(node, vni_list_head, hlist) { |
| 157 | if (eq_tun_id_and_vni((u8 *)&node->geneve->info.key.tun_id, vni) && |
| 158 | ipv6_addr_equal(&addr6, &node->geneve->info.key.u.ipv6.dst)) |
| 159 | return node->geneve; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 160 | } |
| 161 | return NULL; |
| 162 | } |
| 163 | #endif |
| 164 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 165 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
| 166 | { |
| 167 | return (struct genevehdr *)(udp_hdr(skb) + 1); |
| 168 | } |
| 169 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 170 | static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs, |
| 171 | struct sk_buff *skb) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 172 | { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 173 | static u8 zero_vni[3]; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 174 | u8 *vni; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 175 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 176 | if (geneve_get_sk_family(gs) == AF_INET) { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 177 | struct iphdr *iph; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 178 | __be32 addr; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 179 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 180 | iph = ip_hdr(skb); /* outer IP header... */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 181 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 182 | if (gs->collect_md) { |
| 183 | vni = zero_vni; |
| 184 | addr = 0; |
| 185 | } else { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 186 | vni = geneve_hdr(skb)->vni; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 187 | addr = iph->saddr; |
| 188 | } |
| 189 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 190 | return geneve_lookup(gs, addr, vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 191 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 192 | } else if (geneve_get_sk_family(gs) == AF_INET6) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 193 | static struct in6_addr zero_addr6; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 194 | struct ipv6hdr *ip6h; |
| 195 | struct in6_addr addr6; |
| 196 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 197 | ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ |
| 198 | |
| 199 | if (gs->collect_md) { |
| 200 | vni = zero_vni; |
| 201 | addr6 = zero_addr6; |
| 202 | } else { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 203 | vni = geneve_hdr(skb)->vni; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 204 | addr6 = ip6h->saddr; |
| 205 | } |
| 206 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 207 | return geneve6_lookup(gs, addr6, vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 208 | #endif |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 209 | } |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | /* geneve receive/decap routine */ |
| 214 | static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, |
| 215 | struct sk_buff *skb) |
| 216 | { |
| 217 | struct genevehdr *gnvh = geneve_hdr(skb); |
| 218 | struct metadata_dst *tun_dst = NULL; |
| 219 | struct pcpu_sw_netstats *stats; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 220 | unsigned int len; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 221 | int err = 0; |
| 222 | void *oiph; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 223 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 224 | if (ip_tunnel_collect_metadata() || gs->collect_md) { |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 225 | __be16 flags; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 226 | |
| 227 | flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | |
| 228 | (gnvh->oam ? TUNNEL_OAM : 0) | |
| 229 | (gnvh->critical ? TUNNEL_CRIT_OPT : 0); |
| 230 | |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 231 | tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 232 | vni_to_tunnel_id(gnvh->vni), |
| 233 | gnvh->opt_len * 4); |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 234 | if (!tun_dst) { |
| 235 | geneve->dev->stats.rx_dropped++; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 236 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 237 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 238 | /* Update tunnel dst according to Geneve options. */ |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 239 | ip_tunnel_info_opts_set(&tun_dst->u.tun_info, |
Pieter Jansen van Vuuren | 256c87c | 2018-06-26 21:39:36 -0700 | [diff] [blame] | 240 | gnvh->options, gnvh->opt_len * 4, |
| 241 | TUNNEL_GENEVE_OPT); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 242 | } else { |
| 243 | /* Drop packets w/ critical options, |
| 244 | * since we don't support any... |
| 245 | */ |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 246 | if (gnvh->critical) { |
| 247 | geneve->dev->stats.rx_frame_errors++; |
| 248 | geneve->dev->stats.rx_errors++; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 249 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 250 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 251 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 252 | |
| 253 | skb_reset_mac_header(skb); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 254 | skb->protocol = eth_type_trans(skb, geneve->dev); |
| 255 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 256 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 257 | if (tun_dst) |
| 258 | skb_dst_set(skb, &tun_dst->dst); |
| 259 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 260 | /* Ignore packet loops (and multicast echo) */ |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 261 | if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) { |
| 262 | geneve->dev->stats.rx_errors++; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 263 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 264 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 265 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 266 | oiph = skb_network_header(skb); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 267 | skb_reset_network_header(skb); |
| 268 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 269 | if (geneve_get_sk_family(gs) == AF_INET) |
| 270 | err = IP_ECN_decapsulate(oiph, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 271 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 272 | else |
| 273 | err = IP6_ECN_decapsulate(oiph, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 274 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 275 | |
| 276 | if (unlikely(err)) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 277 | if (log_ecn_error) { |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 278 | if (geneve_get_sk_family(gs) == AF_INET) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 279 | net_info_ratelimited("non-ECT from %pI4 " |
| 280 | "with TOS=%#x\n", |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 281 | &((struct iphdr *)oiph)->saddr, |
| 282 | ((struct iphdr *)oiph)->tos); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 283 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 284 | else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 285 | net_info_ratelimited("non-ECT from %pI6\n", |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 286 | &((struct ipv6hdr *)oiph)->saddr); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 287 | #endif |
| 288 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 289 | if (err > 1) { |
| 290 | ++geneve->dev->stats.rx_frame_errors; |
| 291 | ++geneve->dev->stats.rx_errors; |
| 292 | goto drop; |
| 293 | } |
| 294 | } |
| 295 | |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 296 | len = skb->len; |
| 297 | err = gro_cells_receive(&geneve->gro_cells, skb); |
| 298 | if (likely(err == NET_RX_SUCCESS)) { |
| 299 | stats = this_cpu_ptr(geneve->dev->tstats); |
| 300 | u64_stats_update_begin(&stats->syncp); |
| 301 | stats->rx_packets++; |
| 302 | stats->rx_bytes += len; |
| 303 | u64_stats_update_end(&stats->syncp); |
| 304 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 305 | return; |
| 306 | drop: |
| 307 | /* Consume bad packet */ |
| 308 | kfree_skb(skb); |
| 309 | } |
| 310 | |
| 311 | /* Setup stats when device is created */ |
| 312 | static int geneve_init(struct net_device *dev) |
| 313 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 314 | struct geneve_dev *geneve = netdev_priv(dev); |
| 315 | int err; |
| 316 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 317 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 318 | if (!dev->tstats) |
| 319 | return -ENOMEM; |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 320 | |
| 321 | err = gro_cells_init(&geneve->gro_cells, dev); |
| 322 | if (err) { |
| 323 | free_percpu(dev->tstats); |
| 324 | return err; |
| 325 | } |
| 326 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 327 | err = dst_cache_init(&geneve->info.dst_cache, GFP_KERNEL); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 328 | if (err) { |
| 329 | free_percpu(dev->tstats); |
| 330 | gro_cells_destroy(&geneve->gro_cells); |
| 331 | return err; |
| 332 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static void geneve_uninit(struct net_device *dev) |
| 337 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 338 | struct geneve_dev *geneve = netdev_priv(dev); |
| 339 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 340 | dst_cache_destroy(&geneve->info.dst_cache); |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 341 | gro_cells_destroy(&geneve->gro_cells); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 342 | free_percpu(dev->tstats); |
| 343 | } |
| 344 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 345 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 346 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 347 | { |
| 348 | struct genevehdr *geneveh; |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 349 | struct geneve_dev *geneve; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 350 | struct geneve_sock *gs; |
| 351 | int opts_len; |
| 352 | |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 353 | /* Need UDP and Geneve header to be present */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 354 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 355 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 356 | |
| 357 | /* Return packets with reserved bits set */ |
| 358 | geneveh = geneve_hdr(skb); |
| 359 | if (unlikely(geneveh->ver != GENEVE_VER)) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 360 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 361 | |
| 362 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 363 | goto drop; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 364 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 365 | gs = rcu_dereference_sk_user_data(sk); |
| 366 | if (!gs) |
| 367 | goto drop; |
| 368 | |
| 369 | geneve = geneve_lookup_skb(gs, skb); |
| 370 | if (!geneve) |
| 371 | goto drop; |
| 372 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 373 | opts_len = geneveh->opt_len * 4; |
| 374 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, |
Jiri Benc | 7f290c9 | 2016-02-18 11:22:52 +0100 | [diff] [blame] | 375 | htons(ETH_P_TEB), |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 376 | !net_eq(geneve->net, dev_net(geneve->dev)))) { |
| 377 | geneve->dev->stats.rx_dropped++; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 378 | goto drop; |
Girish Moodalbail | fe741e2 | 2017-06-08 17:07:48 -0700 | [diff] [blame] | 379 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 380 | |
Jiri Benc | 9fc4754 | 2016-02-18 11:22:50 +0100 | [diff] [blame] | 381 | geneve_rx(geneve, gs, skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | |
| 384 | drop: |
| 385 | /* Consume bad packet */ |
| 386 | kfree_skb(skb); |
| 387 | return 0; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame^] | 390 | /* Callback from net/ipv{4,6}/udp.c to check that we have a tunnel for errors */ |
| 391 | static int geneve_udp_encap_err_lookup(struct sock *sk, struct sk_buff *skb) |
| 392 | { |
| 393 | struct genevehdr *geneveh; |
| 394 | struct geneve_sock *gs; |
| 395 | u8 zero_vni[3] = { 0 }; |
| 396 | u8 *vni = zero_vni; |
| 397 | |
| 398 | if (skb->len < GENEVE_BASE_HLEN) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | geneveh = geneve_hdr(skb); |
| 402 | if (geneveh->ver != GENEVE_VER) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | if (geneveh->proto_type != htons(ETH_P_TEB)) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | gs = rcu_dereference_sk_user_data(sk); |
| 409 | if (!gs) |
| 410 | return -ENOENT; |
| 411 | |
| 412 | if (geneve_get_sk_family(gs) == AF_INET) { |
| 413 | struct iphdr *iph = ip_hdr(skb); |
| 414 | __be32 addr4 = 0; |
| 415 | |
| 416 | if (!gs->collect_md) { |
| 417 | vni = geneve_hdr(skb)->vni; |
| 418 | addr4 = iph->daddr; |
| 419 | } |
| 420 | |
| 421 | return geneve_lookup(gs, addr4, vni) ? 0 : -ENOENT; |
| 422 | } |
| 423 | |
| 424 | #if IS_ENABLED(CONFIG_IPV6) |
| 425 | if (geneve_get_sk_family(gs) == AF_INET6) { |
| 426 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 427 | struct in6_addr addr6 = { 0 }; |
| 428 | |
| 429 | if (!gs->collect_md) { |
| 430 | vni = geneve_hdr(skb)->vni; |
| 431 | addr6 = ip6h->daddr; |
| 432 | } |
| 433 | |
| 434 | return geneve6_lookup(gs, addr6, vni) ? 0 : -ENOENT; |
| 435 | } |
| 436 | #endif |
| 437 | |
| 438 | return -EPFNOSUPPORT; |
| 439 | } |
| 440 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 441 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 442 | __be16 port, bool ipv6_rx_csum) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 443 | { |
| 444 | struct socket *sock; |
| 445 | struct udp_port_cfg udp_conf; |
| 446 | int err; |
| 447 | |
| 448 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 449 | |
| 450 | if (ipv6) { |
| 451 | udp_conf.family = AF_INET6; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 452 | udp_conf.ipv6_v6only = 1; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 453 | udp_conf.use_udp6_rx_checksums = ipv6_rx_csum; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 454 | } else { |
| 455 | udp_conf.family = AF_INET; |
| 456 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 457 | } |
| 458 | |
| 459 | udp_conf.local_udp_port = port; |
| 460 | |
| 461 | /* Open UDP socket */ |
| 462 | err = udp_sock_create(net, &udp_conf, &sock); |
| 463 | if (err < 0) |
| 464 | return ERR_PTR(err); |
| 465 | |
| 466 | return sock; |
| 467 | } |
| 468 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 469 | static int geneve_hlen(struct genevehdr *gh) |
| 470 | { |
| 471 | return sizeof(*gh) + gh->opt_len * 4; |
| 472 | } |
| 473 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 474 | static struct sk_buff *geneve_gro_receive(struct sock *sk, |
| 475 | struct list_head *head, |
| 476 | struct sk_buff *skb) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 477 | { |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 478 | struct sk_buff *pp = NULL; |
| 479 | struct sk_buff *p; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 480 | 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 Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 506 | list_for_each_entry(p, head, list) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 507 | 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 | |
| 520 | rcu_read_lock(); |
| 521 | ptype = gro_find_receive_by_type(type); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 522 | if (!ptype) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 523 | goto out_unlock; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 524 | |
| 525 | skb_gro_pull(skb, gh_len); |
| 526 | skb_gro_postpull_rcsum(skb, gh, gh_len); |
Sabrina Dubroca | fcd91dd | 2016-10-20 15:58:02 +0200 | [diff] [blame] | 527 | pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb); |
Alexander Duyck | c194cf9 | 2016-03-09 09:24:23 -0800 | [diff] [blame] | 528 | flush = 0; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 529 | |
| 530 | out_unlock: |
| 531 | rcu_read_unlock(); |
| 532 | out: |
Sabrina Dubroca | 603d4cf | 2018-06-30 17:38:55 +0200 | [diff] [blame] | 533 | skb_gro_flush_final(skb, pp, flush); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 534 | |
| 535 | return pp; |
| 536 | } |
| 537 | |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 538 | static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb, |
| 539 | int nhoff) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 540 | { |
| 541 | struct genevehdr *gh; |
| 542 | struct packet_offload *ptype; |
| 543 | __be16 type; |
| 544 | int gh_len; |
| 545 | int err = -ENOSYS; |
| 546 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 547 | gh = (struct genevehdr *)(skb->data + nhoff); |
| 548 | gh_len = geneve_hlen(gh); |
| 549 | type = gh->proto_type; |
| 550 | |
| 551 | rcu_read_lock(); |
| 552 | ptype = gro_find_complete_by_type(type); |
| 553 | if (ptype) |
| 554 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); |
| 555 | |
| 556 | rcu_read_unlock(); |
Jarno Rajahalme | 229740c | 2016-05-03 16:10:21 -0700 | [diff] [blame] | 557 | |
| 558 | skb_set_inner_mac_header(skb, nhoff + gh_len); |
| 559 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 560 | return err; |
| 561 | } |
| 562 | |
| 563 | /* Create new listen socket if needed */ |
| 564 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 565 | bool ipv6, bool ipv6_rx_csum) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 566 | { |
| 567 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 568 | struct geneve_sock *gs; |
| 569 | struct socket *sock; |
| 570 | struct udp_tunnel_sock_cfg tunnel_cfg; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 571 | int h; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 572 | |
| 573 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); |
| 574 | if (!gs) |
| 575 | return ERR_PTR(-ENOMEM); |
| 576 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 577 | sock = geneve_create_sock(net, ipv6, port, ipv6_rx_csum); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 578 | if (IS_ERR(sock)) { |
| 579 | kfree(gs); |
| 580 | return ERR_CAST(sock); |
| 581 | } |
| 582 | |
| 583 | gs->sock = sock; |
| 584 | gs->refcnt = 1; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 585 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 586 | INIT_HLIST_HEAD(&gs->vni_list[h]); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 587 | |
| 588 | /* Initialize the geneve udp offloads structure */ |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 589 | udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 590 | |
| 591 | /* Mark socket as an encapsulation socket */ |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 592 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 593 | tunnel_cfg.sk_user_data = gs; |
| 594 | tunnel_cfg.encap_type = 1; |
Tom Herbert | 4a0090a | 2016-04-05 08:22:55 -0700 | [diff] [blame] | 595 | tunnel_cfg.gro_receive = geneve_gro_receive; |
| 596 | tunnel_cfg.gro_complete = geneve_gro_complete; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 597 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
Stefano Brivio | a079664 | 2018-11-08 12:19:18 +0100 | [diff] [blame^] | 598 | tunnel_cfg.encap_err_lookup = geneve_udp_encap_err_lookup; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 599 | tunnel_cfg.encap_destroy = NULL; |
| 600 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 601 | list_add(&gs->list, &gn->sock_list); |
| 602 | return gs; |
| 603 | } |
| 604 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 605 | static void __geneve_sock_release(struct geneve_sock *gs) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 606 | { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 607 | if (!gs || --gs->refcnt) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 608 | return; |
| 609 | |
| 610 | list_del(&gs->list); |
Alexander Duyck | e7b3db5 | 2016-06-16 12:20:52 -0700 | [diff] [blame] | 611 | udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 612 | udp_tunnel_sock_release(gs->sock); |
| 613 | kfree_rcu(gs, rcu); |
| 614 | } |
| 615 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 616 | static void geneve_sock_release(struct geneve_dev *geneve) |
| 617 | { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 618 | struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 619 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 620 | struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6); |
| 621 | |
| 622 | rcu_assign_pointer(geneve->sock6, NULL); |
| 623 | #endif |
| 624 | |
| 625 | rcu_assign_pointer(geneve->sock4, NULL); |
| 626 | synchronize_net(); |
| 627 | |
| 628 | __geneve_sock_release(gs4); |
| 629 | #if IS_ENABLED(CONFIG_IPV6) |
| 630 | __geneve_sock_release(gs6); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 631 | #endif |
| 632 | } |
| 633 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 634 | static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 635 | sa_family_t family, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 636 | __be16 dst_port) |
| 637 | { |
| 638 | struct geneve_sock *gs; |
| 639 | |
| 640 | list_for_each_entry(gs, &gn->sock_list, list) { |
| 641 | if (inet_sk(gs->sock->sk)->inet_sport == dst_port && |
Jiri Benc | 1e9f12e | 2016-02-18 11:22:49 +0100 | [diff] [blame] | 642 | geneve_get_sk_family(gs) == family) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 643 | return gs; |
| 644 | } |
| 645 | } |
| 646 | return NULL; |
| 647 | } |
| 648 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 649 | static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 650 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 651 | struct net *net = geneve->net; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 652 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 653 | struct geneve_dev_node *node; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 654 | struct geneve_sock *gs; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 655 | __u8 vni[3]; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 656 | __u32 hash; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 657 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 658 | gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->info.key.tp_dst); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 659 | if (gs) { |
| 660 | gs->refcnt++; |
| 661 | goto out; |
| 662 | } |
| 663 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 664 | gs = geneve_socket_create(net, geneve->info.key.tp_dst, ipv6, |
| 665 | geneve->use_udp6_rx_checksums); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 666 | if (IS_ERR(gs)) |
| 667 | return PTR_ERR(gs); |
| 668 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 669 | out: |
| 670 | gs->collect_md = geneve->collect_md; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 671 | #if IS_ENABLED(CONFIG_IPV6) |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 672 | if (ipv6) { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 673 | rcu_assign_pointer(geneve->sock6, gs); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 674 | node = &geneve->hlist6; |
| 675 | } else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 676 | #endif |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 677 | { |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 678 | rcu_assign_pointer(geneve->sock4, gs); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 679 | node = &geneve->hlist4; |
| 680 | } |
| 681 | node->geneve = geneve; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 682 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 683 | tunnel_id_to_vni(geneve->info.key.tun_id, vni); |
| 684 | hash = geneve_net_vni_hash(vni); |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 685 | hlist_add_head_rcu(&node->hlist, &gs->vni_list[hash]); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 689 | static int geneve_open(struct net_device *dev) |
| 690 | { |
| 691 | struct geneve_dev *geneve = netdev_priv(dev); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 692 | bool ipv6 = !!(geneve->info.mode & IP_TUNNEL_INFO_IPV6); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 693 | bool metadata = geneve->collect_md; |
| 694 | int ret = 0; |
| 695 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 696 | #if IS_ENABLED(CONFIG_IPV6) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 697 | if (ipv6 || metadata) |
| 698 | ret = geneve_sock_add(geneve, true); |
| 699 | #endif |
| 700 | if (!ret && (!ipv6 || metadata)) |
| 701 | ret = geneve_sock_add(geneve, false); |
| 702 | if (ret < 0) |
| 703 | geneve_sock_release(geneve); |
| 704 | |
| 705 | return ret; |
| 706 | } |
| 707 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 708 | static int geneve_stop(struct net_device *dev) |
| 709 | { |
| 710 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 711 | |
Jiri Benc | 4b4c21f | 2017-07-02 19:00:58 +0200 | [diff] [blame] | 712 | hlist_del_init_rcu(&geneve->hlist4.hlist); |
| 713 | #if IS_ENABLED(CONFIG_IPV6) |
| 714 | hlist_del_init_rcu(&geneve->hlist6.hlist); |
| 715 | #endif |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 716 | geneve_sock_release(geneve); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 720 | static void geneve_build_header(struct genevehdr *geneveh, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 721 | const struct ip_tunnel_info *info) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 722 | { |
| 723 | geneveh->ver = GENEVE_VER; |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 724 | geneveh->opt_len = info->options_len / 4; |
| 725 | geneveh->oam = !!(info->key.tun_flags & TUNNEL_OAM); |
| 726 | geneveh->critical = !!(info->key.tun_flags & TUNNEL_CRIT_OPT); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 727 | geneveh->rsvd1 = 0; |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 728 | tunnel_id_to_vni(info->key.tun_id, geneveh->vni); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 729 | geneveh->proto_type = htons(ETH_P_TEB); |
| 730 | geneveh->rsvd2 = 0; |
| 731 | |
Pieter Jansen van Vuuren | 256c87c | 2018-06-26 21:39:36 -0700 | [diff] [blame] | 732 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) |
| 733 | ip_tunnel_info_opts_get(geneveh->options, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 734 | } |
| 735 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 736 | static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, |
| 737 | const struct ip_tunnel_info *info, |
| 738 | bool xnet, int ip_hdr_len) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 739 | { |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 740 | bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 741 | struct genevehdr *gnvh; |
| 742 | int min_headroom; |
| 743 | int err; |
| 744 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 745 | skb_reset_mac_header(skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 746 | skb_scrub_packet(skb, xnet); |
| 747 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 748 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + |
| 749 | GENEVE_BASE_HLEN + info->options_len + ip_hdr_len; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 750 | err = skb_cow_head(skb, min_headroom); |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 751 | if (unlikely(err)) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 752 | goto free_dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 753 | |
Alexander Duyck | aed069d | 2016-04-14 15:33:37 -0400 | [diff] [blame] | 754 | err = udp_tunnel_handle_offloads(skb, udp_sum); |
Dan Carpenter | 1ba64fa | 2016-04-19 17:30:56 +0300 | [diff] [blame] | 755 | if (err) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 756 | goto free_dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 757 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 758 | gnvh = __skb_push(skb, sizeof(*gnvh) + info->options_len); |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 759 | geneve_build_header(gnvh, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 760 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 761 | return 0; |
| 762 | |
| 763 | free_dst: |
| 764 | dst_release(dst); |
| 765 | return err; |
| 766 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 767 | |
| 768 | static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, |
| 769 | struct net_device *dev, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 770 | struct geneve_sock *gs4, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 771 | struct flowi4 *fl4, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 772 | const struct ip_tunnel_info *info) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 773 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 774 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 775 | struct geneve_dev *geneve = netdev_priv(dev); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 776 | struct dst_cache *dst_cache; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 777 | struct rtable *rt = NULL; |
| 778 | __u8 tos; |
| 779 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 780 | if (!gs4) |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 781 | return ERR_PTR(-EIO); |
| 782 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 783 | memset(fl4, 0, sizeof(*fl4)); |
| 784 | fl4->flowi4_mark = skb->mark; |
| 785 | fl4->flowi4_proto = IPPROTO_UDP; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 786 | fl4->daddr = info->key.u.ipv4.dst; |
| 787 | fl4->saddr = info->key.u.ipv4.src; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 788 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 789 | tos = info->key.tos; |
| 790 | if ((tos == 1) && !geneve->collect_md) { |
| 791 | tos = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
| 792 | use_cache = false; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 793 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 794 | fl4->flowi4_tos = RT_TOS(tos); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 795 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 796 | dst_cache = (struct dst_cache *)&info->dst_cache; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 797 | if (use_cache) { |
| 798 | rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); |
| 799 | if (rt) |
| 800 | return rt; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 801 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 802 | rt = ip_route_output_key(geneve->net, fl4); |
| 803 | if (IS_ERR(rt)) { |
| 804 | netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 805 | return ERR_PTR(-ENETUNREACH); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 806 | } |
| 807 | if (rt->dst.dev == dev) { /* is this necessary? */ |
| 808 | netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 809 | ip_rt_put(rt); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 810 | return ERR_PTR(-ELOOP); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 811 | } |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 812 | if (use_cache) |
| 813 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 814 | return rt; |
| 815 | } |
| 816 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 817 | #if IS_ENABLED(CONFIG_IPV6) |
| 818 | static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, |
| 819 | struct net_device *dev, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 820 | struct geneve_sock *gs6, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 821 | struct flowi6 *fl6, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 822 | const struct ip_tunnel_info *info) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 823 | { |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 824 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 825 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 826 | struct dst_entry *dst = NULL; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 827 | struct dst_cache *dst_cache; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 828 | __u8 prio; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 829 | |
pravin shelar | fceb9c3 | 2016-10-28 09:59:16 -0700 | [diff] [blame] | 830 | if (!gs6) |
| 831 | return ERR_PTR(-EIO); |
| 832 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 833 | memset(fl6, 0, sizeof(*fl6)); |
| 834 | fl6->flowi6_mark = skb->mark; |
| 835 | fl6->flowi6_proto = IPPROTO_UDP; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 836 | fl6->daddr = info->key.u.ipv6.dst; |
| 837 | fl6->saddr = info->key.u.ipv6.src; |
| 838 | prio = info->key.tos; |
| 839 | if ((prio == 1) && !geneve->collect_md) { |
| 840 | prio = ip_tunnel_get_dsfield(ip_hdr(skb), skb); |
| 841 | use_cache = false; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 842 | } |
| 843 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 844 | fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio), |
| 845 | info->key.label); |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 846 | dst_cache = (struct dst_cache *)&info->dst_cache; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 847 | if (use_cache) { |
| 848 | dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); |
| 849 | if (dst) |
| 850 | return dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 851 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 852 | if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) { |
| 853 | netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); |
| 854 | return ERR_PTR(-ENETUNREACH); |
| 855 | } |
| 856 | if (dst->dev == dev) { /* is this necessary? */ |
| 857 | netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); |
| 858 | dst_release(dst); |
| 859 | return ERR_PTR(-ELOOP); |
| 860 | } |
| 861 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 862 | if (use_cache) |
| 863 | dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 864 | return dst; |
| 865 | } |
| 866 | #endif |
| 867 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 868 | static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 869 | struct geneve_dev *geneve, |
| 870 | const struct ip_tunnel_info *info) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 871 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 872 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
| 873 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); |
| 874 | const struct ip_tunnel_key *key = &info->key; |
| 875 | struct rtable *rt; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 876 | struct flowi4 fl4; |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 877 | __u8 tos, ttl; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 878 | __be16 sport; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 879 | __be16 df; |
pravin shelar | bcceeec | 2016-11-21 11:03:00 -0800 | [diff] [blame] | 880 | int err; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 881 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 882 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 883 | if (IS_ERR(rt)) |
| 884 | return PTR_ERR(rt); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 885 | |
Stefano Brivio | 6b4f92a | 2018-10-12 23:53:59 +0200 | [diff] [blame] | 886 | skb_tunnel_check_pmtu(skb, &rt->dst, |
| 887 | GENEVE_IPV4_HLEN + info->options_len); |
Xin Long | 52a589d | 2017-12-25 14:43:58 +0800 | [diff] [blame] | 888 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 889 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 890 | if (geneve->collect_md) { |
| 891 | tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 892 | ttl = key->ttl; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 893 | } else { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 894 | tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb); |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 895 | if (geneve->ttl_inherit) |
| 896 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
| 897 | else |
| 898 | ttl = key->ttl; |
| 899 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 900 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 901 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
| 902 | |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 903 | err = geneve_build_skb(&rt->dst, skb, info, xnet, sizeof(struct iphdr)); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 904 | if (unlikely(err)) |
| 905 | return err; |
| 906 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 907 | udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 908 | tos, ttl, df, sport, geneve->info.key.tp_dst, |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 909 | !net_eq(geneve->net, dev_net(geneve->dev)), |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 910 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 911 | return 0; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 912 | } |
| 913 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 914 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 915 | static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
pravin shelar | c3ef5aa | 2016-11-21 11:02:59 -0800 | [diff] [blame] | 916 | struct geneve_dev *geneve, |
| 917 | const struct ip_tunnel_info *info) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 918 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 919 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
| 920 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); |
| 921 | const struct ip_tunnel_key *key = &info->key; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 922 | struct dst_entry *dst = NULL; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 923 | struct flowi6 fl6; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 924 | __u8 prio, ttl; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 925 | __be16 sport; |
pravin shelar | bcceeec | 2016-11-21 11:03:00 -0800 | [diff] [blame] | 926 | int err; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 927 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 928 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 929 | if (IS_ERR(dst)) |
| 930 | return PTR_ERR(dst); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 931 | |
Stefano Brivio | 6b4f92a | 2018-10-12 23:53:59 +0200 | [diff] [blame] | 932 | skb_tunnel_check_pmtu(skb, dst, GENEVE_IPV6_HLEN + info->options_len); |
Xin Long | 52a589d | 2017-12-25 14:43:58 +0800 | [diff] [blame] | 933 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 934 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 935 | if (geneve->collect_md) { |
| 936 | prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 937 | ttl = key->ttl; |
| 938 | } else { |
Daniel Borkmann | 95caf6f | 2016-03-18 18:37:58 +0100 | [diff] [blame] | 939 | prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel), |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 940 | ip_hdr(skb), skb); |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 941 | if (geneve->ttl_inherit) |
| 942 | ttl = ip_tunnel_get_ttl(ip_hdr(skb), skb); |
| 943 | else |
| 944 | ttl = key->ttl; |
| 945 | ttl = ttl ? : ip6_dst_hoplimit(dst); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 946 | } |
Haishuang Yan | 31ac1c1 | 2016-11-28 13:26:58 +0800 | [diff] [blame] | 947 | err = geneve_build_skb(dst, skb, info, xnet, sizeof(struct ipv6hdr)); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 948 | if (unlikely(err)) |
| 949 | return err; |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 950 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 951 | udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 952 | &fl6.saddr, &fl6.daddr, prio, ttl, |
| 953 | info->key.label, sport, geneve->info.key.tp_dst, |
| 954 | !(info->key.tun_flags & TUNNEL_CSUM)); |
| 955 | return 0; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 956 | } |
| 957 | #endif |
| 958 | |
| 959 | static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) |
| 960 | { |
| 961 | struct geneve_dev *geneve = netdev_priv(dev); |
| 962 | struct ip_tunnel_info *info = NULL; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 963 | int err; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 964 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 965 | if (geneve->collect_md) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 966 | info = skb_tunnel_info(skb); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 967 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
| 968 | err = -EINVAL; |
| 969 | netdev_dbg(dev, "no tunnel metadata\n"); |
| 970 | goto tx_error; |
| 971 | } |
| 972 | } else { |
| 973 | info = &geneve->info; |
| 974 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 975 | |
Jakub Kicinski | a717e3f | 2017-02-24 11:43:37 -0800 | [diff] [blame] | 976 | rcu_read_lock(); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 977 | #if IS_ENABLED(CONFIG_IPV6) |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 978 | if (info->mode & IP_TUNNEL_INFO_IPV6) |
| 979 | err = geneve6_xmit_skb(skb, dev, geneve, info); |
| 980 | else |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 981 | #endif |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 982 | err = geneve_xmit_skb(skb, dev, geneve, info); |
Jakub Kicinski | a717e3f | 2017-02-24 11:43:37 -0800 | [diff] [blame] | 983 | rcu_read_unlock(); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 984 | |
| 985 | if (likely(!err)) |
| 986 | return NETDEV_TX_OK; |
| 987 | tx_error: |
| 988 | dev_kfree_skb(skb); |
| 989 | |
| 990 | if (err == -ELOOP) |
| 991 | dev->stats.collisions++; |
| 992 | else if (err == -ENETUNREACH) |
| 993 | dev->stats.tx_carrier_errors++; |
| 994 | |
| 995 | dev->stats.tx_errors++; |
| 996 | return NETDEV_TX_OK; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 997 | } |
| 998 | |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 999 | static int geneve_change_mtu(struct net_device *dev, int new_mtu) |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1000 | { |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1001 | if (new_mtu > dev->max_mtu) |
| 1002 | new_mtu = dev->max_mtu; |
Alexey Kodanev | 321acc1 | 2018-04-19 15:42:31 +0300 | [diff] [blame] | 1003 | else if (new_mtu < dev->min_mtu) |
| 1004 | new_mtu = dev->min_mtu; |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1005 | |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1006 | dev->mtu = new_mtu; |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1010 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 1011 | { |
| 1012 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1013 | struct geneve_dev *geneve = netdev_priv(dev); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1014 | |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1015 | if (ip_tunnel_info_af(info) == AF_INET) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1016 | struct rtable *rt; |
| 1017 | struct flowi4 fl4; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1018 | struct geneve_sock *gs4 = rcu_dereference(geneve->sock4); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1019 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1020 | rt = geneve_get_v4_rt(skb, dev, gs4, &fl4, info); |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1021 | if (IS_ERR(rt)) |
| 1022 | return PTR_ERR(rt); |
| 1023 | |
| 1024 | ip_rt_put(rt); |
| 1025 | info->key.u.ipv4.src = fl4.saddr; |
| 1026 | #if IS_ENABLED(CONFIG_IPV6) |
| 1027 | } else if (ip_tunnel_info_af(info) == AF_INET6) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1028 | struct dst_entry *dst; |
| 1029 | struct flowi6 fl6; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1030 | struct geneve_sock *gs6 = rcu_dereference(geneve->sock6); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1031 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1032 | dst = geneve_get_v6_dst(skb, dev, gs6, &fl6, info); |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1033 | if (IS_ERR(dst)) |
| 1034 | return PTR_ERR(dst); |
| 1035 | |
| 1036 | dst_release(dst); |
| 1037 | info->key.u.ipv6.src = fl6.saddr; |
| 1038 | #endif |
| 1039 | } else { |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1040 | return -EINVAL; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1041 | } |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1042 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1043 | info->key.tp_src = udp_flow_src_port(geneve->net, skb, |
| 1044 | 1, USHRT_MAX, true); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1045 | info->key.tp_dst = geneve->info.key.tp_dst; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1049 | static const struct net_device_ops geneve_netdev_ops = { |
| 1050 | .ndo_init = geneve_init, |
| 1051 | .ndo_uninit = geneve_uninit, |
| 1052 | .ndo_open = geneve_open, |
| 1053 | .ndo_stop = geneve_stop, |
| 1054 | .ndo_start_xmit = geneve_xmit, |
| 1055 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1056 | .ndo_change_mtu = geneve_change_mtu, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1057 | .ndo_validate_addr = eth_validate_addr, |
| 1058 | .ndo_set_mac_address = eth_mac_addr, |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1059 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1060 | }; |
| 1061 | |
| 1062 | static void geneve_get_drvinfo(struct net_device *dev, |
| 1063 | struct ethtool_drvinfo *drvinfo) |
| 1064 | { |
| 1065 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); |
| 1066 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); |
| 1067 | } |
| 1068 | |
| 1069 | static const struct ethtool_ops geneve_ethtool_ops = { |
| 1070 | .get_drvinfo = geneve_get_drvinfo, |
| 1071 | .get_link = ethtool_op_get_link, |
| 1072 | }; |
| 1073 | |
| 1074 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1075 | static struct device_type geneve_type = { |
| 1076 | .name = "geneve", |
| 1077 | }; |
| 1078 | |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 1079 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1080 | * supply the listening GENEVE udp ports. Callers are expected |
Sabrina Dubroca | e5de25d | 2016-07-11 13:12:28 +0200 | [diff] [blame] | 1081 | * to implement the ndo_udp_tunnel_add. |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1082 | */ |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1083 | static void geneve_offload_rx_ports(struct net_device *dev, bool push) |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1084 | { |
| 1085 | struct net *net = dev_net(dev); |
| 1086 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1087 | struct geneve_sock *gs; |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1088 | |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1089 | rcu_read_lock(); |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1090 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { |
| 1091 | if (push) { |
| 1092 | udp_tunnel_push_rx_port(dev, gs->sock, |
| 1093 | UDP_TUNNEL_TYPE_GENEVE); |
| 1094 | } else { |
| 1095 | udp_tunnel_drop_rx_port(dev, gs->sock, |
| 1096 | UDP_TUNNEL_TYPE_GENEVE); |
| 1097 | } |
| 1098 | } |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1099 | rcu_read_unlock(); |
| 1100 | } |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1101 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1102 | /* Initialize the device structure. */ |
| 1103 | static void geneve_setup(struct net_device *dev) |
| 1104 | { |
| 1105 | ether_setup(dev); |
| 1106 | |
| 1107 | dev->netdev_ops = &geneve_netdev_ops; |
| 1108 | dev->ethtool_ops = &geneve_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1109 | dev->needs_free_netdev = true; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1110 | |
| 1111 | SET_NETDEV_DEVTYPE(dev, &geneve_type); |
| 1112 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1113 | dev->features |= NETIF_F_LLTX; |
| 1114 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
| 1115 | dev->features |= NETIF_F_RXCSUM; |
| 1116 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1117 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1118 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
| 1119 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1120 | |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1121 | /* MTU range: 68 - (something less than 65535) */ |
| 1122 | dev->min_mtu = ETH_MIN_MTU; |
| 1123 | /* The max_mtu calculation does not take account of GENEVE |
| 1124 | * options, to avoid excluding potentially valid |
| 1125 | * configurations. This will be further reduced by IPvX hdr size. |
| 1126 | */ |
| 1127 | dev->max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len; |
| 1128 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1129 | netif_keep_dst(dev); |
Jiri Benc | fc41cdb | 2016-02-17 15:31:35 +0100 | [diff] [blame] | 1130 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Phil Sutter | ed961ac | 2015-08-18 10:30:31 +0200 | [diff] [blame] | 1131 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Pravin B Shelar | 87cd3dc | 2015-08-26 23:46:48 -0700 | [diff] [blame] | 1132 | eth_hw_addr_random(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { |
| 1136 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, |
| 1137 | [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1138 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1139 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1140 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1141 | [IFLA_GENEVE_LABEL] = { .type = NLA_U32 }, |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1142 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1143 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1144 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
| 1145 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 1146 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1147 | [IFLA_GENEVE_TTL_INHERIT] = { .type = NLA_U8 }, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1148 | }; |
| 1149 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1150 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1151 | struct netlink_ext_ack *extack) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1152 | { |
| 1153 | if (tb[IFLA_ADDRESS]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1154 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 1155 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 1156 | "Provided link layer address is not Ethernet"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1157 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1158 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1159 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1160 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 1161 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
| 1162 | "Provided Ethernet address is not unicast"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1163 | return -EADDRNOTAVAIL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1164 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1165 | } |
| 1166 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1167 | if (!data) { |
| 1168 | NL_SET_ERR_MSG(extack, |
| 1169 | "Not enough attributes provided to perform the operation"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1170 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1171 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1172 | |
| 1173 | if (data[IFLA_GENEVE_ID]) { |
| 1174 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1175 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1176 | if (vni >= GENEVE_N_VID) { |
| 1177 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_ID], |
| 1178 | "Geneve ID must be lower than 16777216"); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1179 | return -ERANGE; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1180 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1186 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1187 | const struct ip_tunnel_info *info, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1188 | bool *tun_on_same_port, |
| 1189 | bool *tun_collect_md) |
| 1190 | { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1191 | struct geneve_dev *geneve, *t = NULL; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1192 | |
| 1193 | *tun_on_same_port = false; |
| 1194 | *tun_collect_md = false; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1195 | list_for_each_entry(geneve, &gn->geneve_list, next) { |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1196 | if (info->key.tp_dst == geneve->info.key.tp_dst) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1197 | *tun_collect_md = geneve->collect_md; |
| 1198 | *tun_on_same_port = true; |
| 1199 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1200 | if (info->key.tun_id == geneve->info.key.tun_id && |
| 1201 | info->key.tp_dst == geneve->info.key.tp_dst && |
| 1202 | !memcmp(&info->key.u, &geneve->info.key.u, sizeof(info->key.u))) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1203 | t = geneve; |
| 1204 | } |
| 1205 | return t; |
| 1206 | } |
| 1207 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1208 | static bool is_tnl_info_zero(const struct ip_tunnel_info *info) |
| 1209 | { |
Stefano Brivio | 3fa5f11 | 2017-10-20 13:31:36 +0200 | [diff] [blame] | 1210 | return !(info->key.tun_id || info->key.tun_flags || info->key.tos || |
| 1211 | info->key.ttl || info->key.label || info->key.tp_src || |
| 1212 | memchr_inv(&info->key.u, 0, sizeof(info->key.u))); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1215 | static bool geneve_dst_addr_equal(struct ip_tunnel_info *a, |
| 1216 | struct ip_tunnel_info *b) |
| 1217 | { |
| 1218 | if (ip_tunnel_info_af(a) == AF_INET) |
| 1219 | return a->key.u.ipv4.dst == b->key.u.ipv4.dst; |
| 1220 | else |
| 1221 | return ipv6_addr_equal(&a->key.u.ipv6.dst, &b->key.u.ipv6.dst); |
| 1222 | } |
| 1223 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1224 | static int geneve_configure(struct net *net, struct net_device *dev, |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1225 | struct netlink_ext_ack *extack, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1226 | const struct ip_tunnel_info *info, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1227 | bool metadata, bool ipv6_rx_csum, |
| 1228 | bool ttl_inherit) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1229 | { |
| 1230 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1231 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
| 1232 | bool tun_collect_md, tun_on_same_port; |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1233 | int err, encap_len; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1234 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1235 | if (metadata && !is_tnl_info_zero(info)) { |
| 1236 | NL_SET_ERR_MSG(extack, |
| 1237 | "Device is externally controlled, so attributes (VNI, Port, and so on) must not be specified"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1238 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1239 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1240 | |
| 1241 | geneve->net = net; |
| 1242 | geneve->dev = dev; |
| 1243 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1244 | t = geneve_find_dev(gn, info, &tun_on_same_port, &tun_collect_md); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1245 | if (t) |
| 1246 | return -EBUSY; |
| 1247 | |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1248 | /* make enough headroom for basic scenario */ |
| 1249 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; |
Eric Garver | 9a1c44d | 2017-06-02 14:54:10 -0400 | [diff] [blame] | 1250 | if (!metadata && ip_tunnel_info_af(info) == AF_INET) { |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1251 | encap_len += sizeof(struct iphdr); |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1252 | dev->max_mtu -= sizeof(struct iphdr); |
| 1253 | } else { |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1254 | encap_len += sizeof(struct ipv6hdr); |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1255 | dev->max_mtu -= sizeof(struct ipv6hdr); |
| 1256 | } |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1257 | dev->needed_headroom = encap_len + ETH_HLEN; |
| 1258 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1259 | if (metadata) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1260 | if (tun_on_same_port) { |
| 1261 | NL_SET_ERR_MSG(extack, |
| 1262 | "There can be only one externally controlled device on a destination port"); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1263 | return -EPERM; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1264 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1265 | } else { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1266 | if (tun_collect_md) { |
| 1267 | NL_SET_ERR_MSG(extack, |
| 1268 | "There already exists an externally controlled device on this destination port"); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1269 | return -EPERM; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1270 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1273 | dst_cache_reset(&geneve->info.dst_cache); |
| 1274 | geneve->info = *info; |
| 1275 | geneve->collect_md = metadata; |
| 1276 | geneve->use_udp6_rx_checksums = ipv6_rx_csum; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1277 | geneve->ttl_inherit = ttl_inherit; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame] | 1278 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1279 | err = register_netdevice(dev); |
| 1280 | if (err) |
| 1281 | return err; |
| 1282 | |
| 1283 | list_add(&geneve->next, &gn->geneve_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1287 | static void init_tnl_info(struct ip_tunnel_info *info, __u16 dst_port) |
| 1288 | { |
| 1289 | memset(info, 0, sizeof(*info)); |
| 1290 | info->key.tp_dst = htons(dst_port); |
| 1291 | } |
| 1292 | |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1293 | static int geneve_nl2info(struct nlattr *tb[], struct nlattr *data[], |
| 1294 | struct netlink_ext_ack *extack, |
| 1295 | struct ip_tunnel_info *info, bool *metadata, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1296 | bool *use_udp6_rx_checksums, bool *ttl_inherit, |
| 1297 | bool changelink) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1298 | { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1299 | int attrtype; |
| 1300 | |
| 1301 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) { |
| 1302 | NL_SET_ERR_MSG(extack, |
| 1303 | "Cannot specify both IPv4 and IPv6 Remote addresses"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1304 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1305 | } |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1306 | |
| 1307 | if (data[IFLA_GENEVE_REMOTE]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1308 | if (changelink && (ip_tunnel_info_af(info) == AF_INET6)) { |
| 1309 | attrtype = IFLA_GENEVE_REMOTE; |
| 1310 | goto change_notsup; |
| 1311 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1312 | |
| 1313 | info->key.u.ipv4.dst = |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1314 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1315 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1316 | if (IN_MULTICAST(ntohl(info->key.u.ipv4.dst))) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1317 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE], |
| 1318 | "Remote IPv4 address cannot be Multicast"); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1319 | return -EINVAL; |
| 1320 | } |
| 1321 | } |
| 1322 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1323 | if (data[IFLA_GENEVE_REMOTE6]) { |
Alexey Kodanev | 4c52a88 | 2018-04-19 15:42:29 +0300 | [diff] [blame] | 1324 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1325 | if (changelink && (ip_tunnel_info_af(info) == AF_INET)) { |
| 1326 | attrtype = IFLA_GENEVE_REMOTE6; |
| 1327 | goto change_notsup; |
| 1328 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1329 | |
| 1330 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 1331 | info->key.u.ipv6.dst = |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1332 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1333 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1334 | if (ipv6_addr_type(&info->key.u.ipv6.dst) & |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1335 | IPV6_ADDR_LINKLOCAL) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1336 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1337 | "Remote IPv6 address cannot be link-local"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1338 | return -EINVAL; |
| 1339 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1340 | if (ipv6_addr_is_multicast(&info->key.u.ipv6.dst)) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1341 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1342 | "Remote IPv6 address cannot be Multicast"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1343 | return -EINVAL; |
| 1344 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1345 | info->key.tun_flags |= TUNNEL_CSUM; |
| 1346 | *use_udp6_rx_checksums = true; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1347 | #else |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1348 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_REMOTE6], |
| 1349 | "IPv6 support not enabled in the kernel"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1350 | return -EPFNOSUPPORT; |
| 1351 | #endif |
| 1352 | } |
| 1353 | |
| 1354 | if (data[IFLA_GENEVE_ID]) { |
| 1355 | __u32 vni; |
| 1356 | __u8 tvni[3]; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1357 | __be64 tunid; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1358 | |
| 1359 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1360 | tvni[0] = (vni & 0x00ff0000) >> 16; |
| 1361 | tvni[1] = (vni & 0x0000ff00) >> 8; |
| 1362 | tvni[2] = vni & 0x000000ff; |
| 1363 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1364 | tunid = vni_to_tunnel_id(tvni); |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1365 | if (changelink && (tunid != info->key.tun_id)) { |
| 1366 | attrtype = IFLA_GENEVE_ID; |
| 1367 | goto change_notsup; |
| 1368 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1369 | info->key.tun_id = tunid; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1370 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1371 | |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1372 | if (data[IFLA_GENEVE_TTL_INHERIT]) { |
| 1373 | if (nla_get_u8(data[IFLA_GENEVE_TTL_INHERIT])) |
| 1374 | *ttl_inherit = true; |
| 1375 | else |
| 1376 | *ttl_inherit = false; |
| 1377 | } else if (data[IFLA_GENEVE_TTL]) { |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1378 | info->key.ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
Hangbin Liu | a97d97b | 2018-09-29 23:06:29 +0800 | [diff] [blame] | 1379 | *ttl_inherit = false; |
| 1380 | } |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1381 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1382 | if (data[IFLA_GENEVE_TOS]) |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1383 | info->key.tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1384 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1385 | if (data[IFLA_GENEVE_LABEL]) { |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1386 | info->key.label = nla_get_be32(data[IFLA_GENEVE_LABEL]) & |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1387 | IPV6_FLOWLABEL_MASK; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1388 | if (info->key.label && (!(info->mode & IP_TUNNEL_INFO_IPV6))) { |
| 1389 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_LABEL], |
| 1390 | "Label attribute only applies for IPv6 Geneve devices"); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1391 | return -EINVAL; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1392 | } |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1393 | } |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1394 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1395 | if (data[IFLA_GENEVE_PORT]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1396 | if (changelink) { |
| 1397 | attrtype = IFLA_GENEVE_PORT; |
| 1398 | goto change_notsup; |
| 1399 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1400 | info->key.tp_dst = nla_get_be16(data[IFLA_GENEVE_PORT]); |
| 1401 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1402 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1403 | if (data[IFLA_GENEVE_COLLECT_METADATA]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1404 | if (changelink) { |
| 1405 | attrtype = IFLA_GENEVE_COLLECT_METADATA; |
| 1406 | goto change_notsup; |
| 1407 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1408 | *metadata = true; |
| 1409 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1410 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1411 | if (data[IFLA_GENEVE_UDP_CSUM]) { |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1412 | if (changelink) { |
| 1413 | attrtype = IFLA_GENEVE_UDP_CSUM; |
| 1414 | goto change_notsup; |
| 1415 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1416 | if (nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
| 1417 | info->key.tun_flags |= TUNNEL_CSUM; |
| 1418 | } |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1419 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1420 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) { |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1421 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1422 | if (changelink) { |
| 1423 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_TX; |
| 1424 | goto change_notsup; |
| 1425 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1426 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) |
| 1427 | info->key.tun_flags &= ~TUNNEL_CSUM; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1428 | #else |
| 1429 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX], |
| 1430 | "IPv6 support not enabled in the kernel"); |
| 1431 | return -EPFNOSUPPORT; |
| 1432 | #endif |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1433 | } |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1434 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1435 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) { |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1436 | #if IS_ENABLED(CONFIG_IPV6) |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1437 | if (changelink) { |
| 1438 | attrtype = IFLA_GENEVE_UDP_ZERO_CSUM6_RX; |
| 1439 | goto change_notsup; |
| 1440 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1441 | if (nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) |
| 1442 | *use_udp6_rx_checksums = false; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1443 | #else |
| 1444 | NL_SET_ERR_MSG_ATTR(extack, data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX], |
| 1445 | "IPv6 support not enabled in the kernel"); |
| 1446 | return -EPFNOSUPPORT; |
| 1447 | #endif |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | return 0; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1451 | change_notsup: |
| 1452 | NL_SET_ERR_MSG_ATTR(extack, data[attrtype], |
| 1453 | "Changing VNI, Port, endpoint IP address family, external, and UDP checksum attributes are not supported"); |
| 1454 | return -EOPNOTSUPP; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1457 | static void geneve_link_config(struct net_device *dev, |
| 1458 | struct ip_tunnel_info *info, struct nlattr *tb[]) |
| 1459 | { |
| 1460 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1461 | int ldev_mtu = 0; |
| 1462 | |
| 1463 | if (tb[IFLA_MTU]) { |
| 1464 | geneve_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | switch (ip_tunnel_info_af(info)) { |
| 1469 | case AF_INET: { |
| 1470 | struct flowi4 fl4 = { .daddr = info->key.u.ipv4.dst }; |
| 1471 | struct rtable *rt = ip_route_output_key(geneve->net, &fl4); |
| 1472 | |
| 1473 | if (!IS_ERR(rt) && rt->dst.dev) { |
| 1474 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV4_HLEN; |
| 1475 | ip_rt_put(rt); |
| 1476 | } |
| 1477 | break; |
| 1478 | } |
| 1479 | #if IS_ENABLED(CONFIG_IPV6) |
| 1480 | case AF_INET6: { |
| 1481 | struct rt6_info *rt = rt6_lookup(geneve->net, |
| 1482 | &info->key.u.ipv6.dst, NULL, 0, |
| 1483 | NULL, 0); |
| 1484 | |
| 1485 | if (rt && rt->dst.dev) |
| 1486 | ldev_mtu = rt->dst.dev->mtu - GENEVE_IPV6_HLEN; |
| 1487 | ip6_rt_put(rt); |
| 1488 | break; |
| 1489 | } |
| 1490 | #endif |
| 1491 | } |
| 1492 | |
| 1493 | if (ldev_mtu <= 0) |
| 1494 | return; |
| 1495 | |
| 1496 | geneve_change_mtu(dev, ldev_mtu - info->options_len); |
| 1497 | } |
| 1498 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1499 | static int geneve_newlink(struct net *net, struct net_device *dev, |
| 1500 | struct nlattr *tb[], struct nlattr *data[], |
| 1501 | struct netlink_ext_ack *extack) |
| 1502 | { |
| 1503 | bool use_udp6_rx_checksums = false; |
| 1504 | struct ip_tunnel_info info; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1505 | bool ttl_inherit = false; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1506 | bool metadata = false; |
| 1507 | int err; |
| 1508 | |
| 1509 | init_tnl_info(&info, GENEVE_UDP_PORT); |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1510 | err = geneve_nl2info(tb, data, extack, &info, &metadata, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1511 | &use_udp6_rx_checksums, &ttl_inherit, false); |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1512 | if (err) |
| 1513 | return err; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1514 | |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1515 | err = geneve_configure(net, dev, extack, &info, metadata, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1516 | use_udp6_rx_checksums, ttl_inherit); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1517 | if (err) |
| 1518 | return err; |
| 1519 | |
| 1520 | geneve_link_config(dev, &info, tb); |
| 1521 | |
| 1522 | return 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1525 | /* Quiesces the geneve device data path for both TX and RX. |
| 1526 | * |
| 1527 | * On transmit geneve checks for non-NULL geneve_sock before it proceeds. |
| 1528 | * So, if we set that socket to NULL under RCU and wait for synchronize_net() |
| 1529 | * to complete for the existing set of in-flight packets to be transmitted, |
| 1530 | * then we would have quiesced the transmit data path. All the future packets |
| 1531 | * will get dropped until we unquiesce the data path. |
| 1532 | * |
| 1533 | * On receive geneve dereference the geneve_sock stashed in the socket. So, |
| 1534 | * if we set that to NULL under RCU and wait for synchronize_net() to |
| 1535 | * complete, then we would have quiesced the receive data path. |
| 1536 | */ |
| 1537 | static void geneve_quiesce(struct geneve_dev *geneve, struct geneve_sock **gs4, |
| 1538 | struct geneve_sock **gs6) |
| 1539 | { |
| 1540 | *gs4 = rtnl_dereference(geneve->sock4); |
| 1541 | rcu_assign_pointer(geneve->sock4, NULL); |
| 1542 | if (*gs4) |
| 1543 | rcu_assign_sk_user_data((*gs4)->sock->sk, NULL); |
| 1544 | #if IS_ENABLED(CONFIG_IPV6) |
| 1545 | *gs6 = rtnl_dereference(geneve->sock6); |
| 1546 | rcu_assign_pointer(geneve->sock6, NULL); |
| 1547 | if (*gs6) |
| 1548 | rcu_assign_sk_user_data((*gs6)->sock->sk, NULL); |
| 1549 | #else |
| 1550 | *gs6 = NULL; |
| 1551 | #endif |
| 1552 | synchronize_net(); |
| 1553 | } |
| 1554 | |
| 1555 | /* Resumes the geneve device data path for both TX and RX. */ |
| 1556 | static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4, |
| 1557 | struct geneve_sock __maybe_unused *gs6) |
| 1558 | { |
| 1559 | rcu_assign_pointer(geneve->sock4, gs4); |
| 1560 | if (gs4) |
| 1561 | rcu_assign_sk_user_data(gs4->sock->sk, gs4); |
| 1562 | #if IS_ENABLED(CONFIG_IPV6) |
| 1563 | rcu_assign_pointer(geneve->sock6, gs6); |
| 1564 | if (gs6) |
| 1565 | rcu_assign_sk_user_data(gs6->sock->sk, gs6); |
| 1566 | #endif |
| 1567 | synchronize_net(); |
| 1568 | } |
| 1569 | |
| 1570 | static int geneve_changelink(struct net_device *dev, struct nlattr *tb[], |
| 1571 | struct nlattr *data[], |
| 1572 | struct netlink_ext_ack *extack) |
| 1573 | { |
| 1574 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1575 | struct geneve_sock *gs4, *gs6; |
| 1576 | struct ip_tunnel_info info; |
| 1577 | bool metadata; |
| 1578 | bool use_udp6_rx_checksums; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1579 | bool ttl_inherit; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1580 | int err; |
| 1581 | |
| 1582 | /* If the geneve device is configured for metadata (or externally |
| 1583 | * controlled, for example, OVS), then nothing can be changed. |
| 1584 | */ |
| 1585 | if (geneve->collect_md) |
| 1586 | return -EOPNOTSUPP; |
| 1587 | |
| 1588 | /* Start with the existing info. */ |
| 1589 | memcpy(&info, &geneve->info, sizeof(info)); |
| 1590 | metadata = geneve->collect_md; |
| 1591 | use_udp6_rx_checksums = geneve->use_udp6_rx_checksums; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1592 | ttl_inherit = geneve->ttl_inherit; |
Girish Moodalbail | c5ebc44 | 2017-08-09 01:09:28 -0700 | [diff] [blame] | 1593 | err = geneve_nl2info(tb, data, extack, &info, &metadata, |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1594 | &use_udp6_rx_checksums, &ttl_inherit, true); |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1595 | if (err) |
| 1596 | return err; |
| 1597 | |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1598 | if (!geneve_dst_addr_equal(&geneve->info, &info)) { |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1599 | dst_cache_reset(&info.dst_cache); |
Alexey Kodanev | c40e89f | 2018-04-19 15:42:32 +0300 | [diff] [blame] | 1600 | geneve_link_config(dev, &info, tb); |
| 1601 | } |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1602 | |
| 1603 | geneve_quiesce(geneve, &gs4, &gs6); |
| 1604 | geneve->info = info; |
| 1605 | geneve->collect_md = metadata; |
| 1606 | geneve->use_udp6_rx_checksums = use_udp6_rx_checksums; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1607 | geneve->ttl_inherit = ttl_inherit; |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1608 | geneve_unquiesce(geneve, gs4, gs6); |
| 1609 | |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1613 | static void geneve_dellink(struct net_device *dev, struct list_head *head) |
| 1614 | { |
| 1615 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1616 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1617 | list_del(&geneve->next); |
| 1618 | unregister_netdevice_queue(dev, head); |
| 1619 | } |
| 1620 | |
| 1621 | static size_t geneve_get_size(const struct net_device *dev) |
| 1622 | { |
| 1623 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1624 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1625 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1626 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
Daniel Borkmann | 8eb3b99 | 2016-03-09 03:00:04 +0100 | [diff] [blame] | 1627 | nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */ |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1628 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1629 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1630 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
| 1631 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ |
| 1632 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1633 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL_INHERIT */ |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1634 | 0; |
| 1635 | } |
| 1636 | |
| 1637 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1638 | { |
| 1639 | struct geneve_dev *geneve = netdev_priv(dev); |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1640 | struct ip_tunnel_info *info = &geneve->info; |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1641 | bool ttl_inherit = geneve->ttl_inherit; |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1642 | bool metadata = geneve->collect_md; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1643 | __u8 tmp_vni[3]; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1644 | __u32 vni; |
| 1645 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1646 | tunnel_id_to_vni(info->key.tun_id, tmp_vni); |
| 1647 | vni = (tmp_vni[0] << 16) | (tmp_vni[1] << 8) | tmp_vni[2]; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1648 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) |
| 1649 | goto nla_put_failure; |
| 1650 | |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1651 | if (!metadata && ip_tunnel_info_af(info) == AF_INET) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1652 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1653 | info->key.u.ipv4.dst)) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1654 | goto nla_put_failure; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1655 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
| 1656 | !!(info->key.tun_flags & TUNNEL_CSUM))) |
| 1657 | goto nla_put_failure; |
| 1658 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1659 | #if IS_ENABLED(CONFIG_IPV6) |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1660 | } else if (!metadata) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1661 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1662 | &info->key.u.ipv6.dst)) |
| 1663 | goto nla_put_failure; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1664 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
| 1665 | !(info->key.tun_flags & TUNNEL_CSUM))) |
| 1666 | goto nla_put_failure; |
Eric Garver | 11387fe | 2017-05-23 18:37:27 -0400 | [diff] [blame] | 1667 | #endif |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1668 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1669 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1670 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, info->key.ttl) || |
| 1671 | nla_put_u8(skb, IFLA_GENEVE_TOS, info->key.tos) || |
| 1672 | nla_put_be32(skb, IFLA_GENEVE_LABEL, info->key.label)) |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1673 | goto nla_put_failure; |
| 1674 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1675 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, info->key.tp_dst)) |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1676 | goto nla_put_failure; |
| 1677 | |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1678 | if (metadata && nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1679 | goto nla_put_failure; |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1680 | |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1681 | #if IS_ENABLED(CONFIG_IPV6) |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1682 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, |
| 1683 | !geneve->use_udp6_rx_checksums)) |
| 1684 | goto nla_put_failure; |
Hangbin Liu | f9094b7 | 2017-11-23 11:27:24 +0800 | [diff] [blame] | 1685 | #endif |
Hangbin Liu | fd7eafd | 2017-11-15 09:43:09 +0800 | [diff] [blame] | 1686 | |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1687 | if (nla_put_u8(skb, IFLA_GENEVE_TTL_INHERIT, ttl_inherit)) |
| 1688 | goto nla_put_failure; |
| 1689 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1690 | return 0; |
| 1691 | |
| 1692 | nla_put_failure: |
| 1693 | return -EMSGSIZE; |
| 1694 | } |
| 1695 | |
| 1696 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { |
| 1697 | .kind = "geneve", |
| 1698 | .maxtype = IFLA_GENEVE_MAX, |
| 1699 | .policy = geneve_policy, |
| 1700 | .priv_size = sizeof(struct geneve_dev), |
| 1701 | .setup = geneve_setup, |
| 1702 | .validate = geneve_validate, |
| 1703 | .newlink = geneve_newlink, |
Girish Moodalbail | 5b861f6 | 2017-07-20 22:44:20 -0700 | [diff] [blame] | 1704 | .changelink = geneve_changelink, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1705 | .dellink = geneve_dellink, |
| 1706 | .get_size = geneve_get_size, |
| 1707 | .fill_info = geneve_fill_info, |
| 1708 | }; |
| 1709 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1710 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
| 1711 | u8 name_assign_type, u16 dst_port) |
| 1712 | { |
| 1713 | struct nlattr *tb[IFLA_MAX + 1]; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1714 | struct ip_tunnel_info info; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1715 | struct net_device *dev; |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1716 | LIST_HEAD(list_kill); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1717 | int err; |
| 1718 | |
| 1719 | memset(tb, 0, sizeof(tb)); |
| 1720 | dev = rtnl_create_link(net, name, name_assign_type, |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 1721 | &geneve_link_ops, tb, NULL); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1722 | if (IS_ERR(dev)) |
| 1723 | return dev; |
| 1724 | |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1725 | init_tnl_info(&info, dst_port); |
Hangbin Liu | 52d0d404 | 2018-09-12 10:04:21 +0800 | [diff] [blame] | 1726 | err = geneve_configure(net, dev, NULL, &info, true, true, false); |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1727 | if (err) { |
| 1728 | free_netdev(dev); |
| 1729 | return ERR_PTR(err); |
| 1730 | } |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1731 | |
| 1732 | /* openvswitch users expect packet sizes to be unrestricted, |
| 1733 | * so set the largest MTU we can. |
| 1734 | */ |
Jarod Wilson | 9157208 | 2016-10-20 13:55:20 -0400 | [diff] [blame] | 1735 | err = geneve_change_mtu(dev, IP_MAX_MTU); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1736 | if (err) |
| 1737 | goto err; |
| 1738 | |
Nicolas Dichtel | 4100948 | 2016-06-13 10:31:07 +0200 | [diff] [blame] | 1739 | err = rtnl_configure_link(dev, NULL); |
| 1740 | if (err < 0) |
| 1741 | goto err; |
| 1742 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1743 | return dev; |
pravin shelar | 9b4437a | 2016-11-21 11:02:58 -0800 | [diff] [blame] | 1744 | err: |
Nicolas Dichtel | 106da66 | 2016-06-13 10:31:04 +0200 | [diff] [blame] | 1745 | geneve_dellink(dev, &list_kill); |
| 1746 | unregister_netdevice_many(&list_kill); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1747 | return ERR_PTR(err); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1748 | } |
| 1749 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); |
| 1750 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1751 | static int geneve_netdevice_event(struct notifier_block *unused, |
| 1752 | unsigned long event, void *ptr) |
| 1753 | { |
| 1754 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1755 | |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1756 | if (event == NETDEV_UDP_TUNNEL_PUSH_INFO || |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1757 | event == NETDEV_UDP_TUNNEL_DROP_INFO) { |
Sabrina Dubroca | 2d2b13f | 2017-07-21 12:49:32 +0200 | [diff] [blame] | 1758 | geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO); |
Sabrina Dubroca | 0458495 | 2017-07-21 12:49:33 +0200 | [diff] [blame] | 1759 | } else if (event == NETDEV_UNREGISTER) { |
| 1760 | geneve_offload_rx_ports(dev, false); |
| 1761 | } else if (event == NETDEV_REGISTER) { |
| 1762 | geneve_offload_rx_ports(dev, true); |
| 1763 | } |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1764 | |
| 1765 | return NOTIFY_DONE; |
| 1766 | } |
| 1767 | |
| 1768 | static struct notifier_block geneve_notifier_block __read_mostly = { |
| 1769 | .notifier_call = geneve_netdevice_event, |
| 1770 | }; |
| 1771 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1772 | static __net_init int geneve_init_net(struct net *net) |
| 1773 | { |
| 1774 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1775 | |
| 1776 | INIT_LIST_HEAD(&gn->geneve_list); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1777 | INIT_LIST_HEAD(&gn->sock_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1778 | return 0; |
| 1779 | } |
| 1780 | |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1781 | static void geneve_destroy_tunnels(struct net *net, struct list_head *head) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1782 | { |
| 1783 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1784 | struct geneve_dev *geneve, *next; |
| 1785 | struct net_device *dev, *aux; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1786 | |
| 1787 | /* gather any geneve devices that were moved into this ns */ |
| 1788 | for_each_netdev_safe(net, dev, aux) |
| 1789 | if (dev->rtnl_link_ops == &geneve_link_ops) |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1790 | unregister_netdevice_queue(dev, head); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1791 | |
| 1792 | /* now gather any other geneve devices that were created in this ns */ |
| 1793 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { |
| 1794 | /* If geneve->dev is in the same netns, it was already added |
| 1795 | * to the list by the previous loop. |
| 1796 | */ |
| 1797 | if (!net_eq(dev_net(geneve->dev), net)) |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1798 | unregister_netdevice_queue(geneve->dev, head); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1799 | } |
| 1800 | |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1801 | WARN_ON_ONCE(!list_empty(&gn->sock_list)); |
| 1802 | } |
| 1803 | |
| 1804 | static void __net_exit geneve_exit_batch_net(struct list_head *net_list) |
| 1805 | { |
| 1806 | struct net *net; |
| 1807 | LIST_HEAD(list); |
| 1808 | |
| 1809 | rtnl_lock(); |
| 1810 | list_for_each_entry(net, net_list, exit_list) |
| 1811 | geneve_destroy_tunnels(net, &list); |
| 1812 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1813 | /* unregister the devices gathered above */ |
| 1814 | unregister_netdevice_many(&list); |
| 1815 | rtnl_unlock(); |
| 1816 | } |
| 1817 | |
| 1818 | static struct pernet_operations geneve_net_ops = { |
| 1819 | .init = geneve_init_net, |
Haishuang Yan | 2843a25 | 2017-12-16 17:54:50 +0800 | [diff] [blame] | 1820 | .exit_batch = geneve_exit_batch_net, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1821 | .id = &geneve_net_id, |
| 1822 | .size = sizeof(struct geneve_net), |
| 1823 | }; |
| 1824 | |
| 1825 | static int __init geneve_init_module(void) |
| 1826 | { |
| 1827 | int rc; |
| 1828 | |
| 1829 | rc = register_pernet_subsys(&geneve_net_ops); |
| 1830 | if (rc) |
| 1831 | goto out1; |
| 1832 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1833 | rc = register_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1834 | if (rc) |
| 1835 | goto out2; |
| 1836 | |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1837 | rc = rtnl_link_register(&geneve_link_ops); |
| 1838 | if (rc) |
| 1839 | goto out3; |
| 1840 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1841 | return 0; |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1842 | out3: |
| 1843 | unregister_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1844 | out2: |
| 1845 | unregister_pernet_subsys(&geneve_net_ops); |
| 1846 | out1: |
| 1847 | return rc; |
| 1848 | } |
| 1849 | late_initcall(geneve_init_module); |
| 1850 | |
| 1851 | static void __exit geneve_cleanup_module(void) |
| 1852 | { |
| 1853 | rtnl_link_unregister(&geneve_link_ops); |
Hannes Frederic Sowa | 681e683 | 2016-04-18 21:19:48 +0200 | [diff] [blame] | 1854 | unregister_netdevice_notifier(&geneve_notifier_block); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1855 | unregister_pernet_subsys(&geneve_net_ops); |
| 1856 | } |
| 1857 | module_exit(geneve_cleanup_module); |
| 1858 | |
| 1859 | MODULE_LICENSE("GPL"); |
| 1860 | MODULE_VERSION(GENEVE_NETDEV_VER); |
| 1861 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); |
| 1862 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); |
| 1863 | MODULE_ALIAS_RTNL_LINK("geneve"); |