kamailio-basic-kemi-lua.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. -- Kamailio - equivalent of routing blocks in Lua
  2. --
  3. -- KSR - the new dynamic object exporting Kamailio functions (kemi)
  4. -- sr - the old static object exporting Kamailio functions
  5. --
  6. -- Relevant remarks:
  7. -- * do not execute Lua 'exit' - that will kill Lua interpreter which is
  8. -- embedded in Kamailio, resulting in killing Kamailio
  9. -- * use KSR.x.exit() to trigger the stop of executing the script
  10. -- * KSR.drop() is only marking the SIP message for drop, but doesn't stop
  11. -- the execution of the script. Use KSR.x.exit() after it or KSR.x.drop()
  12. --
  13. -- global variables corresponding to defined values (e.g., flags) in kamailio.cfg
  14. FLT_ACC=1
  15. FLT_ACCMISSED=2
  16. FLT_ACCFAILED=3
  17. FLT_NATS=5
  18. FLB_NATB=6
  19. FLB_NATSIPPING=7
  20. -- SIP request routing
  21. -- equivalent of request_route{}
  22. function ksr_request_route()
  23. -- per request initial checks
  24. ksr_route_reqinit();
  25. -- NAT detection
  26. ksr_route_natdetect();
  27. -- CANCEL processing
  28. if KSR.is_CANCEL() then
  29. if KSR.tm.t_check_trans()>0 then
  30. ksr_route_relay();
  31. end
  32. return 1;
  33. end
  34. -- handle requests within SIP dialogs
  35. ksr_route_withindlg();
  36. -- -- only initial requests (no To tag)
  37. -- handle retransmissions
  38. if KSR.tmx.t_precheck_trans()>0 then
  39. KSR.tm.t_check_trans();
  40. return 1;
  41. end
  42. if KSR.tm.t_check_trans()==0 then return 1 end
  43. -- authentication
  44. ksr_route_auth();
  45. -- record routing for dialog forming requests (in case they are routed)
  46. -- - remove preloaded route headers
  47. KSR.hdr.remove("Route");
  48. -- if INVITE or SUBSCRIBE
  49. if KSR.is_method_in("IS") then
  50. KSR.rr.record_route();
  51. end
  52. -- account only INVITEs
  53. if KSR.is_INVITE() then
  54. KSR.setflag(FLT_ACC); -- do accounting
  55. end
  56. -- dispatch requests to foreign domains
  57. ksr_route_sipout();
  58. -- -- requests for my local domains
  59. -- handle registrations
  60. ksr_route_registrar();
  61. if KSR.corex.has_ruri_user() < 0 then
  62. -- request with no Username in RURI
  63. KSR.sl.sl_send_reply(484,"Address Incomplete");
  64. return 1;
  65. end
  66. -- user location service
  67. ksr_route_location();
  68. return 1;
  69. end
  70. -- wrapper around tm relay function
  71. function ksr_route_relay()
  72. -- enable additional event routes for forwarded requests
  73. -- - serial forking, RTP relaying handling, a.s.o.
  74. if KSR.is_method_in("IBSU") then
  75. if KSR.tm.t_is_set("branch_route")<0 then
  76. KSR.tm.t_on_branch("ksr_branch_manage");
  77. end
  78. end
  79. if KSR.is_method_in("ISU") then
  80. if KSR.tm.t_is_set("onreply_route")<0 then
  81. KSR.tm.t_on_reply("ksr_onreply_manage");
  82. end
  83. end
  84. if KSR.is_INVITE() then
  85. if KSR.tm.t_is_set("failure_route")<0 then
  86. KSR.tm.t_on_failure("ksr_failure_manage");
  87. end
  88. end
  89. if KSR.tm.t_relay()<0 then
  90. KSR.sl.sl_reply_error();
  91. end
  92. KSR.x.exit();
  93. end
  94. -- Per SIP request initial checks
  95. function ksr_route_reqinit()
  96. if not KSR.is_myself(KSR.pv.get("$si")) then
  97. if not KSR.pv.is_null("$sht(ipban=>$si)") then
  98. -- ip is already blocked
  99. KSR.dbg("request from blocked IP - " .. KSR.pv.get("$rm")
  100. .. " from " .. KSR.pv.get("$fu") .. " (IP:"
  101. .. KSR.pv.get("$si") .. ":" .. KSR.pv.get("$sp") .. ")\n");
  102. KSR.x.exit();
  103. end
  104. if KSR.pike.pike_check_req()<0 then
  105. KSR.err("ALERT: pike blocking " .. KSR.pv.get("$rm")
  106. .. " from " .. KSR.pv.get("$fu") .. " (IP:"
  107. .. KSR.pv.get("$si") .. ":" .. KSR.pv.get("$sp") .. ")\n");
  108. KSR.pv.seti("$sht(ipban=>$si)", 1);
  109. KSR.x.exit();
  110. end
  111. end
  112. if KSR.corex.has_user_agent() > 0 then
  113. local ua = KSR.pv.gete("$ua");
  114. if string.find(ua, "friendly-scanner")
  115. or string.find(ua, "sipcli") then
  116. KSR.sl.sl_send_reply(200, "OK");
  117. KSR.x.exit();
  118. end
  119. end
  120. if KSR.maxfwd.process_maxfwd(10) < 0 then
  121. KSR.sl.sl_send_reply(483,"Too Many Hops");
  122. KSR.x.exit();
  123. end
  124. if KSR.is_OPTIONS()
  125. and KSR.is_myself_ruri()
  126. and KSR.corex.has_ruri_user() < 0 then
  127. KSR.sl.sl_send_reply(200,"Keepalive");
  128. KSR.x.exit();
  129. end
  130. if KSR.sanity.sanity_check(1511, 7)<0 then
  131. KSR.err("Malformed SIP message from "
  132. .. KSR.pv.get("$si") .. ":" .. KSR.pv.get("$sp") .."\n");
  133. KSR.x.exit();
  134. end
  135. end
  136. -- Handle requests within SIP dialogs
  137. function ksr_route_withindlg()
  138. if KSR.siputils.has_totag()<0 then return 1; end
  139. -- sequential request withing a dialog should
  140. -- take the path determined by record-routing
  141. if KSR.rr.loose_route()>0 then
  142. ksr_route_dlguri();
  143. if KSR.is_BYE() then
  144. KSR.setflag(FLT_ACC); -- do accounting ...
  145. KSR.setflag(FLT_ACCFAILED); -- ... even if the transaction fails
  146. elseif KSR.is_ACK() then
  147. -- ACK is forwarded statelessly
  148. ksr_route_natmanage();
  149. elseif KSR.is_NOTIFY() then
  150. -- Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  151. KSR.rr.record_route();
  152. end
  153. ksr_route_relay();
  154. KSR.x.exit();
  155. end
  156. if KSR.is_ACK() then
  157. if KSR.tm.t_check_trans() >0 then
  158. -- no loose-route, but stateful ACK;
  159. -- must be an ACK after a 487
  160. -- or e.g. 404 from upstream server
  161. ksr_route_relay();
  162. KSR.x.exit();
  163. else
  164. -- ACK without matching transaction ... ignore and discard
  165. KSR.x.exit();
  166. end
  167. end
  168. KSR.sl.sl_send_reply(404, "Not here");
  169. KSR.x.exit();
  170. end
  171. -- Handle SIP registrations
  172. function ksr_route_registrar()
  173. if not KSR.is_REGISTER() then return 1; end
  174. if KSR.isflagset(FLT_NATS) then
  175. KSR.setbflag(FLB_NATB);
  176. -- do SIP NAT pinging
  177. KSR.setbflag(FLB_NATSIPPING);
  178. end
  179. if KSR.registrar.save("location", 0)<0 then
  180. KSR.sl.sl_reply_error();
  181. end
  182. KSR.x.exit();
  183. end
  184. -- User location service
  185. function ksr_route_location()
  186. local rc = KSR.registrar.lookup("location");
  187. if rc<0 then
  188. KSR.tm.t_newtran();
  189. if rc==-1 or rc==-3 then
  190. KSR.sl.send_reply("404", "Not Found");
  191. KSR.x.exit();
  192. elseif rc==-2 then
  193. KSR.sl.send_reply("405", "Method Not Allowed");
  194. KSR.x.exit();
  195. end
  196. end
  197. -- when routing via usrloc, log the missed calls also
  198. if KSR.is_INVITE() then
  199. KSR.setflag(FLT_ACCMISSED);
  200. end
  201. ksr_route_relay();
  202. KSR.x.exit();
  203. end
  204. -- IP authorization and user uthentication
  205. function ksr_route_auth()
  206. if not KSR.is_REGISTER() then
  207. if KSR.permissions.allow_source_address(1)>0 then
  208. -- source IP allowed
  209. return 1;
  210. end
  211. end
  212. if KSR.is_REGISTER() or KSR.is_myself_furi() then
  213. -- authenticate requests
  214. if KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0 then
  215. KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0);
  216. KSR.x.exit();
  217. end
  218. -- user authenticated - remove auth header
  219. if not KSR.is_method_in("RP") then
  220. KSR.auth.consume_credentials();
  221. end
  222. end
  223. -- if caller is not local subscriber, then check if it calls
  224. -- a local destination, otherwise deny, not an open relay here
  225. if (not KSR.is_myself_furi())
  226. and (not KSR.is_myself_ruri()) then
  227. KSR.sl.sl_send_reply(403,"Not relaying");
  228. KSR.x.exit();
  229. end
  230. return 1;
  231. end
  232. -- Caller NAT detection
  233. function ksr_route_natdetect()
  234. KSR.force_rport();
  235. if KSR.nathelper.nat_uac_test(19)>0 then
  236. if KSR.is_REGISTER() then
  237. KSR.nathelper.fix_nated_register();
  238. elseif KSR.siputils.is_first_hop()>0 then
  239. KSR.nathelper.set_contact_alias();
  240. end
  241. KSR.setflag(FLT_NATS);
  242. end
  243. return 1;
  244. end
  245. -- RTPProxy control
  246. function ksr_route_natmanage()
  247. if KSR.siputils.is_request()>0 then
  248. if KSR.siputils.has_totag()>0 then
  249. if KSR.rr.check_route_param("nat=yes")>0 then
  250. KSR.setbflag(FLB_NATB);
  251. end
  252. end
  253. end
  254. if (not (KSR.isflagset(FLT_NATS) or KSR.isbflagset(FLB_NATB))) then
  255. return 1;
  256. end
  257. KSR.rtpproxy.rtpproxy_manage("co");
  258. if KSR.siputils.is_request()>0 then
  259. if KSR.siputils.has_totag()<0 then
  260. if KSR.tmx.t_is_branch_route()>0 then
  261. KSR.rr.add_rr_param(";nat=yes");
  262. end
  263. end
  264. end
  265. if KSR.siputils.is_reply()>0 then
  266. if KSR.isbflagset(FLB_NATB) then
  267. KSR.nathelper.set_contact_alias();
  268. end
  269. end
  270. return 1;
  271. end
  272. -- URI update for dialog requests
  273. function ksr_route_dlguri()
  274. if not KSR.isdsturiset() then
  275. KSR.nathelper.handle_ruri_alias();
  276. end
  277. return 1;
  278. end
  279. -- Routing to foreign domains
  280. function ksr_route_sipout()
  281. if KSR.is_myself_ruri() then return 1; end
  282. KSR.hdr.append("P-Hint: outbound\r\n");
  283. ksr_route_relay();
  284. KSR.x.exit();
  285. end
  286. -- Manage outgoing branches
  287. -- equivalent of branch_route[...]{}
  288. function ksr_branch_manage()
  289. KSR.dbg("new branch [".. KSR.pv.get("$T_branch_idx")
  290. .. "] to ".. KSR.pv.get("$ru") .. "\n");
  291. ksr_route_natmanage();
  292. return 1;
  293. end
  294. -- Manage incoming replies
  295. -- equivalent of onreply_route[...]{}
  296. function ksr_onreply_manage()
  297. KSR.dbg("incoming reply\n");
  298. local scode = KSR.pv.get("$rs");
  299. if scode>100 and scode<299 then
  300. ksr_route_natmanage();
  301. end
  302. return 1;
  303. end
  304. -- Manage failure routing cases
  305. -- equivalent of failure_route[...]{}
  306. function ksr_failure_manage()
  307. ksr_route_natmanage();
  308. if KSR.tm.t_is_canceled()>0 then
  309. return 1;
  310. end
  311. return 1;
  312. end
  313. -- SIP response handling
  314. -- equivalent of reply_route{}
  315. function ksr_reply_route()
  316. KSR.info("===== response - from kamailio lua script\n");
  317. return 1;
  318. end