kamailio-basic.cfg 15 KB

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