blob: 7cc86c418182f261954dbcd18e8efd09b08df108 [file] [log] [blame]
Steve French197c1832008-01-10 17:10:23 +00001/*
2 * fs/cifs/dns_resolve.c
3 *
4 * Copyright (c) 2007 Igor Mammedov
5 * Author(s): Igor Mammedov (niallain@gmail.com)
6 * Steve French (sfrench@us.ibm.com)
7 *
8 * Contains the CIFS DFS upcall routines used for hostname to
9 * IP address translation.
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include <keys/user-type.h>
27#include "dns_resolve.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
31
32static int dns_resolver_instantiate(struct key *key, const void *data,
33 size_t datalen)
34{
35 int rc = 0;
36 char *ip;
37
38 ip = kmalloc(datalen+1, GFP_KERNEL);
39 if (!ip)
40 return -ENOMEM;
41
42 memcpy(ip, data, datalen);
43 ip[datalen] = '\0';
44
45 rcu_assign_pointer(key->payload.data, ip);
46
47 return rc;
48}
49
50struct key_type key_type_dns_resolver = {
51 .name = "dns_resolver",
52 .def_datalen = sizeof(struct in_addr),
53 .describe = user_describe,
54 .instantiate = dns_resolver_instantiate,
55 .match = user_match,
56};
57
58
59/* Resolves server name to ip address.
60 * input:
61 * unc - server UNC
62 * output:
63 * *ip_addr - pointer to server ip, caller responcible for freeing it.
64 * return 0 on success
65 */
66int
Steve French366781c2008-01-25 10:12:41 +000067dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
68{
Steve French197c1832008-01-10 17:10:23 +000069 int rc = -EAGAIN;
70 struct key *rkey;
71 char *name;
72 int len;
73
Steve French366781c2008-01-25 10:12:41 +000074 if (!ip_addr || !unc)
Steve French197c1832008-01-10 17:10:23 +000075 return -EINVAL;
76
77 /* search for server name delimiter */
78 len = strlen(unc);
79 if (len < 3) {
Harvey Harrison55f78e12008-03-10 17:14:34 +000080 cFYI(1, ("%s: unc is too short: %s", __func__, unc));
Steve French197c1832008-01-10 17:10:23 +000081 return -EINVAL;
82 }
83 len -= 2;
84 name = memchr(unc+2, '\\', len);
85 if (!name) {
86 cFYI(1, ("%s: probably server name is whole unc: %s",
Harvey Harrison55f78e12008-03-10 17:14:34 +000087 __func__, unc));
Steve French197c1832008-01-10 17:10:23 +000088 } else {
89 len = (name - unc) - 2/* leading // */;
90 }
91
92 name = kmalloc(len+1, GFP_KERNEL);
93 if (!name) {
94 rc = -ENOMEM;
95 return rc;
96 }
97 memcpy(name, unc+2, len);
98 name[len] = 0;
99
100 rkey = request_key(&key_type_dns_resolver, name, "");
101 if (!IS_ERR(rkey)) {
102 len = strlen(rkey->payload.data);
103 *ip_addr = kmalloc(len+1, GFP_KERNEL);
104 if (*ip_addr) {
105 memcpy(*ip_addr, rkey->payload.data, len);
106 (*ip_addr)[len] = '\0';
Harvey Harrison55f78e12008-03-10 17:14:34 +0000107 cFYI(1, ("%s: resolved: %s to %s", __func__,
Steve French197c1832008-01-10 17:10:23 +0000108 rkey->description,
109 *ip_addr
110 ));
111 rc = 0;
112 } else {
113 rc = -ENOMEM;
114 }
115 key_put(rkey);
116 } else {
Harvey Harrison55f78e12008-03-10 17:14:34 +0000117 cERROR(1, ("%s: unable to resolve: %s", __func__, name));
Steve French197c1832008-01-10 17:10:23 +0000118 }
119
120 kfree(name);
121 return rc;
122}
123
124