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