forked from IrisBroadcast/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkamailio.cfg
642 lines (530 loc) · 16.3 KB
/
kamailio.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# Kamailio for CCM
#
# This is an example configuration for Kamailio
#
# - Use of the Dialog module to track new sessions and hangups
# - Use of the Registrar to track new registrations
# - Use of the HTTP module to send data to CCM
#
# This file is based on the Kamailio sample configuration
#
# To learn more about the Kamailio SIP server and download
# the full code, please visit https://www.kamailio.org
#
#
####### Defined Values #########
# *** Value defines - IDs used later in config
#!ifndef DBURL
#!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
#!endif
# - flags
# FLT_ - per transaction (message) flags
#FLB_ - per branch flags
#!define FLT_ACC 1
#!define FLT_ACCMISSED 2
#!define FLT_ACCFAILED 3
#!define FLT_DIALOG 4
#!define FLT_NATS 5
#!define FLB_NATB 6
#!define FLB_NATSIPPING 7
#!define MAXLOOP 10
alias=sip.example.com
####### Global Parameters #########
### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
#!ifdef WITH_DEBUG
debug=4
log_stderror=yes
#!else
debug=2
log_stderror=no
#!endif
memdbg=5
memlog=5
log_facility=LOG_LOCAL0
# number of SIP routing processes
children=2
/* uncomment the next line to disable TCP (default on) */
#disable_tcp=yes
/* uncomment the next line to disable the auto discovery of local aliases
based on reverse DNS on IPs (default on) */
#auto_aliases=no
/* add local domain aliases */
#alias="sip.mydomain.com"
/* uncomment and configure the following line if you want Kamailio to
bind on a specific interface/port/proto (default bind on all available) */
#listen=udp:eth0
/* port to listen to */
port=5060
# life time of TCP connection when there is no traffic
# - a bit higher than registration expires to cope with UA behind NAT
tcp_connection_lifetime=3605
####### Modules Section ########
# set paths to location of modules (to sources or installation folders)
mpath="/usr/local/lib64/kamailio/modules:/usr/local/lib/kamailio/modules:/usr/lib64/kamailio/modules:/usr/lib/kamailio/modules"
loadmodule "kex.so" # Kamailio Server extensions
loadmodule "corex.so" # Kamailio Core extensions
loadmodule "tm.so" # Transaction management
loadmodule "tmx.so" # Transaction management extensions
loadmodule "sl.so" # Stateless forwarding
loadmodule "rr.so" # Record routing
loadmodule "pv.so" # Pseudo-variables
loadmodule "maxfwd.so" # Max-Forward header handling - loop prevention
loadmodule "usrloc.so" # The location database handler
loadmodule "registrar.so" # The SIP registrar, communicating with the location database
loadmodule "textops.so" # Text operations
loadmodule "siputils.so" # Various utilities
loadmodule "xlog.so" # Logging to syslog
loadmodule "sanity.so" # Checking syntax of incoming messages
loadmodule "ctl.so" # The RPC socket
loadmodule "cfg_rpc.so" # Configuration handling over RPC
loadmodule "jsonrpcs.so" # JSON RPC handling
loadmodule "acc.so" # Accounting
loadmodule "db_mysql.so" # MariaDB and MySQL access
loadmodule "auth.so" # Authentication
loadmodule "auth_db.so" # Authentication database connector
loadmodule "permissions.so" # Access Control Lists
loadmodule "mqueue.so" # Message Queue
loadmodule "rtimer.so" # Timers for handling messaging to CCM
loadmodule "dialog.so" # Implementing call states (dialog states)
loadmodule "http_client.so" # The Web Client for restful API access
loadmodule "nathelper.so" # NAT detection and support
loadmodule "rtpproxy.so" # Communications with external RTP proxy
loadmodule "htable.so" # In-memory hash tables (may be replicated to databases)
loadmodule "pike.so" # Denial-of-service attack prevention
# ----------------- setting module-specific parameters ---------------
# ----- ctl params -----
#modparam("ctl", "binrpc", "unix:/var/run/kamailio/kamailio_ctl")
# ----- tm params -----
# auto-discard branches from previous serial forking leg
modparam("tm", "failure_reply_mode", 3)
# default retransmission timeout: 30sec
modparam("tm", "fr_timer", 30000)
# default invite retransmission timeout after 1xx: 120sec
modparam("tm", "fr_inv_timer", 120000)
# ----- rr params -----
# set next param to 1 to add value to ;lr param (helps with some UAs)
modparam("rr", "enable_full_lr", 0)
# do not append from tag to the RR (no need for this script)
modparam("rr", "append_fromtag", 0)
# ----- registrar params -----
modparam("registrar", "method_filtering", 1)
/* uncomment the next line to disable parallel forking via location */
# modparam("registrar", "append_branches", 0)
/* uncomment the next line not to allow more than 10 contacts per AOR */
modparam("registrar", "max_contacts", 1)
# max value for expires of registrations
modparam("registrar", "max_expires", 3600)
# set it to 1 to enable GRUU
modparam("registrar", "gruu_enabled", 0)
# ----- acc params -----
/* what special events should be accounted ? */
modparam("acc", "early_media", 0)
modparam("acc", "report_ack", 0)
modparam("acc", "report_cancels", 0)
/* by default ww do not adjust the direct of the sequential requests.
if you enable this parameter, be sure the enable "append_fromtag"
in "rr" module */
modparam("acc", "detect_direction", 0)
/* account triggers (flags) */
modparam("acc", "log_flag", FLT_ACC)
modparam("acc", "log_missed_flag", FLT_ACCMISSED)
modparam("acc", "log_extra",
"src_user=$fU;src_domain=$fd;src_ip=$si;"
"dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
/* enhanced DB accounting */
#!ifdef WITH_ACCDB
modparam("acc", "db_flag", FLT_ACC)
modparam("acc", "db_missed_flag", FLT_ACCMISSED)
modparam("acc", "db_url", DBURL)
modparam("acc", "db_extra",
"src_user=$fU;src_domain=$fd;src_ip=$si;"
"dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
#!endif
# ----- usrloc params -----
/* enable DB persistency for location entries */
#!ifdef WITH_USRLOCDB
modparam("usrloc", "db_url", DBURL)
modparam("usrloc", "db_mode", 2)
modparam("usrloc", "use_domain", MULTIDOMAIN)
#!endif
# ----- auth_db params -----
modparam("auth_db", "db_url", DBURL)
modparam("auth_db", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")
modparam("auth_db", "load_credentials", "")
# ----- permissions params -----
#!ifdef WITH_IPAUTH
modparam("permissions", "db_url", DBURL)
modparam("permissions", "db_mode", 1)
#!endif
#!ifdef WITH_NAT
# ----- rtpproxy params -----
modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
# ----- nathelper params -----
modparam("nathelper", "natping_interval", 30)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
modparam("nathelper", "sipping_from", "sip:[email protected]")
# params needed for NAT traversal in other modules
modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
modparam("usrloc", "nat_bflag", FLB_NATB)
#!endif
# ----- pike params -----
modparam("pike", "sampling_time_unit", 2)
modparam("pike", "reqs_density_per_unit", 16)
modparam("pike", "remove_latency", 4)
# ----- htable params -----
# ip ban htable with autoexpire after 5 minutes
modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
# The queue for registrations and dialogs
modparam("mqueue", "mqueue", "name=dlg;size=2048;")
modparam("mqueue", "mqueue", "name=reg;size=2048;")
modparam("rtimer", "timer", "name=senddlg;interval=1;mode=1;")
modparam("rtimer", "exec", "timer=senddlg;route=CCMDIALOG;")
modparam("rtimer", "timer", "name=sendreg;interval=1;mode=1;")
modparam("rtimer", "exec", "timer=sendreg;route=CCMREG;")
modparam("dialog", "dlg_flag", 4)
modparam("http_client", "httpcon", "ccm=>http://ccm.example.com:10505/api")
modparam("http_client", "httpproxy", "")
modparam("http_client", "httpproxyport", 0)/* Turn off proxy */
####### Routing Logic ########
# Main SIP request routing logic
# - processing of any incoming SIP request starts with this route
# - note: this is the same as route { ... }
request_route {
# per request initial checks
route(REQINIT);
xlog("L_INFO", "===== $rm to $ru from $fu \n");
# NAT detection
route(NATDETECT);
# CANCEL processing
if (is_method("CANCEL")) {
if (t_check_trans()) {
route(RELAY);
}
exit;
}
# handle requests within SIP dialogs
route(WITHINDLG);
### only initial requests (no To tag)
# handle retransmissions
if(t_precheck_trans()) {
t_check_trans();
exit;
}
t_check_trans();
# authentication
route(AUTH);
# record routing for dialog forming requests (in case they are routed)
# - remove preloaded route headers
remove_hf("Route");
if (is_method("INVITE|SUBSCRIBE")) {
record_route();
}
# account only INVITEs
if (is_method("INVITE")) {
setflag(FLT_ACC); # do accounting
}
# dispatch requests to foreign domains
route(SIPOUT);
### requests for my local domains
# handle registrations
route(REGISTRAR);
if ($rU==$null) {
# request with no Username in RURI
sl_send_reply("484","Address Incomplete");
exit;
}
# dispatch destinations to PSTN
#route(PSTN);
# user location service
route(LOCATION);
}
# Wrapper for relaying requests
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 (!t_relay()) {
sl_reply_error();
}
exit;
}
# Per SIP request initial checks
route[REQINIT] {
# flood detection from same IP and traffic ban for a while
# be sure you exclude checking trusted peers, such as pstn gateways
# - local host excluded (e.g., loop to self)
if(src_ip!=myself) {
if($sht(ipban=>$si)!=$null) {
# ip is already blocked
xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
exit;
}
if (!pike_check_req()) {
xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
$sht(ipban=>$si) = 1;
exit;
}
}
if($ua =~ "friendly-scanner|sipcli") {
# silent drop for scanners - uncomment next line if want to reply
# sl_send_reply("200", "OK");
exit;
}
if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}
if(is_method("OPTIONS") && uri==myself && $rU==$null) {
sl_send_reply("200","Keepalive");
exit;
}
if(!sanity_check("1511", "7")) {
xlog("Malformed SIP message from $si:$sp\n");
exit;
}
}
# Handle requests within SIP dialogs
route[WITHINDLG] {
if (!has_totag()) return;
# sequential request withing a dialog should
# take the path determined by record-routing
if (loose_route()) {
route(DLGURI);
dlg_manage();
if (is_method("BYE")) {
setflag(FLT_ACC); # do accounting ...
setflag(FLT_ACCFAILED); # ... even if the transaction fails
} else if ( is_method("ACK") ) {
# ACK is forwarded statelessy
route(NATMANAGE);
} else if ( is_method("NOTIFY") ) {
# Add Record-Route for in-dialog NOTIFY as per RFC 6665.
record_route();
}
route(RELAY);
exit;
}
if ( is_method("ACK") ) {
if ( t_check_trans() ) {
# no loose-route, but stateful ACK;
# must be an ACK after a 487
# or e.g. 404 from upstream server
route(RELAY);
exit;
} else {
# ACK without matching transaction ... ignore and discard
exit;
}
}
sl_send_reply("404","Not here");
exit;
}
# User location service
route[LOCATION] {
# Save data for CCM
$avp(oexten) = $rU;
$dlg_var(sdp) = $rb;
$dlg_var(fn) = $fn;
$dlg_var(fu) = $fu;
$dlg_var(tn) = $tn;
$dlg_var(tu) = $tu;
$dlg_var(ruri) = $ru;
xlog("L_INFO", " ==> Saving dialog data: $rb $fn $fu $tn $tu RURI $dlg_var(ruri)\n$mb\n");
if (!lookup("location")) {
$var(rc) = $rc;
t_newtran();
dlg_manage();
switch ($var(rc)) {
case -1:
case -3:
send_reply("404", "Not Found");
exit;
case -2:
send_reply("405", "Method Not Allowed");
exit;
}
}
dlg_manage();
# when routing via usrloc, log the missed calls also
if (is_method("INVITE")) {
setflag(FLT_ACCMISSED);
}
route(RELAY);
exit;
}
# IP authorization and user authentication
route[AUTH] {
if (is_method("REGISTER") || from_uri==myself) {
# authenticate requests
if (!proxy_authenticate("$fd", "subscriber")) {
auth_challenge("$fd", "0");
exit;
}
# user authenticated - remove auth header
if(!is_method("REGISTER|PUBLISH")) {
consume_credentials();
}
}
# if caller is not local subscriber, then check if it calls
# a local destination, otherwise deny, not an open relay here
if (from_uri!=myself && uri!=myself) {
sl_send_reply("403","Not relaying");
exit;
}
return;
}
# Caller NAT detection
route[NATDETECT] {
force_rport();
if (nat_uac_test("19")) {
if (is_method("REGISTER")) {
fix_nated_register();
} else {
if(is_first_hop()) {
set_contact_alias();
}
}
setflag(FLT_NATS);
}
return;
}
# RTPProxy control and signaling updates for NAT traversal
route[NATMANAGE]
{
if (is_request()) {
if(has_totag()) {
if(check_route_param("nat=yes")) {
setbflag(FLB_NATB);
}
}
}
if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB))) return;
if(nat_uac_test("8")) {
rtpproxy_manage("co");
} else {
rtpproxy_manage("cor");
}
if (is_request()) {
if (!has_totag()) {
if(t_is_branch_route()) {
add_rr_param(";nat=yes");
}
}
}
if (is_reply()) {
if(isbflagset(FLB_NATB)) {
if(is_first_hop()) {
set_contact_alias();
}
}
}
return;
}
# URI update for dialog requests
route[DLGURI]
{
if(!isdsturiset()) {
handle_ruri_alias();
}
return;
}
# Routing to foreign domains
route[SIPOUT]
{
if (uri==myself) return;
append_hf("P-hint: outbound\r\n");
route(RELAY);
exit;
}
# Manage outgoing branches
branch_route[MANAGE_BRANCH] {
xdbg("new branch [$T_branch_idx] to $ru\n");
route(NATMANAGE);
}
# Manage incoming replies
onreply_route[MANAGE_REPLY]
{
xdbg("incoming reply\n");
if(status=~"[12][0-9][0-9]") {
route(NATMANAGE);
}
}
# Manage failure routing cases
failure_route[MANAGE_FAILURE]
{
route(NATMANAGE);
if (t_is_canceled()) exit;
}
# Send call start information to CCM.
# This is a background process, not to disturb the acutal calls
route[CCMDLG]
{
$var(count) = 0 ;
while (mq_fetch("dlg")) {
$var(res) = http_connect("ccm", "/kamailioevent", "application/json", "\"$mqv(dlg)\"", "$var(result)");
xlog("L_INFO", " ==> HTTP response $var(res) $curlerror($var(res))\n");
$var(count) = $var(count) + 1 ;
if ( $var(count) == MAXLOOP ) {
break;
}
}
}
# Send registration events to CCM
# This is a background process
route[CCMREG]
{
$var(count) = 0 ;
while (mq_fetch("reg")) {
$var(res) = http_connect("ccm", "/kamailioevent", "application/json", "\"$mqv(reg)\"", "$var(result)");
xlog("L_INFO", " ==> HTTP response $var(res) $curlerror($var(res))\n");
$var(count) = $var(count) + 1 ;
if ( $var(count) == MAXLOOP ) {
break;
}
}
}
route[REGISTRAR] {
if (is_method("REGISTER")) {
if (isflagset(FLT_NATS)) {
setbflag(FLB_NATB);
}
if (!save("location")) {
xlog("L_ERR", "--REGISTER: failed to save registration: $tu => contact $ct IP: $si:$sp, Caller-ID: $ci, CSeq: $cs\n");
sl_reply_error();
} else {
# Create data for CCM to handle
$var(msg)=$_s(request|rm::$rm|fn::$(fn{s.escape.common})|fu::$fu|si::$si|sp::$sp|ru::$ru|Ri::$Ri|Rp::$Rp|ua::$ua|rU::$rU|fU::$fU|ci::$ci|tn::$(tn{s.escape.common})|tu::$tu|TS::$TS|Au::$Au|Expires::$hdr(Expires));
mq_add("reg", "report", "$var(msg)" );
}
exit;
}
}
event_route[dialog:start] {
xlog("L_INFO", "DIA--> Starting dialog for $ci :: $ft :: $tt :: $fn $fu :: $tn $tu (Count: $DLG_count) :: Mediastatus: $dlg_var(mediastatus)\n");
$var(msg)=$_s(dialog|dstat::start|hashid::$dlg(h_id)|hashent::$dlg(h_entry)|ci::$dlg(callid)|tot::$tti|fot::$dlg(from_tag)|from_uri::$dlg(from_uri)|to_uri::$dlg(to_uri)|ms::$dlg_var(mediastatus)|ru::$dlg_var(ruri)|fn::$(fn{s.escape.common})|fu::$fu|tn::$(tn{s.escape.common})|tu::$tu|sdp::$dlg_var(sdp));
mq_add("dlg", "report", "$var(msg)" );
}
event_route[dialog:end] {
xlog("L_INFO", " DIA<-- Ending dialog for $ci :: $ft :: $tt :: $fn :: $tn Lifetime $DLG_lifetime\n");
$var(msg)="dialog|dstat::end|hashid::"+ $dlg(h_id)+"|hashent::"+ $dlg(h_entry)+"|ci::"+ $dlg(callid)+"|tot::"+ $dlg(to_tag)+"|fot::"+ $dlg(from_tag)+"|fu::"+ $dlg(from_uri)+"|ru::"+ $dlg(to_uri);
mq_add("dlg", "report", "$var(msg)" );
}
event_route[usrloc:contact-expired]
{
xlog("L_INFO", "===> Expired contact for $ulc(exp=>aor) Socket $ulc(exp=>socket) \n");
$var(aor) = "sip:" + $ulc(exp=>aor) ;
$var(socket) = $(ulc(exp=>socket){s.select,1,:});
$var(msg)=$_s(regexpire|aor::$var(aor)|ip::$var(socket));
mq_add("reg", "report", "$var(msg)" );
}