2 * fs/cifs/dns_resolve.c
4 * Copyright (c) 2007 Igor Mammedov
5 * Author(s): Igor Mammedov (niallain@gmail.com)
6 * Steve French (sfrench@us.ibm.com)
8 * Contains the CIFS DFS upcall routines used for hostname to
9 * IP address translation.
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.
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.
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
26 #include <keys/user-type.h>
27 #include "dns_resolve.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
32 static int dns_resolver_instantiate(struct key *key, const void *data,
38 ip = kmalloc(datalen+1, GFP_KERNEL);
42 memcpy(ip, data, datalen);
45 rcu_assign_pointer(key->payload.data, ip);
51 dns_resolver_destroy(struct key *key)
53 kfree(key->payload.data);
56 struct key_type key_type_dns_resolver = {
57 .name = "dns_resolver",
58 .def_datalen = sizeof(struct in_addr),
59 .describe = user_describe,
60 .instantiate = dns_resolver_instantiate,
61 .destroy = dns_resolver_destroy,
65 /* Checks if supplied name is IP address
70 static int is_ip(const char *name)
73 struct sockaddr_in sin_server;
74 struct sockaddr_in6 sin_server6;
76 rc = cifs_inet_pton(AF_INET, name,
77 &sin_server.sin_addr.s_addr);
80 /* not ipv4 address, try ipv6 */
81 rc = cifs_inet_pton(AF_INET6, name,
82 &sin_server6.sin6_addr.in6_u);
88 /* we failed translating address */
92 /* Resolves server name to ip address.
96 * *ip_addr - pointer to server ip, caller responcible for freeing it.
100 dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
103 struct key *rkey = ERR_PTR(-EAGAIN);
108 if (!ip_addr || !unc)
111 /* search for server name delimiter */
114 cFYI(1, ("%s: unc is too short: %s", __func__, unc));
118 name = memchr(unc+2, '\\', len);
120 cFYI(1, ("%s: probably server name is whole unc: %s",
123 len = (name - unc) - 2/* leading // */;
126 name = kmalloc(len+1, GFP_KERNEL);
131 memcpy(name, unc+2, len);
135 cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
141 rkey = request_key(&key_type_dns_resolver, name, "");
143 data = rkey->payload.data;
145 cERROR(1, ("%s: unable to resolve: %s", __func__, name));
152 *ip_addr = kmalloc(len+1, GFP_KERNEL);
154 memcpy(*ip_addr, data, len);
155 (*ip_addr)[len] = '\0';
157 cFYI(1, ("%s: resolved: %s to %s", __func__,