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