diff options
author | Michael Chan <mchan@broadcom.com> | 2007-12-25 00:28:09 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2008-01-28 18:00:05 -0500 |
commit | 7ffc49a6ee92b7138c2ee28073a8e10e58335d62 (patch) | |
tree | 6421d389980f3a71b1b53ed6e1548eb08dbf1226 /net | |
parent | 21371f768bf7127ee45bfaadd17899df6a439e8f (diff) |
[ETH]: Combine format_addr() with print_mac().
print_mac() used many most net drivers and format_addr() used by
net-sysfs.c are very similar and they can be intergrated.
format_addr() is also identically redefined in the qla4xxx iscsi
driver.
Export a new function sysfs_format_mac() to be used by net-sysfs,
qla4xxx and others in the future. Both print_mac() and
sysfs_format_mac() call _format_mac_addr() to do the formatting.
Changed print_mac() to use unsigned char * to be consistent with
net_device struct's dev_addr. Added buffer length overrun checking
as suggested by Joe Perches.
Signed-off-by: Michael Chan <mchan@broadcom.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net')
-rw-r--r-- | net/core/net-sysfs.c | 15 | ||||
-rw-r--r-- | net/ethernet/eth.c | 30 |
2 files changed, 29 insertions, 16 deletions
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index e41f4b9d2e7e..7635d3f72723 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c | |||
@@ -95,17 +95,6 @@ NETDEVICE_SHOW(type, fmt_dec); | |||
95 | NETDEVICE_SHOW(link_mode, fmt_dec); | 95 | NETDEVICE_SHOW(link_mode, fmt_dec); |
96 | 96 | ||
97 | /* use same locking rules as GIFHWADDR ioctl's */ | 97 | /* use same locking rules as GIFHWADDR ioctl's */ |
98 | static ssize_t format_addr(char *buf, const unsigned char *addr, int len) | ||
99 | { | ||
100 | int i; | ||
101 | char *cp = buf; | ||
102 | |||
103 | for (i = 0; i < len; i++) | ||
104 | cp += sprintf(cp, "%02x%c", addr[i], | ||
105 | i == (len - 1) ? '\n' : ':'); | ||
106 | return cp - buf; | ||
107 | } | ||
108 | |||
109 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, | 98 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, |
110 | char *buf) | 99 | char *buf) |
111 | { | 100 | { |
@@ -114,7 +103,7 @@ static ssize_t show_address(struct device *dev, struct device_attribute *attr, | |||
114 | 103 | ||
115 | read_lock(&dev_base_lock); | 104 | read_lock(&dev_base_lock); |
116 | if (dev_isalive(net)) | 105 | if (dev_isalive(net)) |
117 | ret = format_addr(buf, net->dev_addr, net->addr_len); | 106 | ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); |
118 | read_unlock(&dev_base_lock); | 107 | read_unlock(&dev_base_lock); |
119 | return ret; | 108 | return ret; |
120 | } | 109 | } |
@@ -124,7 +113,7 @@ static ssize_t show_broadcast(struct device *dev, | |||
124 | { | 113 | { |
125 | struct net_device *net = to_net_dev(dev); | 114 | struct net_device *net = to_net_dev(dev); |
126 | if (dev_isalive(net)) | 115 | if (dev_isalive(net)) |
127 | return format_addr(buf, net->broadcast, net->addr_len); | 116 | return sysfs_format_mac(buf, net->broadcast, net->addr_len); |
128 | return -EINVAL; | 117 | return -EINVAL; |
129 | } | 118 | } |
130 | 119 | ||
diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c index 6b2e454ae313..a7b417523e9b 100644 --- a/net/ethernet/eth.c +++ b/net/ethernet/eth.c | |||
@@ -359,10 +359,34 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) | |||
359 | } | 359 | } |
360 | EXPORT_SYMBOL(alloc_etherdev_mq); | 360 | EXPORT_SYMBOL(alloc_etherdev_mq); |
361 | 361 | ||
362 | char *print_mac(char *buf, const u8 *addr) | 362 | static size_t _format_mac_addr(char *buf, int buflen, |
363 | const unsigned char *addr, int len) | ||
363 | { | 364 | { |
364 | sprintf(buf, MAC_FMT, | 365 | int i; |
365 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); | 366 | char *cp = buf; |
367 | |||
368 | for (i = 0; i < len; i++) { | ||
369 | cp += scnprintf(cp, buflen - (cp - buf), "%02x", addr[i]); | ||
370 | if (i == len - 1) | ||
371 | break; | ||
372 | cp += strlcpy(cp, ":", buflen - (cp - buf)); | ||
373 | } | ||
374 | return cp - buf; | ||
375 | } | ||
376 | |||
377 | ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len) | ||
378 | { | ||
379 | size_t l; | ||
380 | |||
381 | l = _format_mac_addr(buf, PAGE_SIZE, addr, len); | ||
382 | l += strlcpy(buf + l, "\n", PAGE_SIZE - l); | ||
383 | return ((ssize_t) l); | ||
384 | } | ||
385 | EXPORT_SYMBOL(sysfs_format_mac); | ||
386 | |||
387 | char *print_mac(char *buf, const unsigned char *addr) | ||
388 | { | ||
389 | _format_mac_addr(buf, MAC_BUF_SIZE, addr, ETH_ALEN); | ||
366 | return buf; | 390 | return buf; |
367 | } | 391 | } |
368 | EXPORT_SYMBOL(print_mac); | 392 | EXPORT_SYMBOL(print_mac); |