mon.c 15 KB

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