[CIFS] Add destroy routine for dns_resolver
[safe/jmp/linux-2.6] / fs / cifs / dns_resolve.c
1 /*
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
32 static 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
50 static void
51 dns_resolver_destroy(struct key *key)
52 {
53         kfree(key->payload.data);
54 }
55
56 struct key_type key_type_dns_resolver = {
57         .name        = "dns_resolver",
58         .def_datalen = sizeof(struct in_addr),
59         .describe    = user_describe,
60         .instantiate = dns_resolver_instantiate,
61         .destroy     = dns_resolver_destroy,
62         .match       = user_match,
63 };
64
65 /* Checks if supplied name is IP address
66  * returns:
67  *              1 - name is IP
68  *              0 - name is not IP
69  */
70 static int is_ip(const char *name)
71 {
72         int rc;
73         struct sockaddr_in sin_server;
74         struct sockaddr_in6 sin_server6;
75
76         rc = cifs_inet_pton(AF_INET, name,
77                         &sin_server.sin_addr.s_addr);
78
79         if (rc <= 0) {
80                 /* not ipv4 address, try ipv6 */
81                 rc = cifs_inet_pton(AF_INET6, name,
82                                 &sin_server6.sin6_addr.in6_u);
83                 if (rc > 0)
84                         return 1;
85         } else {
86                 return 1;
87         }
88         /* we failed translating address */
89         return 0;
90 }
91
92 /* Resolves server name to ip address.
93  * input:
94  *      unc - server UNC
95  * output:
96  *      *ip_addr - pointer to server ip, caller responcible for freeing it.
97  * return 0 on success
98  */
99 int
100 dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
101 {
102         int rc = -EAGAIN;
103         struct key *rkey = ERR_PTR(-EAGAIN);
104         char *name;
105         char *data = NULL;
106         int len;
107
108         if (!ip_addr || !unc)
109                 return -EINVAL;
110
111         /* search for server name delimiter */
112         len = strlen(unc);
113         if (len < 3) {
114                 cFYI(1, ("%s: unc is too short: %s", __func__, unc));
115                 return -EINVAL;
116         }
117         len -= 2;
118         name = memchr(unc+2, '\\', len);
119         if (!name) {
120                 cFYI(1, ("%s: probably server name is whole unc: %s",
121                                         __func__, unc));
122         } else {
123                 len = (name - unc) - 2/* leading // */;
124         }
125
126         name = kmalloc(len+1, GFP_KERNEL);
127         if (!name) {
128                 rc = -ENOMEM;
129                 return rc;
130         }
131         memcpy(name, unc+2, len);
132         name[len] = 0;
133
134         if (is_ip(name)) {
135                 cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
136                                         __func__, name));
137                 data = name;
138                 goto skip_upcall;
139         }
140
141         rkey = request_key(&key_type_dns_resolver, name, "");
142         if (!IS_ERR(rkey)) {
143                 data = rkey->payload.data;
144         } else {
145                 cERROR(1, ("%s: unable to resolve: %s", __func__, name));
146                 goto out;
147         }
148
149 skip_upcall:
150         if (data) {
151                 len = strlen(data);
152                 *ip_addr = kmalloc(len+1, GFP_KERNEL);
153                 if (*ip_addr) {
154                         memcpy(*ip_addr, data, len);
155                         (*ip_addr)[len] = '\0';
156                         if (!IS_ERR(rkey))
157                                 cFYI(1, ("%s: resolved: %s to %s", __func__,
158                                                         name,
159                                                         *ip_addr
160                                         ));
161                         rc = 0;
162                 } else {
163                         rc = -ENOMEM;
164                 }
165                 if (!IS_ERR(rkey))
166                         key_put(rkey);
167         }
168
169 out:
170         kfree(name);
171         return rc;
172 }
173
174