4 * The kernel statd client.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/utsname.h>
11 #include <linux/kernel.h>
12 #include <linux/ktime.h>
13 #include <linux/slab.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/xprtsock.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/lockd/lockd.h>
20 #include <asm/unaligned.h>
22 #define NLMDBG_FACILITY NLMDBG_MONITOR
23 #define NSM_PROGRAM 100024
37 struct nsm_private *priv;
38 u32 prog; /* RPC callback info */
51 static struct rpc_program nsm_program;
52 static LIST_HEAD(nsm_handles);
53 static DEFINE_SPINLOCK(nsm_lock);
58 u32 __read_mostly nsm_local_state;
59 int __read_mostly nsm_use_hostnames;
61 static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
63 return (struct sockaddr *)&nsm->sm_addr;
66 static struct rpc_clnt *nsm_create(void)
68 struct sockaddr_in sin = {
69 .sin_family = AF_INET,
70 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
72 struct rpc_create_args args = {
74 .protocol = XPRT_TRANSPORT_UDP,
75 .address = (struct sockaddr *)&sin,
76 .addrsize = sizeof(sin),
77 .servername = "rpc.statd",
78 .program = &nsm_program,
79 .version = NSM_VERSION,
80 .authflavor = RPC_AUTH_NULL,
81 .flags = RPC_CLNT_CREATE_NOPING,
84 return rpc_create(&args);
87 static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
89 struct rpc_clnt *clnt;
91 struct nsm_args args = {
92 .priv = &nsm->sm_priv,
95 .proc = NLMPROC_NSM_NOTIFY,
96 .mon_name = nsm->sm_mon_name,
97 .nodename = utsname()->nodename,
99 struct rpc_message msg = {
106 status = PTR_ERR(clnt);
107 dprintk("lockd: failed to create NSM upcall transport, "
108 "status=%d\n", status);
112 memset(res, 0, sizeof(*res));
114 msg.rpc_proc = &clnt->cl_procinfo[proc];
115 status = rpc_call_sync(clnt, &msg, 0);
117 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
121 rpc_shutdown_client(clnt);
127 * nsm_monitor - Notify a peer in case we reboot
128 * @host: pointer to nlm_host of peer to notify
130 * If this peer is not already monitored, this function sends an
131 * upcall to the local rpc.statd to record the name/address of
132 * the peer to notify in case we reboot.
134 * Returns zero if the peer is monitored by the local rpc.statd;
135 * otherwise a negative errno value is returned.
137 int nsm_monitor(const struct nlm_host *host)
139 struct nsm_handle *nsm = host->h_nsmhandle;
143 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
145 if (nsm->sm_monitored)
149 * Choose whether to record the caller_name or IP address of
150 * this peer in the local rpc.statd's database.
152 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
154 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
155 if (unlikely(res.status != 0))
157 if (unlikely(status < 0)) {
158 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
162 nsm->sm_monitored = 1;
163 if (unlikely(nsm_local_state != res.state)) {
164 nsm_local_state = res.state;
165 dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
171 * nsm_unmonitor - Unregister peer notification
172 * @host: pointer to nlm_host of peer to stop monitoring
174 * If this peer is monitored, this function sends an upcall to
175 * tell the local rpc.statd not to send this peer a notification
178 void nsm_unmonitor(const struct nlm_host *host)
180 struct nsm_handle *nsm = host->h_nsmhandle;
184 if (atomic_read(&nsm->sm_count) == 1
185 && nsm->sm_monitored && !nsm->sm_sticky) {
186 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
188 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
192 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
195 nsm->sm_monitored = 0;
199 static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
202 struct nsm_handle *nsm;
204 list_for_each_entry(nsm, &nsm_handles, sm_link)
205 if (strlen(nsm->sm_name) == len &&
206 memcmp(nsm->sm_name, hostname, len) == 0)
211 static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
213 struct nsm_handle *nsm;
215 list_for_each_entry(nsm, &nsm_handles, sm_link)
216 if (rpc_cmp_addr(nsm_addr(nsm), sap))
221 static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
223 struct nsm_handle *nsm;
225 list_for_each_entry(nsm, &nsm_handles, sm_link)
226 if (memcmp(nsm->sm_priv.data, priv->data,
227 sizeof(priv->data)) == 0)
233 * Construct a unique cookie to match this nsm_handle to this monitored
234 * host. It is passed to the local rpc.statd via NSMPROC_MON, and
235 * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
238 * The NSM protocol requires that these cookies be unique while the
239 * system is running. We prefer a stronger requirement of making them
240 * unique across reboots. If user space bugs cause a stale cookie to
241 * be sent to the kernel, it could cause the wrong host to lose its
242 * lock state if cookies were not unique across reboots.
244 * The cookies are exposed only to local user space via loopback. They
245 * do not appear on the physical network. If we want greater security
246 * for some reason, nsm_init_private() could perform a one-way hash to
247 * obscure the contents of the cookie.
249 static void nsm_init_private(struct nsm_handle *nsm)
251 u64 *p = (u64 *)&nsm->sm_priv.data;
256 ns = timespec_to_ns(&ts);
257 put_unaligned(ns, p);
258 put_unaligned((unsigned long)nsm, p + 1);
261 static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
263 const char *hostname,
264 const size_t hostname_len)
266 struct nsm_handle *new;
268 new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
269 if (unlikely(new == NULL))
272 atomic_set(&new->sm_count, 1);
273 new->sm_name = (char *)(new + 1);
274 memcpy(nsm_addr(new), sap, salen);
275 new->sm_addrlen = salen;
276 nsm_init_private(new);
278 if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
279 sizeof(new->sm_addrbuf)) == 0)
280 (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
281 "unsupported address family");
282 memcpy(new->sm_name, hostname, hostname_len);
283 new->sm_name[hostname_len] = '\0';
289 * nsm_get_handle - Find or create a cached nsm_handle
290 * @sap: pointer to socket address of handle to find
291 * @salen: length of socket address
292 * @hostname: pointer to C string containing hostname to find
293 * @hostname_len: length of C string
295 * Behavior is modulated by the global nsm_use_hostnames variable.
297 * Returns a cached nsm_handle after bumping its ref count, or
298 * returns a fresh nsm_handle if a handle that matches @sap and/or
299 * @hostname cannot be found in the handle cache. Returns NULL if
302 struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
303 const size_t salen, const char *hostname,
304 const size_t hostname_len)
306 struct nsm_handle *cached, *new = NULL;
308 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
309 if (printk_ratelimit()) {
310 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
311 "in NFS lock request\n",
312 (int)hostname_len, hostname);
318 spin_lock(&nsm_lock);
320 if (nsm_use_hostnames && hostname != NULL)
321 cached = nsm_lookup_hostname(hostname, hostname_len);
323 cached = nsm_lookup_addr(sap);
325 if (cached != NULL) {
326 atomic_inc(&cached->sm_count);
327 spin_unlock(&nsm_lock);
329 dprintk("lockd: found nsm_handle for %s (%s), "
330 "cnt %d\n", cached->sm_name,
332 atomic_read(&cached->sm_count));
337 list_add(&new->sm_link, &nsm_handles);
338 spin_unlock(&nsm_lock);
339 dprintk("lockd: created nsm_handle for %s (%s)\n",
340 new->sm_name, new->sm_addrbuf);
344 spin_unlock(&nsm_lock);
346 new = nsm_create_handle(sap, salen, hostname, hostname_len);
347 if (unlikely(new == NULL))
353 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
354 * @info: pointer to NLMPROC_SM_NOTIFY arguments
356 * Returns a matching nsm_handle if found in the nsm cache. The returned
357 * nsm_handle's reference count is bumped. Otherwise returns NULL if some
360 struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
362 struct nsm_handle *cached;
364 spin_lock(&nsm_lock);
366 cached = nsm_lookup_priv(&info->priv);
367 if (unlikely(cached == NULL)) {
368 spin_unlock(&nsm_lock);
369 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
370 info->len, info->mon);
374 atomic_inc(&cached->sm_count);
375 spin_unlock(&nsm_lock);
377 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
378 cached->sm_name, cached->sm_addrbuf,
379 atomic_read(&cached->sm_count));
384 * nsm_release - Release an NSM handle
385 * @nsm: pointer to handle to be released
388 void nsm_release(struct nsm_handle *nsm)
390 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
391 list_del(&nsm->sm_link);
392 spin_unlock(&nsm_lock);
393 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
394 nsm->sm_name, nsm->sm_addrbuf);
400 * XDR functions for NSM.
402 * See http://www.opengroup.org/ for details on the Network
403 * Status Monitor wire protocol.
406 static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
408 const u32 len = strlen(string);
411 BUG_ON(len > SM_MAXSTRLEN);
412 p = xdr_reserve_space(xdr, 4 + len);
413 xdr_encode_opaque(p, string, len);
417 * "mon_name" specifies the host to be monitored.
419 static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
421 encode_nsm_string(xdr, argp->mon_name);
425 * The "my_id" argument specifies the hostname and RPC procedure
426 * to be called when the status manager receives notification
427 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
430 static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
434 encode_nsm_string(xdr, argp->nodename);
435 p = xdr_reserve_space(xdr, 4 + 4 + 4);
436 *p++ = cpu_to_be32(argp->prog);
437 *p++ = cpu_to_be32(argp->vers);
438 *p = cpu_to_be32(argp->proc);
442 * The "mon_id" argument specifies the non-private arguments
443 * of an NSMPROC_MON or NSMPROC_UNMON call.
445 static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
447 encode_mon_name(xdr, argp);
448 encode_my_id(xdr, argp);
452 * The "priv" argument may contain private information required
453 * by the NSMPROC_MON call. This information will be supplied in the
454 * NLMPROC_SM_NOTIFY call.
456 static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
460 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
461 xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
464 static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
465 const struct nsm_args *argp)
467 encode_mon_id(xdr, argp);
468 encode_priv(xdr, argp);
471 static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
472 const struct nsm_args *argp)
474 encode_mon_id(xdr, argp);
477 static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
478 struct xdr_stream *xdr,
479 struct nsm_res *resp)
483 p = xdr_inline_decode(xdr, 4 + 4);
484 if (unlikely(p == NULL))
486 resp->status = be32_to_cpup(p++);
487 resp->state = be32_to_cpup(p);
489 dprintk("lockd: %s status %d state %d\n",
490 __func__, resp->status, resp->state);
494 static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
495 struct xdr_stream *xdr,
496 struct nsm_res *resp)
500 p = xdr_inline_decode(xdr, 4);
501 if (unlikely(p == NULL))
503 resp->state = be32_to_cpup(p);
505 dprintk("lockd: %s state %d\n", __func__, resp->state);
509 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
510 #define SM_my_id_sz (SM_my_name_sz+3)
511 #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
512 #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
513 #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
514 #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
515 #define SM_monres_sz 2
516 #define SM_unmonres_sz 1
518 static struct rpc_procinfo nsm_procedures[] = {
520 .p_proc = NSMPROC_MON,
521 .p_encode = (kxdreproc_t)nsm_xdr_enc_mon,
522 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res,
523 .p_arglen = SM_mon_sz,
524 .p_replen = SM_monres_sz,
525 .p_statidx = NSMPROC_MON,
529 .p_proc = NSMPROC_UNMON,
530 .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon,
531 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat,
532 .p_arglen = SM_mon_id_sz,
533 .p_replen = SM_unmonres_sz,
534 .p_statidx = NSMPROC_UNMON,
535 .p_name = "UNMONITOR",
539 static struct rpc_version nsm_version1 = {
541 .nrprocs = ARRAY_SIZE(nsm_procedures),
542 .procs = nsm_procedures
545 static struct rpc_version * nsm_version[] = {
549 static struct rpc_stat nsm_stats;
551 static struct rpc_program nsm_program = {
553 .number = NSM_PROGRAM,
554 .nrvers = ARRAY_SIZE(nsm_version),
555 .version = nsm_version,