blob: 2d733131d7cedc117dc98a2323d954c524868ad3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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@redhat.com>. (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 <asm/system.h>
41#include <linux/mm.h>
42#include <linux/interrupt.h>
43#include <linux/errno.h>
44#include <linux/sched.h>
45#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/netdevice.h>
47#include <linux/rtnetlink.h>
48#include <linux/poll.h>
49#include <linux/highmem.h>
Herbert Xu3305b802005-12-13 23:16:37 -080050#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070055#include <net/checksum.h>
56#include <net/sock.h>
57#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * Is a socket 'connection oriented' ?
61 */
62static inline int connection_based(struct sock *sk)
63{
64 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
65}
66
67/*
68 * Wait for a packet..
69 */
70static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
71{
72 int error;
73 DEFINE_WAIT(wait);
74
75 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
76
77 /* Socket errors? */
78 error = sock_error(sk);
79 if (error)
80 goto out_err;
81
82 if (!skb_queue_empty(&sk->sk_receive_queue))
83 goto out;
84
85 /* Socket shut down? */
86 if (sk->sk_shutdown & RCV_SHUTDOWN)
87 goto out_noerr;
88
89 /* Sequenced packets can come disconnected.
90 * If so we report the problem
91 */
92 error = -ENOTCONN;
93 if (connection_based(sk) &&
94 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
95 goto out_err;
96
97 /* handle signals */
98 if (signal_pending(current))
99 goto interrupted;
100
101 error = 0;
102 *timeo_p = schedule_timeout(*timeo_p);
103out:
104 finish_wait(sk->sk_sleep, &wait);
105 return error;
106interrupted:
107 error = sock_intr_errno(*timeo_p);
108out_err:
109 *err = error;
110 goto out;
111out_noerr:
112 *err = 0;
113 error = 1;
114 goto out;
115}
116
117/**
Herbert Xua59322b2007-12-05 01:53:40 -0800118 * __skb_recv_datagram - Receive a datagram skbuff
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700119 * @sk: socket
120 * @flags: MSG_ flags
Herbert Xua59322b2007-12-05 01:53:40 -0800121 * @peeked: returns non-zero if this packet has been seen before
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700122 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
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 */
Herbert Xua59322b2007-12-05 01:53:40 -0800146struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
147 int *peeked, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
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
156 if (error)
157 goto no_packet;
158
Herbert Xua59322b2007-12-05 01:53:40 -0800159 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 do {
162 /* Again only user level code calls this function, so nothing
163 * interrupt level will suddenly eat the receive_queue.
164 *
165 * Look at current nfs client by the way...
166 * However, this function was corrent in any case. 8)
167 */
Herbert Xua59322b2007-12-05 01:53:40 -0800168 unsigned long cpu_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Herbert Xua59322b2007-12-05 01:53:40 -0800170 spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
171 skb = skb_peek(&sk->sk_receive_queue);
172 if (skb) {
173 *peeked = skb->peeked;
174 if (flags & MSG_PEEK) {
175 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800177 } else
178 __skb_unlink(skb, &sk->sk_receive_queue);
179 }
180 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (skb)
183 return skb;
184
185 /* User doesn't want to wait */
186 error = -EAGAIN;
187 if (!timeo)
188 goto no_packet;
189
190 } while (!wait_for_packet(sk, err, &timeo));
191
192 return NULL;
193
194no_packet:
195 *err = error;
196 return NULL;
197}
Herbert Xua59322b2007-12-05 01:53:40 -0800198EXPORT_SYMBOL(__skb_recv_datagram);
199
200struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
201 int noblock, int *err)
202{
203 int peeked;
204
205 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
206 &peeked, err);
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
210{
211 kfree_skb(skb);
212}
213
214/**
Herbert Xu3305b802005-12-13 23:16:37 -0800215 * skb_kill_datagram - Free a datagram skbuff forcibly
216 * @sk: socket
217 * @skb: datagram skbuff
218 * @flags: MSG_ flags
219 *
220 * This function frees a datagram skbuff that was received by
221 * skb_recv_datagram. The flags argument must match the one
222 * used for skb_recv_datagram.
223 *
224 * If the MSG_PEEK flag is set, and the packet is still on the
225 * receive queue of the socket, it will be taken off the queue
226 * before it is freed.
227 *
228 * This function currently only disables BH when acquiring the
229 * sk_receive_queue lock. Therefore it must not be used in a
230 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800231 *
232 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800233 */
234
Herbert Xu27ab2562007-12-05 01:51:58 -0800235int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800236{
Herbert Xu27ab2562007-12-05 01:51:58 -0800237 int err = 0;
238
Herbert Xu3305b802005-12-13 23:16:37 -0800239 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800240 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800241 spin_lock_bh(&sk->sk_receive_queue.lock);
242 if (skb == skb_peek(&sk->sk_receive_queue)) {
243 __skb_unlink(skb, &sk->sk_receive_queue);
244 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800245 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800246 }
247 spin_unlock_bh(&sk->sk_receive_queue.lock);
248 }
249
250 kfree_skb(skb);
Herbert Xu27ab2562007-12-05 01:51:58 -0800251 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800252}
253
254EXPORT_SYMBOL(skb_kill_datagram);
255
256/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700258 * @skb: buffer to copy
259 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700260 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700261 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * Note: the iovec is modified during the copy.
264 */
265int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
266 struct iovec *to, int len)
267{
David S. Miller1a028e52007-04-27 15:21:23 -0700268 int start = skb_headlen(skb);
269 int i, copy = start - offset;
Herbert Xuc75d7212005-11-02 18:55:00 +1100270
David S. Millerb4d9eda2006-02-13 16:06:10 -0800271 /* Copy header. */
272 if (copy > 0) {
273 if (copy > len)
274 copy = len;
275 if (memcpy_toiovec(to, skb->data + offset, copy))
276 goto fault;
277 if ((len -= copy) == 0)
278 return 0;
279 offset += copy;
280 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100281
David S. Millerb4d9eda2006-02-13 16:06:10 -0800282 /* Copy paged appendix. Hmm... why does this look so complicated? */
283 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700284 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
David S. Miller1a028e52007-04-27 15:21:23 -0700286 BUG_TRAP(start <= offset + len);
287
288 end = start + skb_shinfo(skb)->frags[i].size;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800289 if ((copy = end - offset) > 0) {
290 int err;
291 u8 *vaddr;
292 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
293 struct page *page = frag->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (copy > len)
296 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800297 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700298 err = memcpy_toiovec(to, vaddr + frag->page_offset +
299 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800300 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (err)
302 goto fault;
303 if (!(len -= copy))
304 return 0;
305 offset += copy;
306 }
David S. Miller1a028e52007-04-27 15:21:23 -0700307 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800309
310 if (skb_shinfo(skb)->frag_list) {
311 struct sk_buff *list = skb_shinfo(skb)->frag_list;
312
313 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -0700314 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800315
David S. Miller1a028e52007-04-27 15:21:23 -0700316 BUG_TRAP(start <= offset + len);
317
318 end = start + list->len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800319 if ((copy = end - offset) > 0) {
320 if (copy > len)
321 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -0700322 if (skb_copy_datagram_iovec(list,
323 offset - start,
324 to, copy))
David S. Millerb4d9eda2006-02-13 16:06:10 -0800325 goto fault;
326 if ((len -= copy) == 0)
327 return 0;
328 offset += copy;
329 }
David S. Miller1a028e52007-04-27 15:21:23 -0700330 start = end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800333 if (!len)
334 return 0;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336fault:
337 return -EFAULT;
338}
339
340static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
341 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800342 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
David S. Miller1a028e52007-04-27 15:21:23 -0700344 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int pos = 0;
David S. Miller1a028e52007-04-27 15:21:23 -0700346 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Copy header. */
349 if (copy > 0) {
350 int err = 0;
351 if (copy > len)
352 copy = len;
353 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
354 *csump, &err);
355 if (err)
356 goto fault;
357 if ((len -= copy) == 0)
358 return 0;
359 offset += copy;
360 to += copy;
361 pos = copy;
362 }
363
364 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700365 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David S. Miller1a028e52007-04-27 15:21:23 -0700367 BUG_TRAP(start <= offset + len);
368
369 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800371 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int err = 0;
373 u8 *vaddr;
374 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
375 struct page *page = frag->page;
376
377 if (copy > len)
378 copy = len;
379 vaddr = kmap(page);
380 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700381 frag->page_offset +
382 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 to, copy, 0, &err);
384 kunmap(page);
385 if (err)
386 goto fault;
387 *csump = csum_block_add(*csump, csum2, pos);
388 if (!(len -= copy))
389 return 0;
390 offset += copy;
391 to += copy;
392 pos += copy;
393 }
David S. Miller1a028e52007-04-27 15:21:23 -0700394 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 if (skb_shinfo(skb)->frag_list) {
398 struct sk_buff *list = skb_shinfo(skb)->frag_list;
399
400 for (; list; list=list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -0700401 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David S. Miller1a028e52007-04-27 15:21:23 -0700403 BUG_TRAP(start <= offset + len);
404
405 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800407 __wsum csum2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (copy > len)
409 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -0700410 if (skb_copy_and_csum_datagram(list,
411 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 to, copy,
413 &csum2))
414 goto fault;
415 *csump = csum_block_add(*csump, csum2, pos);
416 if ((len -= copy) == 0)
417 return 0;
418 offset += copy;
419 to += copy;
420 pos += copy;
421 }
David S. Miller1a028e52007-04-27 15:21:23 -0700422 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
425 if (!len)
426 return 0;
427
428fault:
429 return -EFAULT;
430}
431
Herbert Xu759e5d02007-03-25 20:10:56 -0700432__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800433{
Al Virod3bc23e2006-11-14 21:24:49 -0800434 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800435
Herbert Xu759e5d02007-03-25 20:10:56 -0700436 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Herbert Xufb286bb2005-11-10 13:01:24 -0800437 if (likely(!sum)) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700438 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800439 netdev_rx_csum_fault(skb->dev);
440 skb->ip_summed = CHECKSUM_UNNECESSARY;
441 }
442 return sum;
443}
Herbert Xu759e5d02007-03-25 20:10:56 -0700444EXPORT_SYMBOL(__skb_checksum_complete_head);
445
446__sum16 __skb_checksum_complete(struct sk_buff *skb)
447{
448 return __skb_checksum_complete_head(skb, skb->len);
449}
Herbert Xufb286bb2005-11-10 13:01:24 -0800450EXPORT_SYMBOL(__skb_checksum_complete);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
453 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700454 * @skb: skbuff
455 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700456 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * Caller _must_ check that skb will fit to this iovec.
459 *
460 * Returns: 0 - success.
461 * -EINVAL - checksum failure.
462 * -EFAULT - fault during copy. Beware, in this case iovec
463 * can be modified!
464 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800465int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int hlen, struct iovec *iov)
467{
Al Virod3bc23e2006-11-14 21:24:49 -0800468 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int chunk = skb->len - hlen;
470
Herbert Xuef8aef52007-09-06 14:06:35 +0100471 if (!chunk)
472 return 0;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* Skip filled elements.
475 * Pretty silly, look at memcpy_toiovec, though 8)
476 */
477 while (!iov->iov_len)
478 iov++;
479
480 if (iov->iov_len < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800481 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto csum_error;
483 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
484 goto fault;
485 } else {
486 csum = csum_partial(skb->data, hlen, skb->csum);
487 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
488 chunk, &csum))
489 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800490 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700492 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800493 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 iov->iov_len -= chunk;
495 iov->iov_base += chunk;
496 }
497 return 0;
498csum_error:
499 return -EINVAL;
500fault:
501 return -EFAULT;
502}
503
504/**
505 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700506 * @file: file struct
507 * @sock: socket
508 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
510 * Datagram poll: Again totally generic. This also handles
511 * sequenced packet sockets providing the socket receive queue
512 * is only ever holding data ready to receive.
513 *
514 * Note: when you _don't_ use this routine for this protocol,
515 * and you use a different write policy from sock_writeable()
516 * then please supply your own write_space callback.
517 */
518unsigned int datagram_poll(struct file *file, struct socket *sock,
519 poll_table *wait)
520{
521 struct sock *sk = sock->sk;
522 unsigned int mask;
523
524 poll_wait(file, sk->sk_sleep, wait);
525 mask = 0;
526
527 /* exceptional events? */
528 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
529 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -0800530 if (sk->sk_shutdown & RCV_SHUTDOWN)
531 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (sk->sk_shutdown == SHUTDOWN_MASK)
533 mask |= POLLHUP;
534
535 /* readable? */
536 if (!skb_queue_empty(&sk->sk_receive_queue) ||
537 (sk->sk_shutdown & RCV_SHUTDOWN))
538 mask |= POLLIN | POLLRDNORM;
539
540 /* Connection-based need to check for termination and startup */
541 if (connection_based(sk)) {
542 if (sk->sk_state == TCP_CLOSE)
543 mask |= POLLHUP;
544 /* connection hasn't started yet? */
545 if (sk->sk_state == TCP_SYN_SENT)
546 return mask;
547 }
548
549 /* writable? */
550 if (sock_writeable(sk))
551 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
552 else
553 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
554
555 return mask;
556}
557
558EXPORT_SYMBOL(datagram_poll);
559EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
560EXPORT_SYMBOL(skb_copy_datagram_iovec);
561EXPORT_SYMBOL(skb_free_datagram);
562EXPORT_SYMBOL(skb_recv_datagram);