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