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