kamailio.cfg 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. #!KAMAILIO
  2. #
  3. # Kamailio SIP Server v5.8 - default configuration script
  4. # - web: https://www.kamailio.org
  5. # - git: https://github.com/kamailio/kamailio
  6. #
  7. # Direct your questions about this file to: <[email protected]>
  8. #
  9. # Refer to the Core CookBook at https://www.kamailio.org/wikidocs/
  10. # for an explanation of possible statements, functions and parameters.
  11. #
  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 /* */
  18. # Note: the config performs symmetric SIP signaling
  19. # - it sends the reply to the source address of the request
  20. # - remove the use of force_rport() for asymmetric SIP signaling
  21. #
  22. # Several features can be enabled using '#!define WITH_FEATURE' directives:
  23. #
  24. # *** To run in debug mode:
  25. # - define WITH_DEBUG
  26. # - debug level increased to 3, logs still sent to syslog
  27. # - debugger module loaded with cfgtrace enabled
  28. #
  29. # *** To enable mysql:
  30. # - define WITH_MYSQL
  31. #
  32. # *** To enable authentication execute:
  33. # - enable mysql
  34. # - define WITH_AUTH
  35. # - add users using 'kamctl' or 'kamcli'
  36. #
  37. # *** To enable IP authentication execute:
  38. # - enable mysql
  39. # - enable authentication
  40. # - define WITH_IPAUTH
  41. # - add IP addresses with group id '1' to 'address' table
  42. #
  43. # *** To enable persistent user location execute:
  44. # - enable mysql
  45. # - define WITH_USRLOCDB
  46. #
  47. # *** To enable presence server execute:
  48. # - enable mysql
  49. # - define WITH_PRESENCE
  50. # - if modified headers or body in config must be used by presence handling:
  51. # - define WITH_MSGREBUILD
  52. #
  53. # *** To enable nat traversal execute:
  54. # - define WITH_NAT
  55. # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
  56. # - install RTPProxy: http://www.rtpproxy.org
  57. # - start RTPProxy:
  58. # rtpproxy -l _your_public_ip_ -s udp:localhost:7722
  59. #
  60. # *** To use RTPEngine (instead of RTPProxy) for nat traversal execute:
  61. # - define WITH_RTPENGINE
  62. # - install RTPEngine: https://github.com/sipwise/rtpengine
  63. # - start RTPEngine:
  64. # rtpengine --listen-ng=127.0.0.1:2223 ...
  65. #
  66. # *** To enable PSTN gateway routing execute:
  67. # - define WITH_PSTN
  68. # - set the value of pstn.gw_ip
  69. # - check route[PSTN] for regexp routing condition
  70. #
  71. # *** To enable database aliases lookup execute:
  72. # - enable mysql
  73. # - define WITH_ALIASDB
  74. #
  75. # *** To enable speed dial lookup execute:
  76. # - enable mysql
  77. # - define WITH_SPEEDDIAL
  78. #
  79. # *** To enable multi-domain support execute:
  80. # - enable mysql
  81. # - define WITH_MULTIDOMAIN
  82. #
  83. # *** To enable TLS support execute:
  84. # - adjust CFGDIR/tls.cfg as needed
  85. # - define WITH_TLS
  86. #
  87. # *** To enable JSONRPC over HTTP(S) support execute:
  88. # - define WITH_JSONRPC
  89. # - adjust event_route[xhttp:request] for access policy
  90. #
  91. # *** To enable anti-flood detection execute:
  92. # - adjust pike and htable=>ipban settings as needed (default is
  93. # block if more than 16 requests in 2 seconds and ban for 300 seconds)
  94. # - define WITH_ANTIFLOOD
  95. #
  96. # *** To load htable module execute:
  97. # - define WITH_HTABLE
  98. #
  99. # *** To block 3XX redirect replies execute:
  100. # - define WITH_BLOCK3XX
  101. #
  102. # *** To block 401 and 407 authentication replies execute:
  103. # - define WITH_BLOCK401407
  104. #
  105. # *** To enable VoiceMail routing execute:
  106. # - define WITH_VOICEMAIL
  107. # - set the value of voicemail.srv_ip
  108. # - adjust the value of voicemail.srv_port
  109. #
  110. # *** To enhance accounting execute:
  111. # - enable mysql
  112. # - define WITH_ACCDB
  113. # - add following columns to database
  114. #!ifdef ACCDB_COMMENT
  115. ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  116. ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  117. ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
  118. ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  119. ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  120. ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  121. ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  122. ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  123. ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
  124. ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  125. ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  126. ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  127. #!endif
  128. ####### Include Local Config If Exists #########
  129. import_file "kamailio-local.cfg"
  130. ####### Defined Values #########
  131. # *** Value defines - IDs used later in config
  132. #!ifdef WITH_DEBUG
  133. #!define DBGLEVEL 3
  134. #!else
  135. #!define DBGLEVEL 2
  136. #!endif
  137. #!ifdef WITH_MYSQL
  138. # - database URL - used to connect to database server by modules such
  139. # as: auth_db, acc, usrloc, a.s.o.
  140. #!trydef DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
  141. #!endif
  142. #!ifdef WITH_MULTIDOMAIN
  143. # - the value for 'use_domain' parameters
  144. #!define MULTIDOMAIN 1
  145. #!else
  146. #!define MULTIDOMAIN 0
  147. #!endif
  148. #!ifdef WITH_ANTIFLOOD
  149. # - hash table 'ipban' used to store blocked IP addresses
  150. #!trydef WITH_HTABLE
  151. #!endif
  152. # - flags
  153. # FLT_ - per transaction (message) flags
  154. #!define FLT_ACC 1
  155. #!define FLT_ACCMISSED 2
  156. #!define FLT_ACCFAILED 3
  157. #!define FLT_NATS 5
  158. # FLB_ - per branch flags
  159. #!define FLB_NATB 6
  160. #!define FLB_NATSIPPING 7
  161. ####### Global Parameters #########
  162. /* LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR, ... */
  163. debug=DBGLEVEL
  164. /* set to 'yes' to print log messages to terminal or use '-E' cli option */
  165. log_stderror=no
  166. memdbg=5
  167. memlog=5
  168. log_facility=LOG_LOCAL0
  169. log_prefix="{$mt $hdr(CSeq) $ci} "
  170. /* number of SIP routing processes for each UDP socket
  171. * - value inherited by tcp_children and sctp_children when not set explicitely */
  172. children=8
  173. /* uncomment the next line to disable TCP (default on) */
  174. # disable_tcp=yes
  175. /* number of SIP routing processes for all TCP/TLS sockets */
  176. # tcp_children=8
  177. /* uncomment the next line to disable the auto discovery of local aliases
  178. * based on reverse DNS on IPs (default on) */
  179. # auto_aliases=no
  180. /* add local domain aliases - it can be set many times */
  181. # alias="sip.mydomain.com"
  182. /* listen sockets - if none set, Kamailio binds to all local IP addresses
  183. * - basic prototype (full prototype can be found in Wiki - Core Cookbook):
  184. * listen=[proto]:[localip]:[lport] advertise [publicip]:[pport]
  185. * - it can be set many times to add more sockets to listen to */
  186. # listen=udp:10.0.0.10:5060
  187. /* life time of TCP connection when there is no traffic
  188. * - a bit higher than registration expires to cope with UA behind NAT */
  189. tcp_connection_lifetime=3605
  190. /* upper limit for TCP connections (it includes the TLS connections) */
  191. tcp_max_connections=2048
  192. /* upper limit for TCP connections for one ip address - default 1024 */
  193. #tcp_accept_iplimit=1024
  194. #!ifdef WITH_JSONRPC
  195. tcp_accept_no_cl=yes
  196. #!endif
  197. #!ifdef WITH_TLS
  198. enable_tls=yes
  199. /* upper limit for TLS connections */
  200. tls_max_connections=2048
  201. /* For OpenSSL 3 integration
  202. * functions calling libssl3 can be invoked in a transient thread
  203. * 0: disable threaded calls
  204. * 1: use thread executors for process#0 only
  205. * 2: no thread executors, but use atfork handler to reset thread-locals to NULL
  206. * 3: use thread executors for all processes */
  207. tls_threads_mode=1
  208. #!endif
  209. /* set it to yes to enable sctp and load sctp.so module */
  210. enable_sctp=no
  211. ####### Custom Parameters #########
  212. /* These parameters can be modified at runtime via RPC interface
  213. * - see the documentation of 'cfg_rpc' module.
  214. *
  215. * Format: group.id = value 'desc' description
  216. * Access: $sel(cfg_get.group.id) or @cfg_get.group.id */
  217. #!ifdef WITH_PSTN
  218. /* PSTN GW Routing
  219. *
  220. * - pstn.gw_ip: valid IP or hostname as string value, example:
  221. * pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
  222. *
  223. * - by default is empty to avoid misrouting */
  224. pstn.gw_ip = "" desc "PSTN GW Address"
  225. pstn.gw_port = "" desc "PSTN GW Port"
  226. #!endif
  227. #!ifdef WITH_VOICEMAIL
  228. /* VoiceMail Routing on offline, busy or no answer
  229. *
  230. * - by default Voicemail server IP is empty to avoid misrouting */
  231. voicemail.srv_ip = "" desc "VoiceMail IP Address"
  232. voicemail.srv_port = "5060" desc "VoiceMail Port"
  233. #!endif
  234. ####### Modules Section ########
  235. /* set paths to location of modules */
  236. # mpath="/usr/local/lib/kamailio/modules/"
  237. # when using TLS with OpenSSL it is recommended to load this module
  238. # first so that OpenSSL is initialized correctly
  239. #!ifdef WITH_TLS
  240. loadmodule "tls.so"
  241. #!endif
  242. #!ifdef WITH_MYSQL
  243. loadmodule "db_mysql.so"
  244. #!endif
  245. #!ifdef WITH_JSONRPC
  246. loadmodule "xhttp.so"
  247. #!endif
  248. loadmodule "jsonrpcs.so"
  249. loadmodule "kex.so"
  250. loadmodule "corex.so"
  251. loadmodule "tm.so"
  252. loadmodule "tmx.so"
  253. loadmodule "sl.so"
  254. loadmodule "rr.so"
  255. loadmodule "pv.so"
  256. loadmodule "maxfwd.so"
  257. loadmodule "usrloc.so"
  258. loadmodule "registrar.so"
  259. loadmodule "textops.so"
  260. loadmodule "textopsx.so"
  261. loadmodule "siputils.so"
  262. loadmodule "xlog.so"
  263. loadmodule "sanity.so"
  264. loadmodule "ctl.so"
  265. loadmodule "cfg_rpc.so"
  266. loadmodule "acc.so"
  267. loadmodule "counters.so"
  268. #!ifdef WITH_AUTH
  269. loadmodule "auth.so"
  270. loadmodule "auth_db.so"
  271. #!ifdef WITH_IPAUTH
  272. loadmodule "permissions.so"
  273. #!endif
  274. #!endif
  275. #!ifdef WITH_ALIASDB
  276. loadmodule "alias_db.so"
  277. #!endif
  278. #!ifdef WITH_SPEEDDIAL
  279. loadmodule "speeddial.so"
  280. #!endif
  281. #!ifdef WITH_MULTIDOMAIN
  282. loadmodule "domain.so"
  283. #!endif
  284. #!ifdef WITH_PRESENCE
  285. loadmodule "presence.so"
  286. loadmodule "presence_xml.so"
  287. #!endif
  288. #!ifdef WITH_NAT
  289. loadmodule "nathelper.so"
  290. #!ifdef WITH_RTPENGINE
  291. loadmodule "rtpengine.so"
  292. #!else
  293. loadmodule "rtpproxy.so"
  294. #!endif
  295. #!endif
  296. #!ifdef WITH_HTABLE
  297. loadmodule "htable.so"
  298. #!endif
  299. #!ifdef WITH_ANTIFLOOD
  300. loadmodule "pike.so"
  301. #!endif
  302. #!ifdef WITH_DEBUG
  303. loadmodule "debugger.so"
  304. #!endif
  305. # ----------------- setting module-specific parameters ---------------
  306. # ----- jsonrpcs params -----
  307. modparam("jsonrpcs", "pretty_format", 1)
  308. /* set the path to RPC fifo control file */
  309. # modparam("jsonrpcs", "fifo_name", "/run/kamailio/kamailio_rpc.fifo")
  310. /* set the path to RPC unix socket control file */
  311. # modparam("jsonrpcs", "dgram_socket", "/run/kamailio/kamailio_rpc.sock")
  312. #!ifdef WITH_JSONRPC
  313. modparam("jsonrpcs", "transport", 7)
  314. #!endif
  315. # ----- ctl params -----
  316. /* set the path to RPC unix socket control file */
  317. # modparam("ctl", "binrpc", "unix:/run/kamailio/kamailio_ctl")
  318. # ----- sanity params -----
  319. modparam("sanity", "autodrop", 0)
  320. # ----- tm params -----
  321. # auto-discard branches from previous serial forking leg
  322. modparam("tm", "failure_reply_mode", 3)
  323. # default retransmission timeout: 30sec
  324. modparam("tm", "fr_timer", 30000)
  325. # default invite retransmission timeout after 1xx: 120sec
  326. modparam("tm", "fr_inv_timer", 120000)
  327. # ----- rr params -----
  328. # set next param to 1 to add value to ;lr param (helps with some UAs)
  329. modparam("rr", "enable_full_lr", 0)
  330. # do not append from tag to the RR (no need for this script)
  331. modparam("rr", "append_fromtag", 0)
  332. # ----- registrar params -----
  333. modparam("registrar", "method_filtering", 1)
  334. /* uncomment the next line to disable parallel forking via location */
  335. # modparam("registrar", "append_branches", 0)
  336. /* uncomment the next line not to allow more than 10 contacts per AOR */
  337. # modparam("registrar", "max_contacts", 10)
  338. /* max value for expires of registrations */
  339. modparam("registrar", "max_expires", 3600)
  340. /* set it to 1 to enable GRUU */
  341. modparam("registrar", "gruu_enabled", 0)
  342. /* set it to 0 to disable Path handling */
  343. modparam("registrar", "use_path", 1)
  344. /* save Path even if not listed in Supported header */
  345. modparam("registrar", "path_mode", 0)
  346. # ----- acc params -----
  347. /* what special events should be accounted ? */
  348. modparam("acc", "early_media", 0)
  349. modparam("acc", "report_ack", 0)
  350. modparam("acc", "report_cancels", 0)
  351. /* by default we do not adjust the direct of the sequential requests.
  352. * if you enable this parameter, be sure the enable "append_fromtag"
  353. * in "rr" module */
  354. modparam("acc", "detect_direction", 0)
  355. /* account triggers (flags) */
  356. modparam("acc", "log_flag", FLT_ACC)
  357. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  358. modparam("acc", "log_extra",
  359. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  360. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  361. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  362. /* enhanced DB accounting */
  363. #!ifdef WITH_ACCDB
  364. modparam("acc", "db_flag", FLT_ACC)
  365. modparam("acc", "db_missed_flag", FLT_ACCMISSED)
  366. modparam("acc", "db_url", DBURL)
  367. modparam("acc", "db_extra",
  368. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  369. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  370. #!endif
  371. # ----- usrloc params -----
  372. modparam("usrloc", "timer_interval", 60)
  373. modparam("usrloc", "timer_procs", 1)
  374. modparam("usrloc", "use_domain", MULTIDOMAIN)
  375. /* enable DB persistency for location entries */
  376. #!ifdef WITH_USRLOCDB
  377. modparam("usrloc", "db_url", DBURL)
  378. modparam("usrloc", "db_mode", 2)
  379. #!endif
  380. # ----- auth_db params -----
  381. #!ifdef WITH_AUTH
  382. modparam("auth_db", "db_url", DBURL)
  383. modparam("auth_db", "calculate_ha1", yes)
  384. modparam("auth_db", "password_column", "password")
  385. modparam("auth_db", "load_credentials", "")
  386. modparam("auth_db", "use_domain", MULTIDOMAIN)
  387. # ----- permissions params -----
  388. #!ifdef WITH_IPAUTH
  389. modparam("permissions", "db_url", DBURL)
  390. modparam("permissions", "load_backends", 1)
  391. #!endif
  392. #!endif
  393. # ----- alias_db params -----
  394. #!ifdef WITH_ALIASDB
  395. modparam("alias_db", "db_url", DBURL)
  396. modparam("alias_db", "use_domain", MULTIDOMAIN)
  397. #!endif
  398. # ----- speeddial params -----
  399. #!ifdef WITH_SPEEDDIAL
  400. modparam("speeddial", "db_url", DBURL)
  401. modparam("speeddial", "use_domain", MULTIDOMAIN)
  402. #!endif
  403. # ----- domain params -----
  404. #!ifdef WITH_MULTIDOMAIN
  405. modparam("domain", "db_url", DBURL)
  406. /* register callback to match myself condition with domains list */
  407. modparam("domain", "register_myself", 1)
  408. #!endif
  409. #!ifdef WITH_PRESENCE
  410. # ----- presence params -----
  411. modparam("presence", "db_url", DBURL)
  412. # ----- presence_xml params -----
  413. modparam("presence_xml", "db_url", DBURL)
  414. modparam("presence_xml", "force_active", 1)
  415. #!endif
  416. #!ifdef WITH_NAT
  417. #!ifdef WITH_RTPENGINE
  418. # ----- rtpengine params -----
  419. modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:2223")
  420. #!else
  421. # ----- rtpproxy params -----
  422. modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
  423. #!endif
  424. # ----- nathelper params -----
  425. modparam("nathelper", "natping_interval", 30)
  426. modparam("nathelper", "ping_nated_only", 1)
  427. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  428. modparam("nathelper", "sipping_from", "sip:[email protected]")
  429. # params needed for NAT traversal in other modules
  430. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  431. modparam("usrloc", "nat_bflag", FLB_NATB)
  432. #!endif
  433. #!ifdef WITH_TLS
  434. # ----- tls params -----
  435. modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
  436. #!endif
  437. #!ifdef WITH_ANTIFLOOD
  438. # ----- pike params -----
  439. modparam("pike", "sampling_time_unit", 2)
  440. modparam("pike", "reqs_density_per_unit", 16)
  441. modparam("pike", "remove_latency", 4)
  442. #!endif
  443. #!ifdef WITH_HTABLE
  444. # ----- htable params -----
  445. #!ifdef WITH_ANTIFLOOD
  446. /* ip ban htable with autoexpire after 5 minutes */
  447. modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
  448. #!endif
  449. #!endif
  450. #!ifdef WITH_DEBUG
  451. # ----- debugger params -----
  452. modparam("debugger", "cfgtrace", 1)
  453. modparam("debugger", "log_level_name", "exec")
  454. #!endif
  455. ####### Routing Logic ########
  456. /* Main SIP request routing logic
  457. * - processing of any incoming SIP request starts with this route
  458. * - note: this is the same as route { ... } */
  459. request_route {
  460. # per request initial checks
  461. route(REQINIT);
  462. # NAT detection
  463. route(NATDETECT);
  464. # CANCEL processing
  465. if (is_method("CANCEL")) {
  466. if (t_check_trans()) {
  467. route(RELAY);
  468. }
  469. exit;
  470. }
  471. # handle retransmissions
  472. if (!is_method("ACK")) {
  473. if(t_precheck_trans()) {
  474. t_check_trans();
  475. exit;
  476. }
  477. t_check_trans();
  478. }
  479. # handle requests within SIP dialogs
  480. route(WITHINDLG);
  481. ### only initial requests (no To tag)
  482. # authentication
  483. route(AUTH);
  484. # record routing for dialog forming requests (in case they are routed)
  485. # - remove preloaded route headers
  486. remove_hf("Route");
  487. if (is_method("INVITE|SUBSCRIBE")) {
  488. record_route();
  489. }
  490. # account only INVITEs
  491. if (is_method("INVITE")) {
  492. setflag(FLT_ACC); # do accounting
  493. }
  494. # dispatch requests to foreign domains
  495. route(SIPOUT);
  496. ### requests for my local domains
  497. # handle presence related requests
  498. route(PRESENCE);
  499. # handle registrations
  500. route(REGISTRAR);
  501. if ($rU==$null) {
  502. # request with no Username in RURI
  503. sl_send_reply("484", "Address Incomplete");
  504. exit;
  505. }
  506. # dispatch destinations to PSTN
  507. route(PSTN);
  508. # user location service
  509. route(LOCATION);
  510. return;
  511. }
  512. # Wrapper for relaying requests
  513. route[RELAY] {
  514. # enable additional event routes for forwarded requests
  515. # - serial forking, RTP relaying handling, a.s.o.
  516. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  517. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  518. }
  519. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  520. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  521. }
  522. if (is_method("INVITE")) {
  523. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  524. }
  525. if (!t_relay()) {
  526. sl_reply_error();
  527. }
  528. exit;
  529. }
  530. # Per SIP request initial checks
  531. route[REQINIT] {
  532. # no connect for sending replies
  533. set_reply_no_connect();
  534. # enforce symmetric signaling
  535. # - send back replies to the source address of request
  536. force_rport();
  537. #!ifdef WITH_ANTIFLOOD
  538. # flood detection from same IP and traffic ban for a while
  539. # be sure you exclude checking trusted peers, such as pstn gateways
  540. # - local host excluded (e.g., loop to self)
  541. if(src_ip!=myself) {
  542. if($sht(ipban=>$si)!=$null) {
  543. # ip is already blocked
  544. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  545. exit;
  546. }
  547. if (!pike_check_req()) {
  548. xalert("ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  549. $sht(ipban=>$si) = 1;
  550. exit;
  551. }
  552. }
  553. #!endif
  554. if($ua =~ "friendly|scanner|sipcli|sipvicious|VaxSIPUserAgent|pplsip") {
  555. # silent drop for scanners - uncomment next line if want to reply
  556. # sl_send_reply("200", "OK");
  557. exit;
  558. }
  559. if (!mf_process_maxfwd_header("10")) {
  560. sl_send_reply("483", "Too Many Hops");
  561. exit;
  562. }
  563. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  564. sl_send_reply("200", "Keepalive");
  565. exit;
  566. }
  567. if(!sanity_check("17895", "7")) {
  568. xlog("Malformed SIP request from $si:$sp\n");
  569. exit;
  570. }
  571. }
  572. # Handle requests within SIP dialogs
  573. route[WITHINDLG] {
  574. if (!has_totag()) return;
  575. # sequential request within a dialog should
  576. # take the path determined by record-routing
  577. if (loose_route()) {
  578. route(DLGURI);
  579. if (is_method("BYE")) {
  580. setflag(FLT_ACC); # do accounting ...
  581. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  582. } else if ( is_method("ACK") ) {
  583. # ACK is forwarded statelessly
  584. route(NATMANAGE);
  585. } else if ( is_method("NOTIFY") ) {
  586. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  587. record_route();
  588. }
  589. route(RELAY);
  590. exit;
  591. }
  592. if (is_method("SUBSCRIBE") && uri == myself) {
  593. # in-dialog subscribe requests
  594. route(PRESENCE);
  595. exit;
  596. }
  597. if ( is_method("ACK") ) {
  598. if ( t_check_trans() ) {
  599. # no loose-route, but stateful ACK;
  600. # must be an ACK after a 487
  601. # or e.g. 404 from upstream server
  602. route(RELAY);
  603. exit;
  604. } else {
  605. # ACK without matching transaction ... ignore and discard
  606. exit;
  607. }
  608. }
  609. sl_send_reply("404", "Not here");
  610. exit;
  611. }
  612. # Handle SIP registrations
  613. route[REGISTRAR] {
  614. if (!is_method("REGISTER")) return;
  615. if(isflagset(FLT_NATS)) {
  616. setbflag(FLB_NATB);
  617. #!ifdef WITH_NATSIPPING
  618. # do SIP NAT pinging
  619. setbflag(FLB_NATSIPPING);
  620. #!endif
  621. }
  622. if (!save("location")) {
  623. sl_reply_error();
  624. }
  625. exit;
  626. }
  627. # User location service
  628. route[LOCATION] {
  629. #!ifdef WITH_SPEEDDIAL
  630. # search for short dialing - 2-digit extension
  631. if($rU=~"^[0-9][0-9]$") {
  632. if(sd_lookup("speed_dial")) {
  633. route(SIPOUT);
  634. }
  635. }
  636. #!endif
  637. #!ifdef WITH_ALIASDB
  638. # search in DB-based aliases
  639. if(alias_db_lookup("dbaliases")) {
  640. route(SIPOUT);
  641. }
  642. #!endif
  643. $avp(oexten) = $rU;
  644. if (!lookup("location")) {
  645. $var(rc) = $rc;
  646. route(TOVOICEMAIL);
  647. t_newtran();
  648. switch ($var(rc)) {
  649. case -1:
  650. case -3:
  651. send_reply("404", "Not Found");
  652. exit;
  653. case -2:
  654. send_reply("405", "Method Not Allowed");
  655. exit;
  656. }
  657. }
  658. # when routing via usrloc, log the missed calls also
  659. if (is_method("INVITE")) {
  660. setflag(FLT_ACCMISSED);
  661. }
  662. route(RELAY);
  663. exit;
  664. }
  665. # Presence server processing
  666. route[PRESENCE] {
  667. if(!is_method("PUBLISH|SUBSCRIBE")) return;
  668. if(is_method("SUBSCRIBE") && $hdr(Event)=="message-summary") {
  669. route(TOVOICEMAIL);
  670. # returns here if no voicemail server is configured
  671. sl_send_reply("404", "No voicemail service");
  672. exit;
  673. }
  674. #!ifdef WITH_PRESENCE
  675. #!ifdef WITH_MSGREBUILD
  676. # apply changes in case the request headers or body were modified
  677. msg_apply_changes();
  678. #!endif
  679. if (!t_newtran()) {
  680. sl_reply_error();
  681. exit;
  682. }
  683. if(is_method("PUBLISH")) {
  684. handle_publish();
  685. t_release();
  686. } else if(is_method("SUBSCRIBE")) {
  687. handle_subscribe();
  688. t_release();
  689. }
  690. exit;
  691. #!endif
  692. # if presence enabled, this part will not be executed
  693. if (is_method("PUBLISH") || $rU==$null) {
  694. sl_send_reply("404", "Not here");
  695. exit;
  696. }
  697. return;
  698. }
  699. # IP authorization and user authentication
  700. route[AUTH] {
  701. #!ifdef WITH_AUTH
  702. #!ifdef WITH_IPAUTH
  703. if((!is_method("REGISTER")) && allow_source_address()) {
  704. # source IP allowed
  705. return;
  706. }
  707. #!endif
  708. if (is_method("REGISTER") || from_uri==myself) {
  709. # authenticate requests
  710. if (!auth_check("$fd", "subscriber", "1")) {
  711. auth_challenge("$fd", "0");
  712. exit;
  713. }
  714. # user authenticated - remove auth header
  715. if(!is_method("REGISTER|PUBLISH"))
  716. consume_credentials();
  717. }
  718. # if caller is not local subscriber, then check if it calls
  719. # a local destination, otherwise deny, not an open relay here
  720. if (from_uri!=myself && uri!=myself) {
  721. sl_send_reply("403", "Not relaying");
  722. exit;
  723. }
  724. #!else
  725. # authentication not enabled - do not relay at all to foreign networks
  726. if(uri!=myself) {
  727. sl_send_reply("403", "Not relaying");
  728. exit;
  729. }
  730. #!endif
  731. return;
  732. }
  733. # Caller NAT detection
  734. route[NATDETECT] {
  735. #!ifdef WITH_NAT
  736. if (nat_uac_test("19")) {
  737. if (is_method("REGISTER")) {
  738. fix_nated_register();
  739. } else {
  740. if(is_first_hop()) {
  741. set_contact_alias();
  742. }
  743. }
  744. setflag(FLT_NATS);
  745. }
  746. #!endif
  747. return;
  748. }
  749. # RTP relaying management and signaling updates for NAT traversal
  750. route[NATMANAGE] {
  751. #!ifdef WITH_NAT
  752. if (is_request()) {
  753. if(has_totag()) {
  754. if(check_route_param("nat=yes")) {
  755. setbflag(FLB_NATB);
  756. }
  757. }
  758. }
  759. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB))) return;
  760. #!ifdef WITH_RTPENGINE
  761. if(nat_uac_test("8")) {
  762. rtpengine_manage("SIP-source-address replace-origin replace-session-connection");
  763. } else {
  764. rtpengine_manage("replace-origin replace-session-connection");
  765. }
  766. #!else
  767. if(nat_uac_test("8")) {
  768. rtpproxy_manage("co");
  769. } else {
  770. rtpproxy_manage("cor");
  771. }
  772. #!endif
  773. if (is_request()) {
  774. if (!has_totag()) {
  775. if(t_is_branch_route()) {
  776. add_rr_param(";nat=yes");
  777. }
  778. }
  779. }
  780. if (is_reply()) {
  781. if(isbflagset(FLB_NATB)) {
  782. if(is_first_hop())
  783. set_contact_alias();
  784. }
  785. }
  786. if(isbflagset(FLB_NATB)) {
  787. # no connect message in a dialog involving NAT traversal
  788. if (is_request()) {
  789. if(has_totag()) {
  790. set_forward_no_connect();
  791. }
  792. }
  793. }
  794. #!endif
  795. return;
  796. }
  797. # URI update for dialog requests
  798. route[DLGURI] {
  799. #!ifdef WITH_NAT
  800. if(!isdsturiset()) {
  801. handle_ruri_alias();
  802. }
  803. #!endif
  804. return;
  805. }
  806. # Routing to foreign domains
  807. route[SIPOUT] {
  808. if (uri==myself) return;
  809. append_hf("P-Hint: outbound\r\n");
  810. route(RELAY);
  811. exit;
  812. }
  813. # PSTN GW routing
  814. route[PSTN] {
  815. #!ifdef WITH_PSTN
  816. # check if PSTN GW IP is defined
  817. if (strempty($sel(cfg_get.pstn.gw_ip))) {
  818. xlog("SCRIPT: PSTN routing enabled but pstn.gw_ip not defined\n");
  819. return;
  820. }
  821. # route to PSTN dialed numbers starting with '+' or '00'
  822. # (international format)
  823. # - update the condition to match your dialing rules for PSTN routing
  824. if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$")) return;
  825. # only local users allowed to call
  826. if(from_uri!=myself) {
  827. sl_send_reply("403", "Not Allowed");
  828. exit;
  829. }
  830. # normalize target number for pstn gateway
  831. # - convert leading 00 to +
  832. if (starts_with("$rU", "00")) {
  833. strip(2);
  834. prefix("+");
  835. }
  836. if (strempty($sel(cfg_get.pstn.gw_port))) {
  837. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
  838. } else {
  839. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip) + ":"
  840. + $sel(cfg_get.pstn.gw_port);
  841. }
  842. route(RELAY);
  843. exit;
  844. #!endif
  845. return;
  846. }
  847. # JSONRPC over HTTP(S) routing
  848. #!ifdef WITH_JSONRPC
  849. event_route[xhttp:request] {
  850. set_reply_close();
  851. set_reply_no_connect();
  852. if(src_ip!=127.0.0.1) {
  853. xhttp_reply("403", "Forbidden", "text/html",
  854. "<html><body>Not allowed from $si</body></html>");
  855. exit;
  856. }
  857. if ($hu =~ "^/RPC") {
  858. jsonrpc_dispatch();
  859. exit;
  860. }
  861. xhttp_reply("200", "OK", "text/html",
  862. "<html><body>Wrong URL $hu</body></html>");
  863. exit;
  864. }
  865. #!endif
  866. # Routing to voicemail server
  867. route[TOVOICEMAIL] {
  868. #!ifdef WITH_VOICEMAIL
  869. if(!is_method("INVITE|SUBSCRIBE")) return;
  870. # check if VoiceMail server IP is defined
  871. if (strempty($sel(cfg_get.voicemail.srv_ip))) {
  872. xlog("SCRIPT: VoiceMail routing enabled but IP not defined\n");
  873. return;
  874. }
  875. if(is_method("INVITE")) {
  876. if($avp(oexten)==$null) return;
  877. $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
  878. + ":" + $sel(cfg_get.voicemail.srv_port);
  879. } else {
  880. if($rU==$null) return;
  881. $ru = "sip:" + $rU + "@" + $sel(cfg_get.voicemail.srv_ip)
  882. + ":" + $sel(cfg_get.voicemail.srv_port);
  883. }
  884. route(RELAY);
  885. exit;
  886. #!endif
  887. return;
  888. }
  889. # Manage outgoing branches
  890. branch_route[MANAGE_BRANCH] {
  891. xdbg("new branch [$T_branch_idx] to $ru\n");
  892. route(NATMANAGE);
  893. return;
  894. }
  895. # Manage incoming replies
  896. reply_route {
  897. if(!sanity_check("17604", "6")) {
  898. xlog("Malformed SIP response from $si:$sp\n");
  899. drop;
  900. }
  901. return;
  902. }
  903. # Manage incoming replies in transaction context
  904. onreply_route[MANAGE_REPLY] {
  905. xdbg("incoming reply\n");
  906. if(status=~"[12][0-9][0-9]") {
  907. route(NATMANAGE);
  908. }
  909. return;
  910. }
  911. # Manage failure routing cases
  912. failure_route[MANAGE_FAILURE] {
  913. route(NATMANAGE);
  914. if (t_is_canceled()) exit;
  915. #!ifdef WITH_BLOCK3XX
  916. # block call redirect based on 3xx replies.
  917. if (t_check_status("3[0-9][0-9]")) {
  918. t_reply("404", "Not found");
  919. exit;
  920. }
  921. #!endif
  922. #!ifdef WITH_BLOCK401407
  923. # block call redirect based on 401, 407 replies.
  924. if (t_check_status("401|407")) {
  925. t_reply("404", "Not found");
  926. exit;
  927. }
  928. #!endif
  929. #!ifdef WITH_VOICEMAIL
  930. # serial forking
  931. # - route to voicemail on busy or no answer (timeout)
  932. if (t_check_status("486|408")) {
  933. $du = $null;
  934. route(TOVOICEMAIL);
  935. exit;
  936. }
  937. #!endif
  938. return;
  939. }