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