svcsock.c 35 KB

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