kamailio-basic.cfg 15 KB

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