svcsock.c 35 KB

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