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