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