svcsock.c 43 KB

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