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