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