kamailio.cfg 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. #!KAMAILIO
  2. #
  3. # This config file implements the basic P-CSCF functionality
  4. # - web: http://www.kamailio.org
  5. # - git: http://sip-router.org
  6. #
  7. # Refer to the Core CookBook at http://www.kamailio.org/dokuwiki/doku.php
  8. # for an explanation of possible statements, functions and parameters.
  9. #
  10. # Direct your questions about this file to: <[email protected]>.
  11. #
  12. # For more information about the various parameters, functions and statements
  13. # try http://sip-router.org/wiki/ .
  14. #
  15. ####### Defined Values #########
  16. # *** Value defines - IDs used later in config
  17. # - flags
  18. # FLT_ - per transaction (message) flags
  19. # FLB_ - per branch flags
  20. #!define FLT_CAPTURE 1
  21. #!define FLT_DIALOG 2
  22. #!define DLG_TIMEOUT_AVP "i:1"
  23. #!define RR_CUSTOM_USER_AVP "i:2"
  24. #!define DISPATCHER_DST_AVP "i:3"
  25. #!define DISPATCHER_GRP_AVP "i:4"
  26. #!define DISPATCHER_CNT_AVP "i:5"
  27. #!define DISPATCHER_SOCK_AVP "i:6"
  28. ####### Global Parameters #########
  29. include_file "scscf.cfg"
  30. #!ifdef WITH_DEBUG
  31. debug=5
  32. log_stderror=no
  33. sip_warning=yes
  34. #!else
  35. debug=2
  36. log_stderror=no
  37. sip_warning=no
  38. #!endif
  39. #!ifdef WITH_XMLRPC
  40. listen=tcp:127.0.0.1:6060
  41. #!endif
  42. alias=HOSTNAME
  43. user_agent_header="User-Agent: Kamailio S-CSCF"
  44. server_header="Server: Kamailio S-CSCF"
  45. /* comment the next line to enable the auto discovery of local aliases
  46. based on reverse DNS on IPs (default on) */
  47. auto_aliases=no
  48. check_via=no # (cmd. line: -v)
  49. dns=no # (cmd. line: -r)
  50. rev_dns=no # (cmd. line: -R)
  51. # Do SRV-Loadbalancing:
  52. dns_srv_lb=yes
  53. # Always: Also try IPv6:
  54. dns_try_ipv6=yes
  55. # Always prefer IPv6:
  56. dns_cache_flags=6
  57. # DNS-Based failover
  58. use_dns_failover = on
  59. # Query NAPTR-Records as well:
  60. dns_try_naptr=no
  61. #!ifdef WITH_XMLRPC
  62. #!ifndef WITH_TCP
  63. #!define WITH_TCP
  64. #!endif
  65. #!ifndef TCP_PROCESSES
  66. # Number of TCP Processes
  67. #!define TCP_PROCESSES 3
  68. #!endif
  69. #!endif
  70. #!ifdef WITH_TCP
  71. # life time of TCP connection when there is no traffic
  72. # - a bit higher than registration expires to cope with UA behind NAT
  73. tcp_connection_lifetime=3615
  74. #!ifdef TCP_PROCESSES
  75. tcp_children=TCP_PROCESSES
  76. #!endif
  77. #!else
  78. disable_tcp=yes
  79. #!endif
  80. children=12
  81. system.shutdownmode = 0 desc "System shutdown mode"
  82. system.service = "Serving-CSCF" desc "Function of this server"
  83. # ------------------ module loading ----------------------------------
  84. mpath="/usr/lib64/kamailio/modules_k/:/usr/lib64/kamailio/modules/:/usr/lib/kamailio/modules_k/:/usr/lib/kamailio/modules/"
  85. # (we try both the lib64 and the lib directory)
  86. loadmodule "tm.so"
  87. loadmodule "pv.so"
  88. loadmodule "sl.so"
  89. loadmodule "rr.so"
  90. loadmodule "ims_dialog.so"
  91. loadmodule "textops.so"
  92. loadmodule "textopsx.so"
  93. loadmodule "maxfwd.so"
  94. loadmodule "xlog.so"
  95. loadmodule "sanity.so"
  96. loadmodule "siputils.so"
  97. loadmodule "kex.so"
  98. loadmodule "tmx.so"
  99. loadmodule "pike.so"
  100. #!ifdef DB_URL
  101. loadmodule "presence"
  102. #!endif
  103. #!ifdef DB_URL
  104. loadmodule "db_mysql"
  105. #!ifdef DB_URL2
  106. loadmodule "db_cluster"
  107. #!endif
  108. #!endif
  109. loadmodule "dispatcher"
  110. loadmodule "enum"
  111. loadmodule "uac"
  112. # Control interfaces:
  113. loadmodule "ctl"
  114. loadmodule "cfg_rpc"
  115. #!ifdef WITH_XMLRPC
  116. loadmodule "xmlrpc"
  117. #!endif
  118. loadmodule "cdp.so"
  119. loadmodule "cdp_avp.so"
  120. loadmodule "ims_usrloc_scscf.so"
  121. loadmodule "ims_registrar_scscf.so"
  122. loadmodule "ims_auth.so"
  123. loadmodule "ims_isc.so"
  124. #!ifdef WITH_RO
  125. loadmodule "ims_charging.so"
  126. #!endif
  127. #!ifdef CAPTURE_NODE
  128. loadmodule "siptrace.so"
  129. #!endif
  130. ##!ifdef WITH_DEBUG
  131. loadmodule "debugger.so"
  132. modparam("debugger", "mod_hash_size", 5)
  133. modparam("debugger", "mod_level_mode", 1)
  134. #modparam("debugger", "mod_level", "ims_usrloc_scscf=3")
  135. #modparam("debugger", "mod_level", "ims_registrar_scscf=3")
  136. modparam("debugger", "mod_level", "ims_auth=3")
  137. #modparam("debugger", "mod_level", "ims_isc=3")
  138. modparam("debugger", "mod_level", "ims_dialog=3")
  139. #modparam("debugger", "mod_level", "ims_charging=3")
  140. ##!endif
  141. # ----------------- setting module-specific parameters ---------------
  142. #!ifdef DB_URL2
  143. # ----- db_cluster params -----
  144. modparam("db_cluster", "connection", DB_URL)
  145. modparam("db_cluster", "connection", DB_URL2)
  146. modparam("db_cluster", "cluster", "cluster1=>con1=2s2s;con2=1s1s")
  147. #!endif
  148. # ----- presence params -----
  149. #!ifdef DB_URL
  150. #!ifdef DB_URL2
  151. modparam("presence", "db_url", "cluster://cluster1")
  152. #!else
  153. modparam("presence", "db_url", DB_URL)
  154. #!endif
  155. #modparam("presence", "fallback2db", 1)
  156. modparam("presence", "db_update_period", 20)
  157. #!endif
  158. loadmodule "jsonrpcs.so"
  159. # ----- jsonrpcs params -----
  160. modparam("jsonrpcs", "pretty_format", 1)
  161. /* set the path to RPC fifo control file */
  162. # modparam("jsonrpcs", "fifo_name", "/var/run/kamailio/kamailio_rpc.fifo")
  163. /* set the path to RPC unix socket control file */
  164. # modparam("jsonrpcs", "dgram_socket", "/var/run/kamailio/kamailio_rpc.sock")
  165. # ----- ctl params -----
  166. modparam("ctl", "binrpc", "unix:/var/run/kamailio_scscf/kamailio_ctl")
  167. # ----- tm params -----
  168. # auto-discard branches from previous serial forking leg
  169. modparam("tm", "failure_reply_mode", 3)
  170. # default retransmission timeout: 10sec
  171. modparam("tm", "fr_timer", 10000)
  172. # default invite retransmission timeout after 1xx: 120sec
  173. modparam("tm", "fr_inv_timer", 120000)
  174. # Don't reply automatically with "100 Trying"
  175. modparam("tm", "auto_inv_100", 0)
  176. # ----- rr params -----
  177. # add value to ;lr param to cope with most of the UAs
  178. modparam("rr", "enable_full_lr", 1)
  179. # append from tag to the RR
  180. modparam("rr", "append_fromtag", 1)
  181. # add a Username to RR-Header
  182. modparam("rr", "add_username", 1)
  183. # Take User from a custom AVP
  184. modparam("rr", "custom_user_avp", "$avp(RR_CUSTOM_USER_AVP)")
  185. # -- usrloc params --
  186. modparam("ims_usrloc_scscf", "enable_debug_file", 0)
  187. modparam("ims_usrloc_scscf", "matching_mode", 0)
  188. modparam("ims_registrar_scscf", "max_contacts", 3);
  189. modparam("ims_usrloc_scscf", "maxcontact_behaviour", 2) #overwrite
  190. #!ifdef DB_URL
  191. #!ifdef DB_URL2
  192. modparam("ims_usrloc_scscf", "db_url", "cluster://cluster1")
  193. #!else
  194. modparam("ims_usrloc_scscf", "db_url", DB_URL)
  195. #!endif
  196. modparam("ims_usrloc_scscf", "db_mode", 1)
  197. #!endif
  198. modparam("ims_registrar_scscf", "subscription_default_expires", 654800)
  199. modparam("ims_registrar_scscf", "subscription_min_expires", 3700)
  200. modparam("ims_registrar_scscf", "subscription_max_expires", 605800)
  201. # -- CDP params --
  202. modparam("cdp","config_file","/etc/kamailio_scscf/scscf.xml")
  203. # -- ims_dialog params --
  204. modparam("ims_dialog", "dlg_flag", FLT_DIALOG)
  205. modparam("ims_dialog", "timeout_avp", "$avp(DLG_TIMEOUT_AVP)")
  206. modparam("ims_dialog", "detect_spirals", 1)
  207. modparam("ims_dialog", "profiles_no_value", "orig ; term")
  208. modparam("ims_dialog", "db_mode", 0)
  209. #!ifdef WITH_XMLRPC
  210. # ----- xmlrpc params -----
  211. modparam("xmlrpc", "route", "XMLRPC");
  212. modparam("xmlrpc", "url_match", "^/RPC")
  213. #!endif
  214. #!ifdef WITH_DEBUG
  215. # ----- debugger params -----
  216. modparam("debugger", "cfgtrace", 1)
  217. #!endif
  218. #!ifdef CAPTURE_NODE
  219. # Destination, where to send the traffic
  220. modparam("siptrace", "duplicate_uri", CAPTURE_NODE)
  221. # Trace all traffic
  222. modparam("siptrace", "trace_on", 1)
  223. modparam("siptrace", "trace_to_database", 0)
  224. modparam("siptrace", "trace_flag", FLT_CAPTURE)
  225. modparam("siptrace", "hep_mode_on", 1)
  226. #!endif
  227. # -- ims_auth params --
  228. modparam("ims_auth", "name", URI)
  229. modparam("ims_auth", "registration_default_algorithm", REG_AUTH_DEFAULT_ALG)
  230. #!ifdef CXDX_FORCED_PEER
  231. modparam("ims_auth", "cxdx_forced_peer", CXDX_FORCED_PEER)
  232. #!endif
  233. modparam("ims_auth", "cxdx_dest_realm", NETWORKNAME)
  234. modparam("ims_auth", "av_check_only_impu", 1)
  235. #modparam("ims_auth", "auth_data_timeout", 5)
  236. modparam("ims_auth","auth_used_vector_timeout", 300)
  237. # -- ims_registrar_scscf params --
  238. #!ifdef WITH_DEBUG
  239. modparam("ims_registrar_scscf", "default_expires", 60)
  240. modparam("ims_registrar_scscf", "min_expires", 60)
  241. modparam("ims_registrar_scscf", "max_expires", 60)
  242. #!else
  243. modparam("ims_registrar_scscf", "default_expires", 604800)
  244. modparam("ims_registrar_scscf", "min_expires", 60)
  245. modparam("ims_registrar_scscf", "max_expires", 604800)
  246. #!endif
  247. modparam("ims_registrar_scscf", "use_path", 1)
  248. modparam("ims_registrar_scscf", "support_wildcardPSI",1)
  249. modparam("ims_registrar_scscf", "user_data_xsd","/etc/kamailio_scscf/CxDataType_Rel7.xsd")
  250. modparam("ims_registrar_scscf", "scscf_name", URI)
  251. modparam("ims_registrar_scscf", "scscf_name", URI)
  252. modparam("ims_registrar_scscf", "cxdx_dest_realm", NETWORKNAME)
  253. #!ifdef WITH_MULTIDOMAIN
  254. # ----- domain params -----
  255. #!ifdef DB_URL
  256. #!ifdef DB_URL2
  257. modparam("domain", "db_url", "cluster://cluster1")
  258. #!else
  259. modparam("domain", "db_url", DB_URL)
  260. #!endif
  261. modparam("domain", "db_mode", 1)
  262. #!endif
  263. # register callback to match myself condition with domains list
  264. modparam("domain", "register_myself", 1)
  265. #!endif
  266. # ----- ims_isc params -----
  267. modparam("ims_isc", "my_uri", HOSTNAME)
  268. modparam("ims_isc", "add_p_served_user", 1)
  269. #!ifdef WITH_RO
  270. # ----- ims_diameter_ro params -----
  271. #!ifdef DB_URL
  272. #!ifdef DB_URL2
  273. #modparam("ims_charging", "db_url", "cluster://cluster1")
  274. #!else
  275. #modparam("ims_charging", "db_url", DB_URL)
  276. #!endif
  277. #modparam("ims_charging", "db_mode", 1)
  278. #!endif
  279. modparam("ims_charging", "origin_host", HOSTNAME);
  280. modparam("ims_charging", "origin_realm", NETWORKNAME);
  281. #!ifdef RO_FORCED_PEER
  282. modparam("ims_charging", "ro_forced_peer", RO_FORCED_PEER);
  283. #!endif
  284. modparam("ims_charging", "destination_host", RO_DESTINATION);
  285. modparam("ims_charging", "destination_realm", NETWORKNAME);
  286. modparam("ims_charging","service_context_id_root", RO_ROOT);
  287. modparam("ims_charging","service_context_id_ext", RO_EXT);
  288. modparam("ims_charging","service_context_id_mnc", RO_MNC);
  289. modparam("ims_charging","service_context_id_mcc", RO_MCC);
  290. modparam("ims_charging","service_context_id_release", RO_RELEASE);
  291. modparam("ims_charging","interim_update_credits",30);
  292. modparam("ims_charging","timer_buffer",5);
  293. #!endif
  294. # ----- enum params -----
  295. modparam("enum", "domain_suffix", ENUM_SUFFIX)
  296. # ----- sanity params -----
  297. modparam("sanity", "autodrop", 0)
  298. # ----------------- Settings for Dispatcher ---------------
  299. modparam("dispatcher", "list_file", "/etc/kamailio_scscf/dispatcher.list")
  300. # Dispatcher: Enable Failover-Support
  301. modparam("dispatcher", "flags", 2)
  302. # Dispatcher: Overwrite Destination address, if required.
  303. modparam("dispatcher", "force_dst", 1)
  304. # AVP's required for Fail-Over-Support:
  305. modparam("dispatcher", "dst_avp", "$avp(DISPATCHER_DST_AVP)")
  306. modparam("dispatcher", "grp_avp", "$avp(DISPATCHER_GRP_AVP)")
  307. modparam("dispatcher", "cnt_avp", "$avp(DISPATCHER_CNT_AVP)")
  308. modparam("dispatcher", "sock_avp", "$avp(DISPATCHER_SOCK_AVP)")
  309. # Try to recover disabled destinations every 15 seconds.
  310. modparam("dispatcher", "ds_ping_interval", 15)
  311. # Actively query the gateways:
  312. modparam("dispatcher", "ds_probing_mode", 1)
  313. modparam("dispatcher", "ds_ping_reply_codes", "class=2;code=404;code=480")
  314. ####### Routing Logic ########
  315. # Main SIP request routing logic
  316. # - processing of any incoming SIP request starts with this route
  317. route {
  318. ##!ifdef WITH_DEBUG
  319. xlog("$rm ($fu ($si:$sp) to $tu, $ci)\n");
  320. ##!endif
  321. # per request initial checks
  322. route(REQINIT);
  323. # Handle Registrations:
  324. if (is_method("REGISTER")) {
  325. route(REGISTER);
  326. exit;
  327. }
  328. # we need to support subscription to reg event
  329. if (is_method("SUBSCRIBE") && search("^(Event|o)([ \t]*):([ \t]*)reg")) {
  330. route(SUBSCRIBE);
  331. break;
  332. }
  333. if (is_method("PUBLISH") && search("^(Event|o)([ \t]*):([ \t]*)reg")) {
  334. route(PUBLISH);
  335. break;
  336. }
  337. #Set DLG flag to track dialogs using dialog2
  338. if (!is_method("REGISTER|SUBSCRIBE"))
  339. setflag(FLT_DIALOG);
  340. # Evaluate Route-Header and set $route_uri
  341. loose_route();
  342. if (is_method("CANCEL|ACK")) {
  343. t_relay();
  344. exit;
  345. }
  346. if (($route_uri =~ "sip:orig@.*") || isc_from_as("orig")) {
  347. xlog("Orig");
  348. # we need something like this to assign SCSCF to unregistered user for services
  349. # support for AS origination on behalf of unregistered useri
  350. # can use the registrar is_registered methods - must see if we need to check orig or term?
  351. # Originating
  352. route(orig);
  353. break;
  354. } else {
  355. isc_from_as("term");
  356. if ($retcode == -2) {
  357. # Treat as originating, since it was retargeted:
  358. route(orig);
  359. break;
  360. }
  361. if ((is_in_profile("orig") || has_totag()) && ($route_uri =~ "sip:mo@"+".*")) {
  362. route(orig_subsequent);
  363. break;
  364. }
  365. if ((is_in_profile("term") || has_totag()) && ($route_uri =~ "sip:mt@"+".*")) {
  366. route(term_subsequent);
  367. break;
  368. }
  369. # Terminating
  370. if (uri == myself || uri =~ "tel:.*") {
  371. if (!term_impu_registered("location")) {
  372. xlog("L_DBG", "We need to do an UNREG server SAR assignemnt");
  373. assign_server_unreg("UNREG_SAR_REPLY", "location", "term");
  374. exit;
  375. }
  376. } else {
  377. sl_send_reply("403","Forbidden - Domain not served");
  378. exit();
  379. }
  380. route(term);
  381. break;
  382. }
  383. }
  384. route[UNREG_SAR_REPLY]
  385. {
  386. xlog("L_DBG","saa_return code is $avp(s:saa_return_code)\n");
  387. switch ($avp(s:saa_return_code)){
  388. case 1: #success
  389. xlog("L_DBG", "SAR success - will route message\n");
  390. route(term);
  391. break;
  392. case -1: #failure
  393. xlog("L_ERR", "SAR failure - error response sent from module\n");
  394. break;
  395. case -2: #error
  396. xlog("L_ERR", "SAR error - error response sent from module\n");
  397. break;
  398. default:
  399. xlog("L_ERR", "Unknown return code from SAR, value is [$avp(s:saa_return_code)]\n");
  400. break;
  401. }
  402. exit;
  403. }
  404. ######################################################################
  405. # Helper routes (Basic-Checks, NAT-Handling/RTP-Control, XML-RPC)
  406. ######################################################################
  407. # Per SIP request initial checks
  408. route[REQINIT] {
  409. $var(used) = 1 - ($stat(free_size) / $stat(total_size));
  410. xlog("L_DBG", "Mem: Total $stat(total_size), Free $stat(free_size) [$var(used)% used]\n");
  411. if ($var(used) > 95) {
  412. send_reply("503", "Server overloaded");
  413. exit;
  414. }
  415. # Trace this message
  416. #!ifdef CAPTURE_NODE
  417. sip_trace();
  418. setflag(FLT_CAPTURE);
  419. #!endif
  420. if (!mf_process_maxfwd_header("10")) {
  421. sl_send_reply("483","Too Many Hops");
  422. exit;
  423. }
  424. if(!sanity_check("1511", "7")) {
  425. xlog("Malformed SIP message from $si:$sp\n");
  426. exit;
  427. }
  428. # Check for shutdown mode:
  429. if (!has_totag() && ($sel(cfg_get.system.shutdownmode) > 0)) {
  430. send_reply("503", "Server shutting down");
  431. exit;
  432. }
  433. # Reply to OPTIONS:
  434. if (is_method("OPTIONS") && (uri==myself)) {
  435. options_reply();
  436. exit;
  437. }
  438. # Ignore Re-Transmits:
  439. if (t_lookup_request()) {
  440. exit;
  441. }
  442. if (is_method("INVITE")) {
  443. send_reply("100", "Trying");
  444. }
  445. }
  446. ######################################################################
  447. # Publish route
  448. ######################################################################
  449. route[PUBLISH]
  450. {
  451. if (!t_newtran()) {
  452. #absorb retransmissions
  453. sl_reply("500","Could not create transaction");
  454. exit;
  455. }
  456. if (can_publish_reg("location")) {
  457. $var(ret)= publish_reg("location");
  458. switch ($var(ret)){
  459. case 1: #success
  460. xlog("L_DBG", "Publish reg successful");
  461. break;
  462. case -1: #failure
  463. xlog("L_ERR", "Publish reg failure - sending 500 Error now\n");
  464. t_reply("500","Server Error publishing subscription");
  465. break;
  466. default:
  467. xlog("L_ERR", "Unknown return code from publish reg event alue is [$var(ret)]\n");
  468. break;
  469. }
  470. } else {
  471. t_reply("403","Forbidden to PUBLISH");
  472. exit;
  473. }
  474. }
  475. ######################################################################
  476. # Subscribe route
  477. ######################################################################
  478. route[SUBSCRIBE]
  479. {
  480. if (!t_newtran()) {
  481. #absorb retransmissions
  482. sl_reply("500","Could not create transaction");
  483. exit;
  484. }
  485. if (!has_totag()) {
  486. xlog("L_DBG", "This is an initial SUBSCRIBE\n");
  487. if (!term_impu_registered("location")) {
  488. xlog("L_DBG", "We need to do an UNREG server SAR assignment\n");
  489. assign_server_unreg("SUBSCRIBE_UNREG_SAR_REPLY", "location", "term");
  490. exit;
  491. }
  492. if (!can_subscribe_to_reg("location")){
  493. t_reply("403","Forbidden to SUBSCRIBE");
  494. exit;
  495. }
  496. }else{
  497. xlog("L_DBG", "This is a subsequent SUBSCRIBE\n");
  498. }
  499. $var(ret)= subscribe_to_reg("location");
  500. switch ($var(ret)){
  501. case 1: #success
  502. xlog("L_DBG", "Subscribe to reg successful");
  503. break;
  504. case -1: #failure
  505. xlog("L_ERR", "Subscribe to reg failure - sending 500 Error now\n");
  506. t_reply("500","Server Error saving subscription");
  507. break;
  508. case -2: #error
  509. xlog("L_ERR", "Subscribe to reg error sending notify - 200 OK so subscription already sent\n");
  510. break;
  511. default:
  512. xlog("L_ERR", "Unknown return code from subscribe to reg event alue is [$var(ret)]\n");
  513. break;
  514. }
  515. }
  516. route[SUBSCRIBE_UNREG_SAR_REPLY]
  517. {
  518. xlog("L_DBG","saa_return code is $avp(s:saa_return_code)\n");
  519. switch ($avp(s:saa_return_code)){
  520. case 1: #success
  521. xlog("L_DBG", "SAR success - will process subscribe\n");
  522. if (can_subscribe_to_reg("location")){
  523. $var(ret)= subscribe_to_reg("location");
  524. switch ($var(ret)){
  525. case 1: #success
  526. xlog("L_DBG", "Subscribe to reg successful");
  527. break;
  528. case -1: #failure
  529. xlog("L_ERR", "Subscribe to reg failure - sending 500 Error now\n");
  530. t_reply("500","Server Error saving subscription");
  531. break;
  532. case -2: #error
  533. xlog("L_ERR", "Subscribe to reg error sending notify - 200 OK so subscription already sent\n");
  534. break;
  535. default:
  536. xlog("L_ERR", "Unknown return code from subscribe to reg event alue is [$var(ret)]\n");
  537. break;
  538. }
  539. }else{
  540. t_reply("403","Forbidden to SUBSCRIBE");
  541. exit;
  542. }
  543. break;
  544. case -1: #failure
  545. xlog("L_ERR", "SAR failure - Sending 403 Forbidden\n");
  546. t_reply("403","Forbidden to SUBSCRIBE");
  547. break;
  548. case -2: #error
  549. xlog("L_ERR", "SAR error - Sending 403 Forbidden\n");
  550. t_reply("403","Forbidden to SUBSCRIBE");
  551. break;
  552. default:
  553. xlog("L_ERR", "Unknown return code from SAR, value is [$avp(s:saa_return_code)] - sending 403 Forbidden\n");
  554. t_reply("403","Forbidden to SUBSCRIBE");
  555. break;
  556. }
  557. exit;
  558. }
  559. ######################################################################
  560. # XMLRPC routing
  561. ######################################################################
  562. #!ifdef WITH_XMLRPC
  563. route[XMLRPC] {
  564. if ((method=="POST" || method=="GET")
  565. #!ifdef XMLRPC_WHITELIST_1
  566. && ((src_ip == XMLRPC_WHITELIST_1)
  567. #!ifdef XMLRPC_WHITELIST_2
  568. || (src_ip == XMLRPC_WHITELIST_2)
  569. #!endif
  570. #!ifdef XMLRPC_WHITELIST_3
  571. || (src_ip == XMLRPC_WHITELIST_3)
  572. #!endif
  573. )
  574. #!endif
  575. ) {
  576. # close connection only for xmlrpclib user agents (there is a bug in
  577. # xmlrpclib: it waits for EOF before interpreting the response).
  578. if ($hdr(User-Agent) =~ "xmlrpclib")
  579. set_reply_close();
  580. set_reply_no_connect();
  581. dispatch_rpc();
  582. exit;
  583. }
  584. send_reply("403", "Forbidden");
  585. exit;
  586. }
  587. #!endif
  588. ######################################################################
  589. # Route for handling Registrations:
  590. ######################################################################
  591. route[REGISTER] {
  592. xlog("ALGORITHM IS [$aa] and User-Agent is [$ua]\n");
  593. $var(alg) = $aa;
  594. if ($aa == $null) {
  595. $var(alg) = "MD5"; #force to MD5 for zoiper.... non-ims
  596. }
  597. #!ifdef WITH_AUTH
  598. if (!ims_www_authenticate("$td")) {
  599. #!else
  600. if (($var(alg) == "MD5") && (!ims_www_authenticate("$td"))) {
  601. #!endif
  602. if ($? == -2) {
  603. send_reply("403", "Authentication Failed");
  604. exit;
  605. } else if ($? == -3) {
  606. send_reply("400", "Bad Request");
  607. exit;
  608. } else if ($? == -9) {
  609. xlog("L_DBG", "Authentication re-sync requested\n");
  610. ims_www_resync_auth("REG_RESYNC_REPLY", "$td");
  611. exit;
  612. } else {
  613. #user has not been authenticated. Lets send a challenge via 401 Unauthorized
  614. xlog("L_DBG","About to challenge! auth_ims\n");
  615. ims_www_challenge("REG_MAR_REPLY", "$td", "$var(alg)");
  616. exit;
  617. }
  618. } else {
  619. xlog("L_DBG", "Auth succeeded\n");
  620. # We need to check if this user is registered or not
  621. if (!impu_registered("location")) {
  622. xlog("L_ERR", "Not REGISTERED\n");
  623. save("PRE_REG_SAR_REPLY","location");
  624. exit;
  625. } else {
  626. isc_match_filter_reg("1","location");
  627. save("REG_SAR_REPLY","location");
  628. exit;
  629. }
  630. }
  631. }
  632. route[REG_MAR_REPLY]
  633. {
  634. #this is async so to know status we have to check the reply avp
  635. xlog("L_DBG","maa_return code is $avp(s:maa_return_code)\n");
  636. switch ($avp(s:maa_return_code)){
  637. case 1: #success
  638. xlog("L_DBG", "MAR success - 401/407 response sent from module\n");
  639. break;
  640. case -1: #failure
  641. xlog("L_ERR", "MAR failure - error response sent from module\n");
  642. break;
  643. case -2: #error
  644. xlog("L_ERR", "MAR error - sending error response now\n");
  645. send_reply("500", "MAR failed");
  646. break;
  647. default:
  648. xlog("L_ERR", "Unknown return code from MAR, value is [$avp(s:maa_return_code)]\n");
  649. send_reply("500", "Unknown response code from MAR");
  650. break;
  651. }
  652. exit;
  653. }
  654. route[PRE_REG_SAR_REPLY]
  655. {
  656. xlog("L_DBG","saa_return code is $avp(s:saa_return_code)\n");
  657. #this is async so to know status we have to check the reply avp
  658. xlog("L_DBG","saa_return code (for scscf_save on register) is $avp(s:saa_return_code)\n");
  659. switch ($avp(s:saa_return_code)){
  660. case 1: #success
  661. xlog("L_DBG", "SAR success - 200 response sent from module\n");
  662. isc_match_filter_reg("0","location");
  663. exit;
  664. case -1: #failure
  665. xlog("L_ERR", "SAR failure - error response sent from module\n");
  666. break;
  667. case -2: #error
  668. xlog("L_ERR", "SAR error - error response sent from module\n");
  669. break;
  670. default:
  671. xlog("L_ERR", "Unknown return code from SAR, value is [$avp(s:saa_return_code)]\n");
  672. break;
  673. }
  674. exit;
  675. }
  676. route[REG_SAR_REPLY]
  677. {
  678. xlog("L_DBG","saa_return code is $avp(s:saa_return_code)\n");
  679. #this is async so to know status we have to check the reply avp
  680. xlog("L_DBG","saa_return code (for scscf_save on register) is $avp(s:saa_return_code)\n");
  681. switch ($avp(s:saa_return_code)){
  682. case 1: #success
  683. xlog("L_DBG", "SAR success - 200 response sent from module\n");
  684. exit;
  685. case -1: #failure
  686. xlog("L_ERR", "SAR failure - error response sent from module\n");
  687. break;
  688. case -2: #error
  689. xlog("L_ERR", "SAR error - error response sent from module\n");
  690. break;
  691. default:
  692. xlog("L_ERR", "Unknown return code from SAR, value is [$avp(s:saa_return_code)]\n");
  693. break;
  694. }
  695. exit;
  696. }
  697. route[REG_RESYNC_REPLY]
  698. {
  699. ims_www_challenge("REG_MAR_REPLY", "$td");
  700. }
  701. ######################################################################
  702. # Apply privacy, if requested
  703. ######################################################################
  704. route[apply_privacy]
  705. {
  706. if (is_present_hf("Privacy") && ($hdr(Privacy)=="id")) {
  707. remove_hf("P-Asserted-Identity");
  708. }
  709. }
  710. ######################################################################
  711. # Originating, Intial Requests
  712. ######################################################################
  713. route[orig]
  714. {
  715. xlog("L_DBG","Enter orig route\n");
  716. set_dlg_profile("orig");
  717. # we MAYBE need something like this to check if a user is barred
  718. # if (S_originating_barred()){
  719. # sl_send_reply("403","Forbidden - Originating Public Identity barred");
  720. # exit;
  721. # }
  722. if (is_method("INVITE|SUBSCRIBE")) {
  723. $avp(RR_CUSTOM_USER_AVP)="mo";
  724. record_route();
  725. }
  726. # Start new transaction:
  727. t_newtran();
  728. # check if dialog saved as fwded to AS
  729. if (isc_match_filter("orig", "location")) {
  730. t_on_failure("isc_orig_failure");
  731. xlog("Orig - msg was fwded to AS\n");
  732. exit;
  733. }
  734. if (!isc_from_as("orig")) {
  735. remove_hf("P-Asserted-Identity");
  736. append_hf("P-Asserted-Identity: <sip:$fU@$fd>\r\n");
  737. }
  738. #!ifdef WITH_RO
  739. # before we allow call - lets check credit
  740. if (is_method("INVITE")) {
  741. xlog("L_DBG","Sending initial CCR Request for call\n");
  742. $var(cc_ret) = Ro_CCR("CHARGING_CCR_ORIG_REPLY", "orig", 30, "0", "0");
  743. if ($var(cc_ret) < 0) {
  744. xlog("L_ERR","CCR Request failure\n");
  745. sl_send_reply("402","Payment required");
  746. exit;
  747. }
  748. xlog("L_DBG","CCR Request success\n");
  749. exit;
  750. }
  751. #!endif
  752. route(FINAL_ORIG);
  753. }
  754. route[FINAL_ORIG]
  755. {
  756. # Check for PSTN destinations:
  757. if (is_method("INVITE")) {
  758. route(PSTN_handling);
  759. }
  760. t_on_reply("orig_reply");
  761. t_relay();
  762. }
  763. route[CHARGING_CCR_ORIG_REPLY]
  764. {
  765. xlog("L_DBG","cca_return code is $avp(s:cca_return_code)\n");
  766. switch ($avp(s:cca_return_code)){
  767. case 1: #success
  768. xlog("L_DBG", "CCR success - will route message\n");
  769. route(FINAL_ORIG);
  770. break;
  771. case -1: #failure
  772. xlog("L_ERR", "CCR failure - error response sent from module\n");
  773. switch ($avp(s:cca_result_code)){
  774. case 5030:
  775. send_reply("403", "Charging User not found");
  776. break;
  777. case 5031:
  778. send_reply("403", "Rating failed");
  779. break;
  780. case 4010:
  781. send_reply("402", "Payment required - Unsufficient funds");
  782. break;
  783. case 5006:
  784. send_reply("486", "Line limit exceeded");
  785. break;
  786. default:
  787. send_reply("402","Payment required ($avp(s:cca_result_code))");
  788. break;
  789. }
  790. case -2: #error
  791. xlog("L_ERR", "CCR error - error response sent from module\n");
  792. send_reply("500", "Charging Error");
  793. break;
  794. default:
  795. xlog("L_ERR", "Unknown return code from CCR: [$avp(s:cca_return_code)] \n");
  796. send_reply("500", "Charging Error");
  797. break;
  798. }
  799. exit;
  800. }
  801. ######################################################################
  802. # Replies to the Initial Requests
  803. ######################################################################
  804. onreply_route[orig_reply]
  805. {
  806. xlog("L_DBG","Orig reply\n");
  807. route(apply_privacy);
  808. break;
  809. }
  810. ######################################################################
  811. # Originating, subsequent requests
  812. ######################################################################
  813. route[orig_subsequent]
  814. {
  815. xlog("L_DBG","Orig_Subsequent\n");
  816. if (!is_method("ACK")) {
  817. t_on_reply("orig_subsequent_reply");
  818. }
  819. t_relay();
  820. }
  821. ######################################################################
  822. # Replies for originating, subsequent requests
  823. ######################################################################
  824. onreply_route[orig_subsequent_reply]
  825. {
  826. xlog("L_DBG","Orig_Subsequent_reply\n");
  827. route(apply_privacy);
  828. break;
  829. }
  830. ######################################################################
  831. # Failure-Route for Requests to an AS
  832. ######################################################################
  833. failure_route[isc_orig_failure]
  834. {
  835. xlog("L_DBG","ISC_Orig_failure\n");
  836. if (t_check_status("(408)|(5..)")){
  837. t_on_failure("isc_orig_failure");
  838. if (isc_match_filter("orig","location")){
  839. xlog("L_DBG","ISC_Orig_failure - msg was fwded to AS\n");
  840. exit;
  841. }
  842. if (isc_from_as("origfail")) {
  843. remove_hf("P-Asserted-Identity");
  844. append_hf("P-Asserted-Identity: <sip:$fU@$fd>\r\n");
  845. }
  846. t_on_reply("orig_reply");
  847. t_relay();
  848. }
  849. }
  850. ######################################################################
  851. # Terminating requests
  852. ######################################################################
  853. route[term]
  854. {
  855. xlog("L_DBG","Term\n");
  856. set_dlg_profile("term");
  857. #we need something like this to check if a user is barred
  858. # if (S_terminating_barred()){
  859. # sl_send_reply("404","Not Found - Terminating user barred");
  860. # exit;
  861. # }
  862. if (is_method("INVITE|SUBSCRIBE")) {
  863. $avp(RR_CUSTOM_USER_AVP)="mt";
  864. $avp(i:20)="mt";
  865. record_route();
  866. }
  867. # check if dialog saved as fwded to AS
  868. if (isc_match_filter("term","location")){
  869. t_on_failure("isc_term_failure");
  870. xlog("L_DBG","Term - msg was fwded to AS\n");
  871. exit;
  872. }
  873. #!ifdef WITH_RO_TERM
  874. # before we allow call - lets check credit
  875. if (is_method("INVITE")) {
  876. xlog("L_DBG","Sending initial CCR Request for call\n");
  877. $var(cc_ret) = Ro_CCR("CHARGING_CCR_TERM_REPLY", "term", 30, "0", "0");
  878. if ($var(cc_ret) < 0) {
  879. xlog("L_ERR","CCR Request failure\n");
  880. sl_send_reply("402","Payment required");
  881. exit;
  882. }
  883. xlog("L_DBG","CCR Request success\n");
  884. exit;
  885. }
  886. #!endif
  887. route(FINAL_TERM);
  888. }
  889. route[FINAL_TERM] {
  890. if (lookup("location")) {
  891. if (uri==myself) {
  892. if (!t_newtran()) {
  893. sl_reply_error();
  894. exit;
  895. }
  896. t_reply("404","Not Found - destination user not found on this S-CSCF");
  897. exit;
  898. }
  899. } else {
  900. # User not registered? Reply with 404.
  901. if (!t_newtran()) {
  902. sl_reply_error();
  903. exit;
  904. }
  905. t_reply("404","Not Found - destination user not found on this S-CSCF");
  906. exit;
  907. }
  908. route(apply_privacy);
  909. t_relay();
  910. }
  911. route[CHARGING_CCR_TERM_REPLY]
  912. {
  913. xlog("L_DBG","cca_return code is $avp(s:cca_return_code)\n");
  914. switch ($avp(s:cca_return_code)){
  915. case 1: #success
  916. xlog("L_DBG", "CCR success - will route message\n");
  917. route(FINAL_TERM);
  918. break;
  919. case -1: #failure
  920. xlog("L_ERR", "CCR failure - error response sent from module\n");
  921. switch ($avp(s:cca_result_code)){
  922. case 5030:
  923. send_reply("403", "Charging User not found");
  924. break;
  925. case 5031:
  926. send_reply("403", "Rating failed");
  927. break;
  928. case 4010:
  929. send_reply("402", "Payment required - Unsufficient funds");
  930. break;
  931. case 5006:
  932. send_reply("486", "Line limit exceeded");
  933. break;
  934. default:
  935. send_reply("402","Payment required ($avp(s:cca_result_code))");
  936. break;
  937. }
  938. case -2: #error
  939. xlog("L_ERR", "CCR error - error response sent from module\n");
  940. send_reply("500", "Charging Error");
  941. break;
  942. default:
  943. xlog("L_ERR", "Unknown return code from CCR: [$avp(s:cca_return_code)] \n");
  944. send_reply("500", "Charging Error");
  945. break;
  946. }
  947. exit;
  948. }
  949. ######################################################################
  950. # Failure Route for Terminating requests
  951. ######################################################################
  952. failure_route[isc_term_failure]
  953. {
  954. xlog("L_DBG","ISC_term_failure\n");
  955. if (t_check_status("(408)|(5..)")){
  956. t_on_failure("isc_term_failure");
  957. if (isc_match_filter("term","location")){
  958. xlog("L_DBG","Term - msg was fwded to AS\n");
  959. exit;
  960. }
  961. if (lookup("location")) {
  962. if (uri==myself) {
  963. t_reply("404","Not Found - destination user not found on this S-CSCF");
  964. exit;
  965. }
  966. } else {
  967. t_reply("404","Not Found - destination user not found on this S-CSCF");
  968. exit;
  969. }
  970. t_relay();
  971. }
  972. }
  973. ######################################################################
  974. # Terminating, subsequent requests
  975. ######################################################################
  976. route[term_subsequent]
  977. {
  978. xlog("L_DBG","term_subsequent\n");
  979. route(apply_privacy);
  980. t_relay();
  981. }
  982. ######################################################################
  983. # Check for PSTN destinations:
  984. ######################################################################
  985. route[PSTN_handling]
  986. {
  987. # First, we translate "tel:"-URI's to SIP-URI's:
  988. # $ru: tel:+(34)-999-888-777
  989. # $fu: sip:[email protected]
  990. # becomes $ru: sip:[email protected];user=phone
  991. if (!tel2sip("$ru", "$fd", "$ru"))
  992. xlog("L_WARN","Failed to convert $ru to a sip:-URI - M=$rm R=$ru F=$fu T=$tu IP=$si:$sp ID=$ci\n\n");
  993. if ($rU =~ "\+[0-9]+") {
  994. # Now let's check, if the number can be found in ENUM:
  995. if(!enum_query()) {
  996. # ENUM failed, send it to the PSTN-Gateway:
  997. route(PSTN);
  998. break;
  999. }
  1000. }
  1001. }
  1002. ######################################################################
  1003. # Send calls to the PSTN-Gateways:
  1004. ######################################################################
  1005. route[PSTN]
  1006. {
  1007. $var(has_trf) = 0;
  1008. if (is_present_hf("Feature-Caps")) {
  1009. xlog("Feature-Caps: $hdr(Feature-Caps) => $(hdr(Feature-Caps){param.value,+g.3gpp.trf}{nameaddr.uri}{uri.host})\n");
  1010. if ($(hdr(Feature-Caps){param.value,+g.3gpp.trf}) != $null) {
  1011. $rd = $(hdr(Feature-Caps){param.value,+g.3gpp.trf}{nameaddr.uri}{uri.host});
  1012. if (!strempty($(ou{uri.params}))) {
  1013. $ru = $ru+";"+$(ou{uri.params});
  1014. }
  1015. t_on_failure("TRF_failure");
  1016. if (t_relay_to("0x02")) {
  1017. $var(has_trf) = 1;
  1018. } else {
  1019. xlog("Relay to $du failed.\n");
  1020. $du = $null;
  1021. }
  1022. }
  1023. }
  1024. if ($var(has_trf) == 0) {
  1025. if (!ds_select_domain("1", "4")) {
  1026. xlog("L_WARN","No PSTN-Gateways available - M=$rm R=$ru F=$fu T=$tu IP=$si:$sp ID=$ci\n\n");
  1027. send_reply("503", "Service not available");
  1028. exit;
  1029. }
  1030. if (!strempty($(ou{uri.params}))) {
  1031. $ru = $ru+";"+$(ou{uri.params});
  1032. }
  1033. t_relay();
  1034. }
  1035. # Relay the request:
  1036. t_on_failure("PSTN_failure");
  1037. exit;
  1038. }
  1039. ######################################################################
  1040. # manage failure routing cases, perform failover
  1041. ######################################################################
  1042. failure_route[TRF_failure] {
  1043. xlog("TRF_failure\n");
  1044. # Choose another gateway, in case we
  1045. # - get a local generated "408"
  1046. # - receive a 5xx or 6xx reply from the proxy.
  1047. if (t_branch_timeout() || t_check_status("[5-6]..")) {
  1048. $du = $null;
  1049. if (!ds_select_domain("1", "4")) {
  1050. xlog("L_WARN","No PSTN-Gateways available - M=$rm R=$ru F=$fu T=$tu IP=$si:$sp ID=$ci\n\n");
  1051. send_reply("503", "Service not available");
  1052. exit;
  1053. }
  1054. if (!strempty($(ou{uri.params}))) {
  1055. $ru = $ru+";"+$(ou{uri.params});
  1056. }
  1057. # Relay the request:
  1058. t_on_failure("PSTN_failure");
  1059. t_relay();
  1060. exit;
  1061. }
  1062. }
  1063. ######################################################################
  1064. # manage failure routing cases, perform failover
  1065. ######################################################################
  1066. failure_route[PSTN_failure] {
  1067. # Choose another gateway, in case we
  1068. # - get a local generated "408"
  1069. # - receive a 5xx or 6xx reply from the proxy.
  1070. if (t_branch_timeout() || t_check_status("[5-6]..")) {
  1071. if (ds_next_domain()) {
  1072. if (!strempty($(ou{uri.params}))) {
  1073. $ru = $ru+";"+$(ou{uri.params});
  1074. }
  1075. # Do Failover in case problems:
  1076. t_on_failure("PSTN_failure");
  1077. t_relay();
  1078. } else {
  1079. # Add a header, to indicate the phone should try again in 30 seconds.
  1080. append_hf("Retry-After: 30\r\n");
  1081. send_reply("503", "Service not available");
  1082. }
  1083. exit;
  1084. }
  1085. }