README 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. The Iptrtpproxy module
  2. Tomas Mandys
  3. Iptel.org
  4. Copyright © 2007 Tomas Mandys
  5. __________________________________________________________________
  6. Table of Contents
  7. 1. Admin Guide
  8. 1. Overview
  9. 2. Known limitations
  10. 3. Dependencies
  11. 4. Parameters
  12. 4.1. config (string)
  13. 4.2. switchboard (string)
  14. 4.3. rpc_heartbeat_timeout (int)
  15. 4.4. hostname (string)
  16. 4.5. declare_codec (string)
  17. 4.6. codec_set (string)
  18. 5. Functions
  19. 5.1. iptrtpproxy_alloc(gate_a_to_b [, existing_sess_ids])
  20. 5.2. iptrtpproxy_update(gate_a_to_b, session_ids)
  21. 5.3. iptrtpproxy_adjust_timeout(gate_a_to_b, session_ids)
  22. 5.4. iptrtpproxy_delete(session_ids)
  23. 5.5. iptrtpproxy_authorize_media()
  24. 5.6. iptrtpproxy_set_param(param, value)
  25. 5.7.
  26. iptrtpproxy_set_param("(aggregation/switchboard)_by_sip_
  27. ip_(a/b)", sip_ip)
  28. 5.8. iptrtpproxy_set_param("protected_session_ids",
  29. sess_ids)
  30. 5.9. iptrtpproxy_set_param("o_name", value)
  31. 5.10. iptrtpproxy_set_param("o_addr", value)
  32. 5.11. iptrtpproxy_set_param("codec_set", value)
  33. 5.12. iptrtpproxy_set_param("remove_codec_mask", value)
  34. 6. Selects
  35. 6.1. @iptrtpproxy.session_ids
  36. 6.2. @iptrtpproxy.sdp_ip
  37. 6.3. @iptrtpproxy.o_name
  38. 6.4. @iptrtpproxy.o_addr
  39. 6.5. @iptrtpproxy.auth_rights
  40. 6.6. @iptrtpproxy.active_media_num
  41. List of Examples
  42. 1.1. Declare switchboard
  43. 1.2. Declare codec_set
  44. 1.3. iptrtpproxy_alloc usage
  45. 1.4. iptrtpproxy_update usage
  46. 1.5. iptrtpproxy_adjust_timeout usage
  47. 1.6. iptrtpproxy_delete usage
  48. 1.7. iptrtpproxy_authorize_media usage
  49. Chapter 1. Admin Guide
  50. Table of Contents
  51. 1. Overview
  52. 2. Known limitations
  53. 3. Dependencies
  54. 4. Parameters
  55. 4.1. config (string)
  56. 4.2. switchboard (string)
  57. 4.3. rpc_heartbeat_timeout (int)
  58. 4.4. hostname (string)
  59. 4.5. declare_codec (string)
  60. 4.6. codec_set (string)
  61. 5. Functions
  62. 5.1. iptrtpproxy_alloc(gate_a_to_b [, existing_sess_ids])
  63. 5.2. iptrtpproxy_update(gate_a_to_b, session_ids)
  64. 5.3. iptrtpproxy_adjust_timeout(gate_a_to_b, session_ids)
  65. 5.4. iptrtpproxy_delete(session_ids)
  66. 5.5. iptrtpproxy_authorize_media()
  67. 5.6. iptrtpproxy_set_param(param, value)
  68. 5.7.
  69. iptrtpproxy_set_param("(aggregation/switchboard)_by_sip_ip_(a/
  70. b)", sip_ip)
  71. 5.8. iptrtpproxy_set_param("protected_session_ids", sess_ids)
  72. 5.9. iptrtpproxy_set_param("o_name", value)
  73. 5.10. iptrtpproxy_set_param("o_addr", value)
  74. 5.11. iptrtpproxy_set_param("codec_set", value)
  75. 5.12. iptrtpproxy_set_param("remove_codec_mask", value)
  76. 6. Selects
  77. 6.1. @iptrtpproxy.session_ids
  78. 6.2. @iptrtpproxy.sdp_ip
  79. 6.3. @iptrtpproxy.o_name
  80. 6.4. @iptrtpproxy.o_addr
  81. 6.5. @iptrtpproxy.auth_rights
  82. 6.6. @iptrtpproxy.active_media_num
  83. 1. Overview
  84. This module provides similar functionality as nathelper but
  85. communicates with netfilter kernel xt_RTPPROXY module using the
  86. libipt_RTPPROXY userspace library. All RTP streams are manipulated
  87. directly in kernel space, no data is copied from kernel to userspace
  88. and back, it reduces load and delay. See
  89. http://www.2p.cz/en/netfilter_rtp_proxy for more details.
  90. This Kamailio module is written as a light-weight module, there is no
  91. dialog management as in Nathelper. The reason is that such an API
  92. should be provided by core or a specialized dialog manager module.
  93. Because such module is not in git, session information may be stored in
  94. extra attributes of the avp_db module and the session id itself in
  95. record route as cookie, see the rr module.
  96. It should be able to support all cases as re-invites when SIP client
  97. offers media change in SDP and when number of medias in offer/answer
  98. are different.
  99. Nathelper may be still used for testing if client is behind the NAT.
  100. There is also support for media authorization. Number of codec sets may
  101. be defined. When a message containing SDP offer/answer is being
  102. processed then current codecs and streams may be inspected, removed or
  103. signallized according a codec set.
  104. 2. Known limitations
  105. * Only IPv4 addresses are supported.
  106. * More media streams per session supported
  107. 3. Dependencies
  108. The following libraries or applications must be installed before
  109. running Kamailio with this module loaded:
  110. * netfilter xt_RTPPROXY & libipt_RTPPROXY, see
  111. http://www.2p.cz/en/netfilter_rtp_proxy
  112. Note
  113. The modules Makefile must be edited and iptdir setup to the directory
  114. with the iptable sources (if different from ~/iptables). Alternatively
  115. compile the module using:
  116. make -C modules/iptrtpproxy iptdir=path_to_iptables_src
  117. 4. Parameters
  118. 4.1. config (string)
  119. 4.2. switchboard (string)
  120. 4.3. rpc_heartbeat_timeout (int)
  121. 4.4. hostname (string)
  122. 4.5. declare_codec (string)
  123. 4.6. codec_set (string)
  124. 4.1. config (string)
  125. References iptrtpproxy.cfg, see iptrtpproxy_helper. Default value is
  126. /etc/iptrtpproxy.cfg. If only codec authorization is to be used then
  127. /dev/null may be used.
  128. 4.2. switchboard (string)
  129. References xt_RTPPROXY switchboard for usage by ser module.
  130. The format is:
  131. "name=" value * ( ";" name "=" value )
  132. name = "aggregation" | "sip-addr-"
  133. The name is the switchboard name as declared in config and will be used
  134. by script functions and references switchboard. It's mandatory
  135. parameter. The special name * set values for all switchboards.
  136. The sip-addr is address used by iptrtpproxy_ser_param(by_sip_ip) to
  137. find a switchboard for particular connection. If not explicitly
  138. configured then RTP switchboard gate address are used for this feature.
  139. The aggregation enables to aggregate more switchboards in cluster and
  140. to widden bandwidth. Aggregation will take sip-addr from the first
  141. switchboard of its.
  142. Example 1.1. Declare switchboard
  143. ...
  144. modparam("iptrtpproxy", "config", "/etc/iptrtpproxy.cfg");
  145. modparam("iptrtpproxy", "switchboard", "name=my1;sip-addr-a=1.2.3.4;sip-
  146. addr-b=5.6.7.8");
  147. modparam("iptrtpproxy", "switchboard", "name=my2;sip-addr-a=2.3.4.5;sip-
  148. addr-b=3.4.5.6;aggregation=my23");
  149. modparam("iptrtpproxy", "switchboard", "name=my3;aggregation=my23");
  150. modparam("iptrtpproxy", "switchboard", "name=*;aggregation=my123");
  151. ...
  152. 4.3. rpc_heartbeat_timeout (int)
  153. Timeout in seconds used for rerequest remote RTP proxy via RPC command
  154. after preceeding error. In other words if a RPC server is unresponsive
  155. at the moment then next attempt will be forced after this timeout.
  156. Default value is 30.
  157. 4.4. hostname (string)
  158. The hostname used by RPC to identify machine where Ser is running to
  159. communicate which RTP proxy via local interface. Default value is taken
  160. from system hostname.
  161. 4.5. declare_codec (string)
  162. There are basic implicit codecs compiled in module, more codecs may be
  163. added by this parameter (one codec per modparam).
  164. 4.6. codec_set (string)
  165. Declares new codec set. Codecs are declared for each media type
  166. independently.
  167. The format is:
  168. "name=" value * ( ";" name "=" value )
  169. name = "media_type" | "rights" | "codecs" | "max_streams" | ( "rtp" | "rtcp"
  170. ) "_" ( "bytes" | "packets" )
  171. media_types = "audio" | "video" | "application" | "text" | "message" | "data"
  172. | "control" | "?" | "*"
  173. The name is the codec set name to be defined.
  174. The media_type belongs to type at m= SDP line. Question mark means
  175. "unknown media" type and asterisk "all media types".
  176. The max_streams defines how many streams (m= lines) is allowed per
  177. media type.
  178. The rights defines if particular codec is allowed 0, disallowed, i.e.
  179. will be removed if bit AND operation with remove_codec_mask is non-zero
  180. or its presence will be signallized by @iptrtpproxy.auth_rights (any
  181. other value).
  182. The codecs comma separated list of codecs. Previous media_type&rights
  183. will be applied.
  184. The rtp/rtcp_bytes/packets limits bandwidth per media_type (0 is
  185. unlimited). It will override bandwidth limited by
  186. iptrtpproxy_set_param("throttle_*").
  187. Example 1.2. Declare codec_set
  188. ...
  189. # enable all codecs, default state when codec is declared
  190. modparam("iptrtpproxy", "codec_set", "name=cs1;media_type=*;max_streams=
  191. 9999;rights=0;codecs=*");
  192. # allow only 2 audio and 1 video stream
  193. modparam("iptrtpproxy", "codec_set", "name=cs2;media_type=*;max_streams=
  194. 0;media_type=audio;max_streams=2;media_type=video;max_streams=1");
  195. # dtto, allow only a few audio and video codecs, GSM codec is allowed bu
  196. t signallized
  197. modparam("iptrtpproxy", "codec_set", "name=cs3;media_type=*;max_streams=
  198. 0;rights=1;codecs=*;media_type=audio;max_streams=2;rights=0;codecs=PCMU,G729,G72
  199. 8,parityfec,telephone-events;rights=2;codecs=GSM;media_type=video;max_streams=1;
  200. rights=0;codecs=jpeg,parityfec");
  201. # limit max. bandwidth for video¨
  202. modparam("iptrtpproxy", "codec_set", "name=cs4;media_type=video;rtp_byte
  203. s=10000;rtcp_bytes=1000");
  204. ...
  205. 5. Functions
  206. 5.1. iptrtpproxy_alloc(gate_a_to_b [, existing_sess_ids])
  207. 5.2. iptrtpproxy_update(gate_a_to_b, session_ids)
  208. 5.3. iptrtpproxy_adjust_timeout(gate_a_to_b, session_ids)
  209. 5.4. iptrtpproxy_delete(session_ids)
  210. 5.5. iptrtpproxy_authorize_media()
  211. 5.6. iptrtpproxy_set_param(param, value)
  212. 5.7. iptrtpproxy_set_param("(aggregation/switchboard)_by_sip_ip_(a/b)",
  213. sip_ip)
  214. 5.8. iptrtpproxy_set_param("protected_session_ids", sess_ids)
  215. 5.9. iptrtpproxy_set_param("o_name", value)
  216. 5.10. iptrtpproxy_set_param("o_addr", value)
  217. 5.11. iptrtpproxy_set_param("codec_set", value)
  218. 5.12. iptrtpproxy_set_param("remove_codec_mask", value)
  219. 5.1. iptrtpproxy_alloc(gate_a_to_b [, existing_sess_ids])
  220. Parses SDP content and allocates for each RTP media stream one RTP
  221. proxy session. SDP is updates to reflect allocated sessions.
  222. Switchboard/aggregation is set using iptrtpproxy_set_param(by_sip_ip)
  223. or iptrtpproxy_set_param("switchboard/aggregation").
  224. Aggregation supports load balancing among more RTP proxies controlled
  225. by RPC. The module try to allocate at machines/switchboards in
  226. following order (priorities) not yet asked (or being heartbeated)
  227. machines, responsive machines, switchboards having percentualy more
  228. free slots, non responsive machines.
  229. Proxy may hide caller identity provided at o= line using
  230. @iptrtpproxy.o_name/addr and iptrtpproxy_set_param(o_name/addr)
  231. functions. But the script is responsible for rewritting to original
  232. values in a response or a callee initiated re-INVITE. Therefore
  233. original value need to be stored in-dialog.
  234. * if gate_a_to_b bit 0 is set then SDP regards to gate-a to gate-b
  235. direction.
  236. * protected_session_ids list of existing sessions enables reusing
  237. already allocated sessions in re-INVITE without allocating new
  238. sessions for each stream in SDP regardless a IP/port is required.
  239. It's mostly undesirable, typically "hold-on" is done via re-INVITE
  240. without any change. There is drawback because callee cannot change
  241. IP:port in 200OK which is legal case in RFC3264. But because some
  242. non-RFC3264 compliant phones dislike proactively changed IP:port at
  243. RTP proxy it seems it's less evil.
  244. * function returns true is a session was created, identifier is
  245. available via select @iptrtpproxy.session_ids.
  246. Example 1.3. iptrtpproxy_alloc usage
  247. ...
  248. if (!iptrtpproxy_set_param("aggregation_by_sip_ip_a", "@received.ip")) {
  249. if (!iptrtpproxy_set_param("switchboard_by_sip_ip_a", "@received
  250. .ip")) {
  251. t_reply("500", "RTP proxy error");
  252. drop;
  253. }
  254. }
  255. eval_push("x:%@next_hop.src_ip");
  256. if (@eval.get[-1] == @received.ip) {
  257. if (@iptrtpproxy.aggregation_a) {
  258. iptrtpproxy_set_param("aggregation_b", "@iptrtpproxy.agg
  259. regation_a");
  260. }
  261. else {
  262. iptrtpproxy_set_param("switchboard_b", "@iptrtpproxy.swi
  263. tchboard_a");
  264. }
  265. }
  266. else {
  267. if (!iptrtpproxy_set_param("aggregation_by_sip_ip_b", "@eval.get
  268. [-1]")) {
  269. if (!iptrtpproxy_set_param("switchboard_by_sip_ip_b", "@
  270. eval.get[-1]")) {
  271. t_reply("500", "RTP proxy error");
  272. drop;
  273. }
  274. }
  275. }
  276. eval_remove(-1, 1);
  277. if (!iptrtpproxy_alloc("1")) {
  278. t_reply("500", "RTP proxy error");
  279. drop;
  280. }
  281. $sess_ids = @iptrtpproxy.session_ids;
  282. ...
  283. 5.2. iptrtpproxy_update(gate_a_to_b, session_ids)
  284. Parses SDP content and updates sessions provided by session_ids and
  285. updates SDP. If succesfull then session_ids may be changed (in case
  286. e.g. media stream has port zero particular session is released), the
  287. result of @iptrtpproxy.session_ids should be stored for future
  288. in-dialog usage.
  289. The SDP contect is also affected by iptrtpproxy_set_param(o_name/addr)
  290. functions. If a stream is deactivated in SDP then Sessions may be
  291. deleted unless mentioned in protected_session_ids.
  292. * if gate_a_to_b bit 0 is set then SDP regards to gate-a to gate-b
  293. direction.
  294. if gate_a_to_b bit 1 is set then SDP is updated only, no RTP
  295. session are affected. Should be used when handling retransmission
  296. in onreply route, retransmission replies are not eaten be tm
  297. module!
  298. Example 1.4. iptrtpproxy_update usage
  299. ...
  300. # load $sess_ids from dialog
  301. if (iptrtpproxy_update("0", $sess_ids)) {
  302. $sess_ids = @iptrtpproxy.session_ids;
  303. # save sess_ids in dialog
  304. }
  305. ...
  306. 5.3. iptrtpproxy_adjust_timeout(gate_a_to_b, session_ids)
  307. Adjust timeout for particular gate. It's useful in "200 OK" decrease
  308. timeout to learning timeout if INVITE has set (long) ringing timeout.
  309. * if gate_a_to_b bit 0 is set then it regards to gate-a to gate-b
  310. direction.
  311. Example 1.5. iptrtpproxy_adjust_timeout usage
  312. ...
  313. # load $sess_ids from dialog
  314. if (status=~"18[0-9]") {
  315. iptrtpproxy_set_param("learning_timeout", "60");
  316. }
  317. else {
  318. iptrtpproxy_set_param("learning_timeout", "5");
  319. }
  320. if (iptrtpproxy_adjust_timeout("0", $sess_ids)) {
  321. }
  322. ...
  323. 5.4. iptrtpproxy_delete(session_ids)
  324. Delete sessions identified by session_ids. May be used when dialog is
  325. being destroyed (BYE) or when INVITE failed in failure route. If
  326. protected_session_ids list is provided then this set is excluded from
  327. sessions to be deleted.
  328. Example 1.6. iptrtpproxy_delete usage
  329. ...
  330. # load $sess_ids from dialog
  331. iptrtpproxy_delete($sess_ids);
  332. ...
  333. 5.5. iptrtpproxy_authorize_media()
  334. Authorizes SDP media according currect codec_set. If bit AND operation
  335. between rights in codec set and remove_codec_mask is non zero then such
  336. a codec are to be removed. The result may be obtained from
  337. @iptrtpproxy.auth_rights which returns max. right which has been
  338. applied when processing all codecs of enabled streams.
  339. The function MUST NOT be called after iptrtpproxy_alloc/update! But the
  340. function may be called several times to authorize using more codec
  341. sets.
  342. Example 1.7. iptrtpproxy_authorize_media usage
  343. ...
  344. if (@iptrtpproxy.active_media_num == "0") break;
  345. iptrtpproxy_set_param("codec_set", "cs2");
  346. iptrtpproxy_set_param("remove_codec_mask", "1");
  347. if (!iptrtpproxy_authorize_media()) {
  348. t_reply("415", "Cannot authorize media");
  349. drop;
  350. }
  351. iptrtpproxy_set_param("codec_set", "cs3");
  352. if (!iptrtpproxy_authorize_media()) {
  353. t_reply("415", "Cannot authorize media");
  354. drop;
  355. }
  356. if (@iptrtpproxy.active_media_num == "0") {
  357. t_reply("488", "Not acceptable here");
  358. drop;
  359. }
  360. if (@iptrtpproxy.auth_rights == "2") {
  361. append_hf_value("Contact: <sip:1.2.3.4>");
  362. t_reply("301", "Redirect to transcoder");
  363. drop;
  364. }
  365. ...
  366. 5.6. iptrtpproxy_set_param(param, value)
  367. Set particular parameter needed mainly by
  368. iptrtpproxy_alloc/update/adjust_timeout. The paramter value is availble
  369. via @iptrtpproxy.<param>.
  370. * Supported parameters: expiration_timeout, ttl, learning_timeout,
  371. always_learn, aggregation_a, aggregation_b, switchboard_a,
  372. switchboard_b, throttle_mark, throttle_rtp_max_bytes,
  373. throttle_rtp_max_packets, throttle_rtcp_max_bytes,
  374. throttle_rtcp_max_packets.
  375. 5.7. iptrtpproxy_set_param("(aggregation/switchboard)_by_sip_ip_(a/b)",
  376. sip_ip)
  377. Find corresponding aggregation or switchboard and set
  378. @iptrtpproxy.aggregation_a/b or @iptrtpproxy.switchboard_a/b.
  379. Switchboards/aggregations are compared against sip-addr, it allow
  380. separate SIP and RTP traffic and RTP aggregation.
  381. * sip_ip IP to be compared, typically @received.ip or
  382. @next_hop.src_ip.
  383. * function returns true if switchboard/aggregation was found
  384. 5.8. iptrtpproxy_set_param("protected_session_ids", sess_ids)
  385. Used for reusing sessions in iptrtpproxy_alloc, iptrtpproxy_update and
  386. iptrtpproxy_delete.
  387. 5.9. iptrtpproxy_set_param("o_name", value)
  388. Username to be rewritten at o= line by iptrtpproxy_alloc/update to hide
  389. caller identity. If value is blank then username is left unchanged.
  390. 5.10. iptrtpproxy_set_param("o_addr", value)
  391. Address to be rewritten at o= line by iptrtpproxy_alloc/update to hide
  392. caller identity. If value is blank then address is left unchanged.
  393. 5.11. iptrtpproxy_set_param("codec_set", value)
  394. Codec set for iptrtpproxy_authorize_media. Current codec set may be
  395. obtained by @iptrtpproxy.codec_set.
  396. 5.12. iptrtpproxy_set_param("remove_codec_mask", value)
  397. Mask used in iptrtpproxy_authorize_media. Current mask may be obtained
  398. by @iptrtpproxy.remove_codec_mask.
  399. 6. Selects
  400. 6.1. @iptrtpproxy.session_ids
  401. 6.2. @iptrtpproxy.sdp_ip
  402. 6.3. @iptrtpproxy.o_name
  403. 6.4. @iptrtpproxy.o_addr
  404. 6.5. @iptrtpproxy.auth_rights
  405. 6.6. @iptrtpproxy.active_media_num
  406. 6.1. @iptrtpproxy.session_ids
  407. Returns sessions allocated/updated in iptrtpproxy_alloc/update.
  408. The format is:
  409. switchboard_name [ ":" [ session_id "/" created ] * ( "," session_id "/" created
  410. ) ] ]
  411. session_id = * ( [0-9] ) ; empty when no session allocated
  412. created = timestamp
  413. 6.2. @iptrtpproxy.sdp_ip
  414. Return first rewritten IP provided at SDP c= line.
  415. 6.3. @iptrtpproxy.o_name
  416. Return username from original o= line.
  417. 6.4. @iptrtpproxy.o_addr
  418. Return address from original o= line.
  419. 6.5. @iptrtpproxy.auth_rights
  420. Result of iptrtpproxy_authorize_media.
  421. 6.6. @iptrtpproxy.active_media_num
  422. Returns number of active media streams in SDP.
  423. iptrtpproxy_authorize_media may disable some streams, i.e. returned
  424. value may change after authorization.