Changeset View
Changeset View
Standalone View
Standalone View
sys/net/if_ethersubr.c
Show First 20 Lines • Show All 1,412 Lines • ▼ Show 20 Lines | if (*mp == NULL) { | ||||
if_printf(ife, "unable to prepend 802.1Q header"); | if_printf(ife, "unable to prepend 802.1Q header"); | ||||
return (false); | return (false); | ||||
} | } | ||||
} | } | ||||
return (true); | return (true); | ||||
} | } | ||||
/* | /* | ||||
* Allocate an address from the FreeBSD Foundation OUI. This uses a | * Allocate an address from the FreeBSD Foundation OUI. This uses a | ||||
* cryptographic hash function on the containing jail's UUID and the interface | * cryptographic hash function on the containing jail's name, UUID and the | ||||
melifaro: Worth updating to reflect that jail name is also a part? | |||||
* name to attempt to provide a unique but stable address. Pseudo-interfaces | * interface name to attempt to provide a unique but stable address. | ||||
* which require a MAC address should use this function to allocate | * Pseudo-interfaces which require a MAC address should use this function to | ||||
* non-locally-administered addresses. | * allocate non-locally-administered addresses. | ||||
*/ | */ | ||||
void | void | ||||
ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) | ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) | ||||
{ | { | ||||
#define ETHER_GEN_ADDR_BUFSIZ HOSTUUIDLEN + IFNAMSIZ + 2 | |||||
SHA1_CTX ctx; | SHA1_CTX ctx; | ||||
char buf[ETHER_GEN_ADDR_BUFSIZ]; | char *buf; | ||||
char uuid[HOSTUUIDLEN + 1]; | char uuid[HOSTUUIDLEN + 1]; | ||||
uint64_t addr; | uint64_t addr; | ||||
int i, sz; | int i, sz; | ||||
char digest[SHA1_RESULTLEN]; | char digest[SHA1_RESULTLEN]; | ||||
char jailname[MAXHOSTNAMELEN]; | |||||
Not Done Inline ActionsNit: maybe jail_name would be a bit more descriptive? melifaro: Nit: maybe `jail_name` would be a bit more descriptive? | |||||
getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); | getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); | ||||
sz = snprintf(buf, ETHER_GEN_ADDR_BUFSIZ, "%s-%s", uuid, ifp->if_xname); | /* If each (vnet) jail would also have a unique hostuuid this would not | ||||
* be necessary. */ | |||||
getjailname(curthread->td_ucred, jailname, sizeof(jailname)); | |||||
sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, if_name(ifp), | |||||
Not Done Inline ActionsNit: probably it's worth considering using if_name() accessor. melifaro: Nit: probably it's worth considering using `if_name()` accessor. | |||||
jailname); | |||||
if (sz < 0) { | |||||
/* Fall back to a random mac address. */ | |||||
arc4rand(hwaddr, sizeof(*hwaddr), 0); | |||||
hwaddr->octet[0] = 0x02; | |||||
return; | |||||
} | |||||
SHA1Init(&ctx); | SHA1Init(&ctx); | ||||
SHA1Update(&ctx, buf, sz); | SHA1Update(&ctx, buf, sz); | ||||
SHA1Final(digest, &ctx); | SHA1Final(digest, &ctx); | ||||
free(buf, M_TEMP); | |||||
addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) & | addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) & | ||||
OUI_FREEBSD_GENERATED_MASK; | OUI_FREEBSD_GENERATED_MASK; | ||||
addr = OUI_FREEBSD(addr); | addr = OUI_FREEBSD(addr); | ||||
for (i = 0; i < ETHER_ADDR_LEN; ++i) { | for (i = 0; i < ETHER_ADDR_LEN; ++i) { | ||||
hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & | hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & | ||||
0xFF; | 0xFF; | ||||
} | } | ||||
} | } | ||||
DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); | DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); | ||||
MODULE_VERSION(ether, 1); | MODULE_VERSION(ether, 1); |
Worth updating to reflect that jail name is also a part?