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