1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * linux/include/linux/sunrpc/svc_xprt.h
4 *
5 * RPC server transport I/O
6 */
7
8 #ifndef SUNRPC_SVC_XPRT_H
9 #define SUNRPC_SVC_XPRT_H
10
11 #include <linux/sunrpc/svc.h>
12
13 struct module;
14
15 struct svc_xprt_ops {
16 struct svc_xprt *(*xpo_create)(struct svc_serv *,
17 struct net *net,
18 struct sockaddr *, int,
19 int);
20 struct svc_xprt *(*xpo_accept)(struct svc_xprt *);
21 int (*xpo_has_wspace)(struct svc_xprt *);
22 int (*xpo_recvfrom)(struct svc_rqst *);
23 int (*xpo_sendto)(struct svc_rqst *);
24 int (*xpo_result_payload)(struct svc_rqst *, unsigned int,
25 unsigned int);
26 void (*xpo_release_rqst)(struct svc_rqst *);
27 void (*xpo_detach)(struct svc_xprt *);
28 void (*xpo_free)(struct svc_xprt *);
29 void (*xpo_secure_port)(struct svc_rqst *rqstp);
30 void (*xpo_kill_temp_xprt)(struct svc_xprt *);
31 void (*xpo_start_tls)(struct svc_xprt *);
32 };
33
34 struct svc_xprt_class {
35 const char *xcl_name;
36 struct module *xcl_owner;
37 const struct svc_xprt_ops *xcl_ops;
38 struct list_head xcl_list;
39 u32 xcl_max_payload;
40 int xcl_ident;
41 };
42
43 /*
44 * This is embedded in an object that wants a callback before deleting
45 * an xprt; intended for use by NFSv4.1, which needs to know when a
46 * client's tcp connection (and hence possibly a backchannel) goes away.
47 */
48 struct svc_xpt_user {
49 struct list_head list;
50 void (*callback)(struct svc_xpt_user *);
51 };
52
53 struct svc_xprt {
54 struct svc_xprt_class *xpt_class;
55 const struct svc_xprt_ops *xpt_ops;
56 struct kref xpt_ref;
57 struct list_head xpt_list;
58 struct list_head xpt_ready;
59 unsigned long xpt_flags;
60 #define XPT_BUSY 0 /* enqueued/receiving */
61 #define XPT_CONN 1 /* conn pending */
62 #define XPT_CLOSE 2 /* dead or dying */
63 #define XPT_DATA 3 /* data pending */
64 #define XPT_TEMP 4 /* connected transport */
65 #define XPT_DEAD 6 /* transport closed */
66 #define XPT_CHNGBUF 7 /* need to change snd/rcv buf sizes */
67 #define XPT_DEFERRED 8 /* deferred request pending */
68 #define XPT_OLD 9 /* used for xprt aging mark+sweep */
69 #define XPT_LISTENER 10 /* listening endpoint */
70 #define XPT_CACHE_AUTH 11 /* cache auth info */
71 #define XPT_LOCAL 12 /* connection from loopback interface */
72 #define XPT_KILL_TEMP 13 /* call xpo_kill_temp_xprt before closing */
73 #define XPT_CONG_CTRL 14 /* has congestion control */
74
75 struct svc_serv *xpt_server; /* service for transport */
76 atomic_t xpt_reserved; /* space on outq that is rsvd */
77 atomic_t xpt_nr_rqsts; /* Number of requests */
78 struct mutex xpt_mutex; /* to serialize sending data */
79 spinlock_t xpt_lock; /* protects sk_deferred
80 * and xpt_auth_cache */
81 void *xpt_auth_cache;/* auth cache */
82 struct list_head xpt_deferred; /* deferred requests that need
83 * to be revisted */
84 struct sockaddr_storage xpt_local; /* local address */
85 size_t xpt_locallen; /* length of address */
86 struct sockaddr_storage xpt_remote; /* remote peer's address */
87 size_t xpt_remotelen; /* length of address */
88 char xpt_remotebuf[INET6_ADDRSTRLEN + 10];
89 struct list_head xpt_users; /* callbacks on free */
90
91 struct net *xpt_net;
92 netns_tracker ns_tracker;
93 const struct cred *xpt_cred;
94 struct rpc_xprt *xpt_bc_xprt; /* NFSv4.1 backchannel */
95 struct rpc_xprt_switch *xpt_bc_xps; /* NFSv4.1 backchannel */
96 };
97
unregister_xpt_user(struct svc_xprt * xpt,struct svc_xpt_user * u)98 static inline void unregister_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
99 {
100 spin_lock(&xpt->xpt_lock);
101 list_del_init(&u->list);
102 spin_unlock(&xpt->xpt_lock);
103 }
104
register_xpt_user(struct svc_xprt * xpt,struct svc_xpt_user * u)105 static inline int register_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
106 {
107 spin_lock(&xpt->xpt_lock);
108 if (test_bit(XPT_CLOSE, &xpt->xpt_flags)) {
109 /*
110 * The connection is about to be deleted soon (or,
111 * worse, may already be deleted--in which case we've
112 * already notified the xpt_users).
113 */
114 spin_unlock(&xpt->xpt_lock);
115 return -ENOTCONN;
116 }
117 list_add(&u->list, &xpt->xpt_users);
118 spin_unlock(&xpt->xpt_lock);
119 return 0;
120 }
121
svc_xprt_is_dead(const struct svc_xprt * xprt)122 static inline bool svc_xprt_is_dead(const struct svc_xprt *xprt)
123 {
124 return (test_bit(XPT_DEAD, &xprt->xpt_flags) != 0) ||
125 (test_bit(XPT_CLOSE, &xprt->xpt_flags) != 0);
126 }
127
128 int svc_reg_xprt_class(struct svc_xprt_class *);
129 void svc_unreg_xprt_class(struct svc_xprt_class *);
130 void svc_xprt_init(struct net *, struct svc_xprt_class *, struct svc_xprt *,
131 struct svc_serv *);
132 int svc_xprt_create(struct svc_serv *serv, const char *xprt_name,
133 struct net *net, const int family,
134 const unsigned short port, int flags,
135 const struct cred *cred);
136 void svc_xprt_destroy_all(struct svc_serv *serv, struct net *net);
137 void svc_xprt_received(struct svc_xprt *xprt);
138 void svc_xprt_enqueue(struct svc_xprt *xprt);
139 void svc_xprt_put(struct svc_xprt *xprt);
140 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt);
141 void svc_xprt_close(struct svc_xprt *xprt);
142 int svc_port_is_privileged(struct sockaddr *sin);
143 int svc_print_xprts(char *buf, int maxlen);
144 struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
145 struct net *net, const sa_family_t af,
146 const unsigned short port);
147 int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen);
148 void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *xprt);
149 void svc_age_temp_xprts_now(struct svc_serv *, struct sockaddr *);
150 void svc_xprt_deferred_close(struct svc_xprt *xprt);
151
svc_xprt_get(struct svc_xprt * xprt)152 static inline void svc_xprt_get(struct svc_xprt *xprt)
153 {
154 kref_get(&xprt->xpt_ref);
155 }
svc_xprt_set_local(struct svc_xprt * xprt,const struct sockaddr * sa,const size_t salen)156 static inline void svc_xprt_set_local(struct svc_xprt *xprt,
157 const struct sockaddr *sa,
158 const size_t salen)
159 {
160 memcpy(&xprt->xpt_local, sa, salen);
161 xprt->xpt_locallen = salen;
162 }
svc_xprt_set_remote(struct svc_xprt * xprt,const struct sockaddr * sa,const size_t salen)163 static inline void svc_xprt_set_remote(struct svc_xprt *xprt,
164 const struct sockaddr *sa,
165 const size_t salen)
166 {
167 memcpy(&xprt->xpt_remote, sa, salen);
168 xprt->xpt_remotelen = salen;
169 snprintf(xprt->xpt_remotebuf, sizeof(xprt->xpt_remotebuf) - 1,
170 "%pISpc", sa);
171 }
172
svc_addr_port(const struct sockaddr * sa)173 static inline unsigned short svc_addr_port(const struct sockaddr *sa)
174 {
175 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
176 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
177
178 switch (sa->sa_family) {
179 case AF_INET:
180 return ntohs(sin->sin_port);
181 case AF_INET6:
182 return ntohs(sin6->sin6_port);
183 }
184
185 return 0;
186 }
187
svc_addr_len(const struct sockaddr * sa)188 static inline size_t svc_addr_len(const struct sockaddr *sa)
189 {
190 switch (sa->sa_family) {
191 case AF_INET:
192 return sizeof(struct sockaddr_in);
193 case AF_INET6:
194 return sizeof(struct sockaddr_in6);
195 }
196 BUG();
197 }
198
svc_xprt_local_port(const struct svc_xprt * xprt)199 static inline unsigned short svc_xprt_local_port(const struct svc_xprt *xprt)
200 {
201 return svc_addr_port((const struct sockaddr *)&xprt->xpt_local);
202 }
203
svc_xprt_remote_port(const struct svc_xprt * xprt)204 static inline unsigned short svc_xprt_remote_port(const struct svc_xprt *xprt)
205 {
206 return svc_addr_port((const struct sockaddr *)&xprt->xpt_remote);
207 }
208
__svc_print_addr(const struct sockaddr * addr,char * buf,const size_t len)209 static inline char *__svc_print_addr(const struct sockaddr *addr,
210 char *buf, const size_t len)
211 {
212 const struct sockaddr_in *sin = (const struct sockaddr_in *)addr;
213 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr;
214
215 switch (addr->sa_family) {
216 case AF_INET:
217 snprintf(buf, len, "%pI4, port=%u", &sin->sin_addr,
218 ntohs(sin->sin_port));
219 break;
220
221 case AF_INET6:
222 snprintf(buf, len, "%pI6, port=%u",
223 &sin6->sin6_addr,
224 ntohs(sin6->sin6_port));
225 break;
226
227 default:
228 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
229 break;
230 }
231
232 return buf;
233 }
234 #endif /* SUNRPC_SVC_XPRT_H */
235