PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/net/net/sunrpc/svcsock.c

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