Module Name: src
Committed By: christos
Date: Wed May 19 20:41:59 UTC 2010
Modified Files:
src/sys/arch/evbmips/adm5120: machdep.c
src/sys/dev/usb: if_cdce.c
src/sys/net: if_ether.h if_etherip.c if_ethersubr.c if_tap.c
Log Message:
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
To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/evbmips/adm5120/machdep.c
cvs rdiff -u -r1.28 -r1.29 src/sys/dev/usb/if_cdce.c
cvs rdiff -u -r1.57 -r1.58 src/sys/net/if_ether.h
cvs rdiff -u -r1.29 -r1.30 src/sys/net/if_etherip.c
cvs rdiff -u -r1.179 -r1.180 src/sys/net/if_ethersubr.c
cvs rdiff -u -r1.64 -r1.65 src/sys/net/if_tap.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/arch/evbmips/adm5120/machdep.c
diff -u src/sys/arch/evbmips/adm5120/machdep.c:1.16 src/sys/arch/evbmips/adm5120/machdep.c:1.17
--- src/sys/arch/evbmips/adm5120/machdep.c:1.16 Mon Feb 8 14:02:27 2010
+++ src/sys/arch/evbmips/adm5120/machdep.c Wed May 19 16:41:59 2010
@@ -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));
Index: src/sys/dev/usb/if_cdce.c
diff -u src/sys/dev/usb/if_cdce.c:1.28 src/sys/dev/usb/if_cdce.c:1.29
--- src/sys/dev/usb/if_cdce.c:1.28 Mon Apr 5 03:21:48 2010
+++ src/sys/dev/usb/if_cdce.c Wed May 19 16:41:59 2010
@@ -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 <[email protected]>
@@ -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();
Index: src/sys/net/if_ether.h
diff -u src/sys/net/if_ether.h:1.57 src/sys/net/if_ether.h:1.58
--- src/sys/net/if_ether.h:1.57 Wed May 19 14:58:22 2010
+++ src/sys/net/if_ether.h Wed May 19 16:41:59 2010
@@ -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.
Index: src/sys/net/if_etherip.c
diff -u src/sys/net/if_etherip.c:1.29 src/sys/net/if_etherip.c:1.30
--- src/sys/net/if_etherip.c:1.29 Mon Apr 5 03:22:23 2010
+++ src/sys/net/if_etherip.c Wed May 19 16:41:59 2010
@@ -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 <[email protected]>
@@ -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);
Index: src/sys/net/if_ethersubr.c
diff -u src/sys/net/if_ethersubr.c:1.179 src/sys/net/if_ethersubr.c:1.180
--- src/sys/net/if_ethersubr.c:1.179 Wed May 19 14:58:22 2010
+++ src/sys/net/if_ethersubr.c Wed May 19 16:41:59 2010
@@ -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;
-
-#define set_value \
- if (*cp > '9' && *cp < 'a') \
- *cp -= 'A' - 10; \
- else if (*cp > '9') \
- *cp -= 'a' - 10; \
- else \
- *cp -= '0'
-
- strlcpy(str, src, sizeof(str));
-
- cp = str;
-
- 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 == ' ') {
+ int i;
+ const u_char *cp = (const void *)str;
+ u_char *ep;
+
+#define atox(c) (((c) < '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
+
+ if (len < ETHER_ADDR_LEN)
+ return ENOSPC;
+
+ 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++;
+ } else
+ *dest++;
+ if (dest == ep)
+ return *cp == '\0' ? 0 : ENAMETOOLONG;
+ switch (*cp) {
+ case ':':
+ case '-':
+ case '.':
cp++;
- continue;
+ 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.
Index: src/sys/net/if_tap.c
diff -u src/sys/net/if_tap.c:1.64 src/sys/net/if_tap.c:1.65
--- src/sys/net/if_tap.c:1.64 Mon Apr 5 03:22:24 2010
+++ src/sys/net/if_tap.c Wed May 19 16:41:59 2010
@@ -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);