I have a requirement where I have to send the UDP packet to a destination multicast IPv6 address. In addition to that the packets must also have corresponding destination multicast MAC address set. I would like to avoid raw sockets where all the packetization (UDP, IP) has to be done in my code. Is it possible to set the MAC address using UDP socket? Is there any other option?
MAC addresses are layer-2 addresses, but UDP ports are layer-4 addresses, and they have nothing to do with each other.
MAC addresses are used for IEEE LAN protocols, but not all layer -2 protocols use MAC addresses. In fact, some IEEE LAN protocols use 48-bit MAC addresses, and some use 64-bit MAC addresses. Other layer-2 protocols use something else for addresses, and some use no addressing. MAC addresses are only relevant, or even seen, on the LAN for the source host.
The idea of layer separation and encapsulation allows leyr-2 protocols, e.g ethernet, to carry any layer-3 protocol (IPv4, IPX, IPv6, AppleTalk, etc.) without knowing or caring which layer-3 protocol. Layer-3 protocols do not care which layer-4 protocol (TCP, UDP, EIGRP, etc.) they carry. Layer-4 protocols do not care which layer-3 protocol carries them, nor do layer-3 protocols care which layer-2 protocol carries them.
For the IEEE LAN protocols that use MAC addresses, ARP will relate the layer-3 protocol address to the specific layer-2 protocol address. Multicast addresses are a special case, and each multicast 48-bit MAC address corresponds to 32 multicast layer-3 IPv4 addresses. The multicast 48-bit MAC address uses the
01-00-5E OUI appended with the last 23 bits of the IPv4 multicast address, or
33-33 appended with the last 32 bits (in certain circumstances) of the IPv6 multicast address.
This is something you do not want to do yourself, just let the network stack handle it for you.