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