d342894c |
1 | /* |
2 | * VXLAN: Virtual eXtensiable Local Area Network |
3 | * |
4 | * Copyright (c) 2012 Vyatta Inc. |
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 |
11 | * - use IANA UDP port number (when defined) |
12 | * - IPv6 (not in RFC) |
13 | */ |
14 | |
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
16 | |
17 | #include <linux/kernel.h> |
18 | #include <linux/types.h> |
19 | #include <linux/module.h> |
20 | #include <linux/errno.h> |
21 | #include <linux/slab.h> |
22 | #include <linux/skbuff.h> |
23 | #include <linux/rculist.h> |
24 | #include <linux/netdevice.h> |
25 | #include <linux/in.h> |
26 | #include <linux/ip.h> |
27 | #include <linux/udp.h> |
28 | #include <linux/igmp.h> |
29 | #include <linux/etherdevice.h> |
30 | #include <linux/if_ether.h> |
d342894c |
31 | #include <linux/hash.h> |
32 | #include <net/ip.h> |
33 | #include <net/icmp.h> |
34 | #include <net/udp.h> |
35 | #include <net/rtnetlink.h> |
36 | #include <net/route.h> |
37 | #include <net/dsfield.h> |
38 | #include <net/inet_ecn.h> |
39 | #include <net/net_namespace.h> |
40 | #include <net/netns/generic.h> |
41 | |
42 | #define VXLAN_VERSION "0.1" |
43 | |
44 | #define VNI_HASH_BITS 10 |
45 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
46 | #define FDB_HASH_BITS 8 |
47 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) |
48 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
49 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
50 | |
51 | #define VXLAN_N_VID (1u << 24) |
52 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) |
53 | /* VLAN + IP header + UDP + VXLAN */ |
54 | #define VXLAN_HEADROOM (4 + 20 + 8 + 8) |
55 | |
56 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ |
57 | |
58 | /* VXLAN protocol header */ |
59 | struct vxlanhdr { |
60 | __be32 vx_flags; |
61 | __be32 vx_vni; |
62 | }; |
63 | |
64 | /* UDP port for VXLAN traffic. */ |
65 | static unsigned int vxlan_port __read_mostly = 8472; |
66 | module_param_named(udp_port, vxlan_port, uint, 0444); |
67 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
68 | |
69 | static bool log_ecn_error = true; |
70 | module_param(log_ecn_error, bool, 0644); |
71 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
72 | |
73 | /* per-net private data for this module */ |
74 | static unsigned int vxlan_net_id; |
75 | struct vxlan_net { |
76 | struct socket *sock; /* UDP encap socket */ |
77 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
78 | }; |
79 | |
80 | /* Forwarding table entry */ |
81 | struct vxlan_fdb { |
82 | struct hlist_node hlist; /* linked list of entries */ |
83 | struct rcu_head rcu; |
84 | unsigned long updated; /* jiffies */ |
85 | unsigned long used; |
86 | __be32 remote_ip; |
87 | u16 state; /* see ndm_state */ |
88 | u8 eth_addr[ETH_ALEN]; |
89 | }; |
90 | |
91 | /* Per-cpu network traffic stats */ |
92 | struct vxlan_stats { |
93 | u64 rx_packets; |
94 | u64 rx_bytes; |
95 | u64 tx_packets; |
96 | u64 tx_bytes; |
97 | struct u64_stats_sync syncp; |
98 | }; |
99 | |
100 | /* Pseudo network device */ |
101 | struct vxlan_dev { |
102 | struct hlist_node hlist; |
103 | struct net_device *dev; |
104 | struct vxlan_stats __percpu *stats; |
105 | __u32 vni; /* virtual network id */ |
106 | __be32 gaddr; /* multicast group */ |
107 | __be32 saddr; /* source address */ |
108 | unsigned int link; /* link to multicast over */ |
05f47d69 |
109 | __u16 port_min; /* source port range */ |
110 | __u16 port_max; |
d342894c |
111 | __u8 tos; /* TOS override */ |
112 | __u8 ttl; |
113 | bool learn; |
114 | |
115 | unsigned long age_interval; |
116 | struct timer_list age_timer; |
117 | spinlock_t hash_lock; |
118 | unsigned int addrcnt; |
119 | unsigned int addrmax; |
120 | unsigned int addrexceeded; |
121 | |
122 | struct hlist_head fdb_head[FDB_HASH_SIZE]; |
123 | }; |
124 | |
125 | /* salt for hash table */ |
126 | static u32 vxlan_salt __read_mostly; |
127 | |
128 | static inline struct hlist_head *vni_head(struct net *net, u32 id) |
129 | { |
130 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
131 | |
132 | return &vn->vni_list[hash_32(id, VNI_HASH_BITS)]; |
133 | } |
134 | |
135 | /* Look up VNI in a per net namespace table */ |
136 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id) |
137 | { |
138 | struct vxlan_dev *vxlan; |
139 | struct hlist_node *node; |
140 | |
141 | hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) { |
142 | if (vxlan->vni == id) |
143 | return vxlan; |
144 | } |
145 | |
146 | return NULL; |
147 | } |
148 | |
149 | /* Fill in neighbour message in skbuff. */ |
150 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, |
151 | const struct vxlan_fdb *fdb, |
152 | u32 portid, u32 seq, int type, unsigned int flags) |
153 | { |
154 | unsigned long now = jiffies; |
155 | struct nda_cacheinfo ci; |
156 | struct nlmsghdr *nlh; |
157 | struct ndmsg *ndm; |
158 | |
159 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
160 | if (nlh == NULL) |
161 | return -EMSGSIZE; |
162 | |
163 | ndm = nlmsg_data(nlh); |
164 | memset(ndm, 0, sizeof(*ndm)); |
165 | ndm->ndm_family = AF_BRIDGE; |
166 | ndm->ndm_state = fdb->state; |
167 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
168 | ndm->ndm_flags = NTF_SELF; |
169 | ndm->ndm_type = NDA_DST; |
170 | |
171 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
172 | goto nla_put_failure; |
173 | |
174 | if (nla_put_be32(skb, NDA_DST, fdb->remote_ip)) |
175 | goto nla_put_failure; |
176 | |
177 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
178 | ci.ndm_confirmed = 0; |
179 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
180 | ci.ndm_refcnt = 0; |
181 | |
182 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
183 | goto nla_put_failure; |
184 | |
185 | return nlmsg_end(skb, nlh); |
186 | |
187 | nla_put_failure: |
188 | nlmsg_cancel(skb, nlh); |
189 | return -EMSGSIZE; |
190 | } |
191 | |
192 | static inline size_t vxlan_nlmsg_size(void) |
193 | { |
194 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
195 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
196 | + nla_total_size(sizeof(__be32)) /* NDA_DST */ |
197 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
198 | } |
199 | |
200 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, |
201 | const struct vxlan_fdb *fdb, int type) |
202 | { |
203 | struct net *net = dev_net(vxlan->dev); |
204 | struct sk_buff *skb; |
205 | int err = -ENOBUFS; |
206 | |
207 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
208 | if (skb == NULL) |
209 | goto errout; |
210 | |
211 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0); |
212 | if (err < 0) { |
213 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
214 | WARN_ON(err == -EMSGSIZE); |
215 | kfree_skb(skb); |
216 | goto errout; |
217 | } |
218 | |
219 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
220 | return; |
221 | errout: |
222 | if (err < 0) |
223 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
224 | } |
225 | |
226 | /* Hash Ethernet address */ |
227 | static u32 eth_hash(const unsigned char *addr) |
228 | { |
229 | u64 value = get_unaligned((u64 *)addr); |
230 | |
231 | /* only want 6 bytes */ |
232 | #ifdef __BIG_ENDIAN |
d342894c |
233 | value >>= 16; |
321fb991 |
234 | #else |
235 | value <<= 16; |
d342894c |
236 | #endif |
237 | return hash_64(value, FDB_HASH_BITS); |
238 | } |
239 | |
240 | /* Hash chain to use given mac address */ |
241 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
242 | const u8 *mac) |
243 | { |
244 | return &vxlan->fdb_head[eth_hash(mac)]; |
245 | } |
246 | |
247 | /* Look up Ethernet address in forwarding table */ |
248 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
249 | const u8 *mac) |
250 | |
251 | { |
252 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); |
253 | struct vxlan_fdb *f; |
254 | struct hlist_node *node; |
255 | |
256 | hlist_for_each_entry_rcu(f, node, head, hlist) { |
257 | if (compare_ether_addr(mac, f->eth_addr) == 0) |
258 | return f; |
259 | } |
260 | |
261 | return NULL; |
262 | } |
263 | |
264 | /* Add new entry to forwarding table -- assumes lock held */ |
265 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
266 | const u8 *mac, __be32 ip, |
267 | __u16 state, __u16 flags) |
268 | { |
269 | struct vxlan_fdb *f; |
270 | int notify = 0; |
271 | |
272 | f = vxlan_find_mac(vxlan, mac); |
273 | if (f) { |
274 | if (flags & NLM_F_EXCL) { |
275 | netdev_dbg(vxlan->dev, |
276 | "lost race to create %pM\n", mac); |
277 | return -EEXIST; |
278 | } |
279 | if (f->state != state) { |
280 | f->state = state; |
281 | f->updated = jiffies; |
282 | notify = 1; |
283 | } |
284 | } else { |
285 | if (!(flags & NLM_F_CREATE)) |
286 | return -ENOENT; |
287 | |
288 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) |
289 | return -ENOSPC; |
290 | |
291 | netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip); |
292 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
293 | if (!f) |
294 | return -ENOMEM; |
295 | |
296 | notify = 1; |
297 | f->remote_ip = ip; |
298 | f->state = state; |
299 | f->updated = f->used = jiffies; |
300 | memcpy(f->eth_addr, mac, ETH_ALEN); |
301 | |
302 | ++vxlan->addrcnt; |
303 | hlist_add_head_rcu(&f->hlist, |
304 | vxlan_fdb_head(vxlan, mac)); |
305 | } |
306 | |
307 | if (notify) |
308 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
309 | |
310 | return 0; |
311 | } |
312 | |
313 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
314 | { |
315 | netdev_dbg(vxlan->dev, |
316 | "delete %pM\n", f->eth_addr); |
317 | |
318 | --vxlan->addrcnt; |
319 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); |
320 | |
321 | hlist_del_rcu(&f->hlist); |
322 | kfree_rcu(f, rcu); |
323 | } |
324 | |
325 | /* Add static entry (via netlink) */ |
326 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
327 | struct net_device *dev, |
328 | const unsigned char *addr, u16 flags) |
329 | { |
330 | struct vxlan_dev *vxlan = netdev_priv(dev); |
331 | __be32 ip; |
332 | int err; |
333 | |
334 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
335 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
336 | ndm->ndm_state); |
337 | return -EINVAL; |
338 | } |
339 | |
340 | if (tb[NDA_DST] == NULL) |
341 | return -EINVAL; |
342 | |
343 | if (nla_len(tb[NDA_DST]) != sizeof(__be32)) |
344 | return -EAFNOSUPPORT; |
345 | |
346 | ip = nla_get_be32(tb[NDA_DST]); |
347 | |
348 | spin_lock_bh(&vxlan->hash_lock); |
349 | err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags); |
350 | spin_unlock_bh(&vxlan->hash_lock); |
351 | |
352 | return err; |
353 | } |
354 | |
355 | /* Delete entry (via netlink) */ |
356 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev, |
357 | const unsigned char *addr) |
358 | { |
359 | struct vxlan_dev *vxlan = netdev_priv(dev); |
360 | struct vxlan_fdb *f; |
361 | int err = -ENOENT; |
362 | |
363 | spin_lock_bh(&vxlan->hash_lock); |
364 | f = vxlan_find_mac(vxlan, addr); |
365 | if (f) { |
366 | vxlan_fdb_destroy(vxlan, f); |
367 | err = 0; |
368 | } |
369 | spin_unlock_bh(&vxlan->hash_lock); |
370 | |
371 | return err; |
372 | } |
373 | |
374 | /* Dump forwarding table */ |
375 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
376 | struct net_device *dev, int idx) |
377 | { |
378 | struct vxlan_dev *vxlan = netdev_priv(dev); |
379 | unsigned int h; |
380 | |
381 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
382 | struct vxlan_fdb *f; |
383 | struct hlist_node *n; |
384 | int err; |
385 | |
386 | hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) { |
387 | if (idx < cb->args[0]) |
388 | goto skip; |
389 | |
390 | err = vxlan_fdb_info(skb, vxlan, f, |
391 | NETLINK_CB(cb->skb).portid, |
392 | cb->nlh->nlmsg_seq, |
393 | RTM_NEWNEIGH, |
394 | NLM_F_MULTI); |
395 | if (err < 0) |
396 | break; |
397 | skip: |
398 | ++idx; |
399 | } |
400 | } |
401 | |
402 | return idx; |
403 | } |
404 | |
405 | /* Watch incoming packets to learn mapping between Ethernet address |
406 | * and Tunnel endpoint. |
407 | */ |
408 | static void vxlan_snoop(struct net_device *dev, |
409 | __be32 src_ip, const u8 *src_mac) |
410 | { |
411 | struct vxlan_dev *vxlan = netdev_priv(dev); |
412 | struct vxlan_fdb *f; |
413 | int err; |
414 | |
415 | f = vxlan_find_mac(vxlan, src_mac); |
416 | if (likely(f)) { |
417 | f->used = jiffies; |
418 | if (likely(f->remote_ip == src_ip)) |
419 | return; |
420 | |
421 | if (net_ratelimit()) |
422 | netdev_info(dev, |
423 | "%pM migrated from %pI4 to %pI4\n", |
424 | src_mac, &f->remote_ip, &src_ip); |
425 | |
426 | f->remote_ip = src_ip; |
427 | f->updated = jiffies; |
428 | } else { |
429 | /* learned new entry */ |
430 | spin_lock(&vxlan->hash_lock); |
431 | err = vxlan_fdb_create(vxlan, src_mac, src_ip, |
432 | NUD_REACHABLE, |
433 | NLM_F_EXCL|NLM_F_CREATE); |
434 | spin_unlock(&vxlan->hash_lock); |
435 | } |
436 | } |
437 | |
438 | |
439 | /* See if multicast group is already in use by other ID */ |
440 | static bool vxlan_group_used(struct vxlan_net *vn, |
441 | const struct vxlan_dev *this) |
442 | { |
443 | const struct vxlan_dev *vxlan; |
444 | struct hlist_node *node; |
445 | unsigned h; |
446 | |
447 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
448 | hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) { |
449 | if (vxlan == this) |
450 | continue; |
451 | |
452 | if (!netif_running(vxlan->dev)) |
453 | continue; |
454 | |
455 | if (vxlan->gaddr == this->gaddr) |
456 | return true; |
457 | } |
458 | |
459 | return false; |
460 | } |
461 | |
462 | /* kernel equivalent to IP_ADD_MEMBERSHIP */ |
463 | static int vxlan_join_group(struct net_device *dev) |
464 | { |
465 | struct vxlan_dev *vxlan = netdev_priv(dev); |
466 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
467 | struct sock *sk = vn->sock->sk; |
468 | struct ip_mreqn mreq = { |
469 | .imr_multiaddr.s_addr = vxlan->gaddr, |
470 | }; |
471 | int err; |
472 | |
473 | /* Already a member of group */ |
474 | if (vxlan_group_used(vn, vxlan)) |
475 | return 0; |
476 | |
477 | /* Need to drop RTNL to call multicast join */ |
478 | rtnl_unlock(); |
479 | lock_sock(sk); |
480 | err = ip_mc_join_group(sk, &mreq); |
481 | release_sock(sk); |
482 | rtnl_lock(); |
483 | |
484 | return err; |
485 | } |
486 | |
487 | |
488 | /* kernel equivalent to IP_DROP_MEMBERSHIP */ |
489 | static int vxlan_leave_group(struct net_device *dev) |
490 | { |
491 | struct vxlan_dev *vxlan = netdev_priv(dev); |
492 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
493 | int err = 0; |
494 | struct sock *sk = vn->sock->sk; |
495 | struct ip_mreqn mreq = { |
496 | .imr_multiaddr.s_addr = vxlan->gaddr, |
497 | }; |
498 | |
499 | /* Only leave group when last vxlan is done. */ |
500 | if (vxlan_group_used(vn, vxlan)) |
501 | return 0; |
502 | |
503 | /* Need to drop RTNL to call multicast leave */ |
504 | rtnl_unlock(); |
505 | lock_sock(sk); |
506 | err = ip_mc_leave_group(sk, &mreq); |
507 | release_sock(sk); |
508 | rtnl_lock(); |
509 | |
510 | return err; |
511 | } |
512 | |
513 | /* Callback from net/ipv4/udp.c to receive packets */ |
514 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
515 | { |
516 | struct iphdr *oip; |
517 | struct vxlanhdr *vxh; |
518 | struct vxlan_dev *vxlan; |
519 | struct vxlan_stats *stats; |
520 | __u32 vni; |
521 | int err; |
522 | |
523 | /* pop off outer UDP header */ |
524 | __skb_pull(skb, sizeof(struct udphdr)); |
525 | |
526 | /* Need Vxlan and inner Ethernet header to be present */ |
527 | if (!pskb_may_pull(skb, sizeof(struct vxlanhdr))) |
528 | goto error; |
529 | |
530 | /* Drop packets with reserved bits set */ |
531 | vxh = (struct vxlanhdr *) skb->data; |
532 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || |
533 | (vxh->vx_vni & htonl(0xff))) { |
534 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
535 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); |
536 | goto error; |
537 | } |
538 | |
539 | __skb_pull(skb, sizeof(struct vxlanhdr)); |
540 | skb_postpull_rcsum(skb, eth_hdr(skb), sizeof(struct vxlanhdr)); |
541 | |
542 | /* Is this VNI defined? */ |
543 | vni = ntohl(vxh->vx_vni) >> 8; |
544 | vxlan = vxlan_find_vni(sock_net(sk), vni); |
545 | if (!vxlan) { |
546 | netdev_dbg(skb->dev, "unknown vni %d\n", vni); |
547 | goto drop; |
548 | } |
549 | |
550 | if (!pskb_may_pull(skb, ETH_HLEN)) { |
551 | vxlan->dev->stats.rx_length_errors++; |
552 | vxlan->dev->stats.rx_errors++; |
553 | goto drop; |
554 | } |
555 | |
556 | /* Re-examine inner Ethernet packet */ |
557 | oip = ip_hdr(skb); |
558 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
559 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
560 | |
561 | /* Ignore packet loops (and multicast echo) */ |
562 | if (compare_ether_addr(eth_hdr(skb)->h_source, |
563 | vxlan->dev->dev_addr) == 0) |
564 | goto drop; |
565 | |
566 | if (vxlan->learn) |
567 | vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source); |
568 | |
569 | __skb_tunnel_rx(skb, vxlan->dev); |
570 | skb_reset_network_header(skb); |
571 | |
572 | err = IP_ECN_decapsulate(oip, skb); |
573 | if (unlikely(err)) { |
574 | if (log_ecn_error) |
575 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
576 | &oip->saddr, oip->tos); |
577 | if (err > 1) { |
578 | ++vxlan->dev->stats.rx_frame_errors; |
579 | ++vxlan->dev->stats.rx_errors; |
580 | goto drop; |
581 | } |
582 | } |
583 | |
584 | stats = this_cpu_ptr(vxlan->stats); |
585 | u64_stats_update_begin(&stats->syncp); |
586 | stats->rx_packets++; |
587 | stats->rx_bytes += skb->len; |
588 | u64_stats_update_end(&stats->syncp); |
589 | |
590 | netif_rx(skb); |
591 | |
592 | return 0; |
593 | error: |
594 | /* Put UDP header back */ |
595 | __skb_push(skb, sizeof(struct udphdr)); |
596 | |
597 | return 1; |
598 | drop: |
599 | /* Consume bad packet */ |
600 | kfree_skb(skb); |
601 | return 0; |
602 | } |
603 | |
604 | /* Extract dsfield from inner protocol */ |
605 | static inline u8 vxlan_get_dsfield(const struct iphdr *iph, |
606 | const struct sk_buff *skb) |
607 | { |
608 | if (skb->protocol == htons(ETH_P_IP)) |
609 | return iph->tos; |
610 | else if (skb->protocol == htons(ETH_P_IPV6)) |
611 | return ipv6_get_dsfield((const struct ipv6hdr *)iph); |
612 | else |
613 | return 0; |
614 | } |
615 | |
616 | /* Propogate ECN bits out */ |
617 | static inline u8 vxlan_ecn_encap(u8 tos, |
618 | const struct iphdr *iph, |
619 | const struct sk_buff *skb) |
620 | { |
621 | u8 inner = vxlan_get_dsfield(iph, skb); |
622 | |
623 | return INET_ECN_encapsulate(tos, inner); |
624 | } |
625 | |
ef59febe |
626 | static __be32 vxlan_find_dst(struct vxlan_dev *vxlan, struct sk_buff *skb) |
627 | { |
628 | const struct ethhdr *eth = (struct ethhdr *) skb->data; |
629 | const struct vxlan_fdb *f; |
630 | |
631 | if (is_multicast_ether_addr(eth->h_dest)) |
632 | return vxlan->gaddr; |
633 | |
634 | f = vxlan_find_mac(vxlan, eth->h_dest); |
635 | if (f) |
636 | return f->remote_ip; |
637 | else |
638 | return vxlan->gaddr; |
639 | |
640 | } |
641 | |
1cad8715 |
642 | static void vxlan_sock_free(struct sk_buff *skb) |
643 | { |
644 | sock_put(skb->sk); |
645 | } |
646 | |
647 | /* On transmit, associate with the tunnel socket */ |
648 | static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb) |
649 | { |
650 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
651 | struct sock *sk = vn->sock->sk; |
652 | |
653 | skb_orphan(skb); |
654 | sock_hold(sk); |
655 | skb->sk = sk; |
656 | skb->destructor = vxlan_sock_free; |
657 | } |
658 | |
05f47d69 |
659 | /* Compute source port for outgoing packet |
660 | * first choice to use L4 flow hash since it will spread |
661 | * better and maybe available from hardware |
662 | * secondary choice is to use jhash on the Ethernet header |
663 | */ |
664 | static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb) |
665 | { |
666 | unsigned int range = (vxlan->port_max - vxlan->port_min) + 1; |
667 | u32 hash; |
668 | |
669 | hash = skb_get_rxhash(skb); |
670 | if (!hash) |
671 | hash = jhash(skb->data, 2 * ETH_ALEN, |
672 | (__force u32) skb->protocol); |
673 | |
674 | return (((u64) hash * range) >> 32) + vxlan->port_min; |
675 | } |
676 | |
d342894c |
677 | /* Transmit local packets over Vxlan |
678 | * |
679 | * Outer IP header inherits ECN and DF from inner header. |
680 | * Outer UDP destination is the VXLAN assigned port. |
05f47d69 |
681 | * source port is based on hash of flow |
d342894c |
682 | */ |
683 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
684 | { |
685 | struct vxlan_dev *vxlan = netdev_priv(dev); |
686 | struct rtable *rt; |
d342894c |
687 | const struct iphdr *old_iph; |
688 | struct iphdr *iph; |
689 | struct vxlanhdr *vxh; |
690 | struct udphdr *uh; |
691 | struct flowi4 fl4; |
d342894c |
692 | unsigned int pkt_len = skb->len; |
d342894c |
693 | __be32 dst; |
05f47d69 |
694 | __u16 src_port; |
d342894c |
695 | __be16 df = 0; |
696 | __u8 tos, ttl; |
697 | int err; |
698 | |
ef59febe |
699 | dst = vxlan_find_dst(vxlan, skb); |
700 | if (!dst) |
701 | goto drop; |
702 | |
d342894c |
703 | /* Need space for new headers (invalidates iph ptr) */ |
704 | if (skb_cow_head(skb, VXLAN_HEADROOM)) |
705 | goto drop; |
706 | |
d342894c |
707 | old_iph = ip_hdr(skb); |
708 | |
d342894c |
709 | ttl = vxlan->ttl; |
710 | if (!ttl && IN_MULTICAST(ntohl(dst))) |
711 | ttl = 1; |
712 | |
713 | tos = vxlan->tos; |
714 | if (tos == 1) |
715 | tos = vxlan_get_dsfield(old_iph, skb); |
716 | |
05f47d69 |
717 | src_port = vxlan_src_port(vxlan, skb); |
d342894c |
718 | |
ca78f181 |
719 | memset(&fl4, 0, sizeof(fl4)); |
720 | fl4.flowi4_oif = vxlan->link; |
721 | fl4.flowi4_tos = RT_TOS(tos); |
722 | fl4.daddr = dst; |
723 | fl4.saddr = vxlan->saddr; |
724 | |
725 | rt = ip_route_output_key(dev_net(dev), &fl4); |
d342894c |
726 | if (IS_ERR(rt)) { |
727 | netdev_dbg(dev, "no route to %pI4\n", &dst); |
728 | dev->stats.tx_carrier_errors++; |
729 | goto tx_error; |
730 | } |
731 | |
732 | if (rt->dst.dev == dev) { |
733 | netdev_dbg(dev, "circular route to %pI4\n", &dst); |
734 | ip_rt_put(rt); |
735 | dev->stats.collisions++; |
736 | goto tx_error; |
737 | } |
738 | |
739 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
740 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | |
741 | IPSKB_REROUTED); |
742 | skb_dst_drop(skb); |
743 | skb_dst_set(skb, &rt->dst); |
744 | |
745 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
746 | vxh->vx_flags = htonl(VXLAN_FLAGS); |
747 | vxh->vx_vni = htonl(vxlan->vni << 8); |
748 | |
749 | __skb_push(skb, sizeof(*uh)); |
750 | skb_reset_transport_header(skb); |
751 | uh = udp_hdr(skb); |
752 | |
753 | uh->dest = htons(vxlan_port); |
05f47d69 |
754 | uh->source = htons(src_port); |
d342894c |
755 | |
756 | uh->len = htons(skb->len); |
757 | uh->check = 0; |
758 | |
759 | __skb_push(skb, sizeof(*iph)); |
760 | skb_reset_network_header(skb); |
761 | iph = ip_hdr(skb); |
762 | iph->version = 4; |
763 | iph->ihl = sizeof(struct iphdr) >> 2; |
764 | iph->frag_off = df; |
765 | iph->protocol = IPPROTO_UDP; |
766 | iph->tos = vxlan_ecn_encap(tos, old_iph, skb); |
ca78f181 |
767 | iph->daddr = dst; |
d342894c |
768 | iph->saddr = fl4.saddr; |
769 | iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
770 | |
1cad8715 |
771 | vxlan_set_owner(dev, skb); |
772 | |
d342894c |
773 | /* See __IPTUNNEL_XMIT */ |
774 | skb->ip_summed = CHECKSUM_NONE; |
775 | ip_select_ident(iph, &rt->dst, NULL); |
776 | |
777 | err = ip_local_out(skb); |
778 | if (likely(net_xmit_eval(err) == 0)) { |
779 | struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats); |
780 | |
781 | u64_stats_update_begin(&stats->syncp); |
782 | stats->tx_packets++; |
783 | stats->tx_bytes += pkt_len; |
784 | u64_stats_update_end(&stats->syncp); |
785 | } else { |
786 | dev->stats.tx_errors++; |
787 | dev->stats.tx_aborted_errors++; |
788 | } |
789 | return NETDEV_TX_OK; |
790 | |
791 | drop: |
792 | dev->stats.tx_dropped++; |
793 | goto tx_free; |
794 | |
795 | tx_error: |
796 | dev->stats.tx_errors++; |
797 | tx_free: |
798 | dev_kfree_skb(skb); |
799 | return NETDEV_TX_OK; |
800 | } |
801 | |
802 | /* Walk the forwarding table and purge stale entries */ |
803 | static void vxlan_cleanup(unsigned long arg) |
804 | { |
805 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; |
806 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
807 | unsigned int h; |
808 | |
809 | if (!netif_running(vxlan->dev)) |
810 | return; |
811 | |
812 | spin_lock_bh(&vxlan->hash_lock); |
813 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
814 | struct hlist_node *p, *n; |
815 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
816 | struct vxlan_fdb *f |
817 | = container_of(p, struct vxlan_fdb, hlist); |
818 | unsigned long timeout; |
819 | |
820 | if (f->state == NUD_PERMANENT) |
821 | continue; |
822 | |
823 | timeout = f->used + vxlan->age_interval * HZ; |
824 | if (time_before_eq(timeout, jiffies)) { |
825 | netdev_dbg(vxlan->dev, |
826 | "garbage collect %pM\n", |
827 | f->eth_addr); |
828 | f->state = NUD_STALE; |
829 | vxlan_fdb_destroy(vxlan, f); |
830 | } else if (time_before(timeout, next_timer)) |
831 | next_timer = timeout; |
832 | } |
833 | } |
834 | spin_unlock_bh(&vxlan->hash_lock); |
835 | |
836 | mod_timer(&vxlan->age_timer, next_timer); |
837 | } |
838 | |
839 | /* Setup stats when device is created */ |
840 | static int vxlan_init(struct net_device *dev) |
841 | { |
842 | struct vxlan_dev *vxlan = netdev_priv(dev); |
843 | |
844 | vxlan->stats = alloc_percpu(struct vxlan_stats); |
845 | if (!vxlan->stats) |
846 | return -ENOMEM; |
847 | |
848 | return 0; |
849 | } |
850 | |
851 | /* Start ageing timer and join group when device is brought up */ |
852 | static int vxlan_open(struct net_device *dev) |
853 | { |
854 | struct vxlan_dev *vxlan = netdev_priv(dev); |
855 | int err; |
856 | |
857 | if (vxlan->gaddr) { |
858 | err = vxlan_join_group(dev); |
859 | if (err) |
860 | return err; |
861 | } |
862 | |
863 | if (vxlan->age_interval) |
864 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
865 | |
866 | return 0; |
867 | } |
868 | |
869 | /* Purge the forwarding table */ |
870 | static void vxlan_flush(struct vxlan_dev *vxlan) |
871 | { |
872 | unsigned h; |
873 | |
874 | spin_lock_bh(&vxlan->hash_lock); |
875 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
876 | struct hlist_node *p, *n; |
877 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
878 | struct vxlan_fdb *f |
879 | = container_of(p, struct vxlan_fdb, hlist); |
880 | vxlan_fdb_destroy(vxlan, f); |
881 | } |
882 | } |
883 | spin_unlock_bh(&vxlan->hash_lock); |
884 | } |
885 | |
886 | /* Cleanup timer and forwarding table on shutdown */ |
887 | static int vxlan_stop(struct net_device *dev) |
888 | { |
889 | struct vxlan_dev *vxlan = netdev_priv(dev); |
890 | |
891 | if (vxlan->gaddr) |
892 | vxlan_leave_group(dev); |
893 | |
894 | del_timer_sync(&vxlan->age_timer); |
895 | |
896 | vxlan_flush(vxlan); |
897 | |
898 | return 0; |
899 | } |
900 | |
901 | /* Merge per-cpu statistics */ |
902 | static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev, |
903 | struct rtnl_link_stats64 *stats) |
904 | { |
905 | struct vxlan_dev *vxlan = netdev_priv(dev); |
906 | struct vxlan_stats tmp, sum = { 0 }; |
907 | unsigned int cpu; |
908 | |
909 | for_each_possible_cpu(cpu) { |
910 | unsigned int start; |
911 | const struct vxlan_stats *stats |
912 | = per_cpu_ptr(vxlan->stats, cpu); |
913 | |
914 | do { |
915 | start = u64_stats_fetch_begin_bh(&stats->syncp); |
916 | memcpy(&tmp, stats, sizeof(tmp)); |
917 | } while (u64_stats_fetch_retry_bh(&stats->syncp, start)); |
918 | |
919 | sum.tx_bytes += tmp.tx_bytes; |
920 | sum.tx_packets += tmp.tx_packets; |
921 | sum.rx_bytes += tmp.rx_bytes; |
922 | sum.rx_packets += tmp.rx_packets; |
923 | } |
924 | |
925 | stats->tx_bytes = sum.tx_bytes; |
926 | stats->tx_packets = sum.tx_packets; |
927 | stats->rx_bytes = sum.rx_bytes; |
928 | stats->rx_packets = sum.rx_packets; |
929 | |
930 | stats->multicast = dev->stats.multicast; |
931 | stats->rx_length_errors = dev->stats.rx_length_errors; |
932 | stats->rx_frame_errors = dev->stats.rx_frame_errors; |
933 | stats->rx_errors = dev->stats.rx_errors; |
934 | |
935 | stats->tx_dropped = dev->stats.tx_dropped; |
936 | stats->tx_carrier_errors = dev->stats.tx_carrier_errors; |
937 | stats->tx_aborted_errors = dev->stats.tx_aborted_errors; |
938 | stats->collisions = dev->stats.collisions; |
939 | stats->tx_errors = dev->stats.tx_errors; |
940 | |
941 | return stats; |
942 | } |
943 | |
944 | /* Stub, nothing needs to be done. */ |
945 | static void vxlan_set_multicast_list(struct net_device *dev) |
946 | { |
947 | } |
948 | |
949 | static const struct net_device_ops vxlan_netdev_ops = { |
950 | .ndo_init = vxlan_init, |
951 | .ndo_open = vxlan_open, |
952 | .ndo_stop = vxlan_stop, |
953 | .ndo_start_xmit = vxlan_xmit, |
954 | .ndo_get_stats64 = vxlan_stats64, |
955 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
956 | .ndo_change_mtu = eth_change_mtu, |
957 | .ndo_validate_addr = eth_validate_addr, |
958 | .ndo_set_mac_address = eth_mac_addr, |
959 | .ndo_fdb_add = vxlan_fdb_add, |
960 | .ndo_fdb_del = vxlan_fdb_delete, |
961 | .ndo_fdb_dump = vxlan_fdb_dump, |
962 | }; |
963 | |
964 | /* Info for udev, that this is a virtual tunnel endpoint */ |
965 | static struct device_type vxlan_type = { |
966 | .name = "vxlan", |
967 | }; |
968 | |
969 | static void vxlan_free(struct net_device *dev) |
970 | { |
971 | struct vxlan_dev *vxlan = netdev_priv(dev); |
972 | |
973 | free_percpu(vxlan->stats); |
974 | free_netdev(dev); |
975 | } |
976 | |
977 | /* Initialize the device structure. */ |
978 | static void vxlan_setup(struct net_device *dev) |
979 | { |
980 | struct vxlan_dev *vxlan = netdev_priv(dev); |
981 | unsigned h; |
05f47d69 |
982 | int low, high; |
d342894c |
983 | |
984 | eth_hw_addr_random(dev); |
985 | ether_setup(dev); |
2840bf22 |
986 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; |
d342894c |
987 | |
988 | dev->netdev_ops = &vxlan_netdev_ops; |
989 | dev->destructor = vxlan_free; |
990 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
991 | |
992 | dev->tx_queue_len = 0; |
993 | dev->features |= NETIF_F_LLTX; |
994 | dev->features |= NETIF_F_NETNS_LOCAL; |
995 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
996 | |
997 | spin_lock_init(&vxlan->hash_lock); |
998 | |
999 | init_timer_deferrable(&vxlan->age_timer); |
1000 | vxlan->age_timer.function = vxlan_cleanup; |
1001 | vxlan->age_timer.data = (unsigned long) vxlan; |
1002 | |
05f47d69 |
1003 | inet_get_local_port_range(&low, &high); |
1004 | vxlan->port_min = low; |
1005 | vxlan->port_max = high; |
1006 | |
d342894c |
1007 | vxlan->dev = dev; |
1008 | |
1009 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
1010 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
1011 | } |
1012 | |
1013 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
1014 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
1015 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
1016 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
1017 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
1018 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
1019 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
1020 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
1021 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
1022 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
05f47d69 |
1023 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
d342894c |
1024 | }; |
1025 | |
1026 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
1027 | { |
1028 | if (tb[IFLA_ADDRESS]) { |
1029 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
1030 | pr_debug("invalid link address (not ethernet)\n"); |
1031 | return -EINVAL; |
1032 | } |
1033 | |
1034 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
1035 | pr_debug("invalid all zero ethernet address\n"); |
1036 | return -EADDRNOTAVAIL; |
1037 | } |
1038 | } |
1039 | |
1040 | if (!data) |
1041 | return -EINVAL; |
1042 | |
1043 | if (data[IFLA_VXLAN_ID]) { |
1044 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
1045 | if (id >= VXLAN_VID_MASK) |
1046 | return -ERANGE; |
1047 | } |
1048 | |
1049 | if (data[IFLA_VXLAN_GROUP]) { |
1050 | __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
1051 | if (!IN_MULTICAST(ntohl(gaddr))) { |
1052 | pr_debug("group address is not IPv4 multicast\n"); |
1053 | return -EADDRNOTAVAIL; |
1054 | } |
1055 | } |
05f47d69 |
1056 | |
1057 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
1058 | const struct ifla_vxlan_port_range *p |
1059 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
1060 | |
1061 | if (ntohs(p->high) < ntohs(p->low)) { |
1062 | pr_debug("port range %u .. %u not valid\n", |
1063 | ntohs(p->low), ntohs(p->high)); |
1064 | return -EINVAL; |
1065 | } |
1066 | } |
1067 | |
d342894c |
1068 | return 0; |
1069 | } |
1070 | |
1071 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
1072 | struct nlattr *tb[], struct nlattr *data[]) |
1073 | { |
1074 | struct vxlan_dev *vxlan = netdev_priv(dev); |
1075 | __u32 vni; |
1076 | int err; |
1077 | |
1078 | if (!data[IFLA_VXLAN_ID]) |
1079 | return -EINVAL; |
1080 | |
1081 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
1082 | if (vxlan_find_vni(net, vni)) { |
1083 | pr_info("duplicate VNI %u\n", vni); |
1084 | return -EEXIST; |
1085 | } |
1086 | vxlan->vni = vni; |
1087 | |
1088 | if (data[IFLA_VXLAN_GROUP]) |
1089 | vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
1090 | |
1091 | if (data[IFLA_VXLAN_LOCAL]) |
1092 | vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); |
1093 | |
1094 | if (data[IFLA_VXLAN_LINK]) { |
1095 | vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]); |
1096 | |
1097 | if (!tb[IFLA_MTU]) { |
1098 | struct net_device *lowerdev; |
1099 | lowerdev = __dev_get_by_index(net, vxlan->link); |
1100 | dev->mtu = lowerdev->mtu - VXLAN_HEADROOM; |
1101 | } |
1102 | } |
1103 | |
1104 | if (data[IFLA_VXLAN_TOS]) |
1105 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
1106 | |
1107 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
1108 | vxlan->learn = true; |
1109 | |
1110 | if (data[IFLA_VXLAN_AGEING]) |
1111 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
1112 | else |
1113 | vxlan->age_interval = FDB_AGE_DEFAULT; |
1114 | |
1115 | if (data[IFLA_VXLAN_LIMIT]) |
1116 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
1117 | |
05f47d69 |
1118 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
1119 | const struct ifla_vxlan_port_range *p |
1120 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
1121 | vxlan->port_min = ntohs(p->low); |
1122 | vxlan->port_max = ntohs(p->high); |
1123 | } |
1124 | |
d342894c |
1125 | err = register_netdevice(dev); |
1126 | if (!err) |
1127 | hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni)); |
1128 | |
1129 | return err; |
1130 | } |
1131 | |
1132 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
1133 | { |
1134 | struct vxlan_dev *vxlan = netdev_priv(dev); |
1135 | |
1136 | hlist_del_rcu(&vxlan->hlist); |
1137 | |
1138 | unregister_netdevice_queue(dev, head); |
1139 | } |
1140 | |
1141 | static size_t vxlan_get_size(const struct net_device *dev) |
1142 | { |
1143 | |
1144 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
1145 | nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */ |
1146 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
1147 | nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */ |
1148 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
1149 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
1150 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
1151 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
1152 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
05f47d69 |
1153 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
d342894c |
1154 | 0; |
1155 | } |
1156 | |
1157 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
1158 | { |
1159 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
05f47d69 |
1160 | struct ifla_vxlan_port_range ports = { |
1161 | .low = htons(vxlan->port_min), |
1162 | .high = htons(vxlan->port_max), |
1163 | }; |
d342894c |
1164 | |
1165 | if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni)) |
1166 | goto nla_put_failure; |
1167 | |
7c41c42c |
1168 | if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr)) |
d342894c |
1169 | goto nla_put_failure; |
1170 | |
1171 | if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link)) |
1172 | goto nla_put_failure; |
1173 | |
7c41c42c |
1174 | if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr)) |
d342894c |
1175 | goto nla_put_failure; |
1176 | |
1177 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || |
1178 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || |
1179 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) || |
1180 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
1181 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax)) |
1182 | goto nla_put_failure; |
1183 | |
05f47d69 |
1184 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
1185 | goto nla_put_failure; |
1186 | |
d342894c |
1187 | return 0; |
1188 | |
1189 | nla_put_failure: |
1190 | return -EMSGSIZE; |
1191 | } |
1192 | |
1193 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
1194 | .kind = "vxlan", |
1195 | .maxtype = IFLA_VXLAN_MAX, |
1196 | .policy = vxlan_policy, |
1197 | .priv_size = sizeof(struct vxlan_dev), |
1198 | .setup = vxlan_setup, |
1199 | .validate = vxlan_validate, |
1200 | .newlink = vxlan_newlink, |
1201 | .dellink = vxlan_dellink, |
1202 | .get_size = vxlan_get_size, |
1203 | .fill_info = vxlan_fill_info, |
1204 | }; |
1205 | |
1206 | static __net_init int vxlan_init_net(struct net *net) |
1207 | { |
1208 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
1209 | struct sock *sk; |
1210 | struct sockaddr_in vxlan_addr = { |
1211 | .sin_family = AF_INET, |
1212 | .sin_addr.s_addr = htonl(INADDR_ANY), |
1213 | }; |
1214 | int rc; |
1215 | unsigned h; |
1216 | |
1217 | /* Create UDP socket for encapsulation receive. */ |
1218 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock); |
1219 | if (rc < 0) { |
1220 | pr_debug("UDP socket create failed\n"); |
1221 | return rc; |
1222 | } |
bfe1b9b1 |
1223 | /* Put in proper namespace */ |
1224 | sk = vn->sock->sk; |
1225 | sk_change_net(sk, net); |
d342894c |
1226 | |
1227 | vxlan_addr.sin_port = htons(vxlan_port); |
1228 | |
1229 | rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr, |
1230 | sizeof(vxlan_addr)); |
1231 | if (rc < 0) { |
1232 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", |
1233 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); |
bfe1b9b1 |
1234 | sk_release_kernel(sk); |
d342894c |
1235 | vn->sock = NULL; |
1236 | return rc; |
1237 | } |
1238 | |
1239 | /* Disable multicast loopback */ |
d342894c |
1240 | inet_sk(sk)->mc_loop = 0; |
1241 | |
1242 | /* Mark socket as an encapsulation socket. */ |
1243 | udp_sk(sk)->encap_type = 1; |
1244 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; |
1245 | udp_encap_enable(); |
1246 | |
1247 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
1248 | INIT_HLIST_HEAD(&vn->vni_list[h]); |
1249 | |
1250 | return 0; |
1251 | } |
1252 | |
1253 | static __net_exit void vxlan_exit_net(struct net *net) |
1254 | { |
1255 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
1256 | |
1257 | if (vn->sock) { |
bfe1b9b1 |
1258 | sk_release_kernel(vn->sock->sk); |
d342894c |
1259 | vn->sock = NULL; |
1260 | } |
1261 | } |
1262 | |
1263 | static struct pernet_operations vxlan_net_ops = { |
1264 | .init = vxlan_init_net, |
1265 | .exit = vxlan_exit_net, |
1266 | .id = &vxlan_net_id, |
1267 | .size = sizeof(struct vxlan_net), |
1268 | }; |
1269 | |
1270 | static int __init vxlan_init_module(void) |
1271 | { |
1272 | int rc; |
1273 | |
1274 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
1275 | |
1276 | rc = register_pernet_device(&vxlan_net_ops); |
1277 | if (rc) |
1278 | goto out1; |
1279 | |
1280 | rc = rtnl_link_register(&vxlan_link_ops); |
1281 | if (rc) |
1282 | goto out2; |
1283 | |
1284 | return 0; |
1285 | |
1286 | out2: |
1287 | unregister_pernet_device(&vxlan_net_ops); |
1288 | out1: |
1289 | return rc; |
1290 | } |
1291 | module_init(vxlan_init_module); |
1292 | |
1293 | static void __exit vxlan_cleanup_module(void) |
1294 | { |
1295 | rtnl_link_unregister(&vxlan_link_ops); |
1296 | unregister_pernet_device(&vxlan_net_ops); |
1297 | } |
1298 | module_exit(vxlan_cleanup_module); |
1299 | |
1300 | MODULE_LICENSE("GPL"); |
1301 | MODULE_VERSION(VXLAN_VERSION); |
1302 | MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>"); |
1303 | MODULE_ALIAS_RTNL_LINK("vxlan"); |