Advertisement
load-net

kamailio.cfg

Jul 9th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.40 KB | None | 0 0
  1. #https://lmtools.com/sites/default/files/kamailio/pcscf/kamailio.cfg
  2.  
  3.  
  4. #!KAMAILIO
  5. #
  6. # This config file implements the basic P-CSCF functionality
  7. # - web: http://www.kamailio.org
  8. # - git: http://sip-router.org
  9. #
  10. # Refer to the Core CookBook at http://www.kamailio.org/dokuwiki/doku.php
  11. # for an explanation of possible statements, functions and parameters.
  12. #
  13. # Direct your questions about this file to: <[email protected]>.
  14. #
  15. # For more information about the various parameters, functions and statements
  16. # try http://sip-router.org/wiki/ .
  17. #
  18.  
  19. include_file "pcscf.cfg"
  20.  
  21. ####### Defined Values #########
  22. # *** Value defines - IDs used later in config
  23.  
  24. # - flags
  25. # FLT_ - per transaction (message) flags
  26. # FLB_ - per branch flags
  27.  
  28. #!define FLT_CAPTURE 1
  29. #!define FLT_DIALOG 2
  30. #!define FLT_NAT 3
  31. #!define FLT_IPV4 4
  32. #!define FLT_MO 5
  33.  
  34. #!define DLG_TIMEOUT_AVP "i:1"
  35. #!define RR_CUSTOM_USER_AVP "i:2"
  36. #!define NATHELPER_RECEIVED_AVP "i:3"
  37. #!define DISPATCHER_DST_AVP "i:4"
  38. #!define DISPATCHER_GRP_AVP "i:5"
  39. #!define DISPATCHER_CNT_AVP "i:6"
  40.  
  41. system.shutdownmode = 0 desc "System shutdown mode"
  42.  
  43. ####### Global Parameters #########
  44. #!ifdef WITH_DEBUG
  45. debug=2
  46. log_stderror=no
  47. sip_warning=yes
  48. #!else
  49. debug=2
  50. log_stderror=no
  51. sip_warning=no
  52. #!endif
  53.  
  54. children=4
  55.  
  56. # Locks all ser pages into memory making it unswappable (in general one
  57. # doesn't want his sip proxy swapped out )
  58. mlock_pages=yes
  59. # Tries to pre-fault all the shared memory, before starting. When "on", start
  60. # time will increase, but combined with mlock_pages will guarantee ser will get
  61. # all its memory from the beginning (no more kswapd slow downs)
  62. shm_force_alloc=yes
  63.  
  64.  
  65. # Do SRV-Loadbalancing:
  66. dns_srv_lb=yes
  67. # Always prefer IPv6:
  68. dns_try_ipv6=yes
  69. # DNS-Based failover
  70. use_dns_failover=yes
  71. # Query NAPTR-Records as well:
  72. dns_try_naptr=no
  73.  
  74. user_agent_header="User-Agent: Kamailio P-CSCF"
  75. server_header="Server: Kamailio P-CSCF"
  76.  
  77. /* comment the next line to enable the auto discovery of local aliases
  78. based on reverse DNS on IPs (default on) */
  79. auto_aliases=no
  80.  
  81. #!ifdef WITH_WEBSOCKET
  82. #!ifndef WITH_TCP
  83. #!define WITH_TCP
  84. #!endif
  85. #!ifndef TCP_PROCESSES
  86. # Number of TCP Processes
  87. #!define TCP_PROCESSES 10
  88. #!endif
  89. #!endif
  90.  
  91. #!ifdef WITH_TLS
  92. # Check, if TCP is enabled:
  93. #!ifndef WITH_TCP
  94. #!define WITH_TCP
  95. #!endif
  96. enable_tls=yes
  97. #!endif
  98.  
  99. #!ifdef WITH_XMLRPC
  100. #!ifndef WITH_TCP
  101. #!define WITH_TCP
  102. #!endif
  103. #!ifndef TCP_PROCESSES
  104. # Number of TCP Processes
  105. #!define TCP_PROCESSES 3
  106. #!endif
  107. #!endif
  108.  
  109. # Check, if NAT is enabled (in case you want to Force all calls through the RTPProxy)
  110. #!ifdef FORCE_RTPRELAY
  111. #!ifndef WITH_NAT
  112. #!define WITH_NAT
  113. #!endif
  114. #!endif
  115.  
  116. # Check, if NAT is enabled (in case you want to Force all calls through the RTPProxy)
  117. #!ifdef WITH_RTPIPV4
  118. #!ifndef WITH_NAT
  119. #!define WITH_NAT
  120. #!endif
  121. #!endif
  122.  
  123. #!define DISPATCHER_LIST_IMS "1"
  124. #!ifdef WITH_SBC
  125. #!define DISPATCHER_LIST_SBC "2"
  126. #!endif
  127.  
  128. #!ifdef WITH_TCP
  129. # life time of TCP connection when there is no traffic
  130. # - a bit higher than registration expires to cope with UA behind NAT
  131. tcp_connection_lifetime=3615
  132. # If a message received over a tcp connection has "alias" in its via a new tcp
  133. # alias port will be created for the connection the message came from (the
  134. # alias port will be set to the via one).
  135. #
  136. # Note: For NAT traversal of TCP clients it is better to not use
  137. # tcp_accept_aliases but just use nathelper module and
  138. # fix_nated_[contact|register] functions.
  139. tcp_accept_aliases=no
  140. # Enable SIP outbound TCP keep-alive using PING-PONG (CRLFCRLF - CRLF).
  141. tcp_crlf_ping=yes
  142.  
  143. tcp_accept_no_cl=yes
  144. tcp_rd_buf_size=16384
  145.  
  146. #!ifdef TCP_PROCESSES
  147. tcp_children=TCP_PROCESSES
  148. #!endif
  149. #!else
  150. disable_tcp=yes
  151. #!endif
  152.  
  153. check_via=no # (cmd. line: -v)
  154. dns=no # (cmd. line: -r)
  155. rev_dns=no # (cmd. line: -R)
  156.  
  157. # ------------------ module loading ----------------------------------
  158. #mpath="/usr/lib64/kamailio/modules_k/:/usr/lib64/kamailio/modules/:/usr/lib/kamailio/modules_k/:/usr/lib/kamailio/modules/"
  159. mpath="/usr/local/ims-kamailio/kamailio-4.2.0/output/lib64/kamailio/modules/"
  160. # (we try both the lib64 and the lib directory)
  161.  
  162. loadmodule "tm"
  163. loadmodule "tmx"
  164.  
  165. loadmodule "pv"
  166. loadmodule "sl"
  167. loadmodule "rr"
  168. loadmodule "dialog_ng"
  169. loadmodule "ims_usrloc_pcscf"
  170. loadmodule "textops"
  171. loadmodule "textopsx"
  172. loadmodule "maxfwd"
  173. loadmodule "xlog"
  174. loadmodule "ims_registrar_pcscf"
  175. loadmodule "sanity"
  176. loadmodule "siputils"
  177. loadmodule "kex"
  178. loadmodule "sdpops.so"
  179.  
  180. #!ifdef DB_URL
  181. loadmodule "db_mysql"
  182. #!ifdef DB_URL2
  183. loadmodule "db_cluster"
  184. #!endif
  185. #!endif
  186.  
  187. #!ifdef WITH_REGINFO
  188. loadmodule "pua.so"
  189. #!endif
  190.  
  191. # Control interfaces:
  192. loadmodule "ctl"
  193. loadmodule "cfg_rpc"
  194. loadmodule "mi_rpc"
  195. loadmodule "mi_fifo"
  196. #!ifdef WITH_XMLRPC
  197. loadmodule "xmlrpc"
  198. #!endif
  199.  
  200. #!ifdef WITH_RX
  201. loadmodule "cdp"
  202. loadmodule "cdp_avp"
  203. loadmodule "ims_qos"
  204. #!endif
  205.  
  206. #!ifdef CAPTURE_NODE
  207. loadmodule "siptrace"
  208. #!endif
  209.  
  210. #!ifdef WITH_DEBUG
  211. loadmodule "debugger"
  212. #!endif
  213.  
  214. #!ifdef WITH_TLS
  215. loadmodule "tls"
  216. #!endif
  217.  
  218. loadmodule "htable"
  219.  
  220. #!ifdef WITH_ANTIFLOOD
  221. loadmodule "pike"
  222. # loadmodule "dispatcher"
  223. #!endif
  224.  
  225. #!ifdef WITH_NAT
  226. loadmodule "path"
  227. loadmodule "rtpengine"
  228. loadmodule "nathelper"
  229. #!endif
  230.  
  231. #!ifdef WITH_WEBSOCKET
  232. loadmodule "xhttp.so"
  233. loadmodule "websocket.so"
  234. #!endif
  235.  
  236. #!ifdef WITH_NATPING
  237. loadmodule "nat_traversal"
  238. #!endif
  239.  
  240. # ----------------- setting module-specific parameters ---------------
  241. #!ifdef DB_URL2
  242. # ----- db_cluster params -----
  243. modparam("db_cluster", "connection", DB_URL)
  244. modparam("db_cluster", "connection", DB_URL2)
  245. modparam("db_cluster", "cluster", "cluster1=>con1=2s2s;con2=1s1s")
  246. #!endif
  247.  
  248. # ----- mi_fifo params -----
  249. modparam("mi_fifo", "fifo_name", "/tmp/pcscf_kamailio_fifo")
  250. modparam("mi_fifo", "fifo_mode", 0666)
  251. modparam("mi_fifo", "fifo_user", "kamailio")
  252. modparam("mi_fifo", "fifo_group", "kamailio")
  253.  
  254. # ----- tm params -----
  255. # auto-discard branches from previous serial forking leg
  256. modparam("tm", "failure_reply_mode", 3)
  257. # default retransmission timeout: 10sec
  258. modparam("tm", "fr_timer", 10000)
  259. # default invite retransmission timeout after 1xx: 120sec
  260. modparam("tm", "fr_inv_timer", 120000)
  261. # Don't reply automatically with "100 Trying"
  262. modparam("tm", "auto_inv_100", 0)
  263.  
  264. # ----- rr params -----
  265. # add value to ;lr param to cope with most of the UAs
  266. modparam("rr", "enable_full_lr", 1)
  267. # append from tag to the RR
  268. modparam("rr", "append_fromtag", 1)
  269. # add a Username to RR-Header
  270. modparam("rr", "add_username", 1)
  271. # Take User from a custom AVP
  272. modparam("rr", "custom_user_avp", "$avp(RR_CUSTOM_USER_AVP)")
  273.  
  274. # -- usrloc params --
  275. #!ifdef DB_URL
  276. #!ifdef DB_URL2
  277. modparam("ims_usrloc_pcscf", "db_url", "cluster://cluster1")
  278. #!else
  279. modparam("ims_usrloc_pcscf", "db_url", DB_URL)
  280. #!endif
  281. modparam("ims_usrloc_pcscf", "db_mode", 1)
  282. #!endif
  283. modparam("ims_usrloc_pcscf", "hashing_type", 2)
  284. modparam("ims_usrloc_pcscf", "lookup_check_received", 1)
  285. modparam("ims_usrloc_pcscf", "enable_debug_file", 0)
  286. modparam("ims_registrar_pcscf", "is_registered_fallback2ip", 2)
  287.  
  288.  
  289. #!ifdef WITH_REGINFO
  290. modparam("ims_registrar_pcscf", "subscribe_to_reginfo", 1)
  291. modparam("ims_registrar_pcscf", "publish_reginfo", 1)
  292. #!else
  293. modparam("ims_registrar_pcscf", "subscribe_to_reginfo", 0)
  294. modparam("ims_registrar_pcscf", "publish_reginfo", 0)
  295. #!endif
  296.  
  297. # -- pua params --
  298. #!ifdef WITH_REGINFO
  299. #!ifdef DB_URL
  300. #!ifdef DB_URL2
  301. modparam("pua", "db_url", "cluster://cluster1")
  302. #!else
  303. modparam("pua", "db_url", DB_URL)
  304. #!endif
  305. #!endif
  306. #!endif
  307.  
  308. #!ifdef WITH_RX
  309. # -- CDP params --
  310. modparam("cdp","config_file","/etc/kamailio/pcscf/pcscf.xml")
  311. # -- diameter_rx params --
  312. modparam("ims_qos", "rx_dest_realm", "NETWORKNAME")
  313. #!endif
  314.  
  315. # -- dialog_ng params --
  316. modparam("dialog_ng", "dlg_flag", FLT_DIALOG)
  317. modparam("dialog_ng", "timeout_avp", "$avp(DLG_TIMEOUT_AVP)")
  318. modparam("dialog_ng", "detect_spirals", 0)
  319. modparam("dialog_ng", "profiles_no_value", "orig ; term")
  320.  
  321. #!ifdef WITH_TLS
  322. # ----- tls params -----
  323. modparam("tls", "config", "/etc/kamailio/pcscf/tls.cfg")
  324. #!endif
  325.  
  326. #!ifdef WITH_ANTIFLOOD
  327. # ----- pike params -----
  328. modparam("pike", "sampling_time_unit", 2)
  329. modparam("pike", "reqs_density_per_unit", 45)
  330. modparam("pike", "remove_latency", 4)
  331.  
  332. # ----- htable params -----
  333. # ip ban htable with autoexpire after 5 minutes
  334. modparam("htable", "htable", "ipban=>size=8;autoexpire=3600;")
  335.  
  336. # ----------------- Settings for Dispatcher ---------------
  337. # modparam("dispatcher", "list_file", "/etc/kamailio/pcscf/dispatcher.list")
  338.  
  339. # Dispatcher: Enable Failover-Support
  340. # modparam("dispatcher", "flags", 2)
  341. # Dispatcher: Overwrite Destination address, if required.
  342. # modparam("dispatcher", "force_dst", 1)
  343. # AVP's required for Fail-Over-Support:
  344. # modparam("dispatcher", "dst_avp", "$avp(DISPATCHER_DST_AVP)")
  345. # modparam("dispatcher", "grp_avp", "$avp(DISPATCHER_GRP_AVP)")
  346. # modparam("dispatcher", "cnt_avp", "$avp(DISPATCHER_CNT_AVP)")
  347. # Try to recover disabled destinations every 15 seconds.
  348. # modparam("dispatcher", "ds_ping_interval", 15)
  349. # Actively query the gateways:
  350. # modparam("dispatcher", "ds_probing_mode", 1)
  351.  
  352. #!endif
  353.  
  354. # ----- htable params -----
  355. #!ifdef WITH_IMS_HDR_CACHE
  356. modparam("htable", "htable", "serviceroutes=>size=16;autoexpire=14400;")
  357. modparam("htable", "htable", "associateduris=>size=16;autoexpire=14400;")
  358. #!endif
  359.  
  360. #!ifdef WITH_XMLRPC
  361. # ----- xmlrpc params -----
  362. modparam("xmlrpc", "route", "XMLRPC");
  363. modparam("xmlrpc", "url_match", "^/RPC")
  364. #!endif
  365.  
  366. #!ifdef WITH_DEBUG
  367. # ----- debugger params -----
  368. modparam("debugger", "cfgtrace", 1)
  369. #!endif
  370.  
  371. #!ifdef CAPTURE_NODE
  372. # Destination, where to send the traffic
  373. modparam("siptrace", "duplicate_uri", CAPTURE_NODE)
  374. # Trace all traffic
  375. modparam("siptrace", "trace_on", 1)
  376. modparam("siptrace", "trace_to_database", 0)
  377. modparam("siptrace", "trace_flag", FLT_CAPTURE)
  378. modparam("siptrace", "hep_mode_on", 1)
  379. #!endif
  380.  
  381. #!ifdef WITH_NAT
  382. # ----- rtpproxy-ng params -----
  383. modparam("rtpengine", "rtpengine_sock", RTPPROXY_ADDRESS)
  384. modparam("path", "use_received", 1)
  385. #!endif
  386.  
  387. #!ifdef WITH_NATPING
  388. # ----- nat_traversal params -----
  389. # If another keepalive is wanted, this is the place
  390. modparam("nat_traversal", "keepalive_interval", 20)
  391. # If another method than NOTIFY is wanted:
  392. modparam("nat_traversal", "keepalive_method", "OPTIONS")
  393. # From?
  394. modparam("nat_traversal", "keepalive_from", "sip:keepalive@HOSTNAME")
  395. # Where we store information about keep-alives:
  396. modparam("nat_traversal", "keepalive_state_file", "/var/run/kamailio/keepalive_state")
  397. #!endif
  398.  
  399. ####### Routing Logic ########
  400. # Main SIP request routing logic
  401. # - processing of any incoming SIP request starts with this route
  402.  
  403. route {
  404. ##!ifdef WITH_DEBUG
  405. xlog("$rm ($fu ($si:$sp) to $tu, $ci)\n");
  406. ##!endif
  407.  
  408. #!ifdef WITH_WEBSOCKET
  409. if (($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT) && !(proto == WS || proto == WSS)) {
  410. xlog("L_WARN", "Websocket-request received on SIP/$Rp\n");
  411. sl_send_reply("403", "Forbidden");
  412. exit;
  413. }
  414. #!endif
  415.  
  416. # per request initial checks
  417. route(REQINIT);
  418.  
  419. # Check for NAT, if enabled.
  420. route(NAT);
  421.  
  422. #Set DLG flag to track dialogs using dialog2
  423. if (is_method("INVITE"))
  424. setflag(FLT_DIALOG);
  425.  
  426. # Check for Subsequent requests:
  427. if (has_totag()) {
  428. # sequential request withing a dialog should
  429. # take the path determined by record-routing
  430. if (loose_route()) {
  431. if ($route_uri =~ "sip:mo@.*") {
  432. setflag(FLT_MO);
  433. }
  434. if(!isdsturiset()) {
  435. handle_ruri_alias();
  436. }
  437. # RTP-Relay, if necessary
  438. route(RTPPROXY);
  439. t_relay();
  440. } else {
  441. if ( is_method("ACK") ) {
  442. if ( t_check_trans() ) {
  443. # no loose-route, but stateful ACK;
  444. # must be an ACK after a 487
  445. # or e.g. 404 from upstream server
  446. t_relay();
  447. exit;
  448. } else {
  449. # ACK without matching transaction ... ignore and discard
  450. exit;
  451. }
  452. }
  453. sl_send_reply("404","Not here");
  454. }
  455. exit;
  456.  
  457. }
  458.  
  459. ### only initial requests (no To tag)
  460. # CANCEL processing
  461. if (is_method("CANCEL")) {
  462. if (t_check_trans())
  463. t_relay();
  464. exit;
  465. }
  466.  
  467. # Check for Re-Transmissions
  468. t_check_trans();
  469.  
  470. if (is_method("REGISTER")) {
  471. route(REGISTER);
  472. exit;
  473. }
  474.  
  475. if (is_method("NOTIFY") && (uri==myself)) {
  476. route(NOTIFY);
  477. exit;
  478. }
  479.  
  480. if (is_method("UPDATE")) {
  481. send_reply("403","Forbidden - Target refresh outside dialog not allowed");
  482. break;
  483. }
  484. if (is_method("BYE|PRACK")) {
  485. send_reply("403","Forbidden - Originating subsequent requests outside dialog not allowed");
  486. break;
  487. }
  488.  
  489.  
  490. #Kamal commented
  491. #if (!ds_is_from_list())
  492. #{
  493. # Originating from Subscriber:
  494. # route(Orig_Initial);
  495. #}
  496. #else
  497. #{
  498. # Terminating to Subscriber:
  499. # route(Term_Initial);
  500. #}
  501.  
  502. #Kamal added 1 line below
  503. route(Term_Initial);
  504.  
  505.  
  506. exit;
  507. }
  508.  
  509. #!ifdef WITH_WEBSOCKET
  510. event_route[xhttp:request] {
  511. set_reply_close();
  512. set_reply_no_connect();
  513.  
  514. if ($Rp != MY_WS_PORT
  515. #!ifdef WITH_TLS
  516. && $Rp != MY_WSS_PORT
  517. #!endif
  518. ) {
  519. xlog("L_WARN", "HTTP request received on $Rp\n");
  520. xhttp_reply("403", "Forbidden", "", "");
  521. exit;
  522. }
  523.  
  524. if ($hdr(Upgrade)=~"websocket"
  525. && in_list("Upgrade", $hdr(Connection), ",")
  526. && $rm=~"GET") {
  527.  
  528. # Validate Host - make sure the client is using the correct
  529. # alias for WebSockets
  530. if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
  531. xlog("L_WARN", "Bad host $hdr(Host)\n");
  532. xhttp_reply("403", "Forbidden", "", "");
  533. exit;
  534. }
  535.  
  536. #!ifdef WEBSOCKET_WEBSERVER
  537. # Validate Origin - make sure the client is from the authorised website
  538. if ($hdr(Origin) != "http://"+WEBSOCKET_WEBSERVER
  539. #!ifdef WITH_TLS
  540. && $hdr(Origin) != "https://"+WEBSOCKET_WEBSERVER
  541. #!endif
  542. ) {
  543. xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
  544. xhttp_reply("403", "Forbidden", "", "");
  545. exit;
  546. }
  547. #!endif
  548.  
  549. # ws_handle_handshake() exits (no further configuration file
  550. # processing of the request) when complete.
  551. if (ws_handle_handshake()) {
  552. # Optional... cache some information about the
  553. # successful connection
  554. exit;
  555. }
  556. }
  557.  
  558. # xhttp_reply("200", "OK", "text/html", "<html><body>Wrong URL $hu</body></html>");
  559. xhttp_reply("404", "Not Found", "", "");
  560. }
  561.  
  562. event_route[websocket:closed] {
  563. xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
  564. }
  565. #!endif
  566.  
  567. ######################################################################
  568. # Helper routes (Basic-Checks, NAT-Handling/RTP-Control, XML-RPC)
  569. ######################################################################
  570. # Per SIP request initial checks
  571. route[REQINIT] {
  572. #!ifdef WITH_ANTIFLOOD
  573. # flood dection from same IP and traffic ban for a while
  574. # be sure you exclude checking trusted peers, such as pstn gateways
  575. # - local host excluded (e.g., loop to self)
  576.  
  577. #Modified below line
  578. #if (!has_totag() && (src_ip!=myself) && !ds_is_from_list())
  579. if (!has_totag() && (src_ip!=myself))
  580. {
  581. if($sht(ipban=>$si)!=$null)
  582. {
  583. # ip is already blocked
  584. xlog("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  585. exit;
  586. }
  587. if (!pike_check_req())
  588. {
  589. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  590. $sht(ipban=>$si) = 1;
  591. exit;
  592. }
  593. }
  594.  
  595. # Kamal - Commented below line
  596. # if ((uri == myself) && !ds_is_from_list()) {
  597. if ((uri == myself)) {
  598. xlog("L_ALERT","ALERT: Request to myself: $ru from $fu (IP:$si:$sp), Blocking\n");
  599. $sht(ipban=>$si) = 1;
  600. exit;
  601. }
  602. #!endif
  603. # Trace this message
  604. #!ifdef CAPTURE_NODE
  605. sip_trace();
  606. setflag(FLT_CAPTURE);
  607. #!endif
  608.  
  609. if (!mf_process_maxfwd_header("10")) {
  610. sl_send_reply("483","Too Many Hops");
  611. exit;
  612. }
  613.  
  614. if(!sanity_check("1511", "7")) {
  615. xlog("Malformed SIP message from $si:$sp\n");
  616. exit;
  617. }
  618.  
  619. # Check for shutdown mode:
  620. if (!has_totag() && ($sel(cfg_get.system.shutdownmode) > 0)) {
  621. send_reply("503", "Server shutting down");
  622. exit;
  623. }
  624.  
  625. # Reply to OPTIONS:
  626. if (is_method("OPTIONS") && ((uri==myself) || (uri == "sip:NETWORKNAME"))) {
  627. options_reply();
  628. exit;
  629. }
  630.  
  631. # Ignore Re-Transmits:
  632. if (t_lookup_request()) {
  633. exit;
  634. }
  635.  
  636. if (is_method("INVITE|REGISTER")) {
  637. send_reply("100", "Trying");
  638. }
  639. }
  640.  
  641. ######################################################################
  642. # XMLRPC routing
  643. ######################################################################
  644. #!ifdef WITH_XMLRPC
  645. route[XMLRPC] {
  646. if ((method=="POST" || method=="GET")
  647. #!ifdef XMLRPC_WHITELIST_1
  648. && ((src_ip == XMLRPC_WHITELIST_1)
  649. #!ifdef XMLRPC_WHITELIST_2
  650. || (src_ip == XMLRPC_WHITELIST_2)
  651. #!endif
  652. #!ifdef XMLRPC_WHITELIST_3
  653. || (src_ip == XMLRPC_WHITELIST_3)
  654. #!endif
  655. )
  656. #!endif
  657. ) {
  658. # close connection only for xmlrpclib user agents (there is a bug in
  659. # xmlrpclib: it waits for EOF before interpreting the response).
  660. if ($hdr(User-Agent) =~ "xmlrpclib")
  661. set_reply_close();
  662. set_reply_no_connect();
  663. dispatch_rpc();
  664. exit;
  665. }
  666. send_reply("403", "Forbidden");
  667. exit;
  668. }
  669. #!endif
  670.  
  671. ######################################################################
  672. # Caller NAT detection route
  673. ######################################################################
  674. route[NAT] {
  675. #!ifdef WITH_NAT
  676. force_rport();
  677. #if (nat_uac_test("19") && !ds_is_from_list()) {
  678. if (nat_uac_test("19")) {
  679. if(is_method("INVITE|SUBSCRIBE")) {
  680. set_contact_alias();
  681. }
  682. setflag(FLT_NAT);
  683. if (is_method("REGISTER")) {
  684. set_contact_alias();
  685. #!ifdef WITH_NATPING
  686. xlog("L_DBG", "Enabling keep-alive\n");
  687. nat_keepalive();
  688. #!endif
  689. }
  690. }
  691. #!ifdef FORCE_RTPRELAY
  692. #if (!ds_is_from_list()) {
  693. # setflag(FLT_NAT);
  694. #}
  695. #!endif
  696. #!endif
  697. return;
  698. }
  699.  
  700. ######################################################################
  701. # Route for RTPProxy control (Originating Requests)
  702. ######################################################################
  703. route[RTPPROXY] {
  704. #!ifdef WITH_DEBUG
  705. if (is_request())
  706. xlog("REQUEST: $rm $ru ($si:$sp, $ci)\n");
  707. else
  708. xlog("REPLY: $rs $rr ($rm, $si:$sp, $ci)\n");
  709. #!endif
  710.  
  711. #!ifdef WITH_NAT
  712. if (is_request() && !isflagset(FLT_NAT)) {
  713. if(check_route_param("nat=yes")) {
  714. setflag(FLT_NAT);
  715. }
  716. }
  717. if !(isflagset(FLT_NAT))
  718. return;
  719.  
  720. if (is_request()) {
  721. if (!has_totag()) {
  722. add_rr_param(";nat=yes");
  723. }
  724. }
  725.  
  726. #if (is_reply() && !ds_is_from_list())
  727. if (is_reply())
  728. add_contact_alias();
  729.  
  730. #!ifdef RTPPROXY_ADDRESS
  731. if(!t_is_set("onreply_route"))
  732. t_on_reply("RTPPROXY_reply");
  733.  
  734. if (is_reply() && !(status=~"[12][0-9][0-9]"))
  735. return;
  736.  
  737. ## P-RTP-Stats snippet for Kamailio/RTPProxy
  738. if (is_method("BYE") && is_reply())
  739. return;
  740.  
  741. if(!has_body("application/sdp"))
  742. return;
  743.  
  744. if (isflagset(FLT_MO)) {
  745. append_hf("X-RTP: mo\r\n");
  746. if (sdp_get_line_startswith("$avp(mline)", "m=")) {
  747. if (is_request()) {
  748. if (is_direction("downstream")) {
  749. if ($avp(mline) =~ "SAVPF") {
  750. $avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
  751. $avp(rtpproxy_answer_flags) = "trust-address replace-origin replace-session-connection ICE=force SRTP AVPF";
  752. add_rr_param(";rtp=SAVPF");
  753. } else {
  754. $avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove";
  755. $avp(rtpproxy_answer_flags) = "trust-address replace-origin replace-session-connection ICE=force";
  756. }
  757. } else {
  758. if(check_route_param("rtp=SAVPF")) {
  759. $avp(rtpproxy_offer_flags) = "trust-address replace-origin replace-session-connection ICE=remove RTP AVP";
  760. $avp(rtpproxy_answer_flags) = "trust-address replace-origin replace-session-connection ICE=force SRTP AVPF";
  761. add_rr_param(";rtp=SAVPF");
  762. } else {
  763. $avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove";
  764. $avp(rtpproxy_answer_flags) = "trust-address replace-origin replace-session-connection ICE=force";
  765. }
  766. }
  767. }
  768. ##!ifdef WITH_DEBUG
  769. #xlog("$$avp(mline) = $avp(mline)\n");
  770. #xlog("$$avp(rtpproxy_offer_flags) = $avp(rtpproxy_offer_flags)\n");
  771. #xlog("$$avp(rtpproxy_answer_flags) = $avp(rtpproxy_answer_flags)\n");
  772. ##!endif
  773. if (is_request() && is_direction("downstream")) rtpengine_manage($avp(rtpproxy_offer_flags));
  774. else rtpengine_manage($avp(rtpproxy_answer_flags));
  775. } else {
  776. # Mobile Originating (User to network)
  777. if (is_request() && is_direction("downstream")) rtpengine_manage("replace-origin replace-session-connection ICE=force");
  778. else rtpengine_manage("trust-address replace-origin replace-session-connection ICE=force");
  779. }
  780. } else {
  781. if (is_present_hf("X-RTP")) {
  782. remove_hf("X-RTP");
  783. } else {
  784. # Mobile Terminating (Network to User)
  785. if (is_request() && is_direction("downstream")) rtpengine_manage("trust-address replace-origin replace-session-connection ICE=remove");
  786. else rtpengine_manage("replace-origin replace-session-connection ICE=remove");
  787. }
  788. }
  789. #!endif
  790. #!endif
  791. return;
  792. }
  793.  
  794. # Replies for RTPPROXY
  795. ######################################################################
  796. onreply_route[RTPPROXY_reply]
  797. {
  798. # Do RTP-Relaying, if necessary:
  799. if (status=~"[12][0-9][0-9]")
  800. route(RTPPROXY);
  801. }
  802.  
  803.  
  804. ######################################################################
  805. ######################################################################
  806. ## REGISTER Handling
  807. ######################################################################
  808. ######################################################################
  809.  
  810. # Route for handling Registrations:
  811. ######################################################################
  812. route[REGISTER] {
  813. if (is_present_hf("Contact"))
  814. pcscf_save_pending("location");
  815. t_on_reply("REGISTER_reply");
  816. t_on_failure("REGISTER_failure");
  817.  
  818. #!ifdef WITH_RX
  819. xlog("L_DBG","Subscribing to signalling bearer status\n");
  820. if (Rx_AAR_Register("REG_AAR_REPLY", "location") == 0)
  821. exit;
  822. }
  823.  
  824. route[REG_AAR_REPLY] {
  825. switch ($avp(s:aar_return_code)) {
  826. case 1:
  827. xlog("L_DBG", "Diameter: AAR success on subscription to signalling\n");
  828. break;
  829. default:
  830. xlog("L_ERR", "Diameter: AAR failed on subscription to signalling\n");
  831. send_reply("403", "Can't register to QoS for signalling");
  832. exit;
  833. }
  834. #!endif
  835.  
  836. #!ifndef WITH_SBC
  837. #!ifdef WITH_WEBSOCKET
  838. if (($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT) && (proto == WS || proto == WSS)) {
  839. $var(ws_transport) = "ws="+$pr+";";
  840. } else {
  841. $var(ws_transport) = "";
  842. }
  843. #!else
  844. $var(ws_transport) = "";
  845. #!endif
  846.  
  847. #!ifdef WITH_NAT
  848. if (isflagset(FLT_NAT)) {
  849. append_hf("Path: <sip:term@HOSTNAME:"+PORT+";nat=yes;received=sip:$si:$sp;$var(ws_transport)lr>\r\n");
  850. } else
  851. #!endif
  852. append_hf("Path: <sip:term@HOSTNAME:"+PORT+";$var(ws_transport)lr>\r\n");
  853.  
  854. append_hf("Supported: path\r\n");
  855. append_hf("Require: path\r\n");
  856. #!endif
  857.  
  858. # Add a visited Network-ID-Header:
  859. if (is_present_hf("P-Visited-Network-ID")) {
  860. $var(new_hdr) = "NETWORKNAME, "+$hdr(P-Visited-Network-ID);
  861. append_hf("P-Visited-Network-ID: $var(new_hdr)\r\n");
  862. } else {
  863. append_hf("P-Visited-Network-ID: NETWORKNAME\r\n");
  864. }
  865.  
  866. #!ifdef WITH_SBC
  867. # Choose an SBC to send the call to:
  868. # Kamal commented
  869. # if (!ds_select_dst(DISPATCHER_LIST_SBC, "4")) {
  870. # send_reply("503", "Service Unavailable (SBC failure)");
  871. # exit;
  872. # }
  873. #!endif
  874.  
  875. t_relay();
  876. exit;
  877. }
  878.  
  879. # Replies for REGISTER requests:
  880. ######################################################################
  881. onreply_route[REGISTER_reply]
  882. {
  883. #!ifdef WITH_IMS_HDR_CACHE
  884. if (is_present_hf("Service-Route")) {
  885. $sht(serviceroutes=>$ci) = $hdr(Service-Route);
  886. } else {
  887. if ($sht(serviceroutes=>$ci) != $null) {
  888. append_hf("Service-Route: $sht(serviceroutes=>$ci)\r\n");
  889. msg_apply_changes();
  890. }
  891. }
  892. if (is_present_hf("P-Associated-URI")) {
  893. $sht(associateduris=>$ci) = $hdr(P-Associated-URI);
  894. } else {
  895. if ($sht(associateduris=>$ci) != $null) {
  896. append_hf("P-Associated-URI: $sht(associateduris=>$ci)\r\n");
  897. msg_apply_changes();
  898. }
  899. }
  900. #!endif
  901. if (t_check_status("200")) {
  902. pcscf_save("location");
  903. }
  904. exit;
  905. }
  906.  
  907. # Negative replies to REGISTER requests:
  908. ######################################################################
  909. failure_route[REGISTER_failure]
  910. {
  911. if (t_check_status("408"))
  912. send_reply("504","Server Time-Out");
  913. }
  914.  
  915. ######################################################################
  916. ######################################################################
  917. ## Originating requests
  918. ######################################################################
  919. ######################################################################
  920.  
  921. ######################################################################
  922. # Originating, Intial Requests
  923. ######################################################################
  924. route[Orig_Initial]
  925. {
  926. # Process route headers, if any:
  927. loose_route();
  928. if (!pcscf_is_registered("location")) {
  929. send_reply("403","Forbidden - You must register first with a S-CSCF");
  930. break;
  931. }
  932.  
  933. # We do not trust the user, let's remove the P-Asserted-Identity, if any:
  934. remove_hf("P-Asserted-Identity");
  935. remove_hf("P-Preferred-Identity");
  936.  
  937. if (is_present_hf("P-Preferred-Identity") && pcscf_assert_identity("location", "$hdr(P-Preferred-Identity)")) {
  938. append_hf("P-Asserted-Identity: $hdr(P-Preferred-Identity)\r\n");
  939. } else if (is_present_hf("P-Asserted-Identity") && pcscf_assert_identity("location", "$hdr(P-Asserted-Identity)")) {
  940. append_hf("P-Asserted-Identity: $hdr(P-Asserted-Identity)\r\n");
  941. } else if (pcscf_assert_identity("location", "$fu")) {
  942. append_hf("P-Asserted-Identity: <$fu>\r\n");
  943. } else {
  944. append_hf("P-Asserted-Identity: <$pcscf_asserted_identity>\r\n");
  945. }
  946.  
  947. if (!pcscf_follows_service_routes("location")){
  948. #Variant 1 - deny access to the network
  949. #send_reply("400","Bad Request - Not following indicated service routes");
  950. #break;
  951.  
  952. #Variant 2 - enforce routes and let the dialog continue
  953. pcscf_force_service_routes("location");
  954. }
  955. #!ifdef WITH_SBC
  956. # Apply changes to this message
  957. msg_apply_changes();
  958. # Copy Route-Header:
  959. append_hf("P-Route: $(hdr(Route){nameaddr.uri})\r\n");
  960. #!endif
  961.  
  962. # add IBCF/THIG route here if required
  963. # Check for "sec-agree" in the Require header:
  964. if (is_present_hf("Require") && $hdr(Require) =~ ".*sec-agree.*") {
  965. # Remove the old Require-Header:
  966. remove_hf("Require");
  967. # Replace ", sec-agree" with ""
  968. $var(new_hdr) = $(hdr(Require){re.subst,/[, ]*sec-agree//gi});
  969. if ($(var(new_hdr){s.len}) > 0) {
  970. append_hf("Require: $var(new_hdr)\r\n");
  971. }
  972. }
  973.  
  974. # Check for "sec-agree" in the Proxy-Require header:
  975. if (is_present_hf("Proxy-Require") && $hdr(Proxy-Require) =~ ".*sec-agree.*") {
  976. # Remove the old Proxy-Require-Header:
  977. remove_hf("Proxy-Require");
  978. # Replace ", sec-agree" with ""
  979. $var(new_hdr) = $(hdr(Proxy-Require){re.subst,/[, ]*sec-agree//gi});
  980. if ($(var(new_hdr){s.len}) > 0) {
  981. append_hf("Proxy-Require: $var(new_hdr)\r\n");
  982. }
  983. }
  984. remove_hf("Security-Verify");
  985.  
  986.  
  987. #!ifdef WITH_RX
  988. xlog("L_DBG","Diameter: Orig authorizing media via Rx\n");
  989. if(Rx_AAR("ORIG_SESSION_AAR","orig")==0){
  990. exit;
  991. }
  992. }
  993.  
  994. route[ORIG_SESSION_AAR] {
  995. if ($avp(s:aar_return_code) != 1) {
  996. xlog("L_ERR", "Diameter: AAR failed\n");
  997. send_reply("403", "QoS not authorized");
  998. exit;
  999. }
  1000. #!endif
  1001. # Do RTP-Relaying, if necessary:
  1002. setflag(FLT_MO);
  1003. route(RTPPROXY);
  1004.  
  1005. #prepend mo as user for record route
  1006. $avp(RR_CUSTOM_USER_AVP)="mo";
  1007.  
  1008. # Do Record-Route for this request:
  1009. record_route();
  1010.  
  1011. set_dlg_profile("orig");
  1012. t_on_reply("Orig_Initial_reply");
  1013.  
  1014. #!ifdef WITH_SBC
  1015. # Choose an SBC to send the call to:
  1016. # Kamal commented
  1017. # if (!ds_select_dst(DISPATCHER_LIST_SBC, "4")) {
  1018. # send_reply("503", "Service Unavailable (SBC failure)");
  1019. # exit;
  1020. # }
  1021. #!endif
  1022.  
  1023. t_relay();
  1024.  
  1025. break;
  1026. }
  1027.  
  1028. ######################################################################
  1029. # Replies to the Initial Requests
  1030. ######################################################################
  1031. onreply_route[Orig_Initial_reply]
  1032. {
  1033. #!ifdef WITH_WEBSOCKET
  1034. if (($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT) && !(proto == WS || proto == WSS)) {
  1035. xlog("L_WARN", "Websocket/SIP response received on $Rp\n");
  1036. drop;
  1037. }
  1038. #!endif
  1039. #!ifdef WITH_RX
  1040. if (t_check_status("180|183|200")){
  1041. xlog("L_DBG","Diameter: Orig authorizing media via Rx\n");
  1042. if(Rx_AAR("ORIG_SESSION_AAR_REPLY","orig")==0){
  1043. exit;
  1044. }
  1045. }
  1046. }
  1047.  
  1048. route[ORIG_SESSION_AAR_REPLY] {
  1049. if ($avp(s:aar_return_code) != 1) {
  1050. xlog("L_ERR", "IMS: AAR failed Orig\n");
  1051. dlg_terminate("all", "Sorry no QoS available");
  1052. } else {
  1053. xlog("L_DBG", "Diameter: Orig AAR success on media authorization\n");
  1054. }
  1055. #!endif
  1056. # Note: We only do the RTP-Update for the successful case,
  1057. # the others simply time-out (if we would do otherwise, RTP-Relaying
  1058. # would fail for forked requests)
  1059.  
  1060. # Do RTP-Relaying, if necessary:
  1061. if (status=~"[12][0-9][0-9]")
  1062. route(RTPPROXY);
  1063. }
  1064.  
  1065. ######################################################################
  1066. ######################################################################
  1067. ## Terminating requests
  1068. ######################################################################
  1069. ######################################################################
  1070.  
  1071. # Terminating, Initial requests
  1072. ######################################################################
  1073. route[Term_Initial]
  1074. {
  1075. loose_route();
  1076.  
  1077. $avp(RR_CUSTOM_USER_AVP)="mt";
  1078. record_route();
  1079.  
  1080. set_dlg_profile("term");
  1081. t_on_failure("Term_Initial_failure");
  1082. t_on_reply("Term_Initial_reply");
  1083.  
  1084. if(!isdsturiset()) {
  1085. handle_ruri_alias();
  1086. if ($rc == 1) {
  1087. setflag(FLT_NAT);
  1088. }
  1089. }
  1090. # Do RTP-Relaying, if necessary:
  1091. resetflag(FLT_MO);
  1092. route(RTPPROXY);
  1093.  
  1094. #!ifdef WITH_RX
  1095. xlog("L_DBG","Diameter: Term authorizing media via Rx\n");
  1096. if(Rx_AAR("TERM_SESSION_AAR","term")==0){
  1097. exit;
  1098. }
  1099. }
  1100.  
  1101. route[TERM_SESSION_AAR] {
  1102. if ($avp(s:aar_return_code) != 1) {
  1103. xlog("L_ERR", "Diameter: AAR failed\n");
  1104. send_reply("403", "QoS not authorized");
  1105. exit;
  1106. }
  1107. #!endif
  1108. t_relay();
  1109. }
  1110.  
  1111. # Replies to terminating, initial requests
  1112. ######################################################################
  1113. onreply_route[Term_Initial_reply]
  1114. {
  1115. #!ifdef WITH_WEBSOCKET
  1116. if (($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT) && !(proto == WS || proto == WSS)) {
  1117. xlog("L_WARN", "Websocket/SIP response received on $Rp\n");
  1118. drop;
  1119. }
  1120. #!endif
  1121. #!ifdef FALSE
  1122. if (t_check_status("183")||t_check_status("200")){
  1123. if (!pcscf_is_registered("location")) {
  1124. xlog("L_DBG", "IMS: INSIDE TERM_INITIAL_REPLY: can't find contact [$ct] in P-CSCF usrloc\n");
  1125. send_reply("403","Forbidden - not registered with P-CSCF");
  1126. break;
  1127. }
  1128. #asserted identity header
  1129. if (pcscf_assert_called_identity("location")) {
  1130. xlog("L_DBG", "P-Called-Party-ID asserted");
  1131. } else {
  1132. xlog("L_DBG", "P-Called-Party-ID not asserted - using default " + "$pcscf_asserted_identity");
  1133. append_hf("P-Asserted-Identity: <$pcscf_asserted_identity>\r\n");
  1134. }
  1135. if(is_present_hf("P-Preferred-Identity")) {
  1136. remove_hf("P-Preferred-Identity");
  1137. }
  1138. msg_apply_changes();
  1139. }
  1140. #!endif
  1141.  
  1142. #!ifdef WITH_RX
  1143. if (t_check_status("180|183|200")){
  1144. xlog("L_DBG","Diameter: Orig authorizing media via Rx\n");
  1145. if(Rx_AAR("TERM_SESSION_AAR_REPLY","term")==0){
  1146. exit;
  1147. }
  1148. }
  1149. }
  1150.  
  1151. route[TERM_SESSION_AAR_REPLY] {
  1152. if ($avp(s:aar_return_code) != 1) {
  1153. xlog("L_ERR", "Diameter: AAR failed\n");
  1154. send_reply("403", "QoS not authorized");
  1155. exit;
  1156. }
  1157. #!endif
  1158.  
  1159. # Do RTP-Relaying, if necessary:
  1160. if (status=~"[12][0-9][0-9]")
  1161. route(RTPPROXY);
  1162. }
  1163.  
  1164. # Replies to terminating, initial requests
  1165. ######################################################################
  1166. failure_route[Term_Initial_failure]
  1167. {
  1168. if (t_is_canceled()) {
  1169. exit;
  1170. }
  1171. if (t_check_status("408")) {
  1172. send_reply("404","User offline");
  1173. exit;
  1174. }
  1175. }
  1176.  
  1177. ######################################################################
  1178. ######################################################################
  1179. ## Inter-System Communications
  1180. ######################################################################
  1181. ######################################################################
  1182.  
  1183. # Notify Route: #
  1184. #####################################################################
  1185. route[NOTIFY]
  1186. {
  1187. xlog("L_DBG", "IMS: INSIDE NOTIFY\n");
  1188. if (reginfo_handle_notify("location")) {
  1189. send_reply("200","OK - P-CSCF processed notification");
  1190. break;
  1191. } else {
  1192. t_reply("500","Error encountered while processing notification");
  1193. break;
  1194. }
  1195. }
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement