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