1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * fs/cifs/dns_resolve.c
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2007 Igor Mammedov
5*4882a593Smuzhiyun * Author(s): Igor Mammedov (niallain@gmail.com)
6*4882a593Smuzhiyun * Steve French (sfrench@us.ibm.com)
7*4882a593Smuzhiyun * Wang Lei (wang840925@gmail.com)
8*4882a593Smuzhiyun * David Howells (dhowells@redhat.com)
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Contains the CIFS DFS upcall routines used for hostname to
11*4882a593Smuzhiyun * IP address translation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This library is free software; you can redistribute it and/or modify
14*4882a593Smuzhiyun * it under the terms of the GNU Lesser General Public License as published
15*4882a593Smuzhiyun * by the Free Software Foundation; either version 2.1 of the License, or
16*4882a593Smuzhiyun * (at your option) any later version.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * This library is distributed in the hope that it will be useful,
19*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
20*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
21*4882a593Smuzhiyun * the GNU Lesser General Public License for more details.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * You should have received a copy of the GNU Lesser General Public License
24*4882a593Smuzhiyun * along with this library; if not, write to the Free Software
25*4882a593Smuzhiyun * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <linux/slab.h>
29*4882a593Smuzhiyun #include <linux/dns_resolver.h>
30*4882a593Smuzhiyun #include "dns_resolve.h"
31*4882a593Smuzhiyun #include "cifsglob.h"
32*4882a593Smuzhiyun #include "cifsproto.h"
33*4882a593Smuzhiyun #include "cifs_debug.h"
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
37*4882a593Smuzhiyun * @unc: UNC path specifying the server (with '/' as delimiter)
38*4882a593Smuzhiyun * @ip_addr: Where to return the IP address.
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * The IP address will be returned in string form, and the caller is
41*4882a593Smuzhiyun * responsible for freeing it.
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * Returns length of result on success, -ve on error.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun int
dns_resolve_server_name_to_ip(const char * unc,char ** ip_addr)46*4882a593Smuzhiyun dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct sockaddr_storage ss;
49*4882a593Smuzhiyun const char *hostname, *sep;
50*4882a593Smuzhiyun char *name;
51*4882a593Smuzhiyun int len, rc;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun if (!ip_addr || !unc)
54*4882a593Smuzhiyun return -EINVAL;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun len = strlen(unc);
57*4882a593Smuzhiyun if (len < 3) {
58*4882a593Smuzhiyun cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
59*4882a593Smuzhiyun return -EINVAL;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Discount leading slashes for cifs */
63*4882a593Smuzhiyun len -= 2;
64*4882a593Smuzhiyun hostname = unc + 2;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Search for server name delimiter */
67*4882a593Smuzhiyun sep = memchr(hostname, '/', len);
68*4882a593Smuzhiyun if (sep)
69*4882a593Smuzhiyun len = sep - hostname;
70*4882a593Smuzhiyun else
71*4882a593Smuzhiyun cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
72*4882a593Smuzhiyun __func__, unc);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Try to interpret hostname as an IPv4 or IPv6 address */
75*4882a593Smuzhiyun rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
76*4882a593Smuzhiyun if (rc > 0)
77*4882a593Smuzhiyun goto name_is_IP_address;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* Perform the upcall */
80*4882a593Smuzhiyun rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
81*4882a593Smuzhiyun NULL, ip_addr, NULL, false);
82*4882a593Smuzhiyun if (rc < 0)
83*4882a593Smuzhiyun cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
84*4882a593Smuzhiyun __func__, len, len, hostname);
85*4882a593Smuzhiyun else
86*4882a593Smuzhiyun cifs_dbg(FYI, "%s: resolved: %*.*s to %s\n",
87*4882a593Smuzhiyun __func__, len, len, hostname, *ip_addr);
88*4882a593Smuzhiyun return rc;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun name_is_IP_address:
91*4882a593Smuzhiyun name = kmalloc(len + 1, GFP_KERNEL);
92*4882a593Smuzhiyun if (!name)
93*4882a593Smuzhiyun return -ENOMEM;
94*4882a593Smuzhiyun memcpy(name, hostname, len);
95*4882a593Smuzhiyun name[len] = 0;
96*4882a593Smuzhiyun cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",
97*4882a593Smuzhiyun __func__, name);
98*4882a593Smuzhiyun *ip_addr = name;
99*4882a593Smuzhiyun return 0;
100*4882a593Smuzhiyun }
101