{"id":1199,"date":"2012-01-29T13:43:09","date_gmt":"2012-01-29T12:43:09","guid":{"rendered":"http:\/\/mccltd.net\/blog\/?p=1199"},"modified":"2015-02-21T09:18:00","modified_gmt":"2015-02-21T09:18:00","slug":"using-tcpdump-to-filter-on-dscp","status":"publish","type":"post","link":"http:\/\/darenmatthews.com\/blog\/?p=1199","title":{"rendered":"Using TCPDUMP to Filter on DSCP"},"content":{"rendered":"<p>The TCPDUMP filter to match a DSCP is a little tricky to construct.  I need to explain  how the filter works and also what TCPDUMP sees in the ToS Byte of the IP header and then how to filter on just the DiffServ extensions.<\/p>\n<div id=\"attachment_759\" style=\"width: 471px\" class=\"wp-caption aligncenter\"><a rel=\"attachment wp-att-759\" href=\"http:\/\/darenmatthews.com\/blog\/?attachment_id=759\"><img aria-describedby=\"caption-attachment-759\" loading=\"lazy\" class=\"size-full wp-image-759 \" title=\"The IP ToS Byte - DSCP and IP ECN\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2010\/04\/The-IP-ToS-Byte-DSCP-and-IP-ECN.jpg\" alt=\"The IP ToS Byte (DSCP and IP ECN)\" width=\"461\" height=\"139\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2010\/04\/The-IP-ToS-Byte-DSCP-and-IP-ECN.jpg 576w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2010\/04\/The-IP-ToS-Byte-DSCP-and-IP-ECN-300x90.jpg 300w\" sizes=\"(max-width: 461px) 100vw, 461px\" \/><\/a><p id=\"caption-attachment-759\" class=\"wp-caption-text\">The IP ToS Byte (DSCP and IP ECN)<\/p><\/div>\n<p><strong>FIRST THE ANSWER:<\/strong><br \/>\n(in case it is not important to understand how the filter is constructed)<\/p>\n<p>&#8211; <strong>an example filtering on DSCP decimal 20 (AF22):<\/strong><br \/>\ntcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 20) -vvv<\/p>\n<p>&#8211; <strong>An example filtering on DSCP Hex 14 (also decimal 20 or AF22):<\/strong><br \/>\ntcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 0x14) \u2013vvv<br \/>\n<!--more--><\/p>\n<p>When traffic is marked with a DSCP (6 bits) , TCPDUMP  will interpret all 8 bits used by the ToS Byte in the IP header.<\/p>\n<p><strong>1.       EXAMPLE CAPTURE <\/strong><\/p>\n<p>I set my LAN to mark traffic with DSCP 20.  Notice that  traffic captured showed the second byte ( or &#8220;byte offset 1&#8221; as we count from 0) of the IP header, which is set to  0x50 (hex 50).\u00a0 (This is the value of the whole ToS Byte).<\/p>\n<p>Why 0x50 and not 0x14, which is the Hex equivalent of DSCP 20 (AF22)?<br \/>\nThis is because DSCP 20 (six bits = 010100) = Hex 14 but using all 8  bits of the second byte (DS field of IP header) the value is 0x50<\/p>\n<p>6 bits = 010100 = Decimal 20 = Hex 14<br \/>\n8 bits = 01010000 = Decimal 80 = Hex 50<\/p>\n<p><code>Linux # <strong>tcpdump -i eth0 -vvv (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 20)<\/strong><br \/>\ntcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 300 bytes<br \/>\n11:00:20.363070 IP (<span style=\"text-decoration: underline;\"><strong>tos 0x50<\/strong><\/span>, ttl 128, id 7903, offset 0, flags [DF], proto TCP (6), length 68)<br \/>\n10.63.64.165.57018 &gt; 79.73.127.147.http: Flags [S], cksum 0x43c7 (correct), seq 1610776870, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK,Unknown Option 7601010a3f40f60005,Unknown Option 760c01,nop,eol], length 0<br \/>\n11:00:20.364257 IP (<span style=\"text-decoration: underline;\"><strong>tos 0x50<\/strong><\/span>, ttl 126, id 7903, offset 0, flags [DF], proto TCP (6), length 60)<br \/>\n79.73.127.147.http &gt; 10.63.64.165.57018: Flags [S.], cksum 0x7ca9 (correct), seq 20020520, ack 1610776871, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK,Unknown Option 760c01,nop,nop,nop,eol], length 0<\/code><\/p>\n<p>2 packets captured<br \/>\n2 packets received by filter<br \/>\n0 packets dropped by kernel<br \/>\nLinux #<\/p>\n<p><strong>2.       TCPDUMP FILTER: <\/strong><\/p>\n<p>There are two ways to filter.  You can match the hex value of the  entire 8 bits of the DS field, so you would need to calculate the 8-bit  hex value of a six bit DSCP, like this:<\/p>\n<p>Steelhead # tcpdump -i eth0 (ip and ip[1] &amp; 0xfc == 0x50) \u2013vvv<\/p>\n<p>Or you can \u201cright-shift\u201d to \u2018eliminate\u2019 the two extraneous bits (the  ECN) and use the correct hex value (or decimal) of the DSCP:<br \/>\nLinux # tcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 20) -vvv<br \/>\nOr<br \/>\nLinux # tcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 0x14) \u2013vvv<\/p>\n<p>What this does is this:<br \/>\n&#8211;          ip[1] refers to the Head byte 1 (second byte &#8220;BYTE OFFSET 1&#8221;, the TOS byte) of the IP header<br \/>\n&#8211;          &amp;    means a Bitwise &#8220;AND&#8221;<br \/>\n&#8211;          0xfc =  11111100 is the mask to use for the AND<\/p>\n<p><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/IP-Header.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1206\" title=\"IP-Header\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/IP-Header.jpg\" alt=\"IP-Header\" width=\"853\" height=\"557\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/IP-Header.jpg 853w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/IP-Header-300x195.jpg 300w\" sizes=\"(max-width: 853px) 100vw, 853px\" \/><\/a><\/p>\n<p>The \u201c&gt;&gt;\u201d means \u201cright shift\u201d (or move bits the specified  number of places to the RIGHT), which is EQUIVALENT TO a divide by 2 the  specified number of times, OR EQUIVALENT TO a divide by (2 to the power  of the specified number). Examples of right shifting:<\/p>\n<p>Original:<br \/>\n<code>01010000 (0x50 \/ 80)<br \/>\n&gt;&gt; 0 (divide by 1)<br \/>\n01010000 (0x50 \/ 80)<br \/>\n&gt;&gt; 1 (divide by 2)<br \/>\n00101000 (0x28 \/ 40)<br \/>\n&gt;&gt; 2 (divide by 2 then by 2, or divide by 4)<br \/>\n00010100 (0x14 \/ 20)<br \/>\n&gt;&gt; 3 (divide by 2 three times, or divide by 8)<br \/>\n00001010 (0x0a \/ 10)<\/code><\/p>\n<p>This is an example of the logical AND:  (i.e. 0 and 0 = 0, 1 and 0 = 0, 1 and 1 = 1)<\/p>\n<p>01010000 original value (0x50 using all 8 bits)<br \/>\n11111100 bitwise mask (0xfc)<br \/>\n01010000 result of bitwise AND<\/p>\n<p>So:<br \/>\n&gt;&gt; 0 would be 01010000 or 0x50<br \/>\n&gt;&gt; 2 would be 010100 or 0x20<\/p>\n<p><strong>In Summary:<\/strong><br \/>\nUsing the right-shift would enable you to use the correct DSCP value in the TCPDUMP filter:<br \/>\nLinux # tcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 20) -vvv<br \/>\nOr<br \/>\nLinux # tcpdump -i eth0 (ip and (ip[1] &amp; 0xfc) &gt;&gt; 2 == 0x14) \u2013vvv<\/p>\n<p>NOT using the right-shift means that you need to convert the 6-bit DSCP to an 8-bit value and use this in your TCPDUMP filter:<br \/>\nLinux # tcpdump -i eth0 (ip and ip[1] &amp; 0xfc == 0x50) \u2013vvv<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The TCPDUMP filter to match a DSCP is a little tricky to construct. I need to explain how the filter works and also what TCPDUMP sees in the ToS Byte of the IP header and then how to filter on just the DiffServ extensions. FIRST THE ANSWER: (in case it is not important to understand [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[53,76],"tags":[21,51,37],"_links":{"self":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1199"}],"collection":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1199"}],"version-history":[{"count":52,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1604,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1199\/revisions\/1604"}],"wp:attachment":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}