svcsock.c 35 KB

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