kamailio.cfg 27 KB

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