csperkins.org

Networked Systems H (2020-2021)

Lecture 2: Connection Establishment in a Fragmented Network

Lecture 2 discusses connection establishment. It begins by reviewing the operation of TCP and showing how TCP connections are established, and what factors influence the performance of connection establishment. It considers the impact TLS and IPv6 on connection establishment, and discusses the need for connection racing. And it reviews the idea of peer-to-peer connection, and the difficulties network address translation causes for peer-to-peer connection establishment. The lecture concludes with a brief explanation of how NAT binding discovery, and the ICE algorithm for peer-to-peer connection establishment, work.

Part 1: Connection Establishment in a Fragmented Network

The 1st part of this lecture reviews the operation of TCP. It outlines the TCP service model, segment format, and programming model. Then, it discusses how TCP connections are established and considers the impact of network latency on TCP connection establishment performance. It concludes with a review of how this connection establishment latency affects protocol design.

00:00:00.000 In this lecture, I’ll talk about the

00:00:03.310 problem of connection establishment in a fragmented

00:00:05.620 network, and discuss some issues that affect

00:00:07.930 the performance of TCP connection establishment and

00:00:10.240 data transfer.

 

00:00:12.000 There are five parts to this lecture.

00:00:14.843 In this first part, I’ll review the

00:00:17.686 TCP transport protocol and its programming model,

00:00:20.429 and talk about client-server connection establishment.

00:00:22.780 In the next part, I’ll discuss the

00:00:25.622 implications of Transport Layer Security, TLS,

00:00:27.973 and the use of IPv6 on connection

00:00:30.716 establishment, and show how these changes affect

00:00:33.459 performance.

00:00:34.601 Following that, in part three, I’ll talk

00:00:37.444 about peer-to-peer connections and the impact of

00:00:40.187 network address translation.

00:00:41.362 Then, in the last two parts,

00:00:43.813 I’ll talk about some of the problems

00:00:46.556 caused by NATs, network address translation devices,

00:00:49.299 and outline how NAT traversal works.

 

00:00:52.000 To begin, I’ll briefly review the Transmission

00:00:55.091 Control Protocol, TCP. I’ll talk about the

00:00:58.082 purpose of TCP, then review the TCP

00:01:01.073 segment format, and the service model TCP

00:01:04.064 offers to applications. Then, I’ll discuss how

00:01:07.055 TCP connections are written using the Berkeley

00:01:10.045 sockets API.

 

00:01:12.000 TCP is currently the most widely used

00:01:14.944 transport protocol in the Internet.

00:01:16.976 A TCP connection provides a reliable,

00:01:19.514 ordered, byte stream delivery service that runs

00:01:22.359 on the best-effort IP network. Once a

00:01:25.203 TCP connection is established, an application can

00:01:28.047 write a sequence of bytes into the

00:01:30.892 socket, representing the connection, and TCP will

00:01:33.736 deliver those bytes to the receiver,

00:01:36.174 reliably, and in the correct order.

00:01:38.612 If any of the IP packets containing

00:01:41.557 TCP segments are lost, the TCP stack

00:01:44.401 in the operating system will notice this,

00:01:47.245 and automatically retransmit the missing segments.

00:01:49.683 Similarly, if any of the IP packets

00:01:52.628 are delayed and arrive out of order,

00:01:55.472 the TCP stack will put the data

00:01:58.317 back into the correct order before delivering

00:02:01.161 it to the application.

00:02:02.786 Finally, TCP will adapt the speed at

00:02:05.731 which it sends the data to match

00:02:08.575 the available network capacity. If the network

00:02:11.420 is busy, TCP will slow down the

00:02:14.264 rate at which it sends data,

00:02:16.702 to fairly share the capacity between transmissions.

00:02:19.546 Similarly, if the network becomes idle,

00:02:21.984 TCP connections will speed up to use

00:02:24.829 the spare capacity. This process is known

00:02:27.673 as congestion control, and TCP implements sophisticated

00:02:30.518 congestion control algorithms.

00:02:31.737 We’ll talk more about TCP congestion control

00:02:34.681 in Lecture 6.

 

00:02:36.000 Applications using TCP are unaware of retransmissions,

00:02:38.636 reordering, and congestion control. They just see

00:02:41.271 a socket, into which they write a

00:02:43.907 stream of bytes. Those bytes are then

00:02:46.542 delivered reliably to the receiver.

00:02:48.425 Internally, those bytes are split into TCP

00:02:51.161 segments. Each segment has a header added

00:02:53.796 to it, to identify the data.

00:02:56.055 The segment is placed inside the data

00:02:58.691 part of an IP packet. That IP

00:03:01.327 packet is, in turn, put inside the

00:03:03.962 data part of a link layer frame,

00:03:06.598 and sent across the network.

00:03:08.480 The IP layer just sees a sequence

00:03:11.216 of TCP segments that it must deliver,

00:03:13.852 and is unaware of their contents.

00:03:16.111 Equally, TCP gives data segments to the

00:03:18.746 IP layer to deliver, and is unaware

00:03:21.382 whether the underlying network is Ethernet,

00:03:23.641 WiFi, optical fibre, or something else.

 

00:03:27.000 The diagram on the slide shows the

00:03:30.075 format of a TCP segment header,

00:03:32.625 inside an IPv4 packet.

00:03:34.325 Data is sent over the link in

00:03:37.400 the order shown, left-to-right, top-to-bottom, in the

00:03:40.375 payload part of a link layer frame.

00:03:43.350 The IP header is sent first,

00:03:45.900 then the TCP segment header, then the

00:03:48.875 TCP payload data.

00:03:50.150 Looking at the TCP segment header,

00:03:52.800 highlighted in green, we see that it

00:03:55.775 comprises a number of fields.

 

00:03:58.000 A TCP segment starts with the source

00:04:00.745 and destination port numbers. The source port

00:04:03.489 number identifies the socket that sent the

00:04:06.234 segment, while the destination port number identifies

00:04:08.979 the socket to which it should be

00:04:11.724 delivered.

00:04:12.866 When establishing a TCP connection, a TCP

00:04:15.711 server binds to a well-known port that

00:04:18.455 identifies the type of service it offers.

00:04:21.200 For example, web servers bind to port

00:04:23.945 80. This gives a known destination port

00:04:26.689 to which clients can connect.

 

00:04:28.000 Clients specify the destination port to which

00:04:30.642 they connect, but usually leave their source

00:04:33.284 port unspecified. The operating system then chooses

00:04:35.926 an unused source port for that connection.

00:04:38.568 All TCP segments sent from the client

00:04:41.210 to the server, as part of a

00:04:43.852 single connection, have the same source and

00:04:46.494 destination ports, and the responses come back

00:04:49.135 with those ports swapped. Each new connection

00:04:51.777 gets a new source port.

00:04:53.665 Following the port numbers in the TCP

00:04:56.406 segment header, come the sequence number and

00:04:59.048 the acknowledgement number. At the start of

00:05:01.690 a TCP connection, the sequence number is

00:05:04.332 set to a random initial value.

00:05:06.597 As data is sent, the TCP sequence

00:05:09.239 number inserted by the sender increases,

00:05:11.503 counting the number of bytes of data

00:05:14.145 being sent.

 

00:05:15.000 The acknowledgement number indicates the next byte

00:05:18.042 of data that’s expected by the receiver.

00:05:21.084 For example, if a TCP segment is

00:05:24.125 received with sequence number 4,000, and that

00:05:27.167 segment contains 100 bytes of data,

00:05:29.775 then the acknowledgement number will be 4,100.

00:05:32.816 This indicates that the next TCP segment

00:05:35.858 expected is that with sequence number 4,100.

 

00:05:39.000 If the acknowledgement number that comes back

00:05:41.543 is different from that expected, it’s a

00:05:44.085 sign that some of the packets have

00:05:46.628 been lost. TCP will then retransmit the

00:05:49.171 segments that were in the lost packets.

00:05:51.713 The data offset field indicates where the

00:05:54.356 payload data starts in the segment.

00:05:56.535 That is, it indicates the size of

00:05:59.078 any TCP options included in the packet.

00:06:01.621 The reserved bits are not used.

 

00:06:04.000 There are then six single-bit flags.

00:06:06.144 The URG bit indicates whether the urgent

00:06:08.645 pointer is valid. The ACK bit indicates

00:06:11.146 whether the acknowledgement number is valid.

00:06:13.290 The PSH bit indicates that this is

00:06:15.791 the last segment in a message,

00:06:17.935 and should be pushed up to the

00:06:20.436 application as soon as possible.

00:06:22.222 The SYN, synchronise, bit is set on

00:06:24.824 the first packet sent on a connection.

00:06:27.325 The FIN bit indicates that the connection

00:06:29.826 should be cleanly closed. And the RST

00:06:32.327 bit indicates that the connection will reset,

00:06:34.828 aborted, without cleanup.

 

00:06:36.000 The receive window size allows the receiver

00:06:38.644 to indicate how much buffer space it

00:06:41.289 has available to receive new data.

00:06:43.556 This allows the receiver tell the sender

00:06:46.200 to slow down, if it’s sending faster

00:06:48.844 than the receiver can process the data.

00:06:51.489 The checksum is used to detect corrupted

00:06:54.233 packets, that can then be retransmitted.

00:06:56.500 Finally, the Urgent Pointer allows TCP senders

00:06:59.244 to indicate that some data is to

00:07:01.889 be processed urgently by the receiver.

00:07:04.156 Unfortunately, experience has shown that the urgent

00:07:06.800 data mechanism in TCP is not usable

00:07:09.444 in practice, due to a combination of

00:07:12.089 an ambiguous specification and inconsistent implementation.

00:07:14.356 The fixed TCP segment header is followed

00:07:17.100 by TCP option headers, that allow TCP

00:07:19.744 to add new features and extensions,

00:07:22.011 and then the payload data.

 

00:07:25.000 TCP retransmits any data that was sent

00:07:27.660 in packets that are lost, and makes

00:07:30.220 sure that data is delivered to the

00:07:32.779 application in the order in which it

00:07:35.339 was originally sent. It also adapts the

00:07:37.899 speed at which it sends data to

00:07:40.459 match the available network capacity. As a

00:07:43.019 result, TCP provides a reliable, ordered,

00:07:45.213 byte stream delivery service.

00:07:46.676 A limitation of the TCP service model

00:07:49.336 is that message boundaries are not preserved.

00:07:51.895 That is, if an application writes,

00:07:54.090 for example, a block of 2,000 bytes

00:07:56.649 to a TCP connection, then TCP will

00:07:59.209 deliver those 2,000 bytes to the receiver,

00:08:01.769 reliably, and in the order they are

00:08:04.329 sent.

00:08:05.445 However, what TCP does not do,

00:08:07.739 is guarantee that those bytes are delivered

00:08:10.299 to the receiving application as a single

00:08:12.858 block of 2,000 bytes. That might happen.

00:08:15.418 Equally, they could be delivered to the

00:08:17.978 application as two blocks of 1,000 bytes.

00:08:20.538 Or as a block of 1,500 bytes,

00:08:23.098 followed by a block of 500 bytes.

00:08:25.658 Or as 2,000 single bytes. Or as

00:08:28.217 any other combination, provided the data is

00:08:30.777 delivered reliably and in the order sent.

00:08:33.337 This complicates the design of applications that

00:08:35.997 use TCP, since they have to parse

00:08:38.557 the data received from a TCP socket

00:08:41.117 to check if they’ve got the complete

00:08:43.676 message.

00:08:44.792 Despite this inconvenience, TCP is the right

00:08:47.452 choice for most applications. If you need

00:08:50.012 to deliver data reliably, and as fast

00:08:52.572 as possible, then use TCP.

 

00:08:54.000 TCP is a client-server protocol. Servers listen

00:08:57.126 for, and respond to, requests from clients.

00:09:00.153 The way you write code to use

00:09:03.279 a TCP connection depends on whether you’re

00:09:06.306 writing a client or a server.

 

00:09:09.000 On the server side, you begin by

00:09:12.188 creating a socket.

00:09:13.554 The first argument to the socket() call

00:09:16.842 is the constant PF_INET, from the sys/socket.h

00:09:20.030 header, if you want the server to

00:09:23.218 listen for connections on IPv4. Alternatively,

00:09:25.950 if you want the server to listen

00:09:29.138 for IPv6 connections, use PF_INET6.

00:09:31.415 The second argument will be the constant

00:09:34.703 SOCK_STREAM, to indicate that a TCP server

00:09:37.891 is wanted. The third argument is unused

00:09:41.078 as must be zero.

 

00:09:43.000 You then call the bind() function,

00:09:45.337 passing the file descriptor representing the newly

00:09:48.065 created socket as the first argument.

00:09:50.402 The other arguments to bind() specify the

00:09:53.229 port number on which the server should

00:09:55.956 listen for incoming connections.

00:09:57.515 The bind() function assigns the requested port

00:10:00.342 number to the socket.

 

00:10:02.000 You then call the listen() function.

00:10:04.957 This starts the server listening for incoming

00:10:08.407 connections.

 

00:10:09.000 Then, you call accept(), to indicate that

00:10:11.780 your server is ready to accept a

00:10:14.560 new connection. The accept() function doesn’t return

00:10:17.340 until a client connects to the server.

00:10:20.120 This could potentially be a long wait.

 

00:10:23.000 Meanwhile, the client application creates its own

00:10:26.013 socket. This is done using the socket()

00:10:29.026 function, in exactly the same way as

00:10:32.039 the server.

 

00:10:33.000 The client then calls connect(), passing the

00:10:35.487 file descriptor for its newly created socket

00:10:37.973 as the first argument. The subsequent arguments

00:10:40.460 contain the IP address of the server,

00:10:42.946 and the port number on which the

00:10:45.433 server is listening.

00:10:46.499 The connect() call makes TCP establish a

00:10:49.085 connection from the client to the server.

00:10:51.572 When it returns, either the connection has

00:10:54.058 been successfully established, or the server is

00:10:56.545 unreachable.

 

00:10:57.000 When the connection request reaches the server,

00:11:00.286 the accept() call completes. The return value

00:11:03.572 is a new file descriptor, representing the

00:11:06.858 newly established connection. The original file descriptor,

00:11:10.144 that was listening for incoming connections,

00:11:12.961 remains unchanged.

 

00:11:14.000 The client and server can now call

00:11:16.414 send() and recv(), to send and receive

00:11:18.829 data over the connection. They can send

00:11:21.243 and receive as much, or as little,

00:11:23.658 data as they want, and TCP places

00:11:26.072 no restrictions on the order in which

00:11:28.487 client and server send.

00:11:29.867 Remember to use the file descriptor representing

00:11:32.381 the accepted connection, not the file descriptor

00:11:34.796 representing the listening socket, when writing the

00:11:37.210 server code.

 

00:11:38.000 Finally, when they’ve finished, client and server

00:11:41.349 call the close() function, to cleanly shut

00:11:44.697 down the connection.

00:11:46.132 Once the client has closed the connection,

00:11:49.581 it’s done.

00:11:50.538 A server can repeatedly accept new connections

00:11:53.986 from the listening socket.

 

00:11:57.000 How does connection establishment actually work? And

00:11:59.541 what are the factors that affect how

00:12:01.982 quickly connections can be established?

00:12:03.726 In the following, I’ll talk in detail

00:12:06.267 about how client-server connection establishment works for

00:12:08.708 TCP, and what factors limit performance.

 

00:12:12.000 TCP-based applications usually work in a a

00:12:14.900 client-server manner. The server listens for connections

00:12:17.700 on a well-known port, and the client

00:12:20.500 connects to the server, sends a request,

00:12:23.300 and receives a response.

 

00:12:25.000 TCP can also, in principle, be used

00:12:27.656 in a peer-to-peer manner.

00:12:29.174 If two devices create TCP sockets,

00:12:31.550 bind to known ports, and simultaneously attempt

00:12:34.206 to connect() to each other, then TCP

00:12:36.862 will able to create a connection,

00:12:39.138 provided there are no firewalls or NAT

00:12:41.794 devices blocking traffic. This is known as

00:12:44.450 simultaneous open.

00:12:45.209 TCP simultaneous open can work, but isn’t

00:12:47.965 especially useful, since it requires both peers

00:12:50.621 to try to connect at the same

00:12:53.276 time. It’s usually better to work in

00:12:55.932 client-server mode, since the server can wait

00:12:58.588 for clients and the client and server

00:13:01.244 don’t need to synchronise when to connect.

 

00:13:05.000 How does client-server TCP connection establishment actually

00:13:09.300 work?

 

00:13:10.000 First, the server creates a TCP socket,

00:13:13.365 binds it to a port, tells it

00:13:16.729 to listen for connections, and calls accept().

00:13:20.094 At that point, the server blocks,

00:13:22.977 waiting for a connection.

 

00:13:25.000 The client creates a TCP socket and

00:13:27.726 calls connect().

00:13:28.505 This triggers the TCP connection setup handshake,

00:13:31.332 and causes the client to send a

00:13:34.058 TCP segment to the server. This segment

00:13:36.784 will have the SYN (“synchronise”) bit set

00:13:39.511 in its TCP segment header, to indicate

00:13:42.237 that it’s the first packet of the

00:13:44.963 connection.

00:13:46.103 It will also include a randomly chosen

00:13:48.929 sequence number. This is the client’s initial

00:13:51.655 sequence number.

00:13:52.434 The initial segment does not include any

00:13:55.261 data.

 

00:13:55.000 When this initial segment arrives at the

00:13:57.548 server, the server will send a TCP

00:14:00.097 segment in response.

00:14:01.189 This segment will also have the SYN

00:14:03.837 bit set, because it’s the first segment

00:14:06.386 sent by the server on the connection.

00:14:08.934 It will also include a randomly chosen

00:14:11.483 initial sequence number. This is the server’s

00:14:14.031 initial sequence number.

00:14:15.123 The segment will also have the ACK

00:14:17.772 bit set, because it’s acknowledging the initial

00:14:20.320 segment sent from the client. TCP acknowledgements

00:14:22.869 report the next sequence number expected,

00:14:25.053 and by convention a SYN segment will

00:14:27.602 consume one sequence number. Accordingly, the acknowledgement

00:14:30.150 number in the TCP segment header will

00:14:32.698 be the client’s initial sequence number plus

00:14:35.247 one.

00:14:36.361 Since this segment has both the SYN

00:14:39.009 and ACK bits set, it’s known as

00:14:41.558 a SYN-ACK packet.

 

00:14:42.000 When the SYN-ACK packet arrives at the

00:14:44.606 client, the client acknowledges its receipt back

00:14:47.211 to the server. The TCP segment it

00:14:49.817 generates to do this will have its

00:14:52.422 ACK bit set to one, to indicate

00:14:55.028 that its acknowledgement number is valid,

00:14:57.261 and the acknowledgement number will be the

00:14:59.867 server’s initial sequence number plus one.

00:15:02.100 The SYN bit is not set on

00:15:04.806 this packet, since it’s not the first

00:15:07.411 packet sent from the client to the

00:15:10.017 server.

00:15:11.139 The sequence number in the TCP segment

00:15:13.844 header will equal the client’s initial sequence

00:15:16.450 number plus one, since the SYN packet

00:15:19.056 consumes one sequence number. This packet also

00:15:21.661 doesn’t include any data, since it’s sent

00:15:24.267 before the connect() call completes.

00:15:26.128 Once this packet has been sent,

00:15:28.461 the client considers the connection established,

00:15:30.694 and the connect() function returns. The client

00:15:33.300 can now send or receive data on

00:15:35.906 the connection.

 

00:15:36.000 Once this final ACK packet arrives at

00:15:38.426 the server, the three-way handshake is complete.

00:15:40.851 At this point the accept() function completes,

00:15:43.277 returning a file descriptor the server can

00:15:45.702 use to access the new connection.

00:15:47.781 The server now considers the connection to

00:15:50.207 be open.

 

00:15:52.000 Once the three-way handshake has completed,

00:15:54.570 the client and server can send and

00:15:57.452 receive data over the connection.

00:15:59.510 The typical case is that the client

00:16:02.492 sends data to the server immediately after

00:16:05.373 it connects, and the server responds with

00:16:08.255 the requested data. There’s no requirement that

00:16:11.137 the client sends first, though, or that

00:16:14.018 client and server alternate in sending data.

 

00:16:17.000 The slide shows an example where the

00:16:19.198 client sends a request comprising a single

00:16:21.396 segment’s worth of data to the server.

00:16:23.594 The server then responds by sending a

00:16:25.792 larger response back, including the acknowledgement for

00:16:27.990 the request on the first segment of

00:16:30.188 the response. Finally, we see the client

00:16:32.385 acknowledge receipt of the segments that comprise

00:16:34.583 the response.

00:16:35.211 This is the typical pattern when a

00:16:37.509 browser fetches a web page from a

00:16:39.707 web server.

00:16:40.335 What’s interesting, is that if we look

00:16:42.633 at the time from when the client

00:16:44.831 calls connect(), until the time it receives

00:16:47.029 the last of data segment from the

00:16:49.227 server, a significant part of that time

00:16:51.425 is taken up by the connection setup

00:16:53.623 handshake.

00:16:54.687 It takes a certain amount of time,

00:16:56.985 known as the round trip time,

00:16:58.869 to send a minimal sized request to

00:17:01.067 the server and get a minimal sized

00:17:03.265 response. Larger requests and responses add to

00:17:05.462 this, based on the time to send

00:17:07.660 the additional data down the link,

00:17:09.544 known as the serialisation time for the

00:17:11.742 data. But, if the amount of data

00:17:13.940 being requested from the server is small,

00:17:16.138 it’s often the round trip time that

00:17:18.336 dominates.

 

00:17:19.000 For example, let’s assume a browser is

00:17:22.067 requesting a simple web page from a

00:17:25.033 web server using HTTP running over TCP.

00:17:28.000 That web page comprises a single HTML

00:17:30.967 file, a CSS style sheet, and an

00:17:33.933 image. The HTML and CSS files are

00:17:36.900 on one server, while the image is

00:17:39.867 located on a different server.

00:17:41.986 How long does it take to retrieve

00:17:45.052 that page?

 

00:17:46.000 Well, the client initially connects to the

00:17:48.380 server where the HTML file is located.

00:17:50.760 This takes one round-trip time for the

00:17:53.140 SYN and SYN-ACK packets to be exchanged,

00:17:55.520 and for the connect() call to complete.

 

00:17:58.000 As soon as the connect() completes,

00:18:00.074 the client sends the request for the

00:18:02.493 HTML. It takes another round-trip for the

00:18:04.913 request to reach the server and the

00:18:07.333 first part of the response to come

00:18:09.752 back, followed by the serialisation time for

00:18:12.172 the rest of the response.

 

00:18:14.000 When it’s received the HTML, the client

00:18:16.434 knows that it needs to retrieve the

00:18:18.868 CSS file and the image.

00:18:20.607 It reuses the existing connection to the

00:18:23.141 first server to retrieve the CSS file.

00:18:25.576 This takes an additional round trip,

00:18:27.662 plus the serialisation time of the CSS

00:18:30.096 data.

00:18:31.194 In parallel to this, it opens a

00:18:33.728 TCP connection to the second server,

00:18:35.815 sends a request for the image,

00:18:37.901 and downloads the response. This takes two

00:18:40.336 round trips, plus the time to send

00:18:42.770 the image data.

00:18:43.813 Whichever of these takes the longest,

00:18:46.000 plus the amount of time to make

00:18:48.434 the initial connection and fetch the HTML,

00:18:50.868 determines the total time to download the

00:18:53.302 page.

 

00:18:53.000 The round trip time depends on the

00:18:55.348 distance from the client to the server.

00:18:57.695 The serialisation time depends on the available

00:19:00.143 capacity of the network. For example,

00:19:02.155 if the image is 1 megabyte,

00:19:04.168 8 megabits, in size, and the available

00:19:06.515 bandwidth of the link is 2 megabits

00:19:08.863 per second, then the image will take

00:19:11.211 4 seconds to download, in addition to

00:19:13.558 the round trip time.

 

00:19:16.000 It can be seen that the total

00:19:18.671 download time depends on both the round

00:19:21.242 trip time, the available bandwidth, and the

00:19:23.813 size of the data being downloaded.

00:19:26.017 What’s a typical round trip time?

00:19:28.321 This depends on the distance between the

00:19:30.992 client and server, and on the amount

00:19:33.563 of network congestion. The table on the

00:19:36.134 right gives some typical values, measured from

00:19:38.705 a laptop on my home network to

00:19:41.276 various destinations.

00:19:42.011 There’s a lot of variation. In the

00:19:44.682 best case, it takes around 33ms to

00:19:47.253 get a response from a server in

00:19:49.824 the UK, around 100ms to get a

00:19:52.395 response from a server on the East

00:19:54.966 coast of the US, around 165ms from

00:19:57.537 a server in California, and around 300ms

00:20:00.108 from Australia. Worst case, when there’s other

00:20:02.679 traffic on the network, is considerably higher.

00:20:05.250 This means that a request sent to

00:20:07.921 a server in New York takes at

00:20:10.492 least 1/10th of a second, irrespective of

00:20:13.064 how much data is requested.

 

00:20:15.000 What about available bandwidth?

00:20:16.580 Well, ADSL typically gets around 25 megabits

00:20:19.445 per second.

00:20:20.235 VDSL, often known as fibre to the

00:20:23.100 kerb, where the connection runs over your

00:20:25.865 home phone line to a cabinet in

00:20:28.630 your street, then over fibre to the

00:20:31.395 exchange and beyond, typically gets around 50

00:20:34.160 megabits per second.

00:20:35.345 And fibre to the premises, where the

00:20:38.210 optical fibre runs direct into your home,

00:20:40.975 can transmit several hundred megabits per second.

00:20:43.740 4G wireless is highly variable, depending on

00:20:46.605 the options enabled by your provider and

00:20:49.370 the reception quality, but somewhere in the

00:20:52.135 15-30 megabits per second range is typical.

 

00:20:56.000 What does this mean in practice?

00:20:58.078 Let’s take the example of a single

00:21:00.486 web page we used before, comprising HTML,

00:21:02.794 a CSS style sheet, and a single

00:21:05.102 image, and plug in some typical numbers

00:21:07.410 for the file sizes, as shown on

00:21:09.718 the slide. Let’s also assume that the

00:21:12.025 round trip time is the same for

00:21:14.333 both servers, to make the numbers easier.

00:21:16.641 The table then plots the total time

00:21:19.049 it would take to download that simple

00:21:21.357 web page, given different values for bandwidth

00:21:23.665 and the round trip time to the

00:21:25.973 servers.

00:21:27.052 The slowest case is the bottom left

00:21:29.460 of the table, where it would take

00:21:31.768 45.1 seconds to download the page,

00:21:33.746 assuming a 1 megabit per second link

00:21:36.054 to a server with a 300ms round

00:21:38.362 trip time. This models a slow connection

00:21:40.670 to a server in Australia.

00:21:42.318 The fastest is the top right of

00:21:44.726 the table, where it takes 0.04 seconds

00:21:47.034 to download the page from a server

00:21:49.342 located 1ms away on a gigabit link.

 

00:21:51.000 What’s interesting is how the download time

00:21:53.826 varies as the link speed improves.

00:21:56.248 If we look at the top row,

00:21:59.174 with 1ms round trip time, we see

00:22:02.000 that if we increase the bandwidth by

00:22:04.826 a factor of ten, from 100Mbps to

00:22:07.652 1Gbps, the time taken to download the

00:22:10.478 page goes down by a factor of

00:22:13.304 ten, a 90% reduction. The link is

00:22:16.130 ten times faster, and the page downloads

00:22:18.956 ten time faster.

00:22:20.167 If we look instead at the bottom

00:22:23.093 row, with 300ms round trip time,

00:22:25.515 increasing the link speed from 100Mbps to

00:22:28.341 1Gbps gives only a 22% reduction in

00:22:31.167 download time.

00:22:31.974 Other links are somewhere in the middle.

 

00:22:35.000 Internet service providers like to advertise their

00:22:37.180 services based on the link speed.

00:22:39.049 They proudly announce that they can now

00:22:41.229 provide gigabit links, and that these are

00:22:43.409 now more than ten times faster than

00:22:45.589 before!

 

00:22:46.000 And this is true.

00:22:47.593 But, in terms of actual download time,

00:22:50.481 unless you’re downloading very large files,

00:22:52.870 the round trip time is often the

00:22:55.658 limiting factor. The download time, for typical

00:22:58.446 pages, may only improve by a factor

00:23:01.233 of two if the link gets 10x

00:23:04.021 faster.

00:23:05.169 Is it still worth paying extra for

00:23:08.057 that faster Internet connection?

 

00:23:10.000 What does this mean for protocol design?

00:23:12.799 The example shows an HTTP/1.1 exchange.

00:23:15.212 Once the connection has been opened,

00:23:17.525 the client sends the data shown in

00:23:20.223 blue, prefixed with the letter “C:”,

00:23:22.536 to the server. The server then responds

00:23:25.235 with the data shown in red,

00:23:27.548 prefixed with the letter “S:”, comprising some

00:23:30.246 header information and the requested page.

00:23:32.559 Everything is completed in a single round

00:23:35.358 trip. Request. Then response.

 

00:23:38.000 Compare that with this example, showing the

00:23:40.648 Simple Mail Transfer Protocol, SMTP, used to

00:23:43.195 send email.

00:23:43.923 As with the previous slide, data sent

00:23:46.571 from client to server is shown in

00:23:49.119 blue and prefixed with the letter “C:”,

00:23:51.667 and that sent from the server to

00:23:54.214 the client is in red, prefixed with

00:23:56.762 the letter “S:”.

00:23:57.854 We see that the protocol is very

00:24:00.502 chatty.

00:24:01.616 Once the connection is established, after the

00:24:04.264 SYN, SYN-ACK, and ACK, the server sends

00:24:06.811 an initial greeting. Establishing the connection and

00:24:09.359 sending this initial greeting takes two round

00:24:11.907 trips.

00:24:13.021 The client then sends HELO, and waits

00:24:15.668 for the go ahead from the server.

00:24:18.216 This takes one more round trip.

 

00:24:19.000 The client then sends the from address,

00:24:21.478 and waits for the server. One more

00:24:23.956 round trip.

00:24:24.664 The client then send the recipients,

00:24:26.888 and waits for the server. One more

00:24:29.366 round trip.

00:24:30.074 The client then says it’d like to

00:24:32.652 send data now, and waits for the

00:24:35.130 server. One more round trip.

 

00:24:37.000 Then, finally the client gets to send

00:24:40.019 the data, and once it’s confirmed that

00:24:43.037 the data was received, sends QUIT,

00:24:45.625 waits, then closes the connection.

00:24:47.781 The whole exchange takes eight round trips.

 

00:24:51.000 Is this necessary or efficient?

00:24:52.873 No!

00:24:54.098 If the protocol were designed differently,

00:24:56.446 all the data could be sent at

00:24:59.068 once, as soon as the connection was

00:25:01.691 opened, and the server could respond with

00:25:04.313 an okay or an error. The eight

00:25:06.936 round trips could be reduced to two:

00:25:09.558 one to establish the connection, one to

00:25:12.181 send the message and get confirmation from

00:25:14.804 the server.

00:25:15.553 This is why email is slow to

00:25:18.275 send.

 

00:25:19.000 TCP establishes connections using a three-way handshake.

00:25:22.150 SYN, SYN-ACK, ACK.

00:25:23.500 The time to establish a connection depends

00:25:26.750 on round trip time and the bandwidth.

 

00:25:30.000 Links are now fast enough that the

00:25:32.415 round trip time is generally the dominant

00:25:34.830 factor, even for relatively slow links.

 

00:25:37.000 The best way to improve application performance

00:25:39.366 is usually to reduce the number of

00:25:41.732 messages that need to be sent from

00:25:44.098 client to server. That is, to reduce

00:25:46.464 the number of round trips. Unless you’re

00:25:48.830 sending a lot of data, increasing the

00:25:51.196 bandwidth generally makes very little difference to

00:25:53.562 performance.

Part 2: Impact of TLS and IPv6 on Connection Establishment

The 2nd part of the lecture discusses the impact of TLS and IPv6 on TCP connection establishment. It shows how the use of TLS, to secure connections, increases the connection establishment latency. And it discusses the "happy eyeballs" technique for connection racing, to reduce connection establishment delays, in dual stack IPv4 and IPv6 networks.

00:00:00.000 In the previous part, I discussed TCP

00:00:03.350 connection establishment, and highlighted that the round-trip

00:00:05.700 time is often the limiting factor for

00:00:08.050 performance.

00:00:09.136 In the following, I want to discuss

00:00:11.586 the performance implications of adding transport layer

00:00:13.936 security to TCP connections, and how to

00:00:16.286 achieve good performance when the destination is

00:00:18.636 a dual-stack IPv4 and IPv6 host.

 

00:00:21.000 In the previous part, I showed how

00:00:23.583 the network round trip time can be

00:00:26.066 the limiting factor in performance. This is

00:00:28.549 because every TCP connection needs at least

00:00:31.033 two round trip times: one to establish

00:00:33.516 the connection, and one for the client

00:00:35.999 to send a request and receive a

00:00:38.482 response from the server.

00:00:39.901 I also showed how the protocol running

00:00:42.484 over TCP can make a significant difference

00:00:44.967 to performance, with the examples of HTTP,

00:00:47.451 which sends a request and receives a

00:00:49.934 response in a single round trip,

00:00:52.062 and SMTP, which makes multiple unnecessary round

00:00:54.545 trips.

 

00:00:55.000 One of the important protocols that runs

00:00:58.127 over TCP is the transport layer security

00:01:01.255 protocol, TLS.

00:01:02.148 TLS provides security for a TCP connection.

00:01:05.376 That is, it allows the client and

00:01:08.503 server to agree encryption and authentication keys

00:01:11.631 to make sure that the data sent

00:01:14.758 over that TCP connection is confidential and

00:01:17.885 protected from modification in transit.

00:01:20.119 TLS is essential to Internet security.

 

00:01:23.000 When you retrieve a secure web page

00:01:25.688 using HTTPS, it first opens a TCP

00:01:28.375 connection to the server. Then, it runs

00:01:31.063 TLS to enable security for that connection.

00:01:33.751 Then, it asks to retrieve the web

00:01:36.439 page.

00:01:37.573 Depending on the version of TLS used,

00:01:40.360 this adds additional time to the connection.

00:01:43.048 With the latest version of TLS,

00:01:45.452 TLS v1.3, it takes one additional round

00:01:48.140 trip to agree the encryption and authentication

00:01:50.827 keys. That is, after the TCP connection

00:01:53.515 has been established, via the SYN -

00:01:56.203 SYN-ACK - ACK handshake, then the client

00:01:58.891 and server need an additional round trip

00:02:01.578 to enable TLS, before they can request

00:02:04.266 data.

 

00:02:04.000 The TLS handshake is in three parts.

00:02:06.800 First, the client sends a TLS ClientHello

00:02:09.600 message to the server to propose security

00:02:12.400 parameters. Then, the server responds with a

00:02:15.200 TLS ServerHello, containing its keys and other

00:02:18.000 security parameters. Finally, assuming there’s a match,

00:02:20.800 the client responds with a TLS Finished

00:02:23.600 message to set the encryption parameters.

00:02:26.000 The client then immediately follows this by

00:02:28.800 sending the application data, such as an

00:02:31.600 HTTP GET request, without waiting for a

00:02:34.400 response.

00:02:35.550 This adds one additional round trip time,

00:02:38.450 in most cases.

 

00:02:39.000 Older versions of TLS take longer.

00:02:41.257 TLS v1.2, for example, takes at least

00:02:43.890 two round trips to negotiate a secure

00:02:46.524 connection.

 

00:02:48.000 What impact does the additional round trip

00:02:50.838 due to TLS have on performance?

00:02:53.271 Well, let’s look again at the simple

00:02:56.210 web page download examples from the previous

00:02:59.048 part.

00:03:00.204 When the round-trip time is negligible,

00:03:02.737 as on the top row with 1ms

00:03:05.575 round trip time, performance is unchanged.

00:03:08.008 As we go down the table,

00:03:10.541 though, performance gets worse. With 100ms round

00:03:13.379 trip time, both the overall performance and

00:03:16.218 the benefit of increasing the link speed

00:03:19.056 go down. The download time for the

00:03:21.895 page on a gigabit link is increased

00:03:24.733 by 45%, from 0.44 to 0.64 seconds,

00:03:27.571 compared to a connection without TLS.

00:03:30.004 And the benefit of going from a

00:03:32.843 100 megabit link to a gigabit link

00:03:35.681 is only 36%, rather than 45% without

00:03:38.520 TLS.

00:03:39.675 With 300ms round trip time the behaviour

00:03:42.613 is even worse. Total download time increases

00:03:45.452 by 48% compared to the non-TLS case,

00:03:48.290 and there’s only a 22% reduction in

00:03:51.129 download time when going from a 100

00:03:53.967 megabit link to a gigabit link.

 

00:03:56.000 This is not to say that TLS

00:03:58.571 is bad! Far from it – security

00:04:01.142 is essential.

00:04:01.877 Rather, it further highlights that the number

00:04:04.548 of round trips that a connection must

00:04:07.119 perform, between client and server, is often

00:04:09.690 the limiting factor in performance.

00:04:11.526 Applications that have good performance will try

00:04:14.197 to reduce the number of TCP connections

00:04:16.768 that they establish, since each connection takes

00:04:19.339 time to establish. They also try to

00:04:21.910 limit the number of request-response exchanges,

00:04:24.114 each taking a round trip, they make

00:04:26.685 on each connection.

00:04:27.786 TLS v1.3, standardised in 2018, was a

00:04:30.457 big win here, because it reduces the

00:04:33.028 number of round trips needed to enable

00:04:35.599 security from two, down to one.

00:04:37.803 When used with TCP, this gives the

00:04:40.374 best possible performance: one round trip to

00:04:42.945 establish the TCP connection, and one to

00:04:45.516 negotiate the security, before the data can

00:04:48.087 be sent.

00:04:48.822 We’ll talk more about TLS and how

00:04:51.493 to improve the performance of secure connections

00:04:54.064 in lectures 3 and 4.

 

00:04:57.000 The other factor affecting TCP connection performance

00:05:00.253 is the ongoing transition to IPv6.

00:05:03.041 This transition means that we currently have

00:05:06.394 two Internets: the IPv4 Internet and the

00:05:09.647 IPv6 Internet.

00:05:10.576 Some hosts can only connect using IPv4.

00:05:13.929 Some hosts can only connect using IPv6.

00:05:17.182 And some hosts have both types of

00:05:20.435 address.

 

00:05:21.000 Similarly, some network links can only carry

00:05:24.168 IPv4 traffic, some only IPv6, and some

00:05:27.336 links can carry both types of traffic.

00:05:30.503 And some firewalls, or other middleboxes,

00:05:33.319 block IPv4, some block IPv6, and some

00:05:36.486 block both types of traffic.

00:05:38.749 Importantly, the IPv6 network is not a

00:05:42.017 subset of the IPv4 network. It’s a

00:05:45.185 separate Internet, that overlaps in places.

 

00:05:49.000 Given that some hosts will be reachable

00:05:51.984 over IPv4 but not IPv6, and vice

00:05:54.868 versa, how do you establish connections during

00:05:57.751 the transition?

00:05:58.575 Well, given a hostname, you perform a

00:06:01.559 DNS lookup to find the IP addresses

00:06:04.443 for that host using the getaddinfo() call.

00:06:07.327 This returns a list of possible IP

00:06:10.211 addresses for the host, including both IPv4

00:06:13.094 and IPv6 addresses.

00:06:14.330 The simple approach is a loop,

00:06:16.902 trying each address in turn, until one

00:06:19.786 successfully connects.

00:06:20.610 This works, but can be very slow.

00:06:23.594 In the example on the slide,

00:06:26.065 Netflix has 16 possible IP addresses,

00:06:28.537 eight IPv6 and eight IPv4, and lists

00:06:31.421 the IPv6 addresses first in its DNS

00:06:34.305 response. If you have only IPv4 connectivity,

00:06:37.189 it may take a long time to

00:06:40.073 try, and fail, to connect to eight

00:06:42.956 different IPv6 addresses before you get to

00:06:45.840 an IPv4 address that works.

 

00:06:49.000 To get good performance, applications use a

00:06:52.245 technique known as “Happy Eyeballs”.

00:06:54.492 This involves making two separate DNS lookups,

00:06:57.738 in parallel, one asking for only IPv4

00:07:00.883 addresses and one for only IPv6 addresses.

00:07:04.029 Starting with whichever of these DNS lookups

00:07:07.274 completes first, the client makes a connection

00:07:10.419 to the first address returned by the

00:07:13.565 server. If that hasn’t succeeded within 100ms,

00:07:16.710 it starts another connection request to the

00:07:19.856 next possible address, alternating between IPv4 and

00:07:23.001 IPv6 addresses.

 

00:07:24.000 The different connection requests proceed in parallel,

00:07:27.355 until once eventually succeeds. That first successful

00:07:30.710 connection is used, whether over IPv4 or

00:07:34.066 IPv6, and the other connection requests are

00:07:37.421 cancelled.

 

00:07:38.000 The happy eyeballs technique tries to balance

00:07:40.897 the time taken to connect vs.

00:07:43.381 the network overload of trying many possible

00:07:46.278 connections at once in parallel. It adds

00:07:49.175 complexity to the connection setup, to achieve

00:07:52.072 good performance.

 

00:07:54.000 The two factors affecting TCP performance are

00:07:56.900 bandwidth and latency. In many cases,

00:07:59.300 the latency, the round trip time,

00:08:01.700 dominates.

00:08:02.850 The are five ways in which applications

00:08:05.750 using TCP improve their performance.

00:08:07.750 The first is that a client should

00:08:10.650 use something like happy eyeballs, overlapping connection

00:08:13.450 requests if the server, if the server

00:08:16.250 has more than one address. This is

00:08:19.050 more complicated to implement than trying to

00:08:21.850 connect to each different address in turn,

00:08:24.650 but connects a lot faster.

 

00:08:26.000 The second way to improve TCP performance

00:08:28.572 is to reduce the number of TCP

00:08:31.143 connections made. Each connection takes time to

00:08:33.715 establish. If you can make a single

00:08:36.287 TCP connection and reuse it for multiple

00:08:38.859 requests, that’s faster than making a new

00:08:41.430 connection for each request.

 

00:08:43.000 Third, if you reduce the number of

00:08:45.598 request-response exchanges made over each connection,

00:08:47.824 you reduce the impact of the round

00:08:50.422 trip latency.

00:08:51.164 All these are possible for any application,

00:08:53.862 by using TCP connections effectively.

00:08:55.718 There are also two more radical changes

00:08:58.416 that can be made.

 

00:09:00.000 The first is to overlap and TCP

00:09:02.540 and TLS connection setup handshakes, by sending

00:09:05.080 the security parameters along with the initial

00:09:07.620 connection request, so that both the connection

00:09:10.160 setup and security parameters can be negotiated

00:09:12.701 in a single round trip. This isn’t

00:09:15.241 possible with TCP, but the QUIC transport

00:09:17.781 protocol, that we’ll discuss in lecture 4,

00:09:20.321 does allow this.

00:09:21.410 Finally, one can always improve performance by

00:09:24.050 reducing the round trip latency. This latency

00:09:26.590 depends on two things: the speed at

00:09:29.130 which the signal propagates down the link,

00:09:31.670 and the amount of other traffic.

00:09:33.847 Since signals travel down electrical cables and

00:09:36.487 optical fibres at the speed of light,

00:09:39.028 there’s little that can be done to

00:09:41.568 increase the propagation speed, although low earth

00:09:44.108 orbit satellites can help, we’ll discuss in

00:09:46.648 lecture 6.

00:09:47.374 Reducing the amount of other traffic queued

00:09:50.014 up at intermediate links is a possibility

00:09:52.554 though, and this can be affected by

00:09:55.094 the choice of TCP congestion control algorithm.

00:09:57.634 We’ll talk about this in lectures 5

00:10:00.174 and 6.

 

00:10:02.000 To summarise, one of the limiting factors

00:10:04.886 with TCP performance is the round trip

00:10:07.671 latency.

00:10:08.819 The use of TLS is essential to

00:10:11.705 improve security, but comes at the expense

00:10:14.490 of an additional round trip that slows

00:10:17.276 down connection establishment. This is solved by

00:10:20.061 the upcoming QUIC transport protocol, that we’ll

00:10:22.847 discuss in lecture 4.

00:10:24.439 Similarly, the ongoing migration to IPv6 means

00:10:27.324 that servers often have both IPv4 and

00:10:30.110 IPv6 addresses, and it’s not clear which

00:10:32.895 of these are reachable. Clients must try

00:10:35.681 to establish multiple connections in parallel,

00:10:38.069 using the happy eyeballs technique, to get

00:10:40.854 good performance.

Part 3: Peer-to-peer Connections

The 3rd part starts to discuss peer-to-peer connections. It talks about how the use of Network Address Translation (NAT) affects addressing and connection establishment, and why it complicates creating peer-to-peer applications.

00:00:00.000 In this part, I’ll start to talk

00:00:03.514 about peer-to-peer connections, network address translation,

00:00:05.668 and how these affect Internet addressing and

00:00:08.182 connection establishment.

 

00:00:10.000 The Internet was designed as a peer-to-peer

00:00:12.361 network, and makes no distinction between clients

00:00:14.722 and servers at the IP layer.

00:00:16.746 In principle, it should be possible to

00:00:19.207 run a TCP server, or a UDP-

00:00:21.568 or TCP-based peer-to-peer application, on any host

00:00:23.929 on the network. As long as the

00:00:26.290 clients have some way of finding the

00:00:28.651 server’s IP address, and knowing what port

00:00:31.012 number it’s using, and as long as

00:00:33.373 any firewall pinholes are opened, then it

00:00:35.734 shouldn’t matter whether a server is located

00:00:38.095 in someone’s home or in a data

00:00:40.456 centre.

00:00:41.543 A server in a data centre is

00:00:44.004 likely to have better performance, of course,

00:00:46.365 because it’s probably got a faster connection

00:00:48.726 to the rest of the network.

 

00:00:50.000 It’s also likely to be more robust,

00:00:52.665 because the data centre will have redundant

00:00:55.331 power and network links, air conditioning,

00:00:57.615 and professional system administrators. But, at the

00:01:00.281 protocol level, there shouldn’t be a difference.

00:01:02.946 In practise, this is not the case.

00:01:05.712 It’s difficult to run a server on

00:01:08.377 a host connected to most residential broadband

00:01:11.042 connections, and it’s difficult to make peer-to-peer

00:01:13.708 connections work.

00:01:14.469 The reason for this is the widespread

00:01:17.235 use of network address translation – NAT.

 

00:01:21.000 What is network address translation?

00:01:22.977 NAT is the process by which several

00:01:25.844 devices can share a single public IP

00:01:28.612 address. It allows several hosts to form

00:01:31.380 a private internal network, with IP addresses

00:01:34.147 assigned from a special-use range. One device

00:01:36.915 – the network address translator, the NAT

00:01:39.682 – is connected to both the private

00:01:42.450 network and to the Internet, and can

00:01:45.218 forward packets between the two networks.

00:01:47.590 As it does so, it rewrites,

00:01:49.962 translates, the IP addresses, and the TCP

00:01:52.730 and UDP port numbers, so all the

00:01:55.497 packets appear to come from the NAT’s

00:01:58.265 IP address.

00:01:59.056 Essentially, it hides an entire private network

00:02:01.923 behind a single IP address.

 

00:02:04.000 This is useful because there aren’t enough

00:02:06.390 IPv4 addresses for every device that wants

00:02:08.779 to connect to the network, and because

00:02:11.169 it’s taking a long time to deploy

00:02:13.559 IPv6.

 

00:02:14.000 NAT is a work around, to let

00:02:17.150 you keep using IPv4 devices, with some

00:02:20.300 limitations, even though there aren’t enough IPv4

00:02:23.450 addresses.

 

00:02:25.000 How does NAT work? Well, let’s first

00:02:27.765 step back, and think about how a

00:02:30.530 single host connects to the network.

 

00:02:33.000 In the figure, a customer owns a

00:02:35.667 single host. That host connects to a

00:02:38.333 network run by an Internet service provider.

00:02:41.000 That ISP, in turn, connects to the

00:02:44.033 broader Internet.

 

00:02:45.000 The ISP owns a range of IP

00:02:47.333 addresses that it can assign to its

00:02:49.667 customers.

 

00:02:50.000 In this example, it owns the IPv4

00:02:53.868 prefix 203.0.113.0/24. That is, the set of

00:02:57.737 IPv4 addresses where the first 24 bits,

00:03:01.605 known as the network part of the

00:03:05.474 address, match those of 203.0.113.0 are assigned

00:03:09.342 to the ISP.

00:03:11.000 These are the IPv4 addresses in the

00:03:17.936 range 203.0.113.0 to 203.0.113.255.

 

00:03:22.000 The address with the host part equal

00:03:24.522 to zero represents the network, and cannot

00:03:27.045 be assigned to a device. The ISP

00:03:29.567 assigns the first usable IP address in

00:03:32.090 the range, 203.0.113.1, to the internal network

00:03:34.612 interface of the router that connects it

00:03:37.134 to the rest of the network,

00:03:39.297 and assigns the rest of the addresses

00:03:41.819 to customer machines.

 

00:03:43.000 One particular customer is assigned IP address

00:03:46.114 203.0.113.7 for their device.

00:03:47.894 The external, Internet-facing, side of the router

00:03:51.108 that connects the ISP to the rest

00:03:54.222 of the network has an IP address

00:03:57.337 assigned by the network to which the

00:04:00.451 ISP connects. In this example, it gets

00:04:03.565 IP address 192.0.2.47.

 

00:04:06.000 The customer’s host connects to a server

00:04:09.815 on the Internet. The server happens to

00:04:13.631 have IP address 192.0.2.53.

00:04:15.811 The customer’s host sends packets that have

00:04:19.726 their destination IP address equal to that

00:04:23.541 of the server, 192.0.2.53, and source IP

00:04:27.357 address equal to that of the customer’s

00:04:31.172 host, 203.0.113.7.

00:04:32.262 Those packets travel through the network without

00:04:36.177 change, and when they arrive that the

00:04:39.993 server, they still have destination IP address

00:04:43.808 192.0.2.53 and source IP address 203.0.113.7.

00:04:47.078 When it sends a reply, the server

00:04:50.994 will set the destination IP address to

00:04:54.809 that of the customer’s device, 203.0.113.7,

00:04:58.079 and use its own address, 192.0.2.53,

00:05:01.350 as the source IP address.

00:05:04.075 No address translation takes place.

 

00:05:08.000 At some point later, the customer buys

00:05:11.019 another host. How does it connect to

00:05:14.038 the network?

 

00:05:15.000 What’s supposed to happen is as follows.

00:05:17.674 First, the customer buys an IP router,

00:05:20.449 or is given one by the ISP.

00:05:23.123 The router is used to create an

00:05:25.797 internal network for the customer, that connects

00:05:28.472 to the ISP’s network.

00:05:30.000 This could be an Ethernet, a WiFi

00:05:33.430 network, or whatever.

 

00:05:35.000 The external interface of that router,

00:05:38.265 that connects the customer to the ISP,

00:05:42.074 inherits the IP address that was previously

00:05:45.884 assigned to the customer’s single device,

00:05:49.149 in this case 203.0.113.7.

00:05:51.326 The ISP also assigns a new IP

00:05:55.235 address range to the customer. This will

00:05:59.044 be a subset of the IP address

00:06:02.853 range the ISP owns. In this example,

00:06:06.663 the customer is assigned the IP address

00:06:10.472 range 203.0.113.16/28. That is, IP addresses where

00:06:14.281 the first 28 bits match those of

00:06:18.091 203.0.113.16, namely the range 203.0.113.16 to 203.0.113.31.

 

00:06:22.000 The customer assigns the first usable address

00:06:25.931 in that range, 203.0.113.17, to the internal

00:06:29.862 network interface of the router, and assigns

00:06:33.792 other addresses to their two hosts.

00:06:37.162 In this example, the two hosts are

00:06:41.092 given addresses 203.0.113.18 and 203.0.113.19.

 

00:06:44.000 The end result is that the ISP

00:06:46.127 delegates some of the IP addresses they

00:06:48.254 own to their customer, and the customer

00:06:50.381 uses them in their network.

 

00:06:53.000 One of the customer’s hosts connects to

00:06:56.133 a server on the Internet.

00:06:58.370 As expected, to do so, that host

00:07:01.603 sends an IP packet with the source

00:07:04.736 IP address set to its IP address,

00:07:07.869 in this case 203.0.113.18, and the destination

00:07:11.001 address set to the IP address of

00:07:14.134 the server, 192.0.2.53.

00:07:15.477 That packet travels through the customer’s network

00:07:18.709 to its router, and is forwarded on

00:07:21.842 to the ISP’s network. It traverses the

00:07:24.975 ISP’s network to the router connecting the

00:07:28.107 ISP to the Internet, and is forwarded

00:07:31.240 on from there to the Internet.

00:07:33.925 Eventually, the packet arrives at the server.

00:07:37.058 When it arrives, it still has destination

00:07:40.191 address equal to that of the server,

00:07:43.323 192.0.2.53, and source address equal to that

00:07:46.456 of the host that sent it,

00:07:49.141 203.0.113.18.

00:07:50.339 When it sends a reply, the server

00:07:53.572 will set the destination IP address to

00:07:56.704 that of the customer’s device, 203.0.113.18 and

00:07:59.837 use its own address, 192.0.2.53, as the

00:08:02.970 source IP address.

00:08:04.312 No address translation takes place.

 

00:08:07.000 That’s what’s supposed to happen, but what

00:08:09.720 actually happens?

00:08:10.497 Well, most likely the ISP either doesn’t

00:08:13.316 have enough IPv4 addresses to delegate some

00:08:16.036 of them to their customer, or they

00:08:18.756 want to charge a lot extra to

00:08:21.475 do so.

00:08:22.252 Accordingly, the customer buys a network address

00:08:25.072 translator, and connects it to the ISP’s

00:08:27.792 network in place of their single original

00:08:30.511 host.

 

00:08:31.000 The external interface of the NAT gets

00:08:34.536 the IP address assigned to the customer’s

00:08:38.071 original host, 203.0.113.7.

00:08:39.586 The customer sets up their internal network

00:08:43.222 as before, but instead of using IP

00:08:46.758 addresses assigned by their ISP, they use

00:08:50.293 one of the private IP address ranges.

00:08:53.829 @ 08:50 In this example, they use

00:08:57.364 addresses in the range 192.168.0.0 to 192.168.255.255.

 

00:09:01.000 The internal interface of the NAT is

00:09:05.515 given IP address 192.168.0.1, and the two

00:09:10.030 hosts get addresses 192.16.0.2 and 192.168.0.3.

 

00:09:15.000 One of the customer’s hosts again connects

00:09:18.325 to a server on the Internet.

00:09:21.175 As expected, that host sends an IP

00:09:24.600 packet with the source IP address set

00:09:27.925 to its IP address, in this case

00:09:31.250 192.168.0.2, and the destination address set to

00:09:34.575 the IP address of the server,

00:09:37.425 192.0.2.53.

 

00:09:38.000 That packet travels through the customer’s network

00:09:40.672 to its NAT router. The NAT rewrites

00:09:43.343 the source address of the packet to

00:09:46.015 match the external address of the NAT,

00:09:48.687 in this case 203.0.113.7, and also rewrites

00:09:51.358 the TCP or UDP port number to

00:09:54.030 some new port number that’s unused on

00:09:56.702 the NAT, and forwards the packet on

00:09:59.373 to the ISP’s network.

 

00:10:01.000 Internally, the NAT keeps a record on

00:10:04.189 the changes it made, associated with the

00:10:07.378 port.

00:10:08.583 The packet traverses the ISP’s network to

00:10:11.872 the router connecting the ISP to the

00:10:15.061 Internet, and is forwarded on from there

00:10:18.250 to the Internet. Eventually, the packet arrives

00:10:21.439 at the server. When it arrives,

00:10:24.172 it still has destination address equal to

00:10:27.361 that of the server, 192.0.2.53, but source

00:10:30.550 address will equal to that of the

00:10:33.739 NAT, 203.0.113.7.

 

00:10:34.000 To the server, the packet appears to

00:10:37.056 have come from the NAT. When it

00:10:40.112 sends a reply, the server will set

00:10:43.168 the destination IP address to that of

00:10:46.224 the NAT, 203.0.113.7, and use its own

00:10:49.280 address, 192.0.2.53, as the source address.

 

00:10:52.000 The reply will traverse the network until

00:10:54.384 it reaches the NAT. The NAT looks

00:10:56.767 at the TCP or UDP port number

00:10:59.151 to which the packet is destined,

00:11:01.194 and uses this to retrieve its internal

00:11:03.577 record of the rewrites that were performed.

00:11:05.961 It then uses this to do the

00:11:08.344 inverse rewrite, changing the destination IP address

00:11:10.728 and port in the packet to those

00:11:13.111 of the host on the private network,

00:11:15.495 then forwards the packet onto the private

00:11:17.878 network for delivery.

 

00:11:20.000 Essentially, the NAT hides a private network

00:11:25.528 behind a single public IP address.

00:11:30.266 The private network can use one of

00:11:35.893 three private IPv4 address ranges: 10.0.0.0/8,

00:11:40.631 176.16.0.0/12, and 192.168.0.0/16.

00:11:43.000 Machines in a private network can directly

00:11:45.596 talk to each other using these private

00:11:48.192 IP addresses, provided that communications stays within

00:11:50.787 the private network.

 

00:11:52.000 When they communicate with the rest of

00:11:54.425 the network, the IP addresses are rewritten

00:11:56.849 so that, to the rest of the

00:11:59.274 network, the private network looks like a

00:12:01.699 single device, with one IP address matching

00:12:04.123 that of the external address of the

00:12:06.548 NAT. This gives the illusion that there

00:12:08.972 are more IPv4 addresses available, by allowing

00:12:11.397 the same private address ranges being re-used

00:12:13.822 in different parts of the network.

 

00:12:16.000 Your home network, for example, almost certainly

00:12:18.822 uses addresses in the 192.168.0.0/16 private address

00:12:21.644 range, and is connected to the rest

00:12:24.466 of the network via a NAT router

00:12:27.287 provided by your ISP.

 

00:12:30.000 This concludes our review of how NAT

00:12:32.390 routers allow multiple devices to share a

00:12:34.779 single IP address. In the next part,

00:12:37.169 I’ll explain some of the problems NATs

00:12:39.559 cause.

Part 4: Problems due to Network Address Translation

The 4th part of the lecture continues the discussion of the problems cause by NAT devices, and why they are used despite these problems. It talks about the use of NAT as a work-around for the lack of IPv4 address space, as a possible translation mechanism between IPv4 and IPv6, and to avoid renumbering. And it talks about the implications of NAT for TCP connections and UDP flows.

00:00:00.000 In the previous part we discussed what

00:00:03.235 is network address translation, and walked through

00:00:05.471 some examples showing how NAT routers allow

00:00:07.706 several hosts on a private network to

00:00:09.942 share a single IP address.

00:00:11.539 In the following, I want to talk

00:00:13.874 about some of the problems caused by

00:00:16.110 NATs, and to discuss some of the

00:00:18.345 reasons why NATs are used despite these

00:00:20.581 problems.

 

00:00:22.000 The first issue with NAT routers is

00:00:24.492 that they break certain classes of application,

00:00:26.984 and encourage centralisation.

00:00:28.051 NATs are designed to support client-server applications,

00:00:30.643 where the client is behind the NAT

00:00:33.135 and the server is a host on

00:00:35.627 the public Internet. Packets sent by a

00:00:38.119 host with a private IP address can

00:00:40.610 pass out through the NAT, and will

00:00:43.102 have their IP address and port translated

00:00:45.594 to use the public IP address of

00:00:48.086 the NAT before they’re forwarded to the

00:00:50.578 public Internet. The NAT will also retain

00:00:53.069 state, so that the reverse translation will

00:00:55.561 be applied to replies to those packets,

00:00:58.053 allowing them to pass back through the

00:01:00.545 NAT.

00:01:01.651 This behaviour allows clients to connect to

00:01:04.243 servers, setting up NAT translation state in

00:01:06.734 the process, and to receive responses.

00:01:08.870 The reverse doesn’t work, though.

 

00:01:10.000 NAT routers rely on outgoing packets to

00:01:12.350 establish the mappings they need to translate

00:01:14.700 incoming packets. That is, when an incoming

00:01:17.050 TCP or UDP packet arrives at some

00:01:19.400 particular port on a NAT, the NAT

00:01:21.750 looks at its record of what it

00:01:24.100 previously sent from that port, and how

00:01:26.450 it was translated, to know what’s the

00:01:28.800 reverse translation to make. If there’s been

00:01:31.150 no outgoing packet on that port,

00:01:33.164 the NAT won’t know how to translate

00:01:35.514 the incoming packet. It won’t know which

00:01:37.864 of the private IP addresses to use

00:01:40.214 as the destination address for the translated

00:01:42.564 packet.

 

00:01:43.000 This complicates running a server behind a

00:01:45.808 NAT, since the NAT won’t know how

00:01:48.616 to translate incoming requests for the server.

00:01:51.423 It’s possible to manually configure the NAT

00:01:54.231 to forward packets appropriately, of course,

00:01:56.638 and protools like UPnP can help with

00:01:59.446 this, but these approaches are complicated or

00:02:02.254 unreliable. It’s generally easier and more reliable

00:02:05.061 to pay a cloud computing provider to

00:02:07.869 host the server, which encourages centralisation onto

00:02:10.677 large hosting services.

00:02:11.880 NATs also make it hard to write

00:02:14.788 peer-to-peer applications. In part, this is because

00:02:17.596 NATs make incoming connections difficult. But it’s

00:02:20.404 also because hosts located behind a NAT

00:02:23.212 only know their private address, so can’t

00:02:26.020 give their peer a public address to

00:02:28.827 which it can connect. There are solutions

00:02:31.635 to this, that I’ll talk about in

00:02:34.443 the next part of this lecture,

00:02:36.850 but they’re complicated, slow, and wasteful.

00:02:39.256 Unless you really need the privacy and

00:02:42.064 latency benefits of a direct peer-to-peer connection,

00:02:44.872 it’s often easier to relay traffic via

00:02:47.680 a server hosted in a data centre

00:02:50.488 somewhere, with a public IP address,

00:02:52.894 again encouraging centralisation of services.

 

00:02:56.000 If NAT routers are so problematic,

00:02:58.389 why do people use them? There are

00:03:01.176 three reasons.

00:03:01.972 The first is to work around the

00:03:04.859 lack of IPv4 address space.

00:03:06.850 As shown in the figure on the

00:03:09.737 right, the Regional Internet Registries have run

00:03:12.524 out of IPv4 addresses. There are no

00:03:15.311 more IPv4 addresses available for ISPs and

00:03:18.098 companies that want to connect to the

00:03:20.885 Internet, and they can’t provide enough IPv4

00:03:23.672 addresses to fulfil demand.

00:03:25.265 The result is that IPv4 addresses are

00:03:28.152 scarce and expensive. ISPs either don’t have

00:03:30.939 enough addresses to meet their customers needs,

00:03:33.726 or the cost of those addresses is

00:03:36.513 prohibitive, and customers use a private network

00:03:39.300 with a NAT instead of using public

00:03:42.087 IPv4 addresses.

00:03:42.883 The transition to IPv6 will solve this

00:03:45.770 problem, since IPv6 makes addresses cheap and

00:03:48.557 plentiful. The smallest possible address allocation for

00:03:51.344 an IPv6 network is a factor of

00:03:54.131 four billion times larger than the entire

00:03:56.919 IPv4 Internet! Unfortunately, the transition to IPv6

00:03:59.706 has been slow.

 

00:04:02.000 This suggests the second reason why NAT

00:04:05.055 is used: to translate between IPv4 and

00:04:08.109 IPv6 addresses.

00:04:08.982 In this model, an ISP, or other

00:04:12.136 network operator, runs IPv6 internally in their

00:04:15.191 network, and does not support IPv4.

00:04:17.809 This gives the ISP a clean,

00:04:20.427 modern, and future-proof network.

00:04:22.173 The ISP also runs two sets of

00:04:25.327 NATs.

00:04:26.514 For customers that want to use IPv4

00:04:29.668 internally, the customer uses a private IPv4

00:04:32.723 network, and the NAT translates the IPv4

00:04:35.777 packets into IPv6 packets when they leave

00:04:38.832 the customer’s network. The principle is the

00:04:41.886 same as the NAT routers we discussed

00:04:44.941 in the last part of this lecture,

00:04:47.995 except that rather than rewriting packets with

00:04:51.050 private IP addresses to have public IPv4

00:04:54.105 addresses, the NAT rewrites the entire IPv4

00:04:57.159 header and replaces it with an IPv6

00:05:00.214 header.

 

00:05:00.000 When packets get to the edge of

00:05:02.854 the ISPs network, where it connects to

00:05:05.708 the public Internet, they’re either forwarded as

00:05:08.562 native IPv6 if the destination is accessible

00:05:11.415 via IPv6, or translated to IPv4 by

00:05:14.269 another NAT.

00:05:15.085 The expectation in this approach to running

00:05:18.038 a network is that, over time,

00:05:20.485 the number of customers and destination networks

00:05:23.338 that need IPv4 will go down,

00:05:25.785 and more traffic will run IPv6 end-to-end.

00:05:28.638 NAT is used as a, hopefully temporary,

00:05:31.492 workaround.

 

00:05:33.000 The third reason to use NAT is

00:05:35.678 to avoid renumbering.

00:05:36.826 Networks that have a public IP address

00:05:39.604 range tend, over time, to end up

00:05:42.282 hard coding IP addresses from that range

00:05:44.960 into configuration files, applications, and settings.

00:05:47.255 This is a mistake. Applications should always

00:05:49.933 use DNS names, to allow the IP

00:05:52.611 addresses to change, but people do it

00:05:55.289 anyway.

00:05:56.422 The result is that it’s difficult to

00:05:59.200 change the IP addresses used by machines

00:06:01.878 on a network. The longer a host

00:06:04.556 has used a particular IP address,

00:06:06.851 the more likely it is that something,

00:06:09.529 somewhere, on the network has that address

00:06:12.207 hard-coded, and will fail if the host’s

00:06:14.885 address changes.

 

00:06:15.000 If a network has an IP address

00:06:17.649 range delegated to it from its ISP,

00:06:20.299 what’s known as a provider allocated IP

00:06:22.948 address range, and wants to change ISP,

00:06:25.597 then it will need to change the

00:06:28.247 IP address range it uses to one

00:06:30.896 delegated from its new provider. Many organisations

00:06:33.546 have found this sufficiently difficult that it’s

00:06:36.195 easier to keep the old IP addresses

00:06:38.844 internally, and use a NAT to translate

00:06:41.494 addresses to the range assigned by the

00:06:44.143 new ISP.

 

00:06:45.000 A similar problem can occur if one

00:06:47.904 company buys another, and has to integrate

00:06:50.809 the IT systems of the new company

00:06:53.713 into its existing network.

00:06:55.373 IPv6 has better auto-configuration support than IPv4,

00:06:58.378 and tries to make renumbering easier,

00:07:00.867 but it’s not clear how well this

00:07:03.772 works. As a result, some network equipment

00:07:06.676 vendors have started selling NATs that translate

00:07:09.581 between two different IPv6 prefixes, to ease

00:07:12.485 renumbering.

 

00:07:13.000 In both cases, a better approach is

00:07:15.836 that an organisation gets what’s known as

00:07:18.671 provider independent IP addresses, directly from one

00:07:21.507 of the Regional Internet Registries, so it

00:07:24.342 owns the IP addresses it uses.

00:07:26.773 In this case, the organisation pays its

00:07:29.608 ISP to route traffic to the addresses

00:07:32.444 it owns, and can move to a

00:07:35.280 new ISP without renumbering.

 

00:07:38.000 Given these reasons why NAT routers will

00:07:40.731 be used, despite their problems, what are

00:07:43.462 the implications for NAT routers on TCP

00:07:46.193 connections?

00:07:47.334 Well, as I’ve explained, outgoing connections create

00:07:50.165 state in the NAT, so replies can

00:07:52.896 be translated to reach the correct host

00:07:55.627 on the private network. The question is,

00:07:58.358 then, how does the NAT know what

00:08:01.089 translation state to setup?

 

00:08:02.000 The way this works is that the

00:08:04.975 NAT router looks at TCP segments it’s

00:08:07.950 translating and forwarding, and watches for packets

00:08:10.925 representing a TCP connection establishment handshake.

00:08:13.475 If the NAT sees an outgoing SYN

00:08:16.450 packet, followed by an incoming SYN-ACK,

00:08:19.000 then an outgoing ACK, with matching sequence

00:08:21.975 and acknowledgment numbers, then it can infer

00:08:24.950 that this is the start of a

00:08:27.925 TCP connection, and setup the appropriate translation.

 

00:08:31.000 TCP connections have a similar exchange at

00:08:33.816 the end of the connection, with FIN,

00:08:36.631 FIN-ACK, and ACK packets. The NAT router

00:08:39.447 can watch for these exchanges, and infer

00:08:42.263 that the corresponding TCP connections have finished,

00:08:45.079 and that the translation state can be

00:08:47.894 removed.

00:08:49.047 Unfortunately, applications and hosts sometimes crash,

00:08:51.560 and connections disappear without sending the FIN,

00:08:54.376 FIN-ACK, and ACK packets. For this reason,

00:08:57.192 NAT routers also implement a timeout.

00:08:59.605 If a connection waits too long between

00:09:02.421 sending packets, the NAT will assume it’s

00:09:05.237 failed, and remove the translation state.

 

00:09:07.000 The recommendation from the IETF is that

00:09:09.441 NATs use a two hour timeout,

00:09:11.532 but measurements have shown that many NATs

00:09:13.973 ignore this and use a shorter timer.

00:09:16.414 The result is that long-lived TCP connections,

00:09:18.954 that would otherwise go idle, need to

00:09:21.395 send something, even if just an empty

00:09:23.835 TCP segment, every few minutes, to prevent

00:09:26.276 NATs on the path from timing out

00:09:28.716 and dropping the connection.

00:09:30.111 If you’ve ever used ssh to login

00:09:32.651 to a remote system, gone to do

00:09:35.092 something else, then come back after a

00:09:37.532 couple of hours and wondered why the

00:09:39.973 ssh connection has failed, this may well

00:09:42.414 be due to NAT timeout.

00:09:44.157 The other issue, as I mentioned at

00:09:46.697 the start of this part, is that

00:09:49.138 the NAT won’t have state for incoming

00:09:51.578 connections, unless manually configured to do so.

00:09:54.019 This makes it difficult to run a

00:09:56.459 server or peer-to-peer application behind the NAT.

 

00:10:00.000 The implications of NAT for UDP flows

00:10:02.882 are similar to those for TCP,

00:10:05.352 except that the lack of connections with

00:10:08.234 UDP complicates things.

00:10:09.469 For TCP, a NAT can watch for

00:10:12.451 the connection establishment and teardown segments,

00:10:14.921 and know when the TCP connections start

00:10:17.803 and finish. TCP connections can fail without

00:10:20.685 sending the FIN, FIN-ACK, ACK exchange,

00:10:23.155 but this is rare, and NAT routers

00:10:26.037 generally rely on watching the TCP connection

00:10:28.919 setup and teardown messages to manage translation

00:10:31.801 state.

00:10:32.963 UDP, on the other hand, has no

00:10:35.945 connection establishment, since it has no concept

00:10:38.827 of connections.

 

00:10:39.000 This is not a great problem when

00:10:41.411 it comes to establishing state in a

00:10:43.822 NAT. If the NAT sees any outgoing

00:10:46.233 UDP packet with a particular address and

00:10:48.644 port, it sets up the state in

00:10:51.056 the NAT to allow replies.

00:10:52.778 The problem comes with knowing when to

00:10:55.289 remove that translation state in the NAT.

00:10:57.700 Since UDP has no “end of connection”

00:11:00.111 message, the only way to do this

00:11:02.522 is with a timeout.

 

00:11:04.000 The most widely used UDP application,

00:11:06.216 historically, has been DNS. DNS clients tend

00:11:08.802 to contact a lot of different servers,

00:11:11.387 but exchange only a small amount of

00:11:13.973 data with each. As a result,

00:11:16.189 many NATs have very short timeouts -

00:11:18.774 on the order of tens of seconds

00:11:21.360 - for UDP translation state, to prevent

00:11:23.945 them accumulating state for too many UDP

00:11:26.531 flows.

 

00:11:27.000 An unfortunate consequence of this, is that

00:11:30.157 applications that use UDP, such as video

00:11:33.313 conferencing and gaming, must send packets frequently,

00:11:36.470 in both directions, to make sure the

00:11:39.626 NAT bindings stay open. The IETF recommends

00:11:42.783 that such applications send and receive something

00:11:45.940 at least once every 15 seconds.

00:11:48.645 This can generate unnecessary traffic.

 

00:11:51.000 There is one benefit, though, that comes

00:11:53.745 from the lack of connection establishment signalling

00:11:56.490 in UDP. With TCP, the NAT can

00:11:59.235 see the SYN, SYN-ACK, ACK exchange,

00:12:01.587 and knows the exact addresses and ports

00:12:04.332 that the client and server are using.

00:12:07.077 This allows the NAT to create a

00:12:09.822 very specific binding, and reject traffic from

00:12:12.567 other addresses.

00:12:13.351 These very specific bindings are a security

00:12:16.196 benefit, but make peer-to-peer connections harder to

00:12:18.941 establish. UDP applications tend to be more

00:12:21.686 flexible in where they accept packets from,

00:12:24.431 so NATs generally establish bindings that allow

00:12:27.176 any UDP packets that arrive on the

00:12:29.920 correct port to be translated and forwarded

00:12:32.665 across the NAT. This makes peer to

00:12:35.410 peer connection establishment much easier for NAT,

00:12:38.155 as we’ll see in the next part.

 

00:12:42.000 NATs work around three real problems:

00:12:44.582 lack of IPv4 address space, IPv4 to

00:12:47.595 IPv6 transition, and renumbering. They work well

00:12:50.608 for client-server applications, where the client is

00:12:53.620 behind the NAT and the server is

00:12:56.633 on the public Internet, but make it

00:12:59.646 hard to run peer-to-peer applications and to

00:13:02.658 host servers on networks that use NATs.

00:13:05.671 This encourages centralisation of the Internet infrastructure

00:13:08.684 onto cloud providers and, as we’ll see

00:13:11.696 in the next part, greatly complicates certain

00:13:14.709 classes of application.

Part 5: NAT Traversal and Peer-to-Peer Connection Establishment

The final part of the lecture, discusses NAT traversal and peer-to-peer connection establishment. It outlines the binding discovery process, by which a client can establish that it's behind a NAT and find the external IP address of that NAT, and the ICE algorithm for candidate exchange and peer-to-peer connection establishment.

00:00:00.000 In the final part of this lecture,

00:00:03.384 I’d like to discuss the problem of

00:00:05.769 NAT traversal. That is, how applications can

00:00:08.153 work around the presence of NAT routers

00:00:10.537 to establish peer-to-peer connections.

 

00:00:13.000 As I described in the previous part,

00:00:15.631 NATs are designed to support outbound connections

00:00:18.261 from a client in the private network

00:00:20.892 to a server on the public Internet,

00:00:23.523 and this use case works well.

00:00:25.778 Other scenarios are less successful.

00:00:27.757 Incoming connections, to a server located in

00:00:30.488 the private network, will fail. This happens

00:00:33.118 because the NAT can’t know how to

00:00:35.749 translate the incoming packets. There are work

00:00:38.380 arounds for this, that involve manually configuring

00:00:41.010 the NAT to forward incoming connections to

00:00:43.641 the correct device, but this is difficult

00:00:46.272 to do correctly.

00:00:47.399 Similarly, peer-to-peer connections through a NAT will

00:00:50.130 also fail, unless the packets are sent

00:00:52.761 in a way that makes the NAT,

00:00:55.392 or the NATs if there are several

00:00:58.022 peers all located in private networks,

00:01:00.277 think that a client-server connection is being

00:01:02.908 opened, and that the response is coming

00:01:05.539 from the server. In the following,

00:01:07.793 I’ll talk about how this can be

00:01:10.524 arranged.

 

00:01:12.000 The figure shows an example where two

00:01:14.871 hosts, A and B, and trying to

00:01:17.741 establish a direct peer to peer connection.

00:01:20.612 For example, this could be two devices

00:01:23.482 in people’s homes that are trying to

00:01:26.353 setup a video call.

00:01:27.993 Each of these hosts is in a

00:01:30.964 private network, and is connected to the

00:01:33.834 public Internet via a NAT. It’s possible,

00:01:36.705 indeed likely, that if these are home

00:01:39.576 networks, then both of the private networks

00:01:42.446 will be using the IP address range

00:01:45.317 192.168.0.0./16, since that’s the default for most

00:01:48.187 home NAT routers. A consequence is that

00:01:51.058 Host A and Host B could both

00:01:53.929 be using the same private IP address,

00:01:56.799 for example both hosts could be using

00:01:59.670 IP address 192.168.0.2.

 

00:02:01.000 This isn’t a problem, since Host A

00:02:03.557 and Host B are on different private

00:02:06.114 networks, each hidden behind a different NAT.

00:02:08.671 The two NATs have different public IP

00:02:11.229 address on the external interface of the

00:02:13.786 router, and what’s used internally is not

00:02:16.343 visible to the rest of the network.

 

00:02:19.000 How do these two hosts go about

00:02:21.442 establishing a connection?

00:02:22.489 Well, Host A can’t send a packet

00:02:25.031 directly to Host B, because it has

00:02:27.473 the same IP address. If it tries,

00:02:29.915 the packet will come straight back to

00:02:32.357 itself!

00:02:33.456 Rather, in order to connect to Host

00:02:35.998 B, Host A will have to discover

00:02:38.440 the external address and port number that

00:02:40.882 NAT B is using for packets sent

00:02:43.324 by Host B. It can then send

00:02:45.766 its packets to NAT B, that will

00:02:48.208 translate and forward them to host B.

 

00:02:50.000 To do this, the two peers,

00:02:52.246 Host A and Host B, both make

00:02:54.866 connections to a referral server located somewhere

00:02:57.486 on the public Internet. This is shown

00:03:00.106 in the dashed red lines on the

00:03:02.727 slide. They ask that server where their

00:03:05.347 packets appear to be coming from.

00:03:07.593 This process is known as binding discovery,

00:03:10.213 and lets the hosts find out how

00:03:12.833 their NAT is translating packets. The result

00:03:15.453 is a candidate address for each host,

00:03:18.073 that it thinks is the external address

00:03:20.694 of the NAT that will translate incoming

00:03:23.314 packets and forward them to it.

00:03:25.560 The peers then exchange these candidate addresses

00:03:28.280 with each other, via the referral server.

 

00:03:31.000 Once they’ve received the candidate addresses from

00:03:33.470 their peer, the two hosts systematically send

00:03:35.941 probe packets, to check if any of

00:03:38.411 these candidates actually work to reach the

00:03:40.881 peer. That is, the hosts check if

00:03:43.351 the outgoing probe packets they send will

00:03:45.822 correctly setup translation state in the NAT,

00:03:48.292 so that incoming probes from the peer

00:03:50.762 will be translated and forwarded to them.

00:03:53.233 And they check that there are no

00:03:55.703 firewalls that are blocking the traffic.

00:03:57.820 If the probes are successfully received,

00:04:00.038 in both directions, then the two hosts

00:04:02.508 can switch to using the direct peer-to-peer

00:04:04.978 path, shown as the solid blue line

00:04:07.449 on the slide, and no longer need

00:04:09.919 the server.

00:04:10.625 If the probes fail, then a direct

00:04:13.195 peer-to-peer connection may not be possible,

00:04:15.312 and the hosts may have to relay

00:04:17.783 all traffic via the referral server.

 

00:04:21.000 The process of finding out what translations

00:04:23.619 a NAT is performing is known as

00:04:26.238 NAT binding discovery.

00:04:27.361 The Session Traversal Utilities for NAT,

00:04:29.706 STUN, is a commonly used protocol that

00:04:32.325 performs NAT binding discovery in the Internet.

00:04:34.944 When a host on a private network

00:04:37.664 sends a packet to a host on

00:04:40.283 the public network, the NAT at the

00:04:42.902 edge of the private network will translate

00:04:45.521 the source IP address and port number

00:04:48.140 in the packet. The host on the

00:04:50.760 private network doesn’t know what translation has

00:04:53.379 been done, but the server that receives

00:04:55.998 the packet can inspect its source address

00:04:58.617 and port, to find out where it

00:05:01.236 came from.

00:05:01.985 For example, when using a UDP socket,

00:05:04.704 an application can use the recvfrom() system

00:05:07.323 call to retrieve both the contents of

00:05:09.942 a UDP packet and its source address.

00:05:12.562 Similarly, for TCP connections, the accept() system

00:05:15.281 call returns the address of the client.

 

00:05:18.000 The server then replies to the client,

00:05:20.317 telling it where the packet appeared to

00:05:22.634 come from. This is what’s known as

00:05:24.952 a server reflexive address. That is,

00:05:26.938 the address that a server thinks the

00:05:29.255 client has.

00:05:29.917 If there’s a NAT between the client

00:05:32.334 and the server, then the server reflexive

00:05:34.652 address will be different to the address

00:05:36.969 from which the client sent the packet.

00:05:39.286 If the client’s addresses and the server

00:05:41.603 reflexive address are the same, the client

00:05:43.921 knows there’s no NAT between it and

00:05:46.238 the server.

 

00:05:47.000 You might ask why a host that’s

00:05:49.311 in a private network doesn’t just ask

00:05:51.623 its NAT how it will translate the

00:05:53.934 packets? Two reasons.

00:05:54.925 The first is that by the time

00:05:57.336 we realised that binding discovery was needed,

00:05:59.648 there were already tens of millions of

00:06:01.959 NATs deployed, with no way to upgrade

00:06:04.271 them to add a way to ask

00:06:06.582 how they’ll translate packets.

00:06:07.903 The second is that a host might

00:06:10.315 not know that it’s behind a NAT,

00:06:12.626 or might be behind more than one

00:06:14.938 NAT, and so won’t know what NAT

00:06:17.249 to ask for the binding.

 

00:06:20.000 When performing binding discovery, it’s important that

00:06:22.634 a host discovers every possible candidate address

00:06:25.268 on which it might be reachable.

00:06:27.525 For example, think about a phone that

00:06:30.259 has both 4G and WiFi interfaces.

00:06:32.517 Each of these interfaces can have an

00:06:35.151 IPv4 address and an IPv6 address,

00:06:37.408 representing the point of attachment to networks

00:06:40.042 it directly connects to. This could be

00:06:42.676 a total of four possible IP addresses

00:06:45.310 for the phone.

00:06:46.438 The phone may be behind IPv4 NAT

00:06:49.172 routers on each of those interfaces,

00:06:51.430 and so each interface might also have

00:06:54.064 a server reflexive address on which it

00:06:56.697 can be reached, that the host can

00:06:59.331 discover using STUN. This can give another

00:07:01.965 two addresses, bringing the total to six.

00:07:04.599 It’s unlikely, but the phone could also

00:07:07.333 be connected via one or more IPv6

00:07:09.967 NATs. This potentially gives two more server

00:07:12.601 reflexive addresses on which it can be

00:07:15.234 reached.

00:07:16.361 In case these server reflexive addresses don’t

00:07:19.094 work, the phone may also be able

00:07:21.728 to use the referral server to relay

00:07:24.362 for it, using a protocol called TURN,

00:07:26.996 acting as a proxy to deliver traffic

00:07:29.630 if a direct connection isn’t possible.

00:07:31.887 This proxy endpoint might be accessible via

00:07:34.521 IPv4 and IPv6.

 

00:07:35.000 The phone might also have a VPN

00:07:37.612 connection, and be able to send and

00:07:40.223 receive traffic via the VPN, as well

00:07:42.835 as directly. That VPN endpoint could be

00:07:45.446 accessible over IPv4 or IPv6, and might

00:07:48.058 itself be behind a NAT, so it’s

00:07:50.669 necessary to check for server reflexive addresses

00:07:53.281 on the VPN interface.

00:07:54.773 Not all of these will exist for

00:07:57.485 every device, of course, but the point

00:08:00.096 is that a modern networked device is

00:08:02.708 often reachable in many different ways.

00:08:04.946 If it’s to successfully connect to another

00:08:07.558 device, in a peer-to-peer manner, it needs

00:08:10.169 to find as many of these candidate

00:08:12.781 addresses as possible.

 

00:08:15.000 Having run a binding discovery protocol to

00:08:17.525 find all its possible candidates, a host

00:08:20.051 sends the list of candidates to the

00:08:22.576 referral server, and the referral server sends

00:08:25.102 them on to its peer. Its peer

00:08:27.627 does the same, and the host receives

00:08:30.153 the peer’s candidates via the referral server.

00:08:32.678 At this point, the two hosts know

00:08:35.204 each others candidate addresses, and are ready

00:08:37.729 to check which of the addresses work.

00:08:40.255 Given that the two peers can communicate

00:08:42.880 via the referral server, you might ask

00:08:45.406 why the peers bother to establish a

00:08:47.931 peer-to-peer connection, and don’t instead just keep

00:08:50.457 communicating via the relay?

 

00:08:52.000 The primary reason is because a direct

00:08:54.689 peer-to-peer connection is usually lower latency than

00:08:57.378 a connection via a relay, and for

00:09:00.067 peer-to-peer applications like video calls, latency matters.

00:09:02.756 The second is that the relay server

00:09:05.546 can eavesdrop on connections that it’s relaying,

00:09:08.235 but not on direct peer-to-peer connections.

00:09:10.540 This is perhaps less of a concern

00:09:13.229 than you might think, since the traffic

00:09:15.918 can be encrypted so it can’t be

00:09:18.607 read by the server. Also, the server

00:09:21.296 knows that the call is happening anyway,

00:09:23.985 and sometimes knowledge that two people are

00:09:26.674 talking is almost as sensitive as knowing

00:09:29.363 what they’re talking about.

 

00:09:32.000 Once they’ve exchanged candidates, the two hosts

00:09:34.630 systematically send probe packets from every one

00:09:37.259 of their candidate addresses to every one

00:09:39.889 of the peer’s candidate addresses in turn,

00:09:42.519 to see if the can establish a

00:09:45.149 direct connection.

 

00:09:46.000 The idea is that a probe packet

00:09:48.545 sent, for example, from Host A to

00:09:51.090 a server reflexive address of Host B,

00:09:53.635 will open a binding in NAT A,

00:09:56.180 even if it fails to reach host

00:09:58.725 B. This open binding will allow a

00:10:01.270 later probe from Host B to the

00:10:03.815 server reflexive address of Host A to

00:10:06.360 reach Host A. This will, in turn,

00:10:08.905 trigger Host A to probe again in

00:10:11.450 response, and this time the probe from

00:10:13.995 Host A to the server reflexive address

00:10:16.540 of Host B will succeed because the

00:10:19.084 probe from Host B opened the necessary

00:10:21.629 binding on NAT B. The two hosts

00:10:24.174 then start sending traffic and keep-alive messages

00:10:26.719 on that path to keep the bindings

00:10:29.264 active, while the probing continues on all

00:10:31.809 the other candidates.

 

00:10:33.000 The probing can take a long time,

00:10:35.478 so candidate addresses are assigned a priority

00:10:37.956 based on how likely the host thinks

00:10:40.434 it is to be reachable on that

00:10:42.913 address, and on its expectation of how

00:10:45.391 well that address will perform. The checks

00:10:47.869 take place in priority order, to quickly

00:10:50.347 try to find a pair of candidates

00:10:52.825 that works.

00:10:53.533 If more than one pair of candidate

00:10:56.111 addresses successfully succeeds, the hosts choose the

00:10:58.590 best path, for example the path with

00:11:01.068 the lowest latency, and drop the other

00:11:03.546 connections.

 

00:11:04.000 The Interactive Connectivity Establishment algorithm, ICE,

00:11:06.736 described by the IETF in RFC 8445

00:11:09.928 describes this probing process is detail.

00:11:12.664 When making a peer-to-peer phone or video

00:11:15.956 call, the ICE algorithm and the probing

00:11:19.148 usually happens while the phone is ringing,

00:11:22.340 so the connection is ready when the

00:11:25.532 call is answered.

 

00:11:28.000 What should be clear by now is

00:11:31.163 that NAT binding discovery, and the systematic

00:11:34.327 connection probing needed for NAT traversal,

00:11:37.038 is complex, slow, and generates a lot

00:11:40.201 of traffic. The RFCs that describe how

00:11:43.365 the process works are almost 200 pages

00:11:46.528 long, and are not easy to implement

00:11:49.691 correctly.

00:11:50.893 The result is reasonably effective for UDP

00:11:54.156 traffic.

00:11:55.358 The STUN protocol, and the ICE algorithm,

00:11:58.622 were developed to support voice-over-IP applications,

00:12:01.333 that run over UDP, and the result

00:12:04.496 works well.

 

00:12:04.000 It’s less effective for peer-to-peer TCP connections.

00:12:07.217 NATs tend to be quite permissive for

00:12:10.435 UDP, translating any incoming UDP packet that

00:12:13.652 reaches the correct address and port,

00:12:16.410 but are often stricter for TCP connections,

00:12:19.627 and check for matching TCP sequence numbers,

00:12:22.844 etc., This makes peer-to-peer TCP connections less

00:12:26.062 likely to be successful.

 

00:12:29.000 In this lecture, I’ve outlined how client-server

00:12:32.164 connection establishment works, and how the use

00:12:35.327 of TLS and IPv6 can affect connection

00:12:38.491 establishment, and can require connection racing using

00:12:41.655 the “happy eyeballs” technique. I also showed

00:12:44.818 that connection establishment latency is often a

00:12:47.982 critical factor limiting the performance of TCP

00:12:51.145 connections.

00:12:52.347 In the later parts, I outlined how

00:12:55.611 and why NAT routers are used,

00:12:58.323 their advantages and disadvantages, and how NAT

00:13:01.486 traversal techniques work to establish peer-to-peer connections.

 

00:13:04.000 Establishing a connection used to be a

00:13:06.600 simple task. What I hope to have

00:13:09.200 shown you is that it’s no longer

00:13:11.800 simple, not in the client-server case,

00:13:14.029 and especially not when peer-to-peer connections are

00:13:16.629 needed.

Discussion

Lecture 2 discussed connection establishment in a fragmented network. It started with a review of the TCP service model, and how it establishes a client-server connection. Then, it showed some of the factors that affect connection establishment performance.

One of the key factors affecting performance was latency, and the number of round trips between the client and server needed to establish the connection. With the aid of a simple example, I tried to show that latency is often the main limitation, rather than bandwidth.

Does the example look reasonable to you? Do the results surprise you? Would you be willing to pay your ISP for a higher bandwidth Internet connection?

We then discussed dual-stack connection establishment for networks that have both IPv4 and IPv6 hosts. I highlighted that the IPv4 and IPv6 networks are separate, and showed that parallel connection establishment is needed.

How does the complexity of parallel connection establishment compare to the sequential DNS look-up shown in lab 1? How would you implement it?

Finally, the lecture discussed network address translation and NAT traversal, allowing several hosts to share an IP address.

When does NAT work well? When is NAT problematic? What does it break? Why do people use NAT devices?

NAT traversal uses the ICE algorithm to establish peer-to-peer connections, using a referral server to exchange addresses then probing to check is candidate addresses work.

Does this approach make sense? How effectively do you think it works?