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