Skip to content

Commit 4940fc8

Browse files
Alexey Dobriyandavem330
authored andcommitted
net: add mac_pton() for parsing MAC address
mac_pton() parses MAC address in form XX:XX:XX:XX:XX:XX and only in that form. mac_pton() doesn't dirty result until it's sure string representation is valid. mac_pton() doesn't care about characters _after_ last octet, it's up to caller to deal with it. mac_pton() diverges from 0/-E return value convention. Target usage: if (!mac_pton(str, whatever->mac)) return -EINVAL; /* ->mac being u8 [ETH_ALEN] is filled at this point. */ /* optionally check str[3 * ETH_ALEN - 1] for termination */ Use mac_pton() in pktgen and netconsole for start. Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 99f823f commit 4940fc8

5 files changed

Lines changed: 38 additions & 86 deletions

File tree

‎drivers/net/netconsole.c‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,6 @@ static ssize_t store_remote_mac(struct netconsole_target *nt,
429429
size_t count)
430430
{
431431
u8 remote_mac[ETH_ALEN];
432-
char *p = (char *) buf;
433-
int i;
434432

435433
if (nt->enabled) {
436434
printk(KERN_ERR "netconsole: target (%s) is enabled, "
@@ -439,23 +437,13 @@ static ssize_t store_remote_mac(struct netconsole_target *nt,
439437
return -EINVAL;
440438
}
441439

442-
for (i = 0; i < ETH_ALEN - 1; i++) {
443-
remote_mac[i] = simple_strtoul(p, &p, 16);
444-
if (*p != ':')
445-
goto invalid;
446-
p++;
447-
}
448-
remote_mac[ETH_ALEN - 1] = simple_strtoul(p, &p, 16);
449-
if (*p && (*p != '\n'))
450-
goto invalid;
451-
440+
if (!mac_pton(buf, remote_mac))
441+
return -EINVAL;
442+
if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
443+
return -EINVAL;
452444
memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
453445

454446
return strnlen(buf, count);
455-
456-
invalid:
457-
printk(KERN_ERR "netconsole: invalid input\n");
458-
return -EINVAL;
459447
}
460448

461449
/*

‎include/linux/if_ether.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);
136136
extern struct ctl_table ether_table[];
137137
#endif
138138

139+
int mac_pton(const char *s, u8 *mac);
139140
extern ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len);
140141

141142
#endif

‎net/core/netpoll.c‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -698,32 +698,8 @@ int netpoll_parse_options(struct netpoll *np, char *opt)
698698

699699
if (*cur != 0) {
700700
/* MAC address */
701-
if ((delim = strchr(cur, ':')) == NULL)
701+
if (!mac_pton(cur, np->remote_mac))
702702
goto parse_failed;
703-
*delim = 0;
704-
np->remote_mac[0] = simple_strtol(cur, NULL, 16);
705-
cur = delim + 1;
706-
if ((delim = strchr(cur, ':')) == NULL)
707-
goto parse_failed;
708-
*delim = 0;
709-
np->remote_mac[1] = simple_strtol(cur, NULL, 16);
710-
cur = delim + 1;
711-
if ((delim = strchr(cur, ':')) == NULL)
712-
goto parse_failed;
713-
*delim = 0;
714-
np->remote_mac[2] = simple_strtol(cur, NULL, 16);
715-
cur = delim + 1;
716-
if ((delim = strchr(cur, ':')) == NULL)
717-
goto parse_failed;
718-
*delim = 0;
719-
np->remote_mac[3] = simple_strtol(cur, NULL, 16);
720-
cur = delim + 1;
721-
if ((delim = strchr(cur, ':')) == NULL)
722-
goto parse_failed;
723-
*delim = 0;
724-
np->remote_mac[4] = simple_strtol(cur, NULL, 16);
725-
cur = delim + 1;
726-
np->remote_mac[5] = simple_strtol(cur, NULL, 16);
727703
}
728704

729705
netpoll_print_options(np);

‎net/core/pktgen.c‎

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,74 +1420,37 @@ static ssize_t pktgen_if_write(struct file *file,
14201420
return count;
14211421
}
14221422
if (!strcmp(name, "dst_mac")) {
1423-
char *v = valstr;
1424-
unsigned char old_dmac[ETH_ALEN];
1425-
unsigned char *m = pkt_dev->dst_mac;
1426-
memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1427-
14281423
len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
14291424
if (len < 0)
14301425
return len;
14311426

14321427
memset(valstr, 0, sizeof(valstr));
14331428
if (copy_from_user(valstr, &user_buffer[i], len))
14341429
return -EFAULT;
1435-
i += len;
1436-
1437-
for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1438-
int value;
1439-
1440-
value = hex_to_bin(*v);
1441-
if (value >= 0)
1442-
*m = *m * 16 + value;
1443-
1444-
if (*v == ':') {
1445-
m++;
1446-
*m = 0;
1447-
}
1448-
}
14491430

1431+
if (!mac_pton(valstr, pkt_dev->dst_mac))
1432+
return -EINVAL;
14501433
/* Set up Dest MAC */
1451-
if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1452-
memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1434+
memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
14531435

1454-
sprintf(pg_result, "OK: dstmac");
1436+
sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14551437
return count;
14561438
}
14571439
if (!strcmp(name, "src_mac")) {
1458-
char *v = valstr;
1459-
unsigned char old_smac[ETH_ALEN];
1460-
unsigned char *m = pkt_dev->src_mac;
1461-
1462-
memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1463-
14641440
len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
14651441
if (len < 0)
14661442
return len;
14671443

14681444
memset(valstr, 0, sizeof(valstr));
14691445
if (copy_from_user(valstr, &user_buffer[i], len))
14701446
return -EFAULT;
1471-
i += len;
1472-
1473-
for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1474-
int value;
1475-
1476-
value = hex_to_bin(*v);
1477-
if (value >= 0)
1478-
*m = *m * 16 + value;
1479-
1480-
if (*v == ':') {
1481-
m++;
1482-
*m = 0;
1483-
}
1484-
}
14851447

1448+
if (!mac_pton(valstr, pkt_dev->src_mac))
1449+
return -EINVAL;
14861450
/* Set up Src MAC */
1487-
if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1488-
memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1451+
memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
14891452

1490-
sprintf(pg_result, "OK: srcmac");
1453+
sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14911454
return count;
14921455
}
14931456

‎net/core/utils.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,27 @@ void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
296296
csum_unfold(*sum)));
297297
}
298298
EXPORT_SYMBOL(inet_proto_csum_replace4);
299+
300+
int mac_pton(const char *s, u8 *mac)
301+
{
302+
int i;
303+
304+
/* XX:XX:XX:XX:XX:XX */
305+
if (strlen(s) < 3 * ETH_ALEN - 1)
306+
return 0;
307+
308+
/* Don't dirty result unless string is valid MAC. */
309+
for (i = 0; i < ETH_ALEN; i++) {
310+
if (!strchr("0123456789abcdefABCDEF", s[i * 3]))
311+
return 0;
312+
if (!strchr("0123456789abcdefABCDEF", s[i * 3 + 1]))
313+
return 0;
314+
if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
315+
return 0;
316+
}
317+
for (i = 0; i < ETH_ALEN; i++) {
318+
mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
319+
}
320+
return 1;
321+
}
322+
EXPORT_SYMBOL(mac_pton);

0 commit comments

Comments
 (0)