mon.c 15 KB

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