projects/linux·blob master · refs

1697 lines · 43.3 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/net/sunrpc/svcsock.c
4 *
5 * These are the RPC server socket internals.
6 *
7 * The server scheduling algorithm does not always distribute the load
8 * evenly when servicing a single client. May need to modify the
9 * svc_xprt_enqueue procedure...
10 *
11 * TCP support is largely untested and may be a little slow. The problem
12 * is that we currently do two separate recvfrom's, one for the 4-byte
13 * record length, and the second for the actual record. This could possibly
14 * be improved by always reading a minimum size of around 100 bytes and
15 * tucking any superfluous bytes away in a temporary store. Still, that
16 * leaves write requests out in the rain. An alternative may be to peek at
17 * the first skb in the queue, and if it matches the next TCP sequence
18 * number, to extract the record marker. Yuck.
19 *
20 * Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
21 */
22
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/module.h>
26#include <linux/errno.h>
27#include <linux/fcntl.h>
28#include <linux/net.h>
29#include <linux/in.h>
30#include <linux/inet.h>
31#include <linux/udp.h>
32#include <linux/tcp.h>
33#include <linux/unistd.h>
34#include <linux/slab.h>
35#include <linux/netdevice.h>
36#include <linux/skbuff.h>
37#include <linux/file.h>
38#include <linux/freezer.h>
39#include <linux/bvec.h>
40
41#include <net/sock.h>
42#include <net/checksum.h>
43#include <net/ip.h>
44#include <net/ipv6.h>
45#include <net/udp.h>
46#include <net/tcp.h>
47#include <net/tcp_states.h>
48#include <net/tls_prot.h>
49#include <net/handshake.h>
50#include <linux/uaccess.h>
51#include <linux/highmem.h>
52#include <asm/ioctls.h>
53#include <linux/key.h>
54
55#include <linux/sunrpc/types.h>
56#include <linux/sunrpc/clnt.h>
57#include <linux/sunrpc/xdr.h>
58#include <linux/sunrpc/msg_prot.h>
59#include <linux/sunrpc/svcsock.h>
60#include <linux/sunrpc/stats.h>
61#include <linux/sunrpc/xprt.h>
62
63#include <trace/events/sock.h>
64#include <trace/events/sunrpc.h>
65
66#include "socklib.h"
67#include "sunrpc.h"
68
69#define RPCDBG_FACILITY RPCDBG_SVCXPRT
70
71/*
72 * For UDP:
73 * 1 for header page
74 * enough pages for RPCSVC_MAXPAYLOAD_UDP
75 * 1 in case payload is not aligned
76 * 1 for tail page
77 */
78enum {
79 SUNRPC_MAX_UDP_SENDPAGES = 1 + RPCSVC_MAXPAYLOAD_UDP / PAGE_SIZE + 1 + 1
80};
81
82/* To-do: to avoid tying up an nfsd thread while waiting for a
83 * handshake request, the request could instead be deferred.
84 */
85enum {
86 SVC_HANDSHAKE_TO = 5U * HZ
87};
88
89static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
90 int flags);
91static int svc_udp_recvfrom(struct svc_rqst *);
92static int svc_udp_sendto(struct svc_rqst *);
93static void svc_sock_detach(struct svc_xprt *);
94static void svc_tcp_sock_detach(struct svc_xprt *);
95static void svc_sock_free(struct svc_xprt *);
96
97static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
98 struct net *, struct sockaddr *,
99 int, int);
100#ifdef CONFIG_DEBUG_LOCK_ALLOC
101static struct lock_class_key svc_key[2];
102static struct lock_class_key svc_slock_key[2];
103
104static void svc_reclassify_socket(struct socket *sock)
105{
106 struct sock *sk = sock->sk;
107
108 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
109 return;
110
111 switch (sk->sk_family) {
112 case AF_INET:
113 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
114 &svc_slock_key[0],
115 "sk_xprt.xpt_lock-AF_INET-NFSD",
116 &svc_key[0]);
117 break;
118
119 case AF_INET6:
120 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
121 &svc_slock_key[1],
122 "sk_xprt.xpt_lock-AF_INET6-NFSD",
123 &svc_key[1]);
124 break;
125
126 default:
127 BUG();
128 }
129}
130#else
131static void svc_reclassify_socket(struct socket *sock)
132{
133}
134#endif
135
136/**
137 * svc_tcp_release_ctxt - Release transport-related resources
138 * @xprt: the transport which owned the context
139 * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
140 *
141 */
142static void svc_tcp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
143{
144}
145
146/**
147 * svc_udp_release_ctxt - Release transport-related resources
148 * @xprt: the transport which owned the context
149 * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
150 *
151 */
152static void svc_udp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
153{
154 struct sk_buff *skb = ctxt;
155
156 if (skb)
157 consume_skb(skb);
158}
159
160union svc_pktinfo_u {
161 struct in_pktinfo pkti;
162 struct in6_pktinfo pkti6;
163};
164#define SVC_PKTINFO_SPACE \
165 CMSG_SPACE(sizeof(union svc_pktinfo_u))
166
167static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
168{
169 struct svc_sock *svsk =
170 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
171 switch (svsk->sk_sk->sk_family) {
172 case AF_INET: {
173 struct in_pktinfo *pki = CMSG_DATA(cmh);
174
175 cmh->cmsg_level = SOL_IP;
176 cmh->cmsg_type = IP_PKTINFO;
177 pki->ipi_ifindex = 0;
178 pki->ipi_spec_dst.s_addr =
179 svc_daddr_in(rqstp)->sin_addr.s_addr;
180 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
181 }
182 break;
183
184 case AF_INET6: {
185 struct in6_pktinfo *pki = CMSG_DATA(cmh);
186 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
187
188 cmh->cmsg_level = SOL_IPV6;
189 cmh->cmsg_type = IPV6_PKTINFO;
190 pki->ipi6_ifindex = daddr->sin6_scope_id;
191 pki->ipi6_addr = daddr->sin6_addr;
192 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
193 }
194 break;
195 }
196}
197
198static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
199 unsigned int length)
200{
201 return 0;
202}
203
204/*
205 * Report socket names for nfsdfs
206 */
207static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
208{
209 const struct sock *sk = svsk->sk_sk;
210 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
211 "udp" : "tcp";
212 int len;
213
214 switch (sk->sk_family) {
215 case PF_INET:
216 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
217 proto_name,
218 &inet_sk(sk)->inet_rcv_saddr,
219 inet_sk(sk)->inet_num);
220 break;
221#if IS_ENABLED(CONFIG_IPV6)
222 case PF_INET6:
223 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
224 proto_name,
225 &sk->sk_v6_rcv_saddr,
226 inet_sk(sk)->inet_num);
227 break;
228#endif
229 default:
230 len = snprintf(buf, remaining, "*unknown-%d*\n",
231 sk->sk_family);
232 }
233
234 if (len >= remaining) {
235 *buf = '\0';
236 return -ENAMETOOLONG;
237 }
238 return len;
239}
240
241static int
242svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
243 struct cmsghdr *cmsg, int ret)
244{
245 u8 content_type = tls_get_record_type(sock->sk, cmsg);
246 u8 level, description;
247
248 switch (content_type) {
249 case 0:
250 break;
251 case TLS_RECORD_TYPE_DATA:
252 /* TLS sets EOR at the end of each application data
253 * record, even though there might be more frames
254 * waiting to be decrypted.
255 */
256 msg->msg_flags &= ~MSG_EOR;
257 break;
258 case TLS_RECORD_TYPE_ALERT:
259 tls_alert_recv(sock->sk, msg, &level, &description);
260 ret = (level == TLS_ALERT_LEVEL_FATAL) ?
261 -ENOTCONN : -EAGAIN;
262 break;
263 default:
264 /* discard this record type */
265 ret = -EAGAIN;
266 }
267 return ret;
268}
269
270static int
271svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
272{
273 union {
274 struct cmsghdr cmsg;
275 u8 buf[CMSG_SPACE(sizeof(u8))];
276 } u;
277 u8 alert[2];
278 struct kvec alert_kvec = {
279 .iov_base = alert,
280 .iov_len = sizeof(alert),
281 };
282 struct msghdr msg = {
283 .msg_flags = *msg_flags,
284 .msg_control = &u,
285 .msg_controllen = sizeof(u),
286 };
287 int ret;
288
289 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
290 alert_kvec.iov_len);
291 ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
292 if (ret > 0 &&
293 tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
294 iov_iter_revert(&msg.msg_iter, ret);
295 ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
296 }
297 return ret;
298}
299
300static int
301svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)
302{
303 int ret;
304 struct socket *sock = svsk->sk_sock;
305
306 ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
307 if (msg->msg_flags & MSG_CTRUNC) {
308 msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
309 if (ret == 0 || ret == -EIO)
310 ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
311 }
312 return ret;
313}
314
315#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
316static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
317{
318 struct bvec_iter bi = {
319 .bi_size = size + seek,
320 };
321 struct bio_vec bv;
322
323 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
324 for_each_bvec(bv, bvec, bi, bi)
325 flush_dcache_page(bv.bv_page);
326}
327#else
328static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
329 size_t seek)
330{
331}
332#endif
333
334/*
335 * Read from @rqstp's transport socket. The incoming message fills whole
336 * pages in @rqstp's rq_pages array until the last page of the message
337 * has been received into a partial page.
338 */
339static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
340 size_t seek)
341{
342 struct svc_sock *svsk =
343 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
344 struct bio_vec *bvec = rqstp->rq_bvec;
345 struct msghdr msg = { NULL };
346 unsigned int i;
347 ssize_t len;
348 size_t t;
349
350 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
351
352 for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE)
353 bvec_set_page(&bvec[i], rqstp->rq_pages[i], PAGE_SIZE, 0);
354
355 iov_iter_bvec(&msg.msg_iter, ITER_DEST, bvec, i, buflen);
356 if (seek) {
357 iov_iter_advance(&msg.msg_iter, seek);
358 buflen -= seek;
359 }
360 len = svc_tcp_sock_recvmsg(svsk, &msg);
361 if (len > 0)
362 svc_flush_bvec(bvec, len, seek);
363
364 /* If we read a full record, then assume there may be more
365 * data to read (stream based sockets only!)
366 */
367 if (len == buflen)
368 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
369
370 return len;
371}
372
373/*
374 * Set socket snd and rcv buffer lengths
375 */
376static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
377{
378 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
379 struct socket *sock = svsk->sk_sock;
380
381 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
382
383 lock_sock(sock->sk);
384 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
385 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
386 sock->sk->sk_write_space(sock->sk);
387 release_sock(sock->sk);
388}
389
390static void svc_sock_secure_port(struct svc_rqst *rqstp)
391{
392 if (svc_port_is_privileged(svc_addr(rqstp)))
393 set_bit(RQ_SECURE, &rqstp->rq_flags);
394 else
395 clear_bit(RQ_SECURE, &rqstp->rq_flags);
396}
397
398/*
399 * INET callback when data has been received on the socket.
400 */
401static void svc_data_ready(struct sock *sk)
402{
403 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
404
405 trace_sk_data_ready(sk);
406
407 if (svsk) {
408 /* Refer to svc_setup_socket() for details. */
409 rmb();
410 svsk->sk_odata(sk);
411 trace_svcsock_data_ready(&svsk->sk_xprt, 0);
412 if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
413 return;
414 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
415 svc_xprt_enqueue(&svsk->sk_xprt);
416 }
417}
418
419/*
420 * INET callback when space is newly available on the socket.
421 */
422static void svc_write_space(struct sock *sk)
423{
424 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
425
426 if (svsk) {
427 /* Refer to svc_setup_socket() for details. */
428 rmb();
429 trace_svcsock_write_space(&svsk->sk_xprt, 0);
430 svsk->sk_owspace(sk);
431 svc_xprt_enqueue(&svsk->sk_xprt);
432 }
433}
434
435static int svc_tcp_has_wspace(struct svc_xprt *xprt)
436{
437 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
438
439 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
440 return 1;
441 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
442}
443
444static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
445{
446 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
447
448 sock_no_linger(svsk->sk_sock->sk);
449}
450
451/**
452 * svc_tcp_handshake_done - Handshake completion handler
453 * @data: address of xprt to wake
454 * @status: status of handshake
455 * @peerid: serial number of key containing the remote peer's identity
456 *
457 * If a security policy is specified as an export option, we don't
458 * have a specific export here to check. So we set a "TLS session
459 * is present" flag on the xprt and let an upper layer enforce local
460 * security policy.
461 */
462static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
463{
464 struct svc_xprt *xprt = data;
465 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
466
467 if (!status) {
468 if (peerid != TLS_NO_PEERID)
469 set_bit(XPT_PEER_AUTH, &xprt->xpt_flags);
470 set_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
471 }
472 clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
473 complete_all(&svsk->sk_handshake_done);
474 svc_xprt_put(xprt);
475}
476
477/**
478 * svc_tcp_handshake - Perform a transport-layer security handshake
479 * @xprt: connected transport endpoint
480 *
481 */
482static void svc_tcp_handshake(struct svc_xprt *xprt)
483{
484 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
485 struct sock *sk = svsk->sk_sock->sk;
486 struct tls_handshake_args args = {
487 .ta_sock = svsk->sk_sock,
488 .ta_done = svc_tcp_handshake_done,
489 .ta_data = xprt,
490 };
491 int ret;
492
493 trace_svc_tls_upcall(xprt);
494
495 clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
496 init_completion(&svsk->sk_handshake_done);
497
498 /* Pin the transport across the asynchronous handshake callback. */
499 svc_xprt_get(xprt);
500
501 ret = tls_server_hello_x509(&args, GFP_KERNEL);
502 if (ret) {
503 trace_svc_tls_not_started(xprt);
504 svc_xprt_put(xprt);
505 goto out_failed;
506 }
507
508 ret = wait_for_completion_interruptible_timeout(&svsk->sk_handshake_done,
509 SVC_HANDSHAKE_TO);
510 if (ret <= 0) {
511 if (tls_handshake_cancel(sk)) {
512 trace_svc_tls_timed_out(xprt);
513 svc_xprt_put(xprt);
514 goto out_close;
515 }
516 /* Cancellation lost to handshake_complete(): the
517 * callback is in flight and should finish quickly.
518 */
519 wait_for_completion(&svsk->sk_handshake_done);
520 }
521
522 if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
523 trace_svc_tls_unavailable(xprt);
524 goto out_close;
525 }
526
527 /* Mark the transport ready in case the remote sent RPC
528 * traffic before the kernel received the handshake
529 * completion downcall.
530 */
531 set_bit(XPT_DATA, &xprt->xpt_flags);
532 svc_xprt_enqueue(xprt);
533 return;
534
535out_close:
536 set_bit(XPT_CLOSE, &xprt->xpt_flags);
537out_failed:
538 clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
539 set_bit(XPT_DATA, &xprt->xpt_flags);
540 svc_xprt_enqueue(xprt);
541}
542
543/*
544 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
545 */
546static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
547 struct cmsghdr *cmh)
548{
549 struct in_pktinfo *pki = CMSG_DATA(cmh);
550 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
551
552 if (cmh->cmsg_type != IP_PKTINFO)
553 return 0;
554
555 daddr->sin_family = AF_INET;
556 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
557 return 1;
558}
559
560/*
561 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
562 */
563static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
564 struct cmsghdr *cmh)
565{
566 struct in6_pktinfo *pki = CMSG_DATA(cmh);
567 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
568
569 if (cmh->cmsg_type != IPV6_PKTINFO)
570 return 0;
571
572 daddr->sin6_family = AF_INET6;
573 daddr->sin6_addr = pki->ipi6_addr;
574 daddr->sin6_scope_id = pki->ipi6_ifindex;
575 return 1;
576}
577
578/*
579 * Copy the UDP datagram's destination address to the rqstp structure.
580 * The 'destination' address in this case is the address to which the
581 * peer sent the datagram, i.e. our local address. For multihomed
582 * hosts, this can change from msg to msg. Note that only the IP
583 * address changes, the port number should remain the same.
584 */
585static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
586 struct cmsghdr *cmh)
587{
588 switch (cmh->cmsg_level) {
589 case SOL_IP:
590 return svc_udp_get_dest_address4(rqstp, cmh);
591 case SOL_IPV6:
592 return svc_udp_get_dest_address6(rqstp, cmh);
593 }
594
595 return 0;
596}
597
598/**
599 * svc_udp_recvfrom - Receive a datagram from a UDP socket.
600 * @rqstp: request structure into which to receive an RPC Call
601 *
602 * Called in a loop when XPT_DATA has been set.
603 *
604 * Returns:
605 * On success, the number of bytes in a received RPC Call, or
606 * %0 if a complete RPC Call message was not ready to return
607 */
608static int svc_udp_recvfrom(struct svc_rqst *rqstp)
609{
610 struct svc_sock *svsk =
611 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
612 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
613 struct sk_buff *skb;
614 union {
615 struct cmsghdr hdr;
616 long all[SVC_PKTINFO_SPACE / sizeof(long)];
617 } buffer;
618 struct cmsghdr *cmh = &buffer.hdr;
619 struct msghdr msg = {
620 .msg_name = svc_addr(rqstp),
621 .msg_control = cmh,
622 .msg_controllen = sizeof(buffer),
623 .msg_flags = MSG_DONTWAIT,
624 };
625 size_t len;
626 int err;
627
628 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
629 /* udp sockets need large rcvbuf as all pending
630 * requests are still in that buffer. sndbuf must
631 * also be large enough that there is enough space
632 * for one reply per thread. We count all threads
633 * rather than threads in a particular pool, which
634 * provides an upper bound on the number of threads
635 * which will access the socket.
636 */
637 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
638
639 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
640 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
641 0, 0, MSG_PEEK | MSG_DONTWAIT);
642 if (err < 0)
643 goto out_recv_err;
644 skb = skb_recv_udp(svsk->sk_sk, MSG_DONTWAIT, &err);
645 if (!skb)
646 goto out_recv_err;
647
648 len = svc_addr_len(svc_addr(rqstp));
649 rqstp->rq_addrlen = len;
650 if (skb->tstamp == 0) {
651 skb->tstamp = ktime_get_real();
652 /* Don't enable netstamp, sunrpc doesn't
653 need that much accuracy */
654 }
655 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
656 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
657
658 len = skb->len;
659 rqstp->rq_arg.len = len;
660 trace_svcsock_udp_recv(&svsk->sk_xprt, len);
661
662 rqstp->rq_prot = IPPROTO_UDP;
663
664 if (!svc_udp_get_dest_address(rqstp, cmh))
665 goto out_cmsg_err;
666 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
667
668 if (skb_is_nonlinear(skb)) {
669 /* we have to copy */
670 local_bh_disable();
671 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
672 goto out_bh_enable;
673 local_bh_enable();
674 consume_skb(skb);
675 } else {
676 /* we can use it in-place */
677 rqstp->rq_arg.head[0].iov_base = skb->data;
678 rqstp->rq_arg.head[0].iov_len = len;
679 if (skb_checksum_complete(skb))
680 goto out_free;
681 rqstp->rq_xprt_ctxt = skb;
682 }
683
684 rqstp->rq_arg.page_base = 0;
685 if (len <= rqstp->rq_arg.head[0].iov_len) {
686 rqstp->rq_arg.head[0].iov_len = len;
687 rqstp->rq_arg.page_len = 0;
688 } else {
689 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
690 }
691
692 if (serv->sv_stats)
693 serv->sv_stats->netudpcnt++;
694
695 svc_sock_secure_port(rqstp);
696 svc_xprt_received(rqstp->rq_xprt);
697 return len;
698
699out_recv_err:
700 if (err != -EAGAIN) {
701 /* possibly an icmp error */
702 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
703 }
704 trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
705 goto out_clear_busy;
706out_cmsg_err:
707 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
708 cmh->cmsg_level, cmh->cmsg_type);
709 goto out_free;
710out_bh_enable:
711 local_bh_enable();
712out_free:
713 kfree_skb(skb);
714out_clear_busy:
715 svc_xprt_received(rqstp->rq_xprt);
716 return 0;
717}
718
719/**
720 * svc_udp_sendto - Send out a reply on a UDP socket
721 * @rqstp: completed svc_rqst
722 *
723 * xpt_mutex ensures @rqstp's whole message is written to the socket
724 * without interruption.
725 *
726 * Returns the number of bytes sent, or a negative errno.
727 */
728static int svc_udp_sendto(struct svc_rqst *rqstp)
729{
730 struct svc_xprt *xprt = rqstp->rq_xprt;
731 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
732 struct xdr_buf *xdr = &rqstp->rq_res;
733 union {
734 struct cmsghdr hdr;
735 long all[SVC_PKTINFO_SPACE / sizeof(long)];
736 } buffer;
737 struct cmsghdr *cmh = &buffer.hdr;
738 struct msghdr msg = {
739 .msg_name = &rqstp->rq_addr,
740 .msg_namelen = rqstp->rq_addrlen,
741 .msg_control = cmh,
742 .msg_flags = MSG_SPLICE_PAGES,
743 .msg_controllen = sizeof(buffer),
744 };
745 int count;
746 int err;
747
748 svc_udp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
749 rqstp->rq_xprt_ctxt = NULL;
750
751 svc_set_cmsg_data(rqstp, cmh);
752
753 mutex_lock(&xprt->xpt_mutex);
754
755 if (svc_xprt_is_dead(xprt))
756 goto out_notconn;
757
758 count = xdr_buf_to_bvec(svsk->sk_bvec, SUNRPC_MAX_UDP_SENDPAGES, xdr);
759 if (count < 0) {
760 err = count;
761 goto out_trace;
762 }
763
764 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
765 count, rqstp->rq_res.len);
766 err = sock_sendmsg(svsk->sk_sock, &msg);
767 if (err == -ECONNREFUSED) {
768 /* ICMP error on earlier request. */
769 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
770 count, rqstp->rq_res.len);
771 err = sock_sendmsg(svsk->sk_sock, &msg);
772 }
773
774out_trace:
775 trace_svcsock_udp_send(xprt, err);
776
777 mutex_unlock(&xprt->xpt_mutex);
778 return err;
779
780out_notconn:
781 mutex_unlock(&xprt->xpt_mutex);
782 return -ENOTCONN;
783}
784
785static int svc_udp_has_wspace(struct svc_xprt *xprt)
786{
787 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
788 struct svc_serv *serv = xprt->xpt_server;
789 unsigned long required;
790
791 /*
792 * Set the SOCK_NOSPACE flag before checking the available
793 * sock space.
794 */
795 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
796 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
797 if (required*2 > sock_wspace(svsk->sk_sk))
798 return 0;
799 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
800 return 1;
801}
802
803static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
804{
805 BUG();
806 return NULL;
807}
808
809static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
810{
811}
812
813static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
814 struct net *net,
815 struct sockaddr *sa, int salen,
816 int flags)
817{
818 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
819}
820
821static const struct svc_xprt_ops svc_udp_ops = {
822 .xpo_create = svc_udp_create,
823 .xpo_recvfrom = svc_udp_recvfrom,
824 .xpo_sendto = svc_udp_sendto,
825 .xpo_result_payload = svc_sock_result_payload,
826 .xpo_release_ctxt = svc_udp_release_ctxt,
827 .xpo_detach = svc_sock_detach,
828 .xpo_free = svc_sock_free,
829 .xpo_has_wspace = svc_udp_has_wspace,
830 .xpo_accept = svc_udp_accept,
831 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
832};
833
834static struct svc_xprt_class svc_udp_class = {
835 .xcl_name = "udp",
836 .xcl_owner = THIS_MODULE,
837 .xcl_ops = &svc_udp_ops,
838 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
839 .xcl_ident = XPRT_TRANSPORT_UDP,
840};
841
842static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
843{
844 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
845 &svsk->sk_xprt, serv);
846 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
847 svsk->sk_sk->sk_data_ready = svc_data_ready;
848 svsk->sk_sk->sk_write_space = svc_write_space;
849
850 /* initialise setting must have enough space to
851 * receive and respond to one request.
852 * svc_udp_recvfrom will re-adjust if necessary
853 */
854 svc_sock_setbufsize(svsk, 3);
855
856 /* data might have come in before data_ready set up */
857 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
858 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
859 set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
860
861 /* make sure we get destination address info */
862 switch (svsk->sk_sk->sk_family) {
863 case AF_INET:
864 ip_sock_set_pktinfo(svsk->sk_sock->sk);
865 break;
866 case AF_INET6:
867 ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
868 break;
869 default:
870 BUG();
871 }
872}
873
874/*
875 * A data_ready event on a listening socket means there's a connection
876 * pending. Do not use state_change as a substitute for it.
877 */
878static void svc_tcp_listen_data_ready(struct sock *sk)
879{
880 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
881
882 trace_sk_data_ready(sk);
883
884 /*
885 * This callback may called twice when a new connection
886 * is established as a child socket inherits everything
887 * from a parent LISTEN socket.
888 * 1) data_ready method of the parent socket will be called
889 * when one of child sockets become ESTABLISHED.
890 * 2) data_ready method of the child socket may be called
891 * when it receives data before the socket is accepted.
892 * In case of 2, we should ignore it silently and DO NOT
893 * dereference svsk.
894 */
895 if (sk->sk_state != TCP_LISTEN)
896 return;
897
898 if (svsk) {
899 /* Refer to svc_setup_socket() for details. */
900 rmb();
901 svsk->sk_odata(sk);
902 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
903 svc_xprt_enqueue(&svsk->sk_xprt);
904 }
905}
906
907/*
908 * A state change on a connected socket means it's dying or dead.
909 */
910static void svc_tcp_state_change(struct sock *sk)
911{
912 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
913
914 if (svsk) {
915 /* Refer to svc_setup_socket() for details. */
916 rmb();
917 svsk->sk_ostate(sk);
918 trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
919 if (sk->sk_state != TCP_ESTABLISHED)
920 svc_xprt_deferred_close(&svsk->sk_xprt);
921 }
922}
923
924/*
925 * Accept a TCP connection
926 */
927static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
928{
929 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
930 struct sockaddr_storage addr;
931 struct sockaddr *sin = (struct sockaddr *) &addr;
932 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
933 struct socket *sock = svsk->sk_sock;
934 struct socket *newsock;
935 struct svc_sock *newsvsk;
936 int err, slen;
937
938 if (!sock)
939 return NULL;
940
941 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
942 err = kernel_accept(sock, &newsock, O_NONBLOCK);
943 if (err < 0) {
944 if (err != -EAGAIN)
945 trace_svcsock_accept_err(xprt, serv->sv_name, err);
946 return NULL;
947 }
948 if (IS_ERR(sock_alloc_file(newsock, O_NONBLOCK, NULL)))
949 return NULL;
950
951 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
952
953 err = kernel_getpeername(newsock, sin);
954 if (err < 0) {
955 trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
956 goto failed; /* aborted connection or whatever */
957 }
958 slen = err;
959
960 /* Reset the inherited callbacks before calling svc_setup_socket */
961 newsock->sk->sk_state_change = svsk->sk_ostate;
962 newsock->sk->sk_data_ready = svsk->sk_odata;
963 newsock->sk->sk_write_space = svsk->sk_owspace;
964
965 /* make sure that a write doesn't block forever when
966 * low on memory
967 */
968 newsock->sk->sk_sndtimeo = HZ*30;
969
970 newsvsk = svc_setup_socket(serv, newsock,
971 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
972 if (IS_ERR(newsvsk))
973 goto failed;
974 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
975 err = kernel_getsockname(newsock, sin);
976 slen = err;
977 if (unlikely(err < 0))
978 slen = offsetof(struct sockaddr, sa_data);
979 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
980
981 if (sock_is_loopback(newsock->sk))
982 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
983 else
984 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
985 if (serv->sv_stats)
986 serv->sv_stats->nettcpconn++;
987
988 return &newsvsk->sk_xprt;
989
990failed:
991 sockfd_put(newsock);
992 return NULL;
993}
994
995static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
996 struct svc_rqst *rqstp)
997{
998 size_t len = svsk->sk_datalen;
999 unsigned int i, npages;
1000
1001 if (!len)
1002 return 0;
1003 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1004 for (i = 0; i < npages; i++) {
1005 if (rqstp->rq_pages[i] != NULL)
1006 svc_rqst_page_release(rqstp, rqstp->rq_pages[i]);
1007 BUG_ON(svsk->sk_pages[i] == NULL);
1008 rqstp->rq_pages[i] = svsk->sk_pages[i];
1009 svsk->sk_pages[i] = NULL;
1010 }
1011 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
1012 return len;
1013}
1014
1015static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
1016{
1017 unsigned int i, len, npages;
1018
1019 if (svsk->sk_datalen == 0)
1020 return;
1021 len = svsk->sk_datalen;
1022 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1023 for (i = 0; i < npages; i++) {
1024 svsk->sk_pages[i] = rqstp->rq_pages[i];
1025 rqstp->rq_pages[i] = NULL;
1026 }
1027 rqstp->rq_pages_nfree = npages;
1028}
1029
1030static void svc_tcp_clear_pages(struct svc_sock *svsk)
1031{
1032 unsigned int i, len, npages;
1033
1034 if (svsk->sk_datalen == 0)
1035 goto out;
1036 len = svsk->sk_datalen;
1037 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1038 for (i = 0; i < npages; i++) {
1039 if (svsk->sk_pages[i] == NULL) {
1040 WARN_ON_ONCE(1);
1041 continue;
1042 }
1043 put_page(svsk->sk_pages[i]);
1044 svsk->sk_pages[i] = NULL;
1045 }
1046out:
1047 svsk->sk_tcplen = 0;
1048 svsk->sk_datalen = 0;
1049}
1050
1051/*
1052 * Receive fragment record header into sk_marker.
1053 */
1054static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
1055 struct svc_rqst *rqstp)
1056{
1057 ssize_t want, len;
1058
1059 /* If we haven't gotten the record length yet,
1060 * get the next four bytes.
1061 */
1062 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
1063 struct msghdr msg = { NULL };
1064 struct kvec iov;
1065
1066 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
1067 iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
1068 iov.iov_len = want;
1069 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
1070 len = svc_tcp_sock_recvmsg(svsk, &msg);
1071 if (len < 0)
1072 return len;
1073 svsk->sk_tcplen += len;
1074 if (len < want) {
1075 /* call again to read the remaining bytes */
1076 goto err_short;
1077 }
1078 trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
1079 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
1080 svsk->sk_xprt.xpt_server->sv_max_mesg)
1081 goto err_too_large;
1082 }
1083 return svc_sock_reclen(svsk);
1084
1085err_too_large:
1086 net_notice_ratelimited("svc: %s oversized RPC fragment (%u octets) from %pISpc\n",
1087 svsk->sk_xprt.xpt_server->sv_name,
1088 svc_sock_reclen(svsk),
1089 (struct sockaddr *)&svsk->sk_xprt.xpt_remote);
1090 svc_xprt_deferred_close(&svsk->sk_xprt);
1091err_short:
1092 return -EAGAIN;
1093}
1094
1095static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
1096{
1097 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
1098 struct rpc_rqst *req = NULL;
1099 struct kvec *src, *dst;
1100 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1101 __be32 xid = *p;
1102
1103 if (!bc_xprt)
1104 return -EAGAIN;
1105 spin_lock(&bc_xprt->queue_lock);
1106 req = xprt_lookup_rqst(bc_xprt, xid);
1107 if (!req)
1108 goto unlock_eagain;
1109
1110 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1111 /*
1112 * XXX!: cheating for now! Only copying HEAD.
1113 * But we know this is good enough for now (in fact, for any
1114 * callback reply in the forseeable future).
1115 */
1116 dst = &req->rq_private_buf.head[0];
1117 src = &rqstp->rq_arg.head[0];
1118 if (dst->iov_len < src->iov_len)
1119 goto unlock_eagain; /* whatever; just giving up. */
1120 memcpy(dst->iov_base, src->iov_base, src->iov_len);
1121 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
1122 rqstp->rq_arg.len = 0;
1123 spin_unlock(&bc_xprt->queue_lock);
1124 return 0;
1125unlock_eagain:
1126 spin_unlock(&bc_xprt->queue_lock);
1127 return -EAGAIN;
1128}
1129
1130static void svc_tcp_fragment_received(struct svc_sock *svsk)
1131{
1132 /* If we have more data, signal svc_xprt_enqueue() to try again */
1133 svsk->sk_tcplen = 0;
1134 svsk->sk_marker = xdr_zero;
1135}
1136
1137/**
1138 * svc_tcp_recvfrom - Receive data from a TCP socket
1139 * @rqstp: request structure into which to receive an RPC Call
1140 *
1141 * Called in a loop when XPT_DATA has been set.
1142 *
1143 * Read the 4-byte stream record marker, then use the record length
1144 * in that marker to set up exactly the resources needed to receive
1145 * the next RPC message into @rqstp.
1146 *
1147 * Returns:
1148 * On success, the number of bytes in a received RPC Call, or
1149 * %0 if a complete RPC Call message was not ready to return
1150 *
1151 * The zero return case handles partial receives and callback Replies.
1152 * The state of a partial receive is preserved in the svc_sock for
1153 * the next call to svc_tcp_recvfrom.
1154 */
1155static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1156{
1157 struct svc_sock *svsk =
1158 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1159 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1160 size_t want, base;
1161 ssize_t len;
1162 __be32 *p;
1163 __be32 calldir;
1164
1165 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1166 len = svc_tcp_read_marker(svsk, rqstp);
1167 if (len < 0)
1168 goto error;
1169
1170 base = svc_tcp_restore_pages(svsk, rqstp);
1171 want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
1172 len = svc_tcp_read_msg(rqstp, base + want, base);
1173 if (len >= 0) {
1174 trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
1175 svsk->sk_tcplen += len;
1176 svsk->sk_datalen += len;
1177 }
1178 if (len != want || !svc_sock_final_rec(svsk))
1179 goto err_incomplete;
1180 if (svsk->sk_datalen < 8)
1181 goto err_nuts;
1182
1183 rqstp->rq_arg.len = svsk->sk_datalen;
1184 rqstp->rq_arg.page_base = 0;
1185 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1186 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1187 rqstp->rq_arg.page_len = 0;
1188 } else
1189 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1190
1191 rqstp->rq_xprt_ctxt = NULL;
1192 rqstp->rq_prot = IPPROTO_TCP;
1193 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1194 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1195 else
1196 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1197
1198 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1199 calldir = p[1];
1200 if (calldir)
1201 len = receive_cb_reply(svsk, rqstp);
1202
1203 /* Reset TCP read info */
1204 svsk->sk_datalen = 0;
1205 svc_tcp_fragment_received(svsk);
1206
1207 if (len < 0)
1208 goto error;
1209
1210 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1211 if (serv->sv_stats)
1212 serv->sv_stats->nettcpcnt++;
1213
1214 svc_sock_secure_port(rqstp);
1215 svc_xprt_received(rqstp->rq_xprt);
1216 return rqstp->rq_arg.len;
1217
1218err_incomplete:
1219 svc_tcp_save_pages(svsk, rqstp);
1220 if (len < 0 && len != -EAGAIN)
1221 goto err_delete;
1222 if (len == want)
1223 svc_tcp_fragment_received(svsk);
1224 else
1225 trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1226 svc_sock_reclen(svsk),
1227 svsk->sk_tcplen - sizeof(rpc_fraghdr));
1228 goto err_noclose;
1229error:
1230 if (len != -EAGAIN)
1231 goto err_delete;
1232 trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
1233 goto err_noclose;
1234err_nuts:
1235 svsk->sk_datalen = 0;
1236err_delete:
1237 trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
1238 svc_xprt_deferred_close(&svsk->sk_xprt);
1239err_noclose:
1240 svc_xprt_received(rqstp->rq_xprt);
1241 return 0; /* record not complete */
1242}
1243
1244/*
1245 * MSG_SPLICE_PAGES is used exclusively to reduce the number of
1246 * copy operations in this path. Therefore the caller must ensure
1247 * that the pages backing @xdr are unchanging.
1248 */
1249static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp,
1250 rpc_fraghdr marker)
1251{
1252 struct msghdr msg = {
1253 .msg_flags = MSG_SPLICE_PAGES,
1254 };
1255 int count;
1256 void *buf;
1257 int ret;
1258
1259 /* The stream record marker is copied into a temporary page
1260 * fragment buffer so that it can be included in sk_bvec.
1261 */
1262 buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker),
1263 GFP_KERNEL);
1264 if (!buf)
1265 return -ENOMEM;
1266 memcpy(buf, &marker, sizeof(marker));
1267 bvec_set_virt(svsk->sk_bvec, buf, sizeof(marker));
1268
1269 count = xdr_buf_to_bvec(svsk->sk_bvec + 1, rqstp->rq_maxpages,
1270 &rqstp->rq_res);
1271 if (count < 0) {
1272 ret = count;
1273 goto out;
1274 }
1275
1276 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
1277 1 + count, sizeof(marker) + rqstp->rq_res.len);
1278 ret = sock_sendmsg(svsk->sk_sock, &msg);
1279out:
1280 page_frag_free(buf);
1281 return ret;
1282}
1283
1284/**
1285 * svc_tcp_sendto - Send out a reply on a TCP socket
1286 * @rqstp: completed svc_rqst
1287 *
1288 * xpt_mutex ensures @rqstp's whole message is written to the socket
1289 * without interruption.
1290 *
1291 * Returns the number of bytes sent, or a negative errno.
1292 */
1293static int svc_tcp_sendto(struct svc_rqst *rqstp)
1294{
1295 struct svc_xprt *xprt = rqstp->rq_xprt;
1296 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1297 struct xdr_buf *xdr = &rqstp->rq_res;
1298 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1299 (u32)xdr->len);
1300 int sent;
1301
1302 svc_tcp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
1303 rqstp->rq_xprt_ctxt = NULL;
1304
1305 mutex_lock(&xprt->xpt_mutex);
1306 if (svc_xprt_is_dead(xprt))
1307 goto out_notconn;
1308 sent = svc_tcp_sendmsg(svsk, rqstp, marker);
1309 trace_svcsock_tcp_send(xprt, sent);
1310 if (sent < 0 || sent != (xdr->len + sizeof(marker)))
1311 goto out_close;
1312 mutex_unlock(&xprt->xpt_mutex);
1313 return sent;
1314
1315out_notconn:
1316 mutex_unlock(&xprt->xpt_mutex);
1317 return -ENOTCONN;
1318out_close:
1319 pr_notice("rpc-srv/tcp: %s: %s %d when sending %zu bytes - shutting down socket\n",
1320 xprt->xpt_server->sv_name,
1321 (sent < 0) ? "got error" : "sent",
1322 sent, xdr->len + sizeof(marker));
1323 svc_xprt_deferred_close(xprt);
1324 mutex_unlock(&xprt->xpt_mutex);
1325 return -EAGAIN;
1326}
1327
1328static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1329 struct net *net,
1330 struct sockaddr *sa, int salen,
1331 int flags)
1332{
1333 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1334}
1335
1336static const struct svc_xprt_ops svc_tcp_ops = {
1337 .xpo_create = svc_tcp_create,
1338 .xpo_recvfrom = svc_tcp_recvfrom,
1339 .xpo_sendto = svc_tcp_sendto,
1340 .xpo_result_payload = svc_sock_result_payload,
1341 .xpo_release_ctxt = svc_tcp_release_ctxt,
1342 .xpo_detach = svc_tcp_sock_detach,
1343 .xpo_free = svc_sock_free,
1344 .xpo_has_wspace = svc_tcp_has_wspace,
1345 .xpo_accept = svc_tcp_accept,
1346 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1347 .xpo_handshake = svc_tcp_handshake,
1348};
1349
1350static struct svc_xprt_class svc_tcp_class = {
1351 .xcl_name = "tcp",
1352 .xcl_owner = THIS_MODULE,
1353 .xcl_ops = &svc_tcp_ops,
1354 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1355 .xcl_ident = XPRT_TRANSPORT_TCP,
1356};
1357
1358void svc_init_xprt_sock(void)
1359{
1360 svc_reg_xprt_class(&svc_tcp_class);
1361 svc_reg_xprt_class(&svc_udp_class);
1362}
1363
1364void svc_cleanup_xprt_sock(void)
1365{
1366 svc_unreg_xprt_class(&svc_tcp_class);
1367 svc_unreg_xprt_class(&svc_udp_class);
1368}
1369
1370static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1371{
1372 struct sock *sk = svsk->sk_sk;
1373
1374 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1375 &svsk->sk_xprt, serv);
1376 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1377 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1378 if (sk->sk_state == TCP_LISTEN) {
1379 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1380 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1381 set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
1382 sk->sk_data_ready = svc_tcp_listen_data_ready;
1383 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1384 } else {
1385 sk->sk_state_change = svc_tcp_state_change;
1386 sk->sk_data_ready = svc_data_ready;
1387 sk->sk_write_space = svc_write_space;
1388
1389 svsk->sk_marker = xdr_zero;
1390 svsk->sk_tcplen = 0;
1391 svsk->sk_datalen = 0;
1392 memset(&svsk->sk_pages[0], 0,
1393 svsk->sk_maxpages * sizeof(struct page *));
1394
1395 tcp_sock_set_nodelay(sk);
1396
1397 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1398 switch (sk->sk_state) {
1399 case TCP_SYN_RECV:
1400 case TCP_ESTABLISHED:
1401 break;
1402 default:
1403 svc_xprt_deferred_close(&svsk->sk_xprt);
1404 }
1405 }
1406}
1407
1408void svc_sock_update_bufs(struct svc_serv *serv)
1409{
1410 /*
1411 * The number of server threads has changed. Update
1412 * rcvbuf and sndbuf accordingly on all sockets
1413 */
1414 struct svc_sock *svsk;
1415
1416 spin_lock_bh(&serv->sv_lock);
1417 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1418 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1419 spin_unlock_bh(&serv->sv_lock);
1420}
1421
1422static int svc_sock_sendpages(struct svc_serv *serv, struct socket *sock, int flags)
1423{
1424 switch (sock->type) {
1425 case SOCK_STREAM:
1426 /* +1 for TCP record marker */
1427 if (flags & SVC_SOCK_TEMPORARY)
1428 return svc_serv_maxpages(serv) + 1;
1429 return 0;
1430 case SOCK_DGRAM:
1431 return SUNRPC_MAX_UDP_SENDPAGES;
1432 }
1433 return -EINVAL;
1434}
1435
1436/*
1437 * Initialize socket for RPC use and create svc_sock struct
1438 */
1439static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1440 struct socket *sock,
1441 int flags)
1442{
1443 struct svc_sock *svsk;
1444 struct sock *inet;
1445 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1446 int sendpages;
1447 unsigned long pages;
1448
1449 sendpages = svc_sock_sendpages(serv, sock, flags);
1450 if (sendpages < 0)
1451 return ERR_PTR(sendpages);
1452
1453 pages = svc_serv_maxpages(serv);
1454 svsk = kzalloc_flex(*svsk, sk_pages, pages);
1455 if (!svsk)
1456 return ERR_PTR(-ENOMEM);
1457
1458 if (sendpages) {
1459 svsk->sk_bvec = kzalloc_objs(*svsk->sk_bvec, sendpages);
1460 if (!svsk->sk_bvec) {
1461 kfree(svsk);
1462 return ERR_PTR(-ENOMEM);
1463 }
1464 }
1465
1466 svsk->sk_maxpages = pages;
1467
1468 inet = sock->sk;
1469
1470 if (pmap_register) {
1471 int err;
1472
1473 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1474 inet->sk_protocol,
1475 ntohs(inet_sk(inet)->inet_sport));
1476 if (err < 0) {
1477 kfree(svsk->sk_bvec);
1478 kfree(svsk);
1479 return ERR_PTR(err);
1480 }
1481 }
1482
1483 svsk->sk_sock = sock;
1484 svsk->sk_sk = inet;
1485 svsk->sk_ostate = inet->sk_state_change;
1486 svsk->sk_odata = inet->sk_data_ready;
1487 svsk->sk_owspace = inet->sk_write_space;
1488 /*
1489 * This barrier is necessary in order to prevent race condition
1490 * with svc_data_ready(), svc_tcp_listen_data_ready(), and others
1491 * when calling callbacks above.
1492 */
1493 wmb();
1494 inet->sk_user_data = svsk;
1495
1496 /* Initialize the socket */
1497 if (sock->type == SOCK_DGRAM)
1498 svc_udp_init(svsk, serv);
1499 else
1500 svc_tcp_init(svsk, serv);
1501
1502 trace_svcsock_new(svsk, sock);
1503 return svsk;
1504}
1505
1506/**
1507 * svc_addsock - add a listener socket to an RPC service
1508 * @serv: pointer to RPC service to which to add a new listener
1509 * @net: caller's network namespace
1510 * @fd: file descriptor of the new listener
1511 * @name_return: pointer to buffer to fill in with name of listener
1512 * @len: size of the buffer
1513 * @cred: credential
1514 *
1515 * Fills in socket name and returns positive length of name if successful.
1516 * Name is terminated with '\n'. On error, returns a negative errno
1517 * value.
1518 */
1519int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
1520 char *name_return, const size_t len, const struct cred *cred)
1521{
1522 int err = 0;
1523 struct socket *so = sockfd_lookup(fd, &err);
1524 struct svc_sock *svsk = NULL;
1525 struct sockaddr_storage addr;
1526 struct sockaddr *sin = (struct sockaddr *)&addr;
1527 int salen;
1528
1529 if (!so)
1530 return err;
1531 err = -EINVAL;
1532 if (sock_net(so->sk) != net)
1533 goto out;
1534 err = -EAFNOSUPPORT;
1535 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1536 goto out;
1537 err = -EPROTONOSUPPORT;
1538 if (so->sk->sk_protocol != IPPROTO_TCP &&
1539 so->sk->sk_protocol != IPPROTO_UDP)
1540 goto out;
1541 err = -EISCONN;
1542 if (so->state > SS_UNCONNECTED)
1543 goto out;
1544 err = -ENOENT;
1545 if (!try_module_get(THIS_MODULE))
1546 goto out;
1547 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1548 if (IS_ERR(svsk)) {
1549 module_put(THIS_MODULE);
1550 err = PTR_ERR(svsk);
1551 goto out;
1552 }
1553 salen = kernel_getsockname(svsk->sk_sock, sin);
1554 if (salen >= 0)
1555 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1556 svsk->sk_xprt.xpt_cred = get_cred(cred);
1557 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1558 return svc_one_sock_name(svsk, name_return, len);
1559out:
1560 sockfd_put(so);
1561 return err;
1562}
1563EXPORT_SYMBOL_GPL(svc_addsock);
1564
1565/*
1566 * Create socket for RPC service.
1567 */
1568static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1569 int protocol,
1570 struct net *net,
1571 struct sockaddr *sin, int len,
1572 int flags)
1573{
1574 struct svc_sock *svsk;
1575 struct socket *sock;
1576 int error;
1577 int type;
1578 struct sockaddr_storage addr;
1579 struct sockaddr *newsin = (struct sockaddr *)&addr;
1580 int newlen;
1581 int family;
1582
1583 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1584 printk(KERN_WARNING "svc: only UDP and TCP "
1585 "sockets supported\n");
1586 return ERR_PTR(-EINVAL);
1587 }
1588
1589 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1590 switch (sin->sa_family) {
1591 case AF_INET6:
1592 family = PF_INET6;
1593 break;
1594 case AF_INET:
1595 family = PF_INET;
1596 break;
1597 default:
1598 return ERR_PTR(-EINVAL);
1599 }
1600
1601 error = __sock_create(net, family, type, protocol, &sock, 1);
1602 if (error < 0)
1603 return ERR_PTR(error);
1604
1605 svc_reclassify_socket(sock);
1606
1607 /*
1608 * If this is an PF_INET6 listener, we want to avoid
1609 * getting requests from IPv4 remotes. Those should
1610 * be shunted to a PF_INET listener via rpcbind.
1611 */
1612 if (family == PF_INET6)
1613 ip6_sock_set_v6only(sock->sk);
1614 if (type == SOCK_STREAM)
1615 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1616 error = kernel_bind(sock, (struct sockaddr_unsized *)sin, len);
1617 if (error < 0)
1618 goto bummer;
1619
1620 error = kernel_getsockname(sock, newsin);
1621 if (error < 0)
1622 goto bummer;
1623 newlen = error;
1624
1625 if (protocol == IPPROTO_TCP) {
1626 sk_net_refcnt_upgrade(sock->sk);
1627 if ((error = kernel_listen(sock, SOMAXCONN)) < 0)
1628 goto bummer;
1629 }
1630
1631 svsk = svc_setup_socket(serv, sock, flags);
1632 if (IS_ERR(svsk)) {
1633 error = PTR_ERR(svsk);
1634 goto bummer;
1635 }
1636 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1637 return (struct svc_xprt *)svsk;
1638bummer:
1639 sock_release(sock);
1640 return ERR_PTR(error);
1641}
1642
1643/*
1644 * Detach the svc_sock from the socket so that no
1645 * more callbacks occur.
1646 */
1647static void svc_sock_detach(struct svc_xprt *xprt)
1648{
1649 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1650 struct sock *sk = svsk->sk_sk;
1651
1652 /* put back the old socket callbacks */
1653 lock_sock(sk);
1654 sk->sk_state_change = svsk->sk_ostate;
1655 sk->sk_data_ready = svsk->sk_odata;
1656 sk->sk_write_space = svsk->sk_owspace;
1657 sk->sk_user_data = NULL;
1658 release_sock(sk);
1659}
1660
1661/*
1662 * Disconnect the socket, and reset the callbacks
1663 */
1664static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1665{
1666 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1667
1668 tls_handshake_close(svsk->sk_sock);
1669
1670 svc_sock_detach(xprt);
1671
1672 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1673 svc_tcp_clear_pages(svsk);
1674 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1675 }
1676}
1677
1678/*
1679 * Free the svc_sock's socket resources and the svc_sock itself.
1680 */
1681static void svc_sock_free(struct svc_xprt *xprt)
1682{
1683 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1684 struct socket *sock = svsk->sk_sock;
1685
1686 trace_svcsock_free(svsk, sock);
1687
1688 tls_handshake_cancel(sock->sk);
1689 if (sock->file)
1690 sockfd_put(sock);
1691 else
1692 sock_release(sock);
1693
1694 page_frag_cache_drain(&svsk->sk_frag_cache);
1695 kfree(svsk->sk_bvec);
1696 kfree(svsk);
1697}