3

If I am using raw sockets to send a UDP packet of size 3000bytes, do I need to handle packet fragmentation myself in the code, or should the raw socket handle fragmentation similar to DGRAM socket?

SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61
  • 1
    If I am not mistaken, and I doubt I am with this one, you do have to worry about lost packets and so forth, UDP is a best efforts protocol. – thecoshman Nov 15 '10 at 19:10
  • The packets (if recv'd) are fine - the order, however, is not guaranteed to be retained. Obviously, if you're send/resp/send/resp, this is not an issue, but will be if you're streaming. The issue with UDP is that once you've implemented all the housekeeping, you just wish you had done TCP in the first place. Typically, this isn't the case when you're not concerned about packets being missed. – KevinDTimm Nov 15 '10 at 19:13
  • Can you clarify what exactly you mean by raw in this instance? Are you writing raw ethernet frames, raw IP packets? What system are you doing this on? Some systems give some kind of "half-n-half" socket... – Flexo Nov 15 '10 at 19:33
  • I mean send IP packets through IP_RAW sockets. – SkypeMeSM Nov 15 '10 at 19:48
  • Follow-up question is here http://stackoverflow.com/questions/4191253/udp-packet-fragmentation-for-raw-sockets. – SkypeMeSM Nov 16 '10 at 04:32

6 Answers6

5

Well, if you are using UDP, you aren't really sending RAW. RAW would be no IP at all, in which case yes you have to handle fragmentation yourself.

With UDP you get IP's fragmentation support, which is IMHO plenty good enough for short-haul networks where collisions should be minimal. Make the link between the two systems a dedicated subnet, and it isn't an issue at all.

What TCP buys you over UDP (among other things) is the stack's ability to just have to resend one fragment if it gets lost or hosed somehow. With UDP if that happens the entire message must be discarded. There's overhead with that though, and for most modern networks you can probably live with that trade-off.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • Agree with the point about the other answers missing the "rawness". Disagree with your conclusion though, some systems have "raw IP" sockets, where you still specify the source address and destination address of the ip header, but write the rest of the packet yourself. In that case there would be an IP header (half constructed by the application and half managed by the lower level) and it would have a DF bit as normal. – Flexo Nov 15 '10 at 19:22
  • @awoodland - Interesting. My experience is mostly with writing my own stacks. If the user is handling filling out the IP packet, I'd think they also would have to handle assembling incomming IP fragments into messages. – T.E.D. Nov 15 '10 at 19:28
  • I thought the question was about transmission only since it asked about sending! The problem is it's not really very clear what's meant by "raw" in this instance. – Flexo Nov 15 '10 at 19:31
  • I have added the follow-up question regarding how to handle fragmentation at http://stackoverflow.com/questions/4191253/udp-packet-fragmentation-for-raw-sockets. Thank you for your answer. – SkypeMeSM Nov 16 '10 at 04:33
3

Nope, packet fragmentation is handled at a lower level. You should see exactly what you put in the packet come back out. That is to say UDP guaranties message boundaries.

stonemetal
  • 6,111
  • 23
  • 25
2

The underlying protocol, IP, still handles fragmentation. As long as you're not setting the DF (don't fragment) bit you should be fine, I think.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Depending on your system this can be handled quite differently. For example on Linux you can ask the lower layers to handle path MTU discovery and give an error (EMSGSIZE) if you try and send something larger than the (known) path MTU.

How "raw" is the raw socket you're talking about? Other systems could just let you control the DF bit (or you might be constructing most of the IP header yourself) in which case the behaviour will also depend on this.

As a rule if you transmit with DF set you will usually get a choice of seeing an error in userspace, or having the lower levels on your host handle PMTU discovery and stopping you sending something too large. If you don't set DF then you (probably) will see appropriate fragmentation from router(s) along the path.

Flexo
  • 87,323
  • 22
  • 191
  • 272
0

For Linux, the answer is yes. If you take a look at Linux's raw socket implementation, there is no reassembly happening for raw sockets.

yeshengm
  • 557
  • 5
  • 13
0

Yes, SOCK_RAW dont handle Fragmentation and reassembly. RAW socket treated your complete data buffer is treated as L2 protocol's data/payload, and it doesn't know about L3 and above.

In case of SOCK_DGRAM or SOCK_STREAM, your data buffer will be treated as data/payload to L4 protocols. so L3 handling(frag/reass) will done by linux stack.