blob: 0458d28d71aa6590376f94200f79c3d3e1c38915 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Steve French197c1832008-01-10 17:10:23 +00002/*
Steve French197c1832008-01-10 17:10:23 +00003 *
4 * Copyright (c) 2007 Igor Mammedov
5 * Author(s): Igor Mammedov (niallain@gmail.com)
6 * Steve French (sfrench@us.ibm.com)
Wang Lei1a4240f2010-08-04 15:16:33 +01007 * Wang Lei (wang840925@gmail.com)
8 * David Howells (dhowells@redhat.com)
Steve French197c1832008-01-10 17:10:23 +00009 *
10 * Contains the CIFS DFS upcall routines used for hostname to
11 * IP address translation.
12 *
Steve French197c1832008-01-10 17:10:23 +000013 */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Wang Lei1a4240f2010-08-04 15:16:33 +010016#include <linux/dns_resolver.h>
Steve French197c1832008-01-10 17:10:23 +000017#include "dns_resolve.h"
18#include "cifsglob.h"
19#include "cifsproto.h"
20#include "cifs_debug.h"
21
Wang Lei1a4240f2010-08-04 15:16:33 +010022/**
23 * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
Jeff Laytond9deef02013-05-24 07:40:06 -040024 * @unc: UNC path specifying the server (with '/' as delimiter)
Wang Lei1a4240f2010-08-04 15:16:33 +010025 * @ip_addr: Where to return the IP address.
Shyam Prasad N506c1da2021-05-18 15:05:50 +000026 * @expiry: Where to return the expiry time for the dns record.
Wang Lei1a4240f2010-08-04 15:16:33 +010027 *
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.
Steve French197c1832008-01-10 17:10:23 +000032 */
33int
Shyam Prasad N506c1da2021-05-18 15:05:50 +000034dns_resolve_server_name_to_ip(const char *unc, char **ip_addr, time64_t *expiry)
Steve French366781c2008-01-25 10:12:41 +000035{
Wang Lei1a4240f2010-08-04 15:16:33 +010036 struct sockaddr_storage ss;
37 const char *hostname, *sep;
Steve French197c1832008-01-10 17:10:23 +000038 char *name;
Wang Lei1a4240f2010-08-04 15:16:33 +010039 int len, rc;
Steve French197c1832008-01-10 17:10:23 +000040
Steve French366781c2008-01-25 10:12:41 +000041 if (!ip_addr || !unc)
Steve French197c1832008-01-10 17:10:23 +000042 return -EINVAL;
43
Steve French197c1832008-01-10 17:10:23 +000044 len = strlen(unc);
45 if (len < 3) {
Joe Perchesf96637b2013-05-04 22:12:25 -050046 cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
Steve French197c1832008-01-10 17:10:23 +000047 return -EINVAL;
48 }
Wang Lei1a4240f2010-08-04 15:16:33 +010049
50 /* Discount leading slashes for cifs */
Steve French197c1832008-01-10 17:10:23 +000051 len -= 2;
Wang Lei1a4240f2010-08-04 15:16:33 +010052 hostname = unc + 2;
53
54 /* Search for server name delimiter */
Jeff Laytond9deef02013-05-24 07:40:06 -040055 sep = memchr(hostname, '/', len);
Wang Lei1a4240f2010-08-04 15:16:33 +010056 if (sep)
Jeff Laytonba038642010-11-30 15:14:48 -050057 len = sep - hostname;
Wang Lei1a4240f2010-08-04 15:16:33 +010058 else
Joe Perchesf96637b2013-05-04 22:12:25 -050059 cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
60 __func__, unc);
Steve French197c1832008-01-10 17:10:23 +000061
Wang Lei1a4240f2010-08-04 15:16:33 +010062 /* 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;
Steve French197c1832008-01-10 17:10:23 +000066
Wang Lei1a4240f2010-08-04 15:16:33 +010067 /* Perform the upcall */
David Howellsa58946c2019-06-26 21:02:33 +010068 rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
Shyam Prasad N506c1da2021-05-18 15:05:50 +000069 NULL, ip_addr, expiry, false);
Wang Lei1a4240f2010-08-04 15:16:33 +010070 if (rc < 0)
Joe Perchesf96637b2013-05-04 22:12:25 -050071 cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
72 __func__, len, len, hostname);
Wang Lei1a4240f2010-08-04 15:16:33 +010073 else
Shyam Prasad N506c1da2021-05-18 15:05:50 +000074 cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n",
75 __func__, len, len, hostname, *ip_addr,
76 expiry ? (*expiry) : 0);
Steve French197c1832008-01-10 17:10:23 +000077 return rc;
Steve French197c1832008-01-10 17:10:23 +000078
Wang Lei1a4240f2010-08-04 15:16:33 +010079name_is_IP_address:
80 name = kmalloc(len + 1, GFP_KERNEL);
81 if (!name)
David Howells4c0c03c2010-07-22 12:53:18 +010082 return -ENOMEM;
Wang Lei1a4240f2010-08-04 15:16:33 +010083 memcpy(name, hostname, len);
84 name[len] = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -050085 cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",
86 __func__, name);
Wang Lei1a4240f2010-08-04 15:16:33 +010087 *ip_addr = name;
David Howells4c0c03c2010-07-22 12:53:18 +010088 return 0;
David Howells4c0c03c2010-07-22 12:53:18 +010089}