Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/net/sunrpc/svcsock.c | |
3 | * | |
4 | * These are the RPC server socket internals. | |
5 | * | |
6 | * The server scheduling algorithm does not always distribute the load | |
7 | * evenly when servicing a single client. May need to modify the | |
f6150c3c | 8 | * svc_xprt_enqueue procedure... |
1da177e4 LT |
9 | * |
10 | * TCP support is largely untested and may be a little slow. The problem | |
11 | * is that we currently do two separate recvfrom's, one for the 4-byte | |
12 | * record length, and the second for the actual record. This could possibly | |
13 | * be improved by always reading a minimum size of around 100 bytes and | |
14 | * tucking any superfluous bytes away in a temporary store. Still, that | |
15 | * leaves write requests out in the rain. An alternative may be to peek at | |
16 | * the first skb in the queue, and if it matches the next TCP sequence | |
17 | * number, to extract the record marker. Yuck. | |
18 | * | |
19 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> | |
20 | */ | |
21 | ||
172589cc | 22 | #include <linux/kernel.h> |
1da177e4 | 23 | #include <linux/sched.h> |
3a9a231d | 24 | #include <linux/module.h> |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/fcntl.h> | |
27 | #include <linux/net.h> | |
28 | #include <linux/in.h> | |
29 | #include <linux/inet.h> | |
30 | #include <linux/udp.h> | |
91483c4b | 31 | #include <linux/tcp.h> |
1da177e4 LT |
32 | #include <linux/unistd.h> |
33 | #include <linux/slab.h> | |
34 | #include <linux/netdevice.h> | |
35 | #include <linux/skbuff.h> | |
b41b66d6 | 36 | #include <linux/file.h> |
7dfb7103 | 37 | #include <linux/freezer.h> |
1da177e4 LT |
38 | #include <net/sock.h> |
39 | #include <net/checksum.h> | |
40 | #include <net/ip.h> | |
b92503b2 | 41 | #include <net/ipv6.h> |
b7872fe8 | 42 | #include <net/tcp.h> |
c752f073 | 43 | #include <net/tcp_states.h> |
1da177e4 LT |
44 | #include <asm/uaccess.h> |
45 | #include <asm/ioctls.h> | |
22911fc5 | 46 | #include <trace/events/skb.h> |
1da177e4 LT |
47 | |
48 | #include <linux/sunrpc/types.h> | |
ad06e4bd | 49 | #include <linux/sunrpc/clnt.h> |
1da177e4 | 50 | #include <linux/sunrpc/xdr.h> |
c0401ea0 | 51 | #include <linux/sunrpc/msg_prot.h> |
1da177e4 LT |
52 | #include <linux/sunrpc/svcsock.h> |
53 | #include <linux/sunrpc/stats.h> | |
4cfc7e60 | 54 | #include <linux/sunrpc/xprt.h> |
1da177e4 | 55 | |
177e4f99 HS |
56 | #include "sunrpc.h" |
57 | ||
360d8738 | 58 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
1da177e4 LT |
59 | |
60 | ||
61 | static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, | |
72c35376 | 62 | int flags); |
676d2369 | 63 | static void svc_udp_data_ready(struct sock *); |
1da177e4 LT |
64 | static int svc_udp_recvfrom(struct svc_rqst *); |
65 | static int svc_udp_sendto(struct svc_rqst *); | |
755cceab | 66 | static void svc_sock_detach(struct svc_xprt *); |
69b6ba37 | 67 | static void svc_tcp_sock_detach(struct svc_xprt *); |
755cceab | 68 | static void svc_sock_free(struct svc_xprt *); |
1da177e4 | 69 | |
b700cbb1 | 70 | static struct svc_xprt *svc_create_socket(struct svc_serv *, int, |
62832c03 PE |
71 | struct net *, struct sockaddr *, |
72 | int, int); | |
9e00abc3 | 73 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
1f11a034 AA |
74 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int, |
75 | struct net *, struct sockaddr *, | |
76 | int, int); | |
77 | static void svc_bc_sock_free(struct svc_xprt *xprt); | |
9e00abc3 | 78 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
1f11a034 | 79 | |
ed07536e PZ |
80 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
81 | static struct lock_class_key svc_key[2]; | |
82 | static struct lock_class_key svc_slock_key[2]; | |
83 | ||
0f0257ea | 84 | static void svc_reclassify_socket(struct socket *sock) |
ed07536e PZ |
85 | { |
86 | struct sock *sk = sock->sk; | |
1b7a1819 | 87 | |
fafc4e1e | 88 | if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) |
1b7a1819 WAA |
89 | return; |
90 | ||
ed07536e PZ |
91 | switch (sk->sk_family) { |
92 | case AF_INET: | |
93 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", | |
def13d74 TT |
94 | &svc_slock_key[0], |
95 | "sk_xprt.xpt_lock-AF_INET-NFSD", | |
96 | &svc_key[0]); | |
ed07536e PZ |
97 | break; |
98 | ||
99 | case AF_INET6: | |
100 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD", | |
def13d74 TT |
101 | &svc_slock_key[1], |
102 | "sk_xprt.xpt_lock-AF_INET6-NFSD", | |
103 | &svc_key[1]); | |
ed07536e PZ |
104 | break; |
105 | ||
106 | default: | |
107 | BUG(); | |
108 | } | |
109 | } | |
110 | #else | |
0f0257ea | 111 | static void svc_reclassify_socket(struct socket *sock) |
ed07536e PZ |
112 | { |
113 | } | |
114 | #endif | |
115 | ||
1da177e4 LT |
116 | /* |
117 | * Release an skbuff after use | |
118 | */ | |
5148bf4e | 119 | static void svc_release_skb(struct svc_rqst *rqstp) |
1da177e4 | 120 | { |
5148bf4e | 121 | struct sk_buff *skb = rqstp->rq_xprt_ctxt; |
1da177e4 LT |
122 | |
123 | if (skb) { | |
57b1d3ba TT |
124 | struct svc_sock *svsk = |
125 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
5148bf4e | 126 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 LT |
127 | |
128 | dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); | |
9d410c79 | 129 | skb_free_datagram_locked(svsk->sk_sk, skb); |
1da177e4 | 130 | } |
1da177e4 LT |
131 | } |
132 | ||
b92503b2 CL |
133 | union svc_pktinfo_u { |
134 | struct in_pktinfo pkti; | |
b92503b2 | 135 | struct in6_pktinfo pkti6; |
b92503b2 | 136 | }; |
bc375ea7 DM |
137 | #define SVC_PKTINFO_SPACE \ |
138 | CMSG_SPACE(sizeof(union svc_pktinfo_u)) | |
b92503b2 CL |
139 | |
140 | static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh) | |
141 | { | |
57b1d3ba TT |
142 | struct svc_sock *svsk = |
143 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
144 | switch (svsk->sk_sk->sk_family) { | |
b92503b2 CL |
145 | case AF_INET: { |
146 | struct in_pktinfo *pki = CMSG_DATA(cmh); | |
147 | ||
148 | cmh->cmsg_level = SOL_IP; | |
149 | cmh->cmsg_type = IP_PKTINFO; | |
150 | pki->ipi_ifindex = 0; | |
849a1cf1 MJ |
151 | pki->ipi_spec_dst.s_addr = |
152 | svc_daddr_in(rqstp)->sin_addr.s_addr; | |
b92503b2 CL |
153 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); |
154 | } | |
155 | break; | |
5a05ed73 | 156 | |
b92503b2 CL |
157 | case AF_INET6: { |
158 | struct in6_pktinfo *pki = CMSG_DATA(cmh); | |
849a1cf1 | 159 | struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); |
b92503b2 CL |
160 | |
161 | cmh->cmsg_level = SOL_IPV6; | |
162 | cmh->cmsg_type = IPV6_PKTINFO; | |
849a1cf1 | 163 | pki->ipi6_ifindex = daddr->sin6_scope_id; |
4e3fd7a0 | 164 | pki->ipi6_addr = daddr->sin6_addr; |
b92503b2 CL |
165 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); |
166 | } | |
167 | break; | |
b92503b2 | 168 | } |
b92503b2 CL |
169 | } |
170 | ||
1da177e4 | 171 | /* |
4cfc7e60 | 172 | * send routine intended to be shared by the fore- and back-channel |
1da177e4 | 173 | */ |
4cfc7e60 RI |
174 | int svc_send_common(struct socket *sock, struct xdr_buf *xdr, |
175 | struct page *headpage, unsigned long headoffset, | |
176 | struct page *tailpage, unsigned long tailoffset) | |
1da177e4 | 177 | { |
1da177e4 LT |
178 | int result; |
179 | int size; | |
180 | struct page **ppage = xdr->pages; | |
181 | size_t base = xdr->page_base; | |
182 | unsigned int pglen = xdr->page_len; | |
226453d8 | 183 | unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST; |
4cfc7e60 RI |
184 | int slen; |
185 | int len = 0; | |
1da177e4 LT |
186 | |
187 | slen = xdr->len; | |
188 | ||
1da177e4 LT |
189 | /* send head */ |
190 | if (slen == xdr->head[0].iov_len) | |
191 | flags = 0; | |
4cfc7e60 | 192 | len = kernel_sendpage(sock, headpage, headoffset, |
44524359 | 193 | xdr->head[0].iov_len, flags); |
1da177e4 LT |
194 | if (len != xdr->head[0].iov_len) |
195 | goto out; | |
196 | slen -= xdr->head[0].iov_len; | |
197 | if (slen == 0) | |
198 | goto out; | |
199 | ||
200 | /* send page data */ | |
201 | size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen; | |
202 | while (pglen > 0) { | |
203 | if (slen == size) | |
204 | flags = 0; | |
e6242e92 | 205 | result = kernel_sendpage(sock, *ppage, base, size, flags); |
1da177e4 LT |
206 | if (result > 0) |
207 | len += result; | |
208 | if (result != size) | |
209 | goto out; | |
210 | slen -= size; | |
211 | pglen -= size; | |
212 | size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen; | |
213 | base = 0; | |
214 | ppage++; | |
215 | } | |
4cfc7e60 | 216 | |
1da177e4 LT |
217 | /* send tail */ |
218 | if (xdr->tail[0].iov_len) { | |
4cfc7e60 RI |
219 | result = kernel_sendpage(sock, tailpage, tailoffset, |
220 | xdr->tail[0].iov_len, 0); | |
1da177e4 LT |
221 | if (result > 0) |
222 | len += result; | |
223 | } | |
4cfc7e60 RI |
224 | |
225 | out: | |
226 | return len; | |
227 | } | |
228 | ||
229 | ||
230 | /* | |
231 | * Generic sendto routine | |
232 | */ | |
233 | static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr) | |
234 | { | |
235 | struct svc_sock *svsk = | |
236 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
237 | struct socket *sock = svsk->sk_sock; | |
238 | union { | |
239 | struct cmsghdr hdr; | |
240 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
241 | } buffer; | |
242 | struct cmsghdr *cmh = &buffer.hdr; | |
243 | int len = 0; | |
244 | unsigned long tailoff; | |
245 | unsigned long headoff; | |
246 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); | |
247 | ||
248 | if (rqstp->rq_prot == IPPROTO_UDP) { | |
249 | struct msghdr msg = { | |
250 | .msg_name = &rqstp->rq_addr, | |
251 | .msg_namelen = rqstp->rq_addrlen, | |
252 | .msg_control = cmh, | |
253 | .msg_controllen = sizeof(buffer), | |
254 | .msg_flags = MSG_MORE, | |
255 | }; | |
256 | ||
257 | svc_set_cmsg_data(rqstp, cmh); | |
258 | ||
d8725c86 | 259 | if (sock_sendmsg(sock, &msg) < 0) |
4cfc7e60 RI |
260 | goto out; |
261 | } | |
262 | ||
263 | tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1); | |
264 | headoff = 0; | |
265 | len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff, | |
266 | rqstp->rq_respages[0], tailoff); | |
267 | ||
1da177e4 | 268 | out: |
ad06e4bd | 269 | dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n", |
57b1d3ba | 270 | svsk, xdr->head[0].iov_base, xdr->head[0].iov_len, |
ad06e4bd | 271 | xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf))); |
1da177e4 LT |
272 | |
273 | return len; | |
274 | } | |
275 | ||
80212d59 N |
276 | /* |
277 | * Report socket names for nfsdfs | |
278 | */ | |
e7942b9f | 279 | static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining) |
80212d59 | 280 | { |
017cb47f CL |
281 | const struct sock *sk = svsk->sk_sk; |
282 | const char *proto_name = sk->sk_protocol == IPPROTO_UDP ? | |
283 | "udp" : "tcp"; | |
80212d59 N |
284 | int len; |
285 | ||
017cb47f | 286 | switch (sk->sk_family) { |
e7942b9f CL |
287 | case PF_INET: |
288 | len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n", | |
017cb47f | 289 | proto_name, |
c720c7e8 ED |
290 | &inet_sk(sk)->inet_rcv_saddr, |
291 | inet_sk(sk)->inet_num); | |
80212d59 | 292 | break; |
c2bb06db | 293 | #if IS_ENABLED(CONFIG_IPV6) |
58de2f86 CL |
294 | case PF_INET6: |
295 | len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n", | |
017cb47f | 296 | proto_name, |
efe4208f | 297 | &sk->sk_v6_rcv_saddr, |
c720c7e8 | 298 | inet_sk(sk)->inet_num); |
80212d59 | 299 | break; |
c2bb06db | 300 | #endif |
80212d59 | 301 | default: |
e7942b9f | 302 | len = snprintf(buf, remaining, "*unknown-%d*\n", |
017cb47f | 303 | sk->sk_family); |
80212d59 | 304 | } |
e7942b9f CL |
305 | |
306 | if (len >= remaining) { | |
307 | *buf = '\0'; | |
308 | return -ENAMETOOLONG; | |
80212d59 N |
309 | } |
310 | return len; | |
311 | } | |
312 | ||
1da177e4 LT |
313 | /* |
314 | * Generic recvfrom routine. | |
315 | */ | |
0f0257ea TT |
316 | static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, |
317 | int buflen) | |
1da177e4 | 318 | { |
57b1d3ba TT |
319 | struct svc_sock *svsk = |
320 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
1ba95105 CL |
321 | struct msghdr msg = { |
322 | .msg_flags = MSG_DONTWAIT, | |
323 | }; | |
324 | int len; | |
1da177e4 | 325 | |
260c1d12 TT |
326 | rqstp->rq_xprt_hlen = 0; |
327 | ||
f8d1ff47 | 328 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1ba95105 CL |
329 | len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen, |
330 | msg.msg_flags); | |
f8d1ff47 TM |
331 | /* If we read a full record, then assume there may be more |
332 | * data to read (stream based sockets only!) | |
333 | */ | |
334 | if (len == buflen) | |
335 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
1da177e4 | 336 | |
1da177e4 | 337 | dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n", |
1ba95105 | 338 | svsk, iov[0].iov_base, iov[0].iov_len, len); |
1da177e4 LT |
339 | return len; |
340 | } | |
341 | ||
31d68ef6 BF |
342 | static int svc_partial_recvfrom(struct svc_rqst *rqstp, |
343 | struct kvec *iov, int nr, | |
344 | int buflen, unsigned int base) | |
345 | { | |
346 | size_t save_iovlen; | |
09acfea5 | 347 | void *save_iovbase; |
31d68ef6 BF |
348 | unsigned int i; |
349 | int ret; | |
350 | ||
351 | if (base == 0) | |
352 | return svc_recvfrom(rqstp, iov, nr, buflen); | |
353 | ||
354 | for (i = 0; i < nr; i++) { | |
355 | if (iov[i].iov_len > base) | |
356 | break; | |
357 | base -= iov[i].iov_len; | |
358 | } | |
359 | save_iovlen = iov[i].iov_len; | |
360 | save_iovbase = iov[i].iov_base; | |
361 | iov[i].iov_len -= base; | |
362 | iov[i].iov_base += base; | |
363 | ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen); | |
364 | iov[i].iov_len = save_iovlen; | |
365 | iov[i].iov_base = save_iovbase; | |
366 | return ret; | |
367 | } | |
368 | ||
1da177e4 LT |
369 | /* |
370 | * Set socket snd and rcv buffer lengths | |
371 | */ | |
0f0257ea TT |
372 | static void svc_sock_setbufsize(struct socket *sock, unsigned int snd, |
373 | unsigned int rcv) | |
1da177e4 LT |
374 | { |
375 | #if 0 | |
376 | mm_segment_t oldfs; | |
377 | oldfs = get_fs(); set_fs(KERNEL_DS); | |
378 | sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, | |
379 | (char*)&snd, sizeof(snd)); | |
380 | sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, | |
381 | (char*)&rcv, sizeof(rcv)); | |
382 | #else | |
383 | /* sock_setsockopt limits use to sysctl_?mem_max, | |
384 | * which isn't acceptable. Until that is made conditional | |
385 | * on not having CAP_SYS_RESOURCE or similar, we go direct... | |
386 | * DaveM said I could! | |
387 | */ | |
388 | lock_sock(sock->sk); | |
389 | sock->sk->sk_sndbuf = snd * 2; | |
390 | sock->sk->sk_rcvbuf = rcv * 2; | |
47fcb03f | 391 | sock->sk->sk_write_space(sock->sk); |
1da177e4 LT |
392 | release_sock(sock->sk); |
393 | #endif | |
394 | } | |
16e4d93f CL |
395 | |
396 | static int svc_sock_secure_port(struct svc_rqst *rqstp) | |
397 | { | |
398 | return svc_port_is_privileged(svc_addr(rqstp)); | |
399 | } | |
400 | ||
0442f14b BF |
401 | static bool sunrpc_waitqueue_active(wait_queue_head_t *wq) |
402 | { | |
403 | if (!wq) | |
404 | return false; | |
405 | /* | |
406 | * There should normally be a memory * barrier here--see | |
407 | * wq_has_sleeper(). | |
408 | * | |
409 | * It appears that isn't currently necessary, though, basically | |
410 | * because callers all appear to have sufficient memory barriers | |
411 | * between the time the relevant change is made and the | |
412 | * time they call these callbacks. | |
413 | * | |
414 | * The nfsd code itself doesn't actually explicitly wait on | |
415 | * these waitqueues, but it may wait on them for example in | |
416 | * sendpage() or sendmsg() calls. (And those may be the only | |
417 | * places, since it it uses nonblocking reads.) | |
418 | * | |
419 | * Maybe we should add the memory barriers anyway, but these are | |
420 | * hot paths so we'd need to be convinced there's no sigificant | |
421 | * penalty. | |
422 | */ | |
423 | return waitqueue_active(wq); | |
424 | } | |
425 | ||
1da177e4 LT |
426 | /* |
427 | * INET callback when data has been received on the socket. | |
428 | */ | |
676d2369 | 429 | static void svc_udp_data_ready(struct sock *sk) |
1da177e4 | 430 | { |
939bb7ef | 431 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
eaefd110 | 432 | wait_queue_head_t *wq = sk_sleep(sk); |
1da177e4 | 433 | |
939bb7ef | 434 | if (svsk) { |
676d2369 DM |
435 | dprintk("svc: socket %p(inet %p), busy=%d\n", |
436 | svsk, sk, | |
02fc6c36 TT |
437 | test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
438 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
f6150c3c | 439 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 440 | } |
0442f14b | 441 | if (sunrpc_waitqueue_active(wq)) |
eaefd110 | 442 | wake_up_interruptible(wq); |
1da177e4 LT |
443 | } |
444 | ||
445 | /* | |
446 | * INET callback when space is newly available on the socket. | |
447 | */ | |
0f0257ea | 448 | static void svc_write_space(struct sock *sk) |
1da177e4 LT |
449 | { |
450 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); | |
eaefd110 | 451 | wait_queue_head_t *wq = sk_sleep(sk); |
1da177e4 LT |
452 | |
453 | if (svsk) { | |
454 | dprintk("svc: socket %p(inet %p), write_space busy=%d\n", | |
02fc6c36 | 455 | svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
f6150c3c | 456 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 LT |
457 | } |
458 | ||
0442f14b | 459 | if (sunrpc_waitqueue_active(wq)) { |
939bb7ef | 460 | dprintk("RPC svc_write_space: someone sleeping on %p\n", |
1da177e4 | 461 | svsk); |
eaefd110 | 462 | wake_up_interruptible(wq); |
1da177e4 LT |
463 | } |
464 | } | |
465 | ||
c7fb3f06 TM |
466 | static int svc_tcp_has_wspace(struct svc_xprt *xprt) |
467 | { | |
468 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
469 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; | |
470 | int required; | |
471 | ||
472 | if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) | |
473 | return 1; | |
474 | required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg; | |
475 | if (sk_stream_wspace(svsk->sk_sk) >= required || | |
476 | (sk_stream_min_wspace(svsk->sk_sk) == 0 && | |
477 | atomic_read(&xprt->xpt_reserved) == 0)) | |
478 | return 1; | |
479 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
480 | return 0; | |
481 | } | |
482 | ||
47fcb03f TM |
483 | static void svc_tcp_write_space(struct sock *sk) |
484 | { | |
c7fb3f06 | 485 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); |
47fcb03f TM |
486 | struct socket *sock = sk->sk_socket; |
487 | ||
c7fb3f06 TM |
488 | if (!sk_stream_is_writeable(sk) || !sock) |
489 | return; | |
490 | if (!svsk || svc_tcp_has_wspace(&svsk->sk_xprt)) | |
47fcb03f TM |
491 | clear_bit(SOCK_NOSPACE, &sock->flags); |
492 | svc_write_space(sk); | |
493 | } | |
494 | ||
51877680 TM |
495 | static void svc_tcp_adjust_wspace(struct svc_xprt *xprt) |
496 | { | |
497 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
498 | ||
499 | if (svc_tcp_has_wspace(xprt)) | |
500 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
501 | } | |
502 | ||
7702ce40 CL |
503 | /* |
504 | * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo | |
505 | */ | |
506 | static int svc_udp_get_dest_address4(struct svc_rqst *rqstp, | |
507 | struct cmsghdr *cmh) | |
508 | { | |
509 | struct in_pktinfo *pki = CMSG_DATA(cmh); | |
849a1cf1 MJ |
510 | struct sockaddr_in *daddr = svc_daddr_in(rqstp); |
511 | ||
7702ce40 CL |
512 | if (cmh->cmsg_type != IP_PKTINFO) |
513 | return 0; | |
849a1cf1 MJ |
514 | |
515 | daddr->sin_family = AF_INET; | |
516 | daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr; | |
7702ce40 CL |
517 | return 1; |
518 | } | |
519 | ||
520 | /* | |
73df66f8 | 521 | * See net/ipv6/datagram.c : ip6_datagram_recv_ctl |
7702ce40 CL |
522 | */ |
523 | static int svc_udp_get_dest_address6(struct svc_rqst *rqstp, | |
524 | struct cmsghdr *cmh) | |
525 | { | |
526 | struct in6_pktinfo *pki = CMSG_DATA(cmh); | |
849a1cf1 MJ |
527 | struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); |
528 | ||
7702ce40 CL |
529 | if (cmh->cmsg_type != IPV6_PKTINFO) |
530 | return 0; | |
849a1cf1 MJ |
531 | |
532 | daddr->sin6_family = AF_INET6; | |
4e3fd7a0 | 533 | daddr->sin6_addr = pki->ipi6_addr; |
849a1cf1 | 534 | daddr->sin6_scope_id = pki->ipi6_ifindex; |
7702ce40 CL |
535 | return 1; |
536 | } | |
537 | ||
9dbc240f TT |
538 | /* |
539 | * Copy the UDP datagram's destination address to the rqstp structure. | |
540 | * The 'destination' address in this case is the address to which the | |
541 | * peer sent the datagram, i.e. our local address. For multihomed | |
542 | * hosts, this can change from msg to msg. Note that only the IP | |
543 | * address changes, the port number should remain the same. | |
544 | */ | |
7702ce40 CL |
545 | static int svc_udp_get_dest_address(struct svc_rqst *rqstp, |
546 | struct cmsghdr *cmh) | |
95756482 | 547 | { |
7702ce40 CL |
548 | switch (cmh->cmsg_level) { |
549 | case SOL_IP: | |
550 | return svc_udp_get_dest_address4(rqstp, cmh); | |
551 | case SOL_IPV6: | |
552 | return svc_udp_get_dest_address6(rqstp, cmh); | |
95756482 | 553 | } |
7702ce40 CL |
554 | |
555 | return 0; | |
95756482 CL |
556 | } |
557 | ||
1da177e4 LT |
558 | /* |
559 | * Receive a datagram from a UDP socket. | |
560 | */ | |
0f0257ea | 561 | static int svc_udp_recvfrom(struct svc_rqst *rqstp) |
1da177e4 | 562 | { |
57b1d3ba TT |
563 | struct svc_sock *svsk = |
564 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 565 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 | 566 | struct sk_buff *skb; |
bc375ea7 DM |
567 | union { |
568 | struct cmsghdr hdr; | |
569 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
570 | } buffer; | |
571 | struct cmsghdr *cmh = &buffer.hdr; | |
7a37f578 N |
572 | struct msghdr msg = { |
573 | .msg_name = svc_addr(rqstp), | |
574 | .msg_control = cmh, | |
575 | .msg_controllen = sizeof(buffer), | |
576 | .msg_flags = MSG_DONTWAIT, | |
577 | }; | |
abc5c44d CL |
578 | size_t len; |
579 | int err; | |
1da177e4 | 580 | |
02fc6c36 | 581 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
1da177e4 LT |
582 | /* udp sockets need large rcvbuf as all pending |
583 | * requests are still in that buffer. sndbuf must | |
584 | * also be large enough that there is enough space | |
3262c816 GB |
585 | * for one reply per thread. We count all threads |
586 | * rather than threads in a particular pool, which | |
587 | * provides an upper bound on the number of threads | |
588 | * which will access the socket. | |
1da177e4 LT |
589 | */ |
590 | svc_sock_setbufsize(svsk->sk_sock, | |
c6b0a9f8 N |
591 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
592 | (serv->sv_nrthreads+3) * serv->sv_max_mesg); | |
1da177e4 | 593 | |
02fc6c36 | 594 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
05ed690e N |
595 | skb = NULL; |
596 | err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, | |
597 | 0, 0, MSG_PEEK | MSG_DONTWAIT); | |
598 | if (err >= 0) | |
599 | skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err); | |
600 | ||
601 | if (skb == NULL) { | |
602 | if (err != -EAGAIN) { | |
603 | /* possibly an icmp error */ | |
604 | dprintk("svc: recvfrom returned error %d\n", -err); | |
02fc6c36 | 605 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 606 | } |
9f9d2ebe | 607 | return 0; |
1da177e4 | 608 | } |
9dbc240f | 609 | len = svc_addr_len(svc_addr(rqstp)); |
9dbc240f | 610 | rqstp->rq_addrlen = len; |
b7aa0bf7 ED |
611 | if (skb->tstamp.tv64 == 0) { |
612 | skb->tstamp = ktime_get_real(); | |
cca5172a | 613 | /* Don't enable netstamp, sunrpc doesn't |
1da177e4 LT |
614 | need that much accuracy */ |
615 | } | |
b7aa0bf7 | 616 | svsk->sk_sk->sk_stamp = skb->tstamp; |
02fc6c36 | 617 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */ |
1da177e4 | 618 | |
1da8c681 | 619 | len = skb->len; |
1da177e4 LT |
620 | rqstp->rq_arg.len = len; |
621 | ||
95756482 | 622 | rqstp->rq_prot = IPPROTO_UDP; |
27459f09 | 623 | |
7702ce40 | 624 | if (!svc_udp_get_dest_address(rqstp, cmh)) { |
e87cc472 JP |
625 | net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n", |
626 | cmh->cmsg_level, cmh->cmsg_type); | |
f23abfdb | 627 | goto out_free; |
7a37f578 | 628 | } |
849a1cf1 | 629 | rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp)); |
1da177e4 LT |
630 | |
631 | if (skb_is_nonlinear(skb)) { | |
632 | /* we have to copy */ | |
633 | local_bh_disable(); | |
634 | if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { | |
635 | local_bh_enable(); | |
636 | /* checksum error */ | |
22911fc5 | 637 | goto out_free; |
1da177e4 LT |
638 | } |
639 | local_bh_enable(); | |
9d410c79 | 640 | skb_free_datagram_locked(svsk->sk_sk, skb); |
1da177e4 LT |
641 | } else { |
642 | /* we can use it in-place */ | |
1da8c681 | 643 | rqstp->rq_arg.head[0].iov_base = skb->data; |
1da177e4 | 644 | rqstp->rq_arg.head[0].iov_len = len; |
22911fc5 ED |
645 | if (skb_checksum_complete(skb)) |
646 | goto out_free; | |
5148bf4e | 647 | rqstp->rq_xprt_ctxt = skb; |
1da177e4 LT |
648 | } |
649 | ||
650 | rqstp->rq_arg.page_base = 0; | |
651 | if (len <= rqstp->rq_arg.head[0].iov_len) { | |
652 | rqstp->rq_arg.head[0].iov_len = len; | |
653 | rqstp->rq_arg.page_len = 0; | |
44524359 | 654 | rqstp->rq_respages = rqstp->rq_pages+1; |
1da177e4 LT |
655 | } else { |
656 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; | |
44524359 | 657 | rqstp->rq_respages = rqstp->rq_pages + 1 + |
172589cc | 658 | DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); |
1da177e4 | 659 | } |
afc59400 | 660 | rqstp->rq_next_page = rqstp->rq_respages+1; |
1da177e4 LT |
661 | |
662 | if (serv->sv_stats) | |
663 | serv->sv_stats->netudpcnt++; | |
664 | ||
665 | return len; | |
f23abfdb BF |
666 | out_free: |
667 | trace_kfree_skb(skb, svc_udp_recvfrom); | |
668 | skb_free_datagram_locked(svsk->sk_sk, skb); | |
669 | return 0; | |
1da177e4 LT |
670 | } |
671 | ||
672 | static int | |
673 | svc_udp_sendto(struct svc_rqst *rqstp) | |
674 | { | |
675 | int error; | |
676 | ||
677 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
678 | if (error == -ECONNREFUSED) | |
679 | /* ICMP error on earlier request. */ | |
680 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
681 | ||
682 | return error; | |
683 | } | |
684 | ||
e831fe65 TT |
685 | static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp) |
686 | { | |
687 | } | |
688 | ||
323bee32 TT |
689 | static int svc_udp_has_wspace(struct svc_xprt *xprt) |
690 | { | |
691 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 692 | struct svc_serv *serv = xprt->xpt_server; |
323bee32 TT |
693 | unsigned long required; |
694 | ||
695 | /* | |
696 | * Set the SOCK_NOSPACE flag before checking the available | |
697 | * sock space. | |
698 | */ | |
699 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
7a90e8cc | 700 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
323bee32 TT |
701 | if (required*2 > sock_wspace(svsk->sk_sk)) |
702 | return 0; | |
703 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
704 | return 1; | |
705 | } | |
706 | ||
38a417cc TT |
707 | static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) |
708 | { | |
709 | BUG(); | |
710 | return NULL; | |
711 | } | |
712 | ||
b700cbb1 | 713 | static struct svc_xprt *svc_udp_create(struct svc_serv *serv, |
62832c03 | 714 | struct net *net, |
b700cbb1 TT |
715 | struct sockaddr *sa, int salen, |
716 | int flags) | |
717 | { | |
62832c03 | 718 | return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags); |
b700cbb1 TT |
719 | } |
720 | ||
360d8738 | 721 | static struct svc_xprt_ops svc_udp_ops = { |
b700cbb1 | 722 | .xpo_create = svc_udp_create, |
5d137990 TT |
723 | .xpo_recvfrom = svc_udp_recvfrom, |
724 | .xpo_sendto = svc_udp_sendto, | |
5148bf4e | 725 | .xpo_release_rqst = svc_release_skb, |
755cceab TT |
726 | .xpo_detach = svc_sock_detach, |
727 | .xpo_free = svc_sock_free, | |
e831fe65 | 728 | .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr, |
323bee32 | 729 | .xpo_has_wspace = svc_udp_has_wspace, |
38a417cc | 730 | .xpo_accept = svc_udp_accept, |
16e4d93f | 731 | .xpo_secure_port = svc_sock_secure_port, |
360d8738 TT |
732 | }; |
733 | ||
734 | static struct svc_xprt_class svc_udp_class = { | |
735 | .xcl_name = "udp", | |
b700cbb1 | 736 | .xcl_owner = THIS_MODULE, |
360d8738 | 737 | .xcl_ops = &svc_udp_ops, |
49023155 | 738 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, |
3c45ddf8 | 739 | .xcl_ident = XPRT_TRANSPORT_UDP, |
360d8738 TT |
740 | }; |
741 | ||
bb5cf160 | 742 | static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 | 743 | { |
7702ce40 | 744 | int err, level, optname, one = 1; |
7a37f578 | 745 | |
bd4620dd SK |
746 | svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class, |
747 | &svsk->sk_xprt, serv); | |
def13d74 | 748 | clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
749 | svsk->sk_sk->sk_data_ready = svc_udp_data_ready; |
750 | svsk->sk_sk->sk_write_space = svc_write_space; | |
1da177e4 LT |
751 | |
752 | /* initialise setting must have enough space to | |
cca5172a | 753 | * receive and respond to one request. |
1da177e4 LT |
754 | * svc_udp_recvfrom will re-adjust if necessary |
755 | */ | |
756 | svc_sock_setbufsize(svsk->sk_sock, | |
bb5cf160 TT |
757 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
758 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); | |
1da177e4 | 759 | |
0f0257ea TT |
760 | /* data might have come in before data_ready set up */ |
761 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
02fc6c36 | 762 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
7a37f578 | 763 | |
7a37f578 | 764 | /* make sure we get destination address info */ |
7702ce40 CL |
765 | switch (svsk->sk_sk->sk_family) { |
766 | case AF_INET: | |
767 | level = SOL_IP; | |
768 | optname = IP_PKTINFO; | |
769 | break; | |
770 | case AF_INET6: | |
771 | level = SOL_IPV6; | |
772 | optname = IPV6_RECVPKTINFO; | |
773 | break; | |
774 | default: | |
775 | BUG(); | |
776 | } | |
777 | err = kernel_setsockopt(svsk->sk_sock, level, optname, | |
778 | (char *)&one, sizeof(one)); | |
779 | dprintk("svc: kernel_setsockopt returned %d\n", err); | |
1da177e4 LT |
780 | } |
781 | ||
782 | /* | |
783 | * A data_ready event on a listening socket means there's a connection | |
784 | * pending. Do not use state_change as a substitute for it. | |
785 | */ | |
676d2369 | 786 | static void svc_tcp_listen_data_ready(struct sock *sk) |
1da177e4 | 787 | { |
939bb7ef | 788 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
eaefd110 | 789 | wait_queue_head_t *wq; |
1da177e4 LT |
790 | |
791 | dprintk("svc: socket %p TCP (listen) state change %d\n", | |
939bb7ef | 792 | sk, sk->sk_state); |
1da177e4 | 793 | |
939bb7ef NB |
794 | /* |
795 | * This callback may called twice when a new connection | |
796 | * is established as a child socket inherits everything | |
797 | * from a parent LISTEN socket. | |
798 | * 1) data_ready method of the parent socket will be called | |
799 | * when one of child sockets become ESTABLISHED. | |
800 | * 2) data_ready method of the child socket may be called | |
801 | * when it receives data before the socket is accepted. | |
802 | * In case of 2, we should ignore it silently. | |
803 | */ | |
804 | if (sk->sk_state == TCP_LISTEN) { | |
805 | if (svsk) { | |
02fc6c36 | 806 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 807 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef NB |
808 | } else |
809 | printk("svc: socket %p: no user data\n", sk); | |
1da177e4 | 810 | } |
939bb7ef | 811 | |
eaefd110 | 812 | wq = sk_sleep(sk); |
0442f14b | 813 | if (sunrpc_waitqueue_active(wq)) |
eaefd110 | 814 | wake_up_interruptible_all(wq); |
1da177e4 LT |
815 | } |
816 | ||
817 | /* | |
818 | * A state change on a connected socket means it's dying or dead. | |
819 | */ | |
0f0257ea | 820 | static void svc_tcp_state_change(struct sock *sk) |
1da177e4 | 821 | { |
939bb7ef | 822 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
eaefd110 | 823 | wait_queue_head_t *wq = sk_sleep(sk); |
1da177e4 LT |
824 | |
825 | dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", | |
939bb7ef | 826 | sk, sk->sk_state, sk->sk_user_data); |
1da177e4 | 827 | |
939bb7ef | 828 | if (!svsk) |
1da177e4 | 829 | printk("svc: socket %p: no user data\n", sk); |
939bb7ef | 830 | else { |
02fc6c36 | 831 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 832 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 | 833 | } |
0442f14b | 834 | if (sunrpc_waitqueue_active(wq)) |
eaefd110 | 835 | wake_up_interruptible_all(wq); |
1da177e4 LT |
836 | } |
837 | ||
676d2369 | 838 | static void svc_tcp_data_ready(struct sock *sk) |
1da177e4 | 839 | { |
939bb7ef | 840 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
eaefd110 | 841 | wait_queue_head_t *wq = sk_sleep(sk); |
1da177e4 LT |
842 | |
843 | dprintk("svc: socket %p TCP data ready (svsk %p)\n", | |
939bb7ef NB |
844 | sk, sk->sk_user_data); |
845 | if (svsk) { | |
02fc6c36 | 846 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 847 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 848 | } |
0442f14b | 849 | if (sunrpc_waitqueue_active(wq)) |
eaefd110 | 850 | wake_up_interruptible(wq); |
1da177e4 LT |
851 | } |
852 | ||
853 | /* | |
854 | * Accept a TCP connection | |
855 | */ | |
38a417cc | 856 | static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) |
1da177e4 | 857 | { |
38a417cc | 858 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
cdd88b9f AM |
859 | struct sockaddr_storage addr; |
860 | struct sockaddr *sin = (struct sockaddr *) &addr; | |
bb5cf160 | 861 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 LT |
862 | struct socket *sock = svsk->sk_sock; |
863 | struct socket *newsock; | |
1da177e4 LT |
864 | struct svc_sock *newsvsk; |
865 | int err, slen; | |
5216a8e7 | 866 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
1da177e4 LT |
867 | |
868 | dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); | |
869 | if (!sock) | |
38a417cc | 870 | return NULL; |
1da177e4 | 871 | |
02fc6c36 | 872 | clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
e6242e92 SS |
873 | err = kernel_accept(sock, &newsock, O_NONBLOCK); |
874 | if (err < 0) { | |
1da177e4 LT |
875 | if (err == -ENOMEM) |
876 | printk(KERN_WARNING "%s: no more sockets!\n", | |
877 | serv->sv_name); | |
e87cc472 JP |
878 | else if (err != -EAGAIN) |
879 | net_warn_ratelimited("%s: accept failed (err %d)!\n", | |
880 | serv->sv_name, -err); | |
38a417cc | 881 | return NULL; |
1da177e4 | 882 | } |
02fc6c36 | 883 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 884 | |
cdd88b9f | 885 | err = kernel_getpeername(newsock, sin, &slen); |
1da177e4 | 886 | if (err < 0) { |
e87cc472 JP |
887 | net_warn_ratelimited("%s: peername failed (err %d)!\n", |
888 | serv->sv_name, -err); | |
1da177e4 LT |
889 | goto failed; /* aborted connection or whatever */ |
890 | } | |
891 | ||
892 | /* Ideally, we would want to reject connections from unauthorized | |
ad06e4bd CL |
893 | * hosts here, but when we get encryption, the IP of the host won't |
894 | * tell us anything. For now just warn about unpriv connections. | |
1da177e4 | 895 | */ |
cdd88b9f | 896 | if (!svc_port_is_privileged(sin)) { |
a48fd0f9 | 897 | dprintk("%s: connect from unprivileged port: %s\n", |
cca5172a | 898 | serv->sv_name, |
cdd88b9f | 899 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 | 900 | } |
ad06e4bd | 901 | dprintk("%s: connect from %s\n", serv->sv_name, |
cdd88b9f | 902 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
903 | |
904 | /* make sure that a write doesn't block forever when | |
905 | * low on memory | |
906 | */ | |
907 | newsock->sk->sk_sndtimeo = HZ*30; | |
908 | ||
72c35376 BF |
909 | newsvsk = svc_setup_socket(serv, newsock, |
910 | (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)); | |
911 | if (IS_ERR(newsvsk)) | |
1da177e4 | 912 | goto failed; |
9dbc240f | 913 | svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen); |
a9747692 FM |
914 | err = kernel_getsockname(newsock, sin, &slen); |
915 | if (unlikely(err < 0)) { | |
916 | dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); | |
917 | slen = offsetof(struct sockaddr, sa_data); | |
918 | } | |
9dbc240f | 919 | svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen); |
067d7817 | 920 | |
ef11ce24 N |
921 | if (sock_is_loopback(newsock->sk)) |
922 | set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); | |
923 | else | |
924 | clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); | |
f9f3cc4f TT |
925 | if (serv->sv_stats) |
926 | serv->sv_stats->nettcpconn++; | |
927 | ||
928 | return &newsvsk->sk_xprt; | |
929 | ||
930 | failed: | |
931 | sock_release(newsock); | |
932 | return NULL; | |
933 | } | |
934 | ||
31d68ef6 BF |
935 | static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) |
936 | { | |
937 | unsigned int i, len, npages; | |
938 | ||
8af345f5 | 939 | if (svsk->sk_datalen == 0) |
31d68ef6 | 940 | return 0; |
8af345f5 | 941 | len = svsk->sk_datalen; |
31d68ef6 BF |
942 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
943 | for (i = 0; i < npages; i++) { | |
944 | if (rqstp->rq_pages[i] != NULL) | |
945 | put_page(rqstp->rq_pages[i]); | |
946 | BUG_ON(svsk->sk_pages[i] == NULL); | |
947 | rqstp->rq_pages[i] = svsk->sk_pages[i]; | |
948 | svsk->sk_pages[i] = NULL; | |
949 | } | |
950 | rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]); | |
951 | return len; | |
952 | } | |
953 | ||
954 | static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) | |
955 | { | |
956 | unsigned int i, len, npages; | |
957 | ||
8af345f5 | 958 | if (svsk->sk_datalen == 0) |
31d68ef6 | 959 | return; |
8af345f5 | 960 | len = svsk->sk_datalen; |
31d68ef6 BF |
961 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
962 | for (i = 0; i < npages; i++) { | |
963 | svsk->sk_pages[i] = rqstp->rq_pages[i]; | |
964 | rqstp->rq_pages[i] = NULL; | |
965 | } | |
966 | } | |
967 | ||
968 | static void svc_tcp_clear_pages(struct svc_sock *svsk) | |
969 | { | |
970 | unsigned int i, len, npages; | |
971 | ||
8af345f5 | 972 | if (svsk->sk_datalen == 0) |
31d68ef6 | 973 | goto out; |
8af345f5 | 974 | len = svsk->sk_datalen; |
31d68ef6 BF |
975 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
976 | for (i = 0; i < npages; i++) { | |
cf3aa02c BF |
977 | if (svsk->sk_pages[i] == NULL) { |
978 | WARN_ON_ONCE(1); | |
979 | continue; | |
980 | } | |
31d68ef6 BF |
981 | put_page(svsk->sk_pages[i]); |
982 | svsk->sk_pages[i] = NULL; | |
983 | } | |
984 | out: | |
985 | svsk->sk_tcplen = 0; | |
8af345f5 | 986 | svsk->sk_datalen = 0; |
31d68ef6 BF |
987 | } |
988 | ||
1da177e4 | 989 | /* |
ad46ccf0 | 990 | * Receive fragment record header. |
8f55f3c0 | 991 | * If we haven't gotten the record length yet, get the next four bytes. |
1da177e4 | 992 | */ |
8f55f3c0 | 993 | static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp) |
1da177e4 | 994 | { |
bb5cf160 | 995 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
5ee78d48 | 996 | unsigned int want; |
8f55f3c0 | 997 | int len; |
1da177e4 | 998 | |
c0401ea0 | 999 | if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) { |
1da177e4 LT |
1000 | struct kvec iov; |
1001 | ||
5ee78d48 | 1002 | want = sizeof(rpc_fraghdr) - svsk->sk_tcplen; |
1da177e4 LT |
1003 | iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; |
1004 | iov.iov_len = want; | |
1005 | if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0) | |
1006 | goto error; | |
1007 | svsk->sk_tcplen += len; | |
1008 | ||
1009 | if (len < want) { | |
c0401ea0 CL |
1010 | dprintk("svc: short recvfrom while reading record " |
1011 | "length (%d of %d)\n", len, want); | |
31d68ef6 | 1012 | return -EAGAIN; |
1da177e4 LT |
1013 | } |
1014 | ||
cc248d4b | 1015 | dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk)); |
836fbadb BF |
1016 | if (svc_sock_reclen(svsk) + svsk->sk_datalen > |
1017 | serv->sv_max_mesg) { | |
3a28e331 BF |
1018 | net_notice_ratelimited("RPC: fragment too large: %d\n", |
1019 | svc_sock_reclen(svsk)); | |
1da177e4 LT |
1020 | goto err_delete; |
1021 | } | |
1022 | } | |
1023 | ||
cc248d4b | 1024 | return svc_sock_reclen(svsk); |
31d68ef6 BF |
1025 | error: |
1026 | dprintk("RPC: TCP recv_record got %d\n", len); | |
8f55f3c0 | 1027 | return len; |
31d68ef6 | 1028 | err_delete: |
8f55f3c0 | 1029 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
8f55f3c0 AB |
1030 | return -EAGAIN; |
1031 | } | |
1032 | ||
586c52cc | 1033 | static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp) |
4cfc7e60 | 1034 | { |
586c52cc | 1035 | struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt; |
4cfc7e60 | 1036 | struct rpc_rqst *req = NULL; |
586c52cc TM |
1037 | struct kvec *src, *dst; |
1038 | __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base; | |
48e6555c BF |
1039 | __be32 xid; |
1040 | __be32 calldir; | |
4cfc7e60 | 1041 | |
4cfc7e60 RI |
1042 | xid = *p++; |
1043 | calldir = *p; | |
1044 | ||
093a1468 | 1045 | if (!bc_xprt) |
586c52cc | 1046 | return -EAGAIN; |
093a1468 TM |
1047 | spin_lock_bh(&bc_xprt->transport_lock); |
1048 | req = xprt_lookup_rqst(bc_xprt, xid); | |
1049 | if (!req) | |
1050 | goto unlock_notfound; | |
586c52cc TM |
1051 | |
1052 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf)); | |
1053 | /* | |
1054 | * XXX!: cheating for now! Only copying HEAD. | |
1055 | * But we know this is good enough for now (in fact, for any | |
1056 | * callback reply in the forseeable future). | |
1057 | */ | |
1058 | dst = &req->rq_private_buf.head[0]; | |
1059 | src = &rqstp->rq_arg.head[0]; | |
1060 | if (dst->iov_len < src->iov_len) | |
093a1468 | 1061 | goto unlock_eagain; /* whatever; just giving up. */ |
586c52cc | 1062 | memcpy(dst->iov_base, src->iov_base, src->iov_len); |
cc248d4b | 1063 | xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len); |
586c52cc | 1064 | rqstp->rq_arg.len = 0; |
093a1468 | 1065 | spin_unlock_bh(&bc_xprt->transport_lock); |
586c52cc | 1066 | return 0; |
093a1468 TM |
1067 | unlock_notfound: |
1068 | printk(KERN_NOTICE | |
1069 | "%s: Got unrecognized reply: " | |
1070 | "calldir 0x%x xpt_bc_xprt %p xid %08x\n", | |
1071 | __func__, ntohl(calldir), | |
1072 | bc_xprt, ntohl(xid)); | |
1073 | unlock_eagain: | |
1074 | spin_unlock_bh(&bc_xprt->transport_lock); | |
1075 | return -EAGAIN; | |
586c52cc TM |
1076 | } |
1077 | ||
1078 | static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len) | |
1079 | { | |
1080 | int i = 0; | |
1081 | int t = 0; | |
1082 | ||
1083 | while (t < len) { | |
1084 | vec[i].iov_base = page_address(pages[i]); | |
1085 | vec[i].iov_len = PAGE_SIZE; | |
1086 | i++; | |
1087 | t += PAGE_SIZE; | |
1088 | } | |
1089 | return i; | |
4cfc7e60 RI |
1090 | } |
1091 | ||
836fbadb BF |
1092 | static void svc_tcp_fragment_received(struct svc_sock *svsk) |
1093 | { | |
1094 | /* If we have more data, signal svc_xprt_enqueue() to try again */ | |
836fbadb BF |
1095 | dprintk("svc: TCP %s record (%d bytes)\n", |
1096 | svc_sock_final_rec(svsk) ? "final" : "nonfinal", | |
1097 | svc_sock_reclen(svsk)); | |
1098 | svsk->sk_tcplen = 0; | |
1099 | svsk->sk_reclen = 0; | |
1100 | } | |
31d68ef6 | 1101 | |
8f55f3c0 AB |
1102 | /* |
1103 | * Receive data from a TCP socket. | |
1104 | */ | |
1105 | static int svc_tcp_recvfrom(struct svc_rqst *rqstp) | |
1106 | { | |
1107 | struct svc_sock *svsk = | |
1108 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); | |
1109 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; | |
1110 | int len; | |
1111 | struct kvec *vec; | |
31d68ef6 | 1112 | unsigned int want, base; |
586c52cc TM |
1113 | __be32 *p; |
1114 | __be32 calldir; | |
5ee78d48 | 1115 | int pnum; |
8f55f3c0 AB |
1116 | |
1117 | dprintk("svc: tcp_recv %p data %d conn %d close %d\n", | |
1118 | svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), | |
1119 | test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), | |
1120 | test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); | |
1121 | ||
1122 | len = svc_tcp_recv_record(svsk, rqstp); | |
1123 | if (len < 0) | |
1124 | goto error; | |
1125 | ||
31d68ef6 | 1126 | base = svc_tcp_restore_pages(svsk, rqstp); |
836fbadb | 1127 | want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr)); |
31d68ef6 | 1128 | |
3cc03b16 | 1129 | vec = rqstp->rq_vec; |
4cfc7e60 | 1130 | |
586c52cc | 1131 | pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], |
836fbadb | 1132 | svsk->sk_datalen + want); |
586c52cc | 1133 | |
44524359 | 1134 | rqstp->rq_respages = &rqstp->rq_pages[pnum]; |
afc59400 | 1135 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
1da177e4 LT |
1136 | |
1137 | /* Now receive data */ | |
31d68ef6 | 1138 | len = svc_partial_recvfrom(rqstp, vec, pnum, want, base); |
8af345f5 | 1139 | if (len >= 0) { |
31d68ef6 | 1140 | svsk->sk_tcplen += len; |
8af345f5 BF |
1141 | svsk->sk_datalen += len; |
1142 | } | |
836fbadb | 1143 | if (len != want || !svc_sock_final_rec(svsk)) { |
be1e4444 | 1144 | svc_tcp_save_pages(svsk, rqstp); |
31d68ef6 | 1145 | if (len < 0 && len != -EAGAIN) |
ad46ccf0 | 1146 | goto err_delete; |
836fbadb BF |
1147 | if (len == want) |
1148 | svc_tcp_fragment_received(svsk); | |
1149 | else | |
3a28e331 BF |
1150 | dprintk("svc: incomplete TCP record (%d of %d)\n", |
1151 | (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)), | |
836fbadb | 1152 | svc_sock_reclen(svsk)); |
31d68ef6 BF |
1153 | goto err_noclose; |
1154 | } | |
1da177e4 | 1155 | |
1f691b07 | 1156 | if (svsk->sk_datalen < 8) { |
cf3aa02c | 1157 | svsk->sk_datalen = 0; |
ad46ccf0 | 1158 | goto err_delete; /* client is nuts. */ |
cf3aa02c | 1159 | } |
ad46ccf0 | 1160 | |
836fbadb | 1161 | rqstp->rq_arg.len = svsk->sk_datalen; |
1da177e4 | 1162 | rqstp->rq_arg.page_base = 0; |
5ee78d48 TM |
1163 | if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) { |
1164 | rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len; | |
1da177e4 | 1165 | rqstp->rq_arg.page_len = 0; |
5ee78d48 TM |
1166 | } else |
1167 | rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; | |
1da177e4 | 1168 | |
5148bf4e | 1169 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 | 1170 | rqstp->rq_prot = IPPROTO_TCP; |
7501cc2b JL |
1171 | if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags)) |
1172 | set_bit(RQ_LOCAL, &rqstp->rq_flags); | |
1173 | else | |
1174 | clear_bit(RQ_LOCAL, &rqstp->rq_flags); | |
1da177e4 | 1175 | |
586c52cc TM |
1176 | p = (__be32 *)rqstp->rq_arg.head[0].iov_base; |
1177 | calldir = p[1]; | |
8985ef0b | 1178 | if (calldir) |
586c52cc | 1179 | len = receive_cb_reply(svsk, rqstp); |
586c52cc | 1180 | |
1da177e4 | 1181 | /* Reset TCP read info */ |
8af345f5 | 1182 | svsk->sk_datalen = 0; |
836fbadb | 1183 | svc_tcp_fragment_received(svsk); |
0601f793 | 1184 | |
8985ef0b BF |
1185 | if (len < 0) |
1186 | goto error; | |
1da177e4 | 1187 | |
9dbc240f | 1188 | svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt); |
1da177e4 LT |
1189 | if (serv->sv_stats) |
1190 | serv->sv_stats->nettcpcnt++; | |
1191 | ||
5ee78d48 | 1192 | return rqstp->rq_arg.len; |
1da177e4 | 1193 | |
8f55f3c0 | 1194 | error: |
31d68ef6 | 1195 | if (len != -EAGAIN) |
ad46ccf0 | 1196 | goto err_delete; |
31d68ef6 | 1197 | dprintk("RPC: TCP recvfrom got EAGAIN\n"); |
9f9d2ebe | 1198 | return 0; |
ad46ccf0 | 1199 | err_delete: |
31d68ef6 BF |
1200 | printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", |
1201 | svsk->sk_xprt.xpt_server->sv_name, -len); | |
1202 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); | |
1203 | err_noclose: | |
9f9d2ebe | 1204 | return 0; /* record not complete */ |
1da177e4 LT |
1205 | } |
1206 | ||
1207 | /* | |
1208 | * Send out data on TCP socket. | |
1209 | */ | |
0f0257ea | 1210 | static int svc_tcp_sendto(struct svc_rqst *rqstp) |
1da177e4 LT |
1211 | { |
1212 | struct xdr_buf *xbufp = &rqstp->rq_res; | |
1213 | int sent; | |
d8ed029d | 1214 | __be32 reclen; |
1da177e4 LT |
1215 | |
1216 | /* Set up the first element of the reply kvec. | |
1217 | * Any other kvecs that may be in use have been taken | |
1218 | * care of by the server implementation itself. | |
1219 | */ | |
1220 | reclen = htonl(0x80000000|((xbufp->len ) - 4)); | |
1221 | memcpy(xbufp->head[0].iov_base, &reclen, 4); | |
1222 | ||
1da177e4 LT |
1223 | sent = svc_sendto(rqstp, &rqstp->rq_res); |
1224 | if (sent != xbufp->len) { | |
0f0257ea TT |
1225 | printk(KERN_NOTICE |
1226 | "rpc-srv/tcp: %s: %s %d when sending %d bytes " | |
1227 | "- shutting down socket\n", | |
57b1d3ba | 1228 | rqstp->rq_xprt->xpt_server->sv_name, |
1da177e4 LT |
1229 | (sent<0)?"got error":"sent only", |
1230 | sent, xbufp->len); | |
57b1d3ba | 1231 | set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags); |
f6150c3c | 1232 | svc_xprt_enqueue(rqstp->rq_xprt); |
1da177e4 LT |
1233 | sent = -EAGAIN; |
1234 | } | |
1235 | return sent; | |
1236 | } | |
1237 | ||
e831fe65 TT |
1238 | /* |
1239 | * Setup response header. TCP has a 4B record length field. | |
1240 | */ | |
1241 | static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp) | |
1242 | { | |
1243 | struct kvec *resv = &rqstp->rq_res.head[0]; | |
1244 | ||
1245 | /* tcp needs a space for the record length... */ | |
1246 | svc_putnl(resv, 0); | |
1247 | } | |
1248 | ||
b700cbb1 | 1249 | static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, |
62832c03 | 1250 | struct net *net, |
b700cbb1 TT |
1251 | struct sockaddr *sa, int salen, |
1252 | int flags) | |
1253 | { | |
62832c03 | 1254 | return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags); |
b700cbb1 TT |
1255 | } |
1256 | ||
9e00abc3 | 1257 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
1f11a034 AA |
1258 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int, |
1259 | struct net *, struct sockaddr *, | |
1260 | int, int); | |
1261 | static void svc_bc_sock_free(struct svc_xprt *xprt); | |
1262 | ||
1263 | static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv, | |
1264 | struct net *net, | |
1265 | struct sockaddr *sa, int salen, | |
1266 | int flags) | |
1267 | { | |
1268 | return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags); | |
1269 | } | |
1270 | ||
1271 | static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt) | |
1272 | { | |
1273 | } | |
1274 | ||
1275 | static struct svc_xprt_ops svc_tcp_bc_ops = { | |
1276 | .xpo_create = svc_bc_tcp_create, | |
1277 | .xpo_detach = svc_bc_tcp_sock_detach, | |
1278 | .xpo_free = svc_bc_sock_free, | |
1279 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, | |
16e4d93f | 1280 | .xpo_secure_port = svc_sock_secure_port, |
1f11a034 AA |
1281 | }; |
1282 | ||
1283 | static struct svc_xprt_class svc_tcp_bc_class = { | |
1284 | .xcl_name = "tcp-bc", | |
1285 | .xcl_owner = THIS_MODULE, | |
1286 | .xcl_ops = &svc_tcp_bc_ops, | |
1287 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, | |
1288 | }; | |
16b2d1e1 AA |
1289 | |
1290 | static void svc_init_bc_xprt_sock(void) | |
1291 | { | |
1292 | svc_reg_xprt_class(&svc_tcp_bc_class); | |
1293 | } | |
1294 | ||
1295 | static void svc_cleanup_bc_xprt_sock(void) | |
1296 | { | |
1297 | svc_unreg_xprt_class(&svc_tcp_bc_class); | |
1298 | } | |
9e00abc3 | 1299 | #else /* CONFIG_SUNRPC_BACKCHANNEL */ |
16b2d1e1 AA |
1300 | static void svc_init_bc_xprt_sock(void) |
1301 | { | |
1302 | } | |
1303 | ||
1304 | static void svc_cleanup_bc_xprt_sock(void) | |
1305 | { | |
1306 | } | |
9e00abc3 | 1307 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
1f11a034 | 1308 | |
360d8738 | 1309 | static struct svc_xprt_ops svc_tcp_ops = { |
b700cbb1 | 1310 | .xpo_create = svc_tcp_create, |
5d137990 TT |
1311 | .xpo_recvfrom = svc_tcp_recvfrom, |
1312 | .xpo_sendto = svc_tcp_sendto, | |
5148bf4e | 1313 | .xpo_release_rqst = svc_release_skb, |
69b6ba37 | 1314 | .xpo_detach = svc_tcp_sock_detach, |
755cceab | 1315 | .xpo_free = svc_sock_free, |
e831fe65 | 1316 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
323bee32 | 1317 | .xpo_has_wspace = svc_tcp_has_wspace, |
38a417cc | 1318 | .xpo_accept = svc_tcp_accept, |
16e4d93f | 1319 | .xpo_secure_port = svc_sock_secure_port, |
51877680 | 1320 | .xpo_adjust_wspace = svc_tcp_adjust_wspace, |
360d8738 TT |
1321 | }; |
1322 | ||
1323 | static struct svc_xprt_class svc_tcp_class = { | |
1324 | .xcl_name = "tcp", | |
b700cbb1 | 1325 | .xcl_owner = THIS_MODULE, |
360d8738 | 1326 | .xcl_ops = &svc_tcp_ops, |
49023155 | 1327 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
3c45ddf8 | 1328 | .xcl_ident = XPRT_TRANSPORT_TCP, |
360d8738 TT |
1329 | }; |
1330 | ||
1331 | void svc_init_xprt_sock(void) | |
1332 | { | |
1333 | svc_reg_xprt_class(&svc_tcp_class); | |
1334 | svc_reg_xprt_class(&svc_udp_class); | |
16b2d1e1 | 1335 | svc_init_bc_xprt_sock(); |
360d8738 TT |
1336 | } |
1337 | ||
1338 | void svc_cleanup_xprt_sock(void) | |
1339 | { | |
1340 | svc_unreg_xprt_class(&svc_tcp_class); | |
1341 | svc_unreg_xprt_class(&svc_udp_class); | |
16b2d1e1 | 1342 | svc_cleanup_bc_xprt_sock(); |
360d8738 TT |
1343 | } |
1344 | ||
bb5cf160 | 1345 | static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 LT |
1346 | { |
1347 | struct sock *sk = svsk->sk_sk; | |
1da177e4 | 1348 | |
bd4620dd SK |
1349 | svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class, |
1350 | &svsk->sk_xprt, serv); | |
def13d74 | 1351 | set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1352 | if (sk->sk_state == TCP_LISTEN) { |
1353 | dprintk("setting up TCP socket for listening\n"); | |
02fc6c36 | 1354 | set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1355 | sk->sk_data_ready = svc_tcp_listen_data_ready; |
02fc6c36 | 1356 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1357 | } else { |
1358 | dprintk("setting up TCP socket for reading\n"); | |
1359 | sk->sk_state_change = svc_tcp_state_change; | |
1360 | sk->sk_data_ready = svc_tcp_data_ready; | |
47fcb03f | 1361 | sk->sk_write_space = svc_tcp_write_space; |
1da177e4 LT |
1362 | |
1363 | svsk->sk_reclen = 0; | |
1364 | svsk->sk_tcplen = 0; | |
8af345f5 | 1365 | svsk->sk_datalen = 0; |
31d68ef6 | 1366 | memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages)); |
1da177e4 | 1367 | |
b7872fe8 | 1368 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
1da177e4 | 1369 | |
02fc6c36 | 1370 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
cca5172a | 1371 | if (sk->sk_state != TCP_ESTABLISHED) |
02fc6c36 | 1372 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1373 | } |
1374 | } | |
1375 | ||
0f0257ea | 1376 | void svc_sock_update_bufs(struct svc_serv *serv) |
1da177e4 LT |
1377 | { |
1378 | /* | |
1379 | * The number of server threads has changed. Update | |
1380 | * rcvbuf and sndbuf accordingly on all sockets | |
1381 | */ | |
8f3a6de3 | 1382 | struct svc_sock *svsk; |
1da177e4 LT |
1383 | |
1384 | spin_lock_bh(&serv->sv_lock); | |
8f3a6de3 | 1385 | list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) |
02fc6c36 | 1386 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1387 | spin_unlock_bh(&serv->sv_lock); |
1388 | } | |
24c3767e | 1389 | EXPORT_SYMBOL_GPL(svc_sock_update_bufs); |
1da177e4 | 1390 | |
1da177e4 LT |
1391 | /* |
1392 | * Initialize socket for RPC use and create svc_sock struct | |
1da177e4 | 1393 | */ |
6b174337 CL |
1394 | static struct svc_sock *svc_setup_socket(struct svc_serv *serv, |
1395 | struct socket *sock, | |
72c35376 | 1396 | int flags) |
1da177e4 LT |
1397 | { |
1398 | struct svc_sock *svsk; | |
1399 | struct sock *inet; | |
6b174337 | 1400 | int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); |
72c35376 | 1401 | int err = 0; |
1da177e4 LT |
1402 | |
1403 | dprintk("svc: svc_setup_socket %p\n", sock); | |
72c35376 BF |
1404 | svsk = kzalloc(sizeof(*svsk), GFP_KERNEL); |
1405 | if (!svsk) | |
1406 | return ERR_PTR(-ENOMEM); | |
1da177e4 LT |
1407 | |
1408 | inet = sock->sk; | |
1409 | ||
1410 | /* Register socket with portmapper */ | |
72c35376 BF |
1411 | if (pmap_register) |
1412 | err = svc_register(serv, sock_net(sock->sk), inet->sk_family, | |
5247fab5 | 1413 | inet->sk_protocol, |
c720c7e8 | 1414 | ntohs(inet_sk(inet)->inet_sport)); |
1da177e4 | 1415 | |
72c35376 | 1416 | if (err < 0) { |
1da177e4 | 1417 | kfree(svsk); |
72c35376 | 1418 | return ERR_PTR(err); |
1da177e4 LT |
1419 | } |
1420 | ||
1da177e4 LT |
1421 | inet->sk_user_data = svsk; |
1422 | svsk->sk_sock = sock; | |
1423 | svsk->sk_sk = inet; | |
1424 | svsk->sk_ostate = inet->sk_state_change; | |
1425 | svsk->sk_odata = inet->sk_data_ready; | |
1426 | svsk->sk_owspace = inet->sk_write_space; | |
1da177e4 LT |
1427 | |
1428 | /* Initialize the socket */ | |
1429 | if (sock->type == SOCK_DGRAM) | |
bb5cf160 | 1430 | svc_udp_init(svsk, serv); |
96604398 OK |
1431 | else { |
1432 | /* initialise setting must have enough space to | |
1433 | * receive and respond to one request. | |
1434 | */ | |
1435 | svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg, | |
1436 | 4 * serv->sv_max_mesg); | |
bb5cf160 | 1437 | svc_tcp_init(svsk, serv); |
96604398 | 1438 | } |
1da177e4 | 1439 | |
1da177e4 LT |
1440 | dprintk("svc: svc_setup_socket created %p (inet %p)\n", |
1441 | svsk, svsk->sk_sk); | |
1442 | ||
1da177e4 LT |
1443 | return svsk; |
1444 | } | |
1445 | ||
30646394 SK |
1446 | bool svc_alien_sock(struct net *net, int fd) |
1447 | { | |
1448 | int err; | |
1449 | struct socket *sock = sockfd_lookup(fd, &err); | |
1450 | bool ret = false; | |
1451 | ||
1452 | if (!sock) | |
1453 | goto out; | |
1454 | if (sock_net(sock->sk) != net) | |
1455 | ret = true; | |
1456 | sockfd_put(sock); | |
1457 | out: | |
1458 | return ret; | |
1459 | } | |
1460 | EXPORT_SYMBOL_GPL(svc_alien_sock); | |
1461 | ||
bfba9ab4 CL |
1462 | /** |
1463 | * svc_addsock - add a listener socket to an RPC service | |
1464 | * @serv: pointer to RPC service to which to add a new listener | |
1465 | * @fd: file descriptor of the new listener | |
1466 | * @name_return: pointer to buffer to fill in with name of listener | |
1467 | * @len: size of the buffer | |
1468 | * | |
1469 | * Fills in socket name and returns positive length of name if successful. | |
1470 | * Name is terminated with '\n'. On error, returns a negative errno | |
1471 | * value. | |
1472 | */ | |
1473 | int svc_addsock(struct svc_serv *serv, const int fd, char *name_return, | |
1474 | const size_t len) | |
b41b66d6 N |
1475 | { |
1476 | int err = 0; | |
1477 | struct socket *so = sockfd_lookup(fd, &err); | |
1478 | struct svc_sock *svsk = NULL; | |
a8e10078 BF |
1479 | struct sockaddr_storage addr; |
1480 | struct sockaddr *sin = (struct sockaddr *)&addr; | |
1481 | int salen; | |
b41b66d6 N |
1482 | |
1483 | if (!so) | |
1484 | return err; | |
a8e10078 | 1485 | err = -EAFNOSUPPORT; |
205ba423 | 1486 | if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6)) |
a8e10078 BF |
1487 | goto out; |
1488 | err = -EPROTONOSUPPORT; | |
1489 | if (so->sk->sk_protocol != IPPROTO_TCP && | |
b41b66d6 | 1490 | so->sk->sk_protocol != IPPROTO_UDP) |
a8e10078 BF |
1491 | goto out; |
1492 | err = -EISCONN; | |
1493 | if (so->state > SS_UNCONNECTED) | |
1494 | goto out; | |
1495 | err = -ENOENT; | |
1496 | if (!try_module_get(THIS_MODULE)) | |
1497 | goto out; | |
1498 | svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS); | |
1499 | if (IS_ERR(svsk)) { | |
1500 | module_put(THIS_MODULE); | |
1501 | err = PTR_ERR(svsk); | |
1502 | goto out; | |
b41b66d6 | 1503 | } |
a8e10078 BF |
1504 | if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0) |
1505 | svc_xprt_set_local(&svsk->sk_xprt, sin, salen); | |
39b55301 | 1506 | svc_add_new_perm_xprt(serv, &svsk->sk_xprt); |
e7942b9f | 1507 | return svc_one_sock_name(svsk, name_return, len); |
a8e10078 BF |
1508 | out: |
1509 | sockfd_put(so); | |
1510 | return err; | |
b41b66d6 N |
1511 | } |
1512 | EXPORT_SYMBOL_GPL(svc_addsock); | |
1513 | ||
1da177e4 LT |
1514 | /* |
1515 | * Create socket for RPC service. | |
1516 | */ | |
b700cbb1 TT |
1517 | static struct svc_xprt *svc_create_socket(struct svc_serv *serv, |
1518 | int protocol, | |
62832c03 | 1519 | struct net *net, |
b700cbb1 TT |
1520 | struct sockaddr *sin, int len, |
1521 | int flags) | |
1da177e4 LT |
1522 | { |
1523 | struct svc_sock *svsk; | |
1524 | struct socket *sock; | |
1525 | int error; | |
1526 | int type; | |
9dbc240f TT |
1527 | struct sockaddr_storage addr; |
1528 | struct sockaddr *newsin = (struct sockaddr *)&addr; | |
1529 | int newlen; | |
c69da774 TM |
1530 | int family; |
1531 | int val; | |
5216a8e7 | 1532 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
1da177e4 | 1533 | |
ad06e4bd CL |
1534 | dprintk("svc: svc_create_socket(%s, %d, %s)\n", |
1535 | serv->sv_program->pg_name, protocol, | |
77f1f67a | 1536 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
1537 | |
1538 | if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { | |
1539 | printk(KERN_WARNING "svc: only UDP and TCP " | |
1540 | "sockets supported\n"); | |
b700cbb1 | 1541 | return ERR_PTR(-EINVAL); |
1da177e4 | 1542 | } |
c69da774 | 1543 | |
1da177e4 | 1544 | type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; |
c69da774 TM |
1545 | switch (sin->sa_family) { |
1546 | case AF_INET6: | |
1547 | family = PF_INET6; | |
1548 | break; | |
1549 | case AF_INET: | |
1550 | family = PF_INET; | |
1551 | break; | |
1552 | default: | |
1553 | return ERR_PTR(-EINVAL); | |
1554 | } | |
1da177e4 | 1555 | |
14ec63c3 | 1556 | error = __sock_create(net, family, type, protocol, &sock, 1); |
77f1f67a | 1557 | if (error < 0) |
b700cbb1 | 1558 | return ERR_PTR(error); |
1da177e4 | 1559 | |
ed07536e PZ |
1560 | svc_reclassify_socket(sock); |
1561 | ||
c69da774 TM |
1562 | /* |
1563 | * If this is an PF_INET6 listener, we want to avoid | |
1564 | * getting requests from IPv4 remotes. Those should | |
1565 | * be shunted to a PF_INET listener via rpcbind. | |
1566 | */ | |
1567 | val = 1; | |
1568 | if (family == PF_INET6) | |
1569 | kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, | |
1570 | (char *)&val, sizeof(val)); | |
1571 | ||
18114746 | 1572 | if (type == SOCK_STREAM) |
4a17fd52 | 1573 | sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */ |
77f1f67a | 1574 | error = kernel_bind(sock, sin, len); |
18114746 ES |
1575 | if (error < 0) |
1576 | goto bummer; | |
1da177e4 | 1577 | |
9dbc240f TT |
1578 | newlen = len; |
1579 | error = kernel_getsockname(sock, newsin, &newlen); | |
1580 | if (error < 0) | |
1581 | goto bummer; | |
1582 | ||
1da177e4 | 1583 | if (protocol == IPPROTO_TCP) { |
e6242e92 | 1584 | if ((error = kernel_listen(sock, 64)) < 0) |
1da177e4 LT |
1585 | goto bummer; |
1586 | } | |
1587 | ||
72c35376 | 1588 | svsk = svc_setup_socket(serv, sock, flags); |
a8e10078 BF |
1589 | if (IS_ERR(svsk)) { |
1590 | error = PTR_ERR(svsk); | |
1591 | goto bummer; | |
e79eff1f | 1592 | } |
a8e10078 BF |
1593 | svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen); |
1594 | return (struct svc_xprt *)svsk; | |
1da177e4 LT |
1595 | bummer: |
1596 | dprintk("svc: svc_create_socket error = %d\n", -error); | |
1597 | sock_release(sock); | |
b700cbb1 | 1598 | return ERR_PTR(error); |
1da177e4 LT |
1599 | } |
1600 | ||
755cceab TT |
1601 | /* |
1602 | * Detach the svc_sock from the socket so that no | |
1603 | * more callbacks occur. | |
1604 | */ | |
1605 | static void svc_sock_detach(struct svc_xprt *xprt) | |
1606 | { | |
1607 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1608 | struct sock *sk = svsk->sk_sk; | |
eaefd110 | 1609 | wait_queue_head_t *wq; |
755cceab TT |
1610 | |
1611 | dprintk("svc: svc_sock_detach(%p)\n", svsk); | |
1612 | ||
1613 | /* put back the old socket callbacks */ | |
1614 | sk->sk_state_change = svsk->sk_ostate; | |
1615 | sk->sk_data_ready = svsk->sk_odata; | |
1616 | sk->sk_write_space = svsk->sk_owspace; | |
69b6ba37 | 1617 | |
eaefd110 | 1618 | wq = sk_sleep(sk); |
0442f14b | 1619 | if (sunrpc_waitqueue_active(wq)) |
eaefd110 | 1620 | wake_up_interruptible(wq); |
69b6ba37 TM |
1621 | } |
1622 | ||
1623 | /* | |
1624 | * Disconnect the socket, and reset the callbacks | |
1625 | */ | |
1626 | static void svc_tcp_sock_detach(struct svc_xprt *xprt) | |
1627 | { | |
1628 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1629 | ||
1630 | dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk); | |
1631 | ||
1632 | svc_sock_detach(xprt); | |
1633 | ||
31d68ef6 BF |
1634 | if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) { |
1635 | svc_tcp_clear_pages(svsk); | |
69b6ba37 | 1636 | kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR); |
31d68ef6 | 1637 | } |
755cceab TT |
1638 | } |
1639 | ||
1640 | /* | |
1641 | * Free the svc_sock's socket resources and the svc_sock itself. | |
1642 | */ | |
1643 | static void svc_sock_free(struct svc_xprt *xprt) | |
1644 | { | |
1645 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1646 | dprintk("svc: svc_sock_free(%p)\n", svsk); | |
1647 | ||
755cceab TT |
1648 | if (svsk->sk_sock->file) |
1649 | sockfd_put(svsk->sk_sock); | |
1650 | else | |
1651 | sock_release(svsk->sk_sock); | |
1652 | kfree(svsk); | |
1653 | } | |
7652e5a0 | 1654 | |
9e00abc3 | 1655 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
7652e5a0 | 1656 | /* |
1f11a034 | 1657 | * Create a back channel svc_xprt which shares the fore channel socket. |
7652e5a0 | 1658 | */ |
1f11a034 AA |
1659 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv, |
1660 | int protocol, | |
1661 | struct net *net, | |
1662 | struct sockaddr *sin, int len, | |
1663 | int flags) | |
7652e5a0 BH |
1664 | { |
1665 | struct svc_sock *svsk; | |
1f11a034 AA |
1666 | struct svc_xprt *xprt; |
1667 | ||
1668 | if (protocol != IPPROTO_TCP) { | |
1669 | printk(KERN_WARNING "svc: only TCP sockets" | |
1670 | " supported on shared back channel\n"); | |
1671 | return ERR_PTR(-EINVAL); | |
1672 | } | |
7652e5a0 | 1673 | |
7652e5a0 BH |
1674 | svsk = kzalloc(sizeof(*svsk), GFP_KERNEL); |
1675 | if (!svsk) | |
1f11a034 | 1676 | return ERR_PTR(-ENOMEM); |
7652e5a0 BH |
1677 | |
1678 | xprt = &svsk->sk_xprt; | |
bd4620dd | 1679 | svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv); |
1f11a034 | 1680 | |
4a19de0f | 1681 | serv->sv_bc_xprt = xprt; |
1f11a034 | 1682 | |
7652e5a0 BH |
1683 | return xprt; |
1684 | } | |
7652e5a0 BH |
1685 | |
1686 | /* | |
1f11a034 | 1687 | * Free a back channel svc_sock. |
7652e5a0 | 1688 | */ |
1f11a034 | 1689 | static void svc_bc_sock_free(struct svc_xprt *xprt) |
7652e5a0 | 1690 | { |
778be232 | 1691 | if (xprt) |
7652e5a0 BH |
1692 | kfree(container_of(xprt, struct svc_sock, sk_xprt)); |
1693 | } | |
9e00abc3 | 1694 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |