PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/net/sunrpc/svcsock.c

https://bitbucket.org/abdulhadif/android_kernel_samsung_espresso10
C | 1663 lines | 1228 code | 212 blank | 223 comment | 192 complexity | a31603b319d9683f69d6971a87a64566 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. static int svc_partial_recvfrom(struct svc_rqst *rqstp,
  344. struct kvec *iov, int nr,
  345. int buflen, unsigned int base)
  346. {
  347. size_t save_iovlen;
  348. void __user *save_iovbase;
  349. unsigned int i;
  350. int ret;
  351. if (base == 0)
  352. return svc_recvfrom(rqstp, iov, nr, buflen);
  353. for (i = 0; i < nr; i++) {
  354. if (iov[i].iov_len > base)
  355. break;
  356. base -= iov[i].iov_len;
  357. }
  358. save_iovlen = iov[i].iov_len;
  359. save_iovbase = iov[i].iov_base;
  360. iov[i].iov_len -= base;
  361. iov[i].iov_base += base;
  362. ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
  363. iov[i].iov_len = save_iovlen;
  364. iov[i].iov_base = save_iovbase;
  365. return ret;
  366. }
  367. /*
  368. * Set socket snd and rcv buffer lengths
  369. */
  370. static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
  371. unsigned int rcv)
  372. {
  373. #if 0
  374. mm_segment_t oldfs;
  375. oldfs = get_fs(); set_fs(KERNEL_DS);
  376. sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
  377. (char*)&snd, sizeof(snd));
  378. sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  379. (char*)&rcv, sizeof(rcv));
  380. #else
  381. /* sock_setsockopt limits use to sysctl_?mem_max,
  382. * which isn't acceptable. Until that is made conditional
  383. * on not having CAP_SYS_RESOURCE or similar, we go direct...
  384. * DaveM said I could!
  385. */
  386. lock_sock(sock->sk);
  387. sock->sk->sk_sndbuf = snd * 2;
  388. sock->sk->sk_rcvbuf = rcv * 2;
  389. sock->sk->sk_write_space(sock->sk);
  390. release_sock(sock->sk);
  391. #endif
  392. }
  393. /*
  394. * INET callback when data has been received on the socket.
  395. */
  396. static void svc_udp_data_ready(struct sock *sk, int count)
  397. {
  398. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  399. wait_queue_head_t *wq = sk_sleep(sk);
  400. if (svsk) {
  401. dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
  402. svsk, sk, count,
  403. test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
  404. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  405. svc_xprt_enqueue(&svsk->sk_xprt);
  406. }
  407. if (wq && waitqueue_active(wq))
  408. wake_up_interruptible(wq);
  409. }
  410. /*
  411. * INET callback when space is newly available on the socket.
  412. */
  413. static void svc_write_space(struct sock *sk)
  414. {
  415. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  416. wait_queue_head_t *wq = sk_sleep(sk);
  417. if (svsk) {
  418. dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
  419. svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
  420. svc_xprt_enqueue(&svsk->sk_xprt);
  421. }
  422. if (wq && waitqueue_active(wq)) {
  423. dprintk("RPC svc_write_space: someone sleeping on %p\n",
  424. svsk);
  425. wake_up_interruptible(wq);
  426. }
  427. }
  428. static void svc_tcp_write_space(struct sock *sk)
  429. {
  430. struct socket *sock = sk->sk_socket;
  431. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
  432. clear_bit(SOCK_NOSPACE, &sock->flags);
  433. svc_write_space(sk);
  434. }
  435. /*
  436. * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
  437. */
  438. static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
  439. struct cmsghdr *cmh)
  440. {
  441. struct in_pktinfo *pki = CMSG_DATA(cmh);
  442. if (cmh->cmsg_type != IP_PKTINFO)
  443. return 0;
  444. rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
  445. return 1;
  446. }
  447. /*
  448. * See net/ipv6/datagram.c : datagram_recv_ctl
  449. */
  450. static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
  451. struct cmsghdr *cmh)
  452. {
  453. struct in6_pktinfo *pki = CMSG_DATA(cmh);
  454. if (cmh->cmsg_type != IPV6_PKTINFO)
  455. return 0;
  456. ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
  457. return 1;
  458. }
  459. /*
  460. * Copy the UDP datagram's destination address to the rqstp structure.
  461. * The 'destination' address in this case is the address to which the
  462. * peer sent the datagram, i.e. our local address. For multihomed
  463. * hosts, this can change from msg to msg. Note that only the IP
  464. * address changes, the port number should remain the same.
  465. */
  466. static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
  467. struct cmsghdr *cmh)
  468. {
  469. switch (cmh->cmsg_level) {
  470. case SOL_IP:
  471. return svc_udp_get_dest_address4(rqstp, cmh);
  472. case SOL_IPV6:
  473. return svc_udp_get_dest_address6(rqstp, cmh);
  474. }
  475. return 0;
  476. }
  477. /*
  478. * Receive a datagram from a UDP socket.
  479. */
  480. static int svc_udp_recvfrom(struct svc_rqst *rqstp)
  481. {
  482. struct svc_sock *svsk =
  483. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  484. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  485. struct sk_buff *skb;
  486. union {
  487. struct cmsghdr hdr;
  488. long all[SVC_PKTINFO_SPACE / sizeof(long)];
  489. } buffer;
  490. struct cmsghdr *cmh = &buffer.hdr;
  491. struct msghdr msg = {
  492. .msg_name = svc_addr(rqstp),
  493. .msg_control = cmh,
  494. .msg_controllen = sizeof(buffer),
  495. .msg_flags = MSG_DONTWAIT,
  496. };
  497. size_t len;
  498. int err;
  499. if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
  500. /* udp sockets need large rcvbuf as all pending
  501. * requests are still in that buffer. sndbuf must
  502. * also be large enough that there is enough space
  503. * for one reply per thread. We count all threads
  504. * rather than threads in a particular pool, which
  505. * provides an upper bound on the number of threads
  506. * which will access the socket.
  507. */
  508. svc_sock_setbufsize(svsk->sk_sock,
  509. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  510. (serv->sv_nrthreads+3) * serv->sv_max_mesg);
  511. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  512. skb = NULL;
  513. err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
  514. 0, 0, MSG_PEEK | MSG_DONTWAIT);
  515. if (err >= 0)
  516. skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
  517. if (skb == NULL) {
  518. if (err != -EAGAIN) {
  519. /* possibly an icmp error */
  520. dprintk("svc: recvfrom returned error %d\n", -err);
  521. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  522. }
  523. return -EAGAIN;
  524. }
  525. len = svc_addr_len(svc_addr(rqstp));
  526. if (len == 0)
  527. return -EAFNOSUPPORT;
  528. rqstp->rq_addrlen = len;
  529. if (skb->tstamp.tv64 == 0) {
  530. skb->tstamp = ktime_get_real();
  531. /* Don't enable netstamp, sunrpc doesn't
  532. need that much accuracy */
  533. }
  534. svsk->sk_sk->sk_stamp = skb->tstamp;
  535. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
  536. len = skb->len - sizeof(struct udphdr);
  537. rqstp->rq_arg.len = len;
  538. rqstp->rq_prot = IPPROTO_UDP;
  539. if (!svc_udp_get_dest_address(rqstp, cmh)) {
  540. if (net_ratelimit())
  541. printk(KERN_WARNING
  542. "svc: received unknown control message %d/%d; "
  543. "dropping RPC reply datagram\n",
  544. cmh->cmsg_level, cmh->cmsg_type);
  545. skb_free_datagram_locked(svsk->sk_sk, skb);
  546. return 0;
  547. }
  548. if (skb_is_nonlinear(skb)) {
  549. /* we have to copy */
  550. local_bh_disable();
  551. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
  552. local_bh_enable();
  553. /* checksum error */
  554. skb_free_datagram_locked(svsk->sk_sk, skb);
  555. return 0;
  556. }
  557. local_bh_enable();
  558. skb_free_datagram_locked(svsk->sk_sk, skb);
  559. } else {
  560. /* we can use it in-place */
  561. rqstp->rq_arg.head[0].iov_base = skb->data +
  562. sizeof(struct udphdr);
  563. rqstp->rq_arg.head[0].iov_len = len;
  564. if (skb_checksum_complete(skb)) {
  565. skb_free_datagram_locked(svsk->sk_sk, skb);
  566. return 0;
  567. }
  568. rqstp->rq_xprt_ctxt = skb;
  569. }
  570. rqstp->rq_arg.page_base = 0;
  571. if (len <= rqstp->rq_arg.head[0].iov_len) {
  572. rqstp->rq_arg.head[0].iov_len = len;
  573. rqstp->rq_arg.page_len = 0;
  574. rqstp->rq_respages = rqstp->rq_pages+1;
  575. } else {
  576. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  577. rqstp->rq_respages = rqstp->rq_pages + 1 +
  578. DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
  579. }
  580. if (serv->sv_stats)
  581. serv->sv_stats->netudpcnt++;
  582. return len;
  583. }
  584. static int
  585. svc_udp_sendto(struct svc_rqst *rqstp)
  586. {
  587. int error;
  588. error = svc_sendto(rqstp, &rqstp->rq_res);
  589. if (error == -ECONNREFUSED)
  590. /* ICMP error on earlier request. */
  591. error = svc_sendto(rqstp, &rqstp->rq_res);
  592. return error;
  593. }
  594. static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
  595. {
  596. }
  597. static int svc_udp_has_wspace(struct svc_xprt *xprt)
  598. {
  599. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  600. struct svc_serv *serv = xprt->xpt_server;
  601. unsigned long required;
  602. /*
  603. * Set the SOCK_NOSPACE flag before checking the available
  604. * sock space.
  605. */
  606. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  607. required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
  608. if (required*2 > sock_wspace(svsk->sk_sk))
  609. return 0;
  610. clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  611. return 1;
  612. }
  613. static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
  614. {
  615. BUG();
  616. return NULL;
  617. }
  618. static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
  619. struct net *net,
  620. struct sockaddr *sa, int salen,
  621. int flags)
  622. {
  623. return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
  624. }
  625. static struct svc_xprt_ops svc_udp_ops = {
  626. .xpo_create = svc_udp_create,
  627. .xpo_recvfrom = svc_udp_recvfrom,
  628. .xpo_sendto = svc_udp_sendto,
  629. .xpo_release_rqst = svc_release_skb,
  630. .xpo_detach = svc_sock_detach,
  631. .xpo_free = svc_sock_free,
  632. .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
  633. .xpo_has_wspace = svc_udp_has_wspace,
  634. .xpo_accept = svc_udp_accept,
  635. };
  636. static struct svc_xprt_class svc_udp_class = {
  637. .xcl_name = "udp",
  638. .xcl_owner = THIS_MODULE,
  639. .xcl_ops = &svc_udp_ops,
  640. .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
  641. };
  642. static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
  643. {
  644. int err, level, optname, one = 1;
  645. svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
  646. clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
  647. svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
  648. svsk->sk_sk->sk_write_space = svc_write_space;
  649. /* initialise setting must have enough space to
  650. * receive and respond to one request.
  651. * svc_udp_recvfrom will re-adjust if necessary
  652. */
  653. svc_sock_setbufsize(svsk->sk_sock,
  654. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
  655. 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
  656. /* data might have come in before data_ready set up */
  657. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  658. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  659. /* make sure we get destination address info */
  660. switch (svsk->sk_sk->sk_family) {
  661. case AF_INET:
  662. level = SOL_IP;
  663. optname = IP_PKTINFO;
  664. break;
  665. case AF_INET6:
  666. level = SOL_IPV6;
  667. optname = IPV6_RECVPKTINFO;
  668. break;
  669. default:
  670. BUG();
  671. }
  672. err = kernel_setsockopt(svsk->sk_sock, level, optname,
  673. (char *)&one, sizeof(one));
  674. dprintk("svc: kernel_setsockopt returned %d\n", err);
  675. }
  676. /*
  677. * A data_ready event on a listening socket means there's a connection
  678. * pending. Do not use state_change as a substitute for it.
  679. */
  680. static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
  681. {
  682. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  683. wait_queue_head_t *wq;
  684. dprintk("svc: socket %p TCP (listen) state change %d\n",
  685. sk, sk->sk_state);
  686. /*
  687. * This callback may called twice when a new connection
  688. * is established as a child socket inherits everything
  689. * from a parent LISTEN socket.
  690. * 1) data_ready method of the parent socket will be called
  691. * when one of child sockets become ESTABLISHED.
  692. * 2) data_ready method of the child socket may be called
  693. * when it receives data before the socket is accepted.
  694. * In case of 2, we should ignore it silently.
  695. */
  696. if (sk->sk_state == TCP_LISTEN) {
  697. if (svsk) {
  698. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  699. svc_xprt_enqueue(&svsk->sk_xprt);
  700. } else
  701. printk("svc: socket %p: no user data\n", sk);
  702. }
  703. wq = sk_sleep(sk);
  704. if (wq && waitqueue_active(wq))
  705. wake_up_interruptible_all(wq);
  706. }
  707. /*
  708. * A state change on a connected socket means it's dying or dead.
  709. */
  710. static void svc_tcp_state_change(struct sock *sk)
  711. {
  712. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  713. wait_queue_head_t *wq = sk_sleep(sk);
  714. dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
  715. sk, sk->sk_state, sk->sk_user_data);
  716. if (!svsk)
  717. printk("svc: socket %p: no user data\n", sk);
  718. else {
  719. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  720. svc_xprt_enqueue(&svsk->sk_xprt);
  721. }
  722. if (wq && waitqueue_active(wq))
  723. wake_up_interruptible_all(wq);
  724. }
  725. static void svc_tcp_data_ready(struct sock *sk, int count)
  726. {
  727. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  728. wait_queue_head_t *wq = sk_sleep(sk);
  729. dprintk("svc: socket %p TCP data ready (svsk %p)\n",
  730. sk, sk->sk_user_data);
  731. if (svsk) {
  732. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  733. svc_xprt_enqueue(&svsk->sk_xprt);
  734. }
  735. if (wq && waitqueue_active(wq))
  736. wake_up_interruptible(wq);
  737. }
  738. /*
  739. * Accept a TCP connection
  740. */
  741. static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
  742. {
  743. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  744. struct sockaddr_storage addr;
  745. struct sockaddr *sin = (struct sockaddr *) &addr;
  746. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  747. struct socket *sock = svsk->sk_sock;
  748. struct socket *newsock;
  749. struct svc_sock *newsvsk;
  750. int err, slen;
  751. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  752. dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
  753. if (!sock)
  754. return NULL;
  755. clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  756. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  757. if (err < 0) {
  758. if (err == -ENOMEM)
  759. printk(KERN_WARNING "%s: no more sockets!\n",
  760. serv->sv_name);
  761. else if (err != -EAGAIN && net_ratelimit())
  762. printk(KERN_WARNING "%s: accept failed (err %d)!\n",
  763. serv->sv_name, -err);
  764. return NULL;
  765. }
  766. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  767. err = kernel_getpeername(newsock, sin, &slen);
  768. if (err < 0) {
  769. if (net_ratelimit())
  770. printk(KERN_WARNING "%s: peername failed (err %d)!\n",
  771. serv->sv_name, -err);
  772. goto failed; /* aborted connection or whatever */
  773. }
  774. /* Ideally, we would want to reject connections from unauthorized
  775. * hosts here, but when we get encryption, the IP of the host won't
  776. * tell us anything. For now just warn about unpriv connections.
  777. */
  778. if (!svc_port_is_privileged(sin)) {
  779. dprintk(KERN_WARNING
  780. "%s: connect from unprivileged port: %s\n",
  781. serv->sv_name,
  782. __svc_print_addr(sin, buf, sizeof(buf)));
  783. }
  784. dprintk("%s: connect from %s\n", serv->sv_name,
  785. __svc_print_addr(sin, buf, sizeof(buf)));
  786. /* make sure that a write doesn't block forever when
  787. * low on memory
  788. */
  789. newsock->sk->sk_sndtimeo = HZ*30;
  790. if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
  791. (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
  792. goto failed;
  793. svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
  794. err = kernel_getsockname(newsock, sin, &slen);
  795. if (unlikely(err < 0)) {
  796. dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
  797. slen = offsetof(struct sockaddr, sa_data);
  798. }
  799. svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
  800. if (serv->sv_stats)
  801. serv->sv_stats->nettcpconn++;
  802. return &newsvsk->sk_xprt;
  803. failed:
  804. sock_release(newsock);
  805. return NULL;
  806. }
  807. static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
  808. {
  809. unsigned int i, len, npages;
  810. if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
  811. return 0;
  812. len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
  813. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  814. for (i = 0; i < npages; i++) {
  815. if (rqstp->rq_pages[i] != NULL)
  816. put_page(rqstp->rq_pages[i]);
  817. BUG_ON(svsk->sk_pages[i] == NULL);
  818. rqstp->rq_pages[i] = svsk->sk_pages[i];
  819. svsk->sk_pages[i] = NULL;
  820. }
  821. rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
  822. return len;
  823. }
  824. static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
  825. {
  826. unsigned int i, len, npages;
  827. if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
  828. return;
  829. len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
  830. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  831. for (i = 0; i < npages; i++) {
  832. svsk->sk_pages[i] = rqstp->rq_pages[i];
  833. rqstp->rq_pages[i] = NULL;
  834. }
  835. }
  836. static void svc_tcp_clear_pages(struct svc_sock *svsk)
  837. {
  838. unsigned int i, len, npages;
  839. if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
  840. goto out;
  841. len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
  842. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  843. for (i = 0; i < npages; i++) {
  844. BUG_ON(svsk->sk_pages[i] == NULL);
  845. put_page(svsk->sk_pages[i]);
  846. svsk->sk_pages[i] = NULL;
  847. }
  848. out:
  849. svsk->sk_tcplen = 0;
  850. }
  851. /*
  852. * Receive data.
  853. * If we haven't gotten the record length yet, get the next four bytes.
  854. * Otherwise try to gobble up as much as possible up to the complete
  855. * record length.
  856. */
  857. static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
  858. {
  859. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  860. unsigned int want;
  861. int len;
  862. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  863. if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
  864. struct kvec iov;
  865. want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
  866. iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
  867. iov.iov_len = want;
  868. if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
  869. goto error;
  870. svsk->sk_tcplen += len;
  871. if (len < want) {
  872. dprintk("svc: short recvfrom while reading record "
  873. "length (%d of %d)\n", len, want);
  874. return -EAGAIN;
  875. }
  876. svsk->sk_reclen = ntohl(svsk->sk_reclen);
  877. if (!(svsk->sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
  878. /* FIXME: technically, a record can be fragmented,
  879. * and non-terminal fragments will not have the top
  880. * bit set in the fragment length header.
  881. * But apparently no known nfs clients send fragmented
  882. * records. */
  883. if (net_ratelimit())
  884. printk(KERN_NOTICE "RPC: multiple fragments "
  885. "per record not supported\n");
  886. goto err_delete;
  887. }
  888. svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
  889. dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
  890. if (svsk->sk_reclen > serv->sv_max_mesg) {
  891. if (net_ratelimit())
  892. printk(KERN_NOTICE "RPC: "
  893. "fragment too large: 0x%08lx\n",
  894. (unsigned long)svsk->sk_reclen);
  895. goto err_delete;
  896. }
  897. }
  898. if (svsk->sk_reclen < 8)
  899. goto err_delete; /* client is nuts. */
  900. len = svsk->sk_reclen;
  901. return len;
  902. error:
  903. dprintk("RPC: TCP recv_record got %d\n", len);
  904. return len;
  905. err_delete:
  906. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  907. return -EAGAIN;
  908. }
  909. static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
  910. {
  911. struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
  912. struct rpc_rqst *req = NULL;
  913. struct kvec *src, *dst;
  914. __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  915. __be32 xid;
  916. __be32 calldir;
  917. xid = *p++;
  918. calldir = *p;
  919. if (bc_xprt)
  920. req = xprt_lookup_rqst(bc_xprt, xid);
  921. if (!req) {
  922. printk(KERN_NOTICE
  923. "%s: Got unrecognized reply: "
  924. "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
  925. __func__, ntohl(calldir),
  926. bc_xprt, xid);
  927. return -EAGAIN;
  928. }
  929. memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
  930. /*
  931. * XXX!: cheating for now! Only copying HEAD.
  932. * But we know this is good enough for now (in fact, for any
  933. * callback reply in the forseeable future).
  934. */
  935. dst = &req->rq_private_buf.head[0];
  936. src = &rqstp->rq_arg.head[0];
  937. if (dst->iov_len < src->iov_len)
  938. return -EAGAIN; /* whatever; just giving up. */
  939. memcpy(dst->iov_base, src->iov_base, src->iov_len);
  940. xprt_complete_rqst(req->rq_task, svsk->sk_reclen);
  941. rqstp->rq_arg.len = 0;
  942. return 0;
  943. }
  944. static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
  945. {
  946. int i = 0;
  947. int t = 0;
  948. while (t < len) {
  949. vec[i].iov_base = page_address(pages[i]);
  950. vec[i].iov_len = PAGE_SIZE;
  951. i++;
  952. t += PAGE_SIZE;
  953. }
  954. return i;
  955. }
  956. /*
  957. * Receive data from a TCP socket.
  958. */
  959. static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
  960. {
  961. struct svc_sock *svsk =
  962. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  963. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  964. int len;
  965. struct kvec *vec;
  966. unsigned int want, base;
  967. __be32 *p;
  968. __be32 calldir;
  969. int pnum;
  970. dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
  971. svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
  972. test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
  973. test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
  974. len = svc_tcp_recv_record(svsk, rqstp);
  975. if (len < 0)
  976. goto error;
  977. base = svc_tcp_restore_pages(svsk, rqstp);
  978. want = svsk->sk_reclen - base;
  979. vec = rqstp->rq_vec;
  980. pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
  981. svsk->sk_reclen);
  982. rqstp->rq_respages = &rqstp->rq_pages[pnum];
  983. /* Now receive data */
  984. len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
  985. if (len >= 0)
  986. svsk->sk_tcplen += len;
  987. if (len != want) {
  988. if (len < 0 && len != -EAGAIN)
  989. goto err_other;
  990. svc_tcp_save_pages(svsk, rqstp);
  991. dprintk("svc: incomplete TCP record (%d of %d)\n",
  992. svsk->sk_tcplen, svsk->sk_reclen);
  993. goto err_noclose;
  994. }
  995. rqstp->rq_arg.len = svsk->sk_reclen;
  996. rqstp->rq_arg.page_base = 0;
  997. if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
  998. rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
  999. rqstp->rq_arg.page_len = 0;
  1000. } else
  1001. rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1002. rqstp->rq_xprt_ctxt = NULL;
  1003. rqstp->rq_prot = IPPROTO_TCP;
  1004. p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  1005. calldir = p[1];
  1006. if (calldir)
  1007. len = receive_cb_reply(svsk, rqstp);
  1008. /* Reset TCP read info */
  1009. svsk->sk_reclen = 0;
  1010. svsk->sk_tcplen = 0;
  1011. /* If we have more data, signal svc_xprt_enqueue() to try again */
  1012. if (svc_recv_available(svsk) > sizeof(rpc_fraghdr))
  1013. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1014. if (len < 0)
  1015. goto error;
  1016. svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
  1017. if (serv->sv_stats)
  1018. serv->sv_stats->nettcpcnt++;
  1019. dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
  1020. return rqstp->rq_arg.len;
  1021. error:
  1022. if (len != -EAGAIN)
  1023. goto err_other;
  1024. dprintk("RPC: TCP recvfrom got EAGAIN\n");
  1025. return -EAGAIN;
  1026. err_other:
  1027. printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
  1028. svsk->sk_xprt.xpt_server->sv_name, -len);
  1029. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1030. err_noclose:
  1031. return -EAGAIN; /* record not complete */
  1032. }
  1033. /*
  1034. * Send out data on TCP socket.
  1035. */
  1036. static int svc_tcp_sendto(struct svc_rqst *rqstp)
  1037. {
  1038. struct xdr_buf *xbufp = &rqstp->rq_res;
  1039. int sent;
  1040. __be32 reclen;
  1041. /* Set up the first element of the reply kvec.
  1042. * Any other kvecs that may be in use have been taken
  1043. * care of by the server implementation itself.
  1044. */
  1045. reclen = htonl(0x80000000|((xbufp->len ) - 4));
  1046. memcpy(xbufp->head[0].iov_base, &reclen, 4);
  1047. sent = svc_sendto(rqstp, &rqstp->rq_res);
  1048. if (sent != xbufp->len) {
  1049. printk(KERN_NOTICE
  1050. "rpc-srv/tcp: %s: %s %d when sending %d bytes "
  1051. "- shutting down socket\n",
  1052. rqstp->rq_xprt->xpt_server->sv_name,
  1053. (sent<0)?"got error":"sent only",
  1054. sent, xbufp->len);
  1055. set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
  1056. svc_xprt_enqueue(rqstp->rq_xprt);
  1057. sent = -EAGAIN;
  1058. }
  1059. return sent;
  1060. }
  1061. /*
  1062. * Setup response header. TCP has a 4B record length field.
  1063. */
  1064. static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
  1065. {
  1066. struct kvec *resv = &rqstp->rq_res.head[0];
  1067. /* tcp needs a space for the record length... */
  1068. svc_putnl(resv, 0);
  1069. }
  1070. static int svc_tcp_has_wspace(struct svc_xprt *xprt)
  1071. {
  1072. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1073. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  1074. int required;
  1075. if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
  1076. return 1;
  1077. required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
  1078. if (sk_stream_wspace(svsk->sk_sk) >= required)
  1079. return 1;
  1080. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  1081. return 0;
  1082. }
  1083. static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
  1084. struct net *net,
  1085. struct sockaddr *sa, int salen,
  1086. int flags)
  1087. {
  1088. return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
  1089. }
  1090. #if defined(CONFIG_NFS_V4_1)
  1091. static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
  1092. struct net *, struct sockaddr *,
  1093. int, int);
  1094. static void svc_bc_sock_free(struct svc_xprt *xprt);
  1095. static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
  1096. struct net *net,
  1097. struct sockaddr *sa, int salen,
  1098. int flags)
  1099. {
  1100. return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
  1101. }
  1102. static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
  1103. {
  1104. }
  1105. static struct svc_xprt_ops svc_tcp_bc_ops = {
  1106. .xpo_create = svc_bc_tcp_create,
  1107. .xpo_detach = svc_bc_tcp_sock_detach,
  1108. .xpo_free = svc_bc_sock_free,
  1109. .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
  1110. };
  1111. static struct svc_xprt_class svc_tcp_bc_class = {
  1112. .xcl_name = "tcp-bc",
  1113. .xcl_owner = THIS_MODULE,
  1114. .xcl_ops = &svc_tcp_bc_ops,
  1115. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  1116. };
  1117. static void svc_init_bc_xprt_sock(void)
  1118. {
  1119. svc_reg_xprt_class(&svc_tcp_bc_class);
  1120. }
  1121. static void svc_cleanup_bc_xprt_sock(void)
  1122. {
  1123. svc_unreg_xprt_class(&svc_tcp_bc_class);
  1124. }
  1125. #else /* CONFIG_NFS_V4_1 */
  1126. static void svc_init_bc_xprt_sock(void)
  1127. {
  1128. }
  1129. static void svc_cleanup_bc_xprt_sock(void)
  1130. {
  1131. }
  1132. #endif /* CONFIG_NFS_V4_1 */
  1133. static struct svc_xprt_ops svc_tcp_ops = {
  1134. .xpo_create = svc_tcp_create,
  1135. .xpo_recvfrom = svc_tcp_recvfrom,
  1136. .xpo_sendto = svc_tcp_sendto,
  1137. .xpo_release_rqst = svc_release_skb,
  1138. .xpo_detach = svc_tcp_sock_detach,
  1139. .xpo_free = svc_sock_free,
  1140. .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
  1141. .xpo_has_wspace = svc_tcp_has_wspace,
  1142. .xpo_accept = svc_tcp_accept,
  1143. };
  1144. static struct svc_xprt_class svc_tcp_class = {
  1145. .xcl_name = "tcp",
  1146. .xcl_owner = THIS_MODULE,
  1147. .xcl_ops = &svc_tcp_ops,
  1148. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  1149. };
  1150. void svc_init_xprt_sock(void)
  1151. {
  1152. svc_reg_xprt_class(&svc_tcp_class);
  1153. svc_reg_xprt_class(&svc_udp_class);
  1154. svc_init_bc_xprt_sock();
  1155. }
  1156. void svc_cleanup_xprt_sock(void)
  1157. {
  1158. svc_unreg_xprt_class(&svc_tcp_class);
  1159. svc_unreg_xprt_class(&svc_udp_class);
  1160. svc_cleanup_bc_xprt_sock();
  1161. }
  1162. static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
  1163. {
  1164. struct sock *sk = svsk->sk_sk;
  1165. svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
  1166. set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
  1167. if (sk->sk_state == TCP_LISTEN) {
  1168. dprintk("setting up TCP socket for listening\n");
  1169. set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
  1170. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1171. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  1172. } else {
  1173. dprintk("setting up TCP socket for reading\n");
  1174. sk->sk_state_change = svc_tcp_state_change;
  1175. sk->sk_data_ready = svc_tcp_data_ready;
  1176. sk->sk_write_space = svc_tcp_write_space;
  1177. svsk->sk_reclen = 0;
  1178. svsk->sk_tcplen = 0;
  1179. memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
  1180. tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
  1181. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1182. if (sk->sk_state != TCP_ESTABLISHED)
  1183. set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
  1184. }
  1185. }
  1186. void svc_sock_update_bufs(struct svc_serv *serv)
  1187. {
  1188. /*
  1189. * The number of server threads has changed. Update
  1190. * rcvbuf and sndbuf accordingly on all sockets
  1191. */
  1192. struct svc_sock *svsk;
  1193. spin_lock_bh(&serv->sv_lock);
  1194. list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
  1195. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1196. list_for_each_entry(svsk, &serv->sv_tempsocks, sk_xprt.xpt_list)
  1197. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1198. spin_unlock_bh(&serv->sv_lock);
  1199. }
  1200. EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
  1201. /*
  1202. * Initialize socket for RPC use and create svc_sock struct
  1203. * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
  1204. */
  1205. static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
  1206. struct socket *sock,
  1207. int *errp, int flags)
  1208. {
  1209. struct svc_sock *svsk;
  1210. struct sock *inet;
  1211. int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
  1212. dprintk("svc: svc_setup_socket %p\n", sock);
  1213. if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
  1214. *errp = -ENOMEM;
  1215. return NULL;
  1216. }
  1217. inet = sock->sk;
  1218. /* Register socket with portmapper */
  1219. if (*errp >= 0 && pmap_register)
  1220. *errp = svc_register(serv, inet->sk_family, inet->sk_protocol,
  1221. ntohs(inet_sk(inet)->inet_sport));
  1222. if (*errp < 0) {
  1223. kfree(svsk);
  1224. return NULL;
  1225. }
  1226. inet->sk_user_data = svsk;
  1227. svsk->sk_sock = sock;
  1228. svsk->sk_sk = inet;
  1229. svsk->sk_ostate = inet->sk_state_change;
  1230. svsk->sk_odata = inet->sk_data_ready;
  1231. svsk->sk_owspace = inet->sk_write_space;
  1232. /* Initialize the socket */
  1233. if (sock->type == SOCK_DGRAM)
  1234. svc_udp_init(svsk, serv);
  1235. else {
  1236. /* initialise setting must have enough space to
  1237. * receive and respond to one request.
  1238. */
  1239. svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
  1240. 4 * serv->sv_max_mesg);
  1241. svc_tcp_init(svsk, serv);
  1242. }
  1243. dprintk("svc: svc_setup_socket created %p (inet %p)\n",
  1244. svsk, svsk->sk_sk);
  1245. return svsk;
  1246. }
  1247. /**
  1248. * svc_addsock - add a listener socket to an RPC service
  1249. * @serv: pointer to RPC service to which to add a new listener
  1250. * @fd: file descriptor of the new listener
  1251. * @name_return: pointer to buffer to fill in with name of listener
  1252. * @len: size of the buffer
  1253. *
  1254. * Fills in socket name and returns positive length of name if successful.
  1255. * Name is terminated with '\n'. On error, returns a negative errno
  1256. * value.
  1257. */
  1258. int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
  1259. const size_t len)
  1260. {
  1261. int err = 0;
  1262. struct socket *so = sockfd_lookup(fd, &err);
  1263. struct svc_sock *svsk = NULL;
  1264. if (!so)
  1265. return err;
  1266. if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
  1267. err = -EAFNOSUPPORT;
  1268. else if (so->sk->sk_protocol != IPPROTO_TCP &&
  1269. so->sk->sk_protocol != IPPROTO_UDP)
  1270. err = -EPROTONOSUPPORT;
  1271. else if (so->state > SS_UNCONNECTED)
  1272. err = -EISCONN;
  1273. else {
  1274. if (!try_module_get(THIS_MODULE))
  1275. err = -ENOENT;
  1276. else
  1277. svsk = svc_setup_socket(serv, so, &err,
  1278. SVC_SOCK_DEFAULTS);
  1279. if (svsk) {
  1280. struct sockaddr_storage addr;
  1281. struct sockaddr *sin = (struct sockaddr *)&addr;
  1282. int salen;
  1283. if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
  1284. svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
  1285. clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
  1286. spin_lock_bh(&serv->sv_lock);
  1287. list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
  1288. spin_unlock_bh(&serv->sv_lock);
  1289. svc_xprt_received(&svsk->sk_xprt);
  1290. err = 0;
  1291. } else
  1292. module_put(THIS_MODULE);
  1293. }
  1294. if (err) {
  1295. sockfd_put(so);
  1296. return err;
  1297. }
  1298. return svc_one_sock_name(svsk, name_return, len);
  1299. }
  1300. EXPORT_SYMBOL_GPL(svc_addsock);
  1301. /*
  1302. * Create socket for RPC service.
  1303. */
  1304. static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
  1305. int protocol,
  1306. struct net *net,
  1307. struct sockaddr *sin, int len,
  1308. int flags)
  1309. {
  1310. struct svc_sock *svsk;
  1311. struct socket *sock;
  1312. int error;
  1313. int type;
  1314. struct sockaddr_storage addr;
  1315. struct sockaddr *newsin = (struct sockaddr *)&addr;
  1316. int newlen;
  1317. int family;
  1318. int val;
  1319. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  1320. dprintk("svc: svc_create_socket(%s, %d, %s)\n",
  1321. serv->sv_program->pg_name, protocol,
  1322. __svc_print_addr(sin, buf, sizeof(buf)));
  1323. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1324. printk(KERN_WARNING "svc: only UDP and TCP "
  1325. "sockets supported\n");
  1326. return ERR_PTR(-EINVAL);
  1327. }
  1328. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1329. switch (sin->sa_family) {
  1330. case AF_INET6:
  1331. family = PF_INET6;
  1332. break;
  1333. case AF_INET:
  1334. family = PF_INET;
  1335. break;
  1336. default:
  1337. return ERR_PTR(-EINVAL);
  1338. }
  1339. error = __sock_create(net, family, type, protocol, &sock, 1);
  1340. if (error < 0)
  1341. return ERR_PTR(error);
  1342. svc_reclassify_socket(sock);
  1343. /*
  1344. * If this is an PF_INET6 listener, we want to avoid
  1345. * getting requests from IPv4 remotes. Those should
  1346. * be shunted to a PF_INET listener via rpcbind.
  1347. */
  1348. val = 1;
  1349. if (family == PF_INET6)
  1350. kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
  1351. (char *)&val, sizeof(val));
  1352. if (type == SOCK_STREAM)
  1353. sock->sk->sk_reuse = 1; /* allow address reuse */
  1354. error = kernel_bind(sock, sin, len);
  1355. if (error < 0)
  1356. goto bummer;
  1357. newlen = len;
  1358. error = kernel_getsockname(sock, newsin, &newlen);
  1359. if (error < 0)
  1360. goto bummer;
  1361. if (protocol == IPPROTO_TCP) {
  1362. if ((error = kernel_listen(sock, 64)) < 0)
  1363. goto bummer;
  1364. }
  1365. if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
  1366. svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
  1367. return (struct svc_xprt *)svsk;
  1368. }
  1369. bummer:
  1370. dprintk("svc: svc_create_socket error = %d\n", -error);
  1371. sock_release(sock);
  1372. return ERR_PTR(error);
  1373. }
  1374. /*
  1375. * Detach the svc_sock from the socket so that no
  1376. * more callbacks occur.
  1377. */
  1378. static void svc_sock_detach(struct svc_xprt *xprt)
  1379. {
  1380. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1381. struct sock *sk = svsk->sk_sk;
  1382. wait_queue_head_t *wq;
  1383. dprintk("svc: svc_sock_detach(%p)\n", svsk);
  1384. /* put back the old socket callbacks */
  1385. sk->sk_state_change = svsk->sk_ostate;
  1386. sk->sk_data_ready = svsk->sk_odata;
  1387. sk->sk_write_space = svsk->sk_owspace;
  1388. wq = sk_sleep(sk);
  1389. if (wq && waitqueue_active(wq))
  1390. wake_up_interruptible(wq);
  1391. }
  1392. /*
  1393. * Disconnect the socket, and reset the callbacks
  1394. */
  1395. static void svc_tcp_sock_detach(struct svc_xprt *xprt)
  1396. {
  1397. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1398. dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
  1399. svc_sock_detach(xprt);
  1400. if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  1401. svc_tcp_clear_pages(svsk);
  1402. kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
  1403. }
  1404. }
  1405. /*
  1406. * Free the svc_sock's socket resources and the svc_sock itself.
  1407. */
  1408. static void svc_sock_free(struct svc_xprt *xprt)
  1409. {
  1410. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1411. dprintk("svc: svc_sock_free(%p)\n", svsk);
  1412. if (svsk->sk_sock->file)
  1413. sockfd_put(svsk->sk_sock);
  1414. else
  1415. sock_release(svsk->sk_sock);
  1416. kfree(svsk);
  1417. }
  1418. #if defined(CONFIG_NFS_V4_1)
  1419. /*
  1420. * Create a back channel svc_xprt which shares the fore channel socket.
  1421. */
  1422. static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
  1423. int protocol,
  1424. struct net *net,
  1425. struct sockaddr *sin, int len,
  1426. int flags)
  1427. {
  1428. struct svc_sock *svsk;
  1429. struct svc_xprt *xprt;
  1430. if (protocol != IPPROTO_TCP) {
  1431. printk(KERN_WARNING "svc: only TCP sockets"
  1432. " supported on shared back channel\n");
  1433. return ERR_PTR(-EINVAL);
  1434. }
  1435. svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
  1436. if (!svsk)
  1437. return ERR_PTR(-ENOMEM);
  1438. xprt = &svsk->sk_xprt;
  1439. svc_xprt_init(&svc_tcp_bc_class, xprt, serv);
  1440. serv->sv_bc_xprt = xprt;
  1441. return xprt;
  1442. }
  1443. /*
  1444. * Free a back channel svc_sock.
  1445. */
  1446. static void svc_bc_sock_free(struct svc_xprt *xprt)
  1447. {
  1448. if (xprt)
  1449. kfree(container_of(xprt, struct svc_sock, sk_xprt));
  1450. }
  1451. #endif /* CONFIG_NFS_V4_1 */