3 # Kamailio (OpenSER) SIP Server v5.1 - default configuration script
4 # - web: https://www.kamailio.org
5 # - git: https://github.com/kamailio/kamailio
7 # Direct your questions about this file to: <sr-users@lists.kamailio.org>
9 # Refer to the Core CookBook at https://www.kamailio.org/wiki/
10 # for an explanation of possible statements, functions and parameters.
12 # Note: the comments can be:
13 # - lines starting with #, but not the pre-processor directives,
14 # which start with #!, like #!define, #!ifdef, #!endif, #!else, #!trydef,
15 # #!subst, #!substdef, ...
16 # - lines starting with //
17 # - blocks enclosed in between /* */
19 # Several features can be enabled using '#!define WITH_FEATURE' directives:
21 # *** To run in debug mode:
24 # *** To enable mysql:
27 # *** To enable authentication execute:
30 # - add users using 'kamctl'
32 # *** To enable IP authentication execute:
34 # - enable authentication
35 # - define WITH_IPAUTH
36 # - add IP addresses with group id '1' to 'address' table
38 # *** To enable persistent user location execute:
40 # - define WITH_USRLOCDB
42 # *** To enable presence server execute:
44 # - define WITH_PRESENCE
46 # *** To enable nat traversal execute:
48 # - install RTPProxy: http://www.rtpproxy.org
50 # rtpproxy -l _your_public_ip_ -s udp:localhost:7722
51 # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
53 # *** To enable PSTN gateway routing execute:
55 # - set the value of pstn.gw_ip
56 # - check route[PSTN] for regexp routing condition
58 # *** To enable database aliases lookup execute:
60 # - define WITH_ALIASDB
62 # *** To enable speed dial lookup execute:
64 # - define WITH_SPEEDDIAL
66 # *** To enable multi-domain support execute:
68 # - define WITH_MULTIDOMAIN
70 # *** To enable TLS support execute:
71 # - adjust CFGDIR/tls.cfg as needed
74 # *** To enable XMLRPC support execute:
75 # - define WITH_XMLRPC
76 # - adjust route[XMLRPC] for access policy
78 # *** To enable anti-flood detection execute:
79 # - adjust pike and htable=>ipban settings as needed (default is
80 # block if more than 16 requests in 2 seconds and ban for 300 seconds)
81 # - define WITH_ANTIFLOOD
83 # *** To block 3XX redirect replies execute:
84 # - define WITH_BLOCK3XX
86 # *** To block 401 and 407 authentication replies execute:
87 # - define WITH_BLOCK401407
89 # *** To enable VoiceMail routing execute:
90 # - define WITH_VOICEMAIL
91 # - set the value of voicemail.srv_ip
92 # - adjust the value of voicemail.srv_port
94 # *** To enhance accounting execute:
97 # - add following columns to database
99 ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
100 ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
101 ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
102 ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
103 ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
104 ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
105 ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
106 ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
107 ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
108 ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
109 ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
110 ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
113 ####### Include Local Config If Exists #########
114 import_file "kamailio-local.cfg"
116 ####### Defined Values #########
118 # *** Value defines - IDs used later in config
120 # - database URL - used to connect to database server by modules such
121 # as: auth_db, acc, usrloc, a.s.o.
123 #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
126 #!ifdef WITH_MULTIDOMAIN
127 # - the value for 'use_domain' parameters
128 #!define MULTIDOMAIN 1
130 #!define MULTIDOMAIN 0
134 # FLT_ - per transaction (message) flags
135 # FLB_ - per branch flags
137 #!define FLT_ACCMISSED 2
138 #!define FLT_ACCFAILED 3
142 #!define FLB_NATSIPPING 7
144 ####### Global Parameters #########
146 ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
158 log_facility=LOG_LOCAL0
160 /* number of SIP routing processes */
163 /* uncomment the next line to disable TCP (default on) */
166 /* uncomment the next line to disable the auto discovery of local aliases
167 * based on reverse DNS on IPs (default on) */
170 /* add local domain aliases */
171 #alias="sip.mydomain.com"
173 /* uncomment and configure the following line if you want Kamailio to
174 * bind on a specific interface/port/proto (default bind on all available) */
175 # listen=udp:10.0.0.10:5060
177 /* port to listen to (default 5060 for udp, tcp, scrtp, or 5061 for tls)*/
184 /* life time of TCP connection when there is no traffic
185 * - a bit higher than registration expires to cope with UA behind NAT */
186 tcp_connection_lifetime=3605
188 ####### Custom Parameters #########
190 /* These parameters can be modified runtime via RPC interface
191 * - see the documentation of 'cfg_rpc' module.
193 * Format: group.id = value 'desc' description
194 * Access: $sel(cfg_get.group.id) or @cfg_get.group.id */
199 * - pstn.gw_ip: valid IP or hostname as string value, example:
200 * pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
202 * - by default is empty to avoid misrouting */
203 pstn.gw_ip = "" desc "PSTN GW Address"
204 pstn.gw_port = "" desc "PSTN GW Port"
207 #!ifdef WITH_VOICEMAIL
208 /* VoiceMail Routing on offline, busy or no answer
210 * - by default Voicemail server IP is empty to avoid misrouting */
211 voicemail.srv_ip = "" desc "VoiceMail IP Address"
212 voicemail.srv_port = "5060" desc "VoiceMail Port"
215 ####### Modules Section ########
217 /* set paths to location of modules */
218 # mpath="/usr/local/lib/kamailio/modules/"
221 loadmodule "db_mysql.so"
224 loadmodule "jsonrpcs.so"
226 loadmodule "corex.so"
232 loadmodule "maxfwd.so"
233 loadmodule "usrloc.so"
234 loadmodule "registrar.so"
235 loadmodule "textops.so"
236 loadmodule "siputils.so"
238 loadmodule "sanity.so"
240 loadmodule "cfg_rpc.so"
242 loadmodule "counters.so"
246 loadmodule "auth_db.so"
248 loadmodule "permissions.so"
253 loadmodule "alias_db.so"
256 #!ifdef WITH_SPEEDDIAL
257 loadmodule "speeddial.so"
260 #!ifdef WITH_MULTIDOMAIN
261 loadmodule "domain.so"
264 #!ifdef WITH_PRESENCE
265 loadmodule "presence.so"
266 loadmodule "presence_xml.so"
270 loadmodule "nathelper.so"
271 loadmodule "rtpproxy.so"
278 #!ifdef WITH_ANTIFLOOD
279 loadmodule "htable.so"
284 loadmodule "xmlrpc.so"
288 loadmodule "debugger.so"
291 # ----------------- setting module-specific parameters ---------------
294 # ----- jsonrpcs params -----
295 modparam("jsonrpcs", "pretty_format", 1)
296 /* set the path to RPC fifo control file */
297 # modparam("jsonrpcs", "fifo_name", "/var/run/kamailio/kamailio_rpc.fifo")
298 /* set the path to RPC unix socket control file */
299 # modparam("jsonrpcs", "dgram_socket", "/var/run/kamailio/kamailio_rpc.sock")
301 # ----- ctl params -----
302 /* set the path to RPC unix socket control file */
303 # modparam("ctl", "binrpc", "unix:/var/run/kamailio/kamailio_ctl")
305 # ----- tm params -----
306 # auto-discard branches from previous serial forking leg
307 modparam("tm", "failure_reply_mode", 3)
308 # default retransmission timeout: 30sec
309 modparam("tm", "fr_timer", 30000)
310 # default invite retransmission timeout after 1xx: 120sec
311 modparam("tm", "fr_inv_timer", 120000)
313 # ----- rr params -----
314 # set next param to 1 to add value to ;lr param (helps with some UAs)
315 modparam("rr", "enable_full_lr", 0)
316 # do not append from tag to the RR (no need for this script)
317 modparam("rr", "append_fromtag", 0)
319 # ----- registrar params -----
320 modparam("registrar", "method_filtering", 1)
321 /* uncomment the next line to disable parallel forking via location */
322 # modparam("registrar", "append_branches", 0)
323 /* uncomment the next line not to allow more than 10 contacts per AOR */
324 # modparam("registrar", "max_contacts", 10)
325 /* max value for expires of registrations */
326 modparam("registrar", "max_expires", 3600)
327 /* set it to 1 to enable GRUU */
328 modparam("registrar", "gruu_enabled", 0)
330 # ----- acc params -----
331 /* what special events should be accounted ? */
332 modparam("acc", "early_media", 0)
333 modparam("acc", "report_ack", 0)
334 modparam("acc", "report_cancels", 0)
335 /* by default ww do not adjust the direct of the sequential requests.
336 * if you enable this parameter, be sure the enable "append_fromtag"
338 modparam("acc", "detect_direction", 0)
339 /* account triggers (flags) */
340 modparam("acc", "log_flag", FLT_ACC)
341 modparam("acc", "log_missed_flag", FLT_ACCMISSED)
342 modparam("acc", "log_extra",
343 "src_user=$fU;src_domain=$fd;src_ip=$si;"
344 "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
345 modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
346 /* enhanced DB accounting */
348 modparam("acc", "db_flag", FLT_ACC)
349 modparam("acc", "db_missed_flag", FLT_ACCMISSED)
350 modparam("acc", "db_url", DBURL)
351 modparam("acc", "db_extra",
352 "src_user=$fU;src_domain=$fd;src_ip=$si;"
353 "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
356 # ----- usrloc params -----
357 /* enable DB persistency for location entries */
358 #!ifdef WITH_USRLOCDB
359 modparam("usrloc", "db_url", DBURL)
360 modparam("usrloc", "db_mode", 2)
361 modparam("usrloc", "use_domain", MULTIDOMAIN)
364 # ----- auth_db params -----
366 modparam("auth_db", "db_url", DBURL)
367 modparam("auth_db", "calculate_ha1", yes)
368 modparam("auth_db", "password_column", "password")
369 modparam("auth_db", "load_credentials", "")
370 modparam("auth_db", "use_domain", MULTIDOMAIN)
372 # ----- permissions params -----
374 modparam("permissions", "db_url", DBURL)
375 modparam("permissions", "db_mode", 1)
380 # ----- alias_db params -----
382 modparam("alias_db", "db_url", DBURL)
383 modparam("alias_db", "use_domain", MULTIDOMAIN)
386 # ----- speeddial params -----
387 #!ifdef WITH_SPEEDDIAL
388 modparam("speeddial", "db_url", DBURL)
389 modparam("speeddial", "use_domain", MULTIDOMAIN)
392 # ----- domain params -----
393 #!ifdef WITH_MULTIDOMAIN
394 modparam("domain", "db_url", DBURL)
395 /* register callback to match myself condition with domains list */
396 modparam("domain", "register_myself", 1)
399 #!ifdef WITH_PRESENCE
400 # ----- presence params -----
401 modparam("presence", "db_url", DBURL)
403 # ----- presence_xml params -----
404 modparam("presence_xml", "db_url", DBURL)
405 modparam("presence_xml", "force_active", 1)
409 # ----- rtpproxy params -----
410 modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
412 # ----- nathelper params -----
413 modparam("nathelper", "natping_interval", 30)
414 modparam("nathelper", "ping_nated_only", 1)
415 modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
416 modparam("nathelper", "sipping_from", "sip:pinger@kamailio.org")
418 # params needed for NAT traversal in other modules
419 modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
420 modparam("usrloc", "nat_bflag", FLB_NATB)
424 # ----- tls params -----
425 modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
428 #!ifdef WITH_ANTIFLOOD
429 # ----- pike params -----
430 modparam("pike", "sampling_time_unit", 2)
431 modparam("pike", "reqs_density_per_unit", 16)
432 modparam("pike", "remove_latency", 4)
434 # ----- htable params -----
435 /* ip ban htable with autoexpire after 5 minutes */
436 modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
440 # ----- xmlrpc params -----
441 modparam("xmlrpc", "route", "XMLRPC");
442 modparam("xmlrpc", "url_match", "^/RPC")
446 # ----- debugger params -----
447 modparam("debugger", "cfgtrace", 1)
448 modparam("debugger", "log_level_name", "exec")
451 ####### Routing Logic ########
454 /* Main SIP request routing logic
455 * - processing of any incoming SIP request starts with this route
456 * - note: this is the same as route { ... } */
459 # per request initial checks
466 if (is_method("CANCEL")) {
467 if (t_check_trans()) {
473 # handle retransmissions
474 if (!is_method("ACK")) {
475 if(t_precheck_trans()) {
482 # handle requests within SIP dialogs
485 ### only initial requests (no To tag)
490 # record routing for dialog forming requests (in case they are routed)
491 # - remove preloaded route headers
493 if (is_method("INVITE|SUBSCRIBE")) {
497 # account only INVITEs
498 if (is_method("INVITE")) {
499 setflag(FLT_ACC); # do accounting
502 # dispatch requests to foreign domains
505 ### requests for my local domains
507 # handle presence related requests
510 # handle registrations
514 # request with no Username in RURI
515 sl_send_reply("484","Address Incomplete");
519 # dispatch destinations to PSTN
522 # user location service
526 # Wrapper for relaying requests
529 # enable additional event routes for forwarded requests
530 # - serial forking, RTP relaying handling, a.s.o.
531 if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
532 if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
534 if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
535 if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
537 if (is_method("INVITE")) {
538 if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
547 # Per SIP request initial checks
549 #!ifdef WITH_ANTIFLOOD
550 # flood detection from same IP and traffic ban for a while
551 # be sure you exclude checking trusted peers, such as pstn gateways
552 # - local host excluded (e.g., loop to self)
554 if($sht(ipban=>$si)!=$null) {
555 # ip is already blocked
556 xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
559 if (!pike_check_req()) {
560 xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
561 $sht(ipban=>$si) = 1;
565 if($ua =~ "friendly-scanner|sipcli") {
566 # silent drop for scanners - uncomment next line if want to reply
567 # sl_send_reply("200", "OK");
572 if (!mf_process_maxfwd_header("10")) {
573 sl_send_reply("483","Too Many Hops");
577 if(is_method("OPTIONS") && uri==myself && $rU==$null) {
578 sl_send_reply("200","Keepalive");
582 if(!sanity_check("1511", "7")) {
583 xlog("Malformed SIP message from $si:$sp\n");
588 # Handle requests within SIP dialogs
590 if (!has_totag()) return;
592 # sequential request withing a dialog should
593 # take the path determined by record-routing
596 if (is_method("BYE")) {
597 setflag(FLT_ACC); # do accounting ...
598 setflag(FLT_ACCFAILED); # ... even if the transaction fails
599 } else if ( is_method("ACK") ) {
600 # ACK is forwarded statelessly
602 } else if ( is_method("NOTIFY") ) {
603 # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
610 if (is_method("SUBSCRIBE") && uri == myself) {
611 # in-dialog subscribe requests
615 if ( is_method("ACK") ) {
616 if ( t_check_trans() ) {
617 # no loose-route, but stateful ACK;
618 # must be an ACK after a 487
619 # or e.g. 404 from upstream server
623 # ACK without matching transaction ... ignore and discard
627 sl_send_reply("404","Not here");
631 # Handle SIP registrations
633 if (!is_method("REGISTER")) return;
635 if(isflagset(FLT_NATS)) {
637 #!ifdef WITH_NATSIPPING
639 setbflag(FLB_NATSIPPING);
642 if (!save("location")) {
648 # User location service
651 #!ifdef WITH_SPEEDDIAL
652 # search for short dialing - 2-digit extension
653 if($rU=~"^[0-9][0-9]$") {
654 if(sd_lookup("speed_dial")) {
661 # search in DB-based aliases
662 if(alias_db_lookup("dbaliases")) {
668 if (!lookup("location")) {
675 send_reply("404", "Not Found");
678 send_reply("405", "Method Not Allowed");
683 # when routing via usrloc, log the missed calls also
684 if (is_method("INVITE")) {
685 setflag(FLT_ACCMISSED);
692 # Presence server processing
694 if(!is_method("PUBLISH|SUBSCRIBE")) return;
696 if(is_method("SUBSCRIBE") && $hdr(Event)=="message-summary") {
698 # returns here if no voicemail server is configured
699 sl_send_reply("404", "No voicemail service");
703 #!ifdef WITH_PRESENCE
709 if(is_method("PUBLISH")) {
712 } else if(is_method("SUBSCRIBE")) {
719 # if presence enabled, this part will not be executed
720 if (is_method("PUBLISH") || $rU==$null) {
721 sl_send_reply("404", "Not here");
727 # IP authorization and user authentication
732 if((!is_method("REGISTER")) && allow_source_address()) {
738 if (is_method("REGISTER") || from_uri==myself) {
739 # authenticate requests
740 if (!auth_check("$fd", "subscriber", "1")) {
741 auth_challenge("$fd", "0");
744 # user authenticated - remove auth header
745 if(!is_method("REGISTER|PUBLISH"))
746 consume_credentials();
748 # if caller is not local subscriber, then check if it calls
749 # a local destination, otherwise deny, not an open relay here
750 if (from_uri!=myself && uri!=myself) {
751 sl_send_reply("403","Not relaying");
759 # Caller NAT detection
763 if (nat_uac_test("19")) {
764 if (is_method("REGISTER")) {
765 fix_nated_register();
777 # RTPProxy control and signaling updates for NAT traversal
782 if(check_route_param("nat=yes")) {
787 if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB))) return;
789 if(nat_uac_test("8")) {
790 rtpproxy_manage("co");
792 rtpproxy_manage("cor");
797 if(t_is_branch_route()) {
798 add_rr_param(";nat=yes");
803 if(isbflagset(FLB_NATB)) {
812 # URI update for dialog requests
822 # Routing to foreign domains
824 if (uri==myself) return;
826 append_hf("P-hint: outbound\r\n");
834 # check if PSTN GW IP is defined
835 if (strempty($sel(cfg_get.pstn.gw_ip))) {
836 xlog("SCRIPT: PSTN routing enabled but pstn.gw_ip not defined\n");
840 # route to PSTN dialed numbers starting with '+' or '00'
841 # (international format)
842 # - update the condition to match your dialing rules for PSTN routing
843 if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$")) return;
845 # only local users allowed to call
846 if(from_uri!=myself) {
847 sl_send_reply("403", "Not Allowed");
851 if (strempty($sel(cfg_get.pstn.gw_port))) {
852 $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
854 $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip) + ":"
855 + $sel(cfg_get.pstn.gw_port);
868 # allow XMLRPC from localhost
869 if ((method=="POST" || method=="GET")
870 && (src_ip==127.0.0.1)) {
871 # close connection only for xmlrpclib user agents (there is a bug in
872 # xmlrpclib: it waits for EOF before interpreting the response).
873 if ($hdr(User-Agent) =~ "xmlrpclib")
875 set_reply_no_connect();
879 send_reply("403", "Forbidden");
884 # Routing to voicemail server
886 #!ifdef WITH_VOICEMAIL
887 if(!is_method("INVITE|SUBSCRIBE")) return;
889 # check if VoiceMail server IP is defined
890 if (strempty($sel(cfg_get.voicemail.srv_ip))) {
891 xlog("SCRIPT: VoiceMail routing enabled but IP not defined\n");
894 if(is_method("INVITE")) {
895 if($avp(oexten)==$null) return;
897 $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
898 + ":" + $sel(cfg_get.voicemail.srv_port);
900 if($rU==$null) return;
902 $ru = "sip:" + $rU + "@" + $sel(cfg_get.voicemail.srv_ip)
903 + ":" + $sel(cfg_get.voicemail.srv_port);
912 # Manage outgoing branches
913 branch_route[MANAGE_BRANCH] {
914 xdbg("new branch [$T_branch_idx] to $ru\n");
918 # Manage incoming replies
919 onreply_route[MANAGE_REPLY] {
920 xdbg("incoming reply\n");
921 if(status=~"[12][0-9][0-9]") {
926 # Manage failure routing cases
927 failure_route[MANAGE_FAILURE] {
930 if (t_is_canceled()) exit;
932 #!ifdef WITH_BLOCK3XX
933 # block call redirect based on 3xx replies.
934 if (t_check_status("3[0-9][0-9]")) {
935 t_reply("404","Not found");
940 #!ifdef WITH_BLOCK401407
941 # block call redirect based on 401, 407 replies.
942 if (t_check_status("401|407")) {
943 t_reply("404","Not found");
948 #!ifdef WITH_VOICEMAIL
950 # - route to voicemail on busy or no answer (timeout)
951 if (t_check_status("486|408")) {