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. #define NLMDBG_FACILITY NLMDBG_MONITOR
  17. #define NSM_PROGRAM 100024
  18. #define NSM_VERSION 1
  19. enum {
  20. NSMPROC_NULL,
  21. NSMPROC_STAT,
  22. NSMPROC_MON,
  23. NSMPROC_UNMON,
  24. NSMPROC_UNMON_ALL,
  25. NSMPROC_SIMU_CRASH,
  26. NSMPROC_NOTIFY,
  27. };
  28. struct nsm_args {
  29. struct nsm_private *priv;
  30. u32 prog; /* RPC callback info */
  31. u32 vers;
  32. u32 proc;
  33. char *mon_name;
  34. };
  35. struct nsm_res {
  36. u32 status;
  37. u32 state;
  38. };
  39. static struct rpc_clnt * nsm_create(void);
  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 nsm_local_state;
  47. static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
  48. {
  49. return (struct sockaddr *)&nsm->sm_addr;
  50. }
  51. static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
  52. const size_t len)
  53. {
  54. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  55. snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
  56. }
  57. static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
  58. const size_t len)
  59. {
  60. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  61. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  62. snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
  63. else if (sin6->sin6_scope_id != 0)
  64. snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
  65. sin6->sin6_scope_id);
  66. else
  67. snprintf(buf, len, "%pI6", &sin6->sin6_addr);
  68. }
  69. static void nsm_display_address(const struct sockaddr *sap,
  70. char *buf, const size_t len)
  71. {
  72. switch (sap->sa_family) {
  73. case AF_INET:
  74. nsm_display_ipv4_address(sap, buf, len);
  75. break;
  76. case AF_INET6:
  77. nsm_display_ipv6_address(sap, buf, len);
  78. break;
  79. default:
  80. snprintf(buf, len, "unsupported address family");
  81. break;
  82. }
  83. }
  84. /*
  85. * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
  86. */
  87. static int
  88. nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
  89. {
  90. struct rpc_clnt *clnt;
  91. int status;
  92. struct nsm_args args = {
  93. .priv = &nsm->sm_priv,
  94. .prog = NLM_PROGRAM,
  95. .vers = 3,
  96. .proc = NLMPROC_NSM_NOTIFY,
  97. .mon_name = nsm->sm_mon_name,
  98. };
  99. struct rpc_message msg = {
  100. .rpc_argp = &args,
  101. .rpc_resp = res,
  102. };
  103. clnt = nsm_create();
  104. if (IS_ERR(clnt)) {
  105. status = PTR_ERR(clnt);
  106. dprintk("lockd: failed to create NSM upcall transport, "
  107. "status=%d\n", status);
  108. goto out;
  109. }
  110. memset(res, 0, sizeof(*res));
  111. msg.rpc_proc = &clnt->cl_procinfo[proc];
  112. status = rpc_call_sync(clnt, &msg, 0);
  113. if (status < 0)
  114. dprintk("lockd: NSM upcall RPC failed, status=%d\n",
  115. status);
  116. else
  117. status = 0;
  118. rpc_shutdown_client(clnt);
  119. out:
  120. return status;
  121. }
  122. /**
  123. * nsm_monitor - Notify a peer in case we reboot
  124. * @host: pointer to nlm_host of peer to notify
  125. *
  126. * If this peer is not already monitored, this function sends an
  127. * upcall to the local rpc.statd to record the name/address of
  128. * the peer to notify in case we reboot.
  129. *
  130. * Returns zero if the peer is monitored by the local rpc.statd;
  131. * otherwise a negative errno value is returned.
  132. */
  133. int nsm_monitor(const struct nlm_host *host)
  134. {
  135. struct nsm_handle *nsm = host->h_nsmhandle;
  136. struct nsm_res res;
  137. int status;
  138. dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
  139. if (nsm->sm_monitored)
  140. return 0;
  141. /*
  142. * Choose whether to record the caller_name or IP address of
  143. * this peer in the local rpc.statd's database.
  144. */
  145. nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
  146. status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
  147. if (res.status != 0)
  148. status = -EIO;
  149. if (status < 0)
  150. printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
  151. else
  152. nsm->sm_monitored = 1;
  153. return status;
  154. }
  155. /**
  156. * nsm_unmonitor - Unregister peer notification
  157. * @host: pointer to nlm_host of peer to stop monitoring
  158. *
  159. * If this peer is monitored, this function sends an upcall to
  160. * tell the local rpc.statd not to send this peer a notification
  161. * when we reboot.
  162. */
  163. void nsm_unmonitor(const struct nlm_host *host)
  164. {
  165. struct nsm_handle *nsm = host->h_nsmhandle;
  166. struct nsm_res res;
  167. int status;
  168. if (atomic_read(&nsm->sm_count) == 1
  169. && nsm->sm_monitored && !nsm->sm_sticky) {
  170. dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
  171. status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
  172. if (res.status != 0)
  173. status = -EIO;
  174. if (status < 0)
  175. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
  176. nsm->sm_name);
  177. else
  178. nsm->sm_monitored = 0;
  179. }
  180. }
  181. static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
  182. const size_t len)
  183. {
  184. struct nsm_handle *nsm;
  185. list_for_each_entry(nsm, &nsm_handles, sm_link)
  186. if (strlen(nsm->sm_name) == len &&
  187. memcmp(nsm->sm_name, hostname, len) == 0)
  188. return nsm;
  189. return NULL;
  190. }
  191. static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
  192. {
  193. struct nsm_handle *nsm;
  194. list_for_each_entry(nsm, &nsm_handles, sm_link)
  195. if (nlm_cmp_addr(nsm_addr(nsm), sap))
  196. return nsm;
  197. return NULL;
  198. }
  199. static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
  200. {
  201. struct nsm_handle *nsm;
  202. list_for_each_entry(nsm, &nsm_handles, sm_link)
  203. if (memcmp(nsm->sm_priv.data, priv->data,
  204. sizeof(priv->data)) == 0)
  205. return nsm;
  206. return NULL;
  207. }
  208. /*
  209. * Construct a unique cookie to match this nsm_handle to this monitored
  210. * host. It is passed to the local rpc.statd via NSMPROC_MON, and
  211. * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
  212. * requests.
  213. *
  214. * The NSM protocol requires that these cookies be unique while the
  215. * system is running. We prefer a stronger requirement of making them
  216. * unique across reboots. If user space bugs cause a stale cookie to
  217. * be sent to the kernel, it could cause the wrong host to lose its
  218. * lock state if cookies were not unique across reboots.
  219. *
  220. * The cookies are exposed only to local user space via loopback. They
  221. * do not appear on the physical network. If we want greater security
  222. * for some reason, nsm_init_private() could perform a one-way hash to
  223. * obscure the contents of the cookie.
  224. */
  225. static void nsm_init_private(struct nsm_handle *nsm)
  226. {
  227. u64 *p = (u64 *)&nsm->sm_priv.data;
  228. struct timespec ts;
  229. ktime_get_ts(&ts);
  230. *p++ = timespec_to_ns(&ts);
  231. *p = (unsigned long)nsm;
  232. }
  233. static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
  234. const size_t salen,
  235. const char *hostname,
  236. const size_t hostname_len)
  237. {
  238. struct nsm_handle *new;
  239. new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
  240. if (unlikely(new == NULL))
  241. return NULL;
  242. atomic_set(&new->sm_count, 1);
  243. new->sm_name = (char *)(new + 1);
  244. memcpy(nsm_addr(new), sap, salen);
  245. new->sm_addrlen = salen;
  246. nsm_init_private(new);
  247. nsm_display_address((const struct sockaddr *)&new->sm_addr,
  248. new->sm_addrbuf, sizeof(new->sm_addrbuf));
  249. memcpy(new->sm_name, hostname, hostname_len);
  250. new->sm_name[hostname_len] = '\0';
  251. return new;
  252. }
  253. /**
  254. * nsm_get_handle - Find or create a cached nsm_handle
  255. * @sap: pointer to socket address of handle to find
  256. * @salen: length of socket address
  257. * @hostname: pointer to C string containing hostname to find
  258. * @hostname_len: length of C string
  259. *
  260. * Behavior is modulated by the global nsm_use_hostnames variable.
  261. *
  262. * Returns a cached nsm_handle after bumping its ref count, or
  263. * returns a fresh nsm_handle if a handle that matches @sap and/or
  264. * @hostname cannot be found in the handle cache. Returns NULL if
  265. * an error occurs.
  266. */
  267. struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
  268. const size_t salen, const char *hostname,
  269. const size_t hostname_len)
  270. {
  271. struct nsm_handle *cached, *new = NULL;
  272. if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
  273. if (printk_ratelimit()) {
  274. printk(KERN_WARNING "Invalid hostname \"%.*s\" "
  275. "in NFS lock request\n",
  276. (int)hostname_len, hostname);
  277. }
  278. return NULL;
  279. }
  280. retry:
  281. spin_lock(&nsm_lock);
  282. if (nsm_use_hostnames && hostname != NULL)
  283. cached = nsm_lookup_hostname(hostname, hostname_len);
  284. else
  285. cached = nsm_lookup_addr(sap);
  286. if (cached != NULL) {
  287. atomic_inc(&cached->sm_count);
  288. spin_unlock(&nsm_lock);
  289. kfree(new);
  290. dprintk("lockd: found nsm_handle for %s (%s), "
  291. "cnt %d\n", cached->sm_name,
  292. cached->sm_addrbuf,
  293. atomic_read(&cached->sm_count));
  294. return cached;
  295. }
  296. if (new != NULL) {
  297. list_add(&new->sm_link, &nsm_handles);
  298. spin_unlock(&nsm_lock);
  299. dprintk("lockd: created nsm_handle for %s (%s)\n",
  300. new->sm_name, new->sm_addrbuf);
  301. return new;
  302. }
  303. spin_unlock(&nsm_lock);
  304. new = nsm_create_handle(sap, salen, hostname, hostname_len);
  305. if (unlikely(new == NULL))
  306. return NULL;
  307. goto retry;
  308. }
  309. /**
  310. * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
  311. * @info: pointer to NLMPROC_SM_NOTIFY arguments
  312. *
  313. * Returns a matching nsm_handle if found in the nsm cache; the returned
  314. * nsm_handle's reference count is bumped and sm_monitored is cleared.
  315. * Otherwise returns NULL if some error occurred.
  316. */
  317. struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
  318. {
  319. struct nsm_handle *cached;
  320. spin_lock(&nsm_lock);
  321. cached = nsm_lookup_priv(&info->priv);
  322. if (unlikely(cached == NULL)) {
  323. spin_unlock(&nsm_lock);
  324. dprintk("lockd: never saw rebooted peer '%.*s' before\n",
  325. info->len, info->mon);
  326. return cached;
  327. }
  328. atomic_inc(&cached->sm_count);
  329. spin_unlock(&nsm_lock);
  330. /*
  331. * During subsequent lock activity, force a fresh
  332. * notification to be set up for this host.
  333. */
  334. cached->sm_monitored = 0;
  335. dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
  336. cached->sm_name, cached->sm_addrbuf,
  337. atomic_read(&cached->sm_count));
  338. return cached;
  339. }
  340. /**
  341. * nsm_release - Release an NSM handle
  342. * @nsm: pointer to handle to be released
  343. *
  344. */
  345. void nsm_release(struct nsm_handle *nsm)
  346. {
  347. if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
  348. list_del(&nsm->sm_link);
  349. spin_unlock(&nsm_lock);
  350. dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
  351. nsm->sm_name, nsm->sm_addrbuf);
  352. kfree(nsm);
  353. }
  354. }
  355. /*
  356. * Create NSM client for the local host
  357. */
  358. static struct rpc_clnt *
  359. nsm_create(void)
  360. {
  361. struct sockaddr_in sin = {
  362. .sin_family = AF_INET,
  363. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  364. .sin_port = 0,
  365. };
  366. struct rpc_create_args args = {
  367. .protocol = XPRT_TRANSPORT_UDP,
  368. .address = (struct sockaddr *)&sin,
  369. .addrsize = sizeof(sin),
  370. .servername = "localhost",
  371. .program = &nsm_program,
  372. .version = NSM_VERSION,
  373. .authflavor = RPC_AUTH_NULL,
  374. };
  375. return rpc_create(&args);
  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. };