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