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