svcsock.c 37 KB

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