svcsock.c 40 KB

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