summaryrefslogtreecommitdiff
path: root/sys/net
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2010-01-12 03:41:30 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2010-01-12 03:41:30 +0000
commit2ffe5c858e64748794af88d21f0dc46133869dca (patch)
tree3156e96efeaa40b97986da7420161875f544ecd4 /sys/net
parent20bb74983055decf011de6c42a46c101cc46ccd9 (diff)
Unify the various fake ethernet generators as ether_fakeaddr() which
is safe for both hardware devices and virtual devices ok mpf, kettenis, moaning and groaning and slow acceptance from mcbride XXX should loop checking for uniqueness after new henning diff goes in
Diffstat (limited to 'sys/net')
-rw-r--r--sys/net/if_ethersubr.c36
-rw-r--r--sys/net/if_tun.c16
-rw-r--r--sys/net/if_vether.c15
3 files changed, 26 insertions, 41 deletions
diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c
index 595355fba0d..39309e025a8 100644
--- a/sys/net/if_ethersubr.c
+++ b/sys/net/if_ethersubr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: if_ethersubr.c,v 1.137 2010/01/11 03:50:56 yasuoka Exp $ */
+/* $OpenBSD: if_ethersubr.c,v 1.138 2010/01/12 03:41:29 deraadt Exp $ */
/* $NetBSD: if_ethersubr.c,v 1.19 1996/05/07 02:40:30 thorpej Exp $ */
/*
@@ -879,30 +879,36 @@ ether_sprintf(ap)
}
/*
+ * Generate a (hopefully) acceptable MAC address, if asked.
+ */
+void
+ether_fakeaddr(struct ifnet *ifp)
+{
+ static int unit;
+ int rng;
+
+ ((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe;
+ ((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1;
+ ((struct arpcom *)ifp)->ac_enaddr[2] = 0xba;
+ ((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf);
+ rng = cold ? random() ^ (long)ifp : arc4random();
+ ((struct arpcom *)ifp)->ac_enaddr[4] = rng;
+ ((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8;
+}
+
+/*
* Perform common duties while attaching to interface list
*/
void
ether_ifattach(ifp)
struct ifnet *ifp;
{
-
/*
* Any interface which provides a MAC address which is obviously
* invalid gets whacked, so that users will notice.
*/
- if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) {
- ((struct arpcom *)ifp)->ac_enaddr[0] = 0x00;
- ((struct arpcom *)ifp)->ac_enaddr[1] = 0xfe;
- ((struct arpcom *)ifp)->ac_enaddr[2] = 0xe1;
- ((struct arpcom *)ifp)->ac_enaddr[3] = 0xba;
- ((struct arpcom *)ifp)->ac_enaddr[4] = 0xd0;
- /*
- * XXX use of random() by anything except the scheduler is
- * normally invalid, but this is boot time, so pre-scheduler,
- * and the random subsystem is not alive yet
- */
- ((struct arpcom *)ifp)->ac_enaddr[5] = (u_char)random() & 0xff;
- }
+ if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr))
+ ether_fakeaddr(ifp);
ifp->if_type = IFT_ETHER;
ifp->if_addrlen = ETHER_ADDR_LEN;
diff --git a/sys/net/if_tun.c b/sys/net/if_tun.c
index b65193ce98c..7c28153f67e 100644
--- a/sys/net/if_tun.c
+++ b/sys/net/if_tun.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: if_tun.c,v 1.101 2010/01/11 03:50:56 yasuoka Exp $ */
+/* $OpenBSD: if_tun.c,v 1.102 2010/01/12 03:41:29 deraadt Exp $ */
/* $NetBSD: if_tun.c,v 1.24 1996/05/07 02:40:48 thorpej Exp $ */
/*
@@ -179,7 +179,6 @@ tun_create(struct if_clone *ifc, int unit, int flags)
{
struct tun_softc *tp;
struct ifnet *ifp;
- u_int32_t macaddr_rnd;
int s;
tp = malloc(sizeof(*tp), M_DEVBUF, M_NOWAIT|M_ZERO);
@@ -189,20 +188,11 @@ tun_create(struct if_clone *ifc, int unit, int flags)
tp->tun_unit = unit;
tp->tun_flags = TUN_INITED|TUN_STAYUP;
- /* generate fake MAC address: 00 bd xx xx xx unit_no */
- tp->arpcom.ac_enaddr[0] = 0x00;
- tp->arpcom.ac_enaddr[1] = 0xbd;
- /*
- * This no longer happens pre-scheduler so let's use the real
- * random subsystem instead of random().
- */
- macaddr_rnd = arc4random();
- bcopy(&macaddr_rnd, &tp->arpcom.ac_enaddr[2], sizeof(u_int32_t));
- tp->arpcom.ac_enaddr[5] = (u_char)unit + 1;
-
ifp = &tp->tun_if;
snprintf(ifp->if_xname, sizeof ifp->if_xname, "%s%d", ifc->ifc_name,
unit);
+ ether_fakeaddr(ifp);
+
ifp->if_softc = tp;
ifp->if_ioctl = tun_ioctl;
ifp->if_output = tun_output;
diff --git a/sys/net/if_vether.c b/sys/net/if_vether.c
index 994fe743299..ad0e0023274 100644
--- a/sys/net/if_vether.c
+++ b/sys/net/if_vether.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: if_vether.c,v 1.6 2009/11/22 12:33:25 deraadt Exp $ */
+/* $OpenBSD: if_vether.c,v 1.7 2010/01/12 03:41:29 deraadt Exp $ */
/*
* Copyright (c) 2009 Theo de Raadt
@@ -98,27 +98,16 @@ vether_clone_create(struct if_clone *ifc, int unit)
{
struct ifnet *ifp;
struct vether_softc *sc;
- u_int32_t macaddr_rnd;
int s;
if ((sc = malloc(sizeof(*sc),
M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
return (ENOMEM);
- /* from if_tun.c: generate fake MAC address: 00 bd xx xx xx unit_no */
- sc->sc_ac.ac_enaddr[0] = 0x00;
- sc->sc_ac.ac_enaddr[1] = 0xbd;
- /*
- * This no longer happens pre-scheduler so let's use the real
- * random subsystem instead of random().
- */
- macaddr_rnd = arc4random();
- bcopy(&macaddr_rnd, &sc->sc_ac.ac_enaddr[2], sizeof(u_int32_t));
- sc->sc_ac.ac_enaddr[5] = (u_char)unit + 1;
-
ifp = &sc->sc_ac.ac_if;
snprintf(ifp->if_xname, sizeof ifp->if_xname, "vether%d", unit);
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
+ ether_fakeaddr(ifp);
ifp->if_softc = sc;
ifp->if_ioctl = vetherioctl;