Search code examples
iosnetwork-programmingipv6tvosmac-address

How to get a remote MAC address via IPv6 in iOS programmatically


I need to find a solution how to get the MAC address from the other device in the WiFi network. There is a good method how to do this for IPv4 (How does iOS app Fing get MAC Address?), but how to do this for IPv6? Since ARP was replaced by the NDP (Neighbour Discovery Protocol), the latter method doesn't work. I would greatly appreciate if anyone could help me.


Solution

  • Layer and Encapsulation

    The network architecture is layered, the upper layer encapsulate the different implementations of lower layer and provide higher abstraction relative to lower layer. The network layer which use IP encapsulate different link layers protocols, like Ethernet, WiFi, PPP (which may run on serial cable which not use MAC address) etc.

    • So, the first problem is what do you mean by remote?

    If you mean other hosts in WAN, it is impossible unless both device implement a specific protocol: you send a request to those device, they reply with his mac address.

    If you mean other host in the same LAN, you can use ARP protocol in IPv4 and NDP (which ) in IPv6.

    Arp & NDP

    ARP send broadcast in LAN when it knows the IP address of a host but not MAC address, then the hosts which find someone is calling for him reply its MAC address.

    NDP provides two main part of functionality, the first is the same with ARP: mapping between network- and link-layer addresses. (The difference is NDP using a multicast address: prefix f02::1:f/104, combined with the low-order 24 bits of the solicited IPv6 address)

    So what you need is to send ICMPv6 Neighbor Solicitation message.

    Address Assignment in IPv6

    Link-local IPv6 addresses (and some global IPv6 addresses) use interface identifiers (IIDs) as a basis for unicast IPv6 address assignment. ... IIDs are ordinarily 64 bits long and are formed either directly from the underlyinglink-layer MAC address of a network interface using a modified EUI-64 format. or by another process that randomizes the value in hopes of providing some degree of privacy against address tracking.

    So in the most common cases, you can get the MAC address of a device directly from their IPv6 link local address.

    Conclusion:

    • implement your protocol in both devices
    • send NDP message to solicited node if it is in same LAN
    • extract MAC address from link local IPv6 address

    Ref

    • Wikipedia
    • TCP/IP Illustrated, volume 1