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