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> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/hash.h> |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 18 | #include <net/dst_metadata.h> |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 19 | #include <net/gro_cells.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 20 | #include <net/rtnetlink.h> |
| 21 | #include <net/geneve.h> |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 22 | #include <net/protocol.h> |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 23 | |
| 24 | #define GENEVE_NETDEV_VER "0.6" |
| 25 | |
| 26 | #define GENEVE_UDP_PORT 6081 |
| 27 | |
| 28 | #define GENEVE_N_VID (1u << 24) |
| 29 | #define GENEVE_VID_MASK (GENEVE_N_VID - 1) |
| 30 | |
| 31 | #define VNI_HASH_BITS 10 |
| 32 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 33 | |
| 34 | static bool log_ecn_error = true; |
| 35 | module_param(log_ecn_error, bool, 0644); |
| 36 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 37 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 38 | #define GENEVE_VER 0 |
| 39 | #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr)) |
| 40 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 41 | /* per-network namespace private data for this module */ |
| 42 | struct geneve_net { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 43 | struct list_head geneve_list; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 44 | struct list_head sock_list; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 45 | }; |
| 46 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 47 | static int geneve_net_id; |
| 48 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 49 | union geneve_addr { |
| 50 | struct sockaddr_in sin; |
| 51 | struct sockaddr_in6 sin6; |
| 52 | struct sockaddr sa; |
| 53 | }; |
| 54 | |
| 55 | static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, }; |
| 56 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 57 | /* Pseudo network device */ |
| 58 | struct geneve_dev { |
| 59 | struct hlist_node hlist; /* vni hash table */ |
| 60 | struct net *net; /* netns for packet i/o */ |
| 61 | struct net_device *dev; /* netdev for geneve tunnel */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 62 | struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */ |
| 63 | #if IS_ENABLED(CONFIG_IPV6) |
| 64 | struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */ |
| 65 | #endif |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 66 | u8 vni[3]; /* virtual network ID for tunnel */ |
| 67 | u8 ttl; /* TTL override */ |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 68 | u8 tos; /* TOS override */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 69 | union geneve_addr remote; /* IP address for link partner */ |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 70 | struct list_head next; /* geneve's per namespace list */ |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 71 | __be16 dst_port; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 72 | bool collect_md; |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 73 | struct gro_cells gro_cells; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 74 | u32 flags; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 75 | struct dst_cache dst_cache; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 78 | /* Geneve device flags */ |
| 79 | #define GENEVE_F_UDP_CSUM BIT(0) |
| 80 | #define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1) |
| 81 | #define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2) |
| 82 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 83 | struct geneve_sock { |
| 84 | bool collect_md; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 85 | struct list_head list; |
| 86 | struct socket *sock; |
| 87 | struct rcu_head rcu; |
| 88 | int refcnt; |
| 89 | struct udp_offload udp_offloads; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 90 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 91 | u32 flags; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 92 | }; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 93 | |
| 94 | static inline __u32 geneve_net_vni_hash(u8 vni[3]) |
| 95 | { |
| 96 | __u32 vnid; |
| 97 | |
| 98 | vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 99 | return hash_32(vnid, VNI_HASH_BITS); |
| 100 | } |
| 101 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 102 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
| 103 | { |
| 104 | #ifdef __BIG_ENDIAN |
| 105 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 106 | #else |
| 107 | return (__force __be64)(((__force u64)vni[0] << 40) | |
| 108 | ((__force u64)vni[1] << 48) | |
| 109 | ((__force u64)vni[2] << 56)); |
| 110 | #endif |
| 111 | } |
| 112 | |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 113 | static struct geneve_dev *geneve_lookup(struct geneve_sock *gs, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 114 | __be32 addr, u8 vni[]) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 115 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 116 | struct hlist_head *vni_list_head; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 117 | struct geneve_dev *geneve; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 118 | __u32 hash; |
| 119 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 120 | /* Find the device for this VNI */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 121 | hash = geneve_net_vni_hash(vni); |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 122 | vni_list_head = &gs->vni_list[hash]; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 123 | hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 124 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 125 | addr == geneve->remote.sin.sin_addr.s_addr) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 126 | return geneve; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 127 | } |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
| 130 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 131 | #if IS_ENABLED(CONFIG_IPV6) |
| 132 | static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs, |
| 133 | struct in6_addr addr6, u8 vni[]) |
| 134 | { |
| 135 | struct hlist_head *vni_list_head; |
| 136 | struct geneve_dev *geneve; |
| 137 | __u32 hash; |
| 138 | |
| 139 | /* Find the device for this VNI */ |
| 140 | hash = geneve_net_vni_hash(vni); |
| 141 | vni_list_head = &gs->vni_list[hash]; |
| 142 | hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) { |
| 143 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && |
| 144 | ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr)) |
| 145 | return geneve; |
| 146 | } |
| 147 | return NULL; |
| 148 | } |
| 149 | #endif |
| 150 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 151 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
| 152 | { |
| 153 | return (struct genevehdr *)(udp_hdr(skb) + 1); |
| 154 | } |
| 155 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 156 | /* geneve receive/decap routine */ |
| 157 | static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb) |
| 158 | { |
| 159 | struct genevehdr *gnvh = geneve_hdr(skb); |
| 160 | struct metadata_dst *tun_dst = NULL; |
| 161 | struct geneve_dev *geneve = NULL; |
| 162 | struct pcpu_sw_netstats *stats; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 163 | struct iphdr *iph = NULL; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 164 | __be32 addr; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 165 | static u8 zero_vni[3]; |
| 166 | u8 *vni; |
| 167 | int err = 0; |
| 168 | sa_family_t sa_family; |
| 169 | #if IS_ENABLED(CONFIG_IPV6) |
| 170 | struct ipv6hdr *ip6h = NULL; |
| 171 | struct in6_addr addr6; |
| 172 | static struct in6_addr zero_addr6; |
| 173 | #endif |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 174 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 175 | sa_family = gs->sock->sk->sk_family; |
John W. Linville | 08399ef | 2015-09-21 10:29:09 -0400 | [diff] [blame] | 176 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 177 | if (sa_family == AF_INET) { |
| 178 | iph = ip_hdr(skb); /* outer IP header... */ |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 179 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 180 | if (gs->collect_md) { |
| 181 | vni = zero_vni; |
| 182 | addr = 0; |
| 183 | } else { |
| 184 | vni = gnvh->vni; |
| 185 | |
| 186 | addr = iph->saddr; |
| 187 | } |
| 188 | |
| 189 | geneve = geneve_lookup(gs, addr, vni); |
| 190 | #if IS_ENABLED(CONFIG_IPV6) |
| 191 | } else if (sa_family == AF_INET6) { |
| 192 | ip6h = ipv6_hdr(skb); /* outer IPv6 header... */ |
| 193 | |
| 194 | if (gs->collect_md) { |
| 195 | vni = zero_vni; |
| 196 | addr6 = zero_addr6; |
| 197 | } else { |
| 198 | vni = gnvh->vni; |
| 199 | |
| 200 | addr6 = ip6h->saddr; |
| 201 | } |
| 202 | |
| 203 | geneve = geneve6_lookup(gs, addr6, vni); |
| 204 | #endif |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 205 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 206 | if (!geneve) |
| 207 | goto drop; |
| 208 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 209 | if (ip_tunnel_collect_metadata() || gs->collect_md) { |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 210 | __be16 flags; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 211 | |
| 212 | flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | |
| 213 | (gnvh->oam ? TUNNEL_OAM : 0) | |
| 214 | (gnvh->critical ? TUNNEL_CRIT_OPT : 0); |
| 215 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 216 | tun_dst = udp_tun_rx_dst(skb, sa_family, flags, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 217 | vni_to_tunnel_id(gnvh->vni), |
| 218 | gnvh->opt_len * 4); |
| 219 | if (!tun_dst) |
| 220 | goto drop; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 221 | /* Update tunnel dst according to Geneve options. */ |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 222 | ip_tunnel_info_opts_set(&tun_dst->u.tun_info, |
| 223 | gnvh->options, gnvh->opt_len * 4); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 224 | } else { |
| 225 | /* Drop packets w/ critical options, |
| 226 | * since we don't support any... |
| 227 | */ |
| 228 | if (gnvh->critical) |
| 229 | goto drop; |
| 230 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 231 | |
| 232 | skb_reset_mac_header(skb); |
| 233 | skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev))); |
| 234 | skb->protocol = eth_type_trans(skb, geneve->dev); |
| 235 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 236 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 237 | if (tun_dst) |
| 238 | skb_dst_set(skb, &tun_dst->dst); |
| 239 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 240 | /* Ignore packet loops (and multicast echo) */ |
| 241 | if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) |
| 242 | goto drop; |
| 243 | |
| 244 | skb_reset_network_header(skb); |
| 245 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 246 | if (iph) |
| 247 | err = IP_ECN_decapsulate(iph, skb); |
| 248 | #if IS_ENABLED(CONFIG_IPV6) |
| 249 | if (ip6h) |
| 250 | err = IP6_ECN_decapsulate(ip6h, skb); |
| 251 | #endif |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 252 | |
| 253 | if (unlikely(err)) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 254 | if (log_ecn_error) { |
| 255 | if (iph) |
| 256 | net_info_ratelimited("non-ECT from %pI4 " |
| 257 | "with TOS=%#x\n", |
| 258 | &iph->saddr, iph->tos); |
| 259 | #if IS_ENABLED(CONFIG_IPV6) |
| 260 | if (ip6h) |
| 261 | net_info_ratelimited("non-ECT from %pI6\n", |
| 262 | &ip6h->saddr); |
| 263 | #endif |
| 264 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 265 | if (err > 1) { |
| 266 | ++geneve->dev->stats.rx_frame_errors; |
| 267 | ++geneve->dev->stats.rx_errors; |
| 268 | goto drop; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | stats = this_cpu_ptr(geneve->dev->tstats); |
| 273 | u64_stats_update_begin(&stats->syncp); |
| 274 | stats->rx_packets++; |
| 275 | stats->rx_bytes += skb->len; |
| 276 | u64_stats_update_end(&stats->syncp); |
| 277 | |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 278 | gro_cells_receive(&geneve->gro_cells, skb); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 279 | return; |
| 280 | drop: |
| 281 | /* Consume bad packet */ |
| 282 | kfree_skb(skb); |
| 283 | } |
| 284 | |
| 285 | /* Setup stats when device is created */ |
| 286 | static int geneve_init(struct net_device *dev) |
| 287 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 288 | struct geneve_dev *geneve = netdev_priv(dev); |
| 289 | int err; |
| 290 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 291 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 292 | if (!dev->tstats) |
| 293 | return -ENOMEM; |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 294 | |
| 295 | err = gro_cells_init(&geneve->gro_cells, dev); |
| 296 | if (err) { |
| 297 | free_percpu(dev->tstats); |
| 298 | return err; |
| 299 | } |
| 300 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 301 | err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL); |
| 302 | if (err) { |
| 303 | free_percpu(dev->tstats); |
| 304 | gro_cells_destroy(&geneve->gro_cells); |
| 305 | return err; |
| 306 | } |
| 307 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static void geneve_uninit(struct net_device *dev) |
| 312 | { |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 313 | struct geneve_dev *geneve = netdev_priv(dev); |
| 314 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 315 | dst_cache_destroy(&geneve->dst_cache); |
Jesse Gross | 8e816df | 2015-08-28 16:54:40 -0700 | [diff] [blame] | 316 | gro_cells_destroy(&geneve->gro_cells); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 317 | free_percpu(dev->tstats); |
| 318 | } |
| 319 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 320 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 321 | static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 322 | { |
| 323 | struct genevehdr *geneveh; |
| 324 | struct geneve_sock *gs; |
| 325 | int opts_len; |
| 326 | |
| 327 | /* Need Geneve and inner Ethernet header to be present */ |
| 328 | if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN))) |
| 329 | goto error; |
| 330 | |
| 331 | /* Return packets with reserved bits set */ |
| 332 | geneveh = geneve_hdr(skb); |
| 333 | if (unlikely(geneveh->ver != GENEVE_VER)) |
| 334 | goto error; |
| 335 | |
| 336 | if (unlikely(geneveh->proto_type != htons(ETH_P_TEB))) |
| 337 | goto error; |
| 338 | |
| 339 | opts_len = geneveh->opt_len * 4; |
| 340 | if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, |
| 341 | htons(ETH_P_TEB))) |
| 342 | goto drop; |
| 343 | |
| 344 | gs = rcu_dereference_sk_user_data(sk); |
| 345 | if (!gs) |
| 346 | goto drop; |
| 347 | |
| 348 | geneve_rx(gs, skb); |
| 349 | return 0; |
| 350 | |
| 351 | drop: |
| 352 | /* Consume bad packet */ |
| 353 | kfree_skb(skb); |
| 354 | return 0; |
| 355 | |
| 356 | error: |
| 357 | /* Let the UDP layer deal with the skb */ |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | static struct socket *geneve_create_sock(struct net *net, bool ipv6, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 362 | __be16 port, u32 flags) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 363 | { |
| 364 | struct socket *sock; |
| 365 | struct udp_port_cfg udp_conf; |
| 366 | int err; |
| 367 | |
| 368 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 369 | |
| 370 | if (ipv6) { |
| 371 | udp_conf.family = AF_INET6; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 372 | udp_conf.ipv6_v6only = 1; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 373 | udp_conf.use_udp6_rx_checksums = |
| 374 | !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 375 | } else { |
| 376 | udp_conf.family = AF_INET; |
| 377 | udp_conf.local_ip.s_addr = htonl(INADDR_ANY); |
| 378 | } |
| 379 | |
| 380 | udp_conf.local_udp_port = port; |
| 381 | |
| 382 | /* Open UDP socket */ |
| 383 | err = udp_sock_create(net, &udp_conf, &sock); |
| 384 | if (err < 0) |
| 385 | return ERR_PTR(err); |
| 386 | |
| 387 | return sock; |
| 388 | } |
| 389 | |
| 390 | static void geneve_notify_add_rx_port(struct geneve_sock *gs) |
| 391 | { |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 392 | struct net_device *dev; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 393 | struct sock *sk = gs->sock->sk; |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 394 | struct net *net = sock_net(sk); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 395 | sa_family_t sa_family = sk->sk_family; |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 396 | __be16 port = inet_sk(sk)->inet_sport; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 397 | int err; |
| 398 | |
| 399 | if (sa_family == AF_INET) { |
Hannes Frederic Sowa | 787d7ac | 2016-01-07 14:28:39 +0100 | [diff] [blame] | 400 | err = udp_add_offload(sock_net(sk), &gs->udp_offloads); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 401 | if (err) |
| 402 | pr_warn("geneve: udp_add_offload failed with status %d\n", |
| 403 | err); |
| 404 | } |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 405 | |
| 406 | rcu_read_lock(); |
| 407 | for_each_netdev_rcu(net, dev) { |
| 408 | if (dev->netdev_ops->ndo_add_geneve_port) |
| 409 | dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, |
| 410 | port); |
| 411 | } |
| 412 | rcu_read_unlock(); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static int geneve_hlen(struct genevehdr *gh) |
| 416 | { |
| 417 | return sizeof(*gh) + gh->opt_len * 4; |
| 418 | } |
| 419 | |
| 420 | static struct sk_buff **geneve_gro_receive(struct sk_buff **head, |
| 421 | struct sk_buff *skb, |
| 422 | struct udp_offload *uoff) |
| 423 | { |
| 424 | struct sk_buff *p, **pp = NULL; |
| 425 | struct genevehdr *gh, *gh2; |
| 426 | unsigned int hlen, gh_len, off_gnv; |
| 427 | const struct packet_offload *ptype; |
| 428 | __be16 type; |
| 429 | int flush = 1; |
| 430 | |
| 431 | off_gnv = skb_gro_offset(skb); |
| 432 | hlen = off_gnv + sizeof(*gh); |
| 433 | gh = skb_gro_header_fast(skb, off_gnv); |
| 434 | if (skb_gro_header_hard(skb, hlen)) { |
| 435 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 436 | if (unlikely(!gh)) |
| 437 | goto out; |
| 438 | } |
| 439 | |
| 440 | if (gh->ver != GENEVE_VER || gh->oam) |
| 441 | goto out; |
| 442 | gh_len = geneve_hlen(gh); |
| 443 | |
| 444 | hlen = off_gnv + gh_len; |
| 445 | if (skb_gro_header_hard(skb, hlen)) { |
| 446 | gh = skb_gro_header_slow(skb, hlen, off_gnv); |
| 447 | if (unlikely(!gh)) |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | flush = 0; |
| 452 | |
| 453 | for (p = *head; p; p = p->next) { |
| 454 | if (!NAPI_GRO_CB(p)->same_flow) |
| 455 | continue; |
| 456 | |
| 457 | gh2 = (struct genevehdr *)(p->data + off_gnv); |
| 458 | if (gh->opt_len != gh2->opt_len || |
| 459 | memcmp(gh, gh2, gh_len)) { |
| 460 | NAPI_GRO_CB(p)->same_flow = 0; |
| 461 | continue; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | type = gh->proto_type; |
| 466 | |
| 467 | rcu_read_lock(); |
| 468 | ptype = gro_find_receive_by_type(type); |
| 469 | if (!ptype) { |
| 470 | flush = 1; |
| 471 | goto out_unlock; |
| 472 | } |
| 473 | |
| 474 | skb_gro_pull(skb, gh_len); |
| 475 | skb_gro_postpull_rcsum(skb, gh, gh_len); |
| 476 | pp = ptype->callbacks.gro_receive(head, skb); |
| 477 | |
| 478 | out_unlock: |
| 479 | rcu_read_unlock(); |
| 480 | out: |
| 481 | NAPI_GRO_CB(skb)->flush |= flush; |
| 482 | |
| 483 | return pp; |
| 484 | } |
| 485 | |
| 486 | static int geneve_gro_complete(struct sk_buff *skb, int nhoff, |
| 487 | struct udp_offload *uoff) |
| 488 | { |
| 489 | struct genevehdr *gh; |
| 490 | struct packet_offload *ptype; |
| 491 | __be16 type; |
| 492 | int gh_len; |
| 493 | int err = -ENOSYS; |
| 494 | |
| 495 | udp_tunnel_gro_complete(skb, nhoff); |
| 496 | |
| 497 | gh = (struct genevehdr *)(skb->data + nhoff); |
| 498 | gh_len = geneve_hlen(gh); |
| 499 | type = gh->proto_type; |
| 500 | |
| 501 | rcu_read_lock(); |
| 502 | ptype = gro_find_complete_by_type(type); |
| 503 | if (ptype) |
| 504 | err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); |
| 505 | |
| 506 | rcu_read_unlock(); |
| 507 | return err; |
| 508 | } |
| 509 | |
| 510 | /* Create new listen socket if needed */ |
| 511 | static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 512 | bool ipv6, u32 flags) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 513 | { |
| 514 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 515 | struct geneve_sock *gs; |
| 516 | struct socket *sock; |
| 517 | struct udp_tunnel_sock_cfg tunnel_cfg; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 518 | int h; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 519 | |
| 520 | gs = kzalloc(sizeof(*gs), GFP_KERNEL); |
| 521 | if (!gs) |
| 522 | return ERR_PTR(-ENOMEM); |
| 523 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 524 | sock = geneve_create_sock(net, ipv6, port, flags); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 525 | if (IS_ERR(sock)) { |
| 526 | kfree(gs); |
| 527 | return ERR_CAST(sock); |
| 528 | } |
| 529 | |
| 530 | gs->sock = sock; |
| 531 | gs->refcnt = 1; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 532 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 533 | INIT_HLIST_HEAD(&gs->vni_list[h]); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 534 | |
| 535 | /* Initialize the geneve udp offloads structure */ |
| 536 | gs->udp_offloads.port = port; |
| 537 | gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive; |
| 538 | gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete; |
| 539 | geneve_notify_add_rx_port(gs); |
| 540 | |
| 541 | /* Mark socket as an encapsulation socket */ |
| 542 | tunnel_cfg.sk_user_data = gs; |
| 543 | tunnel_cfg.encap_type = 1; |
| 544 | tunnel_cfg.encap_rcv = geneve_udp_encap_recv; |
| 545 | tunnel_cfg.encap_destroy = NULL; |
| 546 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 547 | list_add(&gs->list, &gn->sock_list); |
| 548 | return gs; |
| 549 | } |
| 550 | |
| 551 | static void geneve_notify_del_rx_port(struct geneve_sock *gs) |
| 552 | { |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 553 | struct net_device *dev; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 554 | struct sock *sk = gs->sock->sk; |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 555 | struct net *net = sock_net(sk); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 556 | sa_family_t sa_family = sk->sk_family; |
Singhai, Anjali | a8170d2 | 2015-12-14 12:21:17 -0800 | [diff] [blame] | 557 | __be16 port = inet_sk(sk)->inet_sport; |
| 558 | |
| 559 | rcu_read_lock(); |
| 560 | for_each_netdev_rcu(net, dev) { |
| 561 | if (dev->netdev_ops->ndo_del_geneve_port) |
| 562 | dev->netdev_ops->ndo_del_geneve_port(dev, sa_family, |
| 563 | port); |
| 564 | } |
| 565 | |
| 566 | rcu_read_unlock(); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 567 | |
| 568 | if (sa_family == AF_INET) |
| 569 | udp_del_offload(&gs->udp_offloads); |
| 570 | } |
| 571 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 572 | static void __geneve_sock_release(struct geneve_sock *gs) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 573 | { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 574 | if (!gs || --gs->refcnt) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 575 | return; |
| 576 | |
| 577 | list_del(&gs->list); |
| 578 | geneve_notify_del_rx_port(gs); |
| 579 | udp_tunnel_sock_release(gs->sock); |
| 580 | kfree_rcu(gs, rcu); |
| 581 | } |
| 582 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 583 | static void geneve_sock_release(struct geneve_dev *geneve) |
| 584 | { |
| 585 | __geneve_sock_release(geneve->sock4); |
| 586 | #if IS_ENABLED(CONFIG_IPV6) |
| 587 | __geneve_sock_release(geneve->sock6); |
| 588 | #endif |
| 589 | } |
| 590 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 591 | static struct geneve_sock *geneve_find_sock(struct geneve_net *gn, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 592 | sa_family_t family, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 593 | __be16 dst_port) |
| 594 | { |
| 595 | struct geneve_sock *gs; |
| 596 | |
| 597 | list_for_each_entry(gs, &gn->sock_list, list) { |
| 598 | if (inet_sk(gs->sock->sk)->inet_sport == dst_port && |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 599 | inet_sk(gs->sock->sk)->sk.sk_family == family) { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 600 | return gs; |
| 601 | } |
| 602 | } |
| 603 | return NULL; |
| 604 | } |
| 605 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 606 | static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 607 | { |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 608 | struct net *net = geneve->net; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 609 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 610 | struct geneve_sock *gs; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 611 | __u32 hash; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 612 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 613 | gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 614 | if (gs) { |
| 615 | gs->refcnt++; |
| 616 | goto out; |
| 617 | } |
| 618 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 619 | gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 620 | if (IS_ERR(gs)) |
| 621 | return PTR_ERR(gs); |
| 622 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 623 | out: |
| 624 | gs->collect_md = geneve->collect_md; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 625 | gs->flags = geneve->flags; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 626 | #if IS_ENABLED(CONFIG_IPV6) |
| 627 | if (ipv6) |
| 628 | geneve->sock6 = gs; |
| 629 | else |
| 630 | #endif |
| 631 | geneve->sock4 = gs; |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 632 | |
| 633 | hash = geneve_net_vni_hash(geneve->vni); |
| 634 | hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 638 | static int geneve_open(struct net_device *dev) |
| 639 | { |
| 640 | struct geneve_dev *geneve = netdev_priv(dev); |
| 641 | bool ipv6 = geneve->remote.sa.sa_family == AF_INET6; |
| 642 | bool metadata = geneve->collect_md; |
| 643 | int ret = 0; |
| 644 | |
| 645 | geneve->sock4 = NULL; |
| 646 | #if IS_ENABLED(CONFIG_IPV6) |
| 647 | geneve->sock6 = NULL; |
| 648 | if (ipv6 || metadata) |
| 649 | ret = geneve_sock_add(geneve, true); |
| 650 | #endif |
| 651 | if (!ret && (!ipv6 || metadata)) |
| 652 | ret = geneve_sock_add(geneve, false); |
| 653 | if (ret < 0) |
| 654 | geneve_sock_release(geneve); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 659 | static int geneve_stop(struct net_device *dev) |
| 660 | { |
| 661 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 662 | |
Pravin B Shelar | 66d4700 | 2015-08-26 23:46:55 -0700 | [diff] [blame] | 663 | if (!hlist_unhashed(&geneve->hlist)) |
| 664 | hlist_del_rcu(&geneve->hlist); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 665 | geneve_sock_release(geneve); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 669 | static void geneve_build_header(struct genevehdr *geneveh, |
| 670 | __be16 tun_flags, u8 vni[3], |
| 671 | u8 options_len, u8 *options) |
| 672 | { |
| 673 | geneveh->ver = GENEVE_VER; |
| 674 | geneveh->opt_len = options_len / 4; |
| 675 | geneveh->oam = !!(tun_flags & TUNNEL_OAM); |
| 676 | geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT); |
| 677 | geneveh->rsvd1 = 0; |
| 678 | memcpy(geneveh->vni, vni, 3); |
| 679 | geneveh->proto_type = htons(ETH_P_TEB); |
| 680 | geneveh->rsvd2 = 0; |
| 681 | |
| 682 | memcpy(geneveh->options, options, options_len); |
| 683 | } |
| 684 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 685 | static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb, |
| 686 | __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 687 | u32 flags, bool xnet) |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 688 | { |
| 689 | struct genevehdr *gnvh; |
| 690 | int min_headroom; |
| 691 | int err; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 692 | bool udp_sum = !!(flags & GENEVE_F_UDP_CSUM); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 693 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 694 | skb_scrub_packet(skb, xnet); |
| 695 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 696 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
| 697 | + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr); |
| 698 | err = skb_cow_head(skb, min_headroom); |
| 699 | if (unlikely(err)) { |
| 700 | kfree_skb(skb); |
| 701 | goto free_rt; |
| 702 | } |
| 703 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 704 | skb = udp_tunnel_handle_offloads(skb, udp_sum); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 705 | if (IS_ERR(skb)) { |
| 706 | err = PTR_ERR(skb); |
| 707 | goto free_rt; |
| 708 | } |
| 709 | |
| 710 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 711 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 712 | |
| 713 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 714 | return 0; |
| 715 | |
| 716 | free_rt: |
| 717 | ip_rt_put(rt); |
| 718 | return err; |
| 719 | } |
| 720 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 721 | #if IS_ENABLED(CONFIG_IPV6) |
| 722 | static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb, |
| 723 | __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 724 | u32 flags, bool xnet) |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 725 | { |
| 726 | struct genevehdr *gnvh; |
| 727 | int min_headroom; |
| 728 | int err; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 729 | bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 730 | |
| 731 | skb_scrub_packet(skb, xnet); |
| 732 | |
| 733 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
| 734 | + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr); |
| 735 | err = skb_cow_head(skb, min_headroom); |
| 736 | if (unlikely(err)) { |
| 737 | kfree_skb(skb); |
| 738 | goto free_dst; |
| 739 | } |
| 740 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 741 | skb = udp_tunnel_handle_offloads(skb, udp_sum); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 742 | if (IS_ERR(skb)) { |
| 743 | err = PTR_ERR(skb); |
| 744 | goto free_dst; |
| 745 | } |
| 746 | |
| 747 | gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); |
| 748 | geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); |
| 749 | |
| 750 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 751 | return 0; |
| 752 | |
| 753 | free_dst: |
| 754 | dst_release(dst); |
| 755 | return err; |
| 756 | } |
| 757 | #endif |
| 758 | |
| 759 | static struct rtable *geneve_get_v4_rt(struct sk_buff *skb, |
| 760 | struct net_device *dev, |
| 761 | struct flowi4 *fl4, |
| 762 | struct ip_tunnel_info *info) |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 763 | { |
| 764 | struct geneve_dev *geneve = netdev_priv(dev); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 765 | struct dst_cache *dst_cache; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 766 | struct rtable *rt = NULL; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 767 | bool use_cache = true; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 768 | __u8 tos; |
| 769 | |
| 770 | memset(fl4, 0, sizeof(*fl4)); |
| 771 | fl4->flowi4_mark = skb->mark; |
| 772 | fl4->flowi4_proto = IPPROTO_UDP; |
| 773 | |
| 774 | if (info) { |
| 775 | fl4->daddr = info->key.u.ipv4.dst; |
| 776 | fl4->saddr = info->key.u.ipv4.src; |
| 777 | fl4->flowi4_tos = RT_TOS(info->key.tos); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 778 | dst_cache = &info->dst_cache; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 779 | } else { |
| 780 | tos = geneve->tos; |
| 781 | if (tos == 1) { |
| 782 | const struct iphdr *iip = ip_hdr(skb); |
| 783 | |
| 784 | tos = ip_tunnel_get_dsfield(iip, skb); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 785 | use_cache = false; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | fl4->flowi4_tos = RT_TOS(tos); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 789 | fl4->daddr = geneve->remote.sin.sin_addr.s_addr; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 790 | dst_cache = &geneve->dst_cache; |
| 791 | } |
| 792 | |
| 793 | use_cache = use_cache && !skb->mark; |
| 794 | if (use_cache) { |
| 795 | rt = dst_cache_get_ip4(dst_cache, &fl4->saddr); |
| 796 | if (rt) |
| 797 | return rt; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | rt = ip_route_output_key(geneve->net, fl4); |
| 801 | if (IS_ERR(rt)) { |
| 802 | netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 803 | return ERR_PTR(-ENETUNREACH); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 804 | } |
| 805 | if (rt->dst.dev == dev) { /* is this necessary? */ |
| 806 | netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 807 | ip_rt_put(rt); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 808 | return ERR_PTR(-ELOOP); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 809 | } |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 810 | if (use_cache) |
| 811 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 812 | return rt; |
| 813 | } |
| 814 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 815 | #if IS_ENABLED(CONFIG_IPV6) |
| 816 | static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb, |
| 817 | struct net_device *dev, |
| 818 | struct flowi6 *fl6, |
| 819 | struct ip_tunnel_info *info) |
| 820 | { |
| 821 | struct geneve_dev *geneve = netdev_priv(dev); |
| 822 | struct geneve_sock *gs6 = geneve->sock6; |
| 823 | struct dst_entry *dst = NULL; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 824 | struct dst_cache *dst_cache; |
| 825 | bool use_cache = true; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 826 | __u8 prio; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 827 | |
| 828 | memset(fl6, 0, sizeof(*fl6)); |
| 829 | fl6->flowi6_mark = skb->mark; |
| 830 | fl6->flowi6_proto = IPPROTO_UDP; |
| 831 | |
| 832 | if (info) { |
| 833 | fl6->daddr = info->key.u.ipv6.dst; |
| 834 | fl6->saddr = info->key.u.ipv6.src; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 835 | fl6->flowi6_tos = RT_TOS(info->key.tos); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 836 | dst_cache = &info->dst_cache; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 837 | } else { |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 838 | prio = geneve->tos; |
| 839 | if (prio == 1) { |
| 840 | const struct iphdr *iip = ip_hdr(skb); |
| 841 | |
| 842 | prio = ip_tunnel_get_dsfield(iip, skb); |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 843 | use_cache = false; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | fl6->flowi6_tos = RT_TOS(prio); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 847 | fl6->daddr = geneve->remote.sin6.sin6_addr; |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 848 | dst_cache = &geneve->dst_cache; |
| 849 | } |
| 850 | |
| 851 | use_cache = use_cache && !skb->mark; |
| 852 | if (use_cache) { |
| 853 | dst = dst_cache_get_ip6(dst_cache, &fl6->saddr); |
| 854 | if (dst) |
| 855 | return dst; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) { |
| 859 | netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr); |
| 860 | return ERR_PTR(-ENETUNREACH); |
| 861 | } |
| 862 | if (dst->dev == dev) { /* is this necessary? */ |
| 863 | netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr); |
| 864 | dst_release(dst); |
| 865 | return ERR_PTR(-ELOOP); |
| 866 | } |
| 867 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 868 | if (use_cache) |
| 869 | dst_cache_set_ip6(dst_cache, dst, &fl6->saddr); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 870 | return dst; |
| 871 | } |
| 872 | #endif |
| 873 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 874 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
| 875 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) |
| 876 | { |
| 877 | #ifdef __BIG_ENDIAN |
| 878 | vni[0] = (__force __u8)(tun_id >> 16); |
| 879 | vni[1] = (__force __u8)(tun_id >> 8); |
| 880 | vni[2] = (__force __u8)tun_id; |
| 881 | #else |
| 882 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); |
| 883 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); |
| 884 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); |
| 885 | #endif |
| 886 | } |
| 887 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 888 | static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 889 | struct ip_tunnel_info *info) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 890 | { |
| 891 | struct geneve_dev *geneve = netdev_priv(dev); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 892 | struct geneve_sock *gs4 = geneve->sock4; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 893 | struct rtable *rt = NULL; |
John W. Linville | 08399ef | 2015-09-21 10:29:09 -0400 | [diff] [blame] | 894 | const struct iphdr *iip; /* interior IP header */ |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 895 | int err = -EINVAL; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 896 | struct flowi4 fl4; |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 897 | __u8 tos, ttl; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 898 | __be16 sport; |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 899 | __be16 df; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 900 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 901 | u32 flags = geneve->flags; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 902 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 903 | if (geneve->collect_md) { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 904 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 905 | netdev_dbg(dev, "no tunnel metadata\n"); |
| 906 | goto tx_error; |
| 907 | } |
Jiri Benc | 7f9562a | 2015-08-28 20:48:20 +0200 | [diff] [blame] | 908 | if (info && ip_tunnel_info_af(info) != AF_INET) |
| 909 | goto tx_error; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 910 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 911 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 912 | rt = geneve_get_v4_rt(skb, dev, &fl4, info); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 913 | if (IS_ERR(rt)) { |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 914 | err = PTR_ERR(rt); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 915 | goto tx_error; |
| 916 | } |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 917 | |
| 918 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 919 | skb_reset_mac_header(skb); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 920 | |
John W. Linville | 08399ef | 2015-09-21 10:29:09 -0400 | [diff] [blame] | 921 | iip = ip_hdr(skb); |
| 922 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 923 | if (info) { |
| 924 | const struct ip_tunnel_key *key = &info->key; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 925 | u8 *opts = NULL; |
| 926 | u8 vni[3]; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 927 | |
| 928 | tunnel_id_to_vni(key->tun_id, vni); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 929 | if (key->tun_flags & TUNNEL_GENEVE_OPT) |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 930 | opts = ip_tunnel_info_opts(info); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 931 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 932 | if (key->tun_flags & TUNNEL_CSUM) |
| 933 | flags |= GENEVE_F_UDP_CSUM; |
| 934 | else |
| 935 | flags &= ~GENEVE_F_UDP_CSUM; |
| 936 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 937 | err = geneve_build_skb(rt, skb, key->tun_flags, vni, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 938 | info->options_len, opts, flags, xnet); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 939 | if (unlikely(err)) |
| 940 | goto err; |
| 941 | |
John W. Linville | 08399ef | 2015-09-21 10:29:09 -0400 | [diff] [blame] | 942 | tos = ip_tunnel_ecn_encap(key->tos, iip, skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 943 | ttl = key->ttl; |
| 944 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 945 | } else { |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 946 | err = geneve_build_skb(rt, skb, 0, geneve->vni, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 947 | 0, NULL, flags, xnet); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 948 | if (unlikely(err)) |
| 949 | goto err; |
| 950 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 951 | tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 952 | ttl = geneve->ttl; |
| 953 | if (!ttl && IN_MULTICAST(ntohl(fl4.daddr))) |
| 954 | ttl = 1; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 955 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 956 | df = 0; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 957 | } |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 958 | udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr, |
| 959 | tos, ttl, df, sport, geneve->dst_port, |
| 960 | !net_eq(geneve->net, dev_net(geneve->dev)), |
| 961 | !(flags & GENEVE_F_UDP_CSUM)); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 962 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 963 | return NETDEV_TX_OK; |
| 964 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 965 | tx_error: |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 966 | dev_kfree_skb(skb); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 967 | err: |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 968 | if (err == -ELOOP) |
| 969 | dev->stats.collisions++; |
| 970 | else if (err == -ENETUNREACH) |
| 971 | dev->stats.tx_carrier_errors++; |
| 972 | else |
| 973 | dev->stats.tx_errors++; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 974 | return NETDEV_TX_OK; |
| 975 | } |
| 976 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 977 | #if IS_ENABLED(CONFIG_IPV6) |
| 978 | static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, |
| 979 | struct ip_tunnel_info *info) |
| 980 | { |
| 981 | struct geneve_dev *geneve = netdev_priv(dev); |
| 982 | struct geneve_sock *gs6 = geneve->sock6; |
| 983 | struct dst_entry *dst = NULL; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 984 | const struct iphdr *iip; /* interior IP header */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 985 | int err = -EINVAL; |
| 986 | struct flowi6 fl6; |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 987 | __u8 prio, ttl; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 988 | __be16 sport; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 989 | bool xnet = !net_eq(geneve->net, dev_net(geneve->dev)); |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 990 | u32 flags = geneve->flags; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 991 | |
| 992 | if (geneve->collect_md) { |
| 993 | if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) { |
| 994 | netdev_dbg(dev, "no tunnel metadata\n"); |
| 995 | goto tx_error; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | dst = geneve_get_v6_dst(skb, dev, &fl6, info); |
| 1000 | if (IS_ERR(dst)) { |
| 1001 | err = PTR_ERR(dst); |
| 1002 | goto tx_error; |
| 1003 | } |
| 1004 | |
| 1005 | sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true); |
| 1006 | skb_reset_mac_header(skb); |
| 1007 | |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 1008 | iip = ip_hdr(skb); |
| 1009 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1010 | if (info) { |
| 1011 | const struct ip_tunnel_key *key = &info->key; |
| 1012 | u8 *opts = NULL; |
| 1013 | u8 vni[3]; |
| 1014 | |
| 1015 | tunnel_id_to_vni(key->tun_id, vni); |
| 1016 | if (key->tun_flags & TUNNEL_GENEVE_OPT) |
| 1017 | opts = ip_tunnel_info_opts(info); |
| 1018 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1019 | if (key->tun_flags & TUNNEL_CSUM) |
Jesse Gross | 35e2d11 | 2016-01-20 16:22:47 -0800 | [diff] [blame] | 1020 | flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1021 | else |
Jesse Gross | 35e2d11 | 2016-01-20 16:22:47 -0800 | [diff] [blame] | 1022 | flags |= GENEVE_F_UDP_ZERO_CSUM6_TX; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1023 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1024 | err = geneve6_build_skb(dst, skb, key->tun_flags, vni, |
| 1025 | info->options_len, opts, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1026 | flags, xnet); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1027 | if (unlikely(err)) |
| 1028 | goto err; |
| 1029 | |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 1030 | prio = ip_tunnel_ecn_encap(key->tos, iip, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1031 | ttl = key->ttl; |
| 1032 | } else { |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1033 | err = geneve6_build_skb(dst, skb, 0, geneve->vni, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1034 | 0, NULL, flags, xnet); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1035 | if (unlikely(err)) |
| 1036 | goto err; |
| 1037 | |
John W. Linville | 3a56f86 | 2015-10-26 17:01:45 -0400 | [diff] [blame] | 1038 | prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1039 | ttl = geneve->ttl; |
| 1040 | if (!ttl && ipv6_addr_is_multicast(&fl6.daddr)) |
| 1041 | ttl = 1; |
| 1042 | ttl = ttl ? : ip6_dst_hoplimit(dst); |
| 1043 | } |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 1044 | udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev, |
| 1045 | &fl6.saddr, &fl6.daddr, prio, ttl, |
| 1046 | sport, geneve->dst_port, |
| 1047 | !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX)); |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1048 | return NETDEV_TX_OK; |
| 1049 | |
| 1050 | tx_error: |
| 1051 | dev_kfree_skb(skb); |
| 1052 | err: |
| 1053 | if (err == -ELOOP) |
| 1054 | dev->stats.collisions++; |
| 1055 | else if (err == -ENETUNREACH) |
| 1056 | dev->stats.tx_carrier_errors++; |
| 1057 | else |
| 1058 | dev->stats.tx_errors++; |
| 1059 | return NETDEV_TX_OK; |
| 1060 | } |
| 1061 | #endif |
| 1062 | |
| 1063 | static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1064 | { |
| 1065 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1066 | struct ip_tunnel_info *info = NULL; |
| 1067 | |
| 1068 | if (geneve->collect_md) |
| 1069 | info = skb_tunnel_info(skb); |
| 1070 | |
| 1071 | #if IS_ENABLED(CONFIG_IPV6) |
| 1072 | if ((info && ip_tunnel_info_af(info) == AF_INET6) || |
| 1073 | (!info && geneve->remote.sa.sa_family == AF_INET6)) |
| 1074 | return geneve6_xmit_skb(skb, dev, info); |
| 1075 | #endif |
| 1076 | return geneve_xmit_skb(skb, dev, info); |
| 1077 | } |
| 1078 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1079 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 1080 | { |
| 1081 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1082 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1083 | struct rtable *rt; |
| 1084 | struct flowi4 fl4; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1085 | #if IS_ENABLED(CONFIG_IPV6) |
| 1086 | struct dst_entry *dst; |
| 1087 | struct flowi6 fl6; |
| 1088 | #endif |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1089 | |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1090 | if (ip_tunnel_info_af(info) == AF_INET) { |
| 1091 | rt = geneve_get_v4_rt(skb, dev, &fl4, info); |
| 1092 | if (IS_ERR(rt)) |
| 1093 | return PTR_ERR(rt); |
| 1094 | |
| 1095 | ip_rt_put(rt); |
| 1096 | info->key.u.ipv4.src = fl4.saddr; |
| 1097 | #if IS_ENABLED(CONFIG_IPV6) |
| 1098 | } else if (ip_tunnel_info_af(info) == AF_INET6) { |
| 1099 | dst = geneve_get_v6_dst(skb, dev, &fl6, info); |
| 1100 | if (IS_ERR(dst)) |
| 1101 | return PTR_ERR(dst); |
| 1102 | |
| 1103 | dst_release(dst); |
| 1104 | info->key.u.ipv6.src = fl6.saddr; |
| 1105 | #endif |
| 1106 | } else { |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1107 | return -EINVAL; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1108 | } |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1109 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1110 | info->key.tp_src = udp_flow_src_port(geneve->net, skb, |
| 1111 | 1, USHRT_MAX, true); |
| 1112 | info->key.tp_dst = geneve->dst_port; |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1116 | static const struct net_device_ops geneve_netdev_ops = { |
| 1117 | .ndo_init = geneve_init, |
| 1118 | .ndo_uninit = geneve_uninit, |
| 1119 | .ndo_open = geneve_open, |
| 1120 | .ndo_stop = geneve_stop, |
| 1121 | .ndo_start_xmit = geneve_xmit, |
| 1122 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
| 1123 | .ndo_change_mtu = eth_change_mtu, |
| 1124 | .ndo_validate_addr = eth_validate_addr, |
| 1125 | .ndo_set_mac_address = eth_mac_addr, |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1126 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1127 | }; |
| 1128 | |
| 1129 | static void geneve_get_drvinfo(struct net_device *dev, |
| 1130 | struct ethtool_drvinfo *drvinfo) |
| 1131 | { |
| 1132 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); |
| 1133 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); |
| 1134 | } |
| 1135 | |
| 1136 | static const struct ethtool_ops geneve_ethtool_ops = { |
| 1137 | .get_drvinfo = geneve_get_drvinfo, |
| 1138 | .get_link = ethtool_op_get_link, |
| 1139 | }; |
| 1140 | |
| 1141 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1142 | static struct device_type geneve_type = { |
| 1143 | .name = "geneve", |
| 1144 | }; |
| 1145 | |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1146 | /* Calls the ndo_add_geneve_port of the caller in order to |
| 1147 | * supply the listening GENEVE udp ports. Callers are expected |
| 1148 | * to implement the ndo_add_geneve_port. |
| 1149 | */ |
| 1150 | void geneve_get_rx_port(struct net_device *dev) |
| 1151 | { |
| 1152 | struct net *net = dev_net(dev); |
| 1153 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1154 | struct geneve_sock *gs; |
| 1155 | sa_family_t sa_family; |
| 1156 | struct sock *sk; |
| 1157 | __be16 port; |
| 1158 | |
| 1159 | rcu_read_lock(); |
| 1160 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { |
| 1161 | sk = gs->sock->sk; |
| 1162 | sa_family = sk->sk_family; |
| 1163 | port = inet_sk(sk)->inet_sport; |
| 1164 | dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port); |
| 1165 | } |
| 1166 | rcu_read_unlock(); |
| 1167 | } |
| 1168 | EXPORT_SYMBOL_GPL(geneve_get_rx_port); |
| 1169 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1170 | /* Initialize the device structure. */ |
| 1171 | static void geneve_setup(struct net_device *dev) |
| 1172 | { |
| 1173 | ether_setup(dev); |
| 1174 | |
| 1175 | dev->netdev_ops = &geneve_netdev_ops; |
| 1176 | dev->ethtool_ops = &geneve_ethtool_ops; |
| 1177 | dev->destructor = free_netdev; |
| 1178 | |
| 1179 | SET_NETDEV_DEVTYPE(dev, &geneve_type); |
| 1180 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1181 | dev->features |= NETIF_F_LLTX; |
| 1182 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
| 1183 | dev->features |= NETIF_F_RXCSUM; |
| 1184 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1185 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1186 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
| 1187 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1188 | |
| 1189 | netif_keep_dst(dev); |
Phil Sutter | ed961ac | 2015-08-18 10:30:31 +0200 | [diff] [blame] | 1190 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Pravin B Shelar | 87cd3dc | 2015-08-26 23:46:48 -0700 | [diff] [blame] | 1191 | eth_hw_addr_random(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { |
| 1195 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, |
| 1196 | [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1197 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1198 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1199 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1200 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1201 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1202 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
| 1203 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 1204 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1208 | { |
| 1209 | if (tb[IFLA_ADDRESS]) { |
| 1210 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1211 | return -EINVAL; |
| 1212 | |
| 1213 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1214 | return -EADDRNOTAVAIL; |
| 1215 | } |
| 1216 | |
| 1217 | if (!data) |
| 1218 | return -EINVAL; |
| 1219 | |
| 1220 | if (data[IFLA_GENEVE_ID]) { |
| 1221 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1222 | |
| 1223 | if (vni >= GENEVE_VID_MASK) |
| 1224 | return -ERANGE; |
| 1225 | } |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1230 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
| 1231 | __be16 dst_port, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1232 | union geneve_addr *remote, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1233 | u8 vni[], |
| 1234 | bool *tun_on_same_port, |
| 1235 | bool *tun_collect_md) |
| 1236 | { |
| 1237 | struct geneve_dev *geneve, *t; |
| 1238 | |
| 1239 | *tun_on_same_port = false; |
| 1240 | *tun_collect_md = false; |
| 1241 | t = NULL; |
| 1242 | list_for_each_entry(geneve, &gn->geneve_list, next) { |
| 1243 | if (geneve->dst_port == dst_port) { |
| 1244 | *tun_collect_md = geneve->collect_md; |
| 1245 | *tun_on_same_port = true; |
| 1246 | } |
| 1247 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1248 | !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) && |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1249 | dst_port == geneve->dst_port) |
| 1250 | t = geneve; |
| 1251 | } |
| 1252 | return t; |
| 1253 | } |
| 1254 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1255 | static int geneve_configure(struct net *net, struct net_device *dev, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1256 | union geneve_addr *remote, |
| 1257 | __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1258 | bool metadata, u32 flags) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1259 | { |
| 1260 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1261 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
| 1262 | bool tun_collect_md, tun_on_same_port; |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1263 | int err, encap_len; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1264 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1265 | if (!remote) |
| 1266 | return -EINVAL; |
| 1267 | if (metadata && |
| 1268 | (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl)) |
| 1269 | return -EINVAL; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1270 | |
| 1271 | geneve->net = net; |
| 1272 | geneve->dev = dev; |
| 1273 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1274 | geneve->vni[0] = (vni & 0x00ff0000) >> 16; |
| 1275 | geneve->vni[1] = (vni & 0x0000ff00) >> 8; |
| 1276 | geneve->vni[2] = vni & 0x000000ff; |
| 1277 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1278 | if ((remote->sa.sa_family == AF_INET && |
| 1279 | IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) || |
| 1280 | (remote->sa.sa_family == AF_INET6 && |
| 1281 | ipv6_addr_is_multicast(&remote->sin6.sin6_addr))) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1282 | return -EINVAL; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1283 | geneve->remote = *remote; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1284 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1285 | geneve->ttl = ttl; |
| 1286 | geneve->tos = tos; |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1287 | geneve->dst_port = dst_port; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1288 | geneve->collect_md = metadata; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1289 | geneve->flags = flags; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1290 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1291 | t = geneve_find_dev(gn, dst_port, remote, geneve->vni, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1292 | &tun_on_same_port, &tun_collect_md); |
| 1293 | if (t) |
| 1294 | return -EBUSY; |
| 1295 | |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1296 | /* make enough headroom for basic scenario */ |
| 1297 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; |
| 1298 | if (remote->sa.sa_family == AF_INET) |
| 1299 | encap_len += sizeof(struct iphdr); |
| 1300 | else |
| 1301 | encap_len += sizeof(struct ipv6hdr); |
| 1302 | dev->needed_headroom = encap_len + ETH_HLEN; |
| 1303 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1304 | if (metadata) { |
| 1305 | if (tun_on_same_port) |
| 1306 | return -EPERM; |
| 1307 | } else { |
| 1308 | if (tun_collect_md) |
| 1309 | return -EPERM; |
| 1310 | } |
| 1311 | |
Paolo Abeni | 468dfff | 2016-02-12 15:43:58 +0100 | [diff] [blame^] | 1312 | dst_cache_reset(&geneve->dst_cache); |
| 1313 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1314 | err = register_netdevice(dev); |
| 1315 | if (err) |
| 1316 | return err; |
| 1317 | |
| 1318 | list_add(&geneve->next, &gn->geneve_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
| 1321 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1322 | static int geneve_newlink(struct net *net, struct net_device *dev, |
| 1323 | struct nlattr *tb[], struct nlattr *data[]) |
| 1324 | { |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1325 | __be16 dst_port = htons(GENEVE_UDP_PORT); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1326 | __u8 ttl = 0, tos = 0; |
| 1327 | bool metadata = false; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1328 | union geneve_addr remote = geneve_remote_unspec; |
Jesse Gross | e277de5 | 2015-10-16 16:36:00 -0700 | [diff] [blame] | 1329 | __u32 vni = 0; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1330 | u32 flags = 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1331 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1332 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) |
| 1333 | return -EINVAL; |
| 1334 | |
| 1335 | if (data[IFLA_GENEVE_REMOTE]) { |
| 1336 | remote.sa.sa_family = AF_INET; |
| 1337 | remote.sin.sin_addr.s_addr = |
| 1338 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); |
| 1339 | } |
| 1340 | |
| 1341 | if (data[IFLA_GENEVE_REMOTE6]) { |
| 1342 | if (!IS_ENABLED(CONFIG_IPV6)) |
| 1343 | return -EPFNOSUPPORT; |
| 1344 | |
| 1345 | remote.sa.sa_family = AF_INET6; |
| 1346 | remote.sin6.sin6_addr = |
| 1347 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); |
| 1348 | |
| 1349 | if (ipv6_addr_type(&remote.sin6.sin6_addr) & |
| 1350 | IPV6_ADDR_LINKLOCAL) { |
| 1351 | netdev_dbg(dev, "link-local remote is unsupported\n"); |
| 1352 | return -EINVAL; |
| 1353 | } |
| 1354 | } |
| 1355 | |
Jesse Gross | e277de5 | 2015-10-16 16:36:00 -0700 | [diff] [blame] | 1356 | if (data[IFLA_GENEVE_ID]) |
| 1357 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1358 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1359 | if (data[IFLA_GENEVE_TTL]) |
| 1360 | ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
| 1361 | |
| 1362 | if (data[IFLA_GENEVE_TOS]) |
| 1363 | tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
| 1364 | |
| 1365 | if (data[IFLA_GENEVE_PORT]) |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1366 | dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1367 | |
| 1368 | if (data[IFLA_GENEVE_COLLECT_METADATA]) |
| 1369 | metadata = true; |
| 1370 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1371 | if (data[IFLA_GENEVE_UDP_CSUM] && |
| 1372 | nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
| 1373 | flags |= GENEVE_F_UDP_CSUM; |
| 1374 | |
| 1375 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] && |
| 1376 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) |
| 1377 | flags |= GENEVE_F_UDP_ZERO_CSUM6_TX; |
| 1378 | |
| 1379 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] && |
| 1380 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) |
| 1381 | flags |= GENEVE_F_UDP_ZERO_CSUM6_RX; |
| 1382 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1383 | return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1384 | metadata, flags); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1387 | static void geneve_dellink(struct net_device *dev, struct list_head *head) |
| 1388 | { |
| 1389 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1390 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1391 | list_del(&geneve->next); |
| 1392 | unregister_netdevice_queue(dev, head); |
| 1393 | } |
| 1394 | |
| 1395 | static size_t geneve_get_size(const struct net_device *dev) |
| 1396 | { |
| 1397 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1398 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1399 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1400 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1401 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1402 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1403 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
| 1404 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ |
| 1405 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1406 | 0; |
| 1407 | } |
| 1408 | |
| 1409 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1410 | { |
| 1411 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1412 | __u32 vni; |
| 1413 | |
| 1414 | vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2]; |
| 1415 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) |
| 1416 | goto nla_put_failure; |
| 1417 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1418 | if (geneve->remote.sa.sa_family == AF_INET) { |
| 1419 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, |
| 1420 | geneve->remote.sin.sin_addr.s_addr)) |
| 1421 | goto nla_put_failure; |
| 1422 | #if IS_ENABLED(CONFIG_IPV6) |
| 1423 | } else { |
| 1424 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, |
| 1425 | &geneve->remote.sin6.sin6_addr)) |
| 1426 | goto nla_put_failure; |
| 1427 | #endif |
| 1428 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1429 | |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1430 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) || |
| 1431 | nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos)) |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1432 | goto nla_put_failure; |
| 1433 | |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1434 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port)) |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1435 | goto nla_put_failure; |
| 1436 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1437 | if (geneve->collect_md) { |
| 1438 | if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) |
| 1439 | goto nla_put_failure; |
| 1440 | } |
| 1441 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1442 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
| 1443 | !!(geneve->flags & GENEVE_F_UDP_CSUM)) || |
| 1444 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
| 1445 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) || |
| 1446 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, |
| 1447 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX))) |
| 1448 | goto nla_put_failure; |
| 1449 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1450 | return 0; |
| 1451 | |
| 1452 | nla_put_failure: |
| 1453 | return -EMSGSIZE; |
| 1454 | } |
| 1455 | |
| 1456 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { |
| 1457 | .kind = "geneve", |
| 1458 | .maxtype = IFLA_GENEVE_MAX, |
| 1459 | .policy = geneve_policy, |
| 1460 | .priv_size = sizeof(struct geneve_dev), |
| 1461 | .setup = geneve_setup, |
| 1462 | .validate = geneve_validate, |
| 1463 | .newlink = geneve_newlink, |
| 1464 | .dellink = geneve_dellink, |
| 1465 | .get_size = geneve_get_size, |
| 1466 | .fill_info = geneve_fill_info, |
| 1467 | }; |
| 1468 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1469 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
| 1470 | u8 name_assign_type, u16 dst_port) |
| 1471 | { |
| 1472 | struct nlattr *tb[IFLA_MAX + 1]; |
| 1473 | struct net_device *dev; |
| 1474 | int err; |
| 1475 | |
| 1476 | memset(tb, 0, sizeof(tb)); |
| 1477 | dev = rtnl_create_link(net, name, name_assign_type, |
| 1478 | &geneve_link_ops, tb); |
| 1479 | if (IS_ERR(dev)) |
| 1480 | return dev; |
| 1481 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1482 | err = geneve_configure(net, dev, &geneve_remote_unspec, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1483 | 0, 0, 0, htons(dst_port), true, 0); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1484 | if (err) { |
| 1485 | free_netdev(dev); |
| 1486 | return ERR_PTR(err); |
| 1487 | } |
| 1488 | return dev; |
| 1489 | } |
| 1490 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); |
| 1491 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1492 | static __net_init int geneve_init_net(struct net *net) |
| 1493 | { |
| 1494 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1495 | |
| 1496 | INIT_LIST_HEAD(&gn->geneve_list); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1497 | INIT_LIST_HEAD(&gn->sock_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static void __net_exit geneve_exit_net(struct net *net) |
| 1502 | { |
| 1503 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1504 | struct geneve_dev *geneve, *next; |
| 1505 | struct net_device *dev, *aux; |
| 1506 | LIST_HEAD(list); |
| 1507 | |
| 1508 | rtnl_lock(); |
| 1509 | |
| 1510 | /* gather any geneve devices that were moved into this ns */ |
| 1511 | for_each_netdev_safe(net, dev, aux) |
| 1512 | if (dev->rtnl_link_ops == &geneve_link_ops) |
| 1513 | unregister_netdevice_queue(dev, &list); |
| 1514 | |
| 1515 | /* now gather any other geneve devices that were created in this ns */ |
| 1516 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { |
| 1517 | /* If geneve->dev is in the same netns, it was already added |
| 1518 | * to the list by the previous loop. |
| 1519 | */ |
| 1520 | if (!net_eq(dev_net(geneve->dev), net)) |
| 1521 | unregister_netdevice_queue(geneve->dev, &list); |
| 1522 | } |
| 1523 | |
| 1524 | /* unregister the devices gathered above */ |
| 1525 | unregister_netdevice_many(&list); |
| 1526 | rtnl_unlock(); |
| 1527 | } |
| 1528 | |
| 1529 | static struct pernet_operations geneve_net_ops = { |
| 1530 | .init = geneve_init_net, |
| 1531 | .exit = geneve_exit_net, |
| 1532 | .id = &geneve_net_id, |
| 1533 | .size = sizeof(struct geneve_net), |
| 1534 | }; |
| 1535 | |
| 1536 | static int __init geneve_init_module(void) |
| 1537 | { |
| 1538 | int rc; |
| 1539 | |
| 1540 | rc = register_pernet_subsys(&geneve_net_ops); |
| 1541 | if (rc) |
| 1542 | goto out1; |
| 1543 | |
| 1544 | rc = rtnl_link_register(&geneve_link_ops); |
| 1545 | if (rc) |
| 1546 | goto out2; |
| 1547 | |
| 1548 | return 0; |
| 1549 | out2: |
| 1550 | unregister_pernet_subsys(&geneve_net_ops); |
| 1551 | out1: |
| 1552 | return rc; |
| 1553 | } |
| 1554 | late_initcall(geneve_init_module); |
| 1555 | |
| 1556 | static void __exit geneve_cleanup_module(void) |
| 1557 | { |
| 1558 | rtnl_link_unregister(&geneve_link_ops); |
| 1559 | unregister_pernet_subsys(&geneve_net_ops); |
| 1560 | } |
| 1561 | module_exit(geneve_cleanup_module); |
| 1562 | |
| 1563 | MODULE_LICENSE("GPL"); |
| 1564 | MODULE_VERSION(GENEVE_NETDEV_VER); |
| 1565 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); |
| 1566 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); |
| 1567 | MODULE_ALIAS_RTNL_LINK("geneve"); |