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