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