TURN In Few Words

.

TURN is abbreviation for Traversal Using Relays around NAT. It is a control protocol that allows the host behind a NAT to exchange packets with its peers using the relay.  It is specified in the RFC [5766]. The following are few words about this protocol:

  • TURN is part of the ICE (Interactive Connectivity Establishment) but it can be used without ICE.
  • TURN is designed to solve the communication problem when both the client and its peer are behind respective NAT where the hole punching techniques (discovering direct communication path) may fail. In other words TURN is used when a direct communication path between the client and the pair can NOT be found.
  • The public TURN server sits between the two hosts that are behind NAT and relays the packets between them.
  • TURN is client-server protocol. The client is called TURN client and the server is called TURN server.
  • The TURN client obtains (using the TURN protocol -Allocate transaction) on the TURN server what is called relayed transport address (IP address and port).
  • The client sends CreatePermissions request to the TURN server to create permissions (permissions to validate the peer-server communication).
  • The TURN server sees the messages coming from the client as it is coming from a transport address on NAT. This address is called client’s server-reflexive transport address.
  • The NAT forwards packets coming to the client’s server-reflexive transport to the client’s host transport address (private address).
  • The TURN server receives the application data from the client, make the relayed transport address as the source of the packets and relays them to the peer using UDP datagrams.
  • The peer sends the application data in UDP packets to the client’s relayed transport address on the relay server. Then the server checks the permissions and on validation it relays the data to the client.
  • A way to communicate the relayed transport address and peers addresses (server-reflexive transport addresses) is needed (out of scope of the TURN protocol).

TURN

  • In VOIP, if TURN is used with ICE, then the client puts its obtained relayed transport address as an ICE candidate (among other candidates) in the SDP carried by the rendezvous protocol like SIP. When the other peers receives the SIP request, they will know how to reach the client.
  • The TURN messages (encapsulation of the application data) contains an indication of the peer the client is communicating with so the client can use a single relayed transport address to communicate with multiple peers. This is when the the rendezvous protocol (e.g. SIP) supports forking.
  • Using TURN is expensive so when TURN is used with ICE, the ICE uses hole punching techniques first to discover the direct path. If the direct path is not found, then TURN is used.
  • Using TURN makes the communication no longer peer to peer communication.

More Information


 

Linux NAT Using Conntrack and IPtables

.

Doing the Network Address Translation (NAT) into Linux kernel scales the performance up. This mechanism consists of two parts:

The Connection Tracking/Conntrack Modules

It is a tracking technique of the connections. It is used to know how the packets that pass through the system are related to their connections. The connection tracking does NOT manipulate the packets and It works independently of the NAT module. The conntrack entry looks like:

udp 17 170 src=192.168.1.2 dst=192.168.1.5 sport=137 dport=1025 src=192.168.1.5 dst=192.168.1.2 sport=1025 dport=137 [ASSURED] use=1

The conntrack entry is stored into two separate tuples (one for the original direction (red) and another for the reply direction (blue)). Tuples could belong to different linked lists/buckets in conntrack hash table. The connection tracking modules is responsible for creating and removing the tuples.

Note: The tracking of the connections is ALSO used by iptables to do packet matching based on the connection state.

The NAT Modules

The NAT modules do the NATing itself. They use the tuples and modify them based on the NATing rules. In this way the tuples in the connection tracking table remains in consistent state.

nat

If the packet belongs to an existing connection, this means there is already a conntrack entry (two tuples) in the conntrack table. The NAT module knows this by checking a field in the tuple created for the new arrived packet. Then the packet manipulation is done based on the conntrack entry (The manipulation is determined previously).

If the received packet represents a start of a new connection (first packet), the NAT module looks for a rule in the “NAT” table. If a rule is found, the NAT manipulation will be applied based on the rule and the tuples in the conntrack table will be changed. The tuples are created by conntrack at local outtput hook point before NAT for SNAT (Source NAT) so they need to be updated after doing the NAT for the first packet.

Assume the packets are leaving on network interface “eth1″(-o means “output”) to the internet and the interface “eth0” is connected to the local network. To change the source addresses to 1.2.3.4  and the ports 1-1023, you can add this rule:

# iptables -t nat -A POSTROUTING -p tcp -o eth1 -j SNAT –to 1.2.3.4:1-1023

You can specify a range of IP addresses as well (SNAT –to 1.2.3.4-1.2.3.6).

You can also use what is called MASQUERADE where the the sender’s address is replaced by the router’s address.

# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Note: Here i am doing SNAT (Source NAT). You can also do Destination NAT (DNAT) where the conntrack hooks into pre routing hook point. To write DNAT rules, use the chain  PREROUTING and the target DNAT.

NAT Settings

  • You need to load the “nf_conntrack”: # modprobe nf_conntrack
  • You need to start iptables service: # systemctl start iptables
  • You need to enable IP_Forwarding:
    • Temporarily: # echo “1” > /proc/sys/net/ipv4/ip_forward
    • Permanently:  Write net.ipv4.ip_forward = 1 in the file “/etc/sysctl.conf ” and reload (# sysctl -p).
  • Then set NATing rules as mentioned above.
  • Add Forwarding rules to forward packets from one interface to another in both direction:

From the public (interface:eth1) to private(interface eth0):

# iptables -A FORWARD -i eth1 -o eth0 -m state –state RELATED,ESTABLISHED -j ACCEPT

From private(eth0) to public(eth1):

# iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

  • Finally you need to save the IPtables rules to be persistent: # iptables-save

Note

  • If you got this error “nf_conntrack: table full, dropping packet“ and you have enough free memory , you can expand the size of conntrack table, click here.

More Information


RTPEngine Manual Compilation and Installation In Fedora RedHat

.

RTPEngine Main Features

  • OpenSource and free
  • Media traffic running over either IPv4 or IPv6
  • Bridging between IPv4 and IPv6 user agents
  • TOS/QoS field setting
  • Customizable port range
  • Multi-threaded
  • Advertising different addresses for operation behind NAT
  • In-kernel packet forwarding for low-latency and low-CPU performance
  • Automatic fallback to normal userspace operation if kernel module is unavailable
  • OpenSIPS/Kamailio Support.

Installation and Compilation

1- Install The Packages Required To Compile The Daemon:

# yum install glib glib-devel gcc zlib zlib-devel openssl openssl-devel pcre pcre-devel libcurl libcurl-devel xmlrpc-c xmlrpc-c-devel

Note: glib version must be higher than 2.0. Note in the Makefile of the daemon: CFLAGS+= `pkg-config –cflags glib-2.0`

2- Get The Latest Release Of RTPEngine Source From RTPEngine’s GitHub Repository:

Go to the place where “GitHub.com” is remotely storing RTPEngine and get the RTPEngine ‘s repo HTTP-address “https://github.com/sipwise/rtpengine.git“. Then clone the repo.

# git clone https://github.com/sipwise/rtpengine.git

Install redis client and server (If you want to have it on the same server):

# yum install  hiredis hiredis-devel redis

Be sure the server is running: # systemctl status redis

3- Compile The Daemon:

# cd /usr/local/src/rtpengine/daemon
# make

Now the daemon is compiled and we have the executable file rtpengine. Copy this file to /usr/sbin:

 # cp rtpengine /usr/sbin/rtpengine

4- Compile IPtables-Extension “libxt_RTPENGINE” Which Is User-Space Module Used For “In-Kernel” Packet Forwarding (Adding the forwarding rules):

  • Install IPtables development headers: # yum install iptables-devel
  • Compile the iptables extension

# cd /usr/local/src/rtpengine/iptables-extension

# make

After compilation we get the plugin “libxt_RTPENGINE.so” (user-space module). Copy this file to “/lib/xtables/” or “/lib64/xtables/” (see your system):

# cp libxt_RTPENGINE.so /lib64/xtables

5- Compile The Kernel Module “xt_RTPENGINE” Required For In-Kernel Packet Forwarding:

To compile a kernel module, we need the kernel development headers to be installed in “/lib/modules/$VERSION/build/” where VERSION is the kernel version. To know the kernel version, execute  “uname -r”. Here i have 3.11.10-301.fc20.x86_64 so the kernel headers must be here “/lib/modules/3.11.10-301.fc20.x86_64/build/”. where 3.11.10-301.fc20.x86_64 is the kernel that we compile the module with. To install the header files of specific kernel: # yum install kernel-devel-$(uname -r)

Then:

# cd /usr/local/src/rtpengine/kernel-module

# make

After compilation we get this file “xt_RTPENGINE.ko” which is a kernel module. Copy this module into “/lib/modules/3.11.10-301.fc20.x86_64/extra”.

# cp xt_RTPENGINE.ko /lib/modules/3.11.10-301.fc20.x86_64/extra

After this creates a list of module dependencies and add the list to “modules.dep”:

# depmod  /lib/modules/3.11.10-301.fc20.x86_64/extra/xt_RTPENGINE.ko
or for all modules:
# depmod -a

Note: Instead of manually coping the module to extra sub-folder and create the list of dependencies by calling “depmod”, you can add modules_install rule in the Makefile. This will copy the file to “extra” and create the dependencies list:

modules_install:

make -C $(KSRC) M=$(PWD) modules_install

and execute: # make modules_install

Load the module:

You need to boot from the kernel that you compiled the module against. Then yo can load the module as following:

# modprobe xt_RTPENGINE

To check if the module is loaded: # lsmod |grep xt_RTPENGINE

Output: xt_RTPENGINE           27145  0

After the module has been loaded, a new directory called /proc/rtpengine will appear. In this directory you find  two files: control (write-only, used for creating and deleting the forwarding tables) and list (read-only, list the current forwarding tables, it could be an empty list):

 # ls -l   /proc/rtpengine/control

–w–w—-. 1 root root 0 Oct 16 11:54 /proc/rtpengine/control

 # ls -l   /proc/rtpengine/list

 -r–r–r–. 1 root root 0 Oct 16 11:55 /proc/rtpengine/list

To add a forwarding table (done by the daemon) with an ID=$TableID (number between 0-63):

# echo ‘add $TableID’ > /proc/rtpengine/control

The folder  /proc/rtpengine/$TableID will be created. It will contain these files: blist (read-only), control (write-only, used by the daemon to update the forwarding rules), list (read-only), and status (read-only).

To delete a table: # echo ‘del $TableID’ > /proc/rtpengine/control

You can delete the table if it is not used by the rtpengine (the rtpengine is not running).

To display the list of the current tables: # cat /proc/rtpengine/list

You can unload the kernel module “xt_RTPENGINE” when there is no related iptables rules or forwarding tables:

# rmmod xt_RTPENGINE

Adding iptables rules to forward the incoming packets to xt_RTPENGINE module

# iptables -I INPUT -p udp -j RTPENGINE –id $TableID

# ip6tables -I INPUT -p udp -j RTPENGINE –id $TableID

Start The Daemon

Recommendation: Start the RTPEngine as systemd service with a configuration file to specify the options. Click here. Otherwise continue:

# ./rtpengine ….Options…..

Example:

# rtpengine –table=TableID –interface=$IPAddress –listen-ng=127.0.0.1:2223 –pidfile=/var/run/rtpengine.pid –no-fallback

For logging to file:

  • When you start rtpengine, add the options: log-level and log-facility:

# rtpengine –table=$TableID –interface=127.0.0.1 –listen-ng=127.0.0.1:2223 –pidfile=/var/run/rtpengine.pid –no-fallback –log-level=3  –log-facility=local1

The max value of log-level is 7

  • Assuming the /var/log/rtpengine.log will be the log file:

# vim  /etc/rsyslog.conf and add this entry:

local1.*                        /var/log/rtpengine.log

For the connection with Redis database:

  • Check the IP and the port of the Redis server: #  netstat -lpn |grep redis
  • Add the following options when you start the RTPEngine: –redis=IP:PORT and –redis-db=INT

See the list of command-line options in the project’s GitHub repository page. There you can find explanation about all RTPEngine’s stuffs with examples.


More Information


OpenSIPS and SIP PATH Extension

.

Introduction

RFC 3327 (SIP Path Extension) specifies a way to discover and record the sequence of proxies between the UA and the registrar. The constructed routing path will be stored in the database of the registrar along with the corresponding contact address. Each intermediate proxy (NOT terminating routing point) that wants to stay in the routing path, inserts its URI as a new “Path” header field (or as value in the “Path” header field). This mechanism is applied only to requests that are transiting or originating in the home domain. The constructed routing path will be carried as pre-loaded Route set in the upcoming initial requests. This mechanism is similar to the operation of Record-Route mechanism in dialog-initiating requests (In-dialog/Sequential requests) and it can be combined with it (Example – Invite Transaction).

In this article i will talk about SIP Path extension in OpenSIPS context.

Example

Assume we have this scenario:

UA1 – – – – P1 (Outbound Proxy) – – – – P2 – – – – Registrar/Home-Proxy (OpenSIPS) – – – – UA2

The outgoing proxy is the first hop after client’s NAT . The home proxy is a terminal point for SIP routing. The requested path starts by the outgoing proxy and ends at the registrar/home proxy. It is not common that the UA adds a “Path” header field in the request.

OpenSIPS As Registrar/Home-Proxy

OpenSIPS “Registrar” module has “Path” support. When OpenSIPS receives a SIP Register request, it calls the function save(..), among others. Upon calling this function, the values of the Path header(s) along with the contact address will be stored in the database (User Location Table). The contact address will be stored in the column “contact” and the values of Path header(s) will be stored in the column “path”.

In our scenario, if the proxies P1 and P2 receive a Register request and want to be in the path vector, each one adds its own SIP address as a top most value in the “Path” header field and forward the request. When the request reached the registrar, the path vector will be: Path: <sip:P2.testdomain.com;lr>,<sip:P1.testdomain.com;lr>.

The function save(…) takes some flags as a single string parameter. Among others, we have these flags that are related to the “Path” support:

  • p0‘ (OFF Mode): The path vector will be saved in the user location table but it will not be included in the reply of the Register request.
  • p1‘ (Lazy Mode): The path vector will be saved in the user location table and it will be included in the reply only if the UA indicated path support in the request (add the tag “path” as a value in the “Supported” header field of the request: “Supported: path”).
  • p2‘ (Strict Mode): The path vector will be saved in the user location table only if the the UA indicated path support in the registration request (add the tag “path” as a value in the “Supported” header field of the request). If it is not indicated, the request is rejected with “420 – Bad Extension” and the header “Unsupported: path” will be included in the reply along with the received path vector.

The flags above enable “path” support in “registrar” module and specify the mode of this support as well.

According to all above and if the path vector {p2,p1} is sent in the response, the UA can use the path (as preloaded Route set) in the future requests if that is allowed by the policy. In addition to this, any request reached the home proxy can be routed based on that path {p2,p1} which is stored in the database of the registrar.

According to RFC 3327:

The intermediate proxies (not terminating points) should not add a “Path” header field to a request unless the UA add the tag “path” as a value in the “Supported” header field in that request. Here we say the UA indicated support for PATH extension. If the UA indicated path support and the intermediate proxy requires path support in the registrar, then the proxy adds the tag “path” as a value in the “Requires” header field in that request. If the UA has not indicated support for path and the proxy requires support for it in the registrar, the proxy should reject the request with 421 response. This nearly corresponds to using the flag ‘p2’ in the “save” function: save(“location”,”p2″).

The “save(…)” function has also flag ‘v‘ (path received). If this flag is set, the “received” parameter of the first Path URI of a registration {p1} is set as received-uri and the NAT branch flag is set for this contact. This will be explained further down.

To use multiple flags in the “save(…)” function, mix them as one string. Example: save(“location”,”p2v”).

The routing script will have these lines, among others:

….

if (method == “REGISTER”) {

save(“location”, “p2v”);
}

Registrar’s Lookup Operation

When for example UA2 wants to call UA1 (Send Invite request). When the request reached OpenSIPS that operates as a registrar/home proxy, it looks up (Call the function lookup(..)) the AOR of UA1 (UA1@testdomain.com) and returns:

  • Contact = <sip:UA1@192.18.8.10>

  • Path vector= <sip:P2.testdomain.com;lr>,<sip:P1.testdomain.com;lr>

The Request-URI is overwritten by the registered contact address and the Path vector {p2,p1} is inserted as Route set in the same order {p2,p1} in the outgoing Invite request. The topmost Route header field (proxy: p2) is used for routing decision (next hop) in conjunction with the local policy. Calling this function, also sets the destination URI to the first Path URI (outbound proxy: p1).

OpenSIPS As Outbound Proxy with NAT Detection

OpenSIPS has a module named “path” which is designed to be used at intermediate SIP proxies (e.g. outgoing proxy, load balancer,..etc.). Assume we have this scenario:

UA1 – – – NAT – – – P1 (Outbound Proxy -OpenSIPS) – – – P2 – – – Registrar/Home-Proxy

P1 is OpenSIPS that operates as outgoing proxy with NAT detection. To use “path” module, it must be configured in the routing script to be loaded at startup (loadmodule “path.so”). The module “rr” must be loaded before so it must be configured to be loaded before. To insert a path header into Register message, two functions are exported by this module:

  • The function “add_path(…)” which adds a path header in this form “Path: <sip:1.2.3.4;lr>” .The IP address is the IP address of the proxy’s outgoing interface. A port is only added if it is not the default (5060). If it is called with username as parameter, then the username will be added to the URI “Path: <sip:username@1.2.3.4;lr>“.
  • The function “add_path_received(…)” which adds a path header in this form:

Path: <sip:1.2.3.4;received=sip:2.3.4.5:1234;lr>

In our scenario, the outbound proxy p1 will call the function “add_path_received” upon receiving a Register message. The received parameter of its Path URI contains the address where the request has been received (NAT’ed address). This is used to pass the NAT’ed address to the registrar through the next proxy if exists. If it is called with username then the username is added to the URI: “Path: <sip:username@1.2.3.4;received=sip:2.3.4.5:1234;lr>“. Then the proxy will forward the message further to next hop on a way to the registrar.

If the parameter “use_received” exported from “path” module is set to 1 by the outgoing proxy (modparam(“path”, “use_received”, 1), then when the proxy receives a request, the “received” parameter of the its Route URI  (first Route URI ) is evaluated and used as destination URI(route the request to this address).

This behaviour of outbound proxy is considered as complement to registrar behaviour (SIP Path Extension-aware behaviour of Save(…) and Lookup(…)). So Keep the outbound proxy in the constructed path.

The routing script will have these lines, among others:

loadmodule “path.so”

modparam(“path”, “use_received”, 1)

if (method == “REGISTER”) {
if (!add_path_received(“Outbound”))
sl_send_reply(“503”, “Internal Path Error”);

}


Notes

  • SIP “Path” extension is very useful for SIP loadbalancers that are in front of registrars and proxies. As it helps in drawing SIP routing paths.
  • If you like to manipulate the “received” parameter of the Path URI by yourself in the script (not recommended), you can use the script core variables: $Ri which is the IP address of the interface where the request has been received and $Rp which is the port number. Informations like these are detected by the transport of OpenSIPS (under the SIP layer).
  • If you do not understand the structure of the SIP URI. Go Here.

 More Information


Hairpinning, SIP loop, and Spiral

Here i would like to talk about: Hairpinning, SIP Spiral,  and SIP Loop.

  • Hairpinning: RFC 5128#section-2.10 defines hairpinning as following: ”If two hosts (called X1 and X2) are behind the same NAT and exchanging traffic, the NAT may allocate an address on the outside of the NAT for X2, called X2′:x2′. If X1 sends traffic to X2′:x2′, it goes to the NAT, which must relay the traffic from X1 to X2. This is referred to as hairpinning.”. Here is an example: Hairpinning
  • Spiral in SIP: as defined in RFC 3261: the condition where a SIP request routed to the proxy, forwarded onwards, and arrived again to that proxy, but this time differs in Request-URI. Example: call fowarding. Spiral is not considered as an error.
  • Loop in SIP: this is considered as an error. As in Spiral but when the request arrives the second time to the proxy, its Request-ID is identical to the first time. The proxy makes the same routing decision and we come to loop.