blob: 0a68e0b2283970deabf70fab549d820477854bf1 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * linux/fs/lockd/mon.c
3 *
4 * The kernel statd client.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
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>
14
15#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/xprtsock.h>
17#include <linux/sunrpc/svc.h>
18#include <linux/lockd/lockd.h>
19
20#include <asm/unaligned.h>
21
22#define NLMDBG_FACILITY NLMDBG_MONITOR
23#define NSM_PROGRAM 100024
24#define NSM_VERSION 1
25
26enum {
27 NSMPROC_NULL,
28 NSMPROC_STAT,
29 NSMPROC_MON,
30 NSMPROC_UNMON,
31 NSMPROC_UNMON_ALL,
32 NSMPROC_SIMU_CRASH,
33 NSMPROC_NOTIFY,
34};
35
36struct nsm_args {
37 struct nsm_private *priv;
38 u32 prog; /* RPC callback info */
39 u32 vers;
40 u32 proc;
41
42 char *mon_name;
43 char *nodename;
44};
45
46struct nsm_res {
47 u32 status;
48 u32 state;
49};
50
51static const struct rpc_program nsm_program;
52static LIST_HEAD(nsm_handles);
53static DEFINE_SPINLOCK(nsm_lock);
54
55/*
56 * Local NSM state
57 */
58u32 __read_mostly nsm_local_state;
59bool __read_mostly nsm_use_hostnames;
60
61static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
62{
63 return (struct sockaddr *)&nsm->sm_addr;
64}
65
66static struct rpc_clnt *nsm_create(struct net *net)
67{
68 struct sockaddr_in sin = {
69 .sin_family = AF_INET,
70 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
71 };
72 struct rpc_create_args args = {
73 .net = net,
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,
82 };
83
84 return rpc_create(&args);
85}
86
87static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res,
88 struct net *net)
89{
90 struct rpc_clnt *clnt;
91 int status;
92 struct nsm_args args = {
93 .priv = &nsm->sm_priv,
94 .prog = NLM_PROGRAM,
95 .vers = 3,
96 .proc = NLMPROC_NSM_NOTIFY,
97 .mon_name = nsm->sm_mon_name,
98 .nodename = utsname()->nodename,
99 };
100 struct rpc_message msg = {
101 .rpc_argp = &args,
102 .rpc_resp = res,
103 };
104
105 clnt = nsm_create(net);
106 if (IS_ERR(clnt)) {
107 status = PTR_ERR(clnt);
108 dprintk("lockd: failed to create NSM upcall transport, "
109 "status=%d\n", status);
110 goto out;
111 }
112
113 memset(res, 0, sizeof(*res));
114
115 msg.rpc_proc = &clnt->cl_procinfo[proc];
116 status = rpc_call_sync(clnt, &msg, 0);
117 if (status == -ECONNREFUSED) {
118 dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n",
119 status);
120 rpc_force_rebind(clnt);
121 status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
122 }
123 if (status < 0)
124 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
125 status);
126 else
127 status = 0;
128 rpc_shutdown_client(clnt);
129 out:
130 return status;
131}
132
133/**
134 * nsm_monitor - Notify a peer in case we reboot
135 * @host: pointer to nlm_host of peer to notify
136 *
137 * If this peer is not already monitored, this function sends an
138 * upcall to the local rpc.statd to record the name/address of
139 * the peer to notify in case we reboot.
140 *
141 * Returns zero if the peer is monitored by the local rpc.statd;
142 * otherwise a negative errno value is returned.
143 */
144int nsm_monitor(const struct nlm_host *host)
145{
146 struct nsm_handle *nsm = host->h_nsmhandle;
147 struct nsm_res res;
148 int status;
149
150 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
151
152 if (nsm->sm_monitored)
153 return 0;
154
155 /*
156 * Choose whether to record the caller_name or IP address of
157 * this peer in the local rpc.statd's database.
158 */
159 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
160
161 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host->net);
162 if (unlikely(res.status != 0))
163 status = -EIO;
164 if (unlikely(status < 0)) {
165 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
166 return status;
167 }
168
169 nsm->sm_monitored = 1;
170 if (unlikely(nsm_local_state != res.state)) {
171 nsm_local_state = res.state;
172 dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
173 }
174 return 0;
175}
176
177/**
178 * nsm_unmonitor - Unregister peer notification
179 * @host: pointer to nlm_host of peer to stop monitoring
180 *
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
183 * when we reboot.
184 */
185void nsm_unmonitor(const struct nlm_host *host)
186{
187 struct nsm_handle *nsm = host->h_nsmhandle;
188 struct nsm_res res;
189 int status;
190
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);
194
195 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host->net);
196 if (res.status != 0)
197 status = -EIO;
198 if (status < 0)
199 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
200 nsm->sm_name);
201 else
202 nsm->sm_monitored = 0;
203 }
204}
205
206static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
207 const size_t len)
208{
209 struct nsm_handle *nsm;
210
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)
214 return nsm;
215 return NULL;
216}
217
218static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
219{
220 struct nsm_handle *nsm;
221
222 list_for_each_entry(nsm, &nsm_handles, sm_link)
223 if (rpc_cmp_addr(nsm_addr(nsm), sap))
224 return nsm;
225 return NULL;
226}
227
228static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
229{
230 struct nsm_handle *nsm;
231
232 list_for_each_entry(nsm, &nsm_handles, sm_link)
233 if (memcmp(nsm->sm_priv.data, priv->data,
234 sizeof(priv->data)) == 0)
235 return nsm;
236 return NULL;
237}
238
239/*
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
243 * requests.
244 *
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.
250 *
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.
255 */
256static void nsm_init_private(struct nsm_handle *nsm)
257{
258 u64 *p = (u64 *)&nsm->sm_priv.data;
259 struct timespec ts;
260 s64 ns;
261
262 ktime_get_ts(&ts);
263 ns = timespec_to_ns(&ts);
264 put_unaligned(ns, p);
265 put_unaligned((unsigned long)nsm, p + 1);
266}
267
268static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
269 const size_t salen,
270 const char *hostname,
271 const size_t hostname_len)
272{
273 struct nsm_handle *new;
274
275 new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
276 if (unlikely(new == NULL))
277 return NULL;
278
279 atomic_set(&new->sm_count, 1);
280 new->sm_name = (char *)(new + 1);
281 memcpy(nsm_addr(new), sap, salen);
282 new->sm_addrlen = salen;
283 nsm_init_private(new);
284
285 if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
286 sizeof(new->sm_addrbuf)) == 0)
287 (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
288 "unsupported address family");
289 memcpy(new->sm_name, hostname, hostname_len);
290 new->sm_name[hostname_len] = '\0';
291
292 return new;
293}
294
295/**
296 * nsm_get_handle - Find or create a cached nsm_handle
297 * @sap: pointer to socket address of handle to find
298 * @salen: length of socket address
299 * @hostname: pointer to C string containing hostname to find
300 * @hostname_len: length of C string
301 *
302 * Behavior is modulated by the global nsm_use_hostnames variable.
303 *
304 * Returns a cached nsm_handle after bumping its ref count, or
305 * returns a fresh nsm_handle if a handle that matches @sap and/or
306 * @hostname cannot be found in the handle cache. Returns NULL if
307 * an error occurs.
308 */
309struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
310 const size_t salen, const char *hostname,
311 const size_t hostname_len)
312{
313 struct nsm_handle *cached, *new = NULL;
314
315 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
316 if (printk_ratelimit()) {
317 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
318 "in NFS lock request\n",
319 (int)hostname_len, hostname);
320 }
321 return NULL;
322 }
323
324retry:
325 spin_lock(&nsm_lock);
326
327 if (nsm_use_hostnames && hostname != NULL)
328 cached = nsm_lookup_hostname(hostname, hostname_len);
329 else
330 cached = nsm_lookup_addr(sap);
331
332 if (cached != NULL) {
333 atomic_inc(&cached->sm_count);
334 spin_unlock(&nsm_lock);
335 kfree(new);
336 dprintk("lockd: found nsm_handle for %s (%s), "
337 "cnt %d\n", cached->sm_name,
338 cached->sm_addrbuf,
339 atomic_read(&cached->sm_count));
340 return cached;
341 }
342
343 if (new != NULL) {
344 list_add(&new->sm_link, &nsm_handles);
345 spin_unlock(&nsm_lock);
346 dprintk("lockd: created nsm_handle for %s (%s)\n",
347 new->sm_name, new->sm_addrbuf);
348 return new;
349 }
350
351 spin_unlock(&nsm_lock);
352
353 new = nsm_create_handle(sap, salen, hostname, hostname_len);
354 if (unlikely(new == NULL))
355 return NULL;
356 goto retry;
357}
358
359/**
360 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
361 * @info: pointer to NLMPROC_SM_NOTIFY arguments
362 *
363 * Returns a matching nsm_handle if found in the nsm cache. The returned
364 * nsm_handle's reference count is bumped. Otherwise returns NULL if some
365 * error occurred.
366 */
367struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
368{
369 struct nsm_handle *cached;
370
371 spin_lock(&nsm_lock);
372
373 cached = nsm_lookup_priv(&info->priv);
374 if (unlikely(cached == NULL)) {
375 spin_unlock(&nsm_lock);
376 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
377 info->len, info->mon);
378 return cached;
379 }
380
381 atomic_inc(&cached->sm_count);
382 spin_unlock(&nsm_lock);
383
384 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
385 cached->sm_name, cached->sm_addrbuf,
386 atomic_read(&cached->sm_count));
387 return cached;
388}
389
390/**
391 * nsm_release - Release an NSM handle
392 * @nsm: pointer to handle to be released
393 *
394 */
395void nsm_release(struct nsm_handle *nsm)
396{
397 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
398 list_del(&nsm->sm_link);
399 spin_unlock(&nsm_lock);
400 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
401 nsm->sm_name, nsm->sm_addrbuf);
402 kfree(nsm);
403 }
404}
405
406/*
407 * XDR functions for NSM.
408 *
409 * See http://www.opengroup.org/ for details on the Network
410 * Status Monitor wire protocol.
411 */
412
413static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
414{
415 const u32 len = strlen(string);
416 __be32 *p;
417
418 BUG_ON(len > SM_MAXSTRLEN);
419 p = xdr_reserve_space(xdr, 4 + len);
420 xdr_encode_opaque(p, string, len);
421}
422
423/*
424 * "mon_name" specifies the host to be monitored.
425 */
426static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
427{
428 encode_nsm_string(xdr, argp->mon_name);
429}
430
431/*
432 * The "my_id" argument specifies the hostname and RPC procedure
433 * to be called when the status manager receives notification
434 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
435 * has changed.
436 */
437static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
438{
439 __be32 *p;
440
441 encode_nsm_string(xdr, argp->nodename);
442 p = xdr_reserve_space(xdr, 4 + 4 + 4);
443 *p++ = cpu_to_be32(argp->prog);
444 *p++ = cpu_to_be32(argp->vers);
445 *p = cpu_to_be32(argp->proc);
446}
447
448/*
449 * The "mon_id" argument specifies the non-private arguments
450 * of an NSMPROC_MON or NSMPROC_UNMON call.
451 */
452static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
453{
454 encode_mon_name(xdr, argp);
455 encode_my_id(xdr, argp);
456}
457
458/*
459 * The "priv" argument may contain private information required
460 * by the NSMPROC_MON call. This information will be supplied in the
461 * NLMPROC_SM_NOTIFY call.
462 */
463static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
464{
465 __be32 *p;
466
467 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
468 xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
469}
470
471static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
472 const struct nsm_args *argp)
473{
474 encode_mon_id(xdr, argp);
475 encode_priv(xdr, argp);
476}
477
478static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
479 const struct nsm_args *argp)
480{
481 encode_mon_id(xdr, argp);
482}
483
484static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
485 struct xdr_stream *xdr,
486 struct nsm_res *resp)
487{
488 __be32 *p;
489
490 p = xdr_inline_decode(xdr, 4 + 4);
491 if (unlikely(p == NULL))
492 return -EIO;
493 resp->status = be32_to_cpup(p++);
494 resp->state = be32_to_cpup(p);
495
496 dprintk("lockd: %s status %d state %d\n",
497 __func__, resp->status, resp->state);
498 return 0;
499}
500
501static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
502 struct xdr_stream *xdr,
503 struct nsm_res *resp)
504{
505 __be32 *p;
506
507 p = xdr_inline_decode(xdr, 4);
508 if (unlikely(p == NULL))
509 return -EIO;
510 resp->state = be32_to_cpup(p);
511
512 dprintk("lockd: %s state %d\n", __func__, resp->state);
513 return 0;
514}
515
516#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
517#define SM_my_id_sz (SM_my_name_sz+3)
518#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
519#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
520#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
521#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
522#define SM_monres_sz 2
523#define SM_unmonres_sz 1
524
525static struct rpc_procinfo nsm_procedures[] = {
526[NSMPROC_MON] = {
527 .p_proc = NSMPROC_MON,
528 .p_encode = (kxdreproc_t)nsm_xdr_enc_mon,
529 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res,
530 .p_arglen = SM_mon_sz,
531 .p_replen = SM_monres_sz,
532 .p_statidx = NSMPROC_MON,
533 .p_name = "MONITOR",
534 },
535[NSMPROC_UNMON] = {
536 .p_proc = NSMPROC_UNMON,
537 .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon,
538 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat,
539 .p_arglen = SM_mon_id_sz,
540 .p_replen = SM_unmonres_sz,
541 .p_statidx = NSMPROC_UNMON,
542 .p_name = "UNMONITOR",
543 },
544};
545
546static const struct rpc_version nsm_version1 = {
547 .number = 1,
548 .nrprocs = ARRAY_SIZE(nsm_procedures),
549 .procs = nsm_procedures
550};
551
552static const struct rpc_version *nsm_version[] = {
553 [1] = &nsm_version1,
554};
555
556static struct rpc_stat nsm_stats;
557
558static const struct rpc_program nsm_program = {
559 .name = "statd",
560 .number = NSM_PROGRAM,
561 .nrvers = ARRAY_SIZE(nsm_version),
562 .version = nsm_version,
563 .stats = &nsm_stats
564};