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