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