PageRenderTime 115ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/net/core/datagram.c

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