svcsock.c 43 KB

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