Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 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) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 8 | * Wang Lei (wang840925@gmail.com) |
| 9 | * David Howells (dhowells@redhat.com) |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 10 | * |
| 11 | * Contains the CIFS DFS upcall routines used for hostname to |
| 12 | * IP address translation. |
| 13 | * |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 17 | #include <linux/dns_resolver.h> |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 18 | #include "dns_resolve.h" |
| 19 | #include "cifsglob.h" |
| 20 | #include "cifsproto.h" |
| 21 | #include "cifs_debug.h" |
| 22 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 23 | /** |
| 24 | * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address. |
Jeff Layton | d9deef0 | 2013-05-24 07:40:06 -0400 | [diff] [blame] | 25 | * @unc: UNC path specifying the server (with '/' as delimiter) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 26 | * @ip_addr: Where to return the IP address. |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 27 | * @expiry: Where to return the expiry time for the dns record. |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 28 | * |
| 29 | * The IP address will be returned in string form, and the caller is |
| 30 | * responsible for freeing it. |
| 31 | * |
| 32 | * Returns length of result on success, -ve on error. |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 33 | */ |
| 34 | int |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 35 | dns_resolve_server_name_to_ip(const char *unc, char **ip_addr, time64_t *expiry) |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 36 | { |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 37 | struct sockaddr_storage ss; |
| 38 | const char *hostname, *sep; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 39 | char *name; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 40 | int len, rc; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 41 | |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 42 | if (!ip_addr || !unc) |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 45 | len = strlen(unc); |
| 46 | if (len < 3) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 47 | cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 48 | return -EINVAL; |
| 49 | } |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 50 | |
| 51 | /* Discount leading slashes for cifs */ |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 52 | len -= 2; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 53 | hostname = unc + 2; |
| 54 | |
| 55 | /* Search for server name delimiter */ |
Jeff Layton | d9deef0 | 2013-05-24 07:40:06 -0400 | [diff] [blame] | 56 | sep = memchr(hostname, '/', len); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 57 | if (sep) |
Jeff Layton | ba03864 | 2010-11-30 15:14:48 -0500 | [diff] [blame] | 58 | len = sep - hostname; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 59 | else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 60 | cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n", |
| 61 | __func__, unc); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 62 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 63 | /* Try to interpret hostname as an IPv4 or IPv6 address */ |
| 64 | rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len); |
| 65 | if (rc > 0) |
| 66 | goto name_is_IP_address; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 67 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 68 | /* Perform the upcall */ |
David Howells | a58946c | 2019-06-26 21:02:33 +0100 | [diff] [blame] | 69 | rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len, |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 70 | NULL, ip_addr, expiry, false); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 71 | if (rc < 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 72 | cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n", |
| 73 | __func__, len, len, hostname); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 74 | else |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 75 | cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n", |
| 76 | __func__, len, len, hostname, *ip_addr, |
| 77 | expiry ? (*expiry) : 0); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 78 | return rc; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 79 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 80 | name_is_IP_address: |
| 81 | name = kmalloc(len + 1, GFP_KERNEL); |
| 82 | if (!name) |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 83 | return -ENOMEM; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 84 | memcpy(name, hostname, len); |
| 85 | name[len] = 0; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 86 | cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n", |
| 87 | __func__, name); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 88 | *ip_addr = name; |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 89 | return 0; |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 90 | } |