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