mon.c 14 KB

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