From c9b0da9822b03791b99cbf70968712960e1c7347 Mon Sep 17 00:00:00 2001 From: dlg Date: Fri, 23 Oct 2015 01:19:04 +0000 Subject: [PATCH] tweak the vnetid so it can be optional and therefore cleared/deleted. the abstract vnetid is promoted to a uin32_t, and adds a SIOCDVNETID ioctl so it can be cleared. this is all because i set an assignment on implementing a virtual network interface and the students got confused when vnetid 0 didnt show up in ifconfig output. the vnetid in the vxlan(4) protocol is optional, but the current code confuses 0 with no vnetid being set. this makes it clear. ok reyk@ who also simplified my diff --- sbin/ifconfig/ifconfig.8 | 7 ++++-- sbin/ifconfig/ifconfig.c | 16 ++++++++++--- sys/net/if.h | 5 ++-- sys/net/if_vxlan.c | 50 ++++++++++++++++++++++++++++------------ sys/net/if_vxlan.h | 4 ++-- sys/sys/sockio.h | 4 +++- 6 files changed, 61 insertions(+), 25 deletions(-) diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index 35137b537bb..3412f74f09a 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ifconfig.8,v 1.257 2015/10/06 17:23:21 benno Exp $ +.\" $OpenBSD: ifconfig.8,v 1.258 2015/10/23 01:19:04 dlg Exp $ .\" $NetBSD: ifconfig.8,v 1.11 1996/01/04 21:27:29 pk Exp $ .\" $FreeBSD: ifconfig.8,v 1.16 1998/02/01 07:03:29 steve Exp $ .\" @@ -31,7 +31,7 @@ .\" .\" @(#)ifconfig.8 8.4 (Berkeley) 6/1/94 .\" -.Dd $Mdocdate: October 6 2015 $ +.Dd $Mdocdate: October 23 2015 $ .Dt IFCONFIG 8 .Os .Sh NAME @@ -1504,6 +1504,7 @@ for a complete list of the available protocols, .Op Cm tunnel Ar src_address dest_address .Op Cm tunneldomain Ar tableid .Op Cm vnetid Ar network-id +.Op Cm -vnetid .Ek .nr nS 0 .Pp @@ -1559,6 +1560,8 @@ to identify packets with a virtual network. The accepted size of the number depends on the individual tunnel protocol; it is a 24-bit number for .Xr vxlan 4 . +.It Cm -vnetid +Clear the virtual network identifier. .El .\" VLAN .Sh VLAN diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 5c015f67930..6c681e3fa5e 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifconfig.c,v 1.302 2015/10/03 10:44:23 florian Exp $ */ +/* $OpenBSD: ifconfig.c,v 1.303 2015/10/23 01:19:04 dlg Exp $ */ /* $NetBSD: ifconfig.c,v 1.40 1997/10/01 02:19:43 enami Exp $ */ /* @@ -181,6 +181,7 @@ void deletetunnel(const char *, int); void settunnelinst(const char *, int); void settunnelttl(const char *, int); void setvnetid(const char *, int); +void delvnetid(const char *, int); #ifdef INET6 void setia6flags(const char *, int); void setia6pltime(const char *, int); @@ -413,6 +414,7 @@ const struct cmd { { "tunneldomain", NEXTARG, 0, settunnelinst } , { "tunnelttl", NEXTARG, 0, settunnelttl } , { "vnetid", NEXTARG, 0, setvnetid }, + { "-vnetid", 0, 0, delvnetid }, { "pppoedev", NEXTARG, 0, setpppoe_dev }, { "pppoesvc", NEXTARG, 0, setpppoe_svc }, { "-pppoesvc", 1, 0, setpppoe_svc }, @@ -2868,7 +2870,7 @@ phys_status(int force) if (dstport) printf(":%u", ntohs(dstport)); - if (ioctl(s, SIOCGVNETID, (caddr_t)&ifr) == 0 && ifr.ifr_vnetid > 0) + if (ioctl(s, SIOCGVNETID, (caddr_t)&ifr) == 0) printf(" vnetid %d", ifr.ifr_vnetid); if (ioctl(s, SIOCGLIFPHYTTL, (caddr_t)&ifr) == 0 && ifr.ifr_ttl > 0) printf(" ttl %d", ifr.ifr_ttl); @@ -3391,7 +3393,7 @@ void setvnetid(const char *id, int param) { const char *errmsg = NULL; - int vnetid; + uint32_t vnetid; vnetid = strtonum(id, 0, UINT_MAX, &errmsg); if (errmsg) @@ -3403,6 +3405,14 @@ setvnetid(const char *id, int param) warn("SIOCSVNETID"); } +/* ARGSUSED */ +void +delvnetid(const char *ignored, int alsoignored) +{ + if (ioctl(s, SIOCDVNETID, &ifr) < 0) + warn("SIOCDVNETID"); +} + void mpe_status(void) { diff --git a/sys/net/if.h b/sys/net/if.h index 5b17832bd99..82dd41cc06e 100644 --- a/sys/net/if.h +++ b/sys/net/if.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if.h,v 1.169 2015/10/05 15:19:29 uebayasi Exp $ */ +/* $OpenBSD: if.h,v 1.170 2015/10/23 01:19:04 dlg Exp $ */ /* $NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $ */ /* @@ -354,6 +354,7 @@ struct ifreq { struct sockaddr ifru_broadaddr; short ifru_flags; int ifru_metric; + uint32_t ifru_vnetid; uint64_t ifru_media; caddr_t ifru_data; } ifr_ifru; @@ -366,7 +367,7 @@ struct ifreq { #define ifr_hardmtu ifr_ifru.ifru_metric /* hardmtu (overload) */ #define ifr_media ifr_ifru.ifru_media /* media options */ #define ifr_rdomainid ifr_ifru.ifru_metric /* VRF instance (overload) */ -#define ifr_vnetid ifr_ifru.ifru_metric /* Virtual Net Id (overload) */ +#define ifr_vnetid ifr_ifru.ifru_vnetid /* Virtual Net Id */ #define ifr_ttl ifr_ifru.ifru_metric /* tunnel TTL (overload) */ #define ifr_data ifr_ifru.ifru_data /* for use by interface */ }; diff --git a/sys/net/if_vxlan.c b/sys/net/if_vxlan.c index 9062ce921eb..d1ff2791d08 100644 --- a/sys/net/if_vxlan.c +++ b/sys/net/if_vxlan.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_vxlan.c,v 1.31 2015/10/15 13:59:21 yasuoka Exp $ */ +/* $OpenBSD: if_vxlan.c,v 1.32 2015/10/23 01:19:04 dlg Exp $ */ /* * Copyright (c) 2013 Reyk Floeter @@ -80,7 +80,7 @@ int vxlan_enable = 0; u_long vxlan_tagmask; #define VXLAN_TAGHASHSIZE 32 -#define VXLAN_TAGHASH(tag) (tag & vxlan_tagmask) +#define VXLAN_TAGHASH(tag) ((unsigned int)tag & vxlan_tagmask) LIST_HEAD(vxlan_taghash, vxlan_softc) *vxlan_tagh; void @@ -108,7 +108,7 @@ vxlan_clone_create(struct if_clone *ifc, int unit) M_WAITOK|M_ZERO); sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS; sc->sc_dstport = htons(VXLAN_PORT); - sc->sc_vnetid = 0; + sc->sc_vnetid = -1; ifp = &sc->sc_ac.ac_if; snprintf(ifp->if_xname, sizeof ifp->if_xname, "vxlan%d", unit); @@ -420,18 +420,29 @@ vxlanioctl(struct ifnet *ifp, u_long cmd, caddr_t data) break; case SIOCSVNETID: - if (ifr->ifr_vnetid < 0 || ifr->ifr_vnetid > 0x00ffffff) { + if (ifr->ifr_vnetid > 0x00ffffff) { error = EINVAL; break; } s = splnet(); - sc->sc_vnetid = (u_int32_t)ifr->ifr_vnetid; + sc->sc_vnetid = (int)ifr->ifr_vnetid; (void)vxlan_config(ifp, NULL, NULL); splx(s); break; case SIOCGVNETID: - ifr->ifr_vnetid = (int)sc->sc_vnetid; + if (sc->sc_vnetid == -1) { + error = EADDRNOTAVAIL; + break; + } + ifr->ifr_vnetid = (uint32_t)sc->sc_vnetid; + break; + + case SIOCDVNETID: + s = splnet(); + sc->sc_vnetid = -1; + (void)vxlan_config(ifp, NULL, NULL); + splx(s); break; default: @@ -462,7 +473,7 @@ vxlan_lookup(struct mbuf *m, struct udphdr *uh, int iphlen, struct mbuf_list ml = MBUF_LIST_INITIALIZER(); struct vxlan_softc *sc = NULL, *sc_cand = NULL; struct vxlan_header v; - u_int32_t vni; + int vni; struct ifnet *ifp; int skip; struct ether_header *eh; @@ -479,14 +490,18 @@ vxlan_lookup(struct mbuf *m, struct udphdr *uh, int iphlen, m_copydata(m, skip, sizeof(v), (caddr_t)&v); skip += sizeof(v); - vni = ntohl(v.vxlan_id); - - /* Validate header */ - if ((vni == 0) || (vni & VXLAN_RESERVED2) || - (ntohl(v.vxlan_flags) != VXLAN_FLAGS_VNI)) + if (v.vxlan_flags & htonl(VXLAN_RESERVED1) || + v.vxlan_id & htonl(VXLAN_RESERVED2)) return (0); - vni >>= VXLAN_VNI_S; + vni = ntohl(v.vxlan_id) >> VXLAN_VNI_S; + if ((v.vxlan_flags & htonl(VXLAN_FLAGS_VNI)) != 0) { + if (vni != 0) + return (0); + + vni = -1; + } + LIST_FOREACH(sc, &vxlan_tagh[VXLAN_TAGHASH(vni)], sc_entry) { if ((uh->uh_dport == sc->sc_dstport) && vni == sc->sc_vnetid && @@ -613,8 +628,13 @@ vxlan_output(struct ifnet *ifp, struct mbuf *m) #endif vi = (struct vxlanudpiphdr *)ui; - vi->ui_v.vxlan_flags = htonl(VXLAN_FLAGS_VNI); - vi->ui_v.vxlan_id = htonl(sc->sc_vnetid << VXLAN_VNI_S); + if (sc->sc_vnetid != -1) { + vi->ui_v.vxlan_flags = htonl(VXLAN_FLAGS_VNI); + vi->ui_v.vxlan_id = htonl(sc->sc_vnetid << VXLAN_VNI_S); + } else { + vi->ui_v.vxlan_flags = htonl(0); + vi->ui_v.vxlan_id = htonl(0); + } /* UDP checksum should be 0 */ ui->ui_sum = 0; diff --git a/sys/net/if_vxlan.h b/sys/net/if_vxlan.h index f937de21143..2ec1752954c 100644 --- a/sys/net/if_vxlan.h +++ b/sys/net/if_vxlan.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if_vxlan.h,v 1.6 2014/12/19 17:14:40 tedu Exp $ */ +/* $OpenBSD: if_vxlan.h,v 1.7 2015/10/23 01:19:04 dlg Exp $ */ /* * Copyright (c) 2013 Reyk Floeter @@ -53,7 +53,7 @@ struct vxlan_softc { struct sockaddr_storage sc_dst; in_port_t sc_dstport; u_int sc_rdomain; - u_int32_t sc_vnetid; + int sc_vnetid; u_int8_t sc_ttl; LIST_ENTRY(vxlan_softc) sc_entry; diff --git a/sys/sys/sockio.h b/sys/sys/sockio.h index 0cca55d514d..09f9dd1f599 100644 --- a/sys/sys/sockio.h +++ b/sys/sys/sockio.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sockio.h,v 1.60 2015/09/11 13:02:28 stsp Exp $ */ +/* $OpenBSD: sockio.h,v 1.61 2015/10/23 01:19:04 dlg Exp $ */ /* $NetBSD: sockio.h,v 1.5 1995/08/23 00:40:47 thorpej Exp $ */ /*- @@ -194,6 +194,8 @@ #define SIOCSETMPWCFG _IOW('i', 173, struct ifreq) /* set mpw config */ #define SIOCGETMPWCFG _IOWR('i', 174, struct ifreq) /* get mpw config */ +#define SIOCDVNETID _IOW('i', 175, struct ifreq) /* del virt net id */ + #define SIOCSVH _IOWR('i', 245, struct ifreq) /* set carp param */ #define SIOCGVH _IOWR('i', 246, struct ifreq) /* get carp param */ -- 2.20.1