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