svcsock.c 40 KB

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