svcsock.c 43 KB

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