PageRenderTime 440ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/net/core/datagram.c

https://bitbucket.org/codefirex/kernel_samsung_jf
C | 780 lines | 505 code | 91 blank | 184 comment | 114 complexity | c58b2112eb6330cceb561bb6873ff8e0 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 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 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) {
  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 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. trace_kfree_skb(skb, skb_free_datagram_locked);
  222. __kfree_skb(skb);
  223. }
  224. EXPORT_SYMBOL(skb_free_datagram_locked);
  225. /**
  226. * skb_kill_datagram - Free a datagram skbuff forcibly
  227. * @sk: socket
  228. * @skb: datagram skbuff
  229. * @flags: MSG_ flags
  230. *
  231. * This function frees a datagram skbuff that was received by
  232. * skb_recv_datagram. The flags argument must match the one
  233. * used for skb_recv_datagram.
  234. *
  235. * If the MSG_PEEK flag is set, and the packet is still on the
  236. * receive queue of the socket, it will be taken off the queue
  237. * before it is freed.
  238. *
  239. * This function currently only disables BH when acquiring the
  240. * sk_receive_queue lock. Therefore it must not be used in a
  241. * context where that lock is acquired in an IRQ context.
  242. *
  243. * It returns 0 if the packet was removed by us.
  244. */
  245. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  246. {
  247. int err = 0;
  248. if (flags & MSG_PEEK) {
  249. err = -ENOENT;
  250. spin_lock_bh(&sk->sk_receive_queue.lock);
  251. if (skb == skb_peek(&sk->sk_receive_queue)) {
  252. __skb_unlink(skb, &sk->sk_receive_queue);
  253. atomic_dec(&skb->users);
  254. err = 0;
  255. }
  256. spin_unlock_bh(&sk->sk_receive_queue.lock);
  257. }
  258. kfree_skb(skb);
  259. atomic_inc(&sk->sk_drops);
  260. sk_mem_reclaim_partial(sk);
  261. return err;
  262. }
  263. EXPORT_SYMBOL(skb_kill_datagram);
  264. /**
  265. * skb_copy_datagram_iovec - Copy a datagram to an iovec.
  266. * @skb: buffer to copy
  267. * @offset: offset in the buffer to start copying from
  268. * @to: io vector to copy to
  269. * @len: amount of data to copy from buffer to iovec
  270. *
  271. * Note: the iovec is modified during the copy.
  272. */
  273. int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
  274. struct iovec *to, int len)
  275. {
  276. int start = skb_headlen(skb);
  277. int i, copy = start - offset;
  278. struct sk_buff *frag_iter;
  279. trace_skb_copy_datagram_iovec(skb, len);
  280. /* Copy header. */
  281. if (copy > 0) {
  282. if (copy > len)
  283. copy = len;
  284. if (memcpy_toiovec(to, skb->data + offset, copy))
  285. goto fault;
  286. if ((len -= copy) == 0)
  287. return 0;
  288. offset += copy;
  289. }
  290. /* Copy paged appendix. Hmm... why does this look so complicated? */
  291. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  292. int end;
  293. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  294. WARN_ON(start > offset + len);
  295. end = start + skb_frag_size(frag);
  296. if ((copy = end - offset) > 0) {
  297. int err;
  298. u8 *vaddr;
  299. struct page *page = skb_frag_page(frag);
  300. if (copy > len)
  301. copy = len;
  302. vaddr = kmap(page);
  303. err = memcpy_toiovec(to, vaddr + frag->page_offset +
  304. offset - start, copy);
  305. kunmap(page);
  306. if (err)
  307. goto fault;
  308. if (!(len -= copy))
  309. return 0;
  310. offset += copy;
  311. }
  312. start = end;
  313. }
  314. skb_walk_frags(skb, frag_iter) {
  315. int end;
  316. WARN_ON(start > offset + len);
  317. end = start + frag_iter->len;
  318. if ((copy = end - offset) > 0) {
  319. if (copy > len)
  320. copy = len;
  321. if (skb_copy_datagram_iovec(frag_iter,
  322. offset - start,
  323. to, copy))
  324. goto fault;
  325. if ((len -= copy) == 0)
  326. return 0;
  327. offset += copy;
  328. }
  329. start = end;
  330. }
  331. if (!len)
  332. return 0;
  333. fault:
  334. return -EFAULT;
  335. }
  336. EXPORT_SYMBOL(skb_copy_datagram_iovec);
  337. /**
  338. * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
  339. * @skb: buffer to copy
  340. * @offset: offset in the buffer to start copying from
  341. * @to: io vector to copy to
  342. * @to_offset: offset in the io vector to start copying to
  343. * @len: amount of data to copy from buffer to iovec
  344. *
  345. * Returns 0 or -EFAULT.
  346. * Note: the iovec is not modified during the copy.
  347. */
  348. int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
  349. const struct iovec *to, int to_offset,
  350. int len)
  351. {
  352. int start = skb_headlen(skb);
  353. int i, copy = start - offset;
  354. struct sk_buff *frag_iter;
  355. /* Copy header. */
  356. if (copy > 0) {
  357. if (copy > len)
  358. copy = len;
  359. if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
  360. goto fault;
  361. if ((len -= copy) == 0)
  362. return 0;
  363. offset += copy;
  364. to_offset += copy;
  365. }
  366. /* Copy paged appendix. Hmm... why does this look so complicated? */
  367. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  368. int end;
  369. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  370. WARN_ON(start > offset + len);
  371. end = start + skb_frag_size(frag);
  372. if ((copy = end - offset) > 0) {
  373. int err;
  374. u8 *vaddr;
  375. struct page *page = skb_frag_page(frag);
  376. if (copy > len)
  377. copy = len;
  378. vaddr = kmap(page);
  379. err = memcpy_toiovecend(to, vaddr + frag->page_offset +
  380. offset - start, to_offset, copy);
  381. kunmap(page);
  382. if (err)
  383. goto fault;
  384. if (!(len -= copy))
  385. return 0;
  386. offset += copy;
  387. to_offset += copy;
  388. }
  389. start = end;
  390. }
  391. skb_walk_frags(skb, frag_iter) {
  392. int end;
  393. WARN_ON(start > offset + len);
  394. end = start + frag_iter->len;
  395. if ((copy = end - offset) > 0) {
  396. if (copy > len)
  397. copy = len;
  398. if (skb_copy_datagram_const_iovec(frag_iter,
  399. offset - start,
  400. to, to_offset,
  401. copy))
  402. goto fault;
  403. if ((len -= copy) == 0)
  404. return 0;
  405. offset += copy;
  406. to_offset += copy;
  407. }
  408. start = end;
  409. }
  410. if (!len)
  411. return 0;
  412. fault:
  413. return -EFAULT;
  414. }
  415. EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
  416. /**
  417. * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
  418. * @skb: buffer to copy
  419. * @offset: offset in the buffer to start copying to
  420. * @from: io vector to copy to
  421. * @from_offset: offset in the io vector to start copying from
  422. * @len: amount of data to copy to buffer from iovec
  423. *
  424. * Returns 0 or -EFAULT.
  425. * Note: the iovec is not modified during the copy.
  426. */
  427. int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
  428. const struct iovec *from, int from_offset,
  429. int len)
  430. {
  431. int start = skb_headlen(skb);
  432. int i, copy = start - offset;
  433. struct sk_buff *frag_iter;
  434. /* Copy header. */
  435. if (copy > 0) {
  436. if (copy > len)
  437. copy = len;
  438. if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
  439. copy))
  440. goto fault;
  441. if ((len -= copy) == 0)
  442. return 0;
  443. offset += copy;
  444. from_offset += copy;
  445. }
  446. /* Copy paged appendix. Hmm... why does this look so complicated? */
  447. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  448. int end;
  449. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  450. WARN_ON(start > offset + len);
  451. end = start + skb_frag_size(frag);
  452. if ((copy = end - offset) > 0) {
  453. int err;
  454. u8 *vaddr;
  455. struct page *page = skb_frag_page(frag);
  456. if (copy > len)
  457. copy = len;
  458. vaddr = kmap(page);
  459. err = memcpy_fromiovecend(vaddr + frag->page_offset +
  460. offset - start,
  461. from, from_offset, copy);
  462. kunmap(page);
  463. if (err)
  464. goto fault;
  465. if (!(len -= copy))
  466. return 0;
  467. offset += copy;
  468. from_offset += copy;
  469. }
  470. start = end;
  471. }
  472. skb_walk_frags(skb, frag_iter) {
  473. int end;
  474. WARN_ON(start > offset + len);
  475. end = start + frag_iter->len;
  476. if ((copy = end - offset) > 0) {
  477. if (copy > len)
  478. copy = len;
  479. if (skb_copy_datagram_from_iovec(frag_iter,
  480. offset - start,
  481. from,
  482. from_offset,
  483. copy))
  484. goto fault;
  485. if ((len -= copy) == 0)
  486. return 0;
  487. offset += copy;
  488. from_offset += copy;
  489. }
  490. start = end;
  491. }
  492. if (!len)
  493. return 0;
  494. fault:
  495. return -EFAULT;
  496. }
  497. EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
  498. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  499. u8 __user *to, int len,
  500. __wsum *csump)
  501. {
  502. int start = skb_headlen(skb);
  503. int i, copy = start - offset;
  504. struct sk_buff *frag_iter;
  505. int pos = 0;
  506. /* Copy header. */
  507. if (copy > 0) {
  508. int err = 0;
  509. if (copy > len)
  510. copy = len;
  511. *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
  512. *csump, &err);
  513. if (err)
  514. goto fault;
  515. if ((len -= copy) == 0)
  516. return 0;
  517. offset += copy;
  518. to += copy;
  519. pos = copy;
  520. }
  521. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  522. int end;
  523. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  524. WARN_ON(start > offset + len);
  525. end = start + skb_frag_size(frag);
  526. if ((copy = end - offset) > 0) {
  527. __wsum csum2;
  528. int err = 0;
  529. u8 *vaddr;
  530. struct page *page = skb_frag_page(frag);
  531. if (copy > len)
  532. copy = len;
  533. vaddr = kmap(page);
  534. csum2 = csum_and_copy_to_user(vaddr +
  535. frag->page_offset +
  536. offset - start,
  537. to, copy, 0, &err);
  538. kunmap(page);
  539. if (err)
  540. goto fault;
  541. *csump = csum_block_add(*csump, csum2, pos);
  542. if (!(len -= copy))
  543. return 0;
  544. offset += copy;
  545. to += copy;
  546. pos += copy;
  547. }
  548. start = end;
  549. }
  550. skb_walk_frags(skb, frag_iter) {
  551. int end;
  552. WARN_ON(start > offset + len);
  553. end = start + frag_iter->len;
  554. if ((copy = end - offset) > 0) {
  555. __wsum csum2 = 0;
  556. if (copy > len)
  557. copy = len;
  558. if (skb_copy_and_csum_datagram(frag_iter,
  559. offset - start,
  560. to, copy,
  561. &csum2))
  562. goto fault;
  563. *csump = csum_block_add(*csump, csum2, pos);
  564. if ((len -= copy) == 0)
  565. return 0;
  566. offset += copy;
  567. to += copy;
  568. pos += copy;
  569. }
  570. start = end;
  571. }
  572. if (!len)
  573. return 0;
  574. fault:
  575. return -EFAULT;
  576. }
  577. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  578. {
  579. __sum16 sum;
  580. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  581. if (likely(!sum)) {
  582. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  583. netdev_rx_csum_fault(skb->dev);
  584. skb->ip_summed = CHECKSUM_UNNECESSARY;
  585. }
  586. return sum;
  587. }
  588. EXPORT_SYMBOL(__skb_checksum_complete_head);
  589. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  590. {
  591. return __skb_checksum_complete_head(skb, skb->len);
  592. }
  593. EXPORT_SYMBOL(__skb_checksum_complete);
  594. /**
  595. * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
  596. * @skb: skbuff
  597. * @hlen: hardware length
  598. * @iov: io vector
  599. *
  600. * Caller _must_ check that skb will fit to this iovec.
  601. *
  602. * Returns: 0 - success.
  603. * -EINVAL - checksum failure.
  604. * -EFAULT - fault during copy. Beware, in this case iovec
  605. * can be modified!
  606. */
  607. int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  608. int hlen, struct iovec *iov)
  609. {
  610. __wsum csum;
  611. int chunk = skb->len - hlen;
  612. if (!chunk)
  613. return 0;
  614. /* Skip filled elements.
  615. * Pretty silly, look at memcpy_toiovec, though 8)
  616. */
  617. while (!iov->iov_len)
  618. iov++;
  619. if (iov->iov_len < chunk) {
  620. if (__skb_checksum_complete(skb))
  621. goto csum_error;
  622. if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
  623. goto fault;
  624. } else {
  625. csum = csum_partial(skb->data, hlen, skb->csum);
  626. if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
  627. chunk, &csum))
  628. goto fault;
  629. if (csum_fold(csum))
  630. goto csum_error;
  631. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  632. netdev_rx_csum_fault(skb->dev);
  633. iov->iov_len -= chunk;
  634. iov->iov_base += chunk;
  635. }
  636. return 0;
  637. csum_error:
  638. return -EINVAL;
  639. fault:
  640. return -EFAULT;
  641. }
  642. EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
  643. /**
  644. * datagram_poll - generic datagram poll
  645. * @file: file struct
  646. * @sock: socket
  647. * @wait: poll table
  648. *
  649. * Datagram poll: Again totally generic. This also handles
  650. * sequenced packet sockets providing the socket receive queue
  651. * is only ever holding data ready to receive.
  652. *
  653. * Note: when you _don't_ use this routine for this protocol,
  654. * and you use a different write policy from sock_writeable()
  655. * then please supply your own write_space callback.
  656. */
  657. unsigned int datagram_poll(struct file *file, struct socket *sock,
  658. poll_table *wait)
  659. {
  660. struct sock *sk = sock->sk;
  661. unsigned int mask;
  662. sock_poll_wait(file, sk_sleep(sk), wait);
  663. mask = 0;
  664. /* exceptional events? */
  665. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  666. mask |= POLLERR;
  667. if (sk->sk_shutdown & RCV_SHUTDOWN)
  668. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  669. if (sk->sk_shutdown == SHUTDOWN_MASK)
  670. mask |= POLLHUP;
  671. /* readable? */
  672. if (!skb_queue_empty(&sk->sk_receive_queue))
  673. mask |= POLLIN | POLLRDNORM;
  674. /* Connection-based need to check for termination and startup */
  675. if (connection_based(sk)) {
  676. if (sk->sk_state == TCP_CLOSE)
  677. mask |= POLLHUP;
  678. /* connection hasn't started yet? */
  679. if (sk->sk_state == TCP_SYN_SENT)
  680. return mask;
  681. }
  682. /* writable? */
  683. if (sock_writeable(sk))
  684. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  685. else
  686. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  687. return mask;
  688. }
  689. EXPORT_SYMBOL(datagram_poll);