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