Networking for beginners

A series for those who write software, run servers or simply want to understand what happens between pressing Enter in the address bar and the page appearing.
The series has one principle: for every protocol, not only why it is needed but how it works. Which bytes go over the wire, who answers them, what happens when something breaks. That is why almost every part has packet headers, commands and tcpdump output. To repeat the examples, any Linux machine (WSL will do) with tcpdump and Wireshark on your workstation is enough.
The order is bottom-up through the stack and then outwards: first the basics (the models, Ethernet, Wi-Fi and PPPoE), then IPv4 with ARP and ICMP, the UDP and TCP transports, the DHCP and DNS services, a separate block on IPv6 with Neighbor Discovery, SLAAC and DHCPv6, then VLAN, VXLAN, IPsec with IKEv2, VPNs and proxies, routing with OSPF and BGP, and finally TLS with certificates.
What are the OSI and TCP/IP models?

OSI and TCP/IP are two models that describe the network stack as a set of layers: each layer solves one task, uses the layer below and prepends its own header to the data. OSI is a seven-layer scheme that serves as a common language, the one behind "L2", "L3" and "L7"; TCP/IP is a four-layer model of how networks are actually built. On the wire this gives a nesting doll of Ethernet → IP → TCP/UDP → data, and a "what's inside" field in every header — EtherType, Protocol, port — tells the receiver whom to hand the contents to.
How does Ethernet work?

Ethernet delivers data to a neighbour on the cable in frames: a 14-byte header — destination MAC, source MAC and the EtherType "what's inside" field — then 46–1500 bytes of data and a 4-byte CRC; a frame that fails it is silently dropped. A switch remembers which port it saw each source MAC behind, forwards a frame only to the destination's port and floods unknown and broadcast addresses to every port, never changing a byte of the frame. The 64-byte minimum is a legacy of the shared cable and CSMA/CD, which full duplex does without.
How does Wi-Fi work?

Wi-Fi pretends to be Ethernet, but inside it is a shared half-duplex radio channel: one device transmits at a time, and a collision cannot be heard, so instead of CSMA/CD it uses CSMA/CA — listen to the air, back off a random time, send the frame, retransmit if no ACK arrives. An access point is a bridge between 802.11 frames and ordinary Ethernet. Joining takes scanning, authentication, association and the WPA2/WPA3 four-way handshake: the password never crosses the air, and traffic is encrypted with a PTK unique to each session.
What is PPPoE and why is it used?

PPPoE runs a PPP session — a two-party protocol with no addresses — over the ISP's shared Ethernet. The client finds an access concentrator with a broadcast PADI, gets a PADO, sends a PADR and receives a Session ID in the PADS; then frames with EtherType
0x8864carry plain PPP: LCP agrees on the link, PAP or CHAP checks the username and password, IPCP hands out the address. The ISP gets authentication, accounting and session control through RADIUS; the subscriber gets an MTU of 1492, since the PPPoE and PPP headers take 8 bytes out of 1500.How do IPv4 addresses, masks and subnets work?

An IPv4 address is a 32-bit number written as four dotted bytes, and the mask splits it into network and host parts:
192.168.1.10/24and mask255.255.255.0are the same thing. The network address is address AND mask, broadcast is address OR the inverted mask, and a subnet has 2^(32 − prefix) − 2 hosts. Moving the boundary right splits a network into subnets, moving it left merges aligned blocks. Most importantly, a host applies its mask to the destination address to decide whether it is a neighbour or the packet goes to the default gateway.How do the IPv4 header, routing and NAT work?

The IPv4 header is 20 bytes: source and destination addresses, TTL, the Protocol field, the length, the fragmentation fields and a checksum of the header only. At every hop a router decrements TTL, picks the routing table row with the longest matching prefix and wraps the packet in a new frame for the next hop — statelessly, each packet an independent decision. NAT rewrites the source address and port to public ones, keeps a translation table and uses it to restore the address in replies, so inbound connections need port forwarding.
How does ARP work?

ARP translates a neighbour's IP address into the MAC address that an Ethernet frame needs. The host sends a broadcast request asking who has this IP address, the owner answers with a unicast frame giving its MAC, and the result is cached in the neighbour table (
ip neigh), where an entry passes through REACHABLE → STALE → recheck → FAILED. ARP works only within a segment: for other networks the host asks for the gateway's MAC. The protocol has no checks, so anyone in the segment can claim to be the gateway — that is ARP spoofing.What is ICMP and how do ping and traceroute work?

ICMP is the protocol the network uses to tell a sender that delivery failed: a router or host drops the packet and sends back a message with a type and code (Destination Unreachable, Time Exceeded, Fragmentation Needed) that carries the original packet's header.
pingsends an Echo Request and waits for an Echo Reply, measuring the round-trip time.traceroutesends probes with TTL starting at 1 and rising: every router where TTL reaches zero answers with Time Exceeded from its own address, and the path is assembled hop by hop.How does UDP work?

UDP delivers data not to a machine but to a process on it: an eight-byte header — source port, destination port, length and checksum — is prepended to the data, and the datagram is handed to IP. There is no connection: no handshake, no acknowledgements, no retransmissions, no ordering, a lost datagram is simply lost, but message boundaries are preserved and one socket receives all peers. That is why UDP is chosen where latency matters more than completeness or the application knows better what to do about a loss: DNS, DHCP, voice, QUIC.
How does TCP work?

TCP turns unreliable IP into a reliable byte stream in both directions: it numbers every byte, the receiver acknowledges with the number of the next byte it expects, and whatever is not acknowledged the sender retransmits — on the RTO timer or after three duplicate ACKs. A connection opens with the SYN, SYN+ACK, ACK handshake and closes with a FIN in each direction. Speed is limited by two windows: the receive window protects the receiver, the congestion window protects the network; it grows until a loss occurs and is then halved.
How does DHCPv4 work?

DHCP gives a machine with no address an address, mask, gateway and DNS in four messages over UDP 67/68 — DORA: the client broadcasts a DISCOVER from
0.0.0.0, the server offers an address in an OFFER, the client asks for it in a REQUEST, the server confirms with an ACK, and the parameters travel in options. The address is leased, and halfway through the lease the client renews it with a unicast request to its server. Across a router the request is forwarded by a relay agent, and the server picks the pool by its address ingiaddr.How does DNS work?

DNS turns a name into an address through a distributed name tree cut into zones, each served by its own authoritative servers. The stub resolver in the OS sends the query to a recursive server, which walks the tree from the root servers through the
com.servers to theexample.com.servers, gets an authoritative answer and caches every step for the record's TTL. The message travels in UDP on port 53, and if the answer does not fit in the datagram it is repeated over TCP; the whole path can be walked by hand withdig +trace.How do IPv6 addresses and the header work?

An IPv6 address is 128 bits written as eight colon-separated groups of hex digits: leading zeros are dropped and the longest run of zero groups becomes
::. The upper 64 bits are the network prefix (every local network is a/64), the lower 64 the interface identifier a host can generate itself; a link-localfe80::exists before any configuration. The header is always 40 bytes with no checksum and no fragmentation fields: everything optional goes into a chain of extension headers linked by Next Header, and TTL became Hop Limit.What are ICMPv6 and Neighbor Discovery?

ICMPv6 is the protocol at Next Header 58 that in IPv6 does the job of ICMP, ARP and part of DHCP: errors such as Packet Too Big, ping, and the Neighbor Discovery messages. A neighbor is found with a Neighbor Solicitation to its solicited-node multicast group, answered by a Neighbor Advertisement; the router announces itself with Router Advertisements to
ff02::1, and DAD checks that an address is free. All NDP messages carry Hop Limit 255, and ICMPv6 must never be filtered wholesale: without it a host finds neither neighbor nor router.How does SLAAC work?

SLAAC builds an address without a server: the host takes the
/64prefix from a Router Advertisement with the A flag, makes up a 64-bit interface identifier itself, checks the address with DAD and starts working; no one keeps track of which address was given to whom. The identifier comes from the MAC via EUI-64, from a stable hash per RFC 7217, or at random for temporary addresses, which change once a day. An address has two lifetimes — preferred and valid — and every new RA refreshes them; the default gateway always comes from that same RA.How does DHCPv6 work?

DHCPv6 is a separate protocol over UDP 546/547: the client sends a Solicit from its link-local address to
ff02::1:2, gets an Advertise, sends a Request and gets a Reply with an address inside an IA_NA; with Rapid Commit it takes two messages. The client is named by a DUID, not a MAC, starts only on the M or O flag in a Router Advertisement, and never gets a gateway — that comes from the RA. Its main role is prefix delegation: the subscriber's router asks for an IA_PD, gets a/56from the provider and hands out a/64to its networks.What is a VLAN and how does it work?

A VLAN splits one switch and one cable into several independent networks, each of them a separate broadcast domain with its own IP subnet. The switch marks a frame with a four-byte 802.1Q tag carrying a 12-bit VLAN number (4094 in all) and floods broadcasts only to ports of the same VLAN. An access port belongs to one VLAN and hands the host untagged frames, a trunk carries many VLANs with tags, and traffic between VLANs goes only through a router or an L3 switch.
What is VXLAN and why is it needed?

VXLAN wraps an Ethernet frame in UDP (port 4789) with an 8-byte header and a 24-bit VNI: an L2 segment stretches across any IP network, with 16 million networks instead of VLAN's 4094. A VTEP sits on the border between overlay and underlay and learns which remote VTEP a MAC lives behind from traffic (flood-and-learn) or from a control plane — EVPN over BGP, or static entries from an orchestrator, as in Kubernetes. That gives clouds a network per customer and lets data centers move virtual machines between racks without changing addresses.
How do IPsec and IKEv2 work?

IPsec encrypts and signs whole IP packets at the network layer, so any protocol is protected and applications know nothing about it: ESP adds an SPI, a sequence number and an integrity check, and in tunnel mode hides the original packet inside a new IP header. Each direction's keys live in a Security Association, and IKEv2 negotiates them: in IKE_SA_INIT the parties pick algorithms and do a Diffie-Hellman exchange, in IKE_AUTH they authenticate with a PSK, a certificate or EAP and create the first Child SA; behind NAT it all moves to UDP 4500.
How do VPNs and proxies work?

A VPN is a tunnel at the network layer: after connecting, the system gets an interface like
tun0orwg0and routes into it, so every application and any protocol over IP goes through it, and encryption is built in. A proxy is an intermediary at the transport or application layer: the application connects to it via HTTP CONNECT or SOCKS5 and asks it to reach the destination, so it works only where configured. A "VPN" in a browser extension is a proxy, and neither one is anonymity: all that changes is who sees your traffic.How does OSPF work?

OSPF is a link-state routing protocol for one network: each router describes only its own links in LSAs and floods them to everyone, so every router assembles the same map (the LSDB) and computes the shortest paths from itself with Dijkstra's algorithm. It finds neighbors with Hello packets to
224.0.0.5directly in IP (protocol 89), a path's cost is the sum of the outgoing interface costs, and when a link fails a new LSA reaches everyone within seconds and paths are recomputed. Large networks are split into areas with a mandatory area 0.How does BGP work?

BGP is the routing protocol between autonomous systems that the entire internet rests on: neighbors are configured by hand, the session runs over TCP on port 179, and instead of a map routers pass each other prefixes with attributes, the main one being AS_PATH, the list of ASes to the origin. The best path is chosen not by distance but by policy: LOCAL_PREF first, then AS_PATH length, MED and the remaining rules down the chain. eBGP between ASes adds its own number to the path and sets the next hop to itself; iBGP within an AS changes nothing.
How do TLS and certificates work?

TLS protects a single TCP connection: in one handshake round trip client and server exchange one-time Diffie-Hellman keys, obtain a shared secret, and everything after that is encrypted, certificate included. A certificate is a public key, the owner's names in the SAN and a CA's signature; the client builds the chain up to a root in its trust store and verifies the CertificateVerify signature, which only the owner of the private key can produce. That key only signs, so stealing it does not expose recorded traffic — that is forward secrecy.