2 * VXLAN: Virtual eXtensible Local Area Network
4 * Copyright (c) 2012 Vyatta Inc.
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.
11 * - use IANA UDP port number (when defined)
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
27 #include <linux/udp.h>
28 #include <linux/igmp.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/hash.h>
32 #include <linux/ethtool.h>
34 #include <net/ndisc.h>
39 #include <net/rtnetlink.h>
40 #include <net/route.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/net_namespace.h>
44 #include <net/netns/generic.h>
46 #define VXLAN_VERSION "0.1"
48 #define VNI_HASH_BITS 10
49 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
50 #define FDB_HASH_BITS 8
51 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
52 #define FDB_AGE_DEFAULT 300 /* 5 min */
53 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55 #define VXLAN_N_VID (1u << 24)
56 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
57 /* IP header + UDP + VXLAN + Ethernet header */
58 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
60 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62 /* VXLAN protocol header */
68 /* UDP port for VXLAN traffic. */
69 static unsigned int vxlan_port __read_mostly
= 8472;
70 module_param_named(udp_port
, vxlan_port
, uint
, 0444);
71 MODULE_PARM_DESC(udp_port
, "Destination UDP port");
73 static bool log_ecn_error
= true;
74 module_param(log_ecn_error
, bool, 0644);
75 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
77 /* per-net private data for this module */
78 static unsigned int vxlan_net_id
;
80 struct socket
*sock
; /* UDP encap socket */
81 struct hlist_head vni_list
[VNI_HASH_SIZE
];
90 struct vxlan_rdst
*remote_next
;
93 /* Forwarding table entry */
95 struct hlist_node hlist
; /* linked list of entries */
97 unsigned long updated
; /* jiffies */
99 struct vxlan_rdst remote
;
100 u16 state
; /* see ndm_state */
101 u8 eth_addr
[ETH_ALEN
];
104 /* Per-cpu network traffic stats */
110 struct u64_stats_sync syncp
;
113 /* Pseudo network device */
115 struct hlist_node hlist
;
116 struct net_device
*dev
;
117 struct vxlan_stats __percpu
*stats
;
118 __u32 vni
; /* virtual network id */
119 __be32 gaddr
; /* multicast group */
120 __be32 saddr
; /* source address */
121 unsigned int link
; /* link to multicast over */
122 __u16 port_min
; /* source port range */
124 __u8 tos
; /* TOS override */
126 u32 flags
; /* VXLAN_F_* below */
128 unsigned long age_interval
;
129 struct timer_list age_timer
;
130 spinlock_t hash_lock
;
131 unsigned int addrcnt
;
132 unsigned int addrmax
;
134 struct hlist_head fdb_head
[FDB_HASH_SIZE
];
137 #define VXLAN_F_LEARN 0x01
138 #define VXLAN_F_PROXY 0x02
139 #define VXLAN_F_RSC 0x04
140 #define VXLAN_F_L2MISS 0x08
141 #define VXLAN_F_L3MISS 0x10
143 /* salt for hash table */
144 static u32 vxlan_salt __read_mostly
;
146 static inline struct hlist_head
*vni_head(struct net
*net
, u32 id
)
148 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
150 return &vn
->vni_list
[hash_32(id
, VNI_HASH_BITS
)];
153 /* Look up VNI in a per net namespace table */
154 static struct vxlan_dev
*vxlan_find_vni(struct net
*net
, u32 id
)
156 struct vxlan_dev
*vxlan
;
158 hlist_for_each_entry_rcu(vxlan
, vni_head(net
, id
), hlist
) {
159 if (vxlan
->vni
== id
)
166 /* Fill in neighbour message in skbuff. */
167 static int vxlan_fdb_info(struct sk_buff
*skb
, struct vxlan_dev
*vxlan
,
168 const struct vxlan_fdb
*fdb
,
169 u32 portid
, u32 seq
, int type
, unsigned int flags
,
170 const struct vxlan_rdst
*rdst
)
172 unsigned long now
= jiffies
;
173 struct nda_cacheinfo ci
;
174 struct nlmsghdr
*nlh
;
176 bool send_ip
, send_eth
;
178 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
182 ndm
= nlmsg_data(nlh
);
183 memset(ndm
, 0, sizeof(*ndm
));
185 send_eth
= send_ip
= true;
187 if (type
== RTM_GETNEIGH
) {
188 ndm
->ndm_family
= AF_INET
;
189 send_ip
= rdst
->remote_ip
!= htonl(INADDR_ANY
);
190 send_eth
= !is_zero_ether_addr(fdb
->eth_addr
);
192 ndm
->ndm_family
= AF_BRIDGE
;
193 ndm
->ndm_state
= fdb
->state
;
194 ndm
->ndm_ifindex
= vxlan
->dev
->ifindex
;
195 ndm
->ndm_flags
= NTF_SELF
;
196 ndm
->ndm_type
= NDA_DST
;
198 if (send_eth
&& nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->eth_addr
))
199 goto nla_put_failure
;
201 if (send_ip
&& nla_put_be32(skb
, NDA_DST
, rdst
->remote_ip
))
202 goto nla_put_failure
;
204 if (rdst
->remote_port
&& rdst
->remote_port
!= vxlan_port
&&
205 nla_put_be16(skb
, NDA_PORT
, rdst
->remote_port
))
206 goto nla_put_failure
;
207 if (rdst
->remote_vni
!= vxlan
->vni
&&
208 nla_put_be32(skb
, NDA_VNI
, rdst
->remote_vni
))
209 goto nla_put_failure
;
210 if (rdst
->remote_ifindex
&&
211 nla_put_u32(skb
, NDA_IFINDEX
, rdst
->remote_ifindex
))
212 goto nla_put_failure
;
214 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
215 ci
.ndm_confirmed
= 0;
216 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
219 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
220 goto nla_put_failure
;
222 return nlmsg_end(skb
, nlh
);
225 nlmsg_cancel(skb
, nlh
);
229 static inline size_t vxlan_nlmsg_size(void)
231 return NLMSG_ALIGN(sizeof(struct ndmsg
))
232 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
233 + nla_total_size(sizeof(__be32
)) /* NDA_DST */
234 + nla_total_size(sizeof(__be32
)) /* NDA_PORT */
235 + nla_total_size(sizeof(__be32
)) /* NDA_VNI */
236 + nla_total_size(sizeof(__u32
)) /* NDA_IFINDEX */
237 + nla_total_size(sizeof(struct nda_cacheinfo
));
240 static void vxlan_fdb_notify(struct vxlan_dev
*vxlan
,
241 const struct vxlan_fdb
*fdb
, int type
)
243 struct net
*net
= dev_net(vxlan
->dev
);
247 skb
= nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC
);
251 err
= vxlan_fdb_info(skb
, vxlan
, fdb
, 0, 0, type
, 0, &fdb
->remote
);
253 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
254 WARN_ON(err
== -EMSGSIZE
);
259 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
263 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
266 static void vxlan_ip_miss(struct net_device
*dev
, __be32 ipa
)
268 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
271 memset(&f
, 0, sizeof f
);
273 f
.remote
.remote_ip
= ipa
; /* goes to NDA_DST */
274 f
.remote
.remote_vni
= VXLAN_N_VID
;
276 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
279 static void vxlan_fdb_miss(struct vxlan_dev
*vxlan
, const u8 eth_addr
[ETH_ALEN
])
283 memset(&f
, 0, sizeof f
);
285 memcpy(f
.eth_addr
, eth_addr
, ETH_ALEN
);
287 vxlan_fdb_notify(vxlan
, &f
, RTM_GETNEIGH
);
290 /* Hash Ethernet address */
291 static u32
eth_hash(const unsigned char *addr
)
293 u64 value
= get_unaligned((u64
*)addr
);
295 /* only want 6 bytes */
301 return hash_64(value
, FDB_HASH_BITS
);
304 /* Hash chain to use given mac address */
305 static inline struct hlist_head
*vxlan_fdb_head(struct vxlan_dev
*vxlan
,
308 return &vxlan
->fdb_head
[eth_hash(mac
)];
311 /* Look up Ethernet address in forwarding table */
312 static struct vxlan_fdb
*vxlan_find_mac(struct vxlan_dev
*vxlan
,
316 struct hlist_head
*head
= vxlan_fdb_head(vxlan
, mac
);
319 hlist_for_each_entry_rcu(f
, head
, hlist
) {
320 if (compare_ether_addr(mac
, f
->eth_addr
) == 0)
327 /* Add/update destinations for multicast */
328 static int vxlan_fdb_append(struct vxlan_fdb
*f
,
329 __be32 ip
, __u32 port
, __u32 vni
, __u32 ifindex
)
331 struct vxlan_rdst
*rd_prev
, *rd
;
334 for (rd
= &f
->remote
; rd
; rd
= rd
->remote_next
) {
335 if (rd
->remote_ip
== ip
&&
336 rd
->remote_port
== port
&&
337 rd
->remote_vni
== vni
&&
338 rd
->remote_ifindex
== ifindex
)
342 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
346 rd
->remote_port
= port
;
347 rd
->remote_vni
= vni
;
348 rd
->remote_ifindex
= ifindex
;
349 rd
->remote_next
= NULL
;
350 rd_prev
->remote_next
= rd
;
354 /* Add new entry to forwarding table -- assumes lock held */
355 static int vxlan_fdb_create(struct vxlan_dev
*vxlan
,
356 const u8
*mac
, __be32 ip
,
357 __u16 state
, __u16 flags
,
358 __u32 port
, __u32 vni
, __u32 ifindex
)
363 f
= vxlan_find_mac(vxlan
, mac
);
365 if (flags
& NLM_F_EXCL
) {
366 netdev_dbg(vxlan
->dev
,
367 "lost race to create %pM\n", mac
);
370 if (f
->state
!= state
) {
372 f
->updated
= jiffies
;
375 if ((flags
& NLM_F_APPEND
) &&
376 is_multicast_ether_addr(f
->eth_addr
)) {
377 int rc
= vxlan_fdb_append(f
, ip
, port
, vni
, ifindex
);
384 if (!(flags
& NLM_F_CREATE
))
387 if (vxlan
->addrmax
&& vxlan
->addrcnt
>= vxlan
->addrmax
)
390 netdev_dbg(vxlan
->dev
, "add %pM -> %pI4\n", mac
, &ip
);
391 f
= kmalloc(sizeof(*f
), GFP_ATOMIC
);
396 f
->remote
.remote_ip
= ip
;
397 f
->remote
.remote_port
= port
;
398 f
->remote
.remote_vni
= vni
;
399 f
->remote
.remote_ifindex
= ifindex
;
400 f
->remote
.remote_next
= NULL
;
402 f
->updated
= f
->used
= jiffies
;
403 memcpy(f
->eth_addr
, mac
, ETH_ALEN
);
406 hlist_add_head_rcu(&f
->hlist
,
407 vxlan_fdb_head(vxlan
, mac
));
411 vxlan_fdb_notify(vxlan
, f
, RTM_NEWNEIGH
);
416 void vxlan_fdb_free(struct rcu_head
*head
)
418 struct vxlan_fdb
*f
= container_of(head
, struct vxlan_fdb
, rcu
);
420 while (f
->remote
.remote_next
) {
421 struct vxlan_rdst
*rd
= f
->remote
.remote_next
;
423 f
->remote
.remote_next
= rd
->remote_next
;
429 static void vxlan_fdb_destroy(struct vxlan_dev
*vxlan
, struct vxlan_fdb
*f
)
431 netdev_dbg(vxlan
->dev
,
432 "delete %pM\n", f
->eth_addr
);
435 vxlan_fdb_notify(vxlan
, f
, RTM_DELNEIGH
);
437 hlist_del_rcu(&f
->hlist
);
438 call_rcu(&f
->rcu
, vxlan_fdb_free
);
441 /* Add static entry (via netlink) */
442 static int vxlan_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
443 struct net_device
*dev
,
444 const unsigned char *addr
, u16 flags
)
446 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
447 struct net
*net
= dev_net(vxlan
->dev
);
449 u32 port
, vni
, ifindex
;
452 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_REACHABLE
))) {
453 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
458 if (tb
[NDA_DST
] == NULL
)
461 if (nla_len(tb
[NDA_DST
]) != sizeof(__be32
))
462 return -EAFNOSUPPORT
;
464 ip
= nla_get_be32(tb
[NDA_DST
]);
467 if (nla_len(tb
[NDA_PORT
]) != sizeof(u32
))
469 port
= nla_get_u32(tb
[NDA_PORT
]);
474 if (nla_len(tb
[NDA_VNI
]) != sizeof(u32
))
476 vni
= nla_get_u32(tb
[NDA_VNI
]);
480 if (tb
[NDA_IFINDEX
]) {
481 struct net_device
*dev
;
483 if (nla_len(tb
[NDA_IFINDEX
]) != sizeof(u32
))
485 ifindex
= nla_get_u32(tb
[NDA_IFINDEX
]);
486 dev
= dev_get_by_index(net
, ifindex
);
488 return -EADDRNOTAVAIL
;
493 spin_lock_bh(&vxlan
->hash_lock
);
494 err
= vxlan_fdb_create(vxlan
, addr
, ip
, ndm
->ndm_state
, flags
, port
,
496 spin_unlock_bh(&vxlan
->hash_lock
);
501 /* Delete entry (via netlink) */
502 static int vxlan_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
503 struct net_device
*dev
,
504 const unsigned char *addr
)
506 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
510 spin_lock_bh(&vxlan
->hash_lock
);
511 f
= vxlan_find_mac(vxlan
, addr
);
513 vxlan_fdb_destroy(vxlan
, f
);
516 spin_unlock_bh(&vxlan
->hash_lock
);
521 /* Dump forwarding table */
522 static int vxlan_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
523 struct net_device
*dev
, int idx
)
525 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
528 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
532 hlist_for_each_entry_rcu(f
, &vxlan
->fdb_head
[h
], hlist
) {
533 struct vxlan_rdst
*rd
;
534 for (rd
= &f
->remote
; rd
; rd
= rd
->remote_next
) {
535 if (idx
< cb
->args
[0])
538 err
= vxlan_fdb_info(skb
, vxlan
, f
,
539 NETLINK_CB(cb
->skb
).portid
,
554 /* Watch incoming packets to learn mapping between Ethernet address
555 * and Tunnel endpoint.
557 static void vxlan_snoop(struct net_device
*dev
,
558 __be32 src_ip
, const u8
*src_mac
)
560 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
564 f
= vxlan_find_mac(vxlan
, src_mac
);
567 if (likely(f
->remote
.remote_ip
== src_ip
))
572 "%pM migrated from %pI4 to %pI4\n",
573 src_mac
, &f
->remote
.remote_ip
, &src_ip
);
575 f
->remote
.remote_ip
= src_ip
;
576 f
->updated
= jiffies
;
578 /* learned new entry */
579 spin_lock(&vxlan
->hash_lock
);
580 err
= vxlan_fdb_create(vxlan
, src_mac
, src_ip
,
582 NLM_F_EXCL
|NLM_F_CREATE
,
583 vxlan_port
, vxlan
->vni
, 0);
584 spin_unlock(&vxlan
->hash_lock
);
589 /* See if multicast group is already in use by other ID */
590 static bool vxlan_group_used(struct vxlan_net
*vn
,
591 const struct vxlan_dev
*this)
593 const struct vxlan_dev
*vxlan
;
596 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
597 hlist_for_each_entry(vxlan
, &vn
->vni_list
[h
], hlist
) {
601 if (!netif_running(vxlan
->dev
))
604 if (vxlan
->gaddr
== this->gaddr
)
611 /* kernel equivalent to IP_ADD_MEMBERSHIP */
612 static int vxlan_join_group(struct net_device
*dev
)
614 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
615 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
616 struct sock
*sk
= vn
->sock
->sk
;
617 struct ip_mreqn mreq
= {
618 .imr_multiaddr
.s_addr
= vxlan
->gaddr
,
619 .imr_ifindex
= vxlan
->link
,
623 /* Already a member of group */
624 if (vxlan_group_used(vn
, vxlan
))
627 /* Need to drop RTNL to call multicast join */
630 err
= ip_mc_join_group(sk
, &mreq
);
638 /* kernel equivalent to IP_DROP_MEMBERSHIP */
639 static int vxlan_leave_group(struct net_device
*dev
)
641 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
642 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
644 struct sock
*sk
= vn
->sock
->sk
;
645 struct ip_mreqn mreq
= {
646 .imr_multiaddr
.s_addr
= vxlan
->gaddr
,
647 .imr_ifindex
= vxlan
->link
,
650 /* Only leave group when last vxlan is done. */
651 if (vxlan_group_used(vn
, vxlan
))
654 /* Need to drop RTNL to call multicast leave */
657 err
= ip_mc_leave_group(sk
, &mreq
);
664 /* Callback from net/ipv4/udp.c to receive packets */
665 static int vxlan_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
668 struct vxlanhdr
*vxh
;
669 struct vxlan_dev
*vxlan
;
670 struct vxlan_stats
*stats
;
674 /* pop off outer UDP header */
675 __skb_pull(skb
, sizeof(struct udphdr
));
677 /* Need Vxlan and inner Ethernet header to be present */
678 if (!pskb_may_pull(skb
, sizeof(struct vxlanhdr
)))
681 /* Drop packets with reserved bits set */
682 vxh
= (struct vxlanhdr
*) skb
->data
;
683 if (vxh
->vx_flags
!= htonl(VXLAN_FLAGS
) ||
684 (vxh
->vx_vni
& htonl(0xff))) {
685 netdev_dbg(skb
->dev
, "invalid vxlan flags=%#x vni=%#x\n",
686 ntohl(vxh
->vx_flags
), ntohl(vxh
->vx_vni
));
690 __skb_pull(skb
, sizeof(struct vxlanhdr
));
692 /* Is this VNI defined? */
693 vni
= ntohl(vxh
->vx_vni
) >> 8;
694 vxlan
= vxlan_find_vni(sock_net(sk
), vni
);
696 netdev_dbg(skb
->dev
, "unknown vni %d\n", vni
);
700 if (!pskb_may_pull(skb
, ETH_HLEN
)) {
701 vxlan
->dev
->stats
.rx_length_errors
++;
702 vxlan
->dev
->stats
.rx_errors
++;
706 skb_reset_mac_header(skb
);
708 /* Re-examine inner Ethernet packet */
710 skb
->protocol
= eth_type_trans(skb
, vxlan
->dev
);
712 /* Ignore packet loops (and multicast echo) */
713 if (compare_ether_addr(eth_hdr(skb
)->h_source
,
714 vxlan
->dev
->dev_addr
) == 0)
717 if (vxlan
->flags
& VXLAN_F_LEARN
)
718 vxlan_snoop(skb
->dev
, oip
->saddr
, eth_hdr(skb
)->h_source
);
720 __skb_tunnel_rx(skb
, vxlan
->dev
);
721 skb_reset_network_header(skb
);
723 /* If the NIC driver gave us an encapsulated packet with
724 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
725 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
726 * for us. Otherwise force the upper layers to verify it.
728 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
|| !skb
->encapsulation
||
729 !(vxlan
->dev
->features
& NETIF_F_RXCSUM
))
730 skb
->ip_summed
= CHECKSUM_NONE
;
732 skb
->encapsulation
= 0;
734 err
= IP_ECN_decapsulate(oip
, skb
);
737 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
738 &oip
->saddr
, oip
->tos
);
740 ++vxlan
->dev
->stats
.rx_frame_errors
;
741 ++vxlan
->dev
->stats
.rx_errors
;
746 stats
= this_cpu_ptr(vxlan
->stats
);
747 u64_stats_update_begin(&stats
->syncp
);
749 stats
->rx_bytes
+= skb
->len
;
750 u64_stats_update_end(&stats
->syncp
);
756 /* Put UDP header back */
757 __skb_push(skb
, sizeof(struct udphdr
));
761 /* Consume bad packet */
766 static int arp_reduce(struct net_device
*dev
, struct sk_buff
*skb
)
768 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
774 if (dev
->flags
& IFF_NOARP
)
777 if (!pskb_may_pull(skb
, arp_hdr_len(dev
))) {
778 dev
->stats
.tx_dropped
++;
783 if ((parp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
784 parp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
785 parp
->ar_pro
!= htons(ETH_P_IP
) ||
786 parp
->ar_op
!= htons(ARPOP_REQUEST
) ||
787 parp
->ar_hln
!= dev
->addr_len
||
790 arpptr
= (u8
*)parp
+ sizeof(struct arphdr
);
792 arpptr
+= dev
->addr_len
; /* sha */
793 memcpy(&sip
, arpptr
, sizeof(sip
));
794 arpptr
+= sizeof(sip
);
795 arpptr
+= dev
->addr_len
; /* tha */
796 memcpy(&tip
, arpptr
, sizeof(tip
));
798 if (ipv4_is_loopback(tip
) ||
799 ipv4_is_multicast(tip
))
802 n
= neigh_lookup(&arp_tbl
, &tip
, dev
);
805 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
807 struct sk_buff
*reply
;
809 if (!(n
->nud_state
& NUD_CONNECTED
)) {
814 f
= vxlan_find_mac(vxlan
, n
->ha
);
815 if (f
&& f
->remote
.remote_ip
== htonl(INADDR_ANY
)) {
816 /* bridge-local neighbor */
821 reply
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, sip
, dev
, tip
, sha
,
826 skb_reset_mac_header(reply
);
827 __skb_pull(reply
, skb_network_offset(reply
));
828 reply
->ip_summed
= CHECKSUM_UNNECESSARY
;
829 reply
->pkt_type
= PACKET_HOST
;
831 if (netif_rx_ni(reply
) == NET_RX_DROP
)
832 dev
->stats
.rx_dropped
++;
833 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
834 vxlan_ip_miss(dev
, tip
);
840 static bool route_shortcircuit(struct net_device
*dev
, struct sk_buff
*skb
)
842 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
846 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
850 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
852 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
855 n
= neigh_lookup(&arp_tbl
, &pip
->daddr
, dev
);
864 diff
= compare_ether_addr(eth_hdr(skb
)->h_dest
, n
->ha
) != 0;
866 memcpy(eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
868 memcpy(eth_hdr(skb
)->h_dest
, n
->ha
, dev
->addr_len
);
872 } else if (vxlan
->flags
& VXLAN_F_L3MISS
)
873 vxlan_ip_miss(dev
, pip
->daddr
);
877 /* Extract dsfield from inner protocol */
878 static inline u8
vxlan_get_dsfield(const struct iphdr
*iph
,
879 const struct sk_buff
*skb
)
881 if (skb
->protocol
== htons(ETH_P_IP
))
883 else if (skb
->protocol
== htons(ETH_P_IPV6
))
884 return ipv6_get_dsfield((const struct ipv6hdr
*)iph
);
889 /* Propogate ECN bits out */
890 static inline u8
vxlan_ecn_encap(u8 tos
,
891 const struct iphdr
*iph
,
892 const struct sk_buff
*skb
)
894 u8 inner
= vxlan_get_dsfield(iph
, skb
);
896 return INET_ECN_encapsulate(tos
, inner
);
899 static void vxlan_sock_free(struct sk_buff
*skb
)
904 /* On transmit, associate with the tunnel socket */
905 static void vxlan_set_owner(struct net_device
*dev
, struct sk_buff
*skb
)
907 struct vxlan_net
*vn
= net_generic(dev_net(dev
), vxlan_net_id
);
908 struct sock
*sk
= vn
->sock
->sk
;
913 skb
->destructor
= vxlan_sock_free
;
916 /* Compute source port for outgoing packet
917 * first choice to use L4 flow hash since it will spread
918 * better and maybe available from hardware
919 * secondary choice is to use jhash on the Ethernet header
921 static u16
vxlan_src_port(const struct vxlan_dev
*vxlan
, struct sk_buff
*skb
)
923 unsigned int range
= (vxlan
->port_max
- vxlan
->port_min
) + 1;
926 hash
= skb_get_rxhash(skb
);
928 hash
= jhash(skb
->data
, 2 * ETH_ALEN
,
929 (__force u32
) skb
->protocol
);
931 return (((u64
) hash
* range
) >> 32) + vxlan
->port_min
;
934 static int handle_offloads(struct sk_buff
*skb
)
936 if (skb_is_gso(skb
)) {
937 int err
= skb_unclone(skb
, GFP_ATOMIC
);
941 skb_shinfo(skb
)->gso_type
|= (SKB_GSO_UDP_TUNNEL
| SKB_GSO_UDP
);
942 } else if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
943 skb
->ip_summed
= CHECKSUM_NONE
;
948 static netdev_tx_t
vxlan_xmit_one(struct sk_buff
*skb
, struct net_device
*dev
,
949 struct vxlan_rdst
*rdst
, bool did_rsc
)
951 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
953 const struct iphdr
*old_iph
;
955 struct vxlanhdr
*vxh
;
958 unsigned int pkt_len
= skb
->len
;
960 __u16 src_port
, dst_port
;
965 dst_port
= rdst
->remote_port
? rdst
->remote_port
: vxlan_port
;
966 vni
= rdst
->remote_vni
;
967 dst
= rdst
->remote_ip
;
971 __skb_pull(skb
, skb_network_offset(skb
));
972 skb
->ip_summed
= CHECKSUM_NONE
;
973 skb
->pkt_type
= PACKET_HOST
;
975 /* short-circuited back to local bridge */
976 if (netif_rx(skb
) == NET_RX_SUCCESS
) {
977 struct vxlan_stats
*stats
=
978 this_cpu_ptr(vxlan
->stats
);
980 u64_stats_update_begin(&stats
->syncp
);
982 stats
->tx_bytes
+= pkt_len
;
983 u64_stats_update_end(&stats
->syncp
);
985 dev
->stats
.tx_errors
++;
986 dev
->stats
.tx_aborted_errors
++;
993 if (!skb
->encapsulation
) {
994 skb_reset_inner_headers(skb
);
995 skb
->encapsulation
= 1;
998 /* Need space for new headers (invalidates iph ptr) */
999 if (skb_cow_head(skb
, VXLAN_HEADROOM
))
1002 old_iph
= ip_hdr(skb
);
1005 if (!ttl
&& IN_MULTICAST(ntohl(dst
)))
1010 tos
= vxlan_get_dsfield(old_iph
, skb
);
1012 src_port
= vxlan_src_port(vxlan
, skb
);
1014 memset(&fl4
, 0, sizeof(fl4
));
1015 fl4
.flowi4_oif
= rdst
->remote_ifindex
;
1016 fl4
.flowi4_tos
= RT_TOS(tos
);
1018 fl4
.saddr
= vxlan
->saddr
;
1020 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1022 netdev_dbg(dev
, "no route to %pI4\n", &dst
);
1023 dev
->stats
.tx_carrier_errors
++;
1027 if (rt
->dst
.dev
== dev
) {
1028 netdev_dbg(dev
, "circular route to %pI4\n", &dst
);
1030 dev
->stats
.collisions
++;
1034 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1035 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1038 skb_dst_set(skb
, &rt
->dst
);
1040 vxh
= (struct vxlanhdr
*) __skb_push(skb
, sizeof(*vxh
));
1041 vxh
->vx_flags
= htonl(VXLAN_FLAGS
);
1042 vxh
->vx_vni
= htonl(vni
<< 8);
1044 __skb_push(skb
, sizeof(*uh
));
1045 skb_reset_transport_header(skb
);
1048 uh
->dest
= htons(dst_port
);
1049 uh
->source
= htons(src_port
);
1051 uh
->len
= htons(skb
->len
);
1054 __skb_push(skb
, sizeof(*iph
));
1055 skb_reset_network_header(skb
);
1058 iph
->ihl
= sizeof(struct iphdr
) >> 2;
1060 iph
->protocol
= IPPROTO_UDP
;
1061 iph
->tos
= vxlan_ecn_encap(tos
, old_iph
, skb
);
1063 iph
->saddr
= fl4
.saddr
;
1064 iph
->ttl
= ttl
? : ip4_dst_hoplimit(&rt
->dst
);
1065 tunnel_ip_select_ident(skb
, old_iph
, &rt
->dst
);
1069 vxlan_set_owner(dev
, skb
);
1071 if (handle_offloads(skb
))
1074 iptunnel_xmit(skb
, dev
);
1075 return NETDEV_TX_OK
;
1078 dev
->stats
.tx_dropped
++;
1082 dev
->stats
.tx_errors
++;
1085 return NETDEV_TX_OK
;
1088 /* Transmit local packets over Vxlan
1090 * Outer IP header inherits ECN and DF from inner header.
1091 * Outer UDP destination is the VXLAN assigned port.
1092 * source port is based on hash of flow
1094 static netdev_tx_t
vxlan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1096 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1098 bool did_rsc
= false;
1099 struct vxlan_rdst group
, *rdst0
, *rdst
;
1100 struct vxlan_fdb
*f
;
1103 skb_reset_mac_header(skb
);
1106 if ((vxlan
->flags
& VXLAN_F_PROXY
) && ntohs(eth
->h_proto
) == ETH_P_ARP
)
1107 return arp_reduce(dev
, skb
);
1108 else if ((vxlan
->flags
&VXLAN_F_RSC
) && ntohs(eth
->h_proto
) == ETH_P_IP
)
1109 did_rsc
= route_shortcircuit(dev
, skb
);
1111 f
= vxlan_find_mac(vxlan
, eth
->h_dest
);
1114 group
.remote_port
= vxlan_port
;
1115 group
.remote_vni
= vxlan
->vni
;
1116 group
.remote_ip
= vxlan
->gaddr
;
1117 group
.remote_ifindex
= vxlan
->link
;
1118 group
.remote_next
= 0;
1121 if (group
.remote_ip
== htonl(INADDR_ANY
) &&
1122 (vxlan
->flags
& VXLAN_F_L2MISS
) &&
1123 !is_multicast_ether_addr(eth
->h_dest
))
1124 vxlan_fdb_miss(vxlan
, eth
->h_dest
);
1130 /* if there are multiple destinations, send copies */
1131 for (rdst
= rdst0
->remote_next
; rdst
; rdst
= rdst
->remote_next
) {
1132 struct sk_buff
*skb1
;
1134 skb1
= skb_clone(skb
, GFP_ATOMIC
);
1135 rc1
= vxlan_xmit_one(skb1
, dev
, rdst
, did_rsc
);
1136 if (rc
== NETDEV_TX_OK
)
1140 rc1
= vxlan_xmit_one(skb
, dev
, rdst0
, did_rsc
);
1141 if (rc
== NETDEV_TX_OK
)
1146 /* Walk the forwarding table and purge stale entries */
1147 static void vxlan_cleanup(unsigned long arg
)
1149 struct vxlan_dev
*vxlan
= (struct vxlan_dev
*) arg
;
1150 unsigned long next_timer
= jiffies
+ FDB_AGE_INTERVAL
;
1153 if (!netif_running(vxlan
->dev
))
1156 spin_lock_bh(&vxlan
->hash_lock
);
1157 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1158 struct hlist_node
*p
, *n
;
1159 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1161 = container_of(p
, struct vxlan_fdb
, hlist
);
1162 unsigned long timeout
;
1164 if (f
->state
& NUD_PERMANENT
)
1167 timeout
= f
->used
+ vxlan
->age_interval
* HZ
;
1168 if (time_before_eq(timeout
, jiffies
)) {
1169 netdev_dbg(vxlan
->dev
,
1170 "garbage collect %pM\n",
1172 f
->state
= NUD_STALE
;
1173 vxlan_fdb_destroy(vxlan
, f
);
1174 } else if (time_before(timeout
, next_timer
))
1175 next_timer
= timeout
;
1178 spin_unlock_bh(&vxlan
->hash_lock
);
1180 mod_timer(&vxlan
->age_timer
, next_timer
);
1183 /* Setup stats when device is created */
1184 static int vxlan_init(struct net_device
*dev
)
1186 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1188 vxlan
->stats
= alloc_percpu(struct vxlan_stats
);
1195 /* Start ageing timer and join group when device is brought up */
1196 static int vxlan_open(struct net_device
*dev
)
1198 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1202 err
= vxlan_join_group(dev
);
1207 if (vxlan
->age_interval
)
1208 mod_timer(&vxlan
->age_timer
, jiffies
+ FDB_AGE_INTERVAL
);
1213 /* Purge the forwarding table */
1214 static void vxlan_flush(struct vxlan_dev
*vxlan
)
1218 spin_lock_bh(&vxlan
->hash_lock
);
1219 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
) {
1220 struct hlist_node
*p
, *n
;
1221 hlist_for_each_safe(p
, n
, &vxlan
->fdb_head
[h
]) {
1223 = container_of(p
, struct vxlan_fdb
, hlist
);
1224 vxlan_fdb_destroy(vxlan
, f
);
1227 spin_unlock_bh(&vxlan
->hash_lock
);
1230 /* Cleanup timer and forwarding table on shutdown */
1231 static int vxlan_stop(struct net_device
*dev
)
1233 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1236 vxlan_leave_group(dev
);
1238 del_timer_sync(&vxlan
->age_timer
);
1245 /* Merge per-cpu statistics */
1246 static struct rtnl_link_stats64
*vxlan_stats64(struct net_device
*dev
,
1247 struct rtnl_link_stats64
*stats
)
1249 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1250 struct vxlan_stats tmp
, sum
= { 0 };
1253 for_each_possible_cpu(cpu
) {
1255 const struct vxlan_stats
*stats
1256 = per_cpu_ptr(vxlan
->stats
, cpu
);
1259 start
= u64_stats_fetch_begin_bh(&stats
->syncp
);
1260 memcpy(&tmp
, stats
, sizeof(tmp
));
1261 } while (u64_stats_fetch_retry_bh(&stats
->syncp
, start
));
1263 sum
.tx_bytes
+= tmp
.tx_bytes
;
1264 sum
.tx_packets
+= tmp
.tx_packets
;
1265 sum
.rx_bytes
+= tmp
.rx_bytes
;
1266 sum
.rx_packets
+= tmp
.rx_packets
;
1269 stats
->tx_bytes
= sum
.tx_bytes
;
1270 stats
->tx_packets
= sum
.tx_packets
;
1271 stats
->rx_bytes
= sum
.rx_bytes
;
1272 stats
->rx_packets
= sum
.rx_packets
;
1274 stats
->multicast
= dev
->stats
.multicast
;
1275 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1276 stats
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
1277 stats
->rx_errors
= dev
->stats
.rx_errors
;
1279 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
1280 stats
->tx_carrier_errors
= dev
->stats
.tx_carrier_errors
;
1281 stats
->tx_aborted_errors
= dev
->stats
.tx_aborted_errors
;
1282 stats
->collisions
= dev
->stats
.collisions
;
1283 stats
->tx_errors
= dev
->stats
.tx_errors
;
1288 /* Stub, nothing needs to be done. */
1289 static void vxlan_set_multicast_list(struct net_device
*dev
)
1293 static const struct net_device_ops vxlan_netdev_ops
= {
1294 .ndo_init
= vxlan_init
,
1295 .ndo_open
= vxlan_open
,
1296 .ndo_stop
= vxlan_stop
,
1297 .ndo_start_xmit
= vxlan_xmit
,
1298 .ndo_get_stats64
= vxlan_stats64
,
1299 .ndo_set_rx_mode
= vxlan_set_multicast_list
,
1300 .ndo_change_mtu
= eth_change_mtu
,
1301 .ndo_validate_addr
= eth_validate_addr
,
1302 .ndo_set_mac_address
= eth_mac_addr
,
1303 .ndo_fdb_add
= vxlan_fdb_add
,
1304 .ndo_fdb_del
= vxlan_fdb_delete
,
1305 .ndo_fdb_dump
= vxlan_fdb_dump
,
1308 /* Info for udev, that this is a virtual tunnel endpoint */
1309 static struct device_type vxlan_type
= {
1313 static void vxlan_free(struct net_device
*dev
)
1315 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1317 free_percpu(vxlan
->stats
);
1321 /* Initialize the device structure. */
1322 static void vxlan_setup(struct net_device
*dev
)
1324 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1328 eth_hw_addr_random(dev
);
1330 dev
->hard_header_len
= ETH_HLEN
+ VXLAN_HEADROOM
;
1332 dev
->netdev_ops
= &vxlan_netdev_ops
;
1333 dev
->destructor
= vxlan_free
;
1334 SET_NETDEV_DEVTYPE(dev
, &vxlan_type
);
1336 dev
->tx_queue_len
= 0;
1337 dev
->features
|= NETIF_F_LLTX
;
1338 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1339 dev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
1340 dev
->features
|= NETIF_F_RXCSUM
;
1341 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1343 dev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1344 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1345 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1346 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
1348 spin_lock_init(&vxlan
->hash_lock
);
1350 init_timer_deferrable(&vxlan
->age_timer
);
1351 vxlan
->age_timer
.function
= vxlan_cleanup
;
1352 vxlan
->age_timer
.data
= (unsigned long) vxlan
;
1354 inet_get_local_port_range(&low
, &high
);
1355 vxlan
->port_min
= low
;
1356 vxlan
->port_max
= high
;
1360 for (h
= 0; h
< FDB_HASH_SIZE
; ++h
)
1361 INIT_HLIST_HEAD(&vxlan
->fdb_head
[h
]);
1364 static const struct nla_policy vxlan_policy
[IFLA_VXLAN_MAX
+ 1] = {
1365 [IFLA_VXLAN_ID
] = { .type
= NLA_U32
},
1366 [IFLA_VXLAN_GROUP
] = { .len
= FIELD_SIZEOF(struct iphdr
, daddr
) },
1367 [IFLA_VXLAN_LINK
] = { .type
= NLA_U32
},
1368 [IFLA_VXLAN_LOCAL
] = { .len
= FIELD_SIZEOF(struct iphdr
, saddr
) },
1369 [IFLA_VXLAN_TOS
] = { .type
= NLA_U8
},
1370 [IFLA_VXLAN_TTL
] = { .type
= NLA_U8
},
1371 [IFLA_VXLAN_LEARNING
] = { .type
= NLA_U8
},
1372 [IFLA_VXLAN_AGEING
] = { .type
= NLA_U32
},
1373 [IFLA_VXLAN_LIMIT
] = { .type
= NLA_U32
},
1374 [IFLA_VXLAN_PORT_RANGE
] = { .len
= sizeof(struct ifla_vxlan_port_range
) },
1375 [IFLA_VXLAN_PROXY
] = { .type
= NLA_U8
},
1376 [IFLA_VXLAN_RSC
] = { .type
= NLA_U8
},
1377 [IFLA_VXLAN_L2MISS
] = { .type
= NLA_U8
},
1378 [IFLA_VXLAN_L3MISS
] = { .type
= NLA_U8
},
1381 static int vxlan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1383 if (tb
[IFLA_ADDRESS
]) {
1384 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
) {
1385 pr_debug("invalid link address (not ethernet)\n");
1389 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
]))) {
1390 pr_debug("invalid all zero ethernet address\n");
1391 return -EADDRNOTAVAIL
;
1398 if (data
[IFLA_VXLAN_ID
]) {
1399 __u32 id
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1400 if (id
>= VXLAN_VID_MASK
)
1404 if (data
[IFLA_VXLAN_GROUP
]) {
1405 __be32 gaddr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
1406 if (!IN_MULTICAST(ntohl(gaddr
))) {
1407 pr_debug("group address is not IPv4 multicast\n");
1408 return -EADDRNOTAVAIL
;
1412 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1413 const struct ifla_vxlan_port_range
*p
1414 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1416 if (ntohs(p
->high
) < ntohs(p
->low
)) {
1417 pr_debug("port range %u .. %u not valid\n",
1418 ntohs(p
->low
), ntohs(p
->high
));
1426 static void vxlan_get_drvinfo(struct net_device
*netdev
,
1427 struct ethtool_drvinfo
*drvinfo
)
1429 strlcpy(drvinfo
->version
, VXLAN_VERSION
, sizeof(drvinfo
->version
));
1430 strlcpy(drvinfo
->driver
, "vxlan", sizeof(drvinfo
->driver
));
1433 static const struct ethtool_ops vxlan_ethtool_ops
= {
1434 .get_drvinfo
= vxlan_get_drvinfo
,
1435 .get_link
= ethtool_op_get_link
,
1438 static int vxlan_newlink(struct net
*net
, struct net_device
*dev
,
1439 struct nlattr
*tb
[], struct nlattr
*data
[])
1441 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1445 if (!data
[IFLA_VXLAN_ID
])
1448 vni
= nla_get_u32(data
[IFLA_VXLAN_ID
]);
1449 if (vxlan_find_vni(net
, vni
)) {
1450 pr_info("duplicate VNI %u\n", vni
);
1455 if (data
[IFLA_VXLAN_GROUP
])
1456 vxlan
->gaddr
= nla_get_be32(data
[IFLA_VXLAN_GROUP
]);
1458 if (data
[IFLA_VXLAN_LOCAL
])
1459 vxlan
->saddr
= nla_get_be32(data
[IFLA_VXLAN_LOCAL
]);
1461 if (data
[IFLA_VXLAN_LINK
] &&
1462 (vxlan
->link
= nla_get_u32(data
[IFLA_VXLAN_LINK
]))) {
1463 struct net_device
*lowerdev
1464 = __dev_get_by_index(net
, vxlan
->link
);
1467 pr_info("ifindex %d does not exist\n", vxlan
->link
);
1472 dev
->mtu
= lowerdev
->mtu
- VXLAN_HEADROOM
;
1474 /* update header length based on lower device */
1475 dev
->hard_header_len
= lowerdev
->hard_header_len
+
1479 if (data
[IFLA_VXLAN_TOS
])
1480 vxlan
->tos
= nla_get_u8(data
[IFLA_VXLAN_TOS
]);
1482 if (data
[IFLA_VXLAN_TTL
])
1483 vxlan
->ttl
= nla_get_u8(data
[IFLA_VXLAN_TTL
]);
1485 if (!data
[IFLA_VXLAN_LEARNING
] || nla_get_u8(data
[IFLA_VXLAN_LEARNING
]))
1486 vxlan
->flags
|= VXLAN_F_LEARN
;
1488 if (data
[IFLA_VXLAN_AGEING
])
1489 vxlan
->age_interval
= nla_get_u32(data
[IFLA_VXLAN_AGEING
]);
1491 vxlan
->age_interval
= FDB_AGE_DEFAULT
;
1493 if (data
[IFLA_VXLAN_PROXY
] && nla_get_u8(data
[IFLA_VXLAN_PROXY
]))
1494 vxlan
->flags
|= VXLAN_F_PROXY
;
1496 if (data
[IFLA_VXLAN_RSC
] && nla_get_u8(data
[IFLA_VXLAN_RSC
]))
1497 vxlan
->flags
|= VXLAN_F_RSC
;
1499 if (data
[IFLA_VXLAN_L2MISS
] && nla_get_u8(data
[IFLA_VXLAN_L2MISS
]))
1500 vxlan
->flags
|= VXLAN_F_L2MISS
;
1502 if (data
[IFLA_VXLAN_L3MISS
] && nla_get_u8(data
[IFLA_VXLAN_L3MISS
]))
1503 vxlan
->flags
|= VXLAN_F_L3MISS
;
1505 if (data
[IFLA_VXLAN_LIMIT
])
1506 vxlan
->addrmax
= nla_get_u32(data
[IFLA_VXLAN_LIMIT
]);
1508 if (data
[IFLA_VXLAN_PORT_RANGE
]) {
1509 const struct ifla_vxlan_port_range
*p
1510 = nla_data(data
[IFLA_VXLAN_PORT_RANGE
]);
1511 vxlan
->port_min
= ntohs(p
->low
);
1512 vxlan
->port_max
= ntohs(p
->high
);
1515 SET_ETHTOOL_OPS(dev
, &vxlan_ethtool_ops
);
1517 err
= register_netdevice(dev
);
1519 hlist_add_head_rcu(&vxlan
->hlist
, vni_head(net
, vxlan
->vni
));
1524 static void vxlan_dellink(struct net_device
*dev
, struct list_head
*head
)
1526 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1528 hlist_del_rcu(&vxlan
->hlist
);
1530 unregister_netdevice_queue(dev
, head
);
1533 static size_t vxlan_get_size(const struct net_device
*dev
)
1536 return nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_ID */
1537 nla_total_size(sizeof(__be32
)) +/* IFLA_VXLAN_GROUP */
1538 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LINK */
1539 nla_total_size(sizeof(__be32
))+ /* IFLA_VXLAN_LOCAL */
1540 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TTL */
1541 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_TOS */
1542 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_LEARNING */
1543 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_PROXY */
1544 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_RSC */
1545 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L2MISS */
1546 nla_total_size(sizeof(__u8
)) + /* IFLA_VXLAN_L3MISS */
1547 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_AGEING */
1548 nla_total_size(sizeof(__u32
)) + /* IFLA_VXLAN_LIMIT */
1549 nla_total_size(sizeof(struct ifla_vxlan_port_range
)) +
1553 static int vxlan_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1555 const struct vxlan_dev
*vxlan
= netdev_priv(dev
);
1556 struct ifla_vxlan_port_range ports
= {
1557 .low
= htons(vxlan
->port_min
),
1558 .high
= htons(vxlan
->port_max
),
1561 if (nla_put_u32(skb
, IFLA_VXLAN_ID
, vxlan
->vni
))
1562 goto nla_put_failure
;
1564 if (vxlan
->gaddr
&& nla_put_be32(skb
, IFLA_VXLAN_GROUP
, vxlan
->gaddr
))
1565 goto nla_put_failure
;
1567 if (vxlan
->link
&& nla_put_u32(skb
, IFLA_VXLAN_LINK
, vxlan
->link
))
1568 goto nla_put_failure
;
1570 if (vxlan
->saddr
&& nla_put_be32(skb
, IFLA_VXLAN_LOCAL
, vxlan
->saddr
))
1571 goto nla_put_failure
;
1573 if (nla_put_u8(skb
, IFLA_VXLAN_TTL
, vxlan
->ttl
) ||
1574 nla_put_u8(skb
, IFLA_VXLAN_TOS
, vxlan
->tos
) ||
1575 nla_put_u8(skb
, IFLA_VXLAN_LEARNING
,
1576 !!(vxlan
->flags
& VXLAN_F_LEARN
)) ||
1577 nla_put_u8(skb
, IFLA_VXLAN_PROXY
,
1578 !!(vxlan
->flags
& VXLAN_F_PROXY
)) ||
1579 nla_put_u8(skb
, IFLA_VXLAN_RSC
, !!(vxlan
->flags
& VXLAN_F_RSC
)) ||
1580 nla_put_u8(skb
, IFLA_VXLAN_L2MISS
,
1581 !!(vxlan
->flags
& VXLAN_F_L2MISS
)) ||
1582 nla_put_u8(skb
, IFLA_VXLAN_L3MISS
,
1583 !!(vxlan
->flags
& VXLAN_F_L3MISS
)) ||
1584 nla_put_u32(skb
, IFLA_VXLAN_AGEING
, vxlan
->age_interval
) ||
1585 nla_put_u32(skb
, IFLA_VXLAN_LIMIT
, vxlan
->addrmax
))
1586 goto nla_put_failure
;
1588 if (nla_put(skb
, IFLA_VXLAN_PORT_RANGE
, sizeof(ports
), &ports
))
1589 goto nla_put_failure
;
1597 static struct rtnl_link_ops vxlan_link_ops __read_mostly
= {
1599 .maxtype
= IFLA_VXLAN_MAX
,
1600 .policy
= vxlan_policy
,
1601 .priv_size
= sizeof(struct vxlan_dev
),
1602 .setup
= vxlan_setup
,
1603 .validate
= vxlan_validate
,
1604 .newlink
= vxlan_newlink
,
1605 .dellink
= vxlan_dellink
,
1606 .get_size
= vxlan_get_size
,
1607 .fill_info
= vxlan_fill_info
,
1610 static __net_init
int vxlan_init_net(struct net
*net
)
1612 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1614 struct sockaddr_in vxlan_addr
= {
1615 .sin_family
= AF_INET
,
1616 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1621 /* Create UDP socket for encapsulation receive. */
1622 rc
= sock_create_kern(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &vn
->sock
);
1624 pr_debug("UDP socket create failed\n");
1627 /* Put in proper namespace */
1629 sk_change_net(sk
, net
);
1631 vxlan_addr
.sin_port
= htons(vxlan_port
);
1633 rc
= kernel_bind(vn
->sock
, (struct sockaddr
*) &vxlan_addr
,
1634 sizeof(vxlan_addr
));
1636 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1637 &vxlan_addr
.sin_addr
, ntohs(vxlan_addr
.sin_port
), rc
);
1638 sk_release_kernel(sk
);
1643 /* Disable multicast loopback */
1644 inet_sk(sk
)->mc_loop
= 0;
1646 /* Mark socket as an encapsulation socket. */
1647 udp_sk(sk
)->encap_type
= 1;
1648 udp_sk(sk
)->encap_rcv
= vxlan_udp_encap_recv
;
1651 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
1652 INIT_HLIST_HEAD(&vn
->vni_list
[h
]);
1657 static __net_exit
void vxlan_exit_net(struct net
*net
)
1659 struct vxlan_net
*vn
= net_generic(net
, vxlan_net_id
);
1660 struct vxlan_dev
*vxlan
;
1664 for (h
= 0; h
< VNI_HASH_SIZE
; ++h
)
1665 hlist_for_each_entry(vxlan
, &vn
->vni_list
[h
], hlist
)
1666 dev_close(vxlan
->dev
);
1670 sk_release_kernel(vn
->sock
->sk
);
1675 static struct pernet_operations vxlan_net_ops
= {
1676 .init
= vxlan_init_net
,
1677 .exit
= vxlan_exit_net
,
1678 .id
= &vxlan_net_id
,
1679 .size
= sizeof(struct vxlan_net
),
1682 static int __init
vxlan_init_module(void)
1686 get_random_bytes(&vxlan_salt
, sizeof(vxlan_salt
));
1688 rc
= register_pernet_device(&vxlan_net_ops
);
1692 rc
= rtnl_link_register(&vxlan_link_ops
);
1699 unregister_pernet_device(&vxlan_net_ops
);
1703 module_init(vxlan_init_module
);
1705 static void __exit
vxlan_cleanup_module(void)
1707 rtnl_link_unregister(&vxlan_link_ops
);
1708 unregister_pernet_device(&vxlan_net_ops
);
1711 module_exit(vxlan_cleanup_module
);
1713 MODULE_LICENSE("GPL");
1714 MODULE_VERSION(VXLAN_VERSION
);
1715 MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
1716 MODULE_ALIAS_RTNL_LINK("vxlan");