mon.c 14 KB

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