kamailio-basic.cfg 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!KAMAILIO
  2. #
  3. # Kamailio (OpenSER) SIP Server v5.0 - default configuration script
  4. # - web: http://www.kamailio.org
  5. # - git: http://sip-router.org
  6. #
  7. # Direct your questions about this file to: <[email protected]>
  8. #
  9. # Refer to the Core CookBook at http://www.kamailio.org/wiki/
  10. # for an explanation of possible statements, functions and parameters.
  11. #
  12. # Several features can be enabled using '#!define WITH_FEATURE' directives:
  13. #
  14. # *** To run in debug mode:
  15. # - define WITH_DEBUG
  16. #
  17. # *** To enable mysql:
  18. # - define WITH_MYSQL
  19. #
  20. # *** To enable authentication execute:
  21. # - enable mysql
  22. # - define WITH_AUTH
  23. # - add users using 'kamctl'
  24. #
  25. # *** To enable IP authentication execute:
  26. # - enable mysql
  27. # - enable authentication
  28. # - define WITH_IPAUTH
  29. # - add IP addresses with group id '1' to 'address' table
  30. #
  31. # *** To enable persistent user location execute:
  32. # - enable mysql
  33. # - define WITH_USRLOCDB
  34. #
  35. # *** To enable nat traversal execute:
  36. # - define WITH_NAT
  37. # - install RTPProxy: http://www.rtpproxy.org
  38. # - start RTPProxy:
  39. # rtpproxy -l _your_public_ip_ -s udp:localhost:7722
  40. # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
  41. #
  42. # *** To enable TLS support execute:
  43. # - adjust CFGDIR/tls.cfg as needed
  44. # - define WITH_TLS
  45. #
  46. # *** To enhance accounting execute:
  47. # - enable mysql
  48. # - define WITH_ACCDB
  49. # - add following columns to database
  50. #!ifdef ACCDB_COMMENT
  51. ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  52. ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  53. ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
  54. ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  55. ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  56. ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  57. ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  58. ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  59. ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
  60. ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  61. ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  62. ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  63. #!endif
  64. ####### Include Local Config If Exists #########
  65. import_file "kamailio-local.cfg"
  66. ####### Defined Values #########
  67. # *** Value defines - IDs used later in config
  68. #!ifdef WITH_MYSQL
  69. # - database URL - used to connect to database server by modules such
  70. # as: auth_db, acc, usrloc, a.s.o.
  71. #!ifndef DBURL
  72. #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
  73. #!endif
  74. #!endif
  75. #!define MULTIDOMAIN 0
  76. # - flags
  77. # FLT_ - per transaction (message) flags
  78. # FLB_ - per branch flags
  79. #!define FLT_ACC 1
  80. #!define FLT_ACCMISSED 2
  81. #!define FLT_ACCFAILED 3
  82. #!define FLT_NATS 5
  83. #!define FLB_NATB 6
  84. #!define FLB_NATSIPPING 7
  85. ####### Global Parameters #########
  86. ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
  87. #!ifdef WITH_DEBUG
  88. debug=4
  89. log_stderror=yes
  90. #!else
  91. debug=2
  92. log_stderror=no
  93. #!endif
  94. memdbg=5
  95. memlog=5
  96. log_facility=LOG_LOCAL0
  97. fork=yes
  98. children=4
  99. /* uncomment the next line to disable TCP (default on) */
  100. #disable_tcp=yes
  101. /* uncomment the next line to disable the auto discovery of local aliases
  102. * based on reverse DNS on IPs (default on) */
  103. #auto_aliases=no
  104. /* add local domain aliases */
  105. #alias="sip.mydomain.com"
  106. /* uncomment and configure the following line if you want Kamailio to
  107. * bind on a specific interface/port/proto (default bind on all available) */
  108. #listen=udp:10.0.0.10:5060
  109. /* port to listen to
  110. * - can be specified more than once if needed to listen on many ports */
  111. port=5060
  112. #!ifdef WITH_TLS
  113. enable_tls=yes
  114. #!endif
  115. # life time of TCP connection when there is no traffic
  116. # - a bit higher than registration expires to cope with UA behind NAT
  117. tcp_connection_lifetime=3605
  118. ####### Modules Section ########
  119. # set paths to location of modules (to sources or installation folders)
  120. #!ifdef WITH_SRCPATH
  121. mpath="modules"
  122. #!else
  123. mpath="/usr/local/lib/kamailio/modules/"
  124. #!endif
  125. #!ifdef WITH_MYSQL
  126. loadmodule "db_mysql.so"
  127. #!endif
  128. loadmodule "jsonrpc.so"
  129. loadmodule "kex.so"
  130. loadmodule "corex.so"
  131. loadmodule "tm.so"
  132. loadmodule "tmx.so"
  133. loadmodule "sl.so"
  134. loadmodule "rr.so"
  135. loadmodule "pv.so"
  136. loadmodule "maxfwd.so"
  137. loadmodule "usrloc.so"
  138. loadmodule "registrar.so"
  139. loadmodule "textops.so"
  140. loadmodule "siputils.so"
  141. loadmodule "xlog.so"
  142. loadmodule "sanity.so"
  143. loadmodule "ctl.so"
  144. loadmodule "cfg_rpc.so"
  145. loadmodule "acc.so"
  146. loadmodule "counters.so"
  147. #!ifdef WITH_AUTH
  148. loadmodule "auth.so"
  149. loadmodule "auth_db.so"
  150. #!ifdef WITH_IPAUTH
  151. loadmodule "permissions.so"
  152. #!endif
  153. #!endif
  154. #!ifdef WITH_NAT
  155. loadmodule "nathelper.so"
  156. loadmodule "rtpproxy.so"
  157. #!endif
  158. #!ifdef WITH_TLS
  159. loadmodule "tls.so"
  160. #!endif
  161. #!ifdef WITH_DEBUG
  162. loadmodule "debugger.so"
  163. #!endif
  164. # ----------------- setting module-specific parameters ---------------
  165. # ----- jsonrpcs params -----
  166. modparam("jsonrpcs", "pretty_format", 1)
  167. /* set the path to RPC fifo control file */
  168. # modparam("jsonrpcs", "fifo_name", "/var/run/kamailio/kamailio_rpc.fifo")
  169. /* set the path to RPC unix socket control file */
  170. # modparam("jsonrpcs", "dgram_socket", "/var/run/kamailio/kamailio_rpc.sock")
  171. # ----- ctl params -----
  172. #modparam("ctl", "binrpc", "unix:/var/run/kamailio/kamailio_ctl")
  173. # ----- tm params -----
  174. # auto-discard branches from previous serial forking leg
  175. modparam("tm", "failure_reply_mode", 3)
  176. # default retransmission timeout: 30sec
  177. modparam("tm", "fr_timer", 30000)
  178. # default invite retransmission timeout after 1xx: 120sec
  179. modparam("tm", "fr_inv_timer", 120000)
  180. # ----- rr params -----
  181. # add value to ;lr param to cope with most of the UAs
  182. modparam("rr", "enable_full_lr", 1)
  183. # do not append from tag to the RR (no need for this script)
  184. modparam("rr", "append_fromtag", 0)
  185. # ----- registrar params -----
  186. modparam("registrar", "method_filtering", 1)
  187. /* uncomment the next line to disable parallel forking via location */
  188. # modparam("registrar", "append_branches", 0)
  189. /* uncomment the next line not to allow more than 10 contacts per AOR */
  190. #modparam("registrar", "max_contacts", 10)
  191. # max value for expires of registrations
  192. modparam("registrar", "max_expires", 3600)
  193. # set it to 1 to enable GRUU
  194. modparam("registrar", "gruu_enabled", 0)
  195. # ----- acc params -----
  196. /* what special events should be accounted ? */
  197. modparam("acc", "early_media", 0)
  198. modparam("acc", "report_ack", 0)
  199. modparam("acc", "report_cancels", 0)
  200. /* by default we do not adjust the direct of the sequential requests.
  201. * if you enable this parameter, be sure the enable "append_fromtag"
  202. * in "rr" module */
  203. modparam("acc", "detect_direction", 0)
  204. /* account triggers (flags) */
  205. modparam("acc", "log_flag", FLT_ACC)
  206. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  207. modparam("acc", "log_extra",
  208. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  209. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  210. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  211. /* enhanced DB accounting */
  212. #!ifdef WITH_ACCDB
  213. modparam("acc", "db_flag", FLT_ACC)
  214. modparam("acc", "db_missed_flag", FLT_ACCMISSED)
  215. modparam("acc", "db_url", DBURL)
  216. modparam("acc", "db_extra",
  217. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  218. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  219. #!endif
  220. # ----- usrloc params -----
  221. /* enable DB persistency for location entries */
  222. #!ifdef WITH_USRLOCDB
  223. modparam("usrloc", "db_url", DBURL)
  224. modparam("usrloc", "db_mode", 2)
  225. modparam("usrloc", "use_domain", MULTIDOMAIN)
  226. #!endif
  227. # ----- auth_db params -----
  228. #!ifdef WITH_AUTH
  229. modparam("auth_db", "db_url", DBURL)
  230. modparam("auth_db", "calculate_ha1", yes)
  231. modparam("auth_db", "password_column", "password")
  232. modparam("auth_db", "load_credentials", "")
  233. modparam("auth_db", "use_domain", MULTIDOMAIN)
  234. # ----- permissions params -----
  235. #!ifdef WITH_IPAUTH
  236. modparam("permissions", "db_url", DBURL)
  237. modparam("permissions", "db_mode", 1)
  238. #!endif
  239. #!endif
  240. #!ifdef WITH_NAT
  241. # ----- rtpproxy params -----
  242. modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
  243. # ----- nathelper params -----
  244. modparam("nathelper", "natping_interval", 30)
  245. modparam("nathelper", "ping_nated_only", 1)
  246. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  247. modparam("nathelper", "sipping_from", "sip:[email protected]")
  248. # params needed for NAT traversal in other modules
  249. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  250. modparam("usrloc", "nat_bflag", FLB_NATB)
  251. #!endif
  252. #!ifdef WITH_TLS
  253. # ----- tls params -----
  254. modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
  255. #!endif
  256. #!ifdef WITH_DEBUG
  257. # ----- debugger params -----
  258. modparam("debugger", "cfgtrace", 1)
  259. #!endif
  260. ####### Routing Logic ########
  261. # Main SIP request routing logic
  262. # - processing of any incoming SIP request starts with this route
  263. # - note: this is the same as route { ... }
  264. request_route {
  265. # per request initial checks
  266. route(REQINIT);
  267. # NAT detection
  268. route(NATDETECT);
  269. # CANCEL processing
  270. if (is_method("CANCEL")) {
  271. if (t_check_trans()) {
  272. route(RELAY);
  273. }
  274. exit;
  275. }
  276. # handle requests within SIP dialogs
  277. route(WITHINDLG);
  278. ### only initial requests (no To tag)
  279. # handle retransmissions
  280. if(t_precheck_trans()) {
  281. t_check_trans();
  282. exit;
  283. }
  284. t_check_trans();
  285. # authentication
  286. route(AUTH);
  287. # record routing for dialog forming requests (in case they are routed)
  288. # - remove preloaded route headers
  289. remove_hf("Route");
  290. if (is_method("INVITE|SUBSCRIBE"))
  291. record_route();
  292. # account only INVITEs
  293. if (is_method("INVITE")) {
  294. setflag(FLT_ACC); # do accounting
  295. }
  296. # dispatch requests to foreign domains
  297. route(SIPOUT);
  298. ### requests for my local domains
  299. # handle registrations
  300. route(REGISTRAR);
  301. if ($rU==$null) {
  302. # request with no Username in RURI
  303. sl_send_reply("484","Address Incomplete");
  304. exit;
  305. }
  306. # user location service
  307. route(LOCATION);
  308. }
  309. route[RELAY] {
  310. # enable additional event routes for forwarded requests
  311. # - serial forking, RTP relaying handling, a.s.o.
  312. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  313. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  314. }
  315. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  316. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  317. }
  318. if (is_method("INVITE")) {
  319. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  320. }
  321. if (!t_relay()) {
  322. sl_reply_error();
  323. }
  324. exit;
  325. }
  326. # Per SIP request initial checks
  327. route[REQINIT] {
  328. #!ifdef WITH_ANTIFLOOD
  329. # flood dection from same IP and traffic ban for a while
  330. # be sure you exclude checking trusted peers, such as pstn gateways
  331. # - local host excluded (e.g., loop to self)
  332. if(src_ip!=myself) {
  333. if($sht(ipban=>$si)!=$null) {
  334. # ip is already blocked
  335. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  336. exit;
  337. }
  338. if (!pike_check_req()) {
  339. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  340. $sht(ipban=>$si) = 1;
  341. exit;
  342. }
  343. }
  344. if($ua =~ "friendly-scanner") {
  345. sl_send_reply("200", "OK");
  346. exit;
  347. }
  348. #!endif
  349. if (!mf_process_maxfwd_header("10")) {
  350. sl_send_reply("483","Too Many Hops");
  351. exit;
  352. }
  353. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  354. sl_send_reply("200","Keepalive");
  355. exit;
  356. }
  357. if(!sanity_check("1511", "7")) {
  358. xlog("Malformed SIP message from $si:$sp\n");
  359. exit;
  360. }
  361. }
  362. # Handle requests within SIP dialogs
  363. route[WITHINDLG] {
  364. if (!has_totag()) return;
  365. # sequential request withing a dialog should
  366. # take the path determined by record-routing
  367. if (loose_route()) {
  368. route(DLGURI);
  369. if (is_method("BYE")) {
  370. setflag(FLT_ACC); # do accounting ...
  371. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  372. }
  373. else if ( is_method("ACK") ) {
  374. # ACK is forwarded statelessy
  375. route(NATMANAGE);
  376. }
  377. else if ( is_method("NOTIFY") ) {
  378. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  379. record_route();
  380. }
  381. route(RELAY);
  382. exit;
  383. }
  384. if ( is_method("ACK") ) {
  385. if ( t_check_trans() ) {
  386. # no loose-route, but stateful ACK;
  387. # must be an ACK after a 487
  388. # or e.g. 404 from upstream server
  389. route(RELAY);
  390. exit;
  391. } else {
  392. # ACK without matching transaction ... ignore and discard
  393. exit;
  394. }
  395. }
  396. sl_send_reply("404", "Not here");
  397. exit;
  398. }
  399. # Handle SIP registrations
  400. route[REGISTRAR] {
  401. if (!is_method("REGISTER")) return;
  402. if(isflagset(FLT_NATS)) {
  403. setbflag(FLB_NATB);
  404. #!ifdef WITH_NATSIPPING
  405. # do SIP NAT pinging
  406. setbflag(FLB_NATSIPPING);
  407. #!endif
  408. }
  409. if (!save("location"))
  410. sl_reply_error();
  411. exit;
  412. }
  413. # User location service
  414. route[LOCATION] {
  415. if (!lookup("location")) {
  416. $var(rc) = $rc;
  417. t_newtran();
  418. switch ($var(rc)) {
  419. case -1:
  420. case -3:
  421. send_reply("404", "Not Found");
  422. exit;
  423. case -2:
  424. send_reply("405", "Method Not Allowed");
  425. exit;
  426. }
  427. }
  428. # when routing via usrloc, log the missed calls also
  429. if (is_method("INVITE")) {
  430. setflag(FLT_ACCMISSED);
  431. }
  432. route(RELAY);
  433. exit;
  434. }
  435. # IP authorization and user uthentication
  436. route[AUTH] {
  437. #!ifdef WITH_AUTH
  438. #!ifdef WITH_IPAUTH
  439. if((!is_method("REGISTER")) && allow_source_address()) {
  440. # source IP allowed
  441. return;
  442. }
  443. #!endif
  444. if (is_method("REGISTER") || from_uri==myself) {
  445. # authenticate requests
  446. if (!auth_check("$fd", "subscriber", "1")) {
  447. auth_challenge("$fd", "0");
  448. exit;
  449. }
  450. # user authenticated - remove auth header
  451. if(!is_method("REGISTER|PUBLISH"))
  452. consume_credentials();
  453. }
  454. # if caller is not local subscriber, then check if it calls
  455. # a local destination, otherwise deny, not an open relay here
  456. if (from_uri!=myself && uri!=myself) {
  457. sl_send_reply("403","Not relaying");
  458. exit;
  459. }
  460. #!endif
  461. return;
  462. }
  463. # Caller NAT detection
  464. route[NATDETECT] {
  465. #!ifdef WITH_NAT
  466. force_rport();
  467. if (nat_uac_test("19")) {
  468. if (is_method("REGISTER")) {
  469. fix_nated_register();
  470. } else {
  471. if(is_first_hop())
  472. set_contact_alias();
  473. }
  474. setflag(FLT_NATS);
  475. }
  476. #!endif
  477. return;
  478. }
  479. # RTPProxy control
  480. route[NATMANAGE] {
  481. #!ifdef WITH_NAT
  482. if (is_request()) {
  483. if(has_totag()) {
  484. if(check_route_param("nat=yes")) {
  485. setbflag(FLB_NATB);
  486. }
  487. }
  488. }
  489. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
  490. return;
  491. rtpproxy_manage("co");
  492. if (is_request()) {
  493. if (!has_totag()) {
  494. if(t_is_branch_route()) {
  495. add_rr_param(";nat=yes");
  496. }
  497. }
  498. }
  499. if (is_reply()) {
  500. if(isbflagset(FLB_NATB)) {
  501. set_contact_alias();
  502. }
  503. }
  504. #!endif
  505. return;
  506. }
  507. # URI update for dialog requests
  508. route[DLGURI] {
  509. #!ifdef WITH_NAT
  510. if(!isdsturiset()) {
  511. handle_ruri_alias();
  512. }
  513. #!endif
  514. return;
  515. }
  516. # Routing to foreign domains
  517. route[SIPOUT] {
  518. if (uri==myself) return;
  519. append_hf("P-hint: outbound\r\n");
  520. route(RELAY);
  521. exit;
  522. }
  523. # Manage outgoing branches
  524. branch_route[MANAGE_BRANCH] {
  525. xdbg("new branch [$T_branch_idx] to $ru\n");
  526. route(NATMANAGE);
  527. }
  528. # Manage incoming replies
  529. onreply_route[MANAGE_REPLY] {
  530. xdbg("incoming reply\n");
  531. if(status=~"[12][0-9][0-9]")
  532. route(NATMANAGE);
  533. }
  534. # Manage failure routing cases
  535. failure_route[MANAGE_FAILURE] {
  536. route(NATMANAGE);
  537. if (t_is_canceled()) {
  538. exit;
  539. }
  540. }