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