Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/lockd/xdr4.c |
| 4 | * |
| 5 | * XDR support for lockd and the lock client. |
| 6 | * |
| 7 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 8 | * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/nfs.h> |
| 14 | |
| 15 | #include <linux/sunrpc/xdr.h> |
| 16 | #include <linux/sunrpc/clnt.h> |
| 17 | #include <linux/sunrpc/svc.h> |
| 18 | #include <linux/sunrpc/stats.h> |
| 19 | #include <linux/lockd/lockd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Chuck Lever | 7956521 | 2021-06-03 16:52:16 -0400 | [diff] [blame] | 21 | #include "svcxdr.h" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define NLMDBG_FACILITY NLMDBG_XDR |
| 24 | |
| 25 | static inline loff_t |
| 26 | s64_to_loff_t(__s64 offset) |
| 27 | { |
| 28 | return (loff_t)offset; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | static inline s64 |
| 33 | loff_t_to_s64(loff_t offset) |
| 34 | { |
| 35 | s64 res; |
| 36 | if (offset > NLM4_OFFSET_MAX) |
| 37 | res = NLM4_OFFSET_MAX; |
| 38 | else if (offset < -NLM4_OFFSET_MAX) |
| 39 | res = -NLM4_OFFSET_MAX; |
| 40 | else |
| 41 | res = offset; |
| 42 | return res; |
| 43 | } |
| 44 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 45 | static __be32 * |
| 46 | nlm4_encode_cookie(__be32 *p, struct nlm_cookie *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | *p++ = htonl(c->len); |
| 49 | memcpy(p, c->data, c->len); |
| 50 | p+=XDR_QUADLEN(c->len); |
| 51 | return p; |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 55 | * NLM file handles are defined by specification to be a variable-length |
| 56 | * XDR opaque no longer than 1024 bytes. However, this implementation |
| 57 | * limits their length to the size of an NFSv3 file handle. |
| 58 | */ |
| 59 | static bool |
| 60 | svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh) |
| 61 | { |
| 62 | __be32 *p; |
| 63 | u32 len; |
| 64 | |
| 65 | if (xdr_stream_decode_u32(xdr, &len) < 0) |
| 66 | return false; |
| 67 | if (len > NFS_MAXFHSIZE) |
| 68 | return false; |
| 69 | |
| 70 | p = xdr_inline_decode(xdr, len); |
| 71 | if (!p) |
| 72 | return false; |
| 73 | fh->size = len; |
| 74 | memcpy(fh->data, p, len); |
| 75 | memset(fh->data + len, 0, sizeof(fh->data) - len); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 80 | static bool |
| 81 | svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock) |
| 82 | { |
| 83 | struct file_lock *fl = &lock->fl; |
| 84 | u64 len, start; |
| 85 | s64 end; |
| 86 | |
| 87 | if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len)) |
| 88 | return false; |
| 89 | if (!svcxdr_decode_fhandle(xdr, &lock->fh)) |
| 90 | return false; |
| 91 | if (!svcxdr_decode_owner(xdr, &lock->oh)) |
| 92 | return false; |
| 93 | if (xdr_stream_decode_u32(xdr, &lock->svid) < 0) |
| 94 | return false; |
| 95 | if (xdr_stream_decode_u64(xdr, &start) < 0) |
| 96 | return false; |
| 97 | if (xdr_stream_decode_u64(xdr, &len) < 0) |
| 98 | return false; |
| 99 | |
| 100 | locks_init_lock(fl); |
| 101 | fl->fl_flags = FL_POSIX; |
| 102 | fl->fl_type = F_RDLCK; |
| 103 | end = start + len - 1; |
| 104 | fl->fl_start = s64_to_loff_t(start); |
| 105 | if (len == 0 || end < 0) |
| 106 | fl->fl_end = OFFSET_MAX; |
| 107 | else |
| 108 | fl->fl_end = s64_to_loff_t(end); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * Encode result of a TEST/TEST_MSG call |
| 115 | */ |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 116 | static __be32 * |
| 117 | nlm4_encode_testres(__be32 *p, struct nlm_res *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | s64 start, len; |
| 120 | |
| 121 | dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp); |
| 122 | if (!(p = nlm4_encode_cookie(p, &resp->cookie))) |
| 123 | return NULL; |
| 124 | *p++ = resp->status; |
| 125 | |
| 126 | if (resp->status == nlm_lck_denied) { |
| 127 | struct file_lock *fl = &resp->lock.fl; |
| 128 | |
| 129 | *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one; |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 130 | *p++ = htonl(resp->lock.svid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* Encode owner handle. */ |
| 133 | if (!(p = xdr_encode_netobj(p, &resp->lock.oh))) |
| 134 | return NULL; |
| 135 | |
| 136 | start = loff_t_to_s64(fl->fl_start); |
| 137 | if (fl->fl_end == OFFSET_MAX) |
| 138 | len = 0; |
| 139 | else |
| 140 | len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1); |
| 141 | |
| 142 | p = xdr_encode_hyper(p, start); |
| 143 | p = xdr_encode_hyper(p, len); |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 144 | dprintk("xdr: encode_testres (status %u pid %d type %d start %Ld end %Ld)\n", |
| 145 | resp->status, (int)resp->lock.svid, fl->fl_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | (long long)fl->fl_start, (long long)fl->fl_end); |
| 147 | } |
| 148 | |
| 149 | dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp); |
| 150 | return p; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /* |
Chuck Lever | 7956521 | 2021-06-03 16:52:16 -0400 | [diff] [blame] | 155 | * Decode Call arguments |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
Chuck Lever | 7956521 | 2021-06-03 16:52:16 -0400 | [diff] [blame] | 157 | |
| 158 | int |
| 159 | nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p) |
| 160 | { |
| 161 | return 1; |
| 162 | } |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 165 | nlm4svc_decode_testargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 167 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 168 | struct nlm_args *argp = rqstp->rq_argp; |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 169 | u32 exclusive; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 171 | if (!svcxdr_decode_cookie(xdr, &argp->cookie)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return 0; |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 173 | if (xdr_stream_decode_bool(xdr, &exclusive) < 0) |
| 174 | return 0; |
| 175 | if (!svcxdr_decode_lock(xdr, &argp->lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | if (exclusive) |
| 178 | argp->lock.fl.fl_type = F_WRLCK; |
| 179 | |
Chuck Lever | 345b415 | 2021-06-03 16:52:22 -0400 | [diff] [blame] | 180 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int |
Chuck Lever | 0e5977a | 2021-06-03 16:52:28 -0400 | [diff] [blame] | 184 | nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p) |
| 185 | { |
| 186 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 187 | struct nlm_args *argp = rqstp->rq_argp; |
| 188 | u32 exclusive; |
| 189 | |
| 190 | if (!svcxdr_decode_cookie(xdr, &argp->cookie)) |
| 191 | return 0; |
| 192 | if (xdr_stream_decode_bool(xdr, &argp->block) < 0) |
| 193 | return 0; |
| 194 | if (xdr_stream_decode_bool(xdr, &exclusive) < 0) |
| 195 | return 0; |
| 196 | if (!svcxdr_decode_lock(xdr, &argp->lock)) |
| 197 | return 0; |
| 198 | if (exclusive) |
| 199 | argp->lock.fl.fl_type = F_WRLCK; |
| 200 | if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0) |
| 201 | return 0; |
| 202 | if (xdr_stream_decode_u32(xdr, &argp->state) < 0) |
| 203 | return 0; |
| 204 | argp->monitor = 1; /* monitor client by default */ |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | int |
Chuck Lever | 1e1f38d | 2021-06-03 16:52:34 -0400 | [diff] [blame] | 210 | nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p) |
| 211 | { |
| 212 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 213 | struct nlm_args *argp = rqstp->rq_argp; |
| 214 | u32 exclusive; |
| 215 | |
| 216 | if (!svcxdr_decode_cookie(xdr, &argp->cookie)) |
| 217 | return 0; |
| 218 | if (xdr_stream_decode_bool(xdr, &argp->block) < 0) |
| 219 | return 0; |
| 220 | if (xdr_stream_decode_bool(xdr, &exclusive) < 0) |
| 221 | return 0; |
| 222 | if (!svcxdr_decode_lock(xdr, &argp->lock)) |
| 223 | return 0; |
| 224 | if (exclusive) |
| 225 | argp->lock.fl.fl_type = F_WRLCK; |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | int |
Chuck Lever | d76d8c2 | 2021-06-03 16:52:40 -0400 | [diff] [blame] | 230 | nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p) |
| 231 | { |
| 232 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 233 | struct nlm_args *argp = rqstp->rq_argp; |
| 234 | |
| 235 | if (!svcxdr_decode_cookie(xdr, &argp->cookie)) |
| 236 | return 0; |
| 237 | if (!svcxdr_decode_lock(xdr, &argp->lock)) |
| 238 | return 0; |
| 239 | argp->lock.fl.fl_type = F_UNLCK; |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int |
Chuck Lever | b4c24b5 | 2021-06-03 16:52:46 -0400 | [diff] [blame] | 245 | nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p) |
| 246 | { |
| 247 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 248 | struct nlm_res *resp = rqstp->rq_argp; |
| 249 | |
| 250 | if (!svcxdr_decode_cookie(xdr, &resp->cookie)) |
| 251 | return 0; |
| 252 | if (!svcxdr_decode_stats(xdr, &resp->status)) |
| 253 | return 0; |
| 254 | |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | int |
Chuck Lever | bc3665f | 2021-06-03 16:52:52 -0400 | [diff] [blame] | 259 | nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p) |
| 260 | { |
| 261 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 262 | struct nlm_reboot *argp = rqstp->rq_argp; |
| 263 | u32 len; |
| 264 | |
| 265 | if (xdr_stream_decode_u32(xdr, &len) < 0) |
| 266 | return 0; |
| 267 | if (len > SM_MAXSTRLEN) |
| 268 | return 0; |
| 269 | p = xdr_inline_decode(xdr, len); |
| 270 | if (!p) |
| 271 | return 0; |
| 272 | argp->len = len; |
| 273 | argp->mon = (char *)p; |
| 274 | if (xdr_stream_decode_u32(xdr, &argp->state) < 0) |
| 275 | return 0; |
| 276 | p = xdr_inline_decode(xdr, SM_PRIV_SIZE); |
| 277 | if (!p) |
| 278 | return 0; |
| 279 | memcpy(&argp->priv.data, p, sizeof(argp->priv.data)); |
| 280 | |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | int |
Chuck Lever | 7cf96b6 | 2021-06-03 16:52:58 -0400 | [diff] [blame^] | 285 | nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p) |
| 286 | { |
| 287 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
| 288 | struct nlm_args *argp = rqstp->rq_argp; |
| 289 | struct nlm_lock *lock = &argp->lock; |
| 290 | |
| 291 | memset(lock, 0, sizeof(*lock)); |
| 292 | locks_init_lock(&lock->fl); |
| 293 | lock->svid = ~(u32)0; |
| 294 | |
| 295 | if (!svcxdr_decode_cookie(xdr, &argp->cookie)) |
| 296 | return 0; |
| 297 | if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len)) |
| 298 | return 0; |
| 299 | if (!svcxdr_decode_fhandle(xdr, &lock->fh)) |
| 300 | return 0; |
| 301 | if (!svcxdr_decode_owner(xdr, &lock->oh)) |
| 302 | return 0; |
| 303 | /* XXX: Range checks are missing in the original code */ |
| 304 | if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0) |
| 305 | return 0; |
| 306 | if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0) |
| 307 | return 0; |
| 308 | |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 313 | nlm4svc_encode_testres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 315 | struct nlm_res *resp = rqstp->rq_resp; |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (!(p = nlm4_encode_testres(p, resp))) |
| 318 | return 0; |
| 319 | return xdr_ressize_check(rqstp, p); |
| 320 | } |
| 321 | |
| 322 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 323 | nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 325 | struct nlm_res *resp = rqstp->rq_resp; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | if (!(p = nlm4_encode_cookie(p, &resp->cookie))) |
| 328 | return 0; |
| 329 | *p++ = resp->status; |
| 330 | *p++ = xdr_zero; /* sequence argument */ |
| 331 | return xdr_ressize_check(rqstp, p); |
| 332 | } |
| 333 | |
| 334 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 335 | nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 337 | struct nlm_res *resp = rqstp->rq_resp; |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | if (!(p = nlm4_encode_cookie(p, &resp->cookie))) |
| 340 | return 0; |
| 341 | *p++ = resp->status; |
| 342 | return xdr_ressize_check(rqstp, p); |
| 343 | } |
| 344 | |
| 345 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 346 | nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 348 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct nlm_lock *lock = &argp->lock; |
| 350 | |
| 351 | if (!(p = xdr_decode_string_inplace(p, &lock->caller, |
| 352 | &lock->len, NLM_MAXSTRLEN))) |
| 353 | return 0; |
| 354 | argp->state = ntohl(*p++); |
| 355 | return xdr_argsize_check(rqstp, p); |
| 356 | } |
| 357 | |
| 358 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 359 | nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | return xdr_ressize_check(rqstp, p); |
| 362 | } |