Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/lockd/mon.c
0004  *
0005  * The kernel statd client.
0006  *
0007  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
0008  */
0009 
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/ktime.h>
0013 #include <linux/slab.h>
0014 
0015 #include <linux/sunrpc/clnt.h>
0016 #include <linux/sunrpc/addr.h>
0017 #include <linux/sunrpc/xprtsock.h>
0018 #include <linux/sunrpc/svc.h>
0019 #include <linux/lockd/lockd.h>
0020 
0021 #include <asm/unaligned.h>
0022 
0023 #include "netns.h"
0024 
0025 #define NLMDBG_FACILITY     NLMDBG_MONITOR
0026 #define NSM_PROGRAM     100024
0027 #define NSM_VERSION     1
0028 
0029 enum {
0030     NSMPROC_NULL,
0031     NSMPROC_STAT,
0032     NSMPROC_MON,
0033     NSMPROC_UNMON,
0034     NSMPROC_UNMON_ALL,
0035     NSMPROC_SIMU_CRASH,
0036     NSMPROC_NOTIFY,
0037 };
0038 
0039 struct nsm_args {
0040     struct nsm_private  *priv;
0041     u32         prog;       /* RPC callback info */
0042     u32         vers;
0043     u32         proc;
0044 
0045     char            *mon_name;
0046     const char      *nodename;
0047 };
0048 
0049 struct nsm_res {
0050     u32         status;
0051     u32         state;
0052 };
0053 
0054 static const struct rpc_program nsm_program;
0055 static              DEFINE_SPINLOCK(nsm_lock);
0056 
0057 /*
0058  * Local NSM state
0059  */
0060 u32 __read_mostly       nsm_local_state;
0061 bool    __read_mostly       nsm_use_hostnames;
0062 
0063 static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
0064 {
0065     return (struct sockaddr *)&nsm->sm_addr;
0066 }
0067 
0068 static struct rpc_clnt *nsm_create(struct net *net, const char *nodename)
0069 {
0070     struct sockaddr_in sin = {
0071         .sin_family     = AF_INET,
0072         .sin_addr.s_addr    = htonl(INADDR_LOOPBACK),
0073     };
0074     struct rpc_create_args args = {
0075         .net            = net,
0076         .protocol       = XPRT_TRANSPORT_TCP,
0077         .address        = (struct sockaddr *)&sin,
0078         .addrsize       = sizeof(sin),
0079         .servername     = "rpc.statd",
0080         .nodename       = nodename,
0081         .program        = &nsm_program,
0082         .version        = NSM_VERSION,
0083         .authflavor     = RPC_AUTH_NULL,
0084         .flags          = RPC_CLNT_CREATE_NOPING,
0085         .cred           = current_cred(),
0086     };
0087 
0088     return rpc_create(&args);
0089 }
0090 
0091 static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res,
0092              const struct nlm_host *host)
0093 {
0094     int     status;
0095     struct rpc_clnt *clnt;
0096     struct nsm_args args = {
0097         .priv       = &nsm->sm_priv,
0098         .prog       = NLM_PROGRAM,
0099         .vers       = 3,
0100         .proc       = NLMPROC_NSM_NOTIFY,
0101         .mon_name   = nsm->sm_mon_name,
0102         .nodename   = host->nodename,
0103     };
0104     struct rpc_message msg = {
0105         .rpc_argp   = &args,
0106         .rpc_resp   = res,
0107     };
0108 
0109     memset(res, 0, sizeof(*res));
0110 
0111     clnt = nsm_create(host->net, host->nodename);
0112     if (IS_ERR(clnt)) {
0113         dprintk("lockd: failed to create NSM upcall transport, "
0114             "status=%ld, net=%x\n", PTR_ERR(clnt),
0115             host->net->ns.inum);
0116         return PTR_ERR(clnt);
0117     }
0118 
0119     msg.rpc_proc = &clnt->cl_procinfo[proc];
0120     status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
0121     if (status == -ECONNREFUSED) {
0122         dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n",
0123                 status);
0124         rpc_force_rebind(clnt);
0125         status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
0126     }
0127     if (status < 0)
0128         dprintk("lockd: NSM upcall RPC failed, status=%d\n",
0129                 status);
0130     else
0131         status = 0;
0132 
0133     rpc_shutdown_client(clnt);
0134     return status;
0135 }
0136 
0137 /**
0138  * nsm_monitor - Notify a peer in case we reboot
0139  * @host: pointer to nlm_host of peer to notify
0140  *
0141  * If this peer is not already monitored, this function sends an
0142  * upcall to the local rpc.statd to record the name/address of
0143  * the peer to notify in case we reboot.
0144  *
0145  * Returns zero if the peer is monitored by the local rpc.statd;
0146  * otherwise a negative errno value is returned.
0147  */
0148 int nsm_monitor(const struct nlm_host *host)
0149 {
0150     struct nsm_handle *nsm = host->h_nsmhandle;
0151     struct nsm_res  res;
0152     int     status;
0153 
0154     dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
0155 
0156     if (nsm->sm_monitored)
0157         return 0;
0158 
0159     /*
0160      * Choose whether to record the caller_name or IP address of
0161      * this peer in the local rpc.statd's database.
0162      */
0163     nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
0164 
0165     status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host);
0166     if (unlikely(res.status != 0))
0167         status = -EIO;
0168     if (unlikely(status < 0)) {
0169         pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name);
0170         return status;
0171     }
0172 
0173     nsm->sm_monitored = 1;
0174     if (unlikely(nsm_local_state != res.state)) {
0175         nsm_local_state = res.state;
0176         dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
0177     }
0178     return 0;
0179 }
0180 
0181 /**
0182  * nsm_unmonitor - Unregister peer notification
0183  * @host: pointer to nlm_host of peer to stop monitoring
0184  *
0185  * If this peer is monitored, this function sends an upcall to
0186  * tell the local rpc.statd not to send this peer a notification
0187  * when we reboot.
0188  */
0189 void nsm_unmonitor(const struct nlm_host *host)
0190 {
0191     struct nsm_handle *nsm = host->h_nsmhandle;
0192     struct nsm_res  res;
0193     int status;
0194 
0195     if (refcount_read(&nsm->sm_count) == 1
0196      && nsm->sm_monitored && !nsm->sm_sticky) {
0197         dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
0198 
0199         status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host);
0200         if (res.status != 0)
0201             status = -EIO;
0202         if (status < 0)
0203             printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
0204                     nsm->sm_name);
0205         else
0206             nsm->sm_monitored = 0;
0207     }
0208 }
0209 
0210 static struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles,
0211                     const char *hostname, const size_t len)
0212 {
0213     struct nsm_handle *nsm;
0214 
0215     list_for_each_entry(nsm, nsm_handles, sm_link)
0216         if (strlen(nsm->sm_name) == len &&
0217             memcmp(nsm->sm_name, hostname, len) == 0)
0218             return nsm;
0219     return NULL;
0220 }
0221 
0222 static struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles,
0223                     const struct sockaddr *sap)
0224 {
0225     struct nsm_handle *nsm;
0226 
0227     list_for_each_entry(nsm, nsm_handles, sm_link)
0228         if (rpc_cmp_addr(nsm_addr(nsm), sap))
0229             return nsm;
0230     return NULL;
0231 }
0232 
0233 static struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles,
0234                     const struct nsm_private *priv)
0235 {
0236     struct nsm_handle *nsm;
0237 
0238     list_for_each_entry(nsm, nsm_handles, sm_link)
0239         if (memcmp(nsm->sm_priv.data, priv->data,
0240                     sizeof(priv->data)) == 0)
0241             return nsm;
0242     return NULL;
0243 }
0244 
0245 /*
0246  * Construct a unique cookie to match this nsm_handle to this monitored
0247  * host.  It is passed to the local rpc.statd via NSMPROC_MON, and
0248  * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
0249  * requests.
0250  *
0251  * The NSM protocol requires that these cookies be unique while the
0252  * system is running.  We prefer a stronger requirement of making them
0253  * unique across reboots.  If user space bugs cause a stale cookie to
0254  * be sent to the kernel, it could cause the wrong host to lose its
0255  * lock state if cookies were not unique across reboots.
0256  *
0257  * The cookies are exposed only to local user space via loopback.  They
0258  * do not appear on the physical network.  If we want greater security
0259  * for some reason, nsm_init_private() could perform a one-way hash to
0260  * obscure the contents of the cookie.
0261  */
0262 static void nsm_init_private(struct nsm_handle *nsm)
0263 {
0264     u64 *p = (u64 *)&nsm->sm_priv.data;
0265     s64 ns;
0266 
0267     ns = ktime_get_ns();
0268     put_unaligned(ns, p);
0269     put_unaligned((unsigned long)nsm, p + 1);
0270 }
0271 
0272 static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
0273                         const size_t salen,
0274                         const char *hostname,
0275                         const size_t hostname_len)
0276 {
0277     struct nsm_handle *new;
0278 
0279     new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
0280     if (unlikely(new == NULL))
0281         return NULL;
0282 
0283     refcount_set(&new->sm_count, 1);
0284     new->sm_name = (char *)(new + 1);
0285     memcpy(nsm_addr(new), sap, salen);
0286     new->sm_addrlen = salen;
0287     nsm_init_private(new);
0288 
0289     if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
0290                     sizeof(new->sm_addrbuf)) == 0)
0291         (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
0292                 "unsupported address family");
0293     memcpy(new->sm_name, hostname, hostname_len);
0294     new->sm_name[hostname_len] = '\0';
0295 
0296     return new;
0297 }
0298 
0299 /**
0300  * nsm_get_handle - Find or create a cached nsm_handle
0301  * @net: network namespace
0302  * @sap: pointer to socket address of handle to find
0303  * @salen: length of socket address
0304  * @hostname: pointer to C string containing hostname to find
0305  * @hostname_len: length of C string
0306  *
0307  * Behavior is modulated by the global nsm_use_hostnames variable.
0308  *
0309  * Returns a cached nsm_handle after bumping its ref count, or
0310  * returns a fresh nsm_handle if a handle that matches @sap and/or
0311  * @hostname cannot be found in the handle cache.  Returns NULL if
0312  * an error occurs.
0313  */
0314 struct nsm_handle *nsm_get_handle(const struct net *net,
0315                   const struct sockaddr *sap,
0316                   const size_t salen, const char *hostname,
0317                   const size_t hostname_len)
0318 {
0319     struct nsm_handle *cached, *new = NULL;
0320     struct lockd_net *ln = net_generic(net, lockd_net_id);
0321 
0322     if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
0323         if (printk_ratelimit()) {
0324             printk(KERN_WARNING "Invalid hostname \"%.*s\" "
0325                         "in NFS lock request\n",
0326                 (int)hostname_len, hostname);
0327         }
0328         return NULL;
0329     }
0330 
0331 retry:
0332     spin_lock(&nsm_lock);
0333 
0334     if (nsm_use_hostnames && hostname != NULL)
0335         cached = nsm_lookup_hostname(&ln->nsm_handles,
0336                     hostname, hostname_len);
0337     else
0338         cached = nsm_lookup_addr(&ln->nsm_handles, sap);
0339 
0340     if (cached != NULL) {
0341         refcount_inc(&cached->sm_count);
0342         spin_unlock(&nsm_lock);
0343         kfree(new);
0344         dprintk("lockd: found nsm_handle for %s (%s), "
0345                 "cnt %d\n", cached->sm_name,
0346                 cached->sm_addrbuf,
0347                 refcount_read(&cached->sm_count));
0348         return cached;
0349     }
0350 
0351     if (new != NULL) {
0352         list_add(&new->sm_link, &ln->nsm_handles);
0353         spin_unlock(&nsm_lock);
0354         dprintk("lockd: created nsm_handle for %s (%s)\n",
0355                 new->sm_name, new->sm_addrbuf);
0356         return new;
0357     }
0358 
0359     spin_unlock(&nsm_lock);
0360 
0361     new = nsm_create_handle(sap, salen, hostname, hostname_len);
0362     if (unlikely(new == NULL))
0363         return NULL;
0364     goto retry;
0365 }
0366 
0367 /**
0368  * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
0369  * @net:  network namespace
0370  * @info: pointer to NLMPROC_SM_NOTIFY arguments
0371  *
0372  * Returns a matching nsm_handle if found in the nsm cache. The returned
0373  * nsm_handle's reference count is bumped. Otherwise returns NULL if some
0374  * error occurred.
0375  */
0376 struct nsm_handle *nsm_reboot_lookup(const struct net *net,
0377                 const struct nlm_reboot *info)
0378 {
0379     struct nsm_handle *cached;
0380     struct lockd_net *ln = net_generic(net, lockd_net_id);
0381 
0382     spin_lock(&nsm_lock);
0383 
0384     cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv);
0385     if (unlikely(cached == NULL)) {
0386         spin_unlock(&nsm_lock);
0387         dprintk("lockd: never saw rebooted peer '%.*s' before\n",
0388                 info->len, info->mon);
0389         return cached;
0390     }
0391 
0392     refcount_inc(&cached->sm_count);
0393     spin_unlock(&nsm_lock);
0394 
0395     dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
0396             cached->sm_name, cached->sm_addrbuf,
0397             refcount_read(&cached->sm_count));
0398     return cached;
0399 }
0400 
0401 /**
0402  * nsm_release - Release an NSM handle
0403  * @nsm: pointer to handle to be released
0404  *
0405  */
0406 void nsm_release(struct nsm_handle *nsm)
0407 {
0408     if (refcount_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
0409         list_del(&nsm->sm_link);
0410         spin_unlock(&nsm_lock);
0411         dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
0412                 nsm->sm_name, nsm->sm_addrbuf);
0413         kfree(nsm);
0414     }
0415 }
0416 
0417 /*
0418  * XDR functions for NSM.
0419  *
0420  * See https://www.opengroup.org/ for details on the Network
0421  * Status Monitor wire protocol.
0422  */
0423 
0424 static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
0425 {
0426     const u32 len = strlen(string);
0427     __be32 *p;
0428 
0429     p = xdr_reserve_space(xdr, 4 + len);
0430     xdr_encode_opaque(p, string, len);
0431 }
0432 
0433 /*
0434  * "mon_name" specifies the host to be monitored.
0435  */
0436 static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
0437 {
0438     encode_nsm_string(xdr, argp->mon_name);
0439 }
0440 
0441 /*
0442  * The "my_id" argument specifies the hostname and RPC procedure
0443  * to be called when the status manager receives notification
0444  * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
0445  * has changed.
0446  */
0447 static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
0448 {
0449     __be32 *p;
0450 
0451     encode_nsm_string(xdr, argp->nodename);
0452     p = xdr_reserve_space(xdr, 4 + 4 + 4);
0453     *p++ = cpu_to_be32(argp->prog);
0454     *p++ = cpu_to_be32(argp->vers);
0455     *p = cpu_to_be32(argp->proc);
0456 }
0457 
0458 /*
0459  * The "mon_id" argument specifies the non-private arguments
0460  * of an NSMPROC_MON or NSMPROC_UNMON call.
0461  */
0462 static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
0463 {
0464     encode_mon_name(xdr, argp);
0465     encode_my_id(xdr, argp);
0466 }
0467 
0468 /*
0469  * The "priv" argument may contain private information required
0470  * by the NSMPROC_MON call. This information will be supplied in the
0471  * NLMPROC_SM_NOTIFY call.
0472  */
0473 static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
0474 {
0475     __be32 *p;
0476 
0477     p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
0478     xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
0479 }
0480 
0481 static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
0482                 const void *argp)
0483 {
0484     encode_mon_id(xdr, argp);
0485     encode_priv(xdr, argp);
0486 }
0487 
0488 static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
0489                   const void *argp)
0490 {
0491     encode_mon_id(xdr, argp);
0492 }
0493 
0494 static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
0495                 struct xdr_stream *xdr,
0496                 void *data)
0497 {
0498     struct nsm_res *resp = data;
0499     __be32 *p;
0500 
0501     p = xdr_inline_decode(xdr, 4 + 4);
0502     if (unlikely(p == NULL))
0503         return -EIO;
0504     resp->status = be32_to_cpup(p++);
0505     resp->state = be32_to_cpup(p);
0506 
0507     dprintk("lockd: %s status %d state %d\n",
0508         __func__, resp->status, resp->state);
0509     return 0;
0510 }
0511 
0512 static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
0513                 struct xdr_stream *xdr,
0514                 void *data)
0515 {
0516     struct nsm_res *resp = data;
0517     __be32 *p;
0518 
0519     p = xdr_inline_decode(xdr, 4);
0520     if (unlikely(p == NULL))
0521         return -EIO;
0522     resp->state = be32_to_cpup(p);
0523 
0524     dprintk("lockd: %s state %d\n", __func__, resp->state);
0525     return 0;
0526 }
0527 
0528 #define SM_my_name_sz   (1+XDR_QUADLEN(SM_MAXSTRLEN))
0529 #define SM_my_id_sz (SM_my_name_sz+3)
0530 #define SM_mon_name_sz  (1+XDR_QUADLEN(SM_MAXSTRLEN))
0531 #define SM_mon_id_sz    (SM_mon_name_sz+SM_my_id_sz)
0532 #define SM_priv_sz  (XDR_QUADLEN(SM_PRIV_SIZE))
0533 #define SM_mon_sz   (SM_mon_id_sz+SM_priv_sz)
0534 #define SM_monres_sz    2
0535 #define SM_unmonres_sz  1
0536 
0537 static const struct rpc_procinfo nsm_procedures[] = {
0538 [NSMPROC_MON] = {
0539         .p_proc     = NSMPROC_MON,
0540         .p_encode   = nsm_xdr_enc_mon,
0541         .p_decode   = nsm_xdr_dec_stat_res,
0542         .p_arglen   = SM_mon_sz,
0543         .p_replen   = SM_monres_sz,
0544         .p_statidx  = NSMPROC_MON,
0545         .p_name     = "MONITOR",
0546     },
0547 [NSMPROC_UNMON] = {
0548         .p_proc     = NSMPROC_UNMON,
0549         .p_encode   = nsm_xdr_enc_unmon,
0550         .p_decode   = nsm_xdr_dec_stat,
0551         .p_arglen   = SM_mon_id_sz,
0552         .p_replen   = SM_unmonres_sz,
0553         .p_statidx  = NSMPROC_UNMON,
0554         .p_name     = "UNMONITOR",
0555     },
0556 };
0557 
0558 static unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)];
0559 static const struct rpc_version nsm_version1 = {
0560     .number     = 1,
0561     .nrprocs    = ARRAY_SIZE(nsm_procedures),
0562     .procs      = nsm_procedures,
0563     .counts     = nsm_version1_counts,
0564 };
0565 
0566 static const struct rpc_version *nsm_version[] = {
0567     [1] = &nsm_version1,
0568 };
0569 
0570 static struct rpc_stat      nsm_stats;
0571 
0572 static const struct rpc_program nsm_program = {
0573     .name       = "statd",
0574     .number     = NSM_PROGRAM,
0575     .nrvers     = ARRAY_SIZE(nsm_version),
0576     .version    = nsm_version,
0577     .stats      = &nsm_stats
0578 };