From owner-svn-src-all@freebsd.org Wed May 23 13:10:59 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 12261EFD5D1; Wed, 23 May 2018 13:10:59 +0000 (UTC) (envelope-from pizzamig@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B1B6E69DCB; Wed, 23 May 2018 13:10:58 +0000 (UTC) (envelope-from pizzamig@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 79A2C232AC; Wed, 23 May 2018 13:10:58 +0000 (UTC) (envelope-from pizzamig@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4NDAwtp031431; Wed, 23 May 2018 13:10:58 GMT (envelope-from pizzamig@FreeBSD.org) Received: (from pizzamig@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4NDAwJn031430; Wed, 23 May 2018 13:10:58 GMT (envelope-from pizzamig@FreeBSD.org) Message-Id: <201805231310.w4NDAwJn031430@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: pizzamig set sender to pizzamig@FreeBSD.org using -f From: Luca Pizzamiglio Date: Wed, 23 May 2018 13:10:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334094 - head/sys/net X-SVN-Group: head X-SVN-Commit-Author: pizzamig X-SVN-Commit-Paths: head/sys/net X-SVN-Commit-Revision: 334094 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 May 2018 13:10:59 -0000 Author: pizzamig (ports committer) Date: Wed May 23 13:10:57 2018 New Revision: 334094 URL: https://svnweb.freebsd.org/changeset/base/334094 Log: Improve MAC address uniqueness on if_epair(4). As reported in PR184149, it can happen that epair devices can have the same MAC address. This solution is based on a 32-bit hash, obtained combining the if_index of the a interface and the hostid. If the hostid is zero, a random number is used. PR: 184149 Reviewed by: wollman, eugen Approved by: cognet Differential Revision: https://reviews.freebsd.org/D15329 Modified: head/sys/net/if_epair.c Modified: head/sys/net/if_epair.c ============================================================================== --- head/sys/net/if_epair.c Wed May 23 12:10:16 2018 (r334093) +++ head/sys/net/if_epair.c Wed May 23 13:10:57 2018 (r334094) @@ -53,10 +53,14 @@ __FBSDID("$FreeBSD$"); #include +#include +#include #include +#include #include #include #include +#include #include #include #include @@ -713,6 +717,9 @@ epair_clone_create(struct if_clone *ifc, char *name, s struct ifnet *ifp; char *dp; int error, unit, wildcard; + uint64_t hostid; + uint32_t key[3]; + uint32_t hash; uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ /* @@ -724,14 +731,12 @@ epair_clone_create(struct if_clone *ifc, char *name, s if (params) { scb = (struct epair_softc *)params; ifp = scb->ifp; - /* Assign a hopefully unique, locally administered etheraddr. */ - eaddr[0] = 0x02; - eaddr[3] = (ifp->if_index >> 8) & 0xff; - eaddr[4] = ifp->if_index & 0xff; + /* Copy epairNa etheraddr and change the last byte. */ + memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN); eaddr[5] = 0x0b; ether_ifattach(ifp, eaddr); /* Correctly set the name for the cloner list. */ - strlcpy(name, scb->ifp->if_xname, len); + strlcpy(name, ifp->if_xname, len); return (0); } @@ -835,10 +840,21 @@ epair_clone_create(struct if_clone *ifc, char *name, s ifp->if_init = epair_init; if_setsendqlen(ifp, ifqmaxlen); if_setsendqready(ifp); - /* Assign a hopefully unique, locally administered etheraddr. */ + + /* + * Calculate the etheraddr hashing the hostid and the + * interface index. The result would be hopefully unique + */ + getcredhostid(curthread->td_ucred, (unsigned long *)&hostid); + if (hostid == 0) + arc4rand(&hostid, sizeof(hostid), 0); + key[0] = (uint32_t)ifp->if_index; + key[1] = (uint32_t)(hostid & 0xffffffff); + key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff); + hash = jenkins_hash32(key, 3, 0); + eaddr[0] = 0x02; - eaddr[3] = (ifp->if_index >> 8) & 0xff; - eaddr[4] = ifp->if_index & 0xff; + memcpy(&eaddr[1], &hash, 4); eaddr[5] = 0x0a; ether_ifattach(ifp, eaddr); sca->if_qflush = ifp->if_qflush;