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