Blame view

lib/net_utils.c 604 Bytes
4cd5773a2   Andy Shevchenko   net: core: move m...
1
2
3
4
  #include <linux/string.h>
  #include <linux/if_ether.h>
  #include <linux/ctype.h>
  #include <linux/kernel.h>
a69f5edb8   Joe Perches   mac_pton: Use boo...
5
  bool mac_pton(const char *s, u8 *mac)
4cd5773a2   Andy Shevchenko   net: core: move m...
6
7
8
9
10
  {
  	int i;
  
  	/* XX:XX:XX:XX:XX:XX */
  	if (strlen(s) < 3 * ETH_ALEN - 1)
a69f5edb8   Joe Perches   mac_pton: Use boo...
11
  		return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
12
13
14
15
  
  	/* Don't dirty result unless string is valid MAC. */
  	for (i = 0; i < ETH_ALEN; i++) {
  		if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))
a69f5edb8   Joe Perches   mac_pton: Use boo...
16
  			return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
17
  		if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
a69f5edb8   Joe Perches   mac_pton: Use boo...
18
  			return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
19
20
21
22
  	}
  	for (i = 0; i < ETH_ALEN; i++) {
  		mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
  	}
a69f5edb8   Joe Perches   mac_pton: Use boo...
23
  	return true;
4cd5773a2   Andy Shevchenko   net: core: move m...
24
25
  }
  EXPORT_SYMBOL(mac_pton);