Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/string.h>
0003 #include <linux/if_ether.h>
0004 #include <linux/ctype.h>
0005 #include <linux/kernel.h>
0006 
0007 bool mac_pton(const char *s, u8 *mac)
0008 {
0009     int i;
0010 
0011     /* XX:XX:XX:XX:XX:XX */
0012     if (strlen(s) < 3 * ETH_ALEN - 1)
0013         return false;
0014 
0015     /* Don't dirty result unless string is valid MAC. */
0016     for (i = 0; i < ETH_ALEN; i++) {
0017         if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))
0018             return false;
0019         if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
0020             return false;
0021     }
0022     for (i = 0; i < ETH_ALEN; i++) {
0023         mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
0024     }
0025     return true;
0026 }
0027 EXPORT_SYMBOL(mac_pton);