svcsock.c 35 KB

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