mon.c 15 KB

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