stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1 | /* |
Rami Rosen | eb5ce43 | 2012-11-13 13:29:15 +0000 | [diff] [blame] | 2 | * VXLAN: Virtual eXtensible Local Area Network |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3 | * |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 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. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/skbuff.h> |
| 19 | #include <linux/rculist.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/in.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/udp.h> |
| 24 | #include <linux/igmp.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/if_ether.h> |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 27 | #include <linux/if_vlan.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 28 | #include <linux/hash.h> |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 29 | #include <linux/ethtool.h> |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 30 | #include <net/arp.h> |
| 31 | #include <net/ndisc.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 32 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 33 | #include <net/ip_tunnels.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 34 | #include <net/icmp.h> |
| 35 | #include <net/udp.h> |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 36 | #include <net/udp_tunnel.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 37 | #include <net/rtnetlink.h> |
| 38 | #include <net/route.h> |
| 39 | #include <net/dsfield.h> |
| 40 | #include <net/inet_ecn.h> |
| 41 | #include <net/net_namespace.h> |
| 42 | #include <net/netns/generic.h> |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 43 | #include <net/vxlan.h> |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 44 | #include <net/protocol.h> |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 45 | #include <net/udp_tunnel.h> |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 46 | #if IS_ENABLED(CONFIG_IPV6) |
| 47 | #include <net/ipv6.h> |
| 48 | #include <net/addrconf.h> |
| 49 | #include <net/ip6_tunnel.h> |
Cong Wang | 660d98c | 2013-09-02 10:06:52 +0800 | [diff] [blame] | 50 | #include <net/ip6_checksum.h> |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 51 | #endif |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 52 | |
| 53 | #define VXLAN_VERSION "0.1" |
| 54 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 55 | #define PORT_HASH_BITS 8 |
| 56 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 57 | #define VNI_HASH_BITS 10 |
| 58 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 59 | #define FDB_HASH_BITS 8 |
| 60 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) |
| 61 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
| 62 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
| 63 | |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 64 | /* UDP port for VXLAN traffic. |
| 65 | * The IANA assigned port is 4789, but the Linux default is 8472 |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 66 | * for compatibility with early adopters. |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 67 | */ |
Stephen Hemminger | 9daaa39 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 68 | static unsigned short vxlan_port __read_mostly = 8472; |
| 69 | module_param_named(udp_port, vxlan_port, ushort, 0444); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 70 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
| 71 | |
| 72 | static bool log_ecn_error = true; |
| 73 | module_param(log_ecn_error, bool, 0644); |
| 74 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 75 | |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 76 | static int vxlan_net_id; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 77 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 78 | static const u8 all_zeros_mac[ETH_ALEN]; |
| 79 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 80 | /* per-network namespace private data for this module */ |
| 81 | struct vxlan_net { |
| 82 | struct list_head vxlan_list; |
| 83 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 84 | spinlock_t sock_lock; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 87 | union vxlan_addr { |
| 88 | struct sockaddr_in sin; |
| 89 | struct sockaddr_in6 sin6; |
| 90 | struct sockaddr sa; |
| 91 | }; |
| 92 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 93 | struct vxlan_rdst { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 94 | union vxlan_addr remote_ip; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 95 | __be16 remote_port; |
| 96 | u32 remote_vni; |
| 97 | u32 remote_ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 98 | struct list_head list; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 99 | struct rcu_head rcu; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 102 | /* Forwarding table entry */ |
| 103 | struct vxlan_fdb { |
| 104 | struct hlist_node hlist; /* linked list of entries */ |
| 105 | struct rcu_head rcu; |
| 106 | unsigned long updated; /* jiffies */ |
| 107 | unsigned long used; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 108 | struct list_head remotes; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 109 | u16 state; /* see ndm_state */ |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 110 | u8 flags; /* see ndm_flags */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 111 | u8 eth_addr[ETH_ALEN]; |
| 112 | }; |
| 113 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 114 | /* Pseudo network device */ |
| 115 | struct vxlan_dev { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 116 | struct hlist_node hlist; /* vni hash table */ |
| 117 | struct list_head next; /* vxlan's per namespace list */ |
| 118 | struct vxlan_sock *vn_sock; /* listening socket */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 119 | struct net_device *dev; |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 120 | struct net *net; /* netns for packet i/o */ |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 121 | struct vxlan_rdst default_dst; /* default destination */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 122 | union vxlan_addr saddr; /* source address */ |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 123 | __be16 dst_port; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 124 | __u16 port_min; /* source port range */ |
| 125 | __u16 port_max; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 126 | __u8 tos; /* TOS override */ |
| 127 | __u8 ttl; |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 128 | u32 flags; /* VXLAN_F_* in vxlan.h */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 129 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 130 | struct work_struct sock_work; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 131 | struct work_struct igmp_join; |
| 132 | struct work_struct igmp_leave; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 133 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 134 | unsigned long age_interval; |
| 135 | struct timer_list age_timer; |
| 136 | spinlock_t hash_lock; |
| 137 | unsigned int addrcnt; |
| 138 | unsigned int addrmax; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 139 | |
| 140 | struct hlist_head fdb_head[FDB_HASH_SIZE]; |
| 141 | }; |
| 142 | |
| 143 | /* salt for hash table */ |
| 144 | static u32 vxlan_salt __read_mostly; |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 145 | static struct workqueue_struct *vxlan_wq; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 146 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 147 | static void vxlan_sock_work(struct work_struct *work); |
| 148 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 149 | #if IS_ENABLED(CONFIG_IPV6) |
| 150 | static inline |
| 151 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) |
| 152 | { |
| 153 | if (a->sa.sa_family != b->sa.sa_family) |
| 154 | return false; |
| 155 | if (a->sa.sa_family == AF_INET6) |
| 156 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); |
| 157 | else |
| 158 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
| 159 | } |
| 160 | |
| 161 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) |
| 162 | { |
| 163 | if (ipa->sa.sa_family == AF_INET6) |
| 164 | return ipv6_addr_any(&ipa->sin6.sin6_addr); |
| 165 | else |
| 166 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); |
| 167 | } |
| 168 | |
| 169 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) |
| 170 | { |
| 171 | if (ipa->sa.sa_family == AF_INET6) |
| 172 | return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr); |
| 173 | else |
| 174 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); |
| 175 | } |
| 176 | |
| 177 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
| 178 | { |
| 179 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
| 180 | nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr)); |
| 181 | ip->sa.sa_family = AF_INET6; |
| 182 | return 0; |
| 183 | } else if (nla_len(nla) >= sizeof(__be32)) { |
| 184 | ip->sin.sin_addr.s_addr = nla_get_be32(nla); |
| 185 | ip->sa.sa_family = AF_INET; |
| 186 | return 0; |
| 187 | } else { |
| 188 | return -EAFNOSUPPORT; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, |
| 193 | const union vxlan_addr *ip) |
| 194 | { |
| 195 | if (ip->sa.sa_family == AF_INET6) |
| 196 | return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr); |
| 197 | else |
| 198 | return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); |
| 199 | } |
| 200 | |
| 201 | #else /* !CONFIG_IPV6 */ |
| 202 | |
| 203 | static inline |
| 204 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) |
| 205 | { |
| 206 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
| 207 | } |
| 208 | |
| 209 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) |
| 210 | { |
| 211 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); |
| 212 | } |
| 213 | |
| 214 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) |
| 215 | { |
| 216 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); |
| 217 | } |
| 218 | |
| 219 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
| 220 | { |
| 221 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
| 222 | return -EAFNOSUPPORT; |
| 223 | } else if (nla_len(nla) >= sizeof(__be32)) { |
| 224 | ip->sin.sin_addr.s_addr = nla_get_be32(nla); |
| 225 | ip->sa.sa_family = AF_INET; |
| 226 | return 0; |
| 227 | } else { |
| 228 | return -EAFNOSUPPORT; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, |
| 233 | const union vxlan_addr *ip) |
| 234 | { |
| 235 | return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); |
| 236 | } |
| 237 | #endif |
| 238 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 239 | /* Virtual Network hash table head */ |
| 240 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) |
| 241 | { |
| 242 | return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; |
| 243 | } |
| 244 | |
| 245 | /* Socket hash table head */ |
| 246 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 247 | { |
| 248 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 249 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 250 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
| 251 | } |
| 252 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 253 | /* First remote destination for a forwarding entry. |
| 254 | * Guaranteed to be non-NULL because remotes are never deleted. |
| 255 | */ |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 256 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 257 | { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 258 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
| 259 | } |
| 260 | |
| 261 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) |
| 262 | { |
| 263 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 266 | /* Find VXLAN socket based on network namespace, address family and UDP port */ |
| 267 | static struct vxlan_sock *vxlan_find_sock(struct net *net, |
| 268 | sa_family_t family, __be16 port) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 269 | { |
| 270 | struct vxlan_sock *vs; |
| 271 | |
| 272 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 273 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
| 274 | inet_sk(vs->sock->sk)->sk.sk_family == family) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 275 | return vs; |
| 276 | } |
| 277 | return NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 280 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 281 | { |
| 282 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 283 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 284 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 285 | if (vxlan->default_dst.remote_vni == id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 286 | return vxlan; |
| 287 | } |
| 288 | |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 292 | /* Look up VNI in a per net namespace table */ |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 293 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, |
| 294 | sa_family_t family, __be16 port) |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 295 | { |
| 296 | struct vxlan_sock *vs; |
| 297 | |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 298 | vs = vxlan_find_sock(net, family, port); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 299 | if (!vs) |
| 300 | return NULL; |
| 301 | |
| 302 | return vxlan_vs_find_vni(vs, id); |
| 303 | } |
| 304 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 305 | /* Fill in neighbour message in skbuff. */ |
| 306 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 307 | const struct vxlan_fdb *fdb, |
| 308 | u32 portid, u32 seq, int type, unsigned int flags, |
| 309 | const struct vxlan_rdst *rdst) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 310 | { |
| 311 | unsigned long now = jiffies; |
| 312 | struct nda_cacheinfo ci; |
| 313 | struct nlmsghdr *nlh; |
| 314 | struct ndmsg *ndm; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 315 | bool send_ip, send_eth; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 316 | |
| 317 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 318 | if (nlh == NULL) |
| 319 | return -EMSGSIZE; |
| 320 | |
| 321 | ndm = nlmsg_data(nlh); |
| 322 | memset(ndm, 0, sizeof(*ndm)); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 323 | |
| 324 | send_eth = send_ip = true; |
| 325 | |
| 326 | if (type == RTM_GETNEIGH) { |
| 327 | ndm->ndm_family = AF_INET; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 328 | send_ip = !vxlan_addr_any(&rdst->remote_ip); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 329 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
| 330 | } else |
| 331 | ndm->ndm_family = AF_BRIDGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 332 | ndm->ndm_state = fdb->state; |
| 333 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 334 | ndm->ndm_flags = fdb->flags; |
Jun Zhao | 545469f | 2014-07-26 00:38:59 +0800 | [diff] [blame] | 335 | ndm->ndm_type = RTN_UNICAST; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 336 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 337 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 338 | goto nla_put_failure; |
| 339 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 340 | if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip)) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 341 | goto nla_put_failure; |
| 342 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 343 | if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 344 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
| 345 | goto nla_put_failure; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 346 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 347 | nla_put_u32(skb, NDA_VNI, rdst->remote_vni)) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 348 | goto nla_put_failure; |
| 349 | if (rdst->remote_ifindex && |
| 350 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 351 | goto nla_put_failure; |
| 352 | |
| 353 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 354 | ci.ndm_confirmed = 0; |
| 355 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 356 | ci.ndm_refcnt = 0; |
| 357 | |
| 358 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 359 | goto nla_put_failure; |
| 360 | |
| 361 | return nlmsg_end(skb, nlh); |
| 362 | |
| 363 | nla_put_failure: |
| 364 | nlmsg_cancel(skb, nlh); |
| 365 | return -EMSGSIZE; |
| 366 | } |
| 367 | |
| 368 | static inline size_t vxlan_nlmsg_size(void) |
| 369 | { |
| 370 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 371 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 372 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 373 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 374 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
| 375 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 376 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 377 | } |
| 378 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 379 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
| 380 | struct vxlan_rdst *rd, int type) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 381 | { |
| 382 | struct net *net = dev_net(vxlan->dev); |
| 383 | struct sk_buff *skb; |
| 384 | int err = -ENOBUFS; |
| 385 | |
| 386 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
| 387 | if (skb == NULL) |
| 388 | goto errout; |
| 389 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 390 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 391 | if (err < 0) { |
| 392 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
| 393 | WARN_ON(err == -EMSGSIZE); |
| 394 | kfree_skb(skb); |
| 395 | goto errout; |
| 396 | } |
| 397 | |
| 398 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 399 | return; |
| 400 | errout: |
| 401 | if (err < 0) |
| 402 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 403 | } |
| 404 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 405 | static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 406 | { |
| 407 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 408 | struct vxlan_fdb f = { |
| 409 | .state = NUD_STALE, |
| 410 | }; |
| 411 | struct vxlan_rdst remote = { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 412 | .remote_ip = *ipa, /* goes to NDA_DST */ |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 413 | .remote_vni = VXLAN_N_VID, |
| 414 | }; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 415 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 416 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) |
| 420 | { |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 421 | struct vxlan_fdb f = { |
| 422 | .state = NUD_STALE, |
| 423 | }; |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 424 | struct vxlan_rdst remote = { }; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 425 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 426 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
| 427 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 428 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 429 | } |
| 430 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 431 | /* Hash Ethernet address */ |
| 432 | static u32 eth_hash(const unsigned char *addr) |
| 433 | { |
| 434 | u64 value = get_unaligned((u64 *)addr); |
| 435 | |
| 436 | /* only want 6 bytes */ |
| 437 | #ifdef __BIG_ENDIAN |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 438 | value >>= 16; |
stephen hemminger | 321fb99 | 2012-10-09 20:35:47 +0000 | [diff] [blame] | 439 | #else |
| 440 | value <<= 16; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 441 | #endif |
| 442 | return hash_64(value, FDB_HASH_BITS); |
| 443 | } |
| 444 | |
| 445 | /* Hash chain to use given mac address */ |
| 446 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
| 447 | const u8 *mac) |
| 448 | { |
| 449 | return &vxlan->fdb_head[eth_hash(mac)]; |
| 450 | } |
| 451 | |
| 452 | /* Look up Ethernet address in forwarding table */ |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 453 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 454 | const u8 *mac) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 455 | { |
| 456 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); |
| 457 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 458 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 459 | hlist_for_each_entry_rcu(f, head, hlist) { |
Joe Perches | 7367d0b | 2013-09-01 11:51:23 -0700 | [diff] [blame] | 460 | if (ether_addr_equal(mac, f->eth_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 461 | return f; |
| 462 | } |
| 463 | |
| 464 | return NULL; |
| 465 | } |
| 466 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 467 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
| 468 | const u8 *mac) |
| 469 | { |
| 470 | struct vxlan_fdb *f; |
| 471 | |
| 472 | f = __vxlan_find_mac(vxlan, mac); |
| 473 | if (f) |
| 474 | f->used = jiffies; |
| 475 | |
| 476 | return f; |
| 477 | } |
| 478 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 479 | /* caller should hold vxlan->hash_lock */ |
| 480 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 481 | union vxlan_addr *ip, __be16 port, |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 482 | __u32 vni, __u32 ifindex) |
| 483 | { |
| 484 | struct vxlan_rdst *rd; |
| 485 | |
| 486 | list_for_each_entry(rd, &f->remotes, list) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 487 | if (vxlan_addr_equal(&rd->remote_ip, ip) && |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 488 | rd->remote_port == port && |
| 489 | rd->remote_vni == vni && |
| 490 | rd->remote_ifindex == ifindex) |
| 491 | return rd; |
| 492 | } |
| 493 | |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 497 | /* Replace destination of unicast mac */ |
| 498 | static int vxlan_fdb_replace(struct vxlan_fdb *f, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 499 | union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex) |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 500 | { |
| 501 | struct vxlan_rdst *rd; |
| 502 | |
| 503 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 504 | if (rd) |
| 505 | return 0; |
| 506 | |
| 507 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); |
| 508 | if (!rd) |
| 509 | return 0; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 510 | rd->remote_ip = *ip; |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 511 | rd->remote_port = port; |
| 512 | rd->remote_vni = vni; |
| 513 | rd->remote_ifindex = ifindex; |
| 514 | return 1; |
| 515 | } |
| 516 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 517 | /* Add/update destinations for multicast */ |
| 518 | static int vxlan_fdb_append(struct vxlan_fdb *f, |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 519 | union vxlan_addr *ip, __be16 port, __u32 vni, |
| 520 | __u32 ifindex, struct vxlan_rdst **rdp) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 521 | { |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 522 | struct vxlan_rdst *rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 523 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 524 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 525 | if (rd) |
| 526 | return 0; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 527 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 528 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
| 529 | if (rd == NULL) |
| 530 | return -ENOBUFS; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 531 | rd->remote_ip = *ip; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 532 | rd->remote_port = port; |
| 533 | rd->remote_vni = vni; |
| 534 | rd->remote_ifindex = ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 535 | |
| 536 | list_add_tail_rcu(&rd->list, &f->remotes); |
| 537 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 538 | *rdp = rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 539 | return 1; |
| 540 | } |
| 541 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 542 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, |
| 543 | unsigned int off, |
| 544 | struct vxlanhdr *vh, size_t hdrlen, |
| 545 | u32 data) |
| 546 | { |
| 547 | size_t start, offset, plen; |
| 548 | __wsum delta; |
| 549 | |
| 550 | if (skb->remcsum_offload) |
| 551 | return vh; |
| 552 | |
| 553 | if (!NAPI_GRO_CB(skb)->csum_valid) |
| 554 | return NULL; |
| 555 | |
| 556 | start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT; |
| 557 | offset = start + ((data & VXLAN_RCO_UDP) ? |
| 558 | offsetof(struct udphdr, check) : |
| 559 | offsetof(struct tcphdr, check)); |
| 560 | |
| 561 | plen = hdrlen + offset + sizeof(u16); |
| 562 | |
| 563 | /* Pull checksum that will be written */ |
| 564 | if (skb_gro_header_hard(skb, off + plen)) { |
| 565 | vh = skb_gro_header_slow(skb, off + plen, off); |
| 566 | if (!vh) |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | delta = remcsum_adjust((void *)vh + hdrlen, |
| 571 | NAPI_GRO_CB(skb)->csum, start, offset); |
| 572 | |
| 573 | /* Adjust skb->csum since we changed the packet */ |
| 574 | skb->csum = csum_add(skb->csum, delta); |
| 575 | NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta); |
| 576 | |
| 577 | skb->remcsum_offload = 1; |
| 578 | |
| 579 | return vh; |
| 580 | } |
| 581 | |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 582 | static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, |
| 583 | struct sk_buff *skb, |
| 584 | struct udp_offload *uoff) |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 585 | { |
| 586 | struct sk_buff *p, **pp = NULL; |
| 587 | struct vxlanhdr *vh, *vh2; |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 588 | unsigned int hlen, off_vx; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 589 | int flush = 1; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 590 | struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock, |
| 591 | udp_offloads); |
| 592 | u32 flags; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 593 | |
| 594 | off_vx = skb_gro_offset(skb); |
| 595 | hlen = off_vx + sizeof(*vh); |
| 596 | vh = skb_gro_header_fast(skb, off_vx); |
| 597 | if (skb_gro_header_hard(skb, hlen)) { |
| 598 | vh = skb_gro_header_slow(skb, hlen, off_vx); |
| 599 | if (unlikely(!vh)) |
| 600 | goto out; |
| 601 | } |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 602 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 603 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ |
| 604 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); |
| 605 | |
| 606 | flags = ntohl(vh->vx_flags); |
| 607 | |
| 608 | if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { |
| 609 | vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), |
| 610 | ntohl(vh->vx_vni)); |
| 611 | |
| 612 | if (!vh) |
| 613 | goto out; |
| 614 | } |
| 615 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 616 | flush = 0; |
| 617 | |
| 618 | for (p = *head; p; p = p->next) { |
| 619 | if (!NAPI_GRO_CB(p)->same_flow) |
| 620 | continue; |
| 621 | |
| 622 | vh2 = (struct vxlanhdr *)(p->data + off_vx); |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 623 | if (vh->vx_vni != vh2->vx_vni) { |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 624 | NAPI_GRO_CB(p)->same_flow = 0; |
| 625 | continue; |
| 626 | } |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 629 | pp = eth_gro_receive(head, skb); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 630 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 631 | out: |
| 632 | NAPI_GRO_CB(skb)->flush |= flush; |
| 633 | |
| 634 | return pp; |
| 635 | } |
| 636 | |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 637 | static int vxlan_gro_complete(struct sk_buff *skb, int nhoff, |
| 638 | struct udp_offload *uoff) |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 639 | { |
Jesse Gross | cfdf1e1 | 2014-11-10 11:45:13 -0800 | [diff] [blame] | 640 | udp_tunnel_gro_complete(skb, nhoff); |
| 641 | |
Jesse Gross | 9b174d8 | 2014-12-30 19:10:15 -0800 | [diff] [blame] | 642 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 645 | /* Notify netdevs that UDP port started listening */ |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 646 | static void vxlan_notify_add_rx_port(struct vxlan_sock *vs) |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 647 | { |
| 648 | struct net_device *dev; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 649 | struct sock *sk = vs->sock->sk; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 650 | struct net *net = sock_net(sk); |
| 651 | sa_family_t sa_family = sk->sk_family; |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 652 | __be16 port = inet_sk(sk)->inet_sport; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 653 | int err; |
| 654 | |
| 655 | if (sa_family == AF_INET) { |
| 656 | err = udp_add_offload(&vs->udp_offloads); |
| 657 | if (err) |
| 658 | pr_warn("vxlan: udp_add_offload failed with status %d\n", err); |
| 659 | } |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 660 | |
| 661 | rcu_read_lock(); |
| 662 | for_each_netdev_rcu(net, dev) { |
| 663 | if (dev->netdev_ops->ndo_add_vxlan_port) |
| 664 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, |
| 665 | port); |
| 666 | } |
| 667 | rcu_read_unlock(); |
| 668 | } |
| 669 | |
| 670 | /* Notify netdevs that UDP port is no more listening */ |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 671 | static void vxlan_notify_del_rx_port(struct vxlan_sock *vs) |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 672 | { |
| 673 | struct net_device *dev; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 674 | struct sock *sk = vs->sock->sk; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 675 | struct net *net = sock_net(sk); |
| 676 | sa_family_t sa_family = sk->sk_family; |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 677 | __be16 port = inet_sk(sk)->inet_sport; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 678 | |
| 679 | rcu_read_lock(); |
| 680 | for_each_netdev_rcu(net, dev) { |
| 681 | if (dev->netdev_ops->ndo_del_vxlan_port) |
| 682 | dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family, |
| 683 | port); |
| 684 | } |
| 685 | rcu_read_unlock(); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 686 | |
| 687 | if (sa_family == AF_INET) |
| 688 | udp_del_offload(&vs->udp_offloads); |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 689 | } |
| 690 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 691 | /* Add new entry to forwarding table -- assumes lock held */ |
| 692 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 693 | const u8 *mac, union vxlan_addr *ip, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 694 | __u16 state, __u16 flags, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 695 | __be16 port, __u32 vni, __u32 ifindex, |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 696 | __u8 ndm_flags) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 697 | { |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 698 | struct vxlan_rdst *rd = NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 699 | struct vxlan_fdb *f; |
| 700 | int notify = 0; |
| 701 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 702 | f = __vxlan_find_mac(vxlan, mac); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 703 | if (f) { |
| 704 | if (flags & NLM_F_EXCL) { |
| 705 | netdev_dbg(vxlan->dev, |
| 706 | "lost race to create %pM\n", mac); |
| 707 | return -EEXIST; |
| 708 | } |
| 709 | if (f->state != state) { |
| 710 | f->state = state; |
| 711 | f->updated = jiffies; |
| 712 | notify = 1; |
| 713 | } |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 714 | if (f->flags != ndm_flags) { |
| 715 | f->flags = ndm_flags; |
| 716 | f->updated = jiffies; |
| 717 | notify = 1; |
| 718 | } |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 719 | if ((flags & NLM_F_REPLACE)) { |
| 720 | /* Only change unicasts */ |
| 721 | if (!(is_multicast_ether_addr(f->eth_addr) || |
| 722 | is_zero_ether_addr(f->eth_addr))) { |
| 723 | int rc = vxlan_fdb_replace(f, ip, port, vni, |
| 724 | ifindex); |
| 725 | |
| 726 | if (rc < 0) |
| 727 | return rc; |
| 728 | notify |= rc; |
| 729 | } else |
| 730 | return -EOPNOTSUPP; |
| 731 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 732 | if ((flags & NLM_F_APPEND) && |
Mike Rapoport | 58e4c76 | 2013-06-25 16:01:56 +0300 | [diff] [blame] | 733 | (is_multicast_ether_addr(f->eth_addr) || |
| 734 | is_zero_ether_addr(f->eth_addr))) { |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 735 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex, |
| 736 | &rd); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 737 | |
| 738 | if (rc < 0) |
| 739 | return rc; |
| 740 | notify |= rc; |
| 741 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 742 | } else { |
| 743 | if (!(flags & NLM_F_CREATE)) |
| 744 | return -ENOENT; |
| 745 | |
| 746 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) |
| 747 | return -ENOSPC; |
| 748 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 749 | /* Disallow replace to add a multicast entry */ |
| 750 | if ((flags & NLM_F_REPLACE) && |
| 751 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) |
| 752 | return -EOPNOTSUPP; |
| 753 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 754 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 755 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 756 | if (!f) |
| 757 | return -ENOMEM; |
| 758 | |
| 759 | notify = 1; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 760 | f->state = state; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 761 | f->flags = ndm_flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 762 | f->updated = f->used = jiffies; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 763 | INIT_LIST_HEAD(&f->remotes); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 764 | memcpy(f->eth_addr, mac, ETH_ALEN); |
| 765 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 766 | vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 767 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 768 | ++vxlan->addrcnt; |
| 769 | hlist_add_head_rcu(&f->hlist, |
| 770 | vxlan_fdb_head(vxlan, mac)); |
| 771 | } |
| 772 | |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 773 | if (notify) { |
| 774 | if (rd == NULL) |
| 775 | rd = first_remote_rtnl(f); |
| 776 | vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH); |
| 777 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
Wei Yongjun | 6706c82 | 2013-04-11 19:00:35 +0000 | [diff] [blame] | 782 | static void vxlan_fdb_free(struct rcu_head *head) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 783 | { |
| 784 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 785 | struct vxlan_rdst *rd, *nd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 786 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 787 | list_for_each_entry_safe(rd, nd, &f->remotes, list) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 788 | kfree(rd); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 789 | kfree(f); |
| 790 | } |
| 791 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 792 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
| 793 | { |
| 794 | netdev_dbg(vxlan->dev, |
| 795 | "delete %pM\n", f->eth_addr); |
| 796 | |
| 797 | --vxlan->addrcnt; |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 798 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 799 | |
| 800 | hlist_del_rcu(&f->hlist); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 801 | call_rcu(&f->rcu, vxlan_fdb_free); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 804 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 805 | union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex) |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 806 | { |
| 807 | struct net *net = dev_net(vxlan->dev); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 808 | int err; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 809 | |
| 810 | if (tb[NDA_DST]) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 811 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
| 812 | if (err) |
| 813 | return err; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 814 | } else { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 815 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
| 816 | if (remote->sa.sa_family == AF_INET) { |
| 817 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); |
| 818 | ip->sa.sa_family = AF_INET; |
| 819 | #if IS_ENABLED(CONFIG_IPV6) |
| 820 | } else { |
| 821 | ip->sin6.sin6_addr = in6addr_any; |
| 822 | ip->sa.sa_family = AF_INET6; |
| 823 | #endif |
| 824 | } |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | if (tb[NDA_PORT]) { |
| 828 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
| 829 | return -EINVAL; |
| 830 | *port = nla_get_be16(tb[NDA_PORT]); |
| 831 | } else { |
| 832 | *port = vxlan->dst_port; |
| 833 | } |
| 834 | |
| 835 | if (tb[NDA_VNI]) { |
| 836 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) |
| 837 | return -EINVAL; |
| 838 | *vni = nla_get_u32(tb[NDA_VNI]); |
| 839 | } else { |
| 840 | *vni = vxlan->default_dst.remote_vni; |
| 841 | } |
| 842 | |
| 843 | if (tb[NDA_IFINDEX]) { |
| 844 | struct net_device *tdev; |
| 845 | |
| 846 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) |
| 847 | return -EINVAL; |
| 848 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
Ying Xue | 7376394 | 2014-01-15 10:23:41 +0800 | [diff] [blame] | 849 | tdev = __dev_get_by_index(net, *ifindex); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 850 | if (!tdev) |
| 851 | return -EADDRNOTAVAIL; |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 852 | } else { |
| 853 | *ifindex = 0; |
| 854 | } |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 859 | /* Add static entry (via netlink) */ |
| 860 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 861 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 862 | const unsigned char *addr, u16 vid, u16 flags) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 863 | { |
| 864 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 865 | /* struct net *net = dev_net(vxlan->dev); */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 866 | union vxlan_addr ip; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 867 | __be16 port; |
| 868 | u32 vni, ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 869 | int err; |
| 870 | |
| 871 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
| 872 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
| 873 | ndm->ndm_state); |
| 874 | return -EINVAL; |
| 875 | } |
| 876 | |
| 877 | if (tb[NDA_DST] == NULL) |
| 878 | return -EINVAL; |
| 879 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 880 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 881 | if (err) |
| 882 | return err; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 883 | |
Mike Rapoport | 5933a7b | 2014-04-01 09:23:01 +0300 | [diff] [blame] | 884 | if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) |
| 885 | return -EAFNOSUPPORT; |
| 886 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 887 | spin_lock_bh(&vxlan->hash_lock); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 888 | err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 889 | port, vni, ifindex, ndm->ndm_flags); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 890 | spin_unlock_bh(&vxlan->hash_lock); |
| 891 | |
| 892 | return err; |
| 893 | } |
| 894 | |
| 895 | /* Delete entry (via netlink) */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 896 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 897 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 898 | const unsigned char *addr, u16 vid) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 899 | { |
| 900 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 901 | struct vxlan_fdb *f; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 902 | struct vxlan_rdst *rd = NULL; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 903 | union vxlan_addr ip; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 904 | __be16 port; |
| 905 | u32 vni, ifindex; |
| 906 | int err; |
| 907 | |
| 908 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 909 | if (err) |
| 910 | return err; |
| 911 | |
| 912 | err = -ENOENT; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 913 | |
| 914 | spin_lock_bh(&vxlan->hash_lock); |
| 915 | f = vxlan_find_mac(vxlan, addr); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 916 | if (!f) |
| 917 | goto out; |
| 918 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 919 | if (!vxlan_addr_any(&ip)) { |
| 920 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 921 | if (!rd) |
| 922 | goto out; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 923 | } |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 924 | |
| 925 | err = 0; |
| 926 | |
| 927 | /* remove a destination if it's not the only one on the list, |
| 928 | * otherwise destroy the fdb entry |
| 929 | */ |
| 930 | if (rd && !list_is_singular(&f->remotes)) { |
| 931 | list_del_rcu(&rd->list); |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 932 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH); |
Wei Yongjun | dcdc7a5 | 2013-08-17 07:32:09 +0800 | [diff] [blame] | 933 | kfree_rcu(rd, rcu); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 934 | goto out; |
| 935 | } |
| 936 | |
| 937 | vxlan_fdb_destroy(vxlan, f); |
| 938 | |
| 939 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 940 | spin_unlock_bh(&vxlan->hash_lock); |
| 941 | |
| 942 | return err; |
| 943 | } |
| 944 | |
| 945 | /* Dump forwarding table */ |
| 946 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 947 | struct net_device *dev, |
| 948 | struct net_device *filter_dev, int idx) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 949 | { |
| 950 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 951 | unsigned int h; |
| 952 | |
| 953 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 954 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 955 | int err; |
| 956 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 957 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 958 | struct vxlan_rdst *rd; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 959 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 960 | if (idx < cb->args[0]) |
| 961 | goto skip; |
| 962 | |
| 963 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 964 | err = vxlan_fdb_info(skb, vxlan, f, |
| 965 | NETLINK_CB(cb->skb).portid, |
| 966 | cb->nlh->nlmsg_seq, |
| 967 | RTM_NEWNEIGH, |
| 968 | NLM_F_MULTI, rd); |
| 969 | if (err < 0) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 970 | goto out; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 971 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 972 | skip: |
| 973 | ++idx; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 976 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 977 | return idx; |
| 978 | } |
| 979 | |
| 980 | /* Watch incoming packets to learn mapping between Ethernet address |
| 981 | * and Tunnel endpoint. |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 982 | * Return true if packet is bogus and should be droppped. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 983 | */ |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 984 | static bool vxlan_snoop(struct net_device *dev, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 985 | union vxlan_addr *src_ip, const u8 *src_mac) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 986 | { |
| 987 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 988 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 989 | |
| 990 | f = vxlan_find_mac(vxlan, src_mac); |
| 991 | if (likely(f)) { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 992 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 993 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 994 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip))) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 995 | return false; |
| 996 | |
| 997 | /* Don't migrate static entries, drop packets */ |
stephen hemminger | eb064c3 | 2013-06-18 14:27:01 -0700 | [diff] [blame] | 998 | if (f->state & NUD_NOARP) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 999 | return true; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1000 | |
| 1001 | if (net_ratelimit()) |
| 1002 | netdev_info(dev, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1003 | "%pM migrated from %pIS to %pIS\n", |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1004 | src_mac, &rdst->remote_ip, &src_ip); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1005 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1006 | rdst->remote_ip = *src_ip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1007 | f->updated = jiffies; |
Nicolas Dichtel | 9e4b93f | 2014-04-22 15:01:30 +0200 | [diff] [blame] | 1008 | vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1009 | } else { |
| 1010 | /* learned new entry */ |
| 1011 | spin_lock(&vxlan->hash_lock); |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 1012 | |
| 1013 | /* close off race between vxlan_flush and incoming packets */ |
| 1014 | if (netif_running(dev)) |
| 1015 | vxlan_fdb_create(vxlan, src_mac, src_ip, |
| 1016 | NUD_REACHABLE, |
| 1017 | NLM_F_EXCL|NLM_F_CREATE, |
| 1018 | vxlan->dst_port, |
| 1019 | vxlan->default_dst.remote_vni, |
| 1020 | 0, NTF_SELF); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1021 | spin_unlock(&vxlan->hash_lock); |
| 1022 | } |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1023 | |
| 1024 | return false; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1027 | /* See if multicast group is already in use by other ID */ |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1028 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1029 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1030 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1031 | |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1032 | /* The vxlan_sock is only used by dev, leaving group has |
| 1033 | * no effect on other vxlan devices. |
| 1034 | */ |
| 1035 | if (atomic_read(&dev->vn_sock->refcnt) == 1) |
| 1036 | return false; |
| 1037 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1038 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1039 | if (!netif_running(vxlan->dev) || vxlan == dev) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1040 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1041 | |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 1042 | if (vxlan->vn_sock != dev->vn_sock) |
| 1043 | continue; |
| 1044 | |
| 1045 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, |
| 1046 | &dev->default_dst.remote_ip)) |
| 1047 | continue; |
| 1048 | |
| 1049 | if (vxlan->default_dst.remote_ifindex != |
| 1050 | dev->default_dst.remote_ifindex) |
| 1051 | continue; |
| 1052 | |
| 1053 | return true; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1054 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1055 | |
| 1056 | return false; |
| 1057 | } |
| 1058 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1059 | static void vxlan_sock_hold(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1060 | { |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1061 | atomic_inc(&vs->refcnt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1064 | void vxlan_sock_release(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1065 | { |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 1066 | struct sock *sk = vs->sock->sk; |
| 1067 | struct net *net = sock_net(sk); |
| 1068 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1069 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1070 | if (!atomic_dec_and_test(&vs->refcnt)) |
| 1071 | return; |
| 1072 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1073 | spin_lock(&vn->sock_lock); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1074 | hlist_del_rcu(&vs->hlist); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 1075 | vxlan_notify_del_rx_port(vs); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1076 | spin_unlock(&vn->sock_lock); |
| 1077 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1078 | queue_work(vxlan_wq, &vs->del_work); |
| 1079 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1080 | EXPORT_SYMBOL_GPL(vxlan_sock_release); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1081 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1082 | /* Callback to update multicast group membership when first VNI on |
| 1083 | * multicast asddress is brought up |
| 1084 | * Done as workqueue because ip_mc_join_group acquires RTNL. |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1085 | */ |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1086 | static void vxlan_igmp_join(struct work_struct *work) |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1087 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1088 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1089 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1090 | struct sock *sk = vs->sock->sk; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1091 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
| 1092 | int ifindex = vxlan->default_dst.remote_ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1093 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1094 | lock_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1095 | if (ip->sa.sa_family == AF_INET) { |
| 1096 | struct ip_mreqn mreq = { |
| 1097 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, |
| 1098 | .imr_ifindex = ifindex, |
| 1099 | }; |
| 1100 | |
| 1101 | ip_mc_join_group(sk, &mreq); |
| 1102 | #if IS_ENABLED(CONFIG_IPV6) |
| 1103 | } else { |
| 1104 | ipv6_stub->ipv6_sock_mc_join(sk, ifindex, |
| 1105 | &ip->sin6.sin6_addr); |
| 1106 | #endif |
| 1107 | } |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1108 | release_sock(sk); |
| 1109 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1110 | vxlan_sock_release(vs); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1111 | dev_put(vxlan->dev); |
| 1112 | } |
| 1113 | |
| 1114 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ |
| 1115 | static void vxlan_igmp_leave(struct work_struct *work) |
| 1116 | { |
| 1117 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1118 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1119 | struct sock *sk = vs->sock->sk; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1120 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
| 1121 | int ifindex = vxlan->default_dst.remote_ifindex; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1122 | |
| 1123 | lock_sock(sk); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1124 | if (ip->sa.sa_family == AF_INET) { |
| 1125 | struct ip_mreqn mreq = { |
| 1126 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, |
| 1127 | .imr_ifindex = ifindex, |
| 1128 | }; |
| 1129 | |
| 1130 | ip_mc_leave_group(sk, &mreq); |
| 1131 | #if IS_ENABLED(CONFIG_IPV6) |
| 1132 | } else { |
| 1133 | ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, |
| 1134 | &ip->sin6.sin6_addr); |
| 1135 | #endif |
| 1136 | } |
| 1137 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1138 | release_sock(sk); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1139 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1140 | vxlan_sock_release(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1141 | dev_put(vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1144 | static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh, |
| 1145 | size_t hdrlen, u32 data) |
| 1146 | { |
| 1147 | size_t start, offset, plen; |
| 1148 | __wsum delta; |
| 1149 | |
| 1150 | if (skb->remcsum_offload) { |
| 1151 | /* Already processed in GRO path */ |
| 1152 | skb->remcsum_offload = 0; |
| 1153 | return vh; |
| 1154 | } |
| 1155 | |
| 1156 | start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT; |
| 1157 | offset = start + ((data & VXLAN_RCO_UDP) ? |
| 1158 | offsetof(struct udphdr, check) : |
| 1159 | offsetof(struct tcphdr, check)); |
| 1160 | |
| 1161 | plen = hdrlen + offset + sizeof(u16); |
| 1162 | |
| 1163 | if (!pskb_may_pull(skb, plen)) |
| 1164 | return NULL; |
| 1165 | |
| 1166 | vh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
| 1167 | |
| 1168 | if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) |
| 1169 | __skb_checksum_complete(skb); |
| 1170 | |
| 1171 | delta = remcsum_adjust((void *)vh + hdrlen, |
| 1172 | skb->csum, start, offset); |
| 1173 | |
| 1174 | /* Adjust skb->csum since we changed the packet */ |
| 1175 | skb->csum = csum_add(skb->csum, delta); |
| 1176 | |
| 1177 | return vh; |
| 1178 | } |
| 1179 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1180 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 1181 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 1182 | { |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1183 | struct vxlan_sock *vs; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1184 | struct vxlanhdr *vxh; |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1185 | u32 flags, vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1186 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1187 | /* Need Vxlan and inner Ethernet header to be present */ |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 1188 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1189 | goto error; |
| 1190 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 1191 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1192 | flags = ntohl(vxh->vx_flags); |
| 1193 | vni = ntohl(vxh->vx_vni); |
| 1194 | |
| 1195 | if (flags & VXLAN_HF_VNI) { |
| 1196 | flags &= ~VXLAN_HF_VNI; |
| 1197 | } else { |
| 1198 | /* VNI flag always required to be set */ |
| 1199 | goto bad_flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1202 | if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1203 | goto drop; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1204 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1205 | |
Pravin B Shelar | 559835ea | 2013-09-24 10:25:40 -0700 | [diff] [blame] | 1206 | vs = rcu_dereference_sk_user_data(sk); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1207 | if (!vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1208 | goto drop; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1209 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1210 | if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { |
| 1211 | vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni); |
| 1212 | if (!vxh) |
| 1213 | goto drop; |
| 1214 | |
| 1215 | flags &= ~VXLAN_HF_RCO; |
| 1216 | vni &= VXLAN_VID_MASK; |
| 1217 | } |
| 1218 | |
| 1219 | if (flags || (vni & ~VXLAN_VID_MASK)) { |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1220 | /* If there are any unprocessed flags remaining treat |
| 1221 | * this as a malformed packet. This behavior diverges from |
| 1222 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved |
| 1223 | * in reserved fields are to be ignored. The approach here |
| 1224 | * maintains compatbility with previous stack code, and also |
| 1225 | * is more robust and provides a little more security in |
| 1226 | * adding extensions to VXLAN. |
| 1227 | */ |
| 1228 | |
| 1229 | goto bad_flags; |
| 1230 | } |
| 1231 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1232 | vs->rcv(vs, skb, vxh->vx_vni); |
| 1233 | return 0; |
| 1234 | |
| 1235 | drop: |
| 1236 | /* Consume bad packet */ |
| 1237 | kfree_skb(skb); |
| 1238 | return 0; |
| 1239 | |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1240 | bad_flags: |
| 1241 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
| 1242 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); |
| 1243 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1244 | error: |
| 1245 | /* Return non vxlan pkt */ |
| 1246 | return 1; |
| 1247 | } |
| 1248 | |
| 1249 | static void vxlan_rcv(struct vxlan_sock *vs, |
| 1250 | struct sk_buff *skb, __be32 vx_vni) |
| 1251 | { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1252 | struct iphdr *oip = NULL; |
| 1253 | struct ipv6hdr *oip6 = NULL; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1254 | struct vxlan_dev *vxlan; |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 1255 | struct pcpu_sw_netstats *stats; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1256 | union vxlan_addr saddr; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1257 | __u32 vni; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1258 | int err = 0; |
| 1259 | union vxlan_addr *remote_ip; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1260 | |
| 1261 | vni = ntohl(vx_vni) >> 8; |
| 1262 | /* Is this VNI defined? */ |
| 1263 | vxlan = vxlan_vs_find_vni(vs, vni); |
| 1264 | if (!vxlan) |
| 1265 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1266 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1267 | remote_ip = &vxlan->default_dst.remote_ip; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1268 | skb_reset_mac_header(skb); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1269 | skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev))); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1270 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
Tom Herbert | f79b064 | 2014-06-14 23:24:36 -0700 | [diff] [blame] | 1271 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* Ignore packet loops (and multicast echo) */ |
Joe Perches | 7367d0b | 2013-09-01 11:51:23 -0700 | [diff] [blame] | 1274 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1275 | goto drop; |
| 1276 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 1277 | /* Re-examine inner Ethernet packet */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1278 | if (remote_ip->sa.sa_family == AF_INET) { |
| 1279 | oip = ip_hdr(skb); |
| 1280 | saddr.sin.sin_addr.s_addr = oip->saddr; |
| 1281 | saddr.sa.sa_family = AF_INET; |
| 1282 | #if IS_ENABLED(CONFIG_IPV6) |
| 1283 | } else { |
| 1284 | oip6 = ipv6_hdr(skb); |
| 1285 | saddr.sin6.sin6_addr = oip6->saddr; |
| 1286 | saddr.sa.sa_family = AF_INET6; |
| 1287 | #endif |
| 1288 | } |
| 1289 | |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1290 | if ((vxlan->flags & VXLAN_F_LEARN) && |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1291 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source)) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 1292 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1293 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1294 | skb_reset_network_header(skb); |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1295 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1296 | if (oip6) |
| 1297 | err = IP6_ECN_decapsulate(oip6, skb); |
| 1298 | if (oip) |
| 1299 | err = IP_ECN_decapsulate(oip, skb); |
| 1300 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1301 | if (unlikely(err)) { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1302 | if (log_ecn_error) { |
| 1303 | if (oip6) |
| 1304 | net_info_ratelimited("non-ECT from %pI6\n", |
| 1305 | &oip6->saddr); |
| 1306 | if (oip) |
| 1307 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 1308 | &oip->saddr, oip->tos); |
| 1309 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1310 | if (err > 1) { |
| 1311 | ++vxlan->dev->stats.rx_frame_errors; |
| 1312 | ++vxlan->dev->stats.rx_errors; |
| 1313 | goto drop; |
| 1314 | } |
| 1315 | } |
| 1316 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1317 | stats = this_cpu_ptr(vxlan->dev->tstats); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1318 | u64_stats_update_begin(&stats->syncp); |
| 1319 | stats->rx_packets++; |
| 1320 | stats->rx_bytes += skb->len; |
| 1321 | u64_stats_update_end(&stats->syncp); |
| 1322 | |
| 1323 | netif_rx(skb); |
| 1324 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1325 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1326 | drop: |
| 1327 | /* Consume bad packet */ |
| 1328 | kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1331 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
| 1332 | { |
| 1333 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1334 | struct arphdr *parp; |
| 1335 | u8 *arpptr, *sha; |
| 1336 | __be32 sip, tip; |
| 1337 | struct neighbour *n; |
| 1338 | |
| 1339 | if (dev->flags & IFF_NOARP) |
| 1340 | goto out; |
| 1341 | |
| 1342 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { |
| 1343 | dev->stats.tx_dropped++; |
| 1344 | goto out; |
| 1345 | } |
| 1346 | parp = arp_hdr(skb); |
| 1347 | |
| 1348 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && |
| 1349 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 1350 | parp->ar_pro != htons(ETH_P_IP) || |
| 1351 | parp->ar_op != htons(ARPOP_REQUEST) || |
| 1352 | parp->ar_hln != dev->addr_len || |
| 1353 | parp->ar_pln != 4) |
| 1354 | goto out; |
| 1355 | arpptr = (u8 *)parp + sizeof(struct arphdr); |
| 1356 | sha = arpptr; |
| 1357 | arpptr += dev->addr_len; /* sha */ |
| 1358 | memcpy(&sip, arpptr, sizeof(sip)); |
| 1359 | arpptr += sizeof(sip); |
| 1360 | arpptr += dev->addr_len; /* tha */ |
| 1361 | memcpy(&tip, arpptr, sizeof(tip)); |
| 1362 | |
| 1363 | if (ipv4_is_loopback(tip) || |
| 1364 | ipv4_is_multicast(tip)) |
| 1365 | goto out; |
| 1366 | |
| 1367 | n = neigh_lookup(&arp_tbl, &tip, dev); |
| 1368 | |
| 1369 | if (n) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1370 | struct vxlan_fdb *f; |
| 1371 | struct sk_buff *reply; |
| 1372 | |
| 1373 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 1374 | neigh_release(n); |
| 1375 | goto out; |
| 1376 | } |
| 1377 | |
| 1378 | f = vxlan_find_mac(vxlan, n->ha); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1379 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1380 | /* bridge-local neighbor */ |
| 1381 | neigh_release(n); |
| 1382 | goto out; |
| 1383 | } |
| 1384 | |
| 1385 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, |
| 1386 | n->ha, sha); |
| 1387 | |
| 1388 | neigh_release(n); |
| 1389 | |
David Stevens | 7346135 | 2014-03-18 12:32:29 -0400 | [diff] [blame] | 1390 | if (reply == NULL) |
| 1391 | goto out; |
| 1392 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1393 | skb_reset_mac_header(reply); |
| 1394 | __skb_pull(reply, skb_network_offset(reply)); |
| 1395 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 1396 | reply->pkt_type = PACKET_HOST; |
| 1397 | |
| 1398 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 1399 | dev->stats.rx_dropped++; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1400 | } else if (vxlan->flags & VXLAN_F_L3MISS) { |
| 1401 | union vxlan_addr ipa = { |
| 1402 | .sin.sin_addr.s_addr = tip, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 1403 | .sin.sin_family = AF_INET, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1404 | }; |
| 1405 | |
| 1406 | vxlan_ip_miss(dev, &ipa); |
| 1407 | } |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1408 | out: |
| 1409 | consume_skb(skb); |
| 1410 | return NETDEV_TX_OK; |
| 1411 | } |
| 1412 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1413 | #if IS_ENABLED(CONFIG_IPV6) |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1414 | static struct sk_buff *vxlan_na_create(struct sk_buff *request, |
| 1415 | struct neighbour *n, bool isrouter) |
| 1416 | { |
| 1417 | struct net_device *dev = request->dev; |
| 1418 | struct sk_buff *reply; |
| 1419 | struct nd_msg *ns, *na; |
| 1420 | struct ipv6hdr *pip6; |
| 1421 | u8 *daddr; |
| 1422 | int na_olen = 8; /* opt hdr + ETH_ALEN for target */ |
| 1423 | int ns_olen; |
| 1424 | int i, len; |
| 1425 | |
| 1426 | if (dev == NULL) |
| 1427 | return NULL; |
| 1428 | |
| 1429 | len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + |
| 1430 | sizeof(*na) + na_olen + dev->needed_tailroom; |
| 1431 | reply = alloc_skb(len, GFP_ATOMIC); |
| 1432 | if (reply == NULL) |
| 1433 | return NULL; |
| 1434 | |
| 1435 | reply->protocol = htons(ETH_P_IPV6); |
| 1436 | reply->dev = dev; |
| 1437 | skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); |
| 1438 | skb_push(reply, sizeof(struct ethhdr)); |
| 1439 | skb_set_mac_header(reply, 0); |
| 1440 | |
| 1441 | ns = (struct nd_msg *)skb_transport_header(request); |
| 1442 | |
| 1443 | daddr = eth_hdr(request)->h_source; |
| 1444 | ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns); |
| 1445 | for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { |
| 1446 | if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { |
| 1447 | daddr = ns->opt + i + sizeof(struct nd_opt_hdr); |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /* Ethernet header */ |
| 1453 | ether_addr_copy(eth_hdr(reply)->h_dest, daddr); |
| 1454 | ether_addr_copy(eth_hdr(reply)->h_source, n->ha); |
| 1455 | eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); |
| 1456 | reply->protocol = htons(ETH_P_IPV6); |
| 1457 | |
| 1458 | skb_pull(reply, sizeof(struct ethhdr)); |
| 1459 | skb_set_network_header(reply, 0); |
| 1460 | skb_put(reply, sizeof(struct ipv6hdr)); |
| 1461 | |
| 1462 | /* IPv6 header */ |
| 1463 | |
| 1464 | pip6 = ipv6_hdr(reply); |
| 1465 | memset(pip6, 0, sizeof(struct ipv6hdr)); |
| 1466 | pip6->version = 6; |
| 1467 | pip6->priority = ipv6_hdr(request)->priority; |
| 1468 | pip6->nexthdr = IPPROTO_ICMPV6; |
| 1469 | pip6->hop_limit = 255; |
| 1470 | pip6->daddr = ipv6_hdr(request)->saddr; |
| 1471 | pip6->saddr = *(struct in6_addr *)n->primary_key; |
| 1472 | |
| 1473 | skb_pull(reply, sizeof(struct ipv6hdr)); |
| 1474 | skb_set_transport_header(reply, 0); |
| 1475 | |
| 1476 | na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen); |
| 1477 | |
| 1478 | /* Neighbor Advertisement */ |
| 1479 | memset(na, 0, sizeof(*na)+na_olen); |
| 1480 | na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; |
| 1481 | na->icmph.icmp6_router = isrouter; |
| 1482 | na->icmph.icmp6_override = 1; |
| 1483 | na->icmph.icmp6_solicited = 1; |
| 1484 | na->target = ns->target; |
| 1485 | ether_addr_copy(&na->opt[2], n->ha); |
| 1486 | na->opt[0] = ND_OPT_TARGET_LL_ADDR; |
| 1487 | na->opt[1] = na_olen >> 3; |
| 1488 | |
| 1489 | na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, |
| 1490 | &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, |
| 1491 | csum_partial(na, sizeof(*na)+na_olen, 0)); |
| 1492 | |
| 1493 | pip6->payload_len = htons(sizeof(*na)+na_olen); |
| 1494 | |
| 1495 | skb_push(reply, sizeof(struct ipv6hdr)); |
| 1496 | |
| 1497 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 1498 | |
| 1499 | return reply; |
| 1500 | } |
| 1501 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1502 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb) |
| 1503 | { |
| 1504 | struct vxlan_dev *vxlan = netdev_priv(dev); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1505 | struct nd_msg *msg; |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1506 | const struct ipv6hdr *iphdr; |
| 1507 | const struct in6_addr *saddr, *daddr; |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1508 | struct neighbour *n; |
| 1509 | struct inet6_dev *in6_dev; |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1510 | |
| 1511 | in6_dev = __in6_dev_get(dev); |
| 1512 | if (!in6_dev) |
| 1513 | goto out; |
| 1514 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1515 | iphdr = ipv6_hdr(skb); |
| 1516 | saddr = &iphdr->saddr; |
| 1517 | daddr = &iphdr->daddr; |
| 1518 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1519 | msg = (struct nd_msg *)skb_transport_header(skb); |
| 1520 | if (msg->icmph.icmp6_code != 0 || |
| 1521 | msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) |
| 1522 | goto out; |
| 1523 | |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1524 | if (ipv6_addr_loopback(daddr) || |
| 1525 | ipv6_addr_is_multicast(&msg->target)) |
| 1526 | goto out; |
| 1527 | |
| 1528 | n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1529 | |
| 1530 | if (n) { |
| 1531 | struct vxlan_fdb *f; |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1532 | struct sk_buff *reply; |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1533 | |
| 1534 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 1535 | neigh_release(n); |
| 1536 | goto out; |
| 1537 | } |
| 1538 | |
| 1539 | f = vxlan_find_mac(vxlan, n->ha); |
| 1540 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
| 1541 | /* bridge-local neighbor */ |
| 1542 | neigh_release(n); |
| 1543 | goto out; |
| 1544 | } |
| 1545 | |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1546 | reply = vxlan_na_create(skb, n, |
| 1547 | !!(f ? f->flags & NTF_ROUTER : 0)); |
| 1548 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1549 | neigh_release(n); |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1550 | |
| 1551 | if (reply == NULL) |
| 1552 | goto out; |
| 1553 | |
| 1554 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 1555 | dev->stats.rx_dropped++; |
| 1556 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1557 | } else if (vxlan->flags & VXLAN_F_L3MISS) { |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1558 | union vxlan_addr ipa = { |
| 1559 | .sin6.sin6_addr = msg->target, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 1560 | .sin6.sin6_family = AF_INET6, |
David Stevens | 4b29dba | 2014-03-24 10:39:58 -0400 | [diff] [blame] | 1561 | }; |
| 1562 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 1563 | vxlan_ip_miss(dev, &ipa); |
| 1564 | } |
| 1565 | |
| 1566 | out: |
| 1567 | consume_skb(skb); |
| 1568 | return NETDEV_TX_OK; |
| 1569 | } |
| 1570 | #endif |
| 1571 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1572 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
| 1573 | { |
| 1574 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1575 | struct neighbour *n; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 1578 | return false; |
| 1579 | |
| 1580 | n = NULL; |
| 1581 | switch (ntohs(eth_hdr(skb)->h_proto)) { |
| 1582 | case ETH_P_IP: |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 1583 | { |
| 1584 | struct iphdr *pip; |
| 1585 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1586 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 1587 | return false; |
| 1588 | pip = ip_hdr(skb); |
| 1589 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1590 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { |
| 1591 | union vxlan_addr ipa = { |
| 1592 | .sin.sin_addr.s_addr = pip->daddr, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 1593 | .sin.sin_family = AF_INET, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1594 | }; |
| 1595 | |
| 1596 | vxlan_ip_miss(dev, &ipa); |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1600 | break; |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 1601 | } |
| 1602 | #if IS_ENABLED(CONFIG_IPV6) |
| 1603 | case ETH_P_IPV6: |
| 1604 | { |
| 1605 | struct ipv6hdr *pip6; |
| 1606 | |
| 1607 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 1608 | return false; |
| 1609 | pip6 = ipv6_hdr(skb); |
| 1610 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); |
| 1611 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { |
| 1612 | union vxlan_addr ipa = { |
| 1613 | .sin6.sin6_addr = pip6->daddr, |
Gerhard Stenzel | a45e92a | 2014-08-22 21:34:16 +0200 | [diff] [blame] | 1614 | .sin6.sin6_family = AF_INET6, |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 1615 | }; |
| 1616 | |
| 1617 | vxlan_ip_miss(dev, &ipa); |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
| 1621 | break; |
| 1622 | } |
| 1623 | #endif |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1624 | default: |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
| 1628 | if (n) { |
| 1629 | bool diff; |
| 1630 | |
Joe Perches | 7367d0b | 2013-09-01 11:51:23 -0700 | [diff] [blame] | 1631 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1632 | if (diff) { |
| 1633 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, |
| 1634 | dev->addr_len); |
| 1635 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); |
| 1636 | } |
| 1637 | neigh_release(n); |
| 1638 | return diff; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1639 | } |
| 1640 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1641 | return false; |
| 1642 | } |
| 1643 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1644 | #if IS_ENABLED(CONFIG_IPV6) |
Nicolas Dichtel | 1179618 | 2013-09-02 15:34:55 +0200 | [diff] [blame] | 1645 | static int vxlan6_xmit_skb(struct vxlan_sock *vs, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1646 | struct dst_entry *dst, struct sk_buff *skb, |
| 1647 | struct net_device *dev, struct in6_addr *saddr, |
| 1648 | struct in6_addr *daddr, __u8 prio, __u8 ttl, |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1649 | __be16 src_port, __be16 dst_port, __be32 vni, |
| 1650 | bool xnet) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1651 | { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1652 | struct vxlanhdr *vxh; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1653 | int min_headroom; |
| 1654 | int err; |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 1655 | bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk); |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1656 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
| 1657 | u16 hdrlen = sizeof(struct vxlanhdr); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1658 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1659 | if ((vs->flags & VXLAN_F_REMCSUM_TX) && |
| 1660 | skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1661 | int csum_start = skb_checksum_start_offset(skb); |
| 1662 | |
| 1663 | if (csum_start <= VXLAN_MAX_REMCSUM_START && |
| 1664 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && |
| 1665 | (skb->csum_offset == offsetof(struct udphdr, check) || |
| 1666 | skb->csum_offset == offsetof(struct tcphdr, check))) { |
| 1667 | udp_sum = false; |
| 1668 | type |= SKB_GSO_TUNNEL_REMCSUM; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | skb = iptunnel_handle_offloads(skb, udp_sum, type); |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1673 | if (IS_ERR(skb)) { |
| 1674 | err = -EINVAL; |
| 1675 | goto err; |
| 1676 | } |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1677 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1678 | skb_scrub_packet(skb, xnet); |
Nicolas Dichtel | 963a88b | 2013-09-02 15:34:57 +0200 | [diff] [blame] | 1679 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1680 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
| 1681 | + VXLAN_HLEN + sizeof(struct ipv6hdr) |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1682 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1683 | |
| 1684 | /* Need space for new headers (invalidates iph ptr) */ |
| 1685 | err = skb_cow_head(skb, min_headroom); |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1686 | if (unlikely(err)) { |
| 1687 | kfree_skb(skb); |
| 1688 | goto err; |
| 1689 | } |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1690 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 1691 | skb = vlan_hwaccel_push_inside(skb); |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1692 | if (WARN_ON(!skb)) { |
| 1693 | err = -ENOMEM; |
| 1694 | goto err; |
| 1695 | } |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1696 | |
| 1697 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1698 | vxh->vx_flags = htonl(VXLAN_HF_VNI); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1699 | vxh->vx_vni = vni; |
| 1700 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1701 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
| 1702 | u32 data = (skb_checksum_start_offset(skb) - hdrlen) >> |
| 1703 | VXLAN_RCO_SHIFT; |
| 1704 | |
| 1705 | if (skb->csum_offset == offsetof(struct udphdr, check)) |
| 1706 | data |= VXLAN_RCO_UDP; |
| 1707 | |
| 1708 | vxh->vx_vni |= htonl(data); |
| 1709 | vxh->vx_flags |= htonl(VXLAN_HF_RCO); |
| 1710 | |
| 1711 | if (!skb_is_gso(skb)) { |
| 1712 | skb->ip_summed = CHECKSUM_NONE; |
| 1713 | skb->encapsulation = 0; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Tom Herbert | 996c9fd | 2014-09-29 20:22:33 -0700 | [diff] [blame] | 1717 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 1718 | |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 1719 | udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio, |
| 1720 | ttl, src_port, dst_port); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1721 | return 0; |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1722 | err: |
| 1723 | dst_release(dst); |
| 1724 | return err; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1725 | } |
| 1726 | #endif |
| 1727 | |
Nicolas Dichtel | 1179618 | 2013-09-02 15:34:55 +0200 | [diff] [blame] | 1728 | int vxlan_xmit_skb(struct vxlan_sock *vs, |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1729 | struct rtable *rt, struct sk_buff *skb, |
| 1730 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1731 | __be16 src_port, __be16 dst_port, __be32 vni, bool xnet) |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1732 | { |
| 1733 | struct vxlanhdr *vxh; |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1734 | int min_headroom; |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1735 | int err; |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 1736 | bool udp_sum = !vs->sock->sk->sk_no_check_tx; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1737 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
| 1738 | u16 hdrlen = sizeof(struct vxlanhdr); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1739 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1740 | if ((vs->flags & VXLAN_F_REMCSUM_TX) && |
| 1741 | skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1742 | int csum_start = skb_checksum_start_offset(skb); |
| 1743 | |
| 1744 | if (csum_start <= VXLAN_MAX_REMCSUM_START && |
| 1745 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && |
| 1746 | (skb->csum_offset == offsetof(struct udphdr, check) || |
| 1747 | skb->csum_offset == offsetof(struct tcphdr, check))) { |
| 1748 | udp_sum = false; |
| 1749 | type |= SKB_GSO_TUNNEL_REMCSUM; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | skb = iptunnel_handle_offloads(skb, udp_sum, type); |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 1754 | if (IS_ERR(skb)) |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1755 | return PTR_ERR(skb); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1756 | |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1757 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 1758 | + VXLAN_HLEN + sizeof(struct iphdr) |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1759 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1760 | |
| 1761 | /* Need space for new headers (invalidates iph ptr) */ |
| 1762 | err = skb_cow_head(skb, min_headroom); |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1763 | if (unlikely(err)) { |
| 1764 | kfree_skb(skb); |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1765 | return err; |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1766 | } |
Pravin B Shelar | 649c5b8 | 2013-08-19 11:23:22 -0700 | [diff] [blame] | 1767 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 1768 | skb = vlan_hwaccel_push_inside(skb); |
| 1769 | if (WARN_ON(!skb)) |
| 1770 | return -ENOMEM; |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 1771 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1772 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 1773 | vxh->vx_flags = htonl(VXLAN_HF_VNI); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1774 | vxh->vx_vni = vni; |
| 1775 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 1776 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
| 1777 | u32 data = (skb_checksum_start_offset(skb) - hdrlen) >> |
| 1778 | VXLAN_RCO_SHIFT; |
| 1779 | |
| 1780 | if (skb->csum_offset == offsetof(struct udphdr, check)) |
| 1781 | data |= VXLAN_RCO_UDP; |
| 1782 | |
| 1783 | vxh->vx_vni |= htonl(data); |
| 1784 | vxh->vx_flags |= htonl(VXLAN_HF_RCO); |
| 1785 | |
| 1786 | if (!skb_is_gso(skb)) { |
| 1787 | skb->ip_summed = CHECKSUM_NONE; |
| 1788 | skb->encapsulation = 0; |
| 1789 | } |
| 1790 | } |
| 1791 | |
Tom Herbert | 996c9fd | 2014-09-29 20:22:33 -0700 | [diff] [blame] | 1792 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
| 1793 | |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 1794 | return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos, |
| 1795 | ttl, df, src_port, dst_port, xnet); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1796 | } |
| 1797 | EXPORT_SYMBOL_GPL(vxlan_xmit_skb); |
| 1798 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1799 | /* Bypass encapsulation if the destination is local */ |
| 1800 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, |
| 1801 | struct vxlan_dev *dst_vxlan) |
| 1802 | { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 1803 | struct pcpu_sw_netstats *tx_stats, *rx_stats; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1804 | union vxlan_addr loopback; |
| 1805 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 1806 | struct net_device *dev = skb->dev; |
| 1807 | int len = skb->len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1808 | |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 1809 | tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
| 1810 | rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1811 | skb->pkt_type = PACKET_HOST; |
| 1812 | skb->encapsulation = 0; |
| 1813 | skb->dev = dst_vxlan->dev; |
| 1814 | __skb_pull(skb, skb_network_offset(skb)); |
| 1815 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1816 | if (remote_ip->sa.sa_family == AF_INET) { |
| 1817 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 1818 | loopback.sa.sa_family = AF_INET; |
| 1819 | #if IS_ENABLED(CONFIG_IPV6) |
| 1820 | } else { |
| 1821 | loopback.sin6.sin6_addr = in6addr_loopback; |
| 1822 | loopback.sa.sa_family = AF_INET6; |
| 1823 | #endif |
| 1824 | } |
| 1825 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1826 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1827 | vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1828 | |
| 1829 | u64_stats_update_begin(&tx_stats->syncp); |
| 1830 | tx_stats->tx_packets++; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 1831 | tx_stats->tx_bytes += len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1832 | u64_stats_update_end(&tx_stats->syncp); |
| 1833 | |
| 1834 | if (netif_rx(skb) == NET_RX_SUCCESS) { |
| 1835 | u64_stats_update_begin(&rx_stats->syncp); |
| 1836 | rx_stats->rx_packets++; |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 1837 | rx_stats->rx_bytes += len; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1838 | u64_stats_update_end(&rx_stats->syncp); |
| 1839 | } else { |
Li RongQing | ce6502a | 2014-10-16 08:49:41 +0800 | [diff] [blame] | 1840 | dev->stats.rx_dropped++; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1844 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
| 1845 | struct vxlan_rdst *rdst, bool did_rsc) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1846 | { |
| 1847 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1848 | struct rtable *rt = NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1849 | const struct iphdr *old_iph; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1850 | struct flowi4 fl4; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1851 | union vxlan_addr *dst; |
| 1852 | __be16 src_port = 0, dst_port; |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 1853 | u32 vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1854 | __be16 df = 0; |
| 1855 | __u8 tos, ttl; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1856 | int err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1857 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1858 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1859 | vni = rdst->remote_vni; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1860 | dst = &rdst->remote_ip; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1861 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1862 | if (vxlan_addr_any(dst)) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1863 | if (did_rsc) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1864 | /* short-circuited back to local bridge */ |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1865 | vxlan_encap_bypass(skb, vxlan, vxlan); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1866 | return; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1867 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1868 | goto drop; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1869 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1870 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1871 | old_iph = ip_hdr(skb); |
| 1872 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1873 | ttl = vxlan->ttl; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1874 | if (!ttl && vxlan_addr_multicast(dst)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1875 | ttl = 1; |
| 1876 | |
| 1877 | tos = vxlan->tos; |
| 1878 | if (tos == 1) |
Pravin B Shelar | 206aaaf | 2013-03-25 14:49:53 +0000 | [diff] [blame] | 1879 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1880 | |
Tom Herbert | 535fb8d | 2014-07-01 21:32:49 -0700 | [diff] [blame] | 1881 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min, |
| 1882 | vxlan->port_max, true); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1883 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1884 | if (dst->sa.sa_family == AF_INET) { |
| 1885 | memset(&fl4, 0, sizeof(fl4)); |
| 1886 | fl4.flowi4_oif = rdst->remote_ifindex; |
| 1887 | fl4.flowi4_tos = RT_TOS(tos); |
| 1888 | fl4.daddr = dst->sin.sin_addr.s_addr; |
| 1889 | fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr; |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1890 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1891 | rt = ip_route_output_key(vxlan->net, &fl4); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1892 | if (IS_ERR(rt)) { |
| 1893 | netdev_dbg(dev, "no route to %pI4\n", |
| 1894 | &dst->sin.sin_addr.s_addr); |
| 1895 | dev->stats.tx_carrier_errors++; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1896 | goto tx_error; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | if (rt->dst.dev == dev) { |
| 1900 | netdev_dbg(dev, "circular route to %pI4\n", |
| 1901 | &dst->sin.sin_addr.s_addr); |
| 1902 | dev->stats.collisions++; |
Fan Du | fffc15a | 2013-12-09 10:33:53 +0800 | [diff] [blame] | 1903 | goto rt_tx_error; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* Bypass encapsulation if the destination is local */ |
| 1907 | if (rt->rt_flags & RTCF_LOCAL && |
| 1908 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
| 1909 | struct vxlan_dev *dst_vxlan; |
| 1910 | |
| 1911 | ip_rt_put(rt); |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 1912 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, |
| 1913 | dst->sa.sa_family, dst_port); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1914 | if (!dst_vxlan) |
| 1915 | goto tx_error; |
| 1916 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
| 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
| 1921 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
| 1922 | |
Andy Zhou | 3c4d1da | 2014-09-23 01:44:51 -0700 | [diff] [blame] | 1923 | err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb, |
| 1924 | fl4.saddr, dst->sin.sin_addr.s_addr, |
| 1925 | tos, ttl, df, src_port, dst_port, |
| 1926 | htonl(vni << 8), |
| 1927 | !net_eq(vxlan->net, dev_net(vxlan->dev))); |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1928 | if (err < 0) { |
| 1929 | /* skb is already freed. */ |
| 1930 | skb = NULL; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1931 | goto rt_tx_error; |
Pravin B Shelar | 74f4727 | 2014-12-23 16:20:36 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1934 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
| 1935 | #if IS_ENABLED(CONFIG_IPV6) |
| 1936 | } else { |
| 1937 | struct sock *sk = vxlan->vn_sock->sock->sk; |
| 1938 | struct dst_entry *ndst; |
| 1939 | struct flowi6 fl6; |
| 1940 | u32 flags; |
| 1941 | |
| 1942 | memset(&fl6, 0, sizeof(fl6)); |
| 1943 | fl6.flowi6_oif = rdst->remote_ifindex; |
| 1944 | fl6.daddr = dst->sin6.sin6_addr; |
| 1945 | fl6.saddr = vxlan->saddr.sin6.sin6_addr; |
Cong Wang | 8c1bb79 | 2013-09-02 10:06:51 +0800 | [diff] [blame] | 1946 | fl6.flowi6_proto = IPPROTO_UDP; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1947 | |
| 1948 | if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) { |
| 1949 | netdev_dbg(dev, "no route to %pI6\n", |
| 1950 | &dst->sin6.sin6_addr); |
| 1951 | dev->stats.tx_carrier_errors++; |
| 1952 | goto tx_error; |
| 1953 | } |
| 1954 | |
| 1955 | if (ndst->dev == dev) { |
| 1956 | netdev_dbg(dev, "circular route to %pI6\n", |
| 1957 | &dst->sin6.sin6_addr); |
| 1958 | dst_release(ndst); |
| 1959 | dev->stats.collisions++; |
| 1960 | goto tx_error; |
| 1961 | } |
| 1962 | |
| 1963 | /* Bypass encapsulation if the destination is local */ |
| 1964 | flags = ((struct rt6_info *)ndst)->rt6i_flags; |
| 1965 | if (flags & RTF_LOCAL && |
| 1966 | !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
| 1967 | struct vxlan_dev *dst_vxlan; |
| 1968 | |
| 1969 | dst_release(ndst); |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 1970 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, |
| 1971 | dst->sa.sa_family, dst_port); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1972 | if (!dst_vxlan) |
| 1973 | goto tx_error; |
| 1974 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
| 1975 | return; |
| 1976 | } |
| 1977 | |
| 1978 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
| 1979 | |
Nicolas Dichtel | 1179618 | 2013-09-02 15:34:55 +0200 | [diff] [blame] | 1980 | err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1981 | dev, &fl6.saddr, &fl6.daddr, 0, ttl, |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 1982 | src_port, dst_port, htonl(vni << 8), |
| 1983 | !net_eq(vxlan->net, dev_net(vxlan->dev))); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 1984 | #endif |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1985 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1986 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1987 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1988 | |
| 1989 | drop: |
| 1990 | dev->stats.tx_dropped++; |
| 1991 | goto tx_free; |
| 1992 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 1993 | rt_tx_error: |
| 1994 | ip_rt_put(rt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1995 | tx_error: |
| 1996 | dev->stats.tx_errors++; |
| 1997 | tx_free: |
| 1998 | dev_kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2001 | /* Transmit local packets over Vxlan |
| 2002 | * |
| 2003 | * Outer IP header inherits ECN and DF from inner header. |
| 2004 | * Outer UDP destination is the VXLAN assigned port. |
| 2005 | * source port is based on hash of flow |
| 2006 | */ |
| 2007 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 2008 | { |
| 2009 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 2010 | struct ethhdr *eth; |
| 2011 | bool did_rsc = false; |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2012 | struct vxlan_rdst *rdst, *fdst = NULL; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2013 | struct vxlan_fdb *f; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2014 | |
| 2015 | skb_reset_mac_header(skb); |
| 2016 | eth = eth_hdr(skb); |
| 2017 | |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2018 | if ((vxlan->flags & VXLAN_F_PROXY)) { |
| 2019 | if (ntohs(eth->h_proto) == ETH_P_ARP) |
| 2020 | return arp_reduce(dev, skb); |
| 2021 | #if IS_ENABLED(CONFIG_IPV6) |
| 2022 | else if (ntohs(eth->h_proto) == ETH_P_IPV6 && |
Li RongQing | 91269e3 | 2014-10-16 09:17:18 +0800 | [diff] [blame] | 2023 | pskb_may_pull(skb, sizeof(struct ipv6hdr) |
| 2024 | + sizeof(struct nd_msg)) && |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2025 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { |
| 2026 | struct nd_msg *msg; |
| 2027 | |
| 2028 | msg = (struct nd_msg *)skb_transport_header(skb); |
| 2029 | if (msg->icmph.icmp6_code == 0 && |
| 2030 | msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) |
| 2031 | return neigh_reduce(dev, skb); |
| 2032 | } |
Li RongQing | 7a9f526 | 2014-10-17 14:06:16 +0800 | [diff] [blame] | 2033 | eth = eth_hdr(skb); |
Cong Wang | f564f45 | 2013-08-31 13:44:36 +0800 | [diff] [blame] | 2034 | #endif |
| 2035 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2036 | |
| 2037 | f = vxlan_find_mac(vxlan, eth->h_dest); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 2038 | did_rsc = false; |
| 2039 | |
| 2040 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && |
Cong Wang | e15a00a | 2013-08-31 13:44:34 +0800 | [diff] [blame] | 2041 | (ntohs(eth->h_proto) == ETH_P_IP || |
| 2042 | ntohs(eth->h_proto) == ETH_P_IPV6)) { |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 2043 | did_rsc = route_shortcircuit(dev, skb); |
| 2044 | if (did_rsc) |
| 2045 | f = vxlan_find_mac(vxlan, eth->h_dest); |
| 2046 | } |
| 2047 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2048 | if (f == NULL) { |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2049 | f = vxlan_find_mac(vxlan, all_zeros_mac); |
| 2050 | if (f == NULL) { |
| 2051 | if ((vxlan->flags & VXLAN_F_L2MISS) && |
| 2052 | !is_multicast_ether_addr(eth->h_dest)) |
| 2053 | vxlan_fdb_miss(vxlan, eth->h_dest); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2054 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2055 | dev->stats.tx_dropped++; |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2056 | kfree_skb(skb); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2057 | return NETDEV_TX_OK; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 2058 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2061 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
| 2062 | struct sk_buff *skb1; |
| 2063 | |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2064 | if (!fdst) { |
| 2065 | fdst = rdst; |
| 2066 | continue; |
| 2067 | } |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2068 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 2069 | if (skb1) |
| 2070 | vxlan_xmit_one(skb1, dev, rdst, did_rsc); |
| 2071 | } |
| 2072 | |
Eric Dumazet | 8f646c9 | 2014-01-06 09:54:31 -0800 | [diff] [blame] | 2073 | if (fdst) |
| 2074 | vxlan_xmit_one(skb, dev, fdst, did_rsc); |
| 2075 | else |
| 2076 | kfree_skb(skb); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2077 | return NETDEV_TX_OK; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2080 | /* Walk the forwarding table and purge stale entries */ |
| 2081 | static void vxlan_cleanup(unsigned long arg) |
| 2082 | { |
| 2083 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; |
| 2084 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
| 2085 | unsigned int h; |
| 2086 | |
| 2087 | if (!netif_running(vxlan->dev)) |
| 2088 | return; |
| 2089 | |
| 2090 | spin_lock_bh(&vxlan->hash_lock); |
| 2091 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 2092 | struct hlist_node *p, *n; |
| 2093 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 2094 | struct vxlan_fdb *f |
| 2095 | = container_of(p, struct vxlan_fdb, hlist); |
| 2096 | unsigned long timeout; |
| 2097 | |
stephen hemminger | 3c17286 | 2012-10-26 06:24:34 +0000 | [diff] [blame] | 2098 | if (f->state & NUD_PERMANENT) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2099 | continue; |
| 2100 | |
| 2101 | timeout = f->used + vxlan->age_interval * HZ; |
| 2102 | if (time_before_eq(timeout, jiffies)) { |
| 2103 | netdev_dbg(vxlan->dev, |
| 2104 | "garbage collect %pM\n", |
| 2105 | f->eth_addr); |
| 2106 | f->state = NUD_STALE; |
| 2107 | vxlan_fdb_destroy(vxlan, f); |
| 2108 | } else if (time_before(timeout, next_timer)) |
| 2109 | next_timer = timeout; |
| 2110 | } |
| 2111 | } |
| 2112 | spin_unlock_bh(&vxlan->hash_lock); |
| 2113 | |
| 2114 | mod_timer(&vxlan->age_timer, next_timer); |
| 2115 | } |
| 2116 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2117 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) |
| 2118 | { |
| 2119 | __u32 vni = vxlan->default_dst.remote_vni; |
| 2120 | |
| 2121 | vxlan->vn_sock = vs; |
| 2122 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); |
| 2123 | } |
| 2124 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2125 | /* Setup stats when device is created */ |
| 2126 | static int vxlan_init(struct net_device *dev) |
| 2127 | { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2128 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2129 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2130 | struct vxlan_sock *vs; |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 2131 | bool ipv6 = vxlan->flags & VXLAN_F_IPV6; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2132 | |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 2133 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 2134 | if (!dev->tstats) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2135 | return -ENOMEM; |
| 2136 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2137 | spin_lock(&vn->sock_lock); |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 2138 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
| 2139 | vxlan->dst_port); |
Marcelo Leitner | 00c83b0 | 2014-12-11 10:02:22 -0200 | [diff] [blame] | 2140 | if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2141 | /* If we have a socket with same port already, reuse it */ |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2142 | vxlan_vs_add_dev(vs, vxlan); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2143 | } else { |
| 2144 | /* otherwise make new socket outside of RTNL */ |
| 2145 | dev_hold(dev); |
| 2146 | queue_work(vxlan_wq, &vxlan->sock_work); |
| 2147 | } |
| 2148 | spin_unlock(&vn->sock_lock); |
| 2149 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2150 | return 0; |
| 2151 | } |
| 2152 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 2153 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2154 | { |
| 2155 | struct vxlan_fdb *f; |
| 2156 | |
| 2157 | spin_lock_bh(&vxlan->hash_lock); |
| 2158 | f = __vxlan_find_mac(vxlan, all_zeros_mac); |
| 2159 | if (f) |
| 2160 | vxlan_fdb_destroy(vxlan, f); |
| 2161 | spin_unlock_bh(&vxlan->hash_lock); |
| 2162 | } |
| 2163 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2164 | static void vxlan_uninit(struct net_device *dev) |
| 2165 | { |
| 2166 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2167 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 2168 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 2169 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2170 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2171 | if (vs) |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 2172 | vxlan_sock_release(vs); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2173 | free_percpu(dev->tstats); |
| 2174 | } |
| 2175 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2176 | /* Start ageing timer and join group when device is brought up */ |
| 2177 | static int vxlan_open(struct net_device *dev) |
| 2178 | { |
| 2179 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2180 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 2181 | |
| 2182 | /* socket hasn't been created */ |
| 2183 | if (!vs) |
| 2184 | return -ENOTCONN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2185 | |
Gao feng | 79d4a94 | 2013-12-10 16:37:32 +0800 | [diff] [blame] | 2186 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2187 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 2188 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 2189 | queue_work(vxlan_wq, &vxlan->igmp_join); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | if (vxlan->age_interval) |
| 2193 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
| 2194 | |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
| 2198 | /* Purge the forwarding table */ |
| 2199 | static void vxlan_flush(struct vxlan_dev *vxlan) |
| 2200 | { |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 2201 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2202 | |
| 2203 | spin_lock_bh(&vxlan->hash_lock); |
| 2204 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 2205 | struct hlist_node *p, *n; |
| 2206 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 2207 | struct vxlan_fdb *f |
| 2208 | = container_of(p, struct vxlan_fdb, hlist); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2209 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
| 2210 | if (!is_zero_ether_addr(f->eth_addr)) |
| 2211 | vxlan_fdb_destroy(vxlan, f); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | spin_unlock_bh(&vxlan->hash_lock); |
| 2215 | } |
| 2216 | |
| 2217 | /* Cleanup timer and forwarding table on shutdown */ |
| 2218 | static int vxlan_stop(struct net_device *dev) |
| 2219 | { |
| 2220 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2221 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2222 | struct vxlan_sock *vs = vxlan->vn_sock; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2223 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2224 | if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
Gao feng | 95ab099 | 2013-12-10 16:37:33 +0800 | [diff] [blame] | 2225 | !vxlan_group_used(vn, vxlan)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2226 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 2227 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 2228 | queue_work(vxlan_wq, &vxlan->igmp_leave); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 2229 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2230 | |
| 2231 | del_timer_sync(&vxlan->age_timer); |
| 2232 | |
| 2233 | vxlan_flush(vxlan); |
| 2234 | |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2238 | /* Stub, nothing needs to be done. */ |
| 2239 | static void vxlan_set_multicast_list(struct net_device *dev) |
| 2240 | { |
| 2241 | } |
| 2242 | |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 2243 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
| 2244 | { |
| 2245 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 2246 | struct vxlan_rdst *dst = &vxlan->default_dst; |
| 2247 | struct net_device *lowerdev; |
| 2248 | int max_mtu; |
| 2249 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2250 | lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex); |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 2251 | if (lowerdev == NULL) |
| 2252 | return eth_change_mtu(dev, new_mtu); |
| 2253 | |
| 2254 | if (dst->remote_ip.sa.sa_family == AF_INET6) |
| 2255 | max_mtu = lowerdev->mtu - VXLAN6_HEADROOM; |
| 2256 | else |
| 2257 | max_mtu = lowerdev->mtu - VXLAN_HEADROOM; |
| 2258 | |
| 2259 | if (new_mtu < 68 || new_mtu > max_mtu) |
| 2260 | return -EINVAL; |
| 2261 | |
| 2262 | dev->mtu = new_mtu; |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2266 | static const struct net_device_ops vxlan_netdev_ops = { |
| 2267 | .ndo_init = vxlan_init, |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2268 | .ndo_uninit = vxlan_uninit, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2269 | .ndo_open = vxlan_open, |
| 2270 | .ndo_stop = vxlan_stop, |
| 2271 | .ndo_start_xmit = vxlan_xmit, |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 2272 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2273 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
Daniel Borkmann | 345010b | 2013-12-18 00:21:08 +0100 | [diff] [blame] | 2274 | .ndo_change_mtu = vxlan_change_mtu, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2275 | .ndo_validate_addr = eth_validate_addr, |
| 2276 | .ndo_set_mac_address = eth_mac_addr, |
| 2277 | .ndo_fdb_add = vxlan_fdb_add, |
| 2278 | .ndo_fdb_del = vxlan_fdb_delete, |
| 2279 | .ndo_fdb_dump = vxlan_fdb_dump, |
| 2280 | }; |
| 2281 | |
| 2282 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 2283 | static struct device_type vxlan_type = { |
| 2284 | .name = "vxlan", |
| 2285 | }; |
| 2286 | |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 2287 | /* Calls the ndo_add_vxlan_port of the caller in order to |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 2288 | * supply the listening VXLAN udp ports. Callers are expected |
| 2289 | * to implement the ndo_add_vxlan_port. |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 2290 | */ |
| 2291 | void vxlan_get_rx_port(struct net_device *dev) |
| 2292 | { |
| 2293 | struct vxlan_sock *vs; |
| 2294 | struct net *net = dev_net(dev); |
| 2295 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 2296 | sa_family_t sa_family; |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 2297 | __be16 port; |
| 2298 | unsigned int i; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 2299 | |
| 2300 | spin_lock(&vn->sock_lock); |
| 2301 | for (i = 0; i < PORT_HASH_SIZE; ++i) { |
Joseph Gasparakis | 35e4237 | 2013-09-13 07:34:13 -0700 | [diff] [blame] | 2302 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { |
| 2303 | port = inet_sk(vs->sock->sk)->inet_sport; |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 2304 | sa_family = vs->sock->sk->sk_family; |
| 2305 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, |
| 2306 | port); |
| 2307 | } |
| 2308 | } |
| 2309 | spin_unlock(&vn->sock_lock); |
| 2310 | } |
| 2311 | EXPORT_SYMBOL_GPL(vxlan_get_rx_port); |
| 2312 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2313 | /* Initialize the device structure. */ |
| 2314 | static void vxlan_setup(struct net_device *dev) |
| 2315 | { |
| 2316 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 2317 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2318 | |
| 2319 | eth_hw_addr_random(dev); |
| 2320 | ether_setup(dev); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2321 | if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6) |
Cong Wang | 2853af6 | 2014-06-12 11:53:10 -0700 | [diff] [blame] | 2322 | dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2323 | else |
Cong Wang | 2853af6 | 2014-06-12 11:53:10 -0700 | [diff] [blame] | 2324 | dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2325 | |
| 2326 | dev->netdev_ops = &vxlan_netdev_ops; |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2327 | dev->destructor = free_netdev; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2328 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
| 2329 | |
| 2330 | dev->tx_queue_len = 0; |
| 2331 | dev->features |= NETIF_F_LLTX; |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 2332 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 2333 | dev->features |= NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 2334 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 2335 | |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 2336 | dev->vlan_features = dev->features; |
| 2337 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 2338 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 2339 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
Pravin B Shelar | 1eaa817 | 2013-08-19 11:23:29 -0700 | [diff] [blame] | 2340 | dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 2341 | netif_keep_dst(dev); |
stephen hemminger | 6602d00 | 2012-12-31 12:00:21 +0000 | [diff] [blame] | 2342 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2343 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2344 | INIT_LIST_HEAD(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2345 | spin_lock_init(&vxlan->hash_lock); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 2346 | INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); |
| 2347 | INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2348 | INIT_WORK(&vxlan->sock_work, vxlan_sock_work); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2349 | |
| 2350 | init_timer_deferrable(&vxlan->age_timer); |
| 2351 | vxlan->age_timer.function = vxlan_cleanup; |
| 2352 | vxlan->age_timer.data = (unsigned long) vxlan; |
| 2353 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 2354 | vxlan->dst_port = htons(vxlan_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2355 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2356 | vxlan->dev = dev; |
| 2357 | |
| 2358 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
| 2359 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
| 2360 | } |
| 2361 | |
| 2362 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
| 2363 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 2364 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2365 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2366 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
| 2367 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2368 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2369 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
| 2370 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
| 2371 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
| 2372 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
| 2373 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2374 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2375 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
| 2376 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, |
| 2377 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, |
| 2378 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 2379 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
Tom Herbert | 5c91ae0 | 2014-11-06 18:06:01 -0800 | [diff] [blame] | 2380 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
| 2381 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, |
| 2382 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 2383 | [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, |
| 2384 | [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2385 | }; |
| 2386 | |
| 2387 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 2388 | { |
| 2389 | if (tb[IFLA_ADDRESS]) { |
| 2390 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 2391 | pr_debug("invalid link address (not ethernet)\n"); |
| 2392 | return -EINVAL; |
| 2393 | } |
| 2394 | |
| 2395 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 2396 | pr_debug("invalid all zero ethernet address\n"); |
| 2397 | return -EADDRNOTAVAIL; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | if (!data) |
| 2402 | return -EINVAL; |
| 2403 | |
| 2404 | if (data[IFLA_VXLAN_ID]) { |
| 2405 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
| 2406 | if (id >= VXLAN_VID_MASK) |
| 2407 | return -ERANGE; |
| 2408 | } |
| 2409 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2410 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 2411 | const struct ifla_vxlan_port_range *p |
| 2412 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 2413 | |
| 2414 | if (ntohs(p->high) < ntohs(p->low)) { |
| 2415 | pr_debug("port range %u .. %u not valid\n", |
| 2416 | ntohs(p->low), ntohs(p->high)); |
| 2417 | return -EINVAL; |
| 2418 | } |
| 2419 | } |
| 2420 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2421 | return 0; |
| 2422 | } |
| 2423 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 2424 | static void vxlan_get_drvinfo(struct net_device *netdev, |
| 2425 | struct ethtool_drvinfo *drvinfo) |
| 2426 | { |
| 2427 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); |
| 2428 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); |
| 2429 | } |
| 2430 | |
| 2431 | static const struct ethtool_ops vxlan_ethtool_ops = { |
| 2432 | .get_drvinfo = vxlan_get_drvinfo, |
| 2433 | .get_link = ethtool_op_get_link, |
| 2434 | }; |
| 2435 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2436 | static void vxlan_del_work(struct work_struct *work) |
| 2437 | { |
| 2438 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 2439 | udp_tunnel_sock_release(vs->sock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2440 | kfree_rcu(vs, rcu); |
| 2441 | } |
| 2442 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2443 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
| 2444 | __be16 port, u32 flags) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2445 | { |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2446 | struct socket *sock; |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2447 | struct udp_port_cfg udp_conf; |
| 2448 | int err; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2449 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2450 | memset(&udp_conf, 0, sizeof(udp_conf)); |
| 2451 | |
| 2452 | if (ipv6) { |
| 2453 | udp_conf.family = AF_INET6; |
| 2454 | udp_conf.use_udp6_tx_checksums = |
Alexander Duyck | 3dc2b6a | 2014-11-24 20:08:38 -0800 | [diff] [blame] | 2455 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2456 | udp_conf.use_udp6_rx_checksums = |
Alexander Duyck | 3dc2b6a | 2014-11-24 20:08:38 -0800 | [diff] [blame] | 2457 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2458 | } else { |
| 2459 | udp_conf.family = AF_INET; |
| 2460 | udp_conf.local_ip.s_addr = INADDR_ANY; |
| 2461 | udp_conf.use_udp_checksums = |
| 2462 | !!(flags & VXLAN_F_UDP_CSUM); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2463 | } |
| 2464 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2465 | udp_conf.local_udp_port = port; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2466 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2467 | /* Open UDP socket */ |
| 2468 | err = udp_sock_create(net, &udp_conf, &sock); |
| 2469 | if (err < 0) |
| 2470 | return ERR_PTR(err); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2471 | |
Zhi Yong Wu | 39deb2c | 2013-10-28 14:01:48 +0800 | [diff] [blame] | 2472 | return sock; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | /* Create new listen socket if needed */ |
| 2476 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2477 | vxlan_rcv_t *rcv, void *data, |
| 2478 | u32 flags) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2479 | { |
| 2480 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 2481 | struct vxlan_sock *vs; |
| 2482 | struct socket *sock; |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 2483 | unsigned int h; |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2484 | bool ipv6 = !!(flags & VXLAN_F_IPV6); |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 2485 | struct udp_tunnel_sock_cfg tunnel_cfg; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2486 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 2487 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2488 | if (!vs) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2489 | return ERR_PTR(-ENOMEM); |
| 2490 | |
| 2491 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 2492 | INIT_HLIST_HEAD(&vs->vni_list[h]); |
| 2493 | |
| 2494 | INIT_WORK(&vs->del_work, vxlan_del_work); |
| 2495 | |
Tom Herbert | 3ee64f3 | 2014-07-13 19:49:42 -0700 | [diff] [blame] | 2496 | sock = vxlan_create_sock(net, ipv6, port, flags); |
Zhi Yong Wu | 39deb2c | 2013-10-28 14:01:48 +0800 | [diff] [blame] | 2497 | if (IS_ERR(sock)) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2498 | kfree(vs); |
Duan Jiong | e50fddc | 2013-11-01 13:09:43 +0800 | [diff] [blame] | 2499 | return ERR_CAST(sock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2502 | vs->sock = sock; |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2503 | atomic_set(&vs->refcnt, 1); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 2504 | vs->rcv = rcv; |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 2505 | vs->data = data; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 2506 | vs->flags = flags; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2507 | |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 2508 | /* Initialize the vxlan udp offloads structure */ |
| 2509 | vs->udp_offloads.port = port; |
| 2510 | vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive; |
| 2511 | vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete; |
| 2512 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2513 | spin_lock(&vn->sock_lock); |
| 2514 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 2515 | vxlan_notify_add_rx_port(vs); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2516 | spin_unlock(&vn->sock_lock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2517 | |
| 2518 | /* Mark socket as an encapsulation socket. */ |
Andy Zhou | acbf74a | 2014-09-16 17:31:18 -0700 | [diff] [blame] | 2519 | tunnel_cfg.sk_user_data = vs; |
| 2520 | tunnel_cfg.encap_type = 1; |
| 2521 | tunnel_cfg.encap_rcv = vxlan_udp_encap_recv; |
| 2522 | tunnel_cfg.encap_destroy = NULL; |
| 2523 | |
| 2524 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2525 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2526 | return vs; |
| 2527 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2528 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 2529 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
| 2530 | vxlan_rcv_t *rcv, void *data, |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2531 | bool no_share, u32 flags) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2532 | { |
| 2533 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 2534 | struct vxlan_sock *vs; |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 2535 | bool ipv6 = flags & VXLAN_F_IPV6; |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2536 | |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2537 | vs = vxlan_socket_create(net, port, rcv, data, flags); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2538 | if (!IS_ERR(vs)) |
| 2539 | return vs; |
| 2540 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 2541 | if (no_share) /* Return error if sharing is not allowed. */ |
| 2542 | return vs; |
| 2543 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2544 | spin_lock(&vn->sock_lock); |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 2545 | vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port); |
Marcelo Leitner | 00c83b0 | 2014-12-11 10:02:22 -0200 | [diff] [blame] | 2546 | if (vs && ((vs->rcv != rcv) || |
| 2547 | !atomic_add_unless(&vs->refcnt, 1, 0))) |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 2548 | vs = ERR_PTR(-EBUSY); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 2549 | spin_unlock(&vn->sock_lock); |
| 2550 | |
| 2551 | if (!vs) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2552 | vs = ERR_PTR(-EINVAL); |
| 2553 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2554 | return vs; |
| 2555 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 2556 | EXPORT_SYMBOL_GPL(vxlan_sock_add); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2557 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2558 | /* Scheduled at device creation to bind to a socket */ |
| 2559 | static void vxlan_sock_work(struct work_struct *work) |
| 2560 | { |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2561 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2562 | struct net *net = vxlan->net; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2563 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2564 | __be16 port = vxlan->dst_port; |
| 2565 | struct vxlan_sock *nvs; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2566 | |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2567 | nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2568 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2569 | if (!IS_ERR(nvs)) |
| 2570 | vxlan_vs_add_dev(nvs, vxlan); |
| 2571 | spin_unlock(&vn->sock_lock); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2572 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2573 | dev_put(vxlan->dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2576 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
| 2577 | struct nlattr *tb[], struct nlattr *data[]) |
| 2578 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2579 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2580 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2581 | struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2582 | __u32 vni; |
| 2583 | int err; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2584 | bool use_ipv6 = false; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (!data[IFLA_VXLAN_ID]) |
| 2587 | return -EINVAL; |
| 2588 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2589 | vxlan->net = dev_net(dev); |
| 2590 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2591 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2592 | dst->remote_vni = vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2593 | |
Mike Rapoport | 5933a7b | 2014-04-01 09:23:01 +0300 | [diff] [blame] | 2594 | /* Unless IPv6 is explicitly requested, assume IPv4 */ |
| 2595 | dst->remote_ip.sa.sa_family = AF_INET; |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2596 | if (data[IFLA_VXLAN_GROUP]) { |
| 2597 | dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2598 | } else if (data[IFLA_VXLAN_GROUP6]) { |
| 2599 | if (!IS_ENABLED(CONFIG_IPV6)) |
| 2600 | return -EPFNOSUPPORT; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2601 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2602 | nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6], |
| 2603 | sizeof(struct in6_addr)); |
| 2604 | dst->remote_ip.sa.sa_family = AF_INET6; |
| 2605 | use_ipv6 = true; |
| 2606 | } |
| 2607 | |
| 2608 | if (data[IFLA_VXLAN_LOCAL]) { |
| 2609 | vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); |
| 2610 | vxlan->saddr.sa.sa_family = AF_INET; |
| 2611 | } else if (data[IFLA_VXLAN_LOCAL6]) { |
| 2612 | if (!IS_ENABLED(CONFIG_IPV6)) |
| 2613 | return -EPFNOSUPPORT; |
| 2614 | |
| 2615 | /* TODO: respect scope id */ |
| 2616 | nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6], |
| 2617 | sizeof(struct in6_addr)); |
| 2618 | vxlan->saddr.sa.sa_family = AF_INET6; |
| 2619 | use_ipv6 = true; |
| 2620 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2621 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2622 | if (data[IFLA_VXLAN_LINK] && |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2623 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2624 | struct net_device *lowerdev |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2625 | = __dev_get_by_index(net, dst->remote_ifindex); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2626 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2627 | if (!lowerdev) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2628 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2629 | return -ENODEV; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2630 | } |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2631 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2632 | #if IS_ENABLED(CONFIG_IPV6) |
| 2633 | if (use_ipv6) { |
| 2634 | struct inet6_dev *idev = __in6_dev_get(lowerdev); |
| 2635 | if (idev && idev->cnf.disable_ipv6) { |
| 2636 | pr_info("IPv6 is disabled via sysctl\n"); |
| 2637 | return -EPERM; |
| 2638 | } |
| 2639 | vxlan->flags |= VXLAN_F_IPV6; |
| 2640 | } |
| 2641 | #endif |
| 2642 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 2643 | if (!tb[IFLA_MTU]) |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2644 | dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
Alexander Duyck | 1ba56fb | 2012-11-13 13:10:59 +0000 | [diff] [blame] | 2645 | |
Cong Wang | 2853af6 | 2014-06-12 11:53:10 -0700 | [diff] [blame] | 2646 | dev->needed_headroom = lowerdev->hard_header_len + |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2647 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
fan.du | 7bda701 | 2014-01-03 10:18:58 +0800 | [diff] [blame] | 2648 | } else if (use_ipv6) |
| 2649 | vxlan->flags |= VXLAN_F_IPV6; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2650 | |
| 2651 | if (data[IFLA_VXLAN_TOS]) |
| 2652 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
| 2653 | |
Vincent Bernat | afb9718 | 2012-10-30 10:27:16 +0000 | [diff] [blame] | 2654 | if (data[IFLA_VXLAN_TTL]) |
| 2655 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
| 2656 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2657 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2658 | vxlan->flags |= VXLAN_F_LEARN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2659 | |
| 2660 | if (data[IFLA_VXLAN_AGEING]) |
| 2661 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
| 2662 | else |
| 2663 | vxlan->age_interval = FDB_AGE_DEFAULT; |
| 2664 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2665 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
| 2666 | vxlan->flags |= VXLAN_F_PROXY; |
| 2667 | |
| 2668 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) |
| 2669 | vxlan->flags |= VXLAN_F_RSC; |
| 2670 | |
| 2671 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) |
| 2672 | vxlan->flags |= VXLAN_F_L2MISS; |
| 2673 | |
| 2674 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) |
| 2675 | vxlan->flags |= VXLAN_F_L3MISS; |
| 2676 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2677 | if (data[IFLA_VXLAN_LIMIT]) |
| 2678 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
| 2679 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2680 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 2681 | const struct ifla_vxlan_port_range *p |
| 2682 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 2683 | vxlan->port_min = ntohs(p->low); |
| 2684 | vxlan->port_max = ntohs(p->high); |
| 2685 | } |
| 2686 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 2687 | if (data[IFLA_VXLAN_PORT]) |
| 2688 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); |
| 2689 | |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2690 | if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) |
| 2691 | vxlan->flags |= VXLAN_F_UDP_CSUM; |
| 2692 | |
| 2693 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] && |
| 2694 | nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX])) |
| 2695 | vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX; |
| 2696 | |
| 2697 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] && |
| 2698 | nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX])) |
| 2699 | vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX; |
| 2700 | |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 2701 | if (data[IFLA_VXLAN_REMCSUM_TX] && |
| 2702 | nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX])) |
| 2703 | vxlan->flags |= VXLAN_F_REMCSUM_TX; |
| 2704 | |
| 2705 | if (data[IFLA_VXLAN_REMCSUM_RX] && |
| 2706 | nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX])) |
| 2707 | vxlan->flags |= VXLAN_F_REMCSUM_RX; |
| 2708 | |
Marcelo Leitner | 19ca9fc | 2014-11-13 14:43:08 -0200 | [diff] [blame] | 2709 | if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET, |
| 2710 | vxlan->dst_port)) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2711 | pr_info("duplicate VNI %u\n", vni); |
| 2712 | return -EEXIST; |
| 2713 | } |
| 2714 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 2715 | dev->ethtool_ops = &vxlan_ethtool_ops; |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 2716 | |
Sridhar Samudrala | 2936b6a | 2013-09-17 12:12:40 -0700 | [diff] [blame] | 2717 | /* create an fdb entry for a valid default destination */ |
| 2718 | if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { |
| 2719 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
| 2720 | &vxlan->default_dst.remote_ip, |
| 2721 | NUD_REACHABLE|NUD_PERMANENT, |
| 2722 | NLM_F_EXCL|NLM_F_CREATE, |
| 2723 | vxlan->dst_port, |
| 2724 | vxlan->default_dst.remote_vni, |
| 2725 | vxlan->default_dst.remote_ifindex, |
| 2726 | NTF_SELF); |
| 2727 | if (err) |
| 2728 | return err; |
| 2729 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2730 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2731 | err = register_netdevice(dev); |
| 2732 | if (err) { |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 2733 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 2734 | return err; |
| 2735 | } |
| 2736 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2737 | list_add(&vxlan->next, &vn->vxlan_list); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2738 | |
| 2739 | return 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
| 2743 | { |
| 2744 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2745 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2746 | |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 2747 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 2748 | if (!hlist_unhashed(&vxlan->hlist)) |
| 2749 | hlist_del_rcu(&vxlan->hlist); |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 2750 | spin_unlock(&vn->sock_lock); |
| 2751 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2752 | list_del(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2753 | unregister_netdevice_queue(dev, head); |
| 2754 | } |
| 2755 | |
| 2756 | static size_t vxlan_get_size(const struct net_device *dev) |
| 2757 | { |
| 2758 | |
| 2759 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2760 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2761 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2762 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2763 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
| 2764 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
| 2765 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2766 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
| 2767 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ |
| 2768 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ |
| 2769 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2770 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
| 2771 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2772 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2773 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ |
| 2774 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ |
| 2775 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ |
| 2776 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 2777 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ |
| 2778 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2779 | 0; |
| 2780 | } |
| 2781 | |
| 2782 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 2783 | { |
| 2784 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2785 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2786 | struct ifla_vxlan_port_range ports = { |
| 2787 | .low = htons(vxlan->port_min), |
| 2788 | .high = htons(vxlan->port_max), |
| 2789 | }; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2790 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2791 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2792 | goto nla_put_failure; |
| 2793 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2794 | if (!vxlan_addr_any(&dst->remote_ip)) { |
| 2795 | if (dst->remote_ip.sa.sa_family == AF_INET) { |
| 2796 | if (nla_put_be32(skb, IFLA_VXLAN_GROUP, |
| 2797 | dst->remote_ip.sin.sin_addr.s_addr)) |
| 2798 | goto nla_put_failure; |
| 2799 | #if IS_ENABLED(CONFIG_IPV6) |
| 2800 | } else { |
| 2801 | if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr), |
| 2802 | &dst->remote_ip.sin6.sin6_addr)) |
| 2803 | goto nla_put_failure; |
| 2804 | #endif |
| 2805 | } |
| 2806 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2807 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 2808 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2809 | goto nla_put_failure; |
| 2810 | |
Cong Wang | e4c7ed4 | 2013-08-31 13:44:33 +0800 | [diff] [blame] | 2811 | if (!vxlan_addr_any(&vxlan->saddr)) { |
| 2812 | if (vxlan->saddr.sa.sa_family == AF_INET) { |
| 2813 | if (nla_put_be32(skb, IFLA_VXLAN_LOCAL, |
| 2814 | vxlan->saddr.sin.sin_addr.s_addr)) |
| 2815 | goto nla_put_failure; |
| 2816 | #if IS_ENABLED(CONFIG_IPV6) |
| 2817 | } else { |
| 2818 | if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr), |
| 2819 | &vxlan->saddr.sin6.sin6_addr)) |
| 2820 | goto nla_put_failure; |
| 2821 | #endif |
| 2822 | } |
| 2823 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || |
| 2826 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 2827 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
| 2828 | !!(vxlan->flags & VXLAN_F_LEARN)) || |
| 2829 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
| 2830 | !!(vxlan->flags & VXLAN_F_PROXY)) || |
| 2831 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || |
| 2832 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
| 2833 | !!(vxlan->flags & VXLAN_F_L2MISS)) || |
| 2834 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
| 2835 | !!(vxlan->flags & VXLAN_F_L3MISS)) || |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2836 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 2837 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 2838 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) || |
| 2839 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, |
| 2840 | !!(vxlan->flags & VXLAN_F_UDP_CSUM)) || |
| 2841 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
| 2842 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || |
| 2843 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame^] | 2844 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || |
| 2845 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, |
| 2846 | !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) || |
| 2847 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, |
| 2848 | !!(vxlan->flags & VXLAN_F_REMCSUM_RX))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2849 | goto nla_put_failure; |
| 2850 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 2851 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
| 2852 | goto nla_put_failure; |
| 2853 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2854 | return 0; |
| 2855 | |
| 2856 | nla_put_failure: |
| 2857 | return -EMSGSIZE; |
| 2858 | } |
| 2859 | |
| 2860 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
| 2861 | .kind = "vxlan", |
| 2862 | .maxtype = IFLA_VXLAN_MAX, |
| 2863 | .policy = vxlan_policy, |
| 2864 | .priv_size = sizeof(struct vxlan_dev), |
| 2865 | .setup = vxlan_setup, |
| 2866 | .validate = vxlan_validate, |
| 2867 | .newlink = vxlan_newlink, |
| 2868 | .dellink = vxlan_dellink, |
| 2869 | .get_size = vxlan_get_size, |
| 2870 | .fill_info = vxlan_fill_info, |
| 2871 | }; |
| 2872 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2873 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
| 2874 | struct net_device *dev) |
| 2875 | { |
| 2876 | struct vxlan_dev *vxlan, *next; |
| 2877 | LIST_HEAD(list_kill); |
| 2878 | |
| 2879 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { |
| 2880 | struct vxlan_rdst *dst = &vxlan->default_dst; |
| 2881 | |
| 2882 | /* In case we created vxlan device with carrier |
| 2883 | * and we loose the carrier due to module unload |
| 2884 | * we also need to remove vxlan device. In other |
| 2885 | * cases, it's not necessary and remote_ifindex |
| 2886 | * is 0 here, so no matches. |
| 2887 | */ |
| 2888 | if (dst->remote_ifindex == dev->ifindex) |
| 2889 | vxlan_dellink(vxlan->dev, &list_kill); |
| 2890 | } |
| 2891 | |
| 2892 | unregister_netdevice_many(&list_kill); |
| 2893 | } |
| 2894 | |
| 2895 | static int vxlan_lowerdev_event(struct notifier_block *unused, |
| 2896 | unsigned long event, void *ptr) |
| 2897 | { |
| 2898 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 2899 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2900 | |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 2901 | if (event == NETDEV_UNREGISTER) |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2902 | vxlan_handle_lowerdev_unregister(vn, dev); |
| 2903 | |
| 2904 | return NOTIFY_DONE; |
| 2905 | } |
| 2906 | |
| 2907 | static struct notifier_block vxlan_notifier_block __read_mostly = { |
| 2908 | .notifier_call = vxlan_lowerdev_event, |
| 2909 | }; |
| 2910 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2911 | static __net_init int vxlan_init_net(struct net *net) |
| 2912 | { |
| 2913 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 2914 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2915 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2916 | INIT_LIST_HEAD(&vn->vxlan_list); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 2917 | spin_lock_init(&vn->sock_lock); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2918 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 2919 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 2920 | INIT_HLIST_HEAD(&vn->sock_list[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2921 | |
| 2922 | return 0; |
| 2923 | } |
| 2924 | |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2925 | static void __net_exit vxlan_exit_net(struct net *net) |
| 2926 | { |
| 2927 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 2928 | struct vxlan_dev *vxlan, *next; |
| 2929 | struct net_device *dev, *aux; |
| 2930 | LIST_HEAD(list); |
| 2931 | |
| 2932 | rtnl_lock(); |
| 2933 | for_each_netdev_safe(net, dev, aux) |
| 2934 | if (dev->rtnl_link_ops == &vxlan_link_ops) |
| 2935 | unregister_netdevice_queue(dev, &list); |
| 2936 | |
| 2937 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { |
| 2938 | /* If vxlan->dev is in the same netns, it has already been added |
| 2939 | * to the list by the previous loop. |
| 2940 | */ |
| 2941 | if (!net_eq(dev_net(vxlan->dev), net)) |
| 2942 | unregister_netdevice_queue(dev, &list); |
| 2943 | } |
| 2944 | |
| 2945 | unregister_netdevice_many(&list); |
| 2946 | rtnl_unlock(); |
| 2947 | } |
| 2948 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2949 | static struct pernet_operations vxlan_net_ops = { |
| 2950 | .init = vxlan_init_net, |
Nicolas Dichtel | f01ec1c | 2014-04-24 10:02:49 +0200 | [diff] [blame] | 2951 | .exit = vxlan_exit_net, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2952 | .id = &vxlan_net_id, |
| 2953 | .size = sizeof(struct vxlan_net), |
| 2954 | }; |
| 2955 | |
| 2956 | static int __init vxlan_init_module(void) |
| 2957 | { |
| 2958 | int rc; |
| 2959 | |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2960 | vxlan_wq = alloc_workqueue("vxlan", 0, 0); |
| 2961 | if (!vxlan_wq) |
| 2962 | return -ENOMEM; |
| 2963 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2964 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
| 2965 | |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 2966 | rc = register_pernet_subsys(&vxlan_net_ops); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2967 | if (rc) |
| 2968 | goto out1; |
| 2969 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2970 | rc = register_netdevice_notifier(&vxlan_notifier_block); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2971 | if (rc) |
| 2972 | goto out2; |
| 2973 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2974 | rc = rtnl_link_register(&vxlan_link_ops); |
| 2975 | if (rc) |
| 2976 | goto out3; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2977 | |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2978 | return 0; |
| 2979 | out3: |
| 2980 | unregister_netdevice_notifier(&vxlan_notifier_block); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2981 | out2: |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 2982 | unregister_pernet_subsys(&vxlan_net_ops); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2983 | out1: |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2984 | destroy_workqueue(vxlan_wq); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2985 | return rc; |
| 2986 | } |
Cong Wang | 7332a13 | 2013-05-27 22:35:53 +0000 | [diff] [blame] | 2987 | late_initcall(vxlan_init_module); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2988 | |
| 2989 | static void __exit vxlan_cleanup_module(void) |
| 2990 | { |
Stephen Hemminger | b715398 | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2991 | rtnl_link_unregister(&vxlan_link_ops); |
Daniel Borkmann | acaf4e709 | 2014-01-13 18:41:19 +0100 | [diff] [blame] | 2992 | unregister_netdevice_notifier(&vxlan_notifier_block); |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2993 | destroy_workqueue(vxlan_wq); |
Daniel Borkmann | 783c146 | 2014-01-22 21:07:53 +0100 | [diff] [blame] | 2994 | unregister_pernet_subsys(&vxlan_net_ops); |
| 2995 | /* rcu_barrier() is called by netns */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2996 | } |
| 2997 | module_exit(vxlan_cleanup_module); |
| 2998 | |
| 2999 | MODULE_LICENSE("GPL"); |
| 3000 | MODULE_VERSION(VXLAN_VERSION); |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 3001 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
Jesse Brandeburg | ead5139 | 2014-01-17 11:00:33 -0800 | [diff] [blame] | 3002 | MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3003 | MODULE_ALIAS_RTNL_LINK("vxlan"); |