kamailio-basic-kemi-native.cfg 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ####### Routing Logic ########
  2. # Main SIP request routing logic
  3. # - processing of any incoming SIP request starts with this route
  4. # - note: this is the same as route { ... }
  5. request_route {
  6. # per request initial checks
  7. route(REQINIT);
  8. # NAT detection
  9. route(NATDETECT);
  10. # CANCEL processing
  11. if (is_method("CANCEL")) {
  12. if (t_check_trans()) {
  13. route(RELAY);
  14. }
  15. exit;
  16. }
  17. # handle requests within SIP dialogs
  18. route(WITHINDLG);
  19. ### only initial requests (no To tag)
  20. # handle retransmissions
  21. if(t_precheck_trans()) {
  22. t_check_trans();
  23. exit;
  24. }
  25. t_check_trans();
  26. # authentication
  27. route(AUTH);
  28. # record routing for dialog forming requests (in case they are routed)
  29. # - remove preloaded route headers
  30. remove_hf("Route");
  31. if (is_method("INVITE|SUBSCRIBE"))
  32. record_route();
  33. # account only INVITEs
  34. if (is_method("INVITE")) {
  35. setflag(FLT_ACC); # do accounting
  36. }
  37. # dispatch requests to foreign domains
  38. route(SIPOUT);
  39. ### requests for my local domains
  40. # handle registrations
  41. route(REGISTRAR);
  42. if ($rU==$null) {
  43. # request with no Username in RURI
  44. sl_send_reply("484","Address Incomplete");
  45. exit;
  46. }
  47. # user location service
  48. route(LOCATION);
  49. }
  50. route[RELAY] {
  51. # enable additional event routes for forwarded requests
  52. # - serial forking, RTP relaying handling, a.s.o.
  53. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  54. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  55. }
  56. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  57. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  58. }
  59. if (is_method("INVITE")) {
  60. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  61. }
  62. if (!t_relay()) {
  63. sl_reply_error();
  64. }
  65. exit;
  66. }
  67. # Per SIP request initial checks
  68. route[REQINIT] {
  69. #!ifdef WITH_ANTIFLOOD
  70. # flood dection from same IP and traffic ban for a while
  71. # be sure you exclude checking trusted peers, such as pstn gateways
  72. # - local host excluded (e.g., loop to self)
  73. if(src_ip!=myself) {
  74. if($sht(ipban=>$si)!=$null) {
  75. # ip is already blocked
  76. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  77. exit;
  78. }
  79. if (!pike_check_req()) {
  80. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  81. $sht(ipban=>$si) = 1;
  82. exit;
  83. }
  84. }
  85. if($ua =~ "friendly-scanner") {
  86. sl_send_reply("200", "OK");
  87. exit;
  88. }
  89. #!endif
  90. if (!mf_process_maxfwd_header("10")) {
  91. sl_send_reply("483","Too Many Hops");
  92. exit;
  93. }
  94. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  95. sl_send_reply("200","Keepalive");
  96. exit;
  97. }
  98. if(!sanity_check("1511", "7")) {
  99. xlog("Malformed SIP message from $si:$sp\n");
  100. exit;
  101. }
  102. }
  103. # Handle requests within SIP dialogs
  104. route[WITHINDLG] {
  105. if (!has_totag()) return;
  106. # sequential request withing a dialog should
  107. # take the path determined by record-routing
  108. if (loose_route()) {
  109. route(DLGURI);
  110. if (is_method("BYE")) {
  111. setflag(FLT_ACC); # do accounting ...
  112. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  113. }
  114. else if ( is_method("ACK") ) {
  115. # ACK is forwarded statelessy
  116. route(NATMANAGE);
  117. }
  118. else if ( is_method("NOTIFY") ) {
  119. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  120. record_route();
  121. }
  122. route(RELAY);
  123. exit;
  124. }
  125. if ( is_method("ACK") ) {
  126. if ( t_check_trans() ) {
  127. # no loose-route, but stateful ACK;
  128. # must be an ACK after a 487
  129. # or e.g. 404 from upstream server
  130. route(RELAY);
  131. exit;
  132. } else {
  133. # ACK without matching transaction ... ignore and discard
  134. exit;
  135. }
  136. }
  137. sl_send_reply("404", "Not here");
  138. exit;
  139. }
  140. # Handle SIP registrations
  141. route[REGISTRAR] {
  142. if (!is_method("REGISTER")) return;
  143. if(isflagset(FLT_NATS)) {
  144. setbflag(FLB_NATB);
  145. #!ifdef WITH_NATSIPPING
  146. # do SIP NAT pinging
  147. setbflag(FLB_NATSIPPING);
  148. #!endif
  149. }
  150. if (!save("location"))
  151. sl_reply_error();
  152. exit;
  153. }
  154. # User location service
  155. route[LOCATION] {
  156. if (!lookup("location")) {
  157. $var(rc) = $rc;
  158. t_newtran();
  159. switch ($var(rc)) {
  160. case -1:
  161. case -3:
  162. send_reply("404", "Not Found");
  163. exit;
  164. case -2:
  165. send_reply("405", "Method Not Allowed");
  166. exit;
  167. }
  168. }
  169. # when routing via usrloc, log the missed calls also
  170. if (is_method("INVITE")) {
  171. setflag(FLT_ACCMISSED);
  172. }
  173. route(RELAY);
  174. exit;
  175. }
  176. # IP authorization and user uthentication
  177. route[AUTH] {
  178. #!ifdef WITH_AUTH
  179. #!ifdef WITH_IPAUTH
  180. if((!is_method("REGISTER")) && allow_source_address()) {
  181. # source IP allowed
  182. return;
  183. }
  184. #!endif
  185. if (is_method("REGISTER") || from_uri==myself) {
  186. # authenticate requests
  187. if (!auth_check("$fd", "subscriber", "1")) {
  188. auth_challenge("$fd", "0");
  189. exit;
  190. }
  191. # user authenticated - remove auth header
  192. if(!is_method("REGISTER|PUBLISH"))
  193. consume_credentials();
  194. }
  195. # if caller is not local subscriber, then check if it calls
  196. # a local destination, otherwise deny, not an open relay here
  197. if (from_uri!=myself && uri!=myself) {
  198. sl_send_reply("403","Not relaying");
  199. exit;
  200. }
  201. #!endif
  202. return;
  203. }
  204. # Caller NAT detection
  205. route[NATDETECT] {
  206. #!ifdef WITH_NAT
  207. force_rport();
  208. if (nat_uac_test("19")) {
  209. if (is_method("REGISTER")) {
  210. fix_nated_register();
  211. } else {
  212. if(is_first_hop())
  213. set_contact_alias();
  214. }
  215. setflag(FLT_NATS);
  216. }
  217. #!endif
  218. return;
  219. }
  220. # RTPProxy control
  221. route[NATMANAGE] {
  222. #!ifdef WITH_NAT
  223. if (is_request()) {
  224. if(has_totag()) {
  225. if(check_route_param("nat=yes")) {
  226. setbflag(FLB_NATB);
  227. }
  228. }
  229. }
  230. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
  231. return;
  232. rtpproxy_manage("co");
  233. if (is_request()) {
  234. if (!has_totag()) {
  235. if(t_is_branch_route()) {
  236. add_rr_param(";nat=yes");
  237. }
  238. }
  239. }
  240. if (is_reply()) {
  241. if(isbflagset(FLB_NATB)) {
  242. set_contact_alias();
  243. }
  244. }
  245. #!endif
  246. return;
  247. }
  248. # URI update for dialog requests
  249. route[DLGURI] {
  250. #!ifdef WITH_NAT
  251. if(!isdsturiset()) {
  252. handle_ruri_alias();
  253. }
  254. #!endif
  255. return;
  256. }
  257. # Routing to foreign domains
  258. route[SIPOUT] {
  259. if (uri==myself) return;
  260. append_hf("P-hint: outbound\r\n");
  261. route(RELAY);
  262. exit;
  263. }
  264. # Manage outgoing branches
  265. branch_route[MANAGE_BRANCH] {
  266. xdbg("new branch [$T_branch_idx] to $ru\n");
  267. route(NATMANAGE);
  268. }
  269. # Manage incoming replies
  270. onreply_route[MANAGE_REPLY] {
  271. xdbg("incoming reply\n");
  272. if(status=~"[12][0-9][0-9]")
  273. route(NATMANAGE);
  274. }
  275. # Manage failure routing cases
  276. failure_route[MANAGE_FAILURE] {
  277. route(NATMANAGE);
  278. if (t_is_canceled()) {
  279. exit;
  280. }
  281. }