mon.c 15 KB

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