Kamailio installation and configuration

Kamailio is an Open Source SIP Server released under the GPLv2+ license, able of handling thousands of call setups per second, fully supporting the SIP protocol defined in IETF RFC 3261, including the various annexes and addons. The main use of Kamailio is as a SIP Proxy for routing SIP messages, but several additional features are also supported and described on the official website at the link https://www.kamailio.org/w/. On the official documentation you will find the details of the various modules supported by the system, here instead we will briefly see how to install and configure Kamailio for handling SIP calls.

Install and start Kamailio

As setup environment we will use a CentOS 7 server.

	yum install yum-utils
	yum-config-manager --add-repo https://rpm.kamailio.org/centos/kamailio.repo

So let’s check the presence of the repository for Kamailio packages kamailio.repo in /etc/yum.repos.d

	[kamailio]
	name=Kamailio - latest - Packages for the Kamailio latest release
	baseurl=https://rpm.kamailio.org/centos/$releasever/latest/$basearch/
	enabled=1
	metadata_expire=30d
	repo_gpgcheck=0
	gpgkey=https://rpm.kamailio.org/rpm-pub.key
	type=rpm
	skip_if_unavailable=True
	...
	...

Kamailio setup

	yum install kamailio

Once installed, we will find the configuration files in /etc/kamailio (the configuration file is kamailio.cfg) and the unit file is already configured to start the service.

Log configuration

With the default configuration Kamailio will generate logs on the local0 facility, so for a correct management of the logs it is necessary to edit the file /etc/rsyslog.conf as follows:

Add the following line

	#kamailio log
	local0.*   -/var/log/kamailio/kamailio.log

Disable writing log in the messages file

	# Don't log private authentication messages!
	*.info;mail.none;authpriv.none;cron.none;local0.none		/var/log/messages

Configure log rotation adding file /etc/logrotate.d/kamailio

	/var/log/kamailio/kamailio.log {
	missingok
	notifempty
	size 50M
	rotate 5
	dateformat -%Y%m%d-%s
	postrotate
	  /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
	endscript
	}

Finally, create the folder and file where the logs will be produced

	mkdir -p /var/log/kamailio
	touch /var/log/kamailio/kamailio.log
	systemctl restart rsyslog

Now we can start Kamailio with its default configuration

	systemctl start kamailio

Configuring Kamailio to rotate a received call

Suppose we want to configure the system to receive a call by a client to a specific number and rotate it to a destination server. In my case I used the sipp simulator both as a client and as a server (you can find here a short installation and configuration guide), starting two instances of sipp on the same machine, but on different ports, obviously. Going into more detail, my network topology is as follows:

  • SIP Client: IP 10.0.1.66 port 5065
  • Kamailio SIP Proxy: IP 10.0.1.215 port 5060
  • SIP Server: IP 10.0.1.66 port 5060

In the configuration file /etc/kamailio/kamailio.cfg we add a comment to the line if (uri==myself) return; in the SIPOUT route otherwise Kamailio will not RELAY calls directed to itself

	# Routing to foreign domains
	route[SIPOUT] {
		#if (uri==myself) return;

		append_hf("P-Hint: outbound\r\n");
		route(RELAY);
		exit;
	}

Then let’s add our routing rule inside the RELAY route

	route[RELAY] {

		# enable additional event routes for forwarded requests
		# - serial forking, RTP relaying handling, a.s.o.
		if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
			if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
		}
		if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
			if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
		}
		if (is_method("INVITE")) {
			if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
		}
		if (is_method("INVITE")) {
			if ($rU == "12345") {
				if (!t_relay_to_udp("10.0.1.66","5060")) {
					sl_reply_error();
				}
			}
		}
			else if (is_method("ACK")) {
					t_relay();
			}
			else if (is_method("BYE")) {
					t_relay();
			}
		else {
			if (!t_relay()) {
				sl_reply_error();
			}
		}
		exit;
	}

In particular we are intercepting the User part of the Request Uri (12345) present on the received INVITE and we are carrying out the relay to the server. The image below shows the entire call flow of the call between elements of our test environment. After changing the configuration you need to restart Kamailio.

Basic SIP Call

The call flow was obtained using sngrep tool installed on the same machine where Kamailio is running.