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 | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1042 | static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict) |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1043 | { |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1044 | /* The max_mtu calculation does not take account of GENEVE |
| 1045 | * options, to avoid excluding potentially valid |
| 1046 | * configurations. |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1047 | */ |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1048 | int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - sizeof(struct iphdr) |
| 1049 | - dev->hard_header_len; |
| 1050 | |
| 1051 | if (new_mtu < 68) |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1052 | return -EINVAL; |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (new_mtu > max_mtu) { |
| 1055 | if (strict) |
| 1056 | return -EINVAL; |
| 1057 | |
| 1058 | new_mtu = max_mtu; |
| 1059 | } |
| 1060 | |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1061 | dev->mtu = new_mtu; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1065 | static int geneve_change_mtu(struct net_device *dev, int new_mtu) |
| 1066 | { |
| 1067 | return __geneve_change_mtu(dev, new_mtu, true); |
| 1068 | } |
| 1069 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1070 | static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 1071 | { |
| 1072 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1073 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1074 | struct rtable *rt; |
| 1075 | struct flowi4 fl4; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1076 | #if IS_ENABLED(CONFIG_IPV6) |
| 1077 | struct dst_entry *dst; |
| 1078 | struct flowi6 fl6; |
| 1079 | #endif |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1080 | |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1081 | if (ip_tunnel_info_af(info) == AF_INET) { |
| 1082 | rt = geneve_get_v4_rt(skb, dev, &fl4, info); |
| 1083 | if (IS_ERR(rt)) |
| 1084 | return PTR_ERR(rt); |
| 1085 | |
| 1086 | ip_rt_put(rt); |
| 1087 | info->key.u.ipv4.src = fl4.saddr; |
| 1088 | #if IS_ENABLED(CONFIG_IPV6) |
| 1089 | } else if (ip_tunnel_info_af(info) == AF_INET6) { |
| 1090 | dst = geneve_get_v6_dst(skb, dev, &fl6, info); |
| 1091 | if (IS_ERR(dst)) |
| 1092 | return PTR_ERR(dst); |
| 1093 | |
| 1094 | dst_release(dst); |
| 1095 | info->key.u.ipv6.src = fl6.saddr; |
| 1096 | #endif |
| 1097 | } else { |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1098 | return -EINVAL; |
John W. Linville | b8812fa | 2015-10-27 09:49:00 -0400 | [diff] [blame] | 1099 | } |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1100 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1101 | info->key.tp_src = udp_flow_src_port(geneve->net, skb, |
| 1102 | 1, USHRT_MAX, true); |
| 1103 | info->key.tp_dst = geneve->dst_port; |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1107 | static const struct net_device_ops geneve_netdev_ops = { |
| 1108 | .ndo_init = geneve_init, |
| 1109 | .ndo_uninit = geneve_uninit, |
| 1110 | .ndo_open = geneve_open, |
| 1111 | .ndo_stop = geneve_stop, |
| 1112 | .ndo_start_xmit = geneve_xmit, |
| 1113 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
David Wragg | 55e5bfb | 2016-02-10 00:05:57 +0000 | [diff] [blame] | 1114 | .ndo_change_mtu = geneve_change_mtu, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1115 | .ndo_validate_addr = eth_validate_addr, |
| 1116 | .ndo_set_mac_address = eth_mac_addr, |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 1117 | .ndo_fill_metadata_dst = geneve_fill_metadata_dst, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1118 | }; |
| 1119 | |
| 1120 | static void geneve_get_drvinfo(struct net_device *dev, |
| 1121 | struct ethtool_drvinfo *drvinfo) |
| 1122 | { |
| 1123 | strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version)); |
| 1124 | strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver)); |
| 1125 | } |
| 1126 | |
| 1127 | static const struct ethtool_ops geneve_ethtool_ops = { |
| 1128 | .get_drvinfo = geneve_get_drvinfo, |
| 1129 | .get_link = ethtool_op_get_link, |
| 1130 | }; |
| 1131 | |
| 1132 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1133 | static struct device_type geneve_type = { |
| 1134 | .name = "geneve", |
| 1135 | }; |
| 1136 | |
Singhai, Anjali | 05ca402 | 2015-12-14 12:21:20 -0800 | [diff] [blame] | 1137 | /* Calls the ndo_add_geneve_port of the caller in order to |
| 1138 | * supply the listening GENEVE udp ports. Callers are expected |
| 1139 | * to implement the ndo_add_geneve_port. |
| 1140 | */ |
| 1141 | void geneve_get_rx_port(struct net_device *dev) |
| 1142 | { |
| 1143 | struct net *net = dev_net(dev); |
| 1144 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1145 | struct geneve_sock *gs; |
| 1146 | sa_family_t sa_family; |
| 1147 | struct sock *sk; |
| 1148 | __be16 port; |
| 1149 | |
| 1150 | rcu_read_lock(); |
| 1151 | list_for_each_entry_rcu(gs, &gn->sock_list, list) { |
| 1152 | sk = gs->sock->sk; |
| 1153 | sa_family = sk->sk_family; |
| 1154 | port = inet_sk(sk)->inet_sport; |
| 1155 | dev->netdev_ops->ndo_add_geneve_port(dev, sa_family, port); |
| 1156 | } |
| 1157 | rcu_read_unlock(); |
| 1158 | } |
| 1159 | EXPORT_SYMBOL_GPL(geneve_get_rx_port); |
| 1160 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1161 | /* Initialize the device structure. */ |
| 1162 | static void geneve_setup(struct net_device *dev) |
| 1163 | { |
| 1164 | ether_setup(dev); |
| 1165 | |
| 1166 | dev->netdev_ops = &geneve_netdev_ops; |
| 1167 | dev->ethtool_ops = &geneve_ethtool_ops; |
| 1168 | dev->destructor = free_netdev; |
| 1169 | |
| 1170 | SET_NETDEV_DEVTYPE(dev, &geneve_type); |
| 1171 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1172 | dev->features |= NETIF_F_LLTX; |
| 1173 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
| 1174 | dev->features |= NETIF_F_RXCSUM; |
| 1175 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1176 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1177 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
| 1178 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1179 | |
| 1180 | netif_keep_dst(dev); |
Jiri Benc | fc41cdb | 2016-02-17 15:31:35 +0100 | [diff] [blame^] | 1181 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Phil Sutter | ed961ac | 2015-08-18 10:30:31 +0200 | [diff] [blame] | 1182 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Pravin B Shelar | 87cd3dc | 2015-08-26 23:46:48 -0700 | [diff] [blame] | 1183 | eth_hw_addr_random(dev); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = { |
| 1187 | [IFLA_GENEVE_ID] = { .type = NLA_U32 }, |
| 1188 | [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1189 | [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) }, |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1190 | [IFLA_GENEVE_TTL] = { .type = NLA_U8 }, |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1191 | [IFLA_GENEVE_TOS] = { .type = NLA_U8 }, |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1192 | [IFLA_GENEVE_PORT] = { .type = NLA_U16 }, |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1193 | [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1194 | [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 }, |
| 1195 | [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 1196 | [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1197 | }; |
| 1198 | |
| 1199 | static int geneve_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1200 | { |
| 1201 | if (tb[IFLA_ADDRESS]) { |
| 1202 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1203 | return -EINVAL; |
| 1204 | |
| 1205 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1206 | return -EADDRNOTAVAIL; |
| 1207 | } |
| 1208 | |
| 1209 | if (!data) |
| 1210 | return -EINVAL; |
| 1211 | |
| 1212 | if (data[IFLA_GENEVE_ID]) { |
| 1213 | __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
| 1214 | |
| 1215 | if (vni >= GENEVE_VID_MASK) |
| 1216 | return -ERANGE; |
| 1217 | } |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1222 | static struct geneve_dev *geneve_find_dev(struct geneve_net *gn, |
| 1223 | __be16 dst_port, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1224 | union geneve_addr *remote, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1225 | u8 vni[], |
| 1226 | bool *tun_on_same_port, |
| 1227 | bool *tun_collect_md) |
| 1228 | { |
| 1229 | struct geneve_dev *geneve, *t; |
| 1230 | |
| 1231 | *tun_on_same_port = false; |
| 1232 | *tun_collect_md = false; |
| 1233 | t = NULL; |
| 1234 | list_for_each_entry(geneve, &gn->geneve_list, next) { |
| 1235 | if (geneve->dst_port == dst_port) { |
| 1236 | *tun_collect_md = geneve->collect_md; |
| 1237 | *tun_on_same_port = true; |
| 1238 | } |
| 1239 | if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) && |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1240 | !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) && |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1241 | dst_port == geneve->dst_port) |
| 1242 | t = geneve; |
| 1243 | } |
| 1244 | return t; |
| 1245 | } |
| 1246 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1247 | static int geneve_configure(struct net *net, struct net_device *dev, |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1248 | union geneve_addr *remote, |
| 1249 | __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1250 | bool metadata, u32 flags) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1251 | { |
| 1252 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1253 | struct geneve_dev *t, *geneve = netdev_priv(dev); |
| 1254 | bool tun_collect_md, tun_on_same_port; |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1255 | int err, encap_len; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1256 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1257 | if (!remote) |
| 1258 | return -EINVAL; |
| 1259 | if (metadata && |
| 1260 | (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl)) |
| 1261 | return -EINVAL; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1262 | |
| 1263 | geneve->net = net; |
| 1264 | geneve->dev = dev; |
| 1265 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1266 | geneve->vni[0] = (vni & 0x00ff0000) >> 16; |
| 1267 | geneve->vni[1] = (vni & 0x0000ff00) >> 8; |
| 1268 | geneve->vni[2] = vni & 0x000000ff; |
| 1269 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1270 | if ((remote->sa.sa_family == AF_INET && |
| 1271 | IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) || |
| 1272 | (remote->sa.sa_family == AF_INET6 && |
| 1273 | ipv6_addr_is_multicast(&remote->sin6.sin6_addr))) |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1274 | return -EINVAL; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1275 | geneve->remote = *remote; |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1276 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1277 | geneve->ttl = ttl; |
| 1278 | geneve->tos = tos; |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1279 | geneve->dst_port = dst_port; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1280 | geneve->collect_md = metadata; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1281 | geneve->flags = flags; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1282 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1283 | t = geneve_find_dev(gn, dst_port, remote, geneve->vni, |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1284 | &tun_on_same_port, &tun_collect_md); |
| 1285 | if (t) |
| 1286 | return -EBUSY; |
| 1287 | |
Paolo Abeni | 184fc8b | 2015-12-23 16:54:27 +0100 | [diff] [blame] | 1288 | /* make enough headroom for basic scenario */ |
| 1289 | encap_len = GENEVE_BASE_HLEN + ETH_HLEN; |
| 1290 | if (remote->sa.sa_family == AF_INET) |
| 1291 | encap_len += sizeof(struct iphdr); |
| 1292 | else |
| 1293 | encap_len += sizeof(struct ipv6hdr); |
| 1294 | dev->needed_headroom = encap_len + ETH_HLEN; |
| 1295 | |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1296 | if (metadata) { |
| 1297 | if (tun_on_same_port) |
| 1298 | return -EPERM; |
| 1299 | } else { |
| 1300 | if (tun_collect_md) |
| 1301 | return -EPERM; |
| 1302 | } |
| 1303 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1304 | err = register_netdevice(dev); |
| 1305 | if (err) |
| 1306 | return err; |
| 1307 | |
| 1308 | list_add(&geneve->next, &gn->geneve_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1312 | static int geneve_newlink(struct net *net, struct net_device *dev, |
| 1313 | struct nlattr *tb[], struct nlattr *data[]) |
| 1314 | { |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1315 | __be16 dst_port = htons(GENEVE_UDP_PORT); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1316 | __u8 ttl = 0, tos = 0; |
| 1317 | bool metadata = false; |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1318 | union geneve_addr remote = geneve_remote_unspec; |
Jesse Gross | e277de5 | 2015-10-16 16:36:00 -0700 | [diff] [blame] | 1319 | __u32 vni = 0; |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1320 | u32 flags = 0; |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1321 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1322 | if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6]) |
| 1323 | return -EINVAL; |
| 1324 | |
| 1325 | if (data[IFLA_GENEVE_REMOTE]) { |
| 1326 | remote.sa.sa_family = AF_INET; |
| 1327 | remote.sin.sin_addr.s_addr = |
| 1328 | nla_get_in_addr(data[IFLA_GENEVE_REMOTE]); |
| 1329 | } |
| 1330 | |
| 1331 | if (data[IFLA_GENEVE_REMOTE6]) { |
| 1332 | if (!IS_ENABLED(CONFIG_IPV6)) |
| 1333 | return -EPFNOSUPPORT; |
| 1334 | |
| 1335 | remote.sa.sa_family = AF_INET6; |
| 1336 | remote.sin6.sin6_addr = |
| 1337 | nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]); |
| 1338 | |
| 1339 | if (ipv6_addr_type(&remote.sin6.sin6_addr) & |
| 1340 | IPV6_ADDR_LINKLOCAL) { |
| 1341 | netdev_dbg(dev, "link-local remote is unsupported\n"); |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Jesse Gross | e277de5 | 2015-10-16 16:36:00 -0700 | [diff] [blame] | 1346 | if (data[IFLA_GENEVE_ID]) |
| 1347 | vni = nla_get_u32(data[IFLA_GENEVE_ID]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1348 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1349 | if (data[IFLA_GENEVE_TTL]) |
| 1350 | ttl = nla_get_u8(data[IFLA_GENEVE_TTL]); |
| 1351 | |
| 1352 | if (data[IFLA_GENEVE_TOS]) |
| 1353 | tos = nla_get_u8(data[IFLA_GENEVE_TOS]); |
| 1354 | |
| 1355 | if (data[IFLA_GENEVE_PORT]) |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1356 | dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1357 | |
| 1358 | if (data[IFLA_GENEVE_COLLECT_METADATA]) |
| 1359 | metadata = true; |
| 1360 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1361 | if (data[IFLA_GENEVE_UDP_CSUM] && |
| 1362 | nla_get_u8(data[IFLA_GENEVE_UDP_CSUM])) |
| 1363 | flags |= GENEVE_F_UDP_CSUM; |
| 1364 | |
| 1365 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] && |
| 1366 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX])) |
| 1367 | flags |= GENEVE_F_UDP_ZERO_CSUM6_TX; |
| 1368 | |
| 1369 | if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] && |
| 1370 | nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])) |
| 1371 | flags |= GENEVE_F_UDP_ZERO_CSUM6_RX; |
| 1372 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1373 | return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1374 | metadata, flags); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1377 | static void geneve_dellink(struct net_device *dev, struct list_head *head) |
| 1378 | { |
| 1379 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1380 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1381 | list_del(&geneve->next); |
| 1382 | unregister_netdevice_queue(dev, head); |
| 1383 | } |
| 1384 | |
| 1385 | static size_t geneve_get_size(const struct net_device *dev) |
| 1386 | { |
| 1387 | return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */ |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1388 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */ |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1389 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */ |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1390 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */ |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1391 | nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */ |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1392 | nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */ |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1393 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */ |
| 1394 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */ |
| 1395 | nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */ |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1396 | 0; |
| 1397 | } |
| 1398 | |
| 1399 | static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1400 | { |
| 1401 | struct geneve_dev *geneve = netdev_priv(dev); |
| 1402 | __u32 vni; |
| 1403 | |
| 1404 | vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2]; |
| 1405 | if (nla_put_u32(skb, IFLA_GENEVE_ID, vni)) |
| 1406 | goto nla_put_failure; |
| 1407 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1408 | if (geneve->remote.sa.sa_family == AF_INET) { |
| 1409 | if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE, |
| 1410 | geneve->remote.sin.sin_addr.s_addr)) |
| 1411 | goto nla_put_failure; |
| 1412 | #if IS_ENABLED(CONFIG_IPV6) |
| 1413 | } else { |
| 1414 | if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6, |
| 1415 | &geneve->remote.sin6.sin6_addr)) |
| 1416 | goto nla_put_failure; |
| 1417 | #endif |
| 1418 | } |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1419 | |
John W. Linville | d895112 | 2015-06-01 15:51:35 -0400 | [diff] [blame] | 1420 | if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) || |
| 1421 | nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos)) |
John W. Linville | 8760ce5 | 2015-06-01 15:51:34 -0400 | [diff] [blame] | 1422 | goto nla_put_failure; |
| 1423 | |
John W. Linville | 7bbe33f | 2015-09-22 13:09:32 -0400 | [diff] [blame] | 1424 | if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port)) |
Pravin B Shelar | cd7918b | 2015-08-26 23:46:51 -0700 | [diff] [blame] | 1425 | goto nla_put_failure; |
| 1426 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1427 | if (geneve->collect_md) { |
| 1428 | if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA)) |
| 1429 | goto nla_put_failure; |
| 1430 | } |
| 1431 | |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1432 | if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM, |
| 1433 | !!(geneve->flags & GENEVE_F_UDP_CSUM)) || |
| 1434 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, |
| 1435 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) || |
| 1436 | nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, |
| 1437 | !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX))) |
| 1438 | goto nla_put_failure; |
| 1439 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1440 | return 0; |
| 1441 | |
| 1442 | nla_put_failure: |
| 1443 | return -EMSGSIZE; |
| 1444 | } |
| 1445 | |
| 1446 | static struct rtnl_link_ops geneve_link_ops __read_mostly = { |
| 1447 | .kind = "geneve", |
| 1448 | .maxtype = IFLA_GENEVE_MAX, |
| 1449 | .policy = geneve_policy, |
| 1450 | .priv_size = sizeof(struct geneve_dev), |
| 1451 | .setup = geneve_setup, |
| 1452 | .validate = geneve_validate, |
| 1453 | .newlink = geneve_newlink, |
| 1454 | .dellink = geneve_dellink, |
| 1455 | .get_size = geneve_get_size, |
| 1456 | .fill_info = geneve_fill_info, |
| 1457 | }; |
| 1458 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1459 | struct net_device *geneve_dev_create_fb(struct net *net, const char *name, |
| 1460 | u8 name_assign_type, u16 dst_port) |
| 1461 | { |
| 1462 | struct nlattr *tb[IFLA_MAX + 1]; |
| 1463 | struct net_device *dev; |
| 1464 | int err; |
| 1465 | |
| 1466 | memset(tb, 0, sizeof(tb)); |
| 1467 | dev = rtnl_create_link(net, name, name_assign_type, |
| 1468 | &geneve_link_ops, tb); |
| 1469 | if (IS_ERR(dev)) |
| 1470 | return dev; |
| 1471 | |
John W. Linville | 8ed66f0 | 2015-10-26 17:01:44 -0400 | [diff] [blame] | 1472 | err = geneve_configure(net, dev, &geneve_remote_unspec, |
Tom Herbert | abe492b | 2015-12-10 12:37:45 -0800 | [diff] [blame] | 1473 | 0, 0, 0, htons(dst_port), true, 0); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1474 | if (err) |
| 1475 | goto err; |
| 1476 | |
| 1477 | /* openvswitch users expect packet sizes to be unrestricted, |
| 1478 | * so set the largest MTU we can. |
| 1479 | */ |
David Wragg | aeee0e6 | 2016-02-18 17:43:29 +0000 | [diff] [blame] | 1480 | err = __geneve_change_mtu(dev, IP_MAX_MTU, false); |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1481 | if (err) |
| 1482 | goto err; |
| 1483 | |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1484 | return dev; |
David Wragg | 7e05915 | 2016-02-10 00:05:58 +0000 | [diff] [blame] | 1485 | |
| 1486 | err: |
| 1487 | free_netdev(dev); |
| 1488 | return ERR_PTR(err); |
Pravin B Shelar | e305ac6 | 2015-08-26 23:46:52 -0700 | [diff] [blame] | 1489 | } |
| 1490 | EXPORT_SYMBOL_GPL(geneve_dev_create_fb); |
| 1491 | |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1492 | static __net_init int geneve_init_net(struct net *net) |
| 1493 | { |
| 1494 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1495 | |
| 1496 | INIT_LIST_HEAD(&gn->geneve_list); |
Pravin B Shelar | 371bd10 | 2015-08-26 23:46:54 -0700 | [diff] [blame] | 1497 | INIT_LIST_HEAD(&gn->sock_list); |
John W. Linville | 2d07dc7 | 2015-05-13 12:57:30 -0400 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static void __net_exit geneve_exit_net(struct net *net) |
| 1502 | { |
| 1503 | struct geneve_net *gn = net_generic(net, geneve_net_id); |
| 1504 | struct geneve_dev *geneve, *next; |
| 1505 | struct net_device *dev, *aux; |
| 1506 | LIST_HEAD(list); |
| 1507 | |
| 1508 | rtnl_lock(); |
| 1509 | |
| 1510 | /* gather any geneve devices that were moved into this ns */ |
| 1511 | for_each_netdev_safe(net, dev, aux) |
| 1512 | if (dev->rtnl_link_ops == &geneve_link_ops) |
| 1513 | unregister_netdevice_queue(dev, &list); |
| 1514 | |
| 1515 | /* now gather any other geneve devices that were created in this ns */ |
| 1516 | list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) { |
| 1517 | /* If geneve->dev is in the same netns, it was already added |
| 1518 | * to the list by the previous loop. |
| 1519 | */ |
| 1520 | if (!net_eq(dev_net(geneve->dev), net)) |
| 1521 | unregister_netdevice_queue(geneve->dev, &list); |
| 1522 | } |
| 1523 | |
| 1524 | /* unregister the devices gathered above */ |
| 1525 | unregister_netdevice_many(&list); |
| 1526 | rtnl_unlock(); |
| 1527 | } |
| 1528 | |
| 1529 | static struct pernet_operations geneve_net_ops = { |
| 1530 | .init = geneve_init_net, |
| 1531 | .exit = geneve_exit_net, |
| 1532 | .id = &geneve_net_id, |
| 1533 | .size = sizeof(struct geneve_net), |
| 1534 | }; |
| 1535 | |
| 1536 | static int __init geneve_init_module(void) |
| 1537 | { |
| 1538 | int rc; |
| 1539 | |
| 1540 | rc = register_pernet_subsys(&geneve_net_ops); |
| 1541 | if (rc) |
| 1542 | goto out1; |
| 1543 | |
| 1544 | rc = rtnl_link_register(&geneve_link_ops); |
| 1545 | if (rc) |
| 1546 | goto out2; |
| 1547 | |
| 1548 | return 0; |
| 1549 | out2: |
| 1550 | unregister_pernet_subsys(&geneve_net_ops); |
| 1551 | out1: |
| 1552 | return rc; |
| 1553 | } |
| 1554 | late_initcall(geneve_init_module); |
| 1555 | |
| 1556 | static void __exit geneve_cleanup_module(void) |
| 1557 | { |
| 1558 | rtnl_link_unregister(&geneve_link_ops); |
| 1559 | unregister_pernet_subsys(&geneve_net_ops); |
| 1560 | } |
| 1561 | module_exit(geneve_cleanup_module); |
| 1562 | |
| 1563 | MODULE_LICENSE("GPL"); |
| 1564 | MODULE_VERSION(GENEVE_NETDEV_VER); |
| 1565 | MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>"); |
| 1566 | MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic"); |
| 1567 | MODULE_ALIAS_RTNL_LINK("geneve"); |