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 <net/sock.h> 40 #include <net/checksum.h> 41 #include <net/ip.h> 42 #include <net/ipv6.h> 43 #include <net/udp.h> 44 #include <net/tcp.h> 45 #include <net/tcp_states.h> 46 #include <linux/uaccess.h> 47 #include <asm/ioctls.h> 48 #include <trace/events/skb.h> 49 50 #include <linux/sunrpc/types.h> 51 #include <linux/sunrpc/clnt.h> 52 #include <linux/sunrpc/xdr.h> 53 #include <linux/sunrpc/msg_prot.h> 54 #include <linux/sunrpc/svcsock.h> 55 #include <linux/sunrpc/stats.h> 56 #include <linux/sunrpc/xprt.h> 57 58 #include "socklib.h" 59 #include "sunrpc.h" 60 61 #define RPCDBG_FACILITY RPCDBG_SVCXPRT 62 63 64 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, 65 int flags); 66 static int svc_udp_recvfrom(struct svc_rqst *); 67 static int svc_udp_sendto(struct svc_rqst *); 68 static void svc_sock_detach(struct svc_xprt *); 69 static void svc_tcp_sock_detach(struct svc_xprt *); 70 static void svc_sock_free(struct svc_xprt *); 71 72 static struct svc_xprt *svc_create_socket(struct svc_serv *, int, 73 struct net *, struct sockaddr *, 74 int, int); 75 #ifdef CONFIG_DEBUG_LOCK_ALLOC 76 static struct lock_class_key svc_key[2]; 77 static struct lock_class_key svc_slock_key[2]; 78 79 static void svc_reclassify_socket(struct socket *sock) 80 { 81 struct sock *sk = sock->sk; 82 83 if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) 84 return; 85 86 switch (sk->sk_family) { 87 case AF_INET: 88 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", 89 &svc_slock_key[0], 90 "sk_xprt.xpt_lock-AF_INET-NFSD", 91 &svc_key[0]); 92 break; 93 94 case AF_INET6: 95 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD", 96 &svc_slock_key[1], 97 "sk_xprt.xpt_lock-AF_INET6-NFSD", 98 &svc_key[1]); 99 break; 100 101 default: 102 BUG(); 103 } 104 } 105 #else 106 static void svc_reclassify_socket(struct socket *sock) 107 { 108 } 109 #endif 110 111 /* 112 * Release an skbuff after use 113 */ 114 static void svc_release_skb(struct svc_rqst *rqstp) 115 { 116 struct sk_buff *skb = rqstp->rq_xprt_ctxt; 117 118 if (skb) { 119 struct svc_sock *svsk = 120 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); 121 rqstp->rq_xprt_ctxt = NULL; 122 123 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); 124 skb_free_datagram_locked(svsk->sk_sk, skb); 125 } 126 } 127 128 static void svc_release_udp_skb(struct svc_rqst *rqstp) 129 { 130 struct sk_buff *skb = rqstp->rq_xprt_ctxt; 131 132 if (skb) { 133 rqstp->rq_xprt_ctxt = NULL; 134 135 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); 136 consume_skb(skb); 137 } 138 } 139 140 union svc_pktinfo_u { 141 struct in_pktinfo pkti; 142 struct in6_pktinfo pkti6; 143 }; 144 #define SVC_PKTINFO_SPACE \ 145 CMSG_SPACE(sizeof(union svc_pktinfo_u)) 146 147 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh) 148 { 149 struct svc_sock *svsk = 150 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); 151 switch (svsk->sk_sk->sk_family) { 152 case AF_INET: { 153 struct in_pktinfo *pki = CMSG_DATA(cmh); 154 155 cmh->cmsg_level = SOL_IP; 156 cmh->cmsg_type = IP_PKTINFO; 157 pki->ipi_ifindex = 0; 158 pki->ipi_spec_dst.s_addr = 159 svc_daddr_in(rqstp)->sin_addr.s_addr; 160 cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); 161 } 162 break; 163 164 case AF_INET6: { 165 struct in6_pktinfo *pki = CMSG_DATA(cmh); 166 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); 167 168 cmh->cmsg_level = SOL_IPV6; 169 cmh->cmsg_type = IPV6_PKTINFO; 170 pki->ipi6_ifindex = daddr->sin6_scope_id; 171 pki->ipi6_addr = daddr->sin6_addr; 172 cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); 173 } 174 break; 175 } 176 } 177 178 static int svc_sock_read_payload(struct svc_rqst *rqstp, unsigned int offset, 179 unsigned int length) 180 { 181 return 0; 182 } 183 184 /* 185 * Report socket names for nfsdfs 186 */ 187 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining) 188 { 189 const struct sock *sk = svsk->sk_sk; 190 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ? 191 "udp" : "tcp"; 192 int len; 193 194 switch (sk->sk_family) { 195 case PF_INET: 196 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n", 197 proto_name, 198 &inet_sk(sk)->inet_rcv_saddr, 199 inet_sk(sk)->inet_num); 200 break; 201 #if IS_ENABLED(CONFIG_IPV6) 202 case PF_INET6: 203 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n", 204 proto_name, 205 &sk->sk_v6_rcv_saddr, 206 inet_sk(sk)->inet_num); 207 break; 208 #endif 209 default: 210 len = snprintf(buf, remaining, "*unknown-%d*\n", 211 sk->sk_family); 212 } 213 214 if (len >= remaining) { 215 *buf = '\0'; 216 return -ENAMETOOLONG; 217 } 218 return len; 219 } 220 221 /* 222 * Generic recvfrom routine. 223 */ 224 static ssize_t svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, 225 unsigned int nr, size_t buflen, unsigned int base) 226 { 227 struct svc_sock *svsk = 228 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); 229 struct msghdr msg = { NULL }; 230 ssize_t len; 231 232 rqstp->rq_xprt_hlen = 0; 233 234 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 235 iov_iter_kvec(&msg.msg_iter, READ, iov, nr, buflen); 236 if (base != 0) { 237 iov_iter_advance(&msg.msg_iter, base); 238 buflen -= base; 239 } 240 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT); 241 /* If we read a full record, then assume there may be more 242 * data to read (stream based sockets only!) 243 */ 244 if (len == buflen) 245 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 246 247 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n", 248 svsk, iov[0].iov_base, iov[0].iov_len, len); 249 return len; 250 } 251 252 /* 253 * Set socket snd and rcv buffer lengths 254 */ 255 static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs) 256 { 257 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg; 258 struct socket *sock = svsk->sk_sock; 259 260 nreqs = min(nreqs, INT_MAX / 2 / max_mesg); 261 262 lock_sock(sock->sk); 263 sock->sk->sk_sndbuf = nreqs * max_mesg * 2; 264 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2; 265 sock->sk->sk_write_space(sock->sk); 266 release_sock(sock->sk); 267 } 268 269 static void svc_sock_secure_port(struct svc_rqst *rqstp) 270 { 271 if (svc_port_is_privileged(svc_addr(rqstp))) 272 set_bit(RQ_SECURE, &rqstp->rq_flags); 273 else 274 clear_bit(RQ_SECURE, &rqstp->rq_flags); 275 } 276 277 /* 278 * INET callback when data has been received on the socket. 279 */ 280 static void svc_data_ready(struct sock *sk) 281 { 282 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; 283 284 if (svsk) { 285 dprintk("svc: socket %p(inet %p), busy=%d\n", 286 svsk, sk, 287 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); 288 289 /* Refer to svc_setup_socket() for details. */ 290 rmb(); 291 svsk->sk_odata(sk); 292 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags)) 293 svc_xprt_enqueue(&svsk->sk_xprt); 294 } 295 } 296 297 /* 298 * INET callback when space is newly available on the socket. 299 */ 300 static void svc_write_space(struct sock *sk) 301 { 302 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); 303 304 if (svsk) { 305 dprintk("svc: socket %p(inet %p), write_space busy=%d\n", 306 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); 307 308 /* Refer to svc_setup_socket() for details. */ 309 rmb(); 310 svsk->sk_owspace(sk); 311 svc_xprt_enqueue(&svsk->sk_xprt); 312 } 313 } 314 315 static int svc_tcp_has_wspace(struct svc_xprt *xprt) 316 { 317 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 318 319 if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) 320 return 1; 321 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); 322 } 323 324 static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt) 325 { 326 struct svc_sock *svsk; 327 struct socket *sock; 328 struct linger no_linger = { 329 .l_onoff = 1, 330 .l_linger = 0, 331 }; 332 333 svsk = container_of(xprt, struct svc_sock, sk_xprt); 334 sock = svsk->sk_sock; 335 kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, 336 (char *)&no_linger, sizeof(no_linger)); 337 } 338 339 /* 340 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo 341 */ 342 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp, 343 struct cmsghdr *cmh) 344 { 345 struct in_pktinfo *pki = CMSG_DATA(cmh); 346 struct sockaddr_in *daddr = svc_daddr_in(rqstp); 347 348 if (cmh->cmsg_type != IP_PKTINFO) 349 return 0; 350 351 daddr->sin_family = AF_INET; 352 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr; 353 return 1; 354 } 355 356 /* 357 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl 358 */ 359 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp, 360 struct cmsghdr *cmh) 361 { 362 struct in6_pktinfo *pki = CMSG_DATA(cmh); 363 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); 364 365 if (cmh->cmsg_type != IPV6_PKTINFO) 366 return 0; 367 368 daddr->sin6_family = AF_INET6; 369 daddr->sin6_addr = pki->ipi6_addr; 370 daddr->sin6_scope_id = pki->ipi6_ifindex; 371 return 1; 372 } 373 374 /* 375 * Copy the UDP datagram's destination address to the rqstp structure. 376 * The 'destination' address in this case is the address to which the 377 * peer sent the datagram, i.e. our local address. For multihomed 378 * hosts, this can change from msg to msg. Note that only the IP 379 * address changes, the port number should remain the same. 380 */ 381 static int svc_udp_get_dest_address(struct svc_rqst *rqstp, 382 struct cmsghdr *cmh) 383 { 384 switch (cmh->cmsg_level) { 385 case SOL_IP: 386 return svc_udp_get_dest_address4(rqstp, cmh); 387 case SOL_IPV6: 388 return svc_udp_get_dest_address6(rqstp, cmh); 389 } 390 391 return 0; 392 } 393 394 /* 395 * Receive a datagram from a UDP socket. 396 */ 397 static int svc_udp_recvfrom(struct svc_rqst *rqstp) 398 { 399 struct svc_sock *svsk = 400 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); 401 struct svc_serv *serv = svsk->sk_xprt.xpt_server; 402 struct sk_buff *skb; 403 union { 404 struct cmsghdr hdr; 405 long all[SVC_PKTINFO_SPACE / sizeof(long)]; 406 } buffer; 407 struct cmsghdr *cmh = &buffer.hdr; 408 struct msghdr msg = { 409 .msg_name = svc_addr(rqstp), 410 .msg_control = cmh, 411 .msg_controllen = sizeof(buffer), 412 .msg_flags = MSG_DONTWAIT, 413 }; 414 size_t len; 415 int err; 416 417 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) 418 /* udp sockets need large rcvbuf as all pending 419 * requests are still in that buffer. sndbuf must 420 * also be large enough that there is enough space 421 * for one reply per thread. We count all threads 422 * rather than threads in a particular pool, which 423 * provides an upper bound on the number of threads 424 * which will access the socket. 425 */ 426 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3); 427 428 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 429 skb = NULL; 430 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, 431 0, 0, MSG_PEEK | MSG_DONTWAIT); 432 if (err >= 0) 433 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err); 434 435 if (skb == NULL) { 436 if (err != -EAGAIN) { 437 /* possibly an icmp error */ 438 dprintk("svc: recvfrom returned error %d\n", -err); 439 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 440 } 441 return 0; 442 } 443 len = svc_addr_len(svc_addr(rqstp)); 444 rqstp->rq_addrlen = len; 445 if (skb->tstamp == 0) { 446 skb->tstamp = ktime_get_real(); 447 /* Don't enable netstamp, sunrpc doesn't 448 need that much accuracy */ 449 } 450 sock_write_timestamp(svsk->sk_sk, skb->tstamp); 451 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */ 452 453 len = skb->len; 454 rqstp->rq_arg.len = len; 455 456 rqstp->rq_prot = IPPROTO_UDP; 457 458 if (!svc_udp_get_dest_address(rqstp, cmh)) { 459 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n", 460 cmh->cmsg_level, cmh->cmsg_type); 461 goto out_free; 462 } 463 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp)); 464 465 if (skb_is_nonlinear(skb)) { 466 /* we have to copy */ 467 local_bh_disable(); 468 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { 469 local_bh_enable(); 470 /* checksum error */ 471 goto out_free; 472 } 473 local_bh_enable(); 474 consume_skb(skb); 475 } else { 476 /* we can use it in-place */ 477 rqstp->rq_arg.head[0].iov_base = skb->data; 478 rqstp->rq_arg.head[0].iov_len = len; 479 if (skb_checksum_complete(skb)) 480 goto out_free; 481 rqstp->rq_xprt_ctxt = skb; 482 } 483 484 rqstp->rq_arg.page_base = 0; 485 if (len <= rqstp->rq_arg.head[0].iov_len) { 486 rqstp->rq_arg.head[0].iov_len = len; 487 rqstp->rq_arg.page_len = 0; 488 rqstp->rq_respages = rqstp->rq_pages+1; 489 } else { 490 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; 491 rqstp->rq_respages = rqstp->rq_pages + 1 + 492 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); 493 } 494 rqstp->rq_next_page = rqstp->rq_respages+1; 495 496 if (serv->sv_stats) 497 serv->sv_stats->netudpcnt++; 498 499 return len; 500 out_free: 501 kfree_skb(skb); 502 return 0; 503 } 504 505 /** 506 * svc_udp_sendto - Send out a reply on a UDP socket 507 * @rqstp: completed svc_rqst 508 * 509 * Returns the number of bytes sent, or a negative errno. 510 */ 511 static int svc_udp_sendto(struct svc_rqst *rqstp) 512 { 513 struct svc_xprt *xprt = rqstp->rq_xprt; 514 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 515 struct xdr_buf *xdr = &rqstp->rq_res; 516 union { 517 struct cmsghdr hdr; 518 long all[SVC_PKTINFO_SPACE / sizeof(long)]; 519 } buffer; 520 struct cmsghdr *cmh = &buffer.hdr; 521 struct msghdr msg = { 522 .msg_name = &rqstp->rq_addr, 523 .msg_namelen = rqstp->rq_addrlen, 524 .msg_control = cmh, 525 .msg_controllen = sizeof(buffer), 526 }; 527 unsigned int uninitialized_var(sent); 528 int err; 529 530 svc_set_cmsg_data(rqstp, cmh); 531 532 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent); 533 xdr_free_bvec(xdr); 534 if (err == -ECONNREFUSED) { 535 /* ICMP error on earlier request. */ 536 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent); 537 xdr_free_bvec(xdr); 538 } 539 if (err < 0) 540 return err; 541 return sent; 542 } 543 544 static int svc_udp_has_wspace(struct svc_xprt *xprt) 545 { 546 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 547 struct svc_serv *serv = xprt->xpt_server; 548 unsigned long required; 549 550 /* 551 * Set the SOCK_NOSPACE flag before checking the available 552 * sock space. 553 */ 554 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); 555 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; 556 if (required*2 > sock_wspace(svsk->sk_sk)) 557 return 0; 558 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); 559 return 1; 560 } 561 562 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) 563 { 564 BUG(); 565 return NULL; 566 } 567 568 static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt) 569 { 570 } 571 572 static struct svc_xprt *svc_udp_create(struct svc_serv *serv, 573 struct net *net, 574 struct sockaddr *sa, int salen, 575 int flags) 576 { 577 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags); 578 } 579 580 static const struct svc_xprt_ops svc_udp_ops = { 581 .xpo_create = svc_udp_create, 582 .xpo_recvfrom = svc_udp_recvfrom, 583 .xpo_sendto = svc_udp_sendto, 584 .xpo_read_payload = svc_sock_read_payload, 585 .xpo_release_rqst = svc_release_udp_skb, 586 .xpo_detach = svc_sock_detach, 587 .xpo_free = svc_sock_free, 588 .xpo_has_wspace = svc_udp_has_wspace, 589 .xpo_accept = svc_udp_accept, 590 .xpo_secure_port = svc_sock_secure_port, 591 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt, 592 }; 593 594 static struct svc_xprt_class svc_udp_class = { 595 .xcl_name = "udp", 596 .xcl_owner = THIS_MODULE, 597 .xcl_ops = &svc_udp_ops, 598 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, 599 .xcl_ident = XPRT_TRANSPORT_UDP, 600 }; 601 602 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv) 603 { 604 int err, level, optname, one = 1; 605 606 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class, 607 &svsk->sk_xprt, serv); 608 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); 609 svsk->sk_sk->sk_data_ready = svc_data_ready; 610 svsk->sk_sk->sk_write_space = svc_write_space; 611 612 /* initialise setting must have enough space to 613 * receive and respond to one request. 614 * svc_udp_recvfrom will re-adjust if necessary 615 */ 616 svc_sock_setbufsize(svsk, 3); 617 618 /* data might have come in before data_ready set up */ 619 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 620 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); 621 622 /* make sure we get destination address info */ 623 switch (svsk->sk_sk->sk_family) { 624 case AF_INET: 625 level = SOL_IP; 626 optname = IP_PKTINFO; 627 break; 628 case AF_INET6: 629 level = SOL_IPV6; 630 optname = IPV6_RECVPKTINFO; 631 break; 632 default: 633 BUG(); 634 } 635 err = kernel_setsockopt(svsk->sk_sock, level, optname, 636 (char *)&one, sizeof(one)); 637 dprintk("svc: kernel_setsockopt returned %d\n", err); 638 } 639 640 /* 641 * A data_ready event on a listening socket means there's a connection 642 * pending. Do not use state_change as a substitute for it. 643 */ 644 static void svc_tcp_listen_data_ready(struct sock *sk) 645 { 646 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; 647 648 dprintk("svc: socket %p TCP (listen) state change %d\n", 649 sk, sk->sk_state); 650 651 if (svsk) { 652 /* Refer to svc_setup_socket() for details. */ 653 rmb(); 654 svsk->sk_odata(sk); 655 } 656 657 /* 658 * This callback may called twice when a new connection 659 * is established as a child socket inherits everything 660 * from a parent LISTEN socket. 661 * 1) data_ready method of the parent socket will be called 662 * when one of child sockets become ESTABLISHED. 663 * 2) data_ready method of the child socket may be called 664 * when it receives data before the socket is accepted. 665 * In case of 2, we should ignore it silently. 666 */ 667 if (sk->sk_state == TCP_LISTEN) { 668 if (svsk) { 669 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); 670 svc_xprt_enqueue(&svsk->sk_xprt); 671 } else 672 printk("svc: socket %p: no user data\n", sk); 673 } 674 } 675 676 /* 677 * A state change on a connected socket means it's dying or dead. 678 */ 679 static void svc_tcp_state_change(struct sock *sk) 680 { 681 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; 682 683 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", 684 sk, sk->sk_state, sk->sk_user_data); 685 686 if (!svsk) 687 printk("svc: socket %p: no user data\n", sk); 688 else { 689 /* Refer to svc_setup_socket() for details. */ 690 rmb(); 691 svsk->sk_ostate(sk); 692 if (sk->sk_state != TCP_ESTABLISHED) { 693 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); 694 svc_xprt_enqueue(&svsk->sk_xprt); 695 } 696 } 697 } 698 699 /* 700 * Accept a TCP connection 701 */ 702 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) 703 { 704 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 705 struct sockaddr_storage addr; 706 struct sockaddr *sin = (struct sockaddr *) &addr; 707 struct svc_serv *serv = svsk->sk_xprt.xpt_server; 708 struct socket *sock = svsk->sk_sock; 709 struct socket *newsock; 710 struct svc_sock *newsvsk; 711 int err, slen; 712 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); 713 714 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); 715 if (!sock) 716 return NULL; 717 718 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); 719 err = kernel_accept(sock, &newsock, O_NONBLOCK); 720 if (err < 0) { 721 if (err == -ENOMEM) 722 printk(KERN_WARNING "%s: no more sockets!\n", 723 serv->sv_name); 724 else if (err != -EAGAIN) 725 net_warn_ratelimited("%s: accept failed (err %d)!\n", 726 serv->sv_name, -err); 727 return NULL; 728 } 729 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); 730 731 err = kernel_getpeername(newsock, sin); 732 if (err < 0) { 733 net_warn_ratelimited("%s: peername failed (err %d)!\n", 734 serv->sv_name, -err); 735 goto failed; /* aborted connection or whatever */ 736 } 737 slen = err; 738 739 /* Ideally, we would want to reject connections from unauthorized 740 * hosts here, but when we get encryption, the IP of the host won't 741 * tell us anything. For now just warn about unpriv connections. 742 */ 743 if (!svc_port_is_privileged(sin)) { 744 dprintk("%s: connect from unprivileged port: %s\n", 745 serv->sv_name, 746 __svc_print_addr(sin, buf, sizeof(buf))); 747 } 748 dprintk("%s: connect from %s\n", serv->sv_name, 749 __svc_print_addr(sin, buf, sizeof(buf))); 750 751 /* Reset the inherited callbacks before calling svc_setup_socket */ 752 newsock->sk->sk_state_change = svsk->sk_ostate; 753 newsock->sk->sk_data_ready = svsk->sk_odata; 754 newsock->sk->sk_write_space = svsk->sk_owspace; 755 756 /* make sure that a write doesn't block forever when 757 * low on memory 758 */ 759 newsock->sk->sk_sndtimeo = HZ*30; 760 761 newsvsk = svc_setup_socket(serv, newsock, 762 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)); 763 if (IS_ERR(newsvsk)) 764 goto failed; 765 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen); 766 err = kernel_getsockname(newsock, sin); 767 slen = err; 768 if (unlikely(err < 0)) { 769 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); 770 slen = offsetof(struct sockaddr, sa_data); 771 } 772 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen); 773 774 if (sock_is_loopback(newsock->sk)) 775 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); 776 else 777 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); 778 if (serv->sv_stats) 779 serv->sv_stats->nettcpconn++; 780 781 return &newsvsk->sk_xprt; 782 783 failed: 784 sock_release(newsock); 785 return NULL; 786 } 787 788 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) 789 { 790 unsigned int i, len, npages; 791 792 if (svsk->sk_datalen == 0) 793 return 0; 794 len = svsk->sk_datalen; 795 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 796 for (i = 0; i < npages; i++) { 797 if (rqstp->rq_pages[i] != NULL) 798 put_page(rqstp->rq_pages[i]); 799 BUG_ON(svsk->sk_pages[i] == NULL); 800 rqstp->rq_pages[i] = svsk->sk_pages[i]; 801 svsk->sk_pages[i] = NULL; 802 } 803 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]); 804 return len; 805 } 806 807 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) 808 { 809 unsigned int i, len, npages; 810 811 if (svsk->sk_datalen == 0) 812 return; 813 len = svsk->sk_datalen; 814 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 815 for (i = 0; i < npages; i++) { 816 svsk->sk_pages[i] = rqstp->rq_pages[i]; 817 rqstp->rq_pages[i] = NULL; 818 } 819 } 820 821 static void svc_tcp_clear_pages(struct svc_sock *svsk) 822 { 823 unsigned int i, len, npages; 824 825 if (svsk->sk_datalen == 0) 826 goto out; 827 len = svsk->sk_datalen; 828 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 829 for (i = 0; i < npages; i++) { 830 if (svsk->sk_pages[i] == NULL) { 831 WARN_ON_ONCE(1); 832 continue; 833 } 834 put_page(svsk->sk_pages[i]); 835 svsk->sk_pages[i] = NULL; 836 } 837 out: 838 svsk->sk_tcplen = 0; 839 svsk->sk_datalen = 0; 840 } 841 842 /* 843 * Receive fragment record header. 844 * If we haven't gotten the record length yet, get the next four bytes. 845 */ 846 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp) 847 { 848 struct svc_serv *serv = svsk->sk_xprt.xpt_server; 849 unsigned int want; 850 int len; 851 852 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) { 853 struct kvec iov; 854 855 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen; 856 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; 857 iov.iov_len = want; 858 len = svc_recvfrom(rqstp, &iov, 1, want, 0); 859 if (len < 0) 860 goto error; 861 svsk->sk_tcplen += len; 862 863 if (len < want) { 864 dprintk("svc: short recvfrom while reading record " 865 "length (%d of %d)\n", len, want); 866 return -EAGAIN; 867 } 868 869 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk)); 870 if (svc_sock_reclen(svsk) + svsk->sk_datalen > 871 serv->sv_max_mesg) { 872 net_notice_ratelimited("RPC: fragment too large: %d\n", 873 svc_sock_reclen(svsk)); 874 goto err_delete; 875 } 876 } 877 878 return svc_sock_reclen(svsk); 879 error: 880 dprintk("RPC: TCP recv_record got %d\n", len); 881 return len; 882 err_delete: 883 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); 884 return -EAGAIN; 885 } 886 887 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp) 888 { 889 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt; 890 struct rpc_rqst *req = NULL; 891 struct kvec *src, *dst; 892 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base; 893 __be32 xid; 894 __be32 calldir; 895 896 xid = *p++; 897 calldir = *p; 898 899 if (!bc_xprt) 900 return -EAGAIN; 901 spin_lock(&bc_xprt->queue_lock); 902 req = xprt_lookup_rqst(bc_xprt, xid); 903 if (!req) 904 goto unlock_notfound; 905 906 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf)); 907 /* 908 * XXX!: cheating for now! Only copying HEAD. 909 * But we know this is good enough for now (in fact, for any 910 * callback reply in the forseeable future). 911 */ 912 dst = &req->rq_private_buf.head[0]; 913 src = &rqstp->rq_arg.head[0]; 914 if (dst->iov_len < src->iov_len) 915 goto unlock_eagain; /* whatever; just giving up. */ 916 memcpy(dst->iov_base, src->iov_base, src->iov_len); 917 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len); 918 rqstp->rq_arg.len = 0; 919 spin_unlock(&bc_xprt->queue_lock); 920 return 0; 921 unlock_notfound: 922 printk(KERN_NOTICE 923 "%s: Got unrecognized reply: " 924 "calldir 0x%x xpt_bc_xprt %p xid %08x\n", 925 __func__, ntohl(calldir), 926 bc_xprt, ntohl(xid)); 927 unlock_eagain: 928 spin_unlock(&bc_xprt->queue_lock); 929 return -EAGAIN; 930 } 931 932 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len) 933 { 934 int i = 0; 935 int t = 0; 936 937 while (t < len) { 938 vec[i].iov_base = page_address(pages[i]); 939 vec[i].iov_len = PAGE_SIZE; 940 i++; 941 t += PAGE_SIZE; 942 } 943 return i; 944 } 945 946 static void svc_tcp_fragment_received(struct svc_sock *svsk) 947 { 948 /* If we have more data, signal svc_xprt_enqueue() to try again */ 949 dprintk("svc: TCP %s record (%d bytes)\n", 950 svc_sock_final_rec(svsk) ? "final" : "nonfinal", 951 svc_sock_reclen(svsk)); 952 svsk->sk_tcplen = 0; 953 svsk->sk_reclen = 0; 954 } 955 956 /* 957 * Receive data from a TCP socket. 958 */ 959 static int svc_tcp_recvfrom(struct svc_rqst *rqstp) 960 { 961 struct svc_sock *svsk = 962 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); 963 struct svc_serv *serv = svsk->sk_xprt.xpt_server; 964 int len; 965 struct kvec *vec; 966 unsigned int want, base; 967 __be32 *p; 968 __be32 calldir; 969 int pnum; 970 971 dprintk("svc: tcp_recv %p data %d conn %d close %d\n", 972 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), 973 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), 974 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); 975 976 len = svc_tcp_recv_record(svsk, rqstp); 977 if (len < 0) 978 goto error; 979 980 base = svc_tcp_restore_pages(svsk, rqstp); 981 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr)); 982 983 vec = rqstp->rq_vec; 984 985 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want); 986 987 rqstp->rq_respages = &rqstp->rq_pages[pnum]; 988 rqstp->rq_next_page = rqstp->rq_respages + 1; 989 990 /* Now receive data */ 991 len = svc_recvfrom(rqstp, vec, pnum, base + want, base); 992 if (len >= 0) { 993 svsk->sk_tcplen += len; 994 svsk->sk_datalen += len; 995 } 996 if (len != want || !svc_sock_final_rec(svsk)) { 997 svc_tcp_save_pages(svsk, rqstp); 998 if (len < 0 && len != -EAGAIN) 999 goto err_delete; 1000 if (len == want) 1001 svc_tcp_fragment_received(svsk); 1002 else 1003 dprintk("svc: incomplete TCP record (%d of %d)\n", 1004 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)), 1005 svc_sock_reclen(svsk)); 1006 goto err_noclose; 1007 } 1008 1009 if (svsk->sk_datalen < 8) { 1010 svsk->sk_datalen = 0; 1011 goto err_delete; /* client is nuts. */ 1012 } 1013 1014 rqstp->rq_arg.len = svsk->sk_datalen; 1015 rqstp->rq_arg.page_base = 0; 1016 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) { 1017 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len; 1018 rqstp->rq_arg.page_len = 0; 1019 } else 1020 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; 1021 1022 rqstp->rq_xprt_ctxt = NULL; 1023 rqstp->rq_prot = IPPROTO_TCP; 1024 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags)) 1025 set_bit(RQ_LOCAL, &rqstp->rq_flags); 1026 else 1027 clear_bit(RQ_LOCAL, &rqstp->rq_flags); 1028 1029 p = (__be32 *)rqstp->rq_arg.head[0].iov_base; 1030 calldir = p[1]; 1031 if (calldir) 1032 len = receive_cb_reply(svsk, rqstp); 1033 1034 /* Reset TCP read info */ 1035 svsk->sk_datalen = 0; 1036 svc_tcp_fragment_received(svsk); 1037 1038 if (len < 0) 1039 goto error; 1040 1041 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt); 1042 if (serv->sv_stats) 1043 serv->sv_stats->nettcpcnt++; 1044 1045 return rqstp->rq_arg.len; 1046 1047 error: 1048 if (len != -EAGAIN) 1049 goto err_delete; 1050 dprintk("RPC: TCP recvfrom got EAGAIN\n"); 1051 return 0; 1052 err_delete: 1053 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", 1054 svsk->sk_xprt.xpt_server->sv_name, -len); 1055 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); 1056 err_noclose: 1057 return 0; /* record not complete */ 1058 } 1059 1060 /** 1061 * svc_tcp_sendto - Send out a reply on a TCP socket 1062 * @rqstp: completed svc_rqst 1063 * 1064 * Returns the number of bytes sent, or a negative errno. 1065 */ 1066 static int svc_tcp_sendto(struct svc_rqst *rqstp) 1067 { 1068 struct svc_xprt *xprt = rqstp->rq_xprt; 1069 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 1070 struct xdr_buf *xdr = &rqstp->rq_res; 1071 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 1072 (u32)xdr->len); 1073 struct msghdr msg = { 1074 .msg_flags = 0, 1075 }; 1076 unsigned int uninitialized_var(sent); 1077 int err; 1078 1079 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, marker, &sent); 1080 xdr_free_bvec(xdr); 1081 if (err < 0 || sent != (xdr->len + sizeof(marker))) 1082 goto out_close; 1083 return sent; 1084 1085 out_close: 1086 pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n", 1087 xprt->xpt_server->sv_name, 1088 (err < 0) ? "got error" : "sent", 1089 (err < 0) ? err : sent, xdr->len); 1090 set_bit(XPT_CLOSE, &xprt->xpt_flags); 1091 svc_xprt_enqueue(xprt); 1092 return -EAGAIN; 1093 } 1094 1095 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, 1096 struct net *net, 1097 struct sockaddr *sa, int salen, 1098 int flags) 1099 { 1100 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags); 1101 } 1102 1103 static const struct svc_xprt_ops svc_tcp_ops = { 1104 .xpo_create = svc_tcp_create, 1105 .xpo_recvfrom = svc_tcp_recvfrom, 1106 .xpo_sendto = svc_tcp_sendto, 1107 .xpo_read_payload = svc_sock_read_payload, 1108 .xpo_release_rqst = svc_release_skb, 1109 .xpo_detach = svc_tcp_sock_detach, 1110 .xpo_free = svc_sock_free, 1111 .xpo_has_wspace = svc_tcp_has_wspace, 1112 .xpo_accept = svc_tcp_accept, 1113 .xpo_secure_port = svc_sock_secure_port, 1114 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt, 1115 }; 1116 1117 static struct svc_xprt_class svc_tcp_class = { 1118 .xcl_name = "tcp", 1119 .xcl_owner = THIS_MODULE, 1120 .xcl_ops = &svc_tcp_ops, 1121 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, 1122 .xcl_ident = XPRT_TRANSPORT_TCP, 1123 }; 1124 1125 void svc_init_xprt_sock(void) 1126 { 1127 svc_reg_xprt_class(&svc_tcp_class); 1128 svc_reg_xprt_class(&svc_udp_class); 1129 } 1130 1131 void svc_cleanup_xprt_sock(void) 1132 { 1133 svc_unreg_xprt_class(&svc_tcp_class); 1134 svc_unreg_xprt_class(&svc_udp_class); 1135 } 1136 1137 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv) 1138 { 1139 struct sock *sk = svsk->sk_sk; 1140 1141 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class, 1142 &svsk->sk_xprt, serv); 1143 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); 1144 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags); 1145 if (sk->sk_state == TCP_LISTEN) { 1146 dprintk("setting up TCP socket for listening\n"); 1147 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener"); 1148 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); 1149 sk->sk_data_ready = svc_tcp_listen_data_ready; 1150 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); 1151 } else { 1152 dprintk("setting up TCP socket for reading\n"); 1153 sk->sk_state_change = svc_tcp_state_change; 1154 sk->sk_data_ready = svc_data_ready; 1155 sk->sk_write_space = svc_write_space; 1156 1157 svsk->sk_reclen = 0; 1158 svsk->sk_tcplen = 0; 1159 svsk->sk_datalen = 0; 1160 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages)); 1161 1162 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1163 1164 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); 1165 switch (sk->sk_state) { 1166 case TCP_SYN_RECV: 1167 case TCP_ESTABLISHED: 1168 break; 1169 default: 1170 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); 1171 } 1172 } 1173 } 1174 1175 void svc_sock_update_bufs(struct svc_serv *serv) 1176 { 1177 /* 1178 * The number of server threads has changed. Update 1179 * rcvbuf and sndbuf accordingly on all sockets 1180 */ 1181 struct svc_sock *svsk; 1182 1183 spin_lock_bh(&serv->sv_lock); 1184 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) 1185 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); 1186 spin_unlock_bh(&serv->sv_lock); 1187 } 1188 EXPORT_SYMBOL_GPL(svc_sock_update_bufs); 1189 1190 /* 1191 * Initialize socket for RPC use and create svc_sock struct 1192 */ 1193 static struct svc_sock *svc_setup_socket(struct svc_serv *serv, 1194 struct socket *sock, 1195 int flags) 1196 { 1197 struct svc_sock *svsk; 1198 struct sock *inet; 1199 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); 1200 int err = 0; 1201 1202 dprintk("svc: svc_setup_socket %p\n", sock); 1203 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL); 1204 if (!svsk) 1205 return ERR_PTR(-ENOMEM); 1206 1207 inet = sock->sk; 1208 1209 /* Register socket with portmapper */ 1210 if (pmap_register) 1211 err = svc_register(serv, sock_net(sock->sk), inet->sk_family, 1212 inet->sk_protocol, 1213 ntohs(inet_sk(inet)->inet_sport)); 1214 1215 if (err < 0) { 1216 kfree(svsk); 1217 return ERR_PTR(err); 1218 } 1219 1220 svsk->sk_sock = sock; 1221 svsk->sk_sk = inet; 1222 svsk->sk_ostate = inet->sk_state_change; 1223 svsk->sk_odata = inet->sk_data_ready; 1224 svsk->sk_owspace = inet->sk_write_space; 1225 /* 1226 * This barrier is necessary in order to prevent race condition 1227 * with svc_data_ready(), svc_listen_data_ready() and others 1228 * when calling callbacks above. 1229 */ 1230 wmb(); 1231 inet->sk_user_data = svsk; 1232 1233 /* Initialize the socket */ 1234 if (sock->type == SOCK_DGRAM) 1235 svc_udp_init(svsk, serv); 1236 else 1237 svc_tcp_init(svsk, serv); 1238 1239 dprintk("svc: svc_setup_socket created %p (inet %p), " 1240 "listen %d close %d\n", 1241 svsk, svsk->sk_sk, 1242 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags), 1243 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); 1244 1245 return svsk; 1246 } 1247 1248 bool svc_alien_sock(struct net *net, int fd) 1249 { 1250 int err; 1251 struct socket *sock = sockfd_lookup(fd, &err); 1252 bool ret = false; 1253 1254 if (!sock) 1255 goto out; 1256 if (sock_net(sock->sk) != net) 1257 ret = true; 1258 sockfd_put(sock); 1259 out: 1260 return ret; 1261 } 1262 EXPORT_SYMBOL_GPL(svc_alien_sock); 1263 1264 /** 1265 * svc_addsock - add a listener socket to an RPC service 1266 * @serv: pointer to RPC service to which to add a new listener 1267 * @fd: file descriptor of the new listener 1268 * @name_return: pointer to buffer to fill in with name of listener 1269 * @len: size of the buffer 1270 * @cred: credential 1271 * 1272 * Fills in socket name and returns positive length of name if successful. 1273 * Name is terminated with '\n'. On error, returns a negative errno 1274 * value. 1275 */ 1276 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return, 1277 const size_t len, const struct cred *cred) 1278 { 1279 int err = 0; 1280 struct socket *so = sockfd_lookup(fd, &err); 1281 struct svc_sock *svsk = NULL; 1282 struct sockaddr_storage addr; 1283 struct sockaddr *sin = (struct sockaddr *)&addr; 1284 int salen; 1285 1286 if (!so) 1287 return err; 1288 err = -EAFNOSUPPORT; 1289 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6)) 1290 goto out; 1291 err = -EPROTONOSUPPORT; 1292 if (so->sk->sk_protocol != IPPROTO_TCP && 1293 so->sk->sk_protocol != IPPROTO_UDP) 1294 goto out; 1295 err = -EISCONN; 1296 if (so->state > SS_UNCONNECTED) 1297 goto out; 1298 err = -ENOENT; 1299 if (!try_module_get(THIS_MODULE)) 1300 goto out; 1301 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS); 1302 if (IS_ERR(svsk)) { 1303 module_put(THIS_MODULE); 1304 err = PTR_ERR(svsk); 1305 goto out; 1306 } 1307 salen = kernel_getsockname(svsk->sk_sock, sin); 1308 if (salen >= 0) 1309 svc_xprt_set_local(&svsk->sk_xprt, sin, salen); 1310 svsk->sk_xprt.xpt_cred = get_cred(cred); 1311 svc_add_new_perm_xprt(serv, &svsk->sk_xprt); 1312 return svc_one_sock_name(svsk, name_return, len); 1313 out: 1314 sockfd_put(so); 1315 return err; 1316 } 1317 EXPORT_SYMBOL_GPL(svc_addsock); 1318 1319 /* 1320 * Create socket for RPC service. 1321 */ 1322 static struct svc_xprt *svc_create_socket(struct svc_serv *serv, 1323 int protocol, 1324 struct net *net, 1325 struct sockaddr *sin, int len, 1326 int flags) 1327 { 1328 struct svc_sock *svsk; 1329 struct socket *sock; 1330 int error; 1331 int type; 1332 struct sockaddr_storage addr; 1333 struct sockaddr *newsin = (struct sockaddr *)&addr; 1334 int newlen; 1335 int family; 1336 int val; 1337 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); 1338 1339 dprintk("svc: svc_create_socket(%s, %d, %s)\n", 1340 serv->sv_program->pg_name, protocol, 1341 __svc_print_addr(sin, buf, sizeof(buf))); 1342 1343 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { 1344 printk(KERN_WARNING "svc: only UDP and TCP " 1345 "sockets supported\n"); 1346 return ERR_PTR(-EINVAL); 1347 } 1348 1349 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; 1350 switch (sin->sa_family) { 1351 case AF_INET6: 1352 family = PF_INET6; 1353 break; 1354 case AF_INET: 1355 family = PF_INET; 1356 break; 1357 default: 1358 return ERR_PTR(-EINVAL); 1359 } 1360 1361 error = __sock_create(net, family, type, protocol, &sock, 1); 1362 if (error < 0) 1363 return ERR_PTR(error); 1364 1365 svc_reclassify_socket(sock); 1366 1367 /* 1368 * If this is an PF_INET6 listener, we want to avoid 1369 * getting requests from IPv4 remotes. Those should 1370 * be shunted to a PF_INET listener via rpcbind. 1371 */ 1372 val = 1; 1373 if (family == PF_INET6) 1374 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, 1375 (char *)&val, sizeof(val)); 1376 1377 if (type == SOCK_STREAM) 1378 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */ 1379 error = kernel_bind(sock, sin, len); 1380 if (error < 0) 1381 goto bummer; 1382 1383 error = kernel_getsockname(sock, newsin); 1384 if (error < 0) 1385 goto bummer; 1386 newlen = error; 1387 1388 if (protocol == IPPROTO_TCP) { 1389 if ((error = kernel_listen(sock, 64)) < 0) 1390 goto bummer; 1391 } 1392 1393 svsk = svc_setup_socket(serv, sock, flags); 1394 if (IS_ERR(svsk)) { 1395 error = PTR_ERR(svsk); 1396 goto bummer; 1397 } 1398 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen); 1399 return (struct svc_xprt *)svsk; 1400 bummer: 1401 dprintk("svc: svc_create_socket error = %d\n", -error); 1402 sock_release(sock); 1403 return ERR_PTR(error); 1404 } 1405 1406 /* 1407 * Detach the svc_sock from the socket so that no 1408 * more callbacks occur. 1409 */ 1410 static void svc_sock_detach(struct svc_xprt *xprt) 1411 { 1412 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 1413 struct sock *sk = svsk->sk_sk; 1414 1415 dprintk("svc: svc_sock_detach(%p)\n", svsk); 1416 1417 /* put back the old socket callbacks */ 1418 lock_sock(sk); 1419 sk->sk_state_change = svsk->sk_ostate; 1420 sk->sk_data_ready = svsk->sk_odata; 1421 sk->sk_write_space = svsk->sk_owspace; 1422 sk->sk_user_data = NULL; 1423 release_sock(sk); 1424 } 1425 1426 /* 1427 * Disconnect the socket, and reset the callbacks 1428 */ 1429 static void svc_tcp_sock_detach(struct svc_xprt *xprt) 1430 { 1431 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 1432 1433 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk); 1434 1435 svc_sock_detach(xprt); 1436 1437 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) { 1438 svc_tcp_clear_pages(svsk); 1439 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR); 1440 } 1441 } 1442 1443 /* 1444 * Free the svc_sock's socket resources and the svc_sock itself. 1445 */ 1446 static void svc_sock_free(struct svc_xprt *xprt) 1447 { 1448 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); 1449 dprintk("svc: svc_sock_free(%p)\n", svsk); 1450 1451 if (svsk->sk_sock->file) 1452 sockfd_put(svsk->sk_sock); 1453 else 1454 sock_release(svsk->sk_sock); 1455 kfree(svsk); 1456 } 1457