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