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