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