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)
7 * Wang Lei (wang840925@gmail.com)
8 * David Howells (dhowells@redhat.com)
10 * Contains the CIFS DFS upcall routines used for hostname to
11 * IP address translation.
13 * This library is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published
15 * by the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
21 * the GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/slab.h>
29 #include <linux/dns_resolver.h>
30 #include "dns_resolve.h"
32 #include "cifsproto.h"
33 #include "cifs_debug.h"
36 * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
37 * @unc: UNC path specifying the server (with '/' as delimiter)
38 * @ip_addr: Where to return the IP address.
39 * @expiry: Where to return the expiry time for the dns record.
41 * The IP address will be returned in string form, and the caller is
42 * responsible for freeing it.
44 * Returns length of result on success, -ve on error.
47 dns_resolve_server_name_to_ip(const char *unc, char **ip_addr, time64_t *expiry)
49 struct sockaddr_storage ss;
50 const char *hostname, *sep;
59 cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
63 /* Discount leading slashes for cifs */
67 /* Search for server name delimiter */
68 sep = memchr(hostname, '/', len);
72 cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
75 /* Try to interpret hostname as an IPv4 or IPv6 address */
76 rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
78 goto name_is_IP_address;
80 /* Perform the upcall */
81 rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
82 NULL, ip_addr, expiry, false);
84 cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
85 __func__, len, len, hostname);
87 cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n",
88 __func__, len, len, hostname, *ip_addr,
89 expiry ? (*expiry) : 0);
93 name = kmalloc(len + 1, GFP_KERNEL);
96 memcpy(name, hostname, len);
98 cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",