mon.c 14 KB

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