I am trying to set the DF (don't fragment flag) for sending packets using UDP.
Looking at the Richard Steven's book Volume 1 Unix Network Programming; The Sockets Networking API, I am unable to find how to set this.
I suspect that I would do it with setsockopt() but can't find it in the table on page 193.
Please suggest how this is done.
If you are working in Userland with the intention to bypass the Kernel network stack and thus building your own packets and headers and hand them to a custom Kernel module, there is a better option than
setsockopt()
.You can actually set the DF flag just like any other field of
struct iphdr
defined inlinux/ip.h
. The 3-bit IP flags are in fact part of thefrag_off
(Fragment Offset) member of the structure.When you think about it, it makes sense to group those two things as the flags are fragmentation related. According to the RFC-791, the section describing the IP header structure states that Fragment Offset is 13-bit long and there are three 1-bit flags. The
frag_off
member is of type__be16
, which can hold 13 + 3 bits.Long story short, here's a solution:
We are here exactly setting the DF bit using the designed-for-that-particular-purpose
IP_DF
mask.IP_DF
is defined innet/ip.h
(kernel headers, of course), whereasstruct iphdr
is defined inlinux/ip.h
.I agree with the paxdiablo's answer.
where
val
is one of:ip_no_pmtu_disc
in kernel source:You do it with the
setsockopt()
call, by using theIP_DONTFRAG
option::Here's a page explaining this in further detail.
For Linux, it appears you have to use the
IP_MTU_DISCOVER
option with the valueIP_PMTUDISC_DO
(orIP_PMTUDISC_DONT
to turn it off):I haven't tested this, just looked in the header files and a bit of a web search so you'll need to test it.
As to whether there's another way the DF flag could be set:
From this excellent page here:
This looks to me like you can set the system-wide default using
sysctl
:returns
"error: "ip_no_pmtu_disc" is an unknown key"
on my system but it may be set on yours. Other than that, I'm not aware of anything else (other thansetsockopt()
as previously mentioned) that can affect the setting.