1 /*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
8 * svc_sock_enqueue procedure...
9 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/fcntl.h>
25 #include <linux/net.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/udp.h>
29 #include <linux/version.h>
30 #include <linux/unistd.h>
31 #include <linux/malloc.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <net/checksum.h>
36 #include <net/ip.h>
37 #include <asm/uaccess.h>
38
39 #include <linux/sunrpc/types.h>
40 #include <linux/sunrpc/xdr.h>
41 #include <linux/sunrpc/svcsock.h>
42 #include <linux/sunrpc/stats.h>
43
44 /* SMP locking strategy:
45 *
46 * svc_sock->sk_lock and svc_serv->sv_lock protect their
47 * respective structures.
48 *
49 * Antideadlock ordering is sk_lock --> sv_lock.
50 */
51
52 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
53
54
55 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
56 int *errp, int pmap_reg);
57 static void svc_udp_data_ready(struct sock *, int);
58 static int svc_udp_recvfrom(struct svc_rqst *);
59 static int svc_udp_sendto(struct svc_rqst *);
60
61
62 /*
63 * Queue up an idle server thread. Must have serv->sv_lock held.
64 */
65 static inline void
66 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
67 {
68 rpc_append_list(&serv->sv_threads, rqstp);
69 }
70
71 /*
72 * Dequeue an nfsd thread. Must have serv->sv_lock held.
73 */
74 static inline void
75 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
76 {
77 rpc_remove_list(&serv->sv_threads, rqstp);
78 }
79
80 /*
81 * Release an skbuff after use
82 */
83 static inline void
84 svc_release_skb(struct svc_rqst *rqstp)
85 {
86 struct sk_buff *skb = rqstp->rq_skbuff;
87
88 if (!skb)
89 return;
90 rqstp->rq_skbuff = NULL;
91
92 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
93 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
94 }
95
96 /*
97 * Queue up a socket with data pending. If there are idle nfsd
98 * processes, wake 'em up.
99 *
100 * This must be called with svsk->sk_lock held.
101 */
102 static void
103 svc_sock_enqueue(struct svc_sock *svsk)
104 {
105 struct svc_serv *serv = svsk->sk_server;
106 struct svc_rqst *rqstp;
107
108 /* NOTE: Local BH is already disabled by our caller. */
109 spin_lock(&serv->sv_lock);
110
111 if (serv->sv_threads && serv->sv_sockets)
112 printk(KERN_ERR
113 "svc_sock_enqueue: threads and sockets both waiting??\n");
114
115 if (svsk->sk_busy) {
116 /* Don't enqueue socket while daemon is receiving */
117 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
118 goto out_unlock;
119 }
120
121 /* Mark socket as busy. It will remain in this state until the
122 * server has processed all pending data and put the socket back
123 * on the idle list.
124 */
125 svsk->sk_busy = 1;
126
127 if ((rqstp = serv->sv_threads) != NULL) {
128 dprintk("svc: socket %p served by daemon %p\n",
129 svsk->sk_sk, rqstp);
130 svc_serv_dequeue(serv, rqstp);
131 if (rqstp->rq_sock)
132 printk(KERN_ERR
133 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
134 rqstp, rqstp->rq_sock);
135 rqstp->rq_sock = svsk;
136 svsk->sk_inuse++;
137 wake_up(&rqstp->rq_wait);
138 } else {
139 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
140 rpc_append_list(&serv->sv_sockets, svsk);
141 svsk->sk_qued = 1;
142 }
143
144 out_unlock:
145 spin_unlock(&serv->sv_lock);
146 }
147
148 /*
149 * Dequeue the first socket. Must be called with the serv->sv_lock held.
150 */
151 static inline struct svc_sock *
152 svc_sock_dequeue(struct svc_serv *serv)
153 {
154 struct svc_sock *svsk;
155
156 if ((svsk = serv->sv_sockets) != NULL)
157 rpc_remove_list(&serv->sv_sockets, svsk);
158
159 if (svsk) {
160 dprintk("svc: socket %p dequeued, inuse=%d\n",
161 svsk->sk_sk, svsk->sk_inuse);
162 svsk->sk_qued = 0;
163 }
164
165 return svsk;
166 }
167
168 /*
169 * Having read count bytes from a socket, check whether it
170 * needs to be re-enqueued.
171 */
172 static inline void
173 svc_sock_received(struct svc_sock *svsk, int count)
174 {
175 spin_lock_bh(&svsk->sk_lock);
176 if ((svsk->sk_data -= count) < 0) {
177 printk(KERN_NOTICE "svc: sk_data negative!\n");
178 svsk->sk_data = 0;
179 }
180 svsk->sk_rqstp = NULL; /* XXX */
181 svsk->sk_busy = 0;
182 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
183 dprintk("svc: socket %p re-enqueued after receive\n",
184 svsk->sk_sk);
185 svc_sock_enqueue(svsk);
186 }
187 spin_unlock_bh(&svsk->sk_lock);
188 }
189
190 /*
191 * Dequeue a new connection.
192 */
193 static inline void
194 svc_sock_accepted(struct svc_sock *svsk)
195 {
196 spin_lock_bh(&svsk->sk_lock);
197 svsk->sk_busy = 0;
198 svsk->sk_conn--;
199 if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {
200 dprintk("svc: socket %p re-enqueued after accept\n",
201 svsk->sk_sk);
202 svc_sock_enqueue(svsk);
203 }
204 spin_unlock_bh(&svsk->sk_lock);
205 }
206
207 /*
208 * Release a socket after use.
209 */
210 static inline void
211 svc_sock_release(struct svc_rqst *rqstp)
212 {
213 struct svc_sock *svsk = rqstp->rq_sock;
214
215 if (!svsk)
216 return;
217 svc_release_skb(rqstp);
218 rqstp->rq_sock = NULL;
219 if (!--(svsk->sk_inuse) && svsk->sk_dead) {
220 dprintk("svc: releasing dead socket\n");
221 sock_release(svsk->sk_sock);
222 kfree(svsk);
223 }
224 }
225
226 /*
227 * External function to wake up a server waiting for data
228 */
229 void
230 svc_wake_up(struct svc_serv *serv)
231 {
232 struct svc_rqst *rqstp;
233
234 spin_lock_bh(&serv->sv_lock);
235 if ((rqstp = serv->sv_threads) != NULL) {
236 dprintk("svc: daemon %p woken up.\n", rqstp);
237 /*
238 svc_serv_dequeue(serv, rqstp);
239 rqstp->rq_sock = NULL;
240 */
241 wake_up(&rqstp->rq_wait);
242 }
243 spin_unlock_bh(&serv->sv_lock);
244 }
245
246 /*
247 * Generic sendto routine
248 */
249 static int
250 svc_sendto(struct svc_rqst *rqstp, struct iovec *iov, int nr)
251 {
252 mm_segment_t oldfs;
253 struct svc_sock *svsk = rqstp->rq_sock;
254 struct socket *sock = svsk->sk_sock;
255 struct msghdr msg;
256 int i, buflen, len;
257
258 for (i = buflen = 0; i < nr; i++)
259 buflen += iov[i].iov_len;
260
261 msg.msg_name = &rqstp->rq_addr;
262 msg.msg_namelen = sizeof(rqstp->rq_addr);
263 msg.msg_iov = iov;
264 msg.msg_iovlen = nr;
265 msg.msg_control = NULL;
266 msg.msg_controllen = 0;
267
268 msg.msg_flags = MSG_DONTWAIT;
269
270 oldfs = get_fs(); set_fs(KERNEL_DS);
271 len = sock_sendmsg(sock, &msg, buflen);
272 set_fs(oldfs);
273
274 dprintk("svc: socket %p sendto([%p %Zu... ], %d, %d) = %d\n",
275 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, nr, buflen, len);
276
277 return len;
278 }
279
280 /*
281 * Check input queue length
282 */
283 static int
284 svc_recv_available(struct svc_sock *svsk)
285 {
286 mm_segment_t oldfs;
287 struct socket *sock = svsk->sk_sock;
288 int avail, err;
289
290 oldfs = get_fs(); set_fs(KERNEL_DS);
291 err = sock->ops->ioctl(sock, TIOCINQ, (unsigned long) &avail);
292 set_fs(oldfs);
293
294 return (err >= 0)? avail : err;
295 }
296
297 /*
298 * Generic recvfrom routine.
299 */
300 static int
301 svc_recvfrom(struct svc_rqst *rqstp, struct iovec *iov, int nr, int buflen)
302 {
303 mm_segment_t oldfs;
304 struct msghdr msg;
305 struct socket *sock;
306 int len, alen;
307
308 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
309 sock = rqstp->rq_sock->sk_sock;
310
311 msg.msg_name = &rqstp->rq_addr;
312 msg.msg_namelen = sizeof(rqstp->rq_addr);
313 msg.msg_iov = iov;
314 msg.msg_iovlen = nr;
315 msg.msg_control = NULL;
316 msg.msg_controllen = 0;
317
318 msg.msg_flags = MSG_DONTWAIT;
319
320 oldfs = get_fs(); set_fs(KERNEL_DS);
321 len = sock_recvmsg(sock, &msg, buflen, MSG_DONTWAIT);
322 set_fs(oldfs);
323
324 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
325 * possibly we should cache this in the svc_sock structure
326 * at accept time. FIXME
327 */
328 alen = sizeof(rqstp->rq_addr);
329 sock->ops->getname(sock, (struct sockaddr *)&rqstp->rq_addr, &alen, 1);
330
331 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
332 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
333
334 return len;
335 }
336
337 /*
338 * INET callback when data has been received on the socket.
339 */
340 static void
341 svc_udp_data_ready(struct sock *sk, int count)
342 {
343 struct svc_sock *svsk = (struct svc_sock *)(sk->user_data);
344
345 if (!svsk)
346 goto out;
347 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
348 svsk, sk, count, svsk->sk_busy);
349 spin_lock_bh(&svsk->sk_lock);
350 svsk->sk_data = 1;
351 svc_sock_enqueue(svsk);
352 spin_unlock_bh(&svsk->sk_lock);
353 out:
354 if (sk->sleep && waitqueue_active(sk->sleep))
355 wake_up_interruptible(sk->sleep);
356 }
357
358 /*
359 * Receive a datagram from a UDP socket.
360 */
361 static int
362 svc_udp_recvfrom(struct svc_rqst *rqstp)
363 {
364 struct svc_sock *svsk = rqstp->rq_sock;
365 struct svc_serv *serv = svsk->sk_server;
366 struct sk_buff *skb;
367 u32 *data;
368 int err, len;
369
370 svsk->sk_data = 0;
371 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
372 svc_sock_received(svsk, 0);
373 if (err == -EAGAIN)
374 return err;
375 /* possibly an icmp error */
376 dprintk("svc: recvfrom returned error %d\n", -err);
377 }
378
379 if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
380 unsigned int csum = skb->csum;
381 csum = csum_partial(skb->h.raw, skb->len, csum);
382 if ((unsigned short)csum_fold(csum)) {
383 skb_free_datagram(svsk->sk_sk, skb);
384 svc_sock_received(svsk, 0);
385 return 0;
386 }
387 }
388
389 /* There may be more data */
390 svsk->sk_data = 1;
391
392 len = skb->len - sizeof(struct udphdr);
393 data = (u32 *) (skb->h.raw + sizeof(struct udphdr));
394
395 rqstp->rq_skbuff = skb;
396 rqstp->rq_argbuf.base = data;
397 rqstp->rq_argbuf.buf = data;
398 rqstp->rq_argbuf.len = (len >> 2);
399 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
400 rqstp->rq_prot = IPPROTO_UDP;
401
402 /* Get sender address */
403 rqstp->rq_addr.sin_family = AF_INET;
404 rqstp->rq_addr.sin_port = skb->h.uh->source;
405 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
406
407 if (serv->sv_stats)
408 serv->sv_stats->netudpcnt++;
409
410 /* One down, maybe more to go... */
411 svsk->sk_sk->stamp = skb->stamp;
412 svc_sock_received(svsk, 0);
413
414 return len;
415 }
416
417 static int
418 svc_udp_sendto(struct svc_rqst *rqstp)
419 {
420 struct svc_buf *bufp = &rqstp->rq_resbuf;
421 int error;
422
423 /* Set up the first element of the reply iovec.
424 * Any other iovecs that may be in use have been taken
425 * care of by the server implementation itself.
426 */
427 /* bufp->base = bufp->area; */
428 bufp->iov[0].iov_base = bufp->base;
429 bufp->iov[0].iov_len = bufp->len << 2;
430
431 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
432 if (error == -ECONNREFUSED)
433 /* ICMP error on earlier request. */
434 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
435 else if (error == -EAGAIN)
436 /* Ignore and wait for re-xmit */
437 error = 0;
438
439 return error;
440 }
441
442 static int
443 svc_udp_init(struct svc_sock *svsk)
444 {
445 svsk->sk_sk->data_ready = svc_udp_data_ready;
446 svsk->sk_recvfrom = svc_udp_recvfrom;
447 svsk->sk_sendto = svc_udp_sendto;
448
449 return 0;
450 }
451
452 /*
453 * A state change on a listening socket means there's a connection
454 * pending.
455 */
456 static void
457 svc_tcp_state_change1(struct sock *sk)
458 {
459 struct svc_sock *svsk;
460
461 dprintk("svc: socket %p TCP (listen) state change %d\n",
462 sk, sk->state);
463
464 if (sk->state != TCP_ESTABLISHED) {
465 /* Aborted connection, SYN_RECV or whatever... */
466 goto out;
467 }
468 if (!(svsk = (struct svc_sock *) sk->user_data)) {
469 printk("svc: socket %p: no user data\n", sk);
470 goto out;
471 }
472 spin_lock_bh(&svsk->sk_lock);
473 svsk->sk_conn++;
474 svc_sock_enqueue(svsk);
475 spin_unlock_bh(&svsk->sk_lock);
476 out:
477 if (sk->sleep && waitqueue_active(sk->sleep))
478 wake_up_interruptible_all(sk->sleep);
479 }
480
481 /*
482 * A state change on a connected socket means it's dying or dead.
483 */
484 static void
485 svc_tcp_state_change2(struct sock *sk)
486 {
487 struct svc_sock *svsk;
488
489 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
490 sk, sk->state, sk->user_data);
491
492 if (!(svsk = (struct svc_sock *) sk->user_data)) {
493 printk("svc: socket %p: no user data\n", sk);
494 goto out;
495 }
496 spin_lock_bh(&svsk->sk_lock);
497 svsk->sk_close = 1;
498 svc_sock_enqueue(svsk);
499 spin_unlock_bh(&svsk->sk_lock);
500 out:
501 if (sk->sleep && waitqueue_active(sk->sleep))
502 wake_up_interruptible_all(sk->sleep);
503 }
504
505 static void
506 svc_tcp_data_ready(struct sock *sk, int count)
507 {
508 struct svc_sock * svsk;
509
510 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
511 sk, sk->user_data);
512 if (!(svsk = (struct svc_sock *)(sk->user_data)))
513 goto out;
514 spin_lock_bh(&svsk->sk_lock);
515 svsk->sk_data++;
516 svc_sock_enqueue(svsk);
517 spin_unlock_bh(&svsk->sk_lock);
518 out:
519 if (sk->sleep && waitqueue_active(sk->sleep))
520 wake_up_interruptible(sk->sleep);
521 }
522
523 /*
524 * Accept a TCP connection
525 */
526 static void
527 svc_tcp_accept(struct svc_sock *svsk)
528 {
529 struct sockaddr_in sin;
530 struct svc_serv *serv = svsk->sk_server;
531 struct socket *sock = svsk->sk_sock;
532 struct socket *newsock;
533 struct proto_ops *ops;
534 struct svc_sock *newsvsk;
535 int err, slen;
536
537 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
538 if (!sock)
539 return;
540
541 if (!(newsock = sock_alloc())) {
542 printk(KERN_WARNING "%s: no more sockets!\n", serv->sv_name);
543 return;
544 }
545 dprintk("svc: tcp_accept %p allocated\n", newsock);
546
547 newsock->type = sock->type;
548 newsock->ops = ops = sock->ops;
549
550 if ((err = ops->accept(sock, newsock, O_NONBLOCK)) < 0) {
551 if (net_ratelimit())
552 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
553 serv->sv_name, -err);
554 goto failed; /* aborted connection or whatever */
555 }
556
557 slen = sizeof(sin);
558 err = ops->getname(newsock, (struct sockaddr *) &sin, &slen, 1);
559 if (err < 0) {
560 if (net_ratelimit())
561 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
562 serv->sv_name, -err);
563 goto failed; /* aborted connection or whatever */
564 }
565
566 /* Ideally, we would want to reject connections from unauthorized
567 * hosts here, but when we get encription, the IP of the host won't
568 * tell us anything. For now just warn about unpriv connections.
569 */
570 if (ntohs(sin.sin_port) >= 1024) {
571 if (net_ratelimit())
572 printk(KERN_WARNING
573 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
574 serv->sv_name,
575 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
576 }
577
578 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
579 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
580
581 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
582 goto failed;
583
584 /* Precharge. Data may have arrived on the socket before we
585 * installed the data_ready callback.
586 */
587 spin_lock_bh(&newsvsk->sk_lock);
588 newsvsk->sk_data = 1;
589 newsvsk->sk_temp = 1;
590 svc_sock_enqueue(newsvsk);
591 spin_unlock_bh(&newsvsk->sk_lock);
592
593 if (serv->sv_stats)
594 serv->sv_stats->nettcpconn++;
595
596 return;
597
598 failed:
599 sock_release(newsock);
600 return;
601 }
602
603 /*
604 * Receive data from a TCP socket.
605 */
606 static int
607 svc_tcp_recvfrom(struct svc_rqst *rqstp)
608 {
609 struct svc_sock *svsk = rqstp->rq_sock;
610 struct svc_serv *serv = svsk->sk_server;
611 struct svc_buf *bufp = &rqstp->rq_argbuf;
612 int len, ready, used;
613
614 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
615 svsk, svsk->sk_data, svsk->sk_conn, svsk->sk_close);
616
617 if (svsk->sk_close) {
618 svc_delete_socket(svsk);
619 return 0;
620 }
621
622 if (svsk->sk_conn) {
623 svc_tcp_accept(svsk);
624 svc_sock_accepted(svsk);
625 return 0;
626 }
627
628 ready = svsk->sk_data;
629
630 /* Receive data. If we haven't got the record length yet, get
631 * the next four bytes. Otherwise try to gobble up as much as
632 * possible up to the complete record length.
633 */
634 if (svsk->sk_tcplen < 4) {
635 unsigned long want = 4 - svsk->sk_tcplen;
636 struct iovec iov;
637
638 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
639 iov.iov_len = want;
640 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
641 goto error;
642 svsk->sk_tcplen += len;
643
644 svsk->sk_reclen = ntohl(svsk->sk_reclen);
645 if (!(svsk->sk_reclen & 0x80000000)) {
646 /* FIXME: technically, a record can be fragmented,
647 * and non-terminal fragments will not have the top
648 * bit set in the fragment length header.
649 * But apparently no known nfs clients send fragmented
650 * records. */
651 /* FIXME: shutdown socket */
652 printk(KERN_NOTICE "RPC: bad TCP reclen %08lx",
653 (unsigned long) svsk->sk_reclen);
654 return -EIO;
655 }
656 svsk->sk_reclen &= 0x7fffffff;
657 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
658 }
659
660 /* Check whether enough data is available */
661 len = svc_recv_available(svsk);
662 if (len < 0)
663 goto error;
664
665 if (len < svsk->sk_reclen) {
666 /* FIXME: if sk_reclen > window-size, then we will
667 * never be able to receive the record, so should
668 * shutdown the connection
669 */
670 dprintk("svc: incomplete TCP record (%d of %d)\n",
671 len, svsk->sk_reclen);
672 svc_sock_received(svsk, ready);
673 return -EAGAIN; /* record not complete */
674 }
675 /* if we think there is only one more record to read, but
676 * it is bigger than we expect, then two records must have arrived
677 * together, so pretend we aren't using the record.. */
678 if (len > svsk->sk_reclen && ready == 1)
679 used = 0;
680 else used = 1;
681
682 /* Frob argbuf */
683 bufp->iov[0].iov_base += 4;
684 bufp->iov[0].iov_len -= 4;
685
686 /* Now receive data */
687 len = svc_recvfrom(rqstp, bufp->iov, bufp->nriov, svsk->sk_reclen);
688 if (len < 0)
689 goto error;
690
691 dprintk("svc: TCP complete record (%d bytes)\n", len);
692
693 /* Position reply write pointer immediately after
694 * record length */
695 rqstp->rq_resbuf.buf += 1;
696 rqstp->rq_resbuf.len = 1;
697
698 rqstp->rq_skbuff = 0;
699 rqstp->rq_argbuf.buf += 1;
700 rqstp->rq_argbuf.len = (len >> 2);
701 rqstp->rq_prot = IPPROTO_TCP;
702
703 /* Reset TCP read info */
704 svsk->sk_reclen = 0;
705 svsk->sk_tcplen = 0;
706
707 svc_sock_received(svsk, used);
708 if (serv->sv_stats)
709 serv->sv_stats->nettcpcnt++;
710
711 return len;
712
713 error:
714 if (len == -EAGAIN) {
715 dprintk("RPC: TCP recvfrom got EAGAIN\n");
716 svc_sock_received(svsk, ready); /* Clear data ready */
717 } else {
718 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
719 svsk->sk_server->sv_name, -len);
720 svc_sock_received(svsk, 0);
721 }
722
723 return len;
724 }
725
726 /*
727 * Send out data on TCP socket.
728 * FIXME: Make the sendto call non-blocking in order not to hang
729 * a daemon on a dead client. Requires write queue maintenance.
730 */
731 static int
732 svc_tcp_sendto(struct svc_rqst *rqstp)
733 {
734 struct svc_buf *bufp = &rqstp->rq_resbuf;
735 int sent;
736
737 /* Set up the first element of the reply iovec.
738 * Any other iovecs that may be in use have been taken
739 * care of by the server implementation itself.
740 */
741 bufp->iov[0].iov_base = bufp->base;
742 bufp->iov[0].iov_len = bufp->len << 2;
743 bufp->base[0] = htonl(0x80000000|((bufp->len << 2) - 4));
744
745 sent = svc_sendto(rqstp, bufp->iov, bufp->nriov);
746 if (sent != bufp->len<<2) {
747 printk(KERN_NOTICE "rpc-srv/tcp: %s: sent only %d bytes of %d - should shutdown socket\n",
748 rqstp->rq_sock->sk_server->sv_name,
749 sent, bufp->len << 2);
750 /* FIXME: should shutdown the socket, or allocate more memort
751 * or wait and try again or something. Otherwise
752 * client will get confused
753 */
754 }
755 return sent;
756 }
757
758 static int
759 svc_tcp_init(struct svc_sock *svsk)
760 {
761 struct sock *sk = svsk->sk_sk;
762
763 svsk->sk_recvfrom = svc_tcp_recvfrom;
764 svsk->sk_sendto = svc_tcp_sendto;
765
766 if (sk->state == TCP_LISTEN) {
767 dprintk("setting up TCP socket for listening\n");
768 sk->state_change = svc_tcp_state_change1;
769 } else {
770 dprintk("setting up TCP socket for reading\n");
771 sk->state_change = svc_tcp_state_change2;
772 sk->data_ready = svc_tcp_data_ready;
773
774 svsk->sk_reclen = 0;
775 svsk->sk_tcplen = 0;
776 }
777
778 return 0;
779 }
780
781 /*
782 * Receive the next request on any socket.
783 */
784 int
785 svc_recv(struct svc_serv *serv, struct svc_rqst *rqstp, long timeout)
786 {
787 struct svc_sock *svsk;
788 int len;
789 DECLARE_WAITQUEUE(wait, current);
790
791 dprintk("svc: server %p waiting for data (to = %ld)\n",
792 rqstp, timeout);
793
794 if (rqstp->rq_sock)
795 printk(KERN_ERR
796 "svc_recv: service %p, socket not NULL!\n",
797 rqstp);
798 if (waitqueue_active(&rqstp->rq_wait))
799 printk(KERN_ERR
800 "svc_recv: service %p, wait queue active!\n",
801 rqstp);
802
803 /* Initialize the buffers */
804 rqstp->rq_argbuf = rqstp->rq_defbuf;
805 rqstp->rq_resbuf = rqstp->rq_defbuf;
806
807 if (signalled())
808 return -EINTR;
809
810 spin_lock_bh(&serv->sv_lock);
811 if ((svsk = svc_sock_dequeue(serv)) != NULL) {
812 rqstp->rq_sock = svsk;
813 svsk->sk_inuse++;
814 } else {
815 /* No data pending. Go to sleep */
816 svc_serv_enqueue(serv, rqstp);
817
818 /*
819 * We have to be able to interrupt this wait
820 * to bring down the daemons ...
821 */
822 set_current_state(TASK_INTERRUPTIBLE);
823 add_wait_queue(&rqstp->rq_wait, &wait);
824 spin_unlock_bh(&serv->sv_lock);
825
826 schedule_timeout(timeout);
827
828 spin_lock_bh(&serv->sv_lock);
829 remove_wait_queue(&rqstp->rq_wait, &wait);
830
831 if (!(svsk = rqstp->rq_sock)) {
832 svc_serv_dequeue(serv, rqstp);
833 spin_unlock_bh(&serv->sv_lock);
834 dprintk("svc: server %p, no data yet\n", rqstp);
835 return signalled()? -EINTR : -EAGAIN;
836 }
837 }
838 spin_unlock_bh(&serv->sv_lock);
839
840 dprintk("svc: server %p, socket %p, inuse=%d\n",
841 rqstp, svsk, svsk->sk_inuse);
842 len = svsk->sk_recvfrom(rqstp);
843 dprintk("svc: got len=%d\n", len);
844
845 /* No data, incomplete (TCP) read, or accept() */
846 if (len == 0 || len == -EAGAIN) {
847 svc_sock_release(rqstp);
848 return -EAGAIN;
849 }
850
851 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
852 rqstp->rq_userset = 0;
853 rqstp->rq_verfed = 0;
854
855 svc_getlong(&rqstp->rq_argbuf, rqstp->rq_xid);
856 svc_putlong(&rqstp->rq_resbuf, rqstp->rq_xid);
857
858 /* Assume that the reply consists of a single buffer. */
859 rqstp->rq_resbuf.nriov = 1;
860
861 if (serv->sv_stats)
862 serv->sv_stats->netcnt++;
863 return len;
864 }
865
866 /*
867 * Drop request
868 */
869 void
870 svc_drop(struct svc_rqst *rqstp)
871 {
872 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
873 svc_sock_release(rqstp);
874 }
875
876 /*
877 * Return reply to client.
878 */
879 int
880 svc_send(struct svc_rqst *rqstp)
881 {
882 struct svc_sock *svsk;
883 int len;
884
885 if ((svsk = rqstp->rq_sock) == NULL) {
886 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
887 __FILE__, __LINE__);
888 return -EFAULT;
889 }
890
891 /* release the receive skb before sending the reply */
892 svc_release_skb(rqstp);
893
894 len = svsk->sk_sendto(rqstp);
895 svc_sock_release(rqstp);
896
897 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
898 return 0;
899 return len;
900 }
901
902 /*
903 * Initialize socket for RPC use and create svc_sock struct
904 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
905 */
906 static struct svc_sock *
907 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
908 int *errp, int pmap_register)
909 {
910 struct svc_sock *svsk;
911 struct sock *inet;
912
913 dprintk("svc: svc_setup_socket %p\n", sock);
914 if (!(svsk = kmalloc(sizeof(*svsk), GFP_KERNEL))) {
915 *errp = -ENOMEM;
916 return NULL;
917 }
918 memset(svsk, 0, sizeof(*svsk));
919
920 inet = sock->sk;
921 inet->user_data = svsk;
922 svsk->sk_sock = sock;
923 svsk->sk_sk = inet;
924 svsk->sk_ostate = inet->state_change;
925 svsk->sk_odata = inet->data_ready;
926 svsk->sk_server = serv;
927 spin_lock_init(&svsk->sk_lock);
928
929 /* Initialize the socket */
930 if (sock->type == SOCK_DGRAM)
931 *errp = svc_udp_init(svsk);
932 else
933 *errp = svc_tcp_init(svsk);
934 if (svsk->sk_sk == NULL)
935 printk(KERN_WARNING "svsk->sk_sk == NULL after svc_prot_init!\n");
936
937 /* Register socket with portmapper */
938 if (*errp >= 0 && pmap_register)
939 *errp = svc_register(serv, inet->protocol, ntohs(inet->sport));
940
941 if (*errp < 0) {
942 inet->user_data = NULL;
943 kfree(svsk);
944 return NULL;
945 }
946
947 spin_lock_bh(&serv->sv_lock);
948 svsk->sk_list = serv->sv_allsocks;
949 serv->sv_allsocks = svsk;
950 spin_unlock_bh(&serv->sv_lock);
951
952 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
953 svsk, svsk->sk_sk);
954 return svsk;
955 }
956
957 /*
958 * Create socket for RPC service.
959 */
960 static int
961 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
962 {
963 struct svc_sock *svsk;
964 struct socket *sock;
965 int error;
966 int type;
967
968 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
969 serv->sv_program->pg_name, protocol,
970 NIPQUAD(sin->sin_addr.s_addr),
971 ntohs(sin->sin_port));
972
973 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
974 printk(KERN_WARNING "svc: only UDP and TCP "
975 "sockets supported\n");
976 return -EINVAL;
977 }
978 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
979
980 if ((error = sock_create(PF_INET, type, protocol, &sock)) < 0)
981 return error;
982
983 if (sin != NULL) {
984 error = sock->ops->bind(sock, (struct sockaddr *) sin,
985 sizeof(*sin));
986 if (error < 0)
987 goto bummer;
988 }
989
990 if (protocol == IPPROTO_TCP) {
991 if ((error = sock->ops->listen(sock, 5)) < 0)
992 goto bummer;
993 }
994
995 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
996 return 0;
997
998 bummer:
999 dprintk("svc: svc_create_socket error = %d\n", -error);
1000 sock_release(sock);
1001 return error;
1002 }
1003
1004 /*
1005 * Remove a dead socket
1006 */
1007 void
1008 svc_delete_socket(struct svc_sock *svsk)
1009 {
1010 struct svc_sock **rsk;
1011 struct svc_serv *serv;
1012 struct sock *sk;
1013
1014 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1015
1016 serv = svsk->sk_server;
1017 sk = svsk->sk_sk;
1018
1019 sk->state_change = svsk->sk_ostate;
1020 sk->data_ready = svsk->sk_odata;
1021
1022 spin_lock_bh(&serv->sv_lock);
1023
1024 for (rsk = &serv->sv_allsocks; *rsk; rsk = &(*rsk)->sk_list) {
1025 if (*rsk == svsk)
1026 break;
1027 }
1028 if (!*rsk) {
1029 spin_unlock_bh(&serv->sv_lock);
1030 return;
1031 }
1032 *rsk = svsk->sk_list;
1033 if (svsk->sk_qued)
1034 rpc_remove_list(&serv->sv_sockets, svsk);
1035
1036 spin_unlock_bh(&serv->sv_lock);
1037
1038 svsk->sk_dead = 1;
1039
1040 if (!svsk->sk_inuse) {
1041 sock_release(svsk->sk_sock);
1042 kfree(svsk);
1043 } else {
1044 printk(KERN_NOTICE "svc: server socket destroy delayed\n");
1045 /* svsk->sk_server = NULL; */
1046 }
1047 }
1048
1049 /*
1050 * Make a socket for nfsd and lockd
1051 */
1052 int
1053 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1054 {
1055 struct sockaddr_in sin;
1056
1057 dprintk("svc: creating socket proto = %d\n", protocol);
1058 sin.sin_family = AF_INET;
1059 sin.sin_addr.s_addr = INADDR_ANY;
1060 sin.sin_port = htons(port);
1061 return svc_create_socket(serv, protocol, &sin);
1062 }
1063
1064
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.