svcsock.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/svcsock.c
  4. *
  5. * These are the RPC server socket internals.
  6. *
  7. * The server scheduling algorithm does not always distribute the load
  8. * evenly when servicing a single client. May need to modify the
  9. * svc_xprt_enqueue procedure...
  10. *
  11. * TCP support is largely untested and may be a little slow. The problem
  12. * is that we currently do two separate recvfrom's, one for the 4-byte
  13. * record length, and the second for the actual record. This could possibly
  14. * be improved by always reading a minimum size of around 100 bytes and
  15. * tucking any superfluous bytes away in a temporary store. Still, that
  16. * leaves write requests out in the rain. An alternative may be to peek at
  17. * the first skb in the queue, and if it matches the next TCP sequence
  18. * number, to extract the record marker. Yuck.
  19. *
  20. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/module.h>
  25. #include <linux/errno.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/net.h>
  28. #include <linux/in.h>
  29. #include <linux/inet.h>
  30. #include <linux/udp.h>
  31. #include <linux/tcp.h>
  32. #include <linux/unistd.h>
  33. #include <linux/slab.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/file.h>
  37. #include <linux/freezer.h>
  38. #include <linux/bvec.h>
  39. #include <net/sock.h>
  40. #include <net/checksum.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/udp.h>
  44. #include <net/tcp.h>
  45. #include <net/tcp_states.h>
  46. #include <net/tls_prot.h>
  47. #include <net/handshake.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/highmem.h>
  50. #include <asm/ioctls.h>
  51. #include <linux/key.h>
  52. #include <linux/sunrpc/types.h>
  53. #include <linux/sunrpc/clnt.h>
  54. #include <linux/sunrpc/xdr.h>
  55. #include <linux/sunrpc/msg_prot.h>
  56. #include <linux/sunrpc/svcsock.h>
  57. #include <linux/sunrpc/stats.h>
  58. #include <linux/sunrpc/xprt.h>
  59. #include <trace/events/sock.h>
  60. #include <trace/events/sunrpc.h>
  61. #include "socklib.h"
  62. #include "sunrpc.h"
  63. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  64. /*
  65. * For UDP:
  66. * 1 for header page
  67. * enough pages for RPCSVC_MAXPAYLOAD_UDP
  68. * 1 in case payload is not aligned
  69. * 1 for tail page
  70. */
  71. enum {
  72. SUNRPC_MAX_UDP_SENDPAGES = 1 + RPCSVC_MAXPAYLOAD_UDP / PAGE_SIZE + 1 + 1
  73. };
  74. /* To-do: to avoid tying up an nfsd thread while waiting for a
  75. * handshake request, the request could instead be deferred.
  76. */
  77. enum {
  78. SVC_HANDSHAKE_TO = 5U * HZ
  79. };
  80. static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
  81. int flags);
  82. static int svc_udp_recvfrom(struct svc_rqst *);
  83. static int svc_udp_sendto(struct svc_rqst *);
  84. static void svc_sock_detach(struct svc_xprt *);
  85. static void svc_tcp_sock_detach(struct svc_xprt *);
  86. static void svc_sock_free(struct svc_xprt *);
  87. static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
  88. struct net *, struct sockaddr *,
  89. int, int);
  90. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  91. static struct lock_class_key svc_key[2];
  92. static struct lock_class_key svc_slock_key[2];
  93. static void svc_reclassify_socket(struct socket *sock)
  94. {
  95. struct sock *sk = sock->sk;
  96. if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
  97. return;
  98. switch (sk->sk_family) {
  99. case AF_INET:
  100. sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
  101. &svc_slock_key[0],
  102. "sk_xprt.xpt_lock-AF_INET-NFSD",
  103. &svc_key[0]);
  104. break;
  105. case AF_INET6:
  106. sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
  107. &svc_slock_key[1],
  108. "sk_xprt.xpt_lock-AF_INET6-NFSD",
  109. &svc_key[1]);
  110. break;
  111. default:
  112. BUG();
  113. }
  114. }
  115. #else
  116. static void svc_reclassify_socket(struct socket *sock)
  117. {
  118. }
  119. #endif
  120. /**
  121. * svc_tcp_release_ctxt - Release transport-related resources
  122. * @xprt: the transport which owned the context
  123. * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
  124. *
  125. */
  126. static void svc_tcp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
  127. {
  128. }
  129. /**
  130. * svc_udp_release_ctxt - Release transport-related resources
  131. * @xprt: the transport which owned the context
  132. * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
  133. *
  134. */
  135. static void svc_udp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
  136. {
  137. struct sk_buff *skb = ctxt;
  138. if (skb)
  139. consume_skb(skb);
  140. }
  141. union svc_pktinfo_u {
  142. struct in_pktinfo pkti;
  143. struct in6_pktinfo pkti6;
  144. };
  145. #define SVC_PKTINFO_SPACE \
  146. CMSG_SPACE(sizeof(union svc_pktinfo_u))
  147. static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
  148. {
  149. struct svc_sock *svsk =
  150. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  151. switch (svsk->sk_sk->sk_family) {
  152. case AF_INET: {
  153. struct in_pktinfo *pki = CMSG_DATA(cmh);
  154. cmh->cmsg_level = SOL_IP;
  155. cmh->cmsg_type = IP_PKTINFO;
  156. pki->ipi_ifindex = 0;
  157. pki->ipi_spec_dst.s_addr =
  158. svc_daddr_in(rqstp)->sin_addr.s_addr;
  159. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  160. }
  161. break;
  162. case AF_INET6: {
  163. struct in6_pktinfo *pki = CMSG_DATA(cmh);
  164. struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
  165. cmh->cmsg_level = SOL_IPV6;
  166. cmh->cmsg_type = IPV6_PKTINFO;
  167. pki->ipi6_ifindex = daddr->sin6_scope_id;
  168. pki->ipi6_addr = daddr->sin6_addr;
  169. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  170. }
  171. break;
  172. }
  173. }
  174. static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
  175. unsigned int length)
  176. {
  177. return 0;
  178. }
  179. /*
  180. * Report socket names for nfsdfs
  181. */
  182. static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
  183. {
  184. const struct sock *sk = svsk->sk_sk;
  185. const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
  186. "udp" : "tcp";
  187. int len;
  188. switch (sk->sk_family) {
  189. case PF_INET:
  190. len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
  191. proto_name,
  192. &inet_sk(sk)->inet_rcv_saddr,
  193. inet_sk(sk)->inet_num);
  194. break;
  195. #if IS_ENABLED(CONFIG_IPV6)
  196. case PF_INET6:
  197. len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
  198. proto_name,
  199. &sk->sk_v6_rcv_saddr,
  200. inet_sk(sk)->inet_num);
  201. break;
  202. #endif
  203. default:
  204. len = snprintf(buf, remaining, "*unknown-%d*\n",
  205. sk->sk_family);
  206. }
  207. if (len >= remaining) {
  208. *buf = '\0';
  209. return -ENAMETOOLONG;
  210. }
  211. return len;
  212. }
  213. static int
  214. svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
  215. struct cmsghdr *cmsg, int ret)
  216. {
  217. u8 content_type = tls_get_record_type(sock->sk, cmsg);
  218. u8 level, description;
  219. switch (content_type) {
  220. case 0:
  221. break;
  222. case TLS_RECORD_TYPE_DATA:
  223. /* TLS sets EOR at the end of each application data
  224. * record, even though there might be more frames
  225. * waiting to be decrypted.
  226. */
  227. msg->msg_flags &= ~MSG_EOR;
  228. break;
  229. case TLS_RECORD_TYPE_ALERT:
  230. tls_alert_recv(sock->sk, msg, &level, &description);
  231. ret = (level == TLS_ALERT_LEVEL_FATAL) ?
  232. -ENOTCONN : -EAGAIN;
  233. break;
  234. default:
  235. /* discard this record type */
  236. ret = -EAGAIN;
  237. }
  238. return ret;
  239. }
  240. static int
  241. svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
  242. {
  243. union {
  244. struct cmsghdr cmsg;
  245. u8 buf[CMSG_SPACE(sizeof(u8))];
  246. } u;
  247. u8 alert[2];
  248. struct kvec alert_kvec = {
  249. .iov_base = alert,
  250. .iov_len = sizeof(alert),
  251. };
  252. struct msghdr msg = {
  253. .msg_flags = *msg_flags,
  254. .msg_control = &u,
  255. .msg_controllen = sizeof(u),
  256. };
  257. int ret;
  258. iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
  259. alert_kvec.iov_len);
  260. ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
  261. if (ret > 0 &&
  262. tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
  263. iov_iter_revert(&msg.msg_iter, ret);
  264. ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
  265. }
  266. return ret;
  267. }
  268. static int
  269. svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)
  270. {
  271. int ret;
  272. struct socket *sock = svsk->sk_sock;
  273. ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
  274. if (msg->msg_flags & MSG_CTRUNC) {
  275. msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
  276. if (ret == 0 || ret == -EIO)
  277. ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
  278. }
  279. return ret;
  280. }
  281. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  282. static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
  283. {
  284. struct bvec_iter bi = {
  285. .bi_size = size + seek,
  286. };
  287. struct bio_vec bv;
  288. bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
  289. for_each_bvec(bv, bvec, bi, bi)
  290. flush_dcache_page(bv.bv_page);
  291. }
  292. #else
  293. static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
  294. size_t seek)
  295. {
  296. }
  297. #endif
  298. /*
  299. * Read from @rqstp's transport socket. The incoming message fills whole
  300. * pages in @rqstp's rq_pages array until the last page of the message
  301. * has been received into a partial page.
  302. */
  303. static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
  304. size_t seek)
  305. {
  306. struct svc_sock *svsk =
  307. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  308. struct bio_vec *bvec = rqstp->rq_bvec;
  309. struct msghdr msg = { NULL };
  310. unsigned int i;
  311. ssize_t len;
  312. size_t t;
  313. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  314. for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE)
  315. bvec_set_page(&bvec[i], rqstp->rq_pages[i], PAGE_SIZE, 0);
  316. rqstp->rq_respages = &rqstp->rq_pages[i];
  317. rqstp->rq_next_page = rqstp->rq_respages + 1;
  318. iov_iter_bvec(&msg.msg_iter, ITER_DEST, bvec, i, buflen);
  319. if (seek) {
  320. iov_iter_advance(&msg.msg_iter, seek);
  321. buflen -= seek;
  322. }
  323. len = svc_tcp_sock_recvmsg(svsk, &msg);
  324. if (len > 0)
  325. svc_flush_bvec(bvec, len, seek);
  326. /* If we read a full record, then assume there may be more
  327. * data to read (stream based sockets only!)
  328. */
  329. if (len == buflen)
  330. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  331. return len;
  332. }
  333. /*
  334. * Set socket snd and rcv buffer lengths
  335. */
  336. static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
  337. {
  338. unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
  339. struct socket *sock = svsk->sk_sock;
  340. nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
  341. lock_sock(sock->sk);
  342. sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
  343. sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
  344. sock->sk->sk_write_space(sock->sk);
  345. release_sock(sock->sk);
  346. }
  347. static void svc_sock_secure_port(struct svc_rqst *rqstp)
  348. {
  349. if (svc_port_is_privileged(svc_addr(rqstp)))
  350. set_bit(RQ_SECURE, &rqstp->rq_flags);
  351. else
  352. clear_bit(RQ_SECURE, &rqstp->rq_flags);
  353. }
  354. /*
  355. * INET callback when data has been received on the socket.
  356. */
  357. static void svc_data_ready(struct sock *sk)
  358. {
  359. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  360. trace_sk_data_ready(sk);
  361. if (svsk) {
  362. /* Refer to svc_setup_socket() for details. */
  363. rmb();
  364. svsk->sk_odata(sk);
  365. trace_svcsock_data_ready(&svsk->sk_xprt, 0);
  366. if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
  367. return;
  368. if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
  369. svc_xprt_enqueue(&svsk->sk_xprt);
  370. }
  371. }
  372. /*
  373. * INET callback when space is newly available on the socket.
  374. */
  375. static void svc_write_space(struct sock *sk)
  376. {
  377. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  378. if (svsk) {
  379. /* Refer to svc_setup_socket() for details. */
  380. rmb();
  381. trace_svcsock_write_space(&svsk->sk_xprt, 0);
  382. svsk->sk_owspace(sk);
  383. svc_xprt_enqueue(&svsk->sk_xprt);
  384. }
  385. }
  386. static int svc_tcp_has_wspace(struct svc_xprt *xprt)
  387. {
  388. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  389. if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
  390. return 1;
  391. return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  392. }
  393. static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
  394. {
  395. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  396. sock_no_linger(svsk->sk_sock->sk);
  397. }
  398. /**
  399. * svc_tcp_handshake_done - Handshake completion handler
  400. * @data: address of xprt to wake
  401. * @status: status of handshake
  402. * @peerid: serial number of key containing the remote peer's identity
  403. *
  404. * If a security policy is specified as an export option, we don't
  405. * have a specific export here to check. So we set a "TLS session
  406. * is present" flag on the xprt and let an upper layer enforce local
  407. * security policy.
  408. */
  409. static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
  410. {
  411. struct svc_xprt *xprt = data;
  412. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  413. if (!status) {
  414. if (peerid != TLS_NO_PEERID)
  415. set_bit(XPT_PEER_AUTH, &xprt->xpt_flags);
  416. set_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
  417. }
  418. clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
  419. complete_all(&svsk->sk_handshake_done);
  420. }
  421. /**
  422. * svc_tcp_handshake - Perform a transport-layer security handshake
  423. * @xprt: connected transport endpoint
  424. *
  425. */
  426. static void svc_tcp_handshake(struct svc_xprt *xprt)
  427. {
  428. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  429. struct sock *sk = svsk->sk_sock->sk;
  430. struct tls_handshake_args args = {
  431. .ta_sock = svsk->sk_sock,
  432. .ta_done = svc_tcp_handshake_done,
  433. .ta_data = xprt,
  434. };
  435. int ret;
  436. trace_svc_tls_upcall(xprt);
  437. clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
  438. init_completion(&svsk->sk_handshake_done);
  439. ret = tls_server_hello_x509(&args, GFP_KERNEL);
  440. if (ret) {
  441. trace_svc_tls_not_started(xprt);
  442. goto out_failed;
  443. }
  444. ret = wait_for_completion_interruptible_timeout(&svsk->sk_handshake_done,
  445. SVC_HANDSHAKE_TO);
  446. if (ret <= 0) {
  447. if (tls_handshake_cancel(sk)) {
  448. trace_svc_tls_timed_out(xprt);
  449. goto out_close;
  450. }
  451. }
  452. if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
  453. trace_svc_tls_unavailable(xprt);
  454. goto out_close;
  455. }
  456. /* Mark the transport ready in case the remote sent RPC
  457. * traffic before the kernel received the handshake
  458. * completion downcall.
  459. */
  460. set_bit(XPT_DATA, &xprt->xpt_flags);
  461. svc_xprt_enqueue(xprt);
  462. return;
  463. out_close:
  464. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  465. out_failed:
  466. clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
  467. set_bit(XPT_DATA, &xprt->xpt_flags);
  468. svc_xprt_enqueue(xprt);
  469. }
  470. /*
  471. * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
  472. */
  473. static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
  474. struct cmsghdr *cmh)
  475. {
  476. struct in_pktinfo *pki = CMSG_DATA(cmh);
  477. struct sockaddr_in *daddr = svc_daddr_in(rqstp);
  478. if (cmh->cmsg_type != IP_PKTINFO)
  479. return 0;
  480. daddr->sin_family = AF_INET;
  481. daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
  482. return 1;
  483. }
  484. /*
  485. * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
  486. */
  487. static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
  488. struct cmsghdr *cmh)
  489. {
  490. struct in6_pktinfo *pki = CMSG_DATA(cmh);
  491. struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
  492. if (cmh->cmsg_type != IPV6_PKTINFO)
  493. return 0;
  494. daddr->sin6_family = AF_INET6;
  495. daddr->sin6_addr = pki->ipi6_addr;
  496. daddr->sin6_scope_id = pki->ipi6_ifindex;
  497. return 1;
  498. }
  499. /*
  500. * Copy the UDP datagram's destination address to the rqstp structure.
  501. * The 'destination' address in this case is the address to which the
  502. * peer sent the datagram, i.e. our local address. For multihomed
  503. * hosts, this can change from msg to msg. Note that only the IP
  504. * address changes, the port number should remain the same.
  505. */
  506. static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
  507. struct cmsghdr *cmh)
  508. {
  509. switch (cmh->cmsg_level) {
  510. case SOL_IP:
  511. return svc_udp_get_dest_address4(rqstp, cmh);
  512. case SOL_IPV6:
  513. return svc_udp_get_dest_address6(rqstp, cmh);
  514. }
  515. return 0;
  516. }
  517. /**
  518. * svc_udp_recvfrom - Receive a datagram from a UDP socket.
  519. * @rqstp: request structure into which to receive an RPC Call
  520. *
  521. * Called in a loop when XPT_DATA has been set.
  522. *
  523. * Returns:
  524. * On success, the number of bytes in a received RPC Call, or
  525. * %0 if a complete RPC Call message was not ready to return
  526. */
  527. static int svc_udp_recvfrom(struct svc_rqst *rqstp)
  528. {
  529. struct svc_sock *svsk =
  530. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  531. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  532. struct sk_buff *skb;
  533. union {
  534. struct cmsghdr hdr;
  535. long all[SVC_PKTINFO_SPACE / sizeof(long)];
  536. } buffer;
  537. struct cmsghdr *cmh = &buffer.hdr;
  538. struct msghdr msg = {
  539. .msg_name = svc_addr(rqstp),
  540. .msg_control = cmh,
  541. .msg_controllen = sizeof(buffer),
  542. .msg_flags = MSG_DONTWAIT,
  543. };
  544. size_t len;
  545. int err;
  546. if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
  547. /* udp sockets need large rcvbuf as all pending
  548. * requests are still in that buffer. sndbuf must
  549. * also be large enough that there is enough space
  550. * for one reply per thread. We count all threads
  551. * rather than threads in a particular pool, which
  552. * provides an upper bound on the number of threads
  553. * which will access the socket.
  554. */
  555. svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
  556. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  557. err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
  558. 0, 0, MSG_PEEK | MSG_DONTWAIT);
  559. if (err < 0)
  560. goto out_recv_err;
  561. skb = skb_recv_udp(svsk->sk_sk, MSG_DONTWAIT, &err);
  562. if (!skb)
  563. goto out_recv_err;
  564. len = svc_addr_len(svc_addr(rqstp));
  565. rqstp->rq_addrlen = len;
  566. if (skb->tstamp == 0) {
  567. skb->tstamp = ktime_get_real();
  568. /* Don't enable netstamp, sunrpc doesn't
  569. need that much accuracy */
  570. }
  571. sock_write_timestamp(svsk->sk_sk, skb->tstamp);
  572. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
  573. len = skb->len;
  574. rqstp->rq_arg.len = len;
  575. trace_svcsock_udp_recv(&svsk->sk_xprt, len);
  576. rqstp->rq_prot = IPPROTO_UDP;
  577. if (!svc_udp_get_dest_address(rqstp, cmh))
  578. goto out_cmsg_err;
  579. rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
  580. if (skb_is_nonlinear(skb)) {
  581. /* we have to copy */
  582. local_bh_disable();
  583. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
  584. goto out_bh_enable;
  585. local_bh_enable();
  586. consume_skb(skb);
  587. } else {
  588. /* we can use it in-place */
  589. rqstp->rq_arg.head[0].iov_base = skb->data;
  590. rqstp->rq_arg.head[0].iov_len = len;
  591. if (skb_checksum_complete(skb))
  592. goto out_free;
  593. rqstp->rq_xprt_ctxt = skb;
  594. }
  595. rqstp->rq_arg.page_base = 0;
  596. if (len <= rqstp->rq_arg.head[0].iov_len) {
  597. rqstp->rq_arg.head[0].iov_len = len;
  598. rqstp->rq_arg.page_len = 0;
  599. rqstp->rq_respages = rqstp->rq_pages+1;
  600. } else {
  601. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  602. rqstp->rq_respages = rqstp->rq_pages + 1 +
  603. DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
  604. }
  605. rqstp->rq_next_page = rqstp->rq_respages+1;
  606. if (serv->sv_stats)
  607. serv->sv_stats->netudpcnt++;
  608. svc_sock_secure_port(rqstp);
  609. svc_xprt_received(rqstp->rq_xprt);
  610. return len;
  611. out_recv_err:
  612. if (err != -EAGAIN) {
  613. /* possibly an icmp error */
  614. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  615. }
  616. trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
  617. goto out_clear_busy;
  618. out_cmsg_err:
  619. net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
  620. cmh->cmsg_level, cmh->cmsg_type);
  621. goto out_free;
  622. out_bh_enable:
  623. local_bh_enable();
  624. out_free:
  625. kfree_skb(skb);
  626. out_clear_busy:
  627. svc_xprt_received(rqstp->rq_xprt);
  628. return 0;
  629. }
  630. /**
  631. * svc_udp_sendto - Send out a reply on a UDP socket
  632. * @rqstp: completed svc_rqst
  633. *
  634. * xpt_mutex ensures @rqstp's whole message is written to the socket
  635. * without interruption.
  636. *
  637. * Returns the number of bytes sent, or a negative errno.
  638. */
  639. static int svc_udp_sendto(struct svc_rqst *rqstp)
  640. {
  641. struct svc_xprt *xprt = rqstp->rq_xprt;
  642. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  643. struct xdr_buf *xdr = &rqstp->rq_res;
  644. union {
  645. struct cmsghdr hdr;
  646. long all[SVC_PKTINFO_SPACE / sizeof(long)];
  647. } buffer;
  648. struct cmsghdr *cmh = &buffer.hdr;
  649. struct msghdr msg = {
  650. .msg_name = &rqstp->rq_addr,
  651. .msg_namelen = rqstp->rq_addrlen,
  652. .msg_control = cmh,
  653. .msg_flags = MSG_SPLICE_PAGES,
  654. .msg_controllen = sizeof(buffer),
  655. };
  656. unsigned int count;
  657. int err;
  658. svc_udp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
  659. rqstp->rq_xprt_ctxt = NULL;
  660. svc_set_cmsg_data(rqstp, cmh);
  661. mutex_lock(&xprt->xpt_mutex);
  662. if (svc_xprt_is_dead(xprt))
  663. goto out_notconn;
  664. count = xdr_buf_to_bvec(svsk->sk_bvec, SUNRPC_MAX_UDP_SENDPAGES, xdr);
  665. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
  666. count, rqstp->rq_res.len);
  667. err = sock_sendmsg(svsk->sk_sock, &msg);
  668. if (err == -ECONNREFUSED) {
  669. /* ICMP error on earlier request. */
  670. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
  671. count, rqstp->rq_res.len);
  672. err = sock_sendmsg(svsk->sk_sock, &msg);
  673. }
  674. trace_svcsock_udp_send(xprt, err);
  675. mutex_unlock(&xprt->xpt_mutex);
  676. return err;
  677. out_notconn:
  678. mutex_unlock(&xprt->xpt_mutex);
  679. return -ENOTCONN;
  680. }
  681. static int svc_udp_has_wspace(struct svc_xprt *xprt)
  682. {
  683. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  684. struct svc_serv *serv = xprt->xpt_server;
  685. unsigned long required;
  686. /*
  687. * Set the SOCK_NOSPACE flag before checking the available
  688. * sock space.
  689. */
  690. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  691. required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
  692. if (required*2 > sock_wspace(svsk->sk_sk))
  693. return 0;
  694. clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  695. return 1;
  696. }
  697. static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
  698. {
  699. BUG();
  700. return NULL;
  701. }
  702. static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
  703. {
  704. }
  705. static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
  706. struct net *net,
  707. struct sockaddr *sa, int salen,
  708. int flags)
  709. {
  710. return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
  711. }
  712. static const struct svc_xprt_ops svc_udp_ops = {
  713. .xpo_create = svc_udp_create,
  714. .xpo_recvfrom = svc_udp_recvfrom,
  715. .xpo_sendto = svc_udp_sendto,
  716. .xpo_result_payload = svc_sock_result_payload,
  717. .xpo_release_ctxt = svc_udp_release_ctxt,
  718. .xpo_detach = svc_sock_detach,
  719. .xpo_free = svc_sock_free,
  720. .xpo_has_wspace = svc_udp_has_wspace,
  721. .xpo_accept = svc_udp_accept,
  722. .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
  723. };
  724. static struct svc_xprt_class svc_udp_class = {
  725. .xcl_name = "udp",
  726. .xcl_owner = THIS_MODULE,
  727. .xcl_ops = &svc_udp_ops,
  728. .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
  729. .xcl_ident = XPRT_TRANSPORT_UDP,
  730. };
  731. static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
  732. {
  733. svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
  734. &svsk->sk_xprt, serv);
  735. clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
  736. svsk->sk_sk->sk_data_ready = svc_data_ready;
  737. svsk->sk_sk->sk_write_space = svc_write_space;
  738. /* initialise setting must have enough space to
  739. * receive and respond to one request.
  740. * svc_udp_recvfrom will re-adjust if necessary
  741. */
  742. svc_sock_setbufsize(svsk, 3);
  743. /* data might have come in before data_ready set up */
  744. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  745. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  746. set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
  747. /* make sure we get destination address info */
  748. switch (svsk->sk_sk->sk_family) {
  749. case AF_INET:
  750. ip_sock_set_pktinfo(svsk->sk_sock->sk);
  751. break;
  752. case AF_INET6:
  753. ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
  754. break;
  755. default:
  756. BUG();
  757. }
  758. }
  759. /*
  760. * A data_ready event on a listening socket means there's a connection
  761. * pending. Do not use state_change as a substitute for it.
  762. */
  763. static void svc_tcp_listen_data_ready(struct sock *sk)
  764. {
  765. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  766. trace_sk_data_ready(sk);
  767. /*
  768. * This callback may called twice when a new connection
  769. * is established as a child socket inherits everything
  770. * from a parent LISTEN socket.
  771. * 1) data_ready method of the parent socket will be called
  772. * when one of child sockets become ESTABLISHED.
  773. * 2) data_ready method of the child socket may be called
  774. * when it receives data before the socket is accepted.
  775. * In case of 2, we should ignore it silently and DO NOT
  776. * dereference svsk.
  777. */
  778. if (sk->sk_state != TCP_LISTEN)
  779. return;
  780. if (svsk) {
  781. /* Refer to svc_setup_socket() for details. */
  782. rmb();
  783. svsk->sk_odata(sk);
  784. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  785. svc_xprt_enqueue(&svsk->sk_xprt);
  786. }
  787. }
  788. /*
  789. * A state change on a connected socket means it's dying or dead.
  790. */
  791. static void svc_tcp_state_change(struct sock *sk)
  792. {
  793. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  794. if (svsk) {
  795. /* Refer to svc_setup_socket() for details. */
  796. rmb();
  797. svsk->sk_ostate(sk);
  798. trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
  799. if (sk->sk_state != TCP_ESTABLISHED)
  800. svc_xprt_deferred_close(&svsk->sk_xprt);
  801. }
  802. }
  803. /*
  804. * Accept a TCP connection
  805. */
  806. static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
  807. {
  808. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  809. struct sockaddr_storage addr;
  810. struct sockaddr *sin = (struct sockaddr *) &addr;
  811. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  812. struct socket *sock = svsk->sk_sock;
  813. struct socket *newsock;
  814. struct svc_sock *newsvsk;
  815. int err, slen;
  816. if (!sock)
  817. return NULL;
  818. clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  819. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  820. if (err < 0) {
  821. if (err != -EAGAIN)
  822. trace_svcsock_accept_err(xprt, serv->sv_name, err);
  823. return NULL;
  824. }
  825. if (IS_ERR(sock_alloc_file(newsock, O_NONBLOCK, NULL)))
  826. return NULL;
  827. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  828. err = kernel_getpeername(newsock, sin);
  829. if (err < 0) {
  830. trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
  831. goto failed; /* aborted connection or whatever */
  832. }
  833. slen = err;
  834. /* Reset the inherited callbacks before calling svc_setup_socket */
  835. newsock->sk->sk_state_change = svsk->sk_ostate;
  836. newsock->sk->sk_data_ready = svsk->sk_odata;
  837. newsock->sk->sk_write_space = svsk->sk_owspace;
  838. /* make sure that a write doesn't block forever when
  839. * low on memory
  840. */
  841. newsock->sk->sk_sndtimeo = HZ*30;
  842. newsvsk = svc_setup_socket(serv, newsock,
  843. (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
  844. if (IS_ERR(newsvsk))
  845. goto failed;
  846. svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
  847. err = kernel_getsockname(newsock, sin);
  848. slen = err;
  849. if (unlikely(err < 0))
  850. slen = offsetof(struct sockaddr, sa_data);
  851. svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
  852. if (sock_is_loopback(newsock->sk))
  853. set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
  854. else
  855. clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
  856. if (serv->sv_stats)
  857. serv->sv_stats->nettcpconn++;
  858. return &newsvsk->sk_xprt;
  859. failed:
  860. sockfd_put(newsock);
  861. return NULL;
  862. }
  863. static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
  864. struct svc_rqst *rqstp)
  865. {
  866. size_t len = svsk->sk_datalen;
  867. unsigned int i, npages;
  868. if (!len)
  869. return 0;
  870. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  871. for (i = 0; i < npages; i++) {
  872. if (rqstp->rq_pages[i] != NULL)
  873. put_page(rqstp->rq_pages[i]);
  874. BUG_ON(svsk->sk_pages[i] == NULL);
  875. rqstp->rq_pages[i] = svsk->sk_pages[i];
  876. svsk->sk_pages[i] = NULL;
  877. }
  878. rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
  879. return len;
  880. }
  881. static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
  882. {
  883. unsigned int i, len, npages;
  884. if (svsk->sk_datalen == 0)
  885. return;
  886. len = svsk->sk_datalen;
  887. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  888. for (i = 0; i < npages; i++) {
  889. svsk->sk_pages[i] = rqstp->rq_pages[i];
  890. rqstp->rq_pages[i] = NULL;
  891. }
  892. }
  893. static void svc_tcp_clear_pages(struct svc_sock *svsk)
  894. {
  895. unsigned int i, len, npages;
  896. if (svsk->sk_datalen == 0)
  897. goto out;
  898. len = svsk->sk_datalen;
  899. npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  900. for (i = 0; i < npages; i++) {
  901. if (svsk->sk_pages[i] == NULL) {
  902. WARN_ON_ONCE(1);
  903. continue;
  904. }
  905. put_page(svsk->sk_pages[i]);
  906. svsk->sk_pages[i] = NULL;
  907. }
  908. out:
  909. svsk->sk_tcplen = 0;
  910. svsk->sk_datalen = 0;
  911. }
  912. /*
  913. * Receive fragment record header into sk_marker.
  914. */
  915. static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
  916. struct svc_rqst *rqstp)
  917. {
  918. ssize_t want, len;
  919. /* If we haven't gotten the record length yet,
  920. * get the next four bytes.
  921. */
  922. if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
  923. struct msghdr msg = { NULL };
  924. struct kvec iov;
  925. want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
  926. iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
  927. iov.iov_len = want;
  928. iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
  929. len = svc_tcp_sock_recvmsg(svsk, &msg);
  930. if (len < 0)
  931. return len;
  932. svsk->sk_tcplen += len;
  933. if (len < want) {
  934. /* call again to read the remaining bytes */
  935. goto err_short;
  936. }
  937. trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
  938. if (svc_sock_reclen(svsk) + svsk->sk_datalen >
  939. svsk->sk_xprt.xpt_server->sv_max_mesg)
  940. goto err_too_large;
  941. }
  942. return svc_sock_reclen(svsk);
  943. err_too_large:
  944. net_notice_ratelimited("svc: %s oversized RPC fragment (%u octets) from %pISpc\n",
  945. svsk->sk_xprt.xpt_server->sv_name,
  946. svc_sock_reclen(svsk),
  947. (struct sockaddr *)&svsk->sk_xprt.xpt_remote);
  948. svc_xprt_deferred_close(&svsk->sk_xprt);
  949. err_short:
  950. return -EAGAIN;
  951. }
  952. static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
  953. {
  954. struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
  955. struct rpc_rqst *req = NULL;
  956. struct kvec *src, *dst;
  957. __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  958. __be32 xid = *p;
  959. if (!bc_xprt)
  960. return -EAGAIN;
  961. spin_lock(&bc_xprt->queue_lock);
  962. req = xprt_lookup_rqst(bc_xprt, xid);
  963. if (!req)
  964. goto unlock_eagain;
  965. memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
  966. /*
  967. * XXX!: cheating for now! Only copying HEAD.
  968. * But we know this is good enough for now (in fact, for any
  969. * callback reply in the forseeable future).
  970. */
  971. dst = &req->rq_private_buf.head[0];
  972. src = &rqstp->rq_arg.head[0];
  973. if (dst->iov_len < src->iov_len)
  974. goto unlock_eagain; /* whatever; just giving up. */
  975. memcpy(dst->iov_base, src->iov_base, src->iov_len);
  976. xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
  977. rqstp->rq_arg.len = 0;
  978. spin_unlock(&bc_xprt->queue_lock);
  979. return 0;
  980. unlock_eagain:
  981. spin_unlock(&bc_xprt->queue_lock);
  982. return -EAGAIN;
  983. }
  984. static void svc_tcp_fragment_received(struct svc_sock *svsk)
  985. {
  986. /* If we have more data, signal svc_xprt_enqueue() to try again */
  987. svsk->sk_tcplen = 0;
  988. svsk->sk_marker = xdr_zero;
  989. }
  990. /**
  991. * svc_tcp_recvfrom - Receive data from a TCP socket
  992. * @rqstp: request structure into which to receive an RPC Call
  993. *
  994. * Called in a loop when XPT_DATA has been set.
  995. *
  996. * Read the 4-byte stream record marker, then use the record length
  997. * in that marker to set up exactly the resources needed to receive
  998. * the next RPC message into @rqstp.
  999. *
  1000. * Returns:
  1001. * On success, the number of bytes in a received RPC Call, or
  1002. * %0 if a complete RPC Call message was not ready to return
  1003. *
  1004. * The zero return case handles partial receives and callback Replies.
  1005. * The state of a partial receive is preserved in the svc_sock for
  1006. * the next call to svc_tcp_recvfrom.
  1007. */
  1008. static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
  1009. {
  1010. struct svc_sock *svsk =
  1011. container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
  1012. struct svc_serv *serv = svsk->sk_xprt.xpt_server;
  1013. size_t want, base;
  1014. ssize_t len;
  1015. __be32 *p;
  1016. __be32 calldir;
  1017. clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1018. len = svc_tcp_read_marker(svsk, rqstp);
  1019. if (len < 0)
  1020. goto error;
  1021. base = svc_tcp_restore_pages(svsk, rqstp);
  1022. want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
  1023. len = svc_tcp_read_msg(rqstp, base + want, base);
  1024. if (len >= 0) {
  1025. trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
  1026. svsk->sk_tcplen += len;
  1027. svsk->sk_datalen += len;
  1028. }
  1029. if (len != want || !svc_sock_final_rec(svsk))
  1030. goto err_incomplete;
  1031. if (svsk->sk_datalen < 8)
  1032. goto err_nuts;
  1033. rqstp->rq_arg.len = svsk->sk_datalen;
  1034. rqstp->rq_arg.page_base = 0;
  1035. if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
  1036. rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
  1037. rqstp->rq_arg.page_len = 0;
  1038. } else
  1039. rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1040. rqstp->rq_xprt_ctxt = NULL;
  1041. rqstp->rq_prot = IPPROTO_TCP;
  1042. if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
  1043. set_bit(RQ_LOCAL, &rqstp->rq_flags);
  1044. else
  1045. clear_bit(RQ_LOCAL, &rqstp->rq_flags);
  1046. p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  1047. calldir = p[1];
  1048. if (calldir)
  1049. len = receive_cb_reply(svsk, rqstp);
  1050. /* Reset TCP read info */
  1051. svsk->sk_datalen = 0;
  1052. svc_tcp_fragment_received(svsk);
  1053. if (len < 0)
  1054. goto error;
  1055. svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
  1056. if (serv->sv_stats)
  1057. serv->sv_stats->nettcpcnt++;
  1058. svc_sock_secure_port(rqstp);
  1059. svc_xprt_received(rqstp->rq_xprt);
  1060. return rqstp->rq_arg.len;
  1061. err_incomplete:
  1062. svc_tcp_save_pages(svsk, rqstp);
  1063. if (len < 0 && len != -EAGAIN)
  1064. goto err_delete;
  1065. if (len == want)
  1066. svc_tcp_fragment_received(svsk);
  1067. else
  1068. trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
  1069. svc_sock_reclen(svsk),
  1070. svsk->sk_tcplen - sizeof(rpc_fraghdr));
  1071. goto err_noclose;
  1072. error:
  1073. if (len != -EAGAIN)
  1074. goto err_delete;
  1075. trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
  1076. goto err_noclose;
  1077. err_nuts:
  1078. svsk->sk_datalen = 0;
  1079. err_delete:
  1080. trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
  1081. svc_xprt_deferred_close(&svsk->sk_xprt);
  1082. err_noclose:
  1083. svc_xprt_received(rqstp->rq_xprt);
  1084. return 0; /* record not complete */
  1085. }
  1086. /*
  1087. * MSG_SPLICE_PAGES is used exclusively to reduce the number of
  1088. * copy operations in this path. Therefore the caller must ensure
  1089. * that the pages backing @xdr are unchanging.
  1090. */
  1091. static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp,
  1092. rpc_fraghdr marker)
  1093. {
  1094. struct msghdr msg = {
  1095. .msg_flags = MSG_SPLICE_PAGES,
  1096. };
  1097. unsigned int count;
  1098. void *buf;
  1099. int ret;
  1100. /* The stream record marker is copied into a temporary page
  1101. * fragment buffer so that it can be included in sk_bvec.
  1102. */
  1103. buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker),
  1104. GFP_KERNEL);
  1105. if (!buf)
  1106. return -ENOMEM;
  1107. memcpy(buf, &marker, sizeof(marker));
  1108. bvec_set_virt(svsk->sk_bvec, buf, sizeof(marker));
  1109. count = xdr_buf_to_bvec(svsk->sk_bvec + 1, rqstp->rq_maxpages,
  1110. &rqstp->rq_res);
  1111. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
  1112. 1 + count, sizeof(marker) + rqstp->rq_res.len);
  1113. ret = sock_sendmsg(svsk->sk_sock, &msg);
  1114. page_frag_free(buf);
  1115. return ret;
  1116. }
  1117. /**
  1118. * svc_tcp_sendto - Send out a reply on a TCP socket
  1119. * @rqstp: completed svc_rqst
  1120. *
  1121. * xpt_mutex ensures @rqstp's whole message is written to the socket
  1122. * without interruption.
  1123. *
  1124. * Returns the number of bytes sent, or a negative errno.
  1125. */
  1126. static int svc_tcp_sendto(struct svc_rqst *rqstp)
  1127. {
  1128. struct svc_xprt *xprt = rqstp->rq_xprt;
  1129. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1130. struct xdr_buf *xdr = &rqstp->rq_res;
  1131. rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
  1132. (u32)xdr->len);
  1133. int sent;
  1134. svc_tcp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
  1135. rqstp->rq_xprt_ctxt = NULL;
  1136. mutex_lock(&xprt->xpt_mutex);
  1137. if (svc_xprt_is_dead(xprt))
  1138. goto out_notconn;
  1139. sent = svc_tcp_sendmsg(svsk, rqstp, marker);
  1140. trace_svcsock_tcp_send(xprt, sent);
  1141. if (sent < 0 || sent != (xdr->len + sizeof(marker)))
  1142. goto out_close;
  1143. mutex_unlock(&xprt->xpt_mutex);
  1144. return sent;
  1145. out_notconn:
  1146. mutex_unlock(&xprt->xpt_mutex);
  1147. return -ENOTCONN;
  1148. out_close:
  1149. pr_notice("rpc-srv/tcp: %s: %s %d when sending %zu bytes - shutting down socket\n",
  1150. xprt->xpt_server->sv_name,
  1151. (sent < 0) ? "got error" : "sent",
  1152. sent, xdr->len + sizeof(marker));
  1153. svc_xprt_deferred_close(xprt);
  1154. mutex_unlock(&xprt->xpt_mutex);
  1155. return -EAGAIN;
  1156. }
  1157. static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
  1158. struct net *net,
  1159. struct sockaddr *sa, int salen,
  1160. int flags)
  1161. {
  1162. return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
  1163. }
  1164. static const struct svc_xprt_ops svc_tcp_ops = {
  1165. .xpo_create = svc_tcp_create,
  1166. .xpo_recvfrom = svc_tcp_recvfrom,
  1167. .xpo_sendto = svc_tcp_sendto,
  1168. .xpo_result_payload = svc_sock_result_payload,
  1169. .xpo_release_ctxt = svc_tcp_release_ctxt,
  1170. .xpo_detach = svc_tcp_sock_detach,
  1171. .xpo_free = svc_sock_free,
  1172. .xpo_has_wspace = svc_tcp_has_wspace,
  1173. .xpo_accept = svc_tcp_accept,
  1174. .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
  1175. .xpo_handshake = svc_tcp_handshake,
  1176. };
  1177. static struct svc_xprt_class svc_tcp_class = {
  1178. .xcl_name = "tcp",
  1179. .xcl_owner = THIS_MODULE,
  1180. .xcl_ops = &svc_tcp_ops,
  1181. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  1182. .xcl_ident = XPRT_TRANSPORT_TCP,
  1183. };
  1184. void svc_init_xprt_sock(void)
  1185. {
  1186. svc_reg_xprt_class(&svc_tcp_class);
  1187. svc_reg_xprt_class(&svc_udp_class);
  1188. }
  1189. void svc_cleanup_xprt_sock(void)
  1190. {
  1191. svc_unreg_xprt_class(&svc_tcp_class);
  1192. svc_unreg_xprt_class(&svc_udp_class);
  1193. }
  1194. static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
  1195. {
  1196. struct sock *sk = svsk->sk_sk;
  1197. svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
  1198. &svsk->sk_xprt, serv);
  1199. set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
  1200. set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
  1201. if (sk->sk_state == TCP_LISTEN) {
  1202. strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
  1203. set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
  1204. set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
  1205. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1206. set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
  1207. } else {
  1208. sk->sk_state_change = svc_tcp_state_change;
  1209. sk->sk_data_ready = svc_data_ready;
  1210. sk->sk_write_space = svc_write_space;
  1211. svsk->sk_marker = xdr_zero;
  1212. svsk->sk_tcplen = 0;
  1213. svsk->sk_datalen = 0;
  1214. memset(&svsk->sk_pages[0], 0,
  1215. svsk->sk_maxpages * sizeof(struct page *));
  1216. tcp_sock_set_nodelay(sk);
  1217. set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
  1218. switch (sk->sk_state) {
  1219. case TCP_SYN_RECV:
  1220. case TCP_ESTABLISHED:
  1221. break;
  1222. default:
  1223. svc_xprt_deferred_close(&svsk->sk_xprt);
  1224. }
  1225. }
  1226. }
  1227. void svc_sock_update_bufs(struct svc_serv *serv)
  1228. {
  1229. /*
  1230. * The number of server threads has changed. Update
  1231. * rcvbuf and sndbuf accordingly on all sockets
  1232. */
  1233. struct svc_sock *svsk;
  1234. spin_lock_bh(&serv->sv_lock);
  1235. list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
  1236. set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
  1237. spin_unlock_bh(&serv->sv_lock);
  1238. }
  1239. static int svc_sock_sendpages(struct svc_serv *serv, struct socket *sock, int flags)
  1240. {
  1241. switch (sock->type) {
  1242. case SOCK_STREAM:
  1243. /* +1 for TCP record marker */
  1244. if (flags & SVC_SOCK_TEMPORARY)
  1245. return svc_serv_maxpages(serv) + 1;
  1246. return 0;
  1247. case SOCK_DGRAM:
  1248. return SUNRPC_MAX_UDP_SENDPAGES;
  1249. }
  1250. return -EINVAL;
  1251. }
  1252. /*
  1253. * Initialize socket for RPC use and create svc_sock struct
  1254. */
  1255. static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
  1256. struct socket *sock,
  1257. int flags)
  1258. {
  1259. struct svc_sock *svsk;
  1260. struct sock *inet;
  1261. int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
  1262. int sendpages;
  1263. unsigned long pages;
  1264. sendpages = svc_sock_sendpages(serv, sock, flags);
  1265. if (sendpages < 0)
  1266. return ERR_PTR(sendpages);
  1267. pages = svc_serv_maxpages(serv);
  1268. svsk = kzalloc_flex(*svsk, sk_pages, pages);
  1269. if (!svsk)
  1270. return ERR_PTR(-ENOMEM);
  1271. if (sendpages) {
  1272. svsk->sk_bvec = kzalloc_objs(*svsk->sk_bvec, sendpages);
  1273. if (!svsk->sk_bvec) {
  1274. kfree(svsk);
  1275. return ERR_PTR(-ENOMEM);
  1276. }
  1277. }
  1278. svsk->sk_maxpages = pages;
  1279. inet = sock->sk;
  1280. if (pmap_register) {
  1281. int err;
  1282. err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
  1283. inet->sk_protocol,
  1284. ntohs(inet_sk(inet)->inet_sport));
  1285. if (err < 0) {
  1286. kfree(svsk->sk_bvec);
  1287. kfree(svsk);
  1288. return ERR_PTR(err);
  1289. }
  1290. }
  1291. svsk->sk_sock = sock;
  1292. svsk->sk_sk = inet;
  1293. svsk->sk_ostate = inet->sk_state_change;
  1294. svsk->sk_odata = inet->sk_data_ready;
  1295. svsk->sk_owspace = inet->sk_write_space;
  1296. /*
  1297. * This barrier is necessary in order to prevent race condition
  1298. * with svc_data_ready(), svc_tcp_listen_data_ready(), and others
  1299. * when calling callbacks above.
  1300. */
  1301. wmb();
  1302. inet->sk_user_data = svsk;
  1303. /* Initialize the socket */
  1304. if (sock->type == SOCK_DGRAM)
  1305. svc_udp_init(svsk, serv);
  1306. else
  1307. svc_tcp_init(svsk, serv);
  1308. trace_svcsock_new(svsk, sock);
  1309. return svsk;
  1310. }
  1311. /**
  1312. * svc_addsock - add a listener socket to an RPC service
  1313. * @serv: pointer to RPC service to which to add a new listener
  1314. * @net: caller's network namespace
  1315. * @fd: file descriptor of the new listener
  1316. * @name_return: pointer to buffer to fill in with name of listener
  1317. * @len: size of the buffer
  1318. * @cred: credential
  1319. *
  1320. * Fills in socket name and returns positive length of name if successful.
  1321. * Name is terminated with '\n'. On error, returns a negative errno
  1322. * value.
  1323. */
  1324. int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
  1325. char *name_return, const size_t len, const struct cred *cred)
  1326. {
  1327. int err = 0;
  1328. struct socket *so = sockfd_lookup(fd, &err);
  1329. struct svc_sock *svsk = NULL;
  1330. struct sockaddr_storage addr;
  1331. struct sockaddr *sin = (struct sockaddr *)&addr;
  1332. int salen;
  1333. if (!so)
  1334. return err;
  1335. err = -EINVAL;
  1336. if (sock_net(so->sk) != net)
  1337. goto out;
  1338. err = -EAFNOSUPPORT;
  1339. if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
  1340. goto out;
  1341. err = -EPROTONOSUPPORT;
  1342. if (so->sk->sk_protocol != IPPROTO_TCP &&
  1343. so->sk->sk_protocol != IPPROTO_UDP)
  1344. goto out;
  1345. err = -EISCONN;
  1346. if (so->state > SS_UNCONNECTED)
  1347. goto out;
  1348. err = -ENOENT;
  1349. if (!try_module_get(THIS_MODULE))
  1350. goto out;
  1351. svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
  1352. if (IS_ERR(svsk)) {
  1353. module_put(THIS_MODULE);
  1354. err = PTR_ERR(svsk);
  1355. goto out;
  1356. }
  1357. salen = kernel_getsockname(svsk->sk_sock, sin);
  1358. if (salen >= 0)
  1359. svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
  1360. svsk->sk_xprt.xpt_cred = get_cred(cred);
  1361. svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
  1362. return svc_one_sock_name(svsk, name_return, len);
  1363. out:
  1364. sockfd_put(so);
  1365. return err;
  1366. }
  1367. EXPORT_SYMBOL_GPL(svc_addsock);
  1368. /*
  1369. * Create socket for RPC service.
  1370. */
  1371. static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
  1372. int protocol,
  1373. struct net *net,
  1374. struct sockaddr *sin, int len,
  1375. int flags)
  1376. {
  1377. struct svc_sock *svsk;
  1378. struct socket *sock;
  1379. int error;
  1380. int type;
  1381. struct sockaddr_storage addr;
  1382. struct sockaddr *newsin = (struct sockaddr *)&addr;
  1383. int newlen;
  1384. int family;
  1385. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1386. printk(KERN_WARNING "svc: only UDP and TCP "
  1387. "sockets supported\n");
  1388. return ERR_PTR(-EINVAL);
  1389. }
  1390. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1391. switch (sin->sa_family) {
  1392. case AF_INET6:
  1393. family = PF_INET6;
  1394. break;
  1395. case AF_INET:
  1396. family = PF_INET;
  1397. break;
  1398. default:
  1399. return ERR_PTR(-EINVAL);
  1400. }
  1401. error = __sock_create(net, family, type, protocol, &sock, 1);
  1402. if (error < 0)
  1403. return ERR_PTR(error);
  1404. svc_reclassify_socket(sock);
  1405. /*
  1406. * If this is an PF_INET6 listener, we want to avoid
  1407. * getting requests from IPv4 remotes. Those should
  1408. * be shunted to a PF_INET listener via rpcbind.
  1409. */
  1410. if (family == PF_INET6)
  1411. ip6_sock_set_v6only(sock->sk);
  1412. if (type == SOCK_STREAM)
  1413. sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
  1414. error = kernel_bind(sock, (struct sockaddr_unsized *)sin, len);
  1415. if (error < 0)
  1416. goto bummer;
  1417. error = kernel_getsockname(sock, newsin);
  1418. if (error < 0)
  1419. goto bummer;
  1420. newlen = error;
  1421. if (protocol == IPPROTO_TCP) {
  1422. sk_net_refcnt_upgrade(sock->sk);
  1423. if ((error = kernel_listen(sock, SOMAXCONN)) < 0)
  1424. goto bummer;
  1425. }
  1426. svsk = svc_setup_socket(serv, sock, flags);
  1427. if (IS_ERR(svsk)) {
  1428. error = PTR_ERR(svsk);
  1429. goto bummer;
  1430. }
  1431. svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
  1432. return (struct svc_xprt *)svsk;
  1433. bummer:
  1434. sock_release(sock);
  1435. return ERR_PTR(error);
  1436. }
  1437. /*
  1438. * Detach the svc_sock from the socket so that no
  1439. * more callbacks occur.
  1440. */
  1441. static void svc_sock_detach(struct svc_xprt *xprt)
  1442. {
  1443. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1444. struct sock *sk = svsk->sk_sk;
  1445. /* put back the old socket callbacks */
  1446. lock_sock(sk);
  1447. sk->sk_state_change = svsk->sk_ostate;
  1448. sk->sk_data_ready = svsk->sk_odata;
  1449. sk->sk_write_space = svsk->sk_owspace;
  1450. sk->sk_user_data = NULL;
  1451. release_sock(sk);
  1452. }
  1453. /*
  1454. * Disconnect the socket, and reset the callbacks
  1455. */
  1456. static void svc_tcp_sock_detach(struct svc_xprt *xprt)
  1457. {
  1458. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1459. tls_handshake_close(svsk->sk_sock);
  1460. svc_sock_detach(xprt);
  1461. if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  1462. svc_tcp_clear_pages(svsk);
  1463. kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
  1464. }
  1465. }
  1466. /*
  1467. * Free the svc_sock's socket resources and the svc_sock itself.
  1468. */
  1469. static void svc_sock_free(struct svc_xprt *xprt)
  1470. {
  1471. struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
  1472. struct socket *sock = svsk->sk_sock;
  1473. trace_svcsock_free(svsk, sock);
  1474. tls_handshake_cancel(sock->sk);
  1475. if (sock->file)
  1476. sockfd_put(sock);
  1477. else
  1478. sock_release(sock);
  1479. page_frag_cache_drain(&svsk->sk_frag_cache);
  1480. kfree(svsk->sk_bvec);
  1481. kfree(svsk);
  1482. }