Re: SMCCC 1.1 support for psci(4)

2018-05-03 Thread Dimitris Papastamos
On Thu, May 03, 2018 at 11:32:42AM +0200, Mark Kettenis wrote:
> > Date: Thu, 3 May 2018 08:19:01 +0100
> > From: Dimitris Papastamos <s...@2f30.org>
> 
> Hi Dimitris,
> 
> > On Tue, May 01, 2018 at 11:55:00AM +0200, Mark Kettenis wrote:
> > > So after adding a quick hack to mitigate Spectre variant 2 to ARM
> > > Trusted Firmware (ATF), ARM actually designed a proper solution that
> > > minimizes the performance loss and makes the presence of the
> > > workaround detectable.  This is all documented in an update of the SMC
> > > Calling Convention (SMCCC) standard.
> > > 
> > > The diff below implements support for this solution while keeping
> > > support for the hack.  While ARM strongly suggests vendors to update
> > > to a version of ATF that implements SMCCC 1.1 the current ATF for the
> > > Marvell ARMADA 8040 hasn't been updated yet (but does include the
> > > initial hack).
> > > 
> > > Unfortunately the SMCCC 1.1 implementation in ATF doesn't quite
> > > implement the spec.  As a result we have to check whether the
> > > workaround is implemented by issuing the relevant calls on each of the
> > > CPUs that might be affected.  This is important for big.LITTLE designs
> > > such as the RK3399 that include both Cortex-A53 cores that aren't
> > > vulnerable and Cortex-A72 cores that are.
> > 
> > Can you explain a bit the inconsistency between the spec and the TF
> > implementation?  If there is an issue in the TF implementation I can
> > fix it.
> > 
> > On a big.LITTLE system the expected behaviour is for
> > SMCCC_ARCH_FEATURES() to return 1 for the unaffected cores
> > (Cortex-A53) and 0 for the affected cores (Cortex-A72).  The call will
> > only return -1 if TF is built without mitigation support for
> > CVE-2017-5715.
> 
> Looks like I managed to confuse myself and drew the wrong conclusion
> from the description of SMCCC_ARCH_WORKAROUND_1 in section 2.2.4.4:
> 
>   In heterogeneous systems with some PEs that require mitigation and
>   others that do not, the firmware must provide a safe implementation
>   of this function on all PEs.
> 
> I now notice that 2.2.4.2 explicitly spells out the results of the
> SMCCC_ARCH)FEATURES call that matches the behaviour you indicate above
> and doesn't actually contradict what's written in 2.2.4.4.  I'll
> adjust the comments in the OpenBSD code.  Thanks for enlightening me!

Ok, sounds good!

> > You might also want this patch:
> > 
> > https://github.com/ARM-software/arm-trusted-firmware/commit/59dc4ef48757e4de2dc2de13d13e43acd5d91aa0#diff-3de7e205aa9233a71ea7940aae6579fe
> > 
> 
> That certainly makes the code easier to understand.  I'll see if I can
> trick the Marvell folks into backporting that to their TF fork.
> 
> Thanks,
> 
> Mark
> 
> P.S. I'm still thinking about "leveraging" trusted firmware to improve
>  the security of OpenBSD.  I have some ideas, but no concrete
>  plans yet to implement anything.

Sounds interesting!



Re: SMCCC 1.1 support for psci(4)

2018-05-03 Thread Dimitris Papastamos
On Tue, May 01, 2018 at 11:55:00AM +0200, Mark Kettenis wrote:
> So after adding a quick hack to mitigate Spectre variant 2 to ARM
> Trusted Firmware (ATF), ARM actually designed a proper solution that
> minimizes the performance loss and makes the presence of the
> workaround detectable.  This is all documented in an update of the SMC
> Calling Convention (SMCCC) standard.
> 
> The diff below implements support for this solution while keeping
> support for the hack.  While ARM strongly suggests vendors to update
> to a version of ATF that implements SMCCC 1.1 the current ATF for the
> Marvell ARMADA 8040 hasn't been updated yet (but does include the
> initial hack).
> 
> Unfortunately the SMCCC 1.1 implementation in ATF doesn't quite
> implement the spec.  As a result we have to check whether the
> workaround is implemented by issuing the relevant calls on each of the
> CPUs that might be affected.  This is important for big.LITTLE designs
> such as the RK3399 that include both Cortex-A53 cores that aren't
> vulnerable and Cortex-A72 cores that are.

Can you explain a bit the inconsistency between the spec and the TF
implementation?  If there is an issue in the TF implementation I can
fix it.

On a big.LITTLE system the expected behaviour is for
SMCCC_ARCH_FEATURES() to return 1 for the unaffected cores
(Cortex-A53) and 0 for the affected cores (Cortex-A72).  The call will
only return -1 if TF is built without mitigation support for
CVE-2017-5715.

You might also want this patch:

https://github.com/ARM-software/arm-trusted-firmware/commit/59dc4ef48757e4de2dc2de13d13e43acd5d91aa0#diff-3de7e205aa9233a71ea7940aae6579fe



Re: SMCCC 1.1 support for psci(4)

2018-05-03 Thread Dimitris Papastamos
On Thu, May 03, 2018 at 08:19:01AM +0100, Dimitris Papastamos wrote:
> On Tue, May 01, 2018 at 11:55:00AM +0200, Mark Kettenis wrote:
> > So after adding a quick hack to mitigate Spectre variant 2 to ARM
> > Trusted Firmware (ATF), ARM actually designed a proper solution that
> > minimizes the performance loss and makes the presence of the
> > workaround detectable.  This is all documented in an update of the SMC
> > Calling Convention (SMCCC) standard.
> > 
> > The diff below implements support for this solution while keeping
> > support for the hack.  While ARM strongly suggests vendors to update
> > to a version of ATF that implements SMCCC 1.1 the current ATF for the
> > Marvell ARMADA 8040 hasn't been updated yet (but does include the
> > initial hack).
> > 
> > Unfortunately the SMCCC 1.1 implementation in ATF doesn't quite
> > implement the spec.  As a result we have to check whether the
> > workaround is implemented by issuing the relevant calls on each of the
> > CPUs that might be affected.  This is important for big.LITTLE designs
> > such as the RK3399 that include both Cortex-A53 cores that aren't
> > vulnerable and Cortex-A72 cores that are.
> 
> Can you explain a bit the inconsistency between the spec and the TF
> implementation?  If there is an issue in the TF implementation I can
> fix it.
> 
> On a big.LITTLE system the expected behaviour is for
> SMCCC_ARCH_FEATURES() to return 1 for the unaffected cores
> (Cortex-A53) and 0 for the affected cores (Cortex-A72).  The call will
> only return -1 if TF is built without mitigation support for
> CVE-2017-5715.
> 
> You might also want this patch:
> 
> https://github.com/ARM-software/arm-trusted-firmware/commit/59dc4ef48757e4de2dc2de13d13e43acd5d91aa0#diff-3de7e205aa9233a71ea7940aae6579fe

And this patch as well:

https://github.com/ARM-software/arm-trusted-firmware/commit/a205a56ea891c354c642713701075fec28906c40#diff-3de7e205aa9233a71ea7940aae6579fe



Re: Spectre defence for armv7

2018-01-13 Thread Dimitris Papastamos
> + case CPU_ID_CORTEX_A15:
> + case CPU_ID_CORTEX_A57:
> + case CPU_ID_CORTEX_A72:
> + /*
> +  * Vulnerable; BPIALL is "not effective" so must use
> +  * ICIALLU and hope the firmware set the magic bit in
> +  * the ACTLR that actually forces a BTB flush.
> +  */
> + ci->ci_flush_bp = cortex_a15_flush_bp;
> + break;
> + }

Unfortunately there is an A57 erratum (833069) that when "Disabling
MMU Translation with CPUACTLR_EL1 "Enable Invalidates of BTB" bit set
can cause Invalidate by PA or VA to fail".  The only safe way to
trigger BTB invalidation on A57 is to disable and enable the MMU.



[patch] fix typo in pf.conf(5)

2017-01-04 Thread Dimitris Papastamos
Hi,

Typo fix, below.

diff --git a/share/man/man5/pf.conf.5 b/share/man/man5/pf.conf.5
index 3559803..955539b 100644
--- a/share/man/man5/pf.conf.5
+++ b/share/man/man5/pf.conf.5
@@ -368,7 +368,7 @@ Translates to the point-to-point interface's peer 
address(es).
 .Pp
 Host names may also have the
 .Cm :0
-modfier appended to restrict the name resolution to the first of each
+modifier appended to restrict the name resolution to the first of each
 v4 and v6 address found.
 .Pp
 Host name resolution and interface to address translation are done at



Re: snapshot installs

2016-12-30 Thread Dimitris Papastamos
On Fri, Dec 30, 2016 at 11:30:10AM -0700, Theo de Raadt wrote:
> I'm wondering if anyone doing an install/upgrade has noticed any
> behaviour changes in the last week...
> 
> There's a secret diff being tested :-)

i was building from source but just tried a snapshot

is it a scheduler or librthread change? :)



Re: [patch]: Make ip4 mcast membership data structure similar to ip6

2016-12-22 Thread Dimitris Papastamos
On Tue, Dec 13, 2016 at 12:35:07PM +0100, Martin Pieuchot wrote:
> Hello,
> 
> On 10/12/16(Sat) 11:08, Dimitris Papastamos wrote:
> > While I was playing around with the ip4 multicast code, I thought
> > I would attempt to make the membership data structure similar to that
> > of ip6.  This means changing from a dynamic array to a linked list.
> 
> I like this a lot.  Coherency is good.
> 
> > The max membership limit has been lifted (I did not see a similar limit
> > in the ip6 code).  I am not sure if this is appropriate for ip4 though.
> > I can add the limit back if necessary.
> 
> I can't think of a reason why it was there in the first place.
> 
> > My system seems to work with this patch but I have not explicitly
> > tested all the code paths I have modified.  I am working on some small
> > tests to prove that it works.
> 
> carp(4), pfsync(4) and vxlan(4) as well as ports using multicast like
> avahi-daemon should be tested.

Updated diff based on your comments below.  I am still testing this though.
If anyone can help with the testing, it would be great!

diff --git a/share/man/man4/ip.4 b/share/man/man4/ip.4
index 82ad06c..607c500 100644
--- a/share/man/man4/ip.4
+++ b/share/man/man4/ip.4
@@ -431,10 +431,6 @@ the host is multihomed.
 Membership is associated with a single interface;
 programs running on multihomed hosts may need to
 join the same group on more than one interface.
-Up to
-.Dv IP_MAX_MEMBERSHIPS
-(currently 4095) memberships may be added on a
-single socket.
 .Pp
 To drop a membership, use:
 .Bd -literal -offset indent
diff --git a/sys/net/if_pfsync.c b/sys/net/if_pfsync.c
index 117d03f..079a593 100644
--- a/sys/net/if_pfsync.c
+++ b/sys/net/if_pfsync.c
@@ -313,10 +313,7 @@ pfsync_clone_create(struct if_clone *ifc, int unit)
sc->sc_len = PFSYNC_MINPKT;
sc->sc_maxupdates = 128;
 
-   sc->sc_imo.imo_membership = (struct in_multi **)malloc(
-   (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
-   M_WAITOK | M_ZERO);
-   sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
+   LIST_INIT(>sc_imo.imo_memberships);
 
ifp = >sc_if;
snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
@@ -378,7 +375,6 @@ pfsync_clone_destroy(struct ifnet *ifp)
}
 
pool_destroy(>sc_pool);
-   free(sc->sc_imo.imo_membership, M_IPMOPTS, 0);
free(sc, M_DEVBUF, sizeof(*sc));
 
pfsyncif = NULL;
@@ -1319,11 +1315,14 @@ pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
sc->sc_sync_if->if_linkstatehooks,
sc->sc_lhcookie);
sc->sc_sync_if = NULL;
-   if (imo->imo_num_memberships > 0) {
-   in_delmulti(imo->imo_membership[
-   --imo->imo_num_memberships]);
-   imo->imo_ifidx = 0;
+   if (!LIST_EMPTY(>imo_memberships)) {
+   struct in_multi_mship *imm =
+   LIST_FIRST(>imo_memberships);
+
+   LIST_REMOVE(imm, imm_chain);
+   in_leavegroup(imm);
}
+   imo->imo_ifidx = 0;
splx(s);
break;
}
@@ -1345,13 +1344,18 @@ pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
sc->sc_lhcookie);
sc->sc_sync_if = sifp;
 
-   if (imo->imo_num_memberships > 0) {
-   
in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
-   imo->imo_ifidx = 0;
+   if (!LIST_EMPTY(>imo_memberships)) {
+   struct in_multi_mship *imm =
+   LIST_FIRST(>imo_memberships);
+
+   LIST_REMOVE(imm, imm_chain);
+   in_leavegroup(imm);
}
+   imo->imo_ifidx = 0;
 
if (sc->sc_sync_if &&
sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
+   struct in_multi_mship *imm;
struct in_addr addr;
 
if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
@@ -1362,16 +1366,16 @@ pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 
addr.s_addr = INADDR_PFSYNC_GROUP;
 
-   if ((imo->imo_membership[0] =
-   in_addmulti(, sc->sc_sync_if)) == NULL) {
+   if ((imm = in_joingroup(,
+   sc->sc_sync_if)) == NULL) {
sc->

Re: [patch] turn igmpstat into a set of percpu counters

2016-12-22 Thread Dimitris Papastamos
On Sun, Dec 11, 2016 at 07:24:40PM +, Dimitris Papastamos wrote:
> Hi,
> 
> I converted the igmp stats to use percpu counters.  This work is
> basically the same as what dlg@ did for other parts of the stack.
> I looked at the diff and adjusted it for igmp.

ping



[patch] turn igmpstat into a set of percpu counters

2016-12-11 Thread Dimitris Papastamos
Hi,

I converted the igmp stats to use percpu counters.  This work is
basically the same as what dlg@ did for other parts of the stack.
I looked at the diff and adjusted it for igmp.

diff --git a/sys/netinet/igmp.c b/sys/netinet/igmp.c
index 11446ce4188..c2a0a4839b4 100644
--- a/sys/netinet/igmp.c
+++ b/sys/netinet/igmp.c
@@ -101,13 +101,14 @@ int *igmpctl_vars[IGMPCTL_MAXID] = IGMPCTL_VARS;
 intigmp_timers_are_running;
 static struct router_info *rti_head;
 static struct mbuf *router_alert;
-struct igmpstat igmpstat;
+struct cpumem *igmpcounters;
 
 void igmp_checktimer(struct ifnet *);
 void igmp_sendpkt(struct in_multi *, int, in_addr_t);
 int rti_fill(struct in_multi *);
 struct router_info * rti_find(struct ifnet *);
 void igmp_input_if(struct ifnet *, struct mbuf *, int);
+int igmp_sysctl_igmpstat(void *, size_t *, void *);
 
 void
 igmp_init(void)
@@ -117,6 +118,7 @@ igmp_init(void)
igmp_timers_are_running = 0;
rti_head = 0;
 
+   igmpcounters = counters_alloc(igps_ncounters, M_COUNTERS);
router_alert = m_get(M_DONTWAIT, MT_DATA);
if (router_alert == NULL) {
printf("%s: no mbuf\n", __func__);
@@ -217,7 +219,7 @@ igmp_input(struct mbuf *m, ...)
iphlen = va_arg(ap, int);
va_end(ap);
 
-   ++igmpstat.igps_rcv_total;
+   igmpstat_inc(igps_rcv_total);
 
ifp = if_get(m->m_pkthdr.ph_ifidx);
if (ifp == NULL) {
@@ -248,14 +250,14 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int 
iphlen)
 * Validate lengths
 */
if (igmplen < IGMP_MINLEN) {
-   ++igmpstat.igps_rcv_tooshort;
+   igmpstat_inc(igps_rcv_tooshort);
m_freem(m);
return;
}
minlen = iphlen + IGMP_MINLEN;
if ((m->m_flags & M_EXT || m->m_len < minlen) &&
(m = m_pullup(m, minlen)) == NULL) {
-   ++igmpstat.igps_rcv_tooshort;
+   igmpstat_inc(igps_rcv_tooshort);
return;
}
 
@@ -266,7 +268,7 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int iphlen)
m->m_len -= iphlen;
igmp = mtod(m, struct igmp *);
if (in_cksum(m, igmplen)) {
-   ++igmpstat.igps_rcv_badsum;
+   igmpstat_inc(igps_rcv_badsum);
m_freem(m);
return;
}
@@ -277,7 +279,7 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int iphlen)
switch (igmp->igmp_type) {
 
case IGMP_HOST_MEMBERSHIP_QUERY:
-   ++igmpstat.igps_rcv_queries;
+   igmpstat_inc(igps_rcv_queries);
 
if (ifp->if_flags & IFF_LOOPBACK)
break;
@@ -292,7 +294,7 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int iphlen)
rti->rti_age = 0;
 
if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
-   ++igmpstat.igps_rcv_badqueries;
+   igmpstat_inc(igps_rcv_badqueries);
m_freem(m);
return;
}
@@ -317,7 +319,7 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int iphlen)
}
} else {
if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
-   ++igmpstat.igps_rcv_badqueries;
+   igmpstat_inc(igps_rcv_badqueries);
m_freem(m);
return;
}
@@ -367,14 +369,14 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int 
iphlen)
break;
 
case IGMP_v1_HOST_MEMBERSHIP_REPORT:
-   ++igmpstat.igps_rcv_reports;
+   igmpstat_inc(igps_rcv_reports);
 
if (ifp->if_flags & IFF_LOOPBACK)
break;
 
if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
-   ++igmpstat.igps_rcv_badreports;
+   igmpstat_inc(igps_rcv_badreports);
m_freem(m);
return;
}
@@ -401,7 +403,7 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int iphlen)
IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
if (inm != NULL) {
inm->inm_timer = 0;
-   ++igmpstat.igps_rcv_ourreports;
+   igmpstat_inc(igps_rcv_ourreports);
 
switch (inm->inm_state) {
case IGMP_IDLE_MEMBER:
@@ -433,14 +435,14 @@ igmp_input_if(struct ifnet *ifp, struct mbuf *m, int 
iphlen)
break;
 #endif
 
-   ++igmpstat.igps_rcv_reports;
+   igmpstat_inc(igps_rcv_reports);
 
if (ifp->if_flags & IFF_LOOPBACK)
break;
 
if 

[patch]: Make ip4 mcast membership data structure similar to ip6

2016-12-10 Thread Dimitris Papastamos
Hi,

While I was playing around with the ip4 multicast code, I thought
I would attempt to make the membership data structure similar to that
of ip6.  This means changing from a dynamic array to a linked list.

The max membership limit has been lifted (I did not see a similar limit
in the ip6 code).  I am not sure if this is appropriate for ip4 though.
I can add the limit back if necessary.

My system seems to work with this patch but I have not explicitly
tested all the code paths I have modified.  I am working on some small
tests to prove that it works.

Index: share/man/man4/ip.4
===
RCS file: /cvs/src/share/man/man4/ip.4,v
retrieving revision 1.41
diff -u -p -r1.41 ip.4
--- share/man/man4/ip.4 18 Aug 2016 11:45:18 -  1.41
+++ share/man/man4/ip.4 10 Dec 2016 10:31:07 -
@@ -431,10 +431,6 @@ the host is multihomed.
 Membership is associated with a single interface;
 programs running on multihomed hosts may need to
 join the same group on more than one interface.
-Up to
-.Dv IP_MAX_MEMBERSHIPS
-(currently 4095) memberships may be added on a
-single socket.
 .Pp
 To drop a membership, use:
 .Bd -literal -offset indent
Index: sys/net/if_pfsync.c
===
RCS file: /cvs/src/sys/net/if_pfsync.c,v
retrieving revision 1.238
diff -u -p -r1.238 if_pfsync.c
--- sys/net/if_pfsync.c 22 Nov 2016 19:29:54 -  1.238
+++ sys/net/if_pfsync.c 10 Dec 2016 10:31:08 -
@@ -313,10 +313,7 @@ pfsync_clone_create(struct if_clone *ifc
sc->sc_len = PFSYNC_MINPKT;
sc->sc_maxupdates = 128;
 
-   sc->sc_imo.imo_membership = (struct in_multi **)malloc(
-   (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
-   M_WAITOK | M_ZERO);
-   sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
+   LIST_INIT(>sc_imo.imo_memberships);
 
ifp = >sc_if;
snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
@@ -378,7 +375,6 @@ pfsync_clone_destroy(struct ifnet *ifp)
}
 
pool_destroy(>sc_pool);
-   free(sc->sc_imo.imo_membership, M_IPMOPTS, 0);
free(sc, M_DEVBUF, sizeof(*sc));
 
pfsyncif = NULL;
@@ -1319,9 +1315,11 @@ pfsyncioctl(struct ifnet *ifp, u_long cm
sc->sc_sync_if->if_linkstatehooks,
sc->sc_lhcookie);
sc->sc_sync_if = NULL;
-   if (imo->imo_num_memberships > 0) {
-   in_delmulti(imo->imo_membership[
-   --imo->imo_num_memberships]);
+   if (!LIST_EMPTY(>imo_memberships)) {
+   struct in_multi_mship *imm =
+   LIST_FIRST(>imo_memberships);
+   LIST_REMOVE(imm, imm_chain);
+   in_leavegroup(imm);
imo->imo_ifidx = 0;
}
splx(s);
@@ -1345,13 +1343,17 @@ pfsyncioctl(struct ifnet *ifp, u_long cm
sc->sc_lhcookie);
sc->sc_sync_if = sifp;
 
-   if (imo->imo_num_memberships > 0) {
-   
in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
+   if (!LIST_EMPTY(>imo_memberships)) {
+   struct in_multi_mship *imm =
+   LIST_FIRST(>imo_memberships);
+   LIST_REMOVE(imm, imm_chain);
+   in_leavegroup(imm);
imo->imo_ifidx = 0;
}
 
if (sc->sc_sync_if &&
sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
+   struct in_multi_mship *imm;
struct in_addr addr;
 
if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
@@ -1362,16 +1364,15 @@ pfsyncioctl(struct ifnet *ifp, u_long cm
 
addr.s_addr = INADDR_PFSYNC_GROUP;
 
-   if ((imo->imo_membership[0] =
-   in_addmulti(, sc->sc_sync_if)) == NULL) {
+   if ((imm = in_joingroup(, sc->sc_sync_if)) == 
NULL) {
sc->sc_sync_if = NULL;
splx(s);
return (ENOBUFS);
}
-   imo->imo_num_memberships++;
imo->imo_ifidx = sc->sc_sync_if->if_index;
imo->imo_ttl = PFSYNC_DFLTTL;
imo->imo_loop = 0;
+   LIST_INSERT_HEAD(>imo_memberships, imm, imm_chain);
}
 
ip = >sc_template;
Index: sys/net/if_vxlan.c
===
RCS file: /cvs/src/sys/net/if_vxlan.c,v
retrieving revision 1.53
diff 

[PATCH]: ip6 with IFF_ALLMULTI/IFF_PROMISC

2016-11-30 Thread Dimitris Papastamos
Hi everyone,

I am working on a program that requires putting the node in
all-multicast mode.  The program needs to see all the packets going to
solicited-node multicast addresses.  I did this by modifying the
interface flags to add IFF_ALLMULTI.

Unfortunately recvmsg() was only returning for the packets destined
for multicast groups I had explicitly joined and not all of them.

After this change, I can see the packets as expected.  A similar change
will probably be required for the ip4 variant of this function.

I also noticed that for this to work, IFF_PROMISC needs to be set as well.
Shouldn't it be possible to have IFF_MULTI without IFF_PROMISC?  I am using
the em(4) driver and I can see the following code in there:

ifp->if_flags &= ~IFF_ALLMULTI;

if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0 ||
ac->ac_multicnt > MAX_NUM_MULTICAST_ADDRESSES) {
ifp->if_flags |= IFF_ALLMULTI;
reg_rctl |= E1000_RCTL_MPE;
if (ifp->if_flags & IFF_PROMISC)
reg_rctl |= E1000_RCTL_UPE;
} else {
ETHER_FIRST_MULTI(step, ac, enm);
while (enm != NULL) {
bcopy(enm->enm_addrlo, mta + i, ETH_LENGTH_OF_ADDRESS);
i += ETH_LENGTH_OF_ADDRESS;

ETHER_NEXT_MULTI(step, enm);
}

em_mc_addr_list_update(>hw, mta, ac->ac_multicnt,
0, 1);
}

I am not sure how to control ac->ac_multirangecnt from userspace or if
this behaviour is intended in the first place.

diff --git a/sys/netinet6/in6.c b/sys/netinet6/in6.c
index 8b9ff6d..5630fb9 100644
--- a/sys/netinet6/in6.c
+++ b/sys/netinet6/in6.c
@@ -1342,6 +1342,9 @@ in6_hasmulti(struct in6_addr *maddr6, struct ifnet *ifp)
struct in6_multi *in6m;
int joined;
 
+   if ((ifp->if_flags & IFF_ALLMULTI) != 0)
+   return 1;
+
IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
joined = (in6m != NULL);
 



Re: [RFC] domain name matching support for rebound(8)

2016-09-16 Thread Dimitris Papastamos
On Fri, Sep 16, 2016 at 09:09:44AM -0400, Ted Unangst wrote:
> Dimitris Papastamos wrote:
> > Hi everyone,
> > 
> > I've put together a patch for 6.0-stable that adds domain name
> > matching support to rebound(8).  The patch is quite rough at the
> > moment.
> > 
> > The config is as follows:
> > 
> > match "local." 10.0.0.53
> > match "." 8.8.8.8
> 
> So this is taking rebound in a rather different direction than planned. It's
> not supposed to do anything complicated. Also, the plan is to remove its
> config file entirely.
> 
> But I think I know why you want this. I have this pf.conf rule on my firewall.
> 
> pass in on cnmac1 proto { udp , tcp } from any to any port 53 rdr-to 10.1.1.1
> port 53

Yes that works for me then.

By the way, what do you think about TCP caching support?  I could send
a patch to do just that.



[RFC] domain name matching support for rebound(8)

2016-09-16 Thread Dimitris Papastamos
Hi everyone,

I've put together a patch for 6.0-stable that adds domain name
matching support to rebound(8).  The patch is quite rough at the
moment.

The config is as follows:

match "local." 10.0.0.53
match "." 8.8.8.8

Requests to foo.local. are sent over to 10.0.0.53, all other requests
go to 8.8.8.8.  In my implementation, the first match wins.

General drawbacks:

- rebound has to parse DNS requests.  I tried to keep the parsing code
  as small as possible to avoid security problems.

Drawbacks in current implementation:

- No caching for DNS requests over TCP.  I am planning to implement
  this via a unified cache that works for both UDP and TCP.
- non-blocking connect(2) support for TCP.  The original code handled
  that but I reworked it because I wanted to get it working first.

What do you think?

===
RCS file: /cvs/src/usr.sbin/rebound/rebound.c,v
retrieving revision 1.65
diff -u -p -r1.65 rebound.c
--- rebound.c   2 Jul 2016 17:09:09 -   1.65
+++ rebound.c   16 Sep 2016 12:29:39 -
@@ -37,6 +37,8 @@
 #include 
 #include 
 
+#define LEN(x) (sizeof (x) / sizeof *(x))
+
 uint16_t randomid(void);
 
 static struct timespec now;
@@ -100,6 +102,13 @@ struct request {
 };
 static TAILQ_HEAD(, request) reqfifo;
 
+struct match {
+   char pat[256];
+   struct sockaddr_storage to;
+   TAILQ_ENTRY(match) entry;
+};
+static TAILQ_HEAD(, match) matches;
+
 static int conncount;
 static int connmax;
 static uint64_t conntotal;
@@ -215,10 +224,94 @@ servfail(int ud, uint16_t id, struct soc
sendto(ud, , sizeof(pkt), 0, fromaddr, fromlen);
 }
 
+static size_t
+readn(int fd, void *buf, size_t n)
+{
+   size_t total = 0;
+   size_t r;
+
+   while (n > 0) {
+   r = read(fd, buf + total, n);
+   if (r == 0 || r == -1)
+   return -1;
+   total += r;
+   n -= r;
+   }
+   return total;
+}
+
+static size_t
+writen(int fd, void *buf, size_t n)
+{
+   size_t total = 0;
+   size_t r;
+
+   while (n > 0) {
+   r = write(fd, buf + total, n);
+   if (r == 0 || r == -1)
+   return -1;
+   total += r;
+   n -= r;
+   }
+   return total;
+}
+
+int
+parsedomain(uint8_t *buf, size_t buflen, char *host, size_t hostlen)
+{
+   uint8_t *bp = [0], *be = [buflen];
+   char *hp = [0], *he = [hostlen];
+
+   bp += sizeof(struct dnspacket);
+   if (bp >= be)
+   return -1;
+   for (;;) {
+   uint8_t len = *bp++;
+   if (len == 0)
+   break;
+   if (bp + len >= be || hp + len >= he)
+   return -1;
+   memcpy(hp, bp, len);
+   bp += len;
+   hp += len;
+   *hp++ = '.';
+   if (hp == he)
+   return -1;
+   }
+   *hp = '\0';
+   return 0;
+}
+
+int
+matchreq(uint8_t *buf, size_t buflen, struct sockaddr_storage *to)
+{
+   char host[65536];
+   struct match *match;
+
+   /* XXX: check flags/qdcount? */
+   if (parsedomain(buf, buflen, host, sizeof(host)) == -1)
+   return -1;
+   TAILQ_FOREACH(match, , entry) {
+   size_t hlen = strlen(host);
+   size_t glen = strlen(match->pat);
+   if (hlen < glen)
+   continue;
+   if (strcmp([hlen - glen], match->pat) == 0) {
+   memcpy(to, >to, sizeof(*to));
+   logmsg(LOG_DEBUG, "matched domain %s with %s",
+  host, match->pat);
+   /* first match wins */
+   return 0;
+   }
+   }
+   return -1;
+}
+
 static struct request *
-newrequest(int ud, struct sockaddr *remoteaddr)
+newrequest(int ud)
 {
-   struct sockaddr from;
+   struct sockaddr_storage remoteaddr;
+   struct sockaddr from, *to;
socklen_t fromlen;
struct request *req;
uint8_t buf[65536];
@@ -271,13 +364,17 @@ newrequest(int ud, struct sockaddr *remo
}
req->cacheent = hit;
 
-   req->s = socket(remoteaddr->sa_family, SOCK_DGRAM, 0);
+   if (matchreq(buf, r, ) == -1)
+   goto fail;
+   to = (struct sockaddr *)
+
+   req->s = socket(to->sa_family, SOCK_DGRAM, 0);
if (req->s == -1)
goto fail;
 
TAILQ_INSERT_TAIL(, req, fifo);
 
-   if (connect(req->s, remoteaddr, remoteaddr->sa_len) == -1) {
+   if (connect(req->s, to, to->sa_len) == -1) {
logmsg(LOG_NOTICE, "failed to connect (%d)", errno);
if (errno == EADDRNOTAVAIL)
servfail(ud, req->clientid, , fromlen);
@@ -335,36 +432,18 @@ sendreply(int ud, struct request *req)
 }
 
 static struct request *
-tcpphasetwo(struct 

ikectl(8) readpassphrase

2016-09-03 Thread Dimitris Papastamos
Hi,

I've converted ikectl(8) from getpass to readpassphrase.

===
RCS file: /cvs/src/usr.sbin/ikectl/ikeca.c,v
retrieving revision 1.40
diff -u -p -r1.40 ikeca.c
--- ikeca.c 2 Nov 2015 12:21:27 -   1.40
+++ ikeca.c 3 Sep 2016 10:36:10 -
@@ -29,6 +29,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -366,21 +367,26 @@ void
 ca_newpass(char *passfile, char *password)
 {
FILE*f;
-   char*pass;
-   char prev[_PASSWORD_LEN + 1];
+   char pass[1024];
+   char prev[1024];
 
if (password != NULL) {
-   pass = password;
+   if (strlcpy(pass, password, sizeof(pass)) >= sizeof(pass))
+   errx(1, "password too long");
goto done;
}
 
-   pass = getpass("CA passphrase:");
-   if (pass == NULL || *pass == '\0')
-   err(1, "password not set");
+   if (readpassphrase("CA passphrase:", pass,
+   sizeof(pass), RPP_ECHO_OFF) == NULL)
+   err(1, "readpassphrase");
+   if (pass[0] == '\0')
+   errx(1, "password not set");
 
strlcpy(prev, pass, sizeof(prev));
-   pass = getpass("Retype CA passphrase:");
-   if (pass == NULL || strcmp(prev, pass) != 0)
+   if (readpassphrase("Retype CA passphrase:", pass,
+   sizeof(pass), RPP_ECHO_OFF) == NULL)
+   err(1, "readpassphrase");
+   if (strcmp(prev, pass) != 0)
errx(1, "passphrase does not match!");
 
  done:
@@ -637,8 +643,8 @@ ca_export(struct ca *ca, char *keyname, 
DIR *dexp;
struct dirent   *de;
struct stat  st;
-   char*pass;
-   char prev[_PASSWORD_LEN + 1];
+   char pass[1024];
+   char prev[1024];
char cmd[PATH_MAX * 2];
char oname[PATH_MAX];
char src[PATH_MAX];
@@ -659,16 +665,20 @@ ca_export(struct ca *ca, char *keyname, 
while ((p = strchr(oname, ':')) != NULL)
*p = '_';
 
-   if (password != NULL)
-   pass = password;
-   else {
-   pass = getpass("Export passphrase:");
-   if (pass == NULL || *pass == '\0')
-   err(1, "password not set");
-
+   if (password != NULL) {
+   if (strlcpy(pass, password, sizeof(pass)) >= sizeof(pass))
+   errx(1, "password too long");
+   } else {
+   if (readpassphrase("Export passphrase:", pass,
+   sizeof(pass), RPP_ECHO_OFF) == NULL)
+   err(1, "readpassphrase");
+   if (pass[0] == '\0')
+   errx(1, "password not set");
strlcpy(prev, pass, sizeof(prev));
-   pass = getpass("Retype export passphrase:");
-   if (pass == NULL || strcmp(prev, pass) != 0)
+   if (readpassphrase("Retype export passphrase:", pass,
+   sizeof(pass), RPP_ECHO_OFF) == NULL)
+   err(1, "readpassphrase");
+   if (strcmp(prev, pass) != 0)
errx(1, "passphrase does not match!");
}
 



encrypt(1) readpassphrase

2016-08-31 Thread Dimitris Papastamos
Hi,

encrypt(1) updated to use readpassphrase(3) instead of getpass(3).
Thought I would grep the tree after the recent commit to passwd(1)
by tedu.

Index: encrypt.c
===
RCS file: /cvs/src/usr.bin/encrypt/encrypt.c,v
retrieving revision 1.42
diff -u -p -r1.42 encrypt.c
--- encrypt.c   10 Oct 2015 18:14:20 -  1.42
+++ encrypt.c   31 Aug 2016 23:05:17 -
@@ -36,6 +36,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /*
  * Very simple little program, for encrypting passwords from the command
@@ -123,11 +124,13 @@ main(int argc, char **argv)
}
 
if (((argc - optind) < 1)) {
-   char line[BUFSIZ], *string;
+   char line[BUFSIZ];
+   char string[_PASSWORD_LEN + 1];
 
if (prompt) {
-   if ((string = getpass("Enter string: ")) == NULL)
-   err(1, "getpass");
+   if (readpassphrase("Enter string: ", string,
+   sizeof(string), RPP_ECHO_OFF) == NULL)
+   err(1, "readpassphrase");
print_passwd(string, operation, extra);
(void)fputc('\n', stdout);
} else {



[PATCH] fix xenocara build failure

2016-06-16 Thread Dimitris Papastamos
Hi,

I am seeing a build failure on -current when I build xenocara.  I've
already built /usr/src so that's up to date.

/usr/xenocara/lib/mesa/src/mesa/main/varray.c:168: error: no previous prototype 
for '_mesa_bind_vertex_buffer'
/usr/xenocara/lib/mesa/src/mesa/main/varray.c:280: error: no previous prototype 
for '_mesa_update_array_format'
/usr/xenocara/lib/mesa/src/mesa/main/varray.c:787: error: no previous prototype 
for '_mesa_enable_vertex_array_attrib'
*** Error 1 in lib/mesa/obj/src/mesa (Makefile:2819 'main/varray.lo')
*** Error 1 in lib/mesa/obj/src/mesa (Makefile:2956 'all-recursive')
*** Error 1 in lib/mesa/obj/src/mesa (Makefile:1789 'all')
*** Error 1 in lib/mesa/obj/src (Makefile:652 'all-recursive')
*** Error 1 in lib/mesa/obj (Makefile:598 'all-recursive')
*** Error 1 in lib/mesa (/usr/X11R6/share/mk/bsd.xorg.mk:145 'all')
*** Error 1 in lib/mesa (/usr/X11R6/share/mk/bsd.xorg.mk:216 'build')
*** Error 1 in lib (:48 'build')
*** Error 1 in . (:48 'realbuild')
*** Error 1 in /usr/xenocara (Makefile:37 'build')

I build xenocara with the following steps:

upgradexenocara() {
rm -rf /usr/xobj/*
cd /usr/xenocara
make bootstrap
make obj
make build
}

I patched the code as follows:

diff --git a/lib/mesa/src/mesa/main/varray.c b/lib/mesa/src/mesa/main/varray.c
index c2bf295..2e504d1 100644
--- a/lib/mesa/src/mesa/main/varray.c
+++ b/lib/mesa/src/mesa/main/varray.c
@@ -159,7 +159,7 @@ vertex_attrib_binding(struct gl_context *ctx,
  * Binds a buffer object to the vertex buffer binding point given by index,
  * and sets the Offset and Stride fields.
  */
-void
+static void
 _mesa_bind_vertex_buffer(struct gl_context *ctx,
  struct gl_vertex_array_object *vao,
  GLuint index,
@@ -270,7 +270,7 @@ get_legal_types_mask(const struct gl_context *ctx)
  * \param flush_verties  Should \c FLUSH_VERTICES be invoked before updating
  *   state?
  */
-void
+static void
 _mesa_update_array_format(struct gl_context *ctx,
   struct gl_vertex_array_object *vao,
   GLuint attrib, GLint size, GLenum type,
@@ -780,7 +780,7 @@ _mesa_VertexAttribLPointer(GLuint index, GLint size, GLenum 
type,
 }
 
 
-void
+static void
 _mesa_enable_vertex_array_attrib(struct gl_context *ctx,
  struct gl_vertex_array_object *vao,
  unsigned attrib)




Re: Moving away from softnet interrupts

2016-04-20 Thread Dimitris Papastamos
On Mon, Apr 18, 2016 at 10:50:46AM +0200, Martin Pieuchot wrote:
> The current goal of the Network SMP effort is to have a single CPU
> process the IP forwarding path in a process context without holding
> the KERNEL_LOCK().  To achieve this goal we're progressively moving
> code from the softnet interrupt context to the if_input_task.  In
> the end we'll completely get rid of this soft-interrupt.
> 
> So now would be a good time to know if moving all the code currently
> run in a soft-interrupt context to a task uncovers any bug.  I'm
> happily running the diff below on amd64 and macppc, it even gives me
> a small performance boost.
> 
> I'd appreciate more tests especially on exotic archs.

I've been running with this diff since you posted it on my home router.
I have not encountered any issues.

See ifconfig/route show/sysctl.conf/dmesg output below.

ifconfig:

lo0: flags=8049 mtu 32768
priority: 0
groups: lo
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
inet 127.0.0.1 netmask 0xff00
re0: flags=18843 mtu 1500
lladdr 90:e6:ba:74:0e:38
description: WAN
priority: 0
groups: egress
media: Ethernet autoselect (1000baseT full-duplex,rxpause,txpause)
status: active
inet 82.13.64.4 netmask 0xf000 broadcast 82.13.79.255
re1: 
flags=18b43 mtu 
1500
lladdr 80:ee:73:9f:1d:3d
description: Main subnet
priority: 0
media: Ethernet autoselect (1000baseT 
full-duplex,master,rxpause,txpause)
status: active
inet 10.0.0.1 netmask 0xff00 broadcast 10.0.0.255
inet6 fe80::82ee:73ff:fe9f:1d3d%re1 prefixlen 64 scopeid 0x2
inet6 2001:470:1f15:b42::1 prefixlen 64
inet6 fdfc::1 prefixlen 64
enc0: flags=0<>
priority: 0
groups: enc
status: active
axe0: flags=8843 mtu 1500
lladdr d8:eb:97:bd:8a:77
description: VoIP subnet
priority: 0
media: Ethernet autoselect (100baseTX full-duplex)
status: active
inet 10.0.1.1 netmask 0xff00 broadcast 10.0.1.255
tun0: flags=8043 mtu 1500
description: tinc VPN
priority: 0
groups: tun
status: active
inet 172.17.0.2 netmask 0xff00 broadcast 172.17.0.255
tun1: flags=8051 mtu 1304
description: cjdns
priority: 0
groups: tun
status: active
inet6 fe80::92e6:baff:fe74:e38%tun1 ->  prefixlen 64 scopeid 0x7
inet6 fcac:2580:3f14:3393:f405:d913:98af:ec5f ->  prefixlen 8
tun2: flags=8051 mtu 1500
description: stun
priority: 0
groups: tun
status: down
inet 10.10.0.2 --> 10.10.0.1 netmask 0x
gif0: flags=8051 mtu 1480
description: Hurricane Electric 6in4 link
priority: 0
groups: gif egress
tunnel: inet 82.13.64.4 -> 216.66.84.46
inet6 fe80::92e6:baff:fe74:e38%gif0 ->  prefixlen 64 scopeid 0x9
inet6 2001:470:1f14:b42::2 -> 2001:470:1f14:b42::1 prefixlen 128
bridge0: flags=41
description: Dedi bridge
groups: bridge
priority 32768 hellotime 2 fwddelay 15 maxage 20 holdcnt 6 proto rstp
re1 flags=3
port 2 ifpriority 0 ifcost 0
tap0 flags=3
port 13 ifpriority 0 ifcost 0
pflow0: flags=41 mtu 1492
priority: 0
pflow: sender: 10.0.0.1 receiver: 10.0.0.7:3001 version: 5
groups: pflow
pflog0: flags=141 mtu 33144
priority: 0
groups: pflog
tap0: flags=8943 mtu 1500
lladdr fe:e1:ba:d0:b8:5e
priority: 0
groups: tap
status: active
inet 10.0.0.14 netmask 0xff00 broadcast 10.0.0.255

route show:

Routing tables

Internet:
DestinationGatewayFlags   Refs  Use   Mtu  Prio Iface
defaultcpc92320-cmbg19-2- UGS   13 36158691 - 8 re0  
BASE-ADDRESS.MCAST localhost  URS028680 32768 8 lo0  
10.0.0/24  sunUCP410431 - 4 re1  
10.0.0/24  10.0.0.14  UCP00 - 4 tap0 
sun80:ee:73:9f:1d:3d  UHLl   024952 - 1 re1  
jupiter34:64:a9:9a:5f:1c  UHLc   3 14403576 - 4 re1  
callisto   04:18:d6:f0:47:cd  UHLc   016453 - 4 re1  
pluto-mgmt e4:8d:8c:19:38:af  UHLc   010449 - 4 re1  
10.0.0.14  fe:e1:ba:d0:b8:5e  UHLl   0  125 - 1 tap0 

Re: [PATCH] Proposal to remove -f for arp(8) and ndp(8)

2016-03-30 Thread Dimitris Papastamos
On Wed, Mar 30, 2016 at 02:49:06PM +0200, Mike Belopuhov wrote:
> Good day, Dimitris.
> 
> Long time ago in a galaxy far far away I've been using this
> alongside the -F option that I've added.  While managed
> switches are becoming cheaper, I don't see a reason for a
> working feature to go away, especially since there has been
> zero rationale provided apart from "ndp -f" doesn't work.
> Well, then how about fixing ndp?  Half of ndp(8) features
> don't work correctly (hello rdomains), but it's not a valid
> reason to go ahead and rip useful bits out.

Thanks for the feedback.  I will dig out the first patch I sent
which basically fixed ndp(8) instead of removing the feature.



Re: [PATCH] Proposal to remove -f for arp(8) and ndp(8)

2016-03-30 Thread Dimitris Papastamos
Hi everyone,

I totally forgot about this patch.  At the time it couldn't go in
because the tree was locked.  Does it make sense?  If so I will check
whether it applies on -current and resubmit.

On Fri, Jul 31, 2015 at 01:55:07PM +0100, Dimitris Papastamos wrote:
> Hi everyone,
> 
> This is a patch that removes -f for arp(8) and ndp(8).  As it stands
> currently, ndp(8) -f is not hooked in the code so no one is probably
> using that.
> 
> arp(8) -f is currently functional but I am not sure how useful.  If you
> are using this option, please reply to this thread.
> 
> Below you will find a patch that removes -f for both of these tools.
> 
> Cheers,
> Dimitris
> 
> ===
> RCS file: /cvs/src/usr.sbin/arp/arp.8,v
> retrieving revision 1.36
> diff -u -p -r1.36 arp.8
> --- usr.sbin/arp/arp.827 Jul 2015 17:28:39 -  1.36
> +++ usr.sbin/arp/arp.831 Jul 2015 12:51:29 -
> @@ -43,7 +43,6 @@
>  .Ar hostname
>  .Nm arp
>  .Op Fl F
> -.Op Fl f Ar file
>  .Op Fl V Ar rdomain
>  .Fl s Ar hostname ether_addr
>  .Op Cm temp | permanent
> @@ -123,37 +122,6 @@ Force existing entries for the given hos
>  and
>  .Fl s
>  options).
> -.It Fl f Ar file
> -Process entries from
> -.Ar file
> -to be set in the ARP tables.
> -Any entries in the file that already exist for a given host
> -will not be overwritten unless
> -.Fl F
> -is given.
> -Entries in the file should be of the form:
> -.Bd -filled -offset indent
> -.Ar hostname ether_addr
> -.Op Cm temp | permanent
> -.Op Cm pub
> -.Ed
> -.Pp
> -The entry will be static (will not time out) unless the word
> -.Cm temp
> -is given in the command.
> -A static ARP entry can be overwritten by network traffic, unless the word
> -.Cm permanent
> -is given.
> -If the word
> -.Cm pub
> -is given, the entry will be
> -.Dq published ;
> -that is, this system will act as an ARP server,
> -responding to requests for
> -.Ar hostname
> -even though the host address is not its own.
> -This behavior has traditionally been called
> -.Em proxy ARP .
>  .It Fl n
>  Show network addresses as numbers (normally
>  .Nm
> Index: usr.sbin/arp/arp.c
> ===
> RCS file: /cvs/src/usr.sbin/arp/arp.c,v
> retrieving revision 1.64
> diff -u -p -r1.64 arp.c
> --- usr.sbin/arp/arp.c3 Jun 2015 08:10:53 -   1.64
> +++ usr.sbin/arp/arp.c31 Jul 2015 12:51:29 -
> @@ -71,7 +71,6 @@ void nuke_entry(struct sockaddr_dl *sdl,
>   struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
>  static char *ether_str(struct sockaddr_dl *);
>  int wake(const char *ether_addr, const char *iface);
> -int file(char *);
>  int get(const char *);
>  int getinetaddr(const char *, struct in_addr *);
>  void getsocket(void);
> @@ -97,9 +96,8 @@ extern int h_errno;
>  /* which function we're supposed to do */
>  #define F_GET1
>  #define F_SET2
> -#define F_FILESET3
> -#define F_DELETE 4
> -#define F_WAKE   5
> +#define F_DELETE 3
> +#define F_WAKE   4
>  
>  int
>  main(int argc, char *argv[])
> @@ -131,11 +129,6 @@ main(int argc, char *argv[])
>   case 'F':
>   replace = 1;
>   break;
> - case 'f':
> - if (func)
> - usage();
> - func = F_FILESET;
> - break;
>   case 'V':
>   rdomain = strtonum(optarg, 0, RT_TABLEID_MAX, );
>   if (errstr != NULL) {
> @@ -184,11 +177,6 @@ main(int argc, char *argv[])
>   else
>   usage();
>   break;
> - case F_FILESET:
> - if (argc != 1)
> - usage();
> - rtn = file(argv[0]);
> - break;
>   case F_WAKE:
>   if (aflag || nflag || replace || rdomain > 0)
>   usage();
> @@ -203,41 +191,6 @@ main(int argc, char *argv[])
>   return (rtn);
>  }
>  
> -/*
> - * Process a file to set standard arp entries
> - */
> -int
> -file(char *name)
> -{
> - char line[100], arg[5][50], *args[5];
> - int  i, retval;
> - FILE*fp;
> -
> - if ((fp = fopen(name, "r")) == NULL)
> - err(1, "cannot open %s", name);
> - args[0] = [0][0];
> - args[1] = [1][0];
> - args[2] = [2][0];
> - args[3] = [3][0];
> - args[4] = [4][0];
> - retval = 0;
> - whi

Re: [PATCH] hier(7) - /bin and /sbin executables no longer statically linked

2016-01-15 Thread Dimitris Papastamos
On Fri, Jan 15, 2016 at 12:53:32PM -0700, Theo de Raadt wrote:
> Nope.
> 
> The manual page is correct.
> 
> > Small correction.
> > 
> > ===
> > RCS file: /cvs/src/share/man/man7/hier.7,v
> > retrieving revision 1.150
> > diff -u -p -r1.150 hier.7
> > --- hier.7  7 Jan 2016 08:58:14 -   1.150
> > +++ hier.7  15 Jan 2016 19:49:16 -
> > @@ -49,8 +49,6 @@ filesystem
> >  .Xr daily 8 ) .
> >  .It /bin/
> >  User utilities fundamental to both single and multi-user environments.
> > -These programs are statically compiled and therefore do not depend on any
> > -system libraries to run.
> >  .It /bsd
> >  Pure kernel executable
> >  (the operating system loaded into memory at boot-time).
> > @@ -163,8 +161,6 @@ Default home directory for the superuser
> >  .It /sbin/
> >  System programs and administration utilities
> >  fundamental to both single and multi-user environments.
> > -These programs are statically compiled and therefore do not depend on any
> > -system libraries to run.
> >  .It /tmp/
> >  Temporary files that are
> >  .Em not
> > 

Sorry, got confused with file(1) output.



[PATCH] hier(7) - /bin and /sbin executables no longer statically linked

2016-01-15 Thread Dimitris Papastamos
Small correction.

===
RCS file: /cvs/src/share/man/man7/hier.7,v
retrieving revision 1.150
diff -u -p -r1.150 hier.7
--- hier.7  7 Jan 2016 08:58:14 -   1.150
+++ hier.7  15 Jan 2016 19:49:16 -
@@ -49,8 +49,6 @@ filesystem
 .Xr daily 8 ) .
 .It /bin/
 User utilities fundamental to both single and multi-user environments.
-These programs are statically compiled and therefore do not depend on any
-system libraries to run.
 .It /bsd
 Pure kernel executable
 (the operating system loaded into memory at boot-time).
@@ -163,8 +161,6 @@ Default home directory for the superuser
 .It /sbin/
 System programs and administration utilities
 fundamental to both single and multi-user environments.
-These programs are statically compiled and therefore do not depend on any
-system libraries to run.
 .It /tmp/
 Temporary files that are
 .Em not



Re: mpsafe tx for re(4)

2015-12-29 Thread Dimitris Papastamos
On Mon, Dec 28, 2015 at 09:13:34PM +1000, David Gwynne wrote:
> this builds on jmatthew@'s last commit and adds mpsafe tx.
> 
> ive beat on it pretty hard, but more eyes/tests are appreciated.
> 
> ok?

Been running with this on my router since yesterday without issues.
I will continue to run with this patch and report back if anything
goes wrong.



Re: initial 11n support for iwn (n, not m)

2015-12-19 Thread Dimitris Papastamos
On Sat, Dec 19, 2015 at 01:08:26PM +0100, Stefan Sperling wrote:
> On Fri, Dec 18, 2015 at 05:40:39PM -0500, David Hill wrote:
> > With sthen@'s patch I can associate, dhcp, and use net.
> 
> Here's an updated iwn diff with a better approach for Stuart's fix.
> 
> Thanks for helping, Stuart, and to everyone who sent beacons which
> allowed us to narrow this problem down to protection settings being
> set up the wrong way in iwn_run().

Thanks, works well on my T420!

iwn0 at pci2 dev 0 function 0 "Intel Centrino Advanced-N 6205" rev 0x34: msi, 
MIMO 2T2R, MoW, address a0:88:b4:48:4d:a4

iwn0: flags=8843 mtu 1500
lladdr a0:88:b4:48:4d:a4
priority: 4
groups: wlan egress
media: IEEE802.11 autoselect (HT-MCS7 mode 11n)
status: active
ieee80211: nwid ElectricKingdom chan 6 bssid ac:22:0b:32:d5:48
-19dBm wpakey  wpaprotos wpa1,wpa2 wpaakms psk wpaciphers 
tkip,ccmp wpagroupcipher tkip
inet6 fe80::a288:b4ff:fe48:4da4%iwn0 prefixlen 64 scopeid 0x2
inet 10.0.0.3 netmask 0xff00 broadcast 10.0.0.255



Re: mpsafe re(4)

2015-12-06 Thread Dimitris Papastamos
On Sat, Dec 05, 2015 at 06:11:51PM +0100, Jonathan Matthew wrote:
> The main interesting bit here is the txeof and start loops, which previously
> operated based on the prod/cons indices and the contents of the tx queue,
> but now just uses the indices as that's the only way to get a consistent view
> of the tx queue state.
> 
> At the moment I don't think the tx ring is big enough to use IFQ_DEQUEUE
> instead of ifq_deq_begin/commit, but maybe I'm wrong about that.
> 
> can someone try this on an APU1?

I've tested this on my router and it seems to work okay.  I've also used
tcpbench with various combinations.

re0 at pci2 dev 0 function 0 "Realtek 8168" rev 0x0c: RTL8168G/8111G
(0x4c00), msi, address 80:ee:73:9f:1d:3e
re1 at pci3 dev 0 function 0 "Realtek 8168" rev 0x0c: RTL8168G/8111G
(0x4c00), msi, address 80:ee:73:9f:1d:3d



[PATCH] Add a simple roundrobin load balancing feature to rebound(8)

2015-10-28 Thread Dimitris Papastamos
Hi,

I thought it would be cool for rebound(8) to load balance on a number of
DNS servers.

While I was working on this, I did not manage to convince myself as to
whether this should be the default behaviour.

An alternative default would be to use the master server provided.  If
requests started timing out, it would switch to the secondary.  If the
master became available again, then it would switch back to it.

This diff may be useful regardless of what the default should be and
given that there is no config parser at the moment that can handle
options I thought I would send this out to get some feedback.

Cheers,
Dimitris

>From 86c7ad057137181b560341779c79bca339b30e95 Mon Sep 17 00:00:00 2001
From: Dimitris Papastamos <s...@2f30.org>
Date: Wed, 28 Oct 2015 14:45:03 +
Subject: [PATCH] Add a simple roundrobin load balancing feature to rebound(8)

---
 usr.sbin/rebound/rebound.8 |   6 ++-
 usr.sbin/rebound/rebound.c | 105 +++--
 2 files changed, 77 insertions(+), 34 deletions(-)

diff --git a/usr.sbin/rebound/rebound.8 b/usr.sbin/rebound/rebound.8
index e25fc83..d32a5a9 100644
--- a/usr.sbin/rebound/rebound.8
+++ b/usr.sbin/rebound/rebound.8
@@ -28,8 +28,10 @@ The
 daemon proxies DNS requests.
 It listens on localhost and forwards queries to another server.
 .Pp
-At present, the config file consists of a single line containing the next
-hop DNS server.
+At present, the config file consists of lines containing the next hop
+DNS servers.  If more than one server is specified,
+.Nm
+will use them in a roundrobin fashion.
 .Sh SEE ALSO
 .Xr resolv.conf 5 ,
 .Xr unbound 8
diff --git a/usr.sbin/rebound/rebound.c b/usr.sbin/rebound/rebound.c
index 80405ab..7c9c7bd 100644
--- a/usr.sbin/rebound/rebound.c
+++ b/usr.sbin/rebound/rebound.c
@@ -37,6 +37,8 @@
 #include 
 #include 
 
+#define SA(x) ((struct sockaddr *)(x))
+
 uint16_t randomid(void);
 
 static struct timespec now;
@@ -93,6 +95,12 @@ static TAILQ_HEAD(, request) reqfifo;
 static RB_HEAD(reqtree, request) reqtree;
 RB_PROTOTYPE_STATIC(reqtree, request, reqnode, reqcmp)
 
+struct server {
+   struct sockaddr_storage addr;
+   int active;
+   TAILQ_ENTRY(server) entry;
+};
+static TAILQ_HEAD(, server) servers;
 
 static void
 logmsg(int prio, const char *msg, ...)
@@ -125,6 +133,27 @@ logerr(const char *msg, ...)
exit(1);
 }
 
+static void
+nextserver(struct sockaddr_storage *remoteaddr)
+{
+   struct server *s;
+
+   TAILQ_FOREACH(s, , entry)
+   if (s->active)
+   break;
+   if (!s) {
+   s = TAILQ_FIRST();
+   } else {
+   s->active = 0;
+   if (TAILQ_NEXT(s, entry))
+   s = TAILQ_NEXT(s, entry);
+   else
+   s = TAILQ_FIRST();
+   }
+   s->active = 1;
+   memcpy(remoteaddr, >addr, sizeof(*remoteaddr));
+}
+
 static struct dnscache *
 cachelookup(struct dnspacket *dnsreq, size_t reqlen)
 {
@@ -177,8 +206,9 @@ reqcmp(struct request *r1, struct request *r2)
 RB_GENERATE_STATIC(reqtree, request, reqnode, reqcmp)
 
 static struct request *
-newrequest(int ud, struct sockaddr *remoteaddr)
+newrequest(int ud)
 {
+   struct sockaddr_storage remoteaddr;
struct sockaddr from;
socklen_t fromlen;
struct request *req;
@@ -231,10 +261,11 @@ newrequest(int ud, struct sockaddr *remoteaddr)
}
req->cacheent = hit;
 
-   req->s = socket(remoteaddr->sa_family, SOCK_DGRAM, 0);
+   nextserver();
+   req->s = socket(SA()->sa_family, SOCK_DGRAM, 0);
if (req->s == -1)
goto fail;
-   if (connect(req->s, remoteaddr, remoteaddr->sa_len) == -1) {
+   if (connect(req->s, SA(), SA()->sa_len) == -1) {
logmsg(LOG_NOTICE, "failed to connect");
goto fail;
}
@@ -303,8 +334,9 @@ fail:
 }
 
 static struct request *
-newtcprequest(int ld, struct sockaddr *remoteaddr)
+newtcprequest(int ld)
 {
+   struct sockaddr_storage remoteaddr;
struct request *req;
 
if (!(req = calloc(1, sizeof(*req
@@ -321,11 +353,12 @@ newtcprequest(int ld, struct sockaddr *remoteaddr)
if (req->client == -1)
goto fail;
 
-   req->s = socket(remoteaddr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
+   nextserver();
+   req->s = socket(SA()->sa_family, SOCK_STREAM | 
SOCK_NONBLOCK, 0);
if (req->s == -1)
goto fail;
req->phase = 1;
-   if (connect(req->s, remoteaddr, remoteaddr->sa_len) == -1) {
+   if (connect(req->s, SA(), SA()->sa_len) == -1) {
if (errno != EINPROGRESS)
goto fail;
} else {
@@ -340,36 +373,45 @@ fail:
 }
 
 static int
-readconfig(FILE *conf, struct sockaddr_storage *remoteaddr)
+readconfig(FILE *conf)
 {
char buf[1024];
-

Re: [PATCH] Use rbtree for looking up the client in rebound(8)

2015-10-28 Thread Dimitris Papastamos
On Tue, Oct 27, 2015 at 05:20:31PM -0400, Ted Unangst wrote:
> Dimitris Papastamos wrote:
> > There was a comment in the code that indicated that it might be worth
> > investigating the use of trees.  I have not currently done any kind of
> > serious benchmarking on this but I am looking into it.
> 
> nice.
> 
> > +static int
> > +reqcmp(struct request *r1, struct request *r2)
> > +{
> > +   return r1->s - r2->s;
> > +}
> 
> Don't write comparison functions like this. Overflow makes bad things happen.
> The example in the man page is a good way to do it I think.

Ah I see, I've updated the patch.

Index: rebound.c
===
RCS file: /cvs/src/usr.sbin/rebound/rebound.c,v
retrieving revision 1.28
diff -u -p -r1.28 rebound.c
--- rebound.c   26 Oct 2015 12:24:48 -  1.28
+++ rebound.c   28 Oct 2015 08:09:01 -
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -83,11 +84,14 @@ struct request {
socklen_t fromlen;
struct timespec ts;
TAILQ_ENTRY(request) fifo;
+   RB_ENTRY(request) reqnode;
uint16_t clientid;
uint16_t reqid;
struct dnscache *cacheent;
 };
 static TAILQ_HEAD(, request) reqfifo;
+static RB_HEAD(reqtree, request) reqtree;
+RB_PROTOTYPE_STATIC(reqtree, request, reqnode, reqcmp)
 
 
 static void
@@ -144,6 +148,7 @@ freerequest(struct request *req)
struct dnscache *ent;
 
TAILQ_REMOVE(, req, fifo);
+   RB_REMOVE(reqtree, , req);
if (req->client != -1)
close(req->client);
if (req->s != -1)
@@ -164,6 +169,13 @@ freecacheent(struct dnscache *ent)
free(ent);
 }
 
+static int
+reqcmp(struct request *r1, struct request *r2)
+{
+   return (r1->s < r2->s ? -1 : r1->s > r2->s);
+}
+RB_GENERATE_STATIC(reqtree, request, reqnode, reqcmp)
+
 static struct request *
 newrequest(int ud, struct sockaddr *remoteaddr)
 {
@@ -192,6 +204,7 @@ newrequest(int ud, struct sockaddr *remo
return NULL;
 
TAILQ_INSERT_TAIL(, req, fifo);
+   RB_INSERT(reqtree, , req);
req->ts = now;
req->ts.tv_sec += 30;
 
@@ -298,6 +311,7 @@ newtcprequest(int ld, struct sockaddr *r
return NULL;
 
TAILQ_INSERT_TAIL(, req, fifo);
+   RB_INSERT(reqtree, , req);
req->ts = now;
req->ts.tv_sec += 30;
 
@@ -358,7 +372,7 @@ launch(const char *confname, int ud, int
struct sockaddr_storage remoteaddr;
struct kevent chlist[2], kev[4];
struct timespec ts, *timeout = NULL;
-   struct request *req;
+   struct request reqkey, *req;
struct dnscache *ent;
struct passwd *pwd;
FILE *conf;
@@ -438,12 +452,8 @@ launch(const char *confname, int ud, int
kevent(kq, chlist, 1, NULL, 0, NULL);
}
} else if (kev[i].filter == EVFILT_WRITE) {
-   req = TAILQ_FIRST();
-   while (req) {
-   if (req->s == kev[i].ident)
-   break;
-   req = TAILQ_NEXT(req, fifo);
-   }
+   reqkey.s = kev[i].ident;
+   req = RB_FIND(reqtree, , );
if (!req)
logerr("lost request");
req = tcpphasetwo(req);
@@ -455,13 +465,8 @@ launch(const char *confname, int ud, int
kevent(kq, chlist, 2, NULL, 0, NULL);
}
} else if (kev[i].filter == EVFILT_READ) {
-   /* use a tree here? */
-   req = TAILQ_FIRST();
-   while (req) {
-   if (req->s == kev[i].ident)
-   break;
-   req = TAILQ_NEXT(req, fifo);
-   }
+   reqkey.s = kev[i].ident;
+   req = RB_FIND(reqtree, , );
if (!req)
logerr("lost request");
if (req->client == -1)
@@ -547,6 +552,7 @@ main(int argc, char **argv)
if (!debug)
daemon(0, 0);
 
+   RB_INIT();
TAILQ_INIT();
TAILQ_INIT();
 



[PATCH] Use rbtree for looking up the client in rebound(8)

2015-10-27 Thread Dimitris Papastamos
Hi,

There was a comment in the code that indicated that it might be worth
investigating the use of trees.  I have not currently done any kind of
serious benchmarking on this but I am looking into it.

===
RCS file: /cvs/src/usr.sbin/rebound/rebound.c,v
retrieving revision 1.28
diff -u -p -r1.28 rebound.c
--- rebound.c   26 Oct 2015 12:24:48 -  1.28
+++ rebound.c   27 Oct 2015 17:02:36 -
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -83,11 +84,14 @@ struct request {
socklen_t fromlen;
struct timespec ts;
TAILQ_ENTRY(request) fifo;
+   RB_ENTRY(request) reqnode;
uint16_t clientid;
uint16_t reqid;
struct dnscache *cacheent;
 };
 static TAILQ_HEAD(, request) reqfifo;
+static RB_HEAD(reqtree, request) reqtree;
+RB_PROTOTYPE_STATIC(reqtree, request, reqnode, reqcmp)
 
 
 static void
@@ -144,6 +148,7 @@ freerequest(struct request *req)
struct dnscache *ent;
 
TAILQ_REMOVE(, req, fifo);
+   RB_REMOVE(reqtree, , req);
if (req->client != -1)
close(req->client);
if (req->s != -1)
@@ -164,6 +169,13 @@ freecacheent(struct dnscache *ent)
free(ent);
 }
 
+static int
+reqcmp(struct request *r1, struct request *r2)
+{
+   return r1->s - r2->s;
+}
+RB_GENERATE_STATIC(reqtree, request, reqnode, reqcmp)
+
 static struct request *
 newrequest(int ud, struct sockaddr *remoteaddr)
 {
@@ -192,6 +204,7 @@ newrequest(int ud, struct sockaddr *remo
return NULL;
 
TAILQ_INSERT_TAIL(, req, fifo);
+   RB_INSERT(reqtree, , req);
req->ts = now;
req->ts.tv_sec += 30;
 
@@ -298,6 +311,7 @@ newtcprequest(int ld, struct sockaddr *r
return NULL;
 
TAILQ_INSERT_TAIL(, req, fifo);
+   RB_INSERT(reqtree, , req);
req->ts = now;
req->ts.tv_sec += 30;
 
@@ -358,7 +372,7 @@ launch(const char *confname, int ud, int
struct sockaddr_storage remoteaddr;
struct kevent chlist[2], kev[4];
struct timespec ts, *timeout = NULL;
-   struct request *req;
+   struct request reqkey, *req;
struct dnscache *ent;
struct passwd *pwd;
FILE *conf;
@@ -438,12 +452,8 @@ launch(const char *confname, int ud, int
kevent(kq, chlist, 1, NULL, 0, NULL);
}
} else if (kev[i].filter == EVFILT_WRITE) {
-   req = TAILQ_FIRST();
-   while (req) {
-   if (req->s == kev[i].ident)
-   break;
-   req = TAILQ_NEXT(req, fifo);
-   }
+   reqkey.s = kev[i].ident;
+   req = RB_FIND(reqtree, , );
if (!req)
logerr("lost request");
req = tcpphasetwo(req);
@@ -455,13 +465,8 @@ launch(const char *confname, int ud, int
kevent(kq, chlist, 2, NULL, 0, NULL);
}
} else if (kev[i].filter == EVFILT_READ) {
-   /* use a tree here? */
-   req = TAILQ_FIRST();
-   while (req) {
-   if (req->s == kev[i].ident)
-   break;
-   req = TAILQ_NEXT(req, fifo);
-   }
+   reqkey.s = kev[i].ident;
+   req = RB_FIND(reqtree, , );
if (!req)
logerr("lost request");
if (req->client == -1)
@@ -547,6 +552,7 @@ main(int argc, char **argv)
if (!debug)
daemon(0, 0);
 
+   RB_INIT();
TAILQ_INIT();
TAILQ_INIT();
 



Re: [RFC] [PATCH] Implementation of POSIX message queues

2015-09-23 Thread Dimitris Papastamos
On Wed, Sep 23, 2015 at 12:12:01PM -0400, Ted Unangst wrote:
> Dimitris Papastamos wrote:
> > I did some very crude benchmarking on my machine and I could push
> > about 100k messages per second through a single message queue.
> > 
> > At this point, I can start working on manpages + the necessary
> > userspace glue.
> > 
> > Is this patch going in the right direction?
> 
> I don't know. I don't think anybody is very excited about mq...
> 
> A lot of openbsd daemons use the imsg framework that we've built, which I
> think overlaps here. imsg only exists on OpenBSD, but mechanism it's built on
> is portable, just basic unix stuff.
> 
> Some analysis or discussion about how imsg could be improved or replaced by
> mq would be interesting. Otherwise this looks like a feature we won't be
> using, nor is it widely used by others.
> 
> I think the openbsd position on posix conformance is to try to conform
> for the features we have, but not try very hard to chase down all the latest
> additions. We'd prefer in some ways to be a bit behind the times.

Interesting, thanks for getting back to me.  I will look into imsg.

Do you have a TODO on outstanding posix conformance type of work for already
implemented interfaces?  I am interested to help in this area.



Re: [RFC] [PATCH] Implementation of POSIX message queues

2015-09-21 Thread Dimitris Papastamos
Hi everyone,

I had a chance to test this patch more and iron out some bugs.
The main changes from the previous version are:

* Uses pool(9) for allocations smaller than MQ_DEF_MSGSIZE.

* Add poll support.

* Fix a memory leak in domqrecv() that would eventually exhaust
  all available kernel memory.

* Return value of mq_receive() was always 0.  Fix it to return
  the length of the message that was dequeued.

* Small stylistic changes.

* Tune the default limits and add MQ_PRIO_MAX and MQ_OPEN_MAX.

I did some very crude benchmarking on my machine and I could push
about 100k messages per second through a single message queue.

At this point, I can start working on manpages + the necessary
userspace glue.

Is this patch going in the right direction?

Thanks,
Dimitris

>From a4a59e77788b6ac9340c5707b04cd924ab23c923 Mon Sep 17 00:00:00 2001
From: Dimitris Papastamos <s...@2f30.org>
Date: Tue, 15 Sep 2015 16:54:34 +0100
Subject: [PATCH] Initial implementation of mq_*() interfaces

---
 conf/files   |   1 +
 kern/init_main.c |   4 +
 kern/sys_mqueue.c| 746 +++
 kern/syscalls.master |  14 +
 sys/_types.h |   1 +
 sys/file.h   |   1 +
 sys/mqueue.h |  33 +++
 sys/syslimits.h  |   2 +
 sys/types.h  |   1 +
 9 files changed, 803 insertions(+)
 create mode 100644 kern/sys_mqueue.c
 create mode 100644 sys/mqueue.h

diff --git a/conf/files b/conf/files
index 691d7f2..f3d5bfc 100644
--- a/conf/files
+++ b/conf/files
@@ -694,6 +694,7 @@ file kern/subr_prof.c
 file kern/subr_userconf.c  boot_config
 file kern/subr_xxx.c
 file kern/sys_generic.c
+file kern/sys_mqueue.c
 file kern/sys_pipe.c
 file kern/sys_process.cptrace | systrace
 file kern/sys_socket.c
diff --git a/kern/init_main.c b/kern/init_main.c
index e2505ea..cc323d9 100644
--- a/kern/init_main.c
+++ b/kern/init_main.c
@@ -67,6 +67,7 @@
 #ifdef SYSVSEM
 #include 
 #endif
+#include 
 #ifdef SYSVMSG
 #include 
 #endif
@@ -376,6 +377,9 @@ main(void *framep)
seminit();
 #endif
 
+   /* Initialize POSIX message queues. */
+   mqinit();
+
 #ifdef SYSVMSG
/* Initialize System V style message queues. */
msginit();
diff --git a/kern/sys_mqueue.c b/kern/sys_mqueue.c
new file mode 100644
index 000..ddd5d17
--- /dev/null
+++ b/kern/sys_mqueue.c
@@ -0,0 +1,746 @@
+/*
+ * Copyright (c) 2015 Dimitris Papastamos <s...@2f30.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MQ_NAMELEN (NAME_MAX + 1)
+#define MQ_DEF_MSGSIZE 2048
+#define MQ_MAX_MSGSIZE (8 * MQ_DEF_MSGSIZE)
+#define MQ_DEF_MAXMSG  8
+#define MQ_MAX_MAXMSG  (8 * MQ_DEF_MAXMSG)
+
+/* Stored in mq_flags when message queue is marked for removal. */
+#define MQ_DYING   0x1000
+
+struct mq {
+   char   name[MQ_NAMELEN];/* mqueue name */
+   intoflag;   /* mqueue send/recv access flag */
+   mode_t mode;/* mqueue permissions */
+   struct mq_attr attr;/* mqueue attributes, see below */
+   intrchan;   /* read channel to sleep on */
+   intwchan;   /* write channel to sleep on */
+   uid_t  euid;/* mqueue euid owner */
+   gid_t  egid;/* mqueue egid owner */
+   u_int  refcnt;  /* mqueue reference count */
+#define MQ_RSEL 0x1
+#define MQ_WSEL 0x2
+   intpflag;   /* poll internal flags */
+   struct selinfo rsel;/* for compat with select */
+   struct selinfo wsel;
+   struct rwlock  lock;/* mqueue lock */
+   TAILQ_HEAD(, mq_msg) head;  /* mqueue message list */
+   LIST_ENTRY(mq)   entry; /* link with next mqueue */
+};
+
+struct mq_msg {
+   TAILQ_ENTRY(mq_msg) entry;  /* link with next message */
+   size_t   len;   /* message length */
+   u_intprio;  /* message priority */
+   uint8_t  data[];/* raw message data */
+};

[RFC] [PATCH] Implementation of POSIX message queues

2015-09-17 Thread Dimitris Papastamos
Hi everyone,

I've written a patch that adds support for POSIX message queues.  I
started this implementation because I wanted to play around with this
API in a hobby project.  The patch is not yet fully tested but the
main functionality is there to warrant some feedback on both the
implementation and the usefulness of the API.

I've specifically left out the implementation of mq_notify(2) because
OpenBSD lacks sigevent support.  I am planning to look into that if this
work is of interest.  The notification facility of the POSIX mqueue API
is one of its strengths compared to the SysV API.

Depending on the feedback, I am planning to work on the following items:

* Rethink default limits with respect to the default message size, maximum
  message size, default maximum # of messages and absolute maximum # of 
messages.
  Do we need sysctl knobs?

* Respect MQ_OPEN_MAX.  The implementation uses file descriptors internally
  so currently the fd limits apply but it may be desirable to have stricter
  default limits for message queues.

* Hook into poll() and kqfilter().  Non-standard extension.

* Include regress tests for the API and test it against real software
  that depend on this API.

* Think about how to list the currently live message queues.
  This can be done with a tool similar to ipcs(1).  It can also be done
  by exposing the message queues on the filesystem.

* Implement mq_notify(2) and the related sigevent infrastructure.  This
  maybe can be done incrementally.  Unable to estimate how much work
  it is involved to get this working.

* Consider implementing mq_send(3) on top of mq_timedsend(2) in userspace.
  Similarly for mq_receive(3).

* Consider using the pool(9) API for messages that are smaller or
  equal to the default message size.

* Simplify sys_mq_open().  Clean up the rest of the code as much as possible.
  Do proper style(9) checks.

* Cross-check with POSIX to ensure semantics are properly implemented.

Overall, I think the POSIX message queue API is cleaner and easier to
use than the SysV one.  The main and obvious downside is that not much
software is using it currently.

Let me know what you think!

Thanks,
Dimitris

>From 6b2ed6b728de3c2ca8ab17fca527303b44840ba0 Mon Sep 17 00:00:00 2001
From: Dimitris Papastamos <s...@2f30.org>
Date: Tue, 15 Sep 2015 16:54:34 +0100
Subject: [PATCH] Initial implementation of mq_*() interfaces

---
 conf/files   |   1 +
 kern/init_main.c |   4 +
 kern/sys_mqueue.c| 724 +++
 kern/syscalls.master |  14 +
 sys/_types.h |   1 +
 sys/file.h   |   1 +
 sys/mqueue.h |  33 +++
 sys/types.h  |   1 +
 8 files changed, 779 insertions(+)
 create mode 100644 kern/sys_mqueue.c
 create mode 100644 sys/mqueue.h

diff --git a/conf/files b/conf/files
index 691d7f2..f3d5bfc 100644
--- a/conf/files
+++ b/conf/files
@@ -694,6 +694,7 @@ file kern/subr_prof.c
 file kern/subr_userconf.c  boot_config
 file kern/subr_xxx.c
 file kern/sys_generic.c
+file kern/sys_mqueue.c
 file kern/sys_pipe.c
 file kern/sys_process.cptrace | systrace
 file kern/sys_socket.c
diff --git a/kern/init_main.c b/kern/init_main.c
index e2505ea..cc323d9 100644
--- a/kern/init_main.c
+++ b/kern/init_main.c
@@ -67,6 +67,7 @@
 #ifdef SYSVSEM
 #include 
 #endif
+#include 
 #ifdef SYSVMSG
 #include 
 #endif
@@ -376,6 +377,9 @@ main(void *framep)
seminit();
 #endif
 
+   /* Initialize POSIX message queues. */
+   mqinit();
+
 #ifdef SYSVMSG
/* Initialize System V style message queues. */
msginit();
diff --git a/kern/sys_mqueue.c b/kern/sys_mqueue.c
new file mode 100644
index 000..d85cda0
--- /dev/null
+++ b/kern/sys_mqueue.c
@@ -0,0 +1,724 @@
+/*
+ * Copyright (c) 2015 Dimitris Papastamos <s...@2f30.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MQ_NAMELEN (NAME_MAX + 1)
+#define MQ_DEF_MSGSIZE 1024
+/* Stored in mq_flags when message queue is marked for removal. */
+#define MQ_DYING   0x1000
+
+struct mq {
+   char   name[MQ_NAMELEN];
+   into

Re: [PATCH] Initial implementation of posix_fallocate(2)

2015-09-13 Thread Dimitris Papastamos
Hi,

A slightly updated patch.  Removed some checks that were
redundant.

Index: kern/init_sysent.c
===
RCS file: /cvs/src/sys/kern/init_sysent.c,v
retrieving revision 1.171
diff -u -p -r1.171 init_sysent.c
--- kern/init_sysent.c  9 Sep 2015 17:57:57 -   1.171
+++ kern/init_sysent.c  13 Sep 2015 15:47:11 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: init_sysent.c,v 1.171 2015/09/09 17:57:57 deraadt Exp $   
*/
+/* $OpenBSD$   */
 
 /*
  * System call switch table.
@@ -751,5 +751,7 @@ struct sysent sysent[] = {
sys___set_tcb },/* 329 = __set_tcb */
{ 0, 0, SY_NOLOCK | 0,
sys___get_tcb },/* 330 = __get_tcb */
+   { 3, s(struct sys_posix_fallocate_args), 0,
+   sys_posix_fallocate },  /* 331 = posix_fallocate */
 };
 
Index: kern/syscalls.c
===
RCS file: /cvs/src/sys/kern/syscalls.c,v
retrieving revision 1.170
diff -u -p -r1.170 syscalls.c
--- kern/syscalls.c 9 Sep 2015 17:57:57 -   1.170
+++ kern/syscalls.c 13 Sep 2015 15:47:11 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscalls.c,v 1.170 2015/09/09 17:57:57 deraadt Exp $  */
+/* $OpenBSD$   */
 
 /*
  * System call names.
@@ -393,4 +393,5 @@ char *syscallnames[] = {
"#328 (obsolete __tfork51)",/* 328 = obsolete __tfork51 */
"__set_tcb",/* 329 = __set_tcb */
"__get_tcb",/* 330 = __get_tcb */
+   "posix_fallocate",  /* 331 = posix_fallocate */
 };
Index: kern/syscalls.master
===
RCS file: /cvs/src/sys/kern/syscalls.master,v
retrieving revision 1.158
diff -u -p -r1.158 syscalls.master
--- kern/syscalls.master9 Sep 2015 17:56:59 -   1.158
+++ kern/syscalls.master13 Sep 2015 15:47:12 -
@@ -561,3 +561,4 @@
 328OBSOL   __tfork51
 329STD NOLOCK  { void sys___set_tcb(void *tcb); }
 330STD NOLOCK  { void *sys___get_tcb(void); }
+331STD { int sys_posix_fallocate(int fd, off_t offset, off_t 
len); }
Index: kern/vfs_syscalls.c
===
RCS file: /cvs/src/sys/kern/vfs_syscalls.c,v
retrieving revision 1.227
diff -u -p -r1.227 vfs_syscalls.c
--- kern/vfs_syscalls.c 31 Aug 2015 16:13:11 -  1.227
+++ kern/vfs_syscalls.c 13 Sep 2015 15:47:12 -
@@ -3023,3 +3023,40 @@ sys_pwritev(struct proc *p, void *v, reg
1, , retval));
 }
 
+int
+sys_posix_fallocate(struct proc *p, void *v, register_t *retval)
+{
+   struct sys_posix_fallocate_args /* {
+   syscallarg(int) fd;
+   syscallarg(off_t) offset;
+   syscallarg(off_t) len;
+   } */ *uap = v;
+   struct vnode *vp;
+   struct file *fp;
+   int error;
+
+   if (SCARG(uap, offset) < 0 || SCARG(uap, len) < 0)
+   return (EINVAL);
+   if (SCARG(uap, offset) > LLONG_MAX - SCARG(uap, len))
+   return (EFBIG);
+
+   if ((error = getvnode(p, SCARG(uap, fd), )) != 0)
+   return (error);
+   if ((fp->f_flag & FWRITE) == 0) {
+   error = EBADF;
+   goto bad;
+   }
+   vp = fp->f_data;
+   vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
+   if (vp->v_type == VFIFO)
+   error = ESPIPE;
+   else if (vp->v_type != VREG)
+   error = ENODEV;
+   else if ((error = vn_writechk(vp)) == 0)
+   error = vn_fallocate(vp, SCARG(uap, offset),
+SCARG(uap, len), p);
+   VOP_UNLOCK(vp, 0, p);
+bad:
+   FRELE(fp, p);
+   return (error);
+}
Index: kern/vfs_vnops.c
===
RCS file: /cvs/src/sys/kern/vfs_vnops.c,v
retrieving revision 1.82
diff -u -p -r1.82 vfs_vnops.c
--- kern/vfs_vnops.c1 May 2015 01:30:58 -   1.82
+++ kern/vfs_vnops.c13 Sep 2015 15:47:12 -
@@ -573,3 +573,70 @@ vn_isunder(struct vnode *lvp, struct vno
 
return (0);
 }
+
+int
+vn_fallocate(struct vnode *vp, off_t offset, off_t len, struct proc *p)
+{
+   struct vattr va;
+   uint8_t *buf;
+   off_t cur, fsize;
+   long blksize;
+   size_t resid;
+   int error;
+
+   if ((error = VOP_GETATTR(vp, , p->p_ucred, p)) != 0)
+   return (error);
+   fsize = va.va_size;
+   blksize = va.va_blocksize;
+
+   if (offset + len > fsize) {
+   /*
+* Check if the filesystem supports the resulting
+* filesystem size.
+*/
+   VATTR_NULL();
+   va.va_size = offset + len;
+   if ((error = VOP_SETATTR(vp, , p->p_ucred, p)) != 0)
+   return (error);
+
+   /* 

[PATCH] Initial implementation of posix_fallocate(2)

2015-09-12 Thread Dimitris Papastamos
Hi everyone,

I've done a basic implementation of posix_fallocate(2).  I've looked
at both the FreeBSD and NetBSD code when doing this.  My knowledge of
the VFS layer is very limited, so please let me know if anything looks
wrong.

I added a generic function in vfs_vnops.c called vn_fallocate() that
uses VOP_READ() and VOP_WRITE() underneath.  It does not take
advantage of filesystem specific optimizations (there's no
VOP_FALLOCATE or similar). 

If this looks good, then I will progress on writing a manpage and
adding the necessary libc glue.

In theory this diff could have been split in two, one that adds
vn_fallocate() and acts as a no-op and another patch on top of it that
implements posix_fallocate(2).  Let me know if you need me to split
the diff. 

Thanks,
Dimitris

Index: kern/init_sysent.c
===
RCS file: /cvs/src/sys/kern/init_sysent.c,v
retrieving revision 1.171
diff -u -p -r1.171 init_sysent.c
--- kern/init_sysent.c  9 Sep 2015 17:57:57 -   1.171
+++ kern/init_sysent.c  12 Sep 2015 17:37:54 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: init_sysent.c,v 1.171 2015/09/09 17:57:57 deraadt Exp $   
*/
+/* $OpenBSD$   */
 
 /*
  * System call switch table.
@@ -751,5 +751,7 @@ struct sysent sysent[] = {
sys___set_tcb },/* 329 = __set_tcb */
{ 0, 0, SY_NOLOCK | 0,
sys___get_tcb },/* 330 = __get_tcb */
+   { 3, s(struct sys_posix_fallocate_args), 0,
+   sys_posix_fallocate },  /* 331 = posix_fallocate */
 };
 
Index: kern/syscalls.c
===
RCS file: /cvs/src/sys/kern/syscalls.c,v
retrieving revision 1.170
diff -u -p -r1.170 syscalls.c
--- kern/syscalls.c 9 Sep 2015 17:57:57 -   1.170
+++ kern/syscalls.c 12 Sep 2015 17:37:54 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscalls.c,v 1.170 2015/09/09 17:57:57 deraadt Exp $  */
+/* $OpenBSD$   */
 
 /*
  * System call names.
@@ -393,4 +393,5 @@ char *syscallnames[] = {
"#328 (obsolete __tfork51)",/* 328 = obsolete __tfork51 */
"__set_tcb",/* 329 = __set_tcb */
"__get_tcb",/* 330 = __get_tcb */
+   "posix_fallocate",  /* 331 = posix_fallocate */
 };
Index: kern/syscalls.master
===
RCS file: /cvs/src/sys/kern/syscalls.master,v
retrieving revision 1.158
diff -u -p -r1.158 syscalls.master
--- kern/syscalls.master9 Sep 2015 17:56:59 -   1.158
+++ kern/syscalls.master12 Sep 2015 17:37:54 -
@@ -561,3 +561,4 @@
 328OBSOL   __tfork51
 329STD NOLOCK  { void sys___set_tcb(void *tcb); }
 330STD NOLOCK  { void *sys___get_tcb(void); }
+331STD { int sys_posix_fallocate(int fd, off_t offset, off_t 
len); }
Index: kern/vfs_syscalls.c
===
RCS file: /cvs/src/sys/kern/vfs_syscalls.c,v
retrieving revision 1.227
diff -u -p -r1.227 vfs_syscalls.c
--- kern/vfs_syscalls.c 31 Aug 2015 16:13:11 -  1.227
+++ kern/vfs_syscalls.c 12 Sep 2015 17:37:54 -
@@ -3023,3 +3023,52 @@ sys_pwritev(struct proc *p, void *v, reg
1, , retval));
 }
 
+int
+sys_posix_fallocate(struct proc *p, void *v, register_t *retval)
+{
+   struct sys_posix_fallocate_args /* {
+   syscallarg(int) fd;
+   syscallarg(off_t) offset;
+   syscallarg(off_t) len;
+   } */ *uap = v;
+   struct vnode *vp;
+   struct file *fp;
+   int error;
+
+   if (SCARG(uap, offset) < 0 || SCARG(uap, len) < 0)
+   return (EINVAL);
+   if (SCARG(uap, offset) > LLONG_MAX - SCARG(uap, len))
+   return (EFBIG);
+
+   if ((error = getvnode(p, SCARG(uap, fd), )) != 0)
+   return (error);
+
+   switch (fp->f_type) {
+   case DTYPE_VNODE:
+   break;
+   case DTYPE_PIPE:
+   case VFIFO:
+   error = ESPIPE;
+   goto bad;
+   default:
+   error = ENODEV;
+   goto bad;
+   }
+
+   if ((fp->f_flag & FWRITE) == 0) {
+   error = EBADF;
+   goto bad;
+   }
+
+   vp = fp->f_data;
+   vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
+   if (vp->v_type != VREG)
+   error = ENODEV;
+   else if ((error = vn_writechk(vp)) == 0)
+   error = vn_fallocate(vp, SCARG(uap, offset),
+SCARG(uap, len), p);
+   VOP_UNLOCK(vp, 0, p);
+bad:
+   FRELE(fp, p);
+   return (error);
+}
Index: kern/vfs_vnops.c
===
RCS file: /cvs/src/sys/kern/vfs_vnops.c,v
retrieving revision 1.82
diff -u -p -r1.82 vfs_vnops.c
--- kern/vfs_vnops.c1 May 2015 01:30:58 

Re: [PATCH] Initial implementation of posix_fallocate(2)

2015-09-12 Thread Dimitris Papastamos
On Sat, Sep 12, 2015 at 06:57:48PM +0100, Dimitris Papastamos wrote:
> + switch (fp->f_type) {
> + case DTYPE_VNODE:
> + break;
> + case DTYPE_PIPE:
> + case VFIFO:

VFIFO shouldn't be here, I have moved it further down after vn_lock
to check it against v_type.



trunk weirdness

2015-09-05 Thread Dimitris Papastamos
Hi all,

I have a trunk failover setup between my wired and wireless
connection.  My wired is master and my wireless is slave.

My config is as follows:

==> /etc/hostname.em0 <==
up

==> /etc/hostname.iwn0 <==
nwid FOO
wpakey XXX
up

==> /etc/hostname.trunk0 <==
trunkproto failover
trunkport em0
trunkport iwn0
inet 10.0.0.3 255.255.255.0
up

Scenario 1 - boot with em0 active
1) pinging via em0 works
2) switch to iwn0, ping doesn't work <-- problem
3) switch to em0, ping works
4) sh /etc/netstart
5) pinging via em0 works
6) switch to iwn0, ping works

If instead, I boot with iwn0 active, then transitions
work as expected.

OpenBSD 5.8-current (GENERIC.MP) #14: Sat Sep  5 11:36:39 BST 2015
r...@luna.2f30.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 8451125248 (8059MB)
avail mem = 8191094784 (7811MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP SLIC SSDT SSDT SSDT HPET APIC MCFG ECDT ASF! TCPA SSDT 
SSDT DMAR UEFI UEFI UEFI
acpi0: wakeup devices LID_(S3) SLPB(S3) IGBE(S4) EXP4(S4) EHC1(S3) EHC2(S3) 
HDEF(S4)
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpihpet0 at acpi0: 14318179 Hz
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.96 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC,SENSOR,ARAT
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu0: mwait min=64, max=64, C-substates=0.2.1.1.2, IBE
cpu1 at mainbus0: apid 1 (application processor)
cpu1: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC,SENSOR,ARAT
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 1, core 0, package 0
cpu2 at mainbus0: apid 2 (application processor)
cpu2: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC,SENSOR,ARAT
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 0, core 1, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC,SENSOR,ARAT
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 1, core 1, package 0
ioapic0 at mainbus0: apid 2 pa 0xfec0, version 20, 24 pins
acpimcfg0 at acpi0 addr 0xf800, bus 0-63
acpiec0 at acpi0
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus -1 (PEG_)
acpiprt2 at acpi0: bus 2 (EXP1)
acpiprt3 at acpi0: bus 3 (EXP2)
acpiprt4 at acpi0: bus -1 (EXP4)
acpiprt5 at acpi0: bus 13 (EXP5)
acpicpu0 at acpi0: C3(350@104 io@0x415), C1(1000@1 halt), PSS
acpicpu1 at acpi0: C3(350@104 io@0x415), C1(1000@1 halt), PSS
acpicpu2 at acpi0: C3(350@104 io@0x415), C1(1000@1 halt), PSS
acpicpu3 at acpi0: C3(350@104 io@0x415), C1(1000@1 halt), PSS
acpipwrres0 at acpi0: PUBS, resource for EHC1, EHC2
acpitz0 at acpi0: critical temperature is 98 degC
acpibtn0 at acpi0: LID_
acpibtn1 at acpi0: SLPB
acpibat0 at acpi0: BAT0 model "45N1173" serial 26283 type LION oem "SANYO"
acpibat1 at acpi0: BAT1 not present
acpiac0 at acpi0: AC unit online
acpithinkpad0 at acpi0
cpu0: Enhanced SpeedStep 2591 MHz: speeds: 2601, 2600, 2400, 2200, 2000, 1800, 
1600, 1400, 1200, 1000, 800 MHz
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 "Intel Core 2G Host" rev 0x09
vga1 at pci0 dev 2 function 0 "Intel HD Graphics 3000" rev 0x09
intagp at vga1 not configured
inteldrm0 at vga1
drm0 at inteldrm0
inteldrm0: 1600x900
wsdisplay0 at vga1 mux 1: console (std, vt100 emulation)
wsdisplay0: screen 1-5 added (std, vt100 emulation)
"Intel 6 Series MEI" rev 0x04 at pci0 dev 22 function 0 not configured
puc0 at pci0 dev 22 function 3 "Intel 6 Series KT" rev 0x04: ports: 1 com
com4 at puc0 port 0 apic 2 int 19: ns16550a, 16 byte fifo
com4: probed fifo depth: 0 bytes
em0 at pci0 dev 25 function 0 "Intel 82579LM" rev 0x04: msi, address 
00:21:cc:5d:e1:40
ehci0 at pci0 dev 26 function 0 "Intel 6 Series USB" rev 

Re: trunk weirdness

2015-09-05 Thread Dimitris Papastamos
On Sat, Sep 05, 2015 at 12:04:20PM +0100, Dimitris Papastamos wrote:
> Scenario 1 - boot with em0 active
> 1) pinging via em0 works
> 2) switch to iwn0, ping doesn't work <-- problem
> 3) switch to em0, ping works
> 4) sh /etc/netstart
> 5) pinging via em0 works
> 6) switch to iwn0, ping works

Note that if I tcpdump on trunk0, I do not even see the echo
request in step 2.



Re: trunk weirdness

2015-09-05 Thread Dimitris Papastamos
On Sat, Sep 05, 2015 at 12:39:21PM +0100, Stuart Henderson wrote:
> On 2015/09/05 12:13, Dimitris Papastamos wrote:
> > On Sat, Sep 05, 2015 at 12:04:20PM +0100, Dimitris Papastamos wrote:
> > > Scenario 1 - boot with em0 active
> > > 1) pinging via em0 works
> > > 2) switch to iwn0, ping doesn't work <-- problem
> > > 3) switch to em0, ping works
> > > 4) sh /etc/netstart
> > > 5) pinging via em0 works
> > > 6) switch to iwn0, ping works
> > 
> > Note that if I tcpdump on trunk0, I do not even see the echo
> > request in step 2.
> > 
> 
> Showing output of "ifconfig -A" at various stages might give some clue.

Sorry yes I forgot to mention this.  As you can see in the output below
there is no difference between ifconfig -A in the problematic and working
case except the signal strength.

I also just realized that I made a mistake in Scenario 1.  To get
iwn0 to work in this trunk setup, I need to unplug the cable and _then_
run sh /etc/netstart.

1) pinging via em0 works
2) switch to iwn0, ping doesn't work <-- problem
3) sh /etc/netstart
4) pinging via iwn0 works

>From this point on all further transitions work.

The ifconfig -A output for step 1:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 32768
priority: 0
groups: lo
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
inet 127.0.0.1 netmask 0xff00
em0: flags=8b43<UP,BROADCAST,RUNNING,PROMISC,ALLMULTI,SIMPLEX,MULTICAST> mtu 
1500
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkdev trunk0
media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause)
status: active
iwn0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
lladdr aa:bb:cc:dd:ee:ff
priority: 4
trunk: trunkdev trunk0
groups: wlan
media: IEEE802.11 autoselect (DS1 mode 11g)
status: active
ieee80211: nwid FOO chan 6 bssid 00:11:22:33:44:55 -66dBm wpakey  wpaprotos wpa1,wpa2 wpaakms psk wpaciphers tkip,ccmp wpagroupcipher 
tkip
enc0: flags=0<>
priority: 0
groups: enc
status: active
trunk0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkproto failover
trunkport iwn0 
trunkport em0 master,active
groups: trunk egress
media: Ethernet autoselect
status: active
inet 10.0.0.3 netmask 0xff00 broadcast 10.0.0.255
pflog0: flags=141<UP,RUNNING,PROMISC> mtu 33144
priority: 0
groups: pflog

Diff between step 1 and step 2 (problematic case):

--- 1   Sat Sep  5 13:07:55 2015
+++ 2   Sat Sep  5 13:07:55 2015
@@ -8,8 +8,8 @@
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkdev trunk0
-   media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause)
-   status: active
+   media: Ethernet autoselect (none)
+   status: no carrier
 iwn0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
lladdr aa:bb:cc:dd:ee:ff
priority: 4
@@ -26,8 +26,8 @@
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkproto failover
-   trunkport iwn0 
-   trunkport em0 master,active
+   trunkport iwn0 active
+   trunkport em0 master
groups: trunk egress
media: Ethernet autoselect
status: active

Diff between step 1 and step 4 (working case):

--- 1   Sat Sep  5 13:07:55 2015
+++ 5   Sat Sep  5 13:07:55 2015
@@ -8,8 +8,8 @@
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkdev trunk0
-   media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause)
-   status: active
+   media: Ethernet autoselect (none)
+   status: no carrier
 iwn0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
lladdr aa:bb:cc:dd:ee:ff
priority: 4
@@ -17,7 +17,7 @@
groups: wlan
media: IEEE802.11 autoselect (DS1 mode 11g)
status: active
-   ieee80211: nwid FOO chan 6 bssid 00:11:22:33:44:55 -66dBm wpakey  wpaprotos wpa1,wpa2 wpaakms psk wpaciphers tkip,ccmp wpagroupcipher 
tkip
+   ieee80211: nwid FOO chan 6 bssid 00:11:22:33:44:55 -87dBm wpakey  wpaprotos wpa1,wpa2 wpaakms psk wpaciphers tkip,ccmp wpagroupcipher 
tkip
 enc0: flags=0<>
priority: 0
groups: enc
@@ -26,8 +26,8 @@
lladdr aa:bb:cc:dd:ee:ff
priority: 0
trunk: trunkproto failover
-   trunkport iwn0 
-   trunkport em0 master,active
+   trunkport iwn0 active
+   trunkport em0 master
groups: trunk egress
media: Ethernet autoselect
status: active



[PATCH] Fix segmentation fault in ping(8)

2015-08-05 Thread Dimitris Papastamos
Hi,

You can reproduce with ping -s 0 host (it crashes if the packet size
is less than 24).

===
RCS file: /cvs/src/sbin/ping/ping.c,v
retrieving revision 1.123
diff -u -p -r1.123 ping.c
--- ping.c  2 May 2015 18:03:37 -   1.123
+++ ping.c  5 Aug 2015 08:40:22 -
@@ -803,7 +803,7 @@ pr_pack(char *buf, int cc, struct sockad
(void)printf( (TRUNC!));
cp = (u_char *)icp-icmp_data[sizeof(struct payload)];
dp = outpack[8 + sizeof(struct payload)];
-   if (!(options  F_PINGFILLED)) {
+   if (!(options  F_PINGFILLED)  datalen  
sizeof(payload)) {
chacha_ivsetup(fill_stream, payload.mac);
chacha_encrypt_bytes(fill_stream, dp, dp,
datalen - sizeof(payload));



Re: [PATCH] Enable -f in ndp(8)

2015-07-25 Thread Dimitris Papastamos
On Sat, Jul 25, 2015 at 09:20:18PM +0200, Martin Pieuchot wrote:
 On 13/07/15(Mon) 14:04, Dimitris Papastamos wrote:
  Hi,
  
  I noticed -f in ndp(8) did nothing at all so I've enabled it and
  documented the file syntax in the man page.
 
 If it does nothing, I'd say let's kill it.
 

Index: ndp.8
===
RCS file: /cvs/src/usr.sbin/ndp/ndp.8,v
retrieving revision 1.33
diff -u -p -r1.33 ndp.8
--- ndp.8   3 Sep 2014 10:39:41 -   1.33
+++ ndp.8   25 Jul 2015 20:10:08 -
@@ -44,7 +44,6 @@
 .Op Fl H | P | R
 .Op Fl A Ar wait
 .Op Fl d Ar hostname
-.Op Fl f Ar filename
 .Op Fl i Ar interface Op Ar flag ...
 .Op Fl s Ar nodename etheraddr Oo Ic temp Oc Op Ic proxy
 .Op Fl V Ar rdomain
@@ -119,9 +118,6 @@ the node has sent during the current sta
 Erase all the NDP entries.
 .It Fl d Ar hostname
 Delete the specified NDP entry.
-.It Fl f Ar filename
-Parse the file specified by
-.Ar filename .
 .It Fl H
 Harmonize consistency between the routing table and the default router
 list; install the top entry of the list into the kernel routing table.
Index: ndp.c
===
RCS file: /cvs/src/usr.sbin/ndp/ndp.c,v
retrieving revision 1.61
diff -u -p -r1.61 ndp.c
--- ndp.c   3 Jun 2015 08:10:53 -   1.61
+++ ndp.c   25 Jul 2015 20:10:08 -
@@ -123,7 +123,6 @@ char ntop_buf[INET6_ADDRSTRLEN];/* inet
 char host_buf[NI_MAXHOST]; /* getnameinfo() */
 char ifix_buf[IFNAMSIZ];   /* if_indextoname() */
 
-int file(char *);
 void getsocket(void);
 int set(int, char **);
 void get(char *);
@@ -294,41 +293,6 @@ main(int argc, char *argv[])
exit(0);
 }
 
-/*
- * Process a file to set standard ndp entries
- */
-int
-file(char *name)
-{
-   FILE *fp;
-   int i, retval;
-   char line[100], arg[5][50], *args[5];
-
-   if ((fp = fopen(name, r)) == NULL) {
-   fprintf(stderr, ndp: cannot open %s\n, name);
-   exit(1);
-   }
-   args[0] = arg[0][0];
-   args[1] = arg[1][0];
-   args[2] = arg[2][0];
-   args[3] = arg[3][0];
-   args[4] = arg[4][0];
-   retval = 0;
-   while (fgets(line, sizeof(line), fp) != NULL) {
-   i = sscanf(line, %49s %49s %49s %49s %49s,
-   arg[0], arg[1], arg[2], arg[3], arg[4]);
-   if (i  2) {
-   fprintf(stderr, ndp: bad line: %s\n, line);
-   retval = 1;
-   continue;
-   }
-   if (set(i, args))
-   retval = 1;
-   }
-   fclose(fp);
-   return (retval);
-}
-
 void
 getsocket(void)
 {
@@ -792,7 +756,7 @@ usage(void)
 {
printf(usage: ndp [-nrt] [-a | -c | -p] [-H | -P | -R] );
printf([-A wait] [-d hostname]\n);
-   printf(\t[-f filename] [-i interface [flag ...]]\n);
+   printf(\t[-i interface [flag ...]]\n);
printf(\t[-s nodename etheraddr [temp] [proxy]] );
printf([-V rdomain] [hostname]\n);
exit(1);



Re: [PATCH] Enable -f in ndp(8)

2015-07-22 Thread Dimitris Papastamos
On Mon, Jul 13, 2015 at 02:04:40PM +0100, Dimitris Papastamos wrote:
 Hi,
 
 I noticed -f in ndp(8) did nothing at all so I've enabled it and
 documented the file syntax in the man page.
 
 ===
 RCS file: /cvs/src/usr.sbin/ndp/ndp.8,v
 retrieving revision 1.33
 diff -u -p -r1.33 ndp.8
 --- ndp.8 3 Sep 2014 10:39:41 -   1.33
 +++ ndp.8 13 Jul 2015 13:02:49 -
 @@ -122,6 +122,12 @@ Delete the specified NDP entry.
  .It Fl f Ar filename
  Parse the file specified by
  .Ar filename .
 +Entries in the file should be of the form:
 +.Bd -ragged -offset indent -compact
 +.Ar nodename etheraddr
 +.Op Ar temp
 +.Op Ar proxy
 +.Ed
  .It Fl H
  Harmonize consistency between the routing table and the default router
  list; install the top entry of the list into the kernel routing table.
 Index: ndp.c
 ===
 RCS file: /cvs/src/usr.sbin/ndp/ndp.c,v
 retrieving revision 1.61
 diff -u -p -r1.61 ndp.c
 --- ndp.c 3 Jun 2015 08:10:53 -   1.61
 +++ ndp.c 13 Jul 2015 13:02:49 -
 @@ -241,6 +241,8 @@ main(int argc, char *argv[])
   }
   delete(arg);
   break;
 + case 'f':
 + exit(file(arg) ? 1 : 0);
   case 'p':
   if (argc != 0) {
   usage();
 

Is this ok?  Looking at it, it should just be exit(file(arg)) as file()
only returns 0 for success and 1 on failure.



[PATCH] Do not intend cases in switch for doas(1)

2015-07-21 Thread Dimitris Papastamos
Hi,

Just a minor stylistic change.

Cheers,
Dimitris

===
RCS file: /cvs/src/usr.bin/doas/parse.y,v
retrieving revision 1.7
diff -u -p -r1.7 parse.y
--- parse.y 21 Jul 2015 11:04:06 -  1.7
+++ parse.y 21 Jul 2015 11:55:59 -
@@ -198,27 +198,27 @@ yylex(void)
 repeat:
c = getc(yyfp);
switch (c) {
-   case ' ':
-   case '\t':
-   goto repeat; /* skip spaces */
-   case '\\':
-   next = getc(yyfp);
-   if (next == '\n')
-   goto repeat;
-   else
-   c = next;
-   case '\n':
-   case '{':
-   case '}':
-   return c;
-   case '#':
-   while ((c = getc(yyfp)) != '\n'  c != EOF)
-   ; /* skip comments */
-   if (c == EOF)
-   return 0;
-   return c;
-   case EOF:
+   case ' ':
+   case '\t':
+   goto repeat; /* skip spaces */
+   case '\\':
+   next = getc(yyfp);
+   if (next == '\n')
+   goto repeat;
+   else
+   c = next;
+   case '\n':
+   case '{':
+   case '}':
+   return c;
+   case '#':
+   while ((c = getc(yyfp)) != '\n'  c != EOF)
+   ; /* skip comments */
+   if (c == EOF)
return 0;
+   return c;
+   case EOF:
+   return 0;
}
while (1) {
switch (c) {



[PATCH] Enable -f in ndp(8)

2015-07-13 Thread Dimitris Papastamos
Hi,

I noticed -f in ndp(8) did nothing at all so I've enabled it and
documented the file syntax in the man page.

===
RCS file: /cvs/src/usr.sbin/ndp/ndp.8,v
retrieving revision 1.33
diff -u -p -r1.33 ndp.8
--- ndp.8   3 Sep 2014 10:39:41 -   1.33
+++ ndp.8   13 Jul 2015 13:02:49 -
@@ -122,6 +122,12 @@ Delete the specified NDP entry.
 .It Fl f Ar filename
 Parse the file specified by
 .Ar filename .
+Entries in the file should be of the form:
+.Bd -ragged -offset indent -compact
+.Ar nodename etheraddr
+.Op Ar temp
+.Op Ar proxy
+.Ed
 .It Fl H
 Harmonize consistency between the routing table and the default router
 list; install the top entry of the list into the kernel routing table.
Index: ndp.c
===
RCS file: /cvs/src/usr.sbin/ndp/ndp.c,v
retrieving revision 1.61
diff -u -p -r1.61 ndp.c
--- ndp.c   3 Jun 2015 08:10:53 -   1.61
+++ ndp.c   13 Jul 2015 13:02:49 -
@@ -241,6 +241,8 @@ main(int argc, char *argv[])
}
delete(arg);
break;
+   case 'f':
+   exit(file(arg) ? 1 : 0);
case 'p':
if (argc != 0) {
usage();



Re: [PATCH] Disable USB bus probes

2015-04-27 Thread Dimitris Papastamos
On Mon, Apr 27, 2015 at 11:18:55AM +0200, Martin Pieuchot wrote:
 On 22/04/15(Wed) 10:29, Dimitris Papastamos wrote:
  [...] 
  On a side note, What do you think of using a sysctl instead of an ioctl?
  I guess an ioctl will do but it would make it easy for people to disable
  this at boot time by simply adding the relevant knob in /etc/sysctl.conf.
  Otherwise I imagine people adding usbdevs -p off in rc.local.
 
 I think it makes sense.  Note that we're currently not using sysctls for
 USB so I wonder where the node should be placed.  hw.usb I guess.

OK, should I have a look at adding the top-level sysctl node and the knob
for controlling bus probing?  I can then re-work my patch on top of that.



Re: [PATCH] Disable USB bus probes

2015-04-22 Thread Dimitris Papastamos
Below is an updated diff.  I will add the documentation bits
soonish once I've tested all possible configurations as you
requested.

I am also planning to rework the structure of usbdevs.c as it is
a bit confusing currently but that will happen incrementally after
this diff.

Index: sys/dev/usb/uhub.c
===
RCS file: /cvs/src/sys/dev/usb/uhub.c,v
retrieving revision 1.83
diff -u -p -r1.83 uhub.c
--- sys/dev/usb/uhub.c  12 Feb 2015 05:07:52 -  1.83
+++ sys/dev/usb/uhub.c  22 Apr 2015 11:10:46 -
@@ -53,6 +53,9 @@
 #define DPRINTF(x...)
 #endif
 
+/* controls enabling/disabling of USB bus probing */
+int busprobe = 1;
+
 struct uhub_softc {
struct device   sc_dev; /* base device */
struct usbd_device  *sc_hub;/* USB device */
@@ -487,6 +490,9 @@ uhub_explore(struct usbd_device *dev)
else
speed = sc-sc_hub-speed;
}
+
+   if (!busprobe)
+   return (0);
 
/*
 * Reduce the speed, otherwise we won't setup the proper
Index: sys/dev/usb/usb.c
===
RCS file: /cvs/src/sys/dev/usb/usb.c,v
retrieving revision 1.107
diff -u -p -r1.107 usb.c
--- sys/dev/usb/usb.c   14 Mar 2015 03:38:50 -  1.107
+++ sys/dev/usb/usb.c   22 Apr 2015 11:10:46 -
@@ -87,6 +87,8 @@ int   usb_noexplore = 0;
 #define DPRINTFN(n,x)
 #endif
 
+extern int busprobe;
+
 struct usb_softc {
struct devicesc_dev;/* base device */
struct usbd_bus  *sc_bus;   /* USB controller */
@@ -607,6 +609,14 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
 #endif
break;
 #endif /* USB_DEBUG */
+   case USB_GET_BUS_PROBE:
+   *(unsigned int *)data = busprobe;
+   break;  
+   case USB_SET_BUS_PROBE:
+   if ((error = suser(curproc, 0)) != 0)
+   return (error);
+   busprobe = !!*(unsigned int *)data;
+   break;
case USB_REQUEST:
{
struct usb_ctl_request *ur = (void *)data;
Index: sys/dev/usb/usb.h
===
RCS file: /cvs/src/sys/dev/usb/usb.h,v
retrieving revision 1.50
diff -u -p -r1.50 usb.h
--- sys/dev/usb/usb.h   14 Feb 2015 06:18:58 -  1.50
+++ sys/dev/usb/usb.h   22 Apr 2015 11:10:46 -
@@ -720,14 +720,16 @@ struct usb_device_stats {
 };
 
 /* USB controller */
-#define USB_REQUEST_IOWR('U', 1, struct usb_ctl_request)
-#define USB_SETDEBUG   _IOW ('U', 2, unsigned int)
+#define USB_REQUEST_IOWR('U', 1,  struct usb_ctl_request)
+#define USB_SETDEBUG   _IOW ('U', 2,  unsigned int)
 #define USB_DISCOVER   _IO  ('U', 3)
-#define USB_DEVICEINFO _IOWR('U', 4, struct usb_device_info)
-#define USB_DEVICESTATS_IOR ('U', 5, struct usb_device_stats)
-#define USB_DEVICE_GET_CDESC   _IOWR('U', 6, struct usb_device_cdesc)
-#define USB_DEVICE_GET_FDESC   _IOWR('U', 7, struct usb_device_fdesc)
-#define USB_DEVICE_GET_DDESC   _IOWR('U', 8, struct usb_device_ddesc)
+#define USB_DEVICEINFO _IOWR('U', 4,  struct usb_device_info)
+#define USB_DEVICESTATS_IOR ('U', 5,  struct usb_device_stats)
+#define USB_DEVICE_GET_CDESC   _IOWR('U', 6,  struct usb_device_cdesc)
+#define USB_DEVICE_GET_FDESC   _IOWR('U', 7,  struct usb_device_fdesc)
+#define USB_DEVICE_GET_DDESC   _IOWR('U', 8,  struct usb_device_ddesc)
+#define USB_GET_BUS_PROBE  _IOR ('U', 9,  unsigned int)
+#define USB_SET_BUS_PROBE  _IOW ('U', 10, unsigned int)
 
 /* Generic HID device */
 #define USB_GET_REPORT_DESC_IOR ('U', 21, struct usb_ctl_report_desc)
Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  22 Apr 2015 11:10:46 -
@@ -39,6 +39,7 @@
 .Op Fl dv
 .Op Fl a Ar addr
 .Op Fl f Ar dev
+.Op Fl p Ns Op Ar on | off
 .Sh DESCRIPTION
 .Nm
 prints a listing of all USB devices connected to the system
@@ -53,6 +54,10 @@ Only print information about the device 
 Show the device drivers associated with each device.
 .It Fl f Ar dev
 Only print information for the given USB controller.
+.It Fl p Ns Op Ar on | off
+Enable or disable USB bus probing.  The default
+is
+.Ar on .
 .It Fl v
 Be verbose.
 .El
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  22 Apr 2015 

Re: [PATCH] Disable USB bus probes

2015-04-22 Thread Dimitris Papastamos
On Wed, Apr 22, 2015 at 08:54:34AM +0200, Martin Pieuchot wrote:
 On 17/04/15(Fri) 16:47, Dimitris Papastamos wrote:
  Hi,
  
  This patch adds an option to usbdevs(8) to disable USB bus probing
  at runtime.  The operation is restricted to the root user.
 
 It would be nice to show if probing is on or off, for example
 
 # usbdevs -p
 bus probing: on
 
 # usbdevs -p off
 
 But other people might have better suggestions.

Yeah I thought about that, I will add it.

  I am not sure if this approach is sensible or even correct.  Some
  pointers would be much appreciated.
 
 Setting a variable per hub (and here roothub) is overkill, a global
 would be enough.

Will fix.

 How did you try it?  What happen if you plug a hub with multiple
 devices, turn bus probing off then detach the hub?

I have not tried connecting a hub, but connecting a device with probing
on, disabling probing and then removing works as expected by first detaching
the device and disallowing any further probes after disabling them.

I will try with a hub too.

 What happen if you plug a device like a phone that use the power to
 charge its battery after turning probing off.  If I read your diff
 correctly you still allow the device to be charged which is fine.  Did
 you try that?  I think it's worth a documentation note.

Not tried this yet, but will do.

On a side note, What do you think of using a sysctl instead of an ioctl?
I guess an ioctl will do but it would make it easy for people to disable
this at boot time by simply adding the relevant knob in /etc/sysctl.conf.
Otherwise I imagine people adding usbdevs -p off in rc.local.

Cheers,
Dimitris



[PATCH] Disable USB bus probes

2015-04-17 Thread Dimitris Papastamos
Hi,

This patch adds an option to usbdevs(8) to disable USB bus probing
at runtime.  The operation is restricted to the root user.

I am not sure if this approach is sensible or even correct.  Some
pointers would be much appreciated.

This was started as part of a reply by mpi on tech@

  http://marc.info/?l=openbsd-techm=142917883126679w=2

I guess the reasoning behind this is to add some protection against
things like badusb?

Index: sys/dev/usb/uhub.c
===
RCS file: /cvs/src/sys/dev/usb/uhub.c,v
retrieving revision 1.83
diff -u -p -r1.83 uhub.c
--- sys/dev/usb/uhub.c  12 Feb 2015 05:07:52 -  1.83
+++ sys/dev/usb/uhub.c  17 Apr 2015 15:36:30 -
@@ -222,6 +222,7 @@ uhub_attach(struct device *parent, struc
hub-nports = nports;
hub-powerdelay = powerdelay;
hub-ttthink = ttthink  5;
+   hub-noprobe = 0;
 
if (!dev-self_powered  dev-powersrc-parent != NULL 
!dev-powersrc-parent-self_powered) {
@@ -494,6 +495,9 @@ uhub_explore(struct usbd_device *dev)
 */
if (speed  sc-sc_hub-speed)
speed = sc-sc_hub-speed;
+
+   if (dev-hub-noprobe)
+   return (0);
 
/* Get device info and set its address. */
err = usbd_new_device(sc-sc_dev, dev-bus,
Index: sys/dev/usb/usb.c
===
RCS file: /cvs/src/sys/dev/usb/usb.c,v
retrieving revision 1.107
diff -u -p -r1.107 usb.c
--- sys/dev/usb/usb.c   14 Mar 2015 03:38:50 -  1.107
+++ sys/dev/usb/usb.c   17 Apr 2015 15:36:30 -
@@ -607,6 +607,17 @@ usbioctl(dev_t devt, u_long cmd, caddr_t
 #endif
break;
 #endif /* USB_DEBUG */
+   case USB_NO_PROBE:
+   {
+   struct usbd_device *dev = sc-sc_bus-root_hub;
+
+   if ((error = suser(curproc, 0)) != 0)
+   return (error);
+   if (!(flag  FWRITE))
+   return (EBADF);
+   dev-hub-noprobe = !!*(unsigned int *)data;
+   break;
+   }
case USB_REQUEST:
{
struct usb_ctl_request *ur = (void *)data;
Index: sys/dev/usb/usb.h
===
RCS file: /cvs/src/sys/dev/usb/usb.h,v
retrieving revision 1.50
diff -u -p -r1.50 usb.h
--- sys/dev/usb/usb.h   14 Feb 2015 06:18:58 -  1.50
+++ sys/dev/usb/usb.h   17 Apr 2015 15:36:30 -
@@ -728,6 +728,7 @@ struct usb_device_stats {
 #define USB_DEVICE_GET_CDESC   _IOWR('U', 6, struct usb_device_cdesc)
 #define USB_DEVICE_GET_FDESC   _IOWR('U', 7, struct usb_device_fdesc)
 #define USB_DEVICE_GET_DDESC   _IOWR('U', 8, struct usb_device_ddesc)
+#define USB_NO_PROBE   _IOW ('U', 9, unsigned int)
 
 /* Generic HID device */
 #define USB_GET_REPORT_DESC_IOR ('U', 21, struct usb_ctl_report_desc)
Index: sys/dev/usb/usbdivar.h
===
RCS file: /cvs/src/sys/dev/usb/usbdivar.h,v
retrieving revision 1.69
diff -u -p -r1.69 usbdivar.h
--- sys/dev/usb/usbdivar.h  21 Dec 2014 12:04:01 -  1.69
+++ sys/dev/usb/usbdivar.h  17 Apr 2015 15:36:30 -
@@ -94,6 +94,7 @@ struct usbd_hub {
int nports;
u_int8_tpowerdelay;
u_int8_tttthink;
+   u_int8_tnoprobe;
 };
 
 struct usbd_bus {
Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  17 Apr 2015 15:36:31 -
@@ -39,6 +39,7 @@
 .Op Fl dv
 .Op Fl a Ar addr
 .Op Fl f Ar dev
+.Op Fl p Ar on | off
 .Sh DESCRIPTION
 .Nm
 prints a listing of all USB devices connected to the system
@@ -53,6 +54,10 @@ Only print information about the device 
 Show the device drivers associated with each device.
 .It Fl f Ar dev
 Only print information for the given USB controller.
+.It Fl p Ar on | off
+Enable or disable USB bus probing.  The default
+is
+.Ar on .
 .It Fl v
 Be verbose.
 .El
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  17 Apr 2015 15:36:31 -
@@ -30,6 +30,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include limits.h
 #include stdio.h
 #include stdlib.h
 #include string.h
@@ -53,6 +54,7 @@ void usage(void);
 void usbdev(int f, int a, int rec);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
+void setprobe(unsigned int);
 int main(int, char **);
 
 extern char 

Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Dimitris Papastamos
On Thu, Apr 16, 2015 at 12:06:49PM +0200, Martin Pieuchot wrote:
 On 13/04/15(Mon) 17:49, Dimitris Papastamos wrote:
  Hi,
  
  Attached below an updated patch that makes the layout a bit more sensible.
 
 It's nice it works. 
 
 Now like various students that applied for the GSoC you've done the
 first step.  The interesting part of that GSoC is also the boring part:
 the parser.  How are you gonna deal with AUDIO, VIDEO and HID descriptors?
 
 I'm asking the question but I'm not really interested in the answer.  I
 don't see the point of having multiple people working on the same task.
 But if a student is selected for this task, you might want to discuss
 that with him.

Sure, that makes sense.

 Honestly if you are looking for an easy task to contribute to the USB
 land in OpenBSD you could have asked before, there's a lot to do :)

I should have I guess.  The gsoc page was tempting even if I am not a
student.

 So here's a small task, just for you:  can you add an option to
 usbdev(8) to enable/disable USB bus probing?   By default it would be
 on, just like know: when you plug a device is it detected/attached.
 
 If you turn it off to new device will be attached and port events
 will simply be acknowledged?

Will have a look at that.



Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Dimitris Papastamos
ping



[PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
Hi everyone,

This is a preliminary patch that adds descriptor dumping support
to usbdevs(8).  I have attempted to keep any unnecessary changes to
a minimum.  I will do some cleanup work incrementally on top of
this patch.

I've tested this on a single machine running -current so more testing
would be nice.

Let me know what you think and I will rework my patch.

Cheers,
Dimitris

Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  13 Apr 2015 13:57:27 -
@@ -54,7 +54,13 @@ Show the device drivers associated with 
 .It Fl f Ar dev
 Only print information for the given USB controller.
 .It Fl v
-Be verbose.
+Verbose mode.  Causes
+.Nm
+to display detailed information about the devices present.
+Multiple
+.Fl v
+options increase verbosity.  At level 1 or above the device
+and configuration descriptors will be shown.
 .El
 .Sh FILES
 .Bl -tag -width Pa
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  13 Apr 2015 13:57:27 -
@@ -50,6 +50,14 @@ int verbose = 0;
 int showdevs = 0;
 
 void usage(void);
+int getddesc(int, int, int, struct usb_device_ddesc *);
+int getcdesc(int, int, int, struct usb_device_cdesc *);
+int getfdesc(int, int, int, int, struct usb_device_fdesc *);
+void dumpedesc(usb_endpoint_descriptor_t *);
+void dumpidesc(usb_interface_descriptor_t *);
+void dumpcdesc(usb_config_descriptor_t *);
+void dumpddesc(usb_device_descriptor_t *);
+void dumpdesc(int, int, int);
 void usbdev(int f, int a, int rec);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
@@ -67,6 +75,236 @@ usage(void)
 char done[USB_MAX_DEVICES];
 int indent;
 
+int
+getddesc(int f, int b, int a, struct usb_device_ddesc *ddesc)
+{
+   int r;
+
+   ddesc-udd_bus = b;
+   ddesc-udd_addr = a;
+   r = ioctl(f, USB_DEVICE_GET_DDESC, ddesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_DDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getcdesc(int f, int b, int a, struct usb_device_cdesc *cdesc)
+{
+   int r;
+
+   cdesc-udc_bus = b;
+   cdesc-udc_addr = a;
+   cdesc-udc_config_index = USB_CURRENT_CONFIG_INDEX;
+   r = ioctl(f, USB_DEVICE_GET_CDESC, cdesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_CDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getfdesc(int f, int b, int a, int l, struct usb_device_fdesc *fdesc)
+{
+   int r;
+
+   fdesc-udf_bus = b;
+   fdesc-udf_addr = a;
+   fdesc-udf_config_index = USB_CURRENT_CONFIG_INDEX;
+   fdesc-udf_size = l;
+   fdesc-udf_data = malloc(l);
+   if (!fdesc-udf_data)
+   err(1, malloc);
+   r = ioctl(f, USB_DEVICE_GET_FDESC, fdesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_FDESC);
+   free(fdesc-udf_data);
+   return -1;
+   }
+   return 0;
+}
+
+void
+dumpedesc(usb_endpoint_descriptor_t *ed)
+{
+   char *xfertype, *synchtype;
+
+   printf(%*sEndpoint Descriptor:\n, indent, );
+   indent++;
+   printf(%*sbLength: %d\n, indent, , ed-bLength);
+   printf(%*sbDescriptorType: %d\n, indent, , ed-bDescriptorType);
+   printf(%*sbEndpointAddress: %#x EP %d %s\n, indent, , 
ed-bEndpointAddress,
+  UE_GET_ADDR(ed-bEndpointAddress),
+  UE_GET_DIR(ed-bEndpointAddress) ? in : out);
+   printf(%*sbmAttributes: %#x\n, indent, , ed-bmAttributes);
+
+   switch (UE_GET_XFERTYPE(ed-bmAttributes)) {
+   case UE_CONTROL:
+   xfertype = Control;
+   break;
+   case UE_ISOCHRONOUS:
+   xfertype = Isochronous;
+   break;
+   case UE_BULK:
+   xfertype = Bulk;
+   break;
+   case UE_INTERRUPT:
+   xfertype = Interrupt;
+   break;
+   default:
+   xfertype = Unknown;
+   break;
+   }
+
+   switch (UE_GET_ISO_TYPE(ed-bmAttributes)) {
+   case UE_ISO_ASYNC:
+   synchtype = Asynchronous;
+   break;
+   case UE_ISO_ADAPT:
+   synchtype = Adaptive;
+   break;
+   case UE_ISO_SYNC:
+   synchtype = Synchronous;
+   break;
+   default:
+   synchtype = No Synchronization;
+   break;
+   }
+
+   indent++;
+   printf(%*sTransfer type: %s\n, indent, , xfertype);
+   

Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
Hi,

Attached below an updated patch that makes the layout a bit more sensible.

Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  13 Apr 2015 16:46:59 -
@@ -54,7 +54,13 @@ Show the device drivers associated with 
 .It Fl f Ar dev
 Only print information for the given USB controller.
 .It Fl v
-Be verbose.
+Verbose mode.  Causes
+.Nm
+to display detailed information about the devices present.
+Multiple
+.Fl v
+options increase verbosity.  At level 1 or above the device
+and configuration descriptors will be shown.
 .El
 .Sh FILES
 .Bl -tag -width Pa
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  13 Apr 2015 16:46:59 -
@@ -30,14 +30,17 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include sys/types.h
+
+#include err.h
+#include errno.h
+#include stdarg.h
 #include stdio.h
 #include stdlib.h
 #include string.h
-#include sys/types.h
 #include fcntl.h
 #include unistd.h
-#include err.h
-#include errno.h
+
 #include dev/usb/usb.h
 
 #ifndef nitems
@@ -45,15 +48,29 @@
 #endif
 
 #define USBDEV /dev/usb
+#define INDENT_LEVEL 3
 
-int verbose = 0;
-int showdevs = 0;
+int verbose;
+int showdevs;
+
+int indent;
+char done[USB_MAX_DEVICES];
 
 void usage(void);
+void pushlevel(void);
+void poplevel(void);
+void printent(const char *, ...);
+int getddesc(int, int, int, struct usb_device_ddesc *);
+int getcdesc(int, int, int, struct usb_device_cdesc *);
+int getfdesc(int, int, int, int, struct usb_device_fdesc *);
+void dumpedesc(usb_endpoint_descriptor_t *);
+void dumpidesc(usb_interface_descriptor_t *);
+void dumpcdesc(usb_config_descriptor_t *);
+void dumpddesc(usb_device_descriptor_t *);
+void dumpdesc(int, int, int);
 void usbdev(int f, int a, int rec);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
-int main(int, char **);
 
 extern char *__progname;
 
@@ -64,8 +81,256 @@ usage(void)
exit(1);
 }
 
-char done[USB_MAX_DEVICES];
-int indent;
+void
+pushlevel(void)
+{
+   indent += INDENT_LEVEL;
+}
+
+void
+poplevel(void)
+{
+   indent -= INDENT_LEVEL;
+}
+
+void
+printent(const char *fmt, ...)
+{
+   va_list ap;
+
+   va_start(ap, fmt);
+   printf(%*s, indent, );
+   vprintf(fmt, ap);
+   va_end(ap);
+}
+
+int
+getddesc(int f, int b, int a, struct usb_device_ddesc *ddesc)
+{
+   int r;
+
+   ddesc-udd_bus = b;
+   ddesc-udd_addr = a;
+   r = ioctl(f, USB_DEVICE_GET_DDESC, ddesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_DDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getcdesc(int f, int b, int a, struct usb_device_cdesc *cdesc)
+{
+   int r;
+
+   cdesc-udc_bus = b;
+   cdesc-udc_addr = a;
+   cdesc-udc_config_index = USB_CURRENT_CONFIG_INDEX;
+   r = ioctl(f, USB_DEVICE_GET_CDESC, cdesc);
+   if (r) {
+   warn(USB_DEVICE_GET_CDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getfdesc(int f, int b, int a, int l, struct usb_device_fdesc *fdesc)
+{
+   int r;
+
+   fdesc-udf_bus = b;
+   fdesc-udf_addr = a;
+   fdesc-udf_config_index = USB_CURRENT_CONFIG_INDEX;
+   fdesc-udf_size = l;
+   fdesc-udf_data = malloc(l);
+   if (!fdesc-udf_data)
+   err(1, malloc);
+   r = ioctl(f, USB_DEVICE_GET_FDESC, fdesc);
+   if (r) {
+   warn(USB_DEVICE_GET_FDESC);
+   free(fdesc-udf_data);
+   return -1;
+   }
+   return 0;
+}
+
+void
+dumpedesc(usb_endpoint_descriptor_t *ed)
+{
+   char *xfertype, *synchtype;
+
+   printent(Endpoint Descriptor:\n);
+   pushlevel();
+   printent(bLength:  %d\n, ed-bLength);
+   printent(bDescriptorType:  %d\n, ed-bDescriptorType);
+   printent(bEndpointAddress: %#x EP %d %s\n, ed-bEndpointAddress,
+  UE_GET_ADDR(ed-bEndpointAddress),
+  UE_GET_DIR(ed-bEndpointAddress) ? in : out);
+   printent(bmAttributes: %#x\n, ed-bmAttributes);
+
+   switch (UE_GET_XFERTYPE(ed-bmAttributes)) {
+   case UE_CONTROL:
+   xfertype = Control;
+   break;
+   case UE_ISOCHRONOUS:
+   xfertype = Isochronous;
+   break;
+   case UE_BULK:
+   xfertype = Bulk;
+   break;
+   case UE_INTERRUPT:
+   xfertype = Interrupt;
+   break;
+   default:
+   xfertype = Unknown;
+   break;
+   }
+
+   switch 

Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
One more thing I forgot to mention, the current printing layout
is not ideal.  I'll look into making it a bit more readable.



Re: additional drm fixes from linux-stable 3.10.y

2015-02-05 Thread Dimitris Papastamos
On Sat, Jan 31, 2015 at 08:25:23PM +1100, Jonathan Gray wrote:
 Some test reports with this would be great.
 In particular for radeon.

Been running this patch with i915 for 2 days now without any
perceivable issues.

OpenBSD 5.7-beta (GENERIC.MP) #23: Tue Feb  3 17:07:19 GMT 2015
r...@darkstar.2f30.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 8451125248 (8059MB)
avail mem = 877632 (7841MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.6 @ 0xdae9c000 (63 entries)
bios0: vendor LENOVO version 83ET76WW (1.46 ) date 07/05/2013
bios0: LENOVO 4236MBG
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP SLIC SSDT SSDT SSDT HPET APIC MCFG ECDT ASF! TCPA SSDT 
SSDT DMAR UEFI UEFI UEFI
acpi0: wakeup devices LID_(S3) SLPB(S3) IGBE(S4) EXP4(S4) EHC1(S3) EHC2(S3) 
HDEF(S4)
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpihpet0 at acpi0: 14318179 Hz
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.95 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu0: mwait min=64, max=64, C-substates=0.2.1.1.2, IBE
cpu1 at mainbus0: apid 1 (application processor)
cpu1: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 1, core 0, package 0
cpu2 at mainbus0: apid 2 (application processor)
cpu2: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 0, core 1, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz, 2591.58 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,NXE,LONG,LAHF,PERF,ITSC
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 1, core 1, package 0
ioapic0 at mainbus0: apid 2 pa 0xfec0, version 20, 24 pins
acpimcfg0 at acpi0 addr 0xf800, bus 0-63
acpiec0 at acpi0
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus -1 (PEG_)
acpiprt2 at acpi0: bus 2 (EXP1)
acpiprt3 at acpi0: bus 3 (EXP2)
acpiprt4 at acpi0: bus -1 (EXP4)
acpiprt5 at acpi0: bus 13 (EXP5)
acpicpu0 at acpi0: C3, C1, PSS
acpicpu1 at acpi0: C3, C1, PSS
acpicpu2 at acpi0: C3, C1, PSS
acpicpu3 at acpi0: C3, C1, PSS
acpipwrres0 at acpi0: PUBS, resource for EHC1, EHC2
acpitz0 at acpi0: critical temperature is 98 degC
acpibtn0 at acpi0: LID_
acpibtn1 at acpi0: SLPB
acpibat0 at acpi0: BAT0 model 45N1173 serial 26283 type LION oem SANYO
acpibat1 at acpi0: BAT1 not present
acpiac0 at acpi0: AC unit online
acpithinkpad0 at acpi0
cpu0: Enhanced SpeedStep 2591 MHz: speeds: 2601, 2600, 2400, 2200, 2000, 1800, 
1600, 1400, 1200, 1000, 800 MHz
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 Intel Core 2G Host rev 0x09
vga1 at pci0 dev 2 function 0 Intel HD Graphics 3000 rev 0x09
intagp at vga1 not configured
inteldrm0 at vga1
drm0 at inteldrm0
drm: Memory usable by graphics device = 2048M
inteldrm0: 1600x900
wsdisplay0 at vga1 mux 1: console (std, vt100 emulation)
wsdisplay0: screen 1-5 added (std, vt100 emulation)
Intel 6 Series MEI rev 0x04 at pci0 dev 22 function 0 not configured
puc0 at pci0 dev 22 function 3 Intel 6 Series KT rev 0x04: ports: 1 com
com4 at puc0 port 0 apic 2 int 19: ns16550a, 16 byte fifo
com4: probed fifo depth: 0 bytes
em0 at pci0 dev 25 function 0 Intel 82579LM rev 0x04: msi, address 
00:21:cc:5d:e1:40
ehci0 at pci0 dev 26 function 0 Intel 6 Series USB rev 0x04: apic 2 int 16
usb0 at ehci0: USB revision 2.0
uhub0 at usb0 Intel EHCI root hub rev 2.00/1.00 addr 1
azalia0 at pci0 dev 27 function 0 Intel 6 Series HD Audio rev 0x04: msi
azalia0: codecs: Conexant/0x506e, Intel/0x2805, using Conexant/0x506e
audio0 at azalia0
ppb0 at pci0 dev 28 function 0 Intel 6 Series PCIE rev 0xb4: msi
pci1 at ppb0 bus 2
ppb1 at pci0 dev 28 function 1 Intel 6 Series PCIE rev 0xb4: msi
pci2 at ppb1 

Convert bgpd to use siphash

2014-12-04 Thread Dimitris Papastamos
Hi,

This is my attempt at converting bgpd to use siphash.  This does
*not* link as is because the userland implementation of siphash
is currently missing.

In certain cases where multiple data is hashed in succession, it might
have been better to use a siphash context and call SipHash24_Update().

Any feedback?

Cheers,
Dimitris

Index: Makefile
===
RCS file: /cvs/src/usr.sbin/bgpd/Makefile,v
retrieving revision 1.29
diff -u -p -r1.29 Makefile
--- Makefile26 May 2010 16:44:32 -  1.29
+++ Makefile4 Dec 2014 17:30:00 -
@@ -11,8 +11,8 @@ CFLAGS+= -Wmissing-declarations
 CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual
 CFLAGS+= -Wsign-compare
 YFLAGS=
-LDADD+=-lutil
-DPADD+= ${LIBUTIL}
+LDADD+= -lcrypto -lutil
+DPADD+= ${LIBCRYPTO} ${LIBUTIL}
 MAN= bgpd.8 bgpd.conf.5
 
 .include bsd.prog.mk
Index: rde.h
===
RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
retrieving revision 1.147
diff -u -p -r1.147 rde.h
--- rde.h   14 Aug 2013 20:34:26 -  1.147
+++ rde.h   4 Dec 2014 17:30:00 -
@@ -131,7 +131,7 @@ struct attr {
LIST_ENTRY(attr) entry;
u_char  *data;
int  refcnt;
-   u_int32_thash;
+   u_int64_thash;
u_int16_tlen;
u_int8_t flags;
u_int8_t type;
Index: rde_attr.c
===
RCS file: /cvs/src/usr.sbin/bgpd/rde_attr.c,v
retrieving revision 1.92
diff -u -p -r1.92 rde_attr.c
--- rde_attr.c  8 Oct 2014 16:15:37 -   1.92
+++ rde_attr.c  4 Dec 2014 17:30:00 -
@@ -17,9 +17,10 @@
  */
 
 #include sys/types.h
-#include sys/hash.h
 #include sys/queue.h
 
+#include crypto/siphash.h
+
 #include netinet/in.h
 
 #include limits.h
@@ -316,17 +317,20 @@ struct attr *
 attr_alloc(u_int8_t flags, u_int8_t type, const void *data, u_int16_t len)
 {
struct attr *a;
+   SIPHASH_KEY  key;
 
a = calloc(1, sizeof(struct attr));
if (a == NULL)
fatal(attr_optadd);
rdemem.attr_cnt++;
 
+   arc4random_buf(key, sizeof(key));
+
flags = ~ATTR_DEFMASK; /* normalize mask */
a-flags = flags;
-   a-hash = hash32_buf(flags, sizeof(flags), HASHINIT);
+   a-hash = SipHash24(key, flags, sizeof(flags));
a-type = type;
-   a-hash = hash32_buf(type, sizeof(type), a-hash);
+   a-hash = SipHash24(key, type, sizeof(type));
a-len = len;
if (len != 0) {
if ((a-data = malloc(len)) == NULL)
@@ -338,8 +342,8 @@ attr_alloc(u_int8_t flags, u_int8_t type
} else
a-data = NULL;
 
-   a-hash = hash32_buf(len, sizeof(len), a-hash);
-   a-hash = hash32_buf(a-data, a-len, a-hash);
+   a-hash = SipHash24(key, len, sizeof(len));
+   a-hash = SipHash24(key, a-data, a-len);
LIST_INSERT_HEAD(ATTR_HASH(a-hash), a, entry);
 
return (a);
@@ -350,13 +354,15 @@ attr_lookup(u_int8_t flags, u_int8_t typ
 {
struct attr_list*head;
struct attr *a;
-   u_int32_thash;
+   u_int64_thash;
+   SIPHASH_KEY  key;
 
+   arc4random_buf(key, sizeof(key));
flags = ~ATTR_DEFMASK; /* normalize mask */
-   hash = hash32_buf(flags, sizeof(flags), HASHINIT);
-   hash = hash32_buf(type, sizeof(type), hash);
-   hash = hash32_buf(len, sizeof(len), hash);
-   hash = hash32_buf(data, len, hash);
+   hash = SipHash24(key, flags, sizeof(flags));
+   hash = SipHash24(key, type, sizeof(type));
+   hash = SipHash24(key, len, sizeof(len));
+   hash = SipHash24(key, data, len);
head = ATTR_HASH(hash);
 
LIST_FOREACH(a, head, entry) {
@@ -483,6 +489,7 @@ aspath_get(void *data, u_int16_t len)
 {
struct aspath_list  *head;
struct aspath   *aspath;
+   SIPHASH_KEY  key;
 
/* The aspath must already have been checked for correctness. */
aspath = aspath_lookup(data, len);
@@ -499,9 +506,9 @@ aspath_get(void *data, u_int16_t len)
aspath-ascnt = aspath_count(data, len);
memcpy(aspath-data, data, len);
 
+   arc4random_buf(key, sizeof(key));
/* link */
-   head = ASPATH_HASH(hash32_buf(aspath-data, aspath-len,
-   HASHINIT));
+   head = ASPATH_HASH(SipHash24(key, aspath-data, aspath-len));
LIST_INSERT_HEAD(head, aspath, entry);
}
aspath-refcnt++;
@@ -829,9 +836,11 @@ aspath_lookup(const void *data, u_int16_
 {
struct aspath_list  *head;
struct aspath   *aspath;
-   u_int32_t

Re: Convert bgpd to use siphash

2014-12-04 Thread Dimitris Papastamos
Admittedly it would have been better to store the key in the struct
and then use arc4random_buf() only once or so.  I am not familiar with the
code so I will investigate this option.



Re: Convert bgpd to use siphash

2014-12-04 Thread Dimitris Papastamos
On Thu, Dec 04, 2014 at 12:52:04PM -0500, Ted Unangst wrote:
 Ah, so this won't work. That's not how siphash works.
 
 The key needs to last as long as the hash table does, which is
 generally as long as the program. Different keys result in different
 hashes, which makes it hard to look things up later.

Ah, of course.  I will post another diff after siphash hits userspace.



Re: Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-11 Thread Dimitris Papastamos
Hi everyone,

Any interest in this?  It is a cleanup change and not intended to have
any functional differences.

Cheers,
Dimitris



Re: Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-11 Thread Dimitris Papastamos
On Tue, Nov 11, 2014 at 02:21:35PM +0400, Vadim Zhukov wrote:
 Why TAILQ? SLIST should be enough.

SLIST makes sense yes.

 Also, moving sys/types.h is wrong.

OK, will fix.

 Why do you want to get this in? Any more ac(8) patches coming?

Not really, I was just randomly browsing through the source tree
and was looking for something to improve.



keyboard problem related to xhci

2014-11-11 Thread Dimitris Papastamos
Hi everyone,

I just built the kernel with XHCI_DEBUG enabled.  I am having
the following problem:

1) boot the machine with keyboard and mouse plugged in
2) keyboard does not respond at the login prompt
3) plug it out and plug it back in at the same USB port
4) keyboard does not respond
5) plug it out and plug it back in at another USB port
6) keyboard works fine

Below is my dmesg, it shows the same sequence of operations
as described above.

Hope this helps, let me know if you need me to test
anything else.

Cheers,
Dimitris

OpenBSD 5.6-current (GENERIC.MP) #11: Tue Nov 11 19:24:27 GMT 2014
r...@pancakes.2f30.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 7450062848 (7104MB)
avail mem = 7247908864 (6912MB)
warning: no entropy supplied by boot loader
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.8 @ 0xec1e0 (76 entries)
bios0: vendor American Megatrends Inc. version 1.03 date 06/18/2014
bios0: Shuttle Inc. XH81V
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP APIC FPDT SLIC SSDT SSDT MCFG HPET SSDT SSDT
acpi0: wakeup devices PXSX(S4) RP01(S4) PXSX(S4) RP02(S4) PXSX(S4) RP03(S4) 
PXSX(S4) RP04(S4) PXSX(S4) RP05(S4) PXSX(S4) RP06(S4) PXSX(S4) RP07(S4) 
PXSX(S4) RP08(S4) [...]
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3692.01 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu1 at mainbus0: apid 2 (application processor)
cpu1: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 0, core 1, package 0
cpu2 at mainbus0: apid 1 (application processor)
cpu2: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 1, core 0, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 1, core 1, package 0
ioapic0 at mainbus0: apid 8 pa 0xfec0, version 20, 24 pins
acpimcfg0 at acpi0 addr 0xf800, bus 0-63
acpihpet0 at acpi0: 14318179 Hz
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus 1 (RP01)
acpiprt2 at acpi0: bus 2 (RP03)
acpiprt3 at acpi0: bus 3 (RP04)
acpiprt4 at acpi0: bus -1 (PEG0)
acpiec0 at acpi0: not present
acpicpu0 at acpi0: C1, PSS
acpicpu1 at acpi0: C1, PSS
acpicpu2 at acpi0: C1, PSS
acpicpu3 at acpi0: C1, PSS
acpipwrres0 at acpi0: FN00, resource for FAN0
acpipwrres1 at acpi0: FN01, resource for FAN1
acpipwrres2 at acpi0: FN02, resource for FAN2
acpipwrres3 at acpi0: FN03, resource for FAN3
acpipwrres4 at acpi0: FN04, resource for FAN4
acpitz0 at acpi0: critical temperature is 105 degC
acpitz1 at acpi0: critical temperature is 105 degC
acpibat0 at acpi0: BAT0 not present
acpibat1 at acpi0: BAT1 not present
acpibat2 at acpi0: BAT2 not present
acpibtn0 at acpi0: PWRB
acpibtn1 at acpi0: LID0
acpivideo0 at acpi0: GFX0
acpivout0 at acpivideo0: DD1F
cpu0: Enhanced SpeedStep 3692 MHz: speeds: 3700, 3500, 3300, 3100, 2900, 2700, 
2500, 2300, 2200, 2000, 1800, 1600, 1400, 1200, 1000, 800 MHz
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 Intel Core 4G Host rev 0x06
vga1 at pci0 dev 2 function 0 Intel HD Graphics 4600 rev 0x06
intagp at vga1 not configured
inteldrm0 at vga1
drm0 at inteldrm0
drm: Memory usable by graphics device = 2048M
error: [drm:pid0:i915_write32] *ERROR* Unknown unclaimed register before 
writing 

xhci related kernel panic

2014-11-09 Thread Dimitris Papastamos
Hi everyone,

I had to type this off the screen.  My USB keyboard does not work
at the ddb prompt so I cannot provide more information.

xhci_pipe_open: pipe=0x80493000 addr=2 depth=1 port=9 speed=2
xhci0: dev 1 dci 3 (epAddr=0x81)
xhci0: speed 1 mps 8 rhport 9 route 0x0
xhci0: max ESIT payload = 8, average TRB length = 8
xhci0: xhci_cmd_configure_ep
xhci_abort_xfer: xfer=0xff021e3482d0 err=CANCELLED
xhci0: error stopping ep (3)
xhci0: xhci_cmd_configure_ep
xhci_pipe_open: pipe=0x80493000 addr=2 depth=1 port=9 speed=2
xhci0: dev 1 dci 3 (epAddr=0x81)
xhci0: speed 1 mps 8 rhport 9 route 0x0
uvm_fault(0xff01e7f04b60, 0x0, 0, 2) - e
kernel: page fault trap, code=0
Stopped at  xhci_pipe_init+0x1d7: movl %ecx,0(%rax)
ddb{3} 

I built the kernel today and this started happening.  My previous
build was from the 2nd of Novemember.  I believe xhci was disabled
at that time.

The panic only happens if I have a USB device connected during boot
(either my mouse or keyboard).  If I plug them in incrementally once
I reach the login prompt, the system does not panic.

I am providing a dmesg below captured with XHCI_DEBUG enabled.
This dmesg was captured by booting with no USB devices attached, then
manually attaching my USB mouse and keyboard.

Is there anything else I could do to help debug this?

OpenBSD 5.6-current (GENERIC.MP) #7: Sun Nov  9 12:19:51 GMT 2014
r...@pancakes.2f30.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 7450062848 (7104MB)
avail mem = 7247908864 (6912MB)
warning: no entropy supplied by boot loader
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.8 @ 0xec1e0 (76 entries)
bios0: vendor American Megatrends Inc. version 1.03 date 06/18/2014
bios0: Shuttle Inc. XH81V
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP APIC FPDT SLIC SSDT SSDT MCFG HPET SSDT SSDT
acpi0: wakeup devices PXSX(S4) RP01(S4) PXSX(S4) RP02(S4) PXSX(S4) RP03(S4) 
PXSX(S4) RP04(S4) PXSX(S4) RP05(S4) PXSX(S4) RP06(S4) PXSX(S4) RP07(S4) 
PXSX(S4) RP08(S4) [...]
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.97 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu1 at mainbus0: apid 2 (application processor)
cpu1: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 0, core 1, package 0
cpu2 at mainbus0: apid 1 (application processor)
cpu2: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 1, core 0, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz, 3691.47 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,FMA3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,PAGE1GB,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,BMI1,AVX2,SMEP,BMI2,ERMS,INVPCID
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 1, core 1, package 0
ioapic0 at mainbus0: apid 8 pa 0xfec0, version 20, 24 pins
acpimcfg0 at acpi0 addr 0xf800, bus 0-63
acpihpet0 at acpi0: 14318179 Hz
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus 1 (RP01)
acpiprt2 at acpi0: bus 2 (RP03)
acpiprt3 at acpi0: bus 3 (RP04)
acpiprt4 at acpi0: bus -1 (PEG0)
acpiec0 at acpi0: not present
acpicpu0 at acpi0: C1, PSS
acpicpu1 at acpi0: C1, PSS
acpicpu2 at acpi0: C1, PSS
acpicpu3 at acpi0: C1, PSS
acpipwrres0 at acpi0: FN00, resource for FAN0
acpipwrres1 at acpi0: FN01, resource for FAN1
acpipwrres2 at acpi0: FN02, resource for FAN2
acpipwrres3 at acpi0: FN03, resource for FAN3
acpipwrres4 at acpi0: FN04, resource for 

Re: xhci related kernel panic

2014-11-09 Thread Dimitris Papastamos
Hi,

../../../../dev/usb/xhci.c:1109
2c26:   4a 8d 14 ba lea(%rdx,%r15,4),%rdx
2c2a:   48 8d 44 10 01  lea0x1(%rax,%rdx,1),%rax
2c2f:   49 8b 84 c5 b8 05 00mov0x5b8(%r13,%rax,8),%rax
2c36:   00 
2c37:   89 08   mov%ecx,(%rax)

That's also the relevant objdump section.



Re: xhci related kernel panic

2014-11-09 Thread Dimitris Papastamos
On Sun, Nov 09, 2014 at 03:01:47PM +0100, Martin Pieuchot wrote:
 Thanks for the report.  I just committed a fix for this.  The problem
 was in the code closing the pipe.  This would only matter for devices
 closing  opening multiple times their pipes, like mouses or keyboards
 when they are opened/closed.
 
 Please let met know if you still have a problem.

I can confirm it is working now.  Thanks for the quick fix! :)



Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-05 Thread Dimitris Papastamos
Hi,

I've attempted to convert the hand-rolled linked lists over to
TAILQ.  I've tested this briefly and it seems to work okay.

Does it look good?  Let me know if I need to rework anything.

===
RCS file: /cvs/src/usr.sbin/ac/ac.c,v
retrieving revision 1.22
diff -u -p -r1.22 ac.c
--- ac.c31 Aug 2012 19:57:12 -  1.22
+++ ac.c5 Nov 2014 11:30:46 -
@@ -25,9 +25,11 @@
  * SUCH DAMAGE.
  */
 
-#include sys/types.h
 #include sys/file.h
+#include sys/queue.h
 #include sys/time.h
+#include sys/types.h
+
 #include err.h
 #include errno.h
 #include pwd.h
@@ -40,28 +42,28 @@
 /*
  * this is for our list of currently logged in sessions
  */
-struct utmp_list {
-   struct utmp_list *next;
+struct utmp_entry {
struct utmp usr;
+   TAILQ_ENTRY(utmp_entry) next;
 };
 
 /*
  * this is for our list of users that are accumulating time.
  */
-struct user_list {
-   struct user_list *next;
+struct user_entry {
charname[UT_NAMESIZE+1];
time_t  secs;
+   TAILQ_ENTRY(user_entry) next;
 };
 
 /*
  * this is for chosing whether to ignore a login
  */
-struct tty_list {
-   struct tty_list *next;
+struct tty_entry {
charname[UT_LINESIZE+3];
size_t  len;
int ret;
+   TAILQ_ENTRY(tty_entry) next;
 };
 
 /*
@@ -70,8 +72,9 @@ struct tty_list {
 static time_t  Total = 0;
 static time_t  FirstTime = 0;
 static int Flags = 0;
-static struct user_list *Users = NULL;
-static struct tty_list *Ttys = NULL;
+static TAILQ_HEAD(Utmphead, utmp_entry) Utmphead;
+static TAILQ_HEAD(Userhead, user_entry) Userhead;
+static TAILQ_HEAD(Ttyhead, tty_entry) Ttyhead;
 
 #defineAC_W1   /* not _PATH_WTMP */
 #defineAC_D2   /* daily totals (ignore 
-p) */
@@ -88,14 +91,13 @@ int ac(FILE *);
 void   add_tty(char *);
 intdo_tty(char *);
 FILE   *file(char *);
-struct utmp_list   *log_in(struct utmp_list *, struct utmp *);
-struct utmp_list   *log_out(struct utmp_list *, struct utmp *);
-inton_console(struct utmp_list *);
+void   log_in(struct utmp *);
+void   log_out(struct utmp *);
+inton_console(void);
 void   show(char *, time_t);
-void   show_today(struct user_list *, struct utmp_list *,
-   time_t);
-void   show_users(struct user_list *);
-struct user_list   *update_user(struct user_list *, char *, time_t);
+void   show_today(time_t);
+void   show_users(void);
+void   update_user(char *, time_t);
 void   usage(void);
 
 /*
@@ -119,12 +121,12 @@ file(char *name)
 void
 add_tty(char *name)
 {
-   struct tty_list *tp;
+   struct tty_entry *tp;
char *rcp;
 
Flags |= AC_T;
 
-   if ((tp = malloc(sizeof(struct tty_list))) == NULL)
+   if ((tp = malloc(sizeof(sizeof(*tp == NULL)
err(1, malloc);
tp-len = 0;/* full match */
tp-ret = 1;/* do if match */
@@ -137,8 +139,7 @@ add_tty(char *name)
*rcp = '\0';
tp-len = strlen(tp-name); /* match len bytes only */
}
-   tp-next = Ttys;
-   Ttys = tp;
+   TAILQ_INSERT_HEAD(Ttyhead, tp, next);
 }
 
 /*
@@ -147,10 +148,10 @@ add_tty(char *name)
 int
 do_tty(char *name)
 {
-   struct tty_list *tp;
+   struct tty_entry *tp;
int def_ret = 0;
 
-   for (tp = Ttys; tp != NULL; tp = tp-next) {
+   TAILQ_FOREACH(tp, Ttyhead, next) {
if (tp-ret == 0)   /* specific don't */
def_ret = 1;/* default do */
if (tp-len != 0) {
@@ -167,31 +168,30 @@ do_tty(char *name)
 /*
  * update user's login time
  */
-struct user_list *
-update_user(struct user_list *head, char *name, time_t secs)
+void
+update_user(char *name, time_t secs)
 {
-   struct user_list *up;
+   struct user_entry *up;
 
-   for (up = head; up != NULL; up = up-next) {
+   TAILQ_FOREACH(up, Userhead, next) {
if (strncmp(up-name, name, sizeof (up-name) - 1) == 0) {
up-secs += secs;
Total += secs;
-   return head;
+   return;
}
}
/*
 * not found so add new user unless specified users only
 */
if (Flags  AC_U)
-   return head;
+   return;
 
-   if ((up = malloc(sizeof(struct user_list))) == NULL)
+   if ((up = malloc(sizeof(*up))) == NULL)
err(1, malloc);
-   up-next = head;
strlcpy(up-name, name, sizeof (up-name));
up-secs 

Fix use-after-free in yppush(8)

2014-11-04 Thread Dimitris Papastamos
Hi,

It seems to me that we should not free `pfd' at this point.  The saved
max poll fd is not reset to 0 and I do not see any guarantees that `pfd'
will point to valid memory after calling free() here.

Other code that follows the same style, like mountd(8) and rpc.rstatd(8)
do not do this.

OK?

===
RCS file: /cvs/src/usr.sbin/ypserv/yppush/yppush.c,v
retrieving revision 1.29
diff -u -p -r1.29 yppush.c
--- yppush.c11 Oct 2014 02:29:24 -  1.29
+++ yppush.c4 Nov 2014 14:19:56 -
@@ -99,7 +99,6 @@ my_svc_run(void)
exit(0);
default:
svc_getreq_poll(pfd, nready);
-   free(pfd);
break;
}
}



Implement mkfifo() and mkfifoat() using mknodat()

2014-10-23 Thread Dimitris Papastamos
Hi everyone,

This is my attempt to add libc wrappers for mkfifo() and
mkfifoat() using mknodat().

Do I need to do anything else?  Should the manpage for mkfifo(2)
and mkfifoat(2) be moved to section 3?

Cheers,
Dimitris

===
RCS file: /cvs/src/lib/libc/sys/Makefile.inc,v
retrieving revision 1.122
diff -u -p -r1.122 Makefile.inc
--- sys/Makefile.inc31 Aug 2014 04:02:08 -  1.122
+++ sys/Makefile.inc23 Oct 2014 14:14:13 -
@@ -11,7 +11,7 @@ SRCS+=Ovfork.S brk.S cerror.S exect.S f
sigsuspend.S syscall.S tfork_thread.S
 
 # glue to offer userland wrappers for some syscalls
-SRCS+= posix_madvise.c
+SRCS+= posix_madvise.c mkfifo.c mkfifoat.c
 
 # glue to provide compatibility between GCC 1.X and 2.X and for compat
 # with old syscall interfaces.
@@ -41,7 +41,7 @@ ASM=  __get_tcb.o __getcwd.o __semctl.o _
getsockopt.o getthrid.o gettimeofday.o getuid.o ioctl.o \
issetugid.o kevent.o kill.o kqueue.o ktrace.o lchown.o \
link.o linkat.o listen.o lstat.o madvise.o mincore.o \
-   minherit.o mkdir.o mkdirat.o mkfifo.o mkfifoat.o \
+   minherit.o mkdir.o mkdirat.o \
mknod.o mknodat.o mlock.o mlockall.o mount.o mprotect.o \
msgctl.o msgget.o msgrcv.o msgsnd.o msync.o munlock.o \
munlockall.o munmap.o nanosleep.o nfssvc.o \
Index: sys/mkfifo.c
===
RCS file: sys/mkfifo.c
diff -N sys/mkfifo.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ sys/mkfifo.c23 Oct 2014 14:14:13 -
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014 Dimitris Papastamos s...@2f30.org
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include sys/stat.h
+#include fcntl.h
+
+int
+mkfifo(const char *path, mode_t mode)
+{
+   return mknodat(AT_FDCWD, path, mode | S_IFIFO, 0);
+}
Index: sys/mkfifoat.c
===
RCS file: sys/mkfifoat.c
diff -N sys/mkfifoat.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ sys/mkfifoat.c  23 Oct 2014 14:14:13 -
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014 Dimitris Papastamos s...@2f30.org
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include sys/stat.h
+#include fcntl.h
+
+int
+mkfifoat(int fd, const char *path, mode_t mode)
+{
+   return mknodat(fd, path, mode | S_IFIFO, 0);
+}



Re: Implement mkfifo() and mkfifoat() using mknodat()

2014-10-23 Thread Dimitris Papastamos
On Thu, Oct 23, 2014 at 11:12:02AM -0400, Ted Unangst wrote:
 On Thu, Oct 23, 2014 at 15:16, Dimitris Papastamos wrote:
  Hi everyone,
  
  This is my attempt to add libc wrappers for mkfifo() and
  mkfifoat() using mknodat().
 
 Is there a reason? This would seem to do nothing except complicate the
 interpretation of ktrace.

This is a valid point.  I'll drop this for now.



select(2) to poll(2) conversion for mountd(8)

2014-10-20 Thread Dimitris Papastamos
Hi everyone,

I've tested this on my home setup and it seems to work.

Let me know if anything else needs working.

Cheers,
Dimitris

===
RCS file: /cvs/src/sbin/mountd/mountd.c,v
retrieving revision 1.76
diff -u -p -r1.76 mountd.c
--- mountd.c24 Aug 2014 14:45:00 -  1.76
+++ mountd.c20 Oct 2014 14:50:27 -
@@ -54,6 +54,7 @@
 #include grp.h
 #include netdb.h
 #include netgroup.h
+#include poll.h
 #include pwd.h
 #include signal.h
 #include stdio.h
@@ -289,10 +290,9 @@ main(int argc, char *argv[])
 void
 mountd_svc_run(void)
 {
-   fd_set *fds = NULL;
-   int fds_size = 0;
-   extern fd_set *__svc_fdset;
-   extern int __svc_fdsetsize;
+   struct pollfd *pfd = NULL, *newp;
+   nfds_t saved_max_pollfd = 0;
+   int nready;
 
for (;;) {
if (gothup) {
@@ -305,33 +305,30 @@ mountd_svc_run(void)
(caddr_t)0, umntall_each);
exit(0);
}
-   if (__svc_fdset) {
-   int bytes = howmany(__svc_fdsetsize, NFDBITS) *
-   sizeof(fd_mask);
-   if (fds_size != __svc_fdsetsize) {
-   if (fds)
-   free(fds);
-   fds = (fd_set *)malloc(bytes);  /* XXX */
-   fds_size = __svc_fdsetsize;
+   if (svc_max_pollfd  saved_max_pollfd) {
+   newp = reallocarray(pfd, svc_max_pollfd, sizeof(*pfd));
+   if (!newp) {
+   free(pfd);
+   perror(mountd_svc_run: - realloc failed);
+   return;
}
-   memcpy(fds, __svc_fdset, bytes);
-   } else {
-   if (fds)
-   free(fds);
-   fds = NULL;
+   pfd = newp;
+   saved_max_pollfd = svc_max_pollfd;
}
-   switch (select(svc_maxfd+1, fds, 0, 0, (struct timeval *)0)) {
+   memcpy(pfd, svc_pollfd, svc_max_pollfd * sizeof(*pfd));
+
+   nready = poll(pfd, svc_max_pollfd, INFTIM);
+   switch (nready) {
case -1:
if (errno == EINTR)
break;
-   perror(mountd_svc_run: - select failed);
-   if (fds)
-   free(fds);
+   perror(mountd_svc_run: - poll failed);
+   free(pfd);
return;
case 0:
break;
default:
-   svc_getreqset2(fds, svc_maxfd+1);
+   svc_getreq_poll(pfd, nready);
break;
}
}



Re: FIFO fd not marked readable after EOF

2014-10-07 Thread Dimitris Papastamos
On Mon, Oct 06, 2014 at 04:56:08PM -0600, Todd C. Miller wrote:
 The following diff should fix it.
 
  - todd
 
 Index: sys/miscfs/fifofs/fifo_vnops.c
 ===
 RCS file: /home/cvs/openbsd/src/sys/miscfs/fifofs/fifo_vnops.c,v
 retrieving revision 1.41
 diff -u -r1.41 fifo_vnops.c
 --- sys/miscfs/fifofs/fifo_vnops.c14 Sep 2014 14:17:26 -  1.41
 +++ sys/miscfs/fifofs/fifo_vnops.c6 Oct 2014 22:51:50 -
 @@ -147,8 +147,8 @@
   return (error);
   }
   fip-fi_readers = fip-fi_writers = 0;
 + wso-so_state |= SS_CANTSENDMORE;
   wso-so_snd.sb_lowat = PIPE_BUF;
 - rso-so_state |= SS_CANTRCVMORE;
   }
   if (ap-a_mode  FREAD) {
   fip-fi_readers++;
 @@ -287,24 +287,12 @@
  {
   struct vop_poll_args *ap = v;
   struct file filetmp;
 - short ostate;
   int revents = 0;
  
   if (ap-a_events  (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
 - /*
 -  * Socket and FIFO poll(2) semantics differ wrt EOF on read.
 -  * Unlike a normal socket, FIFOs don't care whether or not
 -  * SS_CANTRCVMORE is set.  To get the correct semantics we
 -  * must clear SS_CANTRCVMORE from so_state temporarily.
 -  */
 - ostate = ap-a_vp-v_fifoinfo-fi_readsock-so_state;
 - if (ap-a_events  (POLLIN | POLLRDNORM))
 - ap-a_vp-v_fifoinfo-fi_readsock-so_state =
 - ~SS_CANTRCVMORE;
   filetmp.f_data = ap-a_vp-v_fifoinfo-fi_readsock;
   if (filetmp.f_data)
   revents |= soo_poll(filetmp, ap-a_events, ap-a_p);
 - ap-a_vp-v_fifoinfo-fi_readsock-so_state = ostate;
   }
   if (ap-a_events  (POLLOUT | POLLWRNORM | POLLWRBAND)) {
   filetmp.f_data = ap-a_vp-v_fifoinfo-fi_writesock;

Thanks, that fixed it!



FIFO fd not marked readable after EOF

2014-10-06 Thread Dimitris Papastamos
Hi everyone,

I am using select(2) on a FIFO fd and monitoring
for readability.  select(2) doesn't return after the writer
exits.

The same piece of code marks the fd as readable on Linux.
Not sure which behaviour is correct though.

Doing a very similar test with stdin and closing it before
the call to select(2) behaves as expected and marks the fd
as readable.

I wrote the following PoC to demonstrate this.

Any ideas?

Thanks,
Dimitris

#include sys/select.h
#include sys/stat.h

#include err.h
#include errno.h
#include fcntl.h
#include stdio.h
#include unistd.h

#define FIFONAME testfifo

int
main(void)
{
int r;
int fd;
fd_set rfds;

r = mkfifo(FIFONAME, 0666);
if (r  0  errno != EEXIST)
err(1, mkfifo: %s, FIFONAME);
fd = open(FIFONAME, O_RDONLY | O_NONBLOCK);
if (fd  0)
err(1, open: %s, FIFONAME);
FD_ZERO(rfds);
FD_SET(fd, rfds);
printf(Now run cat  %s and ^D\n, FIFONAME);
again:
r = select(fd + 1, rfds, NULL, NULL, NULL);
if (r  0) {
if (errno == EINTR)
goto again;
err(1, select);
}
if (FD_ISSET(fd, rfds))
printf(fd is readable\n);
close(fd);
unlink(FIFONAME);
return 0;
}



Re: FIFO fd not marked readable after EOF

2014-10-06 Thread Dimitris Papastamos
On Mon, Oct 06, 2014 at 01:08:57PM +0100, Dimitris Papastamos wrote:
 The same piece of code marks the fd as readable on Linux.

I tested on NetBSD and it also seems to mark the fd readable.

 Doing a very similar test with stdin and closing it before
 the call to select(2) behaves as expected and marks the fd
 as readable.

To clarify, I meant calling select(2) on stdin and triggering
EOF marks the fd as readable.



Re: [PATCH] Better overflow handling in rcstime.c

2014-08-03 Thread Dimitris Papastamos
On Sat, Aug 02, 2014 at 11:07:30PM +0200, Fritjof Bornebusch wrote:
 On Wed, Jul 30, 2014 at 09:26:54PM +0100, Dimitris Papastamos wrote:
  On Wed, Jul 30, 2014 at 10:19:19PM +0200, Fritjof Bornebusch wrote:
   + tzone = (int)strtonum(h, -23, 23, errstr);
  
  The explicit cast is not needed here.
  
 That's maybe true, but I don't like implicit casts. :)

Sure, but it is not idiomatic.



Re: [PATCH] Better overflow handling in rcstime.c

2014-07-30 Thread Dimitris Papastamos
On Wed, Jul 30, 2014 at 10:19:19PM +0200, Fritjof Bornebusch wrote:
 + tzone = (int)strtonum(h, -23, 23, errstr);

The explicit cast is not needed here.



Re: [PATCH]: Convert select() to poll() in amd/info_hes.c

2014-05-08 Thread Dimitris Papastamos
On Wed, May 07, 2014 at 12:13:42PM -0700, patrick keshishian wrote:
 On Wed, May 07, 2014 at 02:47:32PM +0100, Dimitris Papastamos wrote:
  Hi,
  
  This piece of code now uses poll() instead of select().
  I have not got round to test this yet, but I will as soon as I have
  a working setup.
  
  Thoughts?
  
  ===
  RCS file: /cvs/src/usr.sbin/amd/amd/info_hes.c,v
  retrieving revision 1.13
  diff -u -p -r1.13 info_hes.c
  --- info_hes.c  24 Feb 2012 06:19:00 -  1.13
  +++ info_hes.c  7 May 2014 13:38:21 -
  @@ -66,7 +66,7 @@
   #endif
   
   static int soacnt;
  -static struct timeval hs_timeout;
  +static int hs_timeout;
   static int servernum;
   #endif /* HAS_HESIOD_RELOAD */
   
  @@ -261,12 +261,11 @@ hs_res_send(char *buf, int buflen, char 
   */
  for (retry = _res.retry; retry  0; retry--) {
  for (ns = 0; ns  hs_nscount; ns++) {
  -   hs_timeout.tv_sec =
  -   (_res.retrans  (_res.retry - retry))
  -   / hs_nscount;
  -   if (hs_timeout.tv_sec = 0)
  -   hs_timeout.tv_sec = 1;
  -   hs_timeout.tv_usec = 0;
  +   hs_timeout = _res.retrans  (_res.retry - retry);
  +   hs_timeout /= hs_nscount;
  +   hs_timeout *= 1000;
  +   if (hs_timeout = 0)
  +   hs_timeout = 1000;
 
 I imagine the values used here are small in scale, but
 I'm not sure. Any concerns of overflow given the new
 code is:
 
  - using an int type for calculations and storage vs
time_t in the original.
  - breaking the bit-shift, divide and assign statement
into two: a) bit-shift / assign, b) divide / assign.

What about using ppoll() instead?



[PATCH 0/2] libssl pqueue simplify allocations

2014-05-07 Thread Dimitris Papastamos
Hi all,

* No need to check for NULL before free.
* No need to cast the return value of the allocation functions.
* Do not use malloc() + memset(), simply use calloc()

Tested against the regression tests for pqueue.

ok?

===
--- pqueue.orig/pqueue.c
+++ pqueue/pqueue.c
@@ -69,8 +69,9 @@ typedef struct _pqueue {
 pitem *
 pitem_new(unsigned char *prio64be, void *data)
 {
-   pitem *item = (pitem *)malloc(sizeof(pitem));
+   pitem *item;
 
+   item = malloc(sizeof(pitem));
if (item == NULL)
return NULL;
 
@@ -85,30 +86,18 @@ pitem_new(unsigned char *prio64be, void
 void
 pitem_free(pitem *item)
 {
-   if (item == NULL)
-   return;
-
free(item);
 }
 
 pqueue_s *
 pqueue_new(void)
 {
-   pqueue_s *pq = (pqueue_s *)malloc(sizeof(pqueue_s));
-
-   if (pq == NULL)
-   return NULL;
-
-   memset(pq, 0x00, sizeof(pqueue_s));
-   return pq;
+   return calloc(1, sizeof(pqueue_s));
 }
 
 void
 pqueue_free(pqueue_s *pq)
 {
-   if (pq == NULL)
-   return;
-
free(pq);
 }
 



[PATCH 1/2] libssl simplify pqueue_find()

2014-05-07 Thread Dimitris Papastamos
Hi,

Simplify pqueue_find() to the very essential bits.  This change should
exhibit no functional changes or side-effects.

Tested against the regression tests for pqueue.

ok?

===
--- pqueue.orig/pqueue.c
+++ pqueue/pqueue.c
@@ -158,22 +158,11 @@ pitem *
 pqueue_find(pqueue_s *pq, unsigned char *prio64be)
 {
pitem *next;
-   pitem *found = NULL;
 
-   if (pq-items == NULL)
-   return NULL;
-
-   for (next = pq-items; next != NULL; next = next-next) {
-   if (memcmp(next-priority, prio64be, 8) == 0) {
-   found = next;
-   break;
-   }
-   }
-
-   if (!found)
-   return NULL;
-
-   return found;
+   for (next = pq-items; next != NULL; next = next-next)
+   if (memcmp(next-priority, prio64be, 8) == 0)
+   return next;
+   return NULL;
 }
 
 void



[PATCH 2/2] libssl remove magic number in pqueue.c

2014-05-07 Thread Dimitris Papastamos
Hi,

* Use sizeof() instead of hardcoding the size.

Tested against the regression tests for pqueue.

ok?

===
--- pqueue.orig/pqueue.c
+++ pqueue/pqueue.c
@@ -160,7 +160,8 @@ pqueue_find(pqueue_s *pq, unsigned char
pitem *next;
 
for (next = pq-items; next != NULL; next = next-next)
-   if (memcmp(next-priority, prio64be, 8) == 0)
+   if (memcmp(next-priority, prio64be,
+  sizeof(next-priority)) == 0)
return next;
return NULL;
 }



[PATCH]: Convert select() to poll() in amd/info_hes.c

2014-05-07 Thread Dimitris Papastamos
Hi,

This piece of code now uses poll() instead of select().
I have not got round to test this yet, but I will as soon as I have
a working setup.

Thoughts?

===
RCS file: /cvs/src/usr.sbin/amd/amd/info_hes.c,v
retrieving revision 1.13
diff -u -p -r1.13 info_hes.c
--- info_hes.c  24 Feb 2012 06:19:00 -  1.13
+++ info_hes.c  7 May 2014 13:38:21 -
@@ -66,7 +66,7 @@
 #endif
 
 static int soacnt;
-static struct timeval hs_timeout;
+static int hs_timeout;
 static int servernum;
 #endif /* HAS_HESIOD_RELOAD */
 
@@ -261,12 +261,11 @@ hs_res_send(char *buf, int buflen, char 
 */
for (retry = _res.retry; retry  0; retry--) {
for (ns = 0; ns  hs_nscount; ns++) {
-   hs_timeout.tv_sec =
-   (_res.retrans  (_res.retry - retry))
-   / hs_nscount;
-   if (hs_timeout.tv_sec = 0)
-   hs_timeout.tv_sec = 1;
-   hs_timeout.tv_usec = 0;
+   hs_timeout = _res.retrans  (_res.retry - retry);
+   hs_timeout /= hs_nscount;
+   hs_timeout *= 1000;
+   if (hs_timeout = 0)
+   hs_timeout = 1000;
if (s  0) {
s = socket(AF_INET, SOCK_STREAM, 0);
if (s  0) {
@@ -340,7 +339,7 @@ hs_readresp(int s, char *answer, int ans
cp = answer;
len = sizeof(short);
while (len != 0 
-   (n = hs_res_vcread(s, (char *)cp, (int)len, hs_timeout))  0) {
+   (n = hs_res_vcread(s, (char *)cp, (int)len, hs_timeout))  0) {
cp += n;
len -= n;
}
@@ -354,7 +353,7 @@ hs_readresp(int s, char *answer, int ans
return(-1);
}
while (len != 0 
-   (n = hs_res_vcread(s, (char *)cp, (int)len, hs_timeout))  0) {
+   (n = hs_res_vcread(s, (char *)cp, (int)len, hs_timeout))  0) {
cp += n;
len -= n;
}
@@ -364,7 +363,7 @@ hs_readresp(int s, char *answer, int ans
 }
 
 int
-hs_res_vcread(int sock, char *buf, int buflen, struct timeval *timeout)
+hs_res_vcread(int sock, char *buf, int buflen, int timeout)
 {
int n;
 
@@ -375,24 +374,16 @@ hs_res_vcread(int sock, char *buf, int b
 }
 
 int
-hs_res_selwait(int sock, struct timeval timeout)
+hs_res_selwait(int sock, int timeout)
 {
-   fd_set *fdsp;
-   int fdsn;
-   int n;
+   struct pollfd pfd[1];
 
/*
 * Wait for reply
 */
-   fdsn = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
-   if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
-   return(0);
-   memset(fdsp, 0, fdsn);
-
-   FD_SET(sock, fdsp);
-   n = select(sock+1, fdsp, NULL, NULL, timeout);
-   free(fdsp);
-   return(n);
+   pfd[0].fd = sock;
+   pfd[0].events = POLLIN;
+   return poll(pfd, 1, timeout);
 }
 
 /* Returns:



Re: patch: use a lookup table in BIO_get_port()

2014-04-30 Thread Dimitris Papastamos
On Tue, Apr 29, 2014 at 07:57:32PM -0600, Theo de Raadt wrote:
 Don't bother with diffs to b_sock.c.  Instead, if you have code
 which uses it, talk to krw.
 
 There is a monster diff coming which rewrites it all.
 
 And by the way, all that code disapears and is replaced by 2 lines.

That's great!

I am just looking for some work to do the for OpenBSD project.  I am
skimming through some source code, hopefully I will come up with some
patches.



patch: use a lookup table in BIO_get_port()

2014-04-29 Thread Dimitris Papastamos
Not sure this is sensible as it encourages people to simply
update the table.

I was inclined to remove the code entirely but I am not sure
what broken systems might rely on this.

Only build tested.

Thoughts?

Index: b_sock.c
===
RCS file: /cvs/src/lib/libssl/src/crypto/bio/b_sock.c,v
retrieving revision 1.33
diff -u -p -r1.33 b_sock.c
--- b_sock.c26 Apr 2014 18:56:37 -  1.33
+++ b_sock.c29 Apr 2014 13:55:39 -
@@ -140,6 +140,19 @@ BIO_get_port(const char *str, unsigned s
 {
int i;
struct servent *s;
+   size_t len;
+   struct {
+   const char *name;
+   int port;
+   } servmap[] = {
+   { http,   80   },
+   { telnet, 23   },
+   { socks,  1080 },
+   { https,  443  },
+   { ssl,443  },
+   { ftp,21   },
+   { gopher, 70   }
+   };
 
if (str == NULL) {
BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
@@ -155,21 +168,14 @@ BIO_get_port(const char *str, unsigned s
*port_ptr = ntohs((unsigned short)s-s_port);
CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
if (s == NULL) {
-   if (strcmp(str, http) == 0)
-   *port_ptr = 80;
-   else if (strcmp(str, telnet) == 0)
-   *port_ptr = 23;
-   else if (strcmp(str, socks) == 0)
-   *port_ptr = 1080;
-   else if (strcmp(str, https) == 0)
-   *port_ptr = 443;
-   else if (strcmp(str, ssl) == 0)
-   *port_ptr = 443;
-   else if (strcmp(str, ftp) == 0)
-   *port_ptr = 21;
-   else if (strcmp(str, gopher) == 0)
-   *port_ptr = 70;
-   else {
+   len = sizeof(servmap) / sizeof(servmap[0]);
+   for (i = 0; i  len; i++) {
+   if (strcmp(str, servmap[i].name) == 0) {
+   *port_ptr = servmap[i].port;
+   break;
+   }
+   }
+   if (i == len) {
SYSerr(SYS_F_GETSERVBYNAME, errno);
ERR_asprintf_error_data(service='%s', str);
return (0);