dns_resolve.c 9.5 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * linux/fs/nfs/dns_resolve.c
 *
 * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
 *
 * Resolves DNS hostnames into valid ip addresses
 */

9 10
#ifdef CONFIG_NFS_USE_KERNEL_DNS

B
Bryan Schumaker 已提交
11
#include <linux/module.h>
12
#include <linux/sunrpc/clnt.h>
13
#include <linux/sunrpc/addr.h>
14
#include <linux/dns_resolver.h>
15
#include "dns_resolve.h"
16

17
ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
18 19 20 21 22 23 24 25
		struct sockaddr *sa, size_t salen)
{
	ssize_t ret;
	char *ip_addr = NULL;
	int ip_len;

	ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL);
	if (ip_len > 0)
26
		ret = rpc_pton(net, ip_addr, ip_len, sa, salen);
27 28 29 30 31
	else
		ret = -ESRCH;
	kfree(ip_addr);
	return ret;
}
B
Bryan Schumaker 已提交
32
EXPORT_SYMBOL_GPL(nfs_dns_resolve_name);
33 34 35

#else

B
Bryan Schumaker 已提交
36
#include <linux/module.h>
37 38 39
#include <linux/hash.h>
#include <linux/string.h>
#include <linux/kmod.h>
40
#include <linux/slab.h>
41 42 43 44 45
#include <linux/module.h>
#include <linux/socket.h>
#include <linux/seq_file.h>
#include <linux/inet.h>
#include <linux/sunrpc/clnt.h>
46
#include <linux/sunrpc/addr.h>
47 48
#include <linux/sunrpc/cache.h>
#include <linux/sunrpc/svcauth.h>
49
#include <linux/sunrpc/rpc_pipe_fs.h>
50 51 52

#include "dns_resolve.h"
#include "cache_lib.h"
53
#include "netns.h"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#define NFS_DNS_HASHBITS 4
#define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)

struct nfs_dns_ent {
	struct cache_head h;

	char *hostname;
	size_t namelen;

	struct sockaddr_storage addr;
	size_t addrlen;
};


69 70 71 72 73 74 75 76 77 78 79 80 81
static void nfs_dns_ent_update(struct cache_head *cnew,
		struct cache_head *ckey)
{
	struct nfs_dns_ent *new;
	struct nfs_dns_ent *key;

	new = container_of(cnew, struct nfs_dns_ent, h);
	key = container_of(ckey, struct nfs_dns_ent, h);

	memcpy(&new->addr, &key->addr, key->addrlen);
	new->addrlen = key->addrlen;
}

82 83 84 85 86 87 88 89 90 91 92 93 94
static void nfs_dns_ent_init(struct cache_head *cnew,
		struct cache_head *ckey)
{
	struct nfs_dns_ent *new;
	struct nfs_dns_ent *key;

	new = container_of(cnew, struct nfs_dns_ent, h);
	key = container_of(ckey, struct nfs_dns_ent, h);

	kfree(new->hostname);
	new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
	if (new->hostname) {
		new->namelen = key->namelen;
95
		nfs_dns_ent_update(cnew, ckey);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	} else {
		new->namelen = 0;
		new->addrlen = 0;
	}
}

static void nfs_dns_ent_put(struct kref *ref)
{
	struct nfs_dns_ent *item;

	item = container_of(ref, struct nfs_dns_ent, h.ref);
	kfree(item->hostname);
	kfree(item);
}

static struct cache_head *nfs_dns_ent_alloc(void)
{
	struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);

	if (item != NULL) {
		item->hostname = NULL;
		item->namelen = 0;
		item->addrlen = 0;
		return &item->h;
	}
	return NULL;
};

static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
{
	return hash_str(key->hostname, NFS_DNS_HASHBITS);
}

static void nfs_dns_request(struct cache_detail *cd,
		struct cache_head *ch,
		char **bpp, int *blen)
{
	struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);

	qword_add(bpp, blen, key->hostname);
	(*bpp)[-1] = '\n';
}

static int nfs_dns_upcall(struct cache_detail *cd,
		struct cache_head *ch)
{
	struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
	int ret;

	ret = nfs_cache_upcall(cd, key->hostname);
	if (ret)
147
		ret = sunrpc_cache_pipe_upcall(cd, ch);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	return ret;
}

static int nfs_dns_match(struct cache_head *ca,
		struct cache_head *cb)
{
	struct nfs_dns_ent *a;
	struct nfs_dns_ent *b;

	a = container_of(ca, struct nfs_dns_ent, h);
	b = container_of(cb, struct nfs_dns_ent, h);

	if (a->namelen == 0 || a->namelen != b->namelen)
		return 0;
	return memcmp(a->hostname, b->hostname, a->namelen) == 0;
}

static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
		struct cache_head *h)
{
	struct nfs_dns_ent *item;
	long ttl;

	if (h == NULL) {
		seq_puts(m, "# ip address      hostname        ttl\n");
		return 0;
	}
	item = container_of(h, struct nfs_dns_ent, h);
176
	ttl = item->h.expiry_time - seconds_since_boot();
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	if (ttl < 0)
		ttl = 0;

	if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
		char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];

		rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
		seq_printf(m, "%15s ", buf);
	} else
		seq_puts(m, "<none>          ");
	seq_printf(m, "%15s %ld\n", item->hostname, ttl);
	return 0;
}

191
static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
192 193 194 195 196 197 198 199 200 201 202 203
		struct nfs_dns_ent *key)
{
	struct cache_head *ch;

	ch = sunrpc_cache_lookup(cd,
			&key->h,
			nfs_dns_hash(key));
	if (!ch)
		return NULL;
	return container_of(ch, struct nfs_dns_ent, h);
}

204
static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		struct nfs_dns_ent *new,
		struct nfs_dns_ent *key)
{
	struct cache_head *ch;

	ch = sunrpc_cache_update(cd,
			&new->h, &key->h,
			nfs_dns_hash(key));
	if (!ch)
		return NULL;
	return container_of(ch, struct nfs_dns_ent, h);
}

static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
{
	char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
	struct nfs_dns_ent key, *item;
N
NeilBrown 已提交
222
	unsigned int ttl;
223 224 225 226 227 228 229 230 231 232
	ssize_t len;
	int ret = -EINVAL;

	if (buf[buflen-1] != '\n')
		goto out;
	buf[buflen-1] = '\0';

	len = qword_get(&buf, buf1, sizeof(buf1));
	if (len <= 0)
		goto out;
233
	key.addrlen = rpc_pton(cd->net, buf1, len,
234 235 236 237 238 239 240 241 242 243 244
			(struct sockaddr *)&key.addr,
			sizeof(key.addr));

	len = qword_get(&buf, buf1, sizeof(buf1));
	if (len <= 0)
		goto out;

	key.hostname = buf1;
	key.namelen = len;
	memset(&key.h, 0, sizeof(key.h));

N
NeilBrown 已提交
245 246
	if (get_uint(&buf, &ttl) < 0)
		goto out;
247 248
	if (ttl == 0)
		goto out;
249
	key.h.expiry_time = ttl + seconds_since_boot();
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

	ret = -ENOMEM;
	item = nfs_dns_lookup(cd, &key);
	if (item == NULL)
		goto out;

	if (key.addrlen == 0)
		set_bit(CACHE_NEGATIVE, &key.h.flags);

	item = nfs_dns_update(cd, &key, item);
	if (item == NULL)
		goto out;

	ret = 0;
	cache_put(&item->h, cd);
out:
	return ret;
}

static int do_cache_lookup(struct cache_detail *cd,
		struct nfs_dns_ent *key,
		struct nfs_dns_ent **item,
		struct nfs_cache_defer_req *dreq)
{
	int ret = -ENOMEM;

	*item = nfs_dns_lookup(cd, key);
	if (*item) {
		ret = cache_check(cd, &(*item)->h, &dreq->req);
		if (ret)
			*item = NULL;
	}
	return ret;
}

static int do_cache_lookup_nowait(struct cache_detail *cd,
		struct nfs_dns_ent *key,
		struct nfs_dns_ent **item)
{
	int ret = -ENOMEM;

	*item = nfs_dns_lookup(cd, key);
	if (!*item)
		goto out_err;
	ret = -ETIMEDOUT;
	if (!test_bit(CACHE_VALID, &(*item)->h.flags)
296
			|| (*item)->h.expiry_time < seconds_since_boot()
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			|| cd->flush_time > (*item)->h.last_refresh)
		goto out_put;
	ret = -ENOENT;
	if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
		goto out_put;
	return 0;
out_put:
	cache_put(&(*item)->h, cd);
out_err:
	*item = NULL;
	return ret;
}

static int do_cache_lookup_wait(struct cache_detail *cd,
		struct nfs_dns_ent *key,
		struct nfs_dns_ent **item)
{
	struct nfs_cache_defer_req *dreq;
	int ret = -ENOMEM;

	dreq = nfs_cache_defer_req_alloc();
	if (!dreq)
		goto out;
	ret = do_cache_lookup(cd, key, item, dreq);
	if (ret == -EAGAIN) {
		ret = nfs_cache_wait_for_upcall(dreq);
		if (!ret)
			ret = do_cache_lookup_nowait(cd, key, item);
	}
	nfs_cache_defer_req_put(dreq);
out:
	return ret;
}

331 332
ssize_t nfs_dns_resolve_name(struct net *net, char *name,
		size_t namelen, struct sockaddr *sa, size_t salen)
333 334 335 336 337 338 339
{
	struct nfs_dns_ent key = {
		.hostname = name,
		.namelen = namelen,
	};
	struct nfs_dns_ent *item = NULL;
	ssize_t ret;
340
	struct nfs_net *nn = net_generic(net, nfs_net_id);
341

342
	ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
343 344 345 346 347 348
	if (ret == 0) {
		if (salen >= item->addrlen) {
			memcpy(sa, &item->addr, item->addrlen);
			ret = item->addrlen;
		} else
			ret = -EOVERFLOW;
349
		cache_put(&item->h, nn->nfs_dns_resolve);
350 351 352 353
	} else if (ret == -ENOENT)
		ret = -ESRCH;
	return ret;
}
B
Bryan Schumaker 已提交
354
EXPORT_SYMBOL_GPL(nfs_dns_resolve_name);
355

356 357 358 359 360 361
static struct cache_detail nfs_dns_resolve_template = {
	.owner		= THIS_MODULE,
	.hash_size	= NFS_DNS_HASHTBL_SIZE,
	.name		= "dns_resolve",
	.cache_put	= nfs_dns_ent_put,
	.cache_upcall	= nfs_dns_upcall,
362
	.cache_request	= nfs_dns_request,
363 364 365 366 367 368 369 370 371
	.cache_parse	= nfs_dns_parse,
	.cache_show	= nfs_dns_show,
	.match		= nfs_dns_match,
	.init		= nfs_dns_ent_init,
	.update		= nfs_dns_ent_update,
	.alloc		= nfs_dns_ent_alloc,
};


372
int nfs_dns_resolver_cache_init(struct net *net)
373
{
374
	int err;
375 376
	struct nfs_net *nn = net_generic(net, nfs_net_id);

377 378 379 380 381
	nn->nfs_dns_resolve = cache_create_net(&nfs_dns_resolve_template, net);
	if (IS_ERR(nn->nfs_dns_resolve))
		return PTR_ERR(nn->nfs_dns_resolve);

	err = nfs_cache_register_net(net, nn->nfs_dns_resolve);
382 383 384
	if (err)
		goto err_reg;
	return 0;
385

386
err_reg:
387
	cache_destroy_net(nn->nfs_dns_resolve, net);
388 389 390 391 392 393 394
	return err;
}

void nfs_dns_resolver_cache_destroy(struct net *net)
{
	struct nfs_net *nn = net_generic(net, nfs_net_id);

395
	nfs_cache_unregister_net(net, nn->nfs_dns_resolve);
396
	cache_destroy_net(nn->nfs_dns_resolve, net);
397 398
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
			   void *ptr)
{
	struct super_block *sb = ptr;
	struct net *net = sb->s_fs_info;
	struct nfs_net *nn = net_generic(net, nfs_net_id);
	struct cache_detail *cd = nn->nfs_dns_resolve;
	int ret = 0;

	if (cd == NULL)
		return 0;

	if (!try_module_get(THIS_MODULE))
		return 0;

	switch (event) {
	case RPC_PIPEFS_MOUNT:
		ret = nfs_cache_register_sb(sb, cd);
		break;
	case RPC_PIPEFS_UMOUNT:
		nfs_cache_unregister_sb(sb, cd);
		break;
	default:
		ret = -ENOTSUPP;
		break;
	}
	module_put(THIS_MODULE);
	return ret;
}

static struct notifier_block nfs_dns_resolver_block = {
	.notifier_call	= rpc_pipefs_event,
};

433 434
int nfs_dns_resolver_init(void)
{
435
	return rpc_pipefs_notifier_register(&nfs_dns_resolver_block);
436 437 438 439
}

void nfs_dns_resolver_destroy(void)
{
440
	rpc_pipefs_notifier_unregister(&nfs_dns_resolver_block);
441
}
442
#endif