TCP and UDP · Introduction · Module 3
Well-known ports: which application waits where
Show topologyHide topology
The router R1 at the left end and the host H1 at the right end, joined by one cable. At R1’s end, Et0/0 above the cable and .1 under it: R1’s interface Ethernet0/0 with address 10.0.1.1. At H1’s end, eth0 above and .10 under it: H1’s interface eth0 with address 10.0.1.10. Under the cable, mid-way, its network: 10.0.1.0/24. A legend explains the short forms.
Well-known ports
Numbers everyone knows
A number everyone knows
A web browser asks for a page from a web server it has never contacted. Where does it get the port number to try?
It needs the number before it sends anything, because the client starts the conversation. A number set by hand works only when every client can be told it. A server that any client may contact needs a number known in advance.
That number comes from a public list. A well-known port is a port number from 0 to 1023 that a public list sets aside for one application, usually for its server. The client knows the number before it starts.
HTTP is the way web pages are fetched; its server waits on TCP port 80. HTTPS is HTTP protected by encryption, on TCP port 443. SSH is a protected way to log in to a device from afar; its server waits on TCP port 22. The web browser on your phone is a client of HTTP and HTTPS servers. R1 runs an SSH server, HTTP and HTTPS servers and a server that turns names into addresses, each on its own well-known port (Figure 3.1).
R1 on the left as a frame holding three slots, top down: “SSH · port 22” (highlighted), “HTTP · port 80” and “HTTPS · port 443”. H1, the client, stands on the right with a host icon; an arrow runs from H1 to the SSH slot, labeled “port 22”.
A well-known port is only where a server waits by default. A server can be set to wait on another port, and then its clients have to be told the new number. A web address such as http://10.0.1.1 names no port, so the browser uses 80.
Files, names and addresses
Fetching a web page and logging in with SSH normally start with a TCP connection. Do copying a file, finding a name and getting an address start the same way?
Some do. FTP, a common way to copy files between hosts, sends its commands over a TCP connection to port 21, and by default the server’s end of the data connection is port 20.
Others do not. TFTP, a simple way to copy files that needs no connection, uses UDP, with its server on port 69.
Before a browser can reach a server by a name such as www.example.com, DNS turns that name into an address. Its questions and answers use UDP port 53, and a name server, the server side of DNS, also takes TCP on port 53. DHCP gives a device its address when it joins a network, which is usually how your phone gets its IPv4 address on Wi-Fi. It uses UDP, with the server waiting on port 67 and the client on port 68.
Why UDP for DNS? An ordinary question and its answer each fit in one short message. Over TCP, a three-segment handshake would have to open a connection first, and more segments would close it. Over UDP the question leaves at once, and the DNS standard prefers single UDP messages for questions because they need less extra traffic.
A port number alone does not tell you TCP or UDP, so learn each application’s transport along with its port.
| Application | Server port | Transport |
|---|---|---|
| FTP | 21 (commands), 20 (data) | TCP |
| SSH | 22 | TCP |
| DNS | 53 | UDP (also TCP) |
| DHCP | 67 | UDP |
| TFTP | 69 | UDP |
| HTTP | 80 | TCP |
| HTTPS | 443 | TCP |
Key idea
Each application has its well-known port (FTP and DHCP have two each) and the transport it normally uses: TCP or UDP, and for DNS both.
How it works
UDP, and two sockets per exchange
UDP: one question, one answer, no connection
H1 asks R1’s name server for an address. With no connection to open, what crosses the link?
A question one way and an answer the other (Figure 3.2), each sent as a datagram, a single UDP message that carries both port numbers and is sent without a connection. No handshake, no FIN: the question goes, the answer comes back.
R1 can log these datagrams. With UDP debugging on (debug ip udp), it logs rcvd lines for datagrams it received and sent lines for datagrams it sent. Each rcvd or sent line names the sender as src and the receiver as dst, each as an address with its port in brackets. Debugging can affect the router’s performance, so turn it off afterwards with undebug all.
Step 1 of 3
H1 sends its question as a datagram, from its port 33717 to R1’s port 53, where the name server waits. Nothing was opened first.
Step 2 of 3
R1’s answer swaps the two ports: it leaves from 53 and goes straight back to H1’s port 33717.
Step 3 of 3
Nothing was opened, so there is nothing to close, and R1’s TCP list has no line for this exchange.
R1, the name server, stands on the left and H1, the client, on the right, each with a lane running down under it; time runs down. Under R1 a note reads “port 53”, under H1 “port 33717”. Top down: an arrow from H1 to R1 labeled “question · from 33717 to 53”, then an arrow from R1 to H1 labeled “answer · from 53 to 33717”. The last step adds a box under the lanes: “no connection: nothing opened, nothing to close”.
On R1, with UDP debugging on, while H1 asked its name server for an address:
R1 received H1’s question and sent its answer back to the port the question came from.
R1#show logging | include UDP… 1 more line (see Full output)1Nov 24 20:58:15.436: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=332Nov 24 20:58:15.436: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=49… 15 more lines (see Full output)
- The question, from
10.0.1.10(33717)to10.0.1.1(53). - The answer, from
10.0.1.1(53)back to10.0.1.10(33717): the two ports are swapped.
Full output · 18 lines
R1#show logging | include UDPNov 24 20:58:15.436: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:15.436: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:15.436: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=49Nov 24 20:58:15.436: Reserved port 64926 in Transport Port Agent for UDP IP type 1Nov 24 20:58:15.436: UDP: sent src=0.0.0.0(64926), dst=255.255.255.255(53), length=44Nov 24 20:58:16.437: Reserved port 15863 in Transport Port Agent for UDP IP type 1Nov 24 20:58:16.437: UDP: sent src=0.0.0.0(15863), dst=255.255.255.255(53), length=44Nov 24 20:58:17.939: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:18.938: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33Nov 24 20:58:18.938: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33Nov 24 20:58:18.939: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:18.939: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:18.939: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33Nov 24 20:58:18.939: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33Nov 24 20:58:18.940: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:18.940: UDP: rcvd src=10.0.1.10(33717), dst=10.0.1.1(53), length=33Nov 24 20:58:18.940: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33Nov 24 20:58:18.940: UDP: sent src=10.0.1.1(53), dst=10.0.1.10(33717), length=33
Did the exchange leave a connection behind? show tcp brief all lists every TCP connection R1 holds, and the ports it waits on as LISTEN lines, LISTEN meaning waiting for a connection request.
On R1, right after the exchange:
R1 lists only waiting lines.
R1#show tcp brief all1Local Address Foreign Address (state)20.0.0.0.53 *.* LISTEN2::.53 *.* LISTEN3::.443 *.* LISTEN0.0.0.0.443 *.* LISTEN::.80 *.* LISTEN0.0.0.0.80 *.* LISTEN0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- Column names: R1’s end, the far end, the state.
- Port 53 here is the name server waiting on TCP, not a trace of the UDP exchange.
- This line and every line under it:
LISTENwith no far end (*.*), R1 waiting. No line in the list has H1’s address.
Full output · 9 lines
R1#show tcp brief allTCB Local Address Foreign Address (state)7BE696B641C8 0.0.0.0.53 *.* LISTEN7BE696B63090 ::.53 *.* LISTEN7BE69B7AC760 ::.443 *.* LISTEN7BE69B7AB480 0.0.0.0.443 *.* LISTEN7BE69B7AA1A0 ::.80 *.* LISTEN7BE69B7A9580 0.0.0.0.80 *.* LISTEN------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
After a question and its answer, you might expect a connection to be left open. UDP made none, so no line in the list belongs to this exchange and there is nothing to close.
Two ends, two sockets
H1 asks the name server a second question. Both questions go to R1’s port 53. How are the two kept apart?
By H1’s end. Each end of an exchange is an address together with a port, such as 10.0.1.1 port 53, and that pair is a socket. The debug lines print each socket as the address with its port in brackets, so 10.0.1.10(33717) is H1’s socket.
H1’s port is not a well-known one. An ephemeral port is the port number a client’s device picks by itself for its end of one exchange or connection, and H1 picked a new one each time it asked, 33717 for its first question and 41266 for its second. Most clients use one for their own end; the DHCP client, on port 68, is an exception.
Two exchanges with the same server socket differ at the client’s socket. Both questions go to one socket on R1 and come from two sockets on H1 (Figure 3.3).
R1 on the left as a frame holding one highlighted socket tag, “10.0.1.1 port 53”. H1 on the right as a frame holding two socket tags: “question 1” above “10.0.1.10 port 33717”, and “question 2” above “10.0.1.10 port 41266”. Two dashed lines run from R1’s socket, one to each of H1’s sockets, with a dot at both ends.
show udp lists R1’s UDP sockets. UDP keeps no connection, but on the name server’s IPv4 line the Remote columns hold the socket of the latest question; the Local columns are R1’s end.
On R1, after H1’s first question:
The latest question came from H1’s socket, 10.0.1.10 port 33717.
R1#show udp1Remote Port Local Port2:: 0 --any-- 53310.0.1.10 33717 --any-- 53
Scroll sideways to see the whole output
- Column names: the far end (
Remote,Port), then R1’s end (Local,Port). - The name server’s line for clients that use IPv6, another kind of address (the
::form). This network uses IPv4. - Far end 10.0.1.10 port 33717. R1’s end:
--any--and port 53;--any--stands for the local address 0.0.0.0, whichever of R1’s addresses a question arrives at.
Full output · 3 lines
R1#show udpProto Remote Port Local Port In Out Stat TTY OutputIF 17(v6) :: 0 --any-- 53 0 0 2000011 0 17 10.0.1.10 33717 --any-- 53 0 0 2000011 0
Scroll sideways to see the whole output
On R1, after H1’s second question:
The same socket on R1, and a new port on H1’s side. The line left out is a socket of R1’s own (local port 10356, far end 255.255.255.255 port 53), not one of H1’s.
R1#show udpRemote Port Local Port:: 0 --any-- 53110.0.1.10 41266 --any-- 53… 1 more line (see Full output)
Scroll sideways to see the whole output
- The same R1 end, port 53. The far end is 10.0.1.10 again, but now from port 41266.
Full output · 4 lines
R1#show udpProto Remote Port Local Port In Out Stat TTY OutputIF 17(v6) :: 0 --any-- 53 0 0 2000011 0 17 10.0.1.10 41266 --any-- 53 0 0 2000011 0 17 255.255.255.255 53 --any-- 10356 0 0 211 0
Scroll sideways to see the whole output
TCP keeps connections apart the same way, since a connection is defined by a pair of sockets, one at each end. So many clients can be connected to one server port at once, each connection differing at the client’s socket, and the first to connect does not take that port for itself.
Key idea
An exchange or a connection is known by its two sockets. UDP sends single datagrams between two sockets, with nothing to open or close.
Use the rule: a socket is an address and a port; the server keeps its socket, the client picks its port.
Verify
Which application waits where on R1
R1 runs several servers. How do you see which application waits on which port?
Read R1’s own end of each line. In show tcp brief all, each LISTEN line is a TCP port where R1 waits for any client.
When R1 is the server, its own end carries the server’s port, by default the well-known one. Each server shows twice, as 0.0.0.0 for any of R1’s IPv4 addresses, where it waits for IPv4 clients such as H1, and as :: for IPv6.
On R1, with its servers on and no client connected:
Tap a line, or “Not in the output”, then submit.
Find the line where R1’s encrypted web server (HTTPS) waits for IPv4 clients such as H1.
R1#show tcp brief all1Local Address Foreign Address (state)
Scroll sideways to see the whole output
R1 waits on TCP for its name server (53), its two web servers (443 and 80) and its SSH server (22), each for IPv4 and IPv6.
- Column names: R1’s end (
Local Address), the far end (Foreign Address), the state. - DNS, port 53: R1’s name server waits on TCP 53 too.
- HTTPS, port 443. The IPv4 line is
0.0.0.0.443. - HTTP, port 80.
- SSH, port 22.
Full output · 9 lines
R1#show tcp brief allTCB Local Address Foreign Address (state)7BE696B641C8 0.0.0.0.53 *.* LISTEN7BE696B63090 ::.53 *.* LISTEN7BE69B7AC760 ::.443 *.* LISTEN7BE69B7AB480 0.0.0.0.443 *.* LISTEN7BE69B7AA1A0 ::.80 *.* LISTEN7BE69B7A9580 0.0.0.0.80 *.* LISTEN------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
show udp lists R1’s UDP sockets in a similar way, but UDP keeps no connection, so no line shows a connection state such as LISTEN or ESTAB.
On R1, before any client has asked its name server a question:
R1’s name server waits for questions on UDP port 53, for IPv6 and for IPv4.
R1#show udpRemote Port Local Port1:: 0 --any-- 5320.0.0.0 0 --any-- 53
Scroll sideways to see the whole output
- The name server for IPv6 (the
::form). - R1’s end,
--any--port 53: the name server, for IPv4. The far end,0.0.0.0port 0, means no client has sent a question to it yet; the socket takes questions from any client.
Full output · 3 lines
R1#show udpProto Remote Port Local Port In Out Stat TTY OutputIF 17(v6) :: 0 --any-- 53 0 0 2000011 0 17 0.0.0.0 0 --any-- 53 0 0 2000011 0
Scroll sideways to see the whole output
Key idea
When R1 is the server, read R1’s own end: its port is where the application waits, by default its well-known port. In a connection, the far end is the client’s socket.
Troubleshoot
When the client finds nothing
A client reaches nothing at the port it was given. Where does the server really wait?
Clients find nothing at port 8080
Symptom
Suppose the clients in this network are told that R1’s web server waits on port 8080. A client that tries 8080 finds nothing there.
Command
show ip http server statusnames the port R1’s web server waits on.Output
On R1:
The web server is on, and its port is 80.
R1#show ip http server status1HTTP server status: Enabled2HTTP server port: 80… 34 more lines (see Full output)
Scroll sideways to see the whole output
- The web server is on:
Enabled. - Key line Port 80, HTTP’s well-known port, not the 8080 the clients were told.
Full output · 36 lines
R1#show ip http server statusHTTP server status: EnabledHTTP server port: 80HTTP server active supplementary listener ports: HTTP server authentication method: enableHTTP server auth-retry 0 time-window 0HTTP server digest algorithm: md5HTTP server access class: 0HTTP server IPv4 access class: NoneHTTP server IPv6 access class: NoneHTTP server base path: unix:htmlHTTP File Upload status: DisabledHTTP server upload path: HTTP server help root: Maximum number of concurrent server connections allowed: 16Maximum number of secondary server connections allowed: 5Server idle time-out: 180 secondsServer life time-out: 180 secondsServer session idle time-out: 600 secondsMaximum number of requests allowed on a connection: 25Server linger time : 60 secondsHTTP server active session modules: ALLHTTP secure server capability: PresentHTTP secure server status: EnabledHTTP secure server port: 443HTTP secure server ciphersuite: dhe-aes-cbc-sha2 dhe-aes-gcm-sha2 ecdhe-rsa-aes-cbc-sha2 ecdhe-rsa-aes-gcm-sha2 ecdhe-ecdsa-aes-gcm-sha2 tls13-aes128-gcm-sha256 tls13-aes256-gcm-sha384 tls13-chacha20-poly1305-sha256HTTP secure server TLS version: TLSv1.3 TLSv1.2HTTP secure server client authentication: DisabledHTTP secure server PIV authentication: DisabledHTTP secure server PIV authorization only: DisabledHTTP secure server trustpoint: HTTP secure server peer validation trustpoint: HTTP secure server ECDHE curve: secp256r1HTTP secure server active session modules: ALL
Scroll sideways to see the whole output
- The web server is on:
What it means
Figure 3.4 · The client tries port 8080, where nothing waits; R1’s web server waits on 80. R1 on the left as a frame with two slots: a highlighted one, “HTTP · port 80”, and a dimmed one, “nothing · port 8080”. H1, the client, stands on the right with a host icon. Its arrow points to the 8080 slot and is crossed out before it reaches R1.
- No other port was set on R1, so its web server uses the default, 80.
- Nothing waits on 8080, so the client finds nothing there.
- The client must go to the port the server really waits on.
Fix
Make the two agree: either the clients use port 80, or R1’s web server moves to 8080. Here R1’s web server was configured to wait on port 8080. Afterwards, on R1:
R1#show ip http server statusHTTP server status: Enabled1HTTP server port: 8080… 34 more lines (see Full output)
Scroll sideways to see the whole output
- The web server now waits on port 8080.
Full output · 36 lines
R1#show ip http server statusHTTP server status: EnabledHTTP server port: 8080HTTP server active supplementary listener ports: HTTP server authentication method: enableHTTP server auth-retry 0 time-window 0HTTP server digest algorithm: md5HTTP server access class: 0HTTP server IPv4 access class: NoneHTTP server IPv6 access class: NoneHTTP server base path: unix:htmlHTTP File Upload status: DisabledHTTP server upload path: HTTP server help root: Maximum number of concurrent server connections allowed: 16Maximum number of secondary server connections allowed: 5Server idle time-out: 180 secondsServer life time-out: 180 secondsServer session idle time-out: 600 secondsMaximum number of requests allowed on a connection: 25Server linger time : 60 secondsHTTP server active session modules: ALLHTTP secure server capability: PresentHTTP secure server status: EnabledHTTP secure server port: 443HTTP secure server ciphersuite: dhe-aes-cbc-sha2 dhe-aes-gcm-sha2 ecdhe-rsa-aes-cbc-sha2 ecdhe-rsa-aes-gcm-sha2 ecdhe-ecdsa-aes-gcm-sha2 tls13-aes128-gcm-sha256 tls13-aes256-gcm-sha384 tls13-chacha20-poly1305-sha256HTTP secure server TLS version: TLSv1.3 TLSv1.2HTTP secure server client authentication: DisabledHTTP secure server PIV authentication: DisabledHTTP secure server PIV authorization only: DisabledHTTP secure server trustpoint: HTTP secure server peer validation trustpoint: HTTP secure server ECDHE curve: secp256r1HTTP secure server active session modules: ALL
Scroll sideways to see the whole output
Proof
R1’s list now shows its web server waiting on port 8080, the port the clients were told, and no line for port 80.
R1#show tcp brief allLocal Address Foreign Address (state)1::.8080 *.* LISTEN10.0.0.0.8080 *.* LISTEN20.0.0.0.53 *.* LISTEN::.53 *.* LISTEN::.443 *.* LISTEN0.0.0.0.443 *.* LISTEN0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
LISTENon port 8080, for IPv6 and IPv4: the web server.- This line and every line under it: the other servers, unchanged. No line has port 80 any more.
Full output · 9 lines
R1#show tcp brief allTCB Local Address Foreign Address (state)7BE69E0601F8 ::.8080 *.* LISTEN7BE69E05F060 0.0.0.0.8080 *.* LISTEN7BE696B641C8 0.0.0.0.53 *.* LISTEN7BE696B63090 ::.53 *.* LISTEN7BE69B7AC760 ::.443 *.* LISTEN7BE69B7AB480 0.0.0.0.443 *.* LISTEN------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
Key idea
Check where the server really waits: the port number, and TCP or UDP. The well-known port is only where it waits by default.
Check
Later, after a configuration change on R1:
Tap a line, or “Not in the output”, then submit.
Find the line where R1’s encrypted web server (HTTPS) waits for IPv4 clients.
R1#show tcp brief allLocal Address Foreign Address (state)
Scroll sideways to see the whole output
Full output · 7 lines
R1#show tcp brief allTCB Local Address Foreign Address (state)7BE69E0601F8 ::.8080 *.* LISTEN7BE69E05F060 0.0.0.0.8080 *.* LISTEN7BE696B641C8 0.0.0.0.53 *.* LISTEN7BE696B63090 ::.53 *.* LISTEN------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
Summary
R1, the name server, stands on the left and H1, the client, on the right, each with a lane running down under it; time runs down. Under R1 a note reads “port 53”, under H1 “port 33717”. Top down: an arrow from H1 to R1 labeled “question · from 33717 to 53”, then an arrow from R1 to H1 labeled “answer · from 53 to 33717”. The last step adds a box under the lanes: “no connection: nothing opened, nothing to close”.
- A well-known port, from 0 to 1023, tells every client where a server waits by default.
- HTTP 80, HTTPS 443, SSH 22, FTP 21 and 20 use TCP; DNS 53 (also TCP), DHCP 67 and 68, TFTP 69 use UDP.
- UDP sends datagrams with no connection; a DNS answer goes from port 53 back to the port the question came from.
- A socket is an address and a port; an exchange or connection is known by its two sockets, and a client’s port is usually an ephemeral one.
- When R1 is the server,
show tcp brief allandshow udplist the ports it waits on; if a client finds nothing, check the port and the transport.