22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/fcntl.h>
27 #include <linux/net.h>
30 #include <linux/udp.h>
31 #include <linux/tcp.h>
33 #include <linux/slab.h>
34 #include <linux/netdevice.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
63 static void svc_udp_data_ready(
struct sock *,
int);
64 static int svc_udp_recvfrom(
struct svc_rqst *);
65 static int svc_udp_sendto(
struct svc_rqst *);
66 static void svc_sock_detach(
struct svc_xprt *);
67 static void svc_tcp_sock_detach(
struct svc_xprt *);
68 static void svc_sock_free(
struct svc_xprt *);
73 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
77 static void svc_bc_sock_free(
struct svc_xprt *xprt);
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 static void svc_reclassify_socket(
struct socket *
sock)
86 struct sock *
sk = sock->
sk;
88 switch (sk->sk_family) {
92 "sk_xprt.xpt_lock-AF_INET-NFSD",
99 "sk_xprt.xpt_lock-AF_INET6-NFSD",
108 static void svc_reclassify_socket(
struct socket *sock)
116 static void svc_release_skb(
struct svc_rqst *rqstp)
125 dprintk(
"svc: service %p, releasing skb %p\n", rqstp, skb);
134 #define SVC_PKTINFO_SPACE \
135 CMSG_SPACE(sizeof(union svc_pktinfo_u))
137 static void svc_set_cmsg_data(
struct svc_rqst *rqstp,
struct cmsghdr *cmh)
141 switch (svsk->
sk_sk->sk_family) {
149 svc_daddr_in(rqstp)->sin_addr.s_addr;
172 struct page *headpage,
unsigned long headoffset,
173 struct page *tailpage,
unsigned long tailoffset)
178 size_t base = xdr->page_base;
179 unsigned int pglen = xdr->page_len;
187 if (slen == xdr->head[0].iov_len)
190 xdr->head[0].iov_len, flags);
191 if (len != xdr->head[0].iov_len)
193 slen -= xdr->head[0].iov_len;
215 if (xdr->tail[0].iov_len) {
217 xdr->tail[0].iov_len, 0);
230 static int svc_sendto(
struct svc_rqst *rqstp,
struct xdr_buf *xdr)
241 unsigned long tailoff;
242 unsigned long headoff;
250 .msg_controllen =
sizeof(
buffer),
254 svc_set_cmsg_data(rqstp, cmh);
260 tailoff = ((
unsigned long)xdr->tail[0].iov_base) & (
PAGE_SIZE-1);
266 dprintk(
"svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
267 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
276 static int svc_one_sock_name(
struct svc_sock *svsk,
char *
buf,
int remaining)
278 const struct sock *sk = svsk->
sk_sk;
283 switch (sk->sk_family) {
285 len =
snprintf(buf, remaining,
"ipv4 %s %pI4 %d\n",
288 inet_sk(sk)->inet_num);
291 len =
snprintf(buf, remaining,
"ipv6 %s %pI6 %d\n",
293 &inet6_sk(sk)->rcv_saddr,
294 inet_sk(sk)->inet_num);
297 len =
snprintf(buf, remaining,
"*unknown-%d*\n",
301 if (len >= remaining) {
311 static int svc_recv_available(
struct svc_sock *svsk)
318 return (err >= 0)? avail :
err;
324 static int svc_recvfrom(
struct svc_rqst *rqstp,
struct kvec *iov,
int nr,
339 dprintk(
"svc: socket %p recvfrom(%p, %Zu) = %d\n",
340 svsk, iov[0].iov_base, iov[0].iov_len, len);
344 static int svc_partial_recvfrom(
struct svc_rqst *rqstp,
346 int buflen,
unsigned int base)
354 return svc_recvfrom(rqstp, iov, nr, buflen);
356 for (i = 0; i <
nr; i++) {
357 if (iov[i].iov_len > base)
365 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
374 static void svc_sock_setbufsize(
struct socket *sock,
unsigned int snd,
381 (
char*)&snd,
sizeof(snd));
383 (
char*)&rcv,
sizeof(rcv));
391 sock->
sk->sk_sndbuf = snd * 2;
392 sock->
sk->sk_rcvbuf = rcv * 2;
393 sock->
sk->sk_write_space(sock->
sk);
400 static void svc_udp_data_ready(
struct sock *sk,
int count)
406 dprintk(
"svc: socket %p(inet %p), count=%d, busy=%d\n",
412 if (wq && waitqueue_active(wq))
419 static void svc_write_space(
struct sock *sk)
425 dprintk(
"svc: socket %p(inet %p), write_space busy=%d\n",
430 if (wq && waitqueue_active(wq)) {
431 dprintk(
"RPC svc_write_space: someone sleeping on %p\n",
437 static void svc_tcp_write_space(
struct sock *sk)
441 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
449 static int svc_udp_get_dest_address4(
struct svc_rqst *rqstp,
466 static int svc_udp_get_dest_address6(
struct svc_rqst *rqstp,
488 static int svc_udp_get_dest_address(
struct svc_rqst *rqstp,
493 return svc_udp_get_dest_address4(rqstp, cmh);
495 return svc_udp_get_dest_address6(rqstp, cmh);
504 static int svc_udp_recvfrom(
struct svc_rqst *rqstp)
518 .msg_controllen =
sizeof(
buffer),
533 svc_sock_setbufsize(svsk->
sk_sock,
547 dprintk(
"svc: recvfrom returned error %d\n", -err);
552 len = svc_addr_len(svc_addr(rqstp));
567 if (!svc_udp_get_dest_address(rqstp, cmh)) {
568 net_warn_ratelimited(
"svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
572 rqstp->
rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
574 if (skb_is_nonlinear(skb)) {
589 if (skb_checksum_complete(skb))
594 rqstp->
rq_arg.page_base = 0;
595 if (len <= rqstp->rq_arg.head[0].iov_len) {
597 rqstp->
rq_arg.page_len = 0;
600 rqstp->
rq_arg.page_len = len - rqstp->
rq_arg.head[0].iov_len;
610 trace_kfree_skb(skb, svc_udp_recvfrom);
616 svc_udp_sendto(
struct svc_rqst *rqstp)
620 error = svc_sendto(rqstp, &rqstp->
rq_res);
623 error = svc_sendto(rqstp, &rqstp->
rq_res);
628 static void svc_udp_prep_reply_hdr(
struct svc_rqst *rqstp)
632 static int svc_udp_has_wspace(
struct svc_xprt *xprt)
636 unsigned long required;
644 if (required*2 > sock_wspace(svsk->
sk_sk))
661 return svc_create_socket(serv,
IPPROTO_UDP, net, sa, salen, flags);
665 .xpo_create = svc_udp_create,
666 .xpo_recvfrom = svc_udp_recvfrom,
667 .xpo_sendto = svc_udp_sendto,
668 .xpo_release_rqst = svc_release_skb,
669 .xpo_detach = svc_sock_detach,
670 .xpo_free = svc_sock_free,
671 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
672 .xpo_has_wspace = svc_udp_has_wspace,
673 .xpo_accept = svc_udp_accept,
679 .xcl_ops = &svc_udp_ops,
690 svsk->
sk_sk->sk_data_ready = svc_udp_data_ready;
691 svsk->
sk_sk->sk_write_space = svc_write_space;
697 svc_sock_setbufsize(svsk->
sk_sock,
698 3 * svsk->
sk_xprt.xpt_server->sv_max_mesg,
699 3 * svsk->
sk_xprt.xpt_server->sv_max_mesg);
706 switch (svsk->
sk_sk->sk_family) {
719 (
char *)&one,
sizeof(one));
720 dprintk(
"svc: kernel_setsockopt returned %d\n", err);
727 static void svc_tcp_listen_data_ready(
struct sock *sk,
int count_unused)
732 dprintk(
"svc: socket %p TCP (listen) state change %d\n",
750 printk(
"svc: socket %p: no user data\n", sk);
754 if (wq && waitqueue_active(wq))
761 static void svc_tcp_state_change(
struct sock *sk)
766 dprintk(
"svc: socket %p TCP (connected) state change %d (svsk %p)\n",
770 printk(
"svc: socket %p: no user data\n", sk);
775 if (wq && waitqueue_active(wq))
779 static void svc_tcp_data_ready(
struct sock *sk,
int count)
784 dprintk(
"svc: socket %p TCP data ready (svsk %p)\n",
790 if (wq && waitqueue_active(wq))
809 dprintk(
"svc: tcp_accept %p sock %p\n", svsk, sock);
839 "%s: connect from unprivileged port: %s\n",
841 __svc_print_addr(sin, buf,
sizeof(buf)));
844 __svc_print_addr(sin, buf,
sizeof(buf)));
849 newsock->
sk->sk_sndtimeo =
HZ*30;
851 newsvsk = svc_setup_socket(serv, newsock,
855 svc_xprt_set_remote(&newsvsk->
sk_xprt, sin, slen);
858 dprintk(
"svc_tcp_accept: kernel_getsockname error %d\n", -err);
861 svc_xprt_set_local(&newsvsk->
sk_xprt, sin, slen);
873 static unsigned int svc_tcp_restore_pages(
struct svc_sock *svsk,
struct svc_rqst *rqstp)
875 unsigned int i, len, npages;
877 if (svsk->
sk_tcplen <=
sizeof(rpc_fraghdr))
879 len = svsk->
sk_tcplen -
sizeof(rpc_fraghdr);
881 for (i = 0; i < npages; i++) {
892 static void svc_tcp_save_pages(
struct svc_sock *svsk,
struct svc_rqst *rqstp)
894 unsigned int i, len, npages;
896 if (svsk->
sk_tcplen <=
sizeof(rpc_fraghdr))
898 len = svsk->
sk_tcplen -
sizeof(rpc_fraghdr);
900 for (i = 0; i < npages; i++) {
906 static void svc_tcp_clear_pages(
struct svc_sock *svsk)
908 unsigned int i, len, npages;
910 if (svsk->
sk_tcplen <=
sizeof(rpc_fraghdr))
912 len = svsk->
sk_tcplen -
sizeof(rpc_fraghdr);
914 for (i = 0; i < npages; i++) {
929 static int svc_tcp_recv_record(
struct svc_sock *svsk,
struct svc_rqst *rqstp)
937 if (svsk->
sk_tcplen <
sizeof(rpc_fraghdr)) {
940 want =
sizeof(rpc_fraghdr) - svsk->
sk_tcplen;
943 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
948 dprintk(
"svc: short recvfrom while reading record "
949 "length (%d of %d)\n", len, want);
954 if (!(svsk->
sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
964 svsk->
sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
980 dprintk(
"RPC: TCP recv_record got %d\n", len);
989 struct rpc_xprt *bc_xprt = svsk->
sk_xprt.xpt_bc_xprt;
1004 "%s: Got unrecognized reply: "
1005 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1006 __func__,
ntohl(calldir),
1011 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
sizeof(
struct xdr_buf));
1017 dst = &req->rq_private_buf.head[0];
1018 src = &rqstp->
rq_arg.head[0];
1027 static int copy_pages_to_kvecs(
struct kvec *vec,
struct page **
pages,
int len)
1045 static int svc_tcp_recvfrom(
struct svc_rqst *rqstp)
1052 unsigned int want, base;
1057 dprintk(
"svc: tcp_recv %p data %d conn %d close %d\n",
1062 len = svc_tcp_recv_record(svsk, rqstp);
1066 base = svc_tcp_restore_pages(svsk, rqstp);
1071 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->
rq_pages[0],
1077 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1081 svc_tcp_save_pages(svsk, rqstp);
1082 if (len < 0 && len != -
EAGAIN)
1084 dprintk(
"svc: incomplete TCP record (%d of %d)\n",
1090 rqstp->
rq_arg.page_base = 0;
1091 if (rqstp->
rq_arg.len <= rqstp->
rq_arg.head[0].iov_len) {
1093 rqstp->
rq_arg.page_len = 0;
1103 len = receive_cb_reply(svsk, rqstp);
1109 if (svc_recv_available(svsk) >
sizeof(rpc_fraghdr))
1119 dprintk(
"svc: TCP complete record (%d bytes)\n", rqstp->
rq_arg.len);
1120 return rqstp->
rq_arg.len;
1125 dprintk(
"RPC: TCP recvfrom got EAGAIN\n");
1129 svsk->
sk_xprt.xpt_server->sv_name, -len);
1138 static int svc_tcp_sendto(
struct svc_rqst *rqstp)
1140 struct xdr_buf *xbufp = &rqstp->
rq_res;
1148 reclen =
htonl(0x80000000|((xbufp->len ) - 4));
1149 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1151 sent = svc_sendto(rqstp, &rqstp->
rq_res);
1152 if (sent != xbufp->len) {
1154 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1155 "- shutting down socket\n",
1156 rqstp->
rq_xprt->xpt_server->sv_name,
1157 (sent<0)?
"got error":
"sent only",
1169 static void svc_tcp_prep_reply_hdr(
struct svc_rqst *rqstp)
1177 static int svc_tcp_has_wspace(
struct svc_xprt *xprt)
1186 if (sk_stream_wspace(svsk->
sk_sk) >= required)
1197 return svc_create_socket(serv,
IPPROTO_TCP, net, sa, salen, flags);
1200 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1204 static void svc_bc_sock_free(
struct svc_xprt *xprt);
1211 return svc_bc_create_socket(serv,
IPPROTO_TCP, net, sa, salen, flags);
1214 static void svc_bc_tcp_sock_detach(
struct svc_xprt *xprt)
1220 .xpo_detach = svc_bc_tcp_sock_detach,
1221 .xpo_free = svc_bc_sock_free,
1222 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1228 .xcl_ops = &svc_tcp_bc_ops,
1232 static void svc_init_bc_xprt_sock(
void)
1237 static void svc_cleanup_bc_xprt_sock(
void)
1242 static void svc_init_bc_xprt_sock(
void)
1246 static void svc_cleanup_bc_xprt_sock(
void)
1252 .xpo_create = svc_tcp_create,
1253 .xpo_recvfrom = svc_tcp_recvfrom,
1254 .xpo_sendto = svc_tcp_sendto,
1255 .xpo_release_rqst = svc_release_skb,
1256 .xpo_detach = svc_tcp_sock_detach,
1257 .xpo_free = svc_sock_free,
1258 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1259 .xpo_has_wspace = svc_tcp_has_wspace,
1260 .xpo_accept = svc_tcp_accept,
1266 .xcl_ops = &svc_tcp_ops,
1274 svc_init_bc_xprt_sock();
1281 svc_cleanup_bc_xprt_sock();
1286 struct sock *sk = svsk->
sk_sk;
1292 dprintk(
"setting up TCP socket for listening\n");
1297 dprintk(
"setting up TCP socket for reading\n");
1325 spin_unlock_bh(&serv->
sv_lock);
1342 dprintk(
"svc: svc_setup_socket %p\n", sock);
1353 ntohs(inet_sk(inet)->inet_sport));
1357 return ERR_PTR(err);
1369 svc_udp_init(svsk, serv);
1376 svc_tcp_init(svsk, serv);
1379 dprintk(
"svc: svc_setup_socket created %p (inet %p)\n",
1424 err = PTR_ERR(svsk);
1428 svc_xprt_set_local(&svsk->
sk_xprt, sin, salen);
1430 return svc_one_sock_name(svsk, name_return, len);
1457 dprintk(
"svc: svc_create_socket(%s, %d, %s)\n",
1459 __svc_print_addr(sin, buf,
sizeof(buf)));
1463 "sockets supported\n");
1479 error =
__sock_create(net, family, type, protocol, &sock, 1);
1481 return ERR_PTR(error);
1483 svc_reclassify_socket(sock);
1493 (
char *)&val,
sizeof(val));
1511 svsk = svc_setup_socket(serv, sock, flags);
1513 error = PTR_ERR(svsk);
1516 svc_xprt_set_local(&svsk->
sk_xprt, newsin, newlen);
1519 dprintk(
"svc: svc_create_socket error = %d\n", -error);
1521 return ERR_PTR(error);
1528 static void svc_sock_detach(
struct svc_xprt *xprt)
1531 struct sock *sk = svsk->
sk_sk;
1534 dprintk(
"svc: svc_sock_detach(%p)\n", svsk);
1542 if (wq && waitqueue_active(wq))
1549 static void svc_tcp_sock_detach(
struct svc_xprt *xprt)
1553 dprintk(
"svc: svc_tcp_sock_detach(%p)\n", svsk);
1555 svc_sock_detach(xprt);
1558 svc_tcp_clear_pages(svsk);
1566 static void svc_sock_free(
struct svc_xprt *xprt)
1569 dprintk(
"svc: svc_sock_free(%p)\n", svsk);
1578 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1593 " supported on shared back channel\n");
1604 serv->sv_bc_xprt = xprt;
1612 static void svc_bc_sock_free(
struct svc_xprt *xprt)