Author: kevans
Date: Thu Mar 14 17:18:00 2019
New Revision: 345139
URL: https://svnweb.freebsd.org/changeset/base/345139

Log:
  ether: centralize fake hwaddr generation
  
  We currently have two places with identical fake hwaddr generation --
  if_vxlan and if_bridge. Lift it into if_ethersubr for reuse in other
  interfaces that may also need a fake addr.
  
  Reviewed by:  bryanv, kp, philip
  Differential Revision:        https://reviews.freebsd.org/D19573

Modified:
  head/sys/net/ethernet.h
  head/sys/net/if_bridge.c
  head/sys/net/if_ethersubr.c
  head/sys/net/if_vxlan.c

Modified: head/sys/net/ethernet.h
==============================================================================
--- head/sys/net/ethernet.h     Thu Mar 14 17:09:07 2019        (r345138)
+++ head/sys/net/ethernet.h     Thu Mar 14 17:18:00 2019        (r345139)
@@ -422,6 +422,7 @@ void        ether_vlan_mtap(struct bpf_if *, struct mbuf *,
 struct mbuf  *ether_vlanencap(struct mbuf *, uint16_t);
 bool   ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p,
            uint16_t vid, uint8_t pcp);
+void   ether_fakeaddr(struct ether_addr *hwaddr);
 
 #ifdef _SYS_EVENTHANDLER_H_
 /* new ethernet interface attached event */

Modified: head/sys/net/if_bridge.c
==============================================================================
--- head/sys/net/if_bridge.c    Thu Mar 14 17:09:07 2019        (r345138)
+++ head/sys/net/if_bridge.c    Thu Mar 14 17:18:00 2019        (r345139)
@@ -226,7 +226,7 @@ struct bridge_softc {
        struct bstp_state       sc_stp;         /* STP state */
        uint32_t                sc_brtexceeded; /* # of cache drops */
        struct ifnet            *sc_ifaddr;     /* member mac copied from */
-       u_char                  sc_defaddr[6];  /* Default MAC address */
+       struct ether_addr       sc_defaddr;     /* Default MAC address */
 };
 
 VNET_DEFINE_STATIC(struct mtx, bridge_list_mtx);
@@ -670,16 +670,14 @@ bridge_clone_create(struct if_clone *ifc, int unit, ca
        getcredhostid(curthread->td_ucred, &hostid);
        do {
                if (fb || hostid == 0) {
-                       arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1);
-                       sc->sc_defaddr[0] &= ~1;/* clear multicast bit */
-                       sc->sc_defaddr[0] |= 2; /* set the LAA bit */
+                       ether_fakeaddr(&sc->sc_defaddr);
                } else {
-                       sc->sc_defaddr[0] = 0x2;
-                       sc->sc_defaddr[1] = (hostid >> 24) & 0xff;
-                       sc->sc_defaddr[2] = (hostid >> 16) & 0xff;
-                       sc->sc_defaddr[3] = (hostid >> 8 ) & 0xff;
-                       sc->sc_defaddr[4] =  hostid        & 0xff;
-                       sc->sc_defaddr[5] = ifp->if_dunit & 0xff;
+                       sc->sc_defaddr.octet[0] = 0x2;
+                       sc->sc_defaddr.octet[1] = (hostid >> 24) & 0xff;
+                       sc->sc_defaddr.octet[2] = (hostid >> 16) & 0xff;
+                       sc->sc_defaddr.octet[3] = (hostid >> 8 ) & 0xff;
+                       sc->sc_defaddr.octet[4] =  hostid        & 0xff;
+                       sc->sc_defaddr.octet[5] = ifp->if_dunit & 0xff;
                }
 
                fb = 1;
@@ -687,7 +685,7 @@ bridge_clone_create(struct if_clone *ifc, int unit, ca
                BRIDGE_LIST_LOCK();
                LIST_FOREACH(sc2, &V_bridge_list, sc_list) {
                        bifp = sc2->sc_ifp;
-                       if (memcmp(sc->sc_defaddr,
+                       if (memcmp(sc->sc_defaddr.octet,
                            IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
                                retry = 1;
                                break;
@@ -697,7 +695,7 @@ bridge_clone_create(struct if_clone *ifc, int unit, ca
        } while (retry == 1);
 
        bstp_attach(&sc->sc_stp, &bridge_ops);
-       ether_ifattach(ifp, sc->sc_defaddr);
+       ether_ifattach(ifp, sc->sc_defaddr.octet);
        /* Now undo some of the damage... */
        ifp->if_baudrate = 0;
        ifp->if_type = IFT_BRIDGE;
@@ -1018,7 +1016,7 @@ bridge_delete_member(struct bridge_softc *sc, struct b
         */
        if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) {
                if (LIST_EMPTY(&sc->sc_iflist)) {
-                       bcopy(sc->sc_defaddr,
+                       bcopy(&sc->sc_defaddr,
                            IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
                        sc->sc_ifaddr = NULL;
                } else {
@@ -1189,7 +1187,7 @@ bridge_ioctl_add(struct bridge_softc *sc, void *arg)
         * the default randomly generated one.
         */
        if (V_bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
-           !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) {
+           !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr.octet, 
ETHER_ADDR_LEN)) {
                bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
                sc->sc_ifaddr = ifs;
                EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);

Modified: head/sys/net/if_ethersubr.c
==============================================================================
--- head/sys/net/if_ethersubr.c Thu Mar 14 17:09:07 2019        (r345138)
+++ head/sys/net/if_ethersubr.c Thu Mar 14 17:18:00 2019        (r345139)
@@ -1401,5 +1401,19 @@ ether_8021q_frame(struct mbuf **mp, struct ifnet *ife,
        return (true);
 }
 
+void
+ether_fakeaddr(struct ether_addr *hwaddr)
+{
+
+       /*
+        * Generate a non-multicast, locally administered address.
+        *
+        * BMV: Should we use the FreeBSD OUI range instead?
+        */
+       arc4rand(hwaddr->octet, ETHER_ADDR_LEN, 1);
+       hwaddr->octet[0] &= ~1;
+       hwaddr->octet[0] |= 2;
+}
+
 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
 MODULE_VERSION(ether, 1);

Modified: head/sys/net/if_vxlan.c
==============================================================================
--- head/sys/net/if_vxlan.c     Thu Mar 14 17:09:07 2019        (r345138)
+++ head/sys/net/if_vxlan.c     Thu Mar 14 17:18:00 2019        (r345139)
@@ -177,7 +177,7 @@ struct vxlan_softc {
        struct sysctl_oid               *vxl_sysctl_node;
        struct sysctl_ctx_list           vxl_sysctl_ctx;
        struct callout                   vxl_callout;
-       uint8_t                          vxl_hwaddr[ETHER_ADDR_LEN];
+       struct ether_addr                vxl_hwaddr;
        int                              vxl_mc_ifindex;
        struct ifnet                    *vxl_mc_ifp;
        struct ifmedia                   vxl_media;
@@ -345,7 +345,6 @@ static int  vxlan_clone_create(struct if_clone *, int, 
 static void    vxlan_clone_destroy(struct ifnet *);
 
 static uint32_t vxlan_mac_hash(struct vxlan_softc *, const uint8_t *);
-static void    vxlan_fakeaddr(struct vxlan_softc *);
 static int     vxlan_media_change(struct ifnet *);
 static void    vxlan_media_status(struct ifnet *, struct ifmediareq *);
 
@@ -2755,8 +2754,8 @@ vxlan_clone_create(struct if_clone *ifc, int unit, cad
        ifmedia_add(&sc->vxl_media, IFM_ETHER | IFM_AUTO, 0, NULL);
        ifmedia_set(&sc->vxl_media, IFM_ETHER | IFM_AUTO);
 
-       vxlan_fakeaddr(sc);
-       ether_ifattach(ifp, sc->vxl_hwaddr);
+       ether_fakeaddr(&sc->vxl_hwaddr);
+       ether_ifattach(ifp, sc->vxl_hwaddr.octet);
 
        ifp->if_baudrate = 0;
        ifp->if_hdrlen = 0;
@@ -2825,20 +2824,6 @@ do {                                                     
                \
 #undef mix
 
        return (c);
-}
-
-static void
-vxlan_fakeaddr(struct vxlan_softc *sc)
-{
-
-       /*
-        * Generate a non-multicast, locally administered address.
-        *
-        * BMV: Should we use the FreeBSD OUI range instead?
-        */
-       arc4rand(sc->vxl_hwaddr, ETHER_ADDR_LEN, 1);
-       sc->vxl_hwaddr[0] &= ~1;
-       sc->vxl_hwaddr[0] |= 2;
 }
 
 static int
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to