mon.c 15 KB

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