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