Skip to content

Kamailio Configuration Tutorial: kamailio.cfg Explained

Learn how kamailio.cfg is structured, what each routing block does, how the default request_route works and how to check the file before restarting.

By · Updated · Published

Photo of network server rack with cables
Photo by andrewfhart · CC BY-SA 2.0 · via Flickr

How is kamailio.cfg organized?

Flow chart of the three parts of kamailio.cfg in order: global parameters such as listen, then module settings that load and configure modules, then routing blocks, where only request_route is mandatory.
The three parts of kamailio.cfg, in order

The kamailio.cfg file has three parts. The core cookbook lists them in this order: global parameters, module settings and routing blocks. Global parameters are plain name=value lines. The most common one is listen, which sets the network addresses Kamailio listens on.

If there is no listen line, the default config binds to all local IP addresses. The commented example in the default file uses a placeholder address. The examples in this post use 192.0.2.10 as an example address, an address from the range reserved for documentation, so replace it with the address of your own server.

Module settings come second. They load modules and configure them. Routing blocks come last, and they hold the logic that decides what happens to each message. Only one of them is mandatory, and that is request_route. In kamailio.cfg, the skeleton below shows the three parts in order.

Kamailio config
listen=udp:192.0.2.10:5064

loadmodule "tm"

request_route {
    # routing logic goes here
}

What does request_route do, and what happens if it does not reply or forward?

request_route runs for every SIP request, and it decides how that request is routed. It is the only mandatory routing block because every request needs a decision. That decision can be an answer sent back to the sender, or the request passed on to another server.

The key rule is the implicit action. When the main route ends, Kamailio drops the SIP request. Nothing is sent back and nothing is forwarded unless the script does it. A script that never calls t_relay(), forward() or sl_send_reply() silently ignores every request it receives.

Replies and forwards therefore have to be explicit. The stateless forward() function sends the request to the address held in $du or $ru. The stateful t_relay() function belongs to the tm module, so that module has to be loaded first. The sl_send_reply() function sends a stateless reply to the current request, with a code and a reason text.

In kamailio.cfg, the request_route below loads tm and sl, rejects a request that has no user part in the Request-URI, and relays everything else.

Kamailio config
loadmodule "tm"
loadmodule "sl"

request_route {
    if ($rU == $null) {
        sl_send_reply("484", "Address Incomplete");
        exit;
    }
    t_relay();
}

The if line checks $rU, the username in the Request-URI. If it is empty, the script sends a 484 reply, and exit stops the script, which works like return(0). Every other request reaches t_relay(), so every path ends with an explicit action.

Which routing blocks are there, and when does each run?

Open Humans Review
Photo by juhansonin · CC BY 2.0 · via Flickr

Each routing block runs for a different trigger, and the table below lists them. It was compiled on November 14, 2026 from the official Kamailio documentation and the default kamailio.cfg. It was not tested. Each row shows the block, when it runs and how it is armed. Armed means the script must first call a function such as t_on_branch, which switches the block on for the transaction.

BlockRuns forHow it is armed
request_routeEach SIP requestAlways. The implicit action afterwards is to drop the request.
route[NAME]A sub-route called with route(NAME)Called from another block. It can return an integer, read via $rc.
branch_route[NAME]Each outgoing branch of a requestt_on_branch (tm)
failure_route[NAME]A transaction whose branches got only negative repliest_on_failure (tm)
reply_routeAll replies received from the networkOptional. Without it, the reply goes to the second Via address.
onreply_route[NAME]Replies handled by tmt_on_reply (tm)
onsend_routeA request being sent outNot for replies, retransmissions or locally generated messages.
event_route[group:event]Specific events, such as core:msg-receivedBy the event

Sub-routes are the blocks you call yourself. route[NAME] defines one, and route(NAME) runs it from any other block. The branch_route block is different. It runs for each outgoing branch of a request, but only after t_on_branch has armed it.

The failure_route block handles a transaction whose branches got only negative replies, meaning replies of 300 or higher. It is armed with t_on_failure. The cookbook uses the name TOVOICEMAIL in its example.

reply_route receives every reply that arrives from the network. If the script does not define it, the reply goes to the second Via address. onreply_route is different. The tm module runs it for replies inside a transaction, and t_on_reply arms it.

onsend_route runs when a request is sent out. It does not run for replies, retransmissions or locally generated messages. event_route runs when a named event happens, such as core:msg-received.

What does the default request_route do, step by step?

The default request_route runs a fixed sequence of sub-routes and checks, in the order below. The default kamailio.cfg on the GitHub master branch is labelled Kamailio SIP Server v6.2. The latest stable release listed on the Kamailio news page is v6.1.5, released October 15, 2026. Check which version you run before you copy lines from master.

The default request_route runs these steps in order:

  1. route(REQINIT) checks the request. It rejects requests with too many hops, using mf_process_maxfwd_header("10") and sl_send_reply("483", "Too Many Hops").
  2. route(NATDETECT) runs NAT detection.
  3. CANCEL and retransmission handling come next.
  4. route(WITHINDLG) handles requests that belong to a dialog that already exists.
  5. route(AUTH) runs authentication.
  6. remove_hf("Route") and record_route() run for INVITE, SUBSCRIBE and REFER requests.
  7. route(SIPOUT) runs, followed by route(PRESENCE).
  8. route(REGISTRAR) handles registration.
  9. If $rU==$null, the script sends sl_send_reply("484", "Address Incomplete").
  10. route(PSTN) runs, and then route(LOCATION) runs last.

In kamailio.cfg, the skeleton below keeps only the route calls, in the same order. The routes it calls are defined elsewhere in the default file. The CANCEL and retransmission steps, the record-routing lines and the 484 check are left out for space.

Kamailio config
request_route {
    route(REQINIT);
    route(NATDETECT);
    # CANCEL and retransmission handling goes here
    route(WITHINDLG);
    route(AUTH);
    route(SIPOUT);
    route(PRESENCE);
    route(REGISTRAR);
    route(PSTN);
    route(LOCATION);
}

The last stop, route(LOCATION), calls lookup("location"). Depending on the return code, it replies 404 Not Found or 405 Method Not Allowed, and otherwise relays the call. Relaying is done by route[RELAY], which arms handlers before it calls t_relay(). It arms t_on_branch("MANAGE_BRANCH") and t_on_reply("MANAGE_REPLY"). It also arms t_on_failure("MANAGE_FAILURE"), and the default file sets that failure handler only when no failure route is already set. If relaying fails, it sends an error reply.

How do you turn features on with #!define?

Features are switched on with #!define WITH_FEATURE lines. The default config names several of them, including WITH_MYSQL, WITH_AUTH, WITH_USRLOCDB, WITH_NAT, WITH_RTPENGINE, WITH_TLS, WITH_PSTN and WITH_ANTIFLOOD. Each name is a switch that the rest of the file can test.

The preprocessor also has #!ifdef, #!ifndef, #!else and #!endif. Code between an #!ifdef line and its #!endif is kept only when that name is defined. The #!trydef directive defines a name only if it is not already defined.

In kamailio.cfg, the lines below turn on two features and keep one block only when WITH_AUTH is defined.

Kamailio config
#!define WITH_NAT
#!define WITH_AUTH

#!ifdef WITH_AUTH
# authentication settings go here
#!endif

The same switch can be set at startup with the -A option, for example -A WITH_AUTH. Files can be pulled in with include_file, which gives an error if the file is missing, or with import_file, which does not.

How do sub-routes, return values and pseudo-variables work?

route(NAME) runs the sub-route defined as route[NAME]. A sub-route can end with return(n). The caller reads the returned code in $rc, which holds the return code of the last function invoked. A negative value counts as false and a positive value counts as true, so a caller can write if ($rc).

exit stops the script and behaves like return(0). drop also stops the script, but it changes the implicit action instead. In branch_route, drop discards the branch.

Pseudo-variables expose parts of the message. The request method is in $rm, and the request URI is in $ru. The username part of the request URI is in $rU. The From and To URIs are in $fu and $tu. The source IP and source port are in $si and $sp, and the Call-ID is in $ci.

For values of your own, use $var(name), which holds a script variable with an integer or string value. The value of the AVP named id is in $avp(id).

In kamailio.cfg, the example below uses one sub-route to check the Request-URI and returns a result to the caller.

Kamailio config
loadmodule "tm"
loadmodule "sl"

route[CHECKUSER] {
    if ($rU == $null) {
        sl_send_reply("484", "Address Incomplete");
        exit;
    }
    return(1);
}

request_route {
    route(CHECKUSER);
    if ($rc) {
        t_relay();
    }
}

When the username is empty, the sub-route sends a reply, and exit stops the whole script, so the caller never reads $rc. Otherwise the sub-route returns 1, which is positive and therefore true, and the caller relays the request.

How do you check kamailio.cfg before restarting?

Run kamailio -c to check the file for syntax errors before you restart. The -f option points to a different file. Without it, Kamailio uses the default path, /usr/local/etc/kamailio/kamailio.cfg. The -A option adds a preprocessor define, so you can check a feature such as WITH_AUTH without editing the file.

The --cfg-print option prints the config with includes and #!ifdef blocks already evaluated. That shows what the server will actually read. The -E option logs to stderr.

Shell
kamailio -c -f /usr/local/etc/kamailio/kamailio.cfg
kamailio -c -f ./kamailio.cfg -A WITH_AUTH
kamailio --cfg-print -f ./kamailio.cfg

Advanced examples: how do the pieces fit together?

Flow chart of the relay sub-route: t_on_branch arms the branch route, t_on_reply arms the reply route, t_on_failure arms the failure route, then t_relay() sends the request on, so the handlers are ready before the request leaves.
How route[RELAY] arms handlers before relaying

These two examples use only the parts covered above. They follow the 6.0.x cookbook and were not tested. The addresses and names are examples, and each one should be checked with kamailio -c before use. The latest release is v6.1.5, released October 15, 2026, so check the names against the version you run.

Example one is a minimal registrar. The registrar module's save("location") adds, removes or updates location records, depending on the Contact and Expires headers. lookup("location") then finds the contacts for the Request-URI.

In kamailio.cfg, the script below handles registration and relays other requests.

Kamailio config
# Load the modules the script uses
loadmodule "tm"
loadmodule "sl"
loadmodule "usrloc"
loadmodule "registrar"

request_route {
    # REGISTER: store or remove the contact
    if ($rm == "REGISTER") {
        save("location");
        exit;
    }
    # Other requests: find the contacts for the Request-URI
    if (!lookup("location")) {
        sl_send_reply("404", "Not found");
        exit;
    }
    t_relay();
}

Line by line, the first branch handles registration. The variable $rm holds the request method, and save("location") writes the contact before exit ends the script. The registrar keeps its location records in usrloc, so usrloc is loaded before it, and sl provides sl_send_reply(). The second branch looks up the Request-URI and sends a 404 reply when no contact is found. Otherwise t_relay() sends the request on.

Example two is a relay sub-route that arms the transaction handlers before it relays the request.

Kamailio config
# The handlers below must be defined elsewhere in the file
route[RELAY] {
    # Arm the branch, reply and failure handlers for this transaction
    t_on_branch("MANAGE_BRANCH");
    t_on_reply("MANAGE_REPLY");
    t_on_failure("MANAGE_FAILURE");
    t_relay();
}

Line by line, t_on_branch and t_on_reply arm the branch and reply routes for this transaction. t_on_failure arms the failure route. t_relay() comes last, so the handlers are ready before the request leaves. The handlers must be defined as branch_route[MANAGE_BRANCH], onreply_route[MANAGE_REPLY] and failure_route[MANAGE_FAILURE]. Control passes to the failure route only after the transaction has completed with a negative result, meaning every branch got a reply of 300 or higher.

Start with the first example, run kamailio -c on it, and only then add the next one.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

How do loadmodule and modparam work?
loadmodule "modulepath" loads a module, for example loadmodule "tm". modparam sets one parameter of a loaded module. The documented example modparam("debugger", "cfgtrace", 1) sets the cfgtrace parameter of the debugger module to 1.
Why does a fresh Kamailio install drop all SIP traffic?
The default kamailio.cfg drops all SIP traffic until ACTIVE is defined, Kamailio is started with -A ACTIVE, or WITH_AUTH or WITH_IPAUTH is enabled. Any one of these switches the server on.

Sources

  1. kamailio.cfg has three parts: global parameters, modules settings and routing blocks. (www.kamailio.org)
  2. The tm module's t_relay([host, port]) relays a SIP request statefully; t_on_failure, t_on_ (www.kamailio.org)
  3. The default kamailio.cfg on the master branch is labelled 'Kamailio SIP Server v6.2' and t (github.com)
  4. The latest Kamailio release listed on kamailio.org is v6.1.5, released October 15, 2026. (www.kamailio.org)