svcsock.c 42 KB

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