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