1// SPDX-License-Identifier: GPL-2.0 2/* 3 * linux/fs/nfs/callback_xdr.c 4 * 5 * Copyright (C) 2004 Trond Myklebust 6 * 7 * NFSv4 callback encode/decode procedures 8 */ 9#include <linux/kernel.h> 10#include <linux/sunrpc/svc.h> 11#include <linux/nfs4.h> 12#include <linux/nfs_fs.h> 13#include <linux/ratelimit.h> 14#include <linux/printk.h> 15#include <linux/slab.h> 16#include <linux/sunrpc/bc_xprt.h> 17#include "nfs4_fs.h" 18#include "callback.h" 19#include "internal.h" 20#include "nfs4session.h" 21#include "nfs4trace.h" 22 23#define CB_OP_TAGLEN_MAXSZ (512) 24#define CB_OP_HDR_RES_MAXSZ (2 * 4) // opcode, status 25#define CB_OP_GETATTR_BITMAP_MAXSZ (4 * 4) // bitmap length, 3 bitmaps 26#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 27 CB_OP_GETATTR_BITMAP_MAXSZ + \ 28 /* change, size, ctime, mtime */\ 29 (2 + 2 + 3 + 3) * 4) 30#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 31 32#if defined(CONFIG_NFS_V4_1) 33#define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 34#define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 35#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ 36 NFS4_MAX_SESSIONID_LEN + \ 37 (1 + 3) * 4) // seqid, 3 slotids 38#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 39#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 40#define CB_OP_NOTIFY_LOCK_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 41#endif /* CONFIG_NFS_V4_1 */ 42#ifdef CONFIG_NFS_V4_2 43#define CB_OP_OFFLOAD_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) 44#endif /* CONFIG_NFS_V4_2 */ 45 46#define NFSDBG_FACILITY NFSDBG_CALLBACK 47 48/* Internal error code */ 49#define NFS4ERR_RESOURCE_HDR 11050 50 51struct callback_op { 52 __be32 (*process_op)(void *, void *, struct cb_process_state *); 53 __be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *); 54 __be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *, 55 const void *); 56 long res_maxsize; 57}; 58 59static struct callback_op callback_ops[]; 60 61static __be32 nfs4_callback_null(struct svc_rqst *rqstp) 62{ 63 return htonl(NFS4_OK); 64} 65 66static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p) 67{ 68 return xdr_argsize_check(rqstp, p); 69} 70 71static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p) 72{ 73 return xdr_ressize_check(rqstp, p); 74} 75 76static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, 77 const char **str, size_t maxlen) 78{ 79 ssize_t err; 80 81 err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen); 82 if (err < 0) 83 return cpu_to_be32(NFS4ERR_RESOURCE); 84 *len = err; 85 return 0; 86} 87 88static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh) 89{ 90 __be32 *p; 91 92 p = xdr_inline_decode(xdr, 4); 93 if (unlikely(p == NULL)) 94 return htonl(NFS4ERR_RESOURCE); 95 fh->size = ntohl(*p); 96 if (fh->size > NFS4_FHSIZE) 97 return htonl(NFS4ERR_BADHANDLE); 98 p = xdr_inline_decode(xdr, fh->size); 99 if (unlikely(p == NULL)) 100 return htonl(NFS4ERR_RESOURCE); 101 memcpy(&fh->data[0], p, fh->size); 102 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size); 103 return 0; 104} 105 106static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap) 107{ 108 __be32 *p; 109 unsigned int attrlen; 110 111 p = xdr_inline_decode(xdr, 4); 112 if (unlikely(p == NULL)) 113 return htonl(NFS4ERR_RESOURCE); 114 attrlen = ntohl(*p); 115 p = xdr_inline_decode(xdr, attrlen << 2); 116 if (unlikely(p == NULL)) 117 return htonl(NFS4ERR_RESOURCE); 118 if (likely(attrlen > 0)) 119 bitmap[0] = ntohl(*p++); 120 if (attrlen > 1) 121 bitmap[1] = ntohl(*p); 122 return 0; 123} 124 125static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 126{ 127 __be32 *p; 128 129 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE); 130 if (unlikely(p == NULL)) 131 return htonl(NFS4ERR_RESOURCE); 132 memcpy(stateid->data, p, NFS4_STATEID_SIZE); 133 return 0; 134} 135 136static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 137{ 138 stateid->type = NFS4_DELEGATION_STATEID_TYPE; 139 return decode_stateid(xdr, stateid); 140} 141 142static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr) 143{ 144 __be32 *p; 145 __be32 status; 146 147 status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ); 148 if (unlikely(status != 0)) 149 return status; 150 p = xdr_inline_decode(xdr, 12); 151 if (unlikely(p == NULL)) 152 return htonl(NFS4ERR_RESOURCE); 153 hdr->minorversion = ntohl(*p++); 154 /* Check for minor version support */ 155 if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) { 156 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */ 157 } else { 158 pr_warn_ratelimited("NFS: %s: NFSv4 server callback with " 159 "illegal minor version %u!\n", 160 __func__, hdr->minorversion); 161 return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 162 } 163 hdr->nops = ntohl(*p); 164 return 0; 165} 166 167static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) 168{ 169 __be32 *p; 170 p = xdr_inline_decode(xdr, 4); 171 if (unlikely(p == NULL)) 172 return htonl(NFS4ERR_RESOURCE_HDR); 173 *op = ntohl(*p); 174 return 0; 175} 176 177static __be32 decode_getattr_args(struct svc_rqst *rqstp, 178 struct xdr_stream *xdr, void *argp) 179{ 180 struct cb_getattrargs *args = argp; 181 __be32 status; 182 183 status = decode_fh(xdr, &args->fh); 184 if (unlikely(status != 0)) 185 return status; 186 return decode_bitmap(xdr, args->bitmap); 187} 188 189static __be32 decode_recall_args(struct svc_rqst *rqstp, 190 struct xdr_stream *xdr, void *argp) 191{ 192 struct cb_recallargs *args = argp; 193 __be32 *p; 194 __be32 status; 195 196 status = decode_delegation_stateid(xdr, &args->stateid); 197 if (unlikely(status != 0)) 198 return status; 199 p = xdr_inline_decode(xdr, 4); 200 if (unlikely(p == NULL)) 201 return htonl(NFS4ERR_RESOURCE); 202 args->truncate = ntohl(*p); 203 return decode_fh(xdr, &args->fh); 204} 205 206#if defined(CONFIG_NFS_V4_1) 207static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 208{ 209 stateid->type = NFS4_LAYOUT_STATEID_TYPE; 210 return decode_stateid(xdr, stateid); 211} 212 213static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp, 214 struct xdr_stream *xdr, void *argp) 215{ 216 struct cb_layoutrecallargs *args = argp; 217 __be32 *p; 218 __be32 status = 0; 219 uint32_t iomode; 220 221 p = xdr_inline_decode(xdr, 4 * sizeof(uint32_t)); 222 if (unlikely(p == NULL)) 223 return htonl(NFS4ERR_BADXDR); 224 225 args->cbl_layout_type = ntohl(*p++); 226 /* Depite the spec's xdr, iomode really belongs in the FILE switch, 227 * as it is unusable and ignored with the other types. 228 */ 229 iomode = ntohl(*p++); 230 args->cbl_layoutchanged = ntohl(*p++); 231 args->cbl_recall_type = ntohl(*p++); 232 233 if (args->cbl_recall_type == RETURN_FILE) { 234 args->cbl_range.iomode = iomode; 235 status = decode_fh(xdr, &args->cbl_fh); 236 if (unlikely(status != 0)) 237 return status; 238 239 p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t)); 240 if (unlikely(p == NULL)) 241 return htonl(NFS4ERR_BADXDR); 242 p = xdr_decode_hyper(p, &args->cbl_range.offset); 243 p = xdr_decode_hyper(p, &args->cbl_range.length); 244 return decode_layout_stateid(xdr, &args->cbl_stateid); 245 } else if (args->cbl_recall_type == RETURN_FSID) { 246 p = xdr_inline_decode(xdr, 2 * sizeof(uint64_t)); 247 if (unlikely(p == NULL)) 248 return htonl(NFS4ERR_BADXDR); 249 p = xdr_decode_hyper(p, &args->cbl_fsid.major); 250 p = xdr_decode_hyper(p, &args->cbl_fsid.minor); 251 } else if (args->cbl_recall_type != RETURN_ALL) 252 return htonl(NFS4ERR_BADXDR); 253 return 0; 254} 255 256static 257__be32 decode_devicenotify_args(struct svc_rqst *rqstp, 258 struct xdr_stream *xdr, 259 void *argp) 260{ 261 struct cb_devicenotifyargs *args = argp; 262 uint32_t tmp, n, i; 263 __be32 *p; 264 __be32 status = 0; 265 266 /* Num of device notifications */ 267 p = xdr_inline_decode(xdr, sizeof(uint32_t)); 268 if (unlikely(p == NULL)) { 269 status = htonl(NFS4ERR_BADXDR); 270 goto out; 271 } 272 n = ntohl(*p++); 273 if (n == 0) 274 goto out; 275 276 args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL); 277 if (!args->devs) { 278 status = htonl(NFS4ERR_DELAY); 279 goto out; 280 } 281 282 /* Decode each dev notification */ 283 for (i = 0; i < n; i++) { 284 struct cb_devicenotifyitem *dev = &args->devs[i]; 285 286 p = xdr_inline_decode(xdr, (4 * sizeof(uint32_t)) + 287 NFS4_DEVICEID4_SIZE); 288 if (unlikely(p == NULL)) { 289 status = htonl(NFS4ERR_BADXDR); 290 goto err; 291 } 292 293 tmp = ntohl(*p++); /* bitmap size */ 294 if (tmp != 1) { 295 status = htonl(NFS4ERR_INVAL); 296 goto err; 297 } 298 dev->cbd_notify_type = ntohl(*p++); 299 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE && 300 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) { 301 status = htonl(NFS4ERR_INVAL); 302 goto err; 303 } 304 305 tmp = ntohl(*p++); /* opaque size */ 306 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) && 307 (tmp != NFS4_DEVICEID4_SIZE + 8)) || 308 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) && 309 (tmp != NFS4_DEVICEID4_SIZE + 4))) { 310 status = htonl(NFS4ERR_INVAL); 311 goto err; 312 } 313 dev->cbd_layout_type = ntohl(*p++); 314 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE); 315 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); 316 317 if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) { 318 p = xdr_inline_decode(xdr, sizeof(uint32_t)); 319 if (unlikely(p == NULL)) { 320 status = htonl(NFS4ERR_BADXDR); 321 goto err; 322 } 323 dev->cbd_immediate = ntohl(*p++); 324 } else { 325 dev->cbd_immediate = 0; 326 } 327 328 dprintk("%s: type %d layout 0x%x immediate %d\n", 329 __func__, dev->cbd_notify_type, dev->cbd_layout_type, 330 dev->cbd_immediate); 331 } 332 args->ndevs = n; 333 dprintk("%s: ndevs %d\n", __func__, args->ndevs); 334 return 0; 335err: 336 kfree(args->devs); 337out: 338 args->devs = NULL; 339 args->ndevs = 0; 340 dprintk("%s: status %d ndevs %d\n", 341 __func__, ntohl(status), args->ndevs); 342 return status; 343} 344 345static __be32 decode_sessionid(struct xdr_stream *xdr, 346 struct nfs4_sessionid *sid) 347{ 348 __be32 *p; 349 350 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN); 351 if (unlikely(p == NULL)) 352 return htonl(NFS4ERR_RESOURCE); 353 354 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN); 355 return 0; 356} 357 358static __be32 decode_rc_list(struct xdr_stream *xdr, 359 struct referring_call_list *rc_list) 360{ 361 __be32 *p; 362 int i; 363 __be32 status; 364 365 status = decode_sessionid(xdr, &rc_list->rcl_sessionid); 366 if (status) 367 goto out; 368 369 status = htonl(NFS4ERR_RESOURCE); 370 p = xdr_inline_decode(xdr, sizeof(uint32_t)); 371 if (unlikely(p == NULL)) 372 goto out; 373 374 rc_list->rcl_nrefcalls = ntohl(*p++); 375 if (rc_list->rcl_nrefcalls) { 376 p = xdr_inline_decode(xdr, 377 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t)); 378 if (unlikely(p == NULL)) 379 goto out; 380 rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls, 381 sizeof(*rc_list->rcl_refcalls), 382 GFP_KERNEL); 383 if (unlikely(rc_list->rcl_refcalls == NULL)) 384 goto out; 385 for (i = 0; i < rc_list->rcl_nrefcalls; i++) { 386 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++); 387 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++); 388 } 389 } 390 status = 0; 391 392out: 393 return status; 394} 395 396static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp, 397 struct xdr_stream *xdr, 398 void *argp) 399{ 400 struct cb_sequenceargs *args = argp; 401 __be32 *p; 402 int i; 403 __be32 status; 404 405 status = decode_sessionid(xdr, &args->csa_sessionid); 406 if (status) 407 return status; 408 409 p = xdr_inline_decode(xdr, 5 * sizeof(uint32_t)); 410 if (unlikely(p == NULL)) 411 return htonl(NFS4ERR_RESOURCE); 412 413 args->csa_addr = svc_addr(rqstp); 414 args->csa_sequenceid = ntohl(*p++); 415 args->csa_slotid = ntohl(*p++); 416 args->csa_highestslotid = ntohl(*p++); 417 args->csa_cachethis = ntohl(*p++); 418 args->csa_nrclists = ntohl(*p++); 419 args->csa_rclists = NULL; 420 if (args->csa_nrclists) { 421 args->csa_rclists = kmalloc_array(args->csa_nrclists, 422 sizeof(*args->csa_rclists), 423 GFP_KERNEL); 424 if (unlikely(args->csa_rclists == NULL)) 425 return htonl(NFS4ERR_RESOURCE); 426 427 for (i = 0; i < args->csa_nrclists; i++) { 428 status = decode_rc_list(xdr, &args->csa_rclists[i]); 429 if (status) { 430 args->csa_nrclists = i; 431 goto out_free; 432 } 433 } 434 } 435 return 0; 436 437out_free: 438 for (i = 0; i < args->csa_nrclists; i++) 439 kfree(args->csa_rclists[i].rcl_refcalls); 440 kfree(args->csa_rclists); 441 return status; 442} 443 444static __be32 decode_recallany_args(struct svc_rqst *rqstp, 445 struct xdr_stream *xdr, 446 void *argp) 447{ 448 struct cb_recallanyargs *args = argp; 449 uint32_t bitmap[2]; 450 __be32 *p, status; 451 452 p = xdr_inline_decode(xdr, 4); 453 if (unlikely(p == NULL)) 454 return htonl(NFS4ERR_BADXDR); 455 args->craa_objs_to_keep = ntohl(*p++); 456 status = decode_bitmap(xdr, bitmap); 457 if (unlikely(status)) 458 return status; 459 args->craa_type_mask = bitmap[0]; 460 461 return 0; 462} 463 464static __be32 decode_recallslot_args(struct svc_rqst *rqstp, 465 struct xdr_stream *xdr, 466 void *argp) 467{ 468 struct cb_recallslotargs *args = argp; 469 __be32 *p; 470 471 p = xdr_inline_decode(xdr, 4); 472 if (unlikely(p == NULL)) 473 return htonl(NFS4ERR_BADXDR); 474 args->crsa_target_highest_slotid = ntohl(*p++); 475 return 0; 476} 477 478static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args) 479{ 480 __be32 *p; 481 unsigned int len; 482 483 p = xdr_inline_decode(xdr, 12); 484 if (unlikely(p == NULL)) 485 return htonl(NFS4ERR_BADXDR); 486 487 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid); 488 len = be32_to_cpu(*p); 489 490 p = xdr_inline_decode(xdr, len); 491 if (unlikely(p == NULL)) 492 return htonl(NFS4ERR_BADXDR); 493 494 /* Only try to decode if the length is right */ 495 if (len == 20) { 496 p += 2; /* skip "lock id:" */ 497 args->cbnl_owner.s_dev = be32_to_cpu(*p++); 498 xdr_decode_hyper(p, &args->cbnl_owner.id); 499 args->cbnl_valid = true; 500 } else { 501 args->cbnl_owner.s_dev = 0; 502 args->cbnl_owner.id = 0; 503 args->cbnl_valid = false; 504 } 505 return 0; 506} 507 508static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, 509 struct xdr_stream *xdr, void *argp) 510{ 511 struct cb_notify_lock_args *args = argp; 512 __be32 status; 513 514 status = decode_fh(xdr, &args->cbnl_fh); 515 if (unlikely(status != 0)) 516 return status; 517 return decode_lockowner(xdr, args); 518} 519 520#endif /* CONFIG_NFS_V4_1 */ 521#ifdef CONFIG_NFS_V4_2 522static __be32 decode_write_response(struct xdr_stream *xdr, 523 struct cb_offloadargs *args) 524{ 525 __be32 *p; 526 527 /* skip the always zero field */ 528 p = xdr_inline_decode(xdr, 4); 529 if (unlikely(!p)) 530 goto out; 531 p++; 532 533 /* decode count, stable_how, verifier */ 534 p = xdr_inline_decode(xdr, 8 + 4); 535 if (unlikely(!p)) 536 goto out; 537 p = xdr_decode_hyper(p, &args->wr_count); 538 args->wr_writeverf.committed = be32_to_cpup(p); 539 p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE); 540 if (likely(p)) { 541 memcpy(&args->wr_writeverf.verifier.data[0], p, 542 NFS4_VERIFIER_SIZE); 543 return 0; 544 } 545out: 546 return htonl(NFS4ERR_RESOURCE); 547} 548 549static __be32 decode_offload_args(struct svc_rqst *rqstp, 550 struct xdr_stream *xdr, 551 void *data) 552{ 553 struct cb_offloadargs *args = data; 554 __be32 *p; 555 __be32 status; 556 557 /* decode fh */ 558 status = decode_fh(xdr, &args->coa_fh); 559 if (unlikely(status != 0)) 560 return status; 561 562 /* decode stateid */ 563 status = decode_stateid(xdr, &args->coa_stateid); 564 if (unlikely(status != 0)) 565 return status; 566 567 /* decode status */ 568 p = xdr_inline_decode(xdr, 4); 569 if (unlikely(!p)) 570 goto out; 571 args->error = ntohl(*p++); 572 if (!args->error) { 573 status = decode_write_response(xdr, args); 574 if (unlikely(status != 0)) 575 return status; 576 } else { 577 p = xdr_inline_decode(xdr, 8); 578 if (unlikely(!p)) 579 goto out; 580 p = xdr_decode_hyper(p, &args->wr_count); 581 } 582 return 0; 583out: 584 return htonl(NFS4ERR_RESOURCE); 585} 586#endif /* CONFIG_NFS_V4_2 */ 587static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) 588{ 589 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0)) 590 return cpu_to_be32(NFS4ERR_RESOURCE); 591 return 0; 592} 593 594static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz) 595{ 596 if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0) 597 return cpu_to_be32(NFS4ERR_RESOURCE); 598 return 0; 599} 600 601static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) 602{ 603 __be32 *p; 604 605 if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) 606 return 0; 607 p = xdr_reserve_space(xdr, 8); 608 if (unlikely(!p)) 609 return htonl(NFS4ERR_RESOURCE); 610 p = xdr_encode_hyper(p, change); 611 return 0; 612} 613 614static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) 615{ 616 __be32 *p; 617 618 if (!(bitmap[0] & FATTR4_WORD0_SIZE)) 619 return 0; 620 p = xdr_reserve_space(xdr, 8); 621 if (unlikely(!p)) 622 return htonl(NFS4ERR_RESOURCE); 623 p = xdr_encode_hyper(p, size); 624 return 0; 625} 626 627static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec64 *time) 628{ 629 __be32 *p; 630 631 p = xdr_reserve_space(xdr, 12); 632 if (unlikely(!p)) 633 return htonl(NFS4ERR_RESOURCE); 634 p = xdr_encode_hyper(p, time->tv_sec); 635 *p = htonl(time->tv_nsec); 636 return 0; 637} 638 639static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time) 640{ 641 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) 642 return 0; 643 return encode_attr_time(xdr,time); 644} 645 646static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time) 647{ 648 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) 649 return 0; 650 return encode_attr_time(xdr,time); 651} 652 653static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) 654{ 655 __be32 status; 656 657 hdr->status = xdr_reserve_space(xdr, 4); 658 if (unlikely(hdr->status == NULL)) 659 return htonl(NFS4ERR_RESOURCE); 660 status = encode_string(xdr, hdr->taglen, hdr->tag); 661 if (unlikely(status != 0)) 662 return status; 663 hdr->nops = xdr_reserve_space(xdr, 4); 664 if (unlikely(hdr->nops == NULL)) 665 return htonl(NFS4ERR_RESOURCE); 666 return 0; 667} 668 669static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) 670{ 671 __be32 *p; 672 673 p = xdr_reserve_space(xdr, 8); 674 if (unlikely(p == NULL)) 675 return htonl(NFS4ERR_RESOURCE_HDR); 676 *p++ = htonl(op); 677 *p = res; 678 return 0; 679} 680 681static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, 682 const void *resp) 683{ 684 const struct cb_getattrres *res = resp; 685 __be32 *savep = NULL; 686 __be32 status = res->status; 687 688 if (unlikely(status != 0)) 689 goto out; 690 status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap)); 691 if (unlikely(status != 0)) 692 goto out; 693 status = cpu_to_be32(NFS4ERR_RESOURCE); 694 savep = xdr_reserve_space(xdr, sizeof(*savep)); 695 if (unlikely(!savep)) 696 goto out; 697 status = encode_attr_change(xdr, res->bitmap, res->change_attr); 698 if (unlikely(status != 0)) 699 goto out; 700 status = encode_attr_size(xdr, res->bitmap, res->size); 701 if (unlikely(status != 0)) 702 goto out; 703 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); 704 if (unlikely(status != 0)) 705 goto out; 706 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); 707 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); 708out: 709 return status; 710} 711 712#if defined(CONFIG_NFS_V4_1) 713 714static __be32 encode_sessionid(struct xdr_stream *xdr, 715 const struct nfs4_sessionid *sid) 716{ 717 __be32 *p; 718 719 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 720 if (unlikely(p == NULL)) 721 return htonl(NFS4ERR_RESOURCE); 722 723 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN); 724 return 0; 725} 726 727static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp, 728 struct xdr_stream *xdr, 729 const void *resp) 730{ 731 const struct cb_sequenceres *res = resp; 732 __be32 *p; 733 __be32 status = res->csr_status; 734 735 if (unlikely(status != 0)) 736 return status; 737 738 status = encode_sessionid(xdr, &res->csr_sessionid); 739 if (status) 740 return status; 741 742 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t)); 743 if (unlikely(p == NULL)) 744 return htonl(NFS4ERR_RESOURCE); 745 746 *p++ = htonl(res->csr_sequenceid); 747 *p++ = htonl(res->csr_slotid); 748 *p++ = htonl(res->csr_highestslotid); 749 *p++ = htonl(res->csr_target_highestslotid); 750 return 0; 751} 752 753static __be32 754preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 755{ 756 if (op_nr == OP_CB_SEQUENCE) { 757 if (nop != 0) 758 return htonl(NFS4ERR_SEQUENCE_POS); 759 } else { 760 if (nop == 0) 761 return htonl(NFS4ERR_OP_NOT_IN_SESSION); 762 } 763 764 switch (op_nr) { 765 case OP_CB_GETATTR: 766 case OP_CB_RECALL: 767 case OP_CB_SEQUENCE: 768 case OP_CB_RECALL_ANY: 769 case OP_CB_RECALL_SLOT: 770 case OP_CB_LAYOUTRECALL: 771 case OP_CB_NOTIFY_DEVICEID: 772 case OP_CB_NOTIFY_LOCK: 773 *op = &callback_ops[op_nr]; 774 break; 775 776 case OP_CB_NOTIFY: 777 case OP_CB_PUSH_DELEG: 778 case OP_CB_RECALLABLE_OBJ_AVAIL: 779 case OP_CB_WANTS_CANCELLED: 780 return htonl(NFS4ERR_NOTSUPP); 781 782 default: 783 return htonl(NFS4ERR_OP_ILLEGAL); 784 } 785 786 return htonl(NFS_OK); 787} 788 789static void nfs4_callback_free_slot(struct nfs4_session *session, 790 struct nfs4_slot *slot) 791{ 792 struct nfs4_slot_table *tbl = &session->bc_slot_table; 793 794 spin_lock(&tbl->slot_tbl_lock); 795 /* 796 * Let the state manager know callback processing done. 797 * A single slot, so highest used slotid is either 0 or -1 798 */ 799 nfs4_free_slot(tbl, slot); 800 spin_unlock(&tbl->slot_tbl_lock); 801} 802 803static void nfs4_cb_free_slot(struct cb_process_state *cps) 804{ 805 if (cps->slot) { 806 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot); 807 cps->slot = NULL; 808 } 809} 810 811#else /* CONFIG_NFS_V4_1 */ 812 813static __be32 814preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 815{ 816 return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 817} 818 819static void nfs4_cb_free_slot(struct cb_process_state *cps) 820{ 821} 822#endif /* CONFIG_NFS_V4_1 */ 823 824#ifdef CONFIG_NFS_V4_2 825static __be32 826preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 827{ 828 __be32 status = preprocess_nfs41_op(nop, op_nr, op); 829 if (status != htonl(NFS4ERR_OP_ILLEGAL)) 830 return status; 831 832 if (op_nr == OP_CB_OFFLOAD) { 833 *op = &callback_ops[op_nr]; 834 return htonl(NFS_OK); 835 } else 836 return htonl(NFS4ERR_NOTSUPP); 837 return htonl(NFS4ERR_OP_ILLEGAL); 838} 839#else /* CONFIG_NFS_V4_2 */ 840static __be32 841preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 842{ 843 return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 844} 845#endif /* CONFIG_NFS_V4_2 */ 846 847static __be32 848preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op) 849{ 850 switch (op_nr) { 851 case OP_CB_GETATTR: 852 case OP_CB_RECALL: 853 *op = &callback_ops[op_nr]; 854 break; 855 default: 856 return htonl(NFS4ERR_OP_ILLEGAL); 857 } 858 859 return htonl(NFS_OK); 860} 861 862static __be32 process_op(int nop, struct svc_rqst *rqstp, 863 struct xdr_stream *xdr_in, void *argp, 864 struct xdr_stream *xdr_out, void *resp, 865 struct cb_process_state *cps) 866{ 867 struct callback_op *op = &callback_ops[0]; 868 unsigned int op_nr; 869 __be32 status; 870 long maxlen; 871 __be32 res; 872 873 status = decode_op_hdr(xdr_in, &op_nr); 874 if (unlikely(status)) 875 return status; 876 877 switch (cps->minorversion) { 878 case 0: 879 status = preprocess_nfs4_op(op_nr, &op); 880 break; 881 case 1: 882 status = preprocess_nfs41_op(nop, op_nr, &op); 883 break; 884 case 2: 885 status = preprocess_nfs42_op(nop, op_nr, &op); 886 break; 887 default: 888 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH); 889 } 890 891 if (status == htonl(NFS4ERR_OP_ILLEGAL)) 892 op_nr = OP_CB_ILLEGAL; 893 if (status) 894 goto encode_hdr; 895 896 if (cps->drc_status) { 897 status = cps->drc_status; 898 goto encode_hdr; 899 } 900 901 maxlen = xdr_out->end - xdr_out->p; 902 if (maxlen > 0 && maxlen < PAGE_SIZE) { 903 status = op->decode_args(rqstp, xdr_in, argp); 904 if (likely(status == 0)) 905 status = op->process_op(argp, resp, cps); 906 } else 907 status = htonl(NFS4ERR_RESOURCE); 908 909encode_hdr: 910 res = encode_op_hdr(xdr_out, op_nr, status); 911 if (unlikely(res)) 912 return res; 913 if (op->encode_res != NULL && status == 0) 914 status = op->encode_res(rqstp, xdr_out, resp); 915 return status; 916} 917 918/* 919 * Decode, process and encode a COMPOUND 920 */ 921static __be32 nfs4_callback_compound(struct svc_rqst *rqstp) 922{ 923 struct cb_compound_hdr_arg hdr_arg = { 0 }; 924 struct cb_compound_hdr_res hdr_res = { NULL }; 925 struct xdr_stream xdr_in, xdr_out; 926 __be32 *p, status; 927 struct cb_process_state cps = { 928 .drc_status = 0, 929 .clp = NULL, 930 .net = SVC_NET(rqstp), 931 }; 932 unsigned int nops = 0; 933 934 xdr_init_decode(&xdr_in, &rqstp->rq_arg, 935 rqstp->rq_arg.head[0].iov_base, NULL); 936 937 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len); 938 xdr_init_encode(&xdr_out, &rqstp->rq_res, p, NULL); 939 940 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); 941 if (status == htonl(NFS4ERR_RESOURCE)) 942 return rpc_garbage_args; 943 944 if (hdr_arg.minorversion == 0) { 945 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident); 946 if (!cps.clp) { 947 trace_nfs_cb_no_clp(rqstp->rq_xid, hdr_arg.cb_ident); 948 goto out_invalidcred; 949 } 950 if (!check_gss_callback_principal(cps.clp, rqstp)) { 951 trace_nfs_cb_badprinc(rqstp->rq_xid, hdr_arg.cb_ident); 952 nfs_put_client(cps.clp); 953 goto out_invalidcred; 954 } 955 } 956 957 cps.minorversion = hdr_arg.minorversion; 958 hdr_res.taglen = hdr_arg.taglen; 959 hdr_res.tag = hdr_arg.tag; 960 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) { 961 if (cps.clp) 962 nfs_put_client(cps.clp); 963 return rpc_system_err; 964 } 965 while (status == 0 && nops != hdr_arg.nops) { 966 status = process_op(nops, rqstp, &xdr_in, 967 rqstp->rq_argp, &xdr_out, rqstp->rq_resp, 968 &cps); 969 nops++; 970 } 971 972 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return 973 * resource error in cb_compound status without returning op */ 974 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) { 975 status = htonl(NFS4ERR_RESOURCE); 976 nops--; 977 } 978 979 *hdr_res.status = status; 980 *hdr_res.nops = htonl(nops); 981 nfs4_cb_free_slot(&cps); 982 nfs_put_client(cps.clp); 983 return rpc_success; 984 985out_invalidcred: 986 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n"); 987 return svc_return_autherr(rqstp, rpc_autherr_badcred); 988} 989 990/* 991 * Define NFS4 callback COMPOUND ops. 992 */ 993static struct callback_op callback_ops[] = { 994 [0] = { 995 .res_maxsize = CB_OP_HDR_RES_MAXSZ, 996 }, 997 [OP_CB_GETATTR] = { 998 .process_op = nfs4_callback_getattr, 999 .decode_args = decode_getattr_args, 1000 .encode_res = encode_getattr_res, 1001 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, 1002 }, 1003 [OP_CB_RECALL] = { 1004 .process_op = nfs4_callback_recall, 1005 .decode_args = decode_recall_args, 1006 .res_maxsize = CB_OP_RECALL_RES_MAXSZ, 1007 }, 1008#if defined(CONFIG_NFS_V4_1) 1009 [OP_CB_LAYOUTRECALL] = { 1010 .process_op = nfs4_callback_layoutrecall, 1011 .decode_args = decode_layoutrecall_args, 1012 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ, 1013 }, 1014 [OP_CB_NOTIFY_DEVICEID] = { 1015 .process_op = nfs4_callback_devicenotify, 1016 .decode_args = decode_devicenotify_args, 1017 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ, 1018 }, 1019 [OP_CB_SEQUENCE] = { 1020 .process_op = nfs4_callback_sequence, 1021 .decode_args = decode_cb_sequence_args, 1022 .encode_res = encode_cb_sequence_res, 1023 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ, 1024 }, 1025 [OP_CB_RECALL_ANY] = { 1026 .process_op = nfs4_callback_recallany, 1027 .decode_args = decode_recallany_args, 1028 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ, 1029 }, 1030 [OP_CB_RECALL_SLOT] = { 1031 .process_op = nfs4_callback_recallslot, 1032 .decode_args = decode_recallslot_args, 1033 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ, 1034 }, 1035 [OP_CB_NOTIFY_LOCK] = { 1036 .process_op = nfs4_callback_notify_lock, 1037 .decode_args = decode_notify_lock_args, 1038 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ, 1039 }, 1040#endif /* CONFIG_NFS_V4_1 */ 1041#ifdef CONFIG_NFS_V4_2 1042 [OP_CB_OFFLOAD] = { 1043 .process_op = nfs4_callback_offload, 1044 .decode_args = decode_offload_args, 1045 .res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ, 1046 }, 1047#endif /* CONFIG_NFS_V4_2 */ 1048}; 1049 1050/* 1051 * Define NFS4 callback procedures 1052 */ 1053static const struct svc_procedure nfs4_callback_procedures1[] = { 1054 [CB_NULL] = { 1055 .pc_func = nfs4_callback_null, 1056 .pc_decode = nfs4_decode_void, 1057 .pc_encode = nfs4_encode_void, 1058 .pc_xdrressize = 1, 1059 }, 1060 [CB_COMPOUND] = { 1061 .pc_func = nfs4_callback_compound, 1062 .pc_encode = nfs4_encode_void, 1063 .pc_argsize = 256, 1064 .pc_ressize = 256, 1065 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, 1066 } 1067}; 1068 1069static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)]; 1070const struct svc_version nfs4_callback_version1 = { 1071 .vs_vers = 1, 1072 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 1073 .vs_proc = nfs4_callback_procedures1, 1074 .vs_count = nfs4_callback_count1, 1075 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 1076 .vs_dispatch = NULL, 1077 .vs_hidden = true, 1078 .vs_need_cong_ctrl = true, 1079}; 1080 1081static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)]; 1082const struct svc_version nfs4_callback_version4 = { 1083 .vs_vers = 4, 1084 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 1085 .vs_proc = nfs4_callback_procedures1, 1086 .vs_count = nfs4_callback_count4, 1087 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 1088 .vs_dispatch = NULL, 1089 .vs_hidden = true, 1090 .vs_need_cong_ctrl = true, 1091}; 1092