Debian Codegraph @

Xpjproject/pjnath/docs/doc_nat.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 /**
21
22 @defgroup nat_intro Introduction to Network Address Translation (NAT) and NAT Traversal
23 @brief This page describes NAT and the problems caused by it and the solutions
24
25
26
27 \section into Introduction to NAT
28
29
30 NAT (Network Address Translation) is a mechanism where a device performs
31 modifications to the TCP/IP address/port number of a packet and maps the
32 IP address from one realm to another (usually from private IP address to
33 public IP address and vice versa). This works by the NAT device allocating
34 a temporary port number on the public side of the NAT upon forwarding
35 outbound packet from the internal host towards the Internet, maintaining
36 this mapping for some predefined time, and forwarding the inbound packets
37 received from the Internet on this public port back to the internal host.
38
39
40 NAT devices are installed primarily to alleviate the exhaustion of IPv4
41 address space by allowing multiple hosts to share a public/Internet address.
42 Also due to its mapping nature (i.e. a mapping can only be created by
43 a transmission from an internal host), NAT device is preferred to be
44 installed even when IPv4 address exhaustion is not a problem (for example
45 when there is only one host at home), to provide some sort of security/shield
46 for the internal hosts against threats from the Internet.
47
48
49 Despite the fact that NAT provides some shields for the internal network,
50 one must distinguish NAT solution from firewall solution. NAT is not
51 a firewall solution. A firewall is a security solution designed to enforce
52 the security policy of an organization, while NAT is a connectivity solution
53 to allow multiple hosts to use a single public IP address. Understandably
54 both functionalities are difficult to separate at times, since many
55 (typically consumer) products claims to do both with the same device and
56 simply label the device a "NAT box". But we do want to make this distinction
57 rather clear, as PJNATH is a NAT traversal helper and not a firewall bypass
58 solution (yet).
59
60
61
62 \section problems The NAT traversal problems
63
64
65 While NAT would work well for typical client server communications (such as
66 web and email), since it's always the client that initiates the conversation
67 and normally client doesn't need to maintain the connection for a long time,
68 installation of NAT would cause major problem for peer-to-peer communication,
69 such as (and especially) VoIP. These problems will be explained in more detail
70 below.
71
72
73 \subsection peer_addr Peer address problem
74
75
76 In VoIP, normally we want the media (audio, and video) to flow directly
77 between the clients, since relaying is costly (both in terms of bandwidth
78 cost for service provider, and additional latency introduced by relaying).
79 To do this, each client informs its media transport address to the other
80 client , by sending it via the VoIP signaling path, and the other side would
81 send its media to this transport address.
82
83
84 And there lies the problem. If the client software is not NAT aware, then
85 it would send its private IP address to the other client, and the other
86 client would not be able to send media to this address.
87
88
89 Traditionally this was solved by using STUN. With this mechanism, the client
90 first finds out its public IP address/port by querying a STUN server, then
91 send sthis public address instead of its private address to the other
92 client. When both sides are using this mechanism, they can then send media
93 packets to these addresses, thereby creating a mapping in the NAT (also
94 called opening a "hole", hence this mechanism is also popularly called
95 "hole punching") and both can then communicate with each other.
96
97
98 But this mechanism does not work in all cases, as will be explained below.
99
100
101
102 \subsection hairpin Hairpinning behavior
103
104
105 Hairpin is a behavior where a NAT device forwards packets from a host in
106 internal network (lets call it host A) back to some other host (host B) in
107 the same internal network, when it detects that the (public IP address)
108 destination of the packet is actually a mapped IP address that was created
109 for the internal host (host B). This is a desirable behavior of a NAT,
110 but unfortunately not all NAT devices support this.
111
112
113 Lacking this behavior, two (internal) hosts behind the same NAT will not
114 be able to communicate with each other if they exchange their public
115 addresses (resolved by STUN above) to each other.
116
117
118
119 \subsection symmetric Symmetric behavior
120
121
122 NAT devices don't behave uniformly and people have been trying to classify
123 their behavior into different classes. Traditionally NAT devices are
124 classified into Full Cone, Restricted Cone, Port Restricted Cone, and
125 Symmetric types, according to <A HREF="http://www.ietf.org/rfc/rfc3489.txt">RFC 3489</A>
126 section 5. A more recent method of classification, as explained by
127 <A HREF="http://www.ietf.org/rfc/rfc4787.txt">RFC 4787</A>, divides
128 the NAT behavioral types into two attributes: the mapping behavior
129 attribute and the filtering behavior attribute. Each attribute can be
130 one of three types: <i>Endpoint-Independent</i>, <i>Address-Dependent</i>,
131 or <i>Address and Port-Dependent</i>. With this new classification method,
132 a Symmetric NAT actually is an Address and Port-Dependent mapping NAT.
133
134
135 Among these types, the Symmetric type is the hardest one to work with.
136 The problem is because the NAT allocates different mapping (of the same
137 internal host) for the communication to the STUN server and the
138 communication to the other (external) hosts, so the IP address/port that
139 is informed by one host to the other is meaningless for the recipient
140 since this is not the actual IP address/port mapping that the NAT device
141 creates. The result is when the recipient host tries to send a packet to
142 this address, the NAT device would drop the packet since it does not
143 recognize the sender of the packet as the "authorized" hosts to send
144 to this address.
145
146
147 There are two solutions for this. The first, we could make the client
148 smarter by switching transmission of the media to the source address of
149 the media packets. This would work since normally clients uses a well
150 known trick called symmetric RTP, where they use one socket for both
151 transmitting and receiving RTP/media packets. We also use this
152 mechanism in PJMEDIA media transport. But this solution only works
153 if a client behind a symmetric NAT is not communicating with other
154 client behind either symmetric NAT or port-restricted NAT.
155
156
157 The second solution is to use media relay, but as have been mentioned
158 above, relaying is costly, both in terms of bandwidth cost for service
159 provider and additional latency introduced by relaying.
160
161
162
163 \subsection binding_timeout Binding timeout
164
165 When a NAT device creates a binding (a public-private IP address
166 mapping), it will associate a timer with it. The timer is used to
167 destroy the binding once there is no activity/traffic associated with
168 the binding. Because of this, a NAT aware application that wishes to
169 keep the binding open must periodically send outbound packets,
170 a mechanism known as keep-alive, or otherwise it will ultimately
171 loose the binding and unable to receive incoming packets from Internet.
172
173
174 \section solutions The NAT traversal solutions
175
176
177 \subsection stun Old STUN (RFC 3489)
178
179 The original STUN (Simple Traversal of User Datagram Protocol (UDP)
180 Through Network Address Translators (NATs)) as defined by
181 <A HREF="http://www.ietf.org/rfc/rfc3489.txt">RFC 3489</A>
182 (published in 2003, but the work was started as early as 2001) was
183 meant to be a standalone, standard-based solution for the NAT
184 connectivity problems above. It is equipped with NAT type detection
185 algoritm and methods to hole-punch the NAT in order to let traffic
186 to get through and has been proven to be quite successful in
187 traversing many types of NATs, hence it has gained a lot of popularity
188 as a simple and effective NAT traversal solution.
189
190 But since then the smart people at IETF has realized that STUN alone
191 is not going to be enough. Besides its nature that STUN solution cannot
192 solve the symmetric-to-symmetric or port-restricted connection,
193 people have also discovered that NAT behavior can change for different
194 traffic (or for the same traffic overtime) hence it was concluded that
195 NAT type detection could produce unreliable results hence one should not
196 rely too much on it.
197
198 Because of this, STUN has since moved its efforts to different strategy.
199 Instead of attempting to provide a standalone solution, it's now providing
200 a part solution and framework to build other (STUN based) protocols
201 on top of it, such as TURN and ICE.
202
203
204 \subsection stunbis STUN/STUNbis (RFC 5389)
205
206 The Session Traversal Utilities for NAT (STUN) is the further development
207 of the old STUN. While it still provides a mechanism for a client to
208 query its public/mapped address to a STUN server, it has deprecated
209 the use of NAT type detection, and now it serves as a framework to build
210 other protocols on top of it (such as TURN and ICE).
211
212
213 \subsection midcom_turn Old TURN (draft-rosenberg-midcom-turn)
214
215 Traversal Using Relay NAT (TURN), a standard-based effort started as early
216 as in November 2001, was meant to be the complementary method for the
217 (old) STUN to complete the solution. The original idea was the host to use
218 STUN to detect the NAT type, and when it has found that the NAT type is
219 symmetric it would use TURN to relay the traffic. But as stated above,
220 this approach was deemed to be unreliable, and now the prefered way to use
221 TURN (and it's a new TURN specification as well) is to combine it with ICE.
222
223
224 \subsection turn TURN (draft-ietf-behave-turn)
225
226 Traversal Using Relays around NAT (TURN) is the latest development of TURN.
227 While the protocol details have changed a lot, the objective is still
228 the same, that is to provide relaying control for the application.
229 As mentioned above, preferably TURN should be used with ICE since relaying
230 is costly in terms of both bandwidth and latency, hence it should be used
231 as the last resort.
232
233
234 \subsection b2bua B2BUA approach
235
236 A SIP Back to Back User Agents (B2BUA) is a SIP entity that sits in the
237 middle of SIP traffic and acts as SIP user agents on both call legs.
238 The primary motivations to have a B2BUA are to be able to provision
239 the call (e.g. billing, enforcing policy) and to help with NAT traversal
240 for the clients. Normally a B2BUA would be equipped with media relaying
241 or otherwise it wouldn't be very useful.
242
243 Products that fall into this category include SIP Session Border
244 Controllers (SBC), and PBXs such as Asterisk are technically a B2BUA
245 as well.
246
247 The benefit of B2BUA with regard to helping NAT traversal is it does not
248 require any modifications to the client to make it go through NATs.
249 And since basically it is a relay, it should be able to traverse
250 symmetric NAT successfully.
251
252 However, since it is a relay, the usual relaying drawbacks apply,
253 namely the bandwidth and latency issue. More over, since a B2BUA acts
254 as user agent in either call-legs (i.e. it terminates the SIP
255 signaling/call on one leg, albeit it creates another call on the other
256 leg), it may also introduce serious issues with end-to-end SIP signaling.
257
258
259 \subsection alg ALG approach
260
261 Nowdays many NAT devices (such as consumer ADSL routers) are equipped
262 with intelligence to inspect and fix VoIP traffic in its effort to help
263 it with the NAT traversal. This feature is called Application Layer
264 Gateway (ALG) intelligence. The idea is since the NAT device knows about
265 the mapping, it might as well try to fix the application traffic so that
266 the traffic could better traverse the NAT. Some tricks that are
267 performed include for example replacing the private IP addresses/ports
268 in the SIP/SDP packet with the mapped public address/port of the host
269 that sends the packet.
270
271 Despite many claims about its usefullness, in reality this has given us
272 more problems than the fix. Too many devices such as these break the
273 SIP signaling, and in more advanced case, ICE negotiation. Some
274 examples of bad situations that we have encountered in the past:
275
276 - NAT device alters the Via address/port fields in the SIP response
277 message, making the response fail to pass SIP response verification
278 as defined by SIP RFC.
279 - In other case, the modifications in the Via headers of the SIP
280 response hides the important information from the SIP server,
281 nameny the actual IP address/port of the client as seen by the SIP
282 server.
283 - Modifications in the Contact URI of REGISTER request/response makes
284 the client unable to detect it's registered binding.
285 - Modifications in the IP addresses/ports in SDP causes ICE
286 negotiation to fail with ice-mismatch status.
287 - The complexity of the ALG processing in itself seems to have caused
288 the device to behave erraticly with managing the address bindings
289 (e.g. it creates a new binding for the second packet sent by the
290 client, even when the previous packet was sent just second ago, or
291 it just sends inbound packet to the wrong host).
292
293
294 Many man-months efforts have been spent just to troubleshoot issues
295 caused by these ALG (mal)functioning, and as it adds complexity to
296 the problem rather than solving it, in general we do not like this
297 approach at all and would prefer it to go away.
298
299
300 \subsection upnp UPnP
301
302 The Universal Plug and Play (UPnP) is a set of protocol specifications
303 to control network appliances and one of its specification is to
304 control NAT device. With this protocol, a client can instruct the
305 NAT device to open a port in the NAT's public side and use this port
306 for its communication. UPnP has gained popularity due to its
307 simplicity, and one can expect it to be available on majority of
308 NAT devices.
309
310 The drawback of UPnP is since it uses multicast in its communication,
311 it will only allow client to control one NAT device that is in the
312 same multicast domain. While this normally is not a problem in
313 household installations (where people normally only have one NAT
314 router), it will not work if the client is behind cascaded routers
315 installation. More over uPnP has serious issues with security due to
316 its lack of authentication, it's probably not the prefered solution
317 for organizations.
318
319 \subsection other Other solutions
320
321 Other solutions to NAT traversal includes:
322
323 - SOCKS, which supports UDP protocol since SOCKS5.
324
325
326
327 \section ice ICE Solution - The Protocol that Works Harder
328
329 A new protocol is being standardized (it's in Work Group Last Call/WGLC
330 stage at the time this article was written) by the IETF, called
331 Interactive Connectivity Establishment (ICE). ICE is the ultimate
332 weapon a client can have in its NAT traversal solution arsenals,
333 as it promises that if there is indeed one path for two clients
334 to communicate, then ICE will find this path. And if there are
335 more than one paths which the clients can communicate, ICE will
336 use the best/most efficient one.
337
338 ICE works by combining several protocols (such as STUN and TURN)
339 altogether and offering several candidate paths for the communication,
340 thereby maximising the chance of success, but at the same time also
341 has the capability to prioritize the candidates, so that the more
342 expensive alternative (namely relay) will only be used as the last
343 resort when else fails. ICE negotiation process involves several
344 stages:
345
346 - candidate gathering, where the client finds out all the possible
347 addresses that it can use for the communication. It may find
348 three types of candidates: host candidate to represent its
349 physical NICs, server reflexive candidate for the address that
350 has been resolved from STUN, and relay candidate for the address
351 that the client has allocated from a TURN relay.
352 - prioritizing these candidates. Typically the relay candidate will
353 have the lowest priority to use since it's the most expensive.
354 - encoding these candidates, sending it to remote peer, and
355 negotiating it with offer-answer.
356 - pairing the candidates, where it pairs every local candidates
357 with every remote candidates that it receives from the remote peer.
358 - checking the connectivity for each candidate pairs.
359 - concluding the result. Since every possible path combinations are
360 checked, if there is a path to communicate ICE will find it.
361
362
363 There are many benetifs of ICE:
364
365 - it's standard based.
366 - it works where STUN works (and more)
367 - unlike standalone STUN solution, it solves the hairpinning issue,
368 since it also offers host candidates.
369 - just as relaying solutions, it works with symmetric NATs. But unlike
370 plain relaying, relay is only used as the last resort, thereby
371 minimizing the bandwidth and latency issue of relaying.
372 - it offers a generic framework for offering and checking address
373 candidates. While the ICE core standard only talks about using STUN
374 and TURN, implementors can add more types of candidates in the ICE
375 offer, for example UDP over TCP or HTTP relays, or even uPnP
376 candidates, and this could be done transparently for the remote
377 peer hence it's compatible and usable even when the remote peer
378 does not support these.
379 - it also adds some kind of security particularly against DoS attacks,
380 since media address must be acknowledged before it can be used.
381
382
383 Having said that, ICE is a complex protocol to implement, making
384 interoperability an issue, and at this time of writing we don't see
385 many implementations of it yet. Fortunately, PJNATH has been one of
386 the first hence more mature ICE implementation, being first released
387 on mid-2007, and we have been testing our implementation at
388 <A HREF="http://www.sipit.net">SIP Interoperability Test (SIPit)</A>
389 events regularly, so hopefully we are one of the most stable as well.
390
391
392 \section pjnath PJNATH - The building blocks for effective NAT traversal solution
393
394 PJSIP NAT Helper (PJNATH) is a library which contains the implementation
395 of standard based NAT traversal solutions. PJNATH can be used as a
396 stand-alone library for your software, or you may use PJSUA-LIB library,
397 a very high level library integrating PJSIP, PJMEDIA, and PJNATH into
398 simple to use APIs.
399
400 PJNATH has the following features:
401
402 - STUNbis implementation, providing both ready to use STUN-aware socket
403 and framework to implement higher level STUN based protocols such as
404 TURN and ICE.
405 - NAT type detection, useful for troubleshooting purposes.
406 - TURN implementation.
407 - ICE implementation.
408
409
410 More protocols will be implemented in the future.
411
412 Go back to \ref index.
413
414 */