svcsock.c 42 KB

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