blob: f730ef35499e4d93f73aade10da408bcb9053716 [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
Steve Frenchd09e8602008-04-26 00:22:23 +000058/* Checks if supplied name is IP address
59 * returns:
60 * 1 - name is IP
61 * 0 - name is not IP
62 */
63static int is_ip(const char *name)
64{
65 int rc;
66 struct sockaddr_in sin_server;
67 struct sockaddr_in6 sin_server6;
68
69 rc = cifs_inet_pton(AF_INET, name,
70 &sin_server.sin_addr.s_addr);
71
72 if (rc <= 0) {
73 /* not ipv4 address, try ipv6 */
74 rc = cifs_inet_pton(AF_INET6, name,
75 &sin_server6.sin6_addr.in6_u);
76 if (rc > 0)
77 return 1;
78 } else {
79 return 1;
80 }
81 /* we failed translating address */
82 return 0;
83}
Steve French197c1832008-01-10 17:10:23 +000084
85/* Resolves server name to ip address.
86 * input:
87 * unc - server UNC
88 * output:
89 * *ip_addr - pointer to server ip, caller responcible for freeing it.
90 * return 0 on success
91 */
92int
Steve French366781c2008-01-25 10:12:41 +000093dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
94{
Steve French197c1832008-01-10 17:10:23 +000095 int rc = -EAGAIN;
Steve Frenchd09e8602008-04-26 00:22:23 +000096 struct key *rkey = ERR_PTR(-EAGAIN);
Steve French197c1832008-01-10 17:10:23 +000097 char *name;
Steve Frenchd09e8602008-04-26 00:22:23 +000098 char *data = NULL;
Steve French197c1832008-01-10 17:10:23 +000099 int len;
100
Steve French366781c2008-01-25 10:12:41 +0000101 if (!ip_addr || !unc)
Steve French197c1832008-01-10 17:10:23 +0000102 return -EINVAL;
103
104 /* search for server name delimiter */
105 len = strlen(unc);
106 if (len < 3) {
Harvey Harrison55f78e12008-03-10 17:14:34 +0000107 cFYI(1, ("%s: unc is too short: %s", __func__, unc));
Steve French197c1832008-01-10 17:10:23 +0000108 return -EINVAL;
109 }
110 len -= 2;
111 name = memchr(unc+2, '\\', len);
112 if (!name) {
113 cFYI(1, ("%s: probably server name is whole unc: %s",
Harvey Harrison55f78e12008-03-10 17:14:34 +0000114 __func__, unc));
Steve French197c1832008-01-10 17:10:23 +0000115 } else {
116 len = (name - unc) - 2/* leading // */;
117 }
118
119 name = kmalloc(len+1, GFP_KERNEL);
120 if (!name) {
121 rc = -ENOMEM;
122 return rc;
123 }
124 memcpy(name, unc+2, len);
125 name[len] = 0;
126
Steve Frenchd09e8602008-04-26 00:22:23 +0000127 if (is_ip(name)) {
128 cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
129 __func__, name));
130 data = name;
131 goto skip_upcall;
132 }
133
Steve French197c1832008-01-10 17:10:23 +0000134 rkey = request_key(&key_type_dns_resolver, name, "");
135 if (!IS_ERR(rkey)) {
Steve Frenchd09e8602008-04-26 00:22:23 +0000136 data = rkey->payload.data;
Steve Frenchd09e8602008-04-26 00:22:23 +0000137 } else {
138 cERROR(1, ("%s: unable to resolve: %s", __func__, name));
139 goto out;
140 }
141
142skip_upcall:
143 if (data) {
144 len = strlen(data);
145 *ip_addr = kmalloc(len+1, GFP_KERNEL);
146 if (*ip_addr) {
147 memcpy(*ip_addr, data, len);
148 (*ip_addr)[len] = '\0';
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