Merge tag 'tegra-for-5.14-arm64-dt-fixes' of git://git.kernel.org/pub/scm/linux/kerne...
[linux-2.6-microblaze.git] / fs / cifs / dns_resolve.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *  fs/cifs/dns_resolve.c
4  *
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)
10  *
11  *   Contains the CIFS DFS upcall routines used for hostname to
12  *   IP address translation.
13  *
14  */
15
16 #include <linux/slab.h>
17 #include <linux/dns_resolver.h>
18 #include "dns_resolve.h"
19 #include "cifsglob.h"
20 #include "cifsproto.h"
21 #include "cifs_debug.h"
22
23 /**
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  *
28  * The IP address will be returned in string form, and the caller is
29  * responsible for freeing it.
30  *
31  * Returns length of result on success, -ve on error.
32  */
33 int
34 dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
35 {
36         struct sockaddr_storage ss;
37         const char *hostname, *sep;
38         char *name;
39         int len, rc;
40
41         if (!ip_addr || !unc)
42                 return -EINVAL;
43
44         len = strlen(unc);
45         if (len < 3) {
46                 cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
47                 return -EINVAL;
48         }
49
50         /* Discount leading slashes for cifs */
51         len -= 2;
52         hostname = unc + 2;
53
54         /* Search for server name delimiter */
55         sep = memchr(hostname, '/', len);
56         if (sep)
57                 len = sep - hostname;
58         else
59                 cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
60                          __func__, unc);
61
62         /* Try to interpret hostname as an IPv4 or IPv6 address */
63         rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
64         if (rc > 0)
65                 goto name_is_IP_address;
66
67         /* Perform the upcall */
68         rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
69                        NULL, ip_addr, NULL, false);
70         if (rc < 0)
71                 cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
72                          __func__, len, len, hostname);
73         else
74                 cifs_dbg(FYI, "%s: resolved: %*.*s to %s\n",
75                          __func__, len, len, hostname, *ip_addr);
76         return rc;
77
78 name_is_IP_address:
79         name = kmalloc(len + 1, GFP_KERNEL);
80         if (!name)
81                 return -ENOMEM;
82         memcpy(name, hostname, len);
83         name[len] = 0;
84         cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",
85                  __func__, name);
86         *ip_addr = name;
87         return 0;
88 }