1 ### m4 macros to make the configuration easier
5 define(`SER_IP', `192.168.0.1')
6 define(`SER_HOSTNAME', `foo.bar')
8 define(`GW_IP_1', `192.168.0.2')
9 define(`GW_IP_2', `192.168.0.3')
11 declare(flags, ACC_FLAG, MISSED_FLAG, VM_FLAG, NAT_FLAG)
12 declare(route, PSTN_ROUTE, NAT_ROUTE, VOICEMAIL_ROUTE, PSTN2_ROUTE)
13 declare(onreply, NAT_REPLY)
14 declare(failure, PSTN_FAILURE, _1_FAILURE)
16 ### End of m4 macro section
21 # sip-router.cfg m4 template
25 # Set the following in your CISCO PSTN gateway:
27 # nat symmetric role passive
28 # nat symmetric check-media-src
33 fifo="/tmp/sip-router_fifo"
35 # uncomment to enter testing mode
40 fifo="/tmp/sip-router_fifox"
44 memlog=4 # memlog set high (>debug) -- no final time-consuming memory reports on exit
53 # if changing fifo mode to a more restrictive value, put
54 # decimal value in there, e.g. dec(rw|rw|rw)=dec(666)=438
57 loadmodule "/usr/local/lib/sip-router/modules/tm.so"
58 loadmodule "/usr/local/lib/sip-router/modules/sl.so"
59 loadmodule "/usr/local/lib/sip-router/modules/acc.so"
60 loadmodule "/usr/local/lib/sip-router/modules/rr.so"
61 loadmodule "/usr/local/lib/sip-router/modules/maxfwd.so"
62 loadmodule "/usr/local/lib/sip-router/modules/mysql.so"
63 loadmodule "/usr/local/lib/sip-router/modules/usrloc.so"
64 loadmodule "/usr/local/lib/sip-router/modules/registrar.so"
65 loadmodule "/usr/local/lib/sip-router/modules/auth.so"
66 loadmodule "/usr/local/lib/sip-router/modules/auth_db.so"
67 loadmodule "/usr/local/lib/sip-router/modules/textops.so"
68 loadmodule "/usr/local/lib/sip-router/modules/uri.so"
69 loadmodule "/usr/local/lib/sip-router/modules/group.so"
70 loadmodule "/usr/local/lib/sip-router/modules/msilo.so"
71 loadmodule "/usr/local/lib/sip-router/modules/nathelper.so"
72 loadmodule "/usr/local/lib/sip-router/modules/enum.so"
73 loadmodule "/usr/local/lib/sip-router/modules/domain.so"
74 #loadmodule "/usr/local/lib/sip-router/modules/permissions.so"
76 modparam("usrloc|acc|auth_db|group|msilo", "db_url", "sql://sip-router:heslo@localhost/sip-router")
79 /* 0 -- dont use mysql, 1 -- write_through, 2--write_back */
80 modparam("usrloc", "db_mode", 2)
81 modparam("usrloc", "timer_interval", 10)
84 modparam("auth_db", "calculate_ha1", yes)
85 modparam("auth_db", "plain_password_column", "password")
86 #modparam("auth_db", "use_rpid", 1)
87 modparam("auth", "nonce_expire", 300)
88 modparam("auth", "rpid_prefix", "<sip:")
89 modparam("auth", "rpid_suffix", "@GW_IP_3>;party=calling;id-type=subscriber;screen=yes;privacy=off")
92 # add value to ;lr param to make some broken UAs happy
93 modparam("rr", "enable_full_lr", 1)
96 # report ACKs too for sake of completeness -- as we account PSTN
97 # destinations which are RR, ACKs should show up
98 modparam("acc", "report_ack", 1)
99 modparam("acc", "log_level", 1)
100 # if BYE fails (telephone is dead, record-routing broken, etc.), generate
101 # a report nevertheless -- otherwise we would have no STOP event; => 1
102 modparam("acc", "failed_transactions", 1)
104 # that is the flag for which we will account -- don't forget to
105 # set the same one :-)
106 # Usage of flags is as follows:
107 # 1 == should account(all to gateway),
108 # 3 == should report on missed calls (transactions to iptel.org's users),
109 # 4 == destination user wishes to use voicemail
112 modparam("acc", "log_flag", ACC_FLAG)
113 modparam("acc", "db_flag", ACC_FLAG)
114 modparam("acc", "log_missed_flag", MISSED_FLAG)
115 modparam("acc", "db_missed_flag", MISSED_FLAG)
117 # report to syslog: From, i-uri, status, digest id, method
118 modparam("acc", "log_fmt", "fisum")
121 modparam("tm", "fr_timer", 20)
122 modparam("tm", "fr_inv_timer", 90)
123 modparam("tm", "wt_timer", 20)
126 modparam("msilo", "registrar", "sip:registrar@SER_HOSTNAME")
129 modparam("enum", "domain_suffix", "e164.arpa.")
132 modparam("domain", "db_mode", 1)
134 # NAT features turned off -- smartnat available only in nat-capable release
135 # We will you flag 6 to mark NATed contacts
136 modparam("registrar", "nat_flag", NAT_FLAG)
138 modparam("nathelper", "natping_interval", 15)
139 # Ping only contacts that are known to be behind NAT
140 modparam("nathelper", "ping_nated_only", 1)
142 # --------------------- request routing logic -------------------
145 if (!mf_process_maxfwd_header("10")) {
146 log("LOG: Too many hops\n");
147 sl_send_reply("483", "Alas Too Many Hops");
151 if (msg:len >= max_len) {
152 sl_send_reply("513", "Message too large");
156 # special handling for natted clients; first, nat test is
157 # executed: it looks for via!=received and RFC1918 addresses
158 # in Contact (may fail if line-folding used); also,
159 # the received test should, if complete, should check all
160 # vias for presence of received
161 if (nat_uac_test("3")) {
162 # allow RR-ed requests, as these may indicate that
163 # a NAT-enabled proxy takes care of it; unless it is
166 if (method == "REGISTER" || !search("^Record-Route:")) {
167 log("LOG: Someone trying to register from private IP, rewriting\n");
169 # This will work only for user agents that support symmetric
170 # communication. We tested quite many of them and majority is
171 # smart smart enough to be symmetric. In some phones, like
172 # it takes a configuration option. With Cisco 7960, it is
173 # called NAT_Enable=Yes, with kphone it is called
174 # "symmetric media" and "symmetric signaling". (The latter
175 # not part of public released yet.)
177 fix_nated_contact(); # Rewrite contact with source IP of signalling
178 if (method == "INVITE") {
179 fix_nated_sdp("1"); # Add direction=active to SDP
181 force_rport(); # Add rport parameter to topmost Via
182 setflag(NAT_FLAG); # Mark as NATed
184 append_to_reply("P-NATed-Caller: Yes\r\n");
189 # anti-spam -- if somene claims to belong to our domain in From,
190 # challenge him (skip REGISTERs -- we will chalenge them later)
191 if (search("(From|F):.*@SER_HOST_REGEX")) {
192 # invites forwarded to other domains, like FWD may cause subsequent
193 # request to come from there but have iptel in From -> verify
194 # only INVITEs (ignore FIFO/UAC's requests, i.e. src_ip==fox)
195 if ((method == "INVITE" || method == "SUBSCRIBE") && !(FROM_MYSELF || FROM_GW)) {
196 if (!(proxy_authorize("DIGEST_REALM", "subscriber"))) {
197 proxy_challenge("DIGEST_REALM", "0");
201 # to maintain outside credibility of our proxy, we enforce
202 # username in From to equal digest username; user with
203 # "john.doe" id could advertise "bill.gates" in From otherwise;
205 log("LOG: From Cheating attempt in INVITE\n");
206 sl_send_reply("403", "That is ugly -- use From=id next time (OB)");
210 # we better don't consume credentials -- some requests may be
211 # spiraled through our server (sfo@iptel->7141@iptel) and the
212 # subsequent iteration may challenge too, for example because of
213 # iptel claim in From; UACs then give up because they
214 # already submitted credentials for the given realm
215 #consume_credentials();
216 }; # non-REGISTER from other domain
217 } else if ((method == "INVITE" || method == "SUBSCRIBE" || method=="REGISTER" ) &&
218 !(uri == myself || uri =~ "TO_GW")) {
219 # and we serve our gateway too (we RR requests to it, so that
220 # its address may show up in subsequent requests after loose_route
221 sl_send_reply("403", "No relaying");
225 # By default we record route everything except REGISTERs
226 if (!(method=="REGISTER")) record_route();
228 # if route forces us to forward to some explicit destination, do so
230 # loose_route returns true in case that a request included
231 # route header fields instructing SER where to relay a request;
232 # if that is the case, stop script processing and just forward there;
233 # one could alternatively ignore the return value and treat the
234 # request as if it was an outbound one; that would not work however
235 # with broken UAs which strip RR parameters from Route. (What happens
236 # is that with two RR /tcp2udp, spirals, etc./ and stripped parameters,
237 # SER a) rewrites r-uri with RR1 b) matches uri==myself against RR1
238 # c) applies mistakenly user-lookup to RR1 in r-uri
241 # check if someone has not introduced a pre-loaded INVITE -- if so,
242 # verify caller's privileges before accepting rr-ing
243 if ((method=="INVITE" || method=="ACK" || method=="CANCEL") && uri =~ "TO_GW") {
244 route(PSTN_ROUTE); # Forward to PSTN gateway
246 append_hf("P-hint: rr-enforced\r\n");
248 if (method=="BYE") setflag(ACC_FLAG);
249 route(NAT_ROUTE); # Generic forward
254 # ------- check for requests targeted out of our domain... -------
255 if (!(uri == myself || uri =~ "TO_GW")) {
256 # ... and we serve our gateway too (we RR requests to it, so that
257 # its address may show up in subsequent requests after
259 append_hf("P-hint: OUTBOUND\r\n");
265 # ------- now, the request is for sure for our domain -----------
266 # registers always MUST be authenticated to
267 # avoid stealing incoming calls
268 if (method == "REGISTER") {
270 if (!allow_register("register.allow", "register.deny")) {
271 log(1, "LOG: alert: Forbidden IP in Contact\n");
272 sl_send_reply("403", "Forbidden");
277 # prohibit attempts to grab someone else's To address
278 # using valid credentials;
279 if (!www_authorize("DIGEST_REALM", "subscriber")) {
280 # challenge if none or invalid credentials
281 www_challenge("DIGEST_REALM", "0");
286 log("LOG: To Cheating attempt\n");
287 sl_send_reply("403", "That is ugly -- use To=id in REGISTERs");
291 # it is an authenticated request, update Contact database now
292 if (!save("location")) {
300 # some UACs might be fooled by Contacts our UACs generate to make MSN
301 # happy (web-im, e.g.) -- tell its urneachable
302 if (uri =~ "sip:daemon@") {
303 sl_send_reply("410", "Daemon is gone");
308 # note: through a temporary error in provisioning interface, there
309 # are now aliases 905xx ... they take precedence overy any PSTN numbers
310 # as they are resolved first
313 # check again, if it is still for our domain after aliases
314 if (!(uri == myself || uri =~ "TO_GW")) {
315 append_hf("P-hint: ALIASED-OUTBOUND\r\n");
320 # Remove leading + if it is a number begining with +
321 if (uri =~ "^[a-zA-Z]+:\+[0-9]+@") {
326 if (!does_uri_exist()) {
327 # Try numeric destinations through the gateway
328 if (uri =~ "^[a-zA-Z]+:[0-9]+@") {
331 sl_send_reply("604", "Does Not Exist Anywhere");
336 # does the user wish redirection on no availability? (i.e., is he
337 # in the voicemail group?) -- determine it now and store it in
338 # flag 4, before we rewrite the flag using UsrLoc
339 if (is_user_in("Request-URI", "voicemail")) {
343 # native SIP destinations are handled using our USRLOC DB
344 if (!lookup("location")) {
345 # handle user which was not found
346 route(VOICEMAIL_ROUTE);
350 # check whether some inventive user has uploaded gateway
351 # contacts to UsrLoc to bypass our authorization logic
352 if (uri =~ "TO_GW") {
353 log(1, "LOG: Weird! Gateway address in UsrLoc!\n");
358 # if user is on-line and is in voicemail group, enable redirection
359 /* no voicemail currently activated
360 if (method == "INVITE" && isflagset(VM_FLAG)) {
361 t_on_failure(_1_FAILURE); # failure_route() not defined
365 # ... and also report on missed calls ... note that reporting
366 # on missed calls is mutually exclusive with silent C timer
367 setflag(MISSED_FLAG);
369 # we now know we may, we know where, let it go out now!
370 append_hf("P-hint: USRLOC\r\n");
375 # Forcing media relay if necesarry
378 if (uri=~"[@:](192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)" && !search("^Route:")) {
379 sl_send_reply("479", "We don't forward to private IP addresses");
382 if (isflagset(NAT_FLAG)) {
383 if (!is_present_hf("P-RTP-Proxy")) {
385 append_hf("P-RTP-Proxy: YES\r\n");
387 append_hf("P-NATed-Calee: Yes\r\n");
390 # nat processing of replies; apply to all transactions (for example,
391 # re-INVITEs from public to private UA are hard to identify as
392 # natted at the moment of request processing); look at replies
394 t_on_reply(NAT_REPLY);
403 onreply_route[NAT_REPLY] {
404 # natted transaction ?
405 if (isflagset(NAT_FLAG) && status =~ "(183)|2[0-9][0-9]") {
408 # otherwise, is it a transaction behind a NAT and we did not
409 # know at time of request processing? (RFC1918 contacts)
410 } else if (nat_uac_test("1")) {
414 # keep Cisco gateway sending keep-alives
415 if (isflagset(7) && status=~"2[0-9][0-9]") { # flag(7) is mentioned NAT_FLAG ??
416 remove_hf("Session-Expires");
417 append_hf("Session-Expires: 60;refresher=UAC\r\n");
424 # logic for calls to the PSTN
428 # discard non-PSTN methods
429 if (!(method == "INVITE" || method == "ACK" || method == "CANCEL" || method == "OPTIONS" || method == "BYE")) {
430 sl_send_reply("500", "only VoIP methods accepted for GW");
437 # continue with requests to PSTN gateway ...
439 # no authentication needed if the destination is on our free-pstn
440 # list or if the caller is the digest-less gateway
442 # apply ACLs only to INVITEs -- we don't need to protect other
443 # requests, as they don't imply charges; also it could cause troubles
444 # when a call comes in via PSTN and goes to a party that can't
445 # authenticate (voicemail, other domain) -- BYEs would fail then
446 if (method == "INVITE") {
447 if (!is_user_in("Request-URI", "free-pstn")) {
448 if (!proxy_authorize("DIGEST_REALM", "subscriber")) {
449 proxy_challenge("DIGEST_REALM", "0");
453 # let's check from=id ... avoids accounting confusion
455 log("LOG: From Cheating attempt\n");
456 sl_send_reply("403", "That is ugly -- use From=id next time (gw)");
460 # Allow free-pstn destinations without any checks
465 if (uri =~ "^sip:00[1-9][0-9]+@") {
466 if (!is_user_in("credentials", "int")) {
467 sl_send_reply("403", "International numbers not allowed");
472 sl_send_reply("403", "Invalid Number");
480 rewritehostport("GW_IP_1:5060");
481 consume_credentials();
482 append_hf("P-Hint: GATEWAY\r\n");
484 # Try alternative gateway on failure
485 t_on_failure(PSTN_FAILURE);
486 # Our PSTN gateway is symmetric and can handle direction=active flag
487 # properly, therefore we don't have to use RTP proxy
493 failure_route[PSTN_FAILURE] {
494 rewritehostport("GW_IP_2:5060");
500 # ------------- handling of unavailable user ------------------
501 route[VOICEMAIL_ROUTE] {
503 if (method == "MESSAGE") {
510 t_reply("202", "Accepted for Later Delivery");
514 t_reply("503", "Service Unavailable");
518 # non-Voip -- just send "off-line"
519 if (!(method == "INVITE" || method == "ACK" || method == "CANCEL")) {
520 sl_send_reply("404", "Not Found");
525 if (method == "ACK") {
526 log(1, "CAUTION: strange thing: ACK passed t_newtran\n");
530 t_reply("404", "Not Found");
533 # we account missed incoming calls; previous statteful processing
534 # guarantees that retransmissions are not accounted
535 if (method == "INVITE") {
536 acc_log_request("404 missed call\n");
537 acc_db_request("404 missed call", "missed_calls");