Blame view

lib/net_utils.c 640 Bytes
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
4cd5773a2   Andy Shevchenko   net: core: move m...
2
3
4
5
  #include <linux/string.h>
  #include <linux/if_ether.h>
  #include <linux/ctype.h>
  #include <linux/kernel.h>
a69f5edb8   Joe Perches   mac_pton: Use boo...
6
  bool mac_pton(const char *s, u8 *mac)
4cd5773a2   Andy Shevchenko   net: core: move m...
7
8
9
10
11
  {
  	int i;
  
  	/* XX:XX:XX:XX:XX:XX */
  	if (strlen(s) < 3 * ETH_ALEN - 1)
a69f5edb8   Joe Perches   mac_pton: Use boo...
12
  		return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
13
14
15
16
  
  	/* 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...
17
  			return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
18
  		if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
a69f5edb8   Joe Perches   mac_pton: Use boo...
19
  			return false;
4cd5773a2   Andy Shevchenko   net: core: move m...
20
21
22
23
  	}
  	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...
24
  	return true;
4cd5773a2   Andy Shevchenko   net: core: move m...
25
26
  }
  EXPORT_SYMBOL(mac_pton);