dns.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Kamailio and DNS Overview
  2. ---------------------------
  3. The DNS subsystem in Kamailio can either directly use libresolv and a combination
  4. of the locally configured DNS server, /etc/hosts and the local Network
  5. Information Service (NIS/YP a.s.o) or cache the query results (both positive
  6. and negative) and look first in its internal cache.
  7. When its internal DNS cache is enabled, Kamailio can also use DNS failover: if
  8. one destination resolves to multiple addresses Kamailio can try all of them until
  9. it finds one to which it can successfully send the packet or it exhausts all
  10. of them. Kamailio (The tm module to be more precise) uses the DNS failover also
  11. when the destination host doesn't send any reply to a forwarded invite within the
  12. SIP timeout interval (whose value can be configured using the tm fr_timer
  13. parameter).
  14. When SRV based load balancing is enabled Kamailio can even do DNS based load
  15. balancing (see RFC2782 and the dns_srv_lb option below). This is using the
  16. weight value in the DNS SRV record.
  17. DNS Cache and Failover Drawbacks
  18. --------------------------------
  19. Using the DNS cache and the DNS failover has also some drawbacks:
  20. 1. Only the locally configured DNS server (usually in /etc/resolv.conf) is
  21. used for the requests (/etc/hosts and the local Network Information Service
  22. are ignored).
  23. Workaround: disable the DNS cache (use_dns_cache=off or compile without -DUSE_DNS_CACHE).
  24. 2. The DNS cache uses extra memory
  25. Workaround: disable the DNS cache.
  26. 3. The DNS failover introduces a very small performance penalty
  27. Workaround: disable the DNS failover (use_dns_failover=off).
  28. 4. The DNS failover increases the memory usage (the internal structures
  29. used to represent the transaction are bigger when the DNS failover support is
  30. compiled).
  31. Workaround: compile without DNS failover support (-DUSE_DNS_FAILOVER).
  32. Turning it off from the config file is not enough in this case (the extra
  33. memory will still be used).
  34. On the other hand using the DNS cache saves lots of DNS queries and makes
  35. DNS based failover and DNS based load balancing possible. If the destination
  36. blacklist is enabled, Kamailio can do failover even if forwarding in stateless
  37. mode.
  38. In the ideal case with the DNS cache enabled Kamailio will do only one query for
  39. a NAPTR (if enabled) or SRV lookup and then it will use the results for the
  40. record's TTL (for example if all the resulting records have 1 minute TTL,
  41. the server won't make another query for this domain for 1 minute). Even negative
  42. answers will be cached.
  43. Without the DNS cache, each NAPTR or SRV lookup will result in at least 2
  44. queries. These queries will happen every time, for each message (even if
  45. all of them go to the same domain).
  46. DNS Resolver Options
  47. --------------------
  48. The DNS resolver options control how Kamailio will interact with the external
  49. DNS servers. These options (with the dns_try_ipv6 exception) are passed to
  50. libresolv and are used each time a DNS request is made.
  51. The default values are system specific and generally depend on the
  52. /etc/resolv.conf content. For servers doing a lot of DNS requests it is
  53. highly recommended to change the default values in the Kamailio config file
  54. (even if using Kamailio's internal DNS cache).
  55. dns_try_ipv6 = on | off - if on and Kamailio listens on at least one ipv6 socket,
  56. ipv6 (AAAA) lookups will be performed if the ipv4 (A) lookups fail.
  57. If off only ipv4 (A) lookups will be used.
  58. Default: on if Kamailio is compiled with ipv6 support.
  59. dns_try_naptr = on | off - if on Kamailio will first try a NAPTR lookup for
  60. destinations that don't have the protocol or port specified and
  61. are not simple ip addresses (as described in RFC 3263). This will
  62. introduce a slight performance penalty and will probably cause extra
  63. DNS lookups. For example a lookup for a non-existing domain will
  64. produce one extra query: NAPTR(domain), SRV(_sip._udp.domain)
  65. and A/AAAA(domain).
  66. If the result of a query contains several NAPTR records, Kamailio will select
  67. among them according to the RFC2915 and Kamailio preference towards a
  68. specific protocol (see dns_udp_pref, dns_tcp_pref and dns_tls_pref
  69. below). For an RFC3263 compliant configuration (choose the remote side
  70. preferred protocol if supported), set dns_udp_pref, dns_tcp_pref and
  71. dns_tls_pref to the same value (>=0), e.g. 0.
  72. Default: off
  73. dns_udp_pref = number - udp protocol preference when doing NAPTR lookups.
  74. This option works together with dns_tcp_pref, dns_tls_pref and
  75. dns_sctp_pref. If all this options have the same positive value and more
  76. NAPTR records are available, Kamailio will select the NAPTR record preferred
  77. by the remote side (according to RFC2915). If the values are positive
  78. but different, Kamailio will select the NAPTR record whose protocol it
  79. prefers the most (the protocol with the highest dns_<proto>_pref
  80. number). If there are several NAPTR records with the same preferred
  81. protocol, Kamailio will select among them based on their order and preference
  82. (see RFC2915).
  83. To completely disable selecting a specific protocol, use a negative
  84. number. For example dns_tcp_pref=-1 will completely disable selection
  85. of tcp NAPTR records, even if this will result in the NAPTR lookup
  86. failure. Note: if a protocol is disabled in Kamailio (e.g. tls_disable=1)
  87. the corresponding NAPTR records selection will be also disabled,
  88. irrespective of the dns_<proto>_preference value.
  89. Default: dns_udp_pref=30, dns_tcp_pref=20, dns_tls_pref=10 and
  90. dns_sctp_pref=20.
  91. (prefer udp, but if no udp NAPTR record found or no SRV-resolvable
  92. udp NAPTR record found use tcp or sctp records and if this fails too
  93. use tls)
  94. dns_tcp_pref = number (see dns_udp_pref above)
  95. dns_tls_pref = number (see dns_udp_pref above)
  96. dns_sctp_pref = number (see dns_udp_pref above)
  97. dns_retr_time = time - time in s before retrying a DNS request.
  98. Default: system specific, depends also on the/etc/resolv.conf content
  99. (usually 5 s).
  100. dns_retr_no = no. - number of DNS retransmissions before giving up.
  101. Default: see above (usually 4)
  102. dns_servers_no = no. - how many DNS servers from the ones defined in
  103. /etc/resolv.conf will be used. Default: all of them.
  104. dns_use_search_list= yes/no - if no, the search list in /etc/resolv.conf
  105. will be ignored (=> fewer lookups => gives up faster).
  106. Default: yes.
  107. HINT: even if you don't have a search list defined, setting this option
  108. to "no" will still be "faster", because an empty search list is in
  109. fact search "" (so even if the search list is empty/missing there will
  110. still be 2 DNS queries, eg. foo+'.' and foo+""+'.')
  111. dns_search_full_match = yes/no - controls the check of the name part
  112. which is found in the answer expanding the searched name before
  113. the answer is treated as correct and "link" (fake CNAME record)
  114. between the short name (query) and long name (answer) is created
  115. which is then stored in dns_cache and reused for next queries.
  116. If set to no - no additional check is done.
  117. If set to yes - the additional part is checked against the search list.
  118. The maximum time a DNS request can take (before failing) is:
  119. (dns_retr_time*dns_retr_no) * (search_list_domains) If dns_try_ipv6 is yes,
  120. mutliply it again by 2.
  121. The option combination that produces the "fastest" DNS resolver config
  122. (the "faster" in the sense that it gives up the quickest) is:
  123. dns_try_ipv6=no
  124. dns_retr_time=1
  125. dns_retr_no=1
  126. dns_servers_no=1
  127. dns_use_search_list=no
  128. The recommended DNS configuration is to have a "close" DNS caching recursive
  129. server configured in /etc/resolv.conf, set the DNS resolver options in Kamailio's
  130. config as in the above example and enable the DNS cache (in Kamailio).
  131. Pay particular attention to dns_servers_no and dns_use_search_list. It's a
  132. good idea to make sure you don't need / use the search list or more then one
  133. DNS server (to avoid unnecessary extra lookups).
  134. DNS Resolver Compile Options
  135. ----------------------------
  136. USE_NAPTR - if defined the naptr lookup support will be compiled in.
  137. NAPTR support still has to be enabled from Kamailio's config file (it's
  138. off by default).
  139. DNS Cache and Failover Config Variables
  140. ---------------------------------------
  141. use_dns_cache = on | off - if off the DNS cache won't be used (all dns
  142. lookups will result into a DNS request). When on all the DNS request
  143. results will be cached.
  144. WARNING: when enabled /etc/hosts will be completely bypassed, all the dns
  145. request will go directly to the system configured (resolv.conf) dns
  146. server.
  147. Default: on.
  148. use_dns_failover = on |off - if on and sending a request fails (due to not
  149. being allowed from an onsend_route, send failure, blacklisted destination
  150. or, when using tm, invite timeout), and the destination resolves to
  151. multiple ip addresses and/or multiple SRV records, the send will be
  152. re-tried using the next ip/record. In tm's case a new branch will be
  153. created for each new send attempt.
  154. Default: off.
  155. Depends on use_dns_cache being on. If tm is used along with DNS failover is
  156. recommended to also turn on dst_blacklist.
  157. dns_srv_lb = on | off or
  158. dns_srv_loadbalancing = on | off - if on instead of doing simple DNS
  159. failover (like above), Kamailio will load balance requests to different srv
  160. records of the same priority based on the SRV records weights (like
  161. described in RFC2782). For a destination which has different priorities
  162. for all its SRV records, this option will be equivalent with simple
  163. DNS failover.
  164. Note: this option requires having DNS failover enabled (see
  165. use_dns_failover above).
  166. Default: off.
  167. dns_try_ipv6 = on | off - shared with the resolver (see resolver
  168. description).
  169. dns_try_naptr = on | off - shared with the resolver (see resolver
  170. description).
  171. dns_udp_pref = number - shared with the resolver (see resolver
  172. description).
  173. dns_tcp_pref = number - shared with the resolver (see resolver
  174. description).
  175. dns_tls_pref = number - shared with the resolver (see resolver
  176. description).
  177. dns_cache_flags = DNS cache specific resolver flags, used for overriding
  178. the default behaviour (low level).
  179. Possible values:
  180. 1 - ipv4 only: only DNS A requests are performed, even if Kamailio listens
  181. also on ipv6 addresses.
  182. 2 - ipv6 only: only DNS AAAA requests are performed. Ignored if
  183. dns_try_ipv6 is off or Kamailio doesn't listen on any ipv6
  184. address.
  185. 4 - prefer ipv6: try first to resolve a host name to an ipv6 address
  186. (DNS AAAA request) and only if this fails try an ipv4
  187. address (DNS A request).
  188. By default the ipv4 addresses are preferred.
  189. Default: 0
  190. dns_cache_negative_ttl = time to live for negative results ("not found") in
  191. seconds. Use 0 to disable.
  192. Default: 60 s.
  193. dns_cache_min_ttl = minimum accepted time to live for a record, in seconds.
  194. If a record has a lower ttl, its value will be discarded and
  195. dns_cache_min_ttl will be used instead.
  196. Default: 0
  197. dns_cache_max_ttl = maximum accepted time to live for a record, in seconds.
  198. If a record has a higher ttl, its value will be discarded and
  199. dns_cache_max_ttl will be used instead.
  200. Default: MAXINT
  201. dns_cache_mem = maximum memory used for the DNS cache in Kb.
  202. Default: 500 Kb
  203. dns_cache_gc_interval = how often (in s) the DNS cache will be garbage
  204. collected.
  205. Default: 120 s.
  206. dns_cache_del_nonexp = yes | no or
  207. dns_cache_delete_nonexpired = yes | no - allow deletion of non-expired
  208. records from the cache when there is no more space left for new
  209. ones. The last-recently used entries are deleted first.
  210. Default: no
  211. dns_cache_init = on | off - if off, the DNS cache is not initialized
  212. at startup and cannot be enabled runtime, that saves some memory.
  213. Default: on
  214. DNS Cache Compile Options
  215. -------------------------
  216. USE_DNS_CACHE - if defined the DNS cache support will be compiled in
  217. (default). If not needed/wanted the dns_cache can be disabled from the
  218. Kamailio's config file. The only advantages for not compiling the DNS cache
  219. support is a slight decrease of the executable size and an extremely
  220. small performance increase (1 less comparison per DNS request).
  221. USE_DNS_FAILOVER - if defined the DNS failover support will be compiled in.
  222. (default). Compiling the DNS failover support has a few disadvantages,
  223. see the "Drawbacks" section.
  224. DNS_SRV_LB - if defined (default) support for load balancing using
  225. SRV records weights (as described in RFC2782) will be compiled in.
  226. Note however that it still must be enabled from the Kamailio config, it's
  227. disabled by default (see the dns_srv_lb config option).
  228. USE_NAPTR - (shared with the resolver) if defined NAPTR support will
  229. be compiled in (default). Note that even if compiled, NAPTR support
  230. must be enabled also from the Kamailio config (see the dns_try_naptr option).
  231. NAPTR_CACHE_ALL_ARS - if defined all the additional records in a NAPTR
  232. answer will be cached. Normally Kamailio would cache only "related" records
  233. (records that are directly referred), but for answers with lots of
  234. A/AAAA records it might happen that not all of the SRV records will fit
  235. in the AR section. In this case, without this compile option Kamailio will
  236. not cache the un-referred A/AAAA records. BY default this option is
  237. disabled.
  238. CACHE_RELEVANT_RECS_ONLY - if defined (default), records in the AR section
  239. of an answer will be cached only if they are "related" to the query.
  240. For example if the query is for a SRV record, A & AAAA records in the
  241. AR section will be cached only if there are SRV records pointing to
  242. them. This avoids adding possible garbage to the cache.
  243. If this option is not defined (experimental), everything in the AR
  244. section will be added to the cache.
  245. Note: To remove a compile options, edit Kamailio's Makefile.defs and remove it
  246. from DEFS list. To add a compile options add it to the make command line,
  247. e.g.: make proper; make all extra_defs=-DUSE_DNS_FAILOVER
  248. or for a permanent solution, edit Makefile.defs and add it to DEFS
  249. (don't foget to prefix it with -D). Some options require editing
  250. dns_cache.c or resolve.[ch] (just grep after them).