netfilter: xt_recent: fix regression in rules using a zero hit_count
[linux-2.6/mini2440.git] / fs / nfs / dns_resolve.c
blobc1fd68bf337d63ffa85dd16435a26527704e5c68
1 /*
2 * linux/fs/nfs/dns_resolve.c
4 * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
6 * Resolves DNS hostnames into valid ip addresses
7 */
9 #include <linux/hash.h>
10 #include <linux/string.h>
11 #include <linux/kmod.h>
12 #include <linux/module.h>
13 #include <linux/socket.h>
14 #include <linux/seq_file.h>
15 #include <linux/inet.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/cache.h>
18 #include <linux/sunrpc/svcauth.h>
20 #include "dns_resolve.h"
21 #include "cache_lib.h"
23 #define NFS_DNS_HASHBITS 4
24 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
26 static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
28 struct nfs_dns_ent {
29 struct cache_head h;
31 char *hostname;
32 size_t namelen;
34 struct sockaddr_storage addr;
35 size_t addrlen;
39 static void nfs_dns_ent_update(struct cache_head *cnew,
40 struct cache_head *ckey)
42 struct nfs_dns_ent *new;
43 struct nfs_dns_ent *key;
45 new = container_of(cnew, struct nfs_dns_ent, h);
46 key = container_of(ckey, struct nfs_dns_ent, h);
48 memcpy(&new->addr, &key->addr, key->addrlen);
49 new->addrlen = key->addrlen;
52 static void nfs_dns_ent_init(struct cache_head *cnew,
53 struct cache_head *ckey)
55 struct nfs_dns_ent *new;
56 struct nfs_dns_ent *key;
58 new = container_of(cnew, struct nfs_dns_ent, h);
59 key = container_of(ckey, struct nfs_dns_ent, h);
61 kfree(new->hostname);
62 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
63 if (new->hostname) {
64 new->namelen = key->namelen;
65 nfs_dns_ent_update(cnew, ckey);
66 } else {
67 new->namelen = 0;
68 new->addrlen = 0;
72 static void nfs_dns_ent_put(struct kref *ref)
74 struct nfs_dns_ent *item;
76 item = container_of(ref, struct nfs_dns_ent, h.ref);
77 kfree(item->hostname);
78 kfree(item);
81 static struct cache_head *nfs_dns_ent_alloc(void)
83 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
85 if (item != NULL) {
86 item->hostname = NULL;
87 item->namelen = 0;
88 item->addrlen = 0;
89 return &item->h;
91 return NULL;
94 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
96 return hash_str(key->hostname, NFS_DNS_HASHBITS);
99 static void nfs_dns_request(struct cache_detail *cd,
100 struct cache_head *ch,
101 char **bpp, int *blen)
103 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
105 qword_add(bpp, blen, key->hostname);
106 (*bpp)[-1] = '\n';
109 static int nfs_dns_upcall(struct cache_detail *cd,
110 struct cache_head *ch)
112 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
113 int ret;
115 ret = nfs_cache_upcall(cd, key->hostname);
116 if (ret)
117 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
118 return ret;
121 static int nfs_dns_match(struct cache_head *ca,
122 struct cache_head *cb)
124 struct nfs_dns_ent *a;
125 struct nfs_dns_ent *b;
127 a = container_of(ca, struct nfs_dns_ent, h);
128 b = container_of(cb, struct nfs_dns_ent, h);
130 if (a->namelen == 0 || a->namelen != b->namelen)
131 return 0;
132 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
135 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
136 struct cache_head *h)
138 struct nfs_dns_ent *item;
139 long ttl;
141 if (h == NULL) {
142 seq_puts(m, "# ip address hostname ttl\n");
143 return 0;
145 item = container_of(h, struct nfs_dns_ent, h);
146 ttl = (long)item->h.expiry_time - (long)get_seconds();
147 if (ttl < 0)
148 ttl = 0;
150 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
151 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
153 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
154 seq_printf(m, "%15s ", buf);
155 } else
156 seq_puts(m, "<none> ");
157 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
158 return 0;
161 struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
162 struct nfs_dns_ent *key)
164 struct cache_head *ch;
166 ch = sunrpc_cache_lookup(cd,
167 &key->h,
168 nfs_dns_hash(key));
169 if (!ch)
170 return NULL;
171 return container_of(ch, struct nfs_dns_ent, h);
174 struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
175 struct nfs_dns_ent *new,
176 struct nfs_dns_ent *key)
178 struct cache_head *ch;
180 ch = sunrpc_cache_update(cd,
181 &new->h, &key->h,
182 nfs_dns_hash(key));
183 if (!ch)
184 return NULL;
185 return container_of(ch, struct nfs_dns_ent, h);
188 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
190 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
191 struct nfs_dns_ent key, *item;
192 unsigned long ttl;
193 ssize_t len;
194 int ret = -EINVAL;
196 if (buf[buflen-1] != '\n')
197 goto out;
198 buf[buflen-1] = '\0';
200 len = qword_get(&buf, buf1, sizeof(buf1));
201 if (len <= 0)
202 goto out;
203 key.addrlen = rpc_pton(buf1, len,
204 (struct sockaddr *)&key.addr,
205 sizeof(key.addr));
207 len = qword_get(&buf, buf1, sizeof(buf1));
208 if (len <= 0)
209 goto out;
211 key.hostname = buf1;
212 key.namelen = len;
213 memset(&key.h, 0, sizeof(key.h));
215 ttl = get_expiry(&buf);
216 if (ttl == 0)
217 goto out;
218 key.h.expiry_time = ttl + get_seconds();
220 ret = -ENOMEM;
221 item = nfs_dns_lookup(cd, &key);
222 if (item == NULL)
223 goto out;
225 if (key.addrlen == 0)
226 set_bit(CACHE_NEGATIVE, &key.h.flags);
228 item = nfs_dns_update(cd, &key, item);
229 if (item == NULL)
230 goto out;
232 ret = 0;
233 cache_put(&item->h, cd);
234 out:
235 return ret;
238 static struct cache_detail nfs_dns_resolve = {
239 .owner = THIS_MODULE,
240 .hash_size = NFS_DNS_HASHTBL_SIZE,
241 .hash_table = nfs_dns_table,
242 .name = "dns_resolve",
243 .cache_put = nfs_dns_ent_put,
244 .cache_upcall = nfs_dns_upcall,
245 .cache_parse = nfs_dns_parse,
246 .cache_show = nfs_dns_show,
247 .match = nfs_dns_match,
248 .init = nfs_dns_ent_init,
249 .update = nfs_dns_ent_update,
250 .alloc = nfs_dns_ent_alloc,
253 static int do_cache_lookup(struct cache_detail *cd,
254 struct nfs_dns_ent *key,
255 struct nfs_dns_ent **item,
256 struct nfs_cache_defer_req *dreq)
258 int ret = -ENOMEM;
260 *item = nfs_dns_lookup(cd, key);
261 if (*item) {
262 ret = cache_check(cd, &(*item)->h, &dreq->req);
263 if (ret)
264 *item = NULL;
266 return ret;
269 static int do_cache_lookup_nowait(struct cache_detail *cd,
270 struct nfs_dns_ent *key,
271 struct nfs_dns_ent **item)
273 int ret = -ENOMEM;
275 *item = nfs_dns_lookup(cd, key);
276 if (!*item)
277 goto out_err;
278 ret = -ETIMEDOUT;
279 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
280 || (*item)->h.expiry_time < get_seconds()
281 || cd->flush_time > (*item)->h.last_refresh)
282 goto out_put;
283 ret = -ENOENT;
284 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
285 goto out_put;
286 return 0;
287 out_put:
288 cache_put(&(*item)->h, cd);
289 out_err:
290 *item = NULL;
291 return ret;
294 static int do_cache_lookup_wait(struct cache_detail *cd,
295 struct nfs_dns_ent *key,
296 struct nfs_dns_ent **item)
298 struct nfs_cache_defer_req *dreq;
299 int ret = -ENOMEM;
301 dreq = nfs_cache_defer_req_alloc();
302 if (!dreq)
303 goto out;
304 ret = do_cache_lookup(cd, key, item, dreq);
305 if (ret == -EAGAIN) {
306 ret = nfs_cache_wait_for_upcall(dreq);
307 if (!ret)
308 ret = do_cache_lookup_nowait(cd, key, item);
310 nfs_cache_defer_req_put(dreq);
311 out:
312 return ret;
315 ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
316 struct sockaddr *sa, size_t salen)
318 struct nfs_dns_ent key = {
319 .hostname = name,
320 .namelen = namelen,
322 struct nfs_dns_ent *item = NULL;
323 ssize_t ret;
325 ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
326 if (ret == 0) {
327 if (salen >= item->addrlen) {
328 memcpy(sa, &item->addr, item->addrlen);
329 ret = item->addrlen;
330 } else
331 ret = -EOVERFLOW;
332 cache_put(&item->h, &nfs_dns_resolve);
333 } else if (ret == -ENOENT)
334 ret = -ESRCH;
335 return ret;
338 int nfs_dns_resolver_init(void)
340 return nfs_cache_register(&nfs_dns_resolve);
343 void nfs_dns_resolver_destroy(void)
345 nfs_cache_unregister(&nfs_dns_resolve);