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>
24 #define NLMDBG_FACILITY NLMDBG_MONITOR
25 #define NSM_PROGRAM 100024
39 struct nsm_private
*priv
;
40 u32 prog
; /* RPC callback info */
53 static const struct rpc_program nsm_program
;
54 static LIST_HEAD(nsm_handles
);
55 static DEFINE_SPINLOCK(nsm_lock
);
60 u32 __read_mostly nsm_local_state
;
61 bool __read_mostly nsm_use_hostnames
;
63 static inline struct sockaddr
*nsm_addr(const struct nsm_handle
*nsm
)
65 return (struct sockaddr
*)&nsm
->sm_addr
;
68 static struct rpc_clnt
*nsm_create(struct net
*net
)
70 struct sockaddr_in sin
= {
71 .sin_family
= AF_INET
,
72 .sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
),
74 struct rpc_create_args args
= {
76 .protocol
= XPRT_TRANSPORT_TCP
,
77 .address
= (struct sockaddr
*)&sin
,
78 .addrsize
= sizeof(sin
),
79 .servername
= "rpc.statd",
80 .program
= &nsm_program
,
81 .version
= NSM_VERSION
,
82 .authflavor
= RPC_AUTH_NULL
,
83 .flags
= RPC_CLNT_CREATE_NOPING
,
86 return rpc_create(&args
);
89 __maybe_unused
static struct rpc_clnt
*nsm_client_get(struct net
*net
)
91 static DEFINE_MUTEX(nsm_create_mutex
);
92 struct rpc_clnt
*clnt
;
93 struct lockd_net
*ln
= net_generic(net
, lockd_net_id
);
95 spin_lock(&ln
->nsm_clnt_lock
);
99 spin_unlock(&ln
->nsm_clnt_lock
);
102 spin_unlock(&ln
->nsm_clnt_lock
);
104 mutex_lock(&nsm_create_mutex
);
105 clnt
= nsm_create(net
);
111 mutex_unlock(&nsm_create_mutex
);
116 __maybe_unused
static void nsm_client_put(struct net
*net
)
118 struct lockd_net
*ln
= net_generic(net
, lockd_net_id
);
119 struct rpc_clnt
*clnt
= ln
->nsm_clnt
;
122 spin_lock(&ln
->nsm_clnt_lock
);
126 shutdown
= !ln
->nsm_users
;
128 spin_unlock(&ln
->nsm_clnt_lock
);
131 rpc_shutdown_client(clnt
);
134 static int nsm_mon_unmon(struct nsm_handle
*nsm
, u32 proc
, struct nsm_res
*res
,
137 struct rpc_clnt
*clnt
;
139 struct nsm_args args
= {
140 .priv
= &nsm
->sm_priv
,
143 .proc
= NLMPROC_NSM_NOTIFY
,
144 .mon_name
= nsm
->sm_mon_name
,
145 .nodename
= utsname()->nodename
,
147 struct rpc_message msg
= {
152 clnt
= nsm_create(net
);
154 status
= PTR_ERR(clnt
);
155 dprintk("lockd: failed to create NSM upcall transport, "
156 "status=%d\n", status
);
160 memset(res
, 0, sizeof(*res
));
162 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
163 status
= rpc_call_sync(clnt
, &msg
, RPC_TASK_SOFTCONN
);
165 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
169 rpc_shutdown_client(clnt
);
175 * nsm_monitor - Notify a peer in case we reboot
176 * @host: pointer to nlm_host of peer to notify
178 * If this peer is not already monitored, this function sends an
179 * upcall to the local rpc.statd to record the name/address of
180 * the peer to notify in case we reboot.
182 * Returns zero if the peer is monitored by the local rpc.statd;
183 * otherwise a negative errno value is returned.
185 int nsm_monitor(const struct nlm_host
*host
)
187 struct nsm_handle
*nsm
= host
->h_nsmhandle
;
191 dprintk("lockd: nsm_monitor(%s)\n", nsm
->sm_name
);
193 if (nsm
->sm_monitored
)
197 * Choose whether to record the caller_name or IP address of
198 * this peer in the local rpc.statd's database.
200 nsm
->sm_mon_name
= nsm_use_hostnames
? nsm
->sm_name
: nsm
->sm_addrbuf
;
202 status
= nsm_mon_unmon(nsm
, NSMPROC_MON
, &res
, host
->net
);
203 if (unlikely(res
.status
!= 0))
205 if (unlikely(status
< 0)) {
206 printk(KERN_NOTICE
"lockd: cannot monitor %s\n", nsm
->sm_name
);
210 nsm
->sm_monitored
= 1;
211 if (unlikely(nsm_local_state
!= res
.state
)) {
212 nsm_local_state
= res
.state
;
213 dprintk("lockd: NSM state changed to %d\n", nsm_local_state
);
219 * nsm_unmonitor - Unregister peer notification
220 * @host: pointer to nlm_host of peer to stop monitoring
222 * If this peer is monitored, this function sends an upcall to
223 * tell the local rpc.statd not to send this peer a notification
226 void nsm_unmonitor(const struct nlm_host
*host
)
228 struct nsm_handle
*nsm
= host
->h_nsmhandle
;
232 if (atomic_read(&nsm
->sm_count
) == 1
233 && nsm
->sm_monitored
&& !nsm
->sm_sticky
) {
234 dprintk("lockd: nsm_unmonitor(%s)\n", nsm
->sm_name
);
236 status
= nsm_mon_unmon(nsm
, NSMPROC_UNMON
, &res
, host
->net
);
240 printk(KERN_NOTICE
"lockd: cannot unmonitor %s\n",
243 nsm
->sm_monitored
= 0;
247 static struct nsm_handle
*nsm_lookup_hostname(const char *hostname
,
250 struct nsm_handle
*nsm
;
252 list_for_each_entry(nsm
, &nsm_handles
, sm_link
)
253 if (strlen(nsm
->sm_name
) == len
&&
254 memcmp(nsm
->sm_name
, hostname
, len
) == 0)
259 static struct nsm_handle
*nsm_lookup_addr(const struct sockaddr
*sap
)
261 struct nsm_handle
*nsm
;
263 list_for_each_entry(nsm
, &nsm_handles
, sm_link
)
264 if (rpc_cmp_addr(nsm_addr(nsm
), sap
))
269 static struct nsm_handle
*nsm_lookup_priv(const struct nsm_private
*priv
)
271 struct nsm_handle
*nsm
;
273 list_for_each_entry(nsm
, &nsm_handles
, sm_link
)
274 if (memcmp(nsm
->sm_priv
.data
, priv
->data
,
275 sizeof(priv
->data
)) == 0)
281 * Construct a unique cookie to match this nsm_handle to this monitored
282 * host. It is passed to the local rpc.statd via NSMPROC_MON, and
283 * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
286 * The NSM protocol requires that these cookies be unique while the
287 * system is running. We prefer a stronger requirement of making them
288 * unique across reboots. If user space bugs cause a stale cookie to
289 * be sent to the kernel, it could cause the wrong host to lose its
290 * lock state if cookies were not unique across reboots.
292 * The cookies are exposed only to local user space via loopback. They
293 * do not appear on the physical network. If we want greater security
294 * for some reason, nsm_init_private() could perform a one-way hash to
295 * obscure the contents of the cookie.
297 static void nsm_init_private(struct nsm_handle
*nsm
)
299 u64
*p
= (u64
*)&nsm
->sm_priv
.data
;
304 ns
= timespec_to_ns(&ts
);
305 put_unaligned(ns
, p
);
306 put_unaligned((unsigned long)nsm
, p
+ 1);
309 static struct nsm_handle
*nsm_create_handle(const struct sockaddr
*sap
,
311 const char *hostname
,
312 const size_t hostname_len
)
314 struct nsm_handle
*new;
316 new = kzalloc(sizeof(*new) + hostname_len
+ 1, GFP_KERNEL
);
317 if (unlikely(new == NULL
))
320 atomic_set(&new->sm_count
, 1);
321 new->sm_name
= (char *)(new + 1);
322 memcpy(nsm_addr(new), sap
, salen
);
323 new->sm_addrlen
= salen
;
324 nsm_init_private(new);
326 if (rpc_ntop(nsm_addr(new), new->sm_addrbuf
,
327 sizeof(new->sm_addrbuf
)) == 0)
328 (void)snprintf(new->sm_addrbuf
, sizeof(new->sm_addrbuf
),
329 "unsupported address family");
330 memcpy(new->sm_name
, hostname
, hostname_len
);
331 new->sm_name
[hostname_len
] = '\0';
337 * nsm_get_handle - Find or create a cached nsm_handle
338 * @sap: pointer to socket address of handle to find
339 * @salen: length of socket address
340 * @hostname: pointer to C string containing hostname to find
341 * @hostname_len: length of C string
343 * Behavior is modulated by the global nsm_use_hostnames variable.
345 * Returns a cached nsm_handle after bumping its ref count, or
346 * returns a fresh nsm_handle if a handle that matches @sap and/or
347 * @hostname cannot be found in the handle cache. Returns NULL if
350 struct nsm_handle
*nsm_get_handle(const struct sockaddr
*sap
,
351 const size_t salen
, const char *hostname
,
352 const size_t hostname_len
)
354 struct nsm_handle
*cached
, *new = NULL
;
356 if (hostname
&& memchr(hostname
, '/', hostname_len
) != NULL
) {
357 if (printk_ratelimit()) {
358 printk(KERN_WARNING
"Invalid hostname \"%.*s\" "
359 "in NFS lock request\n",
360 (int)hostname_len
, hostname
);
366 spin_lock(&nsm_lock
);
368 if (nsm_use_hostnames
&& hostname
!= NULL
)
369 cached
= nsm_lookup_hostname(hostname
, hostname_len
);
371 cached
= nsm_lookup_addr(sap
);
373 if (cached
!= NULL
) {
374 atomic_inc(&cached
->sm_count
);
375 spin_unlock(&nsm_lock
);
377 dprintk("lockd: found nsm_handle for %s (%s), "
378 "cnt %d\n", cached
->sm_name
,
380 atomic_read(&cached
->sm_count
));
385 list_add(&new->sm_link
, &nsm_handles
);
386 spin_unlock(&nsm_lock
);
387 dprintk("lockd: created nsm_handle for %s (%s)\n",
388 new->sm_name
, new->sm_addrbuf
);
392 spin_unlock(&nsm_lock
);
394 new = nsm_create_handle(sap
, salen
, hostname
, hostname_len
);
395 if (unlikely(new == NULL
))
401 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
402 * @info: pointer to NLMPROC_SM_NOTIFY arguments
404 * Returns a matching nsm_handle if found in the nsm cache. The returned
405 * nsm_handle's reference count is bumped. Otherwise returns NULL if some
408 struct nsm_handle
*nsm_reboot_lookup(const struct nlm_reboot
*info
)
410 struct nsm_handle
*cached
;
412 spin_lock(&nsm_lock
);
414 cached
= nsm_lookup_priv(&info
->priv
);
415 if (unlikely(cached
== NULL
)) {
416 spin_unlock(&nsm_lock
);
417 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
418 info
->len
, info
->mon
);
422 atomic_inc(&cached
->sm_count
);
423 spin_unlock(&nsm_lock
);
425 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
426 cached
->sm_name
, cached
->sm_addrbuf
,
427 atomic_read(&cached
->sm_count
));
432 * nsm_release - Release an NSM handle
433 * @nsm: pointer to handle to be released
436 void nsm_release(struct nsm_handle
*nsm
)
438 if (atomic_dec_and_lock(&nsm
->sm_count
, &nsm_lock
)) {
439 list_del(&nsm
->sm_link
);
440 spin_unlock(&nsm_lock
);
441 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
442 nsm
->sm_name
, nsm
->sm_addrbuf
);
448 * XDR functions for NSM.
450 * See http://www.opengroup.org/ for details on the Network
451 * Status Monitor wire protocol.
454 static void encode_nsm_string(struct xdr_stream
*xdr
, const char *string
)
456 const u32 len
= strlen(string
);
459 BUG_ON(len
> SM_MAXSTRLEN
);
460 p
= xdr_reserve_space(xdr
, 4 + len
);
461 xdr_encode_opaque(p
, string
, len
);
465 * "mon_name" specifies the host to be monitored.
467 static void encode_mon_name(struct xdr_stream
*xdr
, const struct nsm_args
*argp
)
469 encode_nsm_string(xdr
, argp
->mon_name
);
473 * The "my_id" argument specifies the hostname and RPC procedure
474 * to be called when the status manager receives notification
475 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
478 static void encode_my_id(struct xdr_stream
*xdr
, const struct nsm_args
*argp
)
482 encode_nsm_string(xdr
, argp
->nodename
);
483 p
= xdr_reserve_space(xdr
, 4 + 4 + 4);
484 *p
++ = cpu_to_be32(argp
->prog
);
485 *p
++ = cpu_to_be32(argp
->vers
);
486 *p
= cpu_to_be32(argp
->proc
);
490 * The "mon_id" argument specifies the non-private arguments
491 * of an NSMPROC_MON or NSMPROC_UNMON call.
493 static void encode_mon_id(struct xdr_stream
*xdr
, const struct nsm_args
*argp
)
495 encode_mon_name(xdr
, argp
);
496 encode_my_id(xdr
, argp
);
500 * The "priv" argument may contain private information required
501 * by the NSMPROC_MON call. This information will be supplied in the
502 * NLMPROC_SM_NOTIFY call.
504 static void encode_priv(struct xdr_stream
*xdr
, const struct nsm_args
*argp
)
508 p
= xdr_reserve_space(xdr
, SM_PRIV_SIZE
);
509 xdr_encode_opaque_fixed(p
, argp
->priv
->data
, SM_PRIV_SIZE
);
512 static void nsm_xdr_enc_mon(struct rpc_rqst
*req
, struct xdr_stream
*xdr
,
513 const struct nsm_args
*argp
)
515 encode_mon_id(xdr
, argp
);
516 encode_priv(xdr
, argp
);
519 static void nsm_xdr_enc_unmon(struct rpc_rqst
*req
, struct xdr_stream
*xdr
,
520 const struct nsm_args
*argp
)
522 encode_mon_id(xdr
, argp
);
525 static int nsm_xdr_dec_stat_res(struct rpc_rqst
*rqstp
,
526 struct xdr_stream
*xdr
,
527 struct nsm_res
*resp
)
531 p
= xdr_inline_decode(xdr
, 4 + 4);
532 if (unlikely(p
== NULL
))
534 resp
->status
= be32_to_cpup(p
++);
535 resp
->state
= be32_to_cpup(p
);
537 dprintk("lockd: %s status %d state %d\n",
538 __func__
, resp
->status
, resp
->state
);
542 static int nsm_xdr_dec_stat(struct rpc_rqst
*rqstp
,
543 struct xdr_stream
*xdr
,
544 struct nsm_res
*resp
)
548 p
= xdr_inline_decode(xdr
, 4);
549 if (unlikely(p
== NULL
))
551 resp
->state
= be32_to_cpup(p
);
553 dprintk("lockd: %s state %d\n", __func__
, resp
->state
);
557 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
558 #define SM_my_id_sz (SM_my_name_sz+3)
559 #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
560 #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
561 #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
562 #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
563 #define SM_monres_sz 2
564 #define SM_unmonres_sz 1
566 static struct rpc_procinfo nsm_procedures
[] = {
568 .p_proc
= NSMPROC_MON
,
569 .p_encode
= (kxdreproc_t
)nsm_xdr_enc_mon
,
570 .p_decode
= (kxdrdproc_t
)nsm_xdr_dec_stat_res
,
571 .p_arglen
= SM_mon_sz
,
572 .p_replen
= SM_monres_sz
,
573 .p_statidx
= NSMPROC_MON
,
577 .p_proc
= NSMPROC_UNMON
,
578 .p_encode
= (kxdreproc_t
)nsm_xdr_enc_unmon
,
579 .p_decode
= (kxdrdproc_t
)nsm_xdr_dec_stat
,
580 .p_arglen
= SM_mon_id_sz
,
581 .p_replen
= SM_unmonres_sz
,
582 .p_statidx
= NSMPROC_UNMON
,
583 .p_name
= "UNMONITOR",
587 static const struct rpc_version nsm_version1
= {
589 .nrprocs
= ARRAY_SIZE(nsm_procedures
),
590 .procs
= nsm_procedures
593 static const struct rpc_version
*nsm_version
[] = {
597 static struct rpc_stat nsm_stats
;
599 static const struct rpc_program nsm_program
= {
601 .number
= NSM_PROGRAM
,
602 .nrvers
= ARRAY_SIZE(nsm_version
),
603 .version
= nsm_version
,