svcsock.c 40 KB

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