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