PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/net/core/datagram.c

https://bitbucket.org/danhamilt1/linux
C | 787 lines | 511 code | 92 blank | 184 comment | 117 complexity | a186d62ef1c508b80409222bd15105bb MD5 | raw file
  1. /*
  2. * SUCS NET3:
  3. *
  4. * Generic datagram handling routines. These are generic for all
  5. * protocols. Possibly a generic IP version on top of these would
  6. * make sense. Not tonight however 8-).
  7. * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
  8. * NetROM layer all have identical poll code and mostly
  9. * identical recvmsg() code. So we share it here. The poll was
  10. * shared before but buried in udp.c so I moved it.
  11. *
  12. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
  13. * udp.c code)
  14. *
  15. * Fixes:
  16. * Alan Cox : NULL return from skb_peek_copy()
  17. * understood
  18. * Alan Cox : Rewrote skb_read_datagram to avoid the
  19. * skb_peek_copy stuff.
  20. * Alan Cox : Added support for SOCK_SEQPACKET.
  21. * IPX can no longer use the SO_TYPE hack
  22. * but AX.25 now works right, and SPX is
  23. * feasible.
  24. * Alan Cox : Fixed write poll of non IP protocol
  25. * crash.
  26. * Florian La Roche: Changed for my new skbuff handling.
  27. * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET.
  28. * Linus Torvalds : BSD semantic fixes.
  29. * Alan Cox : Datagram iovec handling
  30. * Darryl Miles : Fixed non-blocking SOCK_STREAM.
  31. * Alan Cox : POSIXisms
  32. * Pete Wyckoff : Unconnected accept() fix.
  33. *
  34. */
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/kernel.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/mm.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/errno.h>
  42. #include <linux/sched.h>
  43. #include <linux/inet.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/rtnetlink.h>
  46. #include <linux/poll.h>
  47. #include <linux/highmem.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/slab.h>
  50. #include <net/protocol.h>
  51. #include <linux/skbuff.h>
  52. #include <net/checksum.h>
  53. #include <net/sock.h>
  54. #include <net/tcp_states.h>
  55. #include <trace/events/skb.h>
  56. /*
  57. * Is a socket 'connection oriented' ?
  58. */
  59. static inline int connection_based(struct sock *sk)
  60. {
  61. return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
  62. }
  63. static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
  64. void *key)
  65. {
  66. unsigned long bits = (unsigned long)key;
  67. /*
  68. * Avoid a wakeup if event not interesting for us
  69. */
  70. if (bits && !(bits & (POLLIN | POLLERR)))
  71. return 0;
  72. return autoremove_wake_function(wait, mode, sync, key);
  73. }
  74. /*
  75. * Wait for the last received packet to be different from skb
  76. */
  77. static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
  78. const struct sk_buff *skb)
  79. {
  80. int error;
  81. DEFINE_WAIT_FUNC(wait, receiver_wake_function);
  82. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  83. /* Socket errors? */
  84. error = sock_error(sk);
  85. if (error)
  86. goto out_err;
  87. if (sk->sk_receive_queue.prev != skb)
  88. goto out;
  89. /* Socket shut down? */
  90. if (sk->sk_shutdown & RCV_SHUTDOWN)
  91. goto out_noerr;
  92. /* Sequenced packets can come disconnected.
  93. * If so we report the problem
  94. */
  95. error = -ENOTCONN;
  96. if (connection_based(sk) &&
  97. !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
  98. goto out_err;
  99. /* handle signals */
  100. if (signal_pending(current))
  101. goto interrupted;
  102. error = 0;
  103. *timeo_p = schedule_timeout(*timeo_p);
  104. out:
  105. finish_wait(sk_sleep(sk), &wait);
  106. return error;
  107. interrupted:
  108. error = sock_intr_errno(*timeo_p);
  109. out_err:
  110. *err = error;
  111. goto out;
  112. out_noerr:
  113. *err = 0;
  114. error = 1;
  115. goto out;
  116. }
  117. /**
  118. * __skb_recv_datagram - Receive a datagram skbuff
  119. * @sk: socket
  120. * @flags: MSG_ flags
  121. * @peeked: returns non-zero if this packet has been seen before
  122. * @off: an offset in bytes to peek skb from. Returns an offset
  123. * within an skb where data actually starts
  124. * @err: error code returned
  125. *
  126. * Get a datagram skbuff, understands the peeking, nonblocking wakeups
  127. * and possible races. This replaces identical code in packet, raw and
  128. * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
  129. * the long standing peek and read race for datagram sockets. If you
  130. * alter this routine remember it must be re-entrant.
  131. *
  132. * This function will lock the socket if a skb is returned, so the caller
  133. * needs to unlock the socket in that case (usually by calling
  134. * skb_free_datagram)
  135. *
  136. * * It does not lock socket since today. This function is
  137. * * free of race conditions. This measure should/can improve
  138. * * significantly datagram socket latencies at high loads,
  139. * * when data copying to user space takes lots of time.
  140. * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
  141. * * 8) Great win.)
  142. * * --ANK (980729)
  143. *
  144. * The order of the tests when we find no data waiting are specified
  145. * quite explicitly by POSIX 1003.1g, don't change them without having
  146. * the standard around please.
  147. */
  148. struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
  149. int *peeked, int *off, int *err)
  150. {
  151. struct sk_buff *skb, *last;
  152. long timeo;
  153. /*
  154. * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
  155. */
  156. int error = sock_error(sk);
  157. if (error)
  158. goto no_packet;
  159. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  160. do {
  161. /* Again only user level code calls this function, so nothing
  162. * interrupt level will suddenly eat the receive_queue.
  163. *
  164. * Look at current nfs client by the way...
  165. * However, this function was correct in any case. 8)
  166. */
  167. unsigned long cpu_flags;
  168. struct sk_buff_head *queue = &sk->sk_receive_queue;
  169. int _off = *off;
  170. last = (struct sk_buff *)queue;
  171. spin_lock_irqsave(&queue->lock, cpu_flags);
  172. skb_queue_walk(queue, skb) {
  173. last = skb;
  174. *peeked = skb->peeked;
  175. if (flags & MSG_PEEK) {
  176. if (_off >= skb->len && (skb->len || _off ||
  177. skb->peeked)) {
  178. _off -= skb->len;
  179. continue;
  180. }
  181. skb->peeked = 1;
  182. atomic_inc(&skb->users);
  183. } else
  184. __skb_unlink(skb, queue);
  185. spin_unlock_irqrestore(&queue->lock, cpu_flags);
  186. *off = _off;
  187. return skb;
  188. }
  189. spin_unlock_irqrestore(&queue->lock, cpu_flags);
  190. /* User doesn't want to wait */
  191. error = -EAGAIN;
  192. if (!timeo)
  193. goto no_packet;
  194. } while (!wait_for_more_packets(sk, err, &timeo, last));
  195. return NULL;
  196. no_packet:
  197. *err = error;
  198. return NULL;
  199. }
  200. EXPORT_SYMBOL(__skb_recv_datagram);
  201. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
  202. int noblock, int *err)
  203. {
  204. int peeked, off = 0;
  205. return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  206. &peeked, &off, err);
  207. }
  208. EXPORT_SYMBOL(skb_recv_datagram);
  209. void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
  210. {
  211. consume_skb(skb);
  212. sk_mem_reclaim_partial(sk);
  213. }
  214. EXPORT_SYMBOL(skb_free_datagram);
  215. void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
  216. {
  217. bool slow;
  218. if (likely(atomic_read(&skb->users) == 1))
  219. smp_rmb();
  220. else if (likely(!atomic_dec_and_test(&skb->users)))
  221. return;
  222. slow = lock_sock_fast(sk);
  223. skb_orphan(skb);
  224. sk_mem_reclaim_partial(sk);
  225. unlock_sock_fast(sk, slow);
  226. /* skb is now orphaned, can be freed outside of locked section */
  227. __kfree_skb(skb);
  228. }
  229. EXPORT_SYMBOL(skb_free_datagram_locked);
  230. /**
  231. * skb_kill_datagram - Free a datagram skbuff forcibly
  232. * @sk: socket
  233. * @skb: datagram skbuff
  234. * @flags: MSG_ flags
  235. *
  236. * This function frees a datagram skbuff that was received by
  237. * skb_recv_datagram. The flags argument must match the one
  238. * used for skb_recv_datagram.
  239. *
  240. * If the MSG_PEEK flag is set, and the packet is still on the
  241. * receive queue of the socket, it will be taken off the queue
  242. * before it is freed.
  243. *
  244. * This function currently only disables BH when acquiring the
  245. * sk_receive_queue lock. Therefore it must not be used in a
  246. * context where that lock is acquired in an IRQ context.
  247. *
  248. * It returns 0 if the packet was removed by us.
  249. */
  250. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  251. {
  252. int err = 0;
  253. if (flags & MSG_PEEK) {
  254. err = -ENOENT;
  255. spin_lock_bh(&sk->sk_receive_queue.lock);
  256. if (skb == skb_peek(&sk->sk_receive_queue)) {
  257. __skb_unlink(skb, &sk->sk_receive_queue);
  258. atomic_dec(&skb->users);
  259. err = 0;
  260. }
  261. spin_unlock_bh(&sk->sk_receive_queue.lock);
  262. }
  263. kfree_skb(skb);
  264. atomic_inc(&sk->sk_drops);
  265. sk_mem_reclaim_partial(sk);
  266. return err;
  267. }
  268. EXPORT_SYMBOL(skb_kill_datagram);
  269. /**
  270. * skb_copy_datagram_iovec - Copy a datagram to an iovec.
  271. * @skb: buffer to copy
  272. * @offset: offset in the buffer to start copying from
  273. * @to: io vector to copy to
  274. * @len: amount of data to copy from buffer to iovec
  275. *
  276. * Note: the iovec is modified during the copy.
  277. */
  278. int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
  279. struct iovec *to, int len)
  280. {
  281. int start = skb_headlen(skb);
  282. int i, copy = start - offset;
  283. struct sk_buff *frag_iter;
  284. trace_skb_copy_datagram_iovec(skb, len);
  285. /* Copy header. */
  286. if (copy > 0) {
  287. if (copy > len)
  288. copy = len;
  289. if (memcpy_toiovec(to, skb->data + offset, copy))
  290. goto fault;
  291. if ((len -= copy) == 0)
  292. return 0;
  293. offset += copy;
  294. }
  295. /* Copy paged appendix. Hmm... why does this look so complicated? */
  296. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  297. int end;
  298. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  299. WARN_ON(start > offset + len);
  300. end = start + skb_frag_size(frag);
  301. if ((copy = end - offset) > 0) {
  302. int err;
  303. u8 *vaddr;
  304. struct page *page = skb_frag_page(frag);
  305. if (copy > len)
  306. copy = len;
  307. vaddr = kmap(page);
  308. err = memcpy_toiovec(to, vaddr + frag->page_offset +
  309. offset - start, copy);
  310. kunmap(page);
  311. if (err)
  312. goto fault;
  313. if (!(len -= copy))
  314. return 0;
  315. offset += copy;
  316. }
  317. start = end;
  318. }
  319. skb_walk_frags(skb, frag_iter) {
  320. int end;
  321. WARN_ON(start > offset + len);
  322. end = start + frag_iter->len;
  323. if ((copy = end - offset) > 0) {
  324. if (copy > len)
  325. copy = len;
  326. if (skb_copy_datagram_iovec(frag_iter,
  327. offset - start,
  328. to, copy))
  329. goto fault;
  330. if ((len -= copy) == 0)
  331. return 0;
  332. offset += copy;
  333. }
  334. start = end;
  335. }
  336. if (!len)
  337. return 0;
  338. fault:
  339. return -EFAULT;
  340. }
  341. EXPORT_SYMBOL(skb_copy_datagram_iovec);
  342. /**
  343. * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
  344. * @skb: buffer to copy
  345. * @offset: offset in the buffer to start copying from
  346. * @to: io vector to copy to
  347. * @to_offset: offset in the io vector to start copying to
  348. * @len: amount of data to copy from buffer to iovec
  349. *
  350. * Returns 0 or -EFAULT.
  351. * Note: the iovec is not modified during the copy.
  352. */
  353. int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
  354. const struct iovec *to, int to_offset,
  355. int len)
  356. {
  357. int start = skb_headlen(skb);
  358. int i, copy = start - offset;
  359. struct sk_buff *frag_iter;
  360. /* Copy header. */
  361. if (copy > 0) {
  362. if (copy > len)
  363. copy = len;
  364. if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
  365. goto fault;
  366. if ((len -= copy) == 0)
  367. return 0;
  368. offset += copy;
  369. to_offset += copy;
  370. }
  371. /* Copy paged appendix. Hmm... why does this look so complicated? */
  372. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  373. int end;
  374. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  375. WARN_ON(start > offset + len);
  376. end = start + skb_frag_size(frag);
  377. if ((copy = end - offset) > 0) {
  378. int err;
  379. u8 *vaddr;
  380. struct page *page = skb_frag_page(frag);
  381. if (copy > len)
  382. copy = len;
  383. vaddr = kmap(page);
  384. err = memcpy_toiovecend(to, vaddr + frag->page_offset +
  385. offset - start, to_offset, copy);
  386. kunmap(page);
  387. if (err)
  388. goto fault;
  389. if (!(len -= copy))
  390. return 0;
  391. offset += copy;
  392. to_offset += copy;
  393. }
  394. start = end;
  395. }
  396. skb_walk_frags(skb, frag_iter) {
  397. int end;
  398. WARN_ON(start > offset + len);
  399. end = start + frag_iter->len;
  400. if ((copy = end - offset) > 0) {
  401. if (copy > len)
  402. copy = len;
  403. if (skb_copy_datagram_const_iovec(frag_iter,
  404. offset - start,
  405. to, to_offset,
  406. copy))
  407. goto fault;
  408. if ((len -= copy) == 0)
  409. return 0;
  410. offset += copy;
  411. to_offset += copy;
  412. }
  413. start = end;
  414. }
  415. if (!len)
  416. return 0;
  417. fault:
  418. return -EFAULT;
  419. }
  420. EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
  421. /**
  422. * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
  423. * @skb: buffer to copy
  424. * @offset: offset in the buffer to start copying to
  425. * @from: io vector to copy to
  426. * @from_offset: offset in the io vector to start copying from
  427. * @len: amount of data to copy to buffer from iovec
  428. *
  429. * Returns 0 or -EFAULT.
  430. * Note: the iovec is not modified during the copy.
  431. */
  432. int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
  433. const struct iovec *from, int from_offset,
  434. int len)
  435. {
  436. int start = skb_headlen(skb);
  437. int i, copy = start - offset;
  438. struct sk_buff *frag_iter;
  439. /* Copy header. */
  440. if (copy > 0) {
  441. if (copy > len)
  442. copy = len;
  443. if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
  444. copy))
  445. goto fault;
  446. if ((len -= copy) == 0)
  447. return 0;
  448. offset += copy;
  449. from_offset += copy;
  450. }
  451. /* Copy paged appendix. Hmm... why does this look so complicated? */
  452. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  453. int end;
  454. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  455. WARN_ON(start > offset + len);
  456. end = start + skb_frag_size(frag);
  457. if ((copy = end - offset) > 0) {
  458. int err;
  459. u8 *vaddr;
  460. struct page *page = skb_frag_page(frag);
  461. if (copy > len)
  462. copy = len;
  463. vaddr = kmap(page);
  464. err = memcpy_fromiovecend(vaddr + frag->page_offset +
  465. offset - start,
  466. from, from_offset, copy);
  467. kunmap(page);
  468. if (err)
  469. goto fault;
  470. if (!(len -= copy))
  471. return 0;
  472. offset += copy;
  473. from_offset += copy;
  474. }
  475. start = end;
  476. }
  477. skb_walk_frags(skb, frag_iter) {
  478. int end;
  479. WARN_ON(start > offset + len);
  480. end = start + frag_iter->len;
  481. if ((copy = end - offset) > 0) {
  482. if (copy > len)
  483. copy = len;
  484. if (skb_copy_datagram_from_iovec(frag_iter,
  485. offset - start,
  486. from,
  487. from_offset,
  488. copy))
  489. goto fault;
  490. if ((len -= copy) == 0)
  491. return 0;
  492. offset += copy;
  493. from_offset += copy;
  494. }
  495. start = end;
  496. }
  497. if (!len)
  498. return 0;
  499. fault:
  500. return -EFAULT;
  501. }
  502. EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
  503. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  504. u8 __user *to, int len,
  505. __wsum *csump)
  506. {
  507. int start = skb_headlen(skb);
  508. int i, copy = start - offset;
  509. struct sk_buff *frag_iter;
  510. int pos = 0;
  511. /* Copy header. */
  512. if (copy > 0) {
  513. int err = 0;
  514. if (copy > len)
  515. copy = len;
  516. *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
  517. *csump, &err);
  518. if (err)
  519. goto fault;
  520. if ((len -= copy) == 0)
  521. return 0;
  522. offset += copy;
  523. to += copy;
  524. pos = copy;
  525. }
  526. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  527. int end;
  528. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  529. WARN_ON(start > offset + len);
  530. end = start + skb_frag_size(frag);
  531. if ((copy = end - offset) > 0) {
  532. __wsum csum2;
  533. int err = 0;
  534. u8 *vaddr;
  535. struct page *page = skb_frag_page(frag);
  536. if (copy > len)
  537. copy = len;
  538. vaddr = kmap(page);
  539. csum2 = csum_and_copy_to_user(vaddr +
  540. frag->page_offset +
  541. offset - start,
  542. to, copy, 0, &err);
  543. kunmap(page);
  544. if (err)
  545. goto fault;
  546. *csump = csum_block_add(*csump, csum2, pos);
  547. if (!(len -= copy))
  548. return 0;
  549. offset += copy;
  550. to += copy;
  551. pos += copy;
  552. }
  553. start = end;
  554. }
  555. skb_walk_frags(skb, frag_iter) {
  556. int end;
  557. WARN_ON(start > offset + len);
  558. end = start + frag_iter->len;
  559. if ((copy = end - offset) > 0) {
  560. __wsum csum2 = 0;
  561. if (copy > len)
  562. copy = len;
  563. if (skb_copy_and_csum_datagram(frag_iter,
  564. offset - start,
  565. to, copy,
  566. &csum2))
  567. goto fault;
  568. *csump = csum_block_add(*csump, csum2, pos);
  569. if ((len -= copy) == 0)
  570. return 0;
  571. offset += copy;
  572. to += copy;
  573. pos += copy;
  574. }
  575. start = end;
  576. }
  577. if (!len)
  578. return 0;
  579. fault:
  580. return -EFAULT;
  581. }
  582. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  583. {
  584. __sum16 sum;
  585. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  586. if (likely(!sum)) {
  587. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  588. netdev_rx_csum_fault(skb->dev);
  589. skb->ip_summed = CHECKSUM_UNNECESSARY;
  590. }
  591. return sum;
  592. }
  593. EXPORT_SYMBOL(__skb_checksum_complete_head);
  594. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  595. {
  596. return __skb_checksum_complete_head(skb, skb->len);
  597. }
  598. EXPORT_SYMBOL(__skb_checksum_complete);
  599. /**
  600. * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
  601. * @skb: skbuff
  602. * @hlen: hardware length
  603. * @iov: io vector
  604. *
  605. * Caller _must_ check that skb will fit to this iovec.
  606. *
  607. * Returns: 0 - success.
  608. * -EINVAL - checksum failure.
  609. * -EFAULT - fault during copy. Beware, in this case iovec
  610. * can be modified!
  611. */
  612. int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  613. int hlen, struct iovec *iov)
  614. {
  615. __wsum csum;
  616. int chunk = skb->len - hlen;
  617. if (!chunk)
  618. return 0;
  619. /* Skip filled elements.
  620. * Pretty silly, look at memcpy_toiovec, though 8)
  621. */
  622. while (!iov->iov_len)
  623. iov++;
  624. if (iov->iov_len < chunk) {
  625. if (__skb_checksum_complete(skb))
  626. goto csum_error;
  627. if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
  628. goto fault;
  629. } else {
  630. csum = csum_partial(skb->data, hlen, skb->csum);
  631. if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
  632. chunk, &csum))
  633. goto fault;
  634. if (csum_fold(csum))
  635. goto csum_error;
  636. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  637. netdev_rx_csum_fault(skb->dev);
  638. iov->iov_len -= chunk;
  639. iov->iov_base += chunk;
  640. }
  641. return 0;
  642. csum_error:
  643. return -EINVAL;
  644. fault:
  645. return -EFAULT;
  646. }
  647. EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
  648. /**
  649. * datagram_poll - generic datagram poll
  650. * @file: file struct
  651. * @sock: socket
  652. * @wait: poll table
  653. *
  654. * Datagram poll: Again totally generic. This also handles
  655. * sequenced packet sockets providing the socket receive queue
  656. * is only ever holding data ready to receive.
  657. *
  658. * Note: when you _don't_ use this routine for this protocol,
  659. * and you use a different write policy from sock_writeable()
  660. * then please supply your own write_space callback.
  661. */
  662. unsigned int datagram_poll(struct file *file, struct socket *sock,
  663. poll_table *wait)
  664. {
  665. struct sock *sk = sock->sk;
  666. unsigned int mask;
  667. sock_poll_wait(file, sk_sleep(sk), wait);
  668. mask = 0;
  669. /* exceptional events? */
  670. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  671. mask |= POLLERR |
  672. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  673. if (sk->sk_shutdown & RCV_SHUTDOWN)
  674. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  675. if (sk->sk_shutdown == SHUTDOWN_MASK)
  676. mask |= POLLHUP;
  677. /* readable? */
  678. if (!skb_queue_empty(&sk->sk_receive_queue))
  679. mask |= POLLIN | POLLRDNORM;
  680. /* Connection-based need to check for termination and startup */
  681. if (connection_based(sk)) {
  682. if (sk->sk_state == TCP_CLOSE)
  683. mask |= POLLHUP;
  684. /* connection hasn't started yet? */
  685. if (sk->sk_state == TCP_SYN_SENT)
  686. return mask;
  687. }
  688. /* writable? */
  689. if (sock_writeable(sk))
  690. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  691. else
  692. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  693. return mask;
  694. }
  695. EXPORT_SYMBOL(datagram_poll);