6 ms·
Let's code a TCP/IP stack, 1: Ethernet and ARP
- songgao 11y agoShameless plug: In case anybody is interested in playing with Ethernet frames in Go, I've got two tiny packages: TUN/TAP: https://github.com/songgao/water https://github.com/songgao/water (Linux only for now) Raw Ethernet: https://github.com/songgao/ether https://github.com/songgao/ether (Linux, FreeBSD, Darwin)
- dkopi 11y agoImplementing ARP is an awesome introduction to networking and low level packet processing. This was often the first task I would give out during training for Real Time / Embedded programmers A few things I'm missing for this to be a more complete ARP implementation: 1. Answer Ip to Mac conversion requests from the above stack 2. Send an ARP Request if the mac is missing (+Retries) 3. Static ARP configurations (To prevent ARP spoofing attacks) 4. ARP entry aging. Otherwise you'll run really quickly into "ERR: No free space in ARP translation table\n" (And of course a much larger cache than 32 entries) 5. Ignore unsolicited ARP replies (Helps prevent ARP spoofing attacks as well) 6. use uint8_t instead of "unsigned char" for packet structures. Awesome work!
- saminiir 11y agoThanks! I will definitely update the implementation as the project matures.
- contingencies 11y agoUnanswered ARP request repeat delays can be used for layer 2 OS detection. One of my very early projects was a tortured perl implementation; see https://packetstormsecurity.com/files/21904/induce-arp.tgz.html https://packetstormsecurity.com/files/21904/induce-arp.tgz.h...
- dkopi 11y agoIt's not the fingerprinting that impresses as much as the fact that this was done 16 years ago. Nice!
- contingencies 11y agoI also did ICMP OS detection about 5 years before it became public. In those days, I used to run home from school to see which militaries had visited my page that day. They never used to hide their IP then. You'd get US Pacific Command, Korea and Japanese militaries, etc. I felt like I had a front row seat at the militarization of the internet. Unfortunately archive.org never indexed the page I announced it on off this one: https://web.archive.org/web/19991128100455/http://pho.2600.org.au/ https://web.archive.org/web/19991128100455/http://pho.2600.o... .. but an early version of the list of interesting visitors was archived: https://web.archive.org/web/19991128111635/http://pho.2600.org.au/hosts.html https://web.archive.org/web/19991128111635/http://pho.2600.o...
- voltagex_ 11y agoIs there some kind of VM I could play with this on? Happy to go to something like Buildroot but I've never thought of arp like a daemon, it's always just there.
- Animats 11y agoNice. Retro C code. Declarations of working variables at the beginning of the function, and a call to "strcpy". Very 1980s. Doing this in Rust would be a good exercise.
- ptrain 11y agoFor those of us who can't spot this stuff as easily - is this detrimental, or simply out of style? Is there a recommended guide or body of source code this is "modern" C?
- Animats 11y agoHere's a 2011 guide to modern C: [1] Discussed on YC last year.[2] The 1970s string functions, such as "strcpy", that don't have a destination length were deprecated years ago, but they've never been removed from libraries. Microsoft's C compiler has warned about this for a decade.[3] This is one of the major causes of buffer overflows in C, and a large number of exploits involve it.. There's a CERT advisory from Homeland Security about "strcpy".[4] Seeing a "strcpy" in new code is a big red flag. If you see that in an interview, don't hire that programmer. They're dangerous. Since C99, you've been able to write for (int i = 0; i < 10; ++i) ... in C code, and that was in C++ from the early years. The trend in programming is strongly towards declaring and initializing variables at the same time. Remember, in C, local variables declared but not initialized have junk values until assigned. The "all variables declared at the top of a function" style is obsolete. Pointer variables declared without initialization are especially bad. There's a long and painful history of classic C bugs, and the newer versions of C help, just a little, to avoid them. [1] http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf [2] https://news.ycombinator.com/item?id=9018247 https://news.ycombinator.com/item?id=9018247 [3] http://stackoverflow.com/questions/4012222/c-strcpy-gives-a-warning-c4996 http://stackoverflow.com/questions/4012222/c-strcpy-gives-a-... [4] https://buildsecurityin.us-cert.gov/articles/knowledge/coding-practices/strncpy-and-strncat https://buildsecurityin.us-cert.gov/articles/knowledge/codin...
- ktRolster 11y ago
- arm 11y agoThis reminds me of an old book¹ on Open Transport²³⁴ I started reading for fun a few years ago (what I found particularly interesting about Open Transport was that it used Unix System V’s STREAMS⁵ instead of Berkeley sockets). Anyway, I linked the book here because it’s actually quite well-written, and the introductory section in particular actually does a good job of describing basic networking concepts. ―――――― ¹ — https://web.archive.org/web/20040626025717/http://developer.apple.com/documentation/mac/pdf/NetworkingOT.pdf https://web.archive.org/web/20040626025717/http://developer.... ² — https://en.wikipedia.org/wiki/Open_Transport https://en.wikipedia.org/wiki/Open_Transport ³ — https://developer.apple.com/legacy/library/documentation/Carbon/Reference/Open_Transport/index.html https://developer.apple.com/legacy/library/documentation/Car... ⁴ — https://web.archive.org/web/20041113084430/http://developer.apple.com/macos/opentransport/ https://web.archive.org/web/20041113084430/http://developer.... ⁵ — https://en.wikipedia.org/wiki/STREAMS https://en.wikipedia.org/wiki/STREAMS
- pjc50 11y agoSome while ago I wrote an ARP implementation in Verilog for FPGA use; a very simple state machine. Testing it (and the other TCP parts of the system) involved using the TUN/TAP devices to connect the simulation to real Ethernet and the application it was supposed to talk to. TUN/TAP is very useful for doing nonstandard things with networks, implementing your own VPN, etc.
- notalaser 11y agoThis is a discussion that has happened here before and has generated its share of sparks, but I'll risk it :-). It is hardly a problem in the context of a userspace TCP/IP stack (I don't think Linux runs on any machine with non 8-bit bytes); it's just a good illustration about the kind of pitfalls that programming at this level sometimes poses. I would suggest being a little more careful with this: > unsigned char dmac[6]; because there are several platforms, all of them high-performance, non-legacy processors, where unsigned char is not 8 bits :-). E.g. on AD's SHARC, it's 32. You're declaring the struct with __attribute__((packed)), so I assume you're going to want to fill it automatically through DMA at some point in the future. On such a platform, it won't have the expected results. A colleague got bit by this, on a device that's sitting on my desk right now, and it's not a vintage piece of equipment from the 60s. I would suggest using uint8_t instead, just like you're using uint16_t a few lines below.
- saminiir 11y agoA very valid point! I'll update the code.
- notalaser 11y agoGlad I could help. Your little project is pretty ambitious; I hope it turns out all right -- best of luck! You certainly started well.
- ambrop7 11y agoCorrect me if I'm wrong but... It is impossible for unsigned char to be 32 bits and for uint8_t to exist, on a conformant C/C++ implementation. The sizeof char / unsigned char is 1 ("byte" is defined this way). So if 1 byte is more than 8 bits, there cannot be an uint8_t type, since that would make sizeof uint8_t less than 1. Actually, combined with the requirement for char to have at least 8 bits, is follows that uint8_t can only possibly exist if char has 8 bits. Edit: quoting C99, to show that uint8_t must not have padding bits (an assumption in this reasoning). The requirement for uintN_t to have no padding seems omitted in the second paragraph (I assume by mistake), but the third one suggests it really should have no padding. The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits. The typedef name uintN_t designates an unsigned integer type with width N. Thus, uint24_t denotes an unsigned integer type with a width of exactly 24 bits. These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.
- danieljp 11y agoShould I write a TCP/IP stack in 2016, I would do it IPv6 only. There are several methods for translating IPv4 into IPv6 so that you can get both protocols working anyway. Writing it IPv4 only is useless and harder to extend later. And don't come to tell me that we are not using IPv6 please... ( https://www.google.com/intl/en/ipv6/statistics.html https://www.google.com/intl/en/ipv6/statistics.html ) [Edit: link]
- dkopi 11y agoIPv6 support is very important. But IPv6 only is probably a bad choice that will end up disconnecting you from much of the IPv4 only world.
- detaro 11y agoYou obviously need edge devices that translate for you, e.g. using NAT64 (which maps IPv4 to a /96 IPv6 subnet. Since you then have an IPv6 address for each IPv4 address, you don't even need to maintain state on the gateway, so it scales very well) Facebook does data centers that internally only use IPv6 using these techniques: http://blog.ipspace.net/2014/03/facebook-is-close-to-having-ipv6-only.html http://blog.ipspace.net/2014/03/facebook-is-close-to-having-...
- deleted 11y ago[deleted]
- jws 11y agoIn my own TCP/IP stack I have defined things like IPv4 addresses and port numbers as unions instead of integers, and given them functions to convert to host order and back. e.g. typedef union { uint32_t net_order; uint8_t byte[4]; } ipv4_address; _Static_assert( sizeof(ipv4_address) == 4, "ipv4_address is not 4 bytes long"); static inline __attribute__((overloadable)) uint32_t as_host( ipv4_address a) { return other_order_32(a.net_order); } // more functions follow, like // bool equal(a,b) // int compare(a,b) // ipv4_address ipv4_address_from_host( uint32_t) The nice part of this is that it would take an act of willful ignorance on my part to accidentally miss or over include a conversion from network order to host order. The compiler is still happy to slam these around in registers like integers so I haven't changed the runtime performance. The downside is that C won't let you compare structs or unions with ==, so I have to have that equal() function which dirties the source code a bit. That __attribute__((overloadable)) makes as_host() an overloadable function so I don't have to have a giant family of incredibly_long_function_names to convert all my different types to host byte order or to compare them with themselves. That _Static_assert isn't terribly useful in this case, but there is one on all of the structures where I expect to have explicitly specified a layout. That way when the compiler tries to pull a fast one on me because some language lawyer spent too long reading the spec, I'll find out at build time.
- jnbiche 11y agoVery interesting tutorial, more people should know about using tun/tap for directly accessing ethernet/ip frames. However, please note that this is not production-capable code. Casting directly a struct directly into the data as a way to "parse" is easy and convenient, but ignores issues like endianness, alignment, etc. By all means, use a struct, but for production code, pick off the bits bit-by-by or word-by-word using appropriate bit operators and assign them to the struct members. Also, someone already mentioned the `char` issue with certain architectures. I'm sure I'm missing some other issues, since writing safe, portable C code isn't always obvious. I look forward to reading the next part of these series.
- superuser2 11y agoIt doesn't reach all the way down to ARP, but worth noting is UChicago's ChiTCP project in undergraduate Networks class: http://chi.cs.uchicago.edu/chitcp/ http://chi.cs.uchicago.edu/chitcp/
- p1mrx 11y agoThis is not an IP stack, per the modern definition of IP: https://tools.ietf.org/html/rfc6540 https://tools.ietf.org/html/rfc6540
- godojo 11y agoI would assume endianness to be an issue in such code. Where in these structs would the order need to be validated? Or am I wrong in thinking that the byte order matters for x86 targeted c network code?
- jws 11y agox86 is little endian. The internet protocols are mostly big endian. It matters. The good part is you catch your mistakes. Developing networking code on a big endian machine can get you a surprise later.
- FullyFunctional 11y agoThe is interesting and I hope it will get around to all of the modern implementation tweaks. Hopefully not too off-topic but I would love to learn how we would have implemented a state-of-the-art global interconnect protocol today. There's of course efforts like http://named-data.net http://named-data.net but what about the lower levels like about congestion control. Is dropping packages really the best paradigm? EDIT: to be clear, I mean if starting from scratch in a world where IP never existed (and no backwards compatibility mattered).
- vdm 11y agohttps://en.wikipedia.org/wiki/Recursive_InterNetwork_Architecture_(RINA) https://en.wikipedia.org/wiki/Recursive_InterNetwork_Archite...
- nanoojaboo 11y agoso where's part two?