: Combine format_addr() with print_mac().
Subsystems (4)
[ETH]: Combine format_addr() with print_mac().
print_mac() used by most net drivers and format_addr() used by net-sysfs.c are very similar and they can be integrated.
format_addr() is also identically redefined in the qla4xxx iscsi driver.
Export a new function format_mac_addr() to be used by net-sysfs, qla4xxx and others in the future. Both print_mac() and format_mac_addr() call _format_mac_addr() to do the formatting.
drivers/scsi/qla4xxx/ql4_os.c +1 -13
@@ -173,18 +173,6 @@ static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)printk (KERN_ERR "iscsi: invalid stop flag %d\n" , flag); } -static ssize_t format_addr (char *buf,const unsigned char *addr,int len) -{ -int i; -char *cp = buf; - -for (i =0 ; i < len; i++) - cp +=sprintf (cp,"%02x%c" , addr[i], - i == (len -1 ) ?'\n' :':' ); -return cp - buf; -} - -static int qla4xxx_host_get_param (struct Scsi_Host *shost,enum iscsi_host_param param,char *buf) {
@@ -193,7 +181,7 @@ static int qla4xxx_host_get_param(struct Scsi_Host *shost,switch (param) {case ISCSI_HOST_PARAM_HWADDRESS : - len =format_addr (buf, ha->my_mac,MAC_ADDR_LEN ); + len =format_mac_addr (buf, ha->my_mac,MAC_ADDR_LEN );break ;case ISCSI_HOST_PARAM_IPADDRESS : len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
include/linux/if_ether.h +2 -1
@@ -124,10 +124,11 @@ int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);extern struct ctl_table ether_table[];#endif +extern ssize_t format_mac_addr (char *buf,const unsigned char *addr,int len); +/* * Display a 6 byte device address (MAC) in a readable format. */ -#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"extern char *print_mac (char *buf,const u8 *addr);#define DECLARE_MAC_BUF (var) char var[18] __maybe_unused
net/core/net-sysfs.c +2 -13
@@ -95,17 +95,6 @@ NETDEVICE_SHOW(type, fmt_dec);NETDEVICE_SHOW (link_mode, fmt_dec);/* use same locking rules as GIFHWADDR ioctl's */ -static ssize_t format_addr (char *buf,const unsigned char *addr,int len) -{ -int i; -char *cp = buf; - -for (i =0 ; i < len; i++) - cp +=sprintf (cp,"%02x%c" , addr[i], - i == (len -1 ) ?'\n' :':' ); -return cp - buf; -} -static ssize_t show_address (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -114,7 +103,7 @@ static ssize_t show_address(struct device *dev, struct device_attribute *attr,read_lock (&dev_base_lock);if (dev_isalive (net)) - ret =format_addr (buf, net->dev_addr, net->addr_len); + ret =format_mac_addr (buf, net->dev_addr, net->addr_len);read_unlock (&dev_base_lock);return ret; }
@@ -124,7 +113,7 @@ static ssize_t show_broadcast(struct device *dev, {struct net_device *net =to_net_dev (dev);if (dev_isalive (net)) -return format_addr (buf, net->broadcast, net->addr_len); +return format_mac_addr (buf, net->broadcast, net->addr_len);return -EINVAL ; }
net/ethernet/eth.c +25 -2
@@ -359,10 +359,33 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) }EXPORT_SYMBOL (alloc_etherdev_mq); +static ssize_t _format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +int i; +char *cp = buf; + +for (i =0 ; i < len; i++) { + cp +=sprintf (cp,"%02x" , addr[i]); +if (i == len -1 ) +break ; + *cp++ =':' ; + } +return cp - buf; +} + +ssize_t format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +ssize_t l; + + l =_format_mac_addr (buf, addr, len); +strcpy (buf + l,"\n" ); +return l +1 ; +} +EXPORT_SYMBOL (format_mac_addr); +char *print_mac (char *buf,const u8 *addr) { -sprintf (buf,MAC_FMT , - addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]); +_format_mac_addr (buf, addr,ETH_ALEN );return buf; }EXPORT_SYMBOL (print_mac);
Quoted 35 lines · Michael Chan
[ETH]: Combine
format_addr()withprint_mac().print_mac()used by most net drivers andformat_addr()used bynet-sysfs.care very similar and they can be integrated.format_addr()is also identically redefined in the qla4xxx iscsi driver.net/ethernet/eth.c +22
-0@@ -359,10 +359,33 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) }EXPORT_SYMBOL (alloc_etherdev_mq); +static ssize_t _format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +int i; +char *cp = buf; + +for (i =0 ; i < len; i++) { + cp +=sprintf (cp,"%02x" , addr[i]); +if (i == len -1 ) +break ; + *cp++ =':' ; + } +return cp - buf; +} + +ssize_t format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +ssize_t l; + + l =_format_mac_addr (buf, addr, len); +strcpy (buf + l,"\n" ); +return l +1 ; +}
net/ethernet/eth.c struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) · line 391
Quoted 10 lines
net/ethernet/eth.c +3
-2+EXPORT_SYMBOL (format_mac_addr); +char *print_mac (char *buf,const u8 *addr) { -sprintf (buf,MAC_FMT , - addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]); +_format_mac_addr (buf, addr,ETH_ALEN );return buf; }EXPORT_SYMBOL (print_mac);
I think const unsigned char *addr should be const u8 *addr ssize_t? shouldn't it be size_t?
Indexing buf by int len is unchecked.
That could lead to unintended buffer overruns. Maybe add a buflen argument and use snprintf?
I had a patch that added some type-safety to print_mac and prevented this unintended buffer overrun.
It seems it wasn't applied.
---------------------------------------
Subject: Re: [PATCH net-2.6.24] introduce MAC_FMT/MAC_ARG
Date: Mon, 24 Sep 2007 10:28:36 -0700
Here is a patch that adds some type safety to print_mac by using a struct print_mac_buf * instead of char *.
It also reduces the defconfig vmlinux size by 8 bytes.
--
include/linux/if_ether.h | 12 ++++++++++-- net/ethernet/eth.c | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-)
include/linux/if_ether.h +10 -2
@@ -126,7 +126,15 @@ extern struct ctl_table ether_table[];* Display a 6 byte device address (MAC) in a readable format. */ #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -extern char *print_mac (char *buf,const u8 *addr); -#define DECLARE_MAC_BUF (var) char var[18] __maybe_unused + +struct print_mac_buf { +char formatted_mac_addr[18 ]; +}; + +#define DECLARE_MAC_BUF (var) \ + struct print_mac_buf __maybe_unused _##var; \ + struct print_mac_buf __maybe_unused *var = &_##var + +extern char *print_mac (struct print_mac_buf *buf,const u8 *addr);#endif /* _LINUX_IF_ETHER_H */
net/ethernet/eth.c +0 -0
@@ -338,10 +338,10 @@ struct net_device *alloc_etherdev_mq(int
sizeof_priv, unsigned int queue_count)
}
EXPORT_SYMBOL(alloc_etherdev_mq);
-char *print_mac(char *buf, const u8 *addr)
+char *print_mac(struct print_mac_buf *buf, const u8 *addr)
{
- sprintf(buf, MAC_FMT,
+ sprintf(buf->formatted_mac_addr, MAC_FMT,
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
- return buf;
+ return buf->formatted_mac_addr;
}
EXPORT_SYMBOL(print_mac);
Quoted 35 lines · Joe Perches
Quoted 34 lines · Michael Chan
net/ethernet/eth.c +24
-0@@ -359,10 +359,33 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) }EXPORT_SYMBOL (alloc_etherdev_mq); +static ssize_t _format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +int i; +char *cp = buf; + +for (i =0 ; i < len; i++) { + cp +=sprintf (cp,"%02x" , addr[i]); +if (i == len -1 ) +break ; + *cp++ =':' ; + } +return cp - buf; +} + +ssize_t format_mac_addr (char *buf,const unsigned char *addr,int len) +{ +ssize_t l; + + l =_format_mac_addr (buf, addr, len); +strcpy (buf + l,"\n" ); +return l +1 ; +} +EXPORT_SYMBOL (format_mac_addr); +char *print_mac (char *buf,const u8 *addr) {
Quoted 8 lines
net/ethernet/eth.c struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) · line 391
Quoted 6 lines
net/ethernet/eth.c +1
-2-sprintf (buf,MAC_FMT , - addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]); +_format_mac_addr (buf, addr,ETH_ALEN );return buf; }EXPORT_SYMBOL (print_mac);I think const unsigned char *addr should be const u8 *addr
The dev_addr is declared as unsigned char* in struct net_device. To be consistent, can we change print_mac() and MAC_BUF to use unsigned char*? They are really the same.
Quoted 1 line
ssize_t? shouldn't it besize_t?
I'm just keeping the prototype unchanged as originally defined in net- sysfs.c
Quoted 3 lines
Indexing buf by int len is unchecked.
That could lead to unintended buffer overruns. Maybe add a buflen argument and use snprintf?
Again, I kept the semantics the same as the original, but will be happy to add a buflen for better checking.
Quoted 69 lines
I had a patch that added some type-safety to
print_macand prevented this unintended buffer overrun.It seems it wasn't applied.
---------------------------------------
Subject: Re: [PATCH net-2.6.24] introduce MAC_FMT/MAC_ARG
Date: Mon, 24 Sep 2007 10:28:36 -0700Here is a patch that adds some type safety to
print_macby using a structprint_mac_buf* instead of char *.It also reduces the defconfig vmlinux size by 8 bytes.
Signed-off-by Joe Perches <[email protected]>--
include/linux/if_ether.h | 12 ++++++++++-- net/ethernet/eth.c | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-)include/linux/if_ether.h +10
-2@@ -126,7 +126,15 @@ extern struct ctl_table ether_table[];* Display a 6 byte device address (MAC) in a readable format. */ #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -extern char *print_mac (char *buf,const u8 *addr); -#define DECLARE_MAC_BUF (var) char var[18] __maybe_unused + +struct print_mac_buf { +char formatted_mac_addr[18 ]; +}; + +#define DECLARE_MAC_BUF (var) \ + struct print_mac_buf __maybe_unused _##var; \ + struct print_mac_buf __maybe_unused *var = &_##var + +extern char *print_mac (struct print_mac_buf *buf,const u8 *addr);#endif /* _LINUX_IF_ETHER_H */ net/ethernet/eth.c +3
-3@@ -338,10 +338,10 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) }EXPORT_SYMBOL (alloc_etherdev_mq); -char *print_mac (char *buf,const u8 *addr) +char *print_mac (struct print_mac_buf *buf,const u8 *addr) { -sprintf (buf,MAC_FMT , +sprintf (buf->formatted_mac_addr,MAC_FMT , addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]); -return buf; +return buf->formatted_mac_addr; }
EXPORT_SYMBOL(print_mac);
Quoted 3 lines · Michael Chan
The
dev_addris declared as unsigned char* in structnet_device. To be consistent, can we changeprint_mac()andMAC_BUFto use unsigned char*? They are really the same.
That's fine by me. I like consistency.
I don't remember why it was u8 and not unsigned char.
Quoted 3 lines
Quoted 1 line
ssize_t? shouldn't it besize_t?I'm just keeping the prototype unchanged as originally defined in net-
sysfs.c
It's painless to change the prototype.size_t seems more sensible.
Quoted 5 lines
Quoted 3 lines
Indexing buf by int len is unchecked.
That could lead to unintended buffer overruns.
Maybe add a buflen argument and use snprintf?Again, I kept the semantics the same as the original, but will be happy to add a buflen for better checking.
That sounds good.
cheers, Joe
Quoted 8 lines · Joe Perches
Quoted 3 lines · Michael Chan
Quoted 1 line
ssize_t? shouldn't it besize_t?I'm just keeping the prototype unchanged as originally defined in net-
sysfs.cIt's painless to change the prototype.
size_tseems more sensible.
I'll change the internal function to use size_t, but the exported function for sysfs use will be ssize_t since sysfs uses ssize_t.
Here's the revised patch:
[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.
drivers/scsi/qla4xxx/ql4_os.c +1 -13
@@ -173,18 +173,6 @@ static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)printk (KERN_ERR "iscsi: invalid stop flag %d\n" , flag); } -static ssize_t format_addr (char *buf,const unsigned char *addr,int len) -{ -int i; -char *cp = buf; - -for (i =0 ; i < len; i++) - cp +=sprintf (cp,"%02x%c" , addr[i], - i == (len -1 ) ?'\n' :':' ); -return cp - buf; -} - -static int qla4xxx_host_get_param (struct Scsi_Host *shost,enum iscsi_host_param param,char *buf) {
@@ -193,7 +181,7 @@ static int qla4xxx_host_get_param(struct Scsi_Host *shost,switch (param) {case ISCSI_HOST_PARAM_HWADDRESS : - len =format_addr (buf, ha->my_mac,MAC_ADDR_LEN ); + len =sysfs_format_mac (buf, ha->my_mac,MAC_ADDR_LEN );break ;case ISCSI_HOST_PARAM_IPADDRESS : len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
include/linux/if_ether.h +5 -3
@@ -124,12 +124,14 @@ int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);extern struct ctl_table ether_table[];#endif +extern ssize_t sysfs_format_mac (char *buf,const unsigned char *addr,int len); +/* * Display a 6 byte device address (MAC) in a readable format. */ -#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -extern char *print_mac (char *buf,const u8 *addr); -#define DECLARE_MAC_BUF (var) char var[18] __maybe_unused +extern char *print_mac (char *buf,const unsigned char *addr); +#define MAC_BUF_SIZE 18 +#define DECLARE_MAC_BUF (var) char var[MAC_BUF_SIZE] __maybe_unused#endif
net/core/net-sysfs.c +2 -13
@@ -95,17 +95,6 @@ NETDEVICE_SHOW(type, fmt_dec);NETDEVICE_SHOW (link_mode, fmt_dec);/* use same locking rules as GIFHWADDR ioctl's */ -static ssize_t format_addr (char *buf,const unsigned char *addr,int len) -{ -int i; -char *cp = buf; - -for (i =0 ; i < len; i++) - cp +=sprintf (cp,"%02x%c" , addr[i], - i == (len -1 ) ?'\n' :':' ); -return cp - buf; -} -static ssize_t show_address (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -114,7 +103,7 @@ static ssize_t show_address(struct device *dev, struct device_attribute *attr,read_lock (&dev_base_lock);if (dev_isalive (net)) - ret =format_addr (buf, net->dev_addr, net->addr_len); + ret =sysfs_format_mac (buf, net->dev_addr, net->addr_len);read_unlock (&dev_base_lock);return ret; }
@@ -124,7 +113,7 @@ static ssize_t show_broadcast(struct device *dev, {struct net_device *net =to_net_dev (dev);if (dev_isalive (net)) -return format_addr (buf, net->broadcast, net->addr_len); +return sysfs_format_mac (buf, net->broadcast, net->addr_len);return -EINVAL ; }
net/ethernet/eth.c +27 -3
@@ -359,10 +359,34 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count) }EXPORT_SYMBOL (alloc_etherdev_mq); -char *print_mac (char *buf,const u8 *addr) +static size_t _format_mac_addr (char *buf,int buflen, +const unsigned char *addr,int len) { -sprintf (buf,MAC_FMT , - addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]); +int i; +char *cp = buf; + +for (i =0 ; i < len; i++) { + cp +=scnprintf (cp, buflen - (cp - buf),"%02x" , addr[i]); +if (i == len -1 ) +break ; + cp +=strlcpy (cp,":" , buflen - (cp - buf)); + } +return cp - buf; +} + +ssize_t sysfs_format_mac (char *buf,const unsigned char *addr,int len) +{ +size_t l; + + l =_format_mac_addr (buf,PAGE_SIZE , addr, len); + l +=strlcpy (buf + l,"\n" ,PAGE_SIZE - l); +return ((ssize_t ) l); +} +EXPORT_SYMBOL (sysfs_format_mac); + +char *print_mac (char *buf,const unsigned char *addr) +{ +_format_mac_addr (buf,MAC_BUF_SIZE , addr,ETH_ALEN );return buf; }EXPORT_SYMBOL (print_mac);
From: "Michael Chan" <[email protected]>
Date: Sun, 23 Dec 2007 16:20:35 -0800
Quoted 14 lines
Here's the revised patch:
[ETH]: Combine
format_addr()withprint_mac().
print_mac()used many most net drivers andformat_addr()used bynet-sysfs.care 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. Bothprint_mac()andsysfs_format_mac()call_format_mac_addr()to do the formatting.
Quoted 5 lines
Changed
print_mac()to use unsigned char * to be consistent withnet_devicestruct'sdev_addr. Added buffer length overrun checking as suggested by Joe Perches.Signed-off-by Michael Chan <[email protected]>
Applied to net-2.6.25, thanks Michael.