-: Encapsulation and Decapsulation. 2 2
In this lesson, 3 3
we're going to talk about encapsulation and decapsulation. 4 4
Encapsulation is the process of putting headers 5 5
and sometimes trailers around some of our data. 6 6
Think about it like this. 7 7
You just finished writing a letter to your grandma, 8 8
and now you want to send it to her. 9 9
Well, to do that, you need to put it in an envelope. 10 10
Now, when you put the letter in the envelope, 11 11
you're actually encapsulating it. 12 12
Now, once your grandma gets that envelope, 13 13
she wants to be able to read it 14 14
and so she has to take the letter out of the envelope 15 15
in order for her to read it. 16 16
This process is known as decapsulation 17 17
because we're removing the encapsulation 18 18
that was applied earlier. 19 19
Now, I know this is a silly example, 20 20
but that's exactly what happens 21 21
as we send data on our networks. 22 22
It's continually being encapsulated and decapsulated 23 23
as it moves up or down the layers of the OSI model. 24 24
If we move down the OSI layers, from 7 to 1, 25 25
we encapsulate our data. 26 26
If we move upward from Layer 1 up to 7, 27 27
we decapsulate our data. 28 28
So let's take a closer look at how this works 29 29
in the real world. 30 30
In the OSI model, we use protocol data units or PDUs 31 31
to transmit our data. 32 32
A protocol data unit is just a single unit of information 33 33
transmitted within a computer network. 34 34
In the OSI model, they are simply called L, 35 35
the layer number, and PDU. 36 36
For example, L7 PDU is a Layer 7 PDU. 37 37
This type of terminology can be used 38 38
for every single layer we have, 39 39
but we also have special names for the PDUs 40 40
when we reach Layers 1, 2, 3, and 4. 41 41
For Layer 1, we call them bits. 42 42
For Layer 2, we call them frames. 43 43
For Layer 3, we call it packets. 44 44
And Layer 4, we call it segments if we're using TCP 45 45
or datagrams if we're using UDP. 46 46
Now, as a user creates data 47 47
and they want to send it over a network, 48 48
they're going to enter it into an application 49 49
at the application layer, Layer 7. 50 50
This data then has a Layer 7 header added 51 51
that contains metadata 52 52
with the parameters that are agreed upon 53 53
by the specific application. 54 54
So if you're using HTTP or you're using FTP, 55 55
that's going to have specific metadata for that type of data. 56 56
Then that information is going to be passed down to Layer 6 57 57
where it's going to encapsulate the Layer 7 header 58 58
and data together, and then add its own Layer 6 header, 59 59
which contains its own metadata with information 60 60
about the presentation or encryption formats being used. 61 61
Next, it's going to pass this down to Layer 5 62 62
where it encapsulates the Layer 6 header 63 63
and the Layer 6 data 64 64
and it's going to add its own Layer 5 header 65 65
based on the metadata about the session. 66 66
As you can see, it's like taking a letter, 67 67
or in this case data, wrapping it in an envelope 68 68
and then writing some information on that envelope. 69 69
That's our header. 70 70
When we hand it to the next person, 71 71
they're going to put it in an envelope 72 72
and write their own metadata on the outside of the envelope 73 73
and then pass it to the next person. 74 74
And we keep doing this as we go down the layers 75 75
all the way down to Layer 1. 76 76
Now, the headers added at Layers 4, 3, 2, and 1 77 77
are very specific and they actually help ensure the message 78 78
is going to reach its final destination. 79 79
So let's take a look at the header that's added at Layer 4, 80 80
the transport layer. 81 81
Now, if you remember, 82 82
the transport layer uses different protocols 83 83
like TCP or UDP. 84 84
The TCP header has 10 mandatory fields, 85 85
totaling 20 bytes of information. 86 86
This includes our source port, the destination port, 87 87
the sequence number, the acknowledgement number, 88 88
the TCP data offset, the reserve data, 89 89
which is currently always going to be set to zero 90 90
because it's not really used, 91 91
the control flags, the window size, the TCP checksum, 92 92
the urgent pointer, and the mTCP optional data. 93 93
Now, you don't need to know all of these fields in depth, 94 94
but there are a couple that are pretty important. 95 95
For example, the source and destination ports 96 96
are pretty important to understand 97 97
because this helps determine 98 98
where the information is being sent from 99 99
and where it's being sent to, 100 100
and allows it to go through a firewall 101 101
by going to the right ports. 102 102
Also, the sequence number and acknowledgment numbers 103 103
are going to be used to ensure all the data 104 104
is properly received by the destination 105 105
when it's sent by the original transmitter. 106 106
So this is also important when you're using TCP. 107 107
Another important concept in the TCP header 108 108
is the control flags. 109 109
There are six control flags that are used 110 110
to manage data flow before, during, 111 111
and to stop the data communication when you're finished. 112 112
You should already be familiar with the three-way handshake. 113 113
That uses the SYN packet sent by the client, 114 114
the SYN-ACK packet that's sent by the server, 115 115
and the ACK packet that the client sends back to the server 116 116
at the end. 117 117
These packets are sent using the TCP flags of SYN or ACK 118 118
inside your TCP header. 119 119
Now, in addition to the SYN and ACK flags, 120 120
there's also four others: FIN, RST, PSH, and URG. 121 121
First, we have the SYN flag or synchronization flag. 122 122
This is by far the most well-known flag 123 123
in TCP communications because it's used to synchronize 124 124
the connection during the three-way handshake. 125 125
Next, we have the ACK or acknowledgement flag. 126 126
This is also used during the three-way handshake, 127 127
but in addition to that, 128 128
we use it to acknowledge the successful receipt 129 129
of all the packets during the communication. 130 130
The FIN or finished packet is used to tear down 131 131
the virtual connection created by the three-way handshake 132 132
and the SYN flag. 133 133
The FIN flag always appears when the last packets 134 134
are exchanged between a client and a server, 135 135
and the host is now ready to shut down that connection. 136 136
Next, we have the RST flag or reset flag. 137 137
This is going to be used when a client or server 138 138
receives a packet that it was not expecting 139 139
during the current connection. 140 140
For example, if you tried to establish a connection 141 141
with a server that didn't want to accept any new connections, 142 142
it could send back an RST or reset flag 143 143
to inform your client that it's not accepting connections 144 144
and automatically reject a request. 145 145
The next one we have is a PSH flag or push. 146 146
Now, a push flag is used to ensure 147 147
the data is given priority 148 148
and is processed at the sending or receiving ends. 149 149
Most often, this flag is added to a packet 150 150
at the beginning or end of a data transfer. 151 151
The final flag we have is URG or the urgent flag. 152 152
The urgent flag is like the push flag 153 153
and it identifies incoming data as urgent. 154 154
Now, the main difference here between push and urgent 155 155
is that push is used by the sender 156 156
to indicate data with a higher priority level. 157 157
Now, urgent, on the other hand, 158 158
is sent to tell the recipient to process it immediately 159 159
and ignore anything else that's in the queue. 160 160
With urgent, this could lead to packets 161 161
violating the first in first out priority order, 162 162
so it needs to be used 163 163
only by particular applications when necessary. 164 164
Now, if you're using UDP instead of TCP, 165 165
you're going to be using the User Datagram Protocol. 166 166
Now, when we look at the User Datagram Protocol header, 167 167
this is another transport layer or Layer 4 header 168 168
that's going to be used in our networks. 169 169
Remember, UDP is unreliable 170 170
and it's a connectionless protocol, 171 171
so its header is significantly smaller than TCP. 172 172
With UDP, we only have an eight byte header 173 173
instead of the 20 byte header used in TCP. 174 174
UDP only has four fields that are used: 175 175
the source port, the destination port, the length, 176 176
and the checksum. 177 177
The source and destination ports 178 178
are just like the ones used in TCP. 179 179
They dictate where the data is coming from 180 180
and where it's going to. 181 181
The length is used to indicate 182 182
how many bytes the total UDP packet is, 183 183
including the header and its data. 184 184
The checksum is not a mandatory field, 185 185
but instead it can be used to provide some validation 186 186
that the UDP data being sent was actually received 187 187
with some level of integrity. 188 188
Next, let's move down to Layer 3, the network layer. 189 189
As we move down another layer, 190 190
we're going to again encapsulate the data and add a header. 191 191
This time, we're going to add the IP 192 192
or internet protocol header. 193 193
The IP header is going to contain several fields, 194 194
including the IP version, the length of the IP header, 195 195
the type of service, 196 196
which was defined by the standard but never really used, 197 197
the total length of the packet and header, 198 198
the identifier, the flags, the fragmented offset, 199 199
the time to live, the protocol, the header checksum, 200 200
the source IP, the destination IP, 201 201
and the options and padding. 202 202
Now, as we continue down the layers, 203 203
we're going to reach Layer 2, the data link layer, 204 204
and this is going to encapsulate the data 205 205
by adding an Ethernet header. 206 206
Now, this header features just a few things, 207 207
including a destination MAC address, the source MAC address, 208 208
the EtherType field, and an optional VLAN tag 209 209
using either IEEE, 802.1q, or IEEE 802.1ad. 210 210
We're going to talk more about VLANs in a separate video though 211 211
because it's an important concept. 212 212
So let's talk about a MAC address. 213 213
A MAC address is a physical address that's used to identify 214 214
a network card on your local area network. 215 215
This allows our source to find our destination 216 216
by using this type of Layer 2 addressing. 217 217
This is what's processed by switches in your network. 218 218
Now, the EtherType field is used to indicate 219 219
which protocol is encapsulated in the payload event frame. 220 220
So if you're using IPv4 or IPv6, 221 221
this can be indicated here using the EtherType field. 222 222
Now, in addition to the Ethernet header, 223 223
a frame being sent at Layer 2 will also contain a payload. 224 224
In Ethernet, the minimum payload is 42 bytes 225 225
if VLANs are being used 226 226
and 46 bytes if no VLANs are being used. 227 227
Now, when you're trying to send a payload, 228 228
there is a maximum size to this, 229 229
known as an MTU or maximum transmission unit. 230 230
When we talk about payloads, 231 231
this is the data we're trying to send across the network. 232 232
By default, Ethernet uses an MTU of 1,500 bytes 233 233
as its maximum size. 234 234
Now, if you have a payload that's larger than 1,500 bytes, 235 235
then you need to allow for what's known as a jumbo frame. 236 236
This just means the frame is going to be larger 237 237
than 1,500 bytes. 238 238
To configure this on your switch, 239 239
you're going to reconfigure your MTU size 240 240
or your maximum transmission unit size 241 241
to larger than 1,500 bytes. 242 242
All right, that was a ton of information we just covered, 243 243
but let's review a couple of main concepts here. 244 244
First, remember, as data moves from Layer 7 to Layer 1, 245 245
we are going to encapsulate that data. 246 246
So as we move down the OSI layers, 247 247
we're going to encapsulate that data 248 248
and add a header at each of those layers. 249 249
At Layer 4, we're going to add our source 250 250
and destination ports. 251 251
At Layer 3, we add our source and destination IP addresses. 252 252
At Layer 2, we add our source and destination MAC addresses. 253 253
Now, once we get to Layer 1, 254 254
we're simply transmitting our Layer 2 frames 255 255
as a series of ones and zeros over the medium. 256 256
When it's received by the next device, 257 257
for example a switch, 258 258
it's going to put the frames back together 259 259
from the electrical, optical, or radio frequency signals 260 260
that it received over Layer 1. 261 261
Now, it's going to decapsulate the Layer 2 information 262 262
by reading the Ethernet header. 263 263
If the destination MAC is on one of the switch ports, 264 264
it's going to send the message to it. 265 265
If not, it's going to forward it to its default gateway, 266 266
which is a router. 267 267
This router then decapsulates the data to Layer 3, 268 268
and it reads the destination IP address. 269 269
If it's on its network, 270 270
it's going to forward the data to that device. 271 271
If not, it's going to re-encapsulate the data 272 272
and send it out its default gateway. 273 273
And then this process will continue 274 274
until the final destination or host is found. 275 275
Now, once that host is found, 276 276
it's going to keep decapsulating that information 277 277
all the way back up to Layer 7 278 278
where its application can read 279 279
and understand the underlying data. 280 280
Now, we're going to cover a lot more 281 281
about how this data transfer happens 282 282
when we talk about switches and routers 283 283
later in this course. 284 284
But for now, this is the basics you need to understand.