mon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 <linux/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. if (!hostname)
  242. return NULL;
  243. new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
  244. if (unlikely(new == NULL))
  245. return NULL;
  246. refcount_set(&new->sm_count, 1);
  247. new->sm_name = (char *)(new + 1);
  248. memcpy(nsm_addr(new), sap, salen);
  249. new->sm_addrlen = salen;
  250. nsm_init_private(new);
  251. if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
  252. sizeof(new->sm_addrbuf)) == 0)
  253. (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
  254. "unsupported address family");
  255. memcpy(new->sm_name, hostname, hostname_len);
  256. new->sm_name[hostname_len] = '\0';
  257. return new;
  258. }
  259. /**
  260. * nsm_get_handle - Find or create a cached nsm_handle
  261. * @net: network namespace
  262. * @sap: pointer to socket address of handle to find
  263. * @salen: length of socket address
  264. * @hostname: pointer to C string containing hostname to find
  265. * @hostname_len: length of C string
  266. *
  267. * Behavior is modulated by the global nsm_use_hostnames variable.
  268. *
  269. * Returns a cached nsm_handle after bumping its ref count, or
  270. * returns a fresh nsm_handle if a handle that matches @sap and/or
  271. * @hostname cannot be found in the handle cache. Returns NULL if
  272. * an error occurs.
  273. */
  274. struct nsm_handle *nsm_get_handle(const struct net *net,
  275. const struct sockaddr *sap,
  276. const size_t salen, const char *hostname,
  277. const size_t hostname_len)
  278. {
  279. struct nsm_handle *cached, *new = NULL;
  280. struct lockd_net *ln = net_generic(net, lockd_net_id);
  281. if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
  282. if (printk_ratelimit()) {
  283. printk(KERN_WARNING "Invalid hostname \"%.*s\" "
  284. "in NFS lock request\n",
  285. (int)hostname_len, hostname);
  286. }
  287. return NULL;
  288. }
  289. retry:
  290. spin_lock(&nsm_lock);
  291. if (nsm_use_hostnames && hostname != NULL)
  292. cached = nsm_lookup_hostname(&ln->nsm_handles,
  293. hostname, hostname_len);
  294. else
  295. cached = nsm_lookup_addr(&ln->nsm_handles, sap);
  296. if (cached != NULL) {
  297. refcount_inc(&cached->sm_count);
  298. spin_unlock(&nsm_lock);
  299. kfree(new);
  300. dprintk("lockd: found nsm_handle for %s (%s), "
  301. "cnt %d\n", cached->sm_name,
  302. cached->sm_addrbuf,
  303. refcount_read(&cached->sm_count));
  304. return cached;
  305. }
  306. if (new != NULL) {
  307. list_add(&new->sm_link, &ln->nsm_handles);
  308. spin_unlock(&nsm_lock);
  309. dprintk("lockd: created nsm_handle for %s (%s)\n",
  310. new->sm_name, new->sm_addrbuf);
  311. return new;
  312. }
  313. spin_unlock(&nsm_lock);
  314. new = nsm_create_handle(sap, salen, hostname, hostname_len);
  315. if (unlikely(new == NULL))
  316. return NULL;
  317. goto retry;
  318. }
  319. /**
  320. * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
  321. * @net: network namespace
  322. * @info: pointer to NLMPROC_SM_NOTIFY arguments
  323. *
  324. * Returns a matching nsm_handle if found in the nsm cache. The returned
  325. * nsm_handle's reference count is bumped. Otherwise returns NULL if some
  326. * error occurred.
  327. */
  328. struct nsm_handle *nsm_reboot_lookup(const struct net *net,
  329. const struct nlm_reboot *info)
  330. {
  331. struct nsm_handle *cached;
  332. struct lockd_net *ln = net_generic(net, lockd_net_id);
  333. spin_lock(&nsm_lock);
  334. cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv);
  335. if (unlikely(cached == NULL)) {
  336. spin_unlock(&nsm_lock);
  337. dprintk("lockd: never saw rebooted peer '%.*s' before\n",
  338. info->len, info->mon);
  339. return cached;
  340. }
  341. refcount_inc(&cached->sm_count);
  342. spin_unlock(&nsm_lock);
  343. dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
  344. cached->sm_name, cached->sm_addrbuf,
  345. refcount_read(&cached->sm_count));
  346. return cached;
  347. }
  348. /**
  349. * nsm_release - Release an NSM handle
  350. * @nsm: pointer to handle to be released
  351. *
  352. */
  353. void nsm_release(struct nsm_handle *nsm)
  354. {
  355. if (refcount_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
  356. list_del(&nsm->sm_link);
  357. spin_unlock(&nsm_lock);
  358. dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
  359. nsm->sm_name, nsm->sm_addrbuf);
  360. kfree(nsm);
  361. }
  362. }
  363. /*
  364. * XDR functions for NSM.
  365. *
  366. * See https://www.opengroup.org/ for details on the Network
  367. * Status Monitor wire protocol.
  368. */
  369. static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
  370. {
  371. const u32 len = strlen(string);
  372. __be32 *p;
  373. p = xdr_reserve_space(xdr, 4 + len);
  374. xdr_encode_opaque(p, string, len);
  375. }
  376. /*
  377. * "mon_name" specifies the host to be monitored.
  378. */
  379. static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
  380. {
  381. encode_nsm_string(xdr, argp->mon_name);
  382. }
  383. /*
  384. * The "my_id" argument specifies the hostname and RPC procedure
  385. * to be called when the status manager receives notification
  386. * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
  387. * has changed.
  388. */
  389. static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
  390. {
  391. __be32 *p;
  392. encode_nsm_string(xdr, argp->nodename);
  393. p = xdr_reserve_space(xdr, 4 + 4 + 4);
  394. *p++ = cpu_to_be32(argp->prog);
  395. *p++ = cpu_to_be32(argp->vers);
  396. *p = cpu_to_be32(argp->proc);
  397. }
  398. /*
  399. * The "mon_id" argument specifies the non-private arguments
  400. * of an NSMPROC_MON or NSMPROC_UNMON call.
  401. */
  402. static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
  403. {
  404. encode_mon_name(xdr, argp);
  405. encode_my_id(xdr, argp);
  406. }
  407. /*
  408. * The "priv" argument may contain private information required
  409. * by the NSMPROC_MON call. This information will be supplied in the
  410. * NLMPROC_SM_NOTIFY call.
  411. */
  412. static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
  413. {
  414. __be32 *p;
  415. p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
  416. xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
  417. }
  418. static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
  419. const void *argp)
  420. {
  421. encode_mon_id(xdr, argp);
  422. encode_priv(xdr, argp);
  423. }
  424. static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
  425. const void *argp)
  426. {
  427. encode_mon_id(xdr, argp);
  428. }
  429. static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
  430. struct xdr_stream *xdr,
  431. void *data)
  432. {
  433. struct nsm_res *resp = data;
  434. __be32 *p;
  435. p = xdr_inline_decode(xdr, 4 + 4);
  436. if (unlikely(p == NULL))
  437. return -EIO;
  438. resp->status = be32_to_cpup(p++);
  439. resp->state = be32_to_cpup(p);
  440. dprintk("lockd: %s status %d state %d\n",
  441. __func__, resp->status, resp->state);
  442. return 0;
  443. }
  444. static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
  445. struct xdr_stream *xdr,
  446. void *data)
  447. {
  448. struct nsm_res *resp = data;
  449. __be32 *p;
  450. p = xdr_inline_decode(xdr, 4);
  451. if (unlikely(p == NULL))
  452. return -EIO;
  453. resp->state = be32_to_cpup(p);
  454. dprintk("lockd: %s state %d\n", __func__, resp->state);
  455. return 0;
  456. }
  457. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  458. #define SM_my_id_sz (SM_my_name_sz+3)
  459. #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  460. #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
  461. #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
  462. #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
  463. #define SM_monres_sz 2
  464. #define SM_unmonres_sz 1
  465. static const struct rpc_procinfo nsm_procedures[] = {
  466. [NSMPROC_MON] = {
  467. .p_proc = NSMPROC_MON,
  468. .p_encode = nsm_xdr_enc_mon,
  469. .p_decode = nsm_xdr_dec_stat_res,
  470. .p_arglen = SM_mon_sz,
  471. .p_replen = SM_monres_sz,
  472. .p_statidx = NSMPROC_MON,
  473. .p_name = "MONITOR",
  474. },
  475. [NSMPROC_UNMON] = {
  476. .p_proc = NSMPROC_UNMON,
  477. .p_encode = nsm_xdr_enc_unmon,
  478. .p_decode = nsm_xdr_dec_stat,
  479. .p_arglen = SM_mon_id_sz,
  480. .p_replen = SM_unmonres_sz,
  481. .p_statidx = NSMPROC_UNMON,
  482. .p_name = "UNMONITOR",
  483. },
  484. };
  485. static unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)];
  486. static const struct rpc_version nsm_version1 = {
  487. .number = 1,
  488. .nrprocs = ARRAY_SIZE(nsm_procedures),
  489. .procs = nsm_procedures,
  490. .counts = nsm_version1_counts,
  491. };
  492. static const struct rpc_version *nsm_version[] = {
  493. [1] = &nsm_version1,
  494. };
  495. static struct rpc_stat nsm_stats;
  496. static const struct rpc_program nsm_program = {
  497. .name = "statd",
  498. .number = NSM_PROGRAM,
  499. .nrvers = ARRAY_SIZE(nsm_version),
  500. .version = nsm_version,
  501. .stats = &nsm_stats
  502. };