svcsock.c 42 KB

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