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