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