svcsock.c 43 KB

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