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