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