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 /* sanity check the count against the remaining stream */ 275 if (n > xdr_stream_remaining(xdr) / 276 ((4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE)) { 277 status = htonl(NFS4ERR_BADXDR); 278 goto out; 279 } 280 281 args->devs = kmalloc_objs(*args->devs, n); 282 if (!args->devs) { 283 status = htonl(NFS4ERR_DELAY); 284 goto out; 285 } 286 287 /* Decode each dev notification */ 288 for (i = 0; i < n; i++) { 289 struct cb_devicenotifyitem *dev = &args->devs[i]; 290 291 p = xdr_inline_decode(xdr, (4 * sizeof(uint32_t)) + 292 NFS4_DEVICEID4_SIZE); 293 if (unlikely(p == NULL)) { 294 status = htonl(NFS4ERR_BADXDR); 295 goto err; 296 } 297 298 tmp = ntohl(*p++); /* bitmap size */ 299 if (tmp != 1) { 300 status = htonl(NFS4ERR_INVAL); 301 goto err; 302 } 303 dev->cbd_notify_type = ntohl(*p++); 304 if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE && 305 dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) { 306 status = htonl(NFS4ERR_INVAL); 307 goto err; 308 } 309 310 tmp = ntohl(*p++); /* opaque size */ 311 if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) && 312 (tmp != NFS4_DEVICEID4_SIZE + 8)) || 313 ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) && 314 (tmp != NFS4_DEVICEID4_SIZE + 4))) { 315 status = htonl(NFS4ERR_INVAL); 316 goto err; 317 } 318 dev->cbd_layout_type = ntohl(*p++); 319 memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE); 320 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); 321 322 if (dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) { 323 p = xdr_inline_decode(xdr, sizeof(uint32_t)); 324 if (unlikely(p == NULL)) { 325 status = htonl(NFS4ERR_BADXDR); 326 goto err; 327 } 328 dev->cbd_immediate = ntohl(*p++); 329 } else { 330 dev->cbd_immediate = 0; 331 } 332 333 dprintk("%s: type %d layout 0x%x immediate %d\n", 334 __func__, dev->cbd_notify_type, dev->cbd_layout_type, 335 dev->cbd_immediate); 336 } 337 args->ndevs = n; 338 dprintk("%s: ndevs %d\n", __func__, args->ndevs); 339 return 0; 340 err: 341 kfree(args->devs); 342 out: 343 args->devs = NULL; 344 args->ndevs = 0; 345 dprintk("%s: status %d ndevs %d\n", 346 __func__, ntohl(status), args->ndevs); 347 return status; 348 } 349 350 static __be32 decode_sessionid(struct xdr_stream *xdr, 351 struct nfs4_sessionid *sid) 352 { 353 __be32 *p; 354 355 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN); 356 if (unlikely(p == NULL)) 357 return htonl(NFS4ERR_RESOURCE); 358 359 memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN); 360 return 0; 361 } 362 363 static __be32 decode_rc_list(struct xdr_stream *xdr, 364 struct referring_call_list *rc_list) 365 { 366 __be32 *p; 367 int i; 368 __be32 status; 369 370 status = decode_sessionid(xdr, &rc_list->rcl_sessionid); 371 if (status) 372 goto out; 373 374 status = htonl(NFS4ERR_RESOURCE); 375 p = xdr_inline_decode(xdr, sizeof(uint32_t)); 376 if (unlikely(p == NULL)) 377 goto out; 378 379 rc_list->rcl_nrefcalls = ntohl(*p++); 380 if (rc_list->rcl_nrefcalls) { 381 if (unlikely(rc_list->rcl_nrefcalls > xdr->buf->len)) 382 goto out; 383 p = xdr_inline_decode(xdr, 384 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t)); 385 if (unlikely(p == NULL)) 386 goto out; 387 rc_list->rcl_refcalls = kmalloc_objs(*rc_list->rcl_refcalls, 388 rc_list->rcl_nrefcalls); 389 if (unlikely(rc_list->rcl_refcalls == NULL)) 390 goto out; 391 for (i = 0; i < rc_list->rcl_nrefcalls; i++) { 392 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++); 393 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++); 394 } 395 } 396 status = 0; 397 398 out: 399 return status; 400 } 401 402 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp, 403 struct xdr_stream *xdr, 404 void *argp) 405 { 406 struct cb_sequenceargs *args = argp; 407 __be32 *p; 408 int i; 409 __be32 status; 410 411 status = decode_sessionid(xdr, &args->csa_sessionid); 412 if (status) 413 return status; 414 415 p = xdr_inline_decode(xdr, 5 * sizeof(uint32_t)); 416 if (unlikely(p == NULL)) 417 return htonl(NFS4ERR_RESOURCE); 418 419 args->csa_addr = svc_addr(rqstp); 420 args->csa_sequenceid = ntohl(*p++); 421 args->csa_slotid = ntohl(*p++); 422 args->csa_highestslotid = ntohl(*p++); 423 args->csa_cachethis = ntohl(*p++); 424 args->csa_nrclists = ntohl(*p++); 425 args->csa_rclists = NULL; 426 if (args->csa_nrclists) { 427 args->csa_rclists = kzalloc_objs(*args->csa_rclists, 428 args->csa_nrclists); 429 if (unlikely(args->csa_rclists == NULL)) 430 return htonl(NFS4ERR_RESOURCE); 431 432 for (i = 0; i < args->csa_nrclists; i++) { 433 status = decode_rc_list(xdr, &args->csa_rclists[i]); 434 if (status) { 435 args->csa_nrclists = i; 436 goto out_free; 437 } 438 } 439 } 440 return 0; 441 442 out_free: 443 for (i = 0; i < args->csa_nrclists; i++) 444 kfree(args->csa_rclists[i].rcl_refcalls); 445 kfree(args->csa_rclists); 446 return status; 447 } 448 449 static __be32 decode_recallany_args(struct svc_rqst *rqstp, 450 struct xdr_stream *xdr, 451 void *argp) 452 { 453 struct cb_recallanyargs *args = argp; 454 uint32_t bitmap[3]; 455 __be32 *p, status; 456 457 p = xdr_inline_decode(xdr, 4); 458 if (unlikely(p == NULL)) 459 return htonl(NFS4ERR_BADXDR); 460 args->craa_objs_to_keep = ntohl(*p++); 461 status = decode_bitmap(xdr, bitmap); 462 if (unlikely(status)) 463 return status; 464 args->craa_type_mask = bitmap[0]; 465 466 return 0; 467 } 468 469 static __be32 decode_recallslot_args(struct svc_rqst *rqstp, 470 struct xdr_stream *xdr, 471 void *argp) 472 { 473 struct cb_recallslotargs *args = argp; 474 __be32 *p; 475 476 p = xdr_inline_decode(xdr, 4); 477 if (unlikely(p == NULL)) 478 return htonl(NFS4ERR_BADXDR); 479 args->crsa_target_highest_slotid = ntohl(*p++); 480 return 0; 481 } 482 483 static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args) 484 { 485 __be32 *p; 486 unsigned int len; 487 488 p = xdr_inline_decode(xdr, 12); 489 if (unlikely(p == NULL)) 490 return htonl(NFS4ERR_BADXDR); 491 492 p = xdr_decode_hyper(p, &args->cbnl_owner.clientid); 493 len = be32_to_cpu(*p); 494 495 p = xdr_inline_decode(xdr, len); 496 if (unlikely(p == NULL)) 497 return htonl(NFS4ERR_BADXDR); 498 499 /* Only try to decode if the length is right */ 500 if (len == 20) { 501 p += 2; /* skip "lock id:" */ 502 args->cbnl_owner.s_dev = be32_to_cpu(*p++); 503 xdr_decode_hyper(p, &args->cbnl_owner.id); 504 args->cbnl_valid = true; 505 } else { 506 args->cbnl_owner.s_dev = 0; 507 args->cbnl_owner.id = 0; 508 args->cbnl_valid = false; 509 } 510 return 0; 511 } 512 513 static __be32 decode_notify_lock_args(struct svc_rqst *rqstp, 514 struct xdr_stream *xdr, void *argp) 515 { 516 struct cb_notify_lock_args *args = argp; 517 __be32 status; 518 519 status = decode_fh(xdr, &args->cbnl_fh); 520 if (unlikely(status != 0)) 521 return status; 522 return decode_lockowner(xdr, args); 523 } 524 525 #ifdef CONFIG_NFS_V4_2 526 static __be32 decode_write_response(struct xdr_stream *xdr, 527 struct cb_offloadargs *args) 528 { 529 __be32 *p; 530 531 /* skip the always zero field */ 532 p = xdr_inline_decode(xdr, 4); 533 if (unlikely(!p)) 534 goto out; 535 p++; 536 537 /* decode count, stable_how, verifier */ 538 p = xdr_inline_decode(xdr, 8 + 4); 539 if (unlikely(!p)) 540 goto out; 541 p = xdr_decode_hyper(p, &args->wr_count); 542 args->wr_writeverf.committed = be32_to_cpup(p); 543 p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE); 544 if (likely(p)) { 545 memcpy(&args->wr_writeverf.verifier.data[0], p, 546 NFS4_VERIFIER_SIZE); 547 return 0; 548 } 549 out: 550 return htonl(NFS4ERR_RESOURCE); 551 } 552 553 static __be32 decode_offload_args(struct svc_rqst *rqstp, 554 struct xdr_stream *xdr, 555 void *data) 556 { 557 struct cb_offloadargs *args = data; 558 __be32 *p; 559 __be32 status; 560 561 /* decode fh */ 562 status = decode_fh(xdr, &args->coa_fh); 563 if (unlikely(status != 0)) 564 return status; 565 566 /* decode stateid */ 567 status = decode_stateid(xdr, &args->coa_stateid); 568 if (unlikely(status != 0)) 569 return status; 570 571 /* decode status */ 572 p = xdr_inline_decode(xdr, 4); 573 if (unlikely(!p)) 574 goto out; 575 args->error = ntohl(*p++); 576 if (!args->error) { 577 status = decode_write_response(xdr, args); 578 if (unlikely(status != 0)) 579 return status; 580 } else { 581 p = xdr_inline_decode(xdr, 8); 582 if (unlikely(!p)) 583 goto out; 584 p = xdr_decode_hyper(p, &args->wr_count); 585 } 586 return 0; 587 out: 588 return htonl(NFS4ERR_RESOURCE); 589 } 590 #endif /* CONFIG_NFS_V4_2 */ 591 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) 592 { 593 if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0)) 594 return cpu_to_be32(NFS4ERR_RESOURCE); 595 return 0; 596 } 597 598 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz) 599 { 600 if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0) 601 return cpu_to_be32(NFS4ERR_RESOURCE); 602 return 0; 603 } 604 605 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) 606 { 607 __be32 *p; 608 609 if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) 610 return 0; 611 p = xdr_reserve_space(xdr, 8); 612 if (unlikely(!p)) 613 return htonl(NFS4ERR_RESOURCE); 614 p = xdr_encode_hyper(p, change); 615 return 0; 616 } 617 618 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) 619 { 620 __be32 *p; 621 622 if (!(bitmap[0] & FATTR4_WORD0_SIZE)) 623 return 0; 624 p = xdr_reserve_space(xdr, 8); 625 if (unlikely(!p)) 626 return htonl(NFS4ERR_RESOURCE); 627 p = xdr_encode_hyper(p, size); 628 return 0; 629 } 630 631 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec64 *time) 632 { 633 __be32 *p; 634 635 p = xdr_reserve_space(xdr, 12); 636 if (unlikely(!p)) 637 return htonl(NFS4ERR_RESOURCE); 638 p = xdr_encode_hyper(p, time->tv_sec); 639 *p = htonl(time->tv_nsec); 640 return 0; 641 } 642 643 static __be32 encode_attr_atime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time) 644 { 645 if (!(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) 646 return 0; 647 return encode_attr_time(xdr,time); 648 } 649 650 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time) 651 { 652 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) 653 return 0; 654 return encode_attr_time(xdr,time); 655 } 656 657 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time) 658 { 659 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) 660 return 0; 661 return encode_attr_time(xdr,time); 662 } 663 664 static __be32 encode_attr_delegatime(struct xdr_stream *xdr, 665 const uint32_t *bitmap, 666 const struct timespec64 *time) 667 { 668 if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_ACCESS)) 669 return 0; 670 return encode_attr_time(xdr,time); 671 } 672 673 static __be32 encode_attr_delegmtime(struct xdr_stream *xdr, 674 const uint32_t *bitmap, 675 const struct timespec64 *time) 676 { 677 if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_MODIFY)) 678 return 0; 679 return encode_attr_time(xdr,time); 680 } 681 682 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) 683 { 684 __be32 status; 685 686 hdr->status = xdr_reserve_space(xdr, 4); 687 if (unlikely(hdr->status == NULL)) 688 return htonl(NFS4ERR_RESOURCE); 689 status = encode_string(xdr, hdr->taglen, hdr->tag); 690 if (unlikely(status != 0)) 691 return status; 692 hdr->nops = xdr_reserve_space(xdr, 4); 693 if (unlikely(hdr->nops == NULL)) 694 return htonl(NFS4ERR_RESOURCE); 695 return 0; 696 } 697 698 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) 699 { 700 __be32 *p; 701 702 p = xdr_reserve_space(xdr, 8); 703 if (unlikely(p == NULL)) 704 return htonl(NFS4ERR_RESOURCE_HDR); 705 *p++ = htonl(op); 706 *p = res; 707 return 0; 708 } 709 710 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, 711 const void *resp) 712 { 713 const struct cb_getattrres *res = resp; 714 __be32 *savep = NULL; 715 __be32 status = res->status; 716 717 if (unlikely(status != 0)) 718 goto out; 719 status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap)); 720 if (unlikely(status != 0)) 721 goto out; 722 status = cpu_to_be32(NFS4ERR_RESOURCE); 723 savep = xdr_reserve_space(xdr, sizeof(*savep)); 724 if (unlikely(!savep)) 725 goto out; 726 status = encode_attr_change(xdr, res->bitmap, res->change_attr); 727 if (unlikely(status != 0)) 728 goto out; 729 status = encode_attr_size(xdr, res->bitmap, res->size); 730 if (unlikely(status != 0)) 731 goto out; 732 status = encode_attr_atime(xdr, res->bitmap, &res->atime); 733 if (unlikely(status != 0)) 734 goto out; 735 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); 736 if (unlikely(status != 0)) 737 goto out; 738 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); 739 if (unlikely(status != 0)) 740 goto out; 741 status = encode_attr_delegatime(xdr, res->bitmap, &res->atime); 742 if (unlikely(status != 0)) 743 goto out; 744 status = encode_attr_delegmtime(xdr, res->bitmap, &res->mtime); 745 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); 746 out: 747 return status; 748 } 749 750 static __be32 encode_sessionid(struct xdr_stream *xdr, 751 const struct nfs4_sessionid *sid) 752 { 753 __be32 *p; 754 755 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 756 if (unlikely(p == NULL)) 757 return htonl(NFS4ERR_RESOURCE); 758 759 memcpy(p, sid, NFS4_MAX_SESSIONID_LEN); 760 return 0; 761 } 762 763 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp, 764 struct xdr_stream *xdr, 765 const void *resp) 766 { 767 const struct cb_sequenceres *res = resp; 768 __be32 *p; 769 __be32 status = res->csr_status; 770 771 if (unlikely(status != 0)) 772 return status; 773 774 status = encode_sessionid(xdr, &res->csr_sessionid); 775 if (status) 776 return status; 777 778 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t)); 779 if (unlikely(p == NULL)) 780 return htonl(NFS4ERR_RESOURCE); 781 782 *p++ = htonl(res->csr_sequenceid); 783 *p++ = htonl(res->csr_slotid); 784 *p++ = htonl(res->csr_highestslotid); 785 *p++ = htonl(res->csr_target_highestslotid); 786 return 0; 787 } 788 789 static __be32 790 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) 791 { 792 if (op_nr == OP_CB_SEQUENCE) { 793 if (nop != 0) 794 return htonl(NFS4ERR_SEQUENCE_POS); 795 } else { 796 if (nop == 0) 797 return htonl(NFS4ERR_OP_NOT_IN_SESSION); 798 } 799 800 switch (op_nr) { 801 case OP_CB_GETATTR: 802 case OP_CB_RECALL: 803 case OP_CB_SEQUENCE: 804 case OP_CB_RECALL_ANY: 805 case OP_CB_RECALL_SLOT: 806 case OP_CB_LAYOUTRECALL: 807 case OP_CB_NOTIFY_DEVICEID: 808 case OP_CB_NOTIFY_LOCK: 809 *op = &callback_ops[op_nr]; 810 break; 811 812 case OP_CB_NOTIFY: 813 case OP_CB_PUSH_DELEG: 814 case OP_CB_RECALLABLE_OBJ_AVAIL: 815 case OP_CB_WANTS_CANCELLED: 816 return htonl(NFS4ERR_NOTSUPP); 817 818 default: 819 return htonl(NFS4ERR_OP_ILLEGAL); 820 } 821 822 return htonl(NFS_OK); 823 } 824 825 static void nfs4_callback_free_slot(struct nfs4_session *session, 826 struct nfs4_slot *slot) 827 { 828 struct nfs4_slot_table *tbl = &session->bc_slot_table; 829 830 spin_lock(&tbl->slot_tbl_lock); 831 /* 832 * Let the state manager know callback processing done. 833 * A single slot, so highest used slotid is either 0 or -1 834 */ 835 nfs4_free_slot(tbl, slot); 836 spin_unlock(&tbl->slot_tbl_lock); 837 } 838 839 static void nfs4_cb_free_slot(struct cb_process_state *cps) 840 { 841 if (cps->slot) { 842 nfs4_callback_free_slot(cps->clp->cl_session, cps->slot); 843 cps->slot = NULL; 844 } 845 } 846 847 #ifdef CONFIG_NFS_V4_2 848 static __be32 849 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 850 { 851 __be32 status = preprocess_nfs41_op(nop, op_nr, op); 852 if (status != htonl(NFS4ERR_OP_ILLEGAL)) 853 return status; 854 855 if (op_nr == OP_CB_OFFLOAD) { 856 *op = &callback_ops[op_nr]; 857 return htonl(NFS_OK); 858 } else 859 return htonl(NFS4ERR_NOTSUPP); 860 return htonl(NFS4ERR_OP_ILLEGAL); 861 } 862 #else /* CONFIG_NFS_V4_2 */ 863 static __be32 864 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op) 865 { 866 return htonl(NFS4ERR_MINOR_VERS_MISMATCH); 867 } 868 #endif /* CONFIG_NFS_V4_2 */ 869 870 static __be32 871 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op) 872 { 873 switch (op_nr) { 874 case OP_CB_GETATTR: 875 case OP_CB_RECALL: 876 *op = &callback_ops[op_nr]; 877 break; 878 default: 879 return htonl(NFS4ERR_OP_ILLEGAL); 880 } 881 882 return htonl(NFS_OK); 883 } 884 885 static __be32 process_op(int nop, struct svc_rqst *rqstp, 886 struct cb_process_state *cps) 887 { 888 struct xdr_stream *xdr_out = &rqstp->rq_res_stream; 889 struct callback_op *op = &callback_ops[0]; 890 unsigned int op_nr; 891 __be32 status; 892 long maxlen; 893 __be32 res; 894 895 status = decode_op_hdr(&rqstp->rq_arg_stream, &op_nr); 896 if (unlikely(status)) 897 return status; 898 899 switch (cps->minorversion) { 900 case 0: 901 status = preprocess_nfs4_op(op_nr, &op); 902 break; 903 case 1: 904 status = preprocess_nfs41_op(nop, op_nr, &op); 905 break; 906 case 2: 907 status = preprocess_nfs42_op(nop, op_nr, &op); 908 break; 909 default: 910 status = htonl(NFS4ERR_MINOR_VERS_MISMATCH); 911 } 912 913 if (status == htonl(NFS4ERR_OP_ILLEGAL)) 914 op_nr = OP_CB_ILLEGAL; 915 if (status) 916 goto encode_hdr; 917 918 if (cps->drc_status) { 919 status = cps->drc_status; 920 goto encode_hdr; 921 } 922 923 maxlen = xdr_out->end - xdr_out->p; 924 if (maxlen > 0 && maxlen < PAGE_SIZE) { 925 status = op->decode_args(rqstp, &rqstp->rq_arg_stream, 926 rqstp->rq_argp); 927 if (likely(status == 0)) 928 status = op->process_op(rqstp->rq_argp, rqstp->rq_resp, 929 cps); 930 } else 931 status = htonl(NFS4ERR_RESOURCE); 932 933 encode_hdr: 934 res = encode_op_hdr(xdr_out, op_nr, status); 935 if (unlikely(res)) 936 return res; 937 if (op->encode_res != NULL && status == 0) 938 status = op->encode_res(rqstp, xdr_out, rqstp->rq_resp); 939 return status; 940 } 941 942 /* 943 * Decode, process and encode a COMPOUND 944 */ 945 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp) 946 { 947 struct cb_compound_hdr_arg hdr_arg = { 0 }; 948 struct cb_compound_hdr_res hdr_res = { NULL }; 949 struct cb_process_state cps = { 950 .drc_status = 0, 951 .clp = NULL, 952 .net = SVC_NET(rqstp), 953 }; 954 unsigned int nops = 0; 955 __be32 status; 956 957 status = decode_compound_hdr_arg(&rqstp->rq_arg_stream, &hdr_arg); 958 if (status == htonl(NFS4ERR_RESOURCE)) 959 return rpc_garbage_args; 960 961 if (hdr_arg.minorversion == 0) { 962 cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident); 963 if (!cps.clp) { 964 trace_nfs_cb_no_clp(rqstp->rq_xid, hdr_arg.cb_ident); 965 goto out_invalidcred; 966 } 967 if (!check_gss_callback_principal(cps.clp, rqstp)) { 968 trace_nfs_cb_badprinc(rqstp->rq_xid, hdr_arg.cb_ident); 969 nfs_put_client(cps.clp); 970 goto out_invalidcred; 971 } 972 svc_xprt_set_valid(rqstp->rq_xprt); 973 } 974 975 cps.minorversion = hdr_arg.minorversion; 976 hdr_res.taglen = hdr_arg.taglen; 977 hdr_res.tag = hdr_arg.tag; 978 if (encode_compound_hdr_res(&rqstp->rq_res_stream, &hdr_res) != 0) { 979 if (cps.clp) 980 nfs_put_client(cps.clp); 981 return rpc_system_err; 982 } 983 while (status == 0 && nops != hdr_arg.nops) { 984 status = process_op(nops, rqstp, &cps); 985 nops++; 986 } 987 988 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return 989 * resource error in cb_compound status without returning op */ 990 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) { 991 status = htonl(NFS4ERR_RESOURCE); 992 nops--; 993 } 994 995 if (svc_is_backchannel(rqstp) && cps.clp) { 996 rqstp->bc_to_initval = cps.clp->cl_rpcclient->cl_timeout->to_initval; 997 rqstp->bc_to_retries = cps.clp->cl_rpcclient->cl_timeout->to_retries; 998 } 999 1000 *hdr_res.status = status; 1001 *hdr_res.nops = htonl(nops); 1002 nfs4_cb_free_slot(&cps); 1003 nfs_put_client(cps.clp); 1004 return rpc_success; 1005 1006 out_invalidcred: 1007 pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n"); 1008 rqstp->rq_auth_stat = rpc_autherr_badcred; 1009 return rpc_success; 1010 } 1011 1012 static int 1013 nfs_callback_dispatch(struct svc_rqst *rqstp) 1014 { 1015 const struct svc_procedure *procp = rqstp->rq_procinfo; 1016 1017 *rqstp->rq_accept_statp = procp->pc_func(rqstp); 1018 return 1; 1019 } 1020 1021 /* 1022 * Define NFS4 callback COMPOUND ops. 1023 */ 1024 static struct callback_op callback_ops[] = { 1025 [0] = { 1026 .res_maxsize = CB_OP_HDR_RES_MAXSZ, 1027 }, 1028 [OP_CB_GETATTR] = { 1029 .process_op = nfs4_callback_getattr, 1030 .decode_args = decode_getattr_args, 1031 .encode_res = encode_getattr_res, 1032 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, 1033 }, 1034 [OP_CB_RECALL] = { 1035 .process_op = nfs4_callback_recall, 1036 .decode_args = decode_recall_args, 1037 .res_maxsize = CB_OP_RECALL_RES_MAXSZ, 1038 }, 1039 [OP_CB_LAYOUTRECALL] = { 1040 .process_op = nfs4_callback_layoutrecall, 1041 .decode_args = decode_layoutrecall_args, 1042 .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ, 1043 }, 1044 [OP_CB_NOTIFY_DEVICEID] = { 1045 .process_op = nfs4_callback_devicenotify, 1046 .decode_args = decode_devicenotify_args, 1047 .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ, 1048 }, 1049 [OP_CB_SEQUENCE] = { 1050 .process_op = nfs4_callback_sequence, 1051 .decode_args = decode_cb_sequence_args, 1052 .encode_res = encode_cb_sequence_res, 1053 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ, 1054 }, 1055 [OP_CB_RECALL_ANY] = { 1056 .process_op = nfs4_callback_recallany, 1057 .decode_args = decode_recallany_args, 1058 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ, 1059 }, 1060 [OP_CB_RECALL_SLOT] = { 1061 .process_op = nfs4_callback_recallslot, 1062 .decode_args = decode_recallslot_args, 1063 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ, 1064 }, 1065 [OP_CB_NOTIFY_LOCK] = { 1066 .process_op = nfs4_callback_notify_lock, 1067 .decode_args = decode_notify_lock_args, 1068 .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ, 1069 }, 1070 #ifdef CONFIG_NFS_V4_2 1071 [OP_CB_OFFLOAD] = { 1072 .process_op = nfs4_callback_offload, 1073 .decode_args = decode_offload_args, 1074 .res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ, 1075 }, 1076 #endif /* CONFIG_NFS_V4_2 */ 1077 }; 1078 1079 /* 1080 * Define NFS4 callback procedures 1081 */ 1082 static const struct svc_procedure nfs4_callback_procedures1[] = { 1083 [CB_NULL] = { 1084 .pc_func = nfs4_callback_null, 1085 .pc_encode = nfs4_encode_void, 1086 .pc_xdrressize = 1, 1087 .pc_name = "NULL", 1088 }, 1089 [CB_COMPOUND] = { 1090 .pc_func = nfs4_callback_compound, 1091 .pc_encode = nfs4_encode_void, 1092 .pc_argsize = 256, 1093 .pc_argzero = 256, 1094 .pc_ressize = 256, 1095 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, 1096 .pc_name = "COMPOUND", 1097 } 1098 }; 1099 1100 const struct svc_version nfs4_callback_version1 = { 1101 .vs_vers = 1, 1102 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 1103 .vs_proc = nfs4_callback_procedures1, 1104 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 1105 .vs_dispatch = nfs_callback_dispatch, 1106 .vs_hidden = true, 1107 .vs_need_cong_ctrl = true, 1108 }; 1109 1110 const struct svc_version nfs4_callback_version4 = { 1111 .vs_vers = 4, 1112 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), 1113 .vs_proc = nfs4_callback_procedures1, 1114 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, 1115 .vs_dispatch = nfs_callback_dispatch, 1116 .vs_hidden = true, 1117 .vs_need_cong_ctrl = true, 1118 }; 1119