This is my attempt at making sense of this after reading the other two answers here.
If you're wondering about when to use the MSG_CONFIRM
flag, essentially, all it does is tell the underlying ARP network layer to NOT periodically verify the MAC of the recipient IP (which would update the ARP hardware-MAC-address-to-IP-address mapping), because we are confident the IP we are sending to is the device we think it is, since this message are are sending is in direct response to a message we just received from them! In other words, if in doubt, leave OUT the MSG_CONFIRM
flag. Put it in ONLY if the message we are sending is a direct response to a message received, and therefore we want to increase the network efficiency a tiny bit by NOT verifying the MAC again periodically, at the risk that the destination MAC could change and be wrong and no loner match the IP address for the device we think we are sending this message to!
Pros of using MSG_CONFIRM
:
- It increases the efficiency of the network by NOT having ARP reprobe for the MAC of the destination address.
Cons or risks of using MSG_CONFIRM
:
- It may mean that if the old device on the network drops off, and a new destination device pops up on the network with the same destination address as the old one, but with a different hardware MAC, we won't know, because
MSG_CONFIRM
tells ARP not to reprobe that IP address's MAC.
When MSG_CONFIRM
can be used:
- When our message out is a direct reply to a message we just received in from that device, so we are sure its MAC is still correct.
MSG_CONFIRM
in this case can be thought of as a "confirmation" message to the sender that we got their previous message.
Official documentation from here for sendto()
: https://linux.die.net/man/2/sendto (emphasis added):
MSG_CONFIRM
(Since Linux 2.3.15)
Tell the link layer that forward progress happened: you got a successful reply from the other side. If the link layer doesn't get this it will regularly reprobe the neighbor (e.g., via a unicast ARP). Only valid on SOCK_DGRAM
and SOCK_RAW
sockets and currently only implemented for IPv4
and IPv6
. See arp(7) for details.
So, my rule of thumb is: just don't use it. It has little benefit. But, if you choose to use it, only use it
- To reply (via a UDP message) directly to the sender of a UDP message just received via
recvfrom()
, or equivalent, or
- In embedded device networks where you have fixed (static) MAC addresses, IP addresses, and ARP MAC-to-IP mapping, and carefully manually control it all, and care about the tiny bit of network efficiency gain achieved by disabling ARP probing.