svcsock.c 42 KB

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