From owner-svn-src-projects@FreeBSD.ORG Tue Nov 12 22:43:31 2013 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 9DAC099A; Tue, 12 Nov 2013 22:43:31 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 8BE842E00; Tue, 12 Nov 2013 22:43:31 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id rACMhVZH039949; Tue, 12 Nov 2013 22:43:31 GMT (envelope-from markm@svn.freebsd.org) Received: (from markm@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id rACMhS7G039928; Tue, 12 Nov 2013 22:43:28 GMT (envelope-from markm@svn.freebsd.org) Message-Id: <201311122243.rACMhS7G039928@svn.freebsd.org> From: Mark Murray Date: Tue, 12 Nov 2013 22:43:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r258074 - in projects/random_number_generator/sys: dev/glxsb dev/random kern modules net netgraph sys X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Nov 2013 22:43:31 -0000 Author: markm Date: Tue Nov 12 22:43:27 2013 New Revision: 258074 URL: http://svnweb.freebsd.org/changeset/base/258074 Log: More code eyeballing and fixup: * Use sbuf_*(9) for printing interesting stuff from sysctls. Much neater, and gets rid of static buffers. * Use a bitmask to decide which sources to harvest instead of checking inside a structure. Much more general. * Tidy up the usual comments, constants and variable names. * Fix module building on amd64. Modified: projects/random_number_generator/sys/dev/glxsb/glxsb.c projects/random_number_generator/sys/dev/random/ivy.c projects/random_number_generator/sys/dev/random/live_entropy_sources.c projects/random_number_generator/sys/dev/random/nehemiah.c projects/random_number_generator/sys/dev/random/random_adaptors.c projects/random_number_generator/sys/dev/random/random_adaptors.h projects/random_number_generator/sys/dev/random/random_harvestq.c projects/random_number_generator/sys/dev/random/randomdev.c projects/random_number_generator/sys/dev/random/randomdev.h projects/random_number_generator/sys/dev/random/randomdev_soft.c projects/random_number_generator/sys/dev/random/yarrow.c projects/random_number_generator/sys/kern/kern_intr.c projects/random_number_generator/sys/modules/Makefile projects/random_number_generator/sys/net/if_ethersubr.c projects/random_number_generator/sys/net/if_tun.c projects/random_number_generator/sys/netgraph/ng_iface.c projects/random_number_generator/sys/sys/random.h Modified: projects/random_number_generator/sys/dev/glxsb/glxsb.c ============================================================================== --- projects/random_number_generator/sys/dev/glxsb/glxsb.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/glxsb/glxsb.c Tue Nov 12 22:43:27 2013 (r258074) @@ -476,7 +476,7 @@ glxsb_rnd(void *v) if (status & SB_RNS_TRNG_VALID) { value = bus_read_4(sc->sc_sr, SB_RANDOM_NUM); /* feed with one uint32 */ - random_harvest(&value, 4, 32/2, RANDOM_PURE_GLXSB); + random_harvest(&value, sizeof(value), 32/2, RANDOM_PURE_GLXSB); } callout_reset(&sc->sc_rngco, sc->sc_rnghz, glxsb_rnd, sc); Modified: projects/random_number_generator/sys/dev/random/ivy.c ============================================================================== --- projects/random_number_generator/sys/dev/random/ivy.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/ivy.c Tue Nov 12 22:43:27 2013 (r258074) @@ -47,9 +47,8 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include #include +#include #define RETRY_COUNT 10 @@ -109,7 +108,7 @@ rdrand_modevent(module_t mod, int type, case MOD_LOAD: if (cpu_feature2 & CPUID2_RDRAND) { live_entropy_source_register(&random_ivy); - printf("random: live provider: %s\n", random_ivy.ident); + printf("random: live provider: \"%s\"\n", random_ivy.les_ident); } break; Modified: projects/random_number_generator/sys/dev/random/live_entropy_sources.c ============================================================================== --- projects/random_number_generator/sys/dev/random/live_entropy_sources.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/live_entropy_sources.c Tue Nov 12 22:43:27 2013 (r258074) @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -48,14 +49,14 @@ __FBSDID("$FreeBSD$"); #include "live_entropy_sources.h" -LIST_HEAD(les_head, live_entropy_sources); -static struct les_head les_sources = LIST_HEAD_INITIALIZER(les_sources); - /* - * The live_lock protects the consistency of the "struct les_head les_sources" + * The les_lock protects the consistency of the "struct les_head les_sources" */ static struct sx les_lock; /* need a sleepable lock */ +LIST_HEAD(les_head, live_entropy_sources); +static struct les_head les_sources = LIST_HEAD_INITIALIZER(les_sources); + void live_entropy_source_register(struct live_entropy_source *rsource) { @@ -92,23 +93,27 @@ live_entropy_source_deregister(struct li static int live_entropy_source_handler(SYSCTL_HANDLER_ARGS) { - /* XXX: FIX!! Fixed array size */ - char buf[128]; struct live_entropy_sources *lles; - int count; + struct sbuf sbuf; + int error, count; sx_slock(&les_lock); - buf[0] = '\0'; + sbuf_new_for_sysctl(&sbuf, NULL, 64, req); + count = 0; LIST_FOREACH(lles, &les_sources, lles_entries) { - strcat(buf, (count++ ? "," : "")); - strcat(buf, lles->lles_rsource->les_ident); + sbuf_cat(&sbuf, (count++ ? ",'" : "'")); + sbuf_cat(&sbuf, lles->lles_rsource->les_ident); + sbuf_cat(&sbuf, "'"); } + error = sbuf_finish(&sbuf); + sbuf_delete(&sbuf); + sx_sunlock(&les_lock); - return (SYSCTL_OUT(req, buf, strlen(buf))); + return (error); } /* @@ -128,8 +133,9 @@ live_entropy_source_handler(SYSCTL_HANDL void live_entropy_sources_feed(void) { + /* XXX: This wastes a few words of space */ + static u_int destination[ENTROPYSOURCE]; static struct harvest_event event; - static u_int dest = 0; struct live_entropy_sources *lles; int i, n; @@ -153,7 +159,7 @@ live_entropy_sources_feed(void) event.he_size = n; event.he_bits = (n*8)/2; event.he_source = lles->lles_rsource->les_source; - event.he_destination = dest++; + event.he_destination = destination[event.he_source]++; /* Do the actual entropy insertion */ harvest_process_event(&event); Modified: projects/random_number_generator/sys/dev/random/nehemiah.c ============================================================================== --- projects/random_number_generator/sys/dev/random/nehemiah.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/nehemiah.c Tue Nov 12 22:43:27 2013 (r258074) @@ -45,9 +45,8 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include #include +#include static void random_nehemiah_init(void); static void random_nehemiah_deinit(void); @@ -132,7 +131,7 @@ nehemiah_modevent(module_t mod, int type case MOD_LOAD: if (via_feature_rng & VIA_HAS_RNG) { live_entropy_source_register(&random_nehemiah); - printf("random: live provider: %s\n", random_nehemiah.ident); + printf("random: live provider: \"%s\"\n", random_nehemiah.les_ident); random_nehemiah_init(); } break; Modified: projects/random_number_generator/sys/dev/random/random_adaptors.c ============================================================================== --- projects/random_number_generator/sys/dev/random/random_adaptors.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/random_adaptors.c Tue Nov 12 22:43:27 2013 (r258074) @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -46,12 +47,16 @@ __FBSDID("$FreeBSD$"); #include #include +/* These are the data structures and associated items that need to be locked against + * "under-the-feet" changes. + */ +static struct sx random_adaptors_lock; /* need a sleepable lock */ + LIST_HEAD(adaptors_head, random_adaptors); static struct adaptors_head random_adaptors_list = LIST_HEAD_INITIALIZER(random_adaptors_list); -static struct sx random_adaptors_lock; /* need a sleepable lock */ +static struct random_adaptor *random_adaptor = NULL; /* Currently active adaptor */ -/* Contains a pointer to the currently active adaptor */ -static struct random_adaptor *random_adaptor = NULL; +/* End of data items requiring lock protection */ MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures"); @@ -70,6 +75,7 @@ random_adaptor_register(const char *name sx_xlock(&random_adaptors_lock); + /* XXX: FIX!! Make sure we are not inserting a duplicate */ LIST_INSERT_HEAD(&random_adaptors_list, rra, rra_entries); random_adaptor_choose(); @@ -112,9 +118,13 @@ random_adaptor_block(int flag) int ret; KASSERT(random_adaptor != NULL, ("No active random adaptor in %s", __func__)); + sx_slock(&random_adaptors_lock); + ret = random_adaptor->ra_block(flag); + sx_sunlock(&random_adaptors_lock); + return ret; } @@ -185,7 +195,7 @@ random_adaptor_choose(void) char rngs[128], *token, *cp; struct random_adaptors *rra, *rrai; struct random_adaptor *random_adaptor_previous; - u_int primax; + int primax; /* We are going to be messing with random_adaptor. * Exclusive lock is mandatory. @@ -215,7 +225,7 @@ random_adaptor_choose(void) } } - primax = 0U; + primax = 0; if (random_adaptor == NULL) { /* * Fall back to the highest priority item on the available @@ -245,57 +255,53 @@ random_adaptor_choose(void) static int random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS) { - /* XXX: FIX!! Fixed array size, but see below, this may be OK */ - char buf[128], *pbuf; struct random_adaptors *rra; - int count, snp; - size_t lbuf; - - buf[0] = '\0'; - pbuf = buf; - lbuf = 256; - count = 0; + struct sbuf sbuf; + int error, count; sx_slock(&random_adaptors_lock); - LIST_FOREACH(rra, &random_adaptors_list, rra_entries) { - snp = snprintf(pbuf, lbuf, "%s%s(%d)", + sbuf_new_for_sysctl(&sbuf, NULL, 64, req); + + count = 0; + LIST_FOREACH(rra, &random_adaptors_list, rra_entries) + sbuf_printf(&sbuf, "%s%s(%d)", (count++ ? "," : ""), rra->rra_name, rra->rra_ra->ra_priority); - KASSERT(snp > 0, ("buffer overflow")); - lbuf -= (size_t)snp; - pbuf += snp; - } + + error = sbuf_finish(&sbuf); + sbuf_delete(&sbuf); sx_sunlock(&random_adaptors_lock); - return (SYSCTL_OUT(req, buf, strlen(buf))); + return (error); } static int random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS) { - /* XXX: FIX!! Fixed array size, but see below, this may be OK */ - char buf[32]; struct random_adaptors *rra; - const char *name; + struct sbuf sbuf; + int error; KASSERT(random_adaptor != NULL, ("No active random adaptor in %s", __func__)); - name = NULL; - buf[0] = '\0'; - sx_slock(&random_adaptors_lock); + sbuf_new_for_sysctl(&sbuf, NULL, 16, req); + LIST_FOREACH(rra, &random_adaptors_list, rra_entries) if (rra->rra_ra == random_adaptor) { - strncpy(buf, rra->rra_name, sizeof(buf)); + sbuf_cat(&sbuf, rra->rra_name); break; } + error = sbuf_finish(&sbuf); + sbuf_delete(&sbuf); + sx_sunlock(&random_adaptors_lock); - return (SYSCTL_OUT(req, buf, strlen(buf))); + return (error); } /* ARGSUSED */ Modified: projects/random_number_generator/sys/dev/random/random_adaptors.h ============================================================================== --- projects/random_number_generator/sys/dev/random/random_adaptors.h Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/random_adaptors.h Tue Nov 12 22:43:27 2013 (r258074) @@ -43,7 +43,7 @@ typedef void random_adaptor_reseed_func_ struct random_adaptor { const char *ra_ident; int ra_seeded; - u_int ra_priority; + int ra_priority; random_adaptor_init_func_t *ra_init; random_adaptor_deinit_func_t *ra_deinit; random_adaptor_block_func_t *ra_block; Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c ============================================================================== --- projects/random_number_generator/sys/dev/random/random_harvestq.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/random_harvestq.c Tue Nov 12 22:43:27 2013 (r258074) @@ -231,6 +231,7 @@ void random_harvestq_internal(const void *entropy, u_int count, u_int bits, enum random_entropy_source origin) { + /* XXX: This wastes a few words of space */ static u_int destination[ENTROPYSOURCE]; struct harvest_event *event; Modified: projects/random_number_generator/sys/dev/random/randomdev.c ============================================================================== --- projects/random_number_generator/sys/dev/random/randomdev.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/dev/random/randomdev.c Tue Nov 12 22:43:27 2013 (r258074) @@ -84,9 +84,8 @@ static struct cdev *random_dev; /* Allow the sysadmin to select the broad category of * entropy types to harvest. - * Here because the rest of the kernel checks these in random_harvest() calls. */ -struct harvest_select harvest = { 1, 1, 1, 1, 1 }; +u_int randomdev_harvest_source_mask = ((1< #include #include +#include #include #include #include @@ -129,10 +130,35 @@ random_check_boolean(SYSCTL_HANDLER_ARGS return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req)); } +/* ARGSUSED */ +RANDOM_CHECK_UINT(harvestmask, 0, ((1<= 0; i--) + sbuf_cat(&sbuf, (randomdev_harvest_source_mask & (1<somecounter); - for (i = 0; i < event->size; i++) - printf("%02X", event->entropy[i]); - for (; i < 16; i++) - printf(" "); - printf(" %2d %2d %02X %02X\n", event->size, event->bits, event->source, event->destination); - } -#endif - /* Accumulate the event into the appropriate pool * where each event carries the destination information */ @@ -307,10 +293,10 @@ reseed(u_int fastslow) /* 7. Dump to seed file */ #ifdef RANDOM_RWFILE_WRITE_OK /* XXX: Not defined so writes ain't gonna happen */ - seed_file = "/var/db/entropy/seed_cache"; + seed_file = ""; error = randomdev_write_file(seed_file, , PAGE_SIZE); - if (error == 0) { - printf("random: entropy seed file '%s' successfully written\n", seed_file); + if (error == 0) + printf("random: entropy seed file '%s' successfully written\n", seed_file); #endif /* Unblock the device if it was blocked due to being unseeded */ Modified: projects/random_number_generator/sys/kern/kern_intr.c ============================================================================== --- projects/random_number_generator/sys/kern/kern_intr.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/kern/kern_intr.c Tue Nov 12 22:43:27 2013 (r258074) @@ -896,13 +896,10 @@ intr_event_schedule_thread(struct intr_e * If any of the handlers for this ithread claim to be good * sources of entropy, then gather some. */ - if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { - CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, - p->p_pid, td->td_name); + if (ie->ie_flags & IE_ENTROPY) { entropy.event = (uintptr_t)ie; entropy.td = ctd; - random_harvest(&entropy, sizeof(entropy), 2, - RANDOM_INTERRUPT); + random_harvest(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT); } KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); @@ -1050,13 +1047,10 @@ intr_event_schedule_thread(struct intr_e * If any of the handlers for this ithread claim to be good * sources of entropy, then gather some. */ - if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { - CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, - p->p_pid, td->td_name); + if (ie->ie_flags & IE_ENTROPY) { entropy.event = (uintptr_t)ie; entropy.td = ctd; - random_harvest(&entropy, sizeof(entropy), 2, - RANDOM_INTERRUPT); + random_harvest(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT); } KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); @@ -1141,14 +1135,9 @@ swi_sched(void *cookie, int flags) CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, ih->ih_need); - if (harvest.swi) { - CTR2(KTR_INTR, "swi_sched: pid %d (%s) gathering entropy", - curproc->p_pid, curthread->td_name); - entropy.event = (uintptr_t)ih; - entropy.td = curthread; - random_harvest(&entropy, sizeof(entropy), 1, - RANDOM_SWI); - } + entropy.event = (uintptr_t)ih; + entropy.td = curthread; + random_harvest(&entropy, sizeof(entropy), 1, RANDOM_SWI); /* * Set ih_need for this handler so that if the ithread is already Modified: projects/random_number_generator/sys/modules/Makefile ============================================================================== --- projects/random_number_generator/sys/modules/Makefile Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/modules/Makefile Tue Nov 12 22:43:27 2013 (r258074) @@ -744,6 +744,8 @@ _opensolaris= opensolaris .endif .if ${MK_CRYPT} != "no" || defined(ALL_MODULES) _padlock= padlock +_padlock_rng= padlock_rng +_rdrand_rng= rdrand_rng .endif _pccard= pccard _qlxge= qlxge Modified: projects/random_number_generator/sys/net/if_ethersubr.c ============================================================================== --- projects/random_number_generator/sys/net/if_ethersubr.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/net/if_ethersubr.c Tue Nov 12 22:43:27 2013 (r258074) @@ -641,8 +641,7 @@ ether_input_internal(struct ifnet *ifp, m->m_flags |= M_PROMISC; } - if (harvest.ethernet) - random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER); ether_demux(ifp, m); CURVNET_RESTORE(); Modified: projects/random_number_generator/sys/net/if_tun.c ============================================================================== --- projects/random_number_generator/sys/net/if_tun.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/net/if_tun.c Tue Nov 12 22:43:27 2013 (r258074) @@ -918,8 +918,7 @@ tunwrite(struct cdev *dev, struct uio *u m_freem(m); return (EAFNOSUPPORT); } - if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 2, RANDOM_NET_TUN); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_TUN); ifp->if_ibytes += m->m_pkthdr.len; ifp->if_ipackets++; CURVNET_SET(ifp->if_vnet); Modified: projects/random_number_generator/sys/netgraph/ng_iface.c ============================================================================== --- projects/random_number_generator/sys/netgraph/ng_iface.c Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/netgraph/ng_iface.c Tue Nov 12 22:43:27 2013 (r258074) @@ -775,8 +775,7 @@ ng_iface_rcvdata(hook_p hook, item_p ite m_freem(m); return (EAFNOSUPPORT); } - if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG); M_SETFIB(m, ifp->if_fib); netisr_dispatch(isr, m); return (0); Modified: projects/random_number_generator/sys/sys/random.h ============================================================================== --- projects/random_number_generator/sys/sys/random.h Tue Nov 12 21:42:59 2013 (r258073) +++ projects/random_number_generator/sys/sys/random.h Tue Nov 12 22:43:27 2013 (r258074) @@ -36,6 +36,9 @@ int read_random(void *, int); /* * Note: if you add or remove members of random_entropy_source, remember to also update the * KASSERT regarding what valid members are in random_harvest_internal(). + * + * NOTE: complain loudly to markm@ or on the lists if this enum gets more than 32 + * distinct values (0-31)! */ enum random_entropy_source { RANDOM_START = 0, @@ -50,6 +53,7 @@ enum random_entropy_source { RANDOM_INTERRUPT, RANDOM_SWI, RANDOM_UMA_ALLOC, + RANDOM_ENVIRONMENTAL_END, /* High-quality HW RNGs from here on. */ RANDOM_PURE_OCTEON, RANDOM_PURE_SAFE,