18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/fs/lockd/mon.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * The kernel statd client. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/types.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/ktime.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/sunrpc/clnt.h> 168c2ecf20Sopenharmony_ci#include <linux/sunrpc/addr.h> 178c2ecf20Sopenharmony_ci#include <linux/sunrpc/xprtsock.h> 188c2ecf20Sopenharmony_ci#include <linux/sunrpc/svc.h> 198c2ecf20Sopenharmony_ci#include <linux/lockd/lockd.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include "netns.h" 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define NLMDBG_FACILITY NLMDBG_MONITOR 268c2ecf20Sopenharmony_ci#define NSM_PROGRAM 100024 278c2ecf20Sopenharmony_ci#define NSM_VERSION 1 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cienum { 308c2ecf20Sopenharmony_ci NSMPROC_NULL, 318c2ecf20Sopenharmony_ci NSMPROC_STAT, 328c2ecf20Sopenharmony_ci NSMPROC_MON, 338c2ecf20Sopenharmony_ci NSMPROC_UNMON, 348c2ecf20Sopenharmony_ci NSMPROC_UNMON_ALL, 358c2ecf20Sopenharmony_ci NSMPROC_SIMU_CRASH, 368c2ecf20Sopenharmony_ci NSMPROC_NOTIFY, 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistruct nsm_args { 408c2ecf20Sopenharmony_ci struct nsm_private *priv; 418c2ecf20Sopenharmony_ci u32 prog; /* RPC callback info */ 428c2ecf20Sopenharmony_ci u32 vers; 438c2ecf20Sopenharmony_ci u32 proc; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci char *mon_name; 468c2ecf20Sopenharmony_ci const char *nodename; 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct nsm_res { 508c2ecf20Sopenharmony_ci u32 status; 518c2ecf20Sopenharmony_ci u32 state; 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic const struct rpc_program nsm_program; 558c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(nsm_lock); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* 588c2ecf20Sopenharmony_ci * Local NSM state 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ciu32 __read_mostly nsm_local_state; 618c2ecf20Sopenharmony_cibool __read_mostly nsm_use_hostnames; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci return (struct sockaddr *)&nsm->sm_addr; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic struct rpc_clnt *nsm_create(struct net *net, const char *nodename) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct sockaddr_in sin = { 718c2ecf20Sopenharmony_ci .sin_family = AF_INET, 728c2ecf20Sopenharmony_ci .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 738c2ecf20Sopenharmony_ci }; 748c2ecf20Sopenharmony_ci struct rpc_create_args args = { 758c2ecf20Sopenharmony_ci .net = net, 768c2ecf20Sopenharmony_ci .protocol = XPRT_TRANSPORT_TCP, 778c2ecf20Sopenharmony_ci .address = (struct sockaddr *)&sin, 788c2ecf20Sopenharmony_ci .addrsize = sizeof(sin), 798c2ecf20Sopenharmony_ci .servername = "rpc.statd", 808c2ecf20Sopenharmony_ci .nodename = nodename, 818c2ecf20Sopenharmony_ci .program = &nsm_program, 828c2ecf20Sopenharmony_ci .version = NSM_VERSION, 838c2ecf20Sopenharmony_ci .authflavor = RPC_AUTH_NULL, 848c2ecf20Sopenharmony_ci .flags = RPC_CLNT_CREATE_NOPING, 858c2ecf20Sopenharmony_ci .cred = current_cred(), 868c2ecf20Sopenharmony_ci }; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return rpc_create(&args); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res, 928c2ecf20Sopenharmony_ci const struct nlm_host *host) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci int status; 958c2ecf20Sopenharmony_ci struct rpc_clnt *clnt; 968c2ecf20Sopenharmony_ci struct nsm_args args = { 978c2ecf20Sopenharmony_ci .priv = &nsm->sm_priv, 988c2ecf20Sopenharmony_ci .prog = NLM_PROGRAM, 998c2ecf20Sopenharmony_ci .vers = 3, 1008c2ecf20Sopenharmony_ci .proc = NLMPROC_NSM_NOTIFY, 1018c2ecf20Sopenharmony_ci .mon_name = nsm->sm_mon_name, 1028c2ecf20Sopenharmony_ci .nodename = host->nodename, 1038c2ecf20Sopenharmony_ci }; 1048c2ecf20Sopenharmony_ci struct rpc_message msg = { 1058c2ecf20Sopenharmony_ci .rpc_argp = &args, 1068c2ecf20Sopenharmony_ci .rpc_resp = res, 1078c2ecf20Sopenharmony_ci }; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci memset(res, 0, sizeof(*res)); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci clnt = nsm_create(host->net, host->nodename); 1128c2ecf20Sopenharmony_ci if (IS_ERR(clnt)) { 1138c2ecf20Sopenharmony_ci dprintk("lockd: failed to create NSM upcall transport, " 1148c2ecf20Sopenharmony_ci "status=%ld, net=%x\n", PTR_ERR(clnt), 1158c2ecf20Sopenharmony_ci host->net->ns.inum); 1168c2ecf20Sopenharmony_ci return PTR_ERR(clnt); 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci msg.rpc_proc = &clnt->cl_procinfo[proc]; 1208c2ecf20Sopenharmony_ci status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 1218c2ecf20Sopenharmony_ci if (status == -ECONNREFUSED) { 1228c2ecf20Sopenharmony_ci dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n", 1238c2ecf20Sopenharmony_ci status); 1248c2ecf20Sopenharmony_ci rpc_force_rebind(clnt); 1258c2ecf20Sopenharmony_ci status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci if (status < 0) 1288c2ecf20Sopenharmony_ci dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1298c2ecf20Sopenharmony_ci status); 1308c2ecf20Sopenharmony_ci else 1318c2ecf20Sopenharmony_ci status = 0; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci rpc_shutdown_client(clnt); 1348c2ecf20Sopenharmony_ci return status; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci/** 1388c2ecf20Sopenharmony_ci * nsm_monitor - Notify a peer in case we reboot 1398c2ecf20Sopenharmony_ci * @host: pointer to nlm_host of peer to notify 1408c2ecf20Sopenharmony_ci * 1418c2ecf20Sopenharmony_ci * If this peer is not already monitored, this function sends an 1428c2ecf20Sopenharmony_ci * upcall to the local rpc.statd to record the name/address of 1438c2ecf20Sopenharmony_ci * the peer to notify in case we reboot. 1448c2ecf20Sopenharmony_ci * 1458c2ecf20Sopenharmony_ci * Returns zero if the peer is monitored by the local rpc.statd; 1468c2ecf20Sopenharmony_ci * otherwise a negative errno value is returned. 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ciint nsm_monitor(const struct nlm_host *host) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct nsm_handle *nsm = host->h_nsmhandle; 1518c2ecf20Sopenharmony_ci struct nsm_res res; 1528c2ecf20Sopenharmony_ci int status; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci if (nsm->sm_monitored) 1578c2ecf20Sopenharmony_ci return 0; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* 1608c2ecf20Sopenharmony_ci * Choose whether to record the caller_name or IP address of 1618c2ecf20Sopenharmony_ci * this peer in the local rpc.statd's database. 1628c2ecf20Sopenharmony_ci */ 1638c2ecf20Sopenharmony_ci nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host); 1668c2ecf20Sopenharmony_ci if (unlikely(res.status != 0)) 1678c2ecf20Sopenharmony_ci status = -EIO; 1688c2ecf20Sopenharmony_ci if (unlikely(status < 0)) { 1698c2ecf20Sopenharmony_ci pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name); 1708c2ecf20Sopenharmony_ci return status; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci nsm->sm_monitored = 1; 1748c2ecf20Sopenharmony_ci if (unlikely(nsm_local_state != res.state)) { 1758c2ecf20Sopenharmony_ci nsm_local_state = res.state; 1768c2ecf20Sopenharmony_ci dprintk("lockd: NSM state changed to %d\n", nsm_local_state); 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci return 0; 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci/** 1828c2ecf20Sopenharmony_ci * nsm_unmonitor - Unregister peer notification 1838c2ecf20Sopenharmony_ci * @host: pointer to nlm_host of peer to stop monitoring 1848c2ecf20Sopenharmony_ci * 1858c2ecf20Sopenharmony_ci * If this peer is monitored, this function sends an upcall to 1868c2ecf20Sopenharmony_ci * tell the local rpc.statd not to send this peer a notification 1878c2ecf20Sopenharmony_ci * when we reboot. 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_civoid nsm_unmonitor(const struct nlm_host *host) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci struct nsm_handle *nsm = host->h_nsmhandle; 1928c2ecf20Sopenharmony_ci struct nsm_res res; 1938c2ecf20Sopenharmony_ci int status; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci if (refcount_read(&nsm->sm_count) == 1 1968c2ecf20Sopenharmony_ci && nsm->sm_monitored && !nsm->sm_sticky) { 1978c2ecf20Sopenharmony_ci dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host); 2008c2ecf20Sopenharmony_ci if (res.status != 0) 2018c2ecf20Sopenharmony_ci status = -EIO; 2028c2ecf20Sopenharmony_ci if (status < 0) 2038c2ecf20Sopenharmony_ci printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2048c2ecf20Sopenharmony_ci nsm->sm_name); 2058c2ecf20Sopenharmony_ci else 2068c2ecf20Sopenharmony_ci nsm->sm_monitored = 0; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles, 2118c2ecf20Sopenharmony_ci const char *hostname, const size_t len) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct nsm_handle *nsm; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci list_for_each_entry(nsm, nsm_handles, sm_link) 2168c2ecf20Sopenharmony_ci if (strlen(nsm->sm_name) == len && 2178c2ecf20Sopenharmony_ci memcmp(nsm->sm_name, hostname, len) == 0) 2188c2ecf20Sopenharmony_ci return nsm; 2198c2ecf20Sopenharmony_ci return NULL; 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles, 2238c2ecf20Sopenharmony_ci const struct sockaddr *sap) 2248c2ecf20Sopenharmony_ci{ 2258c2ecf20Sopenharmony_ci struct nsm_handle *nsm; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci list_for_each_entry(nsm, nsm_handles, sm_link) 2288c2ecf20Sopenharmony_ci if (rpc_cmp_addr(nsm_addr(nsm), sap)) 2298c2ecf20Sopenharmony_ci return nsm; 2308c2ecf20Sopenharmony_ci return NULL; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles, 2348c2ecf20Sopenharmony_ci const struct nsm_private *priv) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct nsm_handle *nsm; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci list_for_each_entry(nsm, nsm_handles, sm_link) 2398c2ecf20Sopenharmony_ci if (memcmp(nsm->sm_priv.data, priv->data, 2408c2ecf20Sopenharmony_ci sizeof(priv->data)) == 0) 2418c2ecf20Sopenharmony_ci return nsm; 2428c2ecf20Sopenharmony_ci return NULL; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci/* 2468c2ecf20Sopenharmony_ci * Construct a unique cookie to match this nsm_handle to this monitored 2478c2ecf20Sopenharmony_ci * host. It is passed to the local rpc.statd via NSMPROC_MON, and 2488c2ecf20Sopenharmony_ci * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 2498c2ecf20Sopenharmony_ci * requests. 2508c2ecf20Sopenharmony_ci * 2518c2ecf20Sopenharmony_ci * The NSM protocol requires that these cookies be unique while the 2528c2ecf20Sopenharmony_ci * system is running. We prefer a stronger requirement of making them 2538c2ecf20Sopenharmony_ci * unique across reboots. If user space bugs cause a stale cookie to 2548c2ecf20Sopenharmony_ci * be sent to the kernel, it could cause the wrong host to lose its 2558c2ecf20Sopenharmony_ci * lock state if cookies were not unique across reboots. 2568c2ecf20Sopenharmony_ci * 2578c2ecf20Sopenharmony_ci * The cookies are exposed only to local user space via loopback. They 2588c2ecf20Sopenharmony_ci * do not appear on the physical network. If we want greater security 2598c2ecf20Sopenharmony_ci * for some reason, nsm_init_private() could perform a one-way hash to 2608c2ecf20Sopenharmony_ci * obscure the contents of the cookie. 2618c2ecf20Sopenharmony_ci */ 2628c2ecf20Sopenharmony_cistatic void nsm_init_private(struct nsm_handle *nsm) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci u64 *p = (u64 *)&nsm->sm_priv.data; 2658c2ecf20Sopenharmony_ci s64 ns; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci ns = ktime_get_ns(); 2688c2ecf20Sopenharmony_ci put_unaligned(ns, p); 2698c2ecf20Sopenharmony_ci put_unaligned((unsigned long)nsm, p + 1); 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 2738c2ecf20Sopenharmony_ci const size_t salen, 2748c2ecf20Sopenharmony_ci const char *hostname, 2758c2ecf20Sopenharmony_ci const size_t hostname_len) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci struct nsm_handle *new; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (!hostname) 2808c2ecf20Sopenharmony_ci return NULL; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 2838c2ecf20Sopenharmony_ci if (unlikely(new == NULL)) 2848c2ecf20Sopenharmony_ci return NULL; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci refcount_set(&new->sm_count, 1); 2878c2ecf20Sopenharmony_ci new->sm_name = (char *)(new + 1); 2888c2ecf20Sopenharmony_ci memcpy(nsm_addr(new), sap, salen); 2898c2ecf20Sopenharmony_ci new->sm_addrlen = salen; 2908c2ecf20Sopenharmony_ci nsm_init_private(new); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci if (rpc_ntop(nsm_addr(new), new->sm_addrbuf, 2938c2ecf20Sopenharmony_ci sizeof(new->sm_addrbuf)) == 0) 2948c2ecf20Sopenharmony_ci (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf), 2958c2ecf20Sopenharmony_ci "unsupported address family"); 2968c2ecf20Sopenharmony_ci memcpy(new->sm_name, hostname, hostname_len); 2978c2ecf20Sopenharmony_ci new->sm_name[hostname_len] = '\0'; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci return new; 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci/** 3038c2ecf20Sopenharmony_ci * nsm_get_handle - Find or create a cached nsm_handle 3048c2ecf20Sopenharmony_ci * @net: network namespace 3058c2ecf20Sopenharmony_ci * @sap: pointer to socket address of handle to find 3068c2ecf20Sopenharmony_ci * @salen: length of socket address 3078c2ecf20Sopenharmony_ci * @hostname: pointer to C string containing hostname to find 3088c2ecf20Sopenharmony_ci * @hostname_len: length of C string 3098c2ecf20Sopenharmony_ci * 3108c2ecf20Sopenharmony_ci * Behavior is modulated by the global nsm_use_hostnames variable. 3118c2ecf20Sopenharmony_ci * 3128c2ecf20Sopenharmony_ci * Returns a cached nsm_handle after bumping its ref count, or 3138c2ecf20Sopenharmony_ci * returns a fresh nsm_handle if a handle that matches @sap and/or 3148c2ecf20Sopenharmony_ci * @hostname cannot be found in the handle cache. Returns NULL if 3158c2ecf20Sopenharmony_ci * an error occurs. 3168c2ecf20Sopenharmony_ci */ 3178c2ecf20Sopenharmony_cistruct nsm_handle *nsm_get_handle(const struct net *net, 3188c2ecf20Sopenharmony_ci const struct sockaddr *sap, 3198c2ecf20Sopenharmony_ci const size_t salen, const char *hostname, 3208c2ecf20Sopenharmony_ci const size_t hostname_len) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci struct nsm_handle *cached, *new = NULL; 3238c2ecf20Sopenharmony_ci struct lockd_net *ln = net_generic(net, lockd_net_id); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 3268c2ecf20Sopenharmony_ci if (printk_ratelimit()) { 3278c2ecf20Sopenharmony_ci printk(KERN_WARNING "Invalid hostname \"%.*s\" " 3288c2ecf20Sopenharmony_ci "in NFS lock request\n", 3298c2ecf20Sopenharmony_ci (int)hostname_len, hostname); 3308c2ecf20Sopenharmony_ci } 3318c2ecf20Sopenharmony_ci return NULL; 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ciretry: 3358c2ecf20Sopenharmony_ci spin_lock(&nsm_lock); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci if (nsm_use_hostnames && hostname != NULL) 3388c2ecf20Sopenharmony_ci cached = nsm_lookup_hostname(&ln->nsm_handles, 3398c2ecf20Sopenharmony_ci hostname, hostname_len); 3408c2ecf20Sopenharmony_ci else 3418c2ecf20Sopenharmony_ci cached = nsm_lookup_addr(&ln->nsm_handles, sap); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci if (cached != NULL) { 3448c2ecf20Sopenharmony_ci refcount_inc(&cached->sm_count); 3458c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 3468c2ecf20Sopenharmony_ci kfree(new); 3478c2ecf20Sopenharmony_ci dprintk("lockd: found nsm_handle for %s (%s), " 3488c2ecf20Sopenharmony_ci "cnt %d\n", cached->sm_name, 3498c2ecf20Sopenharmony_ci cached->sm_addrbuf, 3508c2ecf20Sopenharmony_ci refcount_read(&cached->sm_count)); 3518c2ecf20Sopenharmony_ci return cached; 3528c2ecf20Sopenharmony_ci } 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci if (new != NULL) { 3558c2ecf20Sopenharmony_ci list_add(&new->sm_link, &ln->nsm_handles); 3568c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 3578c2ecf20Sopenharmony_ci dprintk("lockd: created nsm_handle for %s (%s)\n", 3588c2ecf20Sopenharmony_ci new->sm_name, new->sm_addrbuf); 3598c2ecf20Sopenharmony_ci return new; 3608c2ecf20Sopenharmony_ci } 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci new = nsm_create_handle(sap, salen, hostname, hostname_len); 3658c2ecf20Sopenharmony_ci if (unlikely(new == NULL)) 3668c2ecf20Sopenharmony_ci return NULL; 3678c2ecf20Sopenharmony_ci goto retry; 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci/** 3718c2ecf20Sopenharmony_ci * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 3728c2ecf20Sopenharmony_ci * @net: network namespace 3738c2ecf20Sopenharmony_ci * @info: pointer to NLMPROC_SM_NOTIFY arguments 3748c2ecf20Sopenharmony_ci * 3758c2ecf20Sopenharmony_ci * Returns a matching nsm_handle if found in the nsm cache. The returned 3768c2ecf20Sopenharmony_ci * nsm_handle's reference count is bumped. Otherwise returns NULL if some 3778c2ecf20Sopenharmony_ci * error occurred. 3788c2ecf20Sopenharmony_ci */ 3798c2ecf20Sopenharmony_cistruct nsm_handle *nsm_reboot_lookup(const struct net *net, 3808c2ecf20Sopenharmony_ci const struct nlm_reboot *info) 3818c2ecf20Sopenharmony_ci{ 3828c2ecf20Sopenharmony_ci struct nsm_handle *cached; 3838c2ecf20Sopenharmony_ci struct lockd_net *ln = net_generic(net, lockd_net_id); 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci spin_lock(&nsm_lock); 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv); 3888c2ecf20Sopenharmony_ci if (unlikely(cached == NULL)) { 3898c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 3908c2ecf20Sopenharmony_ci dprintk("lockd: never saw rebooted peer '%.*s' before\n", 3918c2ecf20Sopenharmony_ci info->len, info->mon); 3928c2ecf20Sopenharmony_ci return cached; 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci refcount_inc(&cached->sm_count); 3968c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 3998c2ecf20Sopenharmony_ci cached->sm_name, cached->sm_addrbuf, 4008c2ecf20Sopenharmony_ci refcount_read(&cached->sm_count)); 4018c2ecf20Sopenharmony_ci return cached; 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci/** 4058c2ecf20Sopenharmony_ci * nsm_release - Release an NSM handle 4068c2ecf20Sopenharmony_ci * @nsm: pointer to handle to be released 4078c2ecf20Sopenharmony_ci * 4088c2ecf20Sopenharmony_ci */ 4098c2ecf20Sopenharmony_civoid nsm_release(struct nsm_handle *nsm) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci if (refcount_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 4128c2ecf20Sopenharmony_ci list_del(&nsm->sm_link); 4138c2ecf20Sopenharmony_ci spin_unlock(&nsm_lock); 4148c2ecf20Sopenharmony_ci dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4158c2ecf20Sopenharmony_ci nsm->sm_name, nsm->sm_addrbuf); 4168c2ecf20Sopenharmony_ci kfree(nsm); 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci/* 4218c2ecf20Sopenharmony_ci * XDR functions for NSM. 4228c2ecf20Sopenharmony_ci * 4238c2ecf20Sopenharmony_ci * See https://www.opengroup.org/ for details on the Network 4248c2ecf20Sopenharmony_ci * Status Monitor wire protocol. 4258c2ecf20Sopenharmony_ci */ 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_cistatic void encode_nsm_string(struct xdr_stream *xdr, const char *string) 4288c2ecf20Sopenharmony_ci{ 4298c2ecf20Sopenharmony_ci const u32 len = strlen(string); 4308c2ecf20Sopenharmony_ci __be32 *p; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci p = xdr_reserve_space(xdr, 4 + len); 4338c2ecf20Sopenharmony_ci xdr_encode_opaque(p, string, len); 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci/* 4378c2ecf20Sopenharmony_ci * "mon_name" specifies the host to be monitored. 4388c2ecf20Sopenharmony_ci */ 4398c2ecf20Sopenharmony_cistatic void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci encode_nsm_string(xdr, argp->mon_name); 4428c2ecf20Sopenharmony_ci} 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci/* 4458c2ecf20Sopenharmony_ci * The "my_id" argument specifies the hostname and RPC procedure 4468c2ecf20Sopenharmony_ci * to be called when the status manager receives notification 4478c2ecf20Sopenharmony_ci * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 4488c2ecf20Sopenharmony_ci * has changed. 4498c2ecf20Sopenharmony_ci */ 4508c2ecf20Sopenharmony_cistatic void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 4518c2ecf20Sopenharmony_ci{ 4528c2ecf20Sopenharmony_ci __be32 *p; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci encode_nsm_string(xdr, argp->nodename); 4558c2ecf20Sopenharmony_ci p = xdr_reserve_space(xdr, 4 + 4 + 4); 4568c2ecf20Sopenharmony_ci *p++ = cpu_to_be32(argp->prog); 4578c2ecf20Sopenharmony_ci *p++ = cpu_to_be32(argp->vers); 4588c2ecf20Sopenharmony_ci *p = cpu_to_be32(argp->proc); 4598c2ecf20Sopenharmony_ci} 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci/* 4628c2ecf20Sopenharmony_ci * The "mon_id" argument specifies the non-private arguments 4638c2ecf20Sopenharmony_ci * of an NSMPROC_MON or NSMPROC_UNMON call. 4648c2ecf20Sopenharmony_ci */ 4658c2ecf20Sopenharmony_cistatic void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 4668c2ecf20Sopenharmony_ci{ 4678c2ecf20Sopenharmony_ci encode_mon_name(xdr, argp); 4688c2ecf20Sopenharmony_ci encode_my_id(xdr, argp); 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci/* 4728c2ecf20Sopenharmony_ci * The "priv" argument may contain private information required 4738c2ecf20Sopenharmony_ci * by the NSMPROC_MON call. This information will be supplied in the 4748c2ecf20Sopenharmony_ci * NLMPROC_SM_NOTIFY call. 4758c2ecf20Sopenharmony_ci */ 4768c2ecf20Sopenharmony_cistatic void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 4778c2ecf20Sopenharmony_ci{ 4788c2ecf20Sopenharmony_ci __be32 *p; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 4818c2ecf20Sopenharmony_ci xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr, 4858c2ecf20Sopenharmony_ci const void *argp) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci encode_mon_id(xdr, argp); 4888c2ecf20Sopenharmony_ci encode_priv(xdr, argp); 4898c2ecf20Sopenharmony_ci} 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_cistatic void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr, 4928c2ecf20Sopenharmony_ci const void *argp) 4938c2ecf20Sopenharmony_ci{ 4948c2ecf20Sopenharmony_ci encode_mon_id(xdr, argp); 4958c2ecf20Sopenharmony_ci} 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp, 4988c2ecf20Sopenharmony_ci struct xdr_stream *xdr, 4998c2ecf20Sopenharmony_ci void *data) 5008c2ecf20Sopenharmony_ci{ 5018c2ecf20Sopenharmony_ci struct nsm_res *resp = data; 5028c2ecf20Sopenharmony_ci __be32 *p; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci p = xdr_inline_decode(xdr, 4 + 4); 5058c2ecf20Sopenharmony_ci if (unlikely(p == NULL)) 5068c2ecf20Sopenharmony_ci return -EIO; 5078c2ecf20Sopenharmony_ci resp->status = be32_to_cpup(p++); 5088c2ecf20Sopenharmony_ci resp->state = be32_to_cpup(p); 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci dprintk("lockd: %s status %d state %d\n", 5118c2ecf20Sopenharmony_ci __func__, resp->status, resp->state); 5128c2ecf20Sopenharmony_ci return 0; 5138c2ecf20Sopenharmony_ci} 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_cistatic int nsm_xdr_dec_stat(struct rpc_rqst *rqstp, 5168c2ecf20Sopenharmony_ci struct xdr_stream *xdr, 5178c2ecf20Sopenharmony_ci void *data) 5188c2ecf20Sopenharmony_ci{ 5198c2ecf20Sopenharmony_ci struct nsm_res *resp = data; 5208c2ecf20Sopenharmony_ci __be32 *p; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci p = xdr_inline_decode(xdr, 4); 5238c2ecf20Sopenharmony_ci if (unlikely(p == NULL)) 5248c2ecf20Sopenharmony_ci return -EIO; 5258c2ecf20Sopenharmony_ci resp->state = be32_to_cpup(p); 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci dprintk("lockd: %s state %d\n", __func__, resp->state); 5288c2ecf20Sopenharmony_ci return 0; 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5328c2ecf20Sopenharmony_ci#define SM_my_id_sz (SM_my_name_sz+3) 5338c2ecf20Sopenharmony_ci#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5348c2ecf20Sopenharmony_ci#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5358c2ecf20Sopenharmony_ci#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5368c2ecf20Sopenharmony_ci#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5378c2ecf20Sopenharmony_ci#define SM_monres_sz 2 5388c2ecf20Sopenharmony_ci#define SM_unmonres_sz 1 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic const struct rpc_procinfo nsm_procedures[] = { 5418c2ecf20Sopenharmony_ci[NSMPROC_MON] = { 5428c2ecf20Sopenharmony_ci .p_proc = NSMPROC_MON, 5438c2ecf20Sopenharmony_ci .p_encode = nsm_xdr_enc_mon, 5448c2ecf20Sopenharmony_ci .p_decode = nsm_xdr_dec_stat_res, 5458c2ecf20Sopenharmony_ci .p_arglen = SM_mon_sz, 5468c2ecf20Sopenharmony_ci .p_replen = SM_monres_sz, 5478c2ecf20Sopenharmony_ci .p_statidx = NSMPROC_MON, 5488c2ecf20Sopenharmony_ci .p_name = "MONITOR", 5498c2ecf20Sopenharmony_ci }, 5508c2ecf20Sopenharmony_ci[NSMPROC_UNMON] = { 5518c2ecf20Sopenharmony_ci .p_proc = NSMPROC_UNMON, 5528c2ecf20Sopenharmony_ci .p_encode = nsm_xdr_enc_unmon, 5538c2ecf20Sopenharmony_ci .p_decode = nsm_xdr_dec_stat, 5548c2ecf20Sopenharmony_ci .p_arglen = SM_mon_id_sz, 5558c2ecf20Sopenharmony_ci .p_replen = SM_unmonres_sz, 5568c2ecf20Sopenharmony_ci .p_statidx = NSMPROC_UNMON, 5578c2ecf20Sopenharmony_ci .p_name = "UNMONITOR", 5588c2ecf20Sopenharmony_ci }, 5598c2ecf20Sopenharmony_ci}; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_cistatic unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)]; 5628c2ecf20Sopenharmony_cistatic const struct rpc_version nsm_version1 = { 5638c2ecf20Sopenharmony_ci .number = 1, 5648c2ecf20Sopenharmony_ci .nrprocs = ARRAY_SIZE(nsm_procedures), 5658c2ecf20Sopenharmony_ci .procs = nsm_procedures, 5668c2ecf20Sopenharmony_ci .counts = nsm_version1_counts, 5678c2ecf20Sopenharmony_ci}; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_cistatic const struct rpc_version *nsm_version[] = { 5708c2ecf20Sopenharmony_ci [1] = &nsm_version1, 5718c2ecf20Sopenharmony_ci}; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_cistatic struct rpc_stat nsm_stats; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_cistatic const struct rpc_program nsm_program = { 5768c2ecf20Sopenharmony_ci .name = "statd", 5778c2ecf20Sopenharmony_ci .number = NSM_PROGRAM, 5788c2ecf20Sopenharmony_ci .nrvers = ARRAY_SIZE(nsm_version), 5798c2ecf20Sopenharmony_ci .version = nsm_version, 5808c2ecf20Sopenharmony_ci .stats = &nsm_stats 5818c2ecf20Sopenharmony_ci}; 582