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