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