Debian Codegraph @

src/modules/outbound/README

raw
1 Outbound Module
2
3 Peter Dunkley
4
5 Crocodile RCS Ltd
6
7 Copyright © 2012-2013 Crocodile RCS Ltd
8 __________________________________________________________________
9
10 Table of Contents
11
12 1. Admin Guide
13
14 1. Overview
15
16 1.1. Conditions
17 1.2. Edge Proxy Keep-Alives (STUN)
18 1.3. Flow Timer
19
20 2. Dependencies
21
22 2.1. Kamailio Modules
23 2.2. External Libraries or Applications
24
25 3. Functions
26
27 3.1. check_flow_token()
28
29 4. Parameters
30
31 4.1. force_outbound_flag (integer)
32 4.2. force_no_outbound_flag (integer)
33 4.3. flow_token_secret (string)
34
35 List of Examples
36
37 1.1. Edge Proxy Configuration
38 1.2. Registrar Configuration
39 1.3. check_flow_token usage
40 1.4. Set force_outbound_flag parameter
41 1.5. Set force_no_outbound_flag parameter
42 1.6. Set flow_token_secret parameter
43
44 Chapter 1. Admin Guide
45
46 Table of Contents
47
48 1. Overview
49
50 1.1. Conditions
51 1.2. Edge Proxy Keep-Alives (STUN)
52 1.3. Flow Timer
53
54 2. Dependencies
55
56 2.1. Kamailio Modules
57 2.2. External Libraries or Applications
58
59 3. Functions
60
61 3.1. check_flow_token()
62
63 4. Parameters
64
65 4.1. force_outbound_flag (integer)
66 4.2. force_no_outbound_flag (integer)
67 4.3. flow_token_secret (string)
68
69 1. Overview
70
71 1.1. Conditions
72 1.2. Edge Proxy Keep-Alives (STUN)
73 1.3. Flow Timer
74
75 This module provides C-API functions to enable Kamailio to be used as
76 an outbound Edge Proxy (see RFC5626 section 5).
77
78 The path and rr modules will bind to this module if it is loaded before
79 they are.
80
81 1.1. Conditions
82
83 The module analyses SIP requests so that other module (e.g. path, rr)
84 can determine whether to apply RFC5626 processing. This section applies
85 if outbound processing is not forced by the force_outbound_flag flag,
86 or disabled with force_no_outbound_flag.
87
88 REGISTER request:
89 * single Via: header (first hop) and Contact: header has ;reg-id
90 parameter
91
92 Non-REGISTER requests if either of the following conditions is true:
93 * top Route header URI is myself and has ;ob parameter
94 * Supported: outbound... header, single Via: header, and Contact:
95 header has ;ob parameter
96
97 1.2. Edge Proxy Keep-Alives (STUN)
98
99 Outbound Edge Proxies MUST support STUN NAT keep-alives on their SIP
100 UDP ports. Kamailio supports this though the “stun” module.
101
102 1.3. Flow Timer
103
104 The maximum interval at which a User Agent must send a keep-alive may
105 be specified by the Registrar using the Flow-Timer: header in 2xx
106 responses to REGISTERs.
107
108 When using TCP or TLS as the SIP transport care should be taken to set
109 the “tcp_connection_lifetime” on the Edge Proxy to a value slightly
110 larger than the interval the Registrar is using for flow timer. Setting
111 “tcp_connection_lifetime” to less than the interval could cause
112 connections to be lost, and setting it to a value much larger than the
113 interval will keep connections open far longer than is required (which
114 is wasteful).
115
116 Application-layer keep-alives are optional when the underlying
117 transport already has a keep-alive mechanism. The WebSocket transport
118 has a transport-layer keep-alive. When using the WebSocket transport
119 the “keepalive_timeout” should be set to a value a little greater than
120 the Registrar flow timer interval and a little less than the
121 “tcp_connection_lifetime”.
122
123 Example 1.1. Edge Proxy Configuration
124 #!KAMAILIO
125 #
126 # Edge proxy configuration
127 #
128
129 #!subst "/REGISTRAR_IP/192.168.122.3/"
130 #!subst "/REGISTRAR_PORT/5060/"
131 #!substdef "/FLOW_TIMER/20/"
132
133 ####### Global Parameters #########
134
135 debug=2
136 log_stderror=no
137 log_facility=LOG_LOCAL0
138 fork=yes
139 children=4
140 alias="example.com"
141 mpath="/usr/lib64/kamailio/modules"
142 tcp_connection_lifetime=30 # FLOW_TIMER + 10
143 force_rport=yes
144
145
146 ####### Modules Section ########
147
148 loadmodule "tm.so"
149 loadmodule "sl.so"
150 loadmodule "outbound.so"
151 loadmodule "rr.so"
152 loadmodule "path.so"
153 loadmodule "pv.so"
154 loadmodule "maxfwd.so"
155 loadmodule "xlog.so"
156 loadmodule "sanity.so"
157 loadmodule "ctl.so"
158 loadmodule "textops.so"
159 loadmodule "siputils.so"
160 loadmodule "stun.so"
161
162 # ----------------- setting module-specific parameters ---------------
163
164 # ----- tm params -----
165 modparam("tm", "failure_reply_mode", 3)
166
167 # ----- rr params -----
168 modparam("rr", "append_fromtag", 0)
169
170
171 ####### Routing Logic ########
172
173 request_route {
174 route(REQINIT);
175
176 if (is_method("CANCEL")) {
177 if (t_check_trans()) {
178 route(RELAY);
179 }
180 exit;
181 }
182
183 route(WITHINDLG);
184
185 t_check_trans();
186
187 if (is_method("REGISTER")) {
188 remove_hf("Route");
189 add_path();
190 $du = "sip:REGISTRAR_IP:REGISTRAR_PORT";
191 } else {
192 if (is_method("INVITE|SUBSCRIBE"))
193 record_route();
194
195 if (@via[2] == "") {
196 # From client so route to registrar...
197
198 if ($rU == $null) {
199 sl_send_reply("484", "Address Incomplete");
200 exit;
201 }
202 remove_hf("Route");
203 $du = "sip:REGISTRAR_IP:REGISTRAR_PORT";
204 } else {
205 # From registrar so route using "Route:" headers...
206
207 if (!loose_route()) {
208 switch($rc) {
209 case -2:
210 sl_send_reply("403", "Forbidden");
211 exit;
212 default:
213 xlog("L_ERR", "in request_route\n");
214 sl_reply_error();
215 exit;
216 }
217 }
218
219 t_on_failure("FAIL_OUTBOUND");
220 }
221 }
222
223 route(RELAY);
224 }
225
226 route[RELAY] {
227 if (!t_relay()) {
228 sl_reply_error();
229 }
230 exit;
231 }
232
233 route[REQINIT] {
234 if (!mf_process_maxfwd_header("10")) {
235 sl_send_reply("483","Too Many Hops");
236 exit;
237 }
238
239 if(!sanity_check("1511", "7"))
240 {
241 xlog("Malformed SIP message from $si:$sp\n");
242 exit;
243 }
244 }
245
246 route[WITHINDLG] {
247 if (has_totag()) {
248 if (!loose_route()) {
249 switch($rc) {
250 case -2:
251 sl_send_reply("403", "Forbidden");
252 exit;
253 default:
254 if (is_method("ACK")) {
255 if ( t_check_trans() ) {
256 route(RELAY);
257 exit;
258 } else {
259 exit;
260 }
261 }
262 sl_send_reply("404","Not Found");
263 }
264 } else {
265 if (is_method("NOTIFY")) {
266 record_route();
267 }
268 route(RELAY);
269 }
270 exit;
271 }
272 }
273
274 onreply_route {
275 if (!t_check_trans()) {
276 drop;
277 }
278
279 if ($rm == "REGISTER" && $rs >= 200 && $rs <= 299) {
280 remove_hf("Flow-Timer");
281 if ($(hdr(Require)[*])=~"outbound")
282 insert_hf("Flow-Timer: FLOW_TIMER\r\n", "Call-ID");
283 }
284 }
285
286 failure_route[FAIL_OUTBOUND] {
287 if (t_branch_timeout() && !t_branch_replied()) {
288 send_reply("430", "Flow Failed");
289 }
290 }
291
292 Example 1.2. Registrar Configuration
293 MAILIO
294 #
295 # Registrar configuration
296 #
297
298
299 ####### Global Parameters #########
300
301 debug=2
302 log_stderror=no
303 log_facility=LOG_LOCAL0
304 fork=yes
305 children=4
306 alias="example.com"
307 mpath="/usr/lib64/kamailio/modules"
308
309
310 ####### Modules Section ########
311
312 loadmodule "tm.so"
313 loadmodule "tmx.so"
314 loadmodule "sl.so"
315 loadmodule "rr.so"
316 loadmodule "pv.so"
317 loadmodule "maxfwd.so"
318 loadmodule "xlog.so"
319 loadmodule "sanity.so"
320 loadmodule "ctl.so"
321 loadmodule "textops.so"
322 loadmodule "siputils.so"
323 loadmodule "usrloc.so"
324 loadmodule "registrar.so"
325
326 # ----------------- setting module-specific parameters ---------------
327
328
329 # ----- tm params -----
330 modparam("tm", "failure_reply_mode", 3)
331 modparam("tm", "restart_fr_on_each_reply", 0)
332 modparam("tm", "contact_flows_avp", "tm_contact_flows")
333 modparam("tm", "contacts_avp", "tm_contacts")
334
335 # ----- rr params -----
336 modparam("rr", "append_fromtag", 0)
337
338 # ----- registrar params -----
339 modparam("registrar", "use_path", 1)
340 modparam("registrar", "gruu_enabled", 1)
341 modparam("registrar", "outbound_mode", 1)
342
343
344 ####### Routing Logic ########
345
346 request_route {
347 route(REQINIT);
348
349 if (is_method("CANCEL")) {
350 if (t_check_trans()) {
351 route(RELAY);
352 }
353 exit;
354 }
355
356 route(WITHINDLG);
357
358 t_check_trans();
359
360 remove_hf("Route");
361 if (is_method("INVITE|SUBSCRIBE"))
362 record_route();
363
364 route(REGISTRAR);
365
366 if ($rU==$null) {
367 xlog("L_INFO", "Address Incomplete\n");
368 send_reply("484","Address Incomplete");
369 exit;
370 }
371
372 route(LOCATION);
373 }
374
375
376 route[RELAY] {
377 if (!t_relay()) {
378 xlog("L_ERR", "t_relay() failed\n");
379 sl_reply_error();
380 }
381 exit;
382 }
383
384 route[REQINIT] {
385 if (!mf_process_maxfwd_header("10")) {
386 xlog("L_INFO", "Too Many Hops\n");
387 send_reply("483","Too Many Hops");
388 exit;
389 }
390
391 if(!sanity_check("1511", "7"))
392 {
393 xlog("Malformed SIP message from $si:$sp\n");
394 exit;
395 }
396 }
397
398 route[WITHINDLG] {
399 if (has_totag()) {
400 if (loose_route()) {
401 if (is_method("NOTIFY")) {
402 record_route();
403 }
404 route(RELAY);
405 } else {
406 if (is_method("ACK")) {
407 if (t_check_trans()) {
408 route(RELAY);
409 exit;
410 } else {
411 exit;
412 }
413 }
414 xlog("L_INFO", "Not Found");
415 send_reply("404","Not Found");
416 }
417 exit;
418 }
419 }
420
421 route[REGISTRAR] {
422 if (is_method("REGISTER"))
423 {
424 if (!save("location")) {
425 xlog("L_ERR", "Unable to save location\n");
426 sl_reply_error();
427 }
428 exit;
429 }
430 }
431
432 route[LOCATION] {
433 if (!lookup("location")) {
434 $var(rc) = $rc;
435 t_newtran();
436 switch ($var(rc)) {
437 case -1:
438 case -3:
439 send_reply("404", "Not Found");
440 exit;
441 case -2:
442 send_reply("405", "Method Not Allowed");
443 exit;
444 }
445 }
446
447 if (!t_load_contacts() || !t_next_contacts()) {
448 xlog("L_ERR", "t_(load|next)_contacts() failed\n");
449 sl_reply_error();
450 exit;
451 }
452
453 t_on_failure("FAIL_TRANSACTION");
454 t_on_branch_failure("FAIL-BRANCH");
455 route(RELAY);
456 exit;
457 }
458
459 onreply_route {
460 if (!t_check_trans()) {
461 drop;
462 }
463 }
464
465 failure_route[FAIL_TRANSACTION] {
466 if (!t_check_status("6[0-9][0-9]")) {
467 if (t_next_contacts()) {
468 t_relay();
469 exit;
470 }
471 }
472
473 if (t_check_status("430")) {
474 t_reply("480", "Temporarily Unavailable");
475 exit;
476 }
477 }
478
479 event_route[tm:branch-failure:FAIL-BRANCH] {
480 if (t_check_status("403|430")
481 || (t_branch_timeout() && !t_branch_replied())) {
482 unregister("location", "$tu", "$T_reply_ruid");
483
484 if (t_next_contact_flow()) {
485 t_on_branch_failure("FAIL-BRANCH");
486 t_relay();
487 }
488 }
489 }
490
491 2. Dependencies
492
493 2.1. Kamailio Modules
494 2.2. External Libraries or Applications
495
496 2.1. Kamailio Modules
497
498 The following modules must be loaded before this module:
499 * None
500
501 The following modules are required to make proper use of this module:
502 * stun.
503
504 2.2. External Libraries or Applications
505
506 The following libraries must be installed before running Kamailio with
507 this module loaded:
508 * OpenSSL.
509
510 3. Functions
511
512 3.1. check_flow_token()
513
514 3.1. check_flow_token()
515
516 This function search flow token for the current message and make
517 checks:
518 1. headers can be parsed;
519 2. Route header can be parsed;
520 3. top route header is myself;
521 4. flow token present in the Route header;
522 5. flow token can be decoded;
523 6. TCP/TLS or WS/WSS connection present;
524 7. flow token is not expired;
525
526 Return codes:
527 1. 1 - flow token is valid and can be used to route message to the
528 next hop
529 2. -1 - no Route header present
530 3. -2 - host in the first Route value is not current host
531 4. -3 - no flow token present
532 5. -4 - cannot decode flow token
533 6. -5 - connection associated with this token does not exist
534 7. -6 - flow token expired
535 8. -7 - cannot parse Route header
536 9. -8 - cannot parse headers
537
538 Example 1.3. check_flow_token usage
539 ...
540 check_flow_token();
541 switch $rc {
542 case -8:
543 xlog("cannot parse headers\n");
544 sl_send_reply("403", "Forbidden");
545 break;
546 case -7:
547 xlog("cannot parse Route header\n");
548 sl_send_reply("403", "Forbidden");
549 break;
550 case -6:
551 xlog("expired flow token\n");
552 sl_send_reply("430", "Flow Failed");
553 break;
554 case -5:
555 xlog("no TCP conection\n");
556 sl_send_reply("430", "Flow Failed");
557 break;
558 case -4:
559 xlog("cannot decode flowtoken\n");
560 sl_send_reply("403", "Forbidden");
561 break;
562 case -3:
563 xlog("no flow token\n");
564 loose_route();
565 break;
566 case -2:
567 xlog("Route is not my self\n");
568 sl_send_reply("403", "Forbidden");
569 break;
570 case -1:
571 xlog("no Route header\n");
572 break;
573 case 1:
574 xlog("correct flow token\n");
575 loose_route();
576 break;
577 default:
578 xlog("not supported return code: $rc\n");
579 sl_send_reply("403", "Forbidden");
580 break;
581 }
582 ...
583
584 4. Parameters
585
586 4.1. force_outbound_flag (integer)
587 4.2. force_no_outbound_flag (integer)
588 4.3. flow_token_secret (string)
589
590 4.1. force_outbound_flag (integer)
591
592 A flag which, if set for a request, will force path and rr to add flow
593 tokens to Path: and Record-Route: headers regardless of the request
594 contents.
595
596 Default value is -1.
597
598 Example 1.4. Set force_outbound_flag parameter
599 ...
600 modparam("outbound", "force_outbound_flag", 1)
601 ...
602
603 4.2. force_no_outbound_flag (integer)
604
605 A flag which, if set for a request, will force path and rr not to add
606 flow tokens to Path: and Record-Route: headers regardless of the
607 request contents.
608
609 Default value is -1.
610
611 Example 1.5. Set force_no_outbound_flag parameter
612 ...
613 modparam("outbound", "force_no_outbound_flag", 2)
614 ...
615
616 4.3. flow_token_secret (string)
617
618 Secret phrase used to calculate the outbound key value used for flow
619 tokens validation. Allows to set persistent outbound key.
620
621 If not specified, outbound will use randomly generated outbound key
622
623 Example 1.6. Set flow_token_secret parameter
624 ...
625 modparam("outbound", "flow_token_secret", "johndoessecretphrase")
626 ...