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>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/xprtsock.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/lockd/lockd.h>
18 #include <linux/lockd/sm_inter.h>
21 #define NLMDBG_FACILITY NLMDBG_MONITOR
22 #define NSM_PROGRAM 100024
36 struct nsm_private *priv;
37 u32 prog; /* RPC callback info */
49 static struct rpc_clnt * nsm_create(void);
51 static struct rpc_program nsm_program;
52 static LIST_HEAD(nsm_handles);
53 static DEFINE_SPINLOCK(nsm_lock);
60 static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
63 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
64 snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
67 static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
70 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
72 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
73 snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
74 else if (sin6->sin6_scope_id != 0)
75 snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
78 snprintf(buf, len, "%pI6", &sin6->sin6_addr);
81 static void nsm_display_address(const struct sockaddr *sap,
82 char *buf, const size_t len)
84 switch (sap->sa_family) {
86 nsm_display_ipv4_address(sap, buf, len);
89 nsm_display_ipv6_address(sap, buf, len);
92 snprintf(buf, len, "unsupported address family");
98 * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
101 nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
103 struct rpc_clnt *clnt;
105 struct nsm_args args = {
106 .priv = &nsm->sm_priv,
109 .proc = NLMPROC_NSM_NOTIFY,
110 .mon_name = nsm->sm_mon_name,
112 struct rpc_message msg = {
119 status = PTR_ERR(clnt);
120 dprintk("lockd: failed to create NSM upcall transport, "
121 "status=%d\n", status);
125 memset(res, 0, sizeof(*res));
127 msg.rpc_proc = &clnt->cl_procinfo[proc];
128 status = rpc_call_sync(clnt, &msg, 0);
130 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
134 rpc_shutdown_client(clnt);
140 * nsm_monitor - Notify a peer in case we reboot
141 * @host: pointer to nlm_host of peer to notify
143 * If this peer is not already monitored, this function sends an
144 * upcall to the local rpc.statd to record the name/address of
145 * the peer to notify in case we reboot.
147 * Returns zero if the peer is monitored by the local rpc.statd;
148 * otherwise a negative errno value is returned.
150 int nsm_monitor(const struct nlm_host *host)
152 struct nsm_handle *nsm = host->h_nsmhandle;
156 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
158 if (nsm->sm_monitored)
162 * Choose whether to record the caller_name or IP address of
163 * this peer in the local rpc.statd's database.
165 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
167 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
171 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
173 nsm->sm_monitored = 1;
178 * nsm_unmonitor - Unregister peer notification
179 * @host: pointer to nlm_host of peer to stop monitoring
181 * If this peer is monitored, this function sends an upcall to
182 * tell the local rpc.statd not to send this peer a notification
185 void nsm_unmonitor(const struct nlm_host *host)
187 struct nsm_handle *nsm = host->h_nsmhandle;
191 if (atomic_read(&nsm->sm_count) == 1
192 && nsm->sm_monitored && !nsm->sm_sticky) {
193 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
195 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
199 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
202 nsm->sm_monitored = 0;
206 static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
209 struct nsm_handle *nsm;
211 list_for_each_entry(nsm, &nsm_handles, sm_link)
212 if (strlen(nsm->sm_name) == len &&
213 memcmp(nsm->sm_name, hostname, len) == 0)
218 static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
220 struct nsm_handle *nsm;
222 list_for_each_entry(nsm, &nsm_handles, sm_link)
223 if (nlm_cmp_addr(nsm_addr(nsm), sap))
228 static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
230 struct nsm_handle *nsm;
232 list_for_each_entry(nsm, &nsm_handles, sm_link)
233 if (memcmp(nsm->sm_priv.data, priv->data,
234 sizeof(priv->data)) == 0)
240 * Construct a unique cookie to match this nsm_handle to this monitored
241 * host. It is passed to the local rpc.statd via NSMPROC_MON, and
242 * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
245 * The NSM protocol requires that these cookies be unique while the
246 * system is running. We prefer a stronger requirement of making them
247 * unique across reboots. If user space bugs cause a stale cookie to
248 * be sent to the kernel, it could cause the wrong host to lose its
249 * lock state if cookies were not unique across reboots.
251 * The cookies are exposed only to local user space via loopback. They
252 * do not appear on the physical network. If we want greater security
253 * for some reason, nsm_init_private() could perform a one-way hash to
254 * obscure the contents of the cookie.
256 static void nsm_init_private(struct nsm_handle *nsm)
258 u64 *p = (u64 *)&nsm->sm_priv.data;
262 *p++ = timespec_to_ns(&ts);
263 *p = (unsigned long)nsm;
266 static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
268 const char *hostname,
269 const size_t hostname_len)
271 struct nsm_handle *new;
273 new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
274 if (unlikely(new == NULL))
277 atomic_set(&new->sm_count, 1);
278 new->sm_name = (char *)(new + 1);
279 memcpy(nsm_addr(new), sap, salen);
280 new->sm_addrlen = salen;
281 nsm_init_private(new);
282 nsm_display_address((const struct sockaddr *)&new->sm_addr,
283 new->sm_addrbuf, sizeof(new->sm_addrbuf));
284 memcpy(new->sm_name, hostname, hostname_len);
285 new->sm_name[hostname_len] = '\0';
291 * nsm_get_handle - Find or create a cached nsm_handle
292 * @sap: pointer to socket address of handle to find
293 * @salen: length of socket address
294 * @hostname: pointer to C string containing hostname to find
295 * @hostname_len: length of C string
297 * Behavior is modulated by the global nsm_use_hostnames variable.
299 * Returns a cached nsm_handle after bumping its ref count, or
300 * returns a fresh nsm_handle if a handle that matches @sap and/or
301 * @hostname cannot be found in the handle cache. Returns NULL if
304 struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
305 const size_t salen, const char *hostname,
306 const size_t hostname_len)
308 struct nsm_handle *cached, *new = NULL;
310 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
311 if (printk_ratelimit()) {
312 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
313 "in NFS lock request\n",
314 (int)hostname_len, hostname);
320 spin_lock(&nsm_lock);
322 if (nsm_use_hostnames && hostname != NULL)
323 cached = nsm_lookup_hostname(hostname, hostname_len);
325 cached = nsm_lookup_addr(sap);
327 if (cached != NULL) {
328 atomic_inc(&cached->sm_count);
329 spin_unlock(&nsm_lock);
331 dprintk("lockd: found nsm_handle for %s (%s), "
332 "cnt %d\n", cached->sm_name,
334 atomic_read(&cached->sm_count));
339 list_add(&new->sm_link, &nsm_handles);
340 spin_unlock(&nsm_lock);
341 dprintk("lockd: created nsm_handle for %s (%s)\n",
342 new->sm_name, new->sm_addrbuf);
346 spin_unlock(&nsm_lock);
348 new = nsm_create_handle(sap, salen, hostname, hostname_len);
349 if (unlikely(new == NULL))
355 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
356 * @info: pointer to NLMPROC_SM_NOTIFY arguments
358 * Returns a matching nsm_handle if found in the nsm cache; the returned
359 * nsm_handle's reference count is bumped and sm_monitored is cleared.
360 * Otherwise returns NULL if some error occurred.
362 struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
364 struct nsm_handle *cached;
366 spin_lock(&nsm_lock);
368 cached = nsm_lookup_priv(&info->priv);
369 if (unlikely(cached == NULL)) {
370 spin_unlock(&nsm_lock);
371 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
372 info->len, info->mon);
376 atomic_inc(&cached->sm_count);
377 spin_unlock(&nsm_lock);
380 * During subsequent lock activity, force a fresh
381 * notification to be set up for this host.
383 cached->sm_monitored = 0;
385 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
386 cached->sm_name, cached->sm_addrbuf,
387 atomic_read(&cached->sm_count));
392 * nsm_release - Release an NSM handle
393 * @nsm: pointer to handle to be released
396 void nsm_release(struct nsm_handle *nsm)
398 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
399 list_del(&nsm->sm_link);
400 spin_unlock(&nsm_lock);
401 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
402 nsm->sm_name, nsm->sm_addrbuf);
408 * Create NSM client for the local host
410 static struct rpc_clnt *
413 struct sockaddr_in sin = {
414 .sin_family = AF_INET,
415 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
418 struct rpc_create_args args = {
419 .protocol = XPRT_TRANSPORT_UDP,
420 .address = (struct sockaddr *)&sin,
421 .addrsize = sizeof(sin),
422 .servername = "localhost",
423 .program = &nsm_program,
424 .version = NSM_VERSION,
425 .authflavor = RPC_AUTH_NULL,
428 return rpc_create(&args);
432 * XDR functions for NSM.
434 * See http://www.opengroup.org/ for details on the Network
435 * Status Monitor wire protocol.
438 static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
440 const u32 len = strlen(string);
443 if (unlikely(len > SM_MAXSTRLEN))
445 p = xdr_reserve_space(xdr, sizeof(u32) + len);
446 if (unlikely(p == NULL))
448 xdr_encode_opaque(p, string, len);
453 * "mon_name" specifies the host to be monitored.
455 static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
457 return encode_nsm_string(xdr, argp->mon_name);
461 * The "my_id" argument specifies the hostname and RPC procedure
462 * to be called when the status manager receives notification
463 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
466 static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
471 status = encode_nsm_string(xdr, utsname()->nodename);
472 if (unlikely(status != 0))
474 p = xdr_reserve_space(xdr, 3 * sizeof(u32));
475 if (unlikely(p == NULL))
477 *p++ = htonl(argp->prog);
478 *p++ = htonl(argp->vers);
479 *p++ = htonl(argp->proc);
484 * The "mon_id" argument specifies the non-private arguments
485 * of an NSMPROC_MON or NSMPROC_UNMON call.
487 static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
491 status = encode_mon_name(xdr, argp);
492 if (unlikely(status != 0))
494 return encode_my_id(xdr, argp);
498 * The "priv" argument may contain private information required
499 * by the NSMPROC_MON call. This information will be supplied in the
500 * NLMPROC_SM_NOTIFY call.
502 static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
506 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
507 if (unlikely(p == NULL))
509 xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
513 static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
514 const struct nsm_args *argp)
516 struct xdr_stream xdr;
519 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
520 status = encode_mon_id(&xdr, argp);
521 if (unlikely(status))
523 return encode_priv(&xdr, argp);
526 static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
527 const struct nsm_args *argp)
529 struct xdr_stream xdr;
531 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
532 return encode_mon_id(&xdr, argp);
535 static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p,
536 struct nsm_res *resp)
538 struct xdr_stream xdr;
540 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
541 p = xdr_inline_decode(&xdr, 2 * sizeof(u32));
542 if (unlikely(p == NULL))
544 resp->status = ntohl(*p++);
545 resp->state = ntohl(*p);
547 dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
548 resp->status, resp->state);
552 static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
553 struct nsm_res *resp)
555 struct xdr_stream xdr;
557 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
558 p = xdr_inline_decode(&xdr, sizeof(u32));
559 if (unlikely(p == NULL))
561 resp->state = ntohl(*p);
563 dprintk("lockd: xdr_dec_stat state %d\n", resp->state);
567 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
568 #define SM_my_id_sz (SM_my_name_sz+3)
569 #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
570 #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
571 #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
572 #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
573 #define SM_monres_sz 2
574 #define SM_unmonres_sz 1
576 static struct rpc_procinfo nsm_procedures[] = {
578 .p_proc = NSMPROC_MON,
579 .p_encode = (kxdrproc_t)xdr_enc_mon,
580 .p_decode = (kxdrproc_t)xdr_dec_stat_res,
581 .p_arglen = SM_mon_sz,
582 .p_replen = SM_monres_sz,
583 .p_statidx = NSMPROC_MON,
587 .p_proc = NSMPROC_UNMON,
588 .p_encode = (kxdrproc_t)xdr_enc_unmon,
589 .p_decode = (kxdrproc_t)xdr_dec_stat,
590 .p_arglen = SM_mon_id_sz,
591 .p_replen = SM_unmonres_sz,
592 .p_statidx = NSMPROC_UNMON,
593 .p_name = "UNMONITOR",
597 static struct rpc_version nsm_version1 = {
599 .nrprocs = ARRAY_SIZE(nsm_procedures),
600 .procs = nsm_procedures
603 static struct rpc_version * nsm_version[] = {
607 static struct rpc_stat nsm_stats;
609 static struct rpc_program nsm_program = {
611 .number = NSM_PROGRAM,
612 .nrvers = ARRAY_SIZE(nsm_version),
613 .version = nsm_version,