dns_resolve.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * fs/cifs/dns_resolve.c
  3. *
  4. * Copyright (c) 2007 Igor Mammedov
  5. * Author(s): Igor Mammedov (niallain@gmail.com)
  6. * Steve French (sfrench@us.ibm.com)
  7. *
  8. * Contains the CIFS DFS upcall routines used for hostname to
  9. * IP address translation.
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <keys/user-type.h>
  26. #include "dns_resolve.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. /* Checks if supplied name is IP address
  31. * returns:
  32. * 1 - name is IP
  33. * 0 - name is not IP
  34. */
  35. static int
  36. is_ip(const char *name)
  37. {
  38. int rc;
  39. struct sockaddr_in sin_server;
  40. struct sockaddr_in6 sin_server6;
  41. rc = cifs_inet_pton(AF_INET, name,
  42. &sin_server.sin_addr.s_addr);
  43. if (rc <= 0) {
  44. /* not ipv4 address, try ipv6 */
  45. rc = cifs_inet_pton(AF_INET6, name,
  46. &sin_server6.sin6_addr.in6_u);
  47. if (rc > 0)
  48. return 1;
  49. } else {
  50. return 1;
  51. }
  52. /* we failed translating address */
  53. return 0;
  54. }
  55. static int
  56. dns_resolver_instantiate(struct key *key, const void *data,
  57. size_t datalen)
  58. {
  59. int rc = 0;
  60. char *ip;
  61. ip = kmalloc(datalen + 1, GFP_KERNEL);
  62. if (!ip)
  63. return -ENOMEM;
  64. memcpy(ip, data, datalen);
  65. ip[datalen] = '\0';
  66. /* make sure this looks like an address */
  67. if (!is_ip((const char *) ip)) {
  68. kfree(ip);
  69. return -EINVAL;
  70. }
  71. key->type_data.x[0] = datalen;
  72. rcu_assign_pointer(key->payload.data, ip);
  73. return rc;
  74. }
  75. static void
  76. dns_resolver_destroy(struct key *key)
  77. {
  78. kfree(key->payload.data);
  79. }
  80. struct key_type key_type_dns_resolver = {
  81. .name = "dns_resolver",
  82. .def_datalen = sizeof(struct in_addr),
  83. .describe = user_describe,
  84. .instantiate = dns_resolver_instantiate,
  85. .destroy = dns_resolver_destroy,
  86. .match = user_match,
  87. };
  88. /* Resolves server name to ip address.
  89. * input:
  90. * unc - server UNC
  91. * output:
  92. * *ip_addr - pointer to server ip, caller responcible for freeing it.
  93. * return 0 on success
  94. */
  95. int
  96. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  97. {
  98. int rc = -EAGAIN;
  99. struct key *rkey = ERR_PTR(-EAGAIN);
  100. char *name;
  101. char *data = NULL;
  102. int len;
  103. if (!ip_addr || !unc)
  104. return -EINVAL;
  105. /* search for server name delimiter */
  106. len = strlen(unc);
  107. if (len < 3) {
  108. cFYI(1, ("%s: unc is too short: %s", __func__, unc));
  109. return -EINVAL;
  110. }
  111. len -= 2;
  112. name = memchr(unc+2, '\\', len);
  113. if (!name) {
  114. cFYI(1, ("%s: probably server name is whole unc: %s",
  115. __func__, unc));
  116. } else {
  117. len = (name - unc) - 2/* leading // */;
  118. }
  119. name = kmalloc(len+1, GFP_KERNEL);
  120. if (!name) {
  121. rc = -ENOMEM;
  122. return rc;
  123. }
  124. memcpy(name, unc+2, len);
  125. name[len] = 0;
  126. if (is_ip(name)) {
  127. cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
  128. __func__, name));
  129. data = name;
  130. goto skip_upcall;
  131. }
  132. rkey = request_key(&key_type_dns_resolver, name, "");
  133. if (!IS_ERR(rkey)) {
  134. len = rkey->type_data.x[0];
  135. data = rkey->payload.data;
  136. } else {
  137. cERROR(1, ("%s: unable to resolve: %s", __func__, name));
  138. goto out;
  139. }
  140. skip_upcall:
  141. if (data) {
  142. *ip_addr = kmalloc(len + 1, GFP_KERNEL);
  143. if (*ip_addr) {
  144. memcpy(*ip_addr, data, len + 1);
  145. if (!IS_ERR(rkey))
  146. cFYI(1, ("%s: resolved: %s to %s", __func__,
  147. name,
  148. *ip_addr
  149. ));
  150. rc = 0;
  151. } else {
  152. rc = -ENOMEM;
  153. }
  154. if (!IS_ERR(rkey))
  155. key_put(rkey);
  156. }
  157. out:
  158. kfree(name);
  159. return rc;
  160. }