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