aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorWang Lei <wang840925@gmail.com>2010-08-04 10:16:33 -0400
committerSteve French <sfrench@us.ibm.com>2010-08-05 13:17:51 -0400
commit1a4240f4764ac78adbf4b0ebb49b3bd8c72ffa11 (patch)
tree7d9de5b071e7ab8a8355bdf7902db4c0a0e812b1 /net
parentba5dadbf4e7b531bd7ccecffb4d3935c80a3372e (diff)
DNS: Separate out CIFS DNS Resolver code
Separate out the DNS resolver key type from the CIFS filesystem into its own module so that it can be made available for general use, including the AFS filesystem module. This facility makes it possible for the kernel to upcall to userspace to have it issue DNS requests, package up the replies and present them to the kernel in a useful form. The kernel is then able to cache the DNS replies as keys can be retained in keyrings. Resolver keys are of type "dns_resolver" and have a case-insensitive description that is of the form "[<type>:]<domain_name>". The optional <type> indicates the particular DNS lookup and packaging that's required. The <domain_name> is the query to be made. If <type> isn't given, a basic hostname to IP address lookup is made, and the result is stored in the key in the form of a printable string consisting of a comma-separated list of IPv4 and IPv6 addresses. This key type is supported by userspace helpers driven from /sbin/request-key and configured through /etc/request-key.conf. The cifs.upcall utility is invoked for UNC path server name to IP address resolution. The CIFS functionality is encapsulated by the dns_resolve_unc_to_ip() function, which is used to resolve a UNC path to an IP address for CIFS filesystem. This part remains in the CIFS module for now. See the added Documentation/networking/dns_resolver.txt for more information. Signed-off-by: Wang Lei <wang840925@gmail.com> Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Steve French <sfrench@us.ibm.com>
Diffstat (limited to 'net')
-rw-r--r--net/Kconfig1
-rw-r--r--net/Makefile1
-rw-r--r--net/dns_resolver/Kconfig27
-rw-r--r--net/dns_resolver/Makefile7
-rw-r--r--net/dns_resolver/dns_key.c210
-rw-r--r--net/dns_resolver/dns_query.c159
-rw-r--r--net/dns_resolver/internal.h44
7 files changed, 449 insertions, 0 deletions
diff --git a/net/Kconfig b/net/Kconfig
index e24fa0873f32..e330594d3709 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -213,6 +213,7 @@ source "net/phonet/Kconfig"
213source "net/ieee802154/Kconfig" 213source "net/ieee802154/Kconfig"
214source "net/sched/Kconfig" 214source "net/sched/Kconfig"
215source "net/dcb/Kconfig" 215source "net/dcb/Kconfig"
216source "net/dns_resolver/Kconfig"
216 217
217config RPS 218config RPS
218 boolean 219 boolean
diff --git a/net/Makefile b/net/Makefile
index 41d420070a38..ea60fbce9b1b 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -67,3 +67,4 @@ ifeq ($(CONFIG_NET),y)
67obj-$(CONFIG_SYSCTL) += sysctl_net.o 67obj-$(CONFIG_SYSCTL) += sysctl_net.o
68endif 68endif
69obj-$(CONFIG_WIMAX) += wimax/ 69obj-$(CONFIG_WIMAX) += wimax/
70obj-$(CONFIG_DNS_RESOLVER) += dns_resolver/
diff --git a/net/dns_resolver/Kconfig b/net/dns_resolver/Kconfig
new file mode 100644
index 000000000000..2ec47cb5d0d6
--- /dev/null
+++ b/net/dns_resolver/Kconfig
@@ -0,0 +1,27 @@
1#
2# Configuration for DNS Resolver
3#
4config DNS_RESOLVER
5 tristate "DNS Resolver support"
6 depends on NET && KEYS
7 help
8 Saying Y here will include support for the DNS Resolver key type
9 which can be used to make upcalls to perform DNS lookups in
10 userspace.
11
12 DNS Resolver is used to query DNS server for information. Examples
13 being resolving a UNC hostname element to an IP address for CIFS or
14 performing a DNS query for AFSDB records so that AFS can locate a
15 cell's volume location database servers.
16
17 DNS Resolver is used by the CIFS and AFS modules, and would support
18 samba4 later. DNS Resolver is supported by the userspace upcall
19 helper "/sbin/dns.resolver" via /etc/request-key.conf.
20
21 See <file:Documentation/networking/dns_resolver.txt> for further
22 information.
23
24 To compile this as a module, choose M here: the module will be called
25 dnsresolver.
26
27 If unsure, say N.
diff --git a/net/dns_resolver/Makefile b/net/dns_resolver/Makefile
new file mode 100644
index 000000000000..c0ef4e71dc49
--- /dev/null
+++ b/net/dns_resolver/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the Linux DNS Resolver.
3#
4
5obj-$(CONFIG_DNS_RESOLVER) += dns_resolver.o
6
7dns_resolver-objs := dns_key.o dns_query.o
diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c
new file mode 100644
index 000000000000..1b1b411adcf1
--- /dev/null
+++ b/net/dns_resolver/dns_key.c
@@ -0,0 +1,210 @@
1/* Key type used to cache DNS lookups made by the kernel
2 *
3 * See Documentation/networking/dns_resolver.txt
4 *
5 * Copyright (c) 2007 Igor Mammedov
6 * Author(s): Igor Mammedov (niallain@gmail.com)
7 * Steve French (sfrench@us.ibm.com)
8 * Wang Lei (wang840925@gmail.com)
9 * David Howells (dhowells@redhat.com)
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#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/keyctl.h>
31#include <keys/dns_resolver-type.h>
32#include <keys/user-type.h>
33#include "internal.h"
34
35MODULE_DESCRIPTION("DNS Resolver");
36MODULE_AUTHOR("Wang Lei");
37MODULE_LICENSE("GPL");
38
39unsigned dns_resolver_debug;
40module_param_named(debug, dns_resolver_debug, uint, S_IWUSR | S_IRUGO);
41MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
42
43const struct cred *dns_resolver_cache;
44
45/*
46 * Instantiate a user defined key for dns_resolver.
47 *
48 * The data must be a NUL-terminated string, with the NUL char accounted in
49 * datalen.
50 *
51 * If the data contains a '#' characters, then we take the clause after each
52 * one to be an option of the form 'key=value'. The actual data of interest is
53 * the string leading up to the first '#'. For instance:
54 *
55 * "ip1,ip2,...#foo=bar"
56 */
57static int
58dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
59{
60 struct user_key_payload *upayload;
61 int ret;
62 size_t result_len = 0;
63 const char *data = _data, *opt;
64
65 kenter("%%%d,%s,'%s',%zu",
66 key->serial, key->description, data, datalen);
67
68 if (datalen <= 1 || !data || data[datalen - 1] != '\0')
69 return -EINVAL;
70 datalen--;
71
72 /* deal with any options embedded in the data */
73 opt = memchr(data, '#', datalen);
74 if (!opt) {
75 kdebug("no options currently supported");
76 return -EINVAL;
77 }
78
79 result_len = datalen;
80 ret = key_payload_reserve(key, result_len);
81 if (ret < 0)
82 return -EINVAL;
83
84 upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
85 if (!upayload) {
86 kleave(" = -ENOMEM");
87 return -ENOMEM;
88 }
89
90 upayload->datalen = result_len;
91 memcpy(upayload->data, data, result_len);
92 upayload->data[result_len] = '\0';
93 rcu_assign_pointer(key->payload.data, upayload);
94
95 kleave(" = 0");
96 return 0;
97}
98
99/*
100 * The description is of the form "[<type>:]<domain_name>"
101 *
102 * The domain name may be a simple name or an absolute domain name (which
103 * should end with a period). The domain name is case-independent.
104 */
105static int
106dns_resolver_match(const struct key *key, const void *description)
107{
108 int slen, dlen, ret = 0;
109 const char *src = key->description, *dsp = description;
110
111 kenter("%s,%s", src, dsp);
112
113 if (!src || !dsp)
114 goto no_match;
115
116 if (strcasecmp(src, dsp) == 0)
117 goto matched;
118
119 slen = strlen(src);
120 dlen = strlen(dsp);
121 if (slen <= 0 || dlen <= 0)
122 goto no_match;
123 if (src[slen - 1] == '.')
124 slen--;
125 if (dsp[dlen - 1] == '.')
126 dlen--;
127 if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
128 goto no_match;
129
130matched:
131 ret = 1;
132no_match:
133 kleave(" = %d", ret);
134 return ret;
135}
136
137struct key_type key_type_dns_resolver = {
138 .name = "dns_resolver",
139 .instantiate = dns_resolver_instantiate,
140 .match = dns_resolver_match,
141 .revoke = user_revoke,
142 .destroy = user_destroy,
143 .describe = user_describe,
144 .read = user_read,
145};
146
147static int __init init_dns_resolver(void)
148{
149 struct cred *cred;
150 struct key *keyring;
151 int ret;
152
153 printk(KERN_NOTICE "Registering the %s key type\n",
154 key_type_dns_resolver.name);
155
156 /* create an override credential set with a special thread keyring in
157 * which DNS requests are cached
158 *
159 * this is used to prevent malicious redirections from being installed
160 * with add_key().
161 */
162 cred = prepare_kernel_cred(NULL);
163 if (!cred)
164 return -ENOMEM;
165
166 keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
167 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
168 KEY_USR_VIEW | KEY_USR_READ,
169 KEY_ALLOC_NOT_IN_QUOTA);
170 if (IS_ERR(keyring)) {
171 ret = PTR_ERR(keyring);
172 goto failed_put_cred;
173 }
174
175 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
176 if (ret < 0)
177 goto failed_put_key;
178
179 ret = register_key_type(&key_type_dns_resolver);
180 if (ret < 0)
181 goto failed_put_key;
182
183 /* instruct request_key() to use this special keyring as a cache for
184 * the results it looks up */
185 cred->thread_keyring = keyring;
186 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
187 dns_resolver_cache = cred;
188
189 kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
190 return 0;
191
192failed_put_key:
193 key_put(keyring);
194failed_put_cred:
195 put_cred(cred);
196 return ret;
197}
198
199static void __exit exit_dns_resolver(void)
200{
201 key_revoke(dns_resolver_cache->thread_keyring);
202 unregister_key_type(&key_type_dns_resolver);
203 put_cred(dns_resolver_cache);
204 printk(KERN_NOTICE "Unregistered %s key type\n",
205 key_type_dns_resolver.name);
206}
207
208module_init(init_dns_resolver)
209module_exit(exit_dns_resolver)
210MODULE_LICENSE("GPL");
diff --git a/net/dns_resolver/dns_query.c b/net/dns_resolver/dns_query.c
new file mode 100644
index 000000000000..6c0cf31ea00d
--- /dev/null
+++ b/net/dns_resolver/dns_query.c
@@ -0,0 +1,159 @@
1/* Upcall routine, designed to work as a key type and working through
2 * /sbin/request-key to contact userspace when handling DNS queries.
3 *
4 * See Documentation/networking/dns_resolver.txt
5 *
6 * Copyright (c) 2007 Igor Mammedov
7 * Author(s): Igor Mammedov (niallain@gmail.com)
8 * Steve French (sfrench@us.ibm.com)
9 * Wang Lei (wang840925@gmail.com)
10 * David Howells (dhowells@redhat.com)
11 *
12 * The upcall wrapper used to make an arbitrary DNS query.
13 *
14 * This function requires the appropriate userspace tool dns.upcall to be
15 * installed and something like the following lines should be added to the
16 * /etc/request-key.conf file:
17 *
18 * create dns_resolver * * /sbin/dns.upcall %k
19 *
20 * For example to use this module to query AFSDB RR:
21 *
22 * create dns_resolver afsdb:* * /sbin/dns.afsdb %k
23 *
24 * This library is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU Lesser General Public License as published
26 * by the Free Software Foundation; either version 2.1 of the License, or
27 * (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
32 * the GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * along with this library; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 */
38
39#include <linux/module.h>
40#include <linux/slab.h>
41#include <linux/dns_resolver.h>
42#include <keys/dns_resolver-type.h>
43#include <keys/user-type.h>
44
45#include "internal.h"
46
47/*
48 * dns_query - Query the DNS
49 * @type: Query type (or NULL for straight host->IP lookup)
50 * @name: Name to look up
51 * @namelen: Length of name
52 * @options: Request options (or NULL if no options)
53 * @_result: Where to place the returned data.
54 * @_expiry: Where to store the result expiry time (or NULL)
55 *
56 * The data will be returned in the pointer at *result, and the caller is
57 * responsible for freeing it.
58 *
59 * The description should be of the form "[<query_type>:]<domain_name>", and
60 * the options need to be appropriate for the query type requested. If no
61 * query_type is given, then the query is a straight hostname to IP address
62 * lookup.
63 *
64 * The DNS resolution lookup is performed by upcalling to userspace by way of
65 * requesting a key of type dns_resolver.
66 *
67 * Returns the size of the result on success, -ve error code otherwise.
68 */
69int dns_query(const char *type, const char *name, size_t namelen,
70 const char *options, char **_result, time_t *_expiry)
71{
72 struct key *rkey;
73 struct user_key_payload *upayload;
74 const struct cred *saved_cred;
75 size_t typelen, desclen;
76 char *desc, *cp;
77 int ret, len;
78
79 kenter("%s,%*.*s,%zu,%s",
80 type, (int)namelen, (int)namelen, name, namelen, options);
81
82 if (!name || namelen == 0 || !_result)
83 return -EINVAL;
84
85 /* construct the query key description as "[<type>:]<name>" */
86 typelen = 0;
87 desclen = 0;
88 if (type) {
89 typelen = strlen(type);
90 if (typelen < 1)
91 return -EINVAL;
92 desclen += typelen + 1;
93 }
94
95 if (!namelen)
96 namelen = strlen(name);
97 if (namelen < 3)
98 return -EINVAL;
99 desclen += namelen + 1;
100
101 desc = kmalloc(desclen, GFP_KERNEL);
102 if (!desc)
103 return -ENOMEM;
104
105 cp = desc;
106 if (type) {
107 memcpy(cp, type, typelen);
108 cp += typelen;
109 *cp++ = ':';
110 }
111 memcpy(cp, name, namelen);
112 cp += namelen;
113 *cp = '\0';
114
115 if (!options)
116 options = "";
117 kdebug("call request_key(,%s,%s)", desc, options);
118
119 /* make the upcall, using special credentials to prevent the use of
120 * add_key() to preinstall malicious redirections
121 */
122 saved_cred = override_creds(dns_resolver_cache);
123 rkey = request_key(&key_type_dns_resolver, desc, options);
124 revert_creds(saved_cred);
125 kfree(desc);
126 if (IS_ERR(rkey)) {
127 ret = PTR_ERR(rkey);
128 goto out;
129 }
130
131 down_read(&rkey->sem);
132 rkey->perm |= KEY_USR_VIEW;
133
134 ret = key_validate(rkey);
135 if (ret < 0)
136 goto put;
137
138 upayload = rcu_dereference_protected(rkey->payload.data,
139 lockdep_is_held(&rkey->sem));
140 len = upayload->datalen;
141
142 ret = -ENOMEM;
143 *_result = kmalloc(len + 1, GFP_KERNEL);
144 if (!*_result)
145 goto put;
146
147 memcpy(*_result, upayload->data, len + 1);
148 if (_expiry)
149 *_expiry = rkey->expiry;
150
151 ret = len;
152put:
153 up_read(&rkey->sem);
154 key_put(rkey);
155out:
156 kleave(" = %d", ret);
157 return ret;
158}
159EXPORT_SYMBOL(dns_query);
diff --git a/net/dns_resolver/internal.h b/net/dns_resolver/internal.h
new file mode 100644
index 000000000000..189ca9e9b785
--- /dev/null
+++ b/net/dns_resolver/internal.h
@@ -0,0 +1,44 @@
1/*
2 * Copyright (c) 2010 Wang Lei
3 * Author(s): Wang Lei (wang840925@gmail.com). All Rights Reserved.
4 *
5 * Internal DNS Rsolver stuff
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/compiler.h>
23#include <linux/kernel.h>
24#include <linux/sched.h>
25
26/*
27 * dns_key.c
28 */
29extern const struct cred *dns_resolver_cache;
30
31/*
32 * debug tracing
33 */
34extern unsigned dns_resolver_debug;
35
36#define kdebug(FMT, ...) \
37do { \
38 if (unlikely(dns_resolver_debug)) \
39 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", \
40 current->comm, ##__VA_ARGS__); \
41} while (0)
42
43#define kenter(FMT, ...) kdebug("==> %s("FMT")", __func__, ##__VA_ARGS__)
44#define kleave(FMT, ...) kdebug("<== %s()"FMT"", __func__, ##__VA_ARGS__)