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