kamailio-basic-kemi-lua.lua 8.8 KB

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