Saturday, August 31, 2013

Obtain MAC Address of Ethernet NIC in Linux

Linux manual page netdevice(7) has good description on ioctl's that one may use to control or query Ethernet NIC.

The following sample program returns the Ethernet adapter's MAC address from its interface name.

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>         /* declares IFNAMSIZ */
#include <net/if_arp.h>     /* declares types of link, such as, APRHRD_ETHER */
#include <net/ethernet.h>   /* declares ETH_ALEN */
#include <netinet/ip.h>     /* declares IPPROTO_IP */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int sockfd, i, addrlen;
    struct ifreq ifr;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s IFNAME\n", argv[0]);
        exit(1);
    }

    /* many network sockets would do, here are 3 examples. */
    /* sockfd = socket(AF_PACKET, SOCK_RAW, 0); */
    /* sockfd = socket(AF_UNIX, SOCK_DGRAM, 0); */
    sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sockfd == -1) {
        fprintf(stderr, "Error: calling socket(AF_PACKET, SOCK_DGRAM, 0): %s\n",
            strerror(errno));
        exit(1);
    }

    ifr.ifr_name[IFNAMSIZ-1] = '\0';
    strncpy(ifr.ifr_name, argv[1], IFNAMSIZ-1);

    if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
        fprintf(stderr, "Error: calling ioctl(sockfd, SIOCGIFHWADDR, ...): %s\n",
            strerror(errno));
        exit(1);
    }

    switch(ifr.ifr_hwaddr.sa_family) {
        case ARPHRD_ETHER:
            addrlen = ETH_ALEN;
            break;
        default:
            fprintf(stderr, "Warn: not Ethernet, give up ...\n");
            exit(1);
    }

    for (i=0; i<addrlen-1; i++)
        printf("%02x:", (unsigned char)ifr.ifr_hwaddr.sa_data[i]);
    printf("%02x\n", (unsigned char)ifr.ifr_hwaddr.sa_data[addrlen-1]);

    close(sockfd);
    return 0;
}

Other Devices/Unknown Devices When Installing Windows 8 on HP Probook 6550b

I found three unknown devices when I installed Windows 8 on a HP Probook 6550b. The three unknown devices are actually,

  • JMicron Media Card Reader Drive
       Hardware Ids
         PCI\VEN_197B&DEV_2392&SUBSYS_1619103C&REV_30
         PCI\VEN_197B&DEV_2392&SUBSYS_1619103C
         PCI\VEN_197B&DEV_2392&CC_088000
         PCI\VEN_197B&DEV_2392&CC_0880
       Hardware Ids
         USB\VID_138A&PID_003C&REV_0086
         USB\VID_138A&PID_003C

  • HP 3D DriveGuard
       Hardware Ids
         ACPI\VEN_HPQ&DEV_0004
         ACPI\HPQ0004
         *HPQ0004

  • Validity Fingerprint Sensor (VFS471)
        Hardware Ids
          USB\VID_138A&PID_003C&REV_0086
          USB\VID_138A&PID_003C)


The problem was resolved by downloading and installing three drivers, respectively.

Note that at the time I was installing Windows 8 on the laptop, Windows 8 driver for Validity Fingerprint Sensor was not available on HP's website. I tried the Windows 7 driver and it worked just fine.

After the drivers were installed, you would find the devices listed as follows in Windows Device Manager,
  • JMicro Media Card Reader Driver as "Memory technology devices\JMicron PCIe SD Host Controller" and "Memory technology devices\JMicron PCIe SD/MMC Host Controller"
  • HP 3D DriveGuard as "System devices\HP Mobile Data Protection Sensor"
  • Validity Finterprint Sensor as "Biometric devices\Validity Sensor"

For convenience you may download the drivers for different versions of Windows below: