{"id":1810,"date":"2014-01-28T15:42:10","date_gmt":"2014-01-28T15:42:10","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=1810"},"modified":"2019-12-25T19:31:53","modified_gmt":"2019-12-25T19:31:53","slug":"ethernet-internals","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=1810","title":{"rendered":"Ethernet Internals"},"content":{"rendered":"<h3>MAC Address<\/h3>\n<p><a href=\"http:\/\/serverfault.com\/questions\/40712\/what-range-of-mac-addresses-can-i-safely-use-for-my-virtual-machines\">What range of MAC addresses can I safely use for my virtual machines?<\/a><br \/>\n<a href=\"http:\/\/packetsdropped.wordpress.com\/2011\/01\/13\/mac-address-universally-or-locally-administered-bit-and-individualgroup-bit\/\">MAC Address: Universally or Locally Administered Bit and Individual\/Group Bit<\/a><\/p>\n<h3>Frame Check Sequence (FCS) = Cyclic Redundancy Check (CRC)<\/h3>\n<p><a href=\"http:\/\/blog.bachi.net\/?p=10512\">Cyclic Redundancy Check (CRC), Frame Check Sequence (FCS)<\/a><\/p>\n<h3>UDP Checksum<\/h3>\n<h4>Short UDP Checksum Calculation HowTo<\/h4>\n<p>Following is a description by Ed Beroset of the calculation of the UDP checksum for this packet. Many thanks to Ed for figuring out the details and writing it up.<\/p>\n<p>First, the checksum calculation is defined in <strong>RFC 768<\/strong> but hints as to how to calculate it efficiently are in <strong>RFC 1071<\/strong>. Both are worth reading and contain a much more in-depth description that I&#8217;m going to write here.<\/p>\n<p>The basic idea is that the UDP checksum is a the complement of a 16-bit one&#8217;s complement sum calculated over an IP &#8220;pseudo-header&#8221; and the actual UDP data. The IP pseudo-header is the source address, destination address, protocol (padded with a zero byte) and UDP length. So to take the example of this short packet, the source IP address is <span style=\"font-family: monospace,Courier;\">152.1.51.27<\/span>, and the destination IP address is <span style=\"font-family: monospace,Courier;\">152.14.94.75<\/span>. Divided into 16-bit quantities, these are <span style=\"font-family: monospace,Courier;\">0x9801, 0x331b<\/span> and <span style=\"font-family: monospace,Courier;\">0x980e, 0x5e4b<\/span>. If you add those together using two&#8217;s complement (e.g. with Windows calculator), you get <span style=\"font-family: monospace,Courier;\">0x1c175<\/span>. Note that this overflows a 16-bit quantity, but we&#8217;ll take care of that later. Next is to add in the protocol and UDP length. For this packet, the protocol is UDP so the protocol type byte is 17or <span style=\"font-family: monospace,Courier;\">0x11<\/span>. We pad that with zero to get <span style=\"font-family: monospace,Courier;\">0x0011<\/span> and then add the UDP length which is <span style=\"font-family: monospace,Courier;\">0x000a (10 bytes)<\/span>. So <span style=\"font-family: monospace,Courier;\">0x1c175 + 0x0011 + 0x0! 00a = 0x1c190.<\/span><\/p>\n<p>Now we add the entire UDP datagram, treating it all as 16-bit quantities and skipping the checksum (until we finish calculating it!). For this datagram, that&#8217;s <span style=\"font-family: monospace,Courier;\">0xa08f, 0x2694, 0x000a, 0x6262,<\/span> so if we add all that to our running sum, we get <span style=\"font-family: monospace,Courier;\">0x1c190 + 0xa08f + 0x2694 + 0x000a + 0x6262 = 0x2eb1f.<\/span><\/p>\n<p>Now to convert to a ones complement 16-bit sum we just treat our current sum <span style=\"font-family: monospace,Courier;\">(0x2eb1f)<\/span> as a 32-bit quantity and add the high half to the low half. <span style=\"font-family: monospace,Courier;\">0x0002 + 0xeb1f = 0xeb21<\/span>. (If that still had an overflow, we&#8217;d add the high and low halves again until there was no longer an overflow.) Now we complement that quantity (i.e. flip all the bits, or do a NOT operation) and we get a value of <span style=\"font-family: monospace,Courier;\">0x14de<\/span> which is exactly what the reported checksum shows in the packet.<\/p>\n<p><a href=\"http:\/\/www4.ncsu.edu\/~mlsichit\/Teaching\/407\/Resources\/udpChecksum.html\">Short UDP Checksum Calculation HowTo<\/a><br \/>\n<a href=\"http:\/\/www.roman10.net\/how-to-calculate-iptcpudp-checksumpart-1-theory\/\">How to Calculate IP\/TCP\/UDP Checksum\u2013Part 1 Theory<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/*\r\n * Our algorithm is simple, using a 32 bit accumulator (sum), we add\r\n * sequential 16 bit words to it, and at the end, fold back all the\r\n * carry bits from the top 16 bits into the lower 16 bits.\r\n *\/\r\nuint16_t\r\nraw_packet_calc_checksum(uint16_t *buffer, uint16_t len)\r\n{\r\n    const uint16_t  words = len \/ 2;\r\n    uint32_t        sum;\r\n    uint16_t        i;\r\n    \r\n    sum = 0;\r\n    for (i = 0; i &lt; words; i++) {\r\n        sum = sum + *(buffer + i);\r\n    }\r\n    \r\n    \/* add carry *\/\r\n    sum = (sum &gt;&gt; 16) + sum;\r\n    \r\n    \/* truncate to 16 bits *\/\r\n    return ~sum;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MAC Address What range of MAC addresses can I safely use for my virtual machines? MAC Address: Universally or Locally Administered Bit and Individual\/Group Bit Frame Check Sequence (FCS) = Cyclic Redundancy Check (CRC) Cyclic Redundancy Check (CRC), Frame Check Sequence (FCS) UDP Checksum Short UDP Checksum Calculation HowTo Following is a description by Ed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1810","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1810"}],"version-history":[{"count":22,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1810\/revisions"}],"predecessor-version":[{"id":10514,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1810\/revisions\/10514"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}