blob: 87948147d7ece68d64e7218c8331624410ac2196 [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
Steve Frenchd09e8602008-04-26 00:22:23 +000032/* Checks if supplied name is IP address
33 * returns:
34 * 1 - name is IP
35 * 0 - name is not IP
36 */
Steve French9d815232008-09-23 18:46:07 +000037static int
Jeff Layton681bf722009-06-11 10:27:31 -040038is_ip(char *name)
Steve Frenchd09e8602008-04-26 00:22:23 +000039{
Jeff Layton1e68b2b2009-06-11 10:27:30 -040040 struct sockaddr_storage ss;
Steve Frenchd09e8602008-04-26 00:22:23 +000041
Jeff Layton1e68b2b2009-06-11 10:27:30 -040042 return cifs_convert_address(name, &ss);
Steve Frenchd09e8602008-04-26 00:22:23 +000043}
Steve French197c1832008-01-10 17:10:23 +000044
Steve French9d815232008-09-23 18:46:07 +000045static int
46dns_resolver_instantiate(struct key *key, const void *data,
47 size_t datalen)
48{
49 int rc = 0;
50 char *ip;
51
52 ip = kmalloc(datalen + 1, GFP_KERNEL);
53 if (!ip)
54 return -ENOMEM;
55
56 memcpy(ip, data, datalen);
57 ip[datalen] = '\0';
58
59 /* make sure this looks like an address */
Jeff Layton681bf722009-06-11 10:27:31 -040060 if (!is_ip(ip)) {
Steve French9d815232008-09-23 18:46:07 +000061 kfree(ip);
62 return -EINVAL;
63 }
64
65 key->type_data.x[0] = datalen;
Jeff Laytond9fb5c02009-03-23 01:47:11 -040066 key->payload.data = ip;
Steve French9d815232008-09-23 18:46:07 +000067
68 return rc;
69}
70
71static void
72dns_resolver_destroy(struct key *key)
73{
74 kfree(key->payload.data);
75}
76
77struct key_type key_type_dns_resolver = {
78 .name = "dns_resolver",
79 .def_datalen = sizeof(struct in_addr),
80 .describe = user_describe,
81 .instantiate = dns_resolver_instantiate,
82 .destroy = dns_resolver_destroy,
83 .match = user_match,
84};
85
Steve French197c1832008-01-10 17:10:23 +000086/* Resolves server name to ip address.
87 * input:
88 * unc - server UNC
89 * output:
90 * *ip_addr - pointer to server ip, caller responcible for freeing it.
91 * return 0 on success
92 */
93int
Steve French366781c2008-01-25 10:12:41 +000094dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
95{
Steve French197c1832008-01-10 17:10:23 +000096 int rc = -EAGAIN;
Steve Frenchd09e8602008-04-26 00:22:23 +000097 struct key *rkey = ERR_PTR(-EAGAIN);
Steve French197c1832008-01-10 17:10:23 +000098 char *name;
Steve Frenchd09e8602008-04-26 00:22:23 +000099 char *data = NULL;
Steve French197c1832008-01-10 17:10:23 +0000100 int len;
101
Steve French366781c2008-01-25 10:12:41 +0000102 if (!ip_addr || !unc)
Steve French197c1832008-01-10 17:10:23 +0000103 return -EINVAL;
104
105 /* search for server name delimiter */
106 len = strlen(unc);
107 if (len < 3) {
Harvey Harrison55f78e12008-03-10 17:14:34 +0000108 cFYI(1, ("%s: unc is too short: %s", __func__, unc));
Steve French197c1832008-01-10 17:10:23 +0000109 return -EINVAL;
110 }
111 len -= 2;
112 name = memchr(unc+2, '\\', len);
113 if (!name) {
114 cFYI(1, ("%s: probably server name is whole unc: %s",
Harvey Harrison55f78e12008-03-10 17:14:34 +0000115 __func__, unc));
Steve French197c1832008-01-10 17:10:23 +0000116 } else {
117 len = (name - unc) - 2/* leading // */;
118 }
119
120 name = kmalloc(len+1, GFP_KERNEL);
121 if (!name) {
122 rc = -ENOMEM;
123 return rc;
124 }
125 memcpy(name, unc+2, len);
126 name[len] = 0;
127
Steve Frenchd09e8602008-04-26 00:22:23 +0000128 if (is_ip(name)) {
129 cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
130 __func__, name));
131 data = name;
132 goto skip_upcall;
133 }
134
Steve French197c1832008-01-10 17:10:23 +0000135 rkey = request_key(&key_type_dns_resolver, name, "");
136 if (!IS_ERR(rkey)) {
Steve French9d815232008-09-23 18:46:07 +0000137 len = rkey->type_data.x[0];
Steve Frenchd09e8602008-04-26 00:22:23 +0000138 data = rkey->payload.data;
Steve Frenchd09e8602008-04-26 00:22:23 +0000139 } else {
140 cERROR(1, ("%s: unable to resolve: %s", __func__, name));
141 goto out;
142 }
143
144skip_upcall:
145 if (data) {
Steve French9d815232008-09-23 18:46:07 +0000146 *ip_addr = kmalloc(len + 1, GFP_KERNEL);
Steve Frenchd09e8602008-04-26 00:22:23 +0000147 if (*ip_addr) {
Steve French9d815232008-09-23 18:46:07 +0000148 memcpy(*ip_addr, data, len + 1);
Igor Mammedov5651ced2008-05-20 13:02:01 +0400149 if (!IS_ERR(rkey))
150 cFYI(1, ("%s: resolved: %s to %s", __func__,
151 name,
152 *ip_addr
153 ));
Steve French197c1832008-01-10 17:10:23 +0000154 rc = 0;
155 } else {
156 rc = -ENOMEM;
157 }
Steve Frenchd09e8602008-04-26 00:22:23 +0000158 if (!IS_ERR(rkey))
159 key_put(rkey);
Steve French197c1832008-01-10 17:10:23 +0000160 }
161
Steve Frenchd09e8602008-04-26 00:22:23 +0000162out:
Steve French197c1832008-01-10 17:10:23 +0000163 kfree(name);
164 return rc;
165}
166
167