1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de> 4 */ 5 6 #ifndef _LOCKD_LOCKD_H 7 #define _LOCKD_LOCKD_H 8 9 #include <linux/exportfs.h> 10 #include <linux/in.h> 11 #include <linux/in6.h> 12 #include <net/ipv6.h> 13 #include <linux/fs.h> 14 #include <linux/kref.h> 15 #include <linux/refcount.h> 16 #include <linux/utsname.h> 17 #include "nlm.h" 18 #include <linux/lockd/bind.h> 19 #include "xdr.h" 20 #include <linux/sunrpc/debug.h> 21 #include <linux/sunrpc/svc.h> 22 23 /* 24 * Enable lockd debugging. 25 * Requires CONFIG_SUNRPC_DEBUG. 26 */ 27 #undef ifdebug 28 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 29 # define ifdebug(flag) if (unlikely(nlm_debug & NLMDBG_##flag)) 30 #else 31 # define ifdebug(flag) if (0) 32 #endif 33 34 #define NLMDBG_SVC 0x0001 35 #define NLMDBG_CLIENT 0x0002 36 #define NLMDBG_CLNTLOCK 0x0004 37 #define NLMDBG_SVCLOCK 0x0008 38 #define NLMDBG_MONITOR 0x0010 39 #define NLMDBG_CLNTSUBS 0x0020 40 #define NLMDBG_SVCSUBS 0x0040 41 #define NLMDBG_HOSTCACHE 0x0080 42 #define NLMDBG_XDR 0x0100 43 #define NLMDBG_ALL 0x7fff 44 45 /* 46 * Version string 47 */ 48 #define LOCKD_VERSION "0.5" 49 50 /* 51 * Default timeout for RPC calls (seconds) 52 */ 53 #define LOCKD_DFLT_TIMEO 10 54 55 /* 56 * Number of leading bytes of nfs_fh.data that file_hash() 57 * digests when bucketing nlm_files[]. Sized for historical 58 * NFSv2 handles; nfs_fh.data must be initialized at least 59 * this far before lookup, regardless of fh.size. 60 */ 61 #define LOCKD_FH_HASH_SIZE 32 62 63 /* error codes new to NLMv4 */ 64 #define nlm4_deadlock cpu_to_be32(NLM_DEADLCK) 65 #define nlm4_rofs cpu_to_be32(NLM_ROFS) 66 #define nlm4_stale_fh cpu_to_be32(NLM_STALE_FH) 67 #define nlm4_fbig cpu_to_be32(NLM_FBIG) 68 #define nlm4_failed cpu_to_be32(NLM_FAILED) 69 70 /* 71 * Internal-use status codes, not to be placed on the wire. 72 * Version handlers translate these to appropriate wire values. 73 */ 74 #define nlm__int__drop_reply cpu_to_be32(30000) 75 #define nlm__int__deadlock cpu_to_be32(30001) 76 #define nlm__int__stale_fh cpu_to_be32(30002) 77 #define nlm__int__failed cpu_to_be32(30003) 78 79 /* 80 * Lockd host handle (used both by the client and server personality). 81 */ 82 struct nlm_host { 83 struct hlist_node h_hash; /* doubly linked list */ 84 struct sockaddr_storage h_addr; /* peer address */ 85 size_t h_addrlen; 86 struct sockaddr_storage h_srcaddr; /* our address (optional) */ 87 size_t h_srcaddrlen; 88 struct rpc_clnt *h_rpcclnt; /* RPC client to talk to peer */ 89 char *h_name; /* remote hostname */ 90 u32 h_version; /* interface version */ 91 unsigned short h_proto; /* transport proto */ 92 unsigned short h_reclaiming : 1, 93 h_server : 1, /* server side, not client side */ 94 h_noresvport : 1, 95 h_inuse : 1; 96 wait_queue_head_t h_gracewait; /* wait while reclaiming */ 97 struct rw_semaphore h_rwsem; /* Reboot recovery lock */ 98 u32 h_state; /* pseudo-state counter */ 99 u32 h_nsmstate; /* true remote NSM state */ 100 u32 h_pidcount; /* Pseudopids */ 101 refcount_t h_count; /* reference count */ 102 struct mutex h_mutex; /* mutex for pmap binding */ 103 unsigned long h_nextrebind; /* next portmap call */ 104 unsigned long h_expires; /* eligible for GC */ 105 struct list_head h_lockowners; /* Lockowners for the client */ 106 spinlock_t h_lock; 107 struct list_head h_granted; /* Locks in GRANTED state */ 108 struct list_head h_reclaim; /* Locks in RECLAIM state */ 109 struct nsm_handle *h_nsmhandle; /* NSM status handle */ 110 char *h_addrbuf; /* address eyecatcher */ 111 struct net *net; /* host net */ 112 const struct cred *h_cred; 113 char nodename[UNX_MAXNODENAME + 1]; 114 const struct nlmclnt_operations *h_nlmclnt_ops; /* Callback ops for NLM users */ 115 }; 116 117 /* 118 * The largest string sm_addrbuf should hold is a full-size IPv6 address 119 * (no "::" anywhere) with a scope ID. The buffer size is computed to 120 * hold eight groups of colon-separated four-hex-digit numbers, a 121 * percent sign, a scope id (at most 32 bits, in decimal), and NUL. 122 */ 123 #define NSM_ADDRBUF ((8 * 4 + 7) + (1 + 10) + 1) 124 125 struct nsm_handle { 126 struct list_head sm_link; 127 refcount_t sm_count; 128 char *sm_mon_name; 129 char *sm_name; 130 struct sockaddr_storage sm_addr; 131 size_t sm_addrlen; 132 unsigned int sm_monitored : 1, 133 sm_sticky : 1; /* don't unmonitor */ 134 struct nsm_private sm_priv; 135 char sm_addrbuf[NSM_ADDRBUF]; 136 }; 137 138 /* 139 * Rigorous type checking on sockaddr type conversions 140 */ 141 static inline struct sockaddr *nlm_addr(const struct nlm_host *host) 142 { 143 return (struct sockaddr *)&host->h_addr; 144 } 145 146 static inline struct sockaddr *nlm_srcaddr(const struct nlm_host *host) 147 { 148 return (struct sockaddr *)&host->h_srcaddr; 149 } 150 151 /* 152 * Map an fl_owner_t into a unique 32-bit "pid" 153 */ 154 struct nlm_lockowner { 155 struct list_head list; 156 refcount_t count; 157 158 struct nlm_host *host; 159 fl_owner_t owner; 160 uint32_t pid; 161 }; 162 163 /* 164 * This is the representation of a blocked client lock. 165 */ 166 struct nlm_wait { 167 struct list_head b_list; /* linked list */ 168 wait_queue_head_t b_wait; /* where to wait on */ 169 struct nlm_host *b_host; 170 struct file_lock *b_lock; /* local file lock */ 171 __be32 b_status; /* grant callback status */ 172 }; 173 174 /* 175 * Memory chunk for NLM client RPC request. 176 */ 177 #define NLMCLNT_OHSIZE ((__NEW_UTS_LEN) + 10u) 178 struct nlm_rqst { 179 refcount_t a_count; 180 unsigned int a_flags; /* initial RPC task flags */ 181 struct nlm_host * a_host; /* host handle */ 182 struct lockd_args a_args; /* arguments */ 183 struct lockd_res a_res; /* result */ 184 struct nlm_block * a_block; 185 unsigned int a_retries; /* Retry count */ 186 u8 a_owner[NLMCLNT_OHSIZE]; 187 void * a_callback_data; /* sent to nlmclnt_operations callbacks */ 188 }; 189 190 struct lockd_share; 191 192 /* 193 * This struct describes a file held open by lockd on behalf of 194 * an NFS client. 195 */ 196 struct nlm_file { 197 struct hlist_node f_list; /* linked list */ 198 struct nfs_fh f_handle; /* NFS file handle */ 199 struct file * f_file[2]; /* VFS file pointers, 200 indexed by O_ flags */ 201 struct lockd_share * f_shares; /* DOS shares */ 202 struct list_head f_blocks; /* blocked locks */ 203 unsigned int f_locks; /* guesstimate # of locks */ 204 unsigned int f_count; /* reference count */ 205 struct mutex f_mutex; /* avoid concurrent access */ 206 }; 207 208 /* 209 * This is a server block (i.e. a lock requested by some client which 210 * couldn't be granted because of a conflicting lock). 211 */ 212 #define NLM_NEVER (~(unsigned long) 0) 213 /* timeout on non-blocking call: */ 214 #define NLM_TIMEOUT (7 * HZ) 215 216 struct nlm_block { 217 struct kref b_count; /* Reference count */ 218 struct list_head b_list; /* linked list of all blocks */ 219 struct list_head b_flist; /* linked list (per file) */ 220 struct nlm_rqst * b_call; /* RPC args & callback info */ 221 struct svc_serv * b_daemon; /* NLM service */ 222 struct nlm_host * b_host; /* host handle for RPC clnt */ 223 unsigned long b_when; /* next re-xmit */ 224 unsigned int b_id; /* block id */ 225 unsigned char b_granted; /* VFS granted lock */ 226 struct nlm_file * b_file; /* file in question */ 227 struct cache_req * b_cache_req; /* deferred request handling */ 228 struct cache_deferred_req * b_deferred_req; 229 unsigned int b_flags; /* block flags */ 230 #define B_QUEUED 1 /* lock queued */ 231 #define B_GOT_CALLBACK 2 /* got lock or conflicting lock */ 232 #define B_TIMED_OUT 4 /* filesystem too slow to respond */ 233 }; 234 235 /* 236 * Global variables 237 */ 238 extern const struct rpc_program nlm_program; 239 extern const struct svc_version nlmsvc_version1; 240 extern const struct svc_version nlmsvc_version3; 241 #ifdef CONFIG_LOCKD_V4 242 extern const struct svc_version nlmsvc_version4; 243 #endif 244 extern int nlmsvc_grace_period; 245 extern unsigned long nlm_timeout; 246 extern bool nsm_use_hostnames; 247 extern u32 nsm_local_state; 248 249 extern struct timer_list nlmsvc_retry; 250 251 /* 252 * Lockd client functions 253 */ 254 struct nlm_rqst * nlm_alloc_call(struct nlm_host *host); 255 int nlm_async_call(struct nlm_rqst *, u32, const struct rpc_call_ops *); 256 int nlm_async_reply(struct nlm_rqst *, u32, const struct rpc_call_ops *); 257 void nlmclnt_release_call(struct nlm_rqst *); 258 void nlmclnt_prepare_block(struct nlm_wait *block, struct nlm_host *host, 259 struct file_lock *fl); 260 void nlmclnt_queue_block(struct nlm_wait *block); 261 __be32 nlmclnt_dequeue_block(struct nlm_wait *block); 262 int nlmclnt_wait(struct nlm_wait *block, struct nlm_rqst *req, long timeout); 263 __be32 nlmclnt_grant(const struct sockaddr *addr, 264 const struct lockd_lock *lock); 265 void nlmclnt_recovery(struct nlm_host *); 266 int nlmclnt_reclaim(struct nlm_host *, struct file_lock *, 267 struct nlm_rqst *); 268 void nlmclnt_next_cookie(struct lockd_cookie *); 269 270 #ifdef CONFIG_LOCKD_V4 271 extern const struct rpc_version nlm_version4; 272 #endif 273 274 /* 275 * Host cache 276 */ 277 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap, 278 const size_t salen, 279 const unsigned short protocol, 280 const u32 version, 281 const char *hostname, 282 int noresvport, 283 struct net *net, 284 const struct cred *cred); 285 void nlmclnt_release_host(struct nlm_host *); 286 struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp, 287 const char *hostname, 288 const size_t hostname_len); 289 void nlmsvc_release_host(struct nlm_host *); 290 struct rpc_clnt * nlm_bind_host(struct nlm_host *); 291 void nlm_rebind_host(struct nlm_host *); 292 struct nlm_host * nlm_get_host(struct nlm_host *); 293 void nlm_shutdown_hosts(void); 294 void nlm_shutdown_hosts_net(struct net *net); 295 void nlm_host_rebooted(const struct net *net, 296 const struct lockd_reboot *); 297 298 /* 299 * Host monitoring 300 */ 301 int nsm_monitor(const struct nlm_host *host); 302 void nsm_unmonitor(const struct nlm_host *host); 303 304 struct nsm_handle *nsm_get_handle(const struct net *net, 305 const struct sockaddr *sap, 306 const size_t salen, 307 const char *hostname, 308 const size_t hostname_len); 309 struct nsm_handle *nsm_reboot_lookup(const struct net *net, 310 const struct lockd_reboot *info); 311 void nsm_release(struct nsm_handle *nsm); 312 313 /* 314 * This is used in garbage collection and resource reclaim 315 * A return value != 0 means destroy the lock/block/share 316 */ 317 typedef int (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref); 318 319 /* 320 * Server-side lock handling 321 */ 322 int lock_to_openmode(struct file_lock *); 323 __be32 nlmsvc_lock(struct svc_rqst *, struct nlm_file *, 324 struct nlm_host *, struct lockd_lock *, int, 325 struct lockd_cookie *, int); 326 __be32 nlmsvc_unlock(struct net *net, struct nlm_file *, struct lockd_lock *); 327 __be32 nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, 328 struct nlm_host *host, struct lockd_lock *lock, 329 struct lockd_lock *conflock); 330 __be32 nlmsvc_cancel_blocked(struct net *net, struct nlm_file *, struct lockd_lock *); 331 void nlmsvc_retry_blocked(struct svc_rqst *rqstp); 332 void nlmsvc_traverse_blocks(struct nlm_host *, struct nlm_file *, 333 nlm_host_match_fn_t match); 334 void nlmsvc_grant_reply(struct lockd_cookie *, __be32); 335 void nlmsvc_release_call(struct nlm_rqst *); 336 void nlmsvc_locks_init_private(struct file_lock *, struct nlm_host *, pid_t); 337 int nlmsvc_dispatch(struct svc_rqst *rqstp); 338 339 /* 340 * File handling for the server personality 341 */ 342 __be32 nlm_lookup_file(struct svc_rqst *, struct nlm_file **, 343 struct lockd_lock *, int); 344 void nlm_release_file(struct nlm_file *); 345 void nlmsvc_put_lockowner(struct nlm_lockowner *); 346 void nlmsvc_release_lockowner(struct lockd_lock *); 347 void nlmsvc_mark_resources(struct net *); 348 void nlmsvc_free_host_resources(struct nlm_host *); 349 void nlmsvc_invalidate_all(void); 350 351 static inline struct file *nlmsvc_file_file(const struct nlm_file *file) 352 { 353 return file->f_file[O_RDONLY] ? 354 file->f_file[O_RDONLY] : file->f_file[O_WRONLY]; 355 } 356 357 static inline struct inode *nlmsvc_file_inode(struct nlm_file *file) 358 { 359 return file_inode(nlmsvc_file_file(file)); 360 } 361 362 static inline bool 363 nlmsvc_file_cannot_lock(const struct nlm_file *file) 364 { 365 return exportfs_cannot_lock(nlmsvc_file_file(file)->f_path.dentry->d_sb->s_export_op); 366 } 367 368 static inline int __nlm_privileged_request4(const struct sockaddr *sap) 369 { 370 const struct sockaddr_in *sin = (struct sockaddr_in *)sap; 371 372 if (ntohs(sin->sin_port) > 1023) 373 return 0; 374 375 return ipv4_is_loopback(sin->sin_addr.s_addr); 376 } 377 378 #if IS_ENABLED(CONFIG_IPV6) 379 static inline int __nlm_privileged_request6(const struct sockaddr *sap) 380 { 381 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; 382 383 if (ntohs(sin6->sin6_port) > 1023) 384 return 0; 385 386 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_MAPPED) 387 return ipv4_is_loopback(sin6->sin6_addr.s6_addr32[3]); 388 389 return ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LOOPBACK; 390 } 391 #else /* IS_ENABLED(CONFIG_IPV6) */ 392 static inline int __nlm_privileged_request6(const struct sockaddr *sap) 393 { 394 return 0; 395 } 396 #endif /* IS_ENABLED(CONFIG_IPV6) */ 397 398 /* 399 * Ensure incoming requests are from local privileged callers. 400 * 401 * Return TRUE if sender is local and is connecting via a privileged port; 402 * otherwise return FALSE. 403 */ 404 static inline int nlm_privileged_requester(const struct svc_rqst *rqstp) 405 { 406 const struct sockaddr *sap = svc_addr(rqstp); 407 408 switch (sap->sa_family) { 409 case AF_INET: 410 return __nlm_privileged_request4(sap); 411 case AF_INET6: 412 return __nlm_privileged_request6(sap); 413 default: 414 return 0; 415 } 416 } 417 418 /* 419 * Compare two NLM locks. 420 * When the second lock is of type F_UNLCK, this acts like a wildcard. 421 */ 422 static inline int nlm_compare_locks(const struct file_lock *fl1, 423 const struct file_lock *fl2) 424 { 425 return file_inode(fl1->c.flc_file) == file_inode(fl2->c.flc_file) 426 && fl1->c.flc_pid == fl2->c.flc_pid 427 && fl1->c.flc_owner == fl2->c.flc_owner 428 && fl1->fl_start == fl2->fl_start 429 && fl1->fl_end == fl2->fl_end 430 &&(fl1->c.flc_type == fl2->c.flc_type || fl2->c.flc_type == F_UNLCK); 431 } 432 433 /** 434 * lockd_set_file_lock_range3 - set the byte range of a file_lock 435 * @fl: file_lock whose length fields are to be initialized 436 * @off: starting offset of the lock, in bytes 437 * @len: length of the byte range, in bytes, or zero 438 * 439 * NLMv3 uses a (start, length) representation for lock byte ranges, 440 * while the kernel's file_lock uses (start, end). Treat a length of 441 * zero or arithmetic overflow (end wrapping negative when the sum 442 * exceeds S32_MAX) as "lock to end of file." 443 */ 444 static inline void 445 lockd_set_file_lock_range3(struct file_lock *fl, u32 off, u32 len) 446 { 447 s32 end = off + len - 1; 448 449 fl->fl_start = off; 450 if (len == 0 || end < 0) 451 fl->fl_end = OFFSET_MAX; 452 else 453 fl->fl_end = end; 454 } 455 456 /** 457 * lockd_set_file_lock_range4 - set the byte range of a file_lock 458 * @fl: file_lock whose length fields are to be initialized 459 * @off: starting offset of the lock, in bytes 460 * @len: length of the byte range, in bytes, or zero 461 * 462 * The NLMv4 protocol represents lock byte ranges as (start, length), 463 * where length zero means "lock to end of file." The kernel's file_lock 464 * structure uses (start, end) representation. Convert from NLMv4 format 465 * to file_lock format, clamping the starting offset and treating 466 * arithmetic overflow as "lock to EOF." 467 */ 468 static inline void 469 lockd_set_file_lock_range4(struct file_lock *fl, u64 off, u64 len) 470 { 471 u64 clamped_off = (off > OFFSET_MAX) ? OFFSET_MAX : off; 472 s64 end = clamped_off + len - 1; 473 474 fl->fl_start = clamped_off; 475 if (len == 0 || end < 0) 476 fl->fl_end = OFFSET_MAX; 477 else 478 fl->fl_end = end; 479 } 480 481 extern const struct lock_manager_operations nlmsvc_lock_operations; 482 483 #endif /* _LOCKD_LOCKD_H */ 484