{"id":15,"date":"2021-09-16T05:10:46","date_gmt":"2021-09-16T05:10:46","guid":{"rendered":"http:\/\/6142d14ab8f9b5001713290e"},"modified":"2021-11-09T12:25:39","modified_gmt":"2021-11-09T12:25:39","slug":"sip-fragmentation-on-udp","status":"publish","type":"post","link":"https:\/\/jazchye.com\/index.php\/2021\/09\/16\/sip-fragmentation-on-udp\/","title":{"rendered":"SIP Fragmentation on UDP"},"content":{"rendered":"<p><strong>What is SIP Fragmentation?<\/strong><\/p>\n<p>Every link on the internet has a Maximum Transfer Unit (MTU) size which determines the maximum size of a packet that can traverse the link, in bytes on Layer 2. For Ethernet, this is usually1500 bytes. One packet of data can be transmitted across a standard Ethernet network that is smaller or equal to 1500 bytes.<\/p>\n<p>What if I need to send more data? Internet Protocol has a fragmentation standard that everyone has to follow so that the messages will fit inside the MTU.<\/p>\n<p>For example, with an MTU of 1500 bytes, a single 2500 byte SIP message can fit in two frames, or IP datagrams: one fragment may have 1500 bytes, and the remaining 1000 bytes (plus some bytes for headers) will be in the second fragment.<\/p>\n<p>The small fragments will be travel as separate IP datagram packets after being fragmented from the large message. These messages can be lost. Since IP will have no way to detect nor recover the packet, these messages will simply discard all the other fragments which managed to reach the receiver as well. This is how UDP works.<\/p>\n<p>TCP does not use IP fragmentation, but rather, segmentation, in which the missing segment will be re-transmit if it is lost.<\/p>\n<p>SIP protocol utilizes UDP, and that is why SIP will often face challenges for fragmentation.<\/p>\n<p>Let us look at one example here &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-43\" src=\"http:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_1.jpg\" alt=\"\" width=\"1214\" height=\"512\" srcset=\"https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_1.jpg 1214w, https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_1-300x127.jpg 300w, https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_1-1024x432.jpg 1024w, https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_1-768x324.jpg 768w\" sizes=\"auto, (max-width: 1214px) 100vw, 1214px\" \/><\/p>\n<figure><\/figure>\n<p>We can see that this is a pretty standard SIP INVITE message from extension 1020 to 1002. But why do we get an error of 401 Unauthorized?<\/p>\n<p>If we quickly expand the Message Header for this SIP INVITE, you will be seeing tons of headers. Having tons of headers is not good in SIP, especially when it <em>MIGHT <\/em>cause fragmentation.<\/p>\n<p>Well, just by looking at the headers under message headers, there is too much data, and after looking at the length of this message (1373 bytes), we can deduct that this is an issue with fragmentation.<\/p>\n<p>According to Section18.1.1 in <a href=\"https:\/\/tools.ietf.org\/html\/rfc3261#section-18.1\">RFC3261<\/a> &#8211;<\/p>\n<pre><code>If a request is within 200 bytes of the path MTU, or if it is larger\n   than 1300 bytes and the path MTU is unknown, the request MUST be sent\n   using an RFC 2914 [43] congestion controlled transport protocol, such\n   as TCP. If this causes a change in the transport protocol from the\n   one indicated in the top Via, the value in the top Via MUST be\n   changed.  This prevents fragmentation of messages over UDP and\n   provides congestion control for larger messages.  However,\n   implementations MUST be able to handle messages up to the maximum\n   datagram packet size.  For UDP, this size is 65,535 bytes, including\n   IP and UDP headers.\n\n      The 200 byte \"buffer\" between the message size and the MTU\n      accommodates the fact that the response in SIP can be larger than\n      the request.  This happens due to the addition of Record-Route\n      header field values to the responses to INVITE, for example.  With\n      the extra buffer, the response can be about 170 bytes larger than\n      the request, and still not be fragmented on IPv4 (about 30 bytes\n\n\n\nRosenberg, et. al.          Standards Track                   [Page 142]\n<\/code><\/pre>\n<pre><code> \nRFC 3261            SIP: Session Initiation Protocol           June 2002\n\n\n      is consumed by IP\/UDP, assuming no IPSec).  1300 is chosen when\n      path MTU is not known, based on the assumption of a 1500 byte\n      Ethernet MTU.\n<\/code><\/pre>\n<p>In short, the maximum length for SIP UDP messages should be a maximum of 1300 bytes; otherwise, it has to be sent in TCP. In real life, most endpoints do support TCP, but practically, many don\u2019t, or don\u2019t have it enabled, or it isn\u2019t reachable. So TCP is not a viable option here.<\/p>\n<p><strong>What can we do here?<\/strong><\/p>\n<p>We can simply disable all the custom SIP headers from the client to reduce the length of the message so that it can be lower than 1300 bytes and does not undergo any fragmentation and everything will be good after!<\/p>\n<figure><\/figure>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-44\" src=\"http:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_2.jpg\" alt=\"\" width=\"896\" height=\"516\" srcset=\"https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_2.jpg 896w, https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_2-300x173.jpg 300w, https:\/\/jazchye.com\/wp-content\/uploads\/2021\/10\/SIPFrag_2-768x442.jpg 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is SIP Fragmentation? Every link on the internet has a Maximum Transfer Unit (MTU) size which determines the maximum size of a packet that&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,9],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-ip-phones","category-sip"],"_links":{"self":[{"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":2,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jazchye.com\/index.php\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}