Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libnfs/lib/pdu.c
Line
Count
Source
1
/* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
2
/*
3
   Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as published by
7
   the Free Software Foundation; either version 2.1 of the License, or
8
   (at your option) any later version.
9
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
*/
18
#ifdef HAVE_CONFIG_H
19
#include "config.h"
20
#endif
21
22
#ifdef AROS
23
#include "aros_compat.h"
24
#endif
25
26
#ifdef PS2_EE
27
#include "ps2_compat.h"
28
#endif
29
30
#ifdef PS3_PPU
31
#include "ps3_compat.h"
32
#endif
33
34
#ifdef WIN32
35
#include <win32/win32_compat.h>
36
#endif
37
38
#ifdef HAVE_NETINET_IN_H
39
#include <netinet/in.h>
40
#endif
41
42
#ifdef HAVE_SYS_SOCKET_H
43
#include <sys/socket.h>
44
#endif
45
46
#if defined(HAVE_SYS_UIO_H) || (defined(__APPLE__) && defined(__MACH__))
47
#include <sys/uio.h>
48
#endif
49
50
#ifdef HAVE_STRINGS_H
51
#include <strings.h>
52
#endif
53
54
#include <stdio.h>
55
#include <stdlib.h>
56
#include <string.h>
57
#include <assert.h>
58
#include <errno.h>
59
#include "slist.h"
60
#include "libnfs-zdr.h"
61
#include "libnfs.h"
62
#include "libnfs-raw.h"
63
#include "libnfs-private.h"
64
65
#ifdef HAVE_LIBKRB5
66
#include "krb5-wrapper.h"
67
#endif
68
69
void rpc_reset_queue(struct rpc_queue *q)
70
29.0k
{
71
29.0k
  q->head = NULL;
72
29.0k
  q->tail = NULL;
73
29.0k
  q->tailp = NULL;
74
29.0k
}
75
76
/*
77
 * Push to the tail end of the queue
78
 */
79
void rpc_enqueue(struct rpc_queue *q, struct rpc_pdu *pdu)
80
0
{
81
0
  if (q->head == NULL) {
82
0
          assert(q->tail == NULL);
83
0
    q->head = pdu;
84
0
        } else {
85
0
                assert(pdu != q->head);
86
0
                assert(pdu != q->tail);
87
0
    q->tail->next = pdu;
88
0
        }
89
0
  q->tail = pdu;
90
0
  pdu->next = NULL;
91
0
}
92
93
/*
94
 * Add pdu to the head of outqueue.
95
 * If the pdu currently at the head has been partially written to the socket
96
 * we cannot displace it, as that would interleave bytes from two different
97
 * PDUs on the wire, so in that case we add pdu right after the head.
98
 */
99
void rpc_add_to_outqueue_head(struct rpc_context *rpc, struct rpc_pdu *pdu)
100
0
{
101
0
        if (rpc->outqueue.head == NULL) {
102
0
                assert(rpc->outqueue.tail == NULL);
103
0
                assert(rpc->outqueue.tailp == NULL);
104
0
                assert(rpc->stats.outqueue_len == 0);
105
106
0
                rpc->outqueue.head = rpc->outqueue.tail = pdu;
107
0
                pdu->next = NULL;
108
0
                if (pdu->is_high_prio) {
109
0
                        rpc->outqueue.tailp = pdu;
110
0
                }
111
0
        } else {
112
0
                assert(rpc->stats.outqueue_len > 0);
113
0
                assert(rpc->outqueue.tail->next == NULL);
114
0
                assert(pdu != rpc->outqueue.head);
115
0
                assert(pdu != rpc->outqueue.tail);
116
0
                assert(pdu != rpc->outqueue.tailp);
117
118
0
                if (rpc->outqueue.head->out.num_done == 0) {
119
                        /*
120
                         * Head is untouched, safe to displace it. A high prio
121
                         * pdu placed at the head starts the high prio run, so
122
                         * it becomes tailp only if there is no high prio run
123
                         * yet.
124
                         */
125
0
                        pdu->next = rpc->outqueue.head;
126
0
                        rpc->outqueue.head = pdu;
127
0
                        if (pdu->is_high_prio && rpc->outqueue.tailp == NULL) {
128
0
                                rpc->outqueue.tailp = pdu;
129
0
                        }
130
0
                } else {
131
                        /*
132
                         * Head is partially sent, add right after it. If the
133
                         * high prio run was empty or ended at the head, pdu
134
                         * now terminates it.
135
                         */
136
0
                        if (pdu->is_high_prio &&
137
0
                            (rpc->outqueue.tailp == NULL ||
138
0
                             rpc->outqueue.tailp == rpc->outqueue.head)) {
139
0
                                rpc->outqueue.tailp = pdu;
140
0
                        }
141
0
                        pdu->next = rpc->outqueue.head->next;
142
0
                        rpc->outqueue.head->next = pdu;
143
0
                }
144
145
0
                if (pdu->next == NULL) {
146
0
                        rpc->outqueue.tail = pdu;
147
0
                }
148
0
        }
149
150
0
        assert(rpc->outqueue.tail->next == NULL);
151
0
        assert(rpc->outqueue.tail->is_high_prio ==
152
0
                (rpc->outqueue.tailp == rpc->outqueue.tail));
153
154
0
        if (rpc->stats.outqueue_len++ == 0) {
155
                /*
156
                 * First pdu on a previously empty outqueue, so the service
157
                 * thread may be sitting in poll(). Nudge it.
158
                 */
159
0
                rpc_wakeup_service_thread(rpc);
160
0
        }
161
0
}
162
163
/*
164
 * High priority pdus are added right after tailp, i.e., behind the already
165
 * queued high priority pdus but ahead of every low priority pdu.
166
 */
167
void rpc_add_to_outqueue_highp(struct rpc_context *rpc, struct rpc_pdu *pdu)
168
0
{
169
0
        pdu->is_high_prio = TRUE;
170
171
0
        if (rpc->outqueue.tailp == NULL) {
172
                /* First high priority pdu, add to the head. */
173
0
                rpc_add_to_outqueue_head(rpc, pdu);
174
0
                assert(rpc->outqueue.tailp != NULL);
175
0
        } else {
176
0
                assert(rpc->outqueue.head != NULL);
177
0
                assert(rpc->outqueue.tail != NULL);
178
0
                assert(rpc->stats.outqueue_len > 0);
179
0
                assert(pdu != rpc->outqueue.head);
180
0
                assert(pdu != rpc->outqueue.tail);
181
0
                assert(pdu != rpc->outqueue.tailp);
182
183
0
                pdu->next = rpc->outqueue.tailp->next;
184
0
                rpc->outqueue.tailp->next = pdu;
185
0
                if (rpc->outqueue.tail == rpc->outqueue.tailp) {
186
0
                        rpc->outqueue.tail = pdu;
187
0
                }
188
0
                rpc->outqueue.tailp = pdu;
189
0
                rpc->stats.outqueue_len++;
190
0
        }
191
0
}
192
193
/*
194
 * Low priority pdus are added to the tail.
195
 */
196
void rpc_add_to_outqueue_lowp(struct rpc_context *rpc, struct rpc_pdu *pdu)
197
0
{
198
0
        pdu->is_high_prio = FALSE;
199
0
        rpc_enqueue(&rpc->outqueue, pdu);
200
0
        if (rpc->stats.outqueue_len++ == 0) {
201
                /*
202
                 * First pdu on a previously empty outqueue, so the service
203
                 * thread may be sitting in poll(). Nudge it.
204
                 */
205
0
                rpc_wakeup_service_thread(rpc);
206
0
        }
207
208
0
        assert(rpc->outqueue.head != NULL);
209
0
        assert(rpc->outqueue.tail != NULL);
210
0
        assert(pdu->next == NULL);
211
0
}
212
213
/*
214
 * Return pdu to outqueue to be retransmitted.
215
 * It adds the pdu to the head of outqueue, unless the head pdu is partially
216
 * sent, in which case it adds it right after the head pdu.
217
 */
218
void rpc_return_to_outqueue(struct rpc_context *rpc, struct rpc_pdu *pdu)
219
0
{
220
0
        rpc_add_to_outqueue_head(rpc, pdu);
221
222
        /*
223
         * Only already transmitted PDUs are added back to outqueue, so sending
224
         * it out will entail a retransmit.
225
         */
226
0
        INC_STATS(rpc, num_retransmitted);
227
0
        pdu->is_retransmitted = 1;
228
229
        /*
230
         * Reset output and input cursors as we have to re-send the whole pdu
231
         * again (and read back the response fresh into pdu->in).
232
         */
233
0
        pdu->out.num_done = 0;
234
0
        rpc_reset_cursor(rpc, &pdu->in);
235
0
}
236
237
/*
238
 * Remove pdu from q.
239
 * If found it'll remove the pdu and update q->head and q->tail correctly.
240
 * Returns 0 if remove_pdu not found in q else returns 1.
241
 */
242
int rpc_remove_pdu_from_queue(struct rpc_queue *q, struct rpc_pdu *remove_pdu)
243
0
{
244
0
        if (q->head != NULL) {
245
0
                struct rpc_pdu *pdu = q->head;
246
247
0
                assert(q->tail != NULL);
248
249
                /*
250
                 * remove_pdu is the head pdu.
251
                 * Change the head to point to the next pdu.
252
                 * If tail is also pointing to remove_pdu, this means it's the
253
                 * only PDU and after removing that we will have an empty list.
254
                 */
255
0
                if (q->head == remove_pdu) {
256
0
                        q->head = remove_pdu->next;
257
258
                        /*
259
                         * If tailp pointed at the head it was the only high
260
                         * priority pdu, so the high priority run is now empty.
261
                         */
262
0
                        if (q->tailp == remove_pdu) {
263
0
                                q->tailp = NULL;
264
0
                        }
265
266
0
                        if (q->tail == remove_pdu) {
267
0
                                assert(remove_pdu->next == NULL);
268
0
                                q->tail = NULL;
269
0
                                assert(q->tailp == NULL);
270
0
                                assert(q->head == NULL);
271
0
                        } else {
272
0
                                assert(q->head != NULL);
273
0
                        }
274
275
0
                        remove_pdu->next = NULL;
276
0
                        return 1;
277
0
                }
278
279
                /*
280
                 * remove_pdu is not the head pdu.
281
                 * Search for it and if found, remove it, and update tail if
282
                 * tail is pointing to remove_pdu.
283
                 */
284
0
                while (pdu->next && pdu->next != remove_pdu) {
285
0
                        pdu = pdu->next;
286
0
                }
287
288
0
                if (pdu->next == NULL) {
289
                        /* remove_pdu not found in q */
290
0
                        return 0;
291
0
                }
292
293
0
                pdu->next = remove_pdu->next;
294
295
0
                if (q->tail == remove_pdu) {
296
0
                        q->tail = pdu;
297
0
                }
298
299
                /*
300
                 * remove_pdu terminated the high priority run, so the run now
301
                 * ends at its predecessor, if that is itself high priority,
302
                 * else the run is empty.
303
                 */
304
0
                if (q->tailp == remove_pdu) {
305
0
                        assert(remove_pdu->is_high_prio);
306
0
                        q->tailp = pdu->is_high_prio ? pdu : NULL;
307
0
                }
308
309
0
                remove_pdu->next = NULL;
310
311
0
                return 1;
312
0
        } else {
313
0
                assert(q->tail == NULL);
314
0
                assert(q->tailp == NULL);
315
                /* not found */
316
0
                return 0;
317
0
        }
318
0
}
319
320
static int rpc_remove_pdu_from_queue_unlocked(struct rpc_context *rpc,
321
                                              struct rpc_queue *q,
322
                                              struct rpc_pdu *remove_pdu)
323
0
{
324
0
        int ret;
325
326
#ifdef HAVE_MULTITHREADING
327
        if (rpc->multithreading_enabled) {
328
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
329
        }
330
#endif /* HAVE_MULTITHREADING */
331
0
        ret = rpc_remove_pdu_from_queue(q, remove_pdu);
332
#ifdef HAVE_MULTITHREADING
333
        if (rpc->multithreading_enabled) {
334
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
335
        }
336
#endif /* HAVE_MULTITHREADING */
337
0
        return ret;
338
0
}
339
        
340
unsigned int rpc_hash_xid(struct rpc_context *rpc, uint32_t xid)
341
0
{
342
0
  return (xid * 7919) % rpc->num_hashes;
343
0
}
344
345
0
#define PAD_TO_8_BYTES(x) ((x + 0x07) & ~0x07)
346
347
static struct rpc_pdu *rpc_allocate_reply_pdu(struct rpc_context *rpc,
348
                                              struct rpc_msg *res,
349
                                              size_t alloc_hint)
350
0
{
351
0
  struct rpc_pdu *pdu;
352
353
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
354
355
0
  pdu = malloc(sizeof(struct rpc_pdu) + ZDR_ENCODEBUF_MINSIZE + alloc_hint);
356
0
  if (pdu == NULL) {
357
0
    rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure and encode buffer");
358
0
    return NULL;
359
0
  }
360
0
  memset(pdu, 0, sizeof(struct rpc_pdu));
361
0
        pdu->discard_after_sending = 1;
362
0
  pdu->xid                = 0;
363
0
  pdu->cb                 = NULL;
364
0
  pdu->private_data       = NULL;
365
0
  pdu->zdr_decode_fn      = NULL;
366
0
  pdu->zdr_decode_bufsize = 0;
367
368
0
  pdu->outdata.data = (char *)(pdu + 1);
369
370
0
        pdu->out.iov = pdu->out.fast_iov;
371
0
        pdu->out.iov_capacity = RPC_FAST_VECTORS;
372
373
        /* Add an iovector for the record marker. Ignored for UDP */
374
0
        rpc_add_iovector(rpc, &pdu->out, pdu->outdata.data, 4, NULL);
375
376
        /*
377
         * The buffer starts 4 bytes into the allocation, so it is 4 bytes
378
         * shorter than what was asked for, as rpc_allocate_pdu2() has it.
379
         */
380
0
  zdrmem_create(&pdu->zdr, &pdu->outdata.data[4],
381
0
                      ZDR_ENCODEBUF_MINSIZE + alloc_hint - 4, ZDR_ENCODE);
382
383
0
  if (zdr_replymsg(rpc, &pdu->zdr, res) == 0) {
384
0
    rpc_set_error(rpc, "zdr_replymsg failed with %s",
385
0
            rpc_get_error(rpc));
386
0
    zdr_destroy(&pdu->zdr);
387
0
    free(pdu);
388
0
    return NULL;
389
0
  }
390
391
        /* Add an iovector for the header */
392
0
        rpc_add_iovector(rpc, &pdu->out, &pdu->outdata.data[4],
393
0
                         zdr_getpos(&pdu->zdr), NULL);
394
395
        /* Freshly allocated PDU cannot be retransmitted */
396
0
        assert(!pdu->is_retransmitted);
397
398
0
  return pdu;
399
0
}
400
401
struct rpc_pdu *rpc_allocate_pdu2_auth(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_decode_bufsize, size_t alloc_hint, int iovcnt_hint, const struct AUTH *auth)
402
0
{
403
0
  struct rpc_pdu *pdu;
404
0
  int pdu_size;
405
#ifdef HAVE_LIBKRB5
406
        uint32_t val;
407
#endif
408
409
#ifdef HAVE_TLS
410
  /*
411
   * Caller overloads procedure to convey they want to send AUTH_TLS instead of
412
   * AUTH_NONE for the NULL RPC.
413
   */
414
  const bool_t send_auth_tls = !!(procedure & 0x80000000U);
415
  procedure = (procedure & 0x7FFFFFFFU);
416
417
  /* AUTH_TLS can only be sent for NFS NULL RPC */
418
  assert(!send_auth_tls || (program == NFS_PROGRAM && procedure == 0));
419
#endif /* HAVE_TLS */
420
421
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
422
0
  if (auth == NULL) {
423
0
    auth = rpc->auth;
424
0
  }
425
0
  if (auth == NULL) {
426
0
    rpc_set_error(rpc, "No RPC authentication available for request");
427
0
    return NULL;
428
0
  }
429
430
  /* Since we already know how much buffer we need for the decoding
431
   * we can just piggyback in the same alloc as for the pdu.
432
   */
433
0
  pdu_size = PAD_TO_8_BYTES(sizeof(struct rpc_pdu));
434
0
  pdu_size += PAD_TO_8_BYTES(zdr_decode_bufsize);
435
436
0
  pdu = malloc(pdu_size + ZDR_ENCODEBUF_MINSIZE + alloc_hint);
437
0
  if (pdu == NULL) {
438
0
    rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure and encode buffer");
439
0
    return NULL;
440
0
  }
441
0
  memset(pdu, 0, pdu_size);
442
#ifdef HAVE_MULTITHREADING
443
        if (rpc->multithreading_enabled) {
444
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
445
        }
446
#endif /* HAVE_MULTITHREADING */
447
0
  pdu->xid                = rpc->xid++;
448
#ifdef HAVE_MULTITHREADING
449
        if (rpc->multithreading_enabled) {
450
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
451
        }
452
#endif /* HAVE_MULTITHREADING */
453
0
  pdu->cb                 = cb;
454
0
  pdu->private_data       = private_data;
455
0
  pdu->zdr_decode_fn      = zdr_decode_fn;
456
0
  pdu->zdr_decode_bufsize = zdr_decode_bufsize;
457
458
0
        if (iovcnt_hint > RPC_FAST_VECTORS) {
459
0
                pdu->out.iov = (struct rpc_iovec *) calloc(iovcnt_hint, sizeof(struct rpc_iovec));
460
0
                if (pdu->out.iov == NULL) {
461
0
                    rpc_set_error(rpc, "Out of memory: Failed to allocate out.iov");
462
0
                    goto failed2;
463
0
                }
464
0
                pdu->out.iov_capacity = iovcnt_hint;
465
0
        } else {
466
0
                pdu->out.iov = pdu->out.fast_iov;
467
0
                pdu->out.iov_capacity = RPC_FAST_VECTORS;
468
0
        }
469
470
        /*
471
         * Rest of the code depends on this, so assert it here.
472
         * If the caller uses this pdu for issuing a zero-copy READ,
473
         * pdu->in.base will be set to point to the dynamically allocated
474
         * iovec array.
475
         */
476
0
        assert(pdu->in.base == NULL);
477
478
0
  pdu->outdata.data = ((char *)pdu + pdu_size);
479
480
        /* Add an iovector for the record marker. Ignored for UDP */
481
0
        rpc_add_iovector(rpc, &pdu->out, pdu->outdata.data, 4, NULL);
482
483
0
        zdrmem_create(&pdu->zdr, &pdu->outdata.data[4],
484
0
                      ZDR_ENCODEBUF_MINSIZE + alloc_hint - 4, ZDR_ENCODE);
485
0
  memset(&pdu->msg, 0, sizeof(struct rpc_msg));
486
0
  pdu->msg.xid                = pdu->xid;
487
0
        pdu->msg.direction          = CALL;
488
0
  pdu->msg.body.cbody.rpcvers = RPC_MSG_VERSION;
489
0
  pdu->msg.body.cbody.prog    = program;
490
0
  pdu->msg.body.cbody.vers    = version;
491
0
  pdu->msg.body.cbody.proc    = procedure;
492
493
0
  pdu->do_not_retry      = (program != NFS_PROGRAM);
494
495
  /* For NULL RPC RFC recommends to use NULL authentication */
496
0
  if (procedure == 0) {
497
0
    pdu->msg.body.cbody.cred.oa_flavor    = AUTH_NONE;
498
0
    pdu->msg.body.cbody.cred.oa_length    = 0;
499
0
    pdu->msg.body.cbody.cred.oa_base      = NULL;
500
    /*
501
     * NULL RPC is like a ping which is sent right after connection
502
     * establishment. The transport is still not used for sending
503
     * other RPCs. It's best not to retry NULL RPC and let the caller
504
     * truthfully know about the transport status.
505
     */
506
0
    pdu->do_not_retry                = TRUE;
507
0
  } else {
508
0
    pdu->msg.body.cbody.cred    = auth->ah_cred;
509
0
  }
510
511
0
  pdu->msg.body.cbody.verf    = auth->ah_verf;
512
513
#ifdef HAVE_TLS
514
  /* Should not be already set */
515
  assert(pdu->expect_starttls == FALSE);
516
517
  if (send_auth_tls) {
518
    pdu->msg.body.cbody.cred.oa_flavor    = AUTH_TLS;
519
    pdu->msg.body.cbody.cred.oa_length    = 0;
520
    pdu->msg.body.cbody.cred.oa_base      = NULL;
521
522
    pdu->expect_starttls     = TRUE;
523
        }
524
#endif /* HAVE_TLS */
525
526
#ifdef HAVE_LIBKRB5
527
#ifdef HAVE_MULTITHREADING
528
        if (rpc->multithreading_enabled) {
529
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
530
        }
531
#endif /* HAVE_MULTITHREADING */
532
        if (rpc->sec != RPC_SEC_UNDEFINED) {
533
                ZDR tmpzdr;
534
                int level = RPC_GSS_SVC_NONE;
535
536
                pdu->gss_seqno = rpc->gss_seqno;
537
538
                zdrmem_create(&tmpzdr, pdu->creds, 64, ZDR_ENCODE);
539
                switch (rpc->sec) {
540
                case RPC_SEC_UNDEFINED:
541
                        break;
542
                case RPC_SEC_KRB5:
543
                        level = RPC_GSS_SVC_NONE;
544
                        break;
545
                case RPC_SEC_KRB5I:
546
                        if (pdu->gss_seqno > 0) {
547
                                level = RPC_GSS_SVC_INTEGRITY;
548
                        }
549
                        break;
550
                case RPC_SEC_KRB5P:
551
                        if (pdu->gss_seqno > 0) {
552
                                level = RPC_GSS_SVC_PRIVACY;
553
                        }
554
                        break;
555
                }
556
                if (libnfs_authgss_gen_creds(rpc, &tmpzdr, level) < 0) {
557
                        zdr_destroy(&tmpzdr);
558
                        rpc_set_error(rpc, "zdr_callmsg failed with %s",
559
                                      rpc_get_error(rpc));
560
                        goto failed;
561
                }
562
                pdu->msg.body.cbody.cred.oa_flavor = AUTH_GSS;
563
                pdu->msg.body.cbody.cred.oa_length = tmpzdr.pos;
564
                pdu->msg.body.cbody.cred.oa_base = pdu->creds;
565
                zdr_destroy(&tmpzdr);
566
567
                rpc->gss_seqno++;
568
                if (rpc->gss_seqno > 1) {
569
                        pdu->msg.body.cbody.verf.oa_flavor = AUTH_GSS;
570
                        pdu->msg.body.cbody.verf.gss_context = rpc->gss_context;
571
                }
572
        }
573
#ifdef HAVE_MULTITHREADING
574
        if (rpc->multithreading_enabled) {
575
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
576
        }
577
#endif /* HAVE_MULTITHREADING */
578
#endif /* HAVE_LIBKRB5 */
579
580
0
  if (zdr_callmsg(rpc, &pdu->zdr, &pdu->msg) == 0) {
581
0
    rpc_set_error(rpc, "zdr_callmsg failed with %s",
582
0
            rpc_get_error(rpc));
583
0
                goto failed;
584
0
  }
585
586
#ifdef HAVE_LIBKRB5
587
        switch (rpc->sec) {
588
        case RPC_SEC_UNDEFINED:
589
        case RPC_SEC_KRB5:
590
                break;
591
        case RPC_SEC_KRB5P:
592
        case RPC_SEC_KRB5I:
593
                if (pdu->gss_seqno > 0) {
594
                        pdu->start_of_payload = zdr_getpos(&pdu->zdr);
595
                        val = 0; /* dummy length, will fill in below once we know */
596
                        if (!libnfs_zdr_u_int(&pdu->zdr, &val)) {
597
                                goto failed;
598
                       }
599
                        val = pdu->gss_seqno;
600
                        if (!libnfs_zdr_u_int(&pdu->zdr, &val)) {
601
                                goto failed;
602
                        }
603
                }
604
                break;
605
        }
606
#endif /* HAVE_LIBKRB5 */
607
608
        /* Add an iovector for the header */
609
0
        rpc_add_iovector(rpc, &pdu->out, &pdu->outdata.data[4],
610
0
                         zdr_getpos(&pdu->zdr), NULL);
611
612
        /* Freshly allocated PDU cannot be retransmitted */
613
0
        assert(!pdu->is_retransmitted);
614
615
0
  return pdu;
616
0
 failed:
617
0
        rpc_set_error(rpc, "zdr_callmsg failed with %s",
618
0
                      rpc_get_error(rpc));
619
0
        zdr_destroy(&pdu->zdr);
620
0
 failed2:
621
0
        free(pdu);
622
0
  return NULL;
623
0
}
624
625
struct rpc_pdu *rpc_allocate_pdu2(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_decode_bufsize, size_t alloc_hint, int iovcnt_hint)
626
0
{
627
0
  return rpc_allocate_pdu2_auth(rpc, program, version, procedure, cb,
628
0
                                private_data, zdr_decode_fn,
629
0
                                zdr_decode_bufsize, alloc_hint,
630
0
                                iovcnt_hint, NULL);
631
0
}
632
633
struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_decode_bufsize)
634
0
{
635
0
  return rpc_allocate_pdu2(rpc, program, version, procedure, cb, private_data, zdr_decode_fn, zdr_decode_bufsize, 0, 0);
636
0
}
637
638
void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
639
0
{
640
#ifdef HAVE_LIBKRB5
641
        uint32_t min;
642
#endif /* HAVE_LIBKRB5 */
643
644
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
645
646
0
#ifdef HAVE_NFS4_2
647
        /*
648
         * Hand back the NFSv4.2 session slot this COMPOUND was holding. This
649
         * is the one place every pdu passes through, whether it completed,
650
         * errored, timed out or was cancelled, so it is the only place the
651
         * slot can be released from without leaking it on some path.
652
         */
653
0
        if (pdu->nfs4_slot_held) {
654
0
                pdu->nfs4_slot_held = 0;
655
0
                nfs4_session_put_slot(rpc, pdu->nfs4_slot,
656
0
                                      !pdu->nfs4_slot_sent);
657
0
        }
658
0
#endif /* HAVE_NFS4_2 */
659
660
#ifdef HAVE_RDMA
661
        if (pdu->rdma_slot) {
662
                rpc_rdma_release_pdu(rpc, pdu);
663
        }
664
#endif /* HAVE_RDMA */
665
666
0
  if (pdu->zdr_decode_buf != NULL) {
667
0
    zdr_free(pdu->zdr_decode_fn, pdu->zdr_decode_buf);
668
0
  }
669
670
#ifdef HAVE_LIBKRB5
671
        gss_release_buffer(&min, &pdu->output_buffer);
672
#endif /* HAVE_LIBKRB5 */
673
0
  zdr_destroy(&pdu->zdr);
674
675
0
        rpc_free_iovector(rpc, &pdu->out);
676
0
        rpc_free_cursor(rpc, &pdu->in);
677
0
        free(pdu);
678
0
}
679
680
void rpc_set_next_xid(struct rpc_context *rpc, uint32_t xid)
681
0
{
682
#ifdef HAVE_MULTITHREADING
683
        if (rpc->multithreading_enabled) {
684
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
685
        }
686
#endif /* HAVE_MULTITHREADING */
687
0
  rpc->xid = xid;
688
#ifdef HAVE_MULTITHREADING
689
        if (rpc->multithreading_enabled) {
690
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
691
        }
692
#endif /* HAVE_MULTITHREADING */
693
0
}
694
695
void pdu_set_timeout(struct rpc_context *rpc, struct rpc_pdu *pdu, uint64_t now_msecs)
696
0
{
697
0
  if (rpc->timeout <= 0) {
698
    /* RPC request never times out */
699
0
    pdu->timeout = 0;
700
0
    return;
701
0
  }
702
703
  /* If user hasn't passed the current time, get it now */
704
0
  if (now_msecs == 0) {
705
0
    now_msecs = rpc_current_time();
706
0
  }
707
708
  /*
709
   * If pdu->timeout is 0 it means either this is the first time we are
710
   * setting the timeout for this RPC request or it has already timed out.
711
   * In both these cases we reset pdu->timeout to rpc->timeout from now.
712
   * If pdu->timeout is not 0 it means that the RPC has not yet timed out
713
   * and hence we leave it unchanged.
714
   */
715
0
  if (pdu->timeout == 0) {
716
0
    pdu->timeout = now_msecs + rpc->timeout;
717
#ifndef HAVE_CLOCK_GETTIME
718
    /* If we do not have GETTIME we fallback to time() which
719
     * has 1s granularity for its timestamps.
720
     * We thus need to bump the timeout by 1000ms
721
     * so that the PDU will timeout within 1.0 - 2.0 seconds.
722
     * Otherwise setting a 1s timeout would trigger within
723
     * 0.001 - 1.0s.
724
     */
725
    pdu->timeout += 1000;
726
#endif
727
0
  }
728
729
        /*
730
         * On major timeout we reset both major_timeout and timeout.
731
         * Note that timeout can be updated multiple times before a major
732
         * timeout, depending on the value of rpc->retrans.
733
         */
734
0
  if (pdu->major_timeout == 0) {
735
0
    pdu->major_timeout = now_msecs + ((uint64_t)rpc->timeout * rpc->retrans);
736
0
    pdu->timeout = now_msecs + rpc->timeout;
737
#ifndef HAVE_CLOCK_GETTIME
738
    pdu->major_timeout += 1000;
739
    pdu->timeout += 1000;
740
#endif
741
                /* Never less than pdu->timeout */
742
0
                if (pdu->major_timeout < pdu->timeout) {
743
0
                        pdu->major_timeout = pdu->timeout;
744
0
                }
745
0
        }
746
0
}
747
748
/*
749
 * Queue pdu to rpc->outqueue.
750
 * The PDU is queued at the tail of outqueue unless prio is PDU_Q_PRIO_HI, in
751
 * which case it is queued ahead of all the low priority PDUs already queued
752
 * (but behind the high priority ones, so high priority PDUs keep their
753
 * relative order).
754
 *
755
 * High priority queueing is useful for non-bulk RPCs so that they can be
756
 * promptly sent out instead of waiting behind a possibly large number of
757
 * queued WRITE RPCs. A backlog of WRITEs can take a very long time to drain,
758
 * making commands like stat/ls/find appear to hang.
759
 *
760
 * Note that this only reorders PDUs that have not been sent yet. Callers that
761
 * depend on ordering between two RPCs must wait for the first to complete
762
 * before issuing the second, exactly as before.
763
 */
764
int rpc_queue_pdu2(struct rpc_context *rpc, struct rpc_pdu *pdu, int prio)
765
0
{
766
0
  int i, size = 0, pos;
767
0
        uint32_t recordmarker;
768
        /*
769
         * A PDU that lands at the head of the outqueue can be sent inline
770
         * from here, there is no partially sent PDU ahead of it to interleave
771
         * with.
772
         */
773
0
        bool_t send_now;
774
#ifdef HAVE_LIBKRB5
775
        uint32_t maj, min, val, len;
776
        gss_buffer_desc message_buffer, output_token;
777
        char *buf;
778
#endif /* HAVE_LIBKRB5 */
779
780
0
        assert(prio == PDU_Q_PRIO_LOW || prio == PDU_Q_PRIO_HI);
781
782
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
783
784
#ifdef HAVE_LIBKRB5
785
        switch (rpc->sec) {
786
        case RPC_SEC_UNDEFINED:
787
        case RPC_SEC_KRB5:
788
                break;
789
        case RPC_SEC_KRB5I:
790
                if (pdu->gss_seqno == 0) {
791
                        break;
792
                }
793
                pos = zdr_getpos(&pdu->zdr);
794
                zdr_setpos(&pdu->zdr, pdu->start_of_payload);
795
                val = pos - pdu->start_of_payload - 4;
796
                if (!libnfs_zdr_u_int(&pdu->zdr, &val)) {
797
                        rpc_free_pdu(rpc, pdu);
798
                        return -1;
799
                }
800
                zdr_setpos(&pdu->zdr, pos);
801
802
                /* checksum */
803
                message_buffer.length = zdr_getpos(&pdu->zdr) - pdu->start_of_payload - 4;
804
                message_buffer.value = zdr_getptr(&pdu->zdr) + pdu->start_of_payload + 4;
805
                maj = gss_get_mic(&min, rpc->gss_context,
806
                                  GSS_C_QOP_DEFAULT,
807
                                  &message_buffer,
808
                                  &output_token);
809
                if (maj != GSS_S_COMPLETE) {
810
                        rpc_free_pdu(rpc, pdu);
811
                        return -1;
812
                }
813
                buf = output_token.value;
814
                len = output_token.length;
815
                if (!libnfs_zdr_bytes(&pdu->zdr, &buf, &len, len)) {
816
                        gss_release_buffer(&min, &output_token);
817
                        rpc_free_pdu(rpc, pdu);
818
                        return -1;
819
                }
820
                gss_release_buffer(&min, &output_token);
821
                break;
822
        case RPC_SEC_KRB5P:
823
                if (pdu->gss_seqno == 0) {
824
                        break;
825
                }
826
                pos = zdr_getpos(&pdu->zdr);
827
                message_buffer.length = zdr_getpos(&pdu->zdr) - pdu->start_of_payload - 4;
828
                message_buffer.value = zdr_getptr(&pdu->zdr) + pdu->start_of_payload + 4;
829
                maj = gss_wrap (&min, rpc->gss_context, 1,
830
                                GSS_C_QOP_DEFAULT,
831
                                &message_buffer,
832
                                NULL,
833
                                &output_token);
834
                if (maj != GSS_S_COMPLETE) {
835
                        rpc_free_pdu(rpc, pdu);
836
                        return -1;
837
                }
838
                zdr_setpos(&pdu->zdr, pdu->start_of_payload);
839
                buf = output_token.value;
840
                len = output_token.length;
841
                if (!libnfs_zdr_bytes(&pdu->zdr, &buf, &len, len)) {
842
                        gss_release_buffer(&min, &output_token);
843
                        rpc_free_pdu(rpc, pdu);
844
                        return -1;
845
                }
846
                gss_release_buffer(&min, &output_token);
847
                break;
848
        }
849
#endif /* HAVE_LIBKRB5 */
850
851
0
        pos = zdr_getpos(&pdu->zdr);
852
853
        /*
854
         * Now that the RPC is about to be queued, set absolute timeout values
855
         * for it.
856
         */
857
0
        pdu_set_timeout(rpc, pdu, 0);
858
859
0
        for (i = 1; i < pdu->out.niov; i++) {
860
0
                size += pdu->out.iov[i].len;
861
0
        }
862
0
        pdu->out.total_size = size + 4;
863
864
        /* If we need to add any additional iovectors
865
         *
866
         * We expect to almost always add an iovector here for the remainder
867
         * of the outdata marshalling buffer.
868
         * The exception is WRITE where we add an explicit iovector instead
869
         * of marshalling it in ZDR. This so that we can do zero-copy for
870
         * the WRITE path.
871
         */
872
0
        if (pos > size) {
873
0
                int count = pos - size;
874
875
0
                if (rpc_add_iovector(rpc, &pdu->out,
876
0
                                     &pdu->outdata.data[pdu->out.total_size],
877
0
                                     count, NULL) < 0) {
878
0
                        rpc_free_pdu(rpc, pdu);
879
0
                        return -1;
880
0
                }
881
0
                pdu->out.total_size += count;
882
0
                size = pos;
883
0
        }
884
885
  /* write recordmarker */
886
0
        recordmarker = htonl(size | 0x80000000);
887
0
  memcpy(pdu->out.iov[0].buf, &recordmarker, 4);
888
889
0
        pdu->pdu_stats.enqueue_timestamp = rpc_current_time_us();
890
0
        pdu->pdu_stats.size = size;
891
0
        pdu->pdu_stats.xid = pdu->msg.xid;
892
0
        pdu->pdu_stats.direction = CALL;
893
0
        pdu->pdu_stats.status = 0;
894
0
        pdu->pdu_stats.prog = pdu->msg.body.cbody.prog;
895
0
        pdu->pdu_stats.vers = pdu->msg.body.cbody.vers;
896
0
        pdu->pdu_stats.proc = pdu->msg.body.cbody.proc;
897
0
        pdu->pdu_stats.response_time = 0;
898
899
        /* 4 bytes for the recordmarker */
900
0
        pdu->req_size = size + 4;
901
902
  /*
903
   * For udp we dont queue, we just send it straight away.
904
   *
905
   * Another case where we send straight away is the AUTH_TLS NULL RPC.
906
   * This is particularly important for the reconnect case where we want to
907
   * ensure TLS handshake completes successfully before we can send any of
908
   * the queued RPCs waiting. If we do not send here this AUTH_TLS NULL
909
   * RPC will need to be queued before all other waiting RPCs and even then
910
   * we need to be careful that we don't send any of those RPCs till the
911
   * TLS handshake is completed and the connection is secure.
912
   * Sending inline here makes the handling simpler in rpc_service().
913
   */
914
0
        if (rpc->is_udp && rpc->is_server_context) {
915
0
                if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT,
916
0
                           (struct sockaddr *)&rpc->udp_dest,
917
0
                           sizeof(rpc->udp_dest)) < 0) {
918
0
                        rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
919
0
                        rpc_free_pdu(rpc, pdu);
920
0
                        return -1;
921
0
                }
922
0
                rpc_free_pdu(rpc, pdu);
923
0
                return 0;
924
0
        }
925
926
0
  if (rpc->is_udp != 0
927
#ifdef HAVE_TLS
928
      || pdu->expect_starttls
929
#endif
930
0
  ) {
931
0
    unsigned int hash;
932
933
#ifdef HAVE_TLS
934
    if (pdu->expect_starttls) {
935
      /* Currently we don't support RPC-with-TLS over UDP */
936
      assert(!rpc->is_udp);
937
      assert(!rpc->is_broadcast);
938
939
      RPC_LOG(rpc, 2, "Sending AUTH_TLS NULL RPC (%u bytes)",
940
                                (int)pdu->out.total_size);
941
    }
942
#endif
943
944
0
    hash = rpc_hash_xid(rpc, pdu->xid);
945
946
#ifdef HAVE_MULTITHREADING
947
                if (rpc->multithreading_enabled) {
948
                        nfs_mt_mutex_lock(&rpc->rpc_mutex);
949
                }
950
#endif /* HAVE_MULTITHREADING */
951
0
                rpc_enqueue(&rpc->waitpdu[hash], pdu);
952
0
                rpc->waitpdu_len++;
953
#ifdef HAVE_MULTITHREADING
954
                if (rpc->multithreading_enabled) {
955
                        nfs_mt_mutex_unlock(&rpc->rpc_mutex);
956
                }
957
#endif /* HAVE_MULTITHREADING */
958
959
0
                if (rpc->is_broadcast || rpc->is_server_context) {
960
0
                        if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT,
961
0
                                   (struct sockaddr *)&rpc->udp_dest,
962
0
                                   sizeof(rpc->udp_dest)) < 0) {
963
0
                                rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
964
0
                                rpc_remove_pdu_from_queue_unlocked(rpc, &rpc->waitpdu[hash], pdu);
965
0
                                rpc_free_pdu(rpc, pdu);
966
0
                                return -1;
967
0
                        }
968
0
                        if (rpc->is_server_context) {
969
0
                                rpc_remove_pdu_from_queue_unlocked(rpc, &rpc->waitpdu[hash], pdu);
970
0
                                rpc_free_pdu(rpc, pdu);
971
0
                                return 0;
972
0
                        }
973
0
                } else {
974
                        /*
975
                         * For UDP we don't support vectored write and for TLS
976
                         * the data will be less, so RPC_FAST_VECTORS should
977
                         * be sufficient for both cases.
978
                         */
979
0
                        struct iovec iov[RPC_FAST_VECTORS];
980
0
                        int niov = pdu->out.niov;
981
                        /* No record marker for UDP */
982
0
                        struct iovec *iovp = (rpc->is_udp ? &iov[1] : &iov[0]);
983
0
                        const int iovn = (rpc->is_udp ? niov - 1 : niov);
984
985
0
                        assert(niov <= RPC_FAST_VECTORS);
986
987
0
                        for (i = 0; i < niov; i++) {
988
0
                                iov[i].iov_base = pdu->out.iov[i].buf;
989
0
                                iov[i].iov_len = pdu->out.iov[i].len;
990
0
                        }
991
0
                        if (writev(rpc->fd, iovp, iovn) < 0) {
992
0
                                rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
993
0
                                rpc_remove_pdu_from_queue_unlocked(rpc, &rpc->waitpdu[hash], pdu);
994
0
                                rpc_free_pdu(rpc, pdu);
995
0
                                return -1;
996
0
                        }
997
0
                }
998
999
0
    return 0;
1000
0
  }
1001
1002
0
  pdu->outdata.size = size;
1003
#ifdef HAVE_MULTITHREADING
1004
        if (rpc->multithreading_enabled) {
1005
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
1006
        }
1007
#endif /* HAVE_MULTITHREADING */
1008
        /* Fresh PDU being queued to outqueue, num_done must be 0 */
1009
0
        assert(pdu->out.num_done == 0);
1010
1011
0
        if (prio == PDU_Q_PRIO_HI) {
1012
0
                rpc_add_to_outqueue_highp(rpc, pdu);
1013
0
        } else {
1014
0
                rpc_add_to_outqueue_lowp(rpc, pdu);
1015
0
        }
1016
1017
        /*
1018
         * Sample this under the lock, another thread may queue ahead of us
1019
         * as soon as we drop it.
1020
         */
1021
0
        send_now = (rpc->outqueue.head == pdu);
1022
#ifdef HAVE_MULTITHREADING
1023
        if (rpc->multithreading_enabled) {
1024
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
1025
        }
1026
#endif /* HAVE_MULTITHREADING */
1027
0
        if (send_now) {
1028
0
                rpc_write_to_socket(rpc);
1029
0
        }
1030
1031
0
  return 0;
1032
0
}
1033
1034
int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
1035
0
{
1036
0
        return rpc_queue_pdu2(rpc, pdu, PDU_Q_PRIO_LOW);
1037
0
}
1038
1039
/*
1040
 * A READ that failed has no data in its reply, only a status for the
1041
 * caller to see.
1042
 */
1043
int rpc_read_reply_ok(struct rpc_context *rpc, struct rpc_pdu *pdu)
1044
0
{
1045
0
        if (rpc->program == NFS_PROGRAM && rpc->version == NFS_V3) {
1046
0
                const READ3res *res = (READ3res *)(void *)pdu->zdr_decode_buf;
1047
0
                return res->status == NFS3_OK;
1048
0
        }
1049
0
        if (rpc->program == NFS4_PROGRAM && rpc->version == NFS_V4) {
1050
0
                const COMPOUND4res *res = (COMPOUND4res *)(void *)pdu->zdr_decode_buf;
1051
0
                return res->status == NFS4_OK;
1052
0
        }
1053
0
        return 1;
1054
0
}
1055
1056
static int rpc_process_reply(struct rpc_context *rpc, ZDR *zdr)
1057
0
{
1058
0
  struct rpc_msg msg;
1059
0
        struct rpc_pdu *pdu = rpc->pdu;
1060
0
        uint32_t status = 0xffffffff;
1061
0
        void *data = NULL;
1062
1063
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
1064
1065
  /* Client got a response for its request */
1066
0
  INC_STATS(rpc, num_resp_rcvd);
1067
1068
0
  memset(&msg, 0, sizeof(struct rpc_msg));
1069
0
  msg.body.rbody.reply.areply.verf = libnfs_null_auth;
1070
0
  if (pdu->zdr_decode_bufsize > 0) {
1071
0
    pdu->zdr_decode_buf = (char *)pdu + PAD_TO_8_BYTES(sizeof(struct rpc_pdu));
1072
0
  }
1073
0
  msg.body.rbody.reply.areply.reply_data.results.where = pdu->zdr_decode_buf;
1074
0
  msg.body.rbody.reply.areply.reply_data.results.proc  = pdu->zdr_decode_fn;
1075
#ifdef HAVE_LIBKRB5
1076
        if (rpc->sec == RPC_SEC_KRB5I && pdu->gss_seqno > 0) {
1077
                msg.body.rbody.reply.areply.reply_data.results.krb5i = 1;
1078
                msg.body.rbody.reply.areply.verf.gss_context = rpc->gss_context;
1079
        }
1080
        if (rpc->sec == RPC_SEC_KRB5P && pdu->gss_seqno > 0) {
1081
                msg.body.rbody.reply.areply.reply_data.results.krb5p = 1;
1082
                msg.body.rbody.reply.areply.reply_data.results.output_buffer = &pdu->output_buffer;
1083
                msg.body.rbody.reply.areply.verf.gss_context = rpc->gss_context;
1084
        }
1085
#endif
1086
0
  if (zdr_replymsg(rpc, zdr, &msg) == 0) {
1087
0
    rpc_set_error(rpc, "zdr_replymsg failed in rpc_process_reply: "
1088
0
            "%s", rpc_get_error(rpc));
1089
0
    pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server",
1090
0
      pdu->private_data);
1091
0
    if (pdu->zdr_decode_buf != NULL) {
1092
0
      pdu->zdr_decode_buf = NULL;
1093
0
    }
1094
0
    return 0;
1095
0
  }
1096
0
  if (msg.body.rbody.stat != MSG_ACCEPTED) {
1097
0
    pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
1098
0
    return 0;
1099
0
  }
1100
0
  switch (msg.body.rbody.reply.areply.stat) {
1101
0
  case SUCCESS:
1102
    /* Last RPC response time for tracking RPC transport health */
1103
0
    rpc->last_successful_rpc_response = rpc_current_time();
1104
0
    if (pdu->snr_logged) {
1105
0
      RPC_LOG(rpc, 1, "[pdu %p] Server %s OK",
1106
0
        pdu, rpc->server);
1107
0
    }
1108
1109
                /*
1110
                 * pdu->in.base will be non-NULL if this pdu is used for
1111
                 * zero-copy READ. In that case we still need to read the
1112
                 * data from the socket into the user's zero-copy buffers,
1113
                 * so don't complete it as yet. Caller will arrange to read
1114
                 * the data and complete the PDU once completed.
1115
                 */
1116
#ifdef HAVE_LIBKRB5
1117
                if (rpc->sec == RPC_SEC_UNDEFINED)
1118
#endif /* HAVE_LIBKRB5 */
1119
0
                        if (pdu->in.base) {
1120
0
                                rpc->pdu->free_pdu = 1;
1121
0
                                break;
1122
0
                        }
1123
1124
#ifdef HAVE_TLS
1125
    /*
1126
     * If we are expecting STARTTLS that means we have sent AUTH_TLS
1127
     * NULL RPC which means user has selected xprtsec=[tls,mtls], in
1128
     * which case the server MUST support TLS else we must terminate
1129
     * the RPC session.
1130
     */
1131
    if (pdu->expect_starttls) {
1132
      const char *const starttls_str = "STARTTLS";
1133
      const int starttls_len = 8;
1134
1135
      if (msg.body.rbody.reply.areply.verf.oa_flavor != AUTH_NONE) {
1136
        RPC_LOG(rpc, 1, "Server sent bad verifier flavor (%d) in response "
1137
          "to AUTH_TLS NULL RPC",
1138
          msg.body.rbody.reply.areply.verf.oa_flavor);
1139
                                status = RPC_STATUS_ERROR;
1140
                                data = "Server sent bad verifier flavor";
1141
        break;
1142
      } else if (msg.body.rbody.reply.areply.verf.oa_length != starttls_len ||
1143
           memcmp(msg.body.rbody.reply.areply.verf.oa_base,
1144
            starttls_str, starttls_len)) {
1145
        RPC_LOG(rpc, 1, "Server does not support TLS");
1146
                                status = RPC_STATUS_ERROR;
1147
                                data = "Server does not support TLS";
1148
        break;
1149
      }
1150
    }
1151
#endif /* HAVE_TLS */
1152
1153
#ifdef HAVE_LIBKRB5
1154
                /*
1155
                 * Once the GSS context is up an accepted reply has to carry
1156
                 * an AUTH_GSS verifier, a MIC over the seq_num of our request.
1157
                 * It is what ties the reply to this call and to the context,
1158
                 * so a reply without one cannot be accepted.
1159
                 */
1160
                if (rpc->sec != RPC_SEC_UNDEFINED && pdu->gss_seqno > 0 &&
1161
                    msg.body.rbody.reply.areply.verf.oa_flavor != AUTH_GSS) {
1162
                        status = RPC_STATUS_ERROR;
1163
                        data = "Server sent a reply without an AUTH_GSS "
1164
                               "verifier";
1165
                        break;
1166
                }
1167
1168
                /*
1169
                 * The verifier flavor is chosen by the server, so on its own
1170
                 * it says nothing about how this reply should be read. Only
1171
                 * act on it once we know the connection actually negotiated
1172
                 * GSS; otherwise a server could steer the reply to an
1173
                 * ordinary AUTH_UNIX request down the GSS path below. On a
1174
                 * non-GSS connection the verifier is simply not interesting,
1175
                 * so ignore it rather than failing the RPC.
1176
                 */
1177
                if (rpc->sec != RPC_SEC_UNDEFINED &&
1178
                    msg.body.rbody.reply.areply.verf.oa_flavor == AUTH_GSS) {
1179
                        uint32_t maj, min;
1180
                        gss_buffer_desc message_buffer, token_buffer;
1181
                        uint32_t seqno;
1182
1183
                        /* This is the the gss token from the NULL reply
1184
                         * that finished authentication.
1185
                         */
1186
                        if (pdu->gss_seqno == 0) {
1187
                                struct rpc_gss_init_res *gir;
1188
1189
                                /*
1190
                                 * Only the GSS init NULL RPC decodes into an
1191
                                 * rpc_gss_init_res. Any other PDU reaching
1192
                                 * here would have us reinterpret an unrelated
1193
                                 * decoded result as one, and dereference
1194
                                 * handle_val, a pointer read out of whatever
1195
                                 * that result happens to contain.
1196
                                 */
1197
                                if (pdu->zdr_decode_buf == NULL ||
1198
                                    pdu->zdr_decode_fn !=
1199
                                    (zdrproc_t)zdr_rpc_gss_init_res) {
1200
                                        status = RPC_STATUS_ERROR;
1201
                                        data = "Server sent an AUTH_GSS "
1202
                                               "verifier for a reply that is "
1203
                                               "not a GSS init response";
1204
                                        break;
1205
                                }
1206
1207
                                gir = (struct rpc_gss_init_res *)(void *)pdu->zdr_decode_buf;
1208
1209
                                rpc->context_len = gir->handle.handle_len;
1210
                                free(rpc->context);
1211
                                rpc->context = malloc(rpc->context_len);
1212
                                if (rpc->context == NULL) {
1213
                                        status = RPC_STATUS_ERROR;
1214
                                        data = "Failed to allocate rpc->context";
1215
                                        break;
1216
                                }
1217
                                memcpy(rpc->context, gir->handle.handle_val, rpc->context_len);
1218
1219
                                if (krb5_auth_request(rpc, rpc->auth_data,
1220
                                                      (unsigned char *)gir->gss_token.gss_token_val,
1221
                                                      gir->gss_token.gss_token_len) < 0) {
1222
                                        status = RPC_STATUS_ERROR;
1223
1224
                                        data = "krb5_auth_request returned error";
1225
                                        break;
1226
                                }
1227
                        }
1228
1229
                        if (pdu->gss_seqno > 0) {
1230
                                seqno = htonl(pdu->gss_seqno);
1231
                                message_buffer.value = (char *)&seqno;
1232
                                message_buffer.length = 4;
1233
1234
                                token_buffer.value = msg.body.rbody.reply.areply.verf.oa_base;
1235
                                token_buffer.length = msg.body.rbody.reply.areply.verf.oa_length;
1236
                                maj = gss_verify_mic(&min,
1237
                                                     rpc->gss_context,
1238
                                                     &message_buffer,
1239
                                                     &token_buffer,
1240
                                                     GSS_C_QOP_DEFAULT);
1241
                                if (maj) {
1242
                                        status = RPC_STATUS_ERROR;
1243
                                        data = "gss_verify_mic failed for the verifier";
1244
                                        break;
1245
                                }
1246
                        }
1247
                }
1248
                /*
1249
                 * krb5 protects nothing past the verifier, so once that is
1250
                 * checked a READ can carry on as zero-copy.
1251
                 */
1252
                if (rpc->sec == RPC_SEC_KRB5 && pdu->in.base) {
1253
                        rpc->pdu->free_pdu = 1;
1254
                        break;
1255
                }
1256
                if (pdu->zero_copy_iov &&
1257
                    (rpc->sec == RPC_SEC_KRB5I || rpc->sec == RPC_SEC_KRB5P) &&
1258
                    rpc_read_reply_ok(rpc, pdu)) {
1259
                        struct iovec *iov;
1260
                        int num_iov, num, count;
1261
1262
                        if (!zdr_uint32_t(&pdu->zdr, &pdu->read_count)) {
1263
                                status = RPC_STATUS_ERROR;
1264
                                data = "rpc_process_reply: failed to read onc-rpc array length";
1265
                                break;
1266
                        }
1267
                        count = pdu->read_count;
1268
                        if (count > libnfs_zdr_getsize(&pdu->zdr) - libnfs_zdr_getpos(&pdu->zdr)) {
1269
                                count = libnfs_zdr_getsize(&pdu->zdr) - libnfs_zdr_getpos(&pdu->zdr);
1270
                        }
1271
                        iov = pdu->in.iov;
1272
                        num_iov = pdu->in.iovcnt;
1273
                        while(count && num_iov) {
1274
                                num = count;
1275
                                if (num > iov->iov_len) {
1276
                                        num = iov->iov_len;
1277
                                }
1278
                                memcpy(iov->iov_base, libnfs_zdr_getptr(&pdu->zdr) + libnfs_zdr_getpos(&pdu->zdr), num);
1279
                                libnfs_zdr_setpos(&pdu->zdr, libnfs_zdr_getpos(&pdu->zdr) + num);
1280
                                count -= num;
1281
                                iov++;
1282
                                num_iov--;
1283
                        }
1284
                }
1285
#endif /* HAVE_LIBKRB5 */
1286
1287
0
                status = RPC_STATUS_SUCCESS;
1288
0
                data = pdu->zdr_decode_buf;
1289
0
    break;
1290
0
  case PROG_UNAVAIL:
1291
0
                status = RPC_STATUS_ERROR;
1292
0
                data = "Server responded: Program not available";
1293
0
    break;
1294
0
  case PROG_MISMATCH:
1295
0
                status = RPC_STATUS_ERROR;
1296
0
                data = "Server responded: Program version mismatch";
1297
0
    break;
1298
0
  case PROC_UNAVAIL:
1299
0
                status = RPC_STATUS_ERROR;
1300
0
                data = "Server responded: Procedure not available";
1301
0
    break;
1302
0
  case GARBAGE_ARGS:
1303
0
                status = RPC_STATUS_ERROR;
1304
0
                data = "Server responded: Garbage arguments";
1305
0
    break;
1306
0
  case SYSTEM_ERR:
1307
0
                status = RPC_STATUS_ERROR;
1308
0
                data = "Server responded: System Error";
1309
0
    break;
1310
0
  default:
1311
0
                status = RPC_STATUS_ERROR;
1312
0
                data = "Unknown rpc response from server";
1313
0
    break;
1314
0
  }
1315
1316
0
        pdu->pdu_stats.size = rpc->rm_xid[0];
1317
0
        pdu->pdu_stats.direction = REPLY;
1318
0
        pdu->pdu_stats.status = msg.body.rbody.stat;
1319
0
        pdu->pdu_stats.response_time = rpc_current_time_us() - pdu->pdu_stats.send_timestamp;
1320
0
        if (rpc->stats_cb) {
1321
0
                rpc->stats_cb(rpc, &pdu->pdu_stats, rpc->stats_private_data);
1322
0
        }
1323
1324
0
        if (status != 0xffffffff) {
1325
#ifdef HAVE_LIBKRB5
1326
                /*
1327
                 * A krb5 zero-copy READ still has its data on the socket,
1328
                 * headed for the caller's buffers, so a failed verifier can
1329
                 * only be reported once that has been read.
1330
                 */
1331
                if (status == RPC_STATUS_ERROR && rpc->sec == RPC_SEC_KRB5 &&
1332
                    pdu->in.base &&
1333
                    msg.body.rbody.reply.areply.stat == SUCCESS) {
1334
                        pdu->zero_copy_error = data;
1335
                        pdu->free_pdu = 1;
1336
                        return 0;
1337
                }
1338
#endif /* HAVE_LIBKRB5 */
1339
0
#ifdef HAVE_NFS4_2
1340
0
                if (status == RPC_STATUS_SUCCESS && pdu->nfs4_delay_maxres &&
1341
0
                    nfs4_pdu_retry_delay(rpc, pdu, data)) {
1342
0
                        return 0;
1343
0
                }
1344
0
#endif
1345
0
                pdu->cb(rpc, status, data, pdu->private_data);
1346
0
        }
1347
0
  return 0;
1348
0
}
1349
1350
struct _rpc_msg {
1351
        struct rpc_msg call;
1352
        struct sockaddr_storage udp_src;
1353
};
1354
1355
struct rpc_msg *rpc_copy_deferred_call(struct rpc_context *rpc,
1356
                                       struct rpc_msg *call)
1357
0
{
1358
0
        struct _rpc_msg *c;
1359
1360
0
        c = malloc(sizeof(struct _rpc_msg));
1361
0
        if (c == NULL) {
1362
0
                return NULL;
1363
0
        }
1364
0
        memcpy(c, call, sizeof(struct _rpc_msg));
1365
1366
0
        return &c->call;
1367
0
}
1368
1369
void rpc_free_deferred_call(struct rpc_context *rpc,
1370
                            struct rpc_msg *call)
1371
0
{
1372
0
        free(call);
1373
0
}
1374
1375
static int rpc_send_error_reply(struct rpc_context *rpc,
1376
                                struct rpc_msg *call,
1377
                                enum accept_stat err,
1378
                                int min_vers, int max_vers)
1379
0
{
1380
0
        struct rpc_pdu *pdu;
1381
0
        struct _rpc_msg *c = (struct _rpc_msg *)call;
1382
0
        struct rpc_msg res;
1383
1384
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
1385
1386
0
  memset(&res, 0, sizeof(struct rpc_msg));
1387
0
  res.xid                                      = call->xid;
1388
0
        res.direction                                = REPLY;
1389
0
        res.body.rbody.stat                          = MSG_ACCEPTED;
1390
0
        res.body.rbody.reply.areply.reply_data.mismatch_info.low  = min_vers;
1391
0
        res.body.rbody.reply.areply.reply_data.mismatch_info.high = max_vers;
1392
0
  res.body.rbody.reply.areply.verf             = libnfs_null_auth;
1393
0
  res.body.rbody.reply.areply.stat             = err;
1394
1395
0
        if (rpc->is_udp) {
1396
                /* send the reply back to the client */
1397
0
                memcpy(&rpc->udp_dest, &c->udp_src, sizeof(rpc->udp_dest));
1398
0
        }
1399
1400
0
        pdu  = rpc_allocate_reply_pdu(rpc, &res, 0);
1401
0
        if (pdu == NULL) {
1402
0
                rpc_set_error(rpc, "Failed to send error_reply: %s",
1403
0
                              rpc_get_error(rpc));
1404
0
                return -1;
1405
0
        }
1406
0
        return rpc_queue_pdu(rpc, pdu);
1407
0
}
1408
1409
int rpc_send_reply(struct rpc_context *rpc,
1410
                   struct rpc_msg *call,
1411
                   void *reply,
1412
                   zdrproc_t encode_fn,
1413
                   int alloc_hint)
1414
0
{
1415
0
        struct rpc_pdu *pdu;
1416
0
        struct _rpc_msg *c = (struct _rpc_msg *)call;
1417
0
        struct rpc_msg res;
1418
1419
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
1420
1421
0
  memset(&res, 0, sizeof(struct rpc_msg));
1422
0
  res.xid                                      = call->xid;
1423
0
        res.direction                                = REPLY;
1424
0
        res.body.rbody.stat                          = MSG_ACCEPTED;
1425
0
  res.body.rbody.reply.areply.verf             = libnfs_null_auth;
1426
0
  res.body.rbody.reply.areply.stat             = SUCCESS;
1427
1428
0
        res.body.rbody.reply.areply.reply_data.results.where = reply;
1429
0
  res.body.rbody.reply.areply.reply_data.results.proc  = encode_fn;
1430
1431
0
        if (rpc->is_udp) {
1432
                /* send the reply back to the client */
1433
0
                memcpy(&rpc->udp_dest, &c->udp_src, sizeof(rpc->udp_dest));
1434
0
        }
1435
1436
0
        pdu  = rpc_allocate_reply_pdu(rpc, &res, alloc_hint);
1437
0
        if (pdu == NULL) {
1438
0
                rpc_set_error(rpc, "Failed to send error_reply: %s",
1439
0
                              rpc_get_error(rpc));
1440
0
                return -1;
1441
0
        }
1442
1443
0
        return rpc_queue_pdu(rpc, pdu);
1444
0
}
1445
1446
static int rpc_process_call(struct rpc_context *rpc, ZDR *zdr)
1447
0
{
1448
0
  struct _rpc_msg c;
1449
0
        struct rpc_endpoint *endpoint;
1450
0
        int i, min_version = 0, max_version = 0, found_program = 0;
1451
1452
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
1453
1454
0
        memset(&c.call, 0, sizeof(struct rpc_msg));
1455
0
        if (rpc->is_udp) {
1456
0
                memcpy(&c.udp_src, &rpc->udp_src, sizeof(rpc->udp_src));
1457
0
        }
1458
0
  if (zdr_callmsg(rpc, zdr, &c.call) == 0) {
1459
0
    rpc_set_error(rpc, "Failed to decode CALL message. %s",
1460
0
                              rpc_get_error(rpc));
1461
0
                return rpc_send_error_reply(rpc, &c.call, GARBAGE_ARGS, 0, 0);
1462
0
        }
1463
0
        for (endpoint = rpc->endpoints; endpoint; endpoint = endpoint->next) {
1464
0
                if (c.call.body.cbody.prog == endpoint->program) {
1465
0
                        if (!found_program) {
1466
0
                                min_version = max_version = endpoint->version;
1467
0
                        }
1468
0
                        if (endpoint->version < min_version) {
1469
0
                                min_version = endpoint->version;
1470
0
                        }
1471
0
                        if (endpoint->version > max_version) {
1472
0
                                max_version = endpoint->version;
1473
0
                        }
1474
0
                        found_program = 1;
1475
0
                        if (c.call.body.cbody.vers == endpoint->version) {
1476
0
                                break;
1477
0
                        }
1478
0
                }
1479
0
        }
1480
0
        if (endpoint == NULL) {
1481
0
    rpc_set_error(rpc, "No endpoint found for CALL "
1482
0
                              "program:0x%08x version:%d\n",
1483
0
                              (int)c.call.body.cbody.prog,
1484
0
                              (int)c.call.body.cbody.vers);
1485
0
                if (!found_program) {
1486
0
                        return rpc_send_error_reply(rpc, &c.call, PROG_UNAVAIL,
1487
0
                                                    0, 0);
1488
0
                }
1489
0
                return rpc_send_error_reply(rpc, &c.call, PROG_MISMATCH,
1490
0
                                            min_version, max_version);
1491
0
        }
1492
0
        for (i = 0; i < endpoint->num_procs; i++) {
1493
0
                if (endpoint->procs[i].proc == c.call.body.cbody.proc) {
1494
0
                        if (endpoint->procs[i].decode_buf_size) {
1495
0
                                c.call.body.cbody.args = zdr_malloc(zdr, endpoint->procs[i].decode_buf_size);
1496
0
                                if (c.call.body.cbody.args == NULL) {
1497
0
                                        rpc_set_error(rpc, "Out of memory: Failed to allocate call arguments");
1498
0
                                        return rpc_send_error_reply(rpc, &c.call, SYSTEM_ERR, 0, 0);
1499
0
                                }
1500
0
                                memset(c.call.body.cbody.args, 0, endpoint->procs[i].decode_buf_size);
1501
0
                        }
1502
0
                        if (!endpoint->procs[i].decode_fn(zdr, c.call.body.cbody.args)) {
1503
0
                                rpc_set_error(rpc, "Failed to unmarshall "
1504
0
                                              "call payload");
1505
0
                                return rpc_send_error_reply(rpc, &c.call, GARBAGE_ARGS, 0 ,0);
1506
0
                        }
1507
0
                        return endpoint->procs[i].func(rpc, &c.call, endpoint->procs[i].opaque);
1508
0
                }
1509
0
        }
1510
1511
0
        return rpc_send_error_reply(rpc, &c.call, PROC_UNAVAIL, 0 ,0);
1512
0
}
1513
1514
static struct rpc_pdu *rpc_find_pdu_in(struct rpc_context *rpc, uint32_t xid,
1515
                                       int sent_only, int probe_only)
1516
0
{
1517
0
  struct rpc_pdu *pdu, *prev_pdu;
1518
0
  struct rpc_queue *q;
1519
0
  unsigned int hash;
1520
1521
#ifdef HAVE_MULTITHREADING
1522
        if (rpc->multithreading_enabled) {
1523
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
1524
        }
1525
#endif /* HAVE_MULTITHREADING */
1526
1527
        /*
1528
         * A pdu is on exactly one of these two queues, so which is searched
1529
         * first only decides how much work a lookup costs, never what it
1530
         * finds.
1531
         *
1532
         * The pdus we are waiting for replies to are the hashed ones, and
1533
         * that is what a reply almost always matches, so look there first.
1534
         * The outqueue is a flat list and can be long, since an NFSv4.2
1535
         * session lets an application queue far more requests than the
1536
         * session has slots to carry at once; scanning it for every reply
1537
         * would make replies cost O(queue length).
1538
         *
1539
         * Follow the hash chain. Linear traverse of a singly-linked list, but
1540
         * track the previous entry for optimised removal.
1541
         */
1542
0
  hash = rpc_hash_xid(rpc, xid);
1543
0
  q = &rpc->waitpdu[hash];
1544
0
  prev_pdu = NULL;
1545
0
  for (pdu=q->head; pdu; pdu=pdu->next) {
1546
0
    if (pdu->xid != xid
1547
#ifdef HAVE_TLS
1548
        || (probe_only && !pdu->expect_starttls)
1549
#endif
1550
0
        ) {
1551
0
      prev_pdu = pdu;
1552
0
      continue;
1553
0
    }
1554
0
    if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
1555
      /* Singly-linked but we track head and tail */
1556
0
      if (pdu == q->head)
1557
0
        q->head = pdu->next;
1558
0
      if (pdu == q->tail)
1559
0
        q->tail = prev_pdu;
1560
0
      if (prev_pdu != NULL)
1561
0
        prev_pdu->next = pdu->next;
1562
0
      rpc->waitpdu_len--;
1563
0
    }
1564
0
                break;
1565
0
        }
1566
0
        if (pdu || sent_only) {
1567
0
                goto finished;
1568
0
        }
1569
1570
        /*
1571
         * Not one we are waiting on. It may still be queued but unsent, or
1572
         * sent and then requeued for retransmit after a reconnect.
1573
         */
1574
0
  q = &rpc->outqueue;
1575
0
  prev_pdu = NULL;
1576
0
  for (pdu=q->head; pdu; pdu=pdu->next) {
1577
0
    if (pdu->xid != xid) {
1578
0
      prev_pdu = pdu;
1579
0
      continue;
1580
0
    }
1581
0
    if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
1582
      /* Singly-linked but we track head and tail */
1583
0
      if (pdu == q->head)
1584
0
        q->head = pdu->next;
1585
0
      if (pdu == q->tail)
1586
0
        q->tail = prev_pdu;
1587
      /*
1588
       * tailp terminates the high priority run, so once it
1589
       * goes the run ends at its predecessor, if that is
1590
       * itself high priority.
1591
       */
1592
0
      if (pdu == q->tailp)
1593
0
        q->tailp = (prev_pdu && prev_pdu->is_high_prio) ?
1594
0
          prev_pdu : NULL;
1595
0
      if (prev_pdu != NULL)
1596
0
        prev_pdu->next = pdu->next;
1597
0
      assert(rpc->stats.outqueue_len > 0);
1598
0
      rpc->stats.outqueue_len--;
1599
0
    }
1600
0
                break;
1601
0
        }
1602
        
1603
0
 finished:
1604
0
#ifdef HAVE_NFS4_2
1605
0
        if (!pdu && !sent_only) {
1606
0
                for (pdu = rpc->nfs4_delay_queue.head; pdu; pdu = pdu->next) {
1607
0
                        if (pdu->xid == xid) {
1608
0
                                rpc_remove_pdu_from_queue(&rpc->nfs4_delay_queue, pdu);
1609
0
                                rpc->nfs4_delay_queue_len--;
1610
0
                                break;
1611
0
                        }
1612
0
                }
1613
0
        }
1614
0
#endif
1615
0
        if (pdu) {
1616
0
                pdu->next = NULL;
1617
0
        }
1618
1619
#ifdef HAVE_MULTITHREADING
1620
        if (rpc->multithreading_enabled) {
1621
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
1622
        }
1623
#endif /* HAVE_MULTITHREADING */
1624
1625
0
        return pdu;
1626
0
}
1627
1628
struct rpc_pdu *rpc_find_pdu(struct rpc_context *rpc, uint32_t xid)
1629
0
{
1630
0
        return rpc_find_pdu_in(rpc, xid, 0, 0);
1631
0
}
1632
1633
/* Until TLS is up the only reply we take is the one to the AUTH_TLS probe. */
1634
struct rpc_pdu *rpc_find_reply_pdu(struct rpc_context *rpc, uint32_t xid)
1635
0
{
1636
#ifdef HAVE_TLS
1637
        if (rpc->use_tls &&
1638
            rpc->tls_context.state != TLS_HANDSHAKE_COMPLETED) {
1639
                return rpc_find_pdu_in(rpc, xid, 1, 1);
1640
        }
1641
#endif /* HAVE_TLS */
1642
0
        return rpc_find_pdu_in(rpc, xid, 0, 0);
1643
0
}
1644
1645
/* Unsent pdus are left alone: the outqueue head may be half written. */
1646
int rpc_cancel_sent_pdu(struct rpc_context *rpc, uint32_t xid)
1647
0
{
1648
0
        struct rpc_pdu *pdu;
1649
1650
0
        pdu = rpc_find_pdu_in(rpc, xid, 1, 0);
1651
0
        if (pdu) {
1652
0
                rpc_free_pdu(rpc, pdu);
1653
0
                return 0;
1654
0
        }
1655
1656
0
        return -ENOENT;
1657
0
}
1658
1659
int rpc_cancel_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
1660
0
{
1661
        /*
1662
         * Use rpc_find_pdu() to locate it and remove it from the input list.
1663
         */
1664
0
        pdu = rpc_find_pdu(rpc, pdu->xid);
1665
0
        if (pdu) {
1666
0
                rpc_free_pdu(rpc, pdu);
1667
0
                return 0;
1668
0
        }
1669
1670
0
        return -ENOENT;
1671
0
}
1672
1673
int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
1674
0
{
1675
0
  ZDR zdr;
1676
1677
0
  assert(rpc->magic == RPC_CONTEXT_MAGIC);
1678
1679
0
  memset(&zdr, 0, sizeof(ZDR));
1680
1681
0
  zdrmem_create(&zdr, buf, size, ZDR_DECODE);
1682
0
        if (rpc->is_server_context) {
1683
0
                int ret;
1684
1685
0
                ret = rpc_process_call(rpc, &zdr);
1686
0
                zdr_destroy(&zdr);
1687
1688
0
                return ret;
1689
0
        }
1690
1691
#ifdef HAVE_LIBKRB5
1692
        /*
1693
         * For KRB5I/KRB5P and iovectors, i.e. NFS[34]READ we
1694
         * need to use a ZDR that hangs off the PDU so we can
1695
         * access the ZDR and its buffers to manually copy
1696
         * data into the iovectors.
1697
         */
1698
        if (rpc->pdu->zero_copy_iov &&
1699
            (rpc->sec == RPC_SEC_KRB5I || rpc->sec == RPC_SEC_KRB5P)) {
1700
                zdr_destroy(&rpc->pdu->zdr);
1701
                zdrmem_create(&rpc->pdu->zdr, buf, size, ZDR_DECODE);
1702
                if (rpc_process_reply(rpc, &rpc->pdu->zdr) != 0) {
1703
                        rpc_set_error(rpc, "rpc_procdess_reply failed (for krb5 read)");
1704
                }
1705
                /*
1706
                 * rpc_process_reply() has completed this pdu. pdu->zdr
1707
                 * holds what the decode allocated and is freed with the
1708
                 * pdu, so it must not be overwritten below.
1709
                 */
1710
                return 0;
1711
        }
1712
#endif /* HAVE_LIBKRB5 */
1713
0
        if (rpc_process_reply(rpc, &zdr) != 0) {
1714
0
                rpc_set_error(rpc, "rpc_procdess_reply failed");
1715
0
        }
1716
1717
        /*
1718
         * A zero-copy READ still has to hand its payload to the caller, so the
1719
         * ZDR has to survive this function for the copy in READ_PAYLOAD. That
1720
         * is true whether the reply arrived as one record or was reassembled
1721
         * from fragments: in the latter case the ZDR points into the
1722
         * reassembly buffer, which is live until payload_finished frees it.
1723
         * Excluding the reassembled case here left free_zdr clear, the copy
1724
         * skipped, and the caller's buffer untouched while the callback still
1725
         * reported success.
1726
         */
1727
0
        if (rpc->pdu && rpc->pdu->in.base) {
1728
0
                memcpy(&rpc->pdu->zdr, &zdr, sizeof(zdr));
1729
0
                rpc->pdu->free_zdr = 1;
1730
0
        } else {
1731
0
                zdr_destroy(&zdr);
1732
0
        }
1733
0
        return 0;
1734
0
}
1735
1736
bool_t rpc_pdu_is_retransmitted(struct rpc_pdu *pdu)
1737
0
{
1738
0
        return pdu->is_retransmitted;
1739
0
}
1740
1741
uint32_t rpc_pdu_get_req_size(struct rpc_pdu *pdu)
1742
0
{
1743
0
        return pdu->req_size;
1744
0
}
1745
1746
uint32_t rpc_pdu_get_resp_size(struct rpc_pdu *pdu)
1747
0
{
1748
0
        return pdu->resp_size;
1749
0
}
1750
1751
uint64_t rpc_pdu_get_dispatch_usecs(struct rpc_pdu *pdu)
1752
0
{
1753
0
#ifdef HAVE_CLOCK_GETTIME
1754
0
        return pdu->dispatch_usecs;
1755
#else
1756
        return 0;
1757
#endif
1758
0
}