History

vxlan: add ipv6 support

This patch adds IPv6 support to vxlan device, as the new version RFC already mentions it: http://tools.ietf.org/html/draft-mahalingam-dutt-dcops-vxlan-03

Cong Wang committed 13 years ago

Commit e4c7ed41 parent caf92bc4 files · +622 −148Browse files

Unified · Split

4 files
  1. drivers/net/vxlan.c +618 −146
  2. include/net/vxlan.h +1 −1
  3. include/uapi/linux/if_link.h +2 −0
  4. net/openvswitch/vport-vxlan.c +1 −1
drivers/net/vxlan.c+618−146
@@ -6,9 +6,6 @@ · 5 unmodified lines
66 * This program is free software; you can redistribute it and/or modify
77 * it under the terms of the GNU General Public License version 2 as
88 * published by the Free Software Foundation.
9− *
10− * TODO
11− * - IPv6 (not in RFC)
129 */
1310
1411 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -43,6 +40,11 @@ · 28 unmodified lines
4340 #include <net/net_namespace.h>
4441 #include <net/netns/generic.h>
4542 #include <net/vxlan.h>
43+#if IS_ENABLED(CONFIG_IPV6)
44+#include <net/ipv6.h>
45+#include <net/addrconf.h>
46+#include <net/ip6_tunnel.h>
47+#endif
4648
4749 #define VXLAN_VERSION "0.1"
4850
@@ -59,6 +61,8 @@ · 10 unmodified lines
5961 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
6062 /* IP header + UDP + VXLAN + Ethernet header */
6163 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
64+/* IPv6 header + UDP + VXLAN + Ethernet header */
65+#define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
6266 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
6367
6468 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
@@ -92,8 +96,14 @@ struct vxlan_net { · 27 unmodified lines
9296 spinlock_t sock_lock;
9397 };
9498
99+union vxlan_addr {
100+ struct sockaddr_in sin;
101+ struct sockaddr_in6 sin6;
102+ struct sockaddr sa;
103+};
104+
95105 struct vxlan_rdst {
96− __be32 remote_ip;
106+ union vxlan_addr remote_ip;
97107 __be16 remote_port;
98108 u32 remote_vni;
99109 u32 remote_ifindex;
@@ -120,7 +130,7 @@ struct vxlan_dev { · 20 unmodified lines
120130 struct vxlan_sock *vn_sock; /* listening socket */
121131 struct net_device *dev;
122132 struct vxlan_rdst default_dst; /* default destination */
123− __be32 saddr; /* source address */
133+ union vxlan_addr saddr; /* source address */
124134 __be16 dst_port;
125135 __u16 port_min; /* source port range */
126136 __u16 port_max;
@@ -146,6 +156,7 @@ struct vxlan_dev { · 19 unmodified lines
146156 #define VXLAN_F_RSC 0x04
147157 #define VXLAN_F_L2MISS 0x08
148158 #define VXLAN_F_L3MISS 0x10
159+#define VXLAN_F_IPV6 0x20 /* internal flag */
149160
150161 /* salt for hash table */
151162 static u32 vxlan_salt __read_mostly;
@@ -153,6 +164,96 @@ static struct workqueue_struct *vxlan_wq; · 1 unmodified line
153164
154165 static void vxlan_sock_work(struct work_struct *work);
155166
167+#if IS_ENABLED(CONFIG_IPV6)
168+static inline
169+bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
170+{
171+ if (a->sa.sa_family != b->sa.sa_family)
172+ return false;
173+ if (a->sa.sa_family == AF_INET6)
174+ return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
175+ else
176+ return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
177+}
178+
179+static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
180+{
181+ if (ipa->sa.sa_family == AF_INET6)
182+ return ipv6_addr_any(&ipa->sin6.sin6_addr);
183+ else
184+ return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
185+}
186+
187+static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
188+{
189+ if (ipa->sa.sa_family == AF_INET6)
190+ return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
191+ else
192+ return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
193+}
194+
195+static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
196+{
197+ if (nla_len(nla) >= sizeof(struct in6_addr)) {
198+ nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
199+ ip->sa.sa_family = AF_INET6;
200+ return 0;
201+ } else if (nla_len(nla) >= sizeof(__be32)) {
202+ ip->sin.sin_addr.s_addr = nla_get_be32(nla);
203+ ip->sa.sa_family = AF_INET;
204+ return 0;
205+ } else {
206+ return -EAFNOSUPPORT;
207+ }
208+}
209+
210+static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
211+ const union vxlan_addr *ip)
212+{
213+ if (ip->sa.sa_family == AF_INET6)
214+ return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
215+ else
216+ return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
217+}
218+
219+#else /* !CONFIG_IPV6 */
220+
221+static inline
222+bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
223+{
224+ return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
225+}
226+
227+static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
228+{
229+ return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
230+}
231+
232+static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
233+{
234+ return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
235+}
236+
237+static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
238+{
239+ if (nla_len(nla) >= sizeof(struct in6_addr)) {
240+ return -EAFNOSUPPORT;
241+ } else if (nla_len(nla) >= sizeof(__be32)) {
242+ ip->sin.sin_addr.s_addr = nla_get_be32(nla);
243+ ip->sa.sa_family = AF_INET;
244+ return 0;
245+ } else {
246+ return -EAFNOSUPPORT;
247+ }
248+}
249+
250+static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
251+ const union vxlan_addr *ip)
252+{
253+ return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
254+}
255+#endif
256+
156257 /* Virtual Network hash table head */
157258 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
158259 {
@@ -239,7 +340,7 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, · 80 unmodified lines
239340
240341 if (type == RTM_GETNEIGH) {
241342 ndm->ndm_family = AF_INET;
242− send_ip = rdst->remote_ip != htonl(INADDR_ANY);
343+ send_ip = !vxlan_addr_any(&rdst->remote_ip);
243344 send_eth = !is_zero_ether_addr(fdb->eth_addr);
244345 } else
245346 ndm->ndm_family = AF_BRIDGE;
@@ -251,7 +352,7 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, · 5 unmodified lines
251352 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
252353 goto nla_put_failure;
253354
254− if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
355+ if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
255356 goto nla_put_failure;
256357
257358 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
@@ -283,7 +384,7 @@ static inline size_t vxlan_nlmsg_size(void) · 25 unmodified lines
283384 {
284385 return NLMSG_ALIGN(sizeof(struct ndmsg))
285386 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
286− + nla_total_size(sizeof(__be32)) /* NDA_DST */
387+ + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
287388 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
288389 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
289390 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
@@ -317,14 +418,14 @@ errout: · 27 unmodified lines
317418 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
318419 }
319420
320−static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
421+static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
321422 {
322423 struct vxlan_dev *vxlan = netdev_priv(dev);
323424 struct vxlan_fdb f = {
324425 .state = NUD_STALE,
325426 };
326427 struct vxlan_rdst remote = {
327− .remote_ip = ipa, /* goes to NDA_DST */
428+ .remote_ip = *ipa, /* goes to NDA_DST */
328429 .remote_vni = VXLAN_N_VID,
329430 };
330431
@@ -397,13 +498,13 @@ static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, · 66 unmodified lines
397498
398499 /* caller should hold vxlan->hash_lock */
399500 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
400− __be32 ip, __be16 port,
501+ union vxlan_addr *ip, __be16 port,
401502 __u32 vni, __u32 ifindex)
402503 {
403504 struct vxlan_rdst *rd;
404505
405506 list_for_each_entry(rd, &f->remotes, list) {
406− if (rd->remote_ip == ip &&
507+ if (vxlan_addr_equal(&rd->remote_ip, ip) &&
407508 rd->remote_port == port &&
408509 rd->remote_vni == vni &&
409510 rd->remote_ifindex == ifindex)
@@ -415,7 +516,7 @@ static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, · 5 unmodified lines
415516
416517 /* Replace destination of unicast mac */
417518 static int vxlan_fdb_replace(struct vxlan_fdb *f,
418− __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
519+ union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
419520 {
420521 struct vxlan_rdst *rd;
421522
@@ -426,7 +527,7 @@ static int vxlan_fdb_replace(struct vxlan_fdb *f, · 4 unmodified lines
426527 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
427528 if (!rd)
428529 return 0;
429− rd->remote_ip = ip;
530+ rd->remote_ip = *ip;
430531 rd->remote_port = port;
431532 rd->remote_vni = vni;
432533 rd->remote_ifindex = ifindex;
@@ -435,7 +536,7 @@ static int vxlan_fdb_replace(struct vxlan_fdb *f, · 2 unmodified lines
435536
436537 /* Add/update destinations for multicast */
437538 static int vxlan_fdb_append(struct vxlan_fdb *f,
438− __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
539+ union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
439540 {
440541 struct vxlan_rdst *rd;
441542
@@ -446,7 +547,7 @@ static int vxlan_fdb_append(struct vxlan_fdb *f, · 4 unmodified lines
446547 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
447548 if (rd == NULL)
448549 return -ENOBUFS;
449− rd->remote_ip = ip;
550+ rd->remote_ip = *ip;
450551 rd->remote_port = port;
451552 rd->remote_vni = vni;
452553 rd->remote_ifindex = ifindex;
@@ -458,7 +559,7 @@ static int vxlan_fdb_append(struct vxlan_fdb *f, · 5 unmodified lines
458559
459560 /* Add new entry to forwarding table -- assumes lock held */
460561 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
461− const u8 *mac, __be32 ip,
562+ const u8 *mac, union vxlan_addr *ip,
462563 __u16 state, __u16 flags,
463564 __be16 port, __u32 vni, __u32 ifindex,
464565 __u8 ndm_flags)
@@ -517,7 +618,7 @@ static int vxlan_fdb_create(struct vxlan_dev *vxlan, · 52 unmodified lines
517618 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
518619 return -EOPNOTSUPP;
519620
520− netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
621+ netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
521622 f = kmalloc(sizeof(*f), GFP_ATOMIC);
522623 if (!f)
523624 return -ENOMEM;
@@ -565,17 +666,26 @@ static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) · 41 unmodified lines
565666 }
566667
567668 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
568− __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
669+ union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
569670 {
570671 struct net *net = dev_net(vxlan->dev);
672+ int err;
571673
572674 if (tb[NDA_DST]) {
573− if (nla_len(tb[NDA_DST]) != sizeof(__be32))
574− return -EAFNOSUPPORT;
575−
576− *ip = nla_get_be32(tb[NDA_DST]);
675+ err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
676+ if (err)
677+ return err;
577678 } else {
578− *ip = htonl(INADDR_ANY);
679+ union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
680+ if (remote->sa.sa_family == AF_INET) {
681+ ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
682+ ip->sa.sa_family = AF_INET;
683+#if IS_ENABLED(CONFIG_IPV6)
684+ } else {
685+ ip->sin6.sin6_addr = in6addr_any;
686+ ip->sa.sa_family = AF_INET6;
687+#endif
688+ }
579689 }
580690
581691 if (tb[NDA_PORT]) {
@@ -618,7 +728,7 @@ static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], · 36 unmodified lines
618728 {
619729 struct vxlan_dev *vxlan = netdev_priv(dev);
620730 /* struct net *net = dev_net(vxlan->dev); */
621− __be32 ip;
731+ union vxlan_addr ip;
622732 __be16 port;
623733 u32 vni, ifindex;
624734 int err;
@@ -637,7 +747,7 @@ static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], · 12 unmodified lines
637747 return err;
638748
639749 spin_lock_bh(&vxlan->hash_lock);
640− err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
750+ err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
641751 port, vni, ifindex, ndm->ndm_flags);
642752 spin_unlock_bh(&vxlan->hash_lock);
643753
@@ -652,7 +762,7 @@ static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], · 8 unmodified lines
652762 struct vxlan_dev *vxlan = netdev_priv(dev);
653763 struct vxlan_fdb *f;
654764 struct vxlan_rdst *rd = NULL;
655− __be32 ip;
765+ union vxlan_addr ip;
656766 __be16 port;
657767 u32 vni, ifindex;
658768 int err;
@@ -668,8 +778,8 @@ static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], · 9 unmodified lines
668778 if (!f)
669779 goto out;
670780
671− if (ip != htonl(INADDR_ANY)) {
672− rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
781+ if (!vxlan_addr_any(&ip)) {
782+ rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
673783 if (!rd)
674784 goto out;
675785 }
@@ -732,7 +842,7 @@ out: · 56 unmodified lines
732842 * Return true if packet is bogus and should be droppped.
733843 */
734844 static bool vxlan_snoop(struct net_device *dev,
735− __be32 src_ip, const u8 *src_mac)
845+ union vxlan_addr *src_ip, const u8 *src_mac)
736846 {
737847 struct vxlan_dev *vxlan = netdev_priv(dev);
738848 struct vxlan_fdb *f;
@@ -741,7 +851,7 @@ static bool vxlan_snoop(struct net_device *dev, · 2 unmodified lines
741851 if (likely(f)) {
742852 struct vxlan_rdst *rdst = first_remote_rcu(f);
743853
744− if (likely(rdst->remote_ip == src_ip))
854+ if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
745855 return false;
746856
747857 /* Don't migrate static entries, drop packets */
@@ -750,10 +860,10 @@ static bool vxlan_snoop(struct net_device *dev, · 2 unmodified lines
750860
751861 if (net_ratelimit())
752862 netdev_info(dev,
753− "%pM migrated from %pI4 to %pI4\n",
863+ "%pM migrated from %pIS to %pIS\n",
754864 src_mac, &rdst->remote_ip, &src_ip);
755865
756− rdst->remote_ip = src_ip;
866+ rdst->remote_ip = *src_ip;
757867 f->updated = jiffies;
758868 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
759869 } else {
@@ -775,7 +885,7 @@ static bool vxlan_snoop(struct net_device *dev, · 15 unmodified lines
775885 }
776886
777887 /* See if multicast group is already in use by other ID */
778−static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
888+static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
779889 {
780890 struct vxlan_dev *vxlan;
781891
@@ -783,7 +893,8 @@ static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip) · 1 unmodified line
783893 if (!netif_running(vxlan->dev))
784894 continue;
785895
786− if (vxlan->default_dst.remote_ip == remote_ip)
896+ if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
897+ remote_ip))
787898 return true;
788899 }
789900
@@ -819,13 +930,23 @@ static void vxlan_igmp_join(struct work_struct *work) · 29 unmodified lines
819930 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
820931 struct vxlan_sock *vs = vxlan->vn_sock;
821932 struct sock *sk = vs->sock->sk;
822− struct ip_mreqn mreq = {
823− .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
824− .imr_ifindex = vxlan->default_dst.remote_ifindex,
825− };
933+ union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
934+ int ifindex = vxlan->default_dst.remote_ifindex;
826935
827936 lock_sock(sk);
828− ip_mc_join_group(sk, &mreq);
937+ if (ip->sa.sa_family == AF_INET) {
938+ struct ip_mreqn mreq = {
939+ .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
940+ .imr_ifindex = ifindex,
941+ };
942+
943+ ip_mc_join_group(sk, &mreq);
944+#if IS_ENABLED(CONFIG_IPV6)
945+ } else {
946+ ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
947+ &ip->sin6.sin6_addr);
948+#endif
949+ }
829950 release_sock(sk);
830951
831952 vxlan_sock_release(vs);
@@ -838,13 +959,24 @@ static void vxlan_igmp_leave(struct work_struct *work) · 6 unmodified lines
838959 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
839960 struct vxlan_sock *vs = vxlan->vn_sock;
840961 struct sock *sk = vs->sock->sk;
841− struct ip_mreqn mreq = {
842− .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
843− .imr_ifindex = vxlan->default_dst.remote_ifindex,
844− };
962+ union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
963+ int ifindex = vxlan->default_dst.remote_ifindex;
845964
846965 lock_sock(sk);
847− ip_mc_leave_group(sk, &mreq);
966+ if (ip->sa.sa_family == AF_INET) {
967+ struct ip_mreqn mreq = {
968+ .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
969+ .imr_ifindex = ifindex,
970+ };
971+
972+ ip_mc_leave_group(sk, &mreq);
973+#if IS_ENABLED(CONFIG_IPV6)
974+ } else {
975+ ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
976+ &ip->sin6.sin6_addr);
977+#endif
978+ }
979+
848980 release_sock(sk);
849981
850982 vxlan_sock_release(vs);
@@ -896,11 +1028,14 @@ error: · 45 unmodified lines
8961028 static void vxlan_rcv(struct vxlan_sock *vs,
8971029 struct sk_buff *skb, __be32 vx_vni)
8981030 {
899− struct iphdr *oip;
1031+ struct iphdr *oip = NULL;
1032+ struct ipv6hdr *oip6 = NULL;
9001033 struct vxlan_dev *vxlan;
9011034 struct pcpu_tstats *stats;
1035+ union vxlan_addr saddr;
9021036 __u32 vni;
903− int err;
1037+ int err = 0;
1038+ union vxlan_addr *remote_ip;
9041039
9051040 vni = ntohl(vx_vni) >> 8;
9061041 /* Is this VNI defined? */
@@ -908,6 +1043,7 @@ static void vxlan_rcv(struct vxlan_sock *vs, · 1 unmodified line
9081043 if (!vxlan)
9091044 goto drop;
9101045
1046+ remote_ip = &vxlan->default_dst.remote_ip;
9111047 skb_reset_mac_header(skb);
9121048 skb->protocol = eth_type_trans(skb, vxlan->dev);
9131049
@@ -917,9 +1053,20 @@ static void vxlan_rcv(struct vxlan_sock *vs, · 3 unmodified lines
9171053 goto drop;
9181054
9191055 /* Re-examine inner Ethernet packet */
920− oip = ip_hdr(skb);
1056+ if (remote_ip->sa.sa_family == AF_INET) {
1057+ oip = ip_hdr(skb);
1058+ saddr.sin.sin_addr.s_addr = oip->saddr;
1059+ saddr.sa.sa_family = AF_INET;
1060+#if IS_ENABLED(CONFIG_IPV6)
1061+ } else {
1062+ oip6 = ipv6_hdr(skb);
1063+ saddr.sin6.sin6_addr = oip6->saddr;
1064+ saddr.sa.sa_family = AF_INET6;
1065+#endif
1066+ }
1067+
9211068 if ((vxlan->flags & VXLAN_F_LEARN) &&
922− vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
1069+ vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
9231070 goto drop;
9241071
9251072 skb_reset_network_header(skb);
@@ -935,11 +1082,20 @@ static void vxlan_rcv(struct vxlan_sock *vs, · 9 unmodified lines
9351082
9361083 skb->encapsulation = 0;
9371084
938− err = IP_ECN_decapsulate(oip, skb);
1085+ if (oip6)
1086+ err = IP6_ECN_decapsulate(oip6, skb);
1087+ if (oip)
1088+ err = IP_ECN_decapsulate(oip, skb);
1089+
9391090 if (unlikely(err)) {
940− if (log_ecn_error)
941− net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
942− &oip->saddr, oip->tos);
1091+ if (log_ecn_error) {
1092+ if (oip6)
1093+ net_info_ratelimited("non-ECT from %pI6\n",
1094+ &oip6->saddr);
1095+ if (oip)
1096+ net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1097+ &oip->saddr, oip->tos);
1098+ }
9431099 if (err > 1) {
9441100 ++vxlan->dev->stats.rx_frame_errors;
9451101 ++vxlan->dev->stats.rx_errors;
@@ -1009,7 +1165,7 @@ static int arp_reduce(struct net_device *dev, struct sk_buff *skb) · 63 unmodified lines
10091165 }
10101166
10111167 f = vxlan_find_mac(vxlan, n->ha);
1012− if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
1168+ if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
10131169 /* bridge-local neighbor */
10141170 neigh_release(n);
10151171 goto out;
@@ -1027,8 +1183,14 @@ static int arp_reduce(struct net_device *dev, struct sk_buff *skb) · 11 unmodified lines
10271183
10281184 if (netif_rx_ni(reply) == NET_RX_DROP)
10291185 dev->stats.rx_dropped++;
1030− } else if (vxlan->flags & VXLAN_F_L3MISS)
1031− vxlan_ip_miss(dev, tip);
1186+ } else if (vxlan->flags & VXLAN_F_L3MISS) {
1187+ union vxlan_addr ipa = {
1188+ .sin.sin_addr.s_addr = tip,
1189+ .sa.sa_family = AF_INET,
1190+ };
1191+
1192+ vxlan_ip_miss(dev, &ipa);
1193+ }
10321194 out:
10331195 consume_skb(skb);
10341196 return NETDEV_TX_OK;
@@ -1050,6 +1212,16 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) · 15 unmodified lines
10501212 return false;
10511213 pip = ip_hdr(skb);
10521214 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1215+ if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1216+ union vxlan_addr ipa = {
1217+ .sin.sin_addr.s_addr = pip->daddr,
1218+ .sa.sa_family = AF_INET,
1219+ };
1220+
1221+ vxlan_ip_miss(dev, &ipa);
1222+ return false;
1223+ }
1224+
10531225 break;
10541226 default:
10551227 return false;
@@ -1066,8 +1238,8 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) · 10 unmodified lines
10661238 }
10671239 neigh_release(n);
10681240 return diff;
1069− } else if (vxlan->flags & VXLAN_F_L3MISS)
1070− vxlan_ip_miss(dev, pip->daddr);
1241+ }
1242+
10711243 return false;
10721244 }
10731245
@@ -1118,6 +1290,102 @@ static int handle_offloads(struct sk_buff *skb) · 44 unmodified lines
11181290 return 0;
11191291 }
11201292
1293+#if IS_ENABLED(CONFIG_IPV6)
1294+static int vxlan6_xmit_skb(struct net *net, struct vxlan_sock *vs,
1295+ struct dst_entry *dst, struct sk_buff *skb,
1296+ struct net_device *dev, struct in6_addr *saddr,
1297+ struct in6_addr *daddr, __u8 prio, __u8 ttl,
1298+ __be16 src_port, __be16 dst_port, __be32 vni)
1299+{
1300+ struct ipv6hdr *ip6h;
1301+ struct vxlanhdr *vxh;
1302+ struct udphdr *uh;
1303+ int min_headroom;
1304+ int err;
1305+
1306+ if (!skb->encapsulation) {
1307+ skb_reset_inner_headers(skb);
1308+ skb->encapsulation = 1;
1309+ }
1310+
1311+ min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1312+ + VXLAN_HLEN + sizeof(struct ipv6hdr)
1313+ + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1314+
1315+ /* Need space for new headers (invalidates iph ptr) */
1316+ err = skb_cow_head(skb, min_headroom);
1317+ if (unlikely(err))
1318+ return err;
1319+
1320+ if (vlan_tx_tag_present(skb)) {
1321+ if (WARN_ON(!__vlan_put_tag(skb,
1322+ skb->vlan_proto,
1323+ vlan_tx_tag_get(skb))))
1324+ return -ENOMEM;
1325+
1326+ skb->vlan_tci = 0;
1327+ }
1328+
1329+ vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1330+ vxh->vx_flags = htonl(VXLAN_FLAGS);
1331+ vxh->vx_vni = vni;
1332+
1333+ __skb_push(skb, sizeof(*uh));
1334+ skb_reset_transport_header(skb);
1335+ uh = udp_hdr(skb);
1336+
1337+ uh->dest = dst_port;
1338+ uh->source = src_port;
1339+
1340+ uh->len = htons(skb->len);
1341+ uh->check = 0;
1342+
1343+ memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1344+ IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1345+ IPSKB_REROUTED);
1346+ skb_dst_drop(skb);
1347+ skb_dst_set(skb, dst);
1348+
1349+ if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1350+ __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1351+ skb->ip_summed = CHECKSUM_UNNECESSARY;
1352+ uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1353+ IPPROTO_UDP, csum);
1354+ if (uh->check == 0)
1355+ uh->check = CSUM_MANGLED_0;
1356+ } else {
1357+ skb->ip_summed = CHECKSUM_PARTIAL;
1358+ skb->csum_start = skb_transport_header(skb) - skb->head;
1359+ skb->csum_offset = offsetof(struct udphdr, check);
1360+ uh->check = ~csum_ipv6_magic(saddr, daddr,
1361+ skb->len, IPPROTO_UDP, 0);
1362+ }
1363+
1364+ __skb_push(skb, sizeof(*ip6h));
1365+ skb_reset_network_header(skb);
1366+ ip6h = ipv6_hdr(skb);
1367+ ip6h->version = 6;
1368+ ip6h->priority = prio;
1369+ ip6h->flow_lbl[0] = 0;
1370+ ip6h->flow_lbl[1] = 0;
1371+ ip6h->flow_lbl[2] = 0;
1372+ ip6h->payload_len = htons(skb->len);
1373+ ip6h->nexthdr = IPPROTO_UDP;
1374+ ip6h->hop_limit = ttl;
1375+ ip6h->daddr = *daddr;
1376+ ip6h->saddr = *saddr;
1377+
1378+ vxlan_set_owner(vs->sock->sk, skb);
1379+
1380+ err = handle_offloads(skb);
1381+ if (err)
1382+ return err;
1383+
1384+ ip6tunnel_xmit(skb, dev);
1385+ return 0;
1386+}
1387+#endif
1388+
11211389 int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
11221390 struct rtable *rt, struct sk_buff *skb,
11231391 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
@@ -1182,15 +1450,26 @@ static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, · 58 unmodified lines
11821450 {
11831451 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
11841452 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1453+ union vxlan_addr loopback;
1454+ union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
11851455
11861456 skb->pkt_type = PACKET_HOST;
11871457 skb->encapsulation = 0;
11881458 skb->dev = dst_vxlan->dev;
11891459 __skb_pull(skb, skb_network_offset(skb));
11901460
1461+ if (remote_ip->sa.sa_family == AF_INET) {
1462+ loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1463+ loopback.sa.sa_family = AF_INET;
1464+#if IS_ENABLED(CONFIG_IPV6)
1465+ } else {
1466+ loopback.sin6.sin6_addr = in6addr_loopback;
1467+ loopback.sa.sa_family = AF_INET6;
1468+#endif
1469+ }
1470+
11911471 if (dst_vxlan->flags & VXLAN_F_LEARN)
1192− vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1193− eth_hdr(skb)->h_source);
1472+ vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
11941473
11951474 u64_stats_update_begin(&tx_stats->syncp);
11961475 tx_stats->tx_packets++;
@@ -1211,11 +1490,11 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, · 14 unmodified lines
12111490 struct vxlan_rdst *rdst, bool did_rsc)
12121491 {
12131492 struct vxlan_dev *vxlan = netdev_priv(dev);
1214− struct rtable *rt;
1493+ struct rtable *rt = NULL;
12151494 const struct iphdr *old_iph;
12161495 struct flowi4 fl4;
1217− __be32 dst;
1218− __be16 src_port, dst_port;
1496+ union vxlan_addr *dst;
1497+ __be16 src_port = 0, dst_port;
12191498 u32 vni;
12201499 __be16 df = 0;
12211500 __u8 tos, ttl;
@@ -1223,9 +1502,9 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, · 1 unmodified line
12231502
12241503 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
12251504 vni = rdst->remote_vni;
1226− dst = rdst->remote_ip;
1505+ dst = &rdst->remote_ip;
12271506
1228− if (!dst) {
1507+ if (vxlan_addr_any(dst)) {
12291508 if (did_rsc) {
12301509 /* short-circuited back to local bridge */
12311510 vxlan_encap_bypass(skb, vxlan, vxlan);
@@ -1237,7 +1516,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, · 5 unmodified lines
12371516 old_iph = ip_hdr(skb);
12381517
12391518 ttl = vxlan->ttl;
1240− if (!ttl && IN_MULTICAST(ntohl(dst)))
1519+ if (!ttl && vxlan_addr_multicast(dst))
12411520 ttl = 1;
12421521
12431522 tos = vxlan->tos;
@@ -1246,48 +1525,101 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, · 2 unmodified lines
12461525
12471526 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
12481527
1249− memset(&fl4, 0, sizeof(fl4));
1250− fl4.flowi4_oif = rdst->remote_ifindex;
1251− fl4.flowi4_tos = RT_TOS(tos);
1252− fl4.daddr = dst;
1253− fl4.saddr = vxlan->saddr;
1254−
1255− rt = ip_route_output_key(dev_net(dev), &fl4);
1256− if (IS_ERR(rt)) {
1257− netdev_dbg(dev, "no route to %pI4\n", &dst);
1258− dev->stats.tx_carrier_errors++;
1259− goto tx_error;
1260− }
1528+ if (dst->sa.sa_family == AF_INET) {
1529+ memset(&fl4, 0, sizeof(fl4));
1530+ fl4.flowi4_oif = rdst->remote_ifindex;
1531+ fl4.flowi4_tos = RT_TOS(tos);
1532+ fl4.daddr = dst->sin.sin_addr.s_addr;
1533+ fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1534+
1535+ rt = ip_route_output_key(dev_net(dev), &fl4);
1536+ if (IS_ERR(rt)) {
1537+ netdev_dbg(dev, "no route to %pI4\n",
1538+ &dst->sin.sin_addr.s_addr);
1539+ dev->stats.tx_carrier_errors++;
1540+ goto tx_error;
1541+ }
12611542
1262− if (rt->dst.dev == dev) {
1263− netdev_dbg(dev, "circular route to %pI4\n", &dst);
1264− dev->stats.collisions++;
1265− goto rt_tx_error;
1266− }
1543+ if (rt->dst.dev == dev) {
1544+ netdev_dbg(dev, "circular route to %pI4\n",
1545+ &dst->sin.sin_addr.s_addr);
1546+ dev->stats.collisions++;
1547+ goto tx_error;
1548+ }
1549+
1550+ /* Bypass encapsulation if the destination is local */
1551+ if (rt->rt_flags & RTCF_LOCAL &&
1552+ !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1553+ struct vxlan_dev *dst_vxlan;
1554+
1555+ ip_rt_put(rt);
1556+ dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1557+ if (!dst_vxlan)
1558+ goto tx_error;
1559+ vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1560+ return;
1561+ }
12671562
1268− /* Bypass encapsulation if the destination is local */
1269− if (rt->rt_flags & RTCF_LOCAL &&
1270− !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1271− struct vxlan_dev *dst_vxlan;
1563+ tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1564+ ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
12721565
1273− ip_rt_put(rt);
1274− dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1275− if (!dst_vxlan)
1566+ err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1567+ fl4.saddr, dst->sin.sin_addr.s_addr,
1568+ tos, ttl, df, src_port, dst_port,
1569+ htonl(vni << 8));
1570+
1571+ if (err < 0)
1572+ goto rt_tx_error;
1573+ iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1574+#if IS_ENABLED(CONFIG_IPV6)
1575+ } else {
1576+ struct sock *sk = vxlan->vn_sock->sock->sk;
1577+ struct dst_entry *ndst;
1578+ struct flowi6 fl6;
1579+ u32 flags;
1580+
1581+ memset(&fl6, 0, sizeof(fl6));
1582+ fl6.flowi6_oif = rdst->remote_ifindex;
1583+ fl6.daddr = dst->sin6.sin6_addr;
1584+ fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1585+ fl6.flowi6_proto = skb->protocol;
1586+
1587+ if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1588+ netdev_dbg(dev, "no route to %pI6\n",
1589+ &dst->sin6.sin6_addr);
1590+ dev->stats.tx_carrier_errors++;
12761591 goto tx_error;
1277− vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1278− return;
1279− }
1592+ }
12801593
1281− tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1282− ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1594+ if (ndst->dev == dev) {
1595+ netdev_dbg(dev, "circular route to %pI6\n",
1596+ &dst->sin6.sin6_addr);
1597+ dst_release(ndst);
1598+ dev->stats.collisions++;
1599+ goto tx_error;
1600+ }
12831601
1284− err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1285− fl4.saddr, dst, tos, ttl, df,
1286− src_port, dst_port, htonl(vni << 8));
1602+ /* Bypass encapsulation if the destination is local */
1603+ flags = ((struct rt6_info *)ndst)->rt6i_flags;
1604+ if (flags & RTF_LOCAL &&
1605+ !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1606+ struct vxlan_dev *dst_vxlan;
1607+
1608+ dst_release(ndst);
1609+ dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1610+ if (!dst_vxlan)
1611+ goto tx_error;
1612+ vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1613+ return;
1614+ }
12871615
1288− if (err < 0)
1289− goto rt_tx_error;
1290− iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1616+ ttl = ttl ? : ip6_dst_hoplimit(ndst);
1617+
1618+ err = vxlan6_xmit_skb(dev_net(dev), vxlan->vn_sock, ndst, skb,
1619+ dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1620+ src_port, dst_port, htonl(vni << 8));
1621+#endif
1622+ }
12911623
12921624 return;
12931625
@@ -1464,8 +1796,8 @@ static int vxlan_open(struct net_device *dev) · 170 unmodified lines
14641796 if (!vs)
14651797 return -ENOTCONN;
14661798
1467− if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1468− vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1799+ if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1800+ vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
14691801 vxlan_sock_hold(vs);
14701802 dev_hold(dev);
14711803 queue_work(vxlan_wq, &vxlan->igmp_join);
@@ -1503,8 +1835,8 @@ static int vxlan_stop(struct net_device *dev) · 31 unmodified lines
15031835 struct vxlan_dev *vxlan = netdev_priv(dev);
15041836 struct vxlan_sock *vs = vxlan->vn_sock;
15051837
1506− if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1507− ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1838+ if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1839+ ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
15081840 vxlan_sock_hold(vs);
15091841 dev_hold(dev);
15101842 queue_work(vxlan_wq, &vxlan->igmp_leave);
@@ -1552,7 +1884,10 @@ static void vxlan_setup(struct net_device *dev) · 41 unmodified lines
15521884
15531885 eth_hw_addr_random(dev);
15541886 ether_setup(dev);
1555− dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1887+ if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
1888+ dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
1889+ else
1890+ dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
15561891
15571892 dev->netdev_ops = &vxlan_netdev_ops;
15581893 dev->destructor = free_netdev;
@@ -1597,8 +1932,10 @@ static void vxlan_setup(struct net_device *dev) · 38 unmodified lines
15971932 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
15981933 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
15991934 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1935+ [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
16001936 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
16011937 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1938+ [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
16021939 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
16031940 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
16041941 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
@@ -1669,58 +2006,132 @@ static void vxlan_del_work(struct work_struct *work) · 64 unmodified lines
16692006 kfree_rcu(vs, rcu);
16702007 }
16712008
1672−static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1673− vxlan_rcv_t *rcv, void *data)
2009+#if IS_ENABLED(CONFIG_IPV6)
2010+/* Create UDP socket for encapsulation receive. AF_INET6 socket
2011+ * could be used for both IPv4 and IPv6 communications, but
2012+ * users may set bindv6only=1.
2013+ */
2014+static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2015+{
2016+ struct sock *sk;
2017+ struct socket *sock;
2018+ struct sockaddr_in6 vxlan_addr = {
2019+ .sin6_family = AF_INET6,
2020+ .sin6_port = port,
2021+ };
2022+ int rc, val = 1;
2023+
2024+ rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2025+ if (rc < 0) {
2026+ pr_debug("UDPv6 socket create failed\n");
2027+ return rc;
2028+ }
2029+
2030+ /* Put in proper namespace */
2031+ sk = sock->sk;
2032+ sk_change_net(sk, net);
2033+
2034+ kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2035+ (char *)&val, sizeof(val));
2036+ rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2037+ sizeof(struct sockaddr_in6));
2038+ if (rc < 0) {
2039+ pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2040+ &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2041+ sk_release_kernel(sk);
2042+ return rc;
2043+ }
2044+ /* At this point, IPv6 module should have been loaded in
2045+ * sock_create_kern().
2046+ */
2047+ BUG_ON(!ipv6_stub);
2048+
2049+ *psock = sock;
2050+ /* Disable multicast loopback */
2051+ inet_sk(sk)->mc_loop = 0;
2052+ return 0;
2053+}
2054+
2055+#else
2056+
2057+static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
2058+{
2059+ return -EPFNOSUPPORT;
2060+}
2061+#endif
2062+
2063+static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
16742064 {
1675− struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1676− struct vxlan_sock *vs;
16772065 struct sock *sk;
2066+ struct socket *sock;
16782067 struct sockaddr_in vxlan_addr = {
16792068 .sin_family = AF_INET,
16802069 .sin_addr.s_addr = htonl(INADDR_ANY),
16812070 .sin_port = port,
16822071 };
16832072 int rc;
1684− unsigned int h;
1685−
1686− vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1687− if (!vs) {
1688− pr_debug("memory alocation failure\n");
1689− return ERR_PTR(-ENOMEM);
1690− }
1691−
1692− for (h = 0; h < VNI_HASH_SIZE; ++h)
1693− INIT_HLIST_HEAD(&vs->vni_list[h]);
1694−
1695− INIT_WORK(&vs->del_work, vxlan_del_work);
16962073
16972074 /* Create UDP socket for encapsulation receive. */
1698− rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
2075+ rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
16992076 if (rc < 0) {
17002077 pr_debug("UDP socket create failed\n");
1701− kfree(vs);
1702− return ERR_PTR(rc);
2078+ return rc;
17032079 }
17042080
17052081 /* Put in proper namespace */
1706− sk = vs->sock->sk;
2082+ sk = sock->sk;
17072083 sk_change_net(sk, net);
17082084
1709− rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
2085+ rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
17102086 sizeof(vxlan_addr));
17112087 if (rc < 0) {
17122088 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
17132089 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
17142090 sk_release_kernel(sk);
2091+ return rc;
2092+ }
2093+
2094+ *psock = sock;
2095+ /* Disable multicast loopback */
2096+ inet_sk(sk)->mc_loop = 0;
2097+ return 0;
2098+}
2099+
2100+/* Create new listen socket if needed */
2101+static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2102+ vxlan_rcv_t *rcv, void *data, bool ipv6)
2103+{
2104+ struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2105+ struct vxlan_sock *vs;
2106+ struct socket *sock;
2107+ struct sock *sk;
2108+ int rc = 0;
2109+ unsigned int h;
2110+
2111+ vs = kmalloc(sizeof(*vs), GFP_KERNEL);
2112+ if (!vs)
2113+ return ERR_PTR(-ENOMEM);
2114+
2115+ for (h = 0; h < VNI_HASH_SIZE; ++h)
2116+ INIT_HLIST_HEAD(&vs->vni_list[h]);
2117+
2118+ INIT_WORK(&vs->del_work, vxlan_del_work);
2119+
2120+ if (ipv6)
2121+ rc = create_v6_sock(net, port, &sock);
2122+ else
2123+ rc = create_v4_sock(net, port, &sock);
2124+ if (rc < 0) {
17152125 kfree(vs);
17162126 return ERR_PTR(rc);
17172127 }
2128+
2129+ vs->sock = sock;
2130+ sk = sock->sk;
17182131 atomic_set(&vs->refcnt, 1);
17192132 vs->rcv = rcv;
17202133 vs->data = data;
17212134
1722− /* Disable multicast loopback */
1723− inet_sk(sk)->mc_loop = 0;
17242135 spin_lock(&vn->sock_lock);
17252136 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
17262137 spin_unlock(&vn->sock_lock);
@@ -1728,18 +2139,24 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, · 1 unmodified line
17282139 /* Mark socket as an encapsulation socket. */
17292140 udp_sk(sk)->encap_type = 1;
17302141 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1731− udp_encap_enable();
2142+#if IS_ENABLED(CONFIG_IPV6)
2143+ if (ipv6)
2144+ ipv6_stub->udpv6_encap_enable();
2145+ else
2146+#endif
2147+ udp_encap_enable();
2148+
17322149 return vs;
17332150 }
17342151
17352152 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
17362153 vxlan_rcv_t *rcv, void *data,
1737− bool no_share)
2154+ bool no_share, bool ipv6)
17382155 {
17392156 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
17402157 struct vxlan_sock *vs;
17412158
1742− vs = vxlan_socket_create(net, port, rcv, data);
2159+ vs = vxlan_socket_create(net, port, rcv, data, ipv6);
17432160 if (!IS_ERR(vs))
17442161 return vs;
17452162
@@ -1772,7 +2189,7 @@ static void vxlan_sock_work(struct work_struct *work) · 26 unmodified lines
17722189 __be16 port = vxlan->dst_port;
17732190 struct vxlan_sock *nvs;
17742191
1775− nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
2192+ nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
17762193 spin_lock(&vn->sock_lock);
17772194 if (!IS_ERR(nvs))
17782195 vxlan_vs_add_dev(nvs, vxlan);
@@ -1789,6 +2206,7 @@ static int vxlan_newlink(struct net *net, struct net_device *dev, · 10 unmodified lines
17892206 struct vxlan_rdst *dst = &vxlan->default_dst;
17902207 __u32 vni;
17912208 int err;
2209+ bool use_ipv6 = false;
17922210
17932211 if (!data[IFLA_VXLAN_ID])
17942212 return -EINVAL;
@@ -1796,11 +2214,32 @@ static int vxlan_newlink(struct net *net, struct net_device *dev, · 1 unmodified line
17962214 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
17972215 dst->remote_vni = vni;
17982216
1799− if (data[IFLA_VXLAN_GROUP])
1800− dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2217+ if (data[IFLA_VXLAN_GROUP]) {
2218+ dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2219+ dst->remote_ip.sa.sa_family = AF_INET;
2220+ } else if (data[IFLA_VXLAN_GROUP6]) {
2221+ if (!IS_ENABLED(CONFIG_IPV6))
2222+ return -EPFNOSUPPORT;
2223+
2224+ nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2225+ sizeof(struct in6_addr));
2226+ dst->remote_ip.sa.sa_family = AF_INET6;
2227+ use_ipv6 = true;
2228+ }
18012229
1802− if (data[IFLA_VXLAN_LOCAL])
1803− vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2230+ if (data[IFLA_VXLAN_LOCAL]) {
2231+ vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2232+ vxlan->saddr.sa.sa_family = AF_INET;
2233+ } else if (data[IFLA_VXLAN_LOCAL6]) {
2234+ if (!IS_ENABLED(CONFIG_IPV6))
2235+ return -EPFNOSUPPORT;
2236+
2237+ /* TODO: respect scope id */
2238+ nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2239+ sizeof(struct in6_addr));
2240+ vxlan->saddr.sa.sa_family = AF_INET6;
2241+ use_ipv6 = true;
2242+ }
18042243
18052244 if (data[IFLA_VXLAN_LINK] &&
18062245 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
@@ -1812,12 +2251,23 @@ static int vxlan_newlink(struct net *net, struct net_device *dev, · 5 unmodified lines
18122251 return -ENODEV;
18132252 }
18142253
2254+#if IS_ENABLED(CONFIG_IPV6)
2255+ if (use_ipv6) {
2256+ struct inet6_dev *idev = __in6_dev_get(lowerdev);
2257+ if (idev && idev->cnf.disable_ipv6) {
2258+ pr_info("IPv6 is disabled via sysctl\n");
2259+ return -EPERM;
2260+ }
2261+ vxlan->flags |= VXLAN_F_IPV6;
2262+ }
2263+#endif
2264+
18152265 if (!tb[IFLA_MTU])
1816− dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
2266+ dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
18172267
18182268 /* update header length based on lower device */
18192269 dev->hard_header_len = lowerdev->hard_header_len +
1820− VXLAN_HEADROOM;
2270+ (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
18212271 }
18222272
18232273 if (data[IFLA_VXLAN_TOS])
@@ -1868,7 +2318,7 @@ static int vxlan_newlink(struct net *net, struct net_device *dev, · 44 unmodified lines
18682318
18692319 /* create an fdb entry for default destination */
18702320 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1871− vxlan->default_dst.remote_ip,
2321+ &vxlan->default_dst.remote_ip,
18722322 NUD_REACHABLE|NUD_PERMANENT,
18732323 NLM_F_EXCL|NLM_F_CREATE,
18742324 vxlan->dst_port, vxlan->default_dst.remote_vni,
@@ -1905,9 +2355,9 @@ static size_t vxlan_get_size(const struct net_device *dev) · 30 unmodified lines
19052355 {
19062356
19072357 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
1908− nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
2358+ nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
19092359 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1910− nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
2360+ nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
19112361 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
19122362 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
19132363 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
@@ -1934,14 +2384,36 @@ static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) · 20 unmodified lines
19342384 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
19352385 goto nla_put_failure;
19362386
1937− if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1938− goto nla_put_failure;
2387+ if (!vxlan_addr_any(&dst->remote_ip)) {
2388+ if (dst->remote_ip.sa.sa_family == AF_INET) {
2389+ if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2390+ dst->remote_ip.sin.sin_addr.s_addr))
2391+ goto nla_put_failure;
2392+#if IS_ENABLED(CONFIG_IPV6)
2393+ } else {
2394+ if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2395+ &dst->remote_ip.sin6.sin6_addr))
2396+ goto nla_put_failure;
2397+#endif
2398+ }
2399+ }
19392400
19402401 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
19412402 goto nla_put_failure;
19422403
1943− if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1944− goto nla_put_failure;
2404+ if (!vxlan_addr_any(&vxlan->saddr)) {
2405+ if (vxlan->saddr.sa.sa_family == AF_INET) {
2406+ if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2407+ vxlan->saddr.sin.sin_addr.s_addr))
2408+ goto nla_put_failure;
2409+#if IS_ENABLED(CONFIG_IPV6)
2410+ } else {
2411+ if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2412+ &vxlan->saddr.sin6.sin6_addr))
2413+ goto nla_put_failure;
2414+#endif
2415+ }
2416+ }
19452417
19462418 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
19472419 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
include/net/vxlan.h+1−1
@@ -25,7 +25,7 @@ struct vxlan_sock { · 24 unmodified lines
2525
2626 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2727 vxlan_rcv_t *rcv, void *data,
28− bool no_share);
28+ bool no_share, bool ipv6);
2929
3030 void vxlan_sock_release(struct vxlan_sock *vs);
3131
include/uapi/linux/if_link.h+2−0
@@ -314,6 +314,8 @@ enum { · 313 unmodified lines
314314 IFLA_VXLAN_L2MISS,
315315 IFLA_VXLAN_L3MISS,
316316 IFLA_VXLAN_PORT, /* destination port */
317+ IFLA_VXLAN_GROUP6,
318+ IFLA_VXLAN_LOCAL6,
317319 __IFLA_VXLAN_MAX
318320 };
319321 #define IFLA_VXLAN_MAX (__IFLA_VXLAN_MAX - 1)
net/openvswitch/vport-vxlan.c+1−1
@@ -123,7 +123,7 @@ static struct vport *vxlan_tnl_create(const struct vport_parms *parms) · 122 unmodified lines
123123 vxlan_port = vxlan_vport(vport);
124124 strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
125125
126− vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true);
126+ vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true, false);
127127 if (IS_ERR(vs)) {
128128 ovs_vport_free(vport);
129129 return (void *)vs;