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