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