1 /*
2  *  linux/include/linux/sunrpc/clnt.h
3  *
4  *  Declarations for the high-level RPC client interface
5  *
6  *  Copyright (C) 1995, 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8 
9 #ifndef _LINUX_SUNRPC_CLNT_H
10 #define _LINUX_SUNRPC_CLNT_H
11 
12 #include <linux/socket.h>
13 #include <linux/in.h>
14 #include <linux/in6.h>
15 
16 #include <linux/sunrpc/msg_prot.h>
17 #include <linux/sunrpc/sched.h>
18 #include <linux/sunrpc/xprt.h>
19 #include <linux/sunrpc/auth.h>
20 #include <linux/sunrpc/stats.h>
21 #include <linux/sunrpc/xdr.h>
22 #include <linux/sunrpc/timer.h>
23 #include <asm/signal.h>
24 #include <linux/path.h>
25 #include <net/ipv6.h>
26 
27 struct rpc_inode;
28 
29 /*
30  * The high-level client handle
31  */
32 struct rpc_clnt {
33 	atomic_t		cl_count;	/* Number of references */
34 	struct list_head	cl_clients;	/* Global list of clients */
35 	struct list_head	cl_tasks;	/* List of tasks */
36 	spinlock_t		cl_lock;	/* spinlock */
37 	struct rpc_xprt *	cl_xprt;	/* transport */
38 	struct rpc_procinfo *	cl_procinfo;	/* procedure info */
39 	u32			cl_prog,	/* RPC program number */
40 				cl_vers,	/* RPC version number */
41 				cl_maxproc;	/* max procedure number */
42 
43 	char *			cl_server;	/* server machine name */
44 	char *			cl_protname;	/* protocol name */
45 	struct rpc_auth *	cl_auth;	/* authenticator */
46 	struct rpc_stat *	cl_stats;	/* per-program statistics */
47 	struct rpc_iostats *	cl_metrics;	/* per-client statistics */
48 
49 	unsigned int		cl_softrtry : 1,/* soft timeouts */
50 				cl_discrtry : 1,/* disconnect before retry */
51 				cl_autobind : 1,/* use getport() */
52 				cl_chatty   : 1;/* be verbose */
53 
54 	struct rpc_rtt *	cl_rtt;		/* RTO estimator data */
55 	const struct rpc_timeout *cl_timeout;	/* Timeout strategy */
56 
57 	int			cl_nodelen;	/* nodename length */
58 	char 			cl_nodename[UNX_MAXNODENAME];
59 	struct path		cl_path;
60 	struct rpc_clnt *	cl_parent;	/* Points to parent of clones */
61 	struct rpc_rtt		cl_rtt_default;
62 	struct rpc_timeout	cl_timeout_default;
63 	struct rpc_program *	cl_program;
64 	char			cl_inline_name[32];
65 	char			*cl_principal;	/* target to authenticate to */
66 };
67 
68 /*
69  * General RPC program info
70  */
71 #define RPC_MAXVERSION		4
72 struct rpc_program {
73 	char *			name;		/* protocol name */
74 	u32			number;		/* program number */
75 	unsigned int		nrvers;		/* number of versions */
76 	struct rpc_version **	version;	/* version array */
77 	struct rpc_stat *	stats;		/* statistics */
78 	char *			pipe_dir_name;	/* path to rpc_pipefs dir */
79 };
80 
81 struct rpc_version {
82 	u32			number;		/* version number */
83 	unsigned int		nrprocs;	/* number of procs */
84 	struct rpc_procinfo *	procs;		/* procedure array */
85 };
86 
87 /*
88  * Procedure information
89  */
90 struct rpc_procinfo {
91 	u32			p_proc;		/* RPC procedure number */
92 	kxdreproc_t		p_encode;	/* XDR encode function */
93 	kxdrdproc_t		p_decode;	/* XDR decode function */
94 	unsigned int		p_arglen;	/* argument hdr length (u32) */
95 	unsigned int		p_replen;	/* reply hdr length (u32) */
96 	unsigned int		p_count;	/* call count */
97 	unsigned int		p_timer;	/* Which RTT timer to use */
98 	u32			p_statidx;	/* Which procedure to account */
99 	char *			p_name;		/* name of procedure */
100 };
101 
102 #ifdef __KERNEL__
103 
104 struct rpc_create_args {
105 	struct net		*net;
106 	int			protocol;
107 	struct sockaddr		*address;
108 	size_t			addrsize;
109 	struct sockaddr		*saddress;
110 	const struct rpc_timeout *timeout;
111 	char			*servername;
112 	struct rpc_program	*program;
113 	u32			prognumber;	/* overrides program->number */
114 	u32			version;
115 	rpc_authflavor_t	authflavor;
116 	unsigned long		flags;
117 	char			*client_name;
118 	struct svc_xprt		*bc_xprt;	/* NFSv4.1 backchannel */
119 };
120 
121 /* Values for "flags" field */
122 #define RPC_CLNT_CREATE_HARDRTRY	(1UL << 0)
123 #define RPC_CLNT_CREATE_AUTOBIND	(1UL << 2)
124 #define RPC_CLNT_CREATE_NONPRIVPORT	(1UL << 3)
125 #define RPC_CLNT_CREATE_NOPING		(1UL << 4)
126 #define RPC_CLNT_CREATE_DISCRTRY	(1UL << 5)
127 #define RPC_CLNT_CREATE_QUIET		(1UL << 6)
128 
129 struct rpc_clnt *rpc_create(struct rpc_create_args *args);
130 struct rpc_clnt	*rpc_bind_new_program(struct rpc_clnt *,
131 				struct rpc_program *, u32);
132 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt);
133 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *);
134 void		rpc_shutdown_client(struct rpc_clnt *);
135 void		rpc_release_client(struct rpc_clnt *);
136 void		rpc_task_release_client(struct rpc_task *);
137 
138 int		rpcb_register(u32, u32, int, unsigned short);
139 int		rpcb_v4_register(const u32 program, const u32 version,
140 				 const struct sockaddr *address,
141 				 const char *netid);
142 void		rpcb_getport_async(struct rpc_task *);
143 
144 void		rpc_call_start(struct rpc_task *);
145 int		rpc_call_async(struct rpc_clnt *clnt,
146 			       const struct rpc_message *msg, int flags,
147 			       const struct rpc_call_ops *tk_ops,
148 			       void *calldata);
149 int		rpc_call_sync(struct rpc_clnt *clnt,
150 			      const struct rpc_message *msg, int flags);
151 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred,
152 			       int flags);
153 int		rpc_restart_call_prepare(struct rpc_task *);
154 int		rpc_restart_call(struct rpc_task *);
155 void		rpc_setbufsize(struct rpc_clnt *, unsigned int, unsigned int);
156 size_t		rpc_max_payload(struct rpc_clnt *);
157 void		rpc_force_rebind(struct rpc_clnt *);
158 size_t		rpc_peeraddr(struct rpc_clnt *, struct sockaddr *, size_t);
159 const char	*rpc_peeraddr2str(struct rpc_clnt *, enum rpc_display_format_t);
160 
161 size_t		rpc_ntop(const struct sockaddr *, char *, const size_t);
162 size_t		rpc_pton(const char *, const size_t,
163 			 struct sockaddr *, const size_t);
164 char *		rpc_sockaddr2uaddr(const struct sockaddr *);
165 size_t		rpc_uaddr2sockaddr(const char *, const size_t,
166 				   struct sockaddr *, const size_t);
167 
rpc_get_port(const struct sockaddr * sap)168 static inline unsigned short rpc_get_port(const struct sockaddr *sap)
169 {
170 	switch (sap->sa_family) {
171 	case AF_INET:
172 		return ntohs(((struct sockaddr_in *)sap)->sin_port);
173 	case AF_INET6:
174 		return ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
175 	}
176 	return 0;
177 }
178 
rpc_set_port(struct sockaddr * sap,const unsigned short port)179 static inline void rpc_set_port(struct sockaddr *sap,
180 				const unsigned short port)
181 {
182 	switch (sap->sa_family) {
183 	case AF_INET:
184 		((struct sockaddr_in *)sap)->sin_port = htons(port);
185 		break;
186 	case AF_INET6:
187 		((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
188 		break;
189 	}
190 }
191 
192 #define IPV6_SCOPE_DELIMITER		'%'
193 #define IPV6_SCOPE_ID_LEN		sizeof("%nnnnnnnnnn")
194 
__rpc_cmp_addr4(const struct sockaddr * sap1,const struct sockaddr * sap2)195 static inline bool __rpc_cmp_addr4(const struct sockaddr *sap1,
196 				   const struct sockaddr *sap2)
197 {
198 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sap1;
199 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sap2;
200 
201 	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
202 }
203 
__rpc_copy_addr4(struct sockaddr * dst,const struct sockaddr * src)204 static inline bool __rpc_copy_addr4(struct sockaddr *dst,
205 				    const struct sockaddr *src)
206 {
207 	const struct sockaddr_in *ssin = (struct sockaddr_in *) src;
208 	struct sockaddr_in *dsin = (struct sockaddr_in *) dst;
209 
210 	dsin->sin_family = ssin->sin_family;
211 	dsin->sin_addr.s_addr = ssin->sin_addr.s_addr;
212 	return true;
213 }
214 
215 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
__rpc_cmp_addr6(const struct sockaddr * sap1,const struct sockaddr * sap2)216 static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
217 				   const struct sockaddr *sap2)
218 {
219 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sap1;
220 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sap2;
221 	return ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr);
222 }
223 
__rpc_copy_addr6(struct sockaddr * dst,const struct sockaddr * src)224 static inline bool __rpc_copy_addr6(struct sockaddr *dst,
225 				    const struct sockaddr *src)
226 {
227 	const struct sockaddr_in6 *ssin6 = (const struct sockaddr_in6 *) src;
228 	struct sockaddr_in6 *dsin6 = (struct sockaddr_in6 *) dst;
229 
230 	dsin6->sin6_family = ssin6->sin6_family;
231 	ipv6_addr_copy(&dsin6->sin6_addr, &ssin6->sin6_addr);
232 	return true;
233 }
234 #else	/* !(CONFIG_IPV6 || CONFIG_IPV6_MODULE) */
__rpc_cmp_addr6(const struct sockaddr * sap1,const struct sockaddr * sap2)235 static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
236 				   const struct sockaddr *sap2)
237 {
238 	return false;
239 }
240 
__rpc_copy_addr6(struct sockaddr * dst,const struct sockaddr * src)241 static inline bool __rpc_copy_addr6(struct sockaddr *dst,
242 				    const struct sockaddr *src)
243 {
244 	return false;
245 }
246 #endif	/* !(CONFIG_IPV6 || CONFIG_IPV6_MODULE) */
247 
248 /**
249  * rpc_cmp_addr - compare the address portion of two sockaddrs.
250  * @sap1: first sockaddr
251  * @sap2: second sockaddr
252  *
253  * Just compares the family and address portion. Ignores port, scope, etc.
254  * Returns true if the addrs are equal, false if they aren't.
255  */
rpc_cmp_addr(const struct sockaddr * sap1,const struct sockaddr * sap2)256 static inline bool rpc_cmp_addr(const struct sockaddr *sap1,
257 				const struct sockaddr *sap2)
258 {
259 	if (sap1->sa_family == sap2->sa_family) {
260 		switch (sap1->sa_family) {
261 		case AF_INET:
262 			return __rpc_cmp_addr4(sap1, sap2);
263 		case AF_INET6:
264 			return __rpc_cmp_addr6(sap1, sap2);
265 		}
266 	}
267 	return false;
268 }
269 
270 /**
271  * rpc_copy_addr - copy the address portion of one sockaddr to another
272  * @dst: destination sockaddr
273  * @src: source sockaddr
274  *
275  * Just copies the address portion and family. Ignores port, scope, etc.
276  * Caller is responsible for making certain that dst is large enough to hold
277  * the address in src. Returns true if address family is supported. Returns
278  * false otherwise.
279  */
rpc_copy_addr(struct sockaddr * dst,const struct sockaddr * src)280 static inline bool rpc_copy_addr(struct sockaddr *dst,
281 				 const struct sockaddr *src)
282 {
283 	switch (src->sa_family) {
284 	case AF_INET:
285 		return __rpc_copy_addr4(dst, src);
286 	case AF_INET6:
287 		return __rpc_copy_addr6(dst, src);
288 	}
289 	return false;
290 }
291 
292 /**
293  * rpc_get_scope_id - return scopeid for a given sockaddr
294  * @sa: sockaddr to get scopeid from
295  *
296  * Returns the value of the sin6_scope_id for AF_INET6 addrs, or 0 if
297  * not an AF_INET6 address.
298  */
rpc_get_scope_id(const struct sockaddr * sa)299 static inline u32 rpc_get_scope_id(const struct sockaddr *sa)
300 {
301 	if (sa->sa_family != AF_INET6)
302 		return 0;
303 
304 	return ((struct sockaddr_in6 *) sa)->sin6_scope_id;
305 }
306 
307 #endif /* __KERNEL__ */
308 #endif /* _LINUX_SUNRPC_CLNT_H */
309