PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/net/sunrpc/svcsock.c

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