sip-router.cfg.m4 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. ### m4 macros to make the configuration easier
  2. include(`rules.m4')
  3. define(`SER_IP', `192.168.0.1')
  4. define(`SER_HOSTNAME', `foo.bar')
  5. define(`GW_IP_1', `192.168.0.2')
  6. define(`GW_IP_2', `192.168.0.3')
  7. declare(flags, ACC_FLAG, MISSED_FLAG, VM_FLAG, NAT_FLAG)
  8. declare(route, PSTN_ROUTE, NAT_ROUTE, VOICEMAIL_ROUTE, PSTN2_ROUTE)
  9. declare(onreply, NAT_REPLY)
  10. declare(failure, PSTN_FAILURE, _1_FAILURE)
  11. ### End of m4 macro section
  12. #
  13. # $Id$
  14. #
  15. # sip-router.cfg m4 template
  16. #
  17. #
  18. # Set the following in your CISCO PSTN gateway:
  19. # sip-ua
  20. # nat symmetric role passive
  21. # nat symmetric check-media-src
  22. #
  23. fork=yes
  24. port=5060
  25. log_stderror=no
  26. fifo="/tmp/sip-router_fifo"
  27. # uncomment to enter testing mode
  28. /*
  29. fork=no
  30. port=5064
  31. log_stderror=yes
  32. fifo="/tmp/sip-router_fifox"
  33. */
  34. debug=3
  35. memlog=4 # memlog set high (>debug) -- no final time-consuming memory reports on exit
  36. mhomed=yes
  37. listen=SER_IP
  38. alias="SER_HOSTNAME"
  39. check_via=yes
  40. dns=yes
  41. rev_dns=no
  42. children=16
  43. # if changing fifo mode to a more restrictive value, put
  44. # decimal value in there, e.g. dec(rw|rw|rw)=dec(666)=438
  45. fifo_mode=0666
  46. loadmodule "/usr/local/lib/sip-router/modules/tm.so"
  47. loadmodule "/usr/local/lib/sip-router/modules/sl.so"
  48. loadmodule "/usr/local/lib/sip-router/modules/acc.so"
  49. loadmodule "/usr/local/lib/sip-router/modules/rr.so"
  50. loadmodule "/usr/local/lib/sip-router/modules/maxfwd.so"
  51. loadmodule "/usr/local/lib/sip-router/modules/mysql.so"
  52. loadmodule "/usr/local/lib/sip-router/modules/usrloc.so"
  53. loadmodule "/usr/local/lib/sip-router/modules/registrar.so"
  54. loadmodule "/usr/local/lib/sip-router/modules/auth.so"
  55. loadmodule "/usr/local/lib/sip-router/modules/auth_db.so"
  56. loadmodule "/usr/local/lib/sip-router/modules/textops.so"
  57. loadmodule "/usr/local/lib/sip-router/modules/uri.so"
  58. loadmodule "/usr/local/lib/sip-router/modules/group.so"
  59. loadmodule "/usr/local/lib/sip-router/modules/msilo.so"
  60. loadmodule "/usr/local/lib/sip-router/modules/nathelper.so"
  61. loadmodule "/usr/local/lib/sip-router/modules/enum.so"
  62. loadmodule "/usr/local/lib/sip-router/modules/domain.so"
  63. #loadmodule "/usr/local/lib/sip-router/modules/permissions.so"
  64. modparam("usrloc|acc|auth_db|group|msilo", "db_url", "sql://sip-router:heslo@localhost/sip-router")
  65. # -- usrloc params --
  66. /* 0 -- dont use mysql, 1 -- write_through, 2--write_back */
  67. modparam("usrloc", "db_mode", 2)
  68. modparam("usrloc", "timer_interval", 10)
  69. # -- auth params --
  70. modparam("auth_db", "calculate_ha1", yes)
  71. modparam("auth_db", "plain_password_column", "password")
  72. #modparam("auth_db", "use_rpid", 1)
  73. modparam("auth", "nonce_expire", 300)
  74. modparam("auth", "rpid_prefix", "<sip:")
  75. modparam("auth", "rpid_suffix", "@GW_IP_3>;party=calling;id-type=subscriber;screen=yes;privacy=off")
  76. # -- rr params --
  77. # add value to ;lr param to make some broken UAs happy
  78. modparam("rr", "enable_full_lr", 1)
  79. # -- acc params --
  80. # report ACKs too for sake of completeness -- as we account PSTN
  81. # destinations which are RR, ACKs should show up
  82. modparam("acc", "report_ack", 1)
  83. modparam("acc", "log_level", 1)
  84. # if BYE fails (telephone is dead, record-routing broken, etc.), generate
  85. # a report nevertheless -- otherwise we would have no STOP event; => 1
  86. modparam("acc", "failed_transactions", 1)
  87. # that is the flag for which we will account -- don't forget to
  88. # set the same one :-)
  89. # Usage of flags is as follows:
  90. # 1 == should account(all to gateway),
  91. # 3 == should report on missed calls (transactions to iptel.org's users),
  92. # 4 == destination user wishes to use voicemail
  93. # 6 == nathelper
  94. #
  95. modparam("acc", "log_flag", ACC_FLAG)
  96. modparam("acc", "db_flag", ACC_FLAG)
  97. modparam("acc", "log_missed_flag", MISSED_FLAG)
  98. modparam("acc", "db_missed_flag", MISSED_FLAG)
  99. # report to syslog: From, i-uri, status, digest id, method
  100. modparam("acc", "log_fmt", "fisum")
  101. # -- tm params --
  102. modparam("tm", "fr_timer", 20)
  103. modparam("tm", "fr_inv_timer", 90)
  104. modparam("tm", "wt_timer", 20)
  105. # -- msilo params
  106. modparam("msilo", "registrar", "sip:registrar@SER_HOSTNAME")
  107. # -- enum params --
  108. modparam("enum", "domain_suffix", "e164.arpa.")
  109. # -- multi-domain
  110. modparam("domain", "db_mode", 1)
  111. # NAT features turned off -- smartnat available only in nat-capable release
  112. # We will you flag 6 to mark NATed contacts
  113. modparam("registrar", "nat_flag", NAT_FLAG)
  114. # Enable NAT pinging
  115. modparam("nathelper", "natping_interval", 15)
  116. # Ping only contacts that are known to be behind NAT
  117. modparam("nathelper", "ping_nated_only", 1)
  118. # --------------------- request routing logic -------------------
  119. route {
  120. if (!mf_process_maxfwd_header("10")) {
  121. log("LOG: Too many hops\n");
  122. sl_send_reply("483", "Alas Too Many Hops");
  123. break;
  124. };
  125. if (msg:len >= max_len) {
  126. sl_send_reply("513", "Message too large");
  127. break;
  128. };
  129. # special handling for natted clients; first, nat test is
  130. # executed: it looks for via!=received and RFC1918 addresses
  131. # in Contact (may fail if line-folding used); also,
  132. # the received test should, if complete, should check all
  133. # vias for presence of received
  134. if (nat_uac_test("3")) {
  135. # allow RR-ed requests, as these may indicate that
  136. # a NAT-enabled proxy takes care of it; unless it is
  137. # a REGISTER
  138. if (method == "REGISTER" || !search("^Record-Route:")) {
  139. log("LOG: Someone trying to register from private IP, rewriting\n");
  140. # This will work only for user agents that support symmetric
  141. # communication. We tested quite many of them and majority is
  142. # smart smart enough to be symmetric. In some phones, like
  143. # it takes a configuration option. With Cisco 7960, it is
  144. # called NAT_Enable=Yes, with kphone it is called
  145. # "symmetric media" and "symmetric signaling". (The latter
  146. # not part of public released yet.)
  147. fix_nated_contact(); # Rewrite contact with source IP of signalling
  148. if (method == "INVITE") {
  149. fix_nated_sdp("1"); # Add direction=active to SDP
  150. };
  151. force_rport(); # Add rport parameter to topmost Via
  152. setflag(NAT_FLAG); # Mark as NATed
  153. append_to_reply("P-NATed-Caller: Yes\r\n");
  154. };
  155. };
  156. # anti-spam -- if somene claims to belong to our domain in From,
  157. # challenge him (skip REGISTERs -- we will chalenge them later)
  158. if (search("(From|F):.*@SER_HOST_REGEX")) {
  159. # invites forwarded to other domains, like FWD may cause subsequent
  160. # request to come from there but have iptel in From -> verify
  161. # only INVITEs (ignore FIFO/UAC's requests, i.e. src_ip==fox)
  162. if ((method == "INVITE" || method == "SUBSCRIBE") && !(FROM_MYSELF || FROM_GW)) {
  163. if (!(proxy_authorize("DIGEST_REALM", "subscriber"))) {
  164. proxy_challenge("DIGEST_REALM", "0");
  165. break;
  166. };
  167. # to maintain outside credibility of our proxy, we enforce
  168. # username in From to equal digest username; user with
  169. # "john.doe" id could advertise "bill.gates" in From otherwise;
  170. if (!check_from()) {
  171. log("LOG: From Cheating attempt in INVITE\n");
  172. sl_send_reply("403", "That is ugly -- use From=id next time (OB)");
  173. break;
  174. };
  175. # we better don't consume credentials -- some requests may be
  176. # spiraled through our server (sfo@iptel->7141@iptel) and the
  177. # subsequent iteration may challenge too, for example because of
  178. # iptel claim in From; UACs then give up because they
  179. # already submitted credentials for the given realm
  180. #consume_credentials();
  181. }; # non-REGISTER from other domain
  182. } else if ((method == "INVITE" || method == "SUBSCRIBE" || method=="REGISTER" ) &&
  183. !(uri == myself || uri =~ "TO_GW")) {
  184. # and we serve our gateway too (we RR requests to it, so that
  185. # its address may show up in subsequent requests after loose_route
  186. sl_send_reply("403", "No relaying");
  187. break;
  188. };
  189. # By default we record route everything except REGISTERs
  190. if (!(method=="REGISTER")) record_route();
  191. # if route forces us to forward to some explicit destination, do so
  192. #
  193. # loose_route returns true in case that a request included
  194. # route header fields instructing SER where to relay a request;
  195. # if that is the case, stop script processing and just forward there;
  196. # one could alternatively ignore the return value and treat the
  197. # request as if it was an outbound one; that would not work however
  198. # with broken UAs which strip RR parameters from Route. (What happens
  199. # is that with two RR /tcp2udp, spirals, etc./ and stripped parameters,
  200. # SER a) rewrites r-uri with RR1 b) matches uri==myself against RR1
  201. # c) applies mistakenly user-lookup to RR1 in r-uri
  202. if (loose_route()) {
  203. # check if someone has not introduced a pre-loaded INVITE -- if so,
  204. # verify caller's privileges before accepting rr-ing
  205. if ((method=="INVITE" || method=="ACK" || method=="CANCEL") && uri =~ "TO_GW") {
  206. route(PSTN_ROUTE); # Forward to PSTN gateway
  207. } else {
  208. append_hf("P-hint: rr-enforced\r\n");
  209. # account all BYEs
  210. if (method=="BYE") setflag(ACC_FLAG);
  211. route(NAT_ROUTE); # Generic forward
  212. };
  213. break;
  214. };
  215. # ------- check for requests targeted out of our domain... -------
  216. if (!(uri == myself || uri =~ "TO_GW")) {
  217. # ... and we serve our gateway too (we RR requests to it, so that
  218. # its address may show up in subsequent requests after
  219. # rewriteFromRoute
  220. append_hf("P-hint: OUTBOUND\r\n");
  221. route(NAT_ROUTE);
  222. break;
  223. };
  224. # ------- now, the request is for sure for our domain -----------
  225. # registers always MUST be authenticated to
  226. # avoid stealing incoming calls
  227. if (method == "REGISTER") {
  228. /*
  229. if (!allow_register("register.allow", "register.deny")) {
  230. log(1, "LOG: alert: Forbidden IP in Contact\n");
  231. sl_send_reply("403", "Forbidden");
  232. break;
  233. };
  234. */
  235. # prohibit attempts to grab someone else's To address
  236. # using valid credentials;
  237. if (!www_authorize("DIGEST_REALM", "subscriber")) {
  238. # challenge if none or invalid credentials
  239. www_challenge("DIGEST_REALM", "0");
  240. break;
  241. };
  242. if (!check_to()) {
  243. log("LOG: To Cheating attempt\n");
  244. sl_send_reply("403", "That is ugly -- use To=id in REGISTERs");
  245. break;
  246. };
  247. # it is an authenticated request, update Contact database now
  248. if (!save("location")) {
  249. sl_reply_error();
  250. };
  251. m_dump();
  252. break;
  253. };
  254. # some UACs might be fooled by Contacts our UACs generate to make MSN
  255. # happy (web-im, e.g.) -- tell its urneachable
  256. if (uri =~ "sip:daemon@") {
  257. sl_send_reply("410", "Daemon is gone");
  258. break;
  259. };
  260. # aliases
  261. # note: through a temporary error in provisioning interface, there
  262. # are now aliases 905xx ... they take precedence overy any PSTN numbers
  263. # as they are resolved first
  264. lookup("aliases");
  265. # check again, if it is still for our domain after aliases
  266. if (!(uri == myself || uri =~ "TO_GW")) {
  267. append_hf("P-hint: ALIASED-OUTBOUND\r\n");
  268. route(NAT_ROUTE);
  269. break;
  270. };
  271. # Remove leading + if it is a number begining with +
  272. if (uri =~ "^[a-zA-Z]+:\+[0-9]+@") {
  273. strip(1);
  274. prefix("00");
  275. };
  276. if (!does_uri_exist()) {
  277. # Try numeric destinations through the gateway
  278. if (uri =~ "^[a-zA-Z]+:[0-9]+@") {
  279. route(PSTN_ROUTE);
  280. } else {
  281. sl_send_reply("604", "Does Not Exist Anywhere");
  282. };
  283. break;
  284. };
  285. # does the user wish redirection on no availability? (i.e., is he
  286. # in the voicemail group?) -- determine it now and store it in
  287. # flag 4, before we rewrite the flag using UsrLoc
  288. if (is_user_in("Request-URI", "voicemail")) {
  289. setflag(VM_FLAG);
  290. };
  291. # native SIP destinations are handled using our USRLOC DB
  292. if (!lookup("location")) {
  293. # handle user which was not found
  294. route(VOICEMAIL_ROUTE);
  295. break;
  296. };
  297. # check whether some inventive user has uploaded gateway
  298. # contacts to UsrLoc to bypass our authorization logic
  299. if (uri =~ "TO_GW") {
  300. log(1, "LOG: Weird! Gateway address in UsrLoc!\n");
  301. route(PSTN_ROUTE);
  302. break;
  303. };
  304. # if user is on-line and is in voicemail group, enable redirection
  305. /* no voicemail currently activated
  306. if (method == "INVITE" && isflagset(VM_FLAG)) {
  307. t_on_failure(_1_FAILURE); # failure_route() not defined
  308. };
  309. */
  310. # ... and also report on missed calls ... note that reporting
  311. # on missed calls is mutually exclusive with silent C timer
  312. setflag(MISSED_FLAG);
  313. # we now know we may, we know where, let it go out now!
  314. append_hf("P-hint: USRLOC\r\n");
  315. route(NAT_ROUTE);
  316. }
  317. #
  318. # Forcing media relay if necesarry
  319. #
  320. route[NAT_ROUTE] {
  321. if (uri=~"[@:](192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)" && !search("^Route:")) {
  322. sl_send_reply("479", "We don't forward to private IP addresses");
  323. break;
  324. };
  325. if (isflagset(NAT_FLAG)) {
  326. if (!is_present_hf("P-RTP-Proxy")) {
  327. force_rtp_proxy();
  328. append_hf("P-RTP-Proxy: YES\r\n");
  329. };
  330. append_hf("P-NATed-Calee: Yes\r\n");
  331. };
  332. # nat processing of replies; apply to all transactions (for example,
  333. # re-INVITEs from public to private UA are hard to identify as
  334. # natted at the moment of request processing); look at replies
  335. t_on_reply(NAT_REPLY);
  336. if (!t_relay()) {
  337. sl_reply_error();
  338. break;
  339. };
  340. }
  341. onreply_route[NAT_REPLY] {
  342. # natted transaction ?
  343. if (isflagset(NAT_FLAG) && status =~ "(183)|2[0-9][0-9]") {
  344. fix_nated_contact();
  345. force_rtp_proxy();
  346. # otherwise, is it a transaction behind a NAT and we did not
  347. # know at time of request processing? (RFC1918 contacts)
  348. } else if (nat_uac_test("1")) {
  349. fix_nated_contact();
  350. };
  351. # keep Cisco gateway sending keep-alives
  352. if (isflagset(7) && status=~"2[0-9][0-9]") { # flag(7) is mentioned NAT_FLAG ??
  353. remove_hf("Session-Expires");
  354. append_hf("Session-Expires: 60;refresher=UAC\r\n");
  355. fix_nated_sdp("1");
  356. };
  357. }
  358. #
  359. # logic for calls to the PSTN
  360. #
  361. route[PSTN_ROUTE] {
  362. # discard non-PSTN methods
  363. if (!(method == "INVITE" || method == "ACK" || method == "CANCEL" || method == "OPTIONS" || method == "BYE")) {
  364. sl_send_reply("500", "only VoIP methods accepted for GW");
  365. break;
  366. };
  367. # turn accounting on
  368. setflag(ACC_FLAG);
  369. # continue with requests to PSTN gateway ...
  370. # no authentication needed if the destination is on our free-pstn
  371. # list or if the caller is the digest-less gateway
  372. #
  373. # apply ACLs only to INVITEs -- we don't need to protect other
  374. # requests, as they don't imply charges; also it could cause troubles
  375. # when a call comes in via PSTN and goes to a party that can't
  376. # authenticate (voicemail, other domain) -- BYEs would fail then
  377. if (method == "INVITE") {
  378. if (!is_user_in("Request-URI", "free-pstn")) {
  379. if (!proxy_authorize("DIGEST_REALM", "subscriber")) {
  380. proxy_challenge("DIGEST_REALM", "0");
  381. break;
  382. };
  383. # let's check from=id ... avoids accounting confusion
  384. if (!check_from()) {
  385. log("LOG: From Cheating attempt\n");
  386. sl_send_reply("403", "That is ugly -- use From=id next time (gw)");
  387. break;
  388. };
  389. } else {
  390. # Allow free-pstn destinations without any checks
  391. route(PSTN2_ROUTE);
  392. break;
  393. };
  394. if (uri =~ "^sip:00[1-9][0-9]+@") {
  395. if (!is_user_in("credentials", "int")) {
  396. sl_send_reply("403", "International numbers not allowed");
  397. break;
  398. };
  399. route(PSTN2_ROUTE);
  400. } else {
  401. sl_send_reply("403", "Invalid Number");
  402. break;
  403. };
  404. }; # authorized PSTN
  405. break;
  406. }
  407. route[PSTN2_ROUTE] {
  408. rewritehostport("GW_IP_1:5060");
  409. consume_credentials();
  410. append_hf("P-Hint: GATEWAY\r\n");
  411. # Try alternative gateway on failure
  412. t_on_failure(PSTN_FAILURE);
  413. # Our PSTN gateway is symmetric and can handle direction=active flag
  414. # properly, therefore we don't have to use RTP proxy
  415. t_relay();
  416. }
  417. failure_route[PSTN_FAILURE] {
  418. rewritehostport("GW_IP_2:5060");
  419. append_branch();
  420. t_relay();
  421. }
  422. # ------------- handling of unavailable user ------------------
  423. route[VOICEMAIL_ROUTE] {
  424. # message store
  425. if (method == "MESSAGE") {
  426. if (!t_newtran()) {
  427. sl_reply_error();
  428. break;
  429. };
  430. if (m_store("0")) {
  431. t_reply("202", "Accepted for Later Delivery");
  432. break;
  433. };
  434. t_reply("503", "Service Unavailable");
  435. break;
  436. };
  437. # non-Voip -- just send "off-line"
  438. if (!(method == "INVITE" || method == "ACK" || method == "CANCEL")) {
  439. sl_send_reply("404", "Not Found");
  440. break;
  441. };
  442. if (t_newtran()) {
  443. if (method == "ACK") {
  444. log(1, "CAUTION: strange thing: ACK passed t_newtran\n");
  445. break;
  446. };
  447. t_reply("404", "Not Found");
  448. };
  449. # we account missed incoming calls; previous statteful processing
  450. # guarantees that retransmissions are not accounted
  451. if (method == "INVITE") {
  452. acc_log_request("404 missed call\n");
  453. acc_db_request("404 missed call", "missed_calls");
  454. };
  455. }