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