svcsock.c 43 KB

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