GNU Linux-libre 5.4.257-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         /*
732          * This callback may called twice when a new connection
733          * is established as a child socket inherits everything
734          * from a parent LISTEN socket.
735          * 1) data_ready method of the parent socket will be called
736          *    when one of child sockets become ESTABLISHED.
737          * 2) data_ready method of the child socket may be called
738          *    when it receives data before the socket is accepted.
739          * In case of 2, we should ignore it silently and DO NOT
740          * dereference svsk.
741          */
742         if (sk->sk_state != TCP_LISTEN)
743                 return;
744
745         if (svsk) {
746                 /* Refer to svc_setup_socket() for details. */
747                 rmb();
748                 svsk->sk_odata(sk);
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  * A state change on a connected socket means it's dying or dead.
757  */
758 static void svc_tcp_state_change(struct sock *sk)
759 {
760         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
761
762         dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
763                 sk, sk->sk_state, sk->sk_user_data);
764
765         if (!svsk)
766                 printk("svc: socket %p: no user data\n", sk);
767         else {
768                 /* Refer to svc_setup_socket() for details. */
769                 rmb();
770                 svsk->sk_ostate(sk);
771                 if (sk->sk_state != TCP_ESTABLISHED) {
772                         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
773                         svc_xprt_enqueue(&svsk->sk_xprt);
774                 }
775         }
776 }
777
778 /*
779  * Accept a TCP connection
780  */
781 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
782 {
783         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
784         struct sockaddr_storage addr;
785         struct sockaddr *sin = (struct sockaddr *) &addr;
786         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
787         struct socket   *sock = svsk->sk_sock;
788         struct socket   *newsock;
789         struct svc_sock *newsvsk;
790         int             err, slen;
791         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
792
793         dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
794         if (!sock)
795                 return NULL;
796
797         clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
798         err = kernel_accept(sock, &newsock, O_NONBLOCK);
799         if (err < 0) {
800                 if (err == -ENOMEM)
801                         printk(KERN_WARNING "%s: no more sockets!\n",
802                                serv->sv_name);
803                 else if (err != -EAGAIN)
804                         net_warn_ratelimited("%s: accept failed (err %d)!\n",
805                                              serv->sv_name, -err);
806                 return NULL;
807         }
808         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
809
810         err = kernel_getpeername(newsock, sin);
811         if (err < 0) {
812                 net_warn_ratelimited("%s: peername failed (err %d)!\n",
813                                      serv->sv_name, -err);
814                 goto failed;            /* aborted connection or whatever */
815         }
816         slen = err;
817
818         /* Ideally, we would want to reject connections from unauthorized
819          * hosts here, but when we get encryption, the IP of the host won't
820          * tell us anything.  For now just warn about unpriv connections.
821          */
822         if (!svc_port_is_privileged(sin)) {
823                 dprintk("%s: connect from unprivileged port: %s\n",
824                         serv->sv_name,
825                         __svc_print_addr(sin, buf, sizeof(buf)));
826         }
827         dprintk("%s: connect from %s\n", serv->sv_name,
828                 __svc_print_addr(sin, buf, sizeof(buf)));
829
830         /* Reset the inherited callbacks before calling svc_setup_socket */
831         newsock->sk->sk_state_change = svsk->sk_ostate;
832         newsock->sk->sk_data_ready = svsk->sk_odata;
833         newsock->sk->sk_write_space = svsk->sk_owspace;
834
835         /* make sure that a write doesn't block forever when
836          * low on memory
837          */
838         newsock->sk->sk_sndtimeo = HZ*30;
839
840         newsvsk = svc_setup_socket(serv, newsock,
841                                  (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
842         if (IS_ERR(newsvsk))
843                 goto failed;
844         svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
845         err = kernel_getsockname(newsock, sin);
846         slen = err;
847         if (unlikely(err < 0)) {
848                 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
849                 slen = offsetof(struct sockaddr, sa_data);
850         }
851         svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
852
853         if (sock_is_loopback(newsock->sk))
854                 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
855         else
856                 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
857         if (serv->sv_stats)
858                 serv->sv_stats->nettcpconn++;
859
860         return &newsvsk->sk_xprt;
861
862 failed:
863         sock_release(newsock);
864         return NULL;
865 }
866
867 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
868 {
869         unsigned int i, len, npages;
870
871         if (svsk->sk_datalen == 0)
872                 return 0;
873         len = svsk->sk_datalen;
874         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
875         for (i = 0; i < npages; i++) {
876                 if (rqstp->rq_pages[i] != NULL)
877                         put_page(rqstp->rq_pages[i]);
878                 BUG_ON(svsk->sk_pages[i] == NULL);
879                 rqstp->rq_pages[i] = svsk->sk_pages[i];
880                 svsk->sk_pages[i] = NULL;
881         }
882         rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
883         return len;
884 }
885
886 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
887 {
888         unsigned int i, len, npages;
889
890         if (svsk->sk_datalen == 0)
891                 return;
892         len = svsk->sk_datalen;
893         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
894         for (i = 0; i < npages; i++) {
895                 svsk->sk_pages[i] = rqstp->rq_pages[i];
896                 rqstp->rq_pages[i] = NULL;
897         }
898 }
899
900 static void svc_tcp_clear_pages(struct svc_sock *svsk)
901 {
902         unsigned int i, len, npages;
903
904         if (svsk->sk_datalen == 0)
905                 goto out;
906         len = svsk->sk_datalen;
907         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
908         for (i = 0; i < npages; i++) {
909                 if (svsk->sk_pages[i] == NULL) {
910                         WARN_ON_ONCE(1);
911                         continue;
912                 }
913                 put_page(svsk->sk_pages[i]);
914                 svsk->sk_pages[i] = NULL;
915         }
916 out:
917         svsk->sk_tcplen = 0;
918         svsk->sk_datalen = 0;
919 }
920
921 /*
922  * Receive fragment record header.
923  * If we haven't gotten the record length yet, get the next four bytes.
924  */
925 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
926 {
927         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
928         unsigned int want;
929         int len;
930
931         if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
932                 struct kvec     iov;
933
934                 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
935                 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
936                 iov.iov_len  = want;
937                 len = svc_recvfrom(rqstp, &iov, 1, want, 0);
938                 if (len < 0)
939                         goto error;
940                 svsk->sk_tcplen += len;
941
942                 if (len < want) {
943                         dprintk("svc: short recvfrom while reading record "
944                                 "length (%d of %d)\n", len, want);
945                         return -EAGAIN;
946                 }
947
948                 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
949                 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
950                                                         serv->sv_max_mesg) {
951                         net_notice_ratelimited("RPC: fragment too large: %d\n",
952                                         svc_sock_reclen(svsk));
953                         goto err_delete;
954                 }
955         }
956
957         return svc_sock_reclen(svsk);
958 error:
959         dprintk("RPC: TCP recv_record got %d\n", len);
960         return len;
961 err_delete:
962         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
963         return -EAGAIN;
964 }
965
966 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
967 {
968         struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
969         struct rpc_rqst *req = NULL;
970         struct kvec *src, *dst;
971         __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
972         __be32 xid;
973         __be32 calldir;
974
975         xid = *p++;
976         calldir = *p;
977
978         if (!bc_xprt)
979                 return -EAGAIN;
980         spin_lock(&bc_xprt->queue_lock);
981         req = xprt_lookup_rqst(bc_xprt, xid);
982         if (!req)
983                 goto unlock_notfound;
984
985         memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
986         /*
987          * XXX!: cheating for now!  Only copying HEAD.
988          * But we know this is good enough for now (in fact, for any
989          * callback reply in the forseeable future).
990          */
991         dst = &req->rq_private_buf.head[0];
992         src = &rqstp->rq_arg.head[0];
993         if (dst->iov_len < src->iov_len)
994                 goto unlock_eagain; /* whatever; just giving up. */
995         memcpy(dst->iov_base, src->iov_base, src->iov_len);
996         xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
997         rqstp->rq_arg.len = 0;
998         spin_unlock(&bc_xprt->queue_lock);
999         return 0;
1000 unlock_notfound:
1001         printk(KERN_NOTICE
1002                 "%s: Got unrecognized reply: "
1003                 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1004                 __func__, ntohl(calldir),
1005                 bc_xprt, ntohl(xid));
1006 unlock_eagain:
1007         spin_unlock(&bc_xprt->queue_lock);
1008         return -EAGAIN;
1009 }
1010
1011 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1012 {
1013         int i = 0;
1014         int t = 0;
1015
1016         while (t < len) {
1017                 vec[i].iov_base = page_address(pages[i]);
1018                 vec[i].iov_len = PAGE_SIZE;
1019                 i++;
1020                 t += PAGE_SIZE;
1021         }
1022         return i;
1023 }
1024
1025 static void svc_tcp_fragment_received(struct svc_sock *svsk)
1026 {
1027         /* If we have more data, signal svc_xprt_enqueue() to try again */
1028         dprintk("svc: TCP %s record (%d bytes)\n",
1029                 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1030                 svc_sock_reclen(svsk));
1031         svsk->sk_tcplen = 0;
1032         svsk->sk_reclen = 0;
1033 }
1034
1035 /*
1036  * Receive data from a TCP socket.
1037  */
1038 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1039 {
1040         struct svc_sock *svsk =
1041                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1042         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1043         int             len;
1044         struct kvec *vec;
1045         unsigned int want, base;
1046         __be32 *p;
1047         __be32 calldir;
1048         int pnum;
1049
1050         dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1051                 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1052                 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1053                 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1054
1055         len = svc_tcp_recv_record(svsk, rqstp);
1056         if (len < 0)
1057                 goto error;
1058
1059         base = svc_tcp_restore_pages(svsk, rqstp);
1060         want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
1061
1062         vec = rqstp->rq_vec;
1063
1064         pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want);
1065
1066         rqstp->rq_respages = &rqstp->rq_pages[pnum];
1067         rqstp->rq_next_page = rqstp->rq_respages + 1;
1068
1069         /* Now receive data */
1070         len = svc_recvfrom(rqstp, vec, pnum, base + want, base);
1071         if (len >= 0) {
1072                 svsk->sk_tcplen += len;
1073                 svsk->sk_datalen += len;
1074         }
1075         if (len != want || !svc_sock_final_rec(svsk)) {
1076                 svc_tcp_save_pages(svsk, rqstp);
1077                 if (len < 0 && len != -EAGAIN)
1078                         goto err_delete;
1079                 if (len == want)
1080                         svc_tcp_fragment_received(svsk);
1081                 else
1082                         dprintk("svc: incomplete TCP record (%d of %d)\n",
1083                                 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
1084                                 svc_sock_reclen(svsk));
1085                 goto err_noclose;
1086         }
1087
1088         if (svsk->sk_datalen < 8) {
1089                 svsk->sk_datalen = 0;
1090                 goto err_delete; /* client is nuts. */
1091         }
1092
1093         rqstp->rq_arg.len = svsk->sk_datalen;
1094         rqstp->rq_arg.page_base = 0;
1095         if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1096                 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1097                 rqstp->rq_arg.page_len = 0;
1098         } else
1099                 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1100
1101         rqstp->rq_xprt_ctxt   = NULL;
1102         rqstp->rq_prot        = IPPROTO_TCP;
1103         if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1104                 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1105         else
1106                 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1107
1108         p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1109         calldir = p[1];
1110         if (calldir)
1111                 len = receive_cb_reply(svsk, rqstp);
1112
1113         /* Reset TCP read info */
1114         svsk->sk_datalen = 0;
1115         svc_tcp_fragment_received(svsk);
1116
1117         if (len < 0)
1118                 goto error;
1119
1120         svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1121         if (serv->sv_stats)
1122                 serv->sv_stats->nettcpcnt++;
1123
1124         return rqstp->rq_arg.len;
1125
1126 error:
1127         if (len != -EAGAIN)
1128                 goto err_delete;
1129         dprintk("RPC: TCP recvfrom got EAGAIN\n");
1130         return 0;
1131 err_delete:
1132         printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1133                svsk->sk_xprt.xpt_server->sv_name, -len);
1134         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1135 err_noclose:
1136         return 0;       /* record not complete */
1137 }
1138
1139 /*
1140  * Send out data on TCP socket.
1141  */
1142 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1143 {
1144         struct xdr_buf  *xbufp = &rqstp->rq_res;
1145         int sent;
1146         __be32 reclen;
1147
1148         svc_release_skb(rqstp);
1149
1150         /* Set up the first element of the reply kvec.
1151          * Any other kvecs that may be in use have been taken
1152          * care of by the server implementation itself.
1153          */
1154         reclen = htonl(0x80000000|((xbufp->len ) - 4));
1155         memcpy(xbufp->head[0].iov_base, &reclen, 4);
1156
1157         sent = svc_sendto(rqstp, &rqstp->rq_res);
1158         if (sent != xbufp->len) {
1159                 printk(KERN_NOTICE
1160                        "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1161                        "- shutting down socket\n",
1162                        rqstp->rq_xprt->xpt_server->sv_name,
1163                        (sent<0)?"got error":"sent only",
1164                        sent, xbufp->len);
1165                 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
1166                 svc_xprt_enqueue(rqstp->rq_xprt);
1167                 sent = -EAGAIN;
1168         }
1169         return sent;
1170 }
1171
1172 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1173                                        struct net *net,
1174                                        struct sockaddr *sa, int salen,
1175                                        int flags)
1176 {
1177         return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1178 }
1179
1180 static const struct svc_xprt_ops svc_tcp_ops = {
1181         .xpo_create = svc_tcp_create,
1182         .xpo_recvfrom = svc_tcp_recvfrom,
1183         .xpo_sendto = svc_tcp_sendto,
1184         .xpo_read_payload = svc_sock_read_payload,
1185         .xpo_release_rqst = svc_release_skb,
1186         .xpo_detach = svc_tcp_sock_detach,
1187         .xpo_free = svc_sock_free,
1188         .xpo_has_wspace = svc_tcp_has_wspace,
1189         .xpo_accept = svc_tcp_accept,
1190         .xpo_secure_port = svc_sock_secure_port,
1191         .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1192 };
1193
1194 static struct svc_xprt_class svc_tcp_class = {
1195         .xcl_name = "tcp",
1196         .xcl_owner = THIS_MODULE,
1197         .xcl_ops = &svc_tcp_ops,
1198         .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1199         .xcl_ident = XPRT_TRANSPORT_TCP,
1200 };
1201
1202 void svc_init_xprt_sock(void)
1203 {
1204         svc_reg_xprt_class(&svc_tcp_class);
1205         svc_reg_xprt_class(&svc_udp_class);
1206 }
1207
1208 void svc_cleanup_xprt_sock(void)
1209 {
1210         svc_unreg_xprt_class(&svc_tcp_class);
1211         svc_unreg_xprt_class(&svc_udp_class);
1212 }
1213
1214 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1215 {
1216         struct sock     *sk = svsk->sk_sk;
1217
1218         svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1219                       &svsk->sk_xprt, serv);
1220         set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1221         set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1222         if (sk->sk_state == TCP_LISTEN) {
1223                 dprintk("setting up TCP socket for listening\n");
1224                 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1225                 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1226                 sk->sk_data_ready = svc_tcp_listen_data_ready;
1227                 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1228         } else {
1229                 dprintk("setting up TCP socket for reading\n");
1230                 sk->sk_state_change = svc_tcp_state_change;
1231                 sk->sk_data_ready = svc_data_ready;
1232                 sk->sk_write_space = svc_write_space;
1233
1234                 svsk->sk_reclen = 0;
1235                 svsk->sk_tcplen = 0;
1236                 svsk->sk_datalen = 0;
1237                 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1238
1239                 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1240
1241                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1242                 switch (sk->sk_state) {
1243                 case TCP_SYN_RECV:
1244                 case TCP_ESTABLISHED:
1245                         break;
1246                 default:
1247                         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1248                 }
1249         }
1250 }
1251
1252 void svc_sock_update_bufs(struct svc_serv *serv)
1253 {
1254         /*
1255          * The number of server threads has changed. Update
1256          * rcvbuf and sndbuf accordingly on all sockets
1257          */
1258         struct svc_sock *svsk;
1259
1260         spin_lock_bh(&serv->sv_lock);
1261         list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1262                 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1263         spin_unlock_bh(&serv->sv_lock);
1264 }
1265 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1266
1267 /*
1268  * Initialize socket for RPC use and create svc_sock struct
1269  */
1270 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1271                                                 struct socket *sock,
1272                                                 int flags)
1273 {
1274         struct svc_sock *svsk;
1275         struct sock     *inet;
1276         int             pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1277         int             err = 0;
1278
1279         dprintk("svc: svc_setup_socket %p\n", sock);
1280         svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1281         if (!svsk)
1282                 return ERR_PTR(-ENOMEM);
1283
1284         inet = sock->sk;
1285
1286         /* Register socket with portmapper */
1287         if (pmap_register)
1288                 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1289                                      inet->sk_protocol,
1290                                      ntohs(inet_sk(inet)->inet_sport));
1291
1292         if (err < 0) {
1293                 kfree(svsk);
1294                 return ERR_PTR(err);
1295         }
1296
1297         svsk->sk_sock = sock;
1298         svsk->sk_sk = inet;
1299         svsk->sk_ostate = inet->sk_state_change;
1300         svsk->sk_odata = inet->sk_data_ready;
1301         svsk->sk_owspace = inet->sk_write_space;
1302         /*
1303          * This barrier is necessary in order to prevent race condition
1304          * with svc_data_ready(), svc_listen_data_ready() and others
1305          * when calling callbacks above.
1306          */
1307         wmb();
1308         inet->sk_user_data = svsk;
1309
1310         /* Initialize the socket */
1311         if (sock->type == SOCK_DGRAM)
1312                 svc_udp_init(svsk, serv);
1313         else
1314                 svc_tcp_init(svsk, serv);
1315
1316         dprintk("svc: svc_setup_socket created %p (inet %p), "
1317                         "listen %d close %d\n",
1318                         svsk, svsk->sk_sk,
1319                         test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1320                         test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1321
1322         return svsk;
1323 }
1324
1325 bool svc_alien_sock(struct net *net, int fd)
1326 {
1327         int err;
1328         struct socket *sock = sockfd_lookup(fd, &err);
1329         bool ret = false;
1330
1331         if (!sock)
1332                 goto out;
1333         if (sock_net(sock->sk) != net)
1334                 ret = true;
1335         sockfd_put(sock);
1336 out:
1337         return ret;
1338 }
1339 EXPORT_SYMBOL_GPL(svc_alien_sock);
1340
1341 /**
1342  * svc_addsock - add a listener socket to an RPC service
1343  * @serv: pointer to RPC service to which to add a new listener
1344  * @fd: file descriptor of the new listener
1345  * @name_return: pointer to buffer to fill in with name of listener
1346  * @len: size of the buffer
1347  * @cred: credential
1348  *
1349  * Fills in socket name and returns positive length of name if successful.
1350  * Name is terminated with '\n'.  On error, returns a negative errno
1351  * value.
1352  */
1353 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1354                 const size_t len, const struct cred *cred)
1355 {
1356         int err = 0;
1357         struct socket *so = sockfd_lookup(fd, &err);
1358         struct svc_sock *svsk = NULL;
1359         struct sockaddr_storage addr;
1360         struct sockaddr *sin = (struct sockaddr *)&addr;
1361         int salen;
1362
1363         if (!so)
1364                 return err;
1365         err = -EAFNOSUPPORT;
1366         if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1367                 goto out;
1368         err =  -EPROTONOSUPPORT;
1369         if (so->sk->sk_protocol != IPPROTO_TCP &&
1370             so->sk->sk_protocol != IPPROTO_UDP)
1371                 goto out;
1372         err = -EISCONN;
1373         if (so->state > SS_UNCONNECTED)
1374                 goto out;
1375         err = -ENOENT;
1376         if (!try_module_get(THIS_MODULE))
1377                 goto out;
1378         svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1379         if (IS_ERR(svsk)) {
1380                 module_put(THIS_MODULE);
1381                 err = PTR_ERR(svsk);
1382                 goto out;
1383         }
1384         salen = kernel_getsockname(svsk->sk_sock, sin);
1385         if (salen >= 0)
1386                 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1387         svsk->sk_xprt.xpt_cred = get_cred(cred);
1388         svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1389         return svc_one_sock_name(svsk, name_return, len);
1390 out:
1391         sockfd_put(so);
1392         return err;
1393 }
1394 EXPORT_SYMBOL_GPL(svc_addsock);
1395
1396 /*
1397  * Create socket for RPC service.
1398  */
1399 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1400                                           int protocol,
1401                                           struct net *net,
1402                                           struct sockaddr *sin, int len,
1403                                           int flags)
1404 {
1405         struct svc_sock *svsk;
1406         struct socket   *sock;
1407         int             error;
1408         int             type;
1409         struct sockaddr_storage addr;
1410         struct sockaddr *newsin = (struct sockaddr *)&addr;
1411         int             newlen;
1412         int             family;
1413         int             val;
1414         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1415
1416         dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1417                         serv->sv_program->pg_name, protocol,
1418                         __svc_print_addr(sin, buf, sizeof(buf)));
1419
1420         if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1421                 printk(KERN_WARNING "svc: only UDP and TCP "
1422                                 "sockets supported\n");
1423                 return ERR_PTR(-EINVAL);
1424         }
1425
1426         type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1427         switch (sin->sa_family) {
1428         case AF_INET6:
1429                 family = PF_INET6;
1430                 break;
1431         case AF_INET:
1432                 family = PF_INET;
1433                 break;
1434         default:
1435                 return ERR_PTR(-EINVAL);
1436         }
1437
1438         error = __sock_create(net, family, type, protocol, &sock, 1);
1439         if (error < 0)
1440                 return ERR_PTR(error);
1441
1442         svc_reclassify_socket(sock);
1443
1444         /*
1445          * If this is an PF_INET6 listener, we want to avoid
1446          * getting requests from IPv4 remotes.  Those should
1447          * be shunted to a PF_INET listener via rpcbind.
1448          */
1449         val = 1;
1450         if (family == PF_INET6)
1451                 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1452                                         (char *)&val, sizeof(val));
1453
1454         if (type == SOCK_STREAM)
1455                 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1456         error = kernel_bind(sock, sin, len);
1457         if (error < 0)
1458                 goto bummer;
1459
1460         error = kernel_getsockname(sock, newsin);
1461         if (error < 0)
1462                 goto bummer;
1463         newlen = error;
1464
1465         if (protocol == IPPROTO_TCP) {
1466                 if ((error = kernel_listen(sock, 64)) < 0)
1467                         goto bummer;
1468         }
1469
1470         svsk = svc_setup_socket(serv, sock, flags);
1471         if (IS_ERR(svsk)) {
1472                 error = PTR_ERR(svsk);
1473                 goto bummer;
1474         }
1475         svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1476         return (struct svc_xprt *)svsk;
1477 bummer:
1478         dprintk("svc: svc_create_socket error = %d\n", -error);
1479         sock_release(sock);
1480         return ERR_PTR(error);
1481 }
1482
1483 /*
1484  * Detach the svc_sock from the socket so that no
1485  * more callbacks occur.
1486  */
1487 static void svc_sock_detach(struct svc_xprt *xprt)
1488 {
1489         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1490         struct sock *sk = svsk->sk_sk;
1491
1492         dprintk("svc: svc_sock_detach(%p)\n", svsk);
1493
1494         /* put back the old socket callbacks */
1495         lock_sock(sk);
1496         sk->sk_state_change = svsk->sk_ostate;
1497         sk->sk_data_ready = svsk->sk_odata;
1498         sk->sk_write_space = svsk->sk_owspace;
1499         sk->sk_user_data = NULL;
1500         release_sock(sk);
1501 }
1502
1503 /*
1504  * Disconnect the socket, and reset the callbacks
1505  */
1506 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1507 {
1508         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1509
1510         dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1511
1512         svc_sock_detach(xprt);
1513
1514         if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1515                 svc_tcp_clear_pages(svsk);
1516                 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1517         }
1518 }
1519
1520 /*
1521  * Free the svc_sock's socket resources and the svc_sock itself.
1522  */
1523 static void svc_sock_free(struct svc_xprt *xprt)
1524 {
1525         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1526         dprintk("svc: svc_sock_free(%p)\n", svsk);
1527
1528         if (svsk->sk_sock->file)
1529                 sockfd_put(svsk->sk_sock);
1530         else
1531                 sock_release(svsk->sk_sock);
1532         kfree(svsk);
1533 }