Re: A system without interface?

2014-10-14 Thread Martin Pieuchot
On 08/10/14(Wed) 14:29, Martin Pieuchot wrote:
 I'm looking after the uses of the global list of interface.  These ones
 are pointless, you always have at least one interface on your system.
 
 Ok?

Anyone?

 
 Index: netinet/raw_ip.c
 ===
 RCS file: /home/ncvs/src/sys/netinet/raw_ip.c,v
 retrieving revision 1.75
 diff -u -p -r1.75 raw_ip.c
 --- netinet/raw_ip.c  14 Aug 2014 08:22:38 -  1.75
 +++ netinet/raw_ip.c  8 Oct 2014 12:25:31 -
 @@ -457,7 +457,7 @@ rip_usrreq(struct socket *so, int req, s
   error = EINVAL;
   break;
   }
 - if (TAILQ_EMPTY(ifnet) || addr-sin_family != AF_INET) {
 + if (addr-sin_family != AF_INET) {
   error = EADDRNOTAVAIL;
   break;
   }
 @@ -477,10 +477,6 @@ rip_usrreq(struct socket *so, int req, s
  
   if (nam-m_len != sizeof(*addr)) {
   error = EINVAL;
 - break;
 - }
 - if (TAILQ_EMPTY(ifnet)) {
 - error = EADDRNOTAVAIL;
   break;
   }
   if (addr-sin_family != AF_INET) {
 Index: netinet6/raw_ip6.c
 ===
 RCS file: /home/ncvs/src/sys/netinet6/raw_ip6.c,v
 retrieving revision 1.68
 diff -u -p -r1.68 raw_ip6.c
 --- netinet6/raw_ip6.c22 Jul 2014 11:06:10 -  1.68
 +++ netinet6/raw_ip6.c8 Oct 2014 12:25:31 -
 @@ -665,7 +665,7 @@ rip6_usrreq(struct socket *so, int req, 
   error = EINVAL;
   break;
   }
 - if (TAILQ_EMPTY(ifnet) || (addr-sin6_family != AF_INET6)) {
 + if (addr-sin6_family != AF_INET6) {
   error = EADDRNOTAVAIL;
   break;
   }
 @@ -708,10 +708,6 @@ rip6_usrreq(struct socket *so, int req, 
  
   if (nam-m_len != sizeof(*addr)) {
   error = EINVAL;
 - break;
 - }
 - if (TAILQ_EMPTY(ifnet)) {
 - error = EADDRNOTAVAIL;
   break;
   }
   if (addr-sin6_family != AF_INET6) {
 



Re: A system without interface?

2014-10-14 Thread Mike Belopuhov
On 14 October 2014 11:01, Martin Pieuchot mpieuc...@nolizard.org wrote:
 On 08/10/14(Wed) 14:29, Martin Pieuchot wrote:
 I'm looking after the uses of the global list of interface.  These ones
 are pointless, you always have at least one interface on your system.

 Ok?

 Anyone?


looks good to me. ok mikeb



Re: A system without interface?

2014-10-14 Thread Claudio Jeker
On Tue, Oct 14, 2014 at 11:01:46AM +0200, Martin Pieuchot wrote:
 On 08/10/14(Wed) 14:29, Martin Pieuchot wrote:
  I'm looking after the uses of the global list of interface.  These ones
  are pointless, you always have at least one interface on your system.
  
  Ok?
 
 Anyone?

OK. But we should make loopattach() panic if it fails to clone lo0.

 
  
  Index: netinet/raw_ip.c
  ===
  RCS file: /home/ncvs/src/sys/netinet/raw_ip.c,v
  retrieving revision 1.75
  diff -u -p -r1.75 raw_ip.c
  --- netinet/raw_ip.c14 Aug 2014 08:22:38 -  1.75
  +++ netinet/raw_ip.c8 Oct 2014 12:25:31 -
  @@ -457,7 +457,7 @@ rip_usrreq(struct socket *so, int req, s
  error = EINVAL;
  break;
  }
  -   if (TAILQ_EMPTY(ifnet) || addr-sin_family != AF_INET) {
  +   if (addr-sin_family != AF_INET) {
  error = EADDRNOTAVAIL;
  break;
  }
  @@ -477,10 +477,6 @@ rip_usrreq(struct socket *so, int req, s
   
  if (nam-m_len != sizeof(*addr)) {
  error = EINVAL;
  -   break;
  -   }
  -   if (TAILQ_EMPTY(ifnet)) {
  -   error = EADDRNOTAVAIL;
  break;
  }
  if (addr-sin_family != AF_INET) {
  Index: netinet6/raw_ip6.c
  ===
  RCS file: /home/ncvs/src/sys/netinet6/raw_ip6.c,v
  retrieving revision 1.68
  diff -u -p -r1.68 raw_ip6.c
  --- netinet6/raw_ip6.c  22 Jul 2014 11:06:10 -  1.68
  +++ netinet6/raw_ip6.c  8 Oct 2014 12:25:31 -
  @@ -665,7 +665,7 @@ rip6_usrreq(struct socket *so, int req, 
  error = EINVAL;
  break;
  }
  -   if (TAILQ_EMPTY(ifnet) || (addr-sin6_family != AF_INET6)) {
  +   if (addr-sin6_family != AF_INET6) {
  error = EADDRNOTAVAIL;
  break;
  }
  @@ -708,10 +708,6 @@ rip6_usrreq(struct socket *so, int req, 
   
  if (nam-m_len != sizeof(*addr)) {
  error = EINVAL;
  -   break;
  -   }
  -   if (TAILQ_EMPTY(ifnet)) {
  -   error = EADDRNOTAVAIL;
  break;
  }
  if (addr-sin6_family != AF_INET6) {
  
 

-- 
:wq Claudio



Make loopattach() panic [was Re: A system without interface?]

2014-10-14 Thread Martin Pieuchot
On 14/10/14(Tue) 14:18, Claudio Jeker wrote:
 On Tue, Oct 14, 2014 at 11:01:46AM +0200, Martin Pieuchot wrote:
  On 08/10/14(Wed) 14:29, Martin Pieuchot wrote:
   I'm looking after the uses of the global list of interface.  These ones
   are pointless, you always have at least one interface on your system.
   
   Ok?
  
  Anyone?
 
 OK. But we should make loopattach() panic if it fails to clone lo0.

Makes sense, here's the diff.

Index: net/if_loop.c
===
RCS file: /home/ncvs/src/sys/net/if_loop.c,v
retrieving revision 1.58
diff -u -p -r1.58 if_loop.c
--- net/if_loop.c   7 Oct 2014 08:47:28 -   1.58
+++ net/if_loop.c   14 Oct 2014 12:39:50 -
@@ -154,7 +154,9 @@ struct if_clone loop_cloner =
 void
 loopattach(int n)
 {
-   (void) loop_clone_create(loop_cloner, 0);
+   if (loop_clone_create(loop_cloner, 0))
+   panic(unable to create lo0);
+
if_clone_attach(loop_cloner);
 }
 



Re: mpe patch: use rt_ifa_{add,del}

2014-10-14 Thread Rafael Zalamena
On Wed, Oct 08, 2014 at 06:54:14PM -0300, Rafael Zalamena wrote:
 On Wed, Oct 08, 2014 at 09:22:44AM +0200, Martin Pieuchot wrote:
  On 07/10/14(Tue) 18:44, Rafael Zalamena wrote:
   On Sat, Oct 04, 2014 at 07:39:03PM -0300, Rafael Zalamena wrote:
On Thu, Oct 02, 2014 at 02:36:12PM +0200, Martin Pieuchot wrote:
 On 01/10/14(Wed) 21:54, Rafael Zalamena wrote:
  --- old chat snip ---

   
   Code changed:
* Replaced old function that used to create routes in favor of rt_ifa_*
* Modified rt_ifa_{add,del} to handle MPLS addresses: when creating an
  route to a MPLS interface it means we want to remove labels. Also MPLS
  only works on rdomain 0
  
  Even if they only work on rdomain 0, I'd prefer not to add code to
  enforce this behavior.  It's like making it harder for people to make it
  work any rdomain.
  
  Other than that, I'm ok with your diff.
  
 
 I removed the code that hardcoded RTF_MPLS to rdomain 0, now we use a
 function to handle the rdomain switching to install routes.
 
 Index: sys/net/if_mpe.c
 ===
 RCS file: /home/rzalamena/obsdcvs/src/sys/net/if_mpe.c,v
 retrieving revision 1.35
 diff -u -p -r1.35 if_mpe.c
 --- sys/net/if_mpe.c  22 Jul 2014 11:06:09 -  1.35
 +++ sys/net/if_mpe.c  8 Oct 2014 21:48:15 -
 @@ -61,7 +61,7 @@ int mpeioctl(struct ifnet *, u_long, cad
  void mpestart(struct ifnet *);
  int  mpe_clone_create(struct if_clone *, int);
  int  mpe_clone_destroy(struct ifnet *);
 -int  mpe_newlabel(struct ifnet *, int, struct shim_hdr *);
 +int  mpe_iflabelroute(struct ifnet *, struct shim_hdr *, int);
  
  LIST_HEAD(, mpe_softc)   mpeif_list;
  struct if_clone  mpe_cloner =
 @@ -333,10 +333,10 @@ mpeioctl(struct ifnet *ifp, u_long cmd, 
   ifm = ifp-if_softc;
   if (ifm-sc_shim.shim_label) {
   /* remove old MPLS route */
 - mpe_newlabel(ifp, RTM_DELETE, ifm-sc_shim);
 + mpe_iflabelroute(ifp, ifm-sc_shim, 0);
   }
   /* add new MPLS route */
 - error = mpe_newlabel(ifp, RTM_ADD, shim);
 + error = mpe_iflabelroute(ifp, shim, 1);
   if (error)
   break;
   ifm-sc_shim.shim_label = shim.shim_label;
 @@ -346,8 +346,7 @@ mpeioctl(struct ifnet *ifp, u_long cmd, 
   ifm = ifp-if_softc;
   if (ifr-ifr_rdomainid != ifp-if_rdomain) {
   if (ifm-sc_shim.shim_label) {
 - shim.shim_label = ifm-sc_shim.shim_label;
 - error = mpe_newlabel(ifp, RTM_ADD, shim);
 + mpe_iflabelroute(ifp, ifm-sc_shim, 1);
   }
   }
   /* return with ENOTTY so that the parent handler finishes */
 @@ -443,37 +442,29 @@ mpe_input6(struct mbuf *m, struct ifnet 
  }
  #endif   /* INET6 */
  
 +/*
 + * Install or remove mpe interface label routes using rdomain 0.
 + */
  int
 -mpe_newlabel(struct ifnet *ifp, int cmd, struct shim_hdr *shim)
 +mpe_iflabelroute(struct ifnet *ifp, struct shim_hdr *shim, int add)
  {
 - struct rtentry *nrt;
 - struct sockaddr_mpls dst;
 - struct rt_addrinfo info;
 - int error;
 -
 - bzero(dst, sizeof(dst));
 - dst.smpls_len = sizeof(dst);
 - dst.smpls_family = AF_MPLS;
 - dst.smpls_label = shim-shim_label;
 -
 - bzero(info, sizeof(info));
 - info.rti_flags = RTF_UP | RTF_MPLS;
 - info.rti_mpls = MPLS_OP_POP;
 - info.rti_info[RTAX_DST] = smplstosa(dst);
 - info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)ifp-if_sadl;
 -
 - error = rtrequest1(cmd, info, RTP_CONNECTED, nrt, 0);
 - rt_missmsg(cmd, info, error ? 0 : nrt-rt_flags, ifp, error, 0);
 - if (cmd == RTM_DELETE) {
 - if (error == 0  nrt != NULL) {
 - if (nrt-rt_refcnt = 0) {
 - nrt-rt_refcnt++;
 - rtfree(nrt);
 - }
 - }
 - }
 - if (cmd == RTM_ADD  error == 0  nrt != NULL) {
 - nrt-rt_refcnt--;
 - }
 + int error;
 + struct  sockaddr_mpls smpls;
 + u_short rdomain = ifp-if_rdomain;
 +
 + ifp-if_rdomain = 0;
 +
 + memset(smpls, 0, sizeof(smpls));
 + smpls.smpls_family = AF_MPLS;
 + smpls.smpls_label = shim-shim_label;
 + smpls.smpls_len = sizeof(smpls);
 + if (add)
 + error = rt_ifa_add(ifp-if_lladdr, RTF_MPLS | RTF_UP,
 + smplstosa(smpls));
 + else
 + error = rt_ifa_del(ifp-if_lladdr, RTF_MPLS | RTF_UP,
 + smplstosa(smpls));
 +
 + ifp-if_rdomain = rdomain;
   return (error);
  }
 Index: sys/net/route.c
 ===
 RCS file: /home/rzalamena/obsdcvs/src/sys/net/route.c,v
 retrieving revision 1.185
 diff -u -p -r1.185 

Replace LibreSSL times() call

2014-10-14 Thread Jonas 'Sortie' Termansen
Hi,

I noticed libressl's apps.c is using times(3), which is among the functions I am
aggressively deprecating in my personal system. This patch switches it to use
the clock_gettime and getrusage instead. I pondered using CLOCK_VIRTUAL rather
than getrusage, but it turned out to be not be implemented and not portable.

Unfortunately, OS X doesn't have clock_gettime, so the portable version will
have to add back a times call as a fallback, or perhaps use gettimeofday (but
this doesn't have the proper time-doesn't-go-backwards semantics).

I didn't use the useful, but non-standard timespecsub and TIMEVAL_TO_TIMESPEC
macros from sys/time.h to make things easier for the portable version.
Alternatively they could be used and a fallback implementation can be added to
the libressl time.h wrapper header.

Jonas

--- libressl-2.1.0/apps/apps.c  2014-10-11 18:58:11.0 +0200
+++ libssl/apps/apps.c  2014-10-14 21:31:44.827167386 +0200
@@ -126,7 +126,6 @@
 
 #include sys/types.h
 #include sys/stat.h
-#include sys/times.h
 
 #include ctype.h
 #include errno.h
@@ -135,6 +134,7 @@
 #include limits.h
 #include string.h
 #include strings.h
+#include time.h
 #include unistd.h
 
 #include apps.h
@@ -2203,25 +2203,40 @@
 #endif
 /* !OPENSSL_NO_TLSEXT  !OPENSSL_NO_NEXTPROTONEG */
 
+static struct timespec ts_elapsed(struct timespec a, struct timespec b)
+{
+   a.tv_sec -= b.tv_sec;
+   a.tv_nsec -= b.tv_nsec;
+   if ( a.tv_nsec  0 )
+   {
+   a.tv_nsec += 10L;
+   a.tv_sec -= 1;
+   }
+   return a;
+}
+
 double
 app_tminterval(int stop, int usertime)
 {
-   double ret = 0;
-   struct tms rus;
-   clock_t now = times(rus);
-   static clock_t tmstart;
+   static struct timespec start_ts;
+   struct timespec now_ts;
 
-   if (usertime)
-   now = rus.tms_utime;
+   if (usertime) {
+   struct rusage ru;
+   getrusage(RUSAGE_SELF, ru);
+   now_ts.tv_sec = ru.ru_utime.tv_sec;
+   now_ts.tv_nsec = ru.ru_utime.tv_usec * 1000L;
+   } else {
+   clock_gettime(CLOCK_MONOTONIC, now_ts);
+   }
 
-   if (stop == TM_START)
-   tmstart = now;
-   else {
-   long int tck = sysconf(_SC_CLK_TCK);
-   ret = (now - tmstart) / (double) tck;
+   if (stop == TM_START) {
+   start_ts = now_ts;
+   return 0.0;
}
 
-   return (ret);
+   struct timespec elapsed_ts = ts_elapsed(now_ts, start_ts);
+   return (double) elapsed_ts.tv_sec + (double) elapsed_ts.tv_nsec / 1E9;
 }
 
 int



app_tminterval callers use redundant constants

2014-10-14 Thread Jonas 'Sortie' Termansen
Hi,

The app_tminterval utility function in apps.c has a first parameter that is
either TM_START or TM_STOP as defined in apps.h. The two files that use this
function disregard those constants and define their own along with their own
wrapper utility wrapper function.

This patch keeps the wrapper functions as they provide a little convenience, but
removes the duplicate constants and fixes calls to use the apps.h constants.

There also appears to be some left over compatibility for platforms without
timens. Since the libressl build system doesn't define TIMES, this code thinks
the feature isn't available doesn't when reporting usage.

Jonas

diff -Nur libressl-2.1.0/apps/speed.c libssl/apps/speed.c
--- libressl-2.1.0/apps/speed.c 2014-10-11 18:58:11.0 +0200
+++ libssl/apps/speed.c 2014-10-14 17:45:02.778749560 +0200
@@ -203,9 +203,6 @@
run = 0;
 }
 
-#define START  0
-#define STOP   1
-
 
 static double
 Time_F(int s)
@@ -481,10 +478,6 @@
int multi = 0;
const char *errstr = NULL;
 
-#ifndef TIMES
-   usertime = -1;
-#endif
-
memset(results, 0, sizeof(results));
memset(dsa_key, 0, sizeof(dsa_key));
for (i = 0; i  EC_NUM; i++)
@@ -959,9 +952,7 @@
 
BIO_printf(bio_err, \n);
BIO_printf(bio_err, Available options:\n);
-#if defined(TIMES) || defined(USE_TOD)
BIO_printf(bio_err, -elapsedmeasure time in 
real time instead of CPU user time.\n);
-#endif
 #ifndef OPENSSL_NO_ENGINE
BIO_printf(bio_err, -engine e   use engine e, 
possibly a hardware device.\n);
 #endif
@@ -1066,10 +1057,10 @@
if (doit[D_MDC2]) {
for (j = 0; j  SIZE_NUM; j++) {
print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);
-   Time_F(START);
+   Time_F(TM_START);
for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)
EVP_Digest(buf, (unsigned long) lengths[j], 
(mdc2[0]), NULL, EVP_mdc2(), NULL);
-   d = Time_F(STOP);
+   d = Time_F(TM_STOP);
print_result(D_MDC2, j, count, d);
}
}
@@ -1079,10 +1070,10 @@
if (doit[D_MD4]) {
for (j = 0; j  SIZE_NUM; j++) {
print_message(names[D_MD4], c[D_MD4][j], lengths[j]);
-   Time_F(START);
+   Time_F(TM_START);
for (count = 0, run = 1; COND(c[D_MD4][j]); count++)
EVP_Digest((buf[0]), (unsigned long) 
lengths[j], (md4[0]), NULL, EVP_md4(), NULL);
-   d = Time_F(STOP);
+   d = Time_F(TM_STOP);
print_result(D_MD4, j, count, d);
}
}
@@ -1092,10 +1083,10 @@
if (doit[D_MD5]) {
for (j = 0; j  SIZE_NUM; j++) {
print_message(names[D_MD5], c[D_MD5][j], lengths[j]);
-   Time_F(START);
+   Time_F(TM_START);
for (count = 0, run = 1; COND(c[D_MD5][j]); count++)
EVP_Digest((buf[0]), (unsigned long) 
lengths[j], (md5[0]), NULL, EVP_get_digestbyname(md5), NULL);
-   d = Time_F(STOP);
+   d = Time_F(TM_STOP);
print_result(D_MD5, j, count, d);
}
}
@@ -,13 +1102,13 @@
 
for (j = 0; j  SIZE_NUM; j++) {
print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);
-   Time_F(START);
+   Time_F(TM_START);
for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {
HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
HMAC_Update(hctx, buf, lengths[j]);
HMAC_Final(hctx, (hmac[0]), NULL);
}
-   d = Time_F(STOP);
+   d = Time_F(TM_STOP);
print_result(D_HMAC, j, count, d);
}
HMAC_CTX_cleanup(hctx);
@@ -1127,10 +1118,10 @@
if (doit[D_SHA1]) {
for (j = 0; j  SIZE_NUM; j++) {
print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);
-   Time_F(START);
+   Time_F(TM_START);
for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)
EVP_Digest(buf, (unsigned long) lengths[j], 
(sha[0]), NULL, EVP_sha1(), NULL);
-   d = Time_F(STOP);
+   d = Time_F(TM_STOP);
print_result(D_SHA1, j, count, d);
}
}
@@ -1138,10 +1129,10 @@
if (doit[D_SHA256]) {
for (j = 0; 

CLOCK_VIRTUAL

2014-10-14 Thread Jonas 'Sortie' Termansen
Hi,

I noticed clock_gettime(2) describes a CLOCK_VIRTUAL clock that measures
how long a process has run in user-space. However, it is not implemented
in sys/kern/kern_time.c where it fails with EINVAL in the default switch
case.

It does seem to be implemented in FreeBSD and NetBSD along with a
CLOCK_PROF counterpart. It is not part of POSIX, but a quick online
search suggests it might have been part of ancient POSIX revisions (that
I don't have access to).

I see these options:

1) Remove CLOCK_VIRTUAL fully as it doesn't work and is non-standard.
This might break some ports but that code wasn't working in the first place.

2) Don't mention it at all in clock_gettime(2) and keep it for
compatibility purposes.

3) Document it is unimplemented in the BUGS section.

4) Implement it. The kernel already has the information to implement
getrusage(2) and the CLOCK_PROCESS_CPUTIME_ID case is similar.

If it remains documented, it should also be marked as an extension under
STANDARDS.

Additionally, perhaps further clocks should be added to get the
individual user-time and system-time components of
CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID? These can be
determined in the process case with getrusage(2), but I know of no way
in the thread case.

Jonas