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