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