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