Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Replace ether_nonstatic_aton with a



details:   https://anonhg.NetBSD.org/src/rev/2bbc86831670
branches:  trunk
changeset: 755009:2bbc86831670
user:      christos <christos%NetBSD.org@localhost>
date:      Wed May 19 20:41:59 2010 +0000

description:
Replace ether_nonstatic_aton with a
- better named one
- not suffering from buffer oveflow
- simpler
- handling different separators
- returning error codes for errors

Some ideas from one posted on tech-net by Jonathan A. Kollasch

diffstat:

 sys/arch/evbmips/adm5120/machdep.c |   6 +-
 sys/dev/usb/if_cdce.c              |   6 +-
 sys/net/if_ether.h                 |   4 +-
 sys/net/if_etherip.c               |   6 +-
 sys/net/if_ethersubr.c             |  69 +++++++++++++++++--------------------
 sys/net/if_tap.c                   |   6 +-
 6 files changed, 45 insertions(+), 52 deletions(-)

diffs (224 lines):

diff -r a357bc5aff06 -r 2bbc86831670 sys/arch/evbmips/adm5120/machdep.c
--- a/sys/arch/evbmips/adm5120/machdep.c        Wed May 19 20:04:42 2010 +0000
+++ b/sys/arch/evbmips/adm5120/machdep.c        Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: machdep.c,v 1.16 2010/02/08 19:02:27 joerg Exp $ */
+/* $NetBSD: machdep.c,v 1.17 2010/05/19 20:41:59 christos Exp $ */
 
 /*-
  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
@@ -107,7 +107,7 @@
  */
 
 #include <sys/cdefs.h>                 /* RCS ID & Copyright macro defns */
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.16 2010/02/08 19:02:27 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.17 2010/05/19 20:41:59 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -281,7 +281,7 @@
                } else if (strcmp(key, "kmac") == 0) {
                        prop_data_t pd;
 
-                       ether_nonstatic_aton(enaddr, val);
+                       (void)ether_aton_r(enaddr, sizeof(enaddr), val);
                        if (properties == NULL)
                                continue;
                        pd = prop_data_create_data(enaddr, sizeof(enaddr));
diff -r a357bc5aff06 -r 2bbc86831670 sys/dev/usb/if_cdce.c
--- a/sys/dev/usb/if_cdce.c     Wed May 19 20:04:42 2010 +0000
+++ b/sys/dev/usb/if_cdce.c     Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_cdce.c,v 1.28 2010/04/05 07:21:48 joerg Exp $ */
+/*     $NetBSD: if_cdce.c,v 1.29 2010/05/19 20:41:59 christos Exp $ */
 
 /*
  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul%windriver.com@localhost>
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.28 2010/04/05 07:21:48 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.29 2010/05/19 20:41:59 christos Exp $");
 #ifdef __NetBSD__
 #include "opt_inet.h"
 #endif
@@ -268,7 +268,7 @@
                memcpy(&eaddr[1], &hardclock_ticks, sizeof(u_int32_t));
                eaddr[5] = (u_int8_t)(device_unit(sc->cdce_dev));
        } else {
-               (void)ether_nonstatic_aton(eaddr, eaddr_str);
+               (void)ether_aton_r(eaddr, sizeof(eaddr), eaddr_str);
        }
 
        s = splnet();
diff -r a357bc5aff06 -r 2bbc86831670 sys/net/if_ether.h
--- a/sys/net/if_ether.h        Wed May 19 20:04:42 2010 +0000
+++ b/sys/net/if_ether.h        Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ether.h,v 1.57 2010/05/19 18:58:22 jakllsch Exp $   */
+/*     $NetBSD: if_ether.h,v 1.58 2010/05/19 20:41:59 christos Exp $   */
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -310,7 +310,7 @@
 uint32_t ether_crc32_le(const uint8_t *, size_t);
 uint32_t ether_crc32_be(const uint8_t *, size_t);
 
-int    ether_nonstatic_aton(u_char *, const char *);
+int    ether_aton_r(u_char *, size_t, const char *);
 #else
 /*
  * Prototype ethers(3) functions.
diff -r a357bc5aff06 -r 2bbc86831670 sys/net/if_etherip.c
--- a/sys/net/if_etherip.c      Wed May 19 20:04:42 2010 +0000
+++ b/sys/net/if_etherip.c      Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: if_etherip.c,v 1.29 2010/04/05 07:22:23 joerg Exp $        */
+/*      $NetBSD: if_etherip.c,v 1.30 2010/05/19 20:41:59 christos Exp $        */
 
 /*
  *  Copyright (c) 2006, Hans Rosenfeld <rosenfeld%grumpf.hope-2000.org@localhost>
@@ -86,7 +86,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_etherip.c,v 1.29 2010/04/05 07:22:23 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_etherip.c,v 1.30 2010/05/19 20:41:59 christos Exp $");
 
 #include "opt_inet.h"
 
@@ -691,7 +691,7 @@
                return EINVAL;
 
        /* Commit change */
-       if (ether_nonstatic_aton(enaddr, addr) != 0)
+       if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
                return EINVAL;
 
        if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
diff -r a357bc5aff06 -r 2bbc86831670 sys/net/if_ethersubr.c
--- a/sys/net/if_ethersubr.c    Wed May 19 20:04:42 2010 +0000
+++ b/sys/net/if_ethersubr.c    Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ethersubr.c,v 1.179 2010/05/19 18:58:22 jakllsch Exp $      */
+/*     $NetBSD: if_ethersubr.c,v 1.180 2010/05/19 20:41:59 christos Exp $      */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.179 2010/05/19 18:58:22 jakllsch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.180 2010/05/19 20:41:59 christos Exp $");
 
 #include "opt_inet.h"
 #include "opt_atalk.h"
@@ -1224,50 +1224,43 @@
  * ether_aton implementation, not using a static buffer.
  */
 int
-ether_nonstatic_aton(u_char *dest, const char *src)
+ether_aton_r(u_char *dest, size_t len, const char *str)
 {
-       int i;
-       char str[3 * ETHER_ADDR_LEN + 2];
-       u_char val[ETHER_ADDR_LEN];
-       char *cp;
+        int i;
+        const u_char *cp = (const void *)str;
+       u_char *ep;
 
-#define set_value                      \
-       if (*cp > '9' && *cp < 'a')     \
-               *cp -= 'A' - 10;        \
-       else if (*cp > '9')             \
-               *cp -= 'a' - 10;        \
-       else                            \
-               *cp -= '0'
+#define atox(c)        (((c) < '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
 
-       strlcpy(str, src, sizeof(str));
-
-       cp = str;
+       if (len < ETHER_ADDR_LEN)
+               return ENOSPC;
 
-       for (i = 0; i < ETHER_ADDR_LEN; i++) {
-               if (!isxdigit(*cp))
-                       return 1;
-               set_value;
-               val[i] = *cp++;
-               if (isxdigit(*cp)) {
-                       set_value;
-                       val[i] <<= 4;
-                       val[i] += *cp++;
-               }
-               if (*cp == ':' || *cp == '-' || *cp == '.' || *cp == ' ') {
+       ep = dest + ETHER_ADDR_LEN;
+        
+       while (*cp) {
+                if (!isxdigit(*cp))
+                        return EINVAL;
+               *dest = atox(*cp);
+               cp++;
+                if (isxdigit(*cp)) {
+                        *dest = (*dest << 4) | atox(*cp);
+                       dest++;
                        cp++;
-                       continue;
+                } else
+                       *dest++;
+               if (dest == ep)
+                       return *cp == '\0' ? 0 : ENAMETOOLONG;
+               switch (*cp) {
+               case ':':
+               case '-':
+               case '.':
+                       cp++;
+                       break;
                }
-               if (isxdigit(*cp) || i == (ETHER_ADDR_LEN - 1))
-                       continue;
-               return 1;
-       }
-       memcpy(dest, val, ETHER_ADDR_LEN);
-
-       return 0;
-#undef set_value
+        }
+       return ENOBUFS;
 }
 
-
 /*
  * Convert a sockaddr into an Ethernet address or range of Ethernet
  * addresses.
diff -r a357bc5aff06 -r 2bbc86831670 sys/net/if_tap.c
--- a/sys/net/if_tap.c  Wed May 19 20:04:42 2010 +0000
+++ b/sys/net/if_tap.c  Wed May 19 20:41:59 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_tap.c,v 1.64 2010/04/05 07:22:24 joerg Exp $        */
+/*     $NetBSD: if_tap.c,v 1.65 2010/05/19 20:41:59 christos Exp $     */
 
 /*
  *  Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.64 2010/04/05 07:22:24 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.65 2010/05/19 20:41:59 christos Exp $");
 
 #if defined(_KERNEL_OPT)
 
@@ -1391,7 +1391,7 @@
                return (EINVAL);
 
        /* Commit change */
-       if (ether_nonstatic_aton(enaddr, addr) != 0)
+       if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
                return (EINVAL);
        if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
        return (error);



Home | Main Index | Thread Index | Old Index