1 // SPDX-License-Identifier: LGPL-2.1
3 * fs/cifs/dns_resolve.c
5 * Copyright (c) 2007 Igor Mammedov
6 * Author(s): Igor Mammedov (niallain@gmail.com)
7 * Steve French (sfrench@us.ibm.com)
8 * Wang Lei (wang840925@gmail.com)
9 * David Howells (dhowells@redhat.com)
11 * Contains the CIFS DFS upcall routines used for hostname to
12 * IP address translation.
16 #include <linux/slab.h>
17 #include <linux/dns_resolver.h>
18 #include "dns_resolve.h"
20 #include "cifsproto.h"
21 #include "cifs_debug.h"
24 * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
25 * @unc: UNC path specifying the server (with '/' as delimiter)
26 * @ip_addr: Where to return the IP address.
27 * @expiry: Where to return the expiry time for the dns record.
29 * The IP address will be returned in string form, and the caller is
30 * responsible for freeing it.
32 * Returns length of result on success, -ve on error.
35 dns_resolve_server_name_to_ip(const char *unc, char **ip_addr, time64_t *expiry)
37 struct sockaddr_storage ss;
38 const char *hostname, *sep;
47 cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
51 /* Discount leading slashes for cifs */
55 /* Search for server name delimiter */
56 sep = memchr(hostname, '/', len);
60 cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
63 /* Try to interpret hostname as an IPv4 or IPv6 address */
64 rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
66 goto name_is_IP_address;
68 /* Perform the upcall */
69 rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
70 NULL, ip_addr, expiry, false);
72 cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
73 __func__, len, len, hostname);
75 cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n",
76 __func__, len, len, hostname, *ip_addr,
77 expiry ? (*expiry) : 0);
81 name = kmalloc(len + 1, GFP_KERNEL);
84 memcpy(name, hostname, len);
86 cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",