3 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed for the FreeBSD project
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: lock_proc.c,v 1.7 2000/10/11 20:23:56 is Exp $
33 * $FreeBSD: src/usr.sbin/rpc.lockd/lock_proc.c,v 1.1 2001/03/19 12:50:09 alfred Exp $
36 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
47 #include <netconfig.h>
50 #include <rpcsvc/sm_inter.h>
53 #include <rpcsvc/nlm_prot.h>
54 #include "lockd_lock.h"
57 #define CLIENT_CACHE_SIZE 64 /* No. of client sockets cached */
58 #define CLIENT_CACHE_LIFETIME 120 /* In seconds */
60 #define getrpcaddr(rqstp) (struct sockaddr *)(svc_getrpccaller((rqstp)->rq_xprt)->buf)
62 static void log_from_addr(const char *, struct svc_req *);
63 static int addrcmp(struct sockaddr *, struct sockaddr *);
65 /* log_from_addr ----------------------------------------------------------- */
67 * Purpose: Log name of function called and source address
69 * Notes: Extracts the source address from the transport handle
70 * passed in as part of the called procedure specification
73 log_from_addr(const char *fun_name, struct svc_req *req)
75 struct sockaddr *addr;
76 char hostname_buf[NI_MAXHOST];
78 addr = svc_getrpccaller(req->rq_xprt)->buf;
79 if (getnameinfo(addr , addr->sa_len, hostname_buf, sizeof hostname_buf,
83 syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
86 /* get_client -------------------------------------------------------------- */
88 * Purpose: Get a CLIENT* for making RPC calls to lockd on given host
89 * Returns: CLIENT* pointer, from clnt_udp_create, or NULL if error
90 * Notes: Creating a CLIENT* is quite expensive, involving a
91 * conversation with the remote portmapper to get the
92 * port number. Since a given client is quite likely
93 * to make several locking requests in succession, it is
94 * desirable to cache the created CLIENT*.
96 * Since we are using UDP rather than TCP, there is no cost
97 * to the remote system in keeping these cached indefinitely.
98 * Unfortunately there is a snag: if the remote system
99 * reboots, the cached portmapper results will be invalid,
100 * and we will never detect this since all of the xxx_msg()
101 * calls return no result - we just fire off a udp packet
102 * and hope for the best.
104 * We solve this by discarding cached values after two
105 * minutes, regardless of whether they have been used
106 * in the meanwhile (since a bad one might have been used
107 * plenty of times, as the host keeps retrying the request
108 * and we keep sending the reply back to the wrong port).
110 * Given that the entries will always expire in the order
111 * that they were created, there is no point in a LRU
112 * algorithm for when the cache gets full - entries are
113 * always re-used in sequence.
115 static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
116 static long clnt_cache_time[CLIENT_CACHE_SIZE]; /* time entry created */
117 static struct sockaddr_storage clnt_cache_addr[CLIENT_CACHE_SIZE];
118 static rpcvers_t clnt_cache_vers[CLIENT_CACHE_SIZE];
119 static int clnt_cache_next_to_use = 0;
122 addrcmp(struct sockaddr *sa1, struct sockaddr *sa2)
127 if (sa1->sa_family != sa2->sa_family)
130 switch (sa1->sa_family) {
132 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
133 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
137 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
138 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
145 return memcmp(p1, p2, len);
149 get_client(struct sockaddr *host_addr, rpcvers_t vers)
152 struct timeval retry_time, time_now;
155 struct netconfig *nconf;
156 char host[NI_MAXHOST];
160 gettimeofday(&time_now, NULL);
163 * Search for the given client in the cache, zapping any expired
164 * entries that we happen to notice in passing.
166 for (i = 0; i < CLIENT_CACHE_SIZE; i++) {
167 client = clnt_cache_ptr[i];
168 if (client && ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME)
169 < time_now.tv_sec)) {
170 /* Cache entry has expired. */
172 syslog(LOG_DEBUG, "Expired CLIENT* in cache");
173 clnt_cache_time[i] = 0L;
174 clnt_destroy(client);
175 clnt_cache_ptr[i] = NULL;
178 if (client && !addrcmp((struct sockaddr *)&clnt_cache_addr[i],
179 host_addr) && clnt_cache_vers[i] == vers) {
182 syslog(LOG_DEBUG, "Found CLIENT* in cache");
188 syslog(LOG_DEBUG, "CLIENT* not found in cache, creating");
190 /* Not found in cache. Free the next entry if it is in use. */
191 if (clnt_cache_ptr[clnt_cache_next_to_use]) {
192 clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]);
193 clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
197 * Need a host string for clnt_tp_create. Use NI_NUMERICHOST
198 * to avoid DNS lookups.
200 error = getnameinfo(host_addr, host_addr->sa_len, host, sizeof host,
201 NULL, 0, NI_NUMERICHOST);
203 syslog(LOG_ERR, "unable to get name string for caller: %s",
204 gai_strerror(error));
209 if (host_addr->sa_family == AF_INET6)
214 if (host_addr->sa_family == AF_INET6)
219 nconf = getnetconfigent(netid);
221 syslog(LOG_ERR, "could not get netconfig info for '%s': "
222 "no /etc/netconfig file?", netid);
226 client = clnt_tp_create(host, NLM_PROG, vers, nconf);
227 freenetconfigent(nconf);
230 syslog(LOG_ERR, "%s", clnt_spcreateerror("clntudp_create"));
231 syslog(LOG_ERR, "Unable to return result to %s", host);
235 /* Get the FD of the client, for bindresvport. */
236 clnt_control(client, CLGET_FD, &clnt_fd);
238 /* Regain root privileges, for bindresvport. */
239 old_euid = geteuid();
243 * Bind the client FD to a reserved port.
244 * Some NFS servers reject any NLM request from a non-reserved port.
246 bindresvport(clnt_fd, NULL);
248 /* Drop root privileges again. */
251 /* Success - update the cache entry */
252 clnt_cache_ptr[clnt_cache_next_to_use] = client;
253 memcpy(&clnt_cache_addr[clnt_cache_next_to_use], host_addr,
255 clnt_cache_vers[clnt_cache_next_to_use] = vers;
256 clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
257 if (++clnt_cache_next_to_use >= CLIENT_CACHE_SIZE)
258 clnt_cache_next_to_use = 0;
261 * Disable the default timeout, so we can specify our own in calls
262 * to clnt_call(). (Note that the timeout is a different concept
263 * from the retry period set in clnt_udp_create() above.)
265 retry_time.tv_sec = -1;
266 retry_time.tv_usec = -1;
267 clnt_control(client, CLSET_TIMEOUT, (char *)&retry_time);
270 syslog(LOG_DEBUG, "Created CLIENT* for %s", host);
275 /* transmit_result --------------------------------------------------------- */
277 * Purpose: Transmit result for nlm_xxx_msg pseudo-RPCs
278 * Returns: Nothing - we have no idea if the datagram got there
279 * Notes: clnt_call() will always fail (with timeout) as we are
280 * calling it with timeout 0 as a hack to just issue a datagram
281 * without expecting a result
284 transmit_result(int opcode, nlm_res *result, struct sockaddr *addr)
288 struct timeval timeo;
291 if ((cli = get_client(addr, NLM_VERS)) != NULL) {
292 timeo.tv_sec = 0; /* No timeout - not expecting response */
295 success = clnt_call(cli, opcode, (xdrproc_t)xdr_nlm_res, result,
296 (xdrproc_t)xdr_void, &dummy, timeo);
299 syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
300 success, clnt_sperrno(success));
303 /* transmit4_result --------------------------------------------------------- */
305 * Purpose: Transmit result for nlm4_xxx_msg pseudo-RPCs
306 * Returns: Nothing - we have no idea if the datagram got there
307 * Notes: clnt_call() will always fail (with timeout) as we are
308 * calling it with timeout 0 as a hack to just issue a datagram
309 * without expecting a result
312 transmit4_result(int opcode, nlm4_res *result, struct sockaddr *addr)
316 struct timeval timeo;
319 if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
320 timeo.tv_sec = 0; /* No timeout - not expecting response */
323 success = clnt_call(cli, opcode,
324 (xdrproc_t)xdr_nlm4_res, result,
325 (xdrproc_t)xdr_void, &dummy, timeo);
328 syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
329 success, clnt_sperrno(success));
334 * converts a struct nlm_lock to struct nlm4_lock
336 static void nlmtonlm4(struct nlm_lock *, struct nlm4_lock *);
338 nlmtonlm4(struct nlm_lock *arg, struct nlm4_lock *arg4)
340 arg4->caller_name = arg->caller_name;
343 arg4->svid = arg->svid;
344 arg4->l_offset = arg->l_offset;
345 arg4->l_len = arg->l_len;
347 /* ------------------------------------------------------------------------- */
349 * Functions for Unix<->Unix locking (ie. monitored locking, with rpc.statd
350 * involved to ensure reclaim of locks after a crash of the "stateless"
353 * These all come in two flavours - nlm_xxx() and nlm_xxx_msg().
354 * The first are standard RPCs with argument and result.
355 * The nlm_xxx_msg() calls implement exactly the same functions, but
356 * use two pseudo-RPCs (one in each direction). These calls are NOT
357 * standard use of the RPC protocol in that they do not return a result
358 * at all (NB. this is quite different from returning a void result).
359 * The effect of this is to make the nlm_xxx_msg() calls simple unacknowledged
360 * datagrams, requiring higher-level code to perform retries.
362 * Despite the disadvantages of the nlm_xxx_msg() approach (some of which
363 * are documented in the comments to get_client() above), this is the
364 * interface used by all current commercial NFS implementations
365 * [Solaris, SCO, AIX etc.]. This is presumed to be because these allow
366 * implementations to continue using the standard RPC libraries, while
367 * avoiding the block-until-result nature of the library interface.
369 * No client implementations have been identified so far that make use
370 * of the true RPC version (early SunOS releases would be a likely candidate
374 /* nlm_test ---------------------------------------------------------------- */
376 * Purpose: Test whether a specified lock would be granted if requested
377 * Returns: nlm_granted (or error code)
381 nlm_test_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
383 static nlm_testres res;
384 struct nlm4_lock arg4;
385 struct nlm4_holder *holder;
386 nlmtonlm4(&arg->alock, &arg4);
389 log_from_addr("nlm_test", rqstp);
391 holder = testlock(&arg4, 0);
393 * Copy the cookie from the argument into the result. Note that this
394 * is slightly hazardous, as the structure contains a pointer to a
395 * malloc()ed buffer that will get freed by the caller. However, the
396 * main function transmits the result before freeing the argument
397 * so it is in fact safe.
399 res.cookie = arg->cookie;
400 if (holder == NULL) {
401 res.stat.stat = nlm_granted;
403 res.stat.stat = nlm_denied;
404 memcpy(&res.stat.nlm_testrply_u.holder, holder,
405 sizeof(struct nlm_holder));
406 res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
407 res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
413 nlm_test_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
417 struct sockaddr *addr;
420 struct timeval timeo;
421 struct nlm4_lock arg4;
422 struct nlm4_holder *holder;
424 nlmtonlm4(&arg->alock, &arg4);
427 log_from_addr("nlm_test_msg", rqstp);
429 holder = testlock(&arg4, 0);
431 res.cookie = arg->cookie;
432 if (holder == NULL) {
433 res.stat.stat = nlm_granted;
435 res.stat.stat = nlm_denied;
436 memcpy(&res.stat.nlm_testrply_u.holder, holder,
437 sizeof(struct nlm_holder));
438 res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
439 res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
443 * nlm_test has different result type to the other operations, so
444 * can't use transmit_result() in this case
446 addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
447 if ((cli = get_client(addr, NLM_VERS)) != NULL) {
448 timeo.tv_sec = 0; /* No timeout - not expecting response */
451 success = clnt_call(cli, NLM_TEST_RES,
452 (xdrproc_t)xdr_nlm_testres, &res,
453 (xdrproc_t)xdr_void, &dummy, timeo);
456 syslog(LOG_DEBUG, "clnt_call returns %d", success);
461 /* nlm_lock ---------------------------------------------------------------- */
463 * Purposes: Establish a lock
464 * Returns: granted, denied or blocked
465 * Notes: *** grace period support missing
468 nlm_lock_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
471 struct nlm4_lockargs arg4;
472 nlmtonlm4(&arg->alock, &arg4.alock);
473 arg4.cookie = arg->cookie;
474 arg4.block = arg->block;
475 arg4.exclusive = arg->exclusive;
476 arg4.reclaim = arg->reclaim;
477 arg4.state = arg->state;
480 log_from_addr("nlm_lock", rqstp);
482 /* copy cookie from arg to result. See comment in nlm_test_1() */
483 res.cookie = arg->cookie;
485 res.stat.stat = getlock(&arg4, rqstp, LOCK_MON);
490 nlm_lock_msg_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
493 struct nlm4_lockargs arg4;
495 nlmtonlm4(&arg->alock, &arg4.alock);
496 arg4.cookie = arg->cookie;
497 arg4.block = arg->block;
498 arg4.exclusive = arg->exclusive;
499 arg4.reclaim = arg->reclaim;
500 arg4.state = arg->state;
503 log_from_addr("nlm_lock_msg", rqstp);
505 res.cookie = arg->cookie;
506 res.stat.stat = getlock(&arg4, rqstp, LOCK_ASYNC | LOCK_MON);
507 transmit_result(NLM_LOCK_RES, &res, getrpcaddr(rqstp));
512 /* nlm_cancel -------------------------------------------------------------- */
514 * Purpose: Cancel a blocked lock request
515 * Returns: granted or denied
519 nlm_cancel_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
522 struct nlm4_lock arg4;
524 nlmtonlm4(&arg->alock, &arg4);
527 log_from_addr("nlm_cancel", rqstp);
529 /* copy cookie from arg to result. See comment in nlm_test_1() */
530 res.cookie = arg->cookie;
533 * Since at present we never return 'nlm_blocked', there can never be
534 * a lock to cancel, so this call always fails.
536 res.stat.stat = unlock(&arg4, LOCK_CANCEL);
541 nlm_cancel_msg_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
544 struct nlm4_lock arg4;
546 nlmtonlm4(&arg->alock, &arg4);
549 log_from_addr("nlm_cancel_msg", rqstp);
551 res.cookie = arg->cookie;
553 * Since at present we never return 'nlm_blocked', there can never be
554 * a lock to cancel, so this call always fails.
556 res.stat.stat = unlock(&arg4, LOCK_CANCEL);
557 transmit_result(NLM_CANCEL_RES, &res, getrpcaddr(rqstp));
561 /* nlm_unlock -------------------------------------------------------------- */
563 * Purpose: Release an existing lock
564 * Returns: Always granted, unless during grace period
565 * Notes: "no such lock" error condition is ignored, as the
566 * protocol uses unreliable UDP datagrams, and may well
567 * re-try an unlock that has already succeeded.
570 nlm_unlock_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
573 struct nlm4_lock arg4;
575 nlmtonlm4(&arg->alock, &arg4);
578 log_from_addr("nlm_unlock", rqstp);
580 res.stat.stat = unlock(&arg4, 0);
581 res.cookie = arg->cookie;
587 nlm_unlock_msg_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
590 struct nlm4_lock arg4;
592 nlmtonlm4(&arg->alock, &arg4);
595 log_from_addr("nlm_unlock_msg", rqstp);
597 res.stat.stat = unlock(&arg4, 0);
598 res.cookie = arg->cookie;
600 transmit_result(NLM_UNLOCK_RES, &res, getrpcaddr(rqstp));
604 /* ------------------------------------------------------------------------- */
606 * Client-side pseudo-RPCs for results. Note that for the client there
607 * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
608 * version returns the results in the RPC result, and so the client
609 * does not normally receive incoming RPCs.
611 * The exception to this is nlm_granted(), which is genuinely an RPC
612 * call from the server to the client - a 'call-back' in normal procedure
616 /* nlm_granted ------------------------------------------------------------- */
618 * Purpose: Receive notification that formerly blocked lock now granted
619 * Returns: always success ('granted')
623 nlm_granted_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
628 log_from_addr("nlm_granted", rqstp);
630 /* copy cookie from arg to result. See comment in nlm_test_1() */
631 res.cookie = arg->cookie;
633 res.stat.stat = nlm_granted;
638 nlm_granted_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
643 log_from_addr("nlm_granted_msg", rqstp);
645 res.cookie = arg->cookie;
646 res.stat.stat = nlm_granted;
647 transmit_result(NLM_GRANTED_RES, &res,
648 (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
652 /* nlm_test_res ------------------------------------------------------------ */
654 * Purpose: Accept result from earlier nlm_test_msg() call
658 nlm_test_res_1_svc(nlm_testres *arg, struct svc_req *rqstp)
661 log_from_addr("nlm_test_res", rqstp);
665 /* nlm_lock_res ------------------------------------------------------------ */
667 * Purpose: Accept result from earlier nlm_lock_msg() call
671 nlm_lock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
674 log_from_addr("nlm_lock_res", rqstp);
679 /* nlm_cancel_res ---------------------------------------------------------- */
681 * Purpose: Accept result from earlier nlm_cancel_msg() call
685 nlm_cancel_res_1_svc(nlm_res *arg __unused, struct svc_req *rqstp)
688 log_from_addr("nlm_cancel_res", rqstp);
692 /* nlm_unlock_res ---------------------------------------------------------- */
694 * Purpose: Accept result from earlier nlm_unlock_msg() call
698 nlm_unlock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
701 log_from_addr("nlm_unlock_res", rqstp);
705 /* nlm_granted_res --------------------------------------------------------- */
707 * Purpose: Accept result from earlier nlm_granted_msg() call
711 nlm_granted_res_1_svc(nlm_res *arg __unused, struct svc_req *rqstp)
714 log_from_addr("nlm_granted_res", rqstp);
718 /* ------------------------------------------------------------------------- */
720 * Calls for PCNFS locking (aka non-monitored locking, no involvement
723 * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
726 /* nlm_share --------------------------------------------------------------- */
728 * Purpose: Establish a DOS-style lock
729 * Returns: success or failure
730 * Notes: Blocking locks are not supported - client is expected
731 * to retry if required.
734 nlm_share_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
736 static nlm_shareres res;
739 log_from_addr("nlm_share", rqstp);
741 res.cookie = arg->cookie;
742 res.stat = nlm_granted;
743 res.sequence = 1234356; /* X/Open says this field is ignored? */
747 /* nlm_unshare ------------------------------------------------------------ */
749 * Purpose: Release a DOS-style lock
750 * Returns: nlm_granted, unless in grace period
754 nlm_unshare_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
756 static nlm_shareres res;
759 log_from_addr("nlm_unshare", rqstp);
761 res.cookie = arg->cookie;
762 res.stat = nlm_granted;
763 res.sequence = 1234356; /* X/Open says this field is ignored? */
767 /* nlm_nm_lock ------------------------------------------------------------ */
769 * Purpose: non-monitored version of nlm_lock()
770 * Returns: as for nlm_lock()
771 * Notes: These locks are in the same style as the standard nlm_lock,
772 * but the rpc.statd should not be called to establish a
773 * monitor for the client machine, since that machine is
774 * declared not to be running a rpc.statd, and so would not
775 * respond to the statd protocol.
778 nlm_nm_lock_3_svc(nlm_lockargs *arg, struct svc_req *rqstp)
783 log_from_addr("nlm_nm_lock", rqstp);
785 /* copy cookie from arg to result. See comment in nlm_test_1() */
786 res.cookie = arg->cookie;
787 res.stat.stat = nlm_granted;
791 /* nlm_free_all ------------------------------------------------------------ */
793 * Purpose: Release all locks held by a named client
795 * Notes: Potential denial of service security problem here - the
796 * locks to be released are specified by a host name, independent
797 * of the address from which the request has arrived.
798 * Should probably be rejected if the named host has been
799 * using monitored locks.
802 nlm_free_all_3_svc(nlm_notify *arg __unused, struct svc_req *rqstp)
807 log_from_addr("nlm_free_all", rqstp);
811 /* calls for nlm version 4 (NFSv3) */
812 /* nlm_test ---------------------------------------------------------------- */
814 * Purpose: Test whether a specified lock would be granted if requested
815 * Returns: nlm_granted (or error code)
819 nlm4_test_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
821 static nlm4_testres res;
822 struct nlm4_holder *holder;
825 log_from_addr("nlm4_test", rqstp);
827 holder = testlock(&arg->alock, LOCK_V4);
830 * Copy the cookie from the argument into the result. Note that this
831 * is slightly hazardous, as the structure contains a pointer to a
832 * malloc()ed buffer that will get freed by the caller. However, the
833 * main function transmits the result before freeing the argument
834 * so it is in fact safe.
836 res.cookie = arg->cookie;
837 if (holder == NULL) {
838 res.stat.stat = nlm4_granted;
840 res.stat.stat = nlm4_denied;
841 memcpy(&res.stat.nlm4_testrply_u.holder, holder,
842 sizeof(struct nlm4_holder));
848 nlm4_test_msg_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
852 struct sockaddr *addr;
855 struct timeval timeo;
856 struct nlm4_holder *holder;
859 log_from_addr("nlm4_test_msg", rqstp);
861 holder = testlock(&arg->alock, LOCK_V4);
863 res.cookie = arg->cookie;
864 if (holder == NULL) {
865 res.stat.stat = nlm4_granted;
867 res.stat.stat = nlm4_denied;
868 memcpy(&res.stat.nlm4_testrply_u.holder, holder,
869 sizeof(struct nlm4_holder));
873 * nlm_test has different result type to the other operations, so
874 * can't use transmit4_result() in this case
876 addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
877 if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
878 timeo.tv_sec = 0; /* No timeout - not expecting response */
881 success = clnt_call(cli, NLM4_TEST_RES,
882 (xdrproc_t)xdr_nlm4_testres, &res,
883 (xdrproc_t)xdr_void, &dummy, timeo);
886 syslog(LOG_DEBUG, "clnt_call returns %d", success);
891 /* nlm_lock ---------------------------------------------------------------- */
893 * Purposes: Establish a lock
894 * Returns: granted, denied or blocked
895 * Notes: *** grace period support missing
898 nlm4_lock_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
903 log_from_addr("nlm4_lock", rqstp);
905 /* copy cookie from arg to result. See comment in nlm_test_4() */
906 res.cookie = arg->cookie;
908 res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_V4);
913 nlm4_lock_msg_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
918 log_from_addr("nlm4_lock_msg", rqstp);
920 res.cookie = arg->cookie;
921 res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_ASYNC | LOCK_V4);
922 transmit4_result(NLM4_LOCK_RES, &res, getrpcaddr(rqstp));
927 /* nlm_cancel -------------------------------------------------------------- */
929 * Purpose: Cancel a blocked lock request
930 * Returns: granted or denied
934 nlm4_cancel_4_svc(nlm4_cancargs *arg, struct svc_req *rqstp)
939 log_from_addr("nlm4_cancel", rqstp);
941 /* copy cookie from arg to result. See comment in nlm_test_1() */
942 res.cookie = arg->cookie;
945 * Since at present we never return 'nlm_blocked', there can never be
946 * a lock to cancel, so this call always fails.
948 res.stat.stat = unlock(&arg->alock, LOCK_CANCEL);
953 nlm4_cancel_msg_4_svc(nlm4_cancargs *arg, struct svc_req *rqstp)
958 log_from_addr("nlm4_cancel_msg", rqstp);
960 res.cookie = arg->cookie;
962 * Since at present we never return 'nlm_blocked', there can never be
963 * a lock to cancel, so this call always fails.
965 res.stat.stat = unlock(&arg->alock, LOCK_CANCEL | LOCK_V4);
966 transmit4_result(NLM4_CANCEL_RES, &res, getrpcaddr(rqstp));
970 /* nlm_unlock -------------------------------------------------------------- */
972 * Purpose: Release an existing lock
973 * Returns: Always granted, unless during grace period
974 * Notes: "no such lock" error condition is ignored, as the
975 * protocol uses unreliable UDP datagrams, and may well
976 * re-try an unlock that has already succeeded.
979 nlm4_unlock_4_svc(nlm4_unlockargs *arg, struct svc_req *rqstp)
984 log_from_addr("nlm4_unlock", rqstp);
986 res.stat.stat = unlock(&arg->alock, LOCK_V4);
987 res.cookie = arg->cookie;
993 nlm4_unlock_msg_4_svc(nlm4_unlockargs *arg, struct svc_req *rqstp)
998 log_from_addr("nlm4_unlock_msg", rqstp);
1000 res.stat.stat = unlock(&arg->alock, LOCK_V4);
1001 res.cookie = arg->cookie;
1003 transmit4_result(NLM4_UNLOCK_RES, &res, getrpcaddr(rqstp));
1007 /* ------------------------------------------------------------------------- */
1009 * Client-side pseudo-RPCs for results. Note that for the client there
1010 * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
1011 * version returns the results in the RPC result, and so the client
1012 * does not normally receive incoming RPCs.
1014 * The exception to this is nlm_granted(), which is genuinely an RPC
1015 * call from the server to the client - a 'call-back' in normal procedure
1019 /* nlm_granted ------------------------------------------------------------- */
1021 * Purpose: Receive notification that formerly blocked lock now granted
1022 * Returns: always success ('granted')
1026 nlm4_granted_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
1028 static nlm4_res res;
1031 log_from_addr("nlm4_granted", rqstp);
1033 /* copy cookie from arg to result. See comment in nlm_test_1() */
1034 res.cookie = arg->cookie;
1036 res.stat.stat = nlm4_granted;
1041 nlm4_granted_msg_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
1043 static nlm4_res res;
1046 log_from_addr("nlm4_granted_msg", rqstp);
1048 res.cookie = arg->cookie;
1049 res.stat.stat = nlm4_granted;
1050 transmit4_result(NLM4_GRANTED_RES, &res,
1051 (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
1055 /* nlm_test_res ------------------------------------------------------------ */
1057 * Purpose: Accept result from earlier nlm_test_msg() call
1061 nlm4_test_res_4_svc(nlm4_testres *arg, struct svc_req *rqstp)
1064 log_from_addr("nlm4_test_res", rqstp);
1068 /* nlm_lock_res ------------------------------------------------------------ */
1070 * Purpose: Accept result from earlier nlm_lock_msg() call
1074 nlm4_lock_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
1077 log_from_addr("nlm4_lock_res", rqstp);
1082 /* nlm_cancel_res ---------------------------------------------------------- */
1084 * Purpose: Accept result from earlier nlm_cancel_msg() call
1088 nlm4_cancel_res_4_svc(nlm4_res *arg __unused, struct svc_req *rqstp)
1091 log_from_addr("nlm4_cancel_res", rqstp);
1095 /* nlm_unlock_res ---------------------------------------------------------- */
1097 * Purpose: Accept result from earlier nlm_unlock_msg() call
1101 nlm4_unlock_res_4_svc(nlm4_res *arg __unused, struct svc_req *rqstp)
1104 log_from_addr("nlm4_unlock_res", rqstp);
1108 /* nlm_granted_res --------------------------------------------------------- */
1110 * Purpose: Accept result from earlier nlm_granted_msg() call
1114 nlm4_granted_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
1117 log_from_addr("nlm4_granted_res", rqstp);
1121 /* ------------------------------------------------------------------------- */
1123 * Calls for PCNFS locking (aka non-monitored locking, no involvement
1126 * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
1129 /* nlm_share --------------------------------------------------------------- */
1131 * Purpose: Establish a DOS-style lock
1132 * Returns: success or failure
1133 * Notes: Blocking locks are not supported - client is expected
1134 * to retry if required.
1137 nlm4_share_4_svc(nlm4_shareargs *arg, struct svc_req *rqstp)
1139 static nlm4_shareres res;
1142 log_from_addr("nlm4_share", rqstp);
1144 res.cookie = arg->cookie;
1145 res.stat = nlm4_granted;
1146 res.sequence = 1234356; /* X/Open says this field is ignored? */
1150 /* nlm4_unshare ------------------------------------------------------------ */
1152 * Purpose: Release a DOS-style lock
1153 * Returns: nlm_granted, unless in grace period
1157 nlm4_unshare_4_svc(nlm4_shareargs *arg, struct svc_req *rqstp)
1159 static nlm4_shareres res;
1162 log_from_addr("nlm_unshare", rqstp);
1164 res.cookie = arg->cookie;
1165 res.stat = nlm4_granted;
1166 res.sequence = 1234356; /* X/Open says this field is ignored? */
1170 /* nlm4_nm_lock ------------------------------------------------------------ */
1172 * Purpose: non-monitored version of nlm4_lock()
1173 * Returns: as for nlm4_lock()
1174 * Notes: These locks are in the same style as the standard nlm4_lock,
1175 * but the rpc.statd should not be called to establish a
1176 * monitor for the client machine, since that machine is
1177 * declared not to be running a rpc.statd, and so would not
1178 * respond to the statd protocol.
1181 nlm4_nm_lock_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
1183 static nlm4_res res;
1186 log_from_addr("nlm4_nm_lock", rqstp);
1188 /* copy cookie from arg to result. See comment in nlm4_test_1() */
1189 res.cookie = arg->cookie;
1190 res.stat.stat = nlm4_granted;
1194 /* nlm4_free_all ------------------------------------------------------------ */
1196 * Purpose: Release all locks held by a named client
1198 * Notes: Potential denial of service security problem here - the
1199 * locks to be released are specified by a host name, independent
1200 * of the address from which the request has arrived.
1201 * Should probably be rejected if the named host has been
1202 * using monitored locks.
1205 nlm4_free_all_4_svc(nlm_notify *arg, struct svc_req *rqstp)
1210 log_from_addr("nlm4_free_all", rqstp);
1214 /* nlm_sm_notify --------------------------------------------------------- */
1216 * Purpose: called by rpc.statd when a monitored host state changes.
1220 nlm_sm_notify_0_svc(struct nlm_sm_status *arg, struct svc_req *rqstp __unused)
1223 notify(arg->mon_name, arg->state);