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

/net/core/datagram.c

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