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