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