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. |
| 9 | * |
| 10 | * TODO |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 11 | * - IPv6 (not in RFC) |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/in.h> |
| 25 | #include <linux/ip.h> |
| 26 | #include <linux/udp.h> |
| 27 | #include <linux/igmp.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/if_ether.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 30 | #include <linux/hash.h> |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 31 | #include <linux/ethtool.h> |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 32 | #include <net/arp.h> |
| 33 | #include <net/ndisc.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 34 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 35 | #include <net/ip_tunnels.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 36 | #include <net/icmp.h> |
| 37 | #include <net/udp.h> |
| 38 | #include <net/rtnetlink.h> |
| 39 | #include <net/route.h> |
| 40 | #include <net/dsfield.h> |
| 41 | #include <net/inet_ecn.h> |
| 42 | #include <net/net_namespace.h> |
| 43 | #include <net/netns/generic.h> |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 44 | #include <net/vxlan.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 45 | |
| 46 | #define VXLAN_VERSION "0.1" |
| 47 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 48 | #define PORT_HASH_BITS 8 |
| 49 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 50 | #define VNI_HASH_BITS 10 |
| 51 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 52 | #define FDB_HASH_BITS 8 |
| 53 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) |
| 54 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
| 55 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
| 56 | |
| 57 | #define VXLAN_N_VID (1u << 24) |
| 58 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) |
Alexander Duyck | 52b702f | 2012-11-09 13:35:24 +0000 | [diff] [blame] | 59 | /* IP header + UDP + VXLAN + Ethernet header */ |
| 60 | #define VXLAN_HEADROOM (20 + 8 + 8 + 14) |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 61 | #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 62 | |
| 63 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ |
| 64 | |
| 65 | /* VXLAN protocol header */ |
| 66 | struct vxlanhdr { |
| 67 | __be32 vx_flags; |
| 68 | __be32 vx_vni; |
| 69 | }; |
| 70 | |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 71 | /* UDP port for VXLAN traffic. |
| 72 | * The IANA assigned port is 4789, but the Linux default is 8472 |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 73 | * for compatibility with early adopters. |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 74 | */ |
Stephen Hemminger | 9daaa39 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 75 | static unsigned short vxlan_port __read_mostly = 8472; |
| 76 | module_param_named(udp_port, vxlan_port, ushort, 0444); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 77 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
| 78 | |
| 79 | static bool log_ecn_error = true; |
| 80 | module_param(log_ecn_error, bool, 0644); |
| 81 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 82 | |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 83 | static int vxlan_net_id; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 84 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 85 | static const u8 all_zeros_mac[ETH_ALEN]; |
| 86 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 87 | /* per-network namespace private data for this module */ |
| 88 | struct vxlan_net { |
| 89 | struct list_head vxlan_list; |
| 90 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 91 | spinlock_t sock_lock; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 94 | struct vxlan_rdst { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 95 | __be32 remote_ip; |
| 96 | __be16 remote_port; |
| 97 | u32 remote_vni; |
| 98 | u32 remote_ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 99 | struct list_head list; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 100 | struct rcu_head rcu; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 103 | /* Forwarding table entry */ |
| 104 | struct vxlan_fdb { |
| 105 | struct hlist_node hlist; /* linked list of entries */ |
| 106 | struct rcu_head rcu; |
| 107 | unsigned long updated; /* jiffies */ |
| 108 | unsigned long used; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 109 | struct list_head remotes; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 110 | u16 state; /* see ndm_state */ |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 111 | u8 flags; /* see ndm_flags */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 112 | u8 eth_addr[ETH_ALEN]; |
| 113 | }; |
| 114 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 115 | /* Pseudo network device */ |
| 116 | struct vxlan_dev { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 117 | struct hlist_node hlist; /* vni hash table */ |
| 118 | struct list_head next; /* vxlan's per namespace list */ |
| 119 | struct vxlan_sock *vn_sock; /* listening socket */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 120 | struct net_device *dev; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 121 | struct vxlan_rdst default_dst; /* default destination */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 122 | __be32 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; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 128 | u32 flags; /* VXLAN_F_* below */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 129 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 130 | struct work_struct sock_work; |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 131 | struct work_struct igmp_join; |
| 132 | struct work_struct igmp_leave; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 133 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 134 | unsigned long age_interval; |
| 135 | struct timer_list age_timer; |
| 136 | spinlock_t hash_lock; |
| 137 | unsigned int addrcnt; |
| 138 | unsigned int addrmax; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 139 | |
| 140 | struct hlist_head fdb_head[FDB_HASH_SIZE]; |
| 141 | }; |
| 142 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 143 | #define VXLAN_F_LEARN 0x01 |
| 144 | #define VXLAN_F_PROXY 0x02 |
| 145 | #define VXLAN_F_RSC 0x04 |
| 146 | #define VXLAN_F_L2MISS 0x08 |
| 147 | #define VXLAN_F_L3MISS 0x10 |
| 148 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 149 | /* salt for hash table */ |
| 150 | static u32 vxlan_salt __read_mostly; |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 151 | static struct workqueue_struct *vxlan_wq; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 152 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 153 | static void vxlan_sock_work(struct work_struct *work); |
| 154 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 155 | /* Virtual Network hash table head */ |
| 156 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) |
| 157 | { |
| 158 | return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; |
| 159 | } |
| 160 | |
| 161 | /* Socket hash table head */ |
| 162 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 163 | { |
| 164 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 165 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 166 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
| 167 | } |
| 168 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 169 | /* First remote destination for a forwarding entry. |
| 170 | * Guaranteed to be non-NULL because remotes are never deleted. |
| 171 | */ |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 172 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 173 | { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 174 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
| 175 | } |
| 176 | |
| 177 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) |
| 178 | { |
| 179 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 180 | } |
| 181 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 182 | /* Find VXLAN socket based on network namespace and UDP port */ |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 183 | static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 184 | { |
| 185 | struct vxlan_sock *vs; |
| 186 | |
| 187 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
| 188 | if (inet_sk(vs->sock->sk)->inet_sport == port) |
| 189 | return vs; |
| 190 | } |
| 191 | return NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 194 | 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] | 195 | { |
| 196 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 197 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 198 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 199 | if (vxlan->default_dst.remote_vni == id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 200 | return vxlan; |
| 201 | } |
| 202 | |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 206 | /* Look up VNI in a per net namespace table */ |
| 207 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port) |
| 208 | { |
| 209 | struct vxlan_sock *vs; |
| 210 | |
| 211 | vs = vxlan_find_sock(net, port); |
| 212 | if (!vs) |
| 213 | return NULL; |
| 214 | |
| 215 | return vxlan_vs_find_vni(vs, id); |
| 216 | } |
| 217 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 218 | /* Fill in neighbour message in skbuff. */ |
| 219 | 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] | 220 | const struct vxlan_fdb *fdb, |
| 221 | u32 portid, u32 seq, int type, unsigned int flags, |
| 222 | const struct vxlan_rdst *rdst) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 223 | { |
| 224 | unsigned long now = jiffies; |
| 225 | struct nda_cacheinfo ci; |
| 226 | struct nlmsghdr *nlh; |
| 227 | struct ndmsg *ndm; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 228 | bool send_ip, send_eth; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 229 | |
| 230 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 231 | if (nlh == NULL) |
| 232 | return -EMSGSIZE; |
| 233 | |
| 234 | ndm = nlmsg_data(nlh); |
| 235 | memset(ndm, 0, sizeof(*ndm)); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 236 | |
| 237 | send_eth = send_ip = true; |
| 238 | |
| 239 | if (type == RTM_GETNEIGH) { |
| 240 | ndm->ndm_family = AF_INET; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 241 | send_ip = rdst->remote_ip != htonl(INADDR_ANY); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 242 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
| 243 | } else |
| 244 | ndm->ndm_family = AF_BRIDGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 245 | ndm->ndm_state = fdb->state; |
| 246 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 247 | ndm->ndm_flags = fdb->flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 248 | ndm->ndm_type = NDA_DST; |
| 249 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 250 | 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] | 251 | goto nla_put_failure; |
| 252 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 253 | if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip)) |
| 254 | goto nla_put_failure; |
| 255 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 256 | if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 257 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
| 258 | goto nla_put_failure; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 259 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
Pravin B Shelar | 60d9d4c | 2013-06-20 00:26:31 -0700 | [diff] [blame] | 260 | nla_put_u32(skb, NDA_VNI, rdst->remote_vni)) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 261 | goto nla_put_failure; |
| 262 | if (rdst->remote_ifindex && |
| 263 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 264 | goto nla_put_failure; |
| 265 | |
| 266 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 267 | ci.ndm_confirmed = 0; |
| 268 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 269 | ci.ndm_refcnt = 0; |
| 270 | |
| 271 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 272 | goto nla_put_failure; |
| 273 | |
| 274 | return nlmsg_end(skb, nlh); |
| 275 | |
| 276 | nla_put_failure: |
| 277 | nlmsg_cancel(skb, nlh); |
| 278 | return -EMSGSIZE; |
| 279 | } |
| 280 | |
| 281 | static inline size_t vxlan_nlmsg_size(void) |
| 282 | { |
| 283 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 284 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
| 285 | + nla_total_size(sizeof(__be32)) /* NDA_DST */ |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 286 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 287 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
| 288 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 289 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 290 | } |
| 291 | |
| 292 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 293 | struct vxlan_fdb *fdb, int type) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 294 | { |
| 295 | struct net *net = dev_net(vxlan->dev); |
| 296 | struct sk_buff *skb; |
| 297 | int err = -ENOBUFS; |
| 298 | |
| 299 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
| 300 | if (skb == NULL) |
| 301 | goto errout; |
| 302 | |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 303 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, |
| 304 | first_remote_rtnl(fdb)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 305 | if (err < 0) { |
| 306 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
| 307 | WARN_ON(err == -EMSGSIZE); |
| 308 | kfree_skb(skb); |
| 309 | goto errout; |
| 310 | } |
| 311 | |
| 312 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 313 | return; |
| 314 | errout: |
| 315 | if (err < 0) |
| 316 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 317 | } |
| 318 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 319 | static void vxlan_ip_miss(struct net_device *dev, __be32 ipa) |
| 320 | { |
| 321 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 322 | struct vxlan_fdb f = { |
| 323 | .state = NUD_STALE, |
| 324 | }; |
| 325 | struct vxlan_rdst remote = { |
| 326 | .remote_ip = ipa, /* goes to NDA_DST */ |
| 327 | .remote_vni = VXLAN_N_VID, |
| 328 | }; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 329 | |
| 330 | INIT_LIST_HEAD(&f.remotes); |
| 331 | list_add_rcu(&remote.list, &f.remotes); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 332 | |
| 333 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 334 | } |
| 335 | |
| 336 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) |
| 337 | { |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 338 | struct vxlan_fdb f = { |
| 339 | .state = NUD_STALE, |
| 340 | }; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 341 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 342 | INIT_LIST_HEAD(&f.remotes); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 343 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
| 344 | |
| 345 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 346 | } |
| 347 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 348 | /* Hash Ethernet address */ |
| 349 | static u32 eth_hash(const unsigned char *addr) |
| 350 | { |
| 351 | u64 value = get_unaligned((u64 *)addr); |
| 352 | |
| 353 | /* only want 6 bytes */ |
| 354 | #ifdef __BIG_ENDIAN |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 355 | value >>= 16; |
stephen hemminger | 321fb99 | 2012-10-09 20:35:47 +0000 | [diff] [blame] | 356 | #else |
| 357 | value <<= 16; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 358 | #endif |
| 359 | return hash_64(value, FDB_HASH_BITS); |
| 360 | } |
| 361 | |
| 362 | /* Hash chain to use given mac address */ |
| 363 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
| 364 | const u8 *mac) |
| 365 | { |
| 366 | return &vxlan->fdb_head[eth_hash(mac)]; |
| 367 | } |
| 368 | |
| 369 | /* Look up Ethernet address in forwarding table */ |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 370 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 371 | const u8 *mac) |
| 372 | |
| 373 | { |
| 374 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); |
| 375 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 376 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 377 | hlist_for_each_entry_rcu(f, head, hlist) { |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 378 | if (compare_ether_addr(mac, f->eth_addr) == 0) |
| 379 | return f; |
| 380 | } |
| 381 | |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 385 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
| 386 | const u8 *mac) |
| 387 | { |
| 388 | struct vxlan_fdb *f; |
| 389 | |
| 390 | f = __vxlan_find_mac(vxlan, mac); |
| 391 | if (f) |
| 392 | f->used = jiffies; |
| 393 | |
| 394 | return f; |
| 395 | } |
| 396 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 397 | /* caller should hold vxlan->hash_lock */ |
| 398 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, |
| 399 | __be32 ip, __be16 port, |
| 400 | __u32 vni, __u32 ifindex) |
| 401 | { |
| 402 | struct vxlan_rdst *rd; |
| 403 | |
| 404 | list_for_each_entry(rd, &f->remotes, list) { |
| 405 | if (rd->remote_ip == ip && |
| 406 | rd->remote_port == port && |
| 407 | rd->remote_vni == vni && |
| 408 | rd->remote_ifindex == ifindex) |
| 409 | return rd; |
| 410 | } |
| 411 | |
| 412 | return NULL; |
| 413 | } |
| 414 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 415 | /* Replace destination of unicast mac */ |
| 416 | static int vxlan_fdb_replace(struct vxlan_fdb *f, |
| 417 | __be32 ip, __be16 port, __u32 vni, __u32 ifindex) |
| 418 | { |
| 419 | struct vxlan_rdst *rd; |
| 420 | |
| 421 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 422 | if (rd) |
| 423 | return 0; |
| 424 | |
| 425 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); |
| 426 | if (!rd) |
| 427 | return 0; |
| 428 | rd->remote_ip = ip; |
| 429 | rd->remote_port = port; |
| 430 | rd->remote_vni = vni; |
| 431 | rd->remote_ifindex = ifindex; |
| 432 | return 1; |
| 433 | } |
| 434 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 435 | /* Add/update destinations for multicast */ |
| 436 | static int vxlan_fdb_append(struct vxlan_fdb *f, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 437 | __be32 ip, __be16 port, __u32 vni, __u32 ifindex) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 438 | { |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 439 | struct vxlan_rdst *rd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 440 | |
Mike Rapoport | a5e7c10 | 2013-06-25 16:01:52 +0300 | [diff] [blame] | 441 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 442 | if (rd) |
| 443 | return 0; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 444 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 445 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
| 446 | if (rd == NULL) |
| 447 | return -ENOBUFS; |
| 448 | rd->remote_ip = ip; |
| 449 | rd->remote_port = port; |
| 450 | rd->remote_vni = vni; |
| 451 | rd->remote_ifindex = ifindex; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 452 | |
| 453 | list_add_tail_rcu(&rd->list, &f->remotes); |
| 454 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 455 | return 1; |
| 456 | } |
| 457 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 458 | /* Add new entry to forwarding table -- assumes lock held */ |
| 459 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
| 460 | const u8 *mac, __be32 ip, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 461 | __u16 state, __u16 flags, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 462 | __be16 port, __u32 vni, __u32 ifindex, |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 463 | __u8 ndm_flags) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 464 | { |
| 465 | struct vxlan_fdb *f; |
| 466 | int notify = 0; |
| 467 | |
Sridhar Samudrala | 014be2c | 2013-05-17 06:39:07 +0000 | [diff] [blame] | 468 | f = __vxlan_find_mac(vxlan, mac); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 469 | if (f) { |
| 470 | if (flags & NLM_F_EXCL) { |
| 471 | netdev_dbg(vxlan->dev, |
| 472 | "lost race to create %pM\n", mac); |
| 473 | return -EEXIST; |
| 474 | } |
| 475 | if (f->state != state) { |
| 476 | f->state = state; |
| 477 | f->updated = jiffies; |
| 478 | notify = 1; |
| 479 | } |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 480 | if (f->flags != ndm_flags) { |
| 481 | f->flags = ndm_flags; |
| 482 | f->updated = jiffies; |
| 483 | notify = 1; |
| 484 | } |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 485 | if ((flags & NLM_F_REPLACE)) { |
| 486 | /* Only change unicasts */ |
| 487 | if (!(is_multicast_ether_addr(f->eth_addr) || |
| 488 | is_zero_ether_addr(f->eth_addr))) { |
| 489 | int rc = vxlan_fdb_replace(f, ip, port, vni, |
| 490 | ifindex); |
| 491 | |
| 492 | if (rc < 0) |
| 493 | return rc; |
| 494 | notify |= rc; |
| 495 | } else |
| 496 | return -EOPNOTSUPP; |
| 497 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 498 | if ((flags & NLM_F_APPEND) && |
Mike Rapoport | 58e4c76 | 2013-06-25 16:01:56 +0300 | [diff] [blame] | 499 | (is_multicast_ether_addr(f->eth_addr) || |
| 500 | is_zero_ether_addr(f->eth_addr))) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 501 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); |
| 502 | |
| 503 | if (rc < 0) |
| 504 | return rc; |
| 505 | notify |= rc; |
| 506 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 507 | } else { |
| 508 | if (!(flags & NLM_F_CREATE)) |
| 509 | return -ENOENT; |
| 510 | |
| 511 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) |
| 512 | return -ENOSPC; |
| 513 | |
Thomas Richter | 906dc18 | 2013-07-19 17:20:07 +0200 | [diff] [blame] | 514 | /* Disallow replace to add a multicast entry */ |
| 515 | if ((flags & NLM_F_REPLACE) && |
| 516 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) |
| 517 | return -EOPNOTSUPP; |
| 518 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 519 | netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip); |
| 520 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 521 | if (!f) |
| 522 | return -ENOMEM; |
| 523 | |
| 524 | notify = 1; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 525 | f->state = state; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 526 | f->flags = ndm_flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 527 | f->updated = f->used = jiffies; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 528 | INIT_LIST_HEAD(&f->remotes); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 529 | memcpy(f->eth_addr, mac, ETH_ALEN); |
| 530 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 531 | vxlan_fdb_append(f, ip, port, vni, ifindex); |
| 532 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 533 | ++vxlan->addrcnt; |
| 534 | hlist_add_head_rcu(&f->hlist, |
| 535 | vxlan_fdb_head(vxlan, mac)); |
| 536 | } |
| 537 | |
| 538 | if (notify) |
| 539 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 544 | static void vxlan_fdb_free_rdst(struct rcu_head *head) |
| 545 | { |
| 546 | struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); |
| 547 | kfree(rd); |
| 548 | } |
| 549 | |
Wei Yongjun | 6706c82 | 2013-04-11 19:00:35 +0000 | [diff] [blame] | 550 | static void vxlan_fdb_free(struct rcu_head *head) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 551 | { |
| 552 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 553 | struct vxlan_rdst *rd, *nd; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 554 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 555 | list_for_each_entry_safe(rd, nd, &f->remotes, list) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 556 | kfree(rd); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 557 | kfree(f); |
| 558 | } |
| 559 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 560 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
| 561 | { |
| 562 | netdev_dbg(vxlan->dev, |
| 563 | "delete %pM\n", f->eth_addr); |
| 564 | |
| 565 | --vxlan->addrcnt; |
| 566 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); |
| 567 | |
| 568 | hlist_del_rcu(&f->hlist); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 569 | call_rcu(&f->rcu, vxlan_fdb_free); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 572 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
| 573 | __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex) |
| 574 | { |
| 575 | struct net *net = dev_net(vxlan->dev); |
| 576 | |
| 577 | if (tb[NDA_DST]) { |
| 578 | if (nla_len(tb[NDA_DST]) != sizeof(__be32)) |
| 579 | return -EAFNOSUPPORT; |
| 580 | |
| 581 | *ip = nla_get_be32(tb[NDA_DST]); |
| 582 | } else { |
| 583 | *ip = htonl(INADDR_ANY); |
| 584 | } |
| 585 | |
| 586 | if (tb[NDA_PORT]) { |
| 587 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
| 588 | return -EINVAL; |
| 589 | *port = nla_get_be16(tb[NDA_PORT]); |
| 590 | } else { |
| 591 | *port = vxlan->dst_port; |
| 592 | } |
| 593 | |
| 594 | if (tb[NDA_VNI]) { |
| 595 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) |
| 596 | return -EINVAL; |
| 597 | *vni = nla_get_u32(tb[NDA_VNI]); |
| 598 | } else { |
| 599 | *vni = vxlan->default_dst.remote_vni; |
| 600 | } |
| 601 | |
| 602 | if (tb[NDA_IFINDEX]) { |
| 603 | struct net_device *tdev; |
| 604 | |
| 605 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) |
| 606 | return -EINVAL; |
| 607 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
| 608 | tdev = dev_get_by_index(net, *ifindex); |
| 609 | if (!tdev) |
| 610 | return -EADDRNOTAVAIL; |
| 611 | dev_put(tdev); |
| 612 | } else { |
| 613 | *ifindex = 0; |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 619 | /* Add static entry (via netlink) */ |
| 620 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 621 | struct net_device *dev, |
| 622 | const unsigned char *addr, u16 flags) |
| 623 | { |
| 624 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 625 | /* struct net *net = dev_net(vxlan->dev); */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 626 | __be32 ip; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 627 | __be16 port; |
| 628 | u32 vni, ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 629 | int err; |
| 630 | |
| 631 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
| 632 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
| 633 | ndm->ndm_state); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | if (tb[NDA_DST] == NULL) |
| 638 | return -EINVAL; |
| 639 | |
Mike Rapoport | f0b074b | 2013-06-25 16:01:53 +0300 | [diff] [blame] | 640 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 641 | if (err) |
| 642 | return err; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 643 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 644 | spin_lock_bh(&vxlan->hash_lock); |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 645 | err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, |
| 646 | port, vni, ifindex, ndm->ndm_flags); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 647 | spin_unlock_bh(&vxlan->hash_lock); |
| 648 | |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | /* Delete entry (via netlink) */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 653 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 654 | struct net_device *dev, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 655 | const unsigned char *addr) |
| 656 | { |
| 657 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 658 | struct vxlan_fdb *f; |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 659 | struct vxlan_rdst *rd = NULL; |
| 660 | __be32 ip; |
| 661 | __be16 port; |
| 662 | u32 vni, ifindex; |
| 663 | int err; |
| 664 | |
| 665 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); |
| 666 | if (err) |
| 667 | return err; |
| 668 | |
| 669 | err = -ENOENT; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 670 | |
| 671 | spin_lock_bh(&vxlan->hash_lock); |
| 672 | f = vxlan_find_mac(vxlan, addr); |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 673 | if (!f) |
| 674 | goto out; |
| 675 | |
| 676 | if (ip != htonl(INADDR_ANY)) { |
| 677 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); |
| 678 | if (!rd) |
| 679 | goto out; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 680 | } |
Mike Rapoport | bc7892b | 2013-06-25 16:01:54 +0300 | [diff] [blame] | 681 | |
| 682 | err = 0; |
| 683 | |
| 684 | /* remove a destination if it's not the only one on the list, |
| 685 | * otherwise destroy the fdb entry |
| 686 | */ |
| 687 | if (rd && !list_is_singular(&f->remotes)) { |
| 688 | list_del_rcu(&rd->list); |
| 689 | call_rcu(&rd->rcu, vxlan_fdb_free_rdst); |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | vxlan_fdb_destroy(vxlan, f); |
| 694 | |
| 695 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 696 | spin_unlock_bh(&vxlan->hash_lock); |
| 697 | |
| 698 | return err; |
| 699 | } |
| 700 | |
| 701 | /* Dump forwarding table */ |
| 702 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 703 | struct net_device *dev, int idx) |
| 704 | { |
| 705 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 706 | unsigned int h; |
| 707 | |
| 708 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 709 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 710 | int err; |
| 711 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 712 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 713 | struct vxlan_rdst *rd; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 714 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 715 | if (idx < cb->args[0]) |
| 716 | goto skip; |
| 717 | |
| 718 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 719 | err = vxlan_fdb_info(skb, vxlan, f, |
| 720 | NETLINK_CB(cb->skb).portid, |
| 721 | cb->nlh->nlmsg_seq, |
| 722 | RTM_NEWNEIGH, |
| 723 | NLM_F_MULTI, rd); |
| 724 | if (err < 0) |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 725 | goto out; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 726 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 727 | skip: |
| 728 | ++idx; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 731 | out: |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 732 | return idx; |
| 733 | } |
| 734 | |
| 735 | /* Watch incoming packets to learn mapping between Ethernet address |
| 736 | * and Tunnel endpoint. |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 737 | * Return true if packet is bogus and should be droppped. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 738 | */ |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 739 | static bool vxlan_snoop(struct net_device *dev, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 740 | __be32 src_ip, const u8 *src_mac) |
| 741 | { |
| 742 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 743 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 744 | |
| 745 | f = vxlan_find_mac(vxlan, src_mac); |
| 746 | if (likely(f)) { |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 747 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 748 | |
| 749 | if (likely(rdst->remote_ip == src_ip)) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 750 | return false; |
| 751 | |
| 752 | /* Don't migrate static entries, drop packets */ |
stephen hemminger | eb064c3 | 2013-06-18 14:27:01 -0700 | [diff] [blame] | 753 | if (f->state & NUD_NOARP) |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 754 | return true; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 755 | |
| 756 | if (net_ratelimit()) |
| 757 | netdev_info(dev, |
| 758 | "%pM migrated from %pI4 to %pI4\n", |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 759 | src_mac, &rdst->remote_ip, &src_ip); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 760 | |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 761 | rdst->remote_ip = src_ip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 762 | f->updated = jiffies; |
Stephen Hemminger | 8385f50 | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 763 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 764 | } else { |
| 765 | /* learned new entry */ |
| 766 | spin_lock(&vxlan->hash_lock); |
stephen hemminger | 3bf74b1 | 2013-06-17 12:09:57 -0700 | [diff] [blame] | 767 | |
| 768 | /* close off race between vxlan_flush and incoming packets */ |
| 769 | if (netif_running(dev)) |
| 770 | vxlan_fdb_create(vxlan, src_mac, src_ip, |
| 771 | NUD_REACHABLE, |
| 772 | NLM_F_EXCL|NLM_F_CREATE, |
| 773 | vxlan->dst_port, |
| 774 | vxlan->default_dst.remote_vni, |
| 775 | 0, NTF_SELF); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 776 | spin_unlock(&vxlan->hash_lock); |
| 777 | } |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 778 | |
| 779 | return false; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 782 | /* See if multicast group is already in use by other ID */ |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 783 | static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 784 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 785 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 786 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 787 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 788 | if (!netif_running(vxlan->dev)) |
| 789 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 790 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 791 | if (vxlan->default_dst.remote_ip == remote_ip) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 792 | return true; |
| 793 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 794 | |
| 795 | return false; |
| 796 | } |
| 797 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 798 | static void vxlan_sock_hold(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 799 | { |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 800 | atomic_inc(&vs->refcnt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 803 | void vxlan_sock_release(struct vxlan_sock *vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 804 | { |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 805 | struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
| 806 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 807 | if (!atomic_dec_and_test(&vs->refcnt)) |
| 808 | return; |
| 809 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 810 | spin_lock(&vn->sock_lock); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 811 | hlist_del_rcu(&vs->hlist); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 812 | spin_unlock(&vn->sock_lock); |
| 813 | |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 814 | queue_work(vxlan_wq, &vs->del_work); |
| 815 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 816 | EXPORT_SYMBOL_GPL(vxlan_sock_release); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 817 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 818 | /* Callback to update multicast group membership when first VNI on |
| 819 | * multicast asddress is brought up |
| 820 | * Done as workqueue because ip_mc_join_group acquires RTNL. |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 821 | */ |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 822 | static void vxlan_igmp_join(struct work_struct *work) |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 823 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 824 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 825 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 826 | struct sock *sk = vs->sock->sk; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 827 | struct ip_mreqn mreq = { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 828 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 829 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 832 | lock_sock(sk); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 833 | ip_mc_join_group(sk, &mreq); |
| 834 | release_sock(sk); |
| 835 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 836 | vxlan_sock_release(vs); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 837 | dev_put(vxlan->dev); |
| 838 | } |
| 839 | |
| 840 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ |
| 841 | static void vxlan_igmp_leave(struct work_struct *work) |
| 842 | { |
| 843 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 844 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 845 | struct sock *sk = vs->sock->sk; |
| 846 | struct ip_mreqn mreq = { |
| 847 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 848 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
| 849 | }; |
| 850 | |
| 851 | lock_sock(sk); |
| 852 | ip_mc_leave_group(sk, &mreq); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 853 | release_sock(sk); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 854 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 855 | vxlan_sock_release(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 856 | dev_put(vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 860 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 861 | { |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 862 | struct vxlan_sock *vs; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 863 | struct vxlanhdr *vxh; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 864 | __be16 port; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 865 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 866 | /* Need Vxlan and inner Ethernet header to be present */ |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 867 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 868 | goto error; |
| 869 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 870 | /* Return packets with reserved bits set */ |
| 871 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 872 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || |
| 873 | (vxh->vx_vni & htonl(0xff))) { |
| 874 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
| 875 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); |
| 876 | goto error; |
| 877 | } |
| 878 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 879 | if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 880 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 881 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 882 | port = inet_sk(sk)->inet_sport; |
| 883 | |
| 884 | vs = vxlan_find_sock(sock_net(sk), port); |
| 885 | if (!vs) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 886 | goto drop; |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 887 | |
| 888 | vs->rcv(vs, skb, vxh->vx_vni); |
| 889 | return 0; |
| 890 | |
| 891 | drop: |
| 892 | /* Consume bad packet */ |
| 893 | kfree_skb(skb); |
| 894 | return 0; |
| 895 | |
| 896 | error: |
| 897 | /* Return non vxlan pkt */ |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | static void vxlan_rcv(struct vxlan_sock *vs, |
| 902 | struct sk_buff *skb, __be32 vx_vni) |
| 903 | { |
| 904 | struct iphdr *oip; |
| 905 | struct vxlan_dev *vxlan; |
| 906 | struct pcpu_tstats *stats; |
| 907 | __u32 vni; |
| 908 | int err; |
| 909 | |
| 910 | vni = ntohl(vx_vni) >> 8; |
| 911 | /* Is this VNI defined? */ |
| 912 | vxlan = vxlan_vs_find_vni(vs, vni); |
| 913 | if (!vxlan) |
| 914 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 915 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 916 | skb_reset_mac_header(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 917 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 918 | |
| 919 | /* Ignore packet loops (and multicast echo) */ |
| 920 | if (compare_ether_addr(eth_hdr(skb)->h_source, |
| 921 | vxlan->dev->dev_addr) == 0) |
| 922 | goto drop; |
| 923 | |
Pravin B Shelar | 7ce0475 | 2013-08-19 11:22:54 -0700 | [diff] [blame] | 924 | /* Re-examine inner Ethernet packet */ |
| 925 | oip = ip_hdr(skb); |
stephen hemminger | 26a41ae6 | 2013-06-17 12:09:58 -0700 | [diff] [blame] | 926 | if ((vxlan->flags & VXLAN_F_LEARN) && |
| 927 | vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source)) |
| 928 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 929 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 930 | skb_reset_network_header(skb); |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 931 | |
| 932 | /* If the NIC driver gave us an encapsulated packet with |
| 933 | * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, |
| 934 | * leave the CHECKSUM_UNNECESSARY, the device checksummed it |
| 935 | * for us. Otherwise force the upper layers to verify it. |
| 936 | */ |
| 937 | if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || |
| 938 | !(vxlan->dev->features & NETIF_F_RXCSUM)) |
| 939 | skb->ip_summed = CHECKSUM_NONE; |
| 940 | |
| 941 | skb->encapsulation = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 942 | |
| 943 | err = IP_ECN_decapsulate(oip, skb); |
| 944 | if (unlikely(err)) { |
| 945 | if (log_ecn_error) |
| 946 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 947 | &oip->saddr, oip->tos); |
| 948 | if (err > 1) { |
| 949 | ++vxlan->dev->stats.rx_frame_errors; |
| 950 | ++vxlan->dev->stats.rx_errors; |
| 951 | goto drop; |
| 952 | } |
| 953 | } |
| 954 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 955 | stats = this_cpu_ptr(vxlan->dev->tstats); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 956 | u64_stats_update_begin(&stats->syncp); |
| 957 | stats->rx_packets++; |
| 958 | stats->rx_bytes += skb->len; |
| 959 | u64_stats_update_end(&stats->syncp); |
| 960 | |
| 961 | netif_rx(skb); |
| 962 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 963 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 964 | drop: |
| 965 | /* Consume bad packet */ |
| 966 | kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 967 | } |
| 968 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 969 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
| 970 | { |
| 971 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 972 | struct arphdr *parp; |
| 973 | u8 *arpptr, *sha; |
| 974 | __be32 sip, tip; |
| 975 | struct neighbour *n; |
| 976 | |
| 977 | if (dev->flags & IFF_NOARP) |
| 978 | goto out; |
| 979 | |
| 980 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { |
| 981 | dev->stats.tx_dropped++; |
| 982 | goto out; |
| 983 | } |
| 984 | parp = arp_hdr(skb); |
| 985 | |
| 986 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && |
| 987 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 988 | parp->ar_pro != htons(ETH_P_IP) || |
| 989 | parp->ar_op != htons(ARPOP_REQUEST) || |
| 990 | parp->ar_hln != dev->addr_len || |
| 991 | parp->ar_pln != 4) |
| 992 | goto out; |
| 993 | arpptr = (u8 *)parp + sizeof(struct arphdr); |
| 994 | sha = arpptr; |
| 995 | arpptr += dev->addr_len; /* sha */ |
| 996 | memcpy(&sip, arpptr, sizeof(sip)); |
| 997 | arpptr += sizeof(sip); |
| 998 | arpptr += dev->addr_len; /* tha */ |
| 999 | memcpy(&tip, arpptr, sizeof(tip)); |
| 1000 | |
| 1001 | if (ipv4_is_loopback(tip) || |
| 1002 | ipv4_is_multicast(tip)) |
| 1003 | goto out; |
| 1004 | |
| 1005 | n = neigh_lookup(&arp_tbl, &tip, dev); |
| 1006 | |
| 1007 | if (n) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1008 | struct vxlan_fdb *f; |
| 1009 | struct sk_buff *reply; |
| 1010 | |
| 1011 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 1012 | neigh_release(n); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
| 1016 | f = vxlan_find_mac(vxlan, n->ha); |
stephen hemminger | 5ca5461 | 2013-08-04 17:17:39 -0700 | [diff] [blame] | 1017 | if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1018 | /* bridge-local neighbor */ |
| 1019 | neigh_release(n); |
| 1020 | goto out; |
| 1021 | } |
| 1022 | |
| 1023 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, |
| 1024 | n->ha, sha); |
| 1025 | |
| 1026 | neigh_release(n); |
| 1027 | |
| 1028 | skb_reset_mac_header(reply); |
| 1029 | __skb_pull(reply, skb_network_offset(reply)); |
| 1030 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 1031 | reply->pkt_type = PACKET_HOST; |
| 1032 | |
| 1033 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 1034 | dev->stats.rx_dropped++; |
| 1035 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 1036 | vxlan_ip_miss(dev, tip); |
| 1037 | out: |
| 1038 | consume_skb(skb); |
| 1039 | return NETDEV_TX_OK; |
| 1040 | } |
| 1041 | |
| 1042 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
| 1043 | { |
| 1044 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1045 | struct neighbour *n; |
| 1046 | struct iphdr *pip; |
| 1047 | |
| 1048 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 1049 | return false; |
| 1050 | |
| 1051 | n = NULL; |
| 1052 | switch (ntohs(eth_hdr(skb)->h_proto)) { |
| 1053 | case ETH_P_IP: |
| 1054 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 1055 | return false; |
| 1056 | pip = ip_hdr(skb); |
| 1057 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); |
| 1058 | break; |
| 1059 | default: |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
| 1063 | if (n) { |
| 1064 | bool diff; |
| 1065 | |
| 1066 | diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0; |
| 1067 | if (diff) { |
| 1068 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, |
| 1069 | dev->addr_len); |
| 1070 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); |
| 1071 | } |
| 1072 | neigh_release(n); |
| 1073 | return diff; |
| 1074 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 1075 | vxlan_ip_miss(dev, pip->daddr); |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1079 | static void vxlan_sock_put(struct sk_buff *skb) |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1080 | { |
| 1081 | sock_put(skb->sk); |
| 1082 | } |
| 1083 | |
| 1084 | /* On transmit, associate with the tunnel socket */ |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1085 | static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb) |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1086 | { |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1087 | skb_orphan(skb); |
| 1088 | sock_hold(sk); |
| 1089 | skb->sk = sk; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1090 | skb->destructor = vxlan_sock_put; |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1093 | /* Compute source port for outgoing packet |
| 1094 | * first choice to use L4 flow hash since it will spread |
| 1095 | * better and maybe available from hardware |
| 1096 | * secondary choice is to use jhash on the Ethernet header |
| 1097 | */ |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1098 | __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb) |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1099 | { |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1100 | unsigned int range = (port_max - port_min) + 1; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1101 | u32 hash; |
| 1102 | |
| 1103 | hash = skb_get_rxhash(skb); |
| 1104 | if (!hash) |
| 1105 | hash = jhash(skb->data, 2 * ETH_ALEN, |
| 1106 | (__force u32) skb->protocol); |
| 1107 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1108 | return htons((((u64) hash * range) >> 32) + port_min); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1109 | } |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1110 | EXPORT_SYMBOL_GPL(vxlan_src_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1111 | |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1112 | static int handle_offloads(struct sk_buff *skb) |
| 1113 | { |
| 1114 | if (skb_is_gso(skb)) { |
| 1115 | int err = skb_unclone(skb, GFP_ATOMIC); |
| 1116 | if (unlikely(err)) |
| 1117 | return err; |
| 1118 | |
Dmitry Kravkov | f6ace50 | 2013-04-28 08:16:01 +0000 | [diff] [blame] | 1119 | skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1120 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 1121 | skb->ip_summed = CHECKSUM_NONE; |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1126 | int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs, |
| 1127 | struct rtable *rt, struct sk_buff *skb, |
| 1128 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, |
| 1129 | __be16 src_port, __be16 dst_port, __be32 vni) |
| 1130 | { |
| 1131 | struct vxlanhdr *vxh; |
| 1132 | struct udphdr *uh; |
| 1133 | int err; |
| 1134 | |
| 1135 | if (!skb->encapsulation) { |
| 1136 | skb_reset_inner_headers(skb); |
| 1137 | skb->encapsulation = 1; |
| 1138 | } |
| 1139 | |
| 1140 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
| 1141 | vxh->vx_flags = htonl(VXLAN_FLAGS); |
| 1142 | vxh->vx_vni = vni; |
| 1143 | |
| 1144 | __skb_push(skb, sizeof(*uh)); |
| 1145 | skb_reset_transport_header(skb); |
| 1146 | uh = udp_hdr(skb); |
| 1147 | |
| 1148 | uh->dest = dst_port; |
| 1149 | uh->source = src_port; |
| 1150 | |
| 1151 | uh->len = htons(skb->len); |
| 1152 | uh->check = 0; |
| 1153 | |
| 1154 | vxlan_set_owner(vs->sock->sk, skb); |
| 1155 | |
| 1156 | err = handle_offloads(skb); |
| 1157 | if (err) |
| 1158 | return err; |
| 1159 | |
| 1160 | return iptunnel_xmit(net, rt, skb, src, dst, |
| 1161 | IPPROTO_UDP, tos, ttl, df); |
| 1162 | } |
| 1163 | EXPORT_SYMBOL_GPL(vxlan_xmit_skb); |
| 1164 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1165 | /* Bypass encapsulation if the destination is local */ |
| 1166 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, |
| 1167 | struct vxlan_dev *dst_vxlan) |
| 1168 | { |
| 1169 | struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
| 1170 | struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); |
| 1171 | |
| 1172 | skb->pkt_type = PACKET_HOST; |
| 1173 | skb->encapsulation = 0; |
| 1174 | skb->dev = dst_vxlan->dev; |
| 1175 | __skb_pull(skb, skb_network_offset(skb)); |
| 1176 | |
| 1177 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
Mike Rapoport | 9d9f163 | 2013-04-13 23:21:39 +0000 | [diff] [blame] | 1178 | vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK), |
| 1179 | eth_hdr(skb)->h_source); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1180 | |
| 1181 | u64_stats_update_begin(&tx_stats->syncp); |
| 1182 | tx_stats->tx_packets++; |
| 1183 | tx_stats->tx_bytes += skb->len; |
| 1184 | u64_stats_update_end(&tx_stats->syncp); |
| 1185 | |
| 1186 | if (netif_rx(skb) == NET_RX_SUCCESS) { |
| 1187 | u64_stats_update_begin(&rx_stats->syncp); |
| 1188 | rx_stats->rx_packets++; |
| 1189 | rx_stats->rx_bytes += skb->len; |
| 1190 | u64_stats_update_end(&rx_stats->syncp); |
| 1191 | } else { |
| 1192 | skb->dev->stats.rx_dropped++; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1196 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
| 1197 | struct vxlan_rdst *rdst, bool did_rsc) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1198 | { |
| 1199 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1200 | struct rtable *rt; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1201 | const struct iphdr *old_iph; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1202 | struct flowi4 fl4; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1203 | __be32 dst; |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 1204 | __be16 src_port, dst_port; |
Stephen Hemminger | 234f5b7 | 2013-06-17 14:16:41 -0700 | [diff] [blame] | 1205 | u32 vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1206 | __be16 df = 0; |
| 1207 | __u8 tos, ttl; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1208 | int err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1209 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1210 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1211 | vni = rdst->remote_vni; |
| 1212 | dst = rdst->remote_ip; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (!dst) { |
| 1215 | if (did_rsc) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1216 | /* short-circuited back to local bridge */ |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1217 | vxlan_encap_bypass(skb, vxlan, vxlan); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1218 | return; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1219 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1220 | goto drop; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1221 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1222 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1223 | /* Need space for new headers (invalidates iph ptr) */ |
| 1224 | if (skb_cow_head(skb, VXLAN_HEADROOM)) |
| 1225 | goto drop; |
| 1226 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1227 | old_iph = ip_hdr(skb); |
| 1228 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1229 | ttl = vxlan->ttl; |
| 1230 | if (!ttl && IN_MULTICAST(ntohl(dst))) |
| 1231 | ttl = 1; |
| 1232 | |
| 1233 | tos = vxlan->tos; |
| 1234 | if (tos == 1) |
Pravin B Shelar | 206aaaf | 2013-03-25 14:49:53 +0000 | [diff] [blame] | 1235 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1236 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1237 | src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1238 | |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1239 | memset(&fl4, 0, sizeof(fl4)); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1240 | fl4.flowi4_oif = rdst->remote_ifindex; |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1241 | fl4.flowi4_tos = RT_TOS(tos); |
| 1242 | fl4.daddr = dst; |
| 1243 | fl4.saddr = vxlan->saddr; |
| 1244 | |
| 1245 | rt = ip_route_output_key(dev_net(dev), &fl4); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1246 | if (IS_ERR(rt)) { |
| 1247 | netdev_dbg(dev, "no route to %pI4\n", &dst); |
| 1248 | dev->stats.tx_carrier_errors++; |
| 1249 | goto tx_error; |
| 1250 | } |
| 1251 | |
| 1252 | if (rt->dst.dev == dev) { |
| 1253 | netdev_dbg(dev, "circular route to %pI4\n", &dst); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1254 | dev->stats.collisions++; |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1255 | goto rt_tx_error; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1258 | /* Bypass encapsulation if the destination is local */ |
Mike Rapoport | ab09a6d | 2013-04-13 23:21:51 +0000 | [diff] [blame] | 1259 | if (rt->rt_flags & RTCF_LOCAL && |
| 1260 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1261 | struct vxlan_dev *dst_vxlan; |
| 1262 | |
| 1263 | ip_rt_put(rt); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1264 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1265 | if (!dst_vxlan) |
| 1266 | goto tx_error; |
| 1267 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1268 | return; |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1269 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1270 | |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1271 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
| 1272 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
| 1273 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1274 | err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb, |
| 1275 | fl4.saddr, dst, tos, ttl, df, |
| 1276 | src_port, dst_port, htonl(vni << 8)); |
| 1277 | |
| 1278 | if (err < 0) |
| 1279 | goto rt_tx_error; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 1280 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
| 1281 | |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1282 | return; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1283 | |
| 1284 | drop: |
| 1285 | dev->stats.tx_dropped++; |
| 1286 | goto tx_free; |
| 1287 | |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame^] | 1288 | rt_tx_error: |
| 1289 | ip_rt_put(rt); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1290 | tx_error: |
| 1291 | dev->stats.tx_errors++; |
| 1292 | tx_free: |
| 1293 | dev_kfree_skb(skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1296 | /* Transmit local packets over Vxlan |
| 1297 | * |
| 1298 | * Outer IP header inherits ECN and DF from inner header. |
| 1299 | * Outer UDP destination is the VXLAN assigned port. |
| 1300 | * source port is based on hash of flow |
| 1301 | */ |
| 1302 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1303 | { |
| 1304 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1305 | struct ethhdr *eth; |
| 1306 | bool did_rsc = false; |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1307 | struct vxlan_rdst *rdst; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1308 | struct vxlan_fdb *f; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1309 | |
| 1310 | skb_reset_mac_header(skb); |
| 1311 | eth = eth_hdr(skb); |
| 1312 | |
| 1313 | if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP) |
| 1314 | return arp_reduce(dev, skb); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1315 | |
| 1316 | f = vxlan_find_mac(vxlan, eth->h_dest); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 1317 | did_rsc = false; |
| 1318 | |
| 1319 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && |
| 1320 | ntohs(eth->h_proto) == ETH_P_IP) { |
| 1321 | did_rsc = route_shortcircuit(dev, skb); |
| 1322 | if (did_rsc) |
| 1323 | f = vxlan_find_mac(vxlan, eth->h_dest); |
| 1324 | } |
| 1325 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1326 | if (f == NULL) { |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1327 | f = vxlan_find_mac(vxlan, all_zeros_mac); |
| 1328 | if (f == NULL) { |
| 1329 | if ((vxlan->flags & VXLAN_F_L2MISS) && |
| 1330 | !is_multicast_ether_addr(eth->h_dest)) |
| 1331 | vxlan_fdb_miss(vxlan, eth->h_dest); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1332 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1333 | dev->stats.tx_dropped++; |
| 1334 | dev_kfree_skb(skb); |
| 1335 | return NETDEV_TX_OK; |
Stephen Hemminger | 3e61aa8 | 2013-06-17 14:16:12 -0700 | [diff] [blame] | 1336 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1339 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
| 1340 | struct sk_buff *skb1; |
| 1341 | |
| 1342 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 1343 | if (skb1) |
| 1344 | vxlan_xmit_one(skb1, dev, rdst, did_rsc); |
| 1345 | } |
| 1346 | |
| 1347 | dev_kfree_skb(skb); |
Stephen Hemminger | 4ad1693 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1348 | return NETDEV_TX_OK; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1351 | /* Walk the forwarding table and purge stale entries */ |
| 1352 | static void vxlan_cleanup(unsigned long arg) |
| 1353 | { |
| 1354 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; |
| 1355 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
| 1356 | unsigned int h; |
| 1357 | |
| 1358 | if (!netif_running(vxlan->dev)) |
| 1359 | return; |
| 1360 | |
| 1361 | spin_lock_bh(&vxlan->hash_lock); |
| 1362 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1363 | struct hlist_node *p, *n; |
| 1364 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1365 | struct vxlan_fdb *f |
| 1366 | = container_of(p, struct vxlan_fdb, hlist); |
| 1367 | unsigned long timeout; |
| 1368 | |
stephen hemminger | 3c17286 | 2012-10-26 06:24:34 +0000 | [diff] [blame] | 1369 | if (f->state & NUD_PERMANENT) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1370 | continue; |
| 1371 | |
| 1372 | timeout = f->used + vxlan->age_interval * HZ; |
| 1373 | if (time_before_eq(timeout, jiffies)) { |
| 1374 | netdev_dbg(vxlan->dev, |
| 1375 | "garbage collect %pM\n", |
| 1376 | f->eth_addr); |
| 1377 | f->state = NUD_STALE; |
| 1378 | vxlan_fdb_destroy(vxlan, f); |
| 1379 | } else if (time_before(timeout, next_timer)) |
| 1380 | next_timer = timeout; |
| 1381 | } |
| 1382 | } |
| 1383 | spin_unlock_bh(&vxlan->hash_lock); |
| 1384 | |
| 1385 | mod_timer(&vxlan->age_timer, next_timer); |
| 1386 | } |
| 1387 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1388 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) |
| 1389 | { |
| 1390 | __u32 vni = vxlan->default_dst.remote_vni; |
| 1391 | |
| 1392 | vxlan->vn_sock = vs; |
| 1393 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); |
| 1394 | } |
| 1395 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1396 | /* Setup stats when device is created */ |
| 1397 | static int vxlan_init(struct net_device *dev) |
| 1398 | { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1399 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1400 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
| 1401 | struct vxlan_sock *vs; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1402 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1403 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1404 | if (!dev->tstats) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1405 | return -ENOMEM; |
| 1406 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1407 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1408 | vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1409 | if (vs) { |
| 1410 | /* If we have a socket with same port already, reuse it */ |
| 1411 | atomic_inc(&vs->refcnt); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1412 | vxlan_vs_add_dev(vs, vxlan); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1413 | } else { |
| 1414 | /* otherwise make new socket outside of RTNL */ |
| 1415 | dev_hold(dev); |
| 1416 | queue_work(vxlan_wq, &vxlan->sock_work); |
| 1417 | } |
| 1418 | spin_unlock(&vn->sock_lock); |
| 1419 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1423 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1424 | { |
| 1425 | struct vxlan_fdb *f; |
| 1426 | |
| 1427 | spin_lock_bh(&vxlan->hash_lock); |
| 1428 | f = __vxlan_find_mac(vxlan, all_zeros_mac); |
| 1429 | if (f) |
| 1430 | vxlan_fdb_destroy(vxlan, f); |
| 1431 | spin_unlock_bh(&vxlan->hash_lock); |
| 1432 | } |
| 1433 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1434 | static void vxlan_uninit(struct net_device *dev) |
| 1435 | { |
| 1436 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1437 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1438 | |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1439 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1440 | |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1441 | if (vs) |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1442 | vxlan_sock_release(vs); |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1443 | free_percpu(dev->tstats); |
| 1444 | } |
| 1445 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1446 | /* Start ageing timer and join group when device is brought up */ |
| 1447 | static int vxlan_open(struct net_device *dev) |
| 1448 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1449 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1450 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1451 | struct vxlan_sock *vs = vxlan->vn_sock; |
| 1452 | |
| 1453 | /* socket hasn't been created */ |
| 1454 | if (!vs) |
| 1455 | return -ENOTCONN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1456 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1457 | if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) && |
Cong Wang | 614334d | 2013-08-07 16:35:45 +0800 | [diff] [blame] | 1458 | vxlan_group_used(vn, vxlan->default_dst.remote_ip)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1459 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1460 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1461 | queue_work(vxlan_wq, &vxlan->igmp_join); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | if (vxlan->age_interval) |
| 1465 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | /* Purge the forwarding table */ |
| 1471 | static void vxlan_flush(struct vxlan_dev *vxlan) |
| 1472 | { |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1473 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1474 | |
| 1475 | spin_lock_bh(&vxlan->hash_lock); |
| 1476 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1477 | struct hlist_node *p, *n; |
| 1478 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1479 | struct vxlan_fdb *f |
| 1480 | = container_of(p, struct vxlan_fdb, hlist); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1481 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
| 1482 | if (!is_zero_ether_addr(f->eth_addr)) |
| 1483 | vxlan_fdb_destroy(vxlan, f); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | spin_unlock_bh(&vxlan->hash_lock); |
| 1487 | } |
| 1488 | |
| 1489 | /* Cleanup timer and forwarding table on shutdown */ |
| 1490 | static int vxlan_stop(struct net_device *dev) |
| 1491 | { |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1492 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1493 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1494 | struct vxlan_sock *vs = vxlan->vn_sock; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1495 | |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1496 | if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) && |
| 1497 | ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) { |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1498 | vxlan_sock_hold(vs); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1499 | dev_hold(dev); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1500 | queue_work(vxlan_wq, &vxlan->igmp_leave); |
Stephen Hemminger | 7c47ced | 2013-06-17 14:16:10 -0700 | [diff] [blame] | 1501 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1502 | |
| 1503 | del_timer_sync(&vxlan->age_timer); |
| 1504 | |
| 1505 | vxlan_flush(vxlan); |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1510 | /* Stub, nothing needs to be done. */ |
| 1511 | static void vxlan_set_multicast_list(struct net_device *dev) |
| 1512 | { |
| 1513 | } |
| 1514 | |
| 1515 | static const struct net_device_ops vxlan_netdev_ops = { |
| 1516 | .ndo_init = vxlan_init, |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1517 | .ndo_uninit = vxlan_uninit, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1518 | .ndo_open = vxlan_open, |
| 1519 | .ndo_stop = vxlan_stop, |
| 1520 | .ndo_start_xmit = vxlan_xmit, |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1521 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1522 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
| 1523 | .ndo_change_mtu = eth_change_mtu, |
| 1524 | .ndo_validate_addr = eth_validate_addr, |
| 1525 | .ndo_set_mac_address = eth_mac_addr, |
| 1526 | .ndo_fdb_add = vxlan_fdb_add, |
| 1527 | .ndo_fdb_del = vxlan_fdb_delete, |
| 1528 | .ndo_fdb_dump = vxlan_fdb_dump, |
| 1529 | }; |
| 1530 | |
| 1531 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1532 | static struct device_type vxlan_type = { |
| 1533 | .name = "vxlan", |
| 1534 | }; |
| 1535 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1536 | /* Initialize the device structure. */ |
| 1537 | static void vxlan_setup(struct net_device *dev) |
| 1538 | { |
| 1539 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1540 | unsigned int h; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1541 | int low, high; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1542 | |
| 1543 | eth_hw_addr_random(dev); |
| 1544 | ether_setup(dev); |
stephen hemminger | 2840bf2 | 2012-10-09 20:35:51 +0000 | [diff] [blame] | 1545 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1546 | |
| 1547 | dev->netdev_ops = &vxlan_netdev_ops; |
Stephen Hemminger | ebf4063 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1548 | dev->destructor = free_netdev; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1549 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
| 1550 | |
| 1551 | dev->tx_queue_len = 0; |
| 1552 | dev->features |= NETIF_F_LLTX; |
| 1553 | dev->features |= NETIF_F_NETNS_LOCAL; |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 1554 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1555 | dev->features |= NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1556 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1557 | |
| 1558 | 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] | 1559 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1560 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
stephen hemminger | 6602d00 | 2012-12-31 12:00:21 +0000 | [diff] [blame] | 1561 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1562 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1563 | INIT_LIST_HEAD(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1564 | spin_lock_init(&vxlan->hash_lock); |
stephen hemminger | 3fc2de2 | 2013-07-18 08:40:15 -0700 | [diff] [blame] | 1565 | INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); |
| 1566 | INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1567 | INIT_WORK(&vxlan->sock_work, vxlan_sock_work); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1568 | |
| 1569 | init_timer_deferrable(&vxlan->age_timer); |
| 1570 | vxlan->age_timer.function = vxlan_cleanup; |
| 1571 | vxlan->age_timer.data = (unsigned long) vxlan; |
| 1572 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1573 | inet_get_local_port_range(&low, &high); |
| 1574 | vxlan->port_min = low; |
| 1575 | vxlan->port_max = high; |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1576 | vxlan->dst_port = htons(vxlan_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1577 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1578 | vxlan->dev = dev; |
| 1579 | |
| 1580 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
| 1581 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
| 1582 | } |
| 1583 | |
| 1584 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
| 1585 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1586 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1587 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
| 1588 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
| 1589 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
| 1590 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
| 1591 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
| 1592 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
| 1593 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1594 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1595 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
| 1596 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, |
| 1597 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, |
| 1598 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1599 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1600 | }; |
| 1601 | |
| 1602 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1603 | { |
| 1604 | if (tb[IFLA_ADDRESS]) { |
| 1605 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 1606 | pr_debug("invalid link address (not ethernet)\n"); |
| 1607 | return -EINVAL; |
| 1608 | } |
| 1609 | |
| 1610 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 1611 | pr_debug("invalid all zero ethernet address\n"); |
| 1612 | return -EADDRNOTAVAIL; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if (!data) |
| 1617 | return -EINVAL; |
| 1618 | |
| 1619 | if (data[IFLA_VXLAN_ID]) { |
| 1620 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
| 1621 | if (id >= VXLAN_VID_MASK) |
| 1622 | return -ERANGE; |
| 1623 | } |
| 1624 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1625 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1626 | const struct ifla_vxlan_port_range *p |
| 1627 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1628 | |
| 1629 | if (ntohs(p->high) < ntohs(p->low)) { |
| 1630 | pr_debug("port range %u .. %u not valid\n", |
| 1631 | ntohs(p->low), ntohs(p->high)); |
| 1632 | return -EINVAL; |
| 1633 | } |
| 1634 | } |
| 1635 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1636 | return 0; |
| 1637 | } |
| 1638 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1639 | static void vxlan_get_drvinfo(struct net_device *netdev, |
| 1640 | struct ethtool_drvinfo *drvinfo) |
| 1641 | { |
| 1642 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); |
| 1643 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); |
| 1644 | } |
| 1645 | |
| 1646 | static const struct ethtool_ops vxlan_ethtool_ops = { |
| 1647 | .get_drvinfo = vxlan_get_drvinfo, |
| 1648 | .get_link = ethtool_op_get_link, |
| 1649 | }; |
| 1650 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1651 | static void vxlan_del_work(struct work_struct *work) |
| 1652 | { |
| 1653 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); |
| 1654 | |
| 1655 | sk_release_kernel(vs->sock->sk); |
| 1656 | kfree_rcu(vs, rcu); |
| 1657 | } |
| 1658 | |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1659 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1660 | vxlan_rcv_t *rcv, void *data) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1661 | { |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1662 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1663 | struct vxlan_sock *vs; |
| 1664 | struct sock *sk; |
| 1665 | struct sockaddr_in vxlan_addr = { |
| 1666 | .sin_family = AF_INET, |
| 1667 | .sin_addr.s_addr = htonl(INADDR_ANY), |
Stephen Hemminger | bb3fd68 | 2013-06-17 14:16:40 -0700 | [diff] [blame] | 1668 | .sin_port = port, |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1669 | }; |
| 1670 | int rc; |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1671 | unsigned int h; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1672 | |
| 1673 | vs = kmalloc(sizeof(*vs), GFP_KERNEL); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1674 | if (!vs) { |
| 1675 | pr_debug("memory alocation failure\n"); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1676 | return ERR_PTR(-ENOMEM); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1677 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1678 | |
| 1679 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 1680 | INIT_HLIST_HEAD(&vs->vni_list[h]); |
| 1681 | |
| 1682 | INIT_WORK(&vs->del_work, vxlan_del_work); |
| 1683 | |
| 1684 | /* Create UDP socket for encapsulation receive. */ |
| 1685 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock); |
| 1686 | if (rc < 0) { |
| 1687 | pr_debug("UDP socket create failed\n"); |
| 1688 | kfree(vs); |
| 1689 | return ERR_PTR(rc); |
| 1690 | } |
| 1691 | |
| 1692 | /* Put in proper namespace */ |
| 1693 | sk = vs->sock->sk; |
| 1694 | sk_change_net(sk, net); |
| 1695 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1696 | rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr, |
| 1697 | sizeof(vxlan_addr)); |
| 1698 | if (rc < 0) { |
| 1699 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", |
| 1700 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); |
| 1701 | sk_release_kernel(sk); |
| 1702 | kfree(vs); |
| 1703 | return ERR_PTR(rc); |
| 1704 | } |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1705 | atomic_set(&vs->refcnt, 1); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1706 | vs->rcv = rcv; |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1707 | vs->data = data; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* Disable multicast loopback */ |
| 1710 | inet_sk(sk)->mc_loop = 0; |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1711 | spin_lock(&vn->sock_lock); |
| 1712 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); |
| 1713 | spin_unlock(&vn->sock_lock); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1714 | |
| 1715 | /* Mark socket as an encapsulation socket. */ |
| 1716 | udp_sk(sk)->encap_type = 1; |
| 1717 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; |
| 1718 | udp_encap_enable(); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1719 | return vs; |
| 1720 | } |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1721 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1722 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
| 1723 | vxlan_rcv_t *rcv, void *data, |
| 1724 | bool no_share) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1725 | { |
| 1726 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 1727 | struct vxlan_sock *vs; |
| 1728 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1729 | vs = vxlan_socket_create(net, port, rcv, data); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1730 | if (!IS_ERR(vs)) |
| 1731 | return vs; |
| 1732 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1733 | if (no_share) /* Return error if sharing is not allowed. */ |
| 1734 | return vs; |
| 1735 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1736 | spin_lock(&vn->sock_lock); |
| 1737 | vs = vxlan_find_sock(net, port); |
Pravin B Shelar | 5cfccc5 | 2013-08-19 11:23:02 -0700 | [diff] [blame] | 1738 | if (vs) { |
| 1739 | if (vs->rcv == rcv) |
| 1740 | atomic_inc(&vs->refcnt); |
| 1741 | else |
| 1742 | vs = ERR_PTR(-EBUSY); |
| 1743 | } |
| 1744 | spin_unlock(&vn->sock_lock); |
| 1745 | |
| 1746 | if (!vs) |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1747 | vs = ERR_PTR(-EINVAL); |
| 1748 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1749 | return vs; |
| 1750 | } |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1751 | EXPORT_SYMBOL_GPL(vxlan_sock_add); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1752 | |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1753 | /* Scheduled at device creation to bind to a socket */ |
| 1754 | static void vxlan_sock_work(struct work_struct *work) |
| 1755 | { |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1756 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); |
| 1757 | struct net *net = dev_net(vxlan->dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1758 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1759 | __be16 port = vxlan->dst_port; |
| 1760 | struct vxlan_sock *nvs; |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1761 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1762 | nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1763 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1764 | if (!IS_ERR(nvs)) |
| 1765 | vxlan_vs_add_dev(nvs, vxlan); |
| 1766 | spin_unlock(&vn->sock_lock); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1767 | |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1768 | dev_put(vxlan->dev); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1771 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
| 1772 | struct nlattr *tb[], struct nlattr *data[]) |
| 1773 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1774 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1775 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1776 | struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1777 | __u32 vni; |
| 1778 | int err; |
| 1779 | |
| 1780 | if (!data[IFLA_VXLAN_ID]) |
| 1781 | return -EINVAL; |
| 1782 | |
| 1783 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1784 | dst->remote_vni = vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1785 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1786 | if (data[IFLA_VXLAN_GROUP]) |
| 1787 | dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1788 | |
| 1789 | if (data[IFLA_VXLAN_LOCAL]) |
| 1790 | vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); |
| 1791 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1792 | if (data[IFLA_VXLAN_LINK] && |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1793 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1794 | struct net_device *lowerdev |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1795 | = __dev_get_by_index(net, dst->remote_ifindex); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1796 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1797 | if (!lowerdev) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1798 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1799 | return -ENODEV; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1800 | } |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (!tb[IFLA_MTU]) |
| 1803 | dev->mtu = lowerdev->mtu - VXLAN_HEADROOM; |
Alexander Duyck | 1ba56fb | 2012-11-13 13:10:59 +0000 | [diff] [blame] | 1804 | |
| 1805 | /* update header length based on lower device */ |
| 1806 | dev->hard_header_len = lowerdev->hard_header_len + |
| 1807 | VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | if (data[IFLA_VXLAN_TOS]) |
| 1811 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
| 1812 | |
Vincent Bernat | afb9718 | 2012-10-30 10:27:16 +0000 | [diff] [blame] | 1813 | if (data[IFLA_VXLAN_TTL]) |
| 1814 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
| 1815 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1816 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1817 | vxlan->flags |= VXLAN_F_LEARN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1818 | |
| 1819 | if (data[IFLA_VXLAN_AGEING]) |
| 1820 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
| 1821 | else |
| 1822 | vxlan->age_interval = FDB_AGE_DEFAULT; |
| 1823 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1824 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
| 1825 | vxlan->flags |= VXLAN_F_PROXY; |
| 1826 | |
| 1827 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) |
| 1828 | vxlan->flags |= VXLAN_F_RSC; |
| 1829 | |
| 1830 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) |
| 1831 | vxlan->flags |= VXLAN_F_L2MISS; |
| 1832 | |
| 1833 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) |
| 1834 | vxlan->flags |= VXLAN_F_L3MISS; |
| 1835 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1836 | if (data[IFLA_VXLAN_LIMIT]) |
| 1837 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
| 1838 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1839 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1840 | const struct ifla_vxlan_port_range *p |
| 1841 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1842 | vxlan->port_min = ntohs(p->low); |
| 1843 | vxlan->port_max = ntohs(p->high); |
| 1844 | } |
| 1845 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1846 | if (data[IFLA_VXLAN_PORT]) |
| 1847 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); |
| 1848 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1849 | if (vxlan_find_vni(net, vni, vxlan->dst_port)) { |
| 1850 | pr_info("duplicate VNI %u\n", vni); |
| 1851 | return -EEXIST; |
| 1852 | } |
| 1853 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1854 | SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); |
| 1855 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1856 | /* create an fdb entry for default destination */ |
| 1857 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
| 1858 | vxlan->default_dst.remote_ip, |
| 1859 | NUD_REACHABLE|NUD_PERMANENT, |
| 1860 | NLM_F_EXCL|NLM_F_CREATE, |
| 1861 | vxlan->dst_port, vxlan->default_dst.remote_vni, |
| 1862 | vxlan->default_dst.remote_ifindex, NTF_SELF); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1863 | if (err) |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1864 | return err; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1865 | |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1866 | err = register_netdevice(dev); |
| 1867 | if (err) { |
Stephen Hemminger | ba609e9 | 2013-06-25 17:06:01 -0700 | [diff] [blame] | 1868 | vxlan_fdb_delete_default(vxlan); |
Mike Rapoport | afbd8ba | 2013-06-25 16:01:51 +0300 | [diff] [blame] | 1869 | return err; |
| 1870 | } |
| 1871 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1872 | list_add(&vxlan->next, &vn->vxlan_list); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1873 | |
| 1874 | return 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
| 1878 | { |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1879 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1880 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1881 | |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1882 | spin_lock(&vn->sock_lock); |
Pravin B Shelar | 9c2e24e | 2013-08-19 11:22:48 -0700 | [diff] [blame] | 1883 | if (!hlist_unhashed(&vxlan->hlist)) |
| 1884 | hlist_del_rcu(&vxlan->hlist); |
stephen hemminger | fe5c356 | 2013-07-13 10:18:18 -0700 | [diff] [blame] | 1885 | spin_unlock(&vn->sock_lock); |
| 1886 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1887 | list_del(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1888 | unregister_netdevice_queue(dev, head); |
| 1889 | } |
| 1890 | |
| 1891 | static size_t vxlan_get_size(const struct net_device *dev) |
| 1892 | { |
| 1893 | |
| 1894 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1895 | nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1896 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
| 1897 | nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */ |
| 1898 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
| 1899 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
| 1900 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1901 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
| 1902 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ |
| 1903 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ |
| 1904 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1905 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
| 1906 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1907 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1908 | nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1909 | 0; |
| 1910 | } |
| 1911 | |
| 1912 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1913 | { |
| 1914 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1915 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1916 | struct ifla_vxlan_port_range ports = { |
| 1917 | .low = htons(vxlan->port_min), |
| 1918 | .high = htons(vxlan->port_max), |
| 1919 | }; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1920 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1921 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1922 | goto nla_put_failure; |
| 1923 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1924 | if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1925 | goto nla_put_failure; |
| 1926 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1927 | 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] | 1928 | goto nla_put_failure; |
| 1929 | |
Stephen Hemminger | 7c41c42 | 2012-10-08 14:55:30 -0700 | [diff] [blame] | 1930 | if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1931 | goto nla_put_failure; |
| 1932 | |
| 1933 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || |
| 1934 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1935 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
| 1936 | !!(vxlan->flags & VXLAN_F_LEARN)) || |
| 1937 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
| 1938 | !!(vxlan->flags & VXLAN_F_PROXY)) || |
| 1939 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || |
| 1940 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
| 1941 | !!(vxlan->flags & VXLAN_F_L2MISS)) || |
| 1942 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
| 1943 | !!(vxlan->flags & VXLAN_F_L3MISS)) || |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1944 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1945 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
| 1946 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1947 | goto nla_put_failure; |
| 1948 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1949 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
| 1950 | goto nla_put_failure; |
| 1951 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1952 | return 0; |
| 1953 | |
| 1954 | nla_put_failure: |
| 1955 | return -EMSGSIZE; |
| 1956 | } |
| 1957 | |
| 1958 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
| 1959 | .kind = "vxlan", |
| 1960 | .maxtype = IFLA_VXLAN_MAX, |
| 1961 | .policy = vxlan_policy, |
| 1962 | .priv_size = sizeof(struct vxlan_dev), |
| 1963 | .setup = vxlan_setup, |
| 1964 | .validate = vxlan_validate, |
| 1965 | .newlink = vxlan_newlink, |
| 1966 | .dellink = vxlan_dellink, |
| 1967 | .get_size = vxlan_get_size, |
| 1968 | .fill_info = vxlan_fill_info, |
| 1969 | }; |
| 1970 | |
| 1971 | static __net_init int vxlan_init_net(struct net *net) |
| 1972 | { |
| 1973 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Cong Wang | 31fec5a | 2013-05-27 22:35:52 +0000 | [diff] [blame] | 1974 | unsigned int h; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1975 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1976 | INIT_LIST_HEAD(&vn->vxlan_list); |
Stephen Hemminger | 1c51a91 | 2013-06-17 14:16:11 -0700 | [diff] [blame] | 1977 | spin_lock_init(&vn->sock_lock); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1978 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1979 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 1980 | INIT_HLIST_HEAD(&vn->sock_list[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1981 | |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | static __net_exit void vxlan_exit_net(struct net *net) |
| 1986 | { |
| 1987 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1988 | struct vxlan_dev *vxlan; |
stephen hemminger | 372675a | 2013-07-18 08:38:26 -0700 | [diff] [blame] | 1989 | LIST_HEAD(list); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1990 | |
| 1991 | rtnl_lock(); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame] | 1992 | list_for_each_entry(vxlan, &vn->vxlan_list, next) |
stephen hemminger | 372675a | 2013-07-18 08:38:26 -0700 | [diff] [blame] | 1993 | unregister_netdevice_queue(vxlan->dev, &list); |
| 1994 | unregister_netdevice_many(&list); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1995 | rtnl_unlock(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | static struct pernet_operations vxlan_net_ops = { |
| 1999 | .init = vxlan_init_net, |
| 2000 | .exit = vxlan_exit_net, |
| 2001 | .id = &vxlan_net_id, |
| 2002 | .size = sizeof(struct vxlan_net), |
| 2003 | }; |
| 2004 | |
| 2005 | static int __init vxlan_init_module(void) |
| 2006 | { |
| 2007 | int rc; |
| 2008 | |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2009 | vxlan_wq = alloc_workqueue("vxlan", 0, 0); |
| 2010 | if (!vxlan_wq) |
| 2011 | return -ENOMEM; |
| 2012 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2013 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
| 2014 | |
| 2015 | rc = register_pernet_device(&vxlan_net_ops); |
| 2016 | if (rc) |
| 2017 | goto out1; |
| 2018 | |
| 2019 | rc = rtnl_link_register(&vxlan_link_ops); |
| 2020 | if (rc) |
| 2021 | goto out2; |
| 2022 | |
| 2023 | return 0; |
| 2024 | |
| 2025 | out2: |
| 2026 | unregister_pernet_device(&vxlan_net_ops); |
| 2027 | out1: |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2028 | destroy_workqueue(vxlan_wq); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2029 | return rc; |
| 2030 | } |
Cong Wang | 7332a13 | 2013-05-27 22:35:53 +0000 | [diff] [blame] | 2031 | late_initcall(vxlan_init_module); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2032 | |
| 2033 | static void __exit vxlan_cleanup_module(void) |
| 2034 | { |
Stephen Hemminger | b715398 | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2035 | rtnl_link_unregister(&vxlan_link_ops); |
Stephen Hemminger | 758c57d | 2013-06-17 14:16:09 -0700 | [diff] [blame] | 2036 | destroy_workqueue(vxlan_wq); |
Pravin B Shelar | f89e57c | 2013-07-11 11:38:06 -0700 | [diff] [blame] | 2037 | unregister_pernet_device(&vxlan_net_ops); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 2038 | rcu_barrier(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2039 | } |
| 2040 | module_exit(vxlan_cleanup_module); |
| 2041 | |
| 2042 | MODULE_LICENSE("GPL"); |
| 2043 | MODULE_VERSION(VXLAN_VERSION); |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 2044 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 2045 | MODULE_ALIAS_RTNL_LINK("vxlan"); |